changeset 89:e77bc7c799e8

JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
author Atul Varma <varmaa@toolness.com>
date Sun, 09 Aug 2009 22:54:15 -0700
parents 0b2970e8cd67
children c41f1d2e8f9d
files context.c runtime.h test_pymonkey.py
diffstat 3 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Sun Aug 09 22:26:45 2009 -0700
+++ b/context.c	Sun Aug 09 22:54:15 2009 -0700
@@ -119,6 +119,8 @@
   if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
   JSObject *obj = object->obj;
 
   if (JS_ObjectIsFunction(self->cx, obj)) {
@@ -159,6 +161,8 @@
   if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
   JSObject *obj = object->obj;
 
   if (JS_ObjectIsFunction(self->cx, obj)) {
@@ -216,6 +220,8 @@
                         &string))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
   JSString *jsString = JS_NewUCStringCopyZ(self->cx,
                                            (const jschar *) string);
   if (jsString == NULL) {
@@ -262,6 +268,8 @@
   if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
   if (!JS_InitStandardClasses(self->cx, object->obj)) {
     PyErr_SetString(PYM_error, "JS_InitStandardClasses() failed");
     return NULL;
@@ -284,6 +292,8 @@
                         &source, &sourceLen, &filename, &lineNo))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
   jsval rval;
   JSBool result;
   Py_BEGIN_ALLOW_THREADS;
@@ -312,6 +322,7 @@
                         &name, &value))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
   jsval jsValue;
 
   if (PYM_pyObjectToJsval(self, value, &jsValue) == -1)
@@ -342,6 +353,9 @@
                         &PyTuple_Type, &funcArgs))
     return NULL;
 
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, obj->runtime);
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, fun->base.runtime);
+
   uintN argc = PyTuple_Size(funcArgs);
   jsval argv[argc];
   jsval *currArg = argv;
--- a/runtime.h	Sun Aug 09 22:26:45 2009 -0700
+++ b/runtime.h	Sun Aug 09 22:54:15 2009 -0700
@@ -48,6 +48,12 @@
     return NULL; \
   }
 
+#define PYM_ENSURE_RUNTIME_MATCH(runtime1, runtime2) \
+  if (runtime1 != runtime2) { \
+    PyErr_SetString(PyExc_ValueError, "JS runtime mismatch"); \
+    return NULL; \
+  }
+
 typedef struct {
   PyObject_HEAD
   JSRuntime *rt;
--- a/test_pymonkey.py	Sun Aug 09 22:26:45 2009 -0700
+++ b/test_pymonkey.py	Sun Aug 09 22:54:15 2009 -0700
@@ -498,6 +498,16 @@
                                         self.last_exception.message),
                          'ReferenceError: blarg is not defined')
 
+    def testInitStandardClassesRaisesExcOnRuntimeMismatch(self):
+        cx2 = pymonkey.Runtime().new_context()
+        cx = pymonkey.Runtime().new_context()
+        obj = cx.new_object()
+        self.assertRaises(ValueError,
+                          cx2.init_standard_classes,
+                          obj)
+        self.assertEqual(self.last_exception.message,
+                         'JS runtime mismatch')
+
     def testCallFunctionWorks(self):
         cx = pymonkey.Runtime().new_context()
         obj = cx.new_object()