Skip to content

Commit c4e39ce

Browse files
authored
[jscompiler] Simplify pushing and popping the current file being processed. NFC (#23726)
1 parent 2ee1188 commit c4e39ce

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/modules.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
error,
1616
readFile,
1717
warn,
18-
setCurrentFile,
18+
pushCurrentFile,
19+
popCurrentFile,
1920
printErr,
2021
addToCompileTimeContext,
2122
runInMacroContext,
@@ -275,7 +276,7 @@ export const LibraryManager = {
275276
origLibrary = this.library;
276277
this.library = userLibraryProxy;
277278
}
278-
const oldFile = setCurrentFile(filename);
279+
pushCurrentFile(filename);
279280
try {
280281
processed = processMacros(preprocess(filename), filename);
281282
runInMacroContext(processed, {filename: filename.replace(/\.\w+$/, '.preprocessed$&')});
@@ -295,7 +296,7 @@ export const LibraryManager = {
295296
}
296297
throw e;
297298
} finally {
298-
setCurrentFile(oldFile);
299+
popCurrentFile();
299300
if (origLibrary) {
300301
this.library = origLibrary;
301302
}

src/parseTools.mjs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
printErr,
1919
readFile,
2020
runInMacroContext,
21-
setCurrentFile,
21+
pushCurrentFile,
22+
popCurrentFile,
2223
warn,
2324
srcDir,
2425
} from './utility.mjs';
@@ -36,10 +37,15 @@ export function processMacros(text, filename) {
3637
// The `?` here in makes the regex non-greedy so it matches with the closest
3738
// set of closing braces.
3839
// `[\s\S]` works like `.` but include newline.
39-
return text.replace(/{{{([\s\S]+?)}}}/g, (_, str) => {
40-
const ret = runInMacroContext(str, {filename: filename});
41-
return ret !== null ? ret.toString() : '';
42-
});
40+
pushCurrentFile(filename);
41+
try {
42+
return text.replace(/{{{([\s\S]+?)}}}/g, (_, str) => {
43+
const ret = runInMacroContext(str, {filename: filename});
44+
return ret !== null ? ret.toString() : '';
45+
});
46+
} finally {
47+
popCurrentFile();
48+
}
4349
}
4450

4551
function findIncludeFile(filename, currentDir) {
@@ -86,7 +92,6 @@ export function preprocess(filename) {
8692
const showStack = [];
8793
const showCurrentLine = () => showStack.every((x) => x == SHOW);
8894

89-
const oldFilename = setCurrentFile(filename);
9095
const fileExt = filename.split('.').pop().toLowerCase();
9196
const isHtml = fileExt === 'html' || fileExt === 'htm' ? true : false;
9297
let inStyle = false;
@@ -99,6 +104,7 @@ export function preprocess(filename) {
99104
let ret = '';
100105
let emptyLine = false;
101106

107+
pushCurrentFile(filename);
102108
try {
103109
for (let [i, line] of lines.entries()) {
104110
if (isHtml) {
@@ -209,7 +215,7 @@ no matching #endif found (${showStack.length$}' unmatched preprocessing directiv
209215
);
210216
return ret;
211217
} finally {
212-
setCurrentFile(oldFilename);
218+
popCurrentFile();
213219
}
214220
}
215221

src/utility.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ export function warningOccured() {
4646
return warnings;
4747
}
4848

49-
let currentFile = null;
49+
let currentFile = [];
5050

51-
export function setCurrentFile(f) {
52-
let rtn = currentFile;
53-
currentFile = f;
54-
return rtn;
51+
export function pushCurrentFile(f) {
52+
currentFile.push(f);
53+
}
54+
55+
export function popCurrentFile() {
56+
currentFile.pop();
5557
}
5658

5759
function errorPrefix() {
58-
if (currentFile) {
59-
return currentFile + ': ';
60+
if (currentFile.length > 0) {
61+
return currentFile[currentFile.length - 1] + ': ';
6062
} else {
6163
return '';
6264
}

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14285,7 +14285,7 @@ def test_legacy_runtime(self):
1428514285
self.clear_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE')
1428614286
for opt in ('-O0', '-O3'):
1428714287
err = self.expect_fail([EMCC, test_file('other/test_legacy_runtime.c'), opt] + self.get_emcc_args())
14288-
self.assertContained('warning: invalid item in EXPORTED_RUNTIME_METHODS: allocate', err)
14288+
self.assertContained('invalid item in EXPORTED_RUNTIME_METHODS: allocate', err)
1428914289

1429014290
def test_fetch_settings(self):
1429114291
create_file('pre.js', '''

0 commit comments

Comments
 (0)