diff pymonkey.c @ 0:21aa6e3abb49

Origination.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 12:02:22 -0700
parents
children 2f6bdc9f7245
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pymonkey.c	Sun Jun 28 12:02:22 2009 -0700
@@ -0,0 +1,99 @@
+#include "jsapi.h"
+#include <Python/Python.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_error;
+
+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;
+  }
+
+  JS_EndRequest(cx);
+  JS_DestroyContext(cx);
+  JS_DestroyRuntime(rt);
+
+  // TODO: Get the return value of the script.
+
+  Py_INCREF(Py_None);
+  return Py_None;
+}
+
+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}
+};
+
+PyMODINIT_FUNC
+initpymonkey(void)
+{
+  PyObject *module;
+
+  module = Py_InitModule("pymonkey", PYM_methods);
+  if (module == NULL)
+    return;
+
+  PYM_error = PyErr_NewException("pymonkey.error", NULL, NULL);
+  Py_INCREF(PYM_error);
+  PyModule_AddObject(module, "error", PYM_error);
+}