Skip to content

Commit

Permalink
pythongh-126898 Switch to exporting an es6 module and using a node_en…
Browse files Browse the repository at this point in the history
…try script
  • Loading branch information
hoodmane committed Nov 16, 2024
1 parent 2313f84 commit aecf058
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
8 changes: 5 additions & 3 deletions Tools/wasm/emscripten/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import sysconfig
import tempfile

WASM_DIR = Path(__file__).parent.parent
CHECKOUT = WASM_DIR.parent.parent
EMSCRIPTEN_DIR = Path(__file__).parent
CHECKOUT = EMSCRIPTEN_DIR.parent.parent.parent

CROSS_BUILD_DIR = CHECKOUT / "cross-build"
BUILD_DIR = CROSS_BUILD_DIR / "build"
Expand Down Expand Up @@ -207,7 +207,9 @@ def configure_emscripten_python(context, working_dir):
quiet=context.quiet,
)

python_js = working_dir / "python.js"
shutil.copy(EMSCRIPTEN_DIR / "node_entry.mjs", working_dir / "node_entry.mjs")

python_js = working_dir / "node_entry.mjs"
exec_script = working_dir / "python.sh"
exec_script.write_text(f'#!/bin/sh\nexec {host_runner} {python_js} "$@"\n')
exec_script.chmod(0o755)
Expand Down
23 changes: 23 additions & 0 deletions Tools/wasm/emscripten/node_entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import EmscriptenModule from "./python.mjs";
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

if (typeof process !== "undefined") {
const nodeVersion = Number(process.versions.node.split(".", 1)[0]);
if (nodeVersion < 18) {
process.stderr.write(
`Node version must be >= 18, got version ${process.version}\n`,
);
process.exit(1);
}
}

const settings = {
preRun(Module) {
const __dirname = dirname(fileURLToPath(import.meta.url));
Module.FS.mkdirTree("/lib/");
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: __dirname + "/lib/" }, "/lib/");
}
};

await EmscriptenModule(settings);
15 changes: 0 additions & 15 deletions Tools/wasm/emscripten/node_pre.js

This file was deleted.

4 changes: 2 additions & 2 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ AC_ARG_WITH([suffix],
)
], [
AS_CASE([$ac_sys_system],
[Emscripten], [EXEEXT=.js],
[Emscripten], [EXEEXT=.mjs],
[WASI], [EXEEXT=.wasm],
[EXEEXT=]
)
Expand Down Expand Up @@ -2328,6 +2328,7 @@ AS_CASE([$ac_sys_system],
dnl Include file system support
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXPORTED_RUNTIME_METHODS=FS"])
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
AS_VAR_APPEND([LINKFORSHARED], [" -sMAIN_MODULE"])
Expand All @@ -2341,7 +2342,6 @@ AS_CASE([$ac_sys_system],
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sALLOW_MEMORY_GROWTH"])
dnl not completely sure whether or not we want -sEXIT_RUNTIME, keeping it for now.
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXIT_RUNTIME"])
AS_VAR_APPEND([LDFLAGS_NODIST], [" --pre-js=\$(srcdir)/Tools/wasm/emscripten/node_pre.js"])
WASM_LINKFORSHARED_DEBUG="-gseparate-dwarf --emit-symbol-map"
AS_VAR_IF([wasm_debug], [yes], [
Expand Down

0 comments on commit aecf058

Please sign in to comment.