RustPython With Wasm

Published

RustPython is a python interpreter written in rust. What makes it interesting is that it does no use cpython which makes it much more straightforward to target WebAssembly and do things like execute untrusted code on a host machine.

Getting started

# Clone the repo
git clone git@github.com:RustPython/RustPython.git && cd RustPython

# Add webassembly build target
rustup target add wasm32-wasip1

# Build it
cargo build --target wasm32-wasip1 --features freeze-stdlib,stdlib --release

# Run it with wasmer
curl https://get.wasmer.io -sSfL | sh
wasmer run --dir `pwd` -- target/wasm32-wasip1/release/rustpython.wasm `pwd`/extra_tests/snippets/stdlib_random.py

Adding libraries

rustpython --install-pip
rustpython -m pip install requests
rustpython -c 'import requests; print(requests.get("https://example.com").text)'

Notes

  • How does one add libraries when using wasm? Where do the libraries go?
  • Stdout doesn’t work when running wasmer run ./target/wasm32-wasip1/release/rustpython.wasm. I had to remove the --no-default-features flag when building
  • Wasmer doesn’t install correctly when using cargo install wasmer-cli but it installed fine with the sweat inducing curl piped to sh