Skip to content

Commit

Permalink
Follow pep 7
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Jul 11, 2023
1 parent 00afb90 commit 20ad6ab
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions Python/emscripten_trampoline.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if defined(PY_CALL_TRAMPOLINE)

#include <emscripten.h> // EM_JS, EM_JS_DEPS
#include <emscripten.h> // EM_JS
#include <Python.h>
#include "pycore_runtime.h" // _PyRuntime

Expand All @@ -10,19 +10,24 @@
* https://github.com/GoogleChromeLabs/wasm-feature-detect/blob/main/src/detectors/type-reflection/index.js
*/
EM_JS(int, _PyEM_detect_type_reflection, (), {
return "Function" in WebAssembly;
return "Function" in WebAssembly;
});

void
_Py_EmscriptenTrampoline_Init(_PyRuntimeState *runtime)
{
runtime->wasm_type_reflection_available = _PyEM_detect_type_reflection();
runtime->wasm_type_reflection_available = _PyEM_detect_type_reflection();
}

/**
* Backwards compatible trampoline works with all JS runtimes
*/
EM_JS(PyObject*, _PyEM_TrampolineCall_JavaScript, (PyCFunctionWithKeywords func, PyObject *arg1, PyObject *arg2, PyObject *arg3), {
EM_JS(PyObject*,
_PyEM_TrampolineCall_JavaScript, (PyCFunctionWithKeywords func,
PyObject *arg1,
PyObject *arg2,
PyObject *arg3),
{
return wasmTable.get(func)(arg1, arg2, arg3);
}
);
Expand All @@ -31,14 +36,16 @@ EM_JS(PyObject*, _PyEM_TrampolineCall_JavaScript, (PyCFunctionWithKeywords func,
* In runtimes with WebAssembly type reflection, count the number of parameters
* and cast to the appropriate signature
*/
EM_JS(int, _PyEM_CountFuncParams, (PyCFunctionWithKeywords func), {
let n = _PyEM_CountFuncParams.cache.get(func);
if (n !== undefined) {
EM_JS(int, _PyEM_CountFuncParams, (PyCFunctionWithKeywords func),
{
let n = _PyEM_CountFuncParams.cache.get(func);

if (n !== undefined) {
return n;
}
n = WebAssembly.Function.type(wasmTable.get(func)).parameters.length;
_PyEM_CountFuncParams.cache.set(func, n);
return n;
}
n = WebAssembly.Function.type(wasmTable.get(func)).parameters.length;
_PyEM_CountFuncParams.cache.set(func, n);
return n;
}
_PyEM_CountFuncParams.cache = new Map();
)
Expand All @@ -52,23 +59,24 @@ typedef PyObject* (*three_arg)(PyObject*, PyObject*, PyObject*);

PyObject*
_PyEM_TrampolineCall_Reflection(PyCFunctionWithKeywords func,
PyObject* self,
PyObject* args,
PyObject* kw)
PyObject* self,
PyObject* args,
PyObject* kw)
{
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;
}
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

0 comments on commit 20ad6ab

Please sign in to comment.