Skip to content

Commit 311043c

Browse files
committed
Use new URL pattern in minimal runtime
1 parent ee00f8a commit 311043c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/postamble_minimal.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ var imports = {
182182
};
183183

184184
#if MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
185+
#if EXPORT_ES6 && !ENVIRONMENT_MAY_BE_AUDIO_WORKLET
186+
var moduleUrl = new URL('{{{ TARGET_BASENAME }}}.wasm', import.meta.url);
187+
#endif
188+
#elif !EXPORT_ES6 || AUDIO_WORKLET
189+
var moduleUrl = '{{{ TARGET_BASENAME }}}.wasm';
190+
#else
191+
var moduleUrl = ENVIRONMENT_IS_AUDIO_WORKLET ?
192+
'{{{ TARGET_BASENAME }}}.wasm' :
193+
new URL('{{{ TARGET_BASENAME }}}.wasm', import.meta.url);
194+
#endif
185195
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
186196
#if MIN_SAFARI_VERSION < 150000 || ENVIRONMENT_MAY_BE_NODE
187197
#if ASSERTIONS && !WASM2JS
@@ -197,13 +207,13 @@ instantiatePromise =
197207
// Node's fetch API cannot be used for local files, so we cannot use instantiateStreaming
198208
&& !ENVIRONMENT_IS_NODE
199209
#endif
200-
? WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
210+
? WebAssembly.instantiateStreaming(fetch(moduleUrl), imports)
201211
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
202212
#else
203213
#if AUDIO_WORKLET
204214
instantiatePromise =
205215
#endif
206-
WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports).then((output) => {
216+
WebAssembly.instantiateStreaming(fetch(moduleUrl), imports).then((output) => {
207217
#endif
208218

209219
#else // Non-streaming instantiation

test/test_other.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,21 @@ def test_minimal_runtime_errors(self):
17021702
expected = 'emcc: error: MINIMAL_RUNTIME is not compatible with --preload-file'
17031703
self.assert_fail([EMCC, test_file('hello_world.c'), '-sMINIMAL_RUNTIME', '--preload-file', 'foo'], expected)
17041704

1705+
def test_minimal_runtime_esm_streaming_instantiation(self, args):
1706+
"""Test that MINIMAL_RUNTIME with EXPORT_ES6 and MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
1707+
generates correct URL resolution using import.meta.url."""
1708+
os.mkdir('subdir')
1709+
self.run_process([EMCC, '-o', 'test.mjs',
1710+
'-sMINIMAL_RUNTIME',
1711+
'-sMINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION',
1712+
'-sEXPORT_ES6',
1713+
'-sENVIRONMENT=web',
1714+
test_file('hello_world.c')] + args)
1715+
1716+
src = read_file('test.mjs')
1717+
# Verify that the generated code uses import.meta.url for the wasm URL
1718+
self.assertContained("new URL('test.wasm', import.meta.url)", src)
1719+
17051720
def test_export_all_and_exported_functions(self):
17061721
# EXPORT_ALL should not export library functions by default.
17071722
# This means that to export library function you also need to explicitly

0 commit comments

Comments
 (0)