Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106213: Make Emscripten trampolines work with JSPI #106219

Merged
merged 42 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8e2ad18
Make Emscripten trampolines work with JSPI
hoodmane Jun 28, 2023
6903fd5
Add blurb
hoodmane Jun 29, 2023
af6850c
Add more comments
hoodmane Jun 29, 2023
ffb24ff
Fix typo
hoodmane Jun 29, 2023
85d88ec
Cleanup
hoodmane Jun 29, 2023
9d8f23e
Use EM_JS instead of EM_ASM
hoodmane Jun 30, 2023
2dc1399
Fix typo
hoodmane Jun 30, 2023
418efb4
Merge branch 'main' into jspi-trampoline
hoodmane Jul 5, 2023
d900dfd
Move type reflection flag into _PyRuntime
hoodmane Jul 6, 2023
62a2f7f
Merge branch 'main' into jspi-trampoline
hoodmane Jul 6, 2023
cd3f8bf
Update pycore_emscripten_trampoline.h declarations
hoodmane Jul 7, 2023
2c45f1b
Merge branch 'main' into jspi-trampoline
hoodmane Jul 7, 2023
3951848
Include pycore_runtime.h
hoodmane Jul 7, 2023
dfd43d7
Fix typo
hoodmane Jul 9, 2023
b0b50f4
Use wasmTable.get instead of getWasmTableEntry
hoodmane Jul 10, 2023
1c1b9ed
Replace another instance of getWasmTableEntry with wasmTable.get
hoodmane Jul 10, 2023
44f0371
Use branch in macro to choose trampoline
hoodmane Jul 10, 2023
bd609e5
Fix non emscripten platforms
hoodmane Jul 11, 2023
a00d94f
Fix compile warnings on non-emscripten platforms
hoodmane Jul 11, 2023
68f4dba
Fix spelling
hoodmane Jul 11, 2023
1d49e79
Fix formatting
hoodmane Jul 11, 2023
afb9ad5
Revert whitespace changes
hoodmane Jul 11, 2023
aa213e0
Call _Py_EmscriptenTrampoline_Init before setting _initialized to 1
hoodmane Jul 11, 2023
00afb90
Sort include list
hoodmane Jul 11, 2023
20ad6ab
Follow pep 7
hoodmane Jul 11, 2023
ae7584c
Merge branch 'main' into jspi-trampoline
hoodmane Aug 20, 2023
1b6b6af
Move wasm_type_reflection_available to end of struct
hoodmane Aug 20, 2023
381bb12
Address review comments
hoodmane Aug 20, 2023
58c7613
Update Include/internal/pycore_runtime.h
hoodmane Aug 21, 2023
eccf06d
Merge branch 'main' into jspi-trampoline
brettcannon Aug 21, 2023
5fff95d
Merge branch 'main' into jspi-trampoline
hoodmane Sep 1, 2023
f08a02b
Merge branch 'jspi-trampoline' of github.com:hoodmane/cpython into js…
hoodmane Sep 1, 2023
6b27584
Merge branch 'main' into jspi-trampoline
brettcannon Sep 1, 2023
22a8c75
Fix merge
hoodmane Sep 2, 2023
b40bb7b
Merge branch 'jspi-trampoline' of github.com:hoodmane/cpython into js…
hoodmane Sep 2, 2023
28b33b3
Merge branch 'main' into jspi-trampoline
brettcannon Sep 8, 2023
9ecc480
Merge branch 'main' into jspi-trampoline
brettcannon Sep 8, 2023
4aa1487
Test setting ac_cv_libatomic_needed=no
hoodmane Sep 9, 2023
a57dfdc
Set libatomic_needed=no if cross compiling
hoodmane Sep 9, 2023
6358371
Merge branch 'main' into jspi-trampoline
hoodmane Sep 11, 2023
6781a48
Merge branch 'main' into jspi-trampoline
brettcannon Sep 13, 2023
fd59df4
Merge branch 'main' into jspi-trampoline
brettcannon Sep 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,19 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, PyObject *);
* trampoline mitigates common occurrences of bad fpcasts on Emscripten.
*/
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
extern PyObject*
_PyEM_TrampolineCall(PyCFunctionWithKeywords func,
PyObject* self,
PyObject* args,
PyObject* kw);

