This repository showcases the following:
- How to ompile example assemblyscript to wasm
- it has a function
transform
that takes string and returns didderent string - it has a host defined function called
log
that sends a string to rust side for logging
- it has a function
- How to use WebAssembly runtime (wasmer) to setup and call this module
- call wasm module change with http response from some json web api
- print the output in rust side
Prerequisities:
- nodejs
- nix
- cargo
And then run:
cd transformation
npm install
npm run asbuild # this will create required wasm binaries
cd ..
# run the host side
cargo run
Compiling wasm v0.1.0 (/home/flakm/programming/modivo/wasm)
Finished dev [unoptimized + debuginfo] target(s) in 2.54s
Running `target/debug/wasm`
Compiling module...
Creating the imported function...
[0] my_response: status: 200, body: {"name":"John", "age":30}
[1] my_response: status: 200, body: {"tag":"@John"}
- Only basic testing support there is other project
as-pect
that might be awesome - assemblyscript has to be manually installed and run because it's not packaged in nix
- cargo crane project is super basic example
- better error handling: https://docs.wasmer.io/integrations/examples/errors
- Error handling is painful since we loose a lot of information when code throws error during serialization issue
- Sharing memory is complicated for example if we want to provide host functions to wasm module that will reach back to some memory in host from the guest it will require dereferencing a raw pointer that is leaked (kind of like with FFI) or some kind of hash map backed storage.
-
Wasmer rust - rust example: https://github.com/wasmerio/wasmer-rust-example/blob/master/src/main.rs
wasmer is an open-source runtime for executing WebAssembly on the Server.
-
AssemblyScript A TypeScript-like language for WebAssembly https://www.assemblyscript.org/
wasm-bindgen
rust crate enabling high level interactions between wasm modules allows for similar code:
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
https://www.assemblyscript.org/getting-started.html#setting-up-a-new-project