diff options
author | Linus Groh <mail@linusgroh.de> | 2023-04-12 23:11:16 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-13 13:04:44 +0200 |
commit | 58566d2a3ffe4dd70d1a34e8f373142906edd290 (patch) | |
tree | c2649825cb989ab212f7ffa45fb26de47c8b0576 /Userland | |
parent | d0f80b40b2a662c9cdd697c1f4880cef83cb205d (diff) | |
download | serenity-58566d2a3ffe4dd70d1a34e8f373142906edd290.zip |
LibJS: Add spec comments to SetConstructor
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/SetConstructor.cpp | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/SetConstructor.cpp b/Userland/Libraries/LibJS/Runtime/SetConstructor.cpp index dd692d3e9c..dc5be635a2 100644 --- a/Userland/Libraries/LibJS/Runtime/SetConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/SetConstructor.cpp @@ -37,6 +37,8 @@ ThrowCompletionOr<void> SetConstructor::initialize(Realm& realm) ThrowCompletionOr<Value> SetConstructor::call() { auto& vm = this->vm(); + + // 1. If NewTarget is undefined, throw a TypeError exception. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Set); } @@ -44,27 +46,43 @@ ThrowCompletionOr<Value> SetConstructor::call() ThrowCompletionOr<NonnullGCPtr<Object>> SetConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); + auto iterable = vm.argument(0); + // 2. Let set be ? OrdinaryCreateFromConstructor(NewTarget, "%Set.prototype%", « [[SetData]] »). auto set = TRY(ordinary_create_from_constructor<Set>(vm, new_target, &Intrinsics::set_prototype)); - if (vm.argument(0).is_nullish()) + // 3. Set set.[[SetData]] to a new empty List. + + // 4. If iterable is either undefined or null, return set. + if (iterable.is_nullish()) return set; + // 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<TypeError>(ErrorType::NotAFunction, "'add' property of Set"); - (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> { - TRY(JS::call(vm, adder.as_function(), 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 {}; })); + // b. If next is false, return set. return set; } // 24.2.2.2 get Set [ @@species ], https://tc39.es/ecma262/#sec-get-set-@@species JS_DEFINE_NATIVE_FUNCTION(SetConstructor::symbol_species_getter) { + // 1. Return the this value. return vm.this_value(); } |