dfdeshom / v8onpython

Run Javascript code on Google's V8 via Python

Clone this repository (size: 7.2 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/dfdeshom/v8onpython/
commit 51: 88c8141172af
parent 50: 1e9e15c521a1
small cleanup defaulttip
dfd...@gmail.com
10 months ago
View at rev
v8onpython / v8-src /
filename size last modified message
benchmarks  
include  
samples  
src  
test  
tools  
ChangeLog 20.1 KB 13 months ago updated v8 library source code
LICENSE 2.7 KB 13 months ago updated v8 library source code
SConstruct 17.8 KB 13 months ago updated v8 library source code

README

What is it?
-----------
This is a small module that runs Javascript code on Google's V8 engine.
The module takes any valid Javascript string, compiles it, runs and returns the result of 
of the command as a (Python) string. When appropriate, boolean or float objects are 
also returned. Think of this as eval() in  Python. This is released under the BSD license.

Prerequisites
-------------
To build this, you will need to build v8, of course. You will also need to install Cython. I am
sure this can also run on Pyrex, but I have not formally tried it yet.

Building it
-----------
$ python setup.py build_ext --inplace
# add libv8 to your library path
$ export LD_LIBRARY_PATH=/path/to/libv8

This should build v8onpython.so and let you import the module.

Usage
-----
After building it, import it.

>>> import v8onpython
>>> v = v8onpython.Script()
>>> js_code = "for (i=0;i<1000000;i++){\n j=i+1\n}"
>>> v.compile(js_code)
1000000.0
>>> v.compile('1 < 2')
True
>>> v.compile("'Hello' + ', World!'")
'Hello, World!'
>>> v.compile("1.32w")
'SyntaxError: Unexpected token ILLEGAL'
>>> v.compile('a')
'ReferenceError: a is not defined'


Experimental Javascript console
-------------------------------

As of revision 25, there is a custom JS console/interpreter. It has 
history, colors, command search via Ctrl+R, and tab completion. 
It will get better over time: 
$ python repl.py
[v8onpython Javascript console. Press Ctrl+D to exit]
v8onpy > var x=234
v8onpy > x[TAB]
x                 x.toExponential   x.toFixed         
x.toLocaleString  x.toPrecision     x.toString
v8onpy > x.toString()
234
v8onpy > Array.[TAB]
Array.constructor           Array.isPrototypeOf         Array.propertyIsEnumerable  Array.toString
Array.hasOwnProperty        Array.length                Array.toLocaleString        Array.valueOf
v8onpy > function add(a,b){return a+b}
v8onpy > add(9,'2')
92
v8onpy >


That's all! If you have any feedback, please send me an email at dfdeshom at gmail com

What would be cool
------------------

* Code contributions

* Performance numbers of CPython vs JS on V8.

* expand this and make Python and JS talk faster using
  objects, callbacks, etc