changeset 16:532b7ddca616

Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 19:01:43 -0700
parents baa4cb961330
children 0812422ec99e
files context.c pymonkey.c test_pymonkey.py
diffstat 3 files changed, 63 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Sun Jun 28 18:39:43 2009 -0700
+++ b/context.c	Sun Jun 28 19:01:43 2009 -0700
@@ -63,6 +63,37 @@
   Py_RETURN_NONE;
 }
 
+static PyObject *
+PYM_evaluateScript(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_JSObject *object;
+  const char *source;
+  int sourceLen;
+  const char *filename;
+  int lineNo;
+
+  if (!PyArg_ParseTuple(args, "O!s#si", &PYM_JSObjectType, &object,
+                        &source, &sourceLen, &filename, &lineNo))
+    return NULL;
+
+  JS_BeginRequest(self->cx);
+
+  jsval rval;
+  if (!JS_EvaluateScript(self->cx, object->obj, source, sourceLen,
+                         filename, lineNo, &rval)) {
+    // TODO: Actually get the error that was raised.
+    PyErr_SetString(PYM_error, "Script failed");
+    JS_EndRequest(self->cx);
+    return NULL;
+  }
+
+  PyObject *pyRval = PYM_jsvalToPyObject(rval);
+
+  JS_EndRequest(self->cx);
+
+  return pyRval;
+}
+
 static PyMethodDef PYM_JSContextMethods[] = {
   {"get_runtime", (PyCFunction) PYM_getRuntime, METH_VARARGS,
    "Get the JavaScript runtime associated with this context."},
@@ -71,6 +102,12 @@
   {"init_standard_classes",
    (PyCFunction) PYM_initStandardClasses, METH_VARARGS,
    "Add standard classes and functions to the given object."},
+  {"evaluate_script",
+   (PyCFunction) PYM_evaluateScript, METH_VARARGS,
+   "Evaluate the given JavaScript code in the context of the given "
+   "global object, using the given filename"
+   "and line number information."},
+
   {NULL, NULL, 0, NULL}
 };
 
--- a/pymonkey.c	Sun Jun 28 18:39:43 2009 -0700
+++ b/pymonkey.c	Sun Jun 28 19:01:43 2009 -0700
@@ -4,85 +4,7 @@
 #include "object.h"
 #include "utils.h"
 
-#include <jsapi.h>
-
-static JSClass PYM_jsGlobalClass = {
-  "PymonkeyGlobal", JSCLASS_GLOBAL_FLAGS,
-  JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
-  JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
-  JSCLASS_NO_OPTIONAL_MEMBERS
-};
-
-static PyObject *
-PYM_evaluate(PyObject *self, PyObject *args)
-{
-  const char *source;
-  int sourceLen;
-  const char *filename;
-  int lineNo;
-
-  if (!PyArg_ParseTuple(args, "s#si", &source, &sourceLen,
-                        &filename, &lineNo))
-    return NULL;
-
-  JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L);
-  if (rt == NULL) {
-    PyErr_SetString(PYM_error, "JS_NewRuntime() failed");
-    return NULL;
-  }
-
-  JSContext *cx = JS_NewContext(rt, 8192);
-  if (cx == NULL) {
-    PyErr_SetString(PYM_error, "JS_NewContext() failed");
-    JS_DestroyRuntime(rt);
-  }
-
-  JS_SetOptions(cx, JSOPTION_VAROBJFIX);
-  JS_SetVersion(cx, JSVERSION_LATEST);
-
-  JS_BeginRequest(cx);
-
-  JSObject *global = JS_NewObject(cx, &PYM_jsGlobalClass, NULL, NULL);
-  if (global == NULL) {
-    PyErr_SetString(PYM_error, "JS_NewObject() failed");
-    JS_EndRequest(cx);
-    JS_DestroyContext(cx);
-    JS_DestroyRuntime(rt);
-    return NULL;
-  }
-
-  if (!JS_InitStandardClasses(cx, global)) {
-    PyErr_SetString(PYM_error, "JS_InitStandardClasses() failed");
-    JS_EndRequest(cx);
-    JS_DestroyContext(cx);
-    JS_DestroyRuntime(rt);
-    return NULL;
-  }
-
-  jsval rval;
-  if (!JS_EvaluateScript(cx, global, source, sourceLen, filename,
-                         lineNo, &rval)) {
-    // TODO: Actually get the error that was raised.
-    PyErr_SetString(PYM_error, "Script failed");
-    JS_EndRequest(cx);
-    JS_DestroyContext(cx);
-    JS_DestroyRuntime(rt);
-    return NULL;
-  }
-
-  PyObject *pyRval = PYM_jsvalToPyObject(rval);
-
-  JS_EndRequest(cx);
-  JS_DestroyContext(cx);
-  JS_DestroyRuntime(rt);
-
-  return pyRval;
-}
-
 static PyMethodDef PYM_methods[] = {
-  {"evaluate", PYM_evaluate, METH_VARARGS,
-   "Evaluate the given JavaScript code, using the given filename "
-   "and line number information."},
   {NULL, NULL, 0, NULL}
 };
 
