mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:28:13 +00:00
LibJS: Start implementing iterable framework, add ArrayIterator
With the addition of symbol keys, work can now be done on starting to implement the well-known symbol functionality. The most important of these well-known symbols is by far Symbol.iterator. This patch adds IteratorPrototype, as well as ArrayIterator and ArrayIteratorPrototype. In the future, sometime after StringIterator has also been added, this will allow us to use Symbol.iterator directly in for..of loops, enabling the use of custom iterator objects. Also makes adding iterator support to native objects much easier (as will have to be done for Map and Set, when they get added).
This commit is contained in:
parent
51bfc6c6b3
commit
2ea85355fe
18 changed files with 657 additions and 1 deletions
48
Libraries/LibJS/Tests/iterators/array-iterator.js
Normal file
48
Libraries/LibJS/Tests/iterators/array-iterator.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
test("length", () => {
|
||||
expect(Array.prototype[Symbol.iterator].length).toBe(0);
|
||||
});
|
||||
|
||||
test("same function as Array.prototype.values", () => {
|
||||
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const a = [1, 2, 3];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: 3, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
|
||||
test("works when applied to non-object", () => {
|
||||
[true, false, 9, 2n, Symbol()].forEach(primitive => {
|
||||
const it = [][Symbol.iterator].call(primitive);
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
});
|
||||
|
||||
test("item added to array before exhaustion is accessible", () => {
|
||||
const a = [1, 2];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
a.push(3);
|
||||
expect(it.next()).toEqual({ value: 3, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
|
||||
test("item added to array after exhaustion is inaccesible", () => {
|
||||
const a = [1, 2];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
a.push(3);
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue