changeset 162:d1606a6cf1c0

Made get_object_private() and clear_object_private() more robust.
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 16:28:38 -0700
parents 28d067082390
children 4edaa0e6f382
files src/context.cpp tests/test_pymonkey.py
diffstat 2 files changed, 31 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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(){})', '<string>', 1)
+        self.assertEqual(cx.get_object_private(jsfunc), None)
+
     def testJsWrappedPythonFuncHasPrivate(self):
         def foo(cx, this, args):
             pass