#define _PyCFunction_TrampolineCall(meth, self, args) \
_PyCFunctionWithKeywords_TrampolineCall( \
_PyEM_TrampolineCall( \
(*(PyCFunctionWithKeywords)(void(*)(void))(meth)), (self), (args), NULL)
extern PyObject* _PyCFunctionWithKeywords_TrampolineCall(
PyCFunctionWithKeywords meth, PyObject *, PyObject *, PyObject *);

#define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \
_PyEM_TrampolineCall((meth), (self), (args), (kw))

#else
#define _PyCFunction_TrampolineCall(meth, self, args) \
(meth)((self), (args))
Expand Down
22 changes: 14 additions & 8 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ class property "propertyobject *" "&PyProperty_Type"

// see pycore_object.h
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
#include <emscripten.h>
EM_JS(int, descr_set_trampoline_call, (setter set, PyObject *obj, PyObject *value, void *closure), {
return wasmTable.get(set)(obj, value, closure);
});

EM_JS(PyObject*, descr_get_trampoline_call, (getter get, PyObject *obj, void *closure), {
return wasmTable.get(get)(obj, closure);
});
extern PyObject*
_PyEM_TrampolineCall(PyCFunctionWithKeywords func,
PyObject* self,
PyObject* args,
PyObject* kw);

#define descr_set_trampoline_call(set, obj, value, closure) \
((int)_PyEM_TrampolineCall((PyCFunctionWithKeywords)(set), (obj), (value), (PyObject*)(closure)))


#define descr_get_trampoline_call(get, obj, closure) \
_PyEM_TrampolineCall((PyCFunctionWithKeywords)(get), (obj), (PyObject*)(closure), NULL)

#else

#define descr_set_trampoline_call(set, obj, value, closure) \
(set)((obj), (value), (closure))

Expand Down
7 changes: 0 additions & 7 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,3 @@ cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
return _Py_CheckFunctionResult(tstate, func, result, NULL);
}

#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
#include <emscripten.h>

EM_JS(PyObject*, _PyCFunctionWithKeywords_TrampolineCall, (PyCFunctionWithKeywords func, PyObject *self, PyObject *args, PyObject *kw), {
return wasmTable.get(func)(self, args, kw);
});
#endif
67 changes: 67 additions & 0 deletions Python/emscripten_trampoline.c
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#if defined(PY_CALL_TRAMPOLINE)

#include <emscripten.h>
#include <Python.h>

EMSCRIPTEN_KEEPALIVE int _PyEM_type_reflection_available;
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

EM_JS_DEPS(_PyEMJS_TrampolineCall, "$getWasmTableEntry")
EM_JS(PyObject*, _PyEMJS_TrampolineCall, (PyCFunctionWithKeywords func, PyObject *arg1, PyObject *arg2, PyObject *arg3), {
return getWasmTableEntry(func)(arg1, arg2, arg3);
}
);
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

EM_JS(int, _PyEM_CountFuncParams, (PyCFunctionWithKeywords func), {
let n = _PyEM_CountFuncParams.cache.get(func);
if (n !== undefined) {
return n;
}
n = WebAssembly.Function.type(getWasmTableEntry(func)).parameters.length;
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
_PyEM_CountFuncParams.cache.set(func, n);
return n;
}
_PyEM_CountFuncParams.cache = new Map();
switch(typeof Module.preRun) {
case "undefined":
Module.preRun = [];
break;
case "function":
Module.preRun = [Module.preRun];
break;
}
Module.preRun.push(() => HEAP32[__PyEM_type_reflection_available/4] = "Function" in WebAssembly);
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
)


typedef PyObject* (*zero_arg)(void);
typedef PyObject* (*one_arg)(PyObject*);
typedef PyObject* (*two_arg)(PyObject*, PyObject*);
typedef PyObject* (*three_arg)(PyObject*, PyObject*, PyObject*);

PyObject*
_PyEM_TrampolineCall(PyCFunctionWithKeywords func,
PyObject* self,
PyObject* args,
PyObject* kw)
{
if (!_PyEM_type_reflection_available) {
return _PyEMJS_TrampolineCall(func, self, args, kw);
} else {
switch (_PyEM_CountFuncParams(func)) {
case 0:
return ((zero_arg)func)();
case 1:
return ((one_arg)func)(self);
case 2:
return ((two_arg)func)(self, args);
case 3:
return ((three_arg)func)(self, args, kw);
default:
PyErr_SetString(PyExc_SystemError, "Handler takes too many arguments");
return NULL;
}
}
}


#endif
2 changes: 1 addition & 1 deletion configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4770,7 +4770,7 @@ PLATFORM_OBJS=

AS_CASE([$ac_sys_system],
[Emscripten], [
AS_VAR_APPEND([PLATFORM_OBJS], [' Python/emscripten_signal.o'])
AS_VAR_APPEND([PLATFORM_OBJS], [' Python/emscripten_signal.o Python/emscripten_trampoline.o'])
AS_VAR_APPEND([PLATFORM_HEADERS], [' $(srcdir)/Include/internal/pycore_emscripten_signal.h'])
],
)
Expand Down