2021-06-15 22:16:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-06-20 01:09:39 +01:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2021-06-15 22:16:17 +03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/FinalizationRegistry.h>
|
|
|
|
#include <LibJS/Runtime/FinalizationRegistryConstructor.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2022-02-06 03:46:45 +00:00
|
|
|
#include <LibJS/Runtime/JobCallback.h>
|
2021-06-15 22:16:17 +03:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
FinalizationRegistryConstructor::FinalizationRegistryConstructor(Realm& realm)
|
2022-09-14 19:10:27 -04:00
|
|
|
: NativeFunction(realm.vm().names.FinalizationRegistry.as_string(), *realm.intrinsics().function_prototype())
|
2021-06-15 22:16:17 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
ThrowCompletionOr<void> FinalizationRegistryConstructor::initialize(Realm& realm)
|
2021-06-15 22:16:17 +03:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
|
2021-06-15 22:16:17 +03:00
|
|
|
|
|
|
|
// 26.2.2.1 FinalizationRegistry.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().finalization_registry_prototype(), 0);
|
2021-06-15 22:16:17 +03:00
|
|
|
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2021-06-15 22:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> FinalizationRegistryConstructor::call()
|
2021-06-15 22:16:17 +03:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2023-01-27 21:52:18 +00:00
|
|
|
|
|
|
|
// 1. If NewTarget is undefined, throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.FinalizationRegistry);
|
2021-06-15 22:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
|
2022-12-14 19:18:10 +00:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> FinalizationRegistryConstructor::construct(FunctionObject& new_target)
|
2021-06-15 22:16:17 +03:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2022-02-06 03:46:45 +00:00
|
|
|
// 2. If IsCallable(cleanupCallback) is false, throw a TypeError exception.
|
2021-06-15 22:16:17 +03:00
|
|
|
auto cleanup_callback = vm.argument(0);
|
2021-10-20 21:16:30 +01:00
|
|
|
if (!cleanup_callback.is_function())
|
2023-02-12 21:26:14 -05:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, cleanup_callback.to_string_without_side_effects()));
|
2021-10-20 21:16:30 +01:00
|
|
|
|
2022-02-06 03:46:45 +00:00
|
|
|
// 3. Let finalizationRegistry be ? OrdinaryCreateFromConstructor(NewTarget, "%FinalizationRegistry.prototype%", « [[Realm]], [[CleanupCallback]], [[Cells]] »).
|
2022-04-11 21:32:37 +01:00
|
|
|
// 4. Let fn be the active function object.
|
|
|
|
// NOTE: This is not necessary, the active function object is `this`.
|
2022-02-06 03:46:45 +00:00
|
|
|
// 5. Set finalizationRegistry.[[Realm]] to fn.[[Realm]].
|
|
|
|
// 6. Set finalizationRegistry.[[CleanupCallback]] to HostMakeJobCallback(cleanupCallback).
|
2022-04-11 21:32:37 +01:00
|
|
|
// 7. Set finalizationRegistry.[[Cells]] to a new empty List.
|
|
|
|
// NOTE: This is done inside FinalizationRegistry instead of here.
|
2022-02-06 03:46:45 +00:00
|
|
|
// 8. Return finalizationRegistry.
|
2022-12-14 19:18:10 +00:00
|
|
|
return TRY(ordinary_create_from_constructor<FinalizationRegistry>(vm, new_target, &Intrinsics::finalization_registry_prototype, *realm(), vm.host_make_job_callback(cleanup_callback.as_function())));
|
2021-06-15 22:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|