1010// until the wasm memory has been received via postMessage.
1111
1212#if RUNTIME_DEBUG
13- console . log ( " Running pthread_esm_startup" ) ;
13+ console . log ( ' Running pthread_esm_startup' ) ;
1414#endif
1515
1616#if ENVIRONMENT_MAY_BE_NODE
@@ -34,25 +34,28 @@ self.onmessage = async (msg) => {
3434#if RUNTIME_DEBUG
3535 console . log ( 'pthread_esm_startup' , msg . data . cmd ) ;
3636#endif
37- if ( msg . data . cmd == 'load' ) {
38- // Until we initialize the runtime, queue up any further incoming messages
39- // that can arrive while the async import (await import below) is happening.
40- // For examples the `run` message often arrives right away before the import
41- // is complete.
42- let messageQueue = [ msg ] ;
43- self . onmessage = ( e ) => messageQueue . push ( e ) ;
44-
45- // Now that we have the wasmMemory we can import the main program
46- globalThis . wasmMemory = msg . data . wasmMemory ;
47- const prog = await import ( './{{{ TARGET_JS_NAME }}}' ) ;
48-
49- // Now that the import is completed the main program will have installed
50- // its own `onmessage` handler and replaced our handler.
51- // Now we can dispatch any queued messages to this new handler.
52- for ( const msg of messageQueue ) {
53- await self . onmessage ( msg ) ;
54- }
55-
56- await prog . default ( )
37+
38+ // Until we initialize the runtime, queue up any further incoming messages
39+ // that can arrive while the async import (await import below) is happening.
40+ // For examples the `run` message often arrives right away before the import
41+ // is complete.
42+ #if ASSERTIONS
43+ if ( ! msg . data . wasmMemory ) console . error ( 'first message should include wasmMemory' ) ;
44+ #endif
45+
46+ const messageQueue = [ msg ] ;
47+ self . onmessage = ( e ) => messageQueue . push ( e ) ;
48+
49+ // Now that we have the wasmMemory we can import the main program
50+ globalThis . wasmMemory = msg . data . wasmMemory ;
51+ const prog = await import ( './{{{ TARGET_JS_NAME }}}' ) ;
52+
53+ // Now that the import is completed the main program will have installed
54+ // its own `onmessage` handler and replaced our handler.
55+ // Now we can dispatch any queued messages to this new handler.
56+ for ( const msg of messageQueue ) {
57+ await self . onmessage ( msg ) ;
5758 }
59+
60+ await prog . default ( )
5861} ;
0 commit comments