Skip to content
anistarkPublic

About

A Python to WebAssembly compiler written in Rust.

Topics

Resources

Contributing

Stars

27 stars

Watchers

3 watching

Forks

Repository files navigation

Waspy

A Python to WebAssembly compiler written in Rust.

Crates.io Version Crates.io Downloads Crates.io Downloads (latest version) Open Source License: MIT Contributors maintenance-status

waspy

Waspy compiles a typed subset of Python ahead of time into a standalone WebAssembly module, with no interpreter or VM in the output. A construct either gives CPython's answer or is refused at compile time with a located error and a hint.

Try it without installing anything in the playground.

Installation

cargo add waspy

Or add it to your Cargo.toml:

[dependencies]
waspy = "0.18.0"

The default optimize feature runs Binaryen, a native C++ library, over the output. To build waspy for a target without a C++ toolchain, such as wasm32-unknown-unknown, see Building without Binaryen.

Quick Start

use waspy::compile_python_to_wasm;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let python_code = r#"
def factorial(n: int) -> int:
    result = 1
    i = 1
    while i <= n:
        result = result * i
        i = i + 1
    return result
"#;

    let wasm = compile_python_to_wasm(python_code)?;
    std::fs::write("factorial.wasm", &wasm)?;
    Ok(())
}

Every module-level function is exported under its own name:

// Browser or Node.js
const { instance } = await WebAssembly.instantiate(wasmBuffer);
console.log(instance.exports.factorial(5)); // 120

A program that prints, reads input, or opens files does so through WASI, so it runs under wasmtime run, Node's node:wasi, or a browser shim. You can also run a Python project directly with wasmrun, which uses waspy as its Python plugin.

Documentation

Contributing

Contributions are welcome! See CONTRIBUTING.md for building, running the examples, and testing. If you find a program that compiles and answers differently from CPython, please open an issue.

waspy

License

MIT

About

A Python to WebAssembly compiler written in Rust.

Topics

Resources

Contributing

Stars

27 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages