Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-156780: Emscripten: drop the trampoline's dependence on exports
The trampoline exports _PyRuntime's address itself and reuses a
placeholder's table slot, so libpython no longer needs
-sEXPORTED_FUNCTIONS=__PyRuntime or a growable function table.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
  • Loading branch information
clementperon and claude committed Sep 10, 2026
commit 0a2c8f45e8332e4ecad1e2736d6c244b221e45c2
21 changes: 18 additions & 3 deletions Python/emscripten_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ typedef PyObject* (*TrampolineFunc)(int* success,
PyObject* args,
PyObject* kw);

// Lets JS reach _PyRuntime without it being in -sEXPORTED_FUNCTIONS.
EMSCRIPTEN_KEEPALIVE _PyRuntimeState *const _PyEM_runtime = &_PyRuntime;
Comment thread
clementperon marked this conversation as resolved.

// Its table slot is taken over by the wasm-gc trampoline, so the table
// never grows.
static PyObject*
trampoline_placeholder(int* success, PyCFunctionWithKeywords func,
PyObject* self, PyObject* args, PyObject* kw)
{
Py_FatalError("Emscripten trampoline slot was not set up");
}
EMSCRIPTEN_KEEPALIVE const TrampolineFunc _PyEM_trampoline_slot = trampoline_placeholder;

/**
* Backwards compatible trampoline works with all JS runtimes
*/
Expand Down Expand Up @@ -79,7 +92,9 @@ function getPyEMTrampolinePtr() {
const trampolineInstance = new WebAssembly.Instance(trampolineModule, {
env: { __indirect_function_table: wasmTable, memory: wasmMemory },
});
return addFunction(trampolineInstance.exports.trampoline_call);
const slot = HEAPU32[__PyEM_trampoline_slot / 4];
wasmTable.set(slot, trampolineInstance.exports.trampoline_call);
return slot;
}
// We have to be careful to work correctly with memory snapshots -- the value of
// _PyRuntimeState.emscripten_trampoline needs to reflect whether wasm-gc is
Expand All @@ -90,12 +105,12 @@ function getPyEMTrampolinePtr() {
addOnPreRun(function setEmscriptenTrampoline() {
const ptr = getPyEMTrampolinePtr();
const offset = HEAP32[__PyEM_EMSCRIPTEN_TRAMPOLINE_OFFSET / 4];
HEAP32[(__PyRuntime + offset) / 4] = ptr;
HEAP32[(HEAPU32[__PyEM_runtime / 4] + offset) / 4] = ptr;
});
);

EM_JS_DEPS(_PyEM_TrampolineCall,
"$wasmTable,$wasmMemory,$addFunction,$addOnPreRun");
"$wasmTable,$wasmMemory,$addOnPreRun");

PyObject*
_PyEM_TrampolineCall(PyCFunctionWithKeywords func,
Expand Down
2 changes: 1 addition & 1 deletion configure

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

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ AS_CASE([$ac_sys_system],
dnl Include file system support
AS_VAR_APPEND([LINKFORSHARED], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_RUNTIME_METHODS=FS,callMain,ENV,HEAPU32,TTY,ERRNO_CODES"])
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version,__PyRuntime,_PyGILState_GetThisThreadState,__PyEM_EMSCRIPTEN_TRAMPOLINE_OFFSET"])
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version,_PyGILState_GetThisThreadState,__PyEM_EMSCRIPTEN_TRAMPOLINE_OFFSET"])
AS_VAR_APPEND([LINKFORSHARED], [" -sSTACK_SIZE=5MB"])
dnl Avoid bugs in JS fallback string decoding path
AS_VAR_APPEND([LINKFORSHARED], [" -sTEXTDECODER=2"])
Expand Down
Loading