Minimal, modern embedded V8 for Python.
🏠 Homepage
pip install knight_v8
or:
git clone https://github.com/Esbiya/knight_v8
python setup.py install
knight_v8 is compatible with Python 2 & 3 and based on ctypes. Now this lib only support darwin(x86_64) and linux(glic), waiting for more platform support...
check v8 version:
>>> from knight_v8 import v8_version
>>> v8_version()
'9.6.180.12'
knight_v8 is straightforward to use:
>>> from knight_v8 import Knight
>>> ctx = Knight()
>>> ctx.eval("1+1")
2
>>> ctx.eval("var x = {company: 'Sqreen'}; x.company")
'Sqreen'
>>> print(ctx.eval("'\N{HEAVY BLACK HEART}'"))
❤
Variables are kept inside of a context:
>>> ctx.eval("x.company")
'Sqreen'
While eval
only supports returning primitive data types such as
strings, call
supports returning composite types such as objects:
>>> ctx.compile("var fun = () => ({ foo: 1 });")
>>> ctx.call("fun")
{'foo': 1}
knight_v8 is ES6 capable:
>>> ctx.eval("[1,2,3].includes(5)")
False
Please do not new a lot of Knight instances, if you have to do this, call dispose method of the instance after using out.
>>> ctx = Knight()
>>> ctx.eval("1 + 1")
2
>>> ctx.dispose()
or use with statement, in this case, you don't have to actively release instance:
>>> with Knight() as ctx:
ctx.eval("1 + 1")
2
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator