changeset 13:ca17531e8c81

Added an object class.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 18:19:14 -0700
parents 6d95cfaa1e0b
children 9edcdb4ab12d
files context.c object.c object.h pavement.py pymonkey.c runtime.c test_pymonkey.py
diffstat 7 files changed, 117 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Sun Jun 28 17:43:42 2009 -0700
+++ b/context.c	Sun Jun 28 18:19:14 2009 -0700
@@ -1,4 +1,6 @@
 #include "context.h"
+#include "object.h"
+#include "utils.h"
 
 static void
 PYM_JSContextDealloc(PYM_JSContextObject *self)
@@ -21,9 +23,29 @@
   return (PyObject *) self->runtime;
 }
 
+static PyObject *
+PYM_newObject(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_JSObject *object = PyObject_New(PYM_JSObject,
+                                      &PYM_JSObjectType);
+  if (object == NULL)
+    return NULL;
+
+  object->obj = JS_NewObject(self->cx, &PYM_JS_ObjectClass, NULL, NULL);
+  if (object->obj == NULL) {
+    PyErr_SetString(PYM_error, "JS_NewObject() failed");
+    Py_DECREF(object);
+    return NULL;
+  }
+
+  return (PyObject *) object;
+}
+
 static PyMethodDef PYM_JSContextMethods[] = {
   {"get_runtime", (PyCFunction) PYM_getRuntime, METH_VARARGS,
    "Get the JavaScript runtime associated with this context."},
+  {"new_object", (PyCFunction) PYM_newObject, METH_VARARGS,
+   "Create a new JavaScript object."},
   {NULL, NULL, 0, NULL}
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/object.c	Sun Jun 28 18:19:14 2009 -0700
@@ -0,0 +1,62 @@
+#include "object.h"
+
+JSClass PYM_JS_ObjectClass = {
+  "PymonkeyObject", JSCLASS_GLOBAL_FLAGS,
+  JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+  JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+  JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
+static void
+PYM_JSObjectDealloc(PYM_JSObject *self)
+{
+  // JS_RemoveRoot() always returns JS_TRUE, so don't
+  // bother checking its return value.
+
+  // Umm, we need a context... Crap.
+  //JS_RemoveRoot(
+}
+
+PyTypeObject PYM_JSObjectType = {
+  PyObject_HEAD_INIT(NULL)
+  0,                           /*ob_size*/
+  "pymonkey.Object",           /*tp_name*/
+  sizeof(PYM_JSObject),        /*tp_basicsize*/
+  0,                           /*tp_itemsize*/
+  /*tp_dealloc*/
+  (destructor) PYM_JSObjectDealloc,
+  0,                           /*tp_print*/
+  0,                           /*tp_getattr*/
+  0,                           /*tp_setattr*/
+  0,                           /*tp_compare*/
+  0,                           /*tp_repr*/
+  0,                           /*tp_as_number*/
+  0,                           /*tp_as_sequence*/
+  0,                           /*tp_as_mapping*/
+  0,                           /*tp_hash */
+  0,                           /*tp_call*/
+  0,                           /*tp_str*/
+  0,                           /*tp_getattro*/
+  0,                           /*tp_setattro*/
+  0,                           /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT,          /*tp_flags*/
+  /* tp_doc */
+  "JavaScript Object.",
+  0,		               /* tp_traverse */
+  0,		               /* tp_clear */
+  0,		               /* tp_richcompare */
+  0,		               /* tp_weaklistoffset */
+  0,		               /* tp_iter */
+  0,		               /* tp_iternext */
+  0,                           /* tp_methods */
+  0,                           /* tp_members */
+  0,                           /* tp_getset */
+  0,                           /* tp_base */
+  0,                           /* tp_dict */
+  0,                           /* tp_descr_get */
+  0,                           /* tp_descr_set */
+  0,                           /* tp_dictoffset */
+  0,                           /* tp_init */
+  0,                           /* tp_alloc */
+  0,                           /* tp_new */
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/object.h	Sun Jun 28 18:19:14 2009 -0700
@@ -0,0 +1,19 @@
+#ifndef PYM_OBJECT_H
+#define PYM_OBJECT_H
+
+#include "runtime.h"
+
+#include <jsapi.h>
+#include <Python/Python.h>
+
+extern JSClass PYM_JS_ObjectClass;
+
+typedef struct {
+  PyObject_HEAD
+  PYM_JSRuntimeObject *runtime;
+  JSObject *obj;
+} PYM_JSObject;
+
+extern PyTypeObject PYM_JSObjectType;
+
+#endif
--- a/pavement.py	Sun Jun 28 17:43:42 2009 -0700
+++ b/pavement.py	Sun Jun 28 18:19:14 2009 -0700
@@ -24,6 +24,7 @@
          "-dynamiclib",
          "pymonkey.c",
          "utils.c",
+         "object.c",
          "undefined.c",
          "context.c",
          "runtime.c"]
--- a/pymonkey.c	Sun Jun 28 17:43:42 2009 -0700
+++ b/pymonkey.c	Sun Jun 28 18:19:14 2009 -0700
@@ -1,6 +1,7 @@
 #include "undefined.h"
 #include "runtime.h"
 #include "context.h"
+#include "object.h"
 #include "utils.h"
 
 #include <jsapi.h>
@@ -118,4 +119,10 @@
 
   Py_INCREF(&PYM_JSContextType);
   PyModule_AddObject(module, "Context", (PyObject *) &PYM_JSContextType);
+
+  if (!PyType_Ready(&PYM_JSObjectType) < 0)
+    return;
+
+  Py_INCREF(&PYM_JSObjectType);
+  PyModule_AddObject(module, "Object", (PyObject *) &PYM_JSObjectType);
 }
--- a/runtime.c	Sun Jun 28 17:43:42 2009 -0700
+++ b/runtime.c	Sun Jun 28 18:19:14 2009 -0700
@@ -37,6 +37,8 @@
 {
   PYM_JSContextObject *context = PyObject_New(PYM_JSContextObject,
                                               &PYM_JSContextType);
+  if (context == NULL)
+    return NULL;
 
   context->runtime = self;
   Py_INCREF(self);
--- a/test_pymonkey.py	Sun Jun 28 17:43:42 2009 -0700
+++ b/test_pymonkey.py	Sun Jun 28 18:19:14 2009 -0700
@@ -9,6 +9,10 @@
         self.assertTrue(isinstance(cx, pymonkey.Context))
         self.assertEqual(cx.get_runtime(), rt)
 
+        obj = cx.new_object()
+        self.assertRaises(TypeError, pymonkey.Object)
+        self.assertTrue(isinstance(obj, pymonkey.Object))
+
     def testUndefinedCannotBeInstantiated(self):
         self.assertRaises(TypeError, pymonkey.undefined)