diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index bae5b68..920754c 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -17,6 +17,8 @@ export default function (client: ScramjetClient, self: Self) { configurable: false, }); + const scramtag_ident = "/*scramtag "; + // when we rewrite javascript it will make function.toString leak internals // this can lead to double rewrites which is bad client.Proxy("Function.prototype.toString", { @@ -27,25 +29,24 @@ export default function (client: ScramjetClient, self: Self) { // every function rewritten will have a scramtag comment // it will look like this: // function name() /*scramtag [index] [tag] */ { ... } - const scramtag_ident = "/*scramtag "; - const scramtagstart = stringified.indexOf(scramtag_ident); + const scramtagstart = stringified.indexOf("/*s"); 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 (/) const abstagindex = parseInt( - stringified - .substring(scramtagstart + scramtag_ident.length) - .split(" ")[0] + stringified.substring(scramtagstart + scramtag_ident.length, firstspace) ); // 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; const scramtagend = stringified.indexOf("*/", scramtagstart); - const tag = stringified - .substring(scramtagstart + scramtag_ident.length, scramtagend) - .split(" ")[1]; + const tag = stringified.substring(firstspace + 1, scramtagend); // delete all scramtags inside the function (and nested ones!!) stringified = stringified.replace(/\/\*scramtag.*?\*\//g, ""); @@ -54,8 +55,14 @@ export default function (client: ScramjetClient, self: Self) { let i = 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; // 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; offset += end - start - str.length; i = start - absindex + offset + str.length; + + j++; } - return ctx.return(newString + stringified.slice(i)); + newString += stringified.slice(i); + return ctx.return(newString); }, }); }