Skip to content

Commit 5f07a12

Browse files
committed
[core][rewriter] dpsc: implement WrapGetComputed
1 parent 3d35c62 commit 5f07a12

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

rewriter/js/src/changes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
3838
ident: Atom<'data>,
3939
enclose: bool,
4040
},
41+
WrapGetComputedLeft {
42+
enclose: bool,
43+
},
4144
WrapGetRight {
4245
enclose: bool,
4346
},
@@ -124,6 +127,11 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
124127
} else {
125128
transforms![&cfg.wrapgetbase, ident, "("]
126129
}),
130+
Ty::WrapGetComputedLeft { enclose } => LL::insert(if enclose {
131+
transforms!["(", &cfg.wrapcomputedgetfn, "("]
132+
} else {
133+
transforms![&cfg.wrapcomputedgetfn, "("]
134+
}),
127135
Ty::WrapGetRight { enclose } => LL::insert(if enclose {
128136
transforms!["))"]
129137
} else {

rewriter/js/src/rewrite.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,25 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
3333
propspan: Span,
3434
enclose: bool,
3535
},
36+
/// `window["location"]` -> cfg.wrapgetcomputed(window, "location")
37+
WrapGetComputed {
38+
leftspan: Span,
39+
propspan: Span,
40+
enclose: bool,
41+
},
3642
/// `window.location` -> cfg.wraplocation(window)
3743
WrapSet {
3844
ident: Atom<'data>,
3945
propspan: Span,
4046
leftspan: Span,
4147
rightspan: Span,
4248
},
49+
/// `cfg.wrapcomputedsetfn(window, "location", t)`
50+
WrapSetComputed {
51+
propspan: Span,
52+
leftspan: Span,
53+
rightspan: Span,
54+
},
4355
// dead code only if debug is disabled
4456
#[allow(dead_code)]
4557
/// `$scramerr(name)`
@@ -122,6 +134,16 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
122134
change!(span!(end), WrapGetRight {
123135
enclose,
124136
}),
137+
],
138+
Self::WrapGetComputed { leftspan, propspan, enclose } => smallvec![
139+
change!(span!(start), WrapGetComputedLeft {
140+
enclose
141+
}),
142+
// replace the bracket with ,
143+
change!(Span::new(leftspan.end, propspan.start), Replace { text: "," }),
144+
// replace the other bracket with )
145+
change!(Span::new(propspan.end, propspan.end + 1), ClosingParen { semi: false, replace: true }),
146+
125147
],
126148
Self::WrapSet { ident, propspan, leftspan, rightspan } => smallvec![
127149
change!(span!(start), WrapSet {
@@ -138,6 +160,7 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
138160
}
139161
)
140162
],
163+
RewriteType::WrapSetComputed { .. } => todo!(),
141164
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
142165
Self::ImportFn => smallvec![change!(span, ImportFn)],
143166
Self::MetaFn => smallvec![change!(span, MetaFn)],

rewriter/js/src/visitor.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,35 @@ where
122122
}
123123

124124
fn visit_member_expression(&mut self, it: &MemberExpression<'data>) {
125-
// TODO
126-
// you could break this with ["postMessage"] etc
127-
// however this code only exists because of recaptcha whatever
128-
// and it would slow down js execution a lot
129-
if let MemberExpression::StaticMemberExpression(s) = it {
130-
if s.property.name == "postMessage" {
131-
self.jschanges.add(rewrite!(s.property.span, SetRealmFn));
132-
133-
walk::walk_expression(self, &s.object);
134-
return; // unwise to walk the rest of the tree
135-
}
125+
match &it {
126+
MemberExpression::StaticMemberExpression(s) =>{
127+
// TODO
128+
// you could break this with ["postMessage"] etc
129+
// however this code only exists because of recaptcha whatever
130+
// and it would slow down js execution a lot
131+
if s.property.name == "postMessage" {
132+
self.jschanges.add(rewrite!(s.property.span, SetRealmFn));
133+
134+
walk::walk_expression(self, &s.object);
135+
return; // unwise to walk the rest of the tree
136+
}
136137

137-
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
138-
self.jschanges.add(rewrite!(it.span(), WrapGet {
139-
ident: s.property.name,
140-
propspan: Span::new(s.property.span.start-1, s.property.span.end),
138+
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
139+
self.jschanges.add(rewrite!(it.span(), WrapGet {
140+
ident: s.property.name,
141+
propspan: Span::new(s.property.span.start-1, s.property.span.end),
142+
enclose: false,
143+
}));
144+
}
145+
}
146+
MemberExpression::ComputedMemberExpression(s) => {
147+
self.jschanges.add(rewrite!(it.span(), WrapGetComputed {
148+
leftspan: s.object.span(),
149+
propspan: s.expression.span(),
141150
enclose: false,
142151
}));
143-
}
152+
}
153+
_=>{}
144154
// if !self.flags.strict_rewrites
145155
// && !UNSAFE_GLOBALS.contains(&s.property.name.as_str())
146156
// && let Expression::Identifier(_) | Expression::ThisExpression(_) = &s.object

0 commit comments

Comments
 (0)