Skip to content

Truly independent web browser

LibJS: Put for-in/of pattern bindings back in their TDZ each time

Shannon Boothaf40b33391ec
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 8092f842c48..0b81b283ebc 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -1110,6 +1110,19 @@ impl Generator<'_> {
bindings.map(|bindings| self.enter_scope_environment(bindings, false, None));
self.push_control(Control::Block { environment });
self.current_source_offset = SourceOffset(tree.node_pos(statement));
+ if let LoopTarget::Declared {
+ target: pattern, kind, ..
+ } = target
+ && kind != DeclarationKind::Var
+ && matches!(tree.node_tag(pattern), tag::ARRAY_PATTERN | tag::OBJECT_PATTERN)
+ && tree.pattern_contains_expression(pattern.0)
+ {
+ // NB: The bindings of each iteration are new, and the code of the pattern sees them in their dead zone.
+ for_each_bound_name(tree, pattern, &mut |name| {
+ let ident = self.identifier_of(name);
+ self.put_in_dead_zone(ident);
+ });
+ }
self.assign_to_loop_target(target, value)?;
self.end_of_statement();
self.sweep_rows(right as usize + 1, body as usize + 1)?;
@@ -1353,7 +1366,7 @@ impl Generator<'_> {
} else {
for parameter in parameters.iter() {
let first_name = names.len();
- for_each_parameter_name(tree, parameter, &mut |name| names.push(name));
+ for_each_bound_name(tree, parameter, &mut |name| names.push(name));
let target = match tree.node_tag(parameter) {
tag::REST | tag::ASSIGN_PATTERN => NodeId(tree.node_a(parameter)),
_ => parameter,
@@ -1569,18 +1582,25 @@ impl Generator<'_> {
self.current_source_offset = SourceOffset(tree.node_pos(target));
let ident = self.declare_binding(Name(tree.node_a(target)), kind);
// NB: Whatever a run through this code before left in a local, it is in its dead zone here.
- if kind != DeclarationKind::Var && ident.local_type == Some(LocalType::Variable) {
- let empty = self.context.constants.add_empty();
- self.put(op::Mov {
- dst: Operand::local(ident.local_index),
- src: empty,
- });
+ if kind != DeclarationKind::Var {
+ self.put_in_dead_zone(ident);
}
Ok(())
}
}
}
+ /// Puts a lexical binding that lives in a register back in its dead zone.
+ fn put_in_dead_zone(&mut self, ident: Identifier) {
+ if ident.local_type == Some(LocalType::Variable) {
+ let empty = self.context.constants.add_empty();
+ self.put(op::Mov {
+ dst: Operand::local(ident.local_index),
+ src: empty,
+ });
+ }
+ }
+
/// Gives `value` to `target`, an identifier or a pattern, whose rows start at `first_row`.
fn bind_target(
&mut self,
@@ -1979,23 +1999,24 @@ fn parameter_has_expression(tree: &SyntaxTree, parameter: NodeId) -> bool {
&& tree.pattern_contains_expression(target.0)
}
-/// Calls `callback` with what a parameter binds, in the order of the source.
-fn for_each_parameter_name(tree: &SyntaxTree, parameter: NodeId, callback: &mut impl FnMut(Name)) {
- match tree.node_tag(parameter) {
+/// Calls `callback` with each of the BoundNames of `target`, an identifier or a binding pattern, in the order of the
+/// source.
+fn for_each_bound_name(tree: &SyntaxTree, target: NodeId, callback: &mut impl FnMut(Name)) {
+ match tree.node_tag(target) {
tag::ARRAY_PATTERN | tag::OBJECT_PATTERN => {
- for item in tree.node_list(tree.node_a(parameter)).iter() {
+ for item in tree.node_list(tree.node_a(target)).iter() {
match tree.node_tag(item) {
tag::HOLE => {}
tag::PROPERTY | tag::COMPUTED_PROPERTY => {
- for_each_parameter_name(tree, NodeId(tree.node_b(item)), callback);
+ for_each_bound_name(tree, NodeId(tree.node_b(item)), callback);
}
tag::SHORTHAND => callback(Name(tree.node_a(item))),
- _ => for_each_parameter_name(tree, item, callback),
+ _ => for_each_bound_name(tree, item, callback),
}
}
}
- tag::REST | tag::ASSIGN_PATTERN => for_each_parameter_name(tree, NodeId(tree.node_a(parameter)), callback),
- _ => callback(Name(tree.node_a(parameter))),
+ tag::REST | tag::ASSIGN_PATTERN => for_each_bound_name(tree, NodeId(tree.node_a(target)), callback),
+ _ => callback(Name(tree.node_a(target))),
}
}
diff --git a/Tests/LibJS/Runtime/loops/tdz-on-each-iteration.js b/Tests/LibJS/Runtime/loops/tdz-on-each-iteration.js
index 1115ed42455..7e3a6d147c4 100644
--- a/Tests/LibJS/Runtime/loops/tdz-on-each-iteration.js
+++ b/Tests/LibJS/Runtime/loops/tdz-on-each-iteration.js
@@ -342,4 +342,63 @@ describe("a lexical binding is uninitialized again on each iteration", () => {
}
expect(results).toEqual(["ReferenceError", "ReferenceError", "ReferenceError", "ReferenceError"]);
});
+
+ test("for-in and for-of patterns that read their own bindings", () => {
+ function run(loop) {
+ const results = [];
+ try {
+ loop(results);
+ } catch (error) {
+ results.push(error.name);
+ }
+ return results;
+ }
+
+ expect(
+ run(results => {
+ for (let [a = b, b] of [
+ [1, 2],
+ [undefined, 3],
+ ])
+ results.push(a);
+ })
+ ).toEqual([1, "ReferenceError"]);
+ expect(
+ run(results => {
+ for (let [a = a] of [[1], []]) results.push(a);
+ })
+ ).toEqual([1, "ReferenceError"]);
+ expect(
+ run(results => {
+ for (const { a = b, b } of [{ a: 1, b: 2 }, { b: 3 }]) results.push(a);
+ })
+ ).toEqual([1, "ReferenceError"]);
+ expect(
+ run(results => {
+ for (let { [b]: a, b } of [{ x: 1, b: "x" }, { b: "y" }]) results.push(a);
+ })
+ ).toEqual(["ReferenceError"]);
+ expect(
+ run(results => {
+ for (let [a = b, b] in { ab: 1, "": 2 }) results.push(a);
+ })
+ ).toEqual(["a", "ReferenceError"]);
+ });
+
+ test("for-await-of patterns that read their own bindings", () => {
+ const results = [];
+ (async () => {
+ try {
+ for await (let [a = b, b] of [
+ [1, 2],
+ [undefined, 3],
+ ])
+ results.push(a);
+ } catch (error) {
+ results.push(error.name);
+ }
+ })();
+ runQueuedPromiseJobs();
+ expect(results).toEqual([1, "ReferenceError"]);
+ });
});