The ssvmup npm module installs the Second State Virtual Machine (SSVM) into Node.js as a native addon, and provides the necessary compiler tools. Follow the steps below to install Rust and the ssvmup tool.

# Install ssvmup toolchain

$ npm install -g ssvmup # Append --unsafe-perm if permission denied

​

# Install the nodejs addon for SSVM

$ npm install ssvm

WebAssembly program in Rust

In this example, our Rust program appends the input string after “hello”. Let’s create a new cargo project. Since this program is intended to be called from a host application, not to run as a stand-alone executable, we will create a hello project.

$ cargo new --lib hello

$ cd hello

Edit the Cargo.toml file to add a [lib] section. It tells the compiler where to find the source code for the library and how to generate the bytecode output. We also need to add a dependency of wasm-bindgen here. It is the utility ssvmup uses to generate the JavaScript binding for the Rust WebAssembly program, which is required by the FaaS runtime.

[lib]

name = "hello_lib"

path = "src/lib.rs"

crate-type =["cdylib"]

​

[dependencies]

wasm-bindgen = "=0.2.61"

Below is the content of the Rust program src/lib.rs. You can see that it takes two input parameters. Let's not worry about the context at this moment. The function parameter s comes from the HTTP request when a user calls this function over the web.

use wasm_bindgen::prelude::*;

​

#[wasm_bindgen]

pub fn say(context: &str, s: &str) -> String {

let r = String::from("hello ");

return r + &s;

}

Next, you can compile the Rust source code into WebAssembly bytecode.

$ ssvmup build --nowasi

The result are files in the pkg/ directory. the .wasm file is the WebAssembly bytecode program.

Upload the wasm file to FaaS

Use the following curl command to upload the wasm file to the FaaS service. In the beta stage, it is all FREE!