# HG changeset patch # User Atul Varma # Date 1251674918 25200 # Node ID d1606a6cf1c0023f03a85e03681840224b008c58 # Parent 28d0670823906fdf033adb92627dc119de9edb8b Made get_object_private() and clear_object_private() more robust. diff -r 28d067082390 -r d1606a6cf1c0 src/context.cpp --- a/src/context.cpp Sun Aug 30 15:50:56 2009 -0700 +++ b/src/context.cpp Sun Aug 30 16:28:38 2009 -0700 @@ -223,6 +223,27 @@ Py_RETURN_NONE; } +static int +PYM_maybeGetFunctionHolder(PYM_JSContextObject *context, + PYM_JSObject *object, + JSObject **result) +{ + if (PyType_IsSubtype(object->ob_type, &PYM_JSFunctionType)) { + PYM_JSFunction *func = (PYM_JSFunction *) object; + if (func->isPython) { + jsval holder; + if (!JS_GetReservedSlot(context->cx, object->obj, 0, &holder)) { + PYM_jsExceptionToPython(context); + return -1; + } + *result = JSVAL_TO_OBJECT(holder); + return 0; + } + } + + return 0; +} + static PyObject * PYM_getObjectPrivate(PYM_JSContextObject *self, PyObject *args) { @@ -236,16 +257,8 @@ JSObject *obj = object->obj; - if (JS_ObjectIsFunction(self->cx, obj)) { - jsval functionHolder; - if (!JS_GetReservedSlot(self->cx, obj, 0, &functionHolder)) { - JS_ClearPendingException(self->cx); - Py_RETURN_NONE; - } - if (!JSVAL_IS_OBJECT(functionHolder)) - Py_RETURN_NONE; - obj = JSVAL_TO_OBJECT(functionHolder); - } + if (PYM_maybeGetFunctionHolder(self, object, &obj) != 0) + return NULL; JSClass *klass = JS_GET_CLASS(self->cx, obj); if (klass != &PYM_JS_ObjectClass) @@ -278,16 +291,8 @@ JSObject *obj = object->obj; - if (JS_ObjectIsFunction(self->cx, obj)) { - jsval functionHolder; - if (!JS_GetReservedSlot(self->cx, obj, 0, &functionHolder)) { - JS_ClearPendingException(self->cx); - Py_RETURN_NONE; - } - if (!JSVAL_IS_OBJECT(functionHolder)) - Py_RETURN_NONE; - obj = JSVAL_TO_OBJECT(functionHolder); - } + if (PYM_maybeGetFunctionHolder(self, object, &obj) != 0) + return NULL; JSClass *klass = JS_GET_CLASS(self->cx, obj); if (klass != &PYM_JS_ObjectClass) diff -r 28d067082390 -r d1606a6cf1c0 tests/test_pymonkey.py --- a/tests/test_pymonkey.py Sun Aug 30 15:50:56 2009 -0700 +++ b/tests/test_pymonkey.py Sun Aug 30 16:28:38 2009 -0700 @@ -226,6 +226,12 @@ jsfunc = cx.new_function(foo, foo.__name__) self.assertEqual(jsfunc.filename, None) + def testJsScriptedFuncHasNoPrivate(self): + cx = pymonkey.Runtime().new_context() + jsfunc = cx.evaluate_script(cx.new_object(), + '(function(){})', '', 1) + self.assertEqual(cx.get_object_private(jsfunc), None) + def testJsWrappedPythonFuncHasPrivate(self): def foo(cx, this, args): pass