44#include  <Python.h> 
55#include  "pycore_runtime.h"          // _PyRuntime 
66
7+ #define  EMSCRIPTEN_COUNT_ARGS_OFFSET  20
8+ 
9+ _Static_assert (offsetof(_PyRuntimeState , emscripten_count_args_function ) ==  EMSCRIPTEN_COUNT_ARGS_OFFSET );
10+ 
11+ // Enable macro expanding in the body of EM_JS 
12+ #define  EM_JS_MACROS (ret , func_name , args , body ...)                            \
13+   EM_JS(ret, func_name, args, body)
714
8- typedef  int  (* CountArgsFunc )(PyCFunctionWithKeywords  func );
915// We have to be careful to work correctly with memory snapshots. Even if we are 
1016// loading a memory snapshot, we need to perform the JS initialization work. 
1117// That means we can't call the initialization code from C. Instead, we export 
1218// this function pointer to JS and then fill it in a preRun function which runs 
1319// unconditionally. 
14- EMSCRIPTEN_KEEPALIVE  CountArgsFunc  _PyEM_CountFuncParams  =  NULL ;
15- 
1620/** 
1721 * Backwards compatible trampoline works with all JS runtimes 
1822 */ 
19- EM_JS (PyObject * , _PyEM_TrampolineCall_JS , (PyCFunctionWithKeywords  func , PyObject  * arg1 , PyObject  * arg2 , PyObject  * arg3 ), {
23+ EM_JS_MACROS (PyObject * , _PyEM_TrampolineCall_JS , (PyCFunctionWithKeywords  func , PyObject  * arg1 , PyObject  * arg2 , PyObject  * arg3 ), {
2024    return  wasmTable .get (func )(arg1 , arg2 , arg3 );
2125}
2226
@@ -176,7 +180,7 @@ addOnPreRun(() => {
176180    // If something goes wrong, we'll null out _PyEM_CountFuncParams and fall 
177181    // back to the JS trampoline. 
178182  }
179-   HEAP32 [__PyEM_CountFuncParams / 4 ] =  ptr ;
183+   HEAP32 [__PyRuntime / 4   +   EMSCRIPTEN_COUNT_ARGS_OFFSET ] =  ptr ;
180184});
181185);
182186
@@ -185,16 +189,20 @@ typedef PyObject* (*one_arg)(PyObject*);
185189typedef  PyObject *  (* two_arg )(PyObject * , PyObject * );
186190typedef  PyObject *  (* three_arg )(PyObject * , PyObject * , PyObject * );
187191
192+ typedef  int  (* CountArgsFunc )(PyCFunctionWithKeywords  func );
193+ 
194+ 
188195PyObject * 
189196_PyEM_TrampolineCall (PyCFunctionWithKeywords  func ,
190197              PyObject *  self ,
191198              PyObject *  args ,
192199              PyObject *  kw )
193200{
194-   if  (_PyEM_CountFuncParams  ==  0 ) {
201+   CountArgsFunc  count_args  =  _PyRuntime .emscripten_count_args_function ;
202+   if  (count_args  ==  0 ) {
195203    return  _PyEM_TrampolineCall_JS (func , self , args , kw );
196204  }
197-   switch  (_PyEM_CountFuncParams (func )) {
205+   switch  (count_args (func )) {
198206    case  0 :
199207      return  ((zero_arg )func )();
200208    case  1 :
0 commit comments