$ 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
cargo new file-example
cd file-example
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");
}
cargo build --release --target wasm32-wasi
After building, our target wasm file is located at target/wasm32-wasi/release/file-example.wasm
.
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
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
andlib.js
in the root folder.
├── 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
const { file_demo } = require('./lib.js');
file_demo();
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}});
$ 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
============================================