Truly independent web browser
LibJS: Resolve the names of var patterns before getting their values
Shannon Boothf3318ce27636
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rsindex be02a4f3949..43b84ccb2c0 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs@@ -1102,7 +1102,12 @@ impl Generator<'_> {self.begin_disposal(is_async);self.add_disposable_resource(value, is_async);}- self.bind_target(target, first_row, value, PatternMode::Declares(kind))+ let mode = if matches!(self.tree.node_tag(target), tag::ARRAY_PATTERN | tag::OBJECT_PATTERN) {+ self.mode_of_declared_pattern(kind)+ } else {+ PatternMode::Declares(kind)+ };+ self.bind_target(target, first_row, value, mode)}LoopTarget::Pattern { target, first_row } => {self.bind_target(target, first_row, value, PatternMode::Assigns)@@ -1521,17 +1526,24 @@ impl Generator<'_> {self.declare_target_names(target, kind)?;let value = self.sweep_value(target.index() + 1, init)?;self.current_source_offset = position;- // NB: The object of a `with` can have a property of the name of a `var`, which makes it a reference like any other.- let mode = if kind == DeclarationKind::Var && self.context.scope.is_in_with() {- PatternMode::Assigns- } else {- PatternMode::Declares(kind)- };+ let mode = self.mode_of_declared_pattern(kind);self.bind_target(target, first_row, value, mode)?;self.end_of_statement();Ok(())}+ fn mode_of_declared_pattern(&self, kind: DeclarationKind) -> PatternMode {+ let scope = &self.context.scope;+ let is_sloppy_eval = scope.is_program_without_globals() && !self.strict;+ // NB: The object of a `with` around the code, or around the non-strict `eval` that it is the code of, can have+ // a property of the name of a `var`, which makes it a reference like any other.+ if kind == DeclarationKind::Var && (scope.is_in_with() || is_sloppy_eval) {+ PatternMode::Assigns+ } else {+ PatternMode::Declares(kind)+ }+ }+/// `[a, b] = c`, whose value is what is on the right side.fn sweep_assignment_pattern(&mut self, assignment: NodeId, first_row: usize) -> Result<(), OutOfStack> {let tree = self.tree;diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rsindex b0477ce952c..8e65551a878 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs@@ -349,6 +349,11 @@ impl CodeScope {self.program.is_some()}+ #[inline(always)]+ pub(super) fn is_program_without_globals(&self) -> bool {+ self.program == Some(ProgramKind::WithoutGlobals)+ }+#[inline(always)]pub(super) fn is_in_with(&self) -> bool {self.with_depth != 0diff --git a/Tests/LibJS/Runtime/with-basic.js b/Tests/LibJS/Runtime/with-basic.jsindex fcc74176355..41c3539c8e1 100644--- a/Tests/LibJS/Runtime/with-basic.js+++ b/Tests/LibJS/Runtime/with-basic.js@@ -93,6 +93,48 @@ test("for-in var initializer keeps the resolved with binding through initializerexpect(forInId).toBeUndefined();});+test("var patterns resolve their names before getting their values", () => {+ const object = { forOfId: 1 };+ var forOfId = "var";++ with (object) {+ for (var { forOfId = (delete object.forOfId, 2) } of [{}]);+ }++ expect(object.forOfId).toBe(2);+ expect(forOfId).toBe("var");++ const other = { evalId: 1 };+ var evalId = "var";++ with (other) {+ eval("var [evalId = (delete other.evalId, 2)] = [];");+ }++ expect(other.evalId).toBe(2);+ expect(evalId).toBe("var");++ const added = {};+ var forOfAddedId = "var";++ with (added) {+ for (var { forOfAddedId = ((added.forOfAddedId = 1), 2) } of [{}]);+ }++ expect(added.forOfAddedId).toBe(1);+ expect(forOfAddedId).toBe(2);++ const addedByEval = {};+ var evalAddedId = "var";++ with (addedByEval) {+ eval("var [evalAddedId = ((addedByEval.evalAddedId = 1), 2)] = [];");+ }++ expect(addedByEval.evalAddedId).toBe(1);+ expect(evalAddedId).toBe(2);+});+test("with statement updates empty abrupt completions to undefined", () => {expect(eval("1; do { 2; with ({}) { 3; break; } 4; } while (false);")).toBe(3);expect(eval("5; do { 6; with ({}) { break; } 7; } while (false);")).toBeUndefined();