Skip to content

Truly independent web browser

LibJS: Empty the marks of an array literal that turns into a pattern

Shannon Boothbde58ea3a5bc
diff --git a/Libraries/LibJS/Parser/Rust/src/expr.rs b/Libraries/LibJS/Parser/Rust/src/expr.rs
index b6eb7d579e1..e0a59cf6830 100644
--- a/Libraries/LibJS/Parser/Rust/src/expr.rs
+++ b/Libraries/LibJS/Parser/Rust/src/expr.rs
@@ -1629,6 +1629,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
}
tag::ARRAY | tag::ARRAY_SPREAD | tag::ARRAY_PATTERN => {
+ let literal_tag = self.ast.tag[node as usize];
self.ast.tag[node as usize] = tag::ARRAY_PATTERN;
let list = self.ast.a[node as usize];
let count = self.ast.extra[list as usize];
@@ -1655,6 +1656,21 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
_ => self.convert_to_assignable(element, bind),
}
}
+ if self.marks && literal_tag != tag::ARRAY_PATTERN {
+ let mark = self.ast.b[node as usize];
+ self.ast.tag[mark as usize] = tag::EMPTY;
+ // NB: The mark where the array starts to be built comes right before the rest element.
+ if literal_tag == tag::ARRAY_SPREAD {
+ let before_rest = if count >= 2 {
+ self.ast.extra[(list + count - 1) as usize]
+ } else {
+ mark
+ };
+ let array_start = before_rest as usize + 1;
+ assert_eq!(self.ast.tag[array_start], tag::MARK_ARRAY_START);
+ self.ast.tag[array_start] = tag::EMPTY;
+ }
+ }
}
tag::REST => self.convert_to_assignable(self.ast.a[node as usize], bind),
tag::ASSIGN_PATTERN => {
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 40f1e7d3341..3b5ea9d1a2b 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -1923,8 +1923,6 @@ impl Generator<'_> {
for (index, item) in items.iter().enumerate() {
let first_temporary = self.next_temporary();
let is_rest = tree.node_tag(item) == tag::REST;
- // NB: A rest element started out as a spread element, whose mark means nothing here.
- row += usize::from(tree.ast.tag[row] == tag::MARK_ARRAY_START);
if tree.node_tag(item) == tag::HOLE {
let after_step = self.new_label();
if index != 0 {
diff --git a/Tests/LibJS/Runtime/nested-rest-destructuring-assignment.js b/Tests/LibJS/Runtime/nested-rest-destructuring-assignment.js
new file mode 100644
index 00000000000..9638c4d91e4
--- /dev/null
+++ b/Tests/LibJS/Runtime/nested-rest-destructuring-assignment.js
@@ -0,0 +1,80 @@
+test("nested array pattern that starts with a rest element preserves the assignment result", () => {
+ const o = {};
+ const source = [[1, 2]];
+ const result = ([[...o.x]] = source);
+ expect(o.x).toEqual([1, 2]);
+ expect(result).toBe(source);
+});
+
+test("nested array pattern that starts with a rest element preserves the values around it", () => {
+ const o = {};
+ const array = [1, ([[...o.x]] = [[2]]), 3];
+ expect(array).toEqual([1, [[2]], 3]);
+ expect(o.x).toEqual([2]);
+
+ const object = { a: 1, b: ([[...o.y]] = [[3, 4]]) };
+ expect(object).toEqual({ a: 1, b: [[3, 4]] });
+ expect(o.y).toEqual([3, 4]);
+
+ expect(1 + ([[...o.z]] = [[5]]).length).toBe(2);
+});
+
+test("nested array pattern that starts with a rest element preserves the callee and earlier arguments", () => {
+ const o = {};
+ const source = { p: [1] };
+ const receiver = {
+ check(first, result) {
+ expect(this).toBe(receiver);
+ expect(first).toBe("before");
+ expect(result).toBe(source);
+ return "called";
+ },
+ };
+ expect(
+ receiver.check(
+ "before",
+ ({
+ p: [...o.x],
+ } = source)
+ )
+ ).toBe("called");
+ expect(o.x).toEqual([1]);
+});
+
+test("rest elements with targets of every kind at the start of a nested array pattern", () => {
+ const key = "k";
+ const o = {};
+ class Base {
+ set x(value) {
+ this.fromSuper = value;
+ }
+ }
+ class Derived extends Base {
+ #p;
+ run() {
+ return [
+ ([[...o[key]]] = [[1]]),
+ ([[...super.x]] = [[2]]),
+ ([[...this.#p]] = [[3]]),
+ ([[...[o.a]]] = [[4]]),
+ ([[...{ [key]: o.b }]] = [[5]]),
+ ([[...o.c] = [6]] = []),
+ ([...[...o.d]] = [7]),
+ ([, [...o.e]] = [0, [8]]),
+ ].length;
+ }
+ get p() {
+ return this.#p;
+ }
+ }
+ const derived = new Derived();
+ expect(derived.run()).toBe(8);
+ expect(o.k).toEqual([1]);
+ expect(derived.fromSuper).toEqual([2]);
+ expect(derived.p).toEqual([3]);
+ expect(o.a).toBe(4);
+ expect(o.b).toBeUndefined();
+ expect(o.c).toEqual([6]);
+ expect(o.d).toEqual([7]);
+ expect(o.e).toEqual([8]);
+});