@@ -81,45 +81,31 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
8181}
8282#endif
8383
84- #if ! SINGLE_FILE
85- #if ENVIRONMENT_MAY_BE_NODE && ( ( WASM == 1 && ! WASM2JS ) || WASM == 2 )
86- // Wasm or Wasm2JS loading:
87-
88- if ( ENVIRONMENT_IS_NODE ) {
89- var fs = require ( 'fs' ) ;
90- #if WASM == 2
91- if ( typeof WebAssembly != 'undefined' ) Module [ 'wasm' ] = fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm' ) ;
92- else eval ( fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm.js' ) + '' ) ;
93- #else
94- #if ! WASM2JS
95- Module [ 'wasm' ] = fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm' ) ;
96- #endif
97- #endif
98- }
99- #endif
100-
101- #if ENVIRONMENT_MAY_BE_SHELL && ( ( WASM == 1 && ! WASM2JS ) || WASM == 2 )
102- if ( ENVIRONMENT_IS_SHELL ) {
103- #if WASM == 2
104- if ( typeof WebAssembly != 'undefined ') Module [ 'wasm '] = read ( '{{{ TARGET_BASENAME }}}.wasm' , 'binary' ) ;
105- else eval ( read ( '{{{ TARGET_BASENAME }}}.wasm.js' ) + '' ) ;
106- #else
107- #if ! WASM2JS
108- Module [ 'wasm' ] = read ( '{{{ TARGET_BASENAME }}}.wasm' , 'binary' ) ;
109- #endif
110- #endif
111- }
112- #endif
113-
114- #endif // !SINGLE_FILE
115-
11684// Redefine these in a --pre-js to override behavior. If you would like to
11785// remove out() or err() altogether, you can no-op it out to function() {},
11886// and build with --closure 1 to get Closure optimize out all the uses
11987// altogether.
12088
89+ #if ENVIRONMENT_MAY_BE_NODE && PTHREADS
90+ // Set up the out() and err() hooks, which are how we can print to stdout or
91+ // stderr, respectively.
92+ // Normally just binding console.log/console.error here works fine, but
93+ // under node (with workers) we see missing/out-of-order messages so route
94+ // directly to stdout and stderr.
95+ // See https://github.com/emscripten-core/emscripten/issues/14804
96+ var defaultPrint = console . log . bind ( console ) ;
97+ var defaultPrintErr = console . error . bind ( console ) ;
98+ if ( ENVIRONMENT_IS_NODE ) {
99+ var fs = require ( 'fs' ) ;
100+ defaultPrint = ( ...args ) = > fs . writeSync ( 1 , args . join ( ' ' ) + '\n' ) ;
101+ defaultPrintErr = ( ...args ) = > fs . writeSync ( 2 , args . join ( ' ' ) + '\n' ) ;
102+ }
103+ var out = defaultPrint ;
104+ var err = defaultPrintErr ;
105+ #else
121106var out = ( text ) = > console . log ( text ) ;
122107var err = ( text ) = > console . error ( text ) ;
108+ #endif
123109
124110// Override this function in a --pre-js file to get a signal for when
125111// compilation is ready. In that callback, call the function run() to start
@@ -170,5 +156,61 @@ var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function',
170156 ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER ;
171157#endif
172158
159+ if ( ENVIRONMENT_IS_WORKER ) {
160+ _scriptDir = self . location . href ;
161+ }
162+ #if ENVIRONMENT_MAY_BE_NODE
163+ else if ( ENVIRONMENT_IS_NODE ) {
164+ _scriptDir = __filename ;
165+ }
166+ #endif
167+
168+ #if ENVIRONMENT_MAY_BE_NODE
169+ if ( ENVIRONMENT_IS_NODE ) {
170+ global . Worker = require ( 'worker_threads' ) . Worker ;
171+ }
172+ #endif
173+
173174var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ( ( typeof document != 'undefined' && document . currentScript ) ? document . currentScript . src : undefined ) ;
174175#endif // PTHREADS
176+
177+ #if ! SINGLE_FILE
178+
179+ #if PTHREADS
180+ if ( ! ENVIRONMENT_IS_PTHREAD ) {
181+ #endif
182+
183+ #if ENVIRONMENT_MAY_BE_NODE && ( ( WASM == 1 && ! WASM2JS ) || WASM == 2 )
184+ // Wasm or Wasm2JS loading:
185+
186+ if ( ENVIRONMENT_IS_NODE ) {
187+ var fs = require ( 'fs' ) ;
188+ #if WASM == 2
189+ if ( typeof WebAssembly != 'undefined' ) Module [ 'wasm' ] = fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm' ) ;
190+ else eval ( fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm.js' ) + '' ) ;
191+ #else
192+ #if ! WASM2JS
193+ Module [ 'wasm' ] = fs . readFileSync ( __dirname + '/{{{ TARGET_BASENAME }}}.wasm' ) ;
194+ #endif
195+ #endif
196+ }
197+ #endif
198+
199+ #if ENVIRONMENT_MAY_BE_SHELL && ( ( WASM == 1 && ! WASM2JS ) || WASM == 2 )
200+ if ( ENVIRONMENT_IS_SHELL ) {
201+ #if WASM == 2
202+ if ( typeof WebAssembly != 'undefined ') Module [ 'wasm '] = read ( '{{{ TARGET_BASENAME }}}.wasm' , 'binary' ) ;
203+ else eval ( read ( '{{{ TARGET_BASENAME }}}.wasm.js' ) + '' ) ;
204+ #else
205+ #if ! WASM2JS
206+ Module [ 'wasm' ] = read ( '{{{ TARGET_BASENAME }}}.wasm' , 'binary' ) ;
207+ #endif
208+ #endif
209+ }
210+ #endif
211+
212+ #if PTHREADS
213+ }
214+ #endif
215+
216+ #endif // !SINGLE_FILE
0 commit comments