Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ssvm-napi

hydai13Apache-2.00.4.0

SSVM Node.js Addons

readme

SSVM Node.js Addon

In this example, we setup a string hello world WASM demo and demonstrate interaction with the SSVM addon. Since the string type is NOT natively supported by WASM, our SSVM addon could interact with the wasm files generated by wasm-pack and wasm-bindgen.

Setup for Rust utilities

apt-get update
apt install -y apache2 build-essential
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

Create new project

cargo new --lib hello
cd hello

Change the cargo config file

Add the following to the Cargo.toml file.

[lib]
name = "hello"
path = "src/lib.rs"
crate-type =["cdylib"]

[dependencies]
wasm-bindgen = "0.2.50"

Write Rust code

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

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn say(s: String) -> String {
  let r = String::from("hello ");
  return r + &s;
}

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

#[wasm_bindgen]
pub fn reverse(v: Vec<u8>) -> Vec<u8> {
    let mut r = v.clone();
    r.reverse();
    return r;
}

Build the WASM bytecode

wasm-pack build --target nodejs

After building, our target wasm file is located at pkg/hello_bg.wasm.

Setup for SSVM addon

# Dependencies for SSVM-core
apt update && apt install -y \
  libboost-all-dev
npm install -g ssvm-napi@0.4.0 --unsafe-perm

Use SSVM addon

After building SSVM addon, we could now interact with hello_bg.wasm generated by wasm-pack in Node.js. Make sure you use the corresponding vm method to rust return type.

  • Create a new folder to setup execution environment. (e.g. mkdir application)
  • Copy hello_bg.wasm into your application directory. (e.g. cp hello_gb.wasm <path_to_your_application_folder>)
  • Create js file main.js (or whatever you like) with the following content:
var ssvm = require('ssvm-napi');
var vm = new ssvm.VM("hello_bg.wasm")
var ret = vm.RunString("say", "world");
console.log(ret);

ret = vm.RunInt("add", 3, 4);
console.log(ret);

ret = vm.RunUint8Array("reverse", Uint8Array.from([1, 2, 3]));
console.log(ret);
  • Run main.js
node main.js

...ommited...

hello world

...ommited...

7

...ommited...

Uint8Array(3) [ 3, 2, 1 ]

changelog

0.4.0 (2020-01-17)

Features:

Refactor:

  • Host functions:
    • Use template to generate wasm function type of host function body.
    • Move function module name and function name to host function class.

Tools:

  • Sub-project EVM with evmc
    • SSVM-EVMC integrates EVMC and Ethereum Environment Interface(EEI).
    • SSVM-EVMC is a shared library for EVMC-compatible clients.

Tests:

  • ERC20 contracts for SSVM-EVMC
    • Create an example VM for testing.
    • Test the following functionalities of ERC20 contracts:
      • Deploy ERC20 contract
      • Check balance
      • Check total supply
      • Transfer
      • Approve
      • Check allowance

0.3.2 (2020-01-09)

Fixed issues:

  • Handle empty length of memory in vm_snapshot.
  • Correct error message when execution failed in SSVM proxy mode.

0.3.1 (2020-01-07)

Fixed issues:

  • Change the naming style of JSON format in SSVM proxy mode
    • Use snake case for the keys of JSON files instead
  • Change the arguements and return values formats.
    • Add argument_types and return_types in input JSON format.
  • Expand home directory path
    • Accept ~ in the file path

0.3.0 (2019-12-27)

Features:

  • WebAssembly Validation
    • Implement Wasm Validation mechanism. SSVM will validate wasm modules before execution.
  • Snapshot and restore execution state
    • SSVM provides restore mechanism from the previous execution state.
    • SSVM provides snapshot mechanism to dump the current execution state.
  • JSON interface Spec
    • Initialize and set up SSVM via input JSON format.
    • Retrieve execution results via output JSON format.

Tools:

  • Sub-project RPC service proxy mode
    • SSVM-PROXY is a component of SSVMRPC service.
    • SSVM-PROXY can archive current execution states and serialize these data into output JSON format.
    • SSVM-PROXY can restore previous program states from input JSON format.

0.2.0 (2019-12-18)

Features:

  • Native Cost Metering
    • SSVM provides CostTab for each instruction including Wasm, Wasi, Ewasm.
    • With this feature, users can set the cost limit for measuring the execution cost.
  • Built-in performance timer
    • TimeRecord collects execution time for the performance analysis.
    • TimeRecord supports multiple timers.
    • SSVM also provides Wasi timer API for developers to customize TimeRecord.
  • Multiple Virtual Machine Environment
    • Wasm mode: Support general Wasm program.
    • Wasi mode: In addition to Wasm mode, this mode contains basic Wasi functions like print.
    • QITC mode: In addition to Wasi mode, this mode is designed for ONNC runtime to execute AI models by leveraging Qualcomm Hexagon SDK.
    • Ewasm mode: In addition to Wasm mode, this mode is designed for Ethereum flavor WebAssembly.
  • Start functions enhancement
    • Support start function assignment. This makes users invoke an exported function with a given function name.
    • Support start function arguments and return value. This makes users can insert arguments and retrieve result after execution.
  • Simple statistics output
    • Dump total execution time and instruction per second for benchmarking.
    • Print used gas costs for Ewasm mode.
    • Print storage and return values.

Tools:

  • Sub-project Qualcomm Innovate in Taiwan Challenge(a.k.a QITC) 2019
    • SSVM-QITC enables AI model execution by integrating ONNC runtime and Qualcomm Hexagon SDK.
    • With this tool, users can run AI model inference within a WebAssembly Virtual Machine.
  • Sub-project Ethereum
    • SSVM-EVM integrates the Ethereum Environment Interface(EEI) as a WebAssembly extension.
    • With this tool, users can run blockchain applications, which are compiled into Ewasm bytecodes.
  • Sub-project General Wasi Support
    • SSVM tool provides basic Wasi functions support, such as print function.

0.1.0 (2019-11-29)

Features:

  • Lexer: Support full wasm bytecode format
  • AST: Be able to load a wasm module
  • Instantiate: Support wasm module instantiation

Runtime:

  • Support Wasi-core functions
  • Support Ewasm functions

Test:

  • Support ERC20 token contracts