diff options
author | Linus Groh <mail@linusgroh.de> | 2023-04-12 23:15:53 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-13 13:04:44 +0200 |
commit | bccffed7e9bb7265dd1d92c7eadfa2987b6f12e9 (patch) | |
tree | 8f79a3e63913d7f32064ac5f17f40758f958e39b | |
parent | 77fc05afd6b48c562acc668e905f459fa867b88f (diff) | |
download | serenity-bccffed7e9bb7265dd1d92c7eadfa2987b6f12e9.zip |
LibJS: Add spec comments to WeakSetConstructor
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/WeakSetConstructor.cpp | 30 |
1 files 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<void> WeakSetConstructor::initialize(Realm& realm) ThrowCompletionOr<Value> WeakSetConstructor::call() { auto& vm = this->vm(); + + // 1. If NewTarget is undefined, throw a TypeError exception. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakSet); } @@ -42,22 +44,36 @@ ThrowCompletionOr<Value> WeakSetConstructor::call() ThrowCompletionOr<NonnullGCPtr<Object>> WeakSetConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); + auto iterable = vm.argument(0); + + // 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<WeakSet>(vm, new_target, &Intrinsics::weak_set_prototype)); - auto weak_set = TRY(ordinary_create_from_constructor<WeakSet>(vm, new_target, &Intrinsics::weak_set_prototype)); + // 4. If iterable is either undefined or null, return set. + if (iterable.is_nullish()) + return set; - if (vm.argument(0).is_nullish()) - return weak_set; + // 5. Let adder be ? Get(set, "add"). + auto adder = TRY(set->get(vm.names.add)); - auto adder = TRY(weak_set->get(vm.names.add)); + // 6. If IsCallable(adder) is false, throw a TypeError exception. if (!adder.is_function()) return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'add' property of WeakSet"); - (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> { - 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<Completion> { + // 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; } } |