@@ -10,40 +10,40 @@ var dlopenMissingError = "'To use dlopen, you need enable dynamic linking, see h
1010
1111var LibraryDylink = {
1212#if RELOCATABLE
13+ $isSymbolDefined : function ( symName ) {
14+ // Ignore 'stub' symbols that are auto-generated as part of the original
15+ // `wasmImports` used to instantate the main module.
16+ var existing = wasmImports [ symName ] ;
17+ if ( ! existing || existing . stub ) return false ;
18+ #if ASYNCIFY
19+ // Even if a symbol exists in wasmImports and it not a stub, it could
20+ // be an ASYNCIFY wrapper function.
21+ if ( symName in asyncifyStubs && ! asyncifyStubs [ symName ] ) return false ;
22+ #endif
23+ return true ;
24+ } ,
25+
26+ $resolveGlobalSymbol__deps : [ '$isSymbolDefined' ] ,
1327 $resolveGlobalSymbol__internal : true ,
14- $resolveGlobalSymbol__deps : [ '$asmjsMangle' ] ,
15- $resolveGlobalSymbol : function ( symName , direct ) {
28+ $resolveGlobalSymbol : function ( symName , direct = false ) {
1629 var sym ;
1730#if ! WASM_BIGINT
1831 if ( direct ) {
1932 // First look for the orig$ symbol which is the symbols without
2033 // any legalization performed.
2134 sym = wasmImports [ 'orig$' + symName ] ;
35+ if ( sym ) return sym ;
2236 }
2337#endif
24- if ( ! sym ) {
38+ if ( isSymbolDefined ( symName ) ) {
2539 sym = wasmImports [ symName ] ;
26- // Ignore 'stub' symbols that are auto-generated as part of the original
27- // `wasmImports` used to instantate the main module.
28- if ( sym && sym . stub ) sym = undefined ;
29- }
30-
31- // Check for the symbol on the Module object. This is the only
32- // way to dynamically access JS library symbols that were not
33- // referenced by the main module (and therefore not part of the
34- // initial set of symbols included in wasmImports when it
35- // was declared.
36- if ( ! sym ) {
37- sym = Module [ asmjsMangle ( symName ) ] ;
38- }
39-
40- if ( ! sym && symName . startsWith ( 'invoke_' ) ) {
41- sym = createInvokeFunction ( symName . split ( '_' ) [ 1 ] ) ;
40+ } else if ( symName . startsWith ( 'invoke_' ) ) {
41+ // Create (and cache) new invoke_ functions on demand.
42+ sym = wasmImports [ symName ] = createInvokeFunction ( symName . split ( '_' ) [ 1 ] ) ;
4243 }
43-
4444#if ! DISABLE_EXCEPTION_CATCHING
45- if ( ! sym && symName . startsWith ( "__cxa_find_matching_catch" ) ) {
46- sym = Module [ "___cxa_find_matching_catch" ] ;
45+ else if ( symName . startsWith ( '__cxa_find_matching_catch_' ) ) {
46+ sym = wasmImports [ '__cxa_find_matching_catch' ] ;
4747 }
4848#endif
4949 return sym ;
@@ -456,24 +456,15 @@ var LibraryDylink = {
456456 } ,
457457
458458 // Module.symbols <- libModule.symbols (flags.global handler)
459- $mergeLibSymbols__deps : [ '$asmjsMangle' ] ,
459+ $mergeLibSymbols__deps : [ '$asmjsMangle' , '$isSymbolDefined' ] ,
460460 $mergeLibSymbols : function ( exports , libName ) {
461461 // add symbols into global namespace TODO: weak linking etc.
462462 for ( var sym in exports ) {
463463 if ( ! exports . hasOwnProperty ( sym ) ) {
464464 continue ;
465465 }
466-
467- // When RTLD_GLOBAL is enable, the symbols defined by this shared object will be made
468- // available for symbol resolution of subsequently loaded shared objects.
469- //
470- // We should copy the symbols (which include methods and variables) from SIDE_MODULE to MAIN_MODULE.
471-
472- if ( ! wasmImports . hasOwnProperty ( sym ) ) {
473- wasmImports [ sym ] = exports [ sym ] ;
474- }
475466#if ASSERTIONS == 2
476- else {
467+ if ( isSymbolDefined ( sym ) ) {
477468 var curr = wasmImports [ sym ] , next = exports [ sym ] ;
478469 // don't warn on functions - might be odr, linkonce_odr, etc.
479470 if ( ! ( typeof curr == 'function' && typeof next == 'function' ) ) {
@@ -482,19 +473,40 @@ var LibraryDylink = {
482473 }
483474#endif
484475
485- // Export native export on the Module object.
486- // TODO(sbc): Do all users want this? Should we skip this by default?
487- var module_sym = asmjsMangle ( sym ) ;
488- if ( ! Module . hasOwnProperty ( module_sym ) ) {
489- Module [ module_sym ] = exports [ sym ] ;
476+ // When RTLD_GLOBAL is enabled, the symbols defined by this shared object
477+ // will be made available for symbol resolution of subsequently loaded
478+ // shared objects.
479+ //
480+ // We should copy the symbols (which include methods and variables) from
481+ // SIDE_MODULE to MAIN_MODULE.
482+ const setImport = ( target ) => {
483+ #if ASYNCIFY
484+ if ( target in asyncifyStubs ) {
485+ asyncifyStubs [ target ] = exports [ sym ]
486+ }
487+ #endif
488+ if ( ! isSymbolDefined ( target ) ) {
489+ wasmImports [ target ] = exports [ sym ] ;
490+ }
490491 }
492+ setImport ( sym ) ;
493+
491494#if ! hasExportedSymbol ( 'main' )
492- // If the main module doesn't define main it could be defined in one of
493- // the side modules, and we need to handle the mangled named.
494- if ( sym == '__main_argc_argv' ) {
495- Module [ '_main' ] = exports [ sym ] ;
495+ // Special case for handling of main symbol: If a side module exports
496+ // `main` that also acts a definition for `__main_argc_argv` and vice
497+ // versa.
498+ const main_alias = '__main_argc_argv' ;
499+ if ( sym == 'main' ) {
500+ setImport ( main_alias )
501+ }
502+ if ( sym == main_alias ) {
503+ setImport ( 'main' )
496504 }
497505#endif
506+
507+ if ( sym . startsWith ( 'dynCall_' ) && ! Module . hasOwnProperty ( sym ) ) {
508+ Module [ sym ] = exports [ sym ] ;
509+ }
498510 }
499511 } ,
500512
@@ -575,7 +587,7 @@ var LibraryDylink = {
575587 var moduleExports ;
576588
577589 function resolveSymbol ( sym ) {
578- var resolved = resolveGlobalSymbol ( sym , false ) ;
590+ var resolved = resolveGlobalSymbol ( sym ) ;
579591 if ( ! resolved ) {
580592 resolved = moduleExports [ sym ] ;
581593 }
@@ -613,7 +625,7 @@ var LibraryDylink = {
613625 return tableBase ;
614626#endif
615627 }
616- if ( prop in wasmImports ) {
628+ if ( prop in wasmImports && ! wasmImports [ prop ] . stub ) {
617629 // No stub needed, symbol already exists in symbol table
618630 return wasmImports [ prop ] ;
619631 }
@@ -800,7 +812,7 @@ var LibraryDylink = {
800812 // If a library was already loaded, it is not loaded a second time. However
801813 // flags.global and flags.nodelete are handled every time a load request is made.
802814 // Once a library becomes "global" or "nodelete", it cannot be removed or unloaded.
803- $loadDynamicLibrary__deps : [ '$LDSO' , '$loadWebAssemblyModule' , '$asmjsMangle' , '$ isInternalSym', '$mergeLibSymbols' ] ,
815+ $loadDynamicLibrary__deps : [ '$LDSO' , '$loadWebAssemblyModule' , '$isInternalSym' , '$mergeLibSymbols' ] ,
804816 $loadDynamicLibrary__docs : '/** @param {number=} handle */' ,
805817 $loadDynamicLibrary : function ( lib , flags = { global : true , nodelete : true } , handle = 0 ) {
806818#if DYLINK_DEBUG
0 commit comments