Skip to content

Commit ff60f9c

Browse files
authored
[esm-integration] Enabled runtime symbol exports (#24065)
1 parent 470dc54 commit ff60f9c

8 files changed

+29
-13
lines changed

src/modules.mjs

+17-5
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ function addMissingLibraryStubs(unusedLibSymbols) {
406406
}
407407

408408
function exportSymbol(name) {
409+
if (WASM_ESM_INTEGRATION) {
410+
// In ESM integration mode symbols are exported by being included in
411+
// an export { foo, bar } list so we build up the simple list of names
412+
return name;
413+
}
409414
if (MODULARIZE === 'instance') {
410415
return `__exp_${name} = ${name};`;
411416
}
@@ -416,10 +421,13 @@ function exportSymbol(name) {
416421
function exportRuntimeSymbols() {
417422
// optionally export something.
418423
function maybeExport(name) {
419-
// If requested to be exported, export it. HEAP objects are exported
420-
// separately in updateMemoryViews
421-
if (EXPORTED_RUNTIME_METHODS.has(name) && !name.startsWith('HEAP')) {
422-
return exportSymbol(name);
424+
// If requested to be exported, export it.
425+
if (EXPORTED_RUNTIME_METHODS.has(name)) {
426+
// Unless we are in WASM_ESM_INTEGRATION mode then HEAP objects are
427+
// exported separately in updateMemoryViews
428+
if (WASM_ESM_INTEGRATION || !name.startsWith('HEAP')) {
429+
return exportSymbol(name);
430+
}
423431
}
424432
}
425433

@@ -518,6 +526,10 @@ function exportRuntimeSymbols() {
518526
const exports = runtimeElements.map(maybeExport);
519527
const results = exports.filter((name) => name);
520528

529+
if (WASM_ESM_INTEGRATION) {
530+
return '// Runtime exports\nexport { ' + results.join(', ') + ' };\n';
531+
}
532+
521533
if (ASSERTIONS && !EXPORT_ALL) {
522534
// in ASSERTIONS mode we show a useful error if it is used without being
523535
// exported. See `unexportedRuntimeSymbol` in runtime_debug.js.
@@ -563,7 +575,7 @@ function exportLibrarySymbols() {
563575
function exportJSSymbols() {
564576
// In WASM_ESM_INTEGRATION mode JS library symbols are marked with `export`
565577
// at the point of declaration.
566-
if (WASM_ESM_INTEGRATION) return '';
578+
if (WASM_ESM_INTEGRATION) return exportRuntimeSymbols();
567579
return exportRuntimeSymbols() + ' ' + exportLibrarySymbols();
568580
}
569581

src/postlibrary.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ function processModuleArgs()
3434
#endif
3535
#endif // ASSERTIONS
3636

37-
{{{ exportJSSymbols() }}}
3837
}
38+
39+
{{{ exportJSSymbols() }}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { __main_argc_argv as main } from './hello_world.wasm';
2+
export { default, err, HEAPF32, HEAPF64, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAP64, HEAPU64, stringToNewUTF8 } from './hello_world.support.mjs';
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
51932
1+
51931
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27424
1+
27423
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
49920
1+
49919

test/test_core.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9568,13 +9568,14 @@ def test_esm_integration(self):
95689568
# TODO(sbc): WASM_ESM_INTEGRATION doesn't currently work with closure.
95699569
# self.maybe_closure()
95709570
self.node_args += ['--experimental-wasm-modules', '--no-warnings']
9571-
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sEXPORTED_FUNCTIONS=_main,stringToNewUTF8', '-sWASM_ESM_INTEGRATION', '-Wno-experimental', test_file('hello_world_argv.c')] + self.get_emcc_args())
9571+
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sEXPORTED_RUNTIME_METHODS=err', '-sEXPORTED_FUNCTIONS=_main,stringToNewUTF8', '-sWASM_ESM_INTEGRATION', '-Wno-experimental', test_file('hello_world_argv.c')] + self.get_emcc_args())
95729572
create_file('runner.mjs', '''
9573-
import init, { stringToNewUTF8, main } from "./hello_world.mjs";
9573+
import init, { err, stringToNewUTF8, main } from "./hello_world.mjs";
95749574
await init({arguments: ['foo', 'bar']});
9575-
console.log('this is a pointer:', stringToNewUTF8('hello'));
9575+
err('this is a pointer:', stringToNewUTF8('hello'));
95769576
''')
95779577
self.assertContained('hello, world! (3)', self.run_js('runner.mjs'))
9578+
self.assertFileContents(test_file('core/test_esm_integration.expected.mjs'), read_file('hello_world.mjs'))
95789579

95799580

95809581
# Generate tests for everything

tools/link.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ def create_worker_file(input_file, target_dir, output_file, options):
21142114

21152115
def create_esm_wrapper(wrapper_file, support_target, wasm_target):
21162116
wasm_exports = []
2117-
js_exports = []
2117+
js_exports = list(settings.EXPORTED_RUNTIME_METHODS)
21182118
for f in settings.USER_EXPORTS:
21192119
if f == '_main' and '__main_argc_argv' in settings.WASM_EXPORTS:
21202120
wasm_exports.append('__main_argc_argv as main')

0 commit comments

Comments
 (0)