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/
| filename | size | last modified | ||
|---|---|---|---|---|
| v8-src | ||||
| .hgignore | 18 B | 10 months ago | add .hgignore | |
| LICENSE | 1.5 KB | 10 months ago | licensed as BSD | |
| README | 2.1 KB | 10 months ago | merging with http://www.bitbucket.org/sanxiyn/v8onpython/… | |
| THANKS.txt | 261 B | 10 months ago | merging with http://www.bitbucket.org/sanxiyn/v8onpython/… | |
| _readline.py | 9.2 KB | 10 months ago | steal colorstr() function from tli | |
| helpers.h | 226 B | 10 months ago | get rid of hideous #define hack to execute script->Run() | |
| repl.py | 5.3 KB | 8 weeks ago | small cleanup | |
| setup.py | 478 B | 10 months ago | name change | |
| trypy.html | 2.8 KB | 5 months ago | add new files for web shell | |
| trypy.js | 27.5 KB | 8 weeks ago | small cleanup | |
| trypy.py | 8.0 KB | 8 weeks ago | small cleanup | |
| v8onpython.pyx | 3.4 KB | 10 months ago | type conversion: javascript 'undefined' -> Python 'NoneType' |
README
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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
|