--- a/test_pymonkey.py	Sun Jun 28 18:39:43 2009 -0700
+++ b/test_pymonkey.py	Sun Jun 28 19:01:43 2009 -0700
@@ -2,47 +2,58 @@
 import pymonkey
 
 class PymonkeyTests(unittest.TestCase):
-    def testJSRuntimeWorks(self):
+    def _evaljs(self, code):
         rt = pymonkey.Runtime()
         cx = rt.new_context()
-        self.assertRaises(TypeError, pymonkey.Context)
+        obj = cx.new_object()
+        cx.init_standard_classes(obj)
+        return cx.evaluate_script(obj, code, '<string>', 1)
+
+    def testContextIsInstance(self):
+        cx = pymonkey.Runtime().new_context()
         self.assertTrue(isinstance(cx, pymonkey.Context))
-        self.assertEqual(cx.get_runtime(), rt)
 
-        obj = cx.new_object()
-        self.assertRaises(TypeError, pymonkey.Object)
+    def testContextTypeCannotBeInstantiated(self):
+        self.assertRaises(TypeError, pymonkey.Context)
+
+    def testObjectIsInstance(self):
+        obj = pymonkey.Runtime().new_context().new_object()
         self.assertTrue(isinstance(obj, pymonkey.Object))
 
-        cx.init_standard_classes(obj)
+    def testObjectTypeCannotBeInstantiated(self):
+        self.assertRaises(TypeError, pymonkey.Object)
+
+    def testGetRuntimeWorks(self):
+        rt = pymonkey.Runtime()
+        cx = rt.new_context()
+        self.assertEqual(cx.get_runtime(), rt)
 
     def testUndefinedCannotBeInstantiated(self):
         self.assertRaises(TypeError, pymonkey.undefined)
 
     def testEvaluateReturnsUndefined(self):
-        retval = pymonkey.evaluate("", '<string>', 1)
+        retval = self._evaljs("")
         self.assertTrue(retval is pymonkey.undefined)
 
     def testEvaluateReturnsUnicode(self):
-        retval = pymonkey.evaluate("'o hai\u2026'", '<string>', 1)
+        retval = self._evaljs("'o hai\u2026'")
         self.assertTrue(type(retval) == unicode)
         self.assertEqual(retval, u'o hai\u2026')
 
     def testEvaluateReturnsTrue(self):
-        self.assertTrue(pymonkey.evaluate('true', '<string>', 1) is True)
+        self.assertTrue(self._evaljs('true') is True)
 
     def testEvaluateReturnsFalse(self):
-        self.assertTrue(pymonkey.evaluate('false', '<string>', 1) is False)
+        self.assertTrue(self._evaljs('false') is False)
 
     def testEvaluateReturnsNone(self):
-        self.assertTrue(pymonkey.evaluate('null', '<string>', 1) is None)
+        self.assertTrue(self._evaljs('null') is None)
 
     def testEvaluateReturnsIntegers(self):
-        self.assertEqual(pymonkey.evaluate('1+3', '<string>', 1),
-                         4)
+        self.assertEqual(self._evaljs('1+3'), 4)
 
     def testEvaluateReturnsFloats(self):
-        self.assertEqual(pymonkey.evaluate('1.1+3', '<string>', 1),
-                         4.1)
+        self.assertEqual(self._evaljs('1.1+3'), 4.1)
 
 if __name__ == '__main__':
     unittest.main()