Skip to content

Commit ba4c5f1

Browse files
authored
Allow use of modern JS in library_pthread.js (#17577)
1 parent 6e1c28e commit ba4c5f1

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/library_pthread.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* @license
33
* Copyright 2015 The Emscripten Authors
44
* SPDX-License-Identifier: MIT
5+
*
6+
* Because only modern JS engines support SAB we can use modern JS language
7+
* features within this file (ES2020).
58
*/
69

710
#if !USE_PTHREADS
@@ -90,7 +93,7 @@ var LibraryPThread = {
9093
#if PTHREAD_POOL_SIZE
9194
var pthreadPoolSize = {{{ PTHREAD_POOL_SIZE }}};
9295
// Start loading up the Worker pool, if requested.
93-
for (var i = 0; i < pthreadPoolSize; ++i) {
96+
while (pthreadPoolSize--) {
9497
PThread.allocateUnusedWorker();
9598
}
9699
#endif
@@ -159,11 +162,11 @@ var LibraryPThread = {
159162
#if PTHREADS_DEBUG
160163
err('terminateAllThreads');
161164
#endif
162-
for (var t in PThread.pthreads) {
163-
var worker = PThread.pthreads[t];
164-
if (worker) {
165-
PThread.returnWorkerToPool(worker);
166-
}
165+
for (var worker of Object.values(PThread.pthreads)) {
166+
#if ASSERTIONS
167+
assert(worker);
168+
#endif
169+
PThread.returnWorkerToPool(worker);
167170
}
168171

169172
#if ASSERTIONS
@@ -173,8 +176,7 @@ var LibraryPThread = {
173176
assert(PThread.runningWorkers.length === 0);
174177
#endif
175178

176-
for (var i = 0; i < PThread.unusedWorkers.length; ++i) {
177-
var worker = PThread.unusedWorkers[i];
179+
for (var worker of PThread.unusedWorkers) {
178180
#if ASSERTIONS
179181
// This Worker should not be hosting a pthread at this time.
180182
assert(!worker.pthread_ptr);
@@ -223,9 +225,7 @@ var LibraryPThread = {
223225
#endif
224226
// Call thread init functions (these are the _emscripten_tls_init for each
225227
// module loaded.
226-
for (var i in PThread.tlsInitFunctions) {
227-
if (PThread.tlsInitFunctions.hasOwnProperty(i)) PThread.tlsInitFunctions[i]();
228-
}
228+
PThread.tlsInitFunctions.forEach((f) => f());
229229
},
230230
// Loads the WebAssembly module into the given list of Workers.
231231
// onFinishedLoading: A callback function that will be called once all of
@@ -281,10 +281,12 @@ var LibraryPThread = {
281281
// Worker wants to postMessage() to itself to implement setImmediate()
282282
// emulation.
283283
worker.postMessage(d);
284+
#if expectToReceiveOnModule('onAbort')
284285
} else if (cmd === 'onAbort') {
285286
if (Module['onAbort']) {
286287
Module['onAbort'](d['arg']);
287288
}
289+
#endif
288290
} else if (cmd) {
289291
// The received message looks like something that should be handled by this message
290292
// handler, (since there is a e.data.cmd field present), but is not one of the
@@ -599,8 +601,8 @@ var LibraryPThread = {
599601
$pthreadCreateProxied__internal: true,
600602
$pthreadCreateProxied__proxy: 'sync',
601603
$pthreadCreateProxied__deps: ['__pthread_create_js'],
602-
$pthreadCreateProxied: function(pthread_ptr, attr, start_routine, arg) {
603-
return ___pthread_create_js(pthread_ptr, attr, start_routine, arg);
604+
$pthreadCreateProxied: function(pthread_ptr, attr, startRoutine, arg) {
605+
return ___pthread_create_js(pthread_ptr, attr, startRoutine, arg);
604606
},
605607

606608
// ASan wraps the emscripten_builtin_pthread_create call in
@@ -613,7 +615,7 @@ var LibraryPThread = {
613615
__pthread_create_js__noleakcheck: true,
614616
__pthread_create_js__sig: 'iiiii',
615617
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied'],
616-
__pthread_create_js: function(pthread_ptr, attr, start_routine, arg) {
618+
__pthread_create_js: function(pthread_ptr, attr, startRoutine, arg) {
617619
if (typeof SharedArrayBuffer == 'undefined') {
618620
err('Current environment does not support SharedArrayBuffer, pthreads are not available!');
619621
return {{{ cDefine('EAGAIN') }}};
@@ -646,8 +648,8 @@ var LibraryPThread = {
646648

647649
var offscreenCanvases = {}; // Dictionary of OffscreenCanvas objects we'll transfer to the created thread to own
648650
var moduleCanvasId = Module['canvas'] ? Module['canvas'].id : '';
649-
for (var i in transferredCanvasNames) {
650-
var name = transferredCanvasNames[i].trim();
651+
for (var name of transferredCanvasNames) {
652+
name = name.trim();
651653
var offscreenCanvasInfo;
652654
try {
653655
if (name == '#canvas') {
@@ -727,7 +729,7 @@ var LibraryPThread = {
727729
// need to transfer ownership of objects, then proxy asynchronously via
728730
// postMessage.
729731
if (ENVIRONMENT_IS_PTHREAD && (transferList.length === 0 || error)) {
730-
return pthreadCreateProxied(pthread_ptr, attr, start_routine, arg);
732+
return pthreadCreateProxied(pthread_ptr, attr, startRoutine, arg);
731733
}
732734

733735
// If on the main thread, and accessing Canvas/OffscreenCanvas failed, abort
@@ -737,21 +739,21 @@ var LibraryPThread = {
737739
#if OFFSCREENCANVAS_SUPPORT
738740
// Register for each of the transferred canvases that the new thread now
739741
// owns the OffscreenCanvas.
740-
for (var i in offscreenCanvases) {
742+
for (var canvas of Object.values(offscreenCanvases)) {
741743
// pthread ptr to the thread that owns this canvas.
742-
{{{ makeSetValue('offscreenCanvases[i].canvasSharedPtr', 8, 'pthread_ptr', 'i32') }}};
744+
{{{ makeSetValue('canvas.canvasSharedPtr', 8, 'pthread_ptr', 'i32') }}};
743745
}
744746
#endif
745747

746748
var threadParams = {
747-
startRoutine: start_routine,
748-
pthread_ptr: pthread_ptr,
749-
arg: arg,
749+
startRoutine,
750+
pthread_ptr,
751+
arg,
750752
#if OFFSCREENCANVAS_SUPPORT
751-
moduleCanvasId: moduleCanvasId,
752-
offscreenCanvases: offscreenCanvases,
753+
moduleCanvasId,
754+
offscreenCanvases,
753755
#endif
754-
transferList: transferList
756+
transferList,
755757
};
756758

757759
if (ENVIRONMENT_IS_PTHREAD) {
@@ -838,7 +840,7 @@ var LibraryPThread = {
838840
if (numCallArgs > {{{ cDefine('EM_QUEUED_JS_CALL_MAX_ARGS') }}}-1) throw 'emscripten_proxy_to_main_thread_js: Too many arguments ' + numCallArgs + ' to proxied function idx=' + index + ', maximum supported is ' + ({{{ cDefine('EM_QUEUED_JS_CALL_MAX_ARGS') }}}-1) + '!';
839841
#endif
840842
// Allocate a buffer, which will be copied by the C code.
841-
return withStackSave(function() {
843+
return withStackSave(() => {
842844
// First passed parameter specifies the number of arguments to the function.
843845
// When BigInt support is enabled, we must handle types in a more complex
844846
// way, detecting at runtime if a value is a BigInt or not (as we have no
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15934
1+
15874

0 commit comments

Comments
 (0)