Skip to content

Commit c955dfb

Browse files
velzier58Playz
andauthored
[core] Replace proxy-based rewriter with DPSC approach (#4)
* [core][rewriter] dpsc: partially implement WrapAccess * [core][rewriter] dpsc: implement WrapSet and remove WrapThis * [core][rewriter] dpsc: implement WrapGetComputed * [core][rewriter] dpsc: fix nested WrapGet s * [core][rewriter] dpsc: implement WrapSetComputed * [core][rewriter] dpsc: fix nested computed gets * [core][rewriter] dspc: change eval() from a replace rewrite to an insert rewrite to fix nesting issues * [core][rewriter] dpsc: nuke strictchecker * [core][rewriter] fix dpsc wrapget (#3) * fmt * fix * [core] add dpsc types to config * [core][rewriter] dpsc: fix bad rewrite with wrapcomputedgetleft * [core][rewriter] dpsc: switch from computed wraps to dynamic property wraps * [core] add dpsc globals * [core] NUKE GLOBALPROXY * [core] dpsc: proxy location for document.location too * [core][rewriter] dpsc: fix eval rewrite * [core][rewriter] dpsc: handle the comma operator correctly in wrapped properties * [core] dpsc: oops --------- Co-authored-by: Toshit Chawda <[email protected]>
1 parent 4524f38 commit c955dfb

File tree

17 files changed

+285
-508
lines changed

17 files changed

+285
-508
lines changed

rewriter/js/src/cfg.rs

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

1919
pub wrapfn: String,
20-
pub wrapthisfn: String,
20+
pub wrappropertybase: String,
21+
pub wrappropertyfn: String,
2122
pub importfn: String,
2223
pub rewritefn: String,
2324
pub setrealmfn: String,

rewriter/js/src/changes.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ use crate::{
1717
rewrite::Rewrite,
1818
};
1919

20-
// const STRICTCHECKER: &str = "(function(a){arguments[0]=false;return a})(true)";
21-
const STRICTCHECKER: &str = "(function(){return!this;})()";
22-
2320
macro_rules! change {
2421
($span:expr, $($ty:tt)*) => {
2522
$crate::changes::JsChange::new($span, $crate::changes::JsChangeType::$($ty)*)
@@ -30,21 +27,34 @@ pub(crate) use change;
3027
#[derive(Debug, PartialEq, Eq)]
3128
pub enum JsChangeType<'alloc: 'data, 'data> {
3229
/// insert `${cfg.wrapfn}(`
33-
WrapFnLeft { wrap: bool },
34-
/// insert `,strictchecker)`
35-
WrapFnRight { wrap: bool },
30+
WrapFnLeft {
31+
enclose: bool,
32+
},
33+
/// insert `)`
34+
WrapFnRight {
35+
enclose: bool,
36+
},
37+
38+
WrapPropertyLeft,
39+
WrapPropertyRight,
40+
RewriteProperty {
41+
ident: Atom<'data>,
42+
},
43+
3644
/// insert `${cfg.setrealmfn}({}).`
3745
SetRealmFn,
38-
/// insert `${cfg.wrapthis}(`
39-
WrapThisFn,
4046
/// insert `$scramerr(ident);`
41-
ScramErrFn { ident: Atom<'data> },
47+
ScramErrFn {
48+
ident: Atom<'data>,
49+
},
4250
/// insert `$scramitize(`
4351
ScramitizeFn,
4452
/// insert `eval(${cfg.rewritefn}(`
4553
EvalRewriteFn,
4654
/// insert `: ${cfg.wrapfn}(ident)`
47-
ShorthandObj { ident: Atom<'data> },
55+
ShorthandObj {
56+
ident: Atom<'data>,
57+
},
4858
/// insert scramtag
4959
SourceTag,
5060

@@ -59,10 +69,15 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
5969
},
6070

6171
/// insert `)`
62-
ClosingParen { semi: bool, replace: bool },
72+
ClosingParen {
73+
semi: bool,
74+
replace: bool,
75+
},
6376

6477
/// replace span with text
65-
Replace { text: &'alloc str },
78+
Replace {
79+
text: &'alloc str,
80+
},
6681
/// replace span with ""
6782
Delete,
6883
}
@@ -91,24 +106,28 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
91106
(cfg, flags): &Self::ToLowLevelData,
92107
offset: i32,
93108
) -> TransformLL<'data> {
109+
dbg!(&&self);
94110
use JsChangeType as Ty;
95111
use TransformLL as LL;
96112
match self.ty {
97-
Ty::WrapFnLeft { wrap } => LL::insert(if wrap {
113+
Ty::WrapFnLeft { enclose } => LL::insert(if enclose {
98114
transforms!["(", &cfg.wrapfn, "("]
99115
} else {
100116
transforms![&cfg.wrapfn, "("]
101117
}),
102-
Ty::WrapFnRight { wrap } => LL::insert(if wrap {
103-
transforms![",", STRICTCHECKER, "))"]
118+
Ty::WrapFnRight { enclose } => LL::insert(if enclose {
119+
transforms!["))"]
104120
} else {
105-
transforms![",", STRICTCHECKER, ")"]
121+
transforms![")"]
106122
}),
123+
Ty::WrapPropertyLeft => LL::insert(transforms![&cfg.wrappropertyfn, "(("]),
124+
Ty::WrapPropertyRight => LL::insert(transforms!["))"]),
125+
Ty::RewriteProperty { ident } => LL::replace(transforms![&cfg.wrappropertybase,ident]),
126+
107127
Ty::SetRealmFn => LL::insert(transforms![&cfg.setrealmfn, "({})."]),
108-
Ty::WrapThisFn => LL::insert(transforms![&cfg.wrapthisfn, "("]),
109128
Ty::ScramErrFn { ident } => LL::insert(transforms!["$scramerr(", ident, ");"]),
110129
Ty::ScramitizeFn => LL::insert(transforms![" $scramitize("]),
111-
Ty::EvalRewriteFn => LL::replace(transforms!["eval(", &cfg.rewritefn, "("]),
130+
Ty::EvalRewriteFn => LL::insert(transforms![&cfg.rewritefn, "("]),
112131
Ty::ShorthandObj { ident } => {
113132
LL::insert(transforms![":", &cfg.wrapfn, "(", ident, ")"])
114133
}
@@ -164,6 +183,8 @@ impl Ord for JsChange<'_, '_> {
164183
Ordering::Equal => match (&self.ty, &other.ty) {
165184
(Ty::ScramErrFn { .. }, _) => Ordering::Less,
166185
(_, Ty::ScramErrFn { .. }) => Ordering::Greater,
186+
(Ty::WrapFnRight { .. }, _) => Ordering::Less,
187+
(_, Ty::WrapFnRight { .. }) => Ordering::Greater,
167188
_ => Ordering::Equal,
168189
},
169190
x => x,

rewriter/js/src/rewrite.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ pub(crate) use rewrite;
1515

1616
#[derive(Debug, PartialEq, Eq)]
1717
pub(crate) enum RewriteType<'alloc: 'data, 'data> {
18-
/// `(cfg.wrapfn(ident,strictchecker))` | `cfg.wrapfn(ident,strictchecker)`
18+
/// `(cfg.wrapfn(ident))` | `cfg.wrapfn(ident)`
1919
WrapFn {
20-
wrap: bool,
20+
enclose: bool,
2121
},
2222
/// `cfg.setrealmfn({}).ident`
2323
SetRealmFn,
24-
/// `cfg.wrapthis(this)`
25-
WrapThisFn,
24+
2625
/// `(cfg.importfn("cfg.base"))`
2726
ImportFn,
2827
/// `cfg.metafn("cfg.base")`
2928
MetaFn,
3029

30+
RewriteProperty {
31+
ident: Atom<'data>,
32+
},
33+
WrapProperty,
34+
3135
// dead code only if debug is disabled
3236
#[allow(dead_code)]
3337
/// `$scramerr(name)`
@@ -60,6 +64,7 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
6064
Delete,
6165
}
6266

67+
#[derive(Debug)]
6368
pub(crate) struct Rewrite<'alloc, 'data> {
6469
span: Span,
6570
ty: RewriteType<'alloc, 'data>,
@@ -77,6 +82,8 @@ impl<'alloc: 'data, 'data> Rewrite<'alloc, 'data> {
7782

7883
impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
7984
fn into_inner(self, span: Span) -> SmallVec<[JsChange<'alloc, 'data>; 2]> {
85+
86+
dbg!(&self);
8087
macro_rules! span {
8188
(start) => {
8289
Span::new(span.start, span.start)
@@ -90,24 +97,24 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
9097
($span1:ident $span2:ident end) => {
9198
Span::new($span1.end, $span2.end)
9299
};
100+
($span1:ident $span2:ident between) => {
101+
Span::new($span1.end, $span2.start)
102+
};
93103
}
94104

95105
match self {
96-
Self::WrapFn { wrap } => smallvec![
97-
change!(span!(start), WrapFnLeft { wrap }),
98-
change!(span!(end), WrapFnRight { wrap }),
106+
Self::WrapFn { enclose } => smallvec![
107+
change!(span!(start), WrapFnLeft { enclose }),
108+
change!(span!(end), WrapFnRight { enclose }),
99109
],
110+
Self::RewriteProperty { ident } => smallvec![
111+
change!(span, RewriteProperty { ident }),
112+
],
113+
Self::WrapProperty => smallvec![
114+
change!(span!(start), WrapPropertyLeft),
115+
change!(span!(end), WrapPropertyRight),
116+
],
100117
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
101-
Self::WrapThisFn => smallvec![
102-
change!(span!(start), WrapThisFn),
103-
change!(
104-
span!(end),
105-
ClosingParen {
106-
semi: false,
107-
replace: false
108-
}
109-
),
110-
],
111118
Self::ImportFn => smallvec![change!(span, ImportFn)],
112119
Self::MetaFn => smallvec![change!(span, MetaFn)],
113120
Self::ScramErr { ident } => smallvec![change!(span!(end), ScramErrFn { ident })],
@@ -122,12 +129,12 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
122129
)
123130
],
124131
Self::Eval { inner } => smallvec![
125-
change!(span!(span inner start), EvalRewriteFn),
132+
change!(Span::new(inner.start, inner.start), EvalRewriteFn),
126133
change!(
127-
span!(inner span end),
134+
Span::new(inner.end, inner.end),
128135
ClosingParen {
129136
semi: false,
130-
replace: true
137+
replace: false,
131138
}
132139
)
133140
],

0 commit comments

Comments
 (0)