Skip to content

Commit eda1648

Browse files
committed
Remove hard coded constant
1 parent 6610fb6 commit eda1648

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Python/emscripten_trampoline.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
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-
117
typedef int (*CountArgsFunc)(PyCFunctionWithKeywords func);
128

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);
1622

17-
EM_JS_MACROS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), {
23+
EM_JS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), {
1824
return Module._PyEM_CountArgsPtr; // initialized below
1925
}
2026
// Binary module for the checks. It has to be done in web assembly because
@@ -174,7 +180,8 @@ addOnPreRun(() => {
174180
// back to the JS trampoline.
175181
}
176182
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;
178185
});
179186
);
180187

0 commit comments

Comments
 (0)