mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
microoptimizations
This commit is contained in:
parent
bcbc14a03b
commit
35eb307d4d
1 changed files with 21 additions and 11 deletions
|
@ -17,6 +17,8 @@ export default function (client: ScramjetClient, self: Self) {
|
||||||
configurable: false,
|
configurable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const scramtag_ident = "/*scramtag ";
|
||||||
|
|
||||||
// when we rewrite javascript it will make function.toString leak internals
|
// when we rewrite javascript it will make function.toString leak internals
|
||||||
// this can lead to double rewrites which is bad
|
// this can lead to double rewrites which is bad
|
||||||
client.Proxy("Function.prototype.toString", {
|
client.Proxy("Function.prototype.toString", {
|
||||||
|
@ -27,25 +29,24 @@ export default function (client: ScramjetClient, self: Self) {
|
||||||
// every function rewritten will have a scramtag comment
|
// every function rewritten will have a scramtag comment
|
||||||
// it will look like this:
|
// it will look like this:
|
||||||
// function name() /*scramtag [index] [tag] */ { ... }
|
// function name() /*scramtag [index] [tag] */ { ... }
|
||||||
const scramtag_ident = "/*scramtag ";
|
const scramtagstart = stringified.indexOf("/*s");
|
||||||
const scramtagstart = stringified.indexOf(scramtag_ident);
|
|
||||||
|
|
||||||
if (scramtagstart === -1) return ctx.return(stringified); // it's either a native function or something stolen from scramjet itself
|
if (scramtagstart === -1) return ctx.return(stringified); // it's either a native function or something stolen from scramjet itself
|
||||||
|
|
||||||
|
const firstspace = stringified.indexOf(
|
||||||
|
" ",
|
||||||
|
scramtagstart + scramtag_ident.length
|
||||||
|
);
|
||||||
// [index] holds the index of the first character in the scramtag (/)
|
// [index] holds the index of the first character in the scramtag (/)
|
||||||
const abstagindex = parseInt(
|
const abstagindex = parseInt(
|
||||||
stringified
|
stringified.substring(scramtagstart + scramtag_ident.length, firstspace)
|
||||||
.substring(scramtagstart + scramtag_ident.length)
|
|
||||||
.split(" ")[0]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// subtracting that from the index of the scramtag gives us the starting index of the function relative to the entire file
|
// subtracting that from the index of the scramtag gives us the starting index of the function relative to the entire file
|
||||||
let absindex = abstagindex - scramtagstart;
|
let absindex = abstagindex - scramtagstart;
|
||||||
|
|
||||||
const scramtagend = stringified.indexOf("*/", scramtagstart);
|
const scramtagend = stringified.indexOf("*/", scramtagstart);
|
||||||
const tag = stringified
|
const tag = stringified.substring(firstspace + 1, scramtagend);
|
||||||
.substring(scramtagstart + scramtag_ident.length, scramtagend)
|
|
||||||
.split(" ")[1];
|
|
||||||
|
|
||||||
// delete all scramtags inside the function (and nested ones!!)
|
// delete all scramtags inside the function (and nested ones!!)
|
||||||
stringified = stringified.replace(/\/\*scramtag.*?\*\//g, "");
|
stringified = stringified.replace(/\/\*scramtag.*?\*\//g, "");
|
||||||
|
@ -54,8 +55,14 @@ export default function (client: ScramjetClient, self: Self) {
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
for (const [str, start, end] of maps) {
|
|
||||||
if (start < absindex) continue;
|
let j = 0;
|
||||||
|
while (j < maps.length) {
|
||||||
|
const [str, start, end] = maps[j];
|
||||||
|
if (start < absindex) {
|
||||||
|
j++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (start - absindex + offset > stringified.length) break;
|
if (start - absindex + offset > stringified.length) break;
|
||||||
|
|
||||||
// ooh i should really document this before i forget how it works
|
// ooh i should really document this before i forget how it works
|
||||||
|
@ -63,9 +70,12 @@ export default function (client: ScramjetClient, self: Self) {
|
||||||
newString += str;
|
newString += str;
|
||||||
offset += end - start - str.length;
|
offset += end - start - str.length;
|
||||||
i = start - absindex + offset + str.length;
|
i = start - absindex + offset + str.length;
|
||||||
|
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.return(newString + stringified.slice(i));
|
newString += stringified.slice(i);
|
||||||
|
return ctx.return(newString);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue