From 7c9f1dcced0c93b5e0a4363fe466f22dc9c8cf3e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 12 Apr 2023 23:15:14 +0200 Subject: [PATCH] LibJS: Add spec comments to WeakRefConstructor --- Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp b/Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp index 6f7854491b..3408b80dee 100644 --- a/Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp @@ -34,6 +34,8 @@ ThrowCompletionOr WeakRefConstructor::initialize(Realm& realm) ThrowCompletionOr WeakRefConstructor::call() { auto& vm = this->vm(); + + // 1. If NewTarget is undefined, throw a TypeError exception. return vm.throw_completion(ErrorType::ConstructorWithoutNew, vm.names.WeakRef); } @@ -41,11 +43,16 @@ ThrowCompletionOr WeakRefConstructor::call() ThrowCompletionOr> WeakRefConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); - auto target = vm.argument(0); + + // 2. If CanBeHeldWeakly(target) is false, throw a TypeError exception. if (!can_be_held_weakly(target)) return vm.throw_completion(ErrorType::CannotBeHeldWeakly, TRY_OR_THROW_OOM(vm, target.to_string_without_side_effects())); + // 3. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »). + // 4. Perform AddToKeptObjects(target). + // 5. Set weakRef.[[WeakRefTarget]] to target. + // 6. Return weakRef. if (target.is_object()) return TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::weak_ref_prototype, target.as_object())); VERIFY(target.is_symbol());