Skip to content

Commit e199ce2

Browse files
committed
Move more code out of worker.js
Followup to #17087.
1 parent 694cf59 commit e199ce2

File tree

3 files changed

+29
-58
lines changed

3 files changed

+29
-58
lines changed

src/library.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,23 +3422,33 @@ mergeInto(LibraryManager.library, {
34223422
throw UTF8ToString(str);
34233423
},
34243424

3425-
#if !MINIMAL_RUNTIME
34263425
$handleException: function(e) {
34273426
// Certain exception types we do not treat as errors since they are used for
34283427
// internal control flow.
34293428
// 1. ExitStatus, which is thrown by exit()
34303429
// 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others
34313430
// that wish to return to JS event loop.
3431+
#if MINIMAL_RUNTIME
3432+
if (e != 'unwind') {
3433+
throw e;
3434+
}
3435+
#else
34323436
if (e instanceof ExitStatus || e == 'unwind') {
34333437
return EXITSTATUS;
34343438
}
3435-
#if MINIMAL_RUNTIME
3436-
throw e;
3437-
#else
3439+
#if USE_PTHREADS
3440+
if (ENVIRONMENT_IS_PTHREAD) {
3441+
// In pthreads we allow exceptions to flow all the way
3442+
// out the top level where the top-level handler reports
3443+
// them back the main thread.
3444+
throw e;
3445+
}
3446+
#endif
34383447
quit_(1, e);
34393448
#endif
34403449
},
34413450

3451+
#if !MINIMAL_RUNTIME
34423452
// Callable in pthread without __proxy needed.
34433453
$runtimeKeepalivePush__sig: 'v',
34443454
$runtimeKeepalivePush: function() {

src/library_pthread.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -806,25 +806,17 @@ var LibraryPThread = {
806806
// that the entire process should exit.
807807
// This function is always called from a pthread, but is executed on the
808808
// main thread due the __proxy attribute.
809-
$exitOnMainThread__deps: ['exit',
810-
#if !MINIMAL_RUNTIME
811-
'$handleException',
812-
#endif
813-
],
809+
$exitOnMainThread__deps: ['exit', '$handleException'],
814810
$exitOnMainThread__proxy: 'async',
815811
$exitOnMainThread: function(returnCode) {
816812
#if PTHREADS_DEBUG
817813
err('exitOnMainThread');
818814
#endif
819-
#if MINIMAL_RUNTIME
820-
_exit(returnCode);
821-
#else
822815
try {
823816
_exit(returnCode);
824817
} catch (e) {
825818
handleException(e);
826819
}
827-
#endif
828820
},
829821

830822
emscripten_proxy_to_main_thread_js__deps: ['$withStackSave', 'emscripten_run_in_main_runtime_thread_js'],
@@ -952,7 +944,7 @@ var LibraryPThread = {
952944
#endif
953945
},
954946

955-
$invokeEntryPoint__deps: ['_emscripten_thread_exit'],
947+
$invokeEntryPoint__deps: ['$handleException'],
956948
$invokeEntryPoint: function(ptr, arg) {
957949
#if PTHREADS_DEBUG
958950
err('invokeEntryPoint: ' + ptrToString(ptr));
@@ -972,22 +964,23 @@ var LibraryPThread = {
972964
// *ThreadMain(void *arg) form, or try linking with the Emscripten linker
973965
// flag -sEMULATE_FUNCTION_POINTER_CASTS to add in emulation for this x86
974966
// ABI extension.
975-
var result = {{{ makeDynCall('ii', 'ptr') }}}(arg);
967+
try {
968+
var result = {{{ makeDynCall('ii', 'ptr') }}}(arg);
976969
#if STACK_OVERFLOW_CHECK
977-
checkStackCookie();
970+
checkStackCookie();
978971
#endif
979972
#if MINIMAL_RUNTIME
980-
// In MINIMAL_RUNTIME the noExitRuntime concept does not apply to
981-
// pthreads. To exit a pthread with live runtime, use the function
982-
// emscripten_unwind_to_js_event_loop() in the pthread body.
983-
__emscripten_thread_exit(result);
984-
#else
985-
if (keepRuntimeAlive()) {
986-
PThread.setExitStatus(result);
987-
} else {
988973
__emscripten_thread_exit(result);
989-
}
974+
#else
975+
if (keepRuntimeAlive()) {
976+
PThread.setExitStatus(result);
977+
} else {
978+
__emscripten_thread_exit(result);
979+
}
990980
#endif
981+
} catch (e) {
982+
handleException(e);
983+
}
991984
},
992985

993986
$executeNotifiedProxyingQueue: function(queue) {

src/worker.js

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -238,39 +238,7 @@ self.onmessage = (e) => {
238238
initializedJS = true;
239239
}
240240

241-
try {
242-
Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
243-
} catch(ex) {
244-
if (ex != 'unwind') {
245-
// ExitStatus not present in MINIMAL_RUNTIME
246-
#if !MINIMAL_RUNTIME
247-
if (ex instanceof Module['ExitStatus']) {
248-
if (Module['keepRuntimeAlive']()) {
249-
#if ASSERTIONS
250-
err('Pthread 0x' + Module['_pthread_self']().toString(16) + ' called exit(), staying alive due to noExitRuntime.');
251-
#endif
252-
} else {
253-
#if ASSERTIONS
254-
err('Pthread 0x' + Module['_pthread_self']().toString(16) + ' called exit(), calling _emscripten_thread_exit.');
255-
#endif
256-
Module['__emscripten_thread_exit'](ex.status);
257-
}
258-
}
259-
else
260-
#endif // !MINIMAL_RUNTIME
261-
{
262-
// The pthread "crashed". Do not call `_emscripten_thread_exit` (which
263-
// would make this thread joinable. Instead, re-throw the exception
264-
// and let the top level handler propagate it back to the main thread.
265-
throw ex;
266-
}
267-
#if ASSERTIONS
268-
} else {
269-
// else e == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
270-
err('Pthread 0x' + Module['_pthread_self']().toString(16) + ' completed its main entry point with an `unwind`, keeping the worker alive for asynchronous operation.');
271-
#endif
272-
}
273-
}
241+
Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
274242
} else if (e.data.cmd === 'cancel') { // Main thread is asking for a pthread_cancel() on this thread.
275243
if (Module['_pthread_self']()) {
276244
Module['__emscripten_thread_exit']({{{ cDefine('PTHREAD_CANCELED') }}});

0 commit comments

Comments
 (0)