Unfortunately the python-spidermonkey project is dead and there’s no way of telling if it works with later version of SpiderMonkey. The PyV8 project by contrast is still undergoing active development.
Although PyV8 has a wiki page entitled How To Build it’s not simple to get the project built. They recommend using prebuilt packages, but there are none for recent version of Ubuntu. In this post I’ll describe how to build it on Ubuntu 11.11 and give a simple example of it in action.
The first step is make sure you have the appropriate packages. There may be others that are required and not part of the default install, but there are what I had to install.
sudo aptitude install scons libboost-python-dev
Next you need to checkout both the V8 and PyV8 projects using the commands below.
svn checkout http://v8.googlecode.com/svn/trunk/ v8 svn checkout http://pyv8.googlecode.com/svn/trunk/ pyv8
The key step before building PyV8 is to set the
V8_HOME environment variable to the directory where
you checked out the V8 code. This allows PyV8 to patch V8 and build it as a static library rather than the
default dynamic library. Once you’ve set that you can use the standard Python
setup.py commands to
build and install the library.
cd v8 export PyV8=`pwd` cd ../pyv8 python setup.py build sudo python setup.py install
To start import the
PyV8 library and create a
JSContext object. These are equivalent to
>>> import PyV8 >>> ctxt = PyV8.JSContext()
enter() the context. You should also
it when you are complete.
JSContext objects can be used with
with statements to automate
this, but for a console session it’s simplest to call the method explicitly. Next we call
expressions are returned as Python objects, so here
rendered contains a Python string.