Skip to content

Commit fafea90

Browse files
authored
Refactor global symbol debug hooks. NFC (#22634)
This paves the way to address #22588 in a single location.
1 parent ef0efd2 commit fafea90

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/runtime_debug.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,45 +50,51 @@ function isExportedByForceFilesystem(name) {
5050
name === 'removeRunDependency';
5151
}
5252

53-
function missingGlobal(sym, msg) {
54-
if (typeof globalThis != 'undefined') {
53+
/**
54+
* Intercept access to a global symbol. This enables us to give informative
55+
* warnings/errors when folks attempt to use symbols they did not include in
56+
* their build, or no symbols that no longer exist.
57+
*/
58+
function hookGlobalSymbolAccess(sym, func) {
59+
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
5560
Object.defineProperty(globalThis, sym, {
5661
configurable: true,
5762
get() {
58-
warnOnce(`\`${sym}\` is not longer defined by emscripten. ${msg}`);
63+
func();
5964
return undefined;
6065
}
6166
});
6267
}
6368
}
6469

70+
function missingGlobal(sym, msg) {
71+
hookGlobalSymbolAccess(sym, () => {
72+
warnOnce(`\`${sym}\` is not longer defined by emscripten. ${msg}`);
73+
});
74+
}
75+
6576
missingGlobal('buffer', 'Please use HEAP8.buffer or wasmMemory.buffer');
6677
missingGlobal('asm', 'Please use wasmExports instead');
6778

6879
function missingLibrarySymbol(sym) {
69-
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
70-
Object.defineProperty(globalThis, sym, {
71-
configurable: true,
72-
get() {
73-
// Can't `abort()` here because it would break code that does runtime
74-
// checks. e.g. `if (typeof SDL === 'undefined')`.
75-
var msg = `\`${sym}\` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line`;
76-
// DEFAULT_LIBRARY_FUNCS_TO_INCLUDE requires the name as it appears in
77-
// library.js, which means $name for a JS name with no prefix, or name
78-
// for a JS name like _name.
79-
var librarySymbol = sym;
80-
if (!librarySymbol.startsWith('_')) {
81-
librarySymbol = '$' + sym;
82-
}
83-
msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`;
84-
if (isExportedByForceFilesystem(sym)) {
85-
msg += '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you';
86-
}
87-
warnOnce(msg);
88-
return undefined;
89-
}
90-
});
91-
}
80+
hookGlobalSymbolAccess(sym, () => {
81+
// Can't `abort()` here because it would break code that does runtime
82+
// checks. e.g. `if (typeof SDL === 'undefined')`.
83+
var msg = `\`${sym}\` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line`;
84+
// DEFAULT_LIBRARY_FUNCS_TO_INCLUDE requires the name as it appears in
85+
// library.js, which means $name for a JS name with no prefix, or name
86+
// for a JS name like _name.
87+
var librarySymbol = sym;
88+
if (!librarySymbol.startsWith('_')) {
89+
librarySymbol = '$' + sym;
90+
}
91+
msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`;
92+
if (isExportedByForceFilesystem(sym)) {
93+
msg += '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you';
94+
}
95+
warnOnce(msg);
96+
});
97+
9298
// Any symbol that is not included from the JS library is also (by definition)
9399
// not exported on the Module object.
94100
unexportedRuntimeSymbol(sym);

0 commit comments

Comments
 (0)