Skip to content

Commit

Permalink
[analyze] Do not try to resolve property names as ids during analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans-Halverson committed Feb 3, 2024
1 parent da36d01 commit 333ac6a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 11 deletions.
21 changes: 19 additions & 2 deletions src/js/parser/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,12 @@ impl<'a> AstVisitor for Analyzer<'a> {
self.visit_private_name_use(&expr.property);
}

default_visit_member_expression(self, expr);
self.visit_expression(&mut expr.object);

// Visit computed property expressions, but not id used as named property
if expr.is_computed {
self.visit_expression(&mut expr.property);
}
}

fn visit_binary_expression(&mut self, expr: &mut BinaryExpression) {
Expand Down Expand Up @@ -488,7 +493,10 @@ impl<'a> AstVisitor for Analyzer<'a> {
self.emit_error(prop.loc, ParseError::InvalidPatternInitializer);
}

self.visit_expression(&mut prop.key);
// Visit key expression for computed and shorthand properties, but not id of named property
if prop.is_computed || prop.value.is_none() {
self.visit_expression(&mut prop.key);
}

if prop.is_method {
let func = if let Some(Expression::Function(func)) = prop.value.as_deref_mut() {
Expand Down Expand Up @@ -528,6 +536,15 @@ impl<'a> AstVisitor for Analyzer<'a> {
}
}

fn visit_object_pattern_property(&mut self, prop: &mut ObjectPatternProperty) {
// Visit pattern for computed properties, but not id of named property
if prop.is_computed {
visit_opt!(self, prop.key, visit_expression);
}

self.visit_pattern(&mut prop.value);
}

fn visit_meta_property(&mut self, expr: &mut MetaProperty) {
match expr.kind {
MetaPropertyKind::NewTarget => {
Expand Down
17 changes: 15 additions & 2 deletions tests/js_bytecode/expression/member.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
3: StoreGlobal r0, c1
6: NewClosure r0, c2
9: StoreGlobal r0, c3
12: LoadUndefined r0
14: Ret r0
12: NewClosure r0, c4
15: StoreGlobal r0, c5
18: LoadUndefined r0
20: Ret r0
Constant Table:
0: [BytecodeFunction: testNamedMember]
1: [String: testNamedMember]
2: [BytecodeFunction: testComputedMember]
3: [String: testComputedMember]
4: [BytecodeFunction: propertyNamesNotResolved]
5: [String: propertyNamesNotResolved]
}

[BytecodeFunction: testNamedMember] {
Expand Down Expand Up @@ -40,3 +44,12 @@
Constant Table:
0: [String: foo]
}

[BytecodeFunction: propertyNamesNotResolved] {
Parameters: 1, Registers: 2
0: GetNamedProperty r0, a0, c0
4: LoadUndefined r1
6: Ret r1
Constant Table:
0: [String: a]
}
5 changes: 5 additions & 0 deletions tests/js_bytecode/expression/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ function testNamedMember(x) {
function testComputedMember(x) {
x['foo'];
(1 + 2)[3 + 4];
}

function propertyNamesNotResolved(x) {
// No TDZ check needed
const a = x.a;
}
38 changes: 33 additions & 5 deletions tests/js_bytecode/expression/object/basic.exp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
27: StoreGlobal r0, c9
30: NewClosure r0, c10
33: StoreGlobal r0, c11
36: LoadImmediate r0, 0
39: StoreGlobal r0, c12
42: LoadUndefined r0
44: Ret r0
36: NewClosure r0, c12
39: StoreGlobal r0, c13
42: LoadImmediate r0, 0
45: StoreGlobal r0, c14
48: LoadUndefined r0
50: Ret r0
Constant Table:
0: [BytecodeFunction: emptyObject]
1: [String: emptyObject]
Expand All @@ -29,7 +31,9 @@
9: [String: computedPropertyKeys]
10: [BytecodeFunction: spread]
11: [String: spread]
12: [String: global]
12: [BytecodeFunction: propertyNamesNotResolved]
13: [String: propertyNamesNotResolved]
14: [String: global]
}

[BytecodeFunction: emptyObject] {
Expand Down Expand Up @@ -110,3 +114,27 @@
17: CopyDataProperties r0, r1, r1, 0
22: Ret r0
}

[BytecodeFunction: propertyNamesNotResolved] {
Parameters: 0, Registers: 5
0: LoadEmpty r1
2: LoadEmpty r2
4: NewObject r3
6: LoadImmediate r4, 1
9: DefineNamedProperty r3, c0, r4
13: GetNamedProperty r0, r3, c0
17: NewObject r3
19: CheckTdz r1, c1
22: DefineNamedProperty r3, c1, r1
26: CheckTdz r2, c3
29: DefineNamedProperty r3, c2, r2
33: GetNamedProperty r1, r3, c1
37: GetNamedProperty r2, r3, c3
41: LoadUndefined r3
43: Ret r3
Constant Table:
0: [String: a]
1: [String: b]
2: [String: d]
3: [String: c]
}
8 changes: 8 additions & 0 deletions tests/js_bytecode/expression/object/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ function computedPropertyKeys() {

function spread(a) {
return { ...a, ...(1 + 2) };
}

function propertyNamesNotResolved() {
// No TDZ check needed
const { a } = { a: 1 };

// TDZ check needed
const { b, c } = { b, d: c };
}
38 changes: 36 additions & 2 deletions tests/js_bytecode/pattern/object_destructuring.exp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
33: StoreGlobal r0, c11
36: NewClosure r0, c12
39: StoreGlobal r0, c13
42: LoadUndefined r0
44: Ret r0
42: NewClosure r0, c14
45: StoreGlobal r0, c15
48: NewClosure r0, c16
51: StoreGlobal r0, c17
54: LoadUndefined r0
56: Ret r0
Constant Table:
0: [BytecodeFunction: shorthand]
1: [String: shorthand]
Expand All @@ -31,6 +35,10 @@
11: [String: namedExpression]
12: [BytecodeFunction: rest]
13: [String: rest]
14: [BytecodeFunction: propertyNamesNotResolved]
15: [String: propertyNamesNotResolved]
16: [BytecodeFunction: tdzWithinDestructuring]
17: [String: tdzWithinDestructuring]
}

[BytecodeFunction: shorthand] {
Expand Down Expand Up @@ -199,3 +207,29 @@
0: [String: a]
1: [String: b]
}

[BytecodeFunction: propertyNamesNotResolved] {
Parameters: 0, Registers: 4
0: LoadImmediate r2, 1
3: GetNamedProperty r0, r2, c0
7: GetNamedProperty r3, r2, c1
11: GetNamedProperty r1, r3, c0
15: LoadUndefined r2
17: Ret r2
Constant Table:
0: [String: a]
1: [String: b]
}

[BytecodeFunction: tdzWithinDestructuring] {
Parameters: 0, Registers: 3
0: LoadEmpty r0
2: LoadImmediate r2, 1
5: GetNamedProperty r0, r2, c0
9: CheckTdz r0, c0
12: GetProperty r1, r2, r0
16: LoadUndefined r2
18: Ret r2
Constant Table:
0: [String: a]
}
10 changes: 10 additions & 0 deletions tests/js_bytecode/pattern/object_destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ function rest(x) {
var { ...a } = x;
var { a, b, ...e } = x;
var { a, [1]: b, [2]: c, ...d } = x;
}

function propertyNamesNotResolved() {
// No TDZ check needed
const { a, b: { a: c } } = 1;
}

function tdzWithinDestructuring() {
// TDZ check needed
const { a, [a]: b } = 1;
}

0 comments on commit 333ac6a

Please sign in to comment.