Skip to main content

Python bindings for QuickJS, Fabrice Bellard's small and embeddable JavaScript engine

Project description

quickjs-py

Python bindings for QuickJS, Fabrice Bellard's small and embeddable JavaScript engine.

The QuickJS engine is included as a git submodule and compiled directly into the extension, so there is no external dependency on a system QuickJS install.

Installation

Install the latest release from PyPI:

pip install quickjs-bindings

From source

To build from a checkout, the engine lives in a git submodule, so initialise it first:

git submodule update --init
pip install -e .

A C compiler and the Python development headers are required to build the extension.

QuickJS uses GCC extensions and POSIX headers and cannot be compiled with MSVC. On Windows install mingw-w64 (for example choco install mingw) and make sure gcc is on PATH; the build selects the mingw toolchain automatically. Linux and macOS use the system GCC/Clang.

Quick start

import quickjs

# One-off evaluation
print(quickjs.eval("1 + 2"))            # 3

# A reusable context
ctx = quickjs.Context()
ctx.eval("var counter = 0;")
ctx.eval("counter += 10;")
print(ctx.eval("counter"))              # 10

# Pass Python values into JS
ctx.set("data", {"name": "Ada", "scores": [90, 95]})
print(ctx.eval("data.scores[1]"))       # 95

# Expose Python callables to JS
ctx.set("greet", lambda name: f"hi {name}")
print(ctx.eval('greet("world")'))       # hi world

# Convert JS values back to Python
result = ctx.eval("({a: 1, b: [2, 3]})")
print(result.to_python())               # {'a': 1, 'b': [2, 3]}

# JS errors surface as Python exceptions
try:
    ctx.eval('throw new Error("boom")')
except quickjs.JSError as exc:
    print(exc)                          # Error: boom

Architecture

  • quickjs._quickjs - a C extension wrapping the QuickJS C API (Runtime, Context, Value).
  • quickjs - the public package re-exporting those types plus convenience helpers.

See CLAUDE.md for the full design and TODO.md for the roadmap.

Documentation

  • docs/api.md - full API reference for Runtime, Context and Value.
  • examples/ - runnable example scripts; see examples/README.md.
  • CHANGELOG.md - release history.

Type conversion

Python JavaScript
None null
bool boolean
int number / bigint
float number
str string
bytes ArrayBuffer
list / tuple Array
dict Object
callable function

JS objects, arrays and functions returned to Python are wrapped as quickjs.Value. Call .to_python() to recursively convert arrays/objects into native list/dict structures.

JS null becomes Python None; JS undefined becomes the distinct quickjs.Undefined singleton.

More features

import quickjs

ctx = quickjs.Context()

# Compile once, run many times (and serialise to bytecode)
compiled = ctx.compile("Math.PI * r * r")
blob = compiled.write_object()
restored = ctx.read_object(blob)

# ArrayBuffer / TypedArray access
buf = ctx.new_array_buffer(b"\x01\x02\x03")
ctx.eval("new Uint8Array([4, 5, 6])").to_bytes()      # b'\x04\x05\x06'

# Embed an arbitrary Python object opaquely inside JS
handle = ctx.new_host_object(open("data.txt"))         # round-trips unchanged

# Accessor properties backed by Python callables
obj = ctx.new_object()
obj.define_property("now", get=lambda: __import__("time").time())

# Custom ES module loader
ctx.set_module_loader(lambda name: MODULES.get(name))
ctx.eval("import { x } from 'mod';", module=True)

# Run async JS: async_eval pumps the job queue until the result settles
ctx.eval("async function add(a, b) { return await Promise.resolve(a + b); }")
ctx.async_eval("add(2, 3)")                            # -> 5

# await_promise bridges QuickJS promises into asyncio
async def main():
    return await ctx.await_promise(ctx.eval("add(40, 2)"))   # -> 42

# Engine memory counters
ctx.runtime.compute_memory_usage()                     # -> dict

# Context manager
with quickjs.Context() as c:
    c.eval("1 + 1")

Testing

pip install pytest
pytest -q

License

MIT. The vendored QuickJS sources are under the MIT license; see vendor/quickjs/LICENSE.

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

