MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / makeNewPyMethod

Function makeNewPyMethod

src/PyListProxyHandler.cc:34–53  ·  view source on GitHub ↗

private util if function is a proxy for a python method, mutate it into a new python method bound to thisObject

Source from the content-addressed store, hash-verified

32// private util
33// if function is a proxy for a python method, mutate it into a new python method bound to thisObject
34static bool makeNewPyMethod(JSContext *cx, JS::MutableHandleValue function, JS::HandleObject thisObject) {
35 if (!JS_IsNativeFunction(&(function.toObject()), callPyFunc)) {
36 return true; // we don't need to mutate function if it is not a proxy for a python function
37 }
38
39 PyObject *method = (PyObject *)js::GetFunctionNativeReserved(&(function.toObject()), 0).toPrivate();
40 if (!PyMethod_Check(method)) {
41 PyErr_Format(PyExc_TypeError, "unbound python functions do not have a 'self' to bind");
42 return false;
43 }
44
45 PyObject *func = PyMethod_Function(method);
46 JS::RootedValue thisValue(cx);
47 thisValue.setObject(*thisObject);
48 PyObject *newSelf = pyTypeFactory(cx, thisValue);
49 function.set(jsTypeFactory(cx, PyMethod_New(func, newSelf)));
50 Py_DECREF(newSelf);
51
52 return true;
53}
54
55static bool array_reverse(JSContext *cx, unsigned argc, JS::Value *vp) {
56 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);

Callers 8

array_forEachFunction · 0.85
array_mapFunction · 0.85
array_filterFunction · 0.85
array_someFunction · 0.85
array_everyFunction · 0.85
array_findFunction · 0.85
array_findIndexFunction · 0.85
array_flatMapFunction · 0.85

Calls 3

pyTypeFactoryFunction · 0.85
jsTypeFactoryFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected