LibJS: Implement Iterator.prototype.take

This commit is contained in:
Timothy Flynn 2023-06-25 11:45:52 -04:00 committed by Andreas Kling
parent 7e0083fb65
commit 0e2f9f006d
Notes: sideshowbarker 2024-07-17 01:46:43 +09:00
5 changed files with 194 additions and 0 deletions

View file

@ -498,6 +498,7 @@ namespace JS {
P(supportedLocalesOf) \
P(supportedValuesOf) \
P(symmetricDifference) \
P(take) \
P(tan) \
P(tanh) \
P(test) \

View file

@ -101,6 +101,7 @@
M(NotObjectCoercible, "{} cannot be converted to an object") \
M(NotUndefined, "{} is not undefined") \
M(NumberIsNaN, "{} must not be NaN") \
M(NumberIsNegative, "{} must not be negative") \
M(ObjectDefineOwnPropertyReturnedFalse, "Object's [[DefineOwnProperty]] method returned false") \
M(ObjectDeleteReturnedFalse, "Object's [[Delete]] method returned false") \
M(ObjectFreezeFailed, "Could not freeze object") \

View file

@ -32,6 +32,7 @@ ThrowCompletionOr<void> IteratorPrototype::initialize(Realm& realm)
define_native_function(realm, vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
define_native_function(realm, vm.names.map, map, 1, attr);
define_native_function(realm, vm.names.filter, filter, 1, attr);
define_native_function(realm, vm.names.take, take, 1, attr);
return {};
}
@ -170,4 +171,71 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::filter)
return result;
}
// 3.1.3.4 Iterator.prototype.take ( limit ), https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.take
JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::take)
{
auto& realm = *vm.current_realm();
auto limit = 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. Let numLimit be ? ToNumber(limit).
auto numeric_limit = TRY(limit.to_number(vm));
// 4. If numLimit is NaN, throw a RangeError exception.
if (numeric_limit.is_nan())
return vm.throw_completion<RangeError>(ErrorType::NumberIsNaN, "limit"sv);
// 5. Let integerLimit be ! ToIntegerOrInfinity(numLimit).
auto integer_limit = MUST(numeric_limit.to_integer_or_infinity(vm));
// 6. If integerLimit < 0, throw a RangeError exception.
if (integer_limit < 0)
return vm.throw_completion<RangeError>(ErrorType::NumberIsNegative, "limit"sv);
// 7. Let iterated be ? GetIteratorDirect(O).
auto iterated = TRY(get_iterator_direct(vm, object));
// 8. Let closure be a new Abstract Closure with no parameters that captures iterated and integerLimit and performs the following steps when called:
IteratorHelper::Closure closure = [integer_limit](auto& iterator) -> ThrowCompletionOr<Value> {
auto& vm = iterator.vm();
auto const& iterated = iterator.underlying_iterator();
// a. Let remaining be integerLimit.
// b. Repeat,
// i. If remaining is 0, then
if (iterator.counter() >= integer_limit) {
// 1. Return ? IteratorClose(iterated, NormalCompletion(undefined)).
return iterator.close_result(normal_completion(js_undefined()));
}
// ii. If remaining is not +∞, then
// 1. Set remaining to remaining - 1.
iterator.increment_counter();
// iii. Let next be ? IteratorStep(iterated).
auto next = TRY(iterator_step(vm, iterated));
// iv. If next is false, return undefined.
if (!next)
return iterator.result(js_undefined());
// v. Let completion be Completion(Yield(? IteratorValue(next))).
// vi. IfAbruptCloseIterator(completion, iterated).
return iterator.result(TRY(iterator_value(vm, *next)));
};
// 9. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
// 10. Set result.[[UnderlyingIterator]] to iterated.
auto result = TRY(IteratorHelper::create(realm, move(iterated), move(closure)));
// 11. Return result.
return result;
}
}

View file

@ -24,6 +24,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
JS_DECLARE_NATIVE_FUNCTION(map);
JS_DECLARE_NATIVE_FUNCTION(filter);
JS_DECLARE_NATIVE_FUNCTION(take);
};
}

View file

@ -0,0 +1,123 @@
describe("errors", () => {
test("called with non-numeric object", () => {
expect(() => {
Iterator.prototype.take(Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
test("called with invalid numbers", () => {
expect(() => {
Iterator.prototype.take(NaN);
}).toThrowWithMessage(RangeError, "limit must not be NaN");
expect(() => {
Iterator.prototype.take(-1);
}).toThrowWithMessage(RangeError, "limit must not be negative");
});
test("iterator's next method throws", () => {
function TestError() {}
class TestIterator extends Iterator {
next() {
throw new TestError();
}
}
expect(() => {
const iterator = new TestIterator().take(1);
iterator.next();
}).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(() => {
const iterator = new TestIterator().take(1);
iterator.next();
}).toThrow(TestError);
});
});
describe("normal behavior", () => {
test("length is 1", () => {
expect(Iterator.prototype.take).toHaveLength(1);
});
test("limited to zero", () => {
function* generator() {
yield "a";
yield "b";
}
const iterator = generator().take(0);
let value = iterator.next();
expect(value.value).toBeUndefined();
expect(value.done).toBeTrue();
value = iterator.next();
expect(value.value).toBeUndefined();
expect(value.done).toBeTrue();
});
test("lower limit than the number of values", () => {
function* generator() {
yield "a";
yield "b";
yield "c";
}
const iterator = generator().take(2);
let value = iterator.next();
expect(value.value).toBe("a");
expect(value.done).toBeFalse();
value = iterator.next();
expect(value.value).toBe("b");
expect(value.done).toBeFalse();
value = iterator.next();
expect(value.value).toBeUndefined();
expect(value.done).toBeTrue();
});
test("higher limit than the number of values", () => {
function* generator() {
yield "a";
yield "b";
yield "c";
}
const iterator = generator().take(Infinity);
let value = iterator.next();
expect(value.value).toBe("a");
expect(value.done).toBeFalse();
value = iterator.next();
expect(value.value).toBe("b");
expect(value.done).toBeFalse();
value = iterator.next();
expect(value.value).toBe("c");
expect(value.done).toBeFalse();
value = iterator.next();
expect(value.value).toBeUndefined();
expect(value.done).toBeTrue();
});
});