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:
- Single source:
compile_python_to_wasm,compile_python_to_wasm_with_options - Entry file with user-module imports resolved from disk:
compile_python_file,compile_python_file_with_options - Several sources merged into one module:
compile_multiple_python_files,compile_multiple_python_files_with_options,compile_multiple_python_files_with_config - Project directory:
compile_python_project,compile_python_project_with_options - Metadata without codegen:
get_python_file_metadata,get_python_project_metadata,FunctionSignature - Configuration:
CompilerOptions,Verbosity - Helpers:
type_to_string
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.