esoteric performance golfing

This commit is contained in:
velzie 2024-10-21 20:29:33 -04:00
parent 040ef7b163
commit 0f603a6d03
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
6 changed files with 33 additions and 37 deletions

View file

@ -66,6 +66,7 @@ fn get_config(scramjet: &Object, url: &str) -> Result<Config> {
encode: create_encode_function(get_obj(codec, "encode")?)?, encode: create_encode_function(get_obj(codec, "encode")?)?,
wrapfn: get_str(globals, "wrapfn")?, wrapfn: get_str(globals, "wrapfn")?,
wrapthisfn: get_str(globals, "wrapthisfn")?,
importfn: get_str(globals, "importfn")?, importfn: get_str(globals, "importfn")?,
rewritefn: get_str(globals, "rewritefn")?, rewritefn: get_str(globals, "rewritefn")?,
metafn: get_str(globals, "metafn")?, metafn: get_str(globals, "metafn")?,

View file

@ -118,6 +118,7 @@ fn dorewrite(source_text: &str) -> Result<String> {
prefix: "/scrammedjet/".to_string(), prefix: "/scrammedjet/".to_string(),
encode: Box::new(encode_string), encode: Box::new(encode_string),
wrapfn: "$wrap".to_string(), wrapfn: "$wrap".to_string(),
wrapthisfn: "$gwrap".to_string(),
importfn: "$import".to_string(), importfn: "$import".to_string(),
rewritefn: "$rewrite".to_string(), rewritefn: "$rewrite".to_string(),
metafn: "$meta".to_string(), metafn: "$meta".to_string(),
@ -126,7 +127,7 @@ fn dorewrite(source_text: &str) -> Result<String> {
capture_errors: true, capture_errors: true,
do_sourcemaps: true, do_sourcemaps: true,
scramitize: false, scramitize: false,
strict_rewrites: false, strict_rewrites: true,
}, },
)? )?
.as_slice(), .as_slice(),

View file

@ -44,6 +44,7 @@ pub struct Config {
pub prefix: String, pub prefix: String,
pub wrapfn: String, pub wrapfn: String,
pub wrapthisfn: String,
pub importfn: String, pub importfn: String,
pub rewritefn: String, pub rewritefn: String,
pub setrealmfn: String, pub setrealmfn: String,
@ -171,7 +172,7 @@ impl<'a> Visit<'a> for Rewriter {
fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) { fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) {
self.jschanges.push(JsChange::GenericChange { self.jschanges.push(JsChange::GenericChange {
span: it.span, span: it.span,
text: format!("{}(this)", self.config.wrapfn), text: format!("{}(this)", self.config.wrapthisfn),
}); });
} }

View file

@ -6,42 +6,25 @@ import { config } from "../../shared";
// import { indirectEval } from "./eval"; // import { indirectEval } from "./eval";
export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
return function (identifier: any, args: any) { return function (identifier: any) {
if (iswindow && identifier instanceof self.Window) { if (identifier === self.location) {
return client.globalProxy;
} else if (iswindow && identifier instanceof self.parent.self.Window) {
if (SCRAMJETCLIENT in self.parent.self) {
// ... then we're in a subframe, and the parent frame is also in a proxy context, so we should return its proxy
return self.parent.self[SCRAMJETCLIENT].globalProxy;
} else {
// ... then we should pretend we aren't nested and return the current window
return client.globalProxy;
}
} else if (iswindow && identifier instanceof self.top.self.Window) {
// instead of returning top, we need to return the uppermost parent that's inside a scramjet context
let current = self.self;
for (;;) {
const test = current.parent.self;
if (test === current) break; // there is no parent, actual or emulated.
// ... then `test` represents a window outside of the proxy context, and therefore `current` is the topmost window in the proxy context
if (!(SCRAMJETCLIENT in test)) break;
// test is also insde a proxy, so we should continue up the chain
current = test;
}
return current[SCRAMJETCLIENT].globalProxy.window;
} else if (
(iswindow && identifier instanceof self.Location) ||
(isworker && identifier instanceof self.WorkerLocation)
) {
return client.locationProxy; return client.locationProxy;
} else if (iswindow && identifier instanceof self.Document) { }
return client.documentProxy;
} else if (isworker && identifier instanceof self.WorkerGlobalScope) { if (iswindow) {
return client.globalProxy; if (identifier === self.parent) {
if (SCRAMJETCLIENT in self.parent.self) {
// ... then we're in a subframe, and the parent frame is also in a proxy context, so we should return its proxy
return self.parent.self[SCRAMJETCLIENT].globalProxy;
} else {
// ... then we should pretend we aren't nested and return the current window
return client.globalProxy;
}
} else if (identifier === self.document) {
return client.documentProxy;
}
// TODO .top
} }
return identifier; return identifier;
@ -58,6 +41,14 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
writable: false, writable: false,
configurable: false, configurable: false,
}); });
Object.defineProperty(self, config.globals.wrapthisfn, {
value: function (i) {
if (i === self) return client.globalProxy;
return i;
},
writable: false,
configurable: false,
});
self.$scramitize = function (v) { self.$scramitize = function (v) {
if (typeof v === "string" && v.includes("scramjet")) { if (typeof v === "string" && v.includes("scramjet")) {

View file

@ -11,6 +11,7 @@ export class ScramjetController {
prefix: "/scramjet/", prefix: "/scramjet/",
globals: { globals: {
wrapfn: "$scramjet$wrap", wrapfn: "$scramjet$wrap",
wrapthisfn: "$scramjet$wrapthis",
trysetfn: "$scramjet$tryset", trysetfn: "$scramjet$tryset",
importfn: "$scramjet$import", importfn: "$scramjet$import",
rewritefn: "$scramjet$rewrite", rewritefn: "$scramjet$rewrite",

1
src/types.d.ts vendored
View file

@ -38,6 +38,7 @@ interface ScramjetConfig {
prefix: string; prefix: string;
globals: { globals: {
wrapfn: string; wrapfn: string;
wrapthisfn: string;
trysetfn: string; trysetfn: string;
importfn: string; importfn: string;
rewritefn: string; rewritefn: string;