@@ -677,13 +677,6 @@ def phase_linker_setup(options, state, newargs):
677
677
options .extern_pre_js = read_js_files (options .extern_pre_js )
678
678
options .extern_post_js = read_js_files (options .extern_post_js )
679
679
680
- if settings .PTHREADS :
681
- # Don't run extern pre/post code on pthreads.
682
- if options .extern_pre_js :
683
- options .extern_pre_js = 'if (!isPthread) {' + options .extern_pre_js + '}'
684
- if options .extern_post_js :
685
- options .extern_post_js = 'if (!isPthread) {' + options .extern_post_js + '}'
686
-
687
680
# TODO: support source maps with js_transform
688
681
if options .js_transform and settings .GENERATE_SOURCE_MAP :
689
682
logger .warning ('disabling source maps because a js transform is being done' )
@@ -2082,7 +2075,7 @@ def phase_final_emitting(options, state, target, wasm_target):
2082
2075
create_worker_file ('src/audio_worklet.js' , target_dir , settings .AUDIO_WORKLET_FILE , options )
2083
2076
2084
2077
if settings .MODULARIZE :
2085
- modularize (options )
2078
+ modularize ()
2086
2079
elif settings .USE_CLOSURE_COMPILER :
2087
2080
module_export_name_substitution ()
2088
2081
@@ -2109,8 +2102,6 @@ def phase_final_emitting(options, state, target, wasm_target):
2109
2102
src = read_file (final_js )
2110
2103
final_js += '.epp.js'
2111
2104
with open (final_js , 'w' , encoding = 'utf-8' ) as f :
2112
- if settings .PTHREADS and (options .extern_pre_js or options .extern_post_js ):
2113
- f .write (make_pthread_detection ())
2114
2105
f .write (options .extern_pre_js )
2115
2106
f .write (src )
2116
2107
f .write (options .extern_post_js )
@@ -2352,34 +2343,7 @@ def node_pthread_detection():
2352
2343
return "require('worker_threads').workerData === 'em-pthread'\n "
2353
2344
2354
2345
2355
- def make_pthread_detection ():
2356
- """Create code for detecting if we are running in a pthread.
2357
-
2358
- Normally this detection is done when the module is itself is run but there
2359
- are some cases were we need to do this detection outside of the normal
2360
- module code.
2361
-
2362
- 1. When running in MODULARIZE mode we need use this to know if we should
2363
- run the module constructor on startup (true only for pthreads)
2364
- 2. When using `--extern-pre-js` and `--extern-post-js` we need to avoid
2365
- running this code on pthreads.
2366
- """
2367
-
2368
- if settings .ENVIRONMENT_MAY_BE_WEB or settings .ENVIRONMENT_MAY_BE_WORKER :
2369
- code = "var isPthread = globalThis.self?.name === 'em-pthread';\n "
2370
- # In order to support both web and node we also need to detect node here.
2371
- if settings .ENVIRONMENT_MAY_BE_NODE :
2372
- code += "var isNode = typeof globalThis.process?.versions?.node == 'string';\n "
2373
- code += f'if (isNode) isPthread = { node_pthread_detection ()} \n '
2374
- elif settings .ENVIRONMENT_MAY_BE_NODE :
2375
- code = f'var isPthread = { node_pthread_detection ()} \n '
2376
- else :
2377
- assert False
2378
-
2379
- return code
2380
-
2381
-
2382
- def modularize (options ):
2346
+ def modularize ():
2383
2347
global final_js
2384
2348
logger .debug (f'Modularizing, assigning to var { settings .EXPORT_NAME } ' )
2385
2349
src = read_file (final_js )
@@ -2461,8 +2425,18 @@ def modularize(options):
2461
2425
''' % {'EXPORT_NAME' : settings .EXPORT_NAME }
2462
2426
2463
2427
if settings .PTHREADS :
2464
- if not options .extern_pre_js and not options .extern_post_js :
2465
- src += make_pthread_detection ()
2428
+ # Create code for detecting if we are running in a pthread.
2429
+ # Normally this detection is done when the module is itself run but
2430
+ # when running in MODULARIZE mode we need use this to know if we should
2431
+ # run the module constructor on startup (true only for pthreads).
2432
+ if settings .ENVIRONMENT_MAY_BE_WEB or settings .ENVIRONMENT_MAY_BE_WORKER :
2433
+ src += "var isPthread = globalThis.self?.name === 'em-pthread';\n "
2434
+ # In order to support both web and node we also need to detect node here.
2435
+ if settings .ENVIRONMENT_MAY_BE_NODE :
2436
+ src += "var isNode = typeof globalThis.process?.versions?.node == 'string';\n "
2437
+ src += f'if (isNode) isPthread = { node_pthread_detection ()} \n '
2438
+ elif settings .ENVIRONMENT_MAY_BE_NODE :
2439
+ src += f'var isPthread = { node_pthread_detection ()} \n '
2466
2440
src += '// When running as a pthread, construct a new instance on startup\n '
2467
2441
src += 'isPthread && %s();\n ' % settings .EXPORT_NAME
2468
2442
0 commit comments