Skip to content

Commit 3d35c62

Browse files
committed
[core][rewriter] dpsc: implement WrapSet and remove WrapThis
1 parent b51b6f2 commit 3d35c62

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

rewriter/js/src/cfg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ pub struct Config {
1717
pub prefix: String,
1818

1919
pub wrapfn: String,
20-
pub wrapthisfn: String,
20+
pub wrapgetbase: String,
21+
pub wrapsetbase: String,
22+
pub wrapcomputedgetfn: String,
23+
pub wrapcomputedsetfn: String,
2124
pub importfn: String,
2225
pub rewritefn: String,
2326
pub setrealmfn: String,

rewriter/js/src/changes.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
3434
/// insert `,strictchecker)`
3535
WrapFnRight { enclose: bool },
3636

37-
WrapAccessLeft {
37+
WrapGetLeft {
3838
ident: Atom<'data>,
3939
enclose: bool,
4040
},
41-
WrapAccessRight {
41+
WrapGetRight {
4242
enclose: bool,
4343
},
4444

45+
WrapSet {
46+
ident: Atom<'data>,
47+
propspan: Span
48+
},
49+
4550
/// insert `${cfg.setrealmfn}({}).`
4651
SetRealmFn,
47-
/// insert `${cfg.wrapthis}(`
48-
WrapThisFn,
4952
/// insert `$scramerr(ident);`
5053
ScramErrFn { ident: Atom<'data> },
5154
/// insert `$scramitize(`
@@ -113,19 +116,25 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
113116
} else {
114117
transforms![",", STRICTCHECKER, ")"]
115118
}),
116-
Ty::WrapAccessLeft {
119+
Ty::WrapGetLeft {
117120
ident,
118121
enclose,
119-
} => if enclose {
120-
LL::insert(transforms!["(", "$scramjet$wrap", ident, "("])
121-
} else {LL::insert(transforms!["$scramjet$wrap", ident, "("])},
122-
Ty::WrapAccessRight { enclose } => LL::insert(if enclose {
123-
transforms!["))"]
124-
} else {
125-
transforms![")"]
126-
}),
122+
} => LL::insert(if enclose {
123+
transforms!["(", &cfg.wrapgetbase, ident, "("]
124+
} else {
125+
transforms![&cfg.wrapgetbase, ident, "("]
126+
}),
127+
Ty::WrapGetRight { enclose } => LL::insert(if enclose {
128+
transforms!["))"]
129+
} else {
130+
transforms![")"]
131+
}),
132+
Ty::WrapSet { ident, propspan } => LL::insert(transforms![
133+
&cfg.wrapsetbase,
134+
ident,
135+
"("
136+
]),
127137
Ty::SetRealmFn => LL::insert(transforms![&cfg.setrealmfn, "({})."]),
128-
Ty::WrapThisFn => LL::insert(transforms![&cfg.wrapthisfn, "("]),
129138
Ty::ScramErrFn { ident } => LL::insert(transforms!["$scramerr(", ident, ");"]),
130139
Ty::ScramitizeFn => LL::insert(transforms![" $scramitize("]),
131140
Ty::EvalRewriteFn => LL::replace(transforms!["eval(", &cfg.rewritefn, "("]),

rewriter/js/src/rewrite.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,25 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
2121
},
2222
/// `cfg.setrealmfn({}).ident`
2323
SetRealmFn,
24-
/// `cfg.wrapthis(this)`
25-
WrapThisFn,
2624

2725
/// `(cfg.importfn("cfg.base"))`
2826
ImportFn,
2927
/// `cfg.metafn("cfg.base")`
3028
MetaFn,
3129

3230
/// `window.location` -> cfg.wraplocation(window)
33-
WrapAccess {
31+
WrapGet {
3432
ident: Atom<'data>,
3533
propspan: Span,
3634
enclose: bool,
3735
},
36+
/// `window.location` -> cfg.wraplocation(window)
37+
WrapSet {
38+
ident: Atom<'data>,
39+
propspan: Span,
40+
leftspan: Span,
41+
rightspan: Span,
42+
},
3843
// dead code only if debug is disabled
3944
#[allow(dead_code)]
4045
/// `$scramerr(name)`
@@ -104,31 +109,36 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
104109
change!(span!(start), WrapFnLeft { enclose }),
105110
change!(span!(end), WrapFnRight { enclose }),
106111
],
107-
Self::WrapAccess {
112+
Self::WrapGet {
108113
ident,
109114
propspan,
110115
enclose,
111116
} => smallvec![
112-
change!(span!(start), WrapAccessLeft {
117+
change!(span!(start), WrapGetLeft {
113118
ident,
114119
enclose,
115120
}),
116121
change!(propspan, Delete),
117-
change!(span!(end), WrapAccessRight {
122+
change!(span!(end), WrapGetRight {
118123
enclose,
119124
}),
120125
],
121-
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
122-
Self::WrapThisFn => smallvec![
123-
change!(span!(start), WrapThisFn),
124-
change!(
126+
Self::WrapSet { ident, propspan, leftspan, rightspan } => smallvec![
127+
change!(span!(start), WrapSet {
128+
ident,
129+
propspan,
130+
}),
131+
change!(propspan, Delete),
132+
change!(Span::new(leftspan.end, rightspan.start), Replace { text: "," }),
133+
change!(
125134
span!(end),
126135
ClosingParen {
127136
semi: false,
128-
replace: false
137+
replace: true
129138
}
130-
),
131-
],
139+
)
140+
],
141+
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
132142
Self::ImportFn => smallvec![change!(span, ImportFn)],
133143
Self::MetaFn => smallvec![change!(span, MetaFn)],
134144
Self::ScramErr { ident } => smallvec![change!(span!(end), ScramErrFn { ident })],

rewriter/js/src/visitor.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ where
135135
}
136136

137137
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
138-
self.jschanges.add(rewrite!(it.span(), WrapAccess {
138+
self.jschanges.add(rewrite!(it.span(), WrapGet {
139139
ident: s.property.name,
140140
propspan: Span::new(s.property.span.start-1, s.property.span.end),
141141
enclose: false,
@@ -171,9 +171,6 @@ where
171171

172172
walk::walk_member_expression(self, it);
173173
}
174-
fn visit_this_expression(&mut self, it: &ThisExpression) {
175-
self.jschanges.add(rewrite!(it.span, WrapThisFn));
176-
}
177174

178175
fn visit_debugger_statement(&mut self, it: &DebuggerStatement) {
179176
// delete debugger statements entirely. some sites will spam debugger as an anti-debugging measure, and we don't want that!
@@ -291,9 +288,9 @@ where
291288
walk::walk_unary_expression(self, it);
292289
}
293290

294-
fn visit_update_expression(&mut self, _it: &UpdateExpression<'data>) {
295-
// then no, don't walk it, we don't care
296-
}
291+
// fn visit_update_expression(&mut self, _it: &UpdateExpression<'data>) {
292+
// // this is like a ++ or -- operator
293+
// }
297294

298295
fn visit_meta_property(&mut self, it: &MetaProperty<'data>) {
299296
if it.meta.name == "import" {
@@ -319,6 +316,19 @@ where
319316
return;
320317
}
321318
}
319+
AssignmentTarget::StaticMemberExpression(s) =>{
320+
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
321+
self.jschanges.add(rewrite!(it.span, WrapSet {
322+
ident: s.property.name,
323+
propspan: Span::new(s.property.span.start-1, s.property.span.end),
324+
leftspan: s.span(),
325+
rightspan: it.right.span(),
326+
}));
327+
}
328+
329+
// more to walk
330+
walk::walk_expression(self, &s.object);
331+
}
322332
AssignmentTarget::ArrayAssignmentTarget(_) => {
323333
// [location] = ["https://example.com"]
324334
// this is such a ridiculously specific edge case. just ignore it

0 commit comments

Comments
 (0)