FinalizationRegistryConstructor.cpp 2.8 KB

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