ladybird/Libraries/LibJS/Tests/return.js
Matthew Olsson 6e05685ad4 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.
2020-10-08 23:23:55 +02:00

53 lines
1.1 KiB
JavaScript

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);
});
});