changeset 129:7b7a23615873

Added weakref support for runtimes.
author Atul Varma <varmaa@toolness.com>
date Sun, 23 Aug 2009 17:52:47 -0700
parents 1e4d4d475e75
children 4705522c7431
files src/runtime.cpp src/runtime.h tests/test_pymonkey.py
diffstat 3 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/runtime.cpp	Sun Aug 23 17:39:28 2009 -0700
+++ b/src/runtime.cpp	Sun Aug 23 17:52:47 2009 -0700
@@ -46,6 +46,7 @@
 
   self = (PYM_JSRuntimeObject *) type->tp_alloc(type, 0);
   if (self != NULL) {
+    self->weakrefs = NULL;
     self->thread = PyThread_get_thread_ident();
     self->rt = NULL;
     self->cx = NULL;
@@ -84,6 +85,9 @@
 static void
 PYM_JSRuntimeDealloc(PYM_JSRuntimeObject *self)
 {
+  if (self->weakrefs)
+    PyObject_ClearWeakRefs((PyObject *) self);
+
   if (self->objects.ops) {
     JS_DHashTableFinish(&self->objects);
     self->objects.ops = NULL;
@@ -154,13 +158,15 @@
   0,                           /*tp_getattro*/
   0,                           /*tp_setattro*/
   0,                           /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT,          /*tp_flags*/
+  /*tp_flags*/
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS,
   /* tp_doc */
   "JavaScript Runtime.",
   0,		               /* tp_traverse */
   0,		               /* tp_clear */
   0,		               /* tp_richcompare */
-  0,		               /* tp_weaklistoffset */
+  /* tp_weaklistoffset */
+  offsetof(PYM_JSRuntimeObject, weakrefs),
   0,		               /* tp_iter */
   0,		               /* tp_iternext */
   PYM_JSRuntimeMethods,        /* tp_methods */
--- a/src/runtime.h	Sun Aug 23 17:39:28 2009 -0700
+++ b/src/runtime.h	Sun Aug 23 17:52:47 2009 -0700
@@ -60,6 +60,7 @@
   JSContext *cx;
   JSDHashTable objects;
   long thread;
+  JSObject *weakrefs;
 } PYM_JSRuntimeObject;
 
 extern PyTypeObject PYM_JSRuntimeType;
--- a/tests/test_pymonkey.py	Sun Aug 23 17:39:28 2009 -0700
+++ b/tests/test_pymonkey.py	Sun Aug 23 17:52:47 2009 -0700
@@ -459,6 +459,13 @@
         cx = rt.new_context()
         self.assertEqual(cx.get_runtime(), rt)
 
+    def testRuntimesAreWeakReferencable(self):
+        rt = pymonkey.Runtime()
+        wrt = weakref.ref(rt)
+        self.assertEqual(rt, wrt())
+        del rt
+        self.assertEqual(wrt(), None)
+
     def testContextsAreWeakReferencable(self):
         rt = pymonkey.Runtime()
         cx = rt.new_context()