Skip to content

Commit 5ee7a17

Browse files
run the cpython interpreter inside gabagool
1 parent 23740c8 commit 5ee7a17

8 files changed

Lines changed: 99 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ programs/*.wat
88
!programs/fibonacci.wat
99
!programs/add.wat
1010
!programs/stair_climb.wat
11+
examples/python/python.wasm
12+
examples/python/lib/

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
members = ["gabagool", "gabagool-debug-adapter", "gabagool-wasip1"]
3-
exclude = ["programs-wasi"]
3+
exclude = ["programs-wasi", "examples/game-of-life", "examples/python"]
44
resolver = "2"
55

66
[workspace.package]

examples/python/Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/python/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "python"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
gabagool = { path = "../../gabagool" }
8+
gabagool-wasip1 = { path = "../../gabagool-wasip1" }

examples/python/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# python
2+
3+
This demo runs the CPython 3.13 interpreter compiled to wasm inside `gabagool`. It executes Python scripts by loading `python.wasm` with WASI P1 support.
4+
5+
# Usage
6+
7+
```sh
8+
# fetch the interpreter and stdlib
9+
uv run download_python.py
10+
11+
# pass in any python script
12+
cargo r --release hello.py
13+
```

examples/python/download_python.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
import io
3+
import sys
4+
import zipfile
5+
from pathlib import Path
6+
from urllib.request import urlopen
7+
8+
VERSION = "3.13.12"
9+
WASI_SDK = "24"
10+
URL = f"https://github.com/brettcannon/cpython-wasi-build/releases/download/v{VERSION}/python-{VERSION}-wasi_sdk-{WASI_SDK}.zip"
11+
12+
script_dir = Path(__file__).resolve().parent
13+
14+
if (script_dir / "python.wasm").exists() and (script_dir / "lib").exists():
15+
sys.exit(0)
16+
17+
with urlopen(URL) as resp:
18+
data = resp.read()
19+
20+
with zipfile.ZipFile(io.BytesIO(data)) as zf:
21+
zf.extractall(script_dir)

examples/python/hello.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello world")

examples/python/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::env;
2+
use std::fs;
3+
4+
use gabagool::{Module, Store};
5+
use gabagool_wasip1::WasiCtx;
6+
7+
fn run() {
8+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
9+
let script = env::args().nth(1).unwrap_or_else(|| "hello.py".into());
10+
11+
let wasm = fs::read(format!("{manifest_dir}/python.wasm")).unwrap();
12+
let module = Module::new(&wasm).unwrap();
13+
let mut store = Store::new();
14+
15+
let mut wasi = WasiCtx::new()
16+
.with_args(&["python", &script])
17+
.preopen("/", manifest_dir);
18+
19+
let imports = wasi.imports(&mut store, &module);
20+
let instance = store.instantiate(&module, imports).unwrap();
21+
let exit_code = wasi.run(&mut store, instance).unwrap();
22+
23+
if exit_code != 0 {
24+
eprintln!("python exited with code {exit_code}");
25+
}
26+
}
27+
28+
fn main() {
29+
let builder = std::thread::Builder::new().stack_size(64 * 1024 * 1024);
30+
builder.spawn(run).unwrap().join().unwrap();
31+
}

0 commit comments

Comments
 (0)