Skip to content

Truly independent web browser

LibJS: Leave class and for-in/of scopes before running `finally`

Tim Ledbetter859df6f9f3a4
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
index eb9953ca861..e23d302e9d4 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
@@ -6888,6 +6888,7 @@ fn generate_class_expression(
is_catch_environment: false,
});
generator.push_static_lexical_environment(class_env.clone());
+ generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
// https://tc39.es/ecma262/#sec-runtime-semantics-classdefinitionevaluation
// If _classBinding_ is not *undefined*, then
@@ -7219,6 +7220,7 @@ fn generate_class_expression(
generator.emit(Instruction::SetLexicalEnvironment {
environment: parent_env.operand(),
});
+ generator.end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
generator.pop_tracked_lexical_environment();
// Allocate dst after element keys.
@@ -7435,6 +7437,7 @@ fn enter_for_in_of_head_tdz(generator: &mut Generator, lhs: &ForInOfLhs) -> bool
}
if !names.is_empty() {
generator.push_new_lexical_environment(u32_from_usize(names.len()));
+ generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
for (name, _) in &names {
let id = generator.intern_identifier(name);
generator.emit(Instruction::CreateVariable {
@@ -7453,6 +7456,7 @@ fn enter_for_in_of_head_tdz(generator: &mut Generator, lhs: &ForInOfLhs) -> bool
/// Tear down the TDZ environment after RHS evaluation.
fn leave_for_in_of_head_tdz(generator: &mut Generator) {
+ generator.end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
generator.pop_tracked_lexical_environment();
if !generator.is_current_block_terminated() {
let parent = generator.current_lexical_environment();
@@ -7579,9 +7583,6 @@ fn generate_for_in_statement(
create_for_in_of_lexical_env(generator, lhs);
}
- // Assign to LHS
- assign_to_for_in_of_lhs(generator, lhs, &next_value);
-
// Body — break/continue handle environment restoration via LeaveLexicalEnvironment boundary.
let labels = std::mem::take(&mut generator.pending_labels);
generator.begin_continuable_scope(update_block, labels.clone(), completion.clone());
@@ -7590,6 +7591,9 @@ fn generate_for_in_statement(
generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
}
+ // Assign to LHS
+ assign_to_for_in_of_lhs(generator, lhs, &next_value);
+
if !generator.is_current_block_terminated() {
generate_with_completion(body, generator, completion.as_ref(), preferred_dst);
}
@@ -7834,13 +7838,14 @@ fn generate_for_of_statement_inner(
_ => None,
};
+ generator.begin_continuable_scope(update_block, labels, completion.clone());
+ if needs_lexical_env {
+ generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
+ }
+
if let Some(disposal_kind) = disposal_kind {
// NB: The resource of a `using` or `await using` declaration is disposed of at the end of each iteration,
// including when the declaration is abrupt, and before the iterator is closed.
- generator.begin_continuable_scope(update_block, labels, completion.clone());
- if needs_lexical_env {
- generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
- }
generate_with_disposable_resource_stack(generator, disposal_kind, |generator| {
assign_to_for_in_of_lhs(generator, lhs, &next_value);
if !generator.is_current_block_terminated() {
@@ -7853,11 +7858,6 @@ fn generate_for_of_statement_inner(
assign_to_for_in_of_lhs(generator, lhs, &next_value);
// Body
- generator.begin_continuable_scope(update_block, labels, completion.clone());
- if needs_lexical_env {
- generator.start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
- }
-
if !generator.is_current_block_terminated() {
generate_with_completion(body, generator, completion.as_ref(), preferred_dst);
}
diff --git a/Tests/LibJS/Runtime/try-return-finally.js b/Tests/LibJS/Runtime/try-return-finally.js
index 0d2051aadb7..c8fd5ceaa29 100644
--- a/Tests/LibJS/Runtime/try-return-finally.js
+++ b/Tests/LibJS/Runtime/try-return-finally.js
@@ -174,3 +174,81 @@ test("generator return through an array destructuring restores the environment f
expect(capturedA()).toBe("assigned");
expect(capturedT()).toBe("t");
});
+
+test("generator return from a class definition restores the environment for finally", () => {
+ let capturedA;
+ let capturedT;
+ function* g() {
+ let a = "a";
+ capturedA = () => a;
+ try {
+ {
+ let t = "t";
+ capturedT = () => t;
+ class C {
+ [yield] = 1;
+ }
+ }
+ } finally {
+ a = "assigned";
+ }
+ }
+
+ const generator = g();
+ generator.next();
+ generator.return();
+ expect(capturedA()).toBe("assigned");
+ expect(capturedT()).toBe("t");
+});
+
+test("generator return from a for-of head restores the environment for finally", () => {
+ let capturedA;
+ let capturedT;
+ function* g() {
+ let a = "a";
+ capturedA = () => a;
+ try {
+ {
+ let t = "t";
+ capturedT = () => t;
+ for (const x of yield) {
+ const capturedX = () => x;
+ }
+ }
+ } finally {
+ a = "assigned";
+ }
+ }
+
+ const generator = g();
+ generator.next();
+ generator.return();
+ expect(capturedA()).toBe("assigned");
+ expect(capturedT()).toBe("t");
+});
+
+test("generator return from a for-in loop variable default restores the environment for finally", () => {
+ let capturedA;
+ let capturedT;
+ function* g() {
+ let a = "a";
+ capturedA = () => a;
+ try {
+ {
+ let t = "t";
+ capturedT = () => t;
+ for (const { x = yield } in { p: 1 }) {
+ const capturedX = () => x;
+ }
+ }
+ } finally {
+ a = "assigned";
+ }
+ }
+
+ const generator = g();
+ generator.next();
+ generator.return();
+ expect(capturedA()).toBe("assigned");
+ expect(capturedT()).toBe("t");
+});