PetterS / quickjs

Thin Python wrapper of https://bellard.org/quickjs/
https://github.com/bellard/QuickJS
MIT License
175 stars 19 forks source link
javascript python python-library

CircleCI PyPI version fury.io

Just install with

pip install quickjs

Binaries are provided for:

Usage

from quickjs import Function

f = Function("f", """
    function adder(a, b) {
        return a + b;
    }

    function f(a, b) {
        return adder(a, b);
    }
    """)

assert f(1, 2) == 3

Simple types like int, floats and strings are converted directly. Other types (dicts, lists) are converted via JSON by the Function class. The library is thread-safe if Function is used. If the Context class is used directly, it can only ever be accessed by the same thread. This is true even if the accesses are not concurrent.

Both Function and Context expose set_memory_limit and set_time_limit functions that allow limits for code running in production.

API

The Function class has, apart from being a callable, additional methods:

Documentation

For full functionality, please see test_quickjs.py

Developing

This project uses a git submodule for the upstream code, so clone it with the --recurse-submodules option or run git submodule update --init --recursive afterwards.

Use a poetry shell and make test should work from inside its virtual environment.