quickjs_bindings-0.1.1.tar.gz (591.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

quickjs_bindings-0.1.1-cp313-cp313-win_amd64.whl (460.9 kB view details)

Uploaded CPython 3.13Windows x86-64

quickjs_bindings-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

quickjs_bindings-0.1.1-cp313-cp313-macosx_11_0_arm64.whl (511.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

quickjs_bindings-0.1.1-cp311-cp311-win_amd64.whl (460.2 kB view details)

Uploaded CPython 3.11Windows x86-64

quickjs_bindings-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

quickjs_bindings-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (511.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

quickjs_bindings-0.1.1-cp39-cp39-win_amd64.whl (460.1 kB view details)

Uploaded CPython 3.9Windows x86-64

quickjs_bindings-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

quickjs_bindings-0.1.1-cp39-cp39-macosx_11_0_arm64.whl (511.3 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file quickjs_bindings-0.1.1.tar.gz.

File metadata

  • Download URL: quickjs_bindings-0.1.1.tar.gz
  • Upload date:
  • Size: 591.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for quickjs_bindings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f18ae265c9c360580714e114442b45d4cb01f69fff6f7026c8f4aed8be034c71
MD5 28cced00f29230bafcf0671955642f0e
BLAKE2b-256 7d5fc8313ea2b765b3382636a7d64553cae9a86df6cd9e8634c7fed81603f532

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1.tar.gz:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ae804edf6c763fcc544339c4da79fcdcb9fb67fabd5de84db52ef60dbf5ba780
MD5 adbc064af10ff240ecba6b3516c8faf8
BLAKE2b-256 4e1eb5b1f205eb7cb6a458eb5e29a4ad0a94aafd7e89bac970514d26eedd1485

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp313-cp313-win_amd64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99c67b6a7f874d819ecda862498d172154bd57c5b20b37b9d05ed629c18cd0e2
MD5 0c873b12a6f3ccdaf5577ff1231e35fa
BLAKE2b-256 057f0ad5e3edfe9e76149cd1954d638ff236b0a1b17dc2b82791c2bd5a1d073e

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a7d00d759c84c3878cb812869fed3c6f9c5854424d7f744de9d7d24e7508777e
MD5 cd21cef421c3dff737446764c5c13ea3
BLAKE2b-256 4d2476f4c6041e58cae9af89c73ed9e41c1901d34220a4717613b9d634771876

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 19fcb9845fd29e54c62753dc7a1b2eaee745e4e27ccaaa86f0846af1cd147e9b
MD5 26a4215a72b56e04f6b1bd6f217b5ed0
BLAKE2b-256 1e4b83e210ee47a3efeaa117539ede27a75c67ad1052ffbf89e3336c5f747e02

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp311-cp311-win_amd64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40969870dd8d493f2089ad98f706d4472a7a4fcc776e059c3bf528e9fc5f8154
MD5 8d87d35d9f182a6024ff030f11b1ec31
BLAKE2b-256 5b813e104d6ad606774d3c3d9aa272e9a4db30b9c97b9400a5156d825d44ff67

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 76f3af2d9812de752de071c1ba0c7097e0586a37ab11deacfce2c64796491ce3
MD5 f7bcccc902fb6eae821f45ac2a1192d3
BLAKE2b-256 228cd60f4b1294bdcf817618a26a1e44bf38099130973f93e7a59a9ffd18ff26

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bfadbdc2fbc5e289543b4f212baacc9360cc51a8a3612b4ff38045c0d787897e
MD5 8ac6dba3dd869ffac0d097f223bc4e8a
BLAKE2b-256 7f029788059dec8830455e500e83058643ab34483f0909fbf8d05b7b7452e7b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp39-cp39-win_amd64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68622f17f05afbd922b474a32a0aac3df09e6857ff902170ae1269093c38748b
MD5 09a90a81b0df2f623637f17fe8538af5
BLAKE2b-256 f0bfdcd3e7ab1a1311e3f7c829cd0ed3da30bdd7fa1568e4dd0834013dd98d01

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_bindings-0.1.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c3e2a49610ceb40ec2b722c0369547e084226289a942c539ce6dcad09b77bdf7
MD5 b56bad4db3a20c66e51d4c8fbfa625a8
BLAKE2b-256 01bc69ef2b8ca883b6aa325d1ced9bbb44df9b2432f4896399161414beafc64d

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.1-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: release.yml on slightc/quickjs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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