@@ -47,6 +47,20 @@ typedef PyObject* (*TrampolineFunc)(int* success,
4747 PyObject * args ,
4848 PyObject * kw );
4949
50+ // The JS glue only binds _PyRuntime if it is exported, which an embedder
51+ // linking libpython does not do.
52+ EMSCRIPTEN_KEEPALIVE _PyRuntimeState * const _PyEM_runtime = & _PyRuntime ;
53+
54+ // Placeholder whose function table slot the wasm-gc trampoline takes over,
55+ // so that the table never needs to grow.
56+ static PyObject *
57+ trampoline_placeholder (int * success , PyCFunctionWithKeywords func ,
58+ PyObject * self , PyObject * args , PyObject * kw )
59+ {
60+ Py_FatalError ("Emscripten trampoline slot was not set up" );
61+ }
62+ EMSCRIPTEN_KEEPALIVE const TrampolineFunc _PyEM_trampoline_slot = trampoline_placeholder ;
63+
5064/**
5165 * Backwards compatible trampoline works with all JS runtimes
5266 */
@@ -79,7 +93,9 @@ function getPyEMTrampolinePtr() {
7993 const trampolineInstance = new WebAssembly .Instance (trampolineModule , {
8094 env : { __indirect_function_table : wasmTable , memory : wasmMemory },
8195 });
82- return addFunction (trampolineInstance .exports .trampoline_call );
96+ const slot = HEAPU32 [__PyEM_trampoline_slot / 4 ];
97+ wasmTable .set (slot , trampolineInstance .exports .trampoline_call );
98+ return slot ;
8399}
84100// We have to be careful to work correctly with memory snapshots -- the value of
85101// _PyRuntimeState.emscripten_trampoline needs to reflect whether wasm-gc is
@@ -90,12 +106,12 @@ function getPyEMTrampolinePtr() {
90106addOnPreRun (function setEmscriptenTrampoline () {
91107 const ptr = getPyEMTrampolinePtr ();
92108 const offset = HEAP32 [__PyEM_EMSCRIPTEN_TRAMPOLINE_OFFSET / 4 ];
93- HEAP32 [(__PyRuntime + offset ) / 4 ] = ptr ;
109+ HEAP32 [(HEAPU32 [ __PyEM_runtime / 4 ] + offset ) / 4 ] = ptr ;
94110});
95111);
96112
97113EM_JS_DEPS (_PyEM_TrampolineCall ,
98- "$wasmTable,$wasmMemory,$addFunction,$ addOnPreRun" );
114+ "$wasmTable,$wasmMemory,$addOnPreRun" );
99115
100116PyObject *
101117_PyEM_TrampolineCall (PyCFunctionWithKeywords func ,
0 commit comments