Skip to main content

Crate waspy

Crate waspy 

Source
Expand description

Waspy: A Python to WebAssembly compiler written in Rust.

Waspy translates a typed subset of Python into a standalone WebAssembly module, allowing Python code to run in browsers and other WebAssembly environments. There is no interpreter or runtime dependency: each supported construct compiles to inline WASM, and the produced module’s exported functions are the compiled Python functions.

§Quick start

use waspy::compile_python_to_wasm;

let source = "def add(a: int, b: int) -> int:\n    return a + b\n";
let wasm: Vec<u8> = compile_python_to_wasm(source)?;
assert_eq!(&wasm[0..4], b"\0asm");

§Public API

The stable surface of this crate is the set of items exported from the crate root:

Comments in the compiled sources are preserved in a python.comments WebAssembly custom section and can be read back with core::comments::comments_from_wasm. Custom sections carry no code, so this never changes how a module runs.

All compile entry points return anyhow::Result<Vec<u8>>; the byte vector is a complete, validated WebAssembly binary. Structured compiler errors (with source locations where available) travel in the error chain as core::errors::ChakraError values.

The pipeline modules (core, ir, compiler, optimize, stdlib, analysis, utils) are exposed for advanced embedding and inspection, but their contents are implementation detail and may change between minor versions; depend on the crate-root exports.

Re-exports§

pub use crate::core::options::CompilerOptions;
pub use crate::core::options::Verbosity;
pub use crate::analysis::metadata::FunctionSignature;

Modules§

analysis
Code analysis modules for Waspy.
compiler
core
Core functionality for the Waspy compiler.
ir
optimize
Optimization modules for Waspy.
stdlib
utils
Utility modules for Waspy.

Macros§

log_debug
Log a debug message (only shown with –debug)
log_error
Log an error message (always shown)
log_info
Log an info message (shown at normal verbosity and above)
log_verbose
Log a verbose message (shown with –verbose or –debug)
log_warn
Log a warning message (always shown unless quiet)

Functions§

compile_multiple_python_files
Compile multiple Python source files into a single WASM binary.
compile_multiple_python_files_with_config
Compile multiple Python source files into a single WASM binary with config awareness.
compile_multiple_python_files_with_options
Compile multiple Python source files with options.
compile_python_file
Compile a Python entry file together with the user-written modules it imports (#41), using default options.
compile_python_file_with_options
Compile a Python entry file together with the user-written modules it imports (#41), with the specified options. See compile_python_file.
compile_python_project
Compile a Python project directory to WebAssembly.
compile_python_project_with_options
Compile a Python project with options.
compile_python_to_wasm
Compile Python source code into a WASM binary using default options.
compile_python_to_wasm_with_options
Compile Python source code into a WASM binary with specified options.
get_python_file_metadata
Get metadata about a Python source file without compiling to WASM. Returns a list of function signatures for documentation or analysis.
get_python_project_metadata
Get metadata about an entire Python project. Returns a list of function signatures for all files.
type_to_string
Render an IR type as the Python type name it came from.