BooleanConstructor.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/BooleanConstructor.h>
  8. #include <LibJS/Runtime/BooleanObject.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. BooleanConstructor::BooleanConstructor(Realm& realm)
  12. : NativeFunction(vm().names.Boolean.as_string(), *realm.global_object().function_prototype())
  13. {
  14. }
  15. void BooleanConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(realm);
  19. // 20.3.2.1 Boolean.prototype, https://tc39.es/ecma262/#sec-boolean.prototype
  20. define_direct_property(vm.names.prototype, realm.global_object().boolean_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
  24. ThrowCompletionOr<Value> BooleanConstructor::call()
  25. {
  26. auto& vm = this->vm();
  27. auto b = vm.argument(0).to_boolean();
  28. return Value(b);
  29. }
  30. // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
  31. ThrowCompletionOr<Object*> BooleanConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& vm = this->vm();
  34. auto& global_object = this->global_object();
  35. auto b = vm.argument(0).to_boolean();
  36. return TRY(ordinary_create_from_constructor<BooleanObject>(global_object, new_target, &GlobalObject::boolean_prototype, b));
  37. }
  38. }