Skip to content

Commit 5fc175d

Browse files
authored
Use consistent helper for tests that use -sMODULARIZE (emscripten-core#22537)
As part of this I also extended the `#preprocess` directive so it can be used with `--extern-pre-js` and `--extern-post-js` as well as the existing supprot for `--pre-js` and `--post-js`.
1 parent 3d58456 commit 5fc175d

File tree

5 files changed

+51
-45
lines changed

5 files changed

+51
-45
lines changed

test/modularize_post_js.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#preprocess
2+
3+
// This file gets included via `--extern-post-js` in order to run tests
4+
// that are built with `-sMODULARIZE`.
5+
6+
#if PTHREADS
7+
// Avoid instantiating the module on pthreads.
8+
if (!isPthread)
9+
#endif
10+
{{{ EXPORT_NAME }}}();

test/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def check_js_engines():
114114

115115
def get_and_import_modules():
116116
modules = []
117-
for filename in glob.glob(os.path.join(os.path.dirname(__file__), 'test*.py')):
117+
for filename in glob.glob(os.path.join(common.TEST_ROOT, 'test*.py')):
118118
module_dir, module_file = os.path.split(filename)
119119
module_name, module_ext = os.path.splitext(module_file)
120120
__import__(module_name)

test/test_core.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,10 +2468,7 @@ def test_pthread_proxying(self, modularize):
24682468
if modularize:
24692469
self.set_setting('MODULARIZE')
24702470
self.set_setting('EXPORT_NAME=ModuleFactory')
2471-
# Only instantiate the module on the main thread.
2472-
create_file('extern-post.js',
2473-
'if (typeof importScripts != "function") ModuleFactory();')
2474-
args = ['--extern-post-js=extern-post.js']
2471+
args = ['--extern-post-js', test_file('modularize_post_js.js')]
24752472
self.do_run_in_out_file_test('pthread/test_pthread_proxying.c',
24762473
interleaved_output=False, emcc_args=args)
24772474

@@ -6266,8 +6263,7 @@ def test_unicode_js_library(self):
62666263
err = self.expect_fail([PYTHON, 'expect_fail.py'], expect_traceback=True)
62676264
self.assertContained('UnicodeDecodeError', err)
62686265

6269-
create_file('modularize_post.js', '(async function main(){await Module();})()')
6270-
self.emcc_args += ['-sMODULARIZE', '--js-library', test_file('unicode_library.js'), '--extern-post-js', 'modularize_post.js', '--post-js', test_file('unicode_postjs.js')]
6266+
self.emcc_args += ['-sMODULARIZE', '--js-library', test_file('unicode_library.js'), '--extern-post-js', test_file('modularize_post_js.js'), '--post-js', test_file('unicode_postjs.js')]
62716267
self.do_run_in_out_file_test('test_unicode_js_library.c')
62726268

62736269
def test_funcptr_import_type(self):
@@ -7924,14 +7920,13 @@ def get_wat_addr(call_index):
79247920
def test_modularize_closure_pre(self):
79257921
# test that the combination of modularize + closure + pre-js works. in that mode,
79267922
# closure should not minify the Module object in a way that the pre-js cannot use it.
7927-
create_file('post.js', 'var TheModule = Module();\n')
79287923
if self.is_wasm2js():
79297924
# TODO(sbc): Fix closure warnings with MODULARIZE + WASM=0
79307925
self.ldflags.append('-Wno-error=closure')
79317926

79327927
self.emcc_args += [
79337928
'--pre-js', test_file('core/modularize_closure_pre.js'),
7934-
'--extern-post-js=post.js',
7929+
'--extern-post-js', test_file('modularize_post_js.js'),
79357930
'--closure=1',
79367931
'-g1',
79377932
'-sMODULARIZE',
@@ -9222,8 +9217,7 @@ def test_pthread_offset_converter_modularize(self):
92229217
self.set_setting('USE_OFFSET_CONVERTER')
92239218
self.set_setting('MODULARIZE')
92249219
self.set_setting('EXPORT_NAME', 'foo')
9225-
create_file('post.js', 'if (!isPthread) foo();')
9226-
self.emcc_args += ['--extern-post-js', 'post.js']
9220+
self.emcc_args += ['--extern-post-js', test_file('modularize_post_js.js')]
92279221
if '-g' in self.emcc_args:
92289222
self.emcc_args += ['-DDEBUG']
92299223
self.do_runf('core/test_return_address.c', 'passed')

test/test_other.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tools/link.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,14 @@ def fix_windows_newlines(text):
301301

302302

303303
def read_js_files(files):
304-
contents = '\n'.join(read_file(f) for f in files)
304+
contents = []
305+
for f in files:
306+
content = read_file(f)
307+
if content.startswith('#preprocess\n'):
308+
contents.append(shared.read_and_preprocess(f, expand_macros=True))
309+
else:
310+
contents.append(content)
311+
contents = '\n'.join(contents)
305312
return fix_windows_newlines(contents)
306313

307314

0 commit comments

Comments
 (0)