Skip to content

Commit

Permalink
Fix EXPORT_ES6 + locateFile (#14242)
Browse files Browse the repository at this point in the history
`Module['locateFile']` needs to be checked even in `EXPORT_ES6` mode.

In #14135 I did this for `new Worker`, but not for Wasm binary locator, so it broke existing usages of EXPORT_ES6 + custom `locateFile` function.
  • Loading branch information
RReverser authored May 21, 2021
1 parent b9a502d commit 3950013
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,12 +747,16 @@ function instrumentWasmTableWithAbort() {
#endif

#if EXPORT_ES6
// Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
var wasmBinaryFile = new URL('{{{ WASM_BINARY_FILE }}}', import.meta.url).toString();
#else
var wasmBinaryFile = '{{{ WASM_BINARY_FILE }}}';
if (!isDataURI(wasmBinaryFile)) {
wasmBinaryFile = locateFile(wasmBinaryFile);
if (Module['locateFile']) {
#endif
var wasmBinaryFile = '{{{ WASM_BINARY_FILE }}}';
if (!isDataURI(wasmBinaryFile)) {
wasmBinaryFile = locateFile(wasmBinaryFile);
}
#if EXPORT_ES6
} else {
// Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
var wasmBinaryFile = new URL('{{{ WASM_BINARY_FILE }}}', import.meta.url).toString();
}
#endif

Expand Down
11 changes: 7 additions & 4 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2702,9 +2702,12 @@ def test_wget_data(self):
create_file('test.txt', 'emscripten')
self.btest(test_file('test_wget_data.c'), expected='1', args=['-O2', '-g2', '-s', 'ASYNCIFY'])

def test_locate_file(self):
@parameterized({
'': ([],),
'es6': (['-s', 'EXPORT_ES6=1'],),
})
def test_locate_file(self, args):
for wasm in [0, 1]:
print('wasm', wasm)
self.clear()
create_file('src.cpp', r'''
#include <stdio.h>
Expand All @@ -2728,7 +2731,7 @@ def test_locate_file(self):
create_file('pre.js', 'Module.locateFile = function(x) { return "sub/" + x };')
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data.txt'], stdout=open('data.js', 'w'))
# put pre.js first, then the file packager data, so locateFile is there for the file loading code
self.compile_btest(['src.cpp', '-O2', '-g', '--pre-js', 'pre.js', '--pre-js', 'data.js', '-o', 'page.html', '-s', 'FORCE_FILESYSTEM', '-s', 'WASM=' + str(wasm)])
self.compile_btest(['src.cpp', '-O2', '-g', '--pre-js', 'pre.js', '--pre-js', 'data.js', '-o', 'page.html', '-s', 'FORCE_FILESYSTEM', '-s', 'WASM=' + str(wasm)] + args)
ensure_dir('sub')
if wasm:
shutil.move('page.wasm', Path('sub/page.wasm'))
Expand All @@ -2752,7 +2755,7 @@ def test_locate_file(self):
</body>
''')

def in_html(expected, args=[]):
def in_html(expected):
self.compile_btest(['src.cpp', '-O2', '-g', '--shell-file', 'shell.html', '--pre-js', 'data.js', '-o', 'page.html', '-s', 'SAFE_HEAP', '-s', 'ASSERTIONS', '-s', 'FORCE_FILESYSTEM', '-s', 'WASM=' + str(wasm)] + args)
if wasm:
shutil.move('page.wasm', Path('sub/page.wasm'))
Expand Down

0 comments on commit 3950013

Please sign in to comment.