v8onpython /

Filename Size Date modified Message
v8-src
18 B
1.5 KB
2.2 KB
261 B
9.5 KB
226 B
5.4 KB
478 B
2.8 KB
28.1 KB
8.2 KB
3.4 KB

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