# HG changeset patch # User Atul Varma # Date 1248756248 25200 # Node ID 9b3f4e53e3659173fa77ff66557dcbcfbd26868a # Parent b0360c0ed54665d6fb54e978682725dc0d657bdd Added context.get_object_private() and an optional private object parameter to context.new_object(). diff -r b0360c0ed546 -r 9b3f4e53e365 context.c --- 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} }; diff -r b0360c0ed546 -r 9b3f4e53e365 test_pymonkey.py --- 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!")