private util if function is a proxy for a python method, mutate it into a new python method bound to thisObject
| 32 | // private util |
| 33 | // if function is a proxy for a python method, mutate it into a new python method bound to thisObject |
| 34 | static 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 | |
| 55 | static bool array_reverse(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 56 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
no test coverage detected