FinalizationRegistryConstructor.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. FinalizationRegistryConstructor::FinalizationRegistryConstructor(Realm& realm)
  14. : NativeFunction(vm().names.FinalizationRegistry.as_string(), *realm.global_object().function_prototype())
  15. {
  16. }
  17. void FinalizationRegistryConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(realm);
  21. // 26.2.2.1 FinalizationRegistry.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
  22. define_direct_property(vm.names.prototype, realm.global_object().finalization_registry_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  24. }
  25. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  26. ThrowCompletionOr<Value> FinalizationRegistryConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.FinalizationRegistry);
  30. }
  31. // 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
  32. ThrowCompletionOr<Object*> FinalizationRegistryConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. auto& global_object = this->global_object();
  36. // NOTE: Step 1 is implemented in FinalizationRegistryConstructor::call()
  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>(global_object, 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>(global_object, new_target, &GlobalObject::finalization_registry_prototype, *realm(), vm.host_make_job_callback(cleanup_callback.as_function())));
  50. }
  51. }