Metadata-Version: 2.4
Name: quickjs-bindings
Version: 0.1.0
Summary: Python bindings for the QuickJS JavaScript engine (quickjs-ng)
Author: quickjs-py contributors
License: MIT License
        
        Copyright (c) 2026 Han Chen
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/slightc/quickjs-py
Project-URL: Repository, https://github.com/slightc/quickjs-py
Keywords: quickjs,javascript,js,engine,bindings
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: C
Classifier: Programming Language :: JavaScript
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Interpreters
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Dynamic: license-file

# quickjs-py

Python bindings for [QuickJS](https://github.com/bellard/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:

```sh
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](https://www.mingw-w64.org/) (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

```python
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

```python
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

```sh
pip install pytest
pytest -q
```

## License

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