diff object.c @ 23:951ad1b15587

The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 22:58:04 -0700
parents 988a8998c75f
children d4efcbb06964
line wrap: on
line diff
--- a/object.c	Sun Jun 28 21:49:07 2009 -0700
+++ b/object.c	Sun Jun 28 22:58:04 2009 -0700
@@ -12,15 +12,10 @@
 static void
 PYM_JSObjectDealloc(PYM_JSObject *self)
 {
-  if (self->weakrefList)
-    PyObject_ClearWeakRefs((PyObject *) self);
-
   if (self->obj) {
-    if (PyDict_DelItem(self->runtime->objects,
-                       self->uniqueId) == -1)
-      PySys_WriteStderr("WARNING: PyDict_DelItem() failed.\n");
-    Py_DECREF(self->uniqueId);
-    self->uniqueId = NULL;
+    JS_DHashTableOperate(&self->runtime->objects,
+                         (void *) self->uniqueId,
+                         JS_DHASH_REMOVE);
 
     // JS_RemoveRoot() always returns JS_TRUE, so don't
     // bother checking its return value.
@@ -62,8 +57,7 @@
   0,		               /* tp_traverse */
   0,		               /* tp_clear */
   0,		               /* tp_richcompare */
-  /* tp_weaklistoffset */
-  offsetof(PYM_JSObject, weakrefList),
+  0,                           /* tp_weaklistoffset */
   0,		               /* tp_iter */
   0,		               /* tp_iternext */
   0,                           /* tp_methods */
@@ -86,51 +80,45 @@
     PyErr_SetString(PYM_error, "JS_GetObjectId() failed");
     return NULL;
   }
-  PyObject *pyUniqueId = PyLong_FromLong(uniqueId);
-  if (pyUniqueId == NULL)
-    return NULL;
 
   PYM_JSRuntimeObject *runtime = context->runtime;
-  PyObject *cachedObject = PyDict_GetItem(runtime->objects,
-                                          pyUniqueId);
-  if (cachedObject) {
-    cachedObject = PyWeakref_GetObject(cachedObject);
-    Py_INCREF(cachedObject);
-    Py_DECREF(pyUniqueId);
-    return (PYM_JSObject *) cachedObject;
+  PYM_HashEntry *cached = (PYM_HashEntry *) JS_DHashTableOperate(
+    &runtime->objects,
+    (void *) uniqueId,
+    JS_DHASH_LOOKUP
+    );
+
+  if (JS_DHASH_ENTRY_IS_BUSY((JSDHashEntryHdr *) cached)) {
+    Py_INCREF((PyObject *) cached->value);
+    return (PYM_JSObject *) cached->value;
   }
 
   PYM_JSObject *object = PyObject_New(PYM_JSObject,
                                       &PYM_JSObjectType);
-  if (object == NULL) {
-    Py_DECREF(pyUniqueId);
+  if (object == NULL)
     return NULL;
-  }
 
   object->runtime = NULL;
   object->obj = NULL;
   object->uniqueId = NULL;
-  object->weakrefList = NULL;
 
-  PyObject *weakref = PyWeakref_NewRef((PyObject *) object, NULL);
-  if (weakref == NULL) {
+  cached = (PYM_HashEntry *) JS_DHashTableOperate(&runtime->objects,
+                                                  (void *) uniqueId,
+                                                  JS_DHASH_ADD);
+  if (cached == NULL) {
     Py_DECREF(object);
-    Py_DECREF(pyUniqueId);
+    PyErr_SetString(PYM_error, "JS_DHashTableOperate() failed");
     return NULL;
   }
 
-  if (PyDict_SetItem(runtime->objects, pyUniqueId, weakref) == -1) {
-    Py_DECREF(weakref);
-    Py_DECREF(object);
-    Py_DECREF(pyUniqueId);
-    return NULL;
-  }
+  cached->base.key = (void *) uniqueId;
+  cached->value = object;
 
   object->runtime = context->runtime;
   Py_INCREF(object->runtime);
 
   object->obj = obj;
-  object->uniqueId = pyUniqueId;
+  object->uniqueId = uniqueId;
 
   JS_AddNamedRootRT(object->runtime->rt, &object->obj,
                     "Pymonkey-Generated Object");