|  | 
| 4 | 4 | #include <Python.h> | 
| 5 | 5 | #include "pycore_runtime.h"         // _PyRuntime | 
| 6 | 6 | 
 | 
| 7 |  | -#define EMSCRIPTEN_COUNT_ARGS_OFFSET 20 | 
| 8 |  | - | 
| 9 |  | -_Static_assert(offsetof(_PyRuntimeState, emscripten_count_args_function) == EMSCRIPTEN_COUNT_ARGS_OFFSET); | 
| 10 |  | - | 
| 11 | 7 | typedef int (*CountArgsFunc)(PyCFunctionWithKeywords func); | 
| 12 | 8 | 
 | 
| 13 |  | -// Enable macro expanding in the body of EM_JS | 
| 14 |  | -#define EM_JS_MACROS(ret, func_name, args, body...)                            \ | 
| 15 |  | -  EM_JS(ret, func_name, args, body) | 
|  | 9 | +// Offset of emscripten_count_args_function in _PyRuntimeState. There's a couple | 
|  | 10 | +// of alternatives: | 
|  | 11 | +// 1. Just make emscripten_count_args_function a real C global variable instead | 
|  | 12 | +//    of a field of _PyRuntimeState. This would violate our rule against mutable | 
|  | 13 | +//    globals. | 
|  | 14 | +// 2. #define a preprocessor constant equal to a hard coded number and make a | 
|  | 15 | +//    _Static_assert(offsetof(_PyRuntimeState, emscripten_count_args_function) | 
|  | 16 | +//    == OURCONSTANT) This has the disadvantage that we have to update the hard | 
|  | 17 | +//    coded constant when _PyRuntimeState changes | 
|  | 18 | +// | 
|  | 19 | +// So putting the mutable constant in _PyRuntime and using a immutable global to | 
|  | 20 | +// record the offset so we can access it from JS is probably the best way. | 
|  | 21 | +EMSCRIPTEN_KEEPALIVE const int _PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET = offsetof(_PyRuntimeState, emscripten_count_args_function); | 
| 16 | 22 | 
 | 
| 17 |  | -EM_JS_MACROS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), { | 
|  | 23 | +EM_JS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), { | 
| 18 | 24 |   return Module._PyEM_CountArgsPtr; // initialized below | 
| 19 | 25 | } | 
| 20 | 26 | // Binary module for the checks. It has to be done in web assembly because | 
| @@ -174,7 +180,8 @@ addOnPreRun(() => { | 
| 174 | 180 |     // back to the JS trampoline. | 
| 175 | 181 |   } | 
| 176 | 182 |   Module._PyEM_CountArgsPtr = ptr; | 
| 177 |  | -  HEAP32[__PyRuntime/4 + EMSCRIPTEN_COUNT_ARGS_OFFSET] = ptr; | 
|  | 183 | +  const offset = HEAP32[__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET/4]; | 
|  | 184 | +  HEAP32[__PyRuntime/4 + offset] = ptr; | 
| 178 | 185 | }); | 
| 179 | 186 | ); | 
| 180 | 187 | 
 | 
|  | 
0 commit comments