changeset 155:b0c9d6884da3

Annoying workaround to get_stack() for the script object issue discovered in my last commit.
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 11:34:35 -0700
parents 5ec81091cb89
children 7daa74f861c4
files src/context.cpp tests/test_pymonkey.py
diffstat 2 files changed, 17 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/context.cpp	Sun Aug 30 11:13:00 2009 -0700
+++ b/src/context.cpp	Sun Aug 30 11:34:35 2009 -0700
@@ -143,15 +143,24 @@
     JSScript *script = JS_GetFrameScript(self->cx, frame);
     unsigned int pc = 0;
     unsigned int lineno = 0;
-    PyObject *pyScript;
-    if (script) {
+    PyObject *pyScript = NULL;
+
+    // TODO: Ideally, we'd always convert the script to an object and
+    // set it as an attribute of the function, but this can result in
+    // strange segfaults, perhaps because JS functions destroy their
+    // scripts on finalization while creating an object from a script
+    // makes it subject to GC.  So to be safe, we'll only provide the
+    // script object if one already exists.
+    if (script && JS_GetScriptObject(script)) {
       pyScript = (PyObject *) PYM_newJSScript(self, script);
       if (pyScript == NULL)
         return NULL;
       jsbytecode *pcByte = JS_GetFramePC(self->cx, frame);
       pc = pcByte - script->code;
       lineno = JS_PCToLineNumber(self->cx, script, pcByte);
-    } else {
+    }
+
+    if (pyScript == NULL) {
       pyScript = Py_None;
       Py_INCREF(pyScript);
     }
--- a/tests/test_pymonkey.py	Sun Aug 30 11:13:00 2009 -0700
+++ b/tests/test_pymonkey.py	Sun Aug 30 11:34:35 2009 -0700
@@ -81,13 +81,13 @@
         jsfunc = cx.new_function(func, func.__name__)
         self._clearOnTeardown(jsfunc)
         cx.define_property(obj, func.__name__, jsfunc)
-        cx.evaluate_script(obj, 'func()', '<string>', 1)
-        script = stack_holder[0]['caller']['script']
-        pc = stack_holder[0]['caller']['pc']
+        cx.evaluate_script(obj, '(function() { func() })()', '<string>', 1)
+        script = stack_holder[0]['caller']['caller']['script']
+        pc = stack_holder[0]['caller']['caller']['pc']
         self.assertEqual(script.filename, '<string>')
-        self.assertEqual(stack_holder[0]['caller']['lineno'], 1)
+        self.assertEqual(stack_holder[0]['caller']['caller']['lineno'], 1)
         self.assertTrue(pc >= 0 and pc < len(buffer(script)))
-        self.assertEqual(stack_holder[0]['caller']['caller'], None)
+        self.assertEqual(stack_holder[0]['caller']['caller']['caller'], None)
 
     def testScriptHasFilenameMember(self):
         cx = pymonkey.Runtime().new_context()