@@ -349,9 +349,8 @@ def test_emcc_generate_config(self, compiler):
349349 'node': (['-sENVIRONMENT=node'],),
350350 })
351351 def test_emcc_output_mjs(self, args):
352- create_file('extern-post.js', 'await Module();')
353352 self.run_process([EMCC, '-o', 'hello_world.mjs',
354- '--extern-post-js', 'extern-post .js',
353+ '--extern-post-js', test_file('modularize_post_js .js') ,
355354 test_file('hello_world.c')] + args)
356355 src = read_file('hello_world.mjs')
357356 self.assertContained('export default Module;', src)
@@ -363,11 +362,10 @@ def test_emcc_output_mjs(self, args):
363362 })
364363 @node_pthreads
365364 def test_emcc_output_worker_mjs(self, args):
366- create_file('extern-post.js', 'if (!isPthread) await Module();')
367365 os.mkdir('subdir')
368366 self.run_process([EMCC, '-o', 'subdir/hello_world.mjs',
369367 '-sEXIT_RUNTIME', '-sPROXY_TO_PTHREAD', '-pthread', '-O1',
370- '--extern-post-js', 'extern-post .js',
368+ '--extern-post-js', test_file('modularize_post_js .js') ,
371369 test_file('hello_world.c')] + args)
372370 src = read_file('subdir/hello_world.mjs')
373371 self.assertContained("new URL('hello_world.wasm', import.meta.url)", src)
@@ -377,19 +375,17 @@ def test_emcc_output_worker_mjs(self, args):
377375
378376 @node_pthreads
379377 def test_emcc_output_worker_mjs_single_file(self):
380- create_file('extern-post.js', 'await Module();')
381378 self.run_process([EMCC, '-o', 'hello_world.mjs', '-pthread',
382- '--extern-post-js', 'extern-post .js',
379+ '--extern-post-js', test_file('modularize_post_js .js') ,
383380 test_file('hello_world.c'), '-sSINGLE_FILE'])
384381 src = read_file('hello_world.mjs')
385382 self.assertNotContained("new URL('data:", src)
386383 self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src)
387384 self.assertContained('hello, world!', self.run_js('hello_world.mjs'))
388385
389386 def test_emcc_output_mjs_closure(self):
390- create_file('extern-post.js', 'await Module();')
391387 self.run_process([EMCC, '-o', 'hello_world.mjs',
392- '--extern-post-js', 'extern-post .js',
388+ '--extern-post-js', test_file('modularize_post_js .js') ,
393389 test_file('hello_world.c'), '--closure=1'])
394390 src = read_file('hello_world.mjs')
395391 self.assertContained('new URL("hello_world.wasm", import.meta.url)', src)
@@ -1577,25 +1573,20 @@ def test_minimal_modularize_export_keepalive(self):
15771573 EMSCRIPTEN_KEEPALIVE int libf1() { return 42; }
15781574 ''')
15791575
1580- def write_js_main():
1581- """
1582- With MINIMAL_RUNTIME, the module instantiation function isn't exported neither as a UMD nor as an ES6 module.
1583- Thus, it's impossible to use `require` or `import`.
1584-
1585- This function simply appends the instantiation code to the generated code.
1586- """
1587- runtime = read_file('test.js')
1588- write_file('main.js', f'{runtime}\nModule().then((mod) => console.log(mod._libf1()));')
1576+ # With MINIMAL_RUNTIME, the module instantiation function isn't exported neither as a UMD nor as
1577+ # an ES6 module.
1578+ # Thus, it's impossible to use `require` or `import` and instead run the module
1579+ # as part of --extern-post-js.
1580+ create_file('post.js', 'Module().then((mod) => console.log(mod._libf1()));')
1581+ self.emcc_args += ['--extern-post-js=post.js']
15891582
15901583 # By default, no symbols should be exported when using MINIMAL_RUNTIME.
1591- self.emcc('main.c', [], output_filename='test.js')
1592- write_js_main()
1593- self.assertContained('TypeError: mod._libf1 is not a function', self.run_js('main.js', assert_returncode=NON_ZERO))
1584+ self.emcc('main.c', [])
1585+ self.assertContained('TypeError: mod._libf1 is not a function', self.run_js('a.out.js', assert_returncode=NON_ZERO))
15941586
15951587 # Ensures that EXPORT_KEEPALIVE=1 exports the symbols.
1596- self.emcc('main.c', ['-sEXPORT_KEEPALIVE=1'], output_filename='test.js')
1597- write_js_main()
1598- self.assertContained('42\n', self.run_js('main.js'))
1588+ self.emcc('main.c', ['-sEXPORT_KEEPALIVE=1'])
1589+ self.assertContained('42\n', self.run_js('a.out.js'))
15991590
16001591 @crossplatform
16011592 def test_minimal_runtime_export_all_modularize(self):
@@ -6682,7 +6673,12 @@ def test_define_modularize(self):
66826673 create_file('a.out.js', src)
66836674 self.assertContained("define([], () => Module);", src)
66846675
6685- create_file('run_module.js', 'var m; (global.define = (deps, factory) => { m = factory(); }).amd = true; require("./a.out.js"); m();')
6676+ create_file('run_module.js', '''
6677+ var m;
6678+ (global.define = (deps, factory) => { m = factory(); }).amd = true;
6679+ require("./a.out.js");
6680+ m();
6681+ ''')
66866682 output = self.run_js('run_module.js')
66876683 self.assertContained('hello, world!\n', output)
66886684
@@ -10385,15 +10381,15 @@ def test_node_js_pthread_module(self, es6):
1038510381 create_file('moduleLoader.mjs', '''
1038610382 import test_module from "./subdir/module.mjs";
1038710383 test_module().then((test_module_instance) => {
10388- test_module_instance._main( );
10384+ console.log("done" );
1038910385 });
1039010386 ''')
1039110387 else:
1039210388 ext = '.js'
1039310389 create_file('moduleLoader.js', '''
1039410390 const test_module = require("./subdir/module.js");
1039510391 test_module().then((test_module_instance) => {
10396- test_module_instance._main( );
10392+ console.log("done" );
1039710393 });
1039810394 ''')
1039910395 ensure_dir('subdir')
@@ -10403,7 +10399,7 @@ def test_node_js_pthread_module(self, es6):
1040310399
1040410400 # run the module
1040510401 ret = self.run_js('moduleLoader' + ext)
10406- self.assertContained('hello, world!', ret)
10402+ self.assertContained('hello, world!\ndone\n ', ret)
1040710403
1040810404 create_file('workerLoader.js', f'''
1040910405 const {{ Worker, isMainThread }} = require('worker_threads');
@@ -10412,7 +10408,7 @@ def test_node_js_pthread_module(self, es6):
1041210408
1041310409 # run the same module, but inside of a worker
1041410410 ret = self.run_js('workerLoader.js')
10415- self.assertContained('hello, world!', ret)
10411+ self.assertContained('hello, world!\ndone\n ', ret)
1041610412
1041710413 @no_windows('node system() does not seem to work, see https://github.com/emscripten-core/emscripten/pull/10547')
1041810414 @requires_node
@@ -12180,15 +12176,14 @@ def test_standalone_syscalls(self):
1218012176 self.assertContained(expected, self.run_js('test.wasm', engine))
1218112177
1218212178 @parameterized({
12183- 'wasm2js': (['-sWASM=0'], '' ),
12184- 'modularize': (['-sMODULARIZE'] , 'Module()' ),
12179+ 'wasm2js': (['-sWASM=0'],),
12180+ 'modularize': (['-sMODULARIZE', '--extern-post-js', test_file('modularize_post_js.js')], ),
1218512181 })
12186- def test_promise_polyfill(self, constant_args, extern_post_js ):
12182+ def test_promise_polyfill(self, constant_args):
1218712183 def test(args, expect_fail):
1218812184 # legacy browsers may lack Promise, which wasm2js depends on. see what
1218912185 # happens when we kill the global Promise function.
12190- create_file('extern-post.js', extern_post_js)
12191- self.run_process([EMCC, test_file('hello_world.c')] + constant_args + args + ['--extern-post-js', 'extern-post.js'])
12186+ self.run_process([EMCC, test_file('hello_world.c')] + constant_args + args)
1219212187 js = read_file('a.out.js')
1219312188 create_file('a.out.js', 'Promise = undefined;\n' + js)
1219412189 return self.run_js('a.out.js', assert_returncode=NON_ZERO if expect_fail else 0)
0 commit comments