Mercurial > pymonkey
changeset 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 | 74b7ad049542 |
files | object.c object.h runtime.c runtime.h utils.h |
diffstat | 5 files changed, 41 insertions(+), 42 deletions(-) [+] |
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");
--- a/object.h Sun Jun 28 21:49:07 2009 -0700 +++ b/object.h Sun Jun 28 22:58:04 2009 -0700 @@ -12,8 +12,7 @@ PyObject_HEAD PYM_JSRuntimeObject *runtime; JSObject *obj; - PyObject *uniqueId; - PyObject *weakrefList; + jsid uniqueId; } PYM_JSObject; extern PyTypeObject PYM_JSObjectType;
--- a/runtime.c Sun Jun 28 21:49:07 2009 -0700 +++ b/runtime.c Sun Jun 28 22:58:04 2009 -0700 @@ -11,9 +11,14 @@ self = (PYM_JSRuntimeObject *) type->tp_alloc(type, 0); if (self != NULL) { self->rt = NULL; + self->objects.ops = NULL; - self->objects = PyDict_New(); - if (self->objects == NULL) { + if (!JS_DHashTableInit(&self->objects, + JS_DHashGetStubOps(), + NULL, + sizeof(PYM_HashEntry), + JS_DHASH_DEFAULT_CAPACITY(100))) { + PyErr_SetString(PYM_error, "JS_DHashTableInit() failed"); type->tp_dealloc((PyObject *) self); self = NULL; } @@ -34,9 +39,9 @@ static void PYM_JSRuntimeDealloc(PYM_JSRuntimeObject *self) { - if (self->objects) { - Py_DECREF(self->objects); - self->objects = NULL; + if (self->objects.ops) { + JS_DHashTableFinish(&self->objects); + self->objects.ops = NULL; } if (self->rt) {
--- a/runtime.h Sun Jun 28 21:49:07 2009 -0700 +++ b/runtime.h Sun Jun 28 22:58:04 2009 -0700 @@ -2,12 +2,13 @@ #define PYM_RUNTIME_H #include <jsapi.h> +#include <jsdhash.h> #include <Python/Python.h> typedef struct { PyObject_HEAD JSRuntime *rt; - PyObject *objects; + JSDHashTable objects; } PYM_JSRuntimeObject; extern PyTypeObject PYM_JSRuntimeType;
--- a/utils.h Sun Jun 28 21:49:07 2009 -0700 +++ b/utils.h Sun Jun 28 22:58:04 2009 -0700 @@ -4,8 +4,14 @@ #include "context.h" #include <jsapi.h> +#include <jsdhash.h> #include <Python/Python.h> +typedef struct { + JSDHashEntryStub base; + void *value; +} PYM_HashEntry; + extern PyObject *PYM_error; extern PyObject *