1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

LibJS: Fix return statements not working properly in loops

Previously, when a loop detected an unwind of type ScopeType::Function
(which means a return statement was executed inside of the loop), it
would just return undefined. This set the VM's last_value to undefined,
when it should have been the returned value. This patch makes all loop
statements return the appropriate value in the above case.
This commit is contained in:
Matthew Olsson 2020-10-08 13:17:40 -07:00 committed by Andreas Kling
parent d980073122
commit 6e05685ad4
3 changed files with 59 additions and 8 deletions

View file

@ -0,0 +1,53 @@
describe("returning from loops", () => {
test("returning from while loops", () => {
function foo() {
while (true) {
return 10;
}
}
expect(foo()).toBe(10);
});
test("returning from do-while loops", () => {
function foo() {
do {
return 10;
} while (true);
}
expect(foo()).toBe(10);
});
test("returning from for loops", () => {
function foo() {
for (let i = 0; i < 5; i++) {
return 10;
}
}
expect(foo()).toBe(10);
});
test("returning from for-in loops", () => {
function foo() {
const o = { a: 1, b: 2 };
for (let a in o) {
return 10;
}
}
expect(foo()).toBe(10);
});
test("returning from for-of loops", () => {
function foo() {
const o = [1, 2, 3];
for (let a of o) {
return 10;
}
}
expect(foo()).toBe(10);
});
});