@@ -627,6 +627,7 @@ var LibraryDylink = {
627627 // loadModule loads the wasm module after all its dependencies have been loaded.
628628 // can be called both sync/async.
629629 function loadModule ( ) {
630+ #if PTHREADS
630631 // The first thread to load a given module needs to allocate the static
631632 // table and memory regions. Later threads re-use the same table region
632633 // and can ignore the memory region (since memory is shared between
@@ -636,6 +637,7 @@ var LibraryDylink = {
636637 // locking in `dynlink.c`.
637638 var firstLoad = ! handle || ! { { { makeGetValue ( 'handle' , C_STRUCTS . dso . mem_allocated , 'i8' ) } } } ;
638639 if ( firstLoad ) {
640+ #endif
639641 // alignments are powers of 2
640642 var memAlign = Math . pow ( 2 , metadata . memoryAlign ) ;
641643 // prepare memory
@@ -648,21 +650,27 @@ var LibraryDylink = {
648650 { { { makeSetValue ( 'handle' , C_STRUCTS . dso . table_addr , 'tableBase' , '*' ) } } } ;
649651 { { { makeSetValue ( 'handle' , C_STRUCTS . dso . table_size , 'metadata.tableSize' , 'i32' ) } } } ;
650652 }
653+ #if PTHREADS
651654 } else {
655+ // Read the values for tableBase and memoryBase from shared memory. The
656+ // thread that first loaded the DLL already set these values.
652657 memoryBase = { { { makeGetValue ( 'handle' , C_STRUCTS . dso . mem_addr , '*' ) } } } ;
653658 tableBase = { { { makeGetValue ( 'handle' , C_STRUCTS . dso . table_addr , '*' ) } } } ;
654659 }
660+ #endif
655661
656- var tableGrowthNeeded = tableBase + metadata . tableSize - { { { from64Expr ( 'wasmTable.length' ) } } } ;
657- if ( tableGrowthNeeded > 0 ) {
662+ if ( metadata . tableSize ) {
663+ #if ASSERTIONS
664+ assert ( { { { from64Expr ( 'wasmTable.length' ) } } } == tableBase , `unexpected table size while loading ${ libName } : ${ wasmTable . length } ` ) ;
665+ #endif
658666#if DYLINK_DEBUG
659- dbg ( "loadModule: growing table: " + tableGrowthNeeded ) ;
667+ dbg ( "loadModule: growing table by : " + metadata . tableSize ) ;
660668#endif
661- wasmTable . grow ( { { { toIndexType ( 'tableGrowthNeeded ' ) } } } ) ;
669+ wasmTable . grow ( { { { toIndexType ( 'metadata.tableSize ' ) } } } ) ;
662670 }
663671#if DYLINK_DEBUG
664- dbg ( " loadModule: memory[" + memoryBase + ":" + ( memoryBase + metadata . memorySize ) + "]" +
665- " table[" + tableBase + ":" + ( tableBase + metadata . tableSize ) + "]" ) ;
672+ dbg ( ` loadModule: memory[${ memoryBase } : ${ memoryBase + metadata . memorySize } ]` +
673+ ` table[${ tableBasex } : ${ tableBase + metadata . tableSize } ]` ) ;
666674#endif
667675
668676 // This is the export map that we ultimately return. We declare it here
0 commit comments