diff --git a/src/jsifier.mjs b/src/jsifier.mjs index b8c61b2188c33..5342ffa500039 100644 --- a/src/jsifier.mjs +++ b/src/jsifier.mjs @@ -505,7 +505,7 @@ function(${args}) { // what we just added to the library. } - function addFromLibrary(symbol, dependent, force = false) { + function addFromLibrary(symbol, dependent) { // don't process any special identifiers. These are looked up when // processing the base name of the identifier. if (isDecorator(symbol)) { @@ -514,7 +514,7 @@ function(${args}) { // if the function was implemented in compiled code, there is no need to // include the js version - if (WASM_EXPORTS.has(symbol) && !force) { + if (WASM_EXPORTS.has(symbol)) { return; } @@ -639,7 +639,6 @@ function(${args}) { }); let isFunction = false; - let aliasTarget; const postsetId = symbol + '__postset'; const postset = LibraryManager.library[postsetId]; @@ -659,7 +658,7 @@ function(${args}) { // Redirection for aliases. We include the parent, and at runtime // make ourselves equal to it. This avoid having duplicate // functions with identical content. - aliasTarget = snippet; + const aliasTarget = snippet; snippet = mangleCSymbolName(aliasTarget); deps.push(aliasTarget); } @@ -690,7 +689,7 @@ function(${args}) { 'noExitRuntime cannot be referenced via __deps mechanism. Use DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or EXPORTED_RUNTIME_METHODS', ); } - return addFromLibrary(dep, `${symbol}, referenced by ${dependent}`, dep === aliasTarget); + return addFromLibrary(dep, `${symbol}, referenced by ${dependent}`); } let contentText; if (isFunction) { @@ -777,10 +776,6 @@ function(${args}) { } let commentText = ''; - if (force) { - commentText += '/** @suppress {duplicate } */\n'; - } - let docs = LibraryManager.library[symbol + '__docs']; // Add the docs if they exist and if we are actually emitting a declaration. // See the TODO about wasmTable above. diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index 29a0ff0ea1c08..d8fb2c4f9d11e 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -4346,17 +4346,37 @@ createGLPassthroughFunctions(LibraryGL, glPassthroughFuncs); autoAddDeps(LibraryGL, '$GL'); +function renameSymbol(lib, oldName, newName) { + lib[newName] = lib[oldName]; + delete lib[oldName]; + for (const suffix of decoratorSuffixes) { + const oldDecorator = oldName + suffix; + if (lib.hasOwnProperty(oldDecorator)) { + const newDecorator = newName + suffix; + lib[newDecorator] = lib[oldDecorator]; + delete lib[oldDecorator]; + } + } +} + function recordGLProcAddressGet(lib) { // GL proc address retrieval - allow access through glX and emscripten_glX, to // allow name collisions with user-implemented things having the same name // (see gl.c) + // + // We do this by renaming `glX` symbols to `emscripten_glX` and then setting + // `glX` as an alias of `emscripten_glX`. The reason for this renaming is to + // ensure that `emscripten_glX` is always available, even in cases where native + // code defines `glX`. + const glSyms = []; for (const sym of Object.keys(lib)) { if (sym.startsWith('gl') && !isDecorator(sym)) { - const alias = 'emscripten_' + sym; - lib[alias] = sym; + const newSym = 'emscripten_' + sym; + renameSymbol(lib, sym, newSym); + lib[sym] = newSym; var sig = LibraryManager.library[sym + '__sig']; if (sig) { - lib[alias + '__sig'] = sig; + lib[newSym + '__sig'] = sig; } } } diff --git a/src/utility.mjs b/src/utility.mjs index b300676a5c58d..65459a401dec9 100644 --- a/src/utility.mjs +++ b/src/utility.mjs @@ -157,7 +157,7 @@ export function mergeInto(obj, other, options = null) { const deps = other[key]; if (!Array.isArray(deps)) { error( - `JS library directive ${key}=${deps.toString()} is of type '${type}', but it should be an array`, + `JS library directive ${key}=${deps} is of type '${type}', but it should be an array`, ); } for (let dep of deps) { @@ -199,22 +199,23 @@ export function isJsOnlySymbol(symbol) { return symbol[0] == '$'; } +export const decoratorSuffixes = [ + '__sig', + '__proxy', + '__asm', + '__deps', + '__postset', + '__docs', + '__nothrow', + '__noleakcheck', + '__internal', + '__user', + '__async', + '__i53abi', +]; + export function isDecorator(ident) { - const suffixes = [ - '__sig', - '__proxy', - '__asm', - '__deps', - '__postset', - '__docs', - '__nothrow', - '__noleakcheck', - '__internal', - '__user', - '__async', - '__i53abi', - ]; - return suffixes.some((suffix) => ident.endsWith(suffix)); + return decoratorSuffixes.some((suffix) => ident.endsWith(suffix)); } export function readFile(filename) { @@ -330,6 +331,7 @@ export function runInMacroContext(code, options) { addToCompileTimeContext({ assert, + decoratorSuffixes, error, isDecorator, isJsOnlySymbol, diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index 7a1fb7e1ba03a..4f142ec30a20f 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 245842, + "a.out.js": 245841, "a.out.nodebug.wasm": 597755, - "total": 843597, + "total": 843596, "sent": [ "IMG_Init", "IMG_Load", diff --git a/test/test_override_system_js_lib_symbol.js b/test/test_override_system_js_lib_symbol.js index eab127fa67dad..3ede691a3cf19 100644 --- a/test/test_override_system_js_lib_symbol.js +++ b/test/test_override_system_js_lib_symbol.js @@ -1,7 +1,7 @@ if (!LibraryManager.library.glTexImage3D) throw 'This file should be getting processed after library_webgl2.js!'; addToLibrary({ - orig_glTexImage3D__deps: LibraryManager.library.glTexImage3D__deps, + orig_glTexImage3D__deps: LibraryManager.library.glTexImage3D__deps || [], orig_glTexImage3D: LibraryManager.library.glTexImage3D, glTexImage3D__deps: ['orig_glTexImage3D'],