2
2
* @license
3
3
* Copyright 2015 The Emscripten Authors
4
4
* 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).
5
8
*/
6
9
7
10
#if ! USE_PTHREADS
@@ -90,7 +93,7 @@ var LibraryPThread = {
90
93
#if PTHREAD_POOL_SIZE
91
94
var pthreadPoolSize = { { { PTHREAD_POOL_SIZE } } } ;
92
95
// Start loading up the Worker pool, if requested.
93
- for ( var i = 0 ; i < pthreadPoolSize ; ++ i ) {
96
+ while ( pthreadPoolSize -- ) {
94
97
PThread . allocateUnusedWorker ( ) ;
95
98
}
96
99
#endif
@@ -159,11 +162,11 @@ var LibraryPThread = {
159
162
#if PTHREADS_DEBUG
160
163
err ( 'terminateAllThreads' ) ;
161
164
#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 ) ;
167
170
}
168
171
169
172
#if ASSERTIONS
@@ -173,8 +176,7 @@ var LibraryPThread = {
173
176
assert ( PThread . runningWorkers . length === 0 ) ;
174
177
#endif
175
178
176
- for ( var i = 0 ; i < PThread . unusedWorkers . length ; ++ i ) {
177
- var worker = PThread . unusedWorkers [ i ] ;
179
+ for ( var worker of PThread . unusedWorkers ) {
178
180
#if ASSERTIONS
179
181
// This Worker should not be hosting a pthread at this time.
180
182
assert ( ! worker . pthread_ptr ) ;
@@ -223,9 +225,7 @@ var LibraryPThread = {
223
225
#endif
224
226
// Call thread init functions (these are the _emscripten_tls_init for each
225
227
// 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 ( ) ) ;
229
229
} ,
230
230
// Loads the WebAssembly module into the given list of Workers.
231
231
// onFinishedLoading: A callback function that will be called once all of
@@ -281,10 +281,12 @@ var LibraryPThread = {
281
281
// Worker wants to postMessage() to itself to implement setImmediate()
282
282
// emulation.
283
283
worker . postMessage ( d ) ;
284
+ #if expectToReceiveOnModule ( 'onAbort' )
284
285
} else if ( cmd === 'onAbort' ) {
285
286
if ( Module [ 'onAbort' ] ) {
286
287
Module [ 'onAbort' ] ( d [ 'arg' ] ) ;
287
288
}
289
+ #endif
288
290
} else if ( cmd ) {
289
291
// The received message looks like something that should be handled by this message
290
292
// handler, (since there is a e.data.cmd field present), but is not one of the
@@ -599,8 +601,8 @@ var LibraryPThread = {
599
601
$pthreadCreateProxied__internal : true ,
600
602
$pthreadCreateProxied__proxy : 'sync' ,
601
603
$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 ) ;
604
606
} ,
605
607
606
608
// ASan wraps the emscripten_builtin_pthread_create call in
@@ -613,7 +615,7 @@ var LibraryPThread = {
613
615
__pthread_create_js__noleakcheck : true ,
614
616
__pthread_create_js__sig : 'iiiii' ,
615
617
__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 ) {
617
619
if ( typeof SharedArrayBuffer == 'undefined' ) {
618
620
err ( 'Current environment does not support SharedArrayBuffer, pthreads are not available!' ) ;
619
621
return { { { cDefine ( 'EAGAIN' ) } } } ;
@@ -646,8 +648,8 @@ var LibraryPThread = {
646
648
647
649
var offscreenCanvases = { } ; // Dictionary of OffscreenCanvas objects we'll transfer to the created thread to own
648
650
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 ( ) ;
651
653
var offscreenCanvasInfo ;
652
654
try {
653
655
if ( name == '#canvas' ) {
@@ -727,7 +729,7 @@ var LibraryPThread = {
727
729
// need to transfer ownership of objects, then proxy asynchronously via
728
730
// postMessage.
729
731
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 ) ;
731
733
}
732
734
733
735
// If on the main thread, and accessing Canvas/OffscreenCanvas failed, abort
@@ -737,21 +739,21 @@ var LibraryPThread = {
737
739
#if OFFSCREENCANVAS_SUPPORT
738
740
// Register for each of the transferred canvases that the new thread now
739
741
// owns the OffscreenCanvas.
740
- for ( var i in offscreenCanvases ) {
742
+ for ( var canvas of Object . values ( offscreenCanvases ) ) {
741
743
// 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' ) } } } ;
743
745
}
744
746
#endif
745
747
746
748
var threadParams = {
747
- startRoutine : start_routine ,
748
- pthread_ptr : pthread_ptr ,
749
- arg : arg ,
749
+ startRoutine,
750
+ pthread_ptr,
751
+ arg,
750
752
#if OFFSCREENCANVAS_SUPPORT
751
- moduleCanvasId : moduleCanvasId ,
752
- offscreenCanvases : offscreenCanvases ,
753
+ moduleCanvasId,
754
+ offscreenCanvases,
753
755
#endif
754
- transferList : transferList
756
+ transferList,
755
757
} ;
756
758
757
759
if ( ENVIRONMENT_IS_PTHREAD ) {
@@ -838,7 +840,7 @@ var LibraryPThread = {
838
840
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 ) + '!' ;
839
841
#endif
840
842
// Allocate a buffer, which will be copied by the C code.
841
- return withStackSave ( function ( ) {
843
+ return withStackSave ( ( ) => {
842
844
// First passed parameter specifies the number of arguments to the function.
843
845
// When BigInt support is enabled, we must handle types in a more complex
844
846
// way, detecting at runtime if a value is a BigInt or not (as we have no
0 commit comments