"Hello world!" Rust Wasmer library
🟪 A Rust library compiled to WASM for use with Wasmer
console.log(hello_world_rust_wasmer_lib.add(1, 2));
//=> 3
console.log(hello_world_rust_wasmer_lib.greet("Alan Turing"));
//=> 'Hello Alan Turing!'
const calc = Calculator.new(hello_world_rust_wasmer_lib, 15);
calc.add(5);
console.log(calc.currentValue());
//=> 20
print(hello_world_rust_wasmer_lib.add(1, 2))
#=> 3
print(hello_world_rust_wasmer_lib.greet("Alan Turing"))
#=> 'Hello Alan Turing!'
calc = Calculator.new(hello_world_rust_wasmer_lib, 15)
calc.add(5)
print(calc.current_value())
#=> 20
🚀 Quickstart template for Wasmer-based WASM libraries
🦀 Written in Rust
📖 Follows the Wasmer Pack tutorial series
📥 Imports some host functions
🟨 Can be used with JavaScript
🐍 Can be used with Python
👩⚖️ 0BSD licensed template
Installation
You can install this package using npm through Wasmer's npm redistribution CDN using the Wasmer CLI.
wasmer add jcbhmr/hello-world-rust-wasmer-lib --npm
You can also use --yarn or --pnpm to install the package using Yarn or
pnpm respectively. See wasmer add --help for more info.
⚠️ wasmer add doesn't support targeting browser runtimes. It uses
node:fs/promises readFile().
If you're using Python you can install it using pip:
wasmer add jcbhmr/hello-world-rust-wasmer-lib --pip
Usage
Imports (in order): fib, io
Exports: hello-world-rust-wasmer-lib
Here's an example of how you would import and use this package using Node.js:
import { bindings } from "@jcbhmr/hello-world-rust-wasmer-lib";
import { Calculator } from "@jcbhmr/hello-world-rust-wasmer-lib/src/bindings/hello_world_rust_wasmer_lib/hello_world_rust_wasmer_lib.js";
import * as io from "./io.js";
import * as fib from "./fib.js";
const hello_world_rust_wasmer_lib = await bindings.hello_world_rust_wasmer_lib(
fib,
io,
);
console.log(hello_world_rust_wasmer_lib.add(1, 2));
//=> 3
console.log(hello_world_rust_wasmer_lib.greet("Alan Turing"));
//=> 'Hello Alan Turing!'
const calc = Calculator.new(hello_world_rust_wasmer_lib, 15);
calc.add(5);
console.log(calc.currentValue());
//=> 20
ℹ To use the Calculator class you need to construct it using
Calculator.new(), not new Calculator(). Note that we also need to pass
it the exports bindings context.
To use this package with Python this is what you would do:
from hello_world_rust_wasmer_lib import bindings
from hello_world_rust_wasmer_lib.bindings.hello_world_rust_wasmer_lib import Calculator
import .fib
import .io
hello_world_rust_wasmer_lib = bindings.hello_world_rust_wasmer_lib(fib, io)
print(hello_world_rust_wasmer_lib.add(1, 2))
#=> 3
print(hello_world_rust_wasmer_lib.greet("Alan Turing"))
#=> 'Hello Alan Turing!'
calc = Calculator.new(hello_world_rust_wasmer_lib, 15)
calc.add(5)
print(calc.current_value())
#=> 20
Development
You can make sure everything looks good like this:
cargo wasmer --dry-run --debug --out-dir=./out
mv ./out/wasmer.toml ./out/wapm.toml
wasmer-pack js ./out --out-dir=./out/js
wasmer-pack py ./out --out-dir=./out/py
You can get the wasmer-pack CLI via cargo install wasmer-pack-cli. You can
install cargo-wasmer via cargo install cargo-wasmer.