Skip to content

Commit 9b829be

Browse files
committed
iterate through tokens of alternate script without json value replacements and replace consts with lets, and .prototypes with ["prototype"], and .__proto__ with ["__proto__"]
1 parent c5a600f commit 9b829be

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

src/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ export async function processScript(script: string) {
642642
srcLength += 24
643643
}
644644

645-
const scriptBeforeJSONValueReplacement = (await minify(script, {
645+
let scriptBeforeJSONValueReplacement = (await minify(script, {
646646
ecma: 2015,
647647
compress: {
648648
passes: Infinity,
@@ -658,6 +658,38 @@ export async function processScript(script: string) {
658658
format: { semicolons: false }
659659
})).code || ""
660660

661+
{
662+
const tokens = [ ...tokenize(scriptBeforeJSONValueReplacement, { ecmaVersion: 2015 }) ].reverse().values()
663+
664+
for (const token of tokens) {
665+
// we can't replace any tokens before the block statement or we'll break stuff
666+
if (token.start < blockStatementIndex)
667+
break
668+
669+
switch (token.type) {
670+
case tokenTypes.name: {
671+
if (token.value != "prototype" && token.value != "__proto__")
672+
break
673+
674+
const tokenBefore = tokens.next().value as Token
675+
676+
if (tokenBefore.type != tokenTypes.dot)
677+
break
678+
679+
srcLength += 3
680+
scriptBeforeJSONValueReplacement = stringSplice(scriptBeforeJSONValueReplacement, `["${token.value}"]`, tokenBefore.start, token.end)
681+
} break
682+
683+
case tokenTypes._const: {
684+
scriptBeforeJSONValueReplacement = stringSplice(scriptBeforeJSONValueReplacement, "let", token.start, token.end)
685+
} break
686+
687+
case tokenTypes._this:
688+
throw new Error('"this" keyword is not supported in hackmud')
689+
}
690+
}
691+
}
692+
661693
const jsonValues: any[] = []
662694
let undefinedIsReferenced = false
663695

0 commit comments

Comments
 (0)