Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4booleanobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7using namespace QV4;
8
11
12void Heap::BooleanCtor::init(QV4::ExecutionEngine *engine)
13{
14 Heap::FunctionObject::init(engine, QStringLiteral("Boolean"));
15}
16
17ReturnedValue BooleanCtor::virtualCallAsConstructor(const FunctionObject *that, const Value *argv, int argc, const Value *newTarget)
18{
19 auto v4 = that->engine();
20 bool n = argc ? argv[0].toBoolean() : false;
21
22 ReturnedValue o = Encode(v4->newBooleanObject(n));
23 if (!newTarget)
24 return o;
25 Scope scope(v4);
26 ScopedObject obj(scope, o);
27 obj->setProtoFromNewTarget(newTarget);
28 return obj->asReturnedValue();
29}
30
31ReturnedValue BooleanCtor::virtualCall(const FunctionObject *, const Value *, const Value *argv, int argc)
32{
33 bool value = argc ? argv[0].toBoolean() : 0;
34 return Encode(value);
35}
36
37void BooleanPrototype::init(ExecutionEngine *engine, Object *ctor)
38{
39 Scope scope(engine);
40 ScopedObject o(scope);
41 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
42 ctor->defineReadonlyProperty(engine->id_prototype(), (o = this));
43 defineDefaultProperty(QStringLiteral("constructor"), (o = ctor));
44 defineDefaultProperty(engine->id_toString(), method_toString);
45 defineDefaultProperty(engine->id_valueOf(), method_valueOf);
46}
47
48static bool value(const Value *thisObject, bool *exception)
49{
50 *exception = false;
51 if (thisObject->isBoolean()) {
52 return thisObject->booleanValue();
53 } else {
54 const BooleanObject *that = thisObject->as<BooleanObject>();
55 if (that)
56 return that->value();
57 }
58 *exception = true;
59 return false;
60}
61
62ReturnedValue BooleanPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int)
63{
64 bool exception;
65 bool result = ::value(thisObject, &exception);
66 ExecutionEngine *v4 = b->engine();
67 if (exception)
68 return v4->throwTypeError();
69
70 return (result ? v4->id_true() : v4->id_false())->asReturnedValue();
71}
72
73ReturnedValue BooleanPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int)
74{
75 bool exception;
76 bool result = ::value(thisObject, &exception);
77 if (exception) {
78 ExecutionEngine *v4 = b->engine();
79 return v4->throwTypeError();
80 }
81
82 return Encode(result);
83}
static bool value(const Value *thisObject, bool *exception)
DEFINE_OBJECT_VTABLE(BooleanObject)
DEFINE_OBJECT_VTABLE(BooleanCtor)