Skip to main content

Tangled QuickJS Javascript Engine binding library

Project description

Build Status Stable Version Coverage Python License

thquickjs

Python binding for QuickJS JavaScript Engine. QuickJS is a small and embeddable JavaScript engine. Safely evaluate untrusted JavaScript code. Create and manipulate values inside the QuickJS runtime. Expose host functions to the QuickJS runtime.

Installation

pip install thquickjs

Simple Usage

from thresult import unwrap
from thquickjs.thquickjs import QuickJS


qjs = QuickJS()

with unwrap():
    # call JS function
    code = '''
        var f = (x) => {
            return [ x, x, x ];
        };
    '''

    qjs.eval(code)
    f: callable = qjs.get_function('f')
    result = f(10)
    assert result == [10, 10, 10]

Advanced Usage

from thresult import unwrap
from thquickjs.thquickjs import QuickJS


qjs = QuickJS()

with unwrap():
    # call JS function
    code = '''
        var f = (x) => {
            return [ x ];
        };
    '''

    qjs.eval(code)
    f: callable = qjs.get_function('f')
    result = f(10)
    assert result == [10]

    # register Python function inside JS runtime
    py_name = 'pylam'
    py_func = lambda x: json.dumps([x * 10])
    qjs.add_callable(py_name, py_func)

    # call Python function from JS runtime
    code = '''
        var g = (x) => {
            return [ JSON.parse(pylam(x)) ];
        };
    '''

    qjs.eval(code)
    g: callable = qjs.get_function('g')
    result = g(10)
    assert result == [[100]]
    
    # call Python function registered in JS runtime
    pylam = qjs.get(py_name)
    result = pylam(5)
    assert result == json.dumps([50])

Testing

docker-compose build thquickjs-test ; docker-compose run --rm thquickjs-test

Coverage

docker-compose build thquickjs-coverage ; docker-compose run --rm -v $PWD:/test thquickjs-coverage

Building

docker-compose build thquickjs-build ; docker-compose run --rm thquickjs-build

Licensing

thquickjs is licensed under the BSD 3 license.

Check the LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thquickjs-0.9.26.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

thquickjs-0.9.26-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file thquickjs-0.9.26.tar.gz.

File metadata

  • Download URL: thquickjs-0.9.26.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.4.109+

File hashes

Hashes for thquickjs-0.9.26.tar.gz
Algorithm Hash digest
SHA256 cdf955469144153baff2c704eea64c6e49222bd4100c784cc3e21e6f0a991224
MD5 46a84ca3892789c5b5c81cfac2c3ae99
BLAKE2b-256 c0f25119b6b403bfaf0b0c3fe70d79f1798e1a390387e059eeaeeeb6b44b5dbd

See more details on using hashes here.

File details

Details for the file thquickjs-0.9.26-py3-none-any.whl.

File metadata

  • Download URL: thquickjs-0.9.26-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.4.109+

File hashes

Hashes for thquickjs-0.9.26-py3-none-any.whl
Algorithm Hash digest
SHA256 e3a183f538bcc71ee9400da29723ff480b4e7ddd558d64f3c01b9be59ef94052
MD5 f647df755555604915ffe435896154f5
BLAKE2b-256 3caed85ad396a4b176a5e84f82577ec8ee8a6eeca508ea0aa7eb721d54a4489b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page