Skip to content

Truly independent web browser

LibJS: Leave the scope of a class before running `finally`

Tim Ledbetterf253572d0d61
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index f869c8be6b2..99b746308ef 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -47,6 +47,7 @@ use super::bindings::*;
use super::context::Generator;
use super::nodes::Identifier;
use super::registers::ScopedOperand;
+use super::stream::Control;
use super::stream::HeldRegisters;
use super::stream::ScopeEnvironment;
@@ -1826,11 +1827,18 @@ impl Generator<'_> {
is_parameter: false,
});
}
- ScopeEnvironment { register, parent, held }
+ let environment = ScopeEnvironment { register, parent, held };
+ self.push_control(Control::Block {
+ environment: Some(environment),
+ });
+ environment
}
/// The scope of a class ends, whose environment lives on in what the class is made of.
pub(super) fn end_class_scope(&mut self, environment: ScopeEnvironment) {
+ let Some(Control::Block { environment: Some(_) }) = self.pop_control() else {
+ unreachable!("the scope of a class is what is going on");
+ };
self.pop_tracked_lexical_environment();
let scope = &mut self.context.scope;
scope.scope_environment_depth -= 1;
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 1036d2861f3..14462b2d8c5 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -703,8 +703,8 @@ impl Generator<'_> {
}
#[inline(always)]
- pub(super) fn pop_control(&mut self) {
- self.context.stream.controls.pop();
+ pub(super) fn pop_control(&mut self) -> Option<Control> {
+ self.context.stream.controls.pop()
}
#[inline(always)]
diff --git a/Tests/LibJS/Runtime/try-return-finally.js b/Tests/LibJS/Runtime/try-return-finally.js
index c8fd5ceaa29..806ae466307 100644
--- a/Tests/LibJS/Runtime/try-return-finally.js
+++ b/Tests/LibJS/Runtime/try-return-finally.js
@@ -252,3 +252,98 @@ test("generator return from a for-in loop variable default restores the environm
expect(capturedA()).toBe("assigned");
expect(capturedT()).toBe("t");
});
+
+describe("every way out of a scope restores the environment for finally", () => {
+ const yieldExit = { statement: "yield", expression: "yield" };
+ const throwExit = { statement: "thrower()", expression: "thrower()" };
+ const exits = {
+ "generator return": {
+ ...yieldExit,
+ run: generator => {
+ generator.next();
+ expect(generator.return("r")).toEqual({ value: "r", done: true });
+ },
+ },
+ "generator throw": {
+ ...yieldExit,
+ run: generator => {
+ generator.next();
+ expect(() => generator.throw(new Error())).toThrow(Error);
+ },
+ },
+ throw: {
+ ...throwExit,
+ run: generator => expect(() => generator.next()).toThrow(Error),
+ },
+ return: {
+ statement: "return 'r'",
+ run: generator => expect(generator.next()).toEqual({ value: "r", done: true }),
+ },
+ break: {
+ statement: "break outer",
+ run: generator => expect(generator.next()).toEqual({ value: undefined, done: true }),
+ },
+ continue: {
+ statement: "continue outer",
+ run: generator => expect(generator.next()).toEqual({ value: undefined, done: true }),
+ },
+ };
+
+ const scopes = {
+ "a block": "{ let t = 1; capture(() => t); STATEMENT; }",
+ "a for loop body": "for (let i = 0; i < 1; i++) { capture(() => i); STATEMENT; }",
+ "a for-of loop body": "for (const x of [1]) { capture(() => x); STATEMENT; }",
+ "a for-in loop body": "for (const x in { p: 1 }) { capture(() => x); STATEMENT; }",
+ "a catch clause": "try { throw 1; } catch (e) { capture(() => e); STATEMENT; }",
+ "a switch case": "switch (0) { case 0: let s = 1; capture(() => s); STATEMENT; }",
+ "a with statement": "with ({}) { STATEMENT; }",
+ "a for-of loop head": "for (const x of [EXPRESSION]) { capture(() => x); }",
+ "a for-in loop variable default": "for (const { x = EXPRESSION } in { p: 1 }) { capture(() => x); }",
+ "an array destructuring default": "let [y = EXPRESSION] = []; capture(() => y);",
+ "a class heritage": "class C extends (EXPRESSION, Object) {}",
+ "a class computed key": "class C { [EXPRESSION] = 1; }",
+ "an anonymous class computed key": "(class { [EXPRESSION] = 1; });",
+ "a class computed key after private names": "class C { #p = 1; [EXPRESSION] = 1; }",
+ };
+
+ for (const [scopeName, scope] of Object.entries(scopes)) {
+ for (const [exitName, exit] of Object.entries(exits)) {
+ const code = scope.includes("STATEMENT")
+ ? exit.statement && scope.replace("STATEMENT", exit.statement)
+ : exit.expression && scope.replace("EXPRESSION", exit.expression);
+ if (!code) continue;
+
+ for (const inBlock of [false, true]) {
+ const body = inBlock ? `{ let w = 1; capture(() => w); ${code} }` : code;
+ test(`${exitName} from ${scopeName}${inBlock ? " in a block" : ""}`, () => {
+ const makeGenerator = new Function(
+ "capture",
+ "seen",
+ "thrower",
+ `return function* () {
+ let a = "a";
+ let b = "b";
+ capture(() => a + b);
+ outer: for (let iteration = 0; iteration < 1; iteration++) {
+ try {
+ ${body}
+ } finally {
+ seen.push(b);
+ b = "assigned";
+ }
+ }
+ };`
+ );
+ const captured = [];
+ const seen = [];
+ const thrower = () => {
+ throw new Error();
+ };
+ exit.run(makeGenerator(binding => captured.push(binding), seen, thrower)());
+ expect(seen).toEqual(["b"]);
+ expect(captured[0]()).toBe("aassigned");
+ });
+ }
+ }
+ }
+});