Skip to content

Commit 50532ec

Browse files
authored
Ignore EXPORT_NAME in EXPORT_ES6 mode. NFC (#24138)
The internal name of the factory function is not exposed to the outside world in `EXPORT_ES6` mode, so there is no point in setting this setting.
1 parent 4f5a63b commit 50532ec

File tree

7 files changed

+32
-17
lines changed

7 files changed

+32
-17
lines changed

.circleci/config.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,7 @@ jobs:
730730
- run-tests:
731731
test_targets: "
732732
core0.test_hello_argc
733-
other.test_modularize_incoming
734-
other.test_modularize_incoming_export_name"
733+
other.test_modularize_incoming*"
735734
test-spidermonkey:
736735
executor: linux-python
737736
steps:

src/wasm_worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if (ENVIRONMENT_IS_NODE) {
7171
#endif
7272
importScripts(d.js);
7373
#if MODULARIZE
74-
{{{ EXPORT_NAME }}}(d);
74+
{{{ EXPORT_ES6 ? 'moduleFactory' : EXPORT_NAME }}}(d);
7575
#endif
7676
// Drop now unneeded references to from the Module object in this Worker,
7777
// these are not needed anymore.

test/modularize_post_js.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
// Avoid instantiating the module on pthreads.
88
if (!isPthread)
99
#endif
10+
#if EXPORT_ES6
11+
moduleFactory();
12+
#else
1013
{{{ EXPORT_NAME }}}();
14+
#endif
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1298
1+
1301
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2697
1+
2711

test/test_other.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_esm(self, args):
355355
self.run_process([EMCC, '-o', 'hello_world.mjs',
356356
'--extern-post-js', test_file('modularize_post_js.js'),
357357
test_file('hello_world.c')] + args)
358-
self.assertContained('export default Module;', read_file('hello_world.mjs'))
358+
self.assertContained('export default moduleFactory;', read_file('hello_world.mjs'))
359359
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))
360360

