Skip to content

Commit 25e6e9c

Browse files
authored
[compiler.mjs] Simplify file opening and finding logic. NFC (#23430)
Removes the magic `find` function and instead require explicit file locations at all the various call sites. This means that in each circumstance were we want to load a file there is only once place it can/should be. One side effect of this is that when using the `#include` preprocessor directive the filename must either be absolute or relative to the current file.
1 parent d1fbb4b commit 25e6e9c

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

src/embind/embind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/*global throwInstanceAlreadyDeleted, shallowCopyInternalPointer*/
2626
/*global RegisteredPointer_fromWireType, constNoSmartPtrRawPointerToWireType, nonConstNoSmartPtrRawPointerToWireType, genericPointerToWireType*/
2727

28-
#include "embind/embind_shared.js"
28+
#include "embind_shared.js"
2929

3030
var LibraryEmbind = {
3131
$UnboundTypeError__postset: "UnboundTypeError = Module['UnboundTypeError'] = extendError(Error, 'UnboundTypeError');",

src/embind/embind_gen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Emscripten is available under two separate licenses, the MIT license and the
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
5-
#include "embind/embind_shared.js"
5+
#include "embind_shared.js"
66

77
var LibraryEmbind = {
88

src/modules.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
addToCompileTimeContext,
1919
runInMacroContext,
2020
mergeInto,
21+
localFile,
2122
} from './utility.mjs';
2223
import {preprocess, processMacros} from './parseTools.mjs';
2324

@@ -217,7 +218,7 @@ export const LibraryManager = {
217218
// Save the list for has() queries later.
218219
this.libraries = libraries;
219220

220-
for (const filename of libraries) {
221+
for (var filename of libraries) {
221222
const isUserLibrary = path.isAbsolute(filename);
222223
if (VERBOSE) {
223224
if (isUserLibrary) {
@@ -241,6 +242,9 @@ export const LibraryManager = {
241242
return true;
242243
},
243244
});
245+
} else {
246+
// System libraries are specified relatative to the `src` directory
247+
filename = localFile(filename);
244248
}
245249
const oldFile = setCurrentFile(filename);
246250
try {
@@ -301,9 +305,9 @@ function loadStructInfo(filename) {
301305
if (!BOOTSTRAPPING_STRUCT_INFO) {
302306
// Load struct and define information.
303307
if (MEMORY64) {
304-
loadStructInfo('struct_info_generated_wasm64.json');
308+
loadStructInfo(localFile('struct_info_generated_wasm64.json'));
305309
} else {
306-
loadStructInfo('struct_info_generated.json');
310+
loadStructInfo(localFile('struct_info_generated.json'));
307311
}
308312
}
309313

src/parseTools.mjs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Tests live in test/other/test_parseTools.js.
99
*/
1010

11+
import * as path from 'node:path';
12+
1113
import {
1214
addToCompileTimeContext,
1315
assert,
@@ -119,15 +121,19 @@ export function preprocess(filename) {
119121
showStack.push(truthy ? SHOW : IGNORE);
120122
} else if (first === '#include') {
121123
if (showCurrentLine()) {
122-
let filename = line.substr(line.indexOf(' ') + 1);
123-
if (filename.startsWith('"')) {
124-
filename = filename.substr(1, filename.length - 2);
124+
let includeFile = line.substr(line.indexOf(' ') + 1);
125+
if (includeFile.startsWith('"')) {
126+
includeFile = includeFile.substr(1, includeFile.length - 2);
127+
}
128+
// Include files are always relative to the current file being processed
129+
if (!path.isAbsolute(includeFile)) {
130+
includeFile = path.join(path.dirname(filename), includeFile);
125131
}
126-
const result = preprocess(filename);
132+
const result = preprocess(includeFile);
127133
if (result) {
128-
ret += `// include: ${filename}\n`;
134+
ret += `// include: ${includeFile}\n`;
129135
ret += result;
130-
ret += `// end include: ${filename}\n`;
136+
ret += `// end include: ${includeFile}\n`;
131137
}
132138
}
133139
} else if (first === '#else') {

src/utility.mjs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,17 @@ export function isDecorator(ident) {
227227
}
228228

229229
export function read(filename) {
230-
const absolute = find(filename);
231-
return fs.readFileSync(absolute, 'utf8');
230+
return fs.readFileSync(filename, 'utf8');
232231
}
233232

234233
// Use import.meta.dirname here once we drop support for node v18.
235234
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
236235

237-
function find(filename) {
238-
for (const prefix of [process.cwd(), __dirname]) {
239-
const combined = path.join(prefix, filename);
240-
if (fs.existsSync(combined)) {
241-
return combined;
242-
}
243-
}
244-
return filename;
236+
// Returns an absolute path for a file, resolving it relative to this script
237+
// (i.e. relative to the src/ directory).
238+
export function localFile(filename) {
239+
assert(!path.isAbsolute(filename));
240+
return path.join(__dirname, filename);
245241
}
246242

247243
// Anything needed by the script that we load below must be added to the
@@ -314,15 +310,15 @@ export function applySettings(obj) {
314310
}
315311

316312
export function loadSettingsFile(f) {
317-
var settings = {};
318-
vm.runInNewContext(read(f), settings, {filename: find(f)});
313+
const settings = {};
314+
vm.runInNewContext(read(f), settings, {filename: f});
319315
applySettings(settings);
320316
return settings;
321317
}
322318

323319
export function loadDefaultSettings() {
324-
const rtn = loadSettingsFile('settings.js');
325-
Object.assign(rtn, loadSettingsFile('settings_internal.js'));
320+
const rtn = loadSettingsFile(localFile('settings.js'));
321+
Object.assign(rtn, loadSettingsFile(localFile('settings_internal.js')));
326322
return rtn;
327323
}
328324

tools/maint/gen_sig_info.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,12 @@ def extract_sig_info(sig_info, extra_settings=None, extra_cflags=None, cxx=False
307307
'AUDIO_WORKLET': 1,
308308
'WASM_WORKERS': 1,
309309
'JS_LIBRARIES': [
310-
'src/library_websocket.js',
311-
'src/library_exports.js',
312-
'src/library_webaudio.js',
313-
'src/library_fetch.js',
314-
'src/library_pthread.js',
315-
'src/library_trace.js',
310+
'library_websocket.js',
311+
'library_exports.js',
312+
'library_webaudio.js',
313+
'library_fetch.js',
314+
'library_pthread.js',
315+
'library_trace.js',
316316
],
317317
'SUPPORT_LONGJMP': 'emscripten'
318318
}
@@ -390,9 +390,9 @@ def main(args):
390390
'BUILD_AS_WORKER': 1,
391391
'LINK_AS_CXX': 1,
392392
'AUTO_JS_LIBRARIES': 0}, cxx=True)
393-
extract_sig_info(sig_info, {'WASM_WORKERS': 1, 'JS_LIBRARIES': ['src/library_wasm_worker.js']})
393+
extract_sig_info(sig_info, {'WASM_WORKERS': 1, 'JS_LIBRARIES': ['library_wasm_worker.js']})
394394
extract_sig_info(sig_info, {'USE_GLFW': 3}, ['-DGLFW3'])
395-
extract_sig_info(sig_info, {'JS_LIBRARIES': ['src/embind/embind.js', 'src/embind/emval.js'],
395+
extract_sig_info(sig_info, {'JS_LIBRARIES': ['embind/embind.js', 'embind/emval.js'],
396396
'USE_SDL': 0,
397397
'MAX_WEBGL_VERSION': 0,
398398
'AUTO_JS_LIBRARIES': 0,

0 commit comments

Comments
 (0)