Skip to content

Commit 971a242

Browse files
committed
perf(jsTypeFactory): return the underlying JS function rather than wrapping it again if it's a wrapped JS function by us
1 parent 7e2c6be commit 971a242

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

include/pyTypeFactory.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted
4444
* @param args - Pointer to a PyTupleObject containing the arguments to the python function
4545
* @return PyObject* - The result of the JSFunction called with args coerced to JS types, coerced back to a PyObject type, or NULL if coercion wasn't possible
4646
*/
47-
static PyObject *callJSFunc(PyObject *JSFuncAddress, PyObject *args);
47+
PyObject *callJSFunc(PyObject *JSFuncAddress, PyObject *args);
4848

4949
#endif

src/jsTypeFactory.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ JS::Value jsTypeFactory(JSContext *cx, PyObject *object) {
117117
}
118118
memoizePyTypeAndGCThing(new StrType(object), returnType);
119119
}
120+
else if (PyCFunction_Check(object) && PyCFunction_GetFunction(object) == callJSFunc) {
121+
// If it's a wrapped JS function by us, return the underlying JS function rather than wrapping it again
122+
PyObject *jsCxThisFuncTuple = PyCFunction_GetSelf(object);
123+
JS::RootedValue *jsFunc = (JS::RootedValue *)PyLong_AsVoidPtr(PyTuple_GetItem(jsCxThisFuncTuple, 2));
124+
returnType.set(*jsFunc);
125+
}
120126
else if (PyFunction_Check(object) || PyCFunction_Check(object)) {
121127
// can't determine number of arguments for PyCFunctions, so just assume potentially unbounded
122128
uint16_t nargs = 0;

src/pyTypeFactory.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted
167167
return NULL;
168168
}
169169

170-
static PyObject *callJSFunc(PyObject *jsCxThisFuncTuple, PyObject *args) {
170+
PyObject *callJSFunc(PyObject *jsCxThisFuncTuple, PyObject *args) {
171171
// TODO (Caleb Aikens) convert PyObject *args to JS::Rooted<JS::ValueArray> JSargs
172172
JSContext *cx = (JSContext *)PyLong_AsVoidPtr(PyTuple_GetItem(jsCxThisFuncTuple, 0));
173173
JS::RootedObject *thisObj = (JS::RootedObject *)PyLong_AsVoidPtr(PyTuple_GetItem(jsCxThisFuncTuple, 1));

0 commit comments

Comments
 (0)