From bccffed7e9bb7265dd1d92c7eadfa2987b6f12e9 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 12 Apr 2023 23:15:53 +0200 Subject: [PATCH] LibJS: Add spec comments to WeakSetConstructor --- .../LibJS/Runtime/WeakSetConstructor.cpp | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/WeakSetConstructor.cpp b/Userland/Libraries/LibJS/Runtime/WeakSetConstructor.cpp index 48040f7f55..d4901a6f0d 100644 --- a/Userland/Libraries/LibJS/Runtime/WeakSetConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/WeakSetConstructor.cpp @@ -35,6 +35,8 @@ ThrowCompletionOr WeakSetConstructor::initialize(Realm& realm) ThrowCompletionOr WeakSetConstructor::call() { auto& vm = this->vm(); + + // 1. If NewTarget is undefined, throw a TypeError exception. return vm.throw_completion(ErrorType::ConstructorWithoutNew, vm.names.WeakSet); } @@ -42,22 +44,36 @@ ThrowCompletionOr WeakSetConstructor::call() ThrowCompletionOr> WeakSetConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); + auto iterable = vm.argument(0); - auto weak_set = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::weak_set_prototype)); + // 2. Let set be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakSet.prototype%", « [[WeakSetData]] »). + // 3. Set set.[[WeakSetData]] to a new empty List. + auto set = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::weak_set_prototype)); - if (vm.argument(0).is_nullish()) - return weak_set; + // 4. If iterable is either undefined or null, return set. + if (iterable.is_nullish()) + return set; - auto adder = TRY(weak_set->get(vm.names.add)); + // 5. Let adder be ? Get(set, "add"). + auto adder = TRY(set->get(vm.names.add)); + + // 6. If IsCallable(adder) is false, throw a TypeError exception. if (!adder.is_function()) return vm.throw_completion(ErrorType::NotAFunction, "'add' property of WeakSet"); - (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional { - TRY(JS::call(vm, adder.as_function(), weak_set, iterator_value)); + // 7. Let iteratorRecord be ? GetIterator(iterable, sync). + // 8. Repeat, + // a. Let next be ? IteratorStep(iteratorRecord). + // c. Let nextValue be ? IteratorValue(next). + (void)TRY(get_iterator_values(vm, iterable, [&](Value next_value) -> Optional { + // d. Let status be Completion(Call(adder, set, « nextValue »)). + // e. IfAbruptCloseIterator(status, iteratorRecord). + TRY(JS::call(vm, adder.as_function(), set, next_value)); return {}; })); - return weak_set; + // b. If next is false, return set. + return set; } }