Skip to content

Commit c9e5e0e

Browse files
committed
Make CROSS_ORIGIN pthreads work with EXPORT_ES6
Fixes: #25821
1 parent acb90da commit c9e5e0e

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
@@ -5679,14 +5679,18 @@ def test_rollup(self):
56795679
shutil.copy('hello.wasm', 'dist/')
56805680
self.run_browser('index.html', '/report_result?exit:0')
56815681

5682-
def test_cross_origin(self):
5682+
@parameterized({
5683+
'': ([],),
5684+
'es6': (['-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')],),
5685+
})
5686+
def test_cross_origin(self, args):
56835687
# Verfies that the emscripten-generted JS and Wasm can be hosted on a different origin.
56845688
# This test create a second HTTP server running on port 9999 that servers files from `subdir`.
56855689
# The main html is the servers from the normal 8888 server while the JS and Wasm are hosted
56865690
# on at 9999.
56875691
os.mkdir('subdir')
56885692
create_file('subdir/foo.txt', 'hello')
5689-
self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'])
5693+
self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sRUNTIME_DEBUG', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'] + args)
56905694

56915695
class MyReqestHandler(SimpleHTTPRequestHandler):
56925696
def __init__(self, *args, **kwargs):
@@ -5707,9 +5711,10 @@ def end_headers(self):
57075711

57085712
return SimpleHTTPRequestHandler.end_headers(self)
57095713

5710-
create_file('test.html', '''
5711-
<script src="http://localhost:9999/hello.js"></script>
5712-
''')
5714+
if '-sEXPORT_ES6' in args:
5715+
create_file('test.html', '<script src="http://localhost:9999/hello.js" type="module"></script>')
5716+
else:
5717+
create_file('test.html', '<script src="http://localhost:9999/hello.js"></script>')
57135718

57145719
server = HttpServerThread(ThreadingHTTPServer(('localhost', 9999), MyReqestHandler))
57155720
server.start()

0 commit comments

Comments
 (0)