/src/serenity/Userland/Libraries/LibJS/Runtime/BooleanConstructor.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> |
3 | | * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <LibJS/Runtime/AbstractOperations.h> |
9 | | #include <LibJS/Runtime/BooleanConstructor.h> |
10 | | #include <LibJS/Runtime/BooleanObject.h> |
11 | | #include <LibJS/Runtime/GlobalObject.h> |
12 | | #include <LibJS/Runtime/ValueInlines.h> |
13 | | |
14 | | namespace JS { |
15 | | |
16 | | JS_DEFINE_ALLOCATOR(BooleanConstructor); |
17 | | |
18 | | BooleanConstructor::BooleanConstructor(Realm& realm) |
19 | 0 | : NativeFunction(realm.vm().names.Boolean.as_string(), realm.intrinsics().function_prototype()) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | | void BooleanConstructor::initialize(Realm& realm) |
24 | 0 | { |
25 | 0 | auto& vm = this->vm(); |
26 | 0 | Base::initialize(realm); |
27 | | |
28 | | // 20.3.2.1 Boolean.prototype, https://tc39.es/ecma262/#sec-boolean.prototype |
29 | 0 | define_direct_property(vm.names.prototype, realm.intrinsics().boolean_prototype(), 0); |
30 | |
|
31 | 0 | define_direct_property(vm.names.length, Value(1), Attribute::Configurable); |
32 | 0 | } |
33 | | |
34 | | // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value |
35 | | ThrowCompletionOr<Value> BooleanConstructor::call() |
36 | 0 | { |
37 | 0 | auto& vm = this->vm(); |
38 | 0 | auto value = vm.argument(0); |
39 | | |
40 | | // 1. Let b be ToBoolean(value). |
41 | 0 | auto b = value.to_boolean(); |
42 | | |
43 | | // 2. If NewTarget is undefined, return b. |
44 | 0 | return Value(b); |
45 | 0 | } |
46 | | |
47 | | // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value |
48 | | ThrowCompletionOr<NonnullGCPtr<Object>> BooleanConstructor::construct(FunctionObject& new_target) |
49 | 0 | { |
50 | 0 | auto& vm = this->vm(); |
51 | 0 | auto value = vm.argument(0); |
52 | | |
53 | | // 1. Let b be ToBoolean(value). |
54 | 0 | auto b = value.to_boolean(); |
55 | | |
56 | | // 3. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Boolean.prototype%", « [[BooleanData]] »). |
57 | | // 4. Set O.[[BooleanData]] to b. |
58 | | // 5. Return O. |
59 | 0 | return TRY(ordinary_create_from_constructor<BooleanObject>(vm, new_target, &Intrinsics::boolean_prototype, b)); |
60 | 0 | } |
61 | | |
62 | | } |