Skip to content

Commit 309a955

Browse files
authored
Make CROSS_ORIGIN pthreads work with EXPORT_ES6 (#25823)
Fixes: #25821
1 parent 91fefbf commit 309a955

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/lib/libpthread.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ var LibraryPThread = {
445445
}
446446
worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}});
447447
} else
448+
#endif
449+
#if CROSS_ORIGIN && ENVIRONMENT_MAY_BE_WEB
450+
// Support cross-origin loading by creating a new Blob URL to actually
451+
// perform the `import`. Without this the `new Worker` would fail
452+
// due to CORS restrictions.
453+
// https://github.com/emscripten-core/emscripten/issues/21937
454+
if (ENVIRONMENT_IS_WEB) {
455+
var url = URL.createObjectURL(new Blob([`import '${import.meta.url}'`], { type: 'application/javascript' }));
456+
worker = new Worker(url, {{{ pthreadWorkerOptions }}});
457+
} else
448458
#endif
449459
// We need to generate the URL with import.meta.url as the base URL of the JS file
450460
// instead of just using new URL(import.meta.url) because bundler's only recognize

test/test_browser.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5664,14 +5664,18 @@ def test_rollup(self):
56645664
shutil.copy('hello.wasm', 'dist/')
56655665
self.run_browser('index.html', '/report_result?exit:0')
56665666

5667-
def test_cross_origin(self):
5667+
@parameterized({
5668+
'': ([],),
5669+
'es6': (['-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')],),
5670+
})
5671+
def test_cross_origin(self, args):
56685672
# Verfies that the emscripten-generted JS and Wasm can be hosted on a different origin.
56695673
# This test create a second HTTP server running on port 9999 that servers files from `subdir`.
56705674
# The main html is the servers from the normal 8888 server while the JS and Wasm are hosted
56715675
# on at 9999.
56725676
os.mkdir('subdir')
56735677
create_file('subdir/foo.txt', 'hello')
5674-
self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'])
5678+
self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sRUNTIME_DEBUG', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'] + args)
56755679

56765680
class MyReqestHandler(SimpleHTTPRequestHandler):
56775681
def __init__(self, *args, **kwargs):
@@ -5692,9 +5696,10 @@ def end_headers(self):
56925696

56935697
return SimpleHTTPRequestHandler.end_headers(self)
56945698

5695-
create_file('test.html', '''
5696-
<script src="http://localhost:9999/hello.js"></script>
5697-
''')
5699+
if '-sEXPORT_ES6' in args:
5700+
create_file('test.html', '<script src="http://localhost:9999/hello.js" type="module"></script>')
5701+
else:
5702+
create_file('test.html', '<script src="http://localhost:9999/hello.js"></script>')
56985703

56995704
server = HttpServerThread(ThreadingHTTPServer(('localhost', 9999), MyReqestHandler))
57005705
server.start()

0 commit comments

Comments
 (0)