Skip to main content

Python bindings for QuickJS

Project description

Tests

python-quickjs-runtime

A lightweight Python binding for the QuickJS Javascript Engine.

This library allows you to execute JavaScript code within your Python applications, providing seamless interoperability between Python and JavaScript.

Features

  • Evaluate JavaScript: Run JS code strings directly from Python.
  • ES2023 Support: Support for modern JS features including async/await.
  • Data Exchange: Pass int, float, str, bool, list, dict, and BigInt between Python and JS.
  • Python Callbacks: Call Python functions directly from JavaScript.
  • REPL: Includes a simple interactive shell for testing JS snippets.
  • Easy Build: Uses zigcc-build for reliable cross-platform compilation.

Installation

You can install the package from the source:

pip install .

Usage

Basic Evaluation

from quickjs_runtime import Runtime

rt = Runtime()
ctx = rt.new_context()

# Evaluate simple expressions
result = ctx.eval("1 + 2")
print(result)  # 3

# Evaluate strings
greeting = ctx.eval("'Hello ' + 'World'")
print(greeting)  # "Hello World"

Interoperability

You can set global variables in the JavaScript context and retrieve results.

from quickjs_runtime import Runtime

rt = Runtime()
ctx = rt.new_context()

# Set variables
ctx.set("x", 10)
ctx.set("y", 20)

# Use them in JS
result = ctx.eval("x * y")
print(result)  # 200

Calling Python from JavaScript

You can pass Python functions to the JavaScript context.

from quickjs_runtime import Runtime

def add(a, b):
    return a + b

rt = Runtime()
ctx = rt.new_context()
ctx.set("py_add", add)

# Call the Python function from JS
result = ctx.eval("py_add(5, 7)")
print(result)  # 12

Custom Filenames and Stack Traces

You can provide a filename to eval for better error messages.

from quickjs_runtime import Runtime

rt = Runtime()
ctx = rt.new_context()

try:
    ctx.eval("throw new Error('oops')", filename="my_script.js")
except RuntimeError as e:
    print(e)  # error message containing 'my_script.js'

Async/Await Support

Wait for Promises to settle using eval_sync.

from quickjs_runtime import Runtime

rt = Runtime()
ctx = rt.new_context()

script = """
async function fetchData() {
    return "done";
}
fetchData();
"""

# eval_sync will run the job queue until all promises are settled
ctx.eval_sync(script) 

REPL

The package includes a simple REPL (Read-Eval-Print Loop).

python -m quickjs_runtime

Development

This project uses zigcc-build to compile the C extensions. This ensures a consistent build environment without needing to install system-wide C compilers.

To build and install locally:

python -m build
pip install dist/*.whl

To run tests:

pytest

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_runtime-0.2.4.tar.gz (13.0 kB view details)

Uploaded Source

Built Distributions

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

quickjs_runtime-0.2.4-cp312-cp312-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.12Windows x86-64

quickjs_runtime-0.2.4-cp312-cp312-macosx_15_0_arm64.whl (1.7 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

File details

Details for the file quickjs_runtime-0.2.4.tar.gz.

File metadata

  • Download URL: quickjs_runtime-0.2.4.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for quickjs_runtime-0.2.4.tar.gz
Algorithm Hash digest
SHA256 3ba12b60e7b901637f2d5530da795d08714358bac5d5aba7300c0846d5f66e4c
MD5 0c03431dac81e32f0e962cdf7168d161
BLAKE2b-256 868721806dd14b7665b6d7f63cf7bbd6938c86e61b32a61cdd40a79c740f7c89

See more details on using hashes here.

File details

Details for the file quickjs_runtime-0.2.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for quickjs_runtime-0.2.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3e5125a71b0844d53315c290cb90e7202369d92997c963c5dc805ed9e5af8c58
MD5 3cd8cc1fe4cb7a45c3391ec65b597eb0
BLAKE2b-256 882defbabe9f59c80b63b0ff0c44f9b1a0c809c51f96c266c86e45f8ffc76b98

See more details on using hashes here.

File details

Details for the file quickjs_runtime-0.2.4-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_runtime-0.2.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f4097525bcfae89be50f69ed8b7d44ea73a05628caf7da0de9b34857bf043bbf
MD5 ab85f42fe3e6d6d97f82b654dfcb2a93
BLAKE2b-256 c8c45b7b1534c97ab7746cfe0c5cdd65a0532cb9e1534514620491cb53a89fe7

See more details on using hashes here.

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