Skip to content

Commit 38ab8db

Browse files
authored
[esm-integration] Enable direct execution of modules under node (#24112)
1 parent 059c9b9 commit 38ab8db

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

test/common.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,21 +1937,17 @@ def do_run_in_out_file_test(self, srcfile, **kwargs):
19371937
## Does a complete test - builds, runs, checks output, etc.
19381938
def _build_and_run(self, filename, expected_output, args=None,
19391939
no_build=False,
1940-
libraries=None,
1941-
includes=None,
19421940
assert_returncode=0, assert_identical=False, assert_all=False,
1943-
check_for_error=True, force_c=False, emcc_args=None,
1941+
check_for_error=True,
19441942
interleaved_output=True,
19451943
regex=False,
1946-
output_basename=None):
1944+
**kwargs):
19471945
logger.debug(f'_build_and_run: {filename}')
19481946

19491947
if no_build:
19501948
js_file = filename
19511949
else:
1952-
js_file = self.build(filename, libraries=libraries, includes=includes,
1953-
force_c=force_c, emcc_args=emcc_args,
1954-
output_basename=output_basename)
1950+
js_file = self.build(filename, **kwargs)
19551951
self.assertExists(js_file)
19561952

19571953
engines = self.js_engines.copy()

test/core/test_esm_integration.expected.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@
22
// in order to avoid issues with circular dependencies.
33
import * as unused from './hello_world.wasm';
44
export { default, _foo, _main, err, stringToNewUTF8 } from './hello_world.support.mjs';
5+
6+
// When run as the main module under node, execute main directly here
7+
import init from './hello_world.support.mjs';
8+
const isNode = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
9+
if (isNode) {
10+
const url = await import('url');
11+
const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url;
12+
if (isMainModule) await init();
13+
}

test/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9562,6 +9562,12 @@ def test_wasm_worker_malloc(self):
95629562
def test_wasm_worker_wait_async(self):
95639563
self.do_runf('atomic/test_wait_async.c', emcc_args=['-sWASM_WORKERS'])
95649564

9565+
@requires_node_canary
9566+
@no_wasm64("wasm64 requires wasm export wrappers")
9567+
def test_esm_integration_main(self):
9568+
self.node_args += ['--experimental-wasm-modules', '--no-warnings']
9569+
self.do_runf('hello_world.c', 'hello, world!', emcc_args=['-sWASM_ESM_INTEGRATION', '-Wno-experimental'], output_suffix='.mjs')
9570+
95659571
@requires_node_canary
95669572
@no_wasm64("wasm64 requires wasm export wrappers")
95679573
def test_esm_integration(self):

tools/link.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,18 @@ def create_esm_wrapper(wrapper_file, support_target, wasm_target):
21282128
wrapper.append(f"export {{ default, {js_exports} }} from '{support_url}';")
21292129
else:
21302130
wrapper.append(f"export {{ default }} from '{support_url}';")
2131+
2132+
if settings.ENVIRONMENT_MAY_BE_NODE and settings.INVOKE_RUN and settings.EXPECT_MAIN:
2133+
wrapper.append(f'''
2134+
// When run as the main module under node, execute main directly here
2135+
import init from '{support_url}';
2136+
const isNode = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
2137+
if (isNode) {{
2138+
const url = await import('url');
2139+
const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url;
2140+
if (isMainModule) await init();
2141+
}}''')
2142+
21312143
write_file(wrapper_file, '\n'.join(wrapper) + '\n')
21322144

21332145
# FIXME(sbc): This is a huge hack to rename the imports in the

0 commit comments

Comments
 (0)