scramitizer

This commit is contained in:
velzie 2024-10-10 17:55:01 -04:00
parent c412959a5f
commit 118610cc99
6 changed files with 41 additions and 2 deletions

View file

@ -63,6 +63,7 @@ fn get_config(scramjet: &Object) -> Config {
do_sourcemaps: get_bool(flags, "sourcemaps"),
capture_errors: get_bool(flags, "captureErrors"),
scramitize: get_bool(flags, "scramitize"),
}
}

View file

@ -123,6 +123,7 @@ fn dorewrite(source_text: &str) -> String {
pushsourcemapfn: "$pushsourcemap".to_string(),
capture_errors: true,
do_sourcemaps: true,
scramitize: false,
},
)
.as_slice(),

View file

@ -51,6 +51,7 @@ pub struct Config {
pub encode: EncodeFn,
pub capture_errors: bool,
pub scramitize: bool,
pub do_sourcemaps: bool,
}
@ -127,6 +128,21 @@ impl<'a> Visit<'a> for Rewriter {
return; // unwise to walk the rest of the tree
}
if self.config.scramitize
&& !matches!(s.object, Expression::MetaProperty(_))
&& !matches!(s.object, Expression::Super(_))
{
let span = expression_span(&s.object);
self.jschanges.push(JsChange::GenericChange {
span: Span::new(span.start, span.start),
text: format!(" $scramitize("),
});
self.jschanges.push(JsChange::GenericChange {
span: Span::new(span.end, span.end),
text: format!(")"),
});
}
}
_ => {
// TODO
@ -135,6 +151,7 @@ impl<'a> Visit<'a> for Rewriter {
// and it would slow down js execution a lot
}
}
walk::walk_member_expression(self, it);
}
fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) {
@ -173,6 +190,16 @@ impl<'a> Visit<'a> for Rewriter {
return;
}
}
if self.config.scramitize {
self.jschanges.push(JsChange::GenericChange {
span: Span::new(it.span.start, it.span.start),
text: format!(" $scramitize("),
});
self.jschanges.push(JsChange::GenericChange {
span: Span::new(it.span.end, it.span.end),
text: format!(")"),
});
}
walk::walk_call_expression(self, it);
}

View file

@ -51,6 +51,7 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
};
}
export const order = 4;
export default function (client: ScramjetClient, self: typeof globalThis) {
// the main magic of the proxy. all attempts to access any "banned objects" will be redirected here, and instead served a proxy object
// this contrasts from how other proxies will leave the root object alone and instead attempt to catch every member access
@ -61,6 +62,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
configurable: false,
});
self.$scramitize = function (v) {
if (typeof v === "string" && v.includes("scramjet")) {
debugger;
}
return v;
};
// location = "..." can't be rewritten as wrapfn(location) = ..., so instead it will actually be rewritten as
// ((t)=>$scramjet$tryset(location,"+=",t)||location+=t)(...);
// it has to be a discrete function because there's always the possibility that "location" is a local variable

View file

@ -32,7 +32,8 @@ export class ScramjetController {
naiiveRewriter: false,
captureErrors: true,
syncxhr: false,
cleanerrors: false,
cleanerrors: true,
scramitize: false,
sourcemaps: true,
},
};

1
src/types.d.ts vendored
View file

@ -24,6 +24,7 @@ type ScramjetFlags = {
naiiveRewriter: boolean;
captureErrors: boolean;
cleanerrors: boolean;
scramitize: boolean;
sourcemaps: boolean;
syncxhr: boolean;
};