361361
@requires_node_canary
@@ -381,7 +381,7 @@ def test_esm_worker(self, args):
381381
src = read_file('subdir/hello_world.mjs')
382382
self.assertContained("new URL('hello_world.wasm', import.meta.url)", src)
383383
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), {", src)
384-
self.assertContained('export default Module;', src)
384+
self.assertContained('export default moduleFactory;', src)
385385
self.assertContained('hello, world!', self.run_js('subdir/hello_world.mjs'))
386386

387387
@node_pthreads
@@ -405,12 +405,16 @@ def test_esm_closure(self):
405405
def test_esm_implies_modularize(self):
406406
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6'])
407407
src = read_file('a.out.js')
408-
self.assertContained('export default Module;', src)
408+
self.assertContained('export default moduleFactory;', src)
409409

410410
def test_esm_requires_modularize(self):
411411
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sMODULARIZE=0'])
412412
self.assertContained('EXPORT_ES6 requires MODULARIZE to be set', err)
413413

414+
def test_esm_ignore_export_name(self):
415+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXPORT_NAME=Foo', '-Werror'])
416+
self.assertContained('emcc: error: EXPORT_NAME is not used in EXPORT_ES6 mode [-Wunused-command-line-argument] [-Werror]', err)
417+
414418
def test_emcc_out_file(self):
415419
# Verify that "-ofile" works in addition to "-o" "file"
416420
self.run_process([EMCC, '-c', '-ofoo.o', test_file('hello_world.c')])
@@ -7058,8 +7062,7 @@ def test_modularize_new_misuse(self):
70587062

70597063
@parameterized({
70607064
'': ([],),
7061-
'export_name': (['-sEXPORT_NAME=Foo'],),
7062-
'closure': (['-sEXPORT_NAME=Foo', '--closure=1'],),
7065+
'closure': (['--closure=1'],),
70637066
})
70647067
@crossplatform
70657068
def test_modularize_incoming(self, args):
@@ -11160,6 +11163,7 @@ def test_node_js_pthread_module(self, es6):
1116011163
''')
1116111164
else:
1116211165
ext = '.js'
11166+
self.emcc_args += ['-sMODULARIZE', '-sEXPORT_NAME=test_module']
1116311167
create_file('moduleLoader.js', '''
1116411168
const test_module = require("./subdir/module.js");
1116511169
test_module().then((test_module_instance) => {
@@ -11169,7 +11173,7 @@ def test_node_js_pthread_module(self, es6):
1116911173
ensure_dir('subdir')
1117011174

1117111175
# build hello_world.c
11172-
self.run_process([EMCC, test_file('hello_world.c'), '-o', 'subdir/module' + ext, '-pthread', '-sPTHREAD_POOL_SIZE=2', '-sMODULARIZE', '-sEXPORT_NAME=test_module'] + self.get_emcc_args())
11176+
self.run_process([EMCC, test_file('hello_world.c'), '-o', 'subdir/module' + ext, '-pthread', '-sPTHREAD_POOL_SIZE=2'] + self.get_emcc_args())
1117311177

1117411178
# run the module
1117511179
ret = self.run_js('moduleLoader' + ext)

tools/link.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,10 @@ def get_full_import_name(name):
17961796
if not js_manipulation.isidentifier(settings.EXPORT_NAME):
17971797
exit_with_error(f'EXPORT_NAME is not a valid JS identifier: `{settings.EXPORT_NAME}`')
17981798

1799+
if settings.EXPORT_ES6 and 'EXPORT_NAME' in user_settings:
1800+
diagnostics.warning('unused-command-line-argument', 'EXPORT_NAME is not used in EXPORT_ES6 mode')
1801+
settings.EXPORT_NAME = None
1802+
17991803
if settings.EMSCRIPTEN_TRACING:
18001804
add_system_js_lib('libtrace.js')
18011805
if settings.ALLOW_MEMORY_GROWTH:
@@ -2464,11 +2468,15 @@ def modularize():
24642468
'maybe_async': maybe_async,
24652469
'generated_js': generated_js
24662470
}
2471+
if settings.EXPORT_ES6:
2472+
factory_name = 'moduleFactory'
2473+
else:
2474+
factory_name = settings.EXPORT_NAME
24672475

24682476
if settings.MINIMAL_RUNTIME and not settings.PTHREADS:
24692477
# Single threaded MINIMAL_RUNTIME programs do not need access to
24702478
# document.currentScript, so a simple export declaration is enough.
2471-
src = f'var {settings.EXPORT_NAME} = {wrapper_function};'
2479+
src = f'var {factory_name} = {wrapper_function};'
24722480
else:
24732481
script_url_web = ''
24742482
# When MODULARIZE this JS may be executed later,
@@ -2477,12 +2485,12 @@ def modularize():
24772485
if settings.ENVIRONMENT_MAY_BE_WEB and not settings.EXPORT_ES6:
24782486
script_url_web = "var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;"
24792487
src = '''\
2480-
var %(EXPORT_NAME)s = (() => {
2488+
var %(factory_name)s = (() => {
24812489
%(script_url_web)s
24822490
return (%(wrapper_function)s);
24832491
})();
24842492
''' % {
2485-
'EXPORT_NAME': settings.EXPORT_NAME,
2493+
'factory_name': factory_name,
24862494
'script_url_web': script_url_web,
24872495
'wrapper_function': wrapper_function,
24882496
}
@@ -2497,11 +2505,11 @@ def modularize():
24972505
# Module variable name. This should happen even in MINIMAL_RUNTIME builds
24982506
# for MODULARIZE and EXPORT_ES6 to work correctly.
24992507
if settings.AUDIO_WORKLET:
2500-
src += f'globalThis.AudioWorkletModule = {settings.EXPORT_NAME};\n'
2508+
src += f'globalThis.AudioWorkletModule = {factory_name};\n'
25012509

25022510
# Export using a UMD style export, or ES6 exports if selected
25032511
if settings.EXPORT_ES6:
2504-
src += 'export default %s;\n' % settings.EXPORT_NAME
2512+
src += 'export default moduleFactory;\n'
25052513
elif not settings.MINIMAL_RUNTIME:
25062514
src += '''\
25072515
if (typeof exports === 'object' && typeof module === 'object') {
@@ -2530,7 +2538,7 @@ def modularize():
25302538
if settings.MODULARIZE == 'instance':
25312539
src += 'isPthread && init();\n'
25322540
else:
2533-
src += 'isPthread && %s();\n' % settings.EXPORT_NAME
2541+
src += f'isPthread && {factory_name}();\n'
25342542

25352543
final_js += '.modular.js'
25362544
write_file(final_js, src)

0 commit comments

Comments
 (0)