Skip to content

Commit 1c7a732

Browse files
Jake ChampionJakeChampion
authored andcommitted
refactor: have the precompile function take in the full application code and always be called from compileApplicationToWasm
This makes it simpler to test the regex precompilation functionality and makes it simpler to add more precompilation phases in the future
1 parent 5a804bf commit 1c7a732

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/compileApplicationToWasm.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ const PREAMBLE = `;{
3131
const precompile = (r) => { r.exec('a'); r.exec('\\u1000'); };`;
3232
const POSTAMBLE = "}";
3333

34+
// TODO: This should also detect and update sourcemaps if they are present, otherwise the sourcemaps would be incorrect.
35+
//
3436
/// Emit a block of javascript that will pre-compile the regular expressions given. As spidermonkey
3537
/// will intern regular expressions, duplicating them at the top level and testing them with both
3638
/// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch
3739
/// handler.
38-
function precompile(literals) {
40+
function precompile(inputApplication) {
41+
let lits = findRegexLiterals(inputApplication);
42+
43+
if (lits.length === 0) {
44+
return inputApplication;
45+
}
46+
3947
return (
4048
PREAMBLE +
41-
literals
49+
lits
4250
.map((regex) => {
4351
return `precompile(/${regex.pattern}/${regex.flags});`;
4452
})
@@ -113,11 +121,7 @@ export async function compileApplicationToWasm(input, output, wasmEngine) {
113121
process.exit(1);
114122
}
115123

116-
let lits = findRegexLiterals(inputContents);
117-
118-
if (lits.length != 0) {
119-
inputContents += precompile(lits);
120-
}
124+
let application = precompile(inputContents);
121125

122126
try {
123127
let wizerProcess = spawnSync(
@@ -131,7 +135,7 @@ export async function compileApplicationToWasm(input, output, wasmEngine) {
131135
],
132136
{
133137
stdio: [null, process.stdout, process.stderr],
134-
input: inputContents,
138+
input: application,
135139
shell: true,
136140
encoding: "utf-8",
137141
}

0 commit comments

Comments
 (0)