Skip to content

Commit 7e47a69

Browse files
authored
Refactor JS compiler code. NFC (emscripten-core#25696)
This refactor is in preparation for some other refactoring, including adding parallel JS processing.
1 parent 14ffb90 commit 7e47a69

File tree

1 file changed

+59
-41
lines changed

1 file changed

+59
-41
lines changed

src/modules.mjs

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ function getTempDir() {
223223
return tempDir;
224224
}
225225

226+
function preprocessFiles(filenames) {
227+
const results = {};
228+
for (const filename of filenames) {
229+
debugLog(`pre-processing JS library: ${filename}`);
230+
pushCurrentFile(filename);
231+
try {
232+
results[filename] = processMacros(preprocess(filename), filename);
233+
} catch (e) {
234+
error(`error preprocessing JS library "${filename}":`);
235+
throw e;
236+
} finally {
237+
popCurrentFile();
238+
}
239+
}
240+
return results;
241+
}
226242

227243
export const LibraryManager = {
228244
library: {},
@@ -251,6 +267,13 @@ export const LibraryManager = {
251267
// Save the list for has() queries later.
252268
this.libraries = calculateLibraries();
253269

270+
const preprocessed = preprocessFiles(this.libraries);
271+
for (const [filename, contents] of Object.entries(preprocessed)) {
272+
this.executeJSLibraryFile(filename, contents);
273+
}
274+
},
275+
276+
executeJSLibraryFile(filename, contents) {
254277
const userLibraryProxy = new Proxy(this.library, {
255278
set(target, prop, value) {
256279
target[prop] = value;
@@ -261,52 +284,47 @@ export const LibraryManager = {
261284
},
262285
});
263286

264-
for (let filename of this.libraries) {
265-
const isUserLibrary = !isBeneath(filename, systemLibdir);
287+
const isUserLibrary = !isBeneath(filename, systemLibdir);
288+
if (isUserLibrary) {
289+
debugLog(`executing user JS library: ${filename}`);
290+
} else {
291+
debugLog(`exectuing system JS library: ${filename}`);
292+
}
266293

267-
if (isUserLibrary) {
268-
debugLog('processing user library: ' + filename);
269-
} else {
270-
debugLog('processing system library: ' + filename);
271-
}
272-
let origLibrary = undefined;
273-
let processed = undefined;
274-
// When we parse user libraries also set `__user` attribute
275-
// on each element so that we can distinguish them later.
276-
if (isUserLibrary) {
277-
origLibrary = this.library;
278-
this.library = userLibraryProxy;
279-
}
280-
pushCurrentFile(filename);
281-
let preprocessedName = filename.replace(/\.\w+$/, '.preprocessed$&')
294+
let origLibrary;
295+
// When we parse user libraries also set `__user` attribute
296+
// on each element so that we can distinguish them later.
297+
if (isUserLibrary) {
298+
origLibrary = this.library;
299+
this.library = userLibraryProxy;
300+
}
301+
pushCurrentFile(filename);
302+
let preprocessedName = filename.replace(/\.\w+$/, '.preprocessed$&')
303+
if (VERBOSE) {
304+
preprocessedName = path.join(getTempDir(), path.basename(filename));
305+
}
306+
307+
try {
308+
runInMacroContext(contents, {filename: preprocessedName})
309+
} catch (e) {
310+
error(`failure to execute JS library "${filename}":`);
282311
if (VERBOSE) {
283-
preprocessedName = path.join(getTempDir(), path.basename(filename));
284-
}
285-
try {
286-
processed = processMacros(preprocess(filename), filename);
287-
runInMacroContext(processed, {filename: preprocessedName})
288-
} catch (e) {
289-
error(`failure to execute JS library "${filename}":`);
290-
if (processed) {
291-
if (VERBOSE) {
292-
fs.writeFileSync(preprocessedName, processed);
293-
error(`preprocessed JS saved to ${preprocessedName}`)
294-
} else {
295-
error('use -sVERBOSE to save preprocessed JS');
296-
}
297-
}
298-
throw e;
299-
} finally {
300-
popCurrentFile();
301-
if (origLibrary) {
302-
this.library = origLibrary;
303-
}
312+
fs.writeFileSync(preprocessedName, contents);
313+
error(`preprocessed JS saved to ${preprocessedName}`)
314+
} else {
315+
error('use -sVERBOSE to save preprocessed JS');
304316
}
305-
if (VERBOSE) {
306-
fs.rmSync(getTempDir(), { recursive: true, force: true });
317+
throw e;
318+
} finally {
319+
popCurrentFile();
320+
if (origLibrary) {
321+
this.library = origLibrary;
307322
}
308323
}
309-
},
324+
if (VERBOSE) {
325+
fs.rmSync(getTempDir(), { recursive: true, force: true });
326+
}
327+
}
310328
};
311329

312330
// options is optional input object containing mergeInto params

0 commit comments

Comments
 (0)