FinalizationRegistryConstructor.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/FinalizationRegistry.h>
  9. #include <LibJS/Runtime/FinalizationRegistryConstructor.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS {
  12. FinalizationRegistryConstructor::FinalizationRegistryConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.FinalizationRegistry.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void FinalizationRegistryConstructor::initialize(GlobalObject& global_object)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(global_object);
  20. // 26.2.2.1 FinalizationRegistry.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
  21. define_direct_property(vm.names.prototype, global_object.finalization_registry_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. }
  24. FinalizationRegistryConstructor::~FinalizationRegistryConstructor()
  25. {
  26. }
  27. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  28. Value FinalizationRegistryConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.FinalizationRegistry);
  32. return {};
  33. }
  34. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  35. Value FinalizationRegistryConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto cleanup_callback = vm.argument(0);
  40. if (!cleanup_callback.is_function()) {
  41. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, cleanup_callback.to_string_without_side_effects());
  42. return {};
  43. }
  44. return TRY_OR_DISCARD(ordinary_create_from_constructor<FinalizationRegistry>(global_object, new_target, &GlobalObject::finalization_registry_prototype, cleanup_callback.as_function()));
  45. }
  46. }