Skip to content

Commit 0810bf1

Browse files
committed
Move top level constant into _PyRuntime
1 parent b883d28 commit 0810bf1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Include/internal/pycore_runtime.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ typedef struct pyruntimestate {
6767
/* Is Python fully initialized? Set to 1 by Py_Initialize() */
6868
int initialized;
6969

70+
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
71+
/* Choose between trampoline based on type reflection vs based on EM_JS */
72+
int (*emscripten_count_args_function)(PyCFunctionWithKeywords func);
73+
#endif
74+
7075
/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
7176
is called again.
7277

Python/emscripten_trampoline.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +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+
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*);
185189
typedef PyObject* (*two_arg)(PyObject*, PyObject*);
186190
typedef PyObject* (*three_arg)(PyObject*, PyObject*, PyObject*);
187191

192+
typedef int (*CountArgsFunc)(PyCFunctionWithKeywords func);
193+
194+
188195
PyObject*
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

Comments
 (0)