changeset 71:9b3f4e53e365

Added context.get_object_private() and an optional private object parameter to context.new_object().
author Atul Varma <varmaa@toolness.com>
date Mon, 27 Jul 2009 21:44:08 -0700
parents b0360c0ed546
children cd545c03eeef
files context.c test_pymonkey.py
diffstat 2 files changed, 48 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Mon Jul 27 04:47:33 2009 -0700
+++ b/context.c	Mon Jul 27 21:44:08 2009 -0700
@@ -111,9 +111,42 @@
 }
 
 static PyObject *
+PYM_getObjectPrivate(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_JSObject *object;
+
+  if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object))
+    return NULL;
+
+  JSClass *klass = JS_GET_CLASS(cx, object->obj);
+  if (klass != &PYM_JS_ObjectClass)
+    Py_RETURN_NONE;
+
+  PyObject *pyObject;
+
+  if (!PYM_JS_getPrivatePyObject(self->cx, object->obj, &pyObject)) {
+    // TODO: Get the actual JS exception. Any exception that exists
+    // here will probably still be pending on the JS context.
+    PyErr_SetString(PYM_error, "Getting private failed.");
+    return NULL;
+  }
+
+  if (pyObject == NULL)
+    pyObject = Py_None;
+
+  Py_INCREF(pyObject);
+  return pyObject;
+}
+
+static PyObject *
 PYM_newObject(PYM_JSContextObject *self, PyObject *args)
 {
-  JSObject *obj = PYM_JS_newObject(self->cx, NULL);
+  PyObject *privateObj = NULL;
+
+  if (!PyArg_ParseTuple(args, "|O", &privateObj))
+    return NULL;
+
+  JSObject *obj = PYM_JS_newObject(self->cx, privateObj);
   if (obj == NULL) {
     PyErr_SetString(PYM_error, "PYM_JS_newObject() failed");
     return NULL;
@@ -370,6 +403,8 @@
   {"trigger_operation_callback", (PyCFunction) PYM_triggerOperationCallback,
    METH_VARARGS,
    "Triggers the operation callback for the context."},
+  {"get_object_private", (PyCFunction) PYM_getObjectPrivate, METH_VARARGS,
+   "Returns the private Python object stored in the JavaScript object."},
   {NULL, NULL, 0, NULL}
 };
 
--- a/test_pymonkey.py	Mon Jul 27 04:47:33 2009 -0700
+++ b/test_pymonkey.py	Mon Jul 27 21:44:08 2009 -0700
@@ -31,6 +31,18 @@
             was_raised = True
         self.assertTrue(was_raised)
 
+    def testGetObjectPrivateWorks(self):
+        class Foo(object):
+            pass
+        pyobj = Foo()
+        cx = pymonkey.Runtime().new_context()
+        obj = cx.new_object(pyobj)
+        pyobj = weakref.ref(pyobj)
+        self.assertEqual(pyobj(), cx.get_object_private(obj))
+        del obj
+        del cx
+        self.assertEqual(pyobj(), None)
+
     def testOperationCallbackIsCalled(self):
         def opcb(cx):
             raise Exception("stop eet!")