Skip to content

Commit

Permalink
Refactor global symbol debug hooks. NFC (#22634)
Browse files Browse the repository at this point in the history
This paves the way to address #22588 in a single location.
  • Loading branch information
sbc100 authored Sep 26, 2024
1 parent ef0efd2 commit fafea90
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/runtime_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,51 @@ function isExportedByForceFilesystem(name) {
name === 'removeRunDependency';
}

function missingGlobal(sym, msg) {
if (typeof globalThis != 'undefined') {
/**
* Intercept access to a global symbol. This enables us to give informative
* warnings/errors when folks attempt to use symbols they did not include in
* their build, or no symbols that no longer exist.
*/
function hookGlobalSymbolAccess(sym, func) {
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
Object.defineProperty(globalThis, sym, {
configurable: true,
get() {
warnOnce(`\`${sym}\` is not longer defined by emscripten. ${msg}`);
func();
return undefined;
}
});
}
}

function missingGlobal(sym, msg) {
hookGlobalSymbolAccess(sym, () => {
warnOnce(`\`${sym}\` is not longer defined by emscripten. ${msg}`);
});
}

missingGlobal('buffer', 'Please use HEAP8.buffer or wasmMemory.buffer');
missingGlobal('asm', 'Please use wasmExports instead');

function missingLibrarySymbol(sym) {
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
Object.defineProperty(globalThis, sym, {
configurable: true,
get() {
// Can't `abort()` here because it would break code that does runtime
// checks. e.g. `if (typeof SDL === 'undefined')`.
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`;
// DEFAULT_LIBRARY_FUNCS_TO_INCLUDE requires the name as it appears in
// library.js, which means $name for a JS name with no prefix, or name
// for a JS name like _name.
var librarySymbol = sym;
if (!librarySymbol.startsWith('_')) {
librarySymbol = '$' + sym;
}
msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`;
if (isExportedByForceFilesystem(sym)) {
msg += '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you';
}
warnOnce(msg);
return undefined;
}
});
}
hookGlobalSymbolAccess(sym, () => {
// Can't `abort()` here because it would break code that does runtime
// checks. e.g. `if (typeof SDL === 'undefined')`.
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`;
// DEFAULT_LIBRARY_FUNCS_TO_INCLUDE requires the name as it appears in
// library.js, which means $name for a JS name with no prefix, or name
// for a JS name like _name.
var librarySymbol = sym;
if (!librarySymbol.startsWith('_')) {
librarySymbol = '$' + sym;
}
msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`;
if (isExportedByForceFilesystem(sym)) {
msg += '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you';
}
warnOnce(msg);
});

// Any symbol that is not included from the JS library is also (by definition)
// not exported on the Module object.
unexportedRuntimeSymbol(sym);
Expand Down

0 comments on commit fafea90

Please sign in to comment.