WeakRefConstructor.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/WeakRef.h>
  9. #include <LibJS/Runtime/WeakRefConstructor.h>
  10. namespace JS {
  11. WeakRefConstructor::WeakRefConstructor(GlobalObject& global_object)
  12. : NativeFunction(vm().names.WeakRef, *global_object.function_prototype())
  13. {
  14. }
  15. void WeakRefConstructor::initialize(GlobalObject& global_object)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(global_object);
  19. // 26.1.2.1 WeakRef.prototype, https://tc39.es/ecma262/#sec-weak-ref.prototype
  20. define_property(vm.names.prototype, global_object.weak_ref_prototype(), 0);
  21. define_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. WeakRefConstructor::~WeakRefConstructor()
  24. {
  25. }
  26. // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target
  27. Value WeakRefConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.WeakRef);
  31. return {};
  32. }
  33. // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target
  34. Value WeakRefConstructor::construct(Function&)
  35. {
  36. auto& vm = this->vm();
  37. auto target = vm.argument(0);
  38. if (!target.is_object()) {
  39. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, target.to_string_without_side_effects());
  40. return {};
  41. }
  42. // FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%WeakRef.prototype%")
  43. return WeakRef::create(global_object(), &target.as_object());
  44. }
  45. }