Skip to content

Commit b51b6f2

Browse files
committed
[core][rewriter] dpsc: partially implement WrapAccess
1 parent be6e182 commit b51b6f2

File tree

3 files changed

+95
-32
lines changed

3 files changed

+95
-32
lines changed

rewriter/js/src/changes.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ pub(crate) use change;
3030
#[derive(Debug, PartialEq, Eq)]
3131
pub enum JsChangeType<'alloc: 'data, 'data> {
3232
/// insert `${cfg.wrapfn}(`
33-
WrapFnLeft { wrap: bool },
33+
WrapFnLeft { enclose: bool },
3434
/// insert `,strictchecker)`
35-
WrapFnRight { wrap: bool },
35+
WrapFnRight { enclose: bool },
36+
37+
WrapAccessLeft {
38+
ident: Atom<'data>,
39+
enclose: bool,
40+
},
41+
WrapAccessRight {
42+
enclose: bool,
43+
},
44+
3645
/// insert `${cfg.setrealmfn}({}).`
3746
SetRealmFn,
3847
/// insert `${cfg.wrapthis}(`
@@ -94,16 +103,27 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
94103
use JsChangeType as Ty;
95104
use TransformLL as LL;
96105
match self.ty {
97-
Ty::WrapFnLeft { wrap } => LL::insert(if wrap {
106+
Ty::WrapFnLeft { enclose } => LL::insert(if enclose {
98107
transforms!["(", &cfg.wrapfn, "("]
99108
} else {
100109
transforms![&cfg.wrapfn, "("]
101110
}),
102-
Ty::WrapFnRight { wrap } => LL::insert(if wrap {
111+
Ty::WrapFnRight { enclose } => LL::insert(if enclose {
103112
transforms![",", STRICTCHECKER, "))"]
104113
} else {
105114
transforms![",", STRICTCHECKER, ")"]
106115
}),
116+
Ty::WrapAccessLeft {
117+
ident,
118+
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+
}),
107127
Ty::SetRealmFn => LL::insert(transforms![&cfg.setrealmfn, "({})."]),
108128
Ty::WrapThisFn => LL::insert(transforms![&cfg.wrapthisfn, "("]),
109129
Ty::ScramErrFn { ident } => LL::insert(transforms!["$scramerr(", ident, ");"]),

rewriter/js/src/rewrite.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ pub(crate) use rewrite;
1717
pub(crate) enum RewriteType<'alloc: 'data, 'data> {
1818
/// `(cfg.wrapfn(ident,strictchecker))` | `cfg.wrapfn(ident,strictchecker)`
1919
WrapFn {
20-
wrap: bool,
20+
enclose: bool,
2121
},
2222
/// `cfg.setrealmfn({}).ident`
2323
SetRealmFn,
2424
/// `cfg.wrapthis(this)`
2525
WrapThisFn,
26+
2627
/// `(cfg.importfn("cfg.base"))`
2728
ImportFn,
2829
/// `cfg.metafn("cfg.base")`
2930
MetaFn,
3031

32+
/// `window.location` -> cfg.wraplocation(window)
33+
WrapAccess {
34+
ident: Atom<'data>,
35+
propspan: Span,
36+
enclose: bool,
37+
},
3138
// dead code only if debug is disabled
3239
#[allow(dead_code)]
3340
/// `$scramerr(name)`
@@ -93,9 +100,23 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
93100
}
94101

95102
match self {
96-
Self::WrapFn { wrap } => smallvec![
97-
change!(span!(start), WrapFnLeft { wrap }),
98-
change!(span!(end), WrapFnRight { wrap }),
103+
Self::WrapFn { enclose } => smallvec![
104+
change!(span!(start), WrapFnLeft { enclose }),
105+
change!(span!(end), WrapFnRight { enclose }),
106+
],
107+
Self::WrapAccess {
108+
ident,
109+
propspan,
110+
enclose,
111+
} => smallvec![
112+
change!(span!(start), WrapAccessLeft {
113+
ident,
114+
enclose,
115+
}),
116+
change!(propspan, Delete),
117+
change!(span!(end), WrapAccessRight {
118+
enclose,
119+
}),
99120
],
100121
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
101122
Self::WrapThisFn => smallvec![

rewriter/js/src/visitor.rs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ use crate::{
2323
//
2424
// maybe move this out of this lib?
2525
const UNSAFE_GLOBALS: &[&str] = &[
26-
"window",
27-
"self",
28-
"globalThis",
29-
"this",
3026
"parent",
3127
"top",
3228
"location",
33-
"document",
34-
"eval",
35-
"frames",
29+
"eval"
3630
];
3731

3832
pub struct Visitor<'alloc, 'data, E>
@@ -71,17 +65,25 @@ where
7165

7266
fn rewrite_ident(&mut self, name: &Atom, span: Span) {
7367
if UNSAFE_GLOBALS.contains(&name.as_str()) {
74-
self.jschanges.add(rewrite!(span, WrapFn { wrap: true }));
68+
self.jschanges.add(rewrite!(span, WrapFn { enclose: true }));
7569
}
7670
}
7771

7872
fn walk_member_expression(&mut self, it: &Expression) -> bool {
7973
match it {
8074
Expression::Identifier(s) => {
81-
self.rewrite_ident(&s.name, s.span);
82-
true
75+
false
8376
}
84-
Expression::StaticMemberExpression(s) => self.walk_member_expression(&s.object),
77+
Expression::StaticMemberExpression(s) => {
78+
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
79+
// self.jschanges.add(rewrite!(s.span, WrapAccess {
80+
// ident: s.property.name,
81+
// propspan: s.property.span,
82+
// }
83+
// ));
84+
}
85+
self.walk_member_expression(&s.object)
86+
},
8587
Expression::ComputedMemberExpression(s) => self.walk_member_expression(&s.object),
8688
_ => false,
8789
}
@@ -109,7 +111,7 @@ where
109111
//
110112
if UNSAFE_GLOBALS.contains(&it.name.as_str()) {
111113
self.jschanges
112-
.add(rewrite!(it.span, WrapFn { wrap: false }));
114+
.add(rewrite!(it.span, WrapFn { enclose: false }));
113115
}
114116
// }
115117
}
@@ -132,21 +134,41 @@ where
132134
return; // unwise to walk the rest of the tree
133135
}
134136

135-
if !self.flags.strict_rewrites
136-
&& !UNSAFE_GLOBALS.contains(&s.property.name.as_str())
137-
&& let Expression::Identifier(_) | Expression::ThisExpression(_) = &s.object
138-
{
139-
// cull tree - this should be safe
140-
return;
141-
}
137+
if UNSAFE_GLOBALS.contains(&s.property.name.as_str()) {
138+
self.jschanges.add(rewrite!(it.span(), WrapAccess {
139+
ident: s.property.name,
140+
propspan: Span::new(s.property.span.start-1, s.property.span.end),
141+
enclose: false,
142+
}));
143+
}
144+
// if !self.flags.strict_rewrites
145+
// && !UNSAFE_GLOBALS.contains(&s.property.name.as_str())
146+
// && let Expression::Identifier(_) | Expression::ThisExpression(_) = &s.object
147+
// {
148+
// // cull tree - this should be safe
149+
// return;
150+
// }
151+
152+
// if self.flags.scramitize
153+
// && !matches!(s.object, Expression::MetaProperty(_) | Expression::Super(_))
154+
// {
155+
// self.scramitize(s.object.span());
156+
// }
157+
}
142158

143-
if self.flags.scramitize
144-
&& !matches!(s.object, Expression::MetaProperty(_) | Expression::Super(_))
145-
{
146-
self.scramitize(s.object.span());
147-
}
159+
match &it.object() {
160+
Expression::Identifier(_) => {
161+
return;
162+
}
163+
Expression::ThisExpression(_)=> {
164+
// this is safe, we don't need to walk it
165+
return;
166+
}
167+
_=>{}
148168
}
149169

170+
171+
150172
walk::walk_member_expression(self, it);
151173
}
152174
fn visit_this_expression(&mut self, it: &ThisExpression) {

0 commit comments

Comments
 (0)