1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:57:35 +00:00

LibJS: Implement parsing and executing for-await-of loops

This commit is contained in:
davidot 2021-11-23 16:09:28 +01:00 committed by Linus Groh
parent b3699029e2
commit e69276e704
5 changed files with 242 additions and 7 deletions

View file

@ -0,0 +1,71 @@
describe("basic behavior", () => {
test("empty array", () => {
var enteredFunction = false;
var rejected = false;
async function f() {
enteredFunction = true;
for await (const v of []) {
expect().fail("Should not enter loop");
}
}
f().then(
() => {
expect(enteredFunction).toBeTrue();
},
() => {
rejected = true;
}
);
runQueuedPromiseJobs();
expect(enteredFunction).toBeTrue();
expect(rejected).toBeFalse();
});
test("sync iterator", () => {
var loopIterations = 0;
var rejected = false;
async function f() {
for await (const v of [1]) {
expect(v).toBe(1);
loopIterations++;
}
}
f().then(
() => {
expect(loopIterations).toBe(1);
},
() => {
rejected = true;
}
);
runQueuedPromiseJobs();
expect(loopIterations).toBe(1);
expect(rejected).toBeFalse();
});
});
describe("only allowed in async functions", () => {
test("async functions", () => {
expect("async function foo() { for await (const v of []) return v; }").toEval();
expect("(async function () { for await (const v of []) return v; })").toEval();
expect("async () => { for await (const v of []) return v; }").toEval();
});
test("regular functions", () => {
expect("function foo() { for await (const v of []) return v; }").not.toEval();
expect("(function () { for await (const v of []) return v; })").not.toEval();
expect("() => { for await (const v of []) return v; }").not.toEval();
});
test("generator functions", () => {
expect("function* foo() { for await (const v of []) return v; }").not.toEval();
expect("(function* () { for await (const v of []) return v; })").not.toEval();
});
test("async genrator functions", () => {
expect("async function* foo() { for await (const v of []) yield v; }").toEval();
expect("(async function* () { for await (const v of []) yield v; })").toEval();
});
});