1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:38:10 +00:00

LibJS: Integrate iterator protocol into language features

Finally use Symbol.iterator protocol in language features :) currently
only used in for-of loops and spread expressions, but will have more
uses later (Maps, Sets, Array.from, etc).
This commit is contained in:
Matthew Olsson 2020-07-13 08:27:20 -07:00 committed by Andreas Kling
parent 4970c448bf
commit a51b2393f2
8 changed files with 187 additions and 127 deletions

View file

@ -28,6 +28,59 @@ describe("correct behavior", () => {
for (char of "abc");
expect(char).toBe("c");
});
test("respects custom Symbol.iterator method", () => {
const o = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i++ == 3) {
return { done: true };
}
return { value: this.i, done: false };
},
};
},
};
const a = [];
for (const k of o) {
a.push(k);
}
expect(a).toEqual([1, 2, 3]);
});
test("loops through custom iterator if there is an exception thrown part way through", () => {
// This tests against the way custom iterators used to be implemented, where the values
// were all collected at once before the for-of body was executed, instead of getting
// the values one at a time
const o = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i++ === 3) {
throw new Error();
}
return { value: this.i };
},
};
},
};
const a = [];
try {
for (let k of o) {
a.push(k);
}
expect().fail();
} catch (e) {
expect(a).toEqual([1, 2, 3]);
}
});
});
describe("errors", () => {
@ -35,13 +88,13 @@ describe("errors", () => {
expect(() => {
for (const _ of 123) {
}
}).toThrowWithMessage(TypeError, "for..of right-hand side must be iterable");
}).toThrowWithMessage(TypeError, "123 is not iterable");
});
test("right hand side is an object", () => {
expect(() => {
for (const _ of { foo: 1, bar: 2 }) {
}
}).toThrowWithMessage(TypeError, "for..of right-hand side must be iterable");
}).toThrowWithMessage(TypeError, "[object Object] is not iterable");
});
});