2020-04-06 22:51:16 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com>
|
2023-01-27 21:30:28 +00:00
|
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
2020-04-06 22:51:16 -05:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 22:51:16 -05:00
|
|
|
*/
|
|
|
|
|
2021-06-20 01:09:39 +01:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-04-06 22:51:16 -05:00
|
|
|
#include <LibJS/Runtime/BooleanConstructor.h>
|
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2023-10-06 17:54:21 +02:00
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2020-04-06 22:51:16 -05:00
|
|
|
|
|
|
|
namespace JS {
|
2020-04-07 16:00:44 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(BooleanConstructor);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
BooleanConstructor::BooleanConstructor(Realm& realm)
|
2023-04-13 00:47:15 +02:00
|
|
|
: NativeFunction(realm.vm().names.Boolean.as_string(), realm.intrinsics().function_prototype())
|
2020-04-06 22:51:16 -05:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void BooleanConstructor::initialize(Realm& realm)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
|
|
|
// 20.3.2.1 Boolean.prototype, https://tc39.es/ecma262/#sec-boolean.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().boolean_prototype(), 0);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-06 22:51:16 -05:00
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:41 +01:00
|
|
|
// 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> BooleanConstructor::call()
|
2020-04-06 22:51:16 -05:00
|
|
|
{
|
2021-06-20 01:09:39 +01:00
|
|
|
auto& vm = this->vm();
|
2023-01-27 21:30:28 +00:00
|
|
|
auto value = vm.argument(0);
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2023-01-27 21:30:28 +00:00
|
|
|
// 1. Let b be ToBoolean(value).
|
|
|
|
auto b = value.to_boolean();
|
|
|
|
|
|
|
|
// 2. If NewTarget is undefined, return b.
|
2021-06-20 01:09:39 +01:00
|
|
|
return Value(b);
|
2020-04-06 22:51:16 -05:00
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:41 +01:00
|
|
|
// 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
|
2024-11-15 04:01:23 +13:00
|
|
|
ThrowCompletionOr<GC::Ref<Object>> BooleanConstructor::construct(FunctionObject& new_target)
|
2020-04-06 22:51:16 -05:00
|
|
|
{
|
2021-06-20 01:09:39 +01:00
|
|
|
auto& vm = this->vm();
|
2023-01-27 21:30:28 +00:00
|
|
|
auto value = vm.argument(0);
|
|
|
|
|
|
|
|
// 1. Let b be ToBoolean(value).
|
|
|
|
auto b = value.to_boolean();
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2023-01-27 21:30:28 +00:00
|
|
|
// 3. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Boolean.prototype%", « [[BooleanData]] »).
|
|
|
|
// 4. Set O.[[BooleanData]] to b.
|
|
|
|
// 5. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
return TRY(ordinary_create_from_constructor<BooleanObject>(vm, new_target, &Intrinsics::boolean_prototype, b));
|
2020-04-06 22:51:16 -05:00
|
|
|
}
|
2020-04-07 16:00:44 +01:00
|
|
|
|
2020-04-06 22:51:16 -05:00
|
|
|
}
|