changeset 19:fbb9a61fa030

Moved context creation code into its own public function in context.c.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 20:08:59 -0700
parents f3223debd70b
children abede8af8cf5
files context.c context.h object.h runtime.c
diffstat 4 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Sun Jun 28 20:00:48 2009 -0700
+++ b/context.c	Sun Jun 28 20:08:59 2009 -0700
@@ -32,6 +32,8 @@
     return NULL;
   }
 
+  // If this fails, we don't need to worry about cleaning up
+  // obj because it'll get cleaned up at the next GC.
   return (PyObject *) PYM_newJSObject(self->runtime, obj);
 }
 
@@ -184,3 +186,19 @@
   0,                           /* tp_alloc */
   0,                           /* tp_new */
 };
+
+extern PYM_JSContextObject *
+PYM_newJSContextObject(PYM_JSRuntimeObject *runtime, JSContext *cx)
+{
+  PYM_JSContextObject *context = PyObject_New(PYM_JSContextObject,
+                                              &PYM_JSContextType);
+  if (context == NULL)
+    return NULL;
+
+  context->runtime = runtime;
+  Py_INCREF(runtime);
+
+  context->cx = cx;
+
+  return context;
+}
--- a/context.h	Sun Jun 28 20:00:48 2009 -0700
+++ b/context.h	Sun Jun 28 20:08:59 2009 -0700
@@ -14,4 +14,8 @@
 
 extern PyTypeObject PYM_JSContextType;
 
+extern PYM_JSContextObject *
+PYM_newJSContextObject(PYM_JSRuntimeObject *runtime,
+                       JSContext *cx);
+
 #endif
--- a/object.h	Sun Jun 28 20:00:48 2009 -0700
+++ b/object.h	Sun Jun 28 20:08:59 2009 -0700
@@ -16,6 +16,7 @@
 
 extern PyTypeObject PYM_JSObjectType;
 
-extern PYM_JSObject *PYM_newJSObject(PYM_JSRuntimeObject *runtime,
-                                     JSObject *obj);
+extern PYM_JSObject *
+PYM_newJSObject(PYM_JSRuntimeObject *runtime, JSObject *obj);
+
 #endif
--- a/runtime.c	Sun Jun 28 20:00:48 2009 -0700
+++ b/runtime.c	Sun Jun 28 20:08:59 2009 -0700
@@ -35,25 +35,21 @@
 static PyObject *
 PYM_newContext(PYM_JSRuntimeObject *self, PyObject *args)
 {
-  PYM_JSContextObject *context = PyObject_New(PYM_JSContextObject,
-                                              &PYM_JSContextType);
-  if (context == NULL)
-    return NULL;
-
-  context->runtime = self;
-  Py_INCREF(self);
-
-  context->cx = JS_NewContext(self->rt, 8192);
-  if (context->cx == NULL) {
+  JSContext *cx = JS_NewContext(self->rt, 8192);
+  if (cx == NULL) {
     PyErr_SetString(PYM_error, "JS_NewContext() failed");
-    Py_DECREF(context);
     return NULL;
   }
 
-  JS_SetOptions(context->cx, JSOPTION_VAROBJFIX);
-  JS_SetVersion(context->cx, JSVERSION_LATEST);
+  JS_SetOptions(cx, JSOPTION_VAROBJFIX);
+  JS_SetVersion(cx, JSVERSION_LATEST);
+
+  PyObject *retval = (PyObject *) PYM_newJSContextObject(self, cx);
 
-  return (PyObject *) context;
+  if (retval == NULL)
+    JS_DestroyContext(cx);
+
+  return retval;
 }
 
 static PyMethodDef PYM_JSRuntimeMethods[] = {