Skip to main content

Python bindings for the QuickJS JavaScript engine (quickjs-ng)

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

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.0.tar.gz (586.2 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.0-cp313-cp313-win_amd64.whl (459.2 kB view details)

Uploaded CPython 3.13Windows x86-64

quickjs_bindings-0.1.0-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.0-cp313-cp313-macosx_11_0_arm64.whl (509.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

quickjs_bindings-0.1.0-cp311-cp311-win_amd64.whl (458.6 kB view details)

Uploaded CPython 3.11Windows x86-64

quickjs_bindings-0.1.0-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.0-cp311-cp311-macosx_11_0_arm64.whl (509.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

quickjs_bindings-0.1.0-cp39-cp39-win_amd64.whl (458.5 kB view details)

Uploaded CPython 3.9Windows x86-64

quickjs_bindings-0.1.0-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.0-cp39-cp39-macosx_11_0_arm64.whl (509.4 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: quickjs_bindings-0.1.0.tar.gz
  • Upload date:
  • Size: 586.2 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.0.tar.gz
Algorithm Hash digest
SHA256 049a07cdc4731324b1d789cf104acca2c2a8c81a7ce88e07f38534db1da69414
MD5 bb58ac1307988c163a9b807709324dbb
BLAKE2b-256 d989a28d55d4e9f4035f760c17288f6cc2fe06bb2b5a95bbab225a341a7d28cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0.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.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 094e04038ad3112c4f13ee142384955e66638210cca83d080e778b2e1dad69fb
MD5 9ba1a2d9c5e393197e1b68c3b6dd31a2
BLAKE2b-256 c838b85cabbc1cd7e50729c3ca2c0cc884ebc5b48f6581dc611a0649bf7a794b

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 349990a080d0c8a0128a17c74c0fa2345c6c5de084041c05210b9013aef977e9
MD5 477d98f2cdfed5f5e24a4f5674da5cd5
BLAKE2b-256 2d5152f6b2813780be623085b374c727da616d4badf17091425b239bc347436c

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3d904aa254ee31122e14aaf2f2439b1d33290b382982ff5201a735fd84cb02d9
MD5 faa8f6c8e9aab623e464bc15b6f05253
BLAKE2b-256 1d2a02dcf5db2d18afe62eec6e54e4c5589a2b4371a57f21fa1741bc93cf6422

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b1714fb1cd7de42d39355d0f275cc3c54bad368389fa21fa2073312cefe1e5c3
MD5 44d67edc6579600c9af62c683dcd4004
BLAKE2b-256 a4c66f0fc13d9226778eb312474b5a76502b3649a1b8e05347c7794175971e0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 97762345643439cf87671e0afed87b7d037a60c76bc2105a16aef0d02288ce42
MD5 67b885b82df97aaedb6d3406ca8dce79
BLAKE2b-256 2a437966ee5463b036253b8a53e50587e9b938d06199713cef5747fa4d583dad

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0cb90f949663672993831defe53aebb33c6658adac447e3468a2ea65af425542
MD5 e6dcc81f0c2378c414e98584c253430d
BLAKE2b-256 69c386109955d802df80dc8f31de2b636b2b6f10f58a02a0654163b0d9ea8b6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 979774a4de9aa036ea578580600eec90a68a89e0f9727c7fe00ed673cdb42576
MD5 a979734033774ba02480545015cae895
BLAKE2b-256 4b2dc5cdad5b9df988d873ca8d3bf9837a44e102c07dc357e6799d52ca9ce494

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cd3e96016d04b8655fb2ab52c446dc7dc05d65cace713f13bb911069455ef172
MD5 9f59d6fe855f94be115d9ab8f5b60e80
BLAKE2b-256 e320260cec630c01c65dea6ce483440fabf73f858aff76396c5b7cf694fc2ae4

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_bindings-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2350176d2dc1eb44efe4fd755014cbd21d5c240450c83870b6d0e3b0d63784f3
MD5 e3b637d18c4bdd232b2795ff93fa8a11
BLAKE2b-256 db9e7a29212cddb8974b4531760db0426178c88445b27ba5d0042baf045823f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_bindings-0.1.0-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