|
32 | 32 | #include "include/modules/pythonmonkey/pythonmonkey.hh" |
33 | 33 |
|
34 | 34 | #include <jsapi.h> |
| 35 | +#include <jsfriendapi.h> |
35 | 36 | #include <js/Object.h> |
36 | 37 | #include <js/ValueArray.h> |
37 | 38 |
|
@@ -123,9 +124,16 @@ PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted |
123 | 124 | return new ExceptionType(cx, obj); |
124 | 125 | } |
125 | 126 | case js::ESClass::Function: { |
126 | | - // FIXME (Tom Tang): `jsCxThisFuncTuple` and the tuple items are not going to be GCed |
127 | | - PyObject *jsCxThisFuncTuple = PyTuple_Pack(3, PyLong_FromVoidPtr(cx), PyLong_FromVoidPtr(thisObj), PyLong_FromVoidPtr(rval)); |
128 | | - PyObject *pyFunc = PyCFunction_New(&callJSFuncDef, jsCxThisFuncTuple); |
| 127 | + PyObject *pyFunc; |
| 128 | + if (JS_IsNativeFunction(obj, callPyFunc)) { // It's a wrapped python function by us |
| 129 | + // Get the underlying python function from the 0th reserved slot |
| 130 | + JS::Value pyFuncVal = js::GetFunctionNativeReserved(obj, 0); |
| 131 | + pyFunc = (PyObject *)(pyFuncVal.toPrivate()); |
| 132 | + } else { |
| 133 | + // FIXME (Tom Tang): `jsCxThisFuncTuple` and the tuple items are not going to be GCed |
| 134 | + PyObject *jsCxThisFuncTuple = PyTuple_Pack(3, PyLong_FromVoidPtr(cx), PyLong_FromVoidPtr(thisObj), PyLong_FromVoidPtr(rval)); |
| 135 | + pyFunc = PyCFunction_New(&callJSFuncDef, jsCxThisFuncTuple); |
| 136 | + } |
129 | 137 | FuncType *f = new FuncType(pyFunc); |
130 | 138 | memoizePyTypeAndGCThing(f, *rval); // TODO (Caleb Aikens) consider putting this in the FuncType constructor |
131 | 139 | return f; |
@@ -167,7 +175,7 @@ PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted |
167 | 175 | return NULL; |
168 | 176 | } |
169 | 177 |
|
170 | | -static PyObject *callJSFunc(PyObject *jsCxThisFuncTuple, PyObject *args) { |
| 178 | +PyObject *callJSFunc(PyObject *jsCxThisFuncTuple, PyObject *args) { |
171 | 179 | // TODO (Caleb Aikens) convert PyObject *args to JS::Rooted<JS::ValueArray> JSargs |
172 | 180 | JSContext *cx = (JSContext *)PyLong_AsVoidPtr(PyTuple_GetItem(jsCxThisFuncTuple, 0)); |
173 | 181 | JS::RootedObject *thisObj = (JS::RootedObject *)PyLong_AsVoidPtr(PyTuple_GetItem(jsCxThisFuncTuple, 1)); |
|
0 commit comments