Skip to content

Commit 608b73e

Browse files
committed
perf(pyTypeFactory): return the underlying Python function rather than wrapping it again
1 parent 971a242 commit 608b73e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/pyTypeFactory.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "include/modules/pythonmonkey/pythonmonkey.hh"
3333

3434
#include <jsapi.h>
35+
#include <jsfriendapi.h>
3536
#include <js/Object.h>
3637
#include <js/ValueArray.h>
3738

@@ -123,9 +124,16 @@ PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted
123124
return new ExceptionType(cx, obj);
124125
}
125126
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+
}
129137
FuncType *f = new FuncType(pyFunc);
130138
memoizePyTypeAndGCThing(f, *rval); // TODO (Caleb Aikens) consider putting this in the FuncType constructor
131139
return f;

0 commit comments

Comments
 (0)