| 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 | |
| 4 | #include "qv4booleanobject_p.h" |
| 5 | |
| 6 | using namespace QV4; |
| 7 | |
| 8 | DEFINE_OBJECT_VTABLE(BooleanCtor); |
| 9 | DEFINE_OBJECT_VTABLE(BooleanObject); |
| 10 | |
| 11 | void Heap::BooleanCtor::init(QV4::ExecutionEngine *engine) |
| 12 | { |
| 13 | Heap::FunctionObject::init(engine, QStringLiteral("Boolean" )); |
| 14 | } |
| 15 | |
| 16 | ReturnedValue BooleanCtor::virtualCallAsConstructor(const FunctionObject *that, const Value *argv, int argc, const Value *newTarget) |
| 17 | { |
| 18 | auto v4 = that->engine(); |
| 19 | bool n = argc ? argv[0].toBoolean() : false; |
| 20 | |
| 21 | ReturnedValue o = Encode(v4->newBooleanObject(b: n)); |
| 22 | if (!newTarget) |
| 23 | return o; |
| 24 | Scope scope(v4); |
| 25 | ScopedObject obj(scope, o); |
| 26 | obj->setProtoFromNewTarget(newTarget); |
| 27 | return obj->asReturnedValue(); |
| 28 | } |
| 29 | |
| 30 | ReturnedValue BooleanCtor::virtualCall(const FunctionObject *, const Value *, const Value *argv, int argc) |
| 31 | { |
| 32 | bool value = argc ? argv[0].toBoolean() : 0; |
| 33 | return Encode(value); |
| 34 | } |
| 35 | |
| 36 | void BooleanPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 37 | { |
| 38 | Scope scope(engine); |
| 39 | ScopedObject o(scope); |
| 40 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1)); |
| 41 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this)); |
| 42 | defineDefaultProperty(QStringLiteral("constructor" ), value: (o = ctor)); |
| 43 | defineDefaultProperty(name: engine->id_toString(), code: method_toString); |
| 44 | defineDefaultProperty(name: engine->id_valueOf(), code: method_valueOf); |
| 45 | } |
| 46 | |
| 47 | static bool value(const Value *thisObject, bool *exception) |
| 48 | { |
| 49 | *exception = false; |
| 50 | if (thisObject->isBoolean()) { |
| 51 | return thisObject->booleanValue(); |
| 52 | } else { |
| 53 | const BooleanObject *that = thisObject->as<BooleanObject>(); |
| 54 | if (that) |
| 55 | return that->value(); |
| 56 | } |
| 57 | *exception = true; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | ReturnedValue BooleanPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 62 | { |
| 63 | bool exception; |
| 64 | bool result = ::value(thisObject, exception: &exception); |
| 65 | ExecutionEngine *v4 = b->engine(); |
| 66 | if (exception) |
| 67 | return v4->throwTypeError(); |
| 68 | |
| 69 | return (result ? v4->id_true() : v4->id_false())->asReturnedValue(); |
| 70 | } |
| 71 | |
| 72 | ReturnedValue BooleanPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 73 | { |
| 74 | bool exception; |
| 75 | bool result = ::value(thisObject, exception: &exception); |
| 76 | if (exception) { |
| 77 | ExecutionEngine *v4 = b->engine(); |
| 78 | return v4->throwTypeError(); |
| 79 | } |
| 80 | |
| 81 | return Encode(result); |
| 82 | } |
| 83 | |