FinalizationRegistryConstructor.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <LibJS/Runtime/JobCallback.h>
  12. namespace JS {
  13. JS_DEFINE_ALLOCATOR(FinalizationRegistryConstructor);
  14. FinalizationRegistryConstructor::FinalizationRegistryConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.FinalizationRegistry.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void FinalizationRegistryConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. // 26.2.2.1 FinalizationRegistry.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().finalization_registry_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  25. }
  26. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  27. ThrowCompletionOr<Value> FinalizationRegistryConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. // 1. If NewTarget is undefined, throw a TypeError exception.
  31. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.FinalizationRegistry);
  32. }
  33. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  34. ThrowCompletionOr<NonnullGCPtr<Object>> FinalizationRegistryConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. // 2. If IsCallable(cleanupCallback) is false, throw a TypeError exception.
  38. auto cleanup_callback = vm.argument(0);
  39. if (!cleanup_callback.is_function())
  40. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, cleanup_callback.to_string_without_side_effects());
  41. // 3. Let finalizationRegistry be ? OrdinaryCreateFromConstructor(NewTarget, "%FinalizationRegistry.prototype%", « [[Realm]], [[CleanupCallback]], [[Cells]] »).
  42. // 4. Let fn be the active function object.
  43. // NOTE: This is not necessary, the active function object is `this`.
  44. // 5. Set finalizationRegistry.[[Realm]] to fn.[[Realm]].
  45. // 6. Set finalizationRegistry.[[CleanupCallback]] to HostMakeJobCallback(cleanupCallback).
  46. // 7. Set finalizationRegistry.[[Cells]] to a new empty List.
  47. // NOTE: This is done inside FinalizationRegistry instead of here.
  48. // 8. Return finalizationRegistry.
  49. return TRY(ordinary_create_from_constructor<FinalizationRegistry>(vm, new_target, &Intrinsics::finalization_registry_prototype, *realm(), vm.host_make_job_callback(cleanup_callback.as_function())));
  50. }
  51. }