/knight_v8

Minimal, modern embedded V8 for Python.

Primary LanguageC++ISC LicenseISC

Welcome to knight_v8 👋

Version License: ISCL

Minimal, modern embedded V8 for Python.

Install

pip install knight_v8

or:

git clone https://github.com/Esbiya/knight_v8
python setup.py install

Compatibility

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...

Examples

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

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator