From 1f05b0638f038cf648256eb9fa33fa4e67a67752 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 25 Jun 2023 20:08:29 -0400 Subject: [PATCH] LibJS: Implement Iterator.prototype.every --- .../LibJS/Runtime/IteratorPrototype.cpp | 48 ++++++++ .../LibJS/Runtime/IteratorPrototype.h | 1 + .../Iterator/Iterator.prototype.every.js | 113 ++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Iterator/Iterator.prototype.every.js diff --git a/Userland/Libraries/LibJS/Runtime/IteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/IteratorPrototype.cpp index b6ee452594c..5f22054a53f 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorPrototype.cpp @@ -40,6 +40,7 @@ ThrowCompletionOr IteratorPrototype::initialize(Realm& realm) define_native_function(realm, vm.names.toArray, to_array, 0, attr); define_native_function(realm, vm.names.forEach, for_each, 1, attr); define_native_function(realm, vm.names.some, some, 1, attr); + define_native_function(realm, vm.names.every, every, 1, attr); return {}; } @@ -642,4 +643,51 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::some) } } +// 3.1.3.11 Iterator.prototype.every ( predicate ), https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.every +JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::every) +{ + auto predicate = vm.argument(0); + + // 1. Let O be the this value. + // 2. If O is not an Object, throw a TypeError exception. + auto object = TRY(this_object(vm)); + + // 3. If IsCallable(predicate) is false, throw a TypeError exception. + if (!predicate.is_function()) + return vm.throw_completion(ErrorType::NotAFunction, "predicate"sv); + + // 4. Let iterated be ? GetIteratorDirect(O). + auto iterated = TRY(get_iterator_direct(vm, object)); + + // 5. Let counter be 0. + size_t counter = 0; + + // 6. Repeat, + while (true) { + // a. Let next be ? IteratorStep(iterated). + auto next = TRY(iterator_step(vm, iterated)); + + // b. If next is false, return true. + if (!next) + return Value { true }; + + // c. Let value be ? IteratorValue(next). + auto value = TRY(iterator_value(vm, *next)); + + // d. Let result be Completion(Call(predicate, undefined, « value, 𝔽(counter) »)). + auto result = call(vm, predicate.as_function(), js_undefined(), value, Value { counter }); + + // e. IfAbruptCloseIterator(result, iterated). + if (result.is_error()) + return *TRY(iterator_close(vm, iterated, result.release_error())); + + // f. If ToBoolean(result) is false, return ? IteratorClose(iterated, NormalCompletion(false)). + if (!result.value().to_boolean()) + return *TRY(iterator_close(vm, iterated, normal_completion(Value { false }))); + + // g. Set counter to counter + 1. + ++counter; + } +} + } diff --git a/Userland/Libraries/LibJS/Runtime/IteratorPrototype.h b/Userland/Libraries/LibJS/Runtime/IteratorPrototype.h index fd6c89c221f..feb99ac2085 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/IteratorPrototype.h @@ -31,6 +31,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_array); JS_DECLARE_NATIVE_FUNCTION(for_each); JS_DECLARE_NATIVE_FUNCTION(some); + JS_DECLARE_NATIVE_FUNCTION(every); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Iterator/Iterator.prototype.every.js b/Userland/Libraries/LibJS/Tests/builtins/Iterator/Iterator.prototype.every.js new file mode 100644 index 00000000000..1e4fc7000b2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Iterator/Iterator.prototype.every.js @@ -0,0 +1,113 @@ +describe("errors", () => { + test("called with non-callable object", () => { + expect(() => { + Iterator.prototype.every(Symbol.hasInstance); + }).toThrowWithMessage(TypeError, "predicate is not a function"); + }); + + test("iterator's next method throws", () => { + function TestError() {} + + class TestIterator extends Iterator { + next() { + throw new TestError(); + } + } + + expect(() => { + new TestIterator().every(() => 0); + }).toThrow(TestError); + }); + + test("value returned by iterator's next method throws", () => { + function TestError() {} + + class TestIterator extends Iterator { + next() { + return { + done: false, + get value() { + throw new TestError(); + }, + }; + } + } + + expect(() => { + new TestIterator().every(() => 0); + }).toThrow(TestError); + }); + + test("predicate function throws", () => { + function TestError() {} + + class TestIterator extends Iterator { + next() { + return { + done: false, + value: 1, + }; + } + } + + expect(() => { + new TestIterator().every(() => { + throw new TestError(); + }); + }).toThrow(TestError); + }); +}); + +describe("normal behavior", () => { + test("length is 1", () => { + expect(Iterator.prototype.every).toHaveLength(1); + }); + + test("predicate function can see every value", () => { + function* generator() { + yield "a"; + yield "b"; + } + + let count = 0; + + const result = generator().every((value, index) => { + ++count; + + switch (index) { + case 0: + expect(value).toBe("a"); + break; + case 1: + expect(value).toBe("b"); + break; + default: + expect().fail(`Unexpected reducer invocation: value=${value} index=${index}`); + break; + } + + return true; + }); + + expect(count).toBe(2); + expect(result).toBeTrue(); + }); + + test("iteration stops when predicate returns false", () => { + function* generator() { + yield "a"; + yield "b"; + yield "c"; + } + + let count = 0; + + const result = generator().every(value => { + ++count; + return value === "a"; + }); + + expect(count).toBe(2); + expect(result).toBeFalse(); + }); +});