# HG changeset patch # User Atul Varma # Date 1246215742 25200 # Node ID 21aa6e3abb49d89f7b127f3c1cad426c1ef736f3 Origination. diff -r 000000000000 -r 21aa6e3abb49 pavement.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pavement.py Sun Jun 28 12:02:22 2009 -0700 @@ -0,0 +1,44 @@ +import os +import subprocess +import shutil +import sys + +from paver.easy import * + +@task +def auto(options): + objdir = os.path.join("..", "mozilla-stuff", "basic-firefox") + objdir = os.path.abspath(objdir) + incdir = os.path.join(objdir, "dist", "include") + libdir = os.path.join(objdir, "dist", "lib") + + print "Building extension." + + result = subprocess.call( + ["g++", + "-framework", "Python", + "-I%s" % incdir, + "-L%s" % libdir, + "-lmozjs", + "-o", "pymonkey.so", + "-dynamiclib", + "pymonkey.c"] + ) + + if result: + sys.exit(result) + + print "Running test suite." + + new_env = {} + new_env.update(os.environ) + new_env['DYLD_LIBRARY_PATH'] = libdir + + result = subprocess.call( + [sys.executable, + "test_pymonkey.py"], + env = new_env + ) + + if result: + sys.exit(result) diff -r 000000000000 -r 21aa6e3abb49 pymonkey.c --- /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 + +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); +} diff -r 000000000000 -r 21aa6e3abb49 test_pymonkey.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_pymonkey.py Sun Jun 28 12:02:22 2009 -0700 @@ -0,0 +1,9 @@ +import unittest +import pymonkey + +class PymonkeyTests(unittest.TestCase): + def test_evaluate(self): + pymonkey.evaluate('1+3', '', 1) + +if __name__ == '__main__': + unittest.main()