Skip to content

Commit 0a6bf54

Browse files
committed
Initial commit
0 parents  commit 0a6bf54

15 files changed

Lines changed: 726 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
test:
11+
name: Test Python ${{ matrix.python-version }} on ${{ matrix.os }}
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-latest, windows-latest, macos-latest]
17+
python-version: ["3.13"]
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
with:
22+
submodules: true
23+
24+
- name: Set up Python ${{ matrix.python-version }}
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install pytest
33+
34+
- name: Install project
35+
run: |
36+
pip install .
37+
38+
- name: Run tests
39+
run: |
40+
pytest

‎.gitignore‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
*.pyd
9+
*.dll
10+
*.dylib
11+
*.o
12+
*.obj
13+
14+
# Distribution / packaging
15+
.Python
16+
build/
17+
develop-eggs/
18+
dist/
19+
downloads/
20+
eggs/
21+
.eggs/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
var/
27+
wheels/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# Virtual Environments
35+
.env
36+
.venv
37+
env/
38+
venv/
39+
ENV/
40+
env.bak/
41+
venv.bak/
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# VS Code
59+
.vscode/
60+
61+
# Project specific
62+
test_standalone.exe
63+
test_standalone.pdb

‎.gitmodules‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "quickjs"]
2+
path = quickjs
3+
url = https://github.com/bellard/quickjs

‎README.md‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# python-quickjs-runtime
2+
3+
A lightweight, high-performance Python binding for the [QuickJS](https://bellard.org/quickjs/) Javascript Engine.
4+
5+
This library allows you to execute JavaScript code within your Python applications, providing seamless interoperability between Python and JavaScript.
6+
7+
## Features
8+
9+
* **Evaluate JavaScript**: Run JS code strings or files directly from Python.
10+
* **Data Exchange**: Pass `int`, `float`, `str`, `bool`, and `None` between Python and JS.
11+
* **Python Callbacks**: Call Python functions directly from JavaScript.
12+
* **Console Support**: Built-in `console.log`, `console.error`, etc., mapped to Python's stdout/stderr.
13+
* **REPL**: Includes a simple interactive shell for testing JS snippets.
14+
* **Easy Build**: Uses `zigcc-build` for reliable cross-platform compilation.
15+
16+
## Installation
17+
18+
You can install the package from the source:
19+
20+
```bash
21+
pip install .
22+
```
23+
24+
## Usage
25+
26+
### Basic Evaluation
27+
28+
```python
29+
from quickjs_runtime import Context
30+
31+
ctx = Context()
32+
33+
# Evaluate simple expressions
34+
result = ctx.eval("1 + 2")
35+
print(result) # 3
36+
37+
# Evaluate strings
38+
greeting = ctx.eval("'Hello ' + 'World'")
39+
print(greeting) # "Hello World"
40+
```
41+
42+
### Interoperability
43+
44+
You can set global variables in the JavaScript context and retrieve results.
45+
46+
```python
47+
ctx = Context()
48+
49+
# Set variables
50+
ctx.set("x", 10)
51+
ctx.set("y", 20)
52+
53+
# Use them in JS
54+
result = ctx.eval("x * y")
55+
print(result) # 200
56+
```
57+
58+
### Calling Python from JavaScript
59+
60+
You can pass Python functions to the JavaScript context.
61+
62+
```python
63+
def add(a, b):
64+
return a + b
65+
66+
ctx = Context()
67+
ctx.set("py_add", add)
68+
69+
# Call the Python function from JS
70+
result = ctx.eval("py_add(5, 7)")
71+
print(result) # 12
72+
```
73+
74+
### Evaluating Files
75+
76+
```python
77+
ctx = Context()
78+
# Assuming 'script.js' exists
79+
result = ctx.eval_file("script.js")
80+
```
81+
82+
### Console Output
83+
84+
The `console` object is available in the JS context and redirects to Python's standard output/error.
85+
86+
```python
87+
ctx = Context()
88+
ctx.eval("console.log('This goes to stdout')")
89+
ctx.eval("console.error('This goes to stderr')")
90+
```
91+
92+
## REPL
93+
94+
The package includes a simple REPL (Read-Eval-Print Loop).
95+
96+
```bash
97+
python -m quickjs_runtime
98+
```
99+
100+
## Development
101+
102+
This project uses `zigcc-build` to compile the C extensions. This ensures a consistent build environment without needing to install system-wide C compilers.
103+
104+
To build and install locally:
105+
106+
```bash
107+
python -m build
108+
pip install dist/*.whl
109+
```
110+
111+
To run tests:
112+
113+
```bash
114+
pytest
115+
```

0 commit comments

Comments
 (0)