Skip to content

Commit d3b399c

Browse files
committed
[core][rewriter] add temploc destructure rewrites
1 parent 5dabc5a commit d3b399c

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

rewriter/js/src/cfg.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub struct Config {
2525
pub setrealmfn: String,
2626
pub metafn: String,
2727
pub pushsourcemapfn: String,
28+
29+
pub trysetfn: String,
30+
pub templocid: String,
2831
}
2932

3033
#[derive(Debug)]

rewriter/js/src/changes.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
4343
RebindProperty {
4444
ident: Atom<'data>,
4545
},
46+
TempVar,
4647

4748
WrapObjectAssignmentLeft {
4849
restids: Vec<Atom<'data>>,
50+
location_assigned: bool,
4951
},
5052

5153
/// insert `${cfg.setrealmfn}({}).`
@@ -132,11 +134,15 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
132134
Ty::RebindProperty { ident } => {
133135
LL::replace(transforms![&cfg.wrappropertybase, ident, ":", ident])
134136
}
135-
Ty::WrapObjectAssignmentLeft { restids } => {
137+
Ty::TempVar => LL::replace(transforms![&cfg.templocid]),
138+
Ty::WrapObjectAssignmentLeft { restids, location_assigned } => {
136139
let mut steps = String::new();
137140
for id in restids {
138141
steps.push_str(&format!("{}({}),", &cfg.cleanrestfn, id.as_str()));
139142
}
143+
if location_assigned {
144+
steps.push_str(&format!("{}(location,\"=\",{})||(location={}),", &cfg.trysetfn, &cfg.templocid, &cfg.templocid));
145+
}
140146
let steps: &'static str = Box::leak(steps.into_boxed_str());
141147
LL::insert(transforms!["((t)=>(", &steps, "t))("])
142148
}
@@ -157,7 +163,9 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
157163
Ty::ImportFn => LL::replace(transforms![&cfg.importfn, "(\"", &flags.base, "\","]),
158164
Ty::MetaFn => LL::replace(transforms![&cfg.metafn, "(\"", &flags.base, "\")"]),
159165
Ty::AssignmentLeft { name, op } => LL::replace(transforms![
160-
"((t)=>$scramjet$tryset(",
166+
"((t)=>",
167+
&cfg.trysetfn,
168+
"(",
161169
name,
162170
",\"",
163171
op,

rewriter/js/src/rewrite.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
3636
RebindProperty {
3737
ident: Atom<'data>,
3838
},
39+
// `location` -> `cfg.templocid`
40+
TempVar,
3941

4042
WrapObjectAssignment {
4143
restids: Vec<Atom<'data>>,
44+
location_assigned: bool,
4245
},
4346

4447
/// `cfg.wrapprop({})`
@@ -119,12 +122,13 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
119122
],
120123
Self::RewriteProperty { ident } => smallvec![change!(span, RewriteProperty { ident }),],
121124
Self::RebindProperty { ident } => smallvec![change!(span, RebindProperty { ident })],
125+
Self::TempVar => smallvec![change!(span, TempVar)],
122126
Self::WrapProperty => smallvec![
123127
change!(span!(start), WrapPropertyLeft),
124128
change!(span!(end), WrapPropertyRight),
125129
],
126-
Self::WrapObjectAssignment { restids } => smallvec![
127-
change!(span!(start), WrapObjectAssignmentLeft { restids }),
130+
Self::WrapObjectAssignment { restids, location_assigned } => smallvec![
131+
change!(span!(start), WrapObjectAssignmentLeft { restids, location_assigned }),
128132
change!(
129133
span!(end),
130134
ClosingParen {

rewriter/js/src/visitor.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ where
108108
&mut self,
109109
s: &ObjectAssignmentTarget<'data>,
110110
restids: &mut Vec<Atom<'data>>,
111+
location_assigned: &mut bool,
111112
) {
112113
if let Some(r) = &s.rest {
113114
// { ...rest } = self;
@@ -119,7 +120,6 @@ where
119120
}
120121
_ => panic!("what?"),
121122
}
122-
return;
123123
}
124124
for prop in &s.properties {
125125
match prop {
@@ -179,7 +179,14 @@ where
179179
walk::walk_expression(self, &d.init);
180180
}
181181
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(p) => {
182-
self.recurse_object_assignment_target(p, restids);
182+
183+
self.recurse_object_assignment_target(p, restids, location_assigned);
184+
}
185+
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(p)=>{
186+
if p.name == "location" {
187+
self.jschanges.add(rewrite!(p.span(), TempVar));
188+
*location_assigned = true;
189+
}
183190
}
184191
_ => {}
185192
}
@@ -517,11 +524,12 @@ where
517524
}
518525

519526
let mut restids: Vec<Atom<'data>> = Vec::new();
520-
self.recurse_object_assignment_target(o, &mut restids);
527+
let mut location_assigned: bool = false;
528+
self.recurse_object_assignment_target(o, &mut restids, &mut location_assigned);
521529

522-
if restids.len() > 0 {
530+
if restids.len() > 0 || location_assigned {
523531
self.jschanges
524-
.add(rewrite!(it.span, WrapObjectAssignment { restids }));
532+
.add(rewrite!(it.span, WrapObjectAssignment { restids, location_assigned }));
525533
}
526534
return;
527535
}

rewriter/native/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ pub struct RewriterOptions {
4242
#[clap(long, default_value = "$pushsourcemap")]
4343
pushsourcemapfn: String,
4444

45+
#[clap(long, default_value = "$tryset")]
46+
trysetfn: String,
47+
#[clap(long, default_value = "$temploc")]
48+
templocid: String,
49+
4550
#[clap(long, default_value = "https://google.com/glorngle/si.js")]
4651
base: String,
4752
#[clap(long, default_value = "glongle1")]

rewriter/native/src/rewriter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ impl NativeRewriter {
6060
metafn: cfg.metafn.clone(),
6161
setrealmfn: cfg.setrealmfn.clone(),
6262
pushsourcemapfn: cfg.pushsourcemapfn.clone(),
63+
trysetfn: cfg.trysetfn.clone(),
64+
templocid: cfg.templocid.clone(),
6365
},
6466
NativeUrlRewriter,
6567
);

rewriter/wasm/src/jsr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ fn get_config(scramjet: &Object) -> Result<Config> {
5858
metafn: get_str(globals, "metafn")?,
5959
setrealmfn: get_str(globals, "setrealmfn")?,
6060
pushsourcemapfn: get_str(globals, "pushsourcemapfn")?,
61+
trysetfn: get_str(globals, "trysetfn")?,
62+
templocid: get_str(globals, "templocid")?,
6163
})
6264
}
6365

src/controller/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ export class ScramjetController {
2121
wrappropertybase: "$scramjet__",
2222
wrappropertyfn: "$scramjet$prop",
2323
cleanrestfn: "$scramjet$clean",
24-
trysetfn: "$scramjet$tryset",
2524
importfn: "$scramjet$import",
2625
rewritefn: "$scramjet$rewrite",
2726
metafn: "$scramjet$meta",
2827
setrealmfn: "$scramjet$setrealm",
2928
pushsourcemapfn: "$scramjet$pushsourcemap",
29+
trysetfn: "$scramjet$tryset",
30+
templocid: "$scramjet$temploc",
3031
},
3132
files: {
3233
wasm: "/scramjet.wasm.wasm",

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ export interface ScramjetConfig {
2424
wrappropertybase: string;
2525
wrappropertyfn: string;
2626
cleanrestfn: string;
27-
trysetfn: string;
2827
importfn: string;
2928
rewritefn: string;
3029
metafn: string;
3130
setrealmfn: string;
3231
pushsourcemapfn: string;
32+
trysetfn: string;
33+
templocid: string;
3334
};
3435
files: {
3536
wasm: string;

0 commit comments

Comments
 (0)