Skip to content

Commit c93b3a1

Browse files
authored
Extract startWorker helper. NFC (#18313)
1 parent 68e3bb0 commit c93b3a1

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

src/closure-externs/closure-externs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ var EMSCRIPTEN$AWAIT$IMPORT;
1818
// Don't minify createRequire
1919
var createRequire;
2020

21+
// Don't minify startWorker which we use to start workers once the runtime is ready.
22+
/**
23+
* @param {Object} Module
24+
*/
25+
var startWorker = function(Module) {};
26+
2127
// Closure externs used by library_sockfs.js
2228

2329
/**

src/postamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function run(args) {
292292
readyPromiseResolve(Module);
293293
#endif // MODULARIZE
294294
initRuntime();
295-
postMessage({ 'cmd': 'loaded' });
295+
startWorker(Module);
296296
return;
297297
}
298298
#endif

src/postamble_minimal.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,6 @@ WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
246246
#else
247247
ready();
248248
#endif
249-
250-
#if USE_PTHREADS
251-
// This Worker is now ready to host pthreads, tell the main thread we can proceed.
252-
if (ENVIRONMENT_IS_PTHREAD) {
253-
postMessage({ 'cmd': 'loaded' });
254-
}
255-
#endif
256249
}
257250

258251
#if ASSERTIONS || WASM == 2

src/shell_minimal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ function ready() {
119119
#elif ASSERTIONS
120120
out('ready() called, and INVOKE_RUN=0. The runtime is now ready for you to call run() to invoke application _main(). You can also override ready() in a --pre-js file to get this signal as a callback')
121121
#endif
122+
#if USE_PTHREADS
123+
// This Worker is now ready to host pthreads, tell the main thread we can proceed.
124+
if (ENVIRONMENT_IS_PTHREAD) {
125+
startWorker(Module);
126+
}
127+
#endif
122128
}
123129

124130
#if POLYFILL

src/worker.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ self.onunhandledrejection = (e) => {
114114
throw e.reason ?? e;
115115
};
116116

117+
// Add a callback for when the runtime is initialized.
118+
self.startWorker = (instance) => {
119+
#if MODULARIZE
120+
Module = instance;
121+
#endif
122+
// Notify the main thread that this thread has loaded.
123+
postMessage({ 'cmd': 'loaded' });
124+
};
125+
117126
self.onmessage = (e) => {
118127
try {
119128
if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
@@ -167,11 +176,8 @@ self.onmessage = (e) => {
167176
#endif
168177

169178
#if MODULARIZE && EXPORT_ES6
170-
(e.data.urlOrBlob ? import(e.data.urlOrBlob) : import('./{{{ TARGET_JS_NAME }}}')).then(function(exports) {
171-
return exports.default(Module);
172-
}).then(function(instance) {
173-
Module = instance;
174-
});
179+
(e.data.urlOrBlob ? import(e.data.urlOrBlob) : import('./{{{ TARGET_JS_NAME }}}'))
180+
.then(exports => exports.default(Module));
175181
#else
176182
if (typeof e.data.urlOrBlob == 'string') {
177183
#if TRUSTED_TYPES
@@ -194,13 +200,9 @@ self.onmessage = (e) => {
194200
}
195201
#if MODULARIZE
196202
#if MINIMAL_RUNTIME
197-
{{{ EXPORT_NAME }}}(imports).then(function (instance) {
198-
Module = instance;
199-
});
203+
{{{ EXPORT_NAME }}}(imports);
200204
#else
201-
{{{ EXPORT_NAME }}}(Module).then(function (instance) {
202-
Module = instance;
203-
});
205+
{{{ EXPORT_NAME }}}(Module);
204206
#endif
205207
#endif
206208
#endif // MODULARIZE && EXPORT_ES6

0 commit comments

Comments
 (0)