diff --git a/crates/engine/src/tests/script_run.rs b/crates/engine/src/tests/script_run.rs index eb8f43cc41..ebce013c57 100644 --- a/crates/engine/src/tests/script_run.rs +++ b/crates/engine/src/tests/script_run.rs @@ -326,8 +326,9 @@ fn a_js_script_reads_ecmascript_2015_and_a_jsx_script_does_not() { assert_eq!(modern["log"], json!([2]), "{modern}"); let legacy = s.execute("script.run", &path(&write("third.jsx", "#targetengine kept\nvc.log(k2);\nconst c = 1;"))); assert!(legacy.unwrap_err().to_string().contains("SyntaxError")); - let legacy = s.execute("script.run", &path(&write("fourth.jsx", "#targetengine kept\nvc.log(k2 + kept);"))).unwrap(); - assert_eq!(legacy["log"], json!([3]), "{legacy}"); + let fourth = "#targetengine kept\nvc.log(k2 + kept);\ntry { eval('let x = 1'); } catch (e) { vc.log(e.name); }"; + let legacy = s.execute("script.run", &path(&write("fourth.jsx", fourth))).unwrap(); + assert_eq!(legacy["log"], json!([3, "SyntaxError"]), "{legacy}"); // `for`-`of` and destructuring, in a new engine and in one a `.jsx` script made. let loops = "var s = 0;\nfor (const [a, { b = 1 }] of [[1, {}], [2, { b: 3 }]]) s += a + b;\nvar { p, q: [r] } = { p: 4, q: [5] };\nvc.log(s);\nvc.log(p + r);\nvc.log(typeof Symbol);"; let r = s.execute("script.run", &path(&write("loops.js", loops))).unwrap(); diff --git a/crates/js/src/bytecode.rs b/crates/js/src/bytecode.rs index a8d8471a4c..269573ffbb 100644 --- a/crates/js/src/bytecode.rs +++ b/crates/js/src/bytecode.rs @@ -211,6 +211,9 @@ pub(crate) enum Op { IterStep(u32), /// `record x₁…xₙ` → `record x₁…xₙ value`: the next value, `undefined` once it is done. IterValue(u32), + /// `record` → `record`: steps the iterator without reading the result's `value`, for a hole + /// in an array pattern. + IterSkip, /// `record x₁…xₙ` → `record x₁…xₙ array`: the values left, in an array. IterRest(u32), /// `record` → nothing: closes the iterator unless it is done (IteratorClose). @@ -404,7 +407,7 @@ fn effect(op: Op, finally_tables: &[Vec]) -> Result { Op::LoadParam(_) | Op::TemplateObject(_) => e(0, 1), Op::ArraySpread | Op::NewSpread => e(2, 1), Op::CallSpread(_) => e(3, 1), - Op::ToStr | Op::GetIterator | Op::RequireObjectCoercible => e(1, 1), + Op::ToStr | Op::GetIterator | Op::IterSkip | Op::RequireObjectCoercible => e(1, 1), Op::IterStep(t) => Effect { pops: 1, next: Some(2), jumps: vec![(t, 1)] }, Op::IterValue(n) | Op::IterRest(n) => e(n.checked_add(1).ok_or("operand too large")?, n.checked_add(2).ok_or("operand too large")?), Op::PatternGet(n) => e(n.checked_add(2).ok_or("operand too large")?, n.checked_add(3).ok_or("operand too large")?), diff --git a/crates/js/src/compiler.rs b/crates/js/src/compiler.rs index 3321475da6..611f052278 100644 --- a/crates/js/src/compiler.rs +++ b/crates/js/src/compiler.rs @@ -65,8 +65,9 @@ enum Ctx { /// The protected part (and catch clause) of a `try` with a `finally` block. Finally { try_depth: u32, env_depth: u32, entry: Label, exits: Vec }, /// The body of a `for-of` loop, whose Iterator Record is the top value at `depth`: a jump - /// out of the loop closes the iterator. - Iterator { depth: u32, env_depth: u32 }, + /// out of the loop goes to the label `exits` holds for it, after the loop's protected range, + /// where the iterator is closed and the jump goes on. + Iterator { depth: u32, env_depth: u32, exits: Vec<(Exit, Label)> }, } struct FnCompiler<'a> { @@ -335,7 +336,14 @@ impl<'a> FnCompiler<'a> { work.extend(f.iter().rev().map(|&st| (st, nested || inner, s))); } if let Some(h) = handler { - let s = enter(self.lexical_names(&h.body)); + // A `var` cannot redeclare a destructured catch parameter's names + // (ECMA-262 11th edition section B.3.5), so a block function of that name + // in the body is not also made a `var` (section B.3.3). + let mut names = self.lexical_names(&h.body); + if matches!(h.param, Binding::Pattern(_)) { + names.extend(self.bound_keys(&h.param)); + } + let s = enter(names); work.extend(h.body.iter().rev().map(|&st| (st, nested || inner, s))); } let s = enter(self.lexical_names(block)); @@ -976,8 +984,9 @@ impl<'a> FnCompiler<'a> { self.route_through_finally(i, exit, try_depth, env_depth, entry)?; break; } - Some(&Ctx::Iterator { depth, env_depth }) => { - // The loop is left: its iterator is closed, a `return` keeping its value. + Some(&Ctx::Iterator { depth, env_depth, .. }) => { + // The loop is left: the jump goes past the loop's exception handler, where its + // iterator is closed, a `return` keeping its value (see `for_of`). if exit == Exit::Return { let Some(under) = self.depth.checked_sub(1 + depth) else { return internal("stack below a for-of loop") }; if under > 0 { @@ -988,8 +997,9 @@ impl<'a> FnCompiler<'a> { } else { self.unwind_to(depth, env_depth)?; } - self.emit(Op::IterClose); - self.env_depth = env_depth; + let label = self.iterator_exit(i, exit)?; + self.jump(Op::Jump, label); + break; } None => return internal("context out of range"), } @@ -999,6 +1009,19 @@ impl<'a> FnCompiler<'a> { Ok(()) } + /// The label where the `for-of` loop of the iterator context `ctx` closes its iterator for + /// `exit` and goes on with it. + fn iterator_exit(&mut self, ctx: usize, exit: Exit) -> CResult