Skip to content

Commit aecf058

Browse files
committed
pythongh-126898 Switch to exporting an es6 module and using a node_entry script
1 parent 2313f84 commit aecf058

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

Tools/wasm/emscripten/__main__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import sysconfig
1717
import tempfile
1818

19-
WASM_DIR = Path(__file__).parent.parent
20-
CHECKOUT = WASM_DIR.parent.parent
19+
EMSCRIPTEN_DIR = Path(__file__).parent
20+
CHECKOUT = EMSCRIPTEN_DIR.parent.parent.parent
2121

2222
CROSS_BUILD_DIR = CHECKOUT / "cross-build"
2323
BUILD_DIR = CROSS_BUILD_DIR / "build"
@@ -207,7 +207,9 @@ def configure_emscripten_python(context, working_dir):
207207
quiet=context.quiet,
208208
)
209209

210-
python_js = working_dir / "python.js"
210+
shutil.copy(EMSCRIPTEN_DIR / "node_entry.mjs", working_dir / "node_entry.mjs")
211+
212+
python_js = working_dir / "node_entry.mjs"
211213
exec_script = working_dir / "python.sh"
212214
exec_script.write_text(f'#!/bin/sh\nexec {host_runner} {python_js} "$@"\n')
213215
exec_script.chmod(0o755)

Tools/wasm/emscripten/node_entry.mjs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import EmscriptenModule from "./python.mjs";
2+
import { dirname } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
if (typeof process !== "undefined") {
6+
const nodeVersion = Number(process.versions.node.split(".", 1)[0]);
7+
if (nodeVersion < 18) {
8+
process.stderr.write(
9+
`Node version must be >= 18, got version ${process.version}\n`,
10+
);
11+
process.exit(1);
12+
}
13+
}
14+
15+
const settings = {
16+
preRun(Module) {
17+
const __dirname = dirname(fileURLToPath(import.meta.url));
18+
Module.FS.mkdirTree("/lib/");
19+
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: __dirname + "/lib/" }, "/lib/");
20+
}
21+
};
22+
23+
await EmscriptenModule(settings);

Tools/wasm/emscripten/node_pre.js

-15
This file was deleted.

configure

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+2-2
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ AC_ARG_WITH([suffix],
13271327
)
13281328
], [
13291329
AS_CASE([$ac_sys_system],
1330-
[Emscripten], [EXEEXT=.js],
1330+
[Emscripten], [EXEEXT=.mjs],
13311331
[WASI], [EXEEXT=.wasm],
13321332
[EXEEXT=]
13331333
)
@@ -2328,6 +2328,7 @@ AS_CASE([$ac_sys_system],
23282328
23292329
dnl Include file system support
23302330
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
2331+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXPORTED_RUNTIME_METHODS=FS"])
23312332
23322333
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
23332334
AS_VAR_APPEND([LINKFORSHARED], [" -sMAIN_MODULE"])
@@ -2341,7 +2342,6 @@ AS_CASE([$ac_sys_system],
23412342
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sALLOW_MEMORY_GROWTH"])
23422343
dnl not completely sure whether or not we want -sEXIT_RUNTIME, keeping it for now.
23432344
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXIT_RUNTIME"])
2344-
AS_VAR_APPEND([LDFLAGS_NODIST], [" --pre-js=\$(srcdir)/Tools/wasm/emscripten/node_pre.js"])
23452345
WASM_LINKFORSHARED_DEBUG="-gseparate-dwarf --emit-symbol-map"
23462346
23472347
AS_VAR_IF([wasm_debug], [yes], [

0 commit comments

Comments
 (0)