Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Latest commit

 

History

History
183 lines (145 loc) · 4.97 KB

Tutorial_Wasm32_Wasi.md

File metadata and controls

183 lines (145 loc) · 4.97 KB

How to run wasm applications with ssvm-napi (General Wasm32-wasi with interpreter mode)

Environment Setup for Rust, Nodejs, and ssvmup

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt install build-essential curl wget git vim libboost-all-dev

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm install v14.2.0
$ nvm use v14.2.0

$ npm i -g ssvmup

Example 1. Print environment variables, arguments, and test filesystem

Create a new rust project

cargo new file-example
cd file-example

Write Rust code

Below is the entire content of the src/main.rs file.

use std::env;
use std::fs;
use std::fs::File;
use std::io::{Write, Read};

fn main() {
    println!("This is a demo application to show how to run a standalone wasi program with ssvm-napi!");
    println!("============================================");
    println!("Print environment variables");
    println!("--------------------------------------------");
    println!("The env vars are as follows.");
    for (key, value) in env::vars() {
        println!("{}: {}", key, value);
    }
    println!("============================================\n");
    println!("Print arguments");
    println!("--------------------------------------------");
    println!("The args are as follows.");
    for argument in env::args() {
        println!("{}", argument);
    }
    println!("============================================\n");
    println!("Test filesystem, create a /hello.txt, read and write to it, and then delete it");
    println!("--------------------------------------------");
    let path = "/hello.txt".to_string();
    let content = "Hello from SSVM\nThis file is located at wasm binary folder".to_string();
    let mut output = File::create(&path).unwrap();
    output.write_all(&content.as_bytes()).unwrap();
    let mut f = File::open(&path).unwrap();
    let mut s = String::new();
    let ret = match f.read_to_string(&mut s) {
        Ok(_) => s,
        Err(e) => e.to_string(),
    };
    println!("Output: {}", ret);
    fs::remove_file(&path).expect("Unable to delete");
    println!("============================================\n");
}

Build the WASM bytecode with cargo wasm32-wasi backend

cargo build --release --target wasm32-wasi

After building, our target wasm file is located at target/wasm32-wasi/release/file-example.wasm.

Install SSVM addon for your application

npm install ssvm

or if you want to build from source:

export CXX=g++-9
npm install --build-from-source https://github.com/second-state/ssvm-napi

Use SSVM addon

After installing the SSVM addon, we could now interact with file_example.wasm generated by wasm32-wasi backend in Node.js.

  • Create js file app.js and lib.js in the root folder.

Folder structure

├── Cargo.lock
├── Cargo.toml
├── README.md
├── app.js
├── lib.js
├── node_modules
│   └── ssvm
├── package-lock.json
├── src
│   └── main.rs
└── target
    ├── release
    │   ├── ...omitted...
    │   └── incremental
    └── wasm32-wasi
        └── release
            ├── ...omitted...
            ├── file-example.d
            ├── file-example.wasm
            └── incremental

The entire content of app.js:

const { file_demo } = require('./lib.js');

file_demo();

The entire content of lib.js:

let vm;
module.exports.file_demo = function() {
        return vm.Start();
};

const ssvm = require('ssvm');
const path = require('path').join(__dirname, 'target/wasm32-wasi/release/file-example.wasm');

vm = new ssvm.VM(path, {"EnableWasiStartFunction": true, env: process.env, args: process.argv, preopens:{'/': __dirname}});

Execute and check results

$ node app.js arg1 arg2

This is a demo application to show how to run a standalone wasi program with ssvm-napi!
============================================
Print environment variables
--------------------------------------------
The env vars are as follows.
LANG: C.UTF-8
(...omitted...)
PATH: /bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin
PWD: /home/hydai/workspace/wasm-learning/ssvm/file-example
_: /home/hydai/.nvm/versions/node/v14.5.0/bin/node
============================================

Print arguments
--------------------------------------------
The args are as follows.
_start
arg1
arg2
============================================

Test filesystem, create a /hello.txt, read and write to it, and then delete it
--------------------------------------------
Output: Hello from SSVM
This file is located at wasm binary folder
============================================