Skip to content

Commit 47c54b5

Browse files
committed
Extend test_other.test_mainScriptUrlOrBlob and refactor tests
1 parent 85c84a5 commit 47c54b5

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

test/pthread/main_js_with_loader.html

-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@
147147
};
148148
</script>
149149

150-
<!-- Exactly one of these two scripts will exist -->
151-
<script src="loader.js"></script>
152150
<script type="module" src="loader.mjs"></script>
153151
</body>
154152
</html>

test/test_browser.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -4767,37 +4767,31 @@ def test_pthread_reltime(self):
47674767
def test_mainScriptUrlOrBlob(self, es6, use_blob):
47684768
# TODO: enable this with wasm, currently pthreads/atomics have limitations
47694769
self.set_setting('EXIT_RUNTIME')
4770-
js_name = 'hello_thread_with_loader.js'
4771-
ext = '.js'
4770+
js_name = 'hello_thread_with_loader.%s' % ('mjs' if es6 else 'js')
47724771
if es6:
4773-
ext = '.mjs'
47744772
self.emcc_args += ['-sEXPORT_ES6']
4775-
js_name = 'hello_thread_with_loader.mjs'
47764773
if es6 and use_blob:
47774774
create_file('loader.mjs', '''
47784775
Module['locateFile'] = (path,_prefix) => path;
4779-
fetch('hello_thread_with_loader.mjs').then((rsp) => rsp.blob().then((blob) => {
4780-
Module['mainScriptUrlOrBlob'] = blob;
4781-
import(URL.createObjectURL(blob)).then((fac) => fac.default(Module))
4782-
}));
4776+
let blob = await (await fetch('hello_thread_with_loader.mjs')).blob();
4777+
Module['mainScriptUrlOrBlob'] = blob;
4778+
(await import(URL.createObjectURL(blob))).default(Module);
47834779
''')
47844780
elif use_blob:
4785-
create_file('loader.js', '''
4786-
Module['locateFile'] = (path,_prefix) => path;
4787-
fetch('hello_thread_with_loader.js').then((rsp) => rsp.blob().then((blob) => {
4781+
create_file('loader.mjs', '''
4782+
let blob = await (await fetch('hello_thread_with_loader.js')).blob();
47884783
Module['mainScriptUrlOrBlob'] = blob;
47894784
var script = document.createElement('script');
47904785
script.src = URL.createObjectURL(blob);
47914786
document.body.appendChild(script);
4792-
}));
47934787
''')
47944788
elif es6:
47954789
create_file('loader.mjs', '''
47964790
Module['mainScriptUrlOrBlob'] = 'hello_thread_with_loader.mjs';
4797-
import('./'+Module['mainScriptUrlOrBlob']).then((fac) => fac.default(Module))
4791+
(await import('./hello_thread_with_loader.mjs')).default(Module);
47984792
''')
47994793
else:
4800-
create_file('loader.js', '''
4794+
create_file('loader.mjs', '''
48014795
var script = document.createElement('script');
48024796
Module['mainScriptUrlOrBlob'] = 'hello_thread_with_loader.js';
48034797
script.src = Module['mainScriptUrlOrBlob'];

test/test_other.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -15805,15 +15805,31 @@ def test_rollup(self):
1580515805
def test_rlimit(self):
1580615806
self.do_other_test('test_rlimit.c', emcc_args=['-O1'])
1580715807

15808-
def test_mainScriptUrlOrBlob(self):
15808+
@parameterized({
15809+
'': (False, False),
15810+
'es6': (True, False),
15811+
})
15812+
def test_mainScriptUrlOrBlob(self, es6, use_blob):
1580915813
# Use `foo.js` instead of the current script name when creating new threads
15810-
create_file('pre.js', 'Module = { mainScriptUrlOrBlob: "./foo.js" }')
15811-
self.run_process([EMCC, test_file('hello_world.c'), '-sEXIT_RUNTIME', '-sPROXY_TO_PTHREAD', '-pthread', '--pre-js=pre.js'])
15812-
15813-
# First run without foo.js present to verify that the pthread creation fails
15814-
err = self.run_js('a.out.js', assert_returncode=NON_ZERO)
15815-
self.assertContained('Cannot find module.*foo.js', err, regex=True)
15816-
15817-
# Now create foo.js and the program should run as expected.
15818-
shutil.copy('a.out.js', 'foo.js')
15819-
self.assertContained('hello, world', self.run_js('a.out.js'))
15814+
if es6:
15815+
self.emcc_args += ['-sEXPORT_ES6']
15816+
create_file('pre.js', '')
15817+
create_file('run.mjs', 'import("./foo.mjs").then((fac) => fac.default({mainScriptUrlOrBlob:"./foo.mjs"}))')
15818+
binfile = 'a.out.mjs'
15819+
real_binfile = 'foo.mjs'
15820+
runfile = 'run.mjs'
15821+
else:
15822+
create_file('pre.js', 'Module = { mainScriptUrlOrBlob: "./foo.js" }')
15823+
binfile = 'a.out.js'
15824+
real_binfile = 'foo.js'
15825+
runfile = binfile
15826+
15827+
self.run_process([EMCC, test_file('hello_world.c'), '-sEXIT_RUNTIME', '-sPROXY_TO_PTHREAD', '-pthread', '--pre-js=pre.js', '-o', binfile])
15828+
15829+
# First run without foo.{mjs,js} present to verify that the pthread creation fails
15830+
err = self.run_js(runfile, assert_returncode=NON_ZERO)
15831+
self.assertContained('Cannot find module.*foo', err, regex=True)
15832+
15833+
# Now create foo.{mjs,js} and the program should run as expected.
15834+
shutil.copy(binfile, real_binfile)
15835+
self.assertContained('hello, world', self.run_js(runfile))

0 commit comments

Comments
 (0)