BooleanConstructor.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(GlobalObject& global_object)
  12. : NativeFunction(vm().names.Boolean.as_string(), *global_object.function_prototype())
  13. {
  14. }
  15. void BooleanConstructor::initialize(GlobalObject& global_object)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(global_object);
  19. // 20.3.2.1 Boolean.prototype, https://tc39.es/ecma262/#sec-boolean.prototype
  20. define_direct_property(vm.names.prototype, global_object.boolean_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. BooleanConstructor::~BooleanConstructor()
  24. {
  25. }
  26. // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
  27. ThrowCompletionOr<Value> BooleanConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. auto b = vm.argument(0).to_boolean();
  31. return Value(b);
  32. }
  33. // 20.3.1.1 Boolean ( value ), https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
  34. ThrowCompletionOr<Object*> BooleanConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& global_object = this->global_object();
  38. auto b = vm.argument(0).to_boolean();
  39. return TRY(ordinary_create_from_constructor<BooleanObject>(global_object, new_target, &GlobalObject::boolean_prototype, b));
  40. }
  41. }