#wasm-app #wasmy #macro #customize #vm #abi

macro wasmy-macros

macros of wasmy (easily customize my wasm app)

12 unstable releases (3 breaking)

0.5.6 Mar 15, 2022
0.5.5 Mar 9, 2022
0.4.2 Mar 3, 2022
0.3.3 Feb 28, 2022
0.2.2 Feb 26, 2022

#7 in #customize


Used in 4 crates (2 directly)

Apache-2.0

10KB
161 lines

wasmy WasmGroup-QQ42726807

wasmy, easily customize my wasm app!

features

  • Completely shield vm-wasm interaction details
  • Simple and flexible ABI, supports freely adding vm and wasm handlers using attribute macros (#[vm_handle(0)] /#[wasm_handle(0)])
  • Provide attribute macro #[wasm_onload] support to initialize wasm
  • Support multi-threaded concurrency
  • Provides context, layering friendly
  • Features a security sandbox
  • Use protobuf as the interaction protocol
  • Support custom ABI

crates

[dependencies]
wasmy-vm = "0.5"
[dependencies]
wasmy-abi = "0.5"
wasmy-macros = "0.5"

example

  • wasm code (target = "wasm32-wasi")
use rand::random;
use wasmy_abi::*;
use wasmy_abi::test::*;

#[wasm_handle(0)]
fn multiply(ctx: WasmCtx, args: TestArgs) -> Result<TestRets> {
    let rid = random::<u8>() as i32;
    println!("[Wasm-Simple({})] handle wasm method({}) ctx={:?}, args={{{:?}}}", rid, 0, ctx, args);

    let mut vm_args = TestArgs::new();
    vm_args.a = rid;
    vm_args.b = rid;
    let vm_rets: TestRets = ctx.call_vm(0, vm_args)?;
    println!("[Wasm-Simple({})] call vm method({}): args={{{:?}}}, rets={}", rid, 0, vm_rets, vm_rets.get_c());

    let mut rets = TestRets::new();
    rets.set_c(args.a * args.b);
    Ok(rets)
}
  • vm code
use wasmy_vm::*;
use crate::test::{TestArgs, TestRets};

...

fn main() {
    link_mod();
    println!("wasmy, easily customize my wasm app!");
    ...
    let wasm_caller = load_wasm(wasm_path).unwrap();
    let mut data = TestArgs::new();
    data.set_a(2);
    data.set_b(5);
    for i in 1..=3 {
        let res: TestRets = wasm_caller.call(0, data.clone()).unwrap();
        println!("NO.{}: {}+{}={}", i, data.get_a(), data.get_b(), res.get_c())
    }
}

// Make sure the mod is linked
fn link_mod() {
    #[vm_handle(0)]
    fn add(args: TestArgs) -> Result<TestRets> {
        let mut rets = TestRets::new();
        rets.set_c(args.a + args.b);
        Ok(rets)
    }
    // more #[vm_handle(i32)] fn ...
}

test simple example

  • raw cargo cmd:
$ rustup target add wasm32-wasi

$ cargo +nightly build --target=wasm32-wasi --example=simple
$ cargo +nightly run --example=svm -- ../../wasm32-wasi/debug/examples/simple.wasm
  • alias cargo cmd:
$ rustup target add wasm32-wasi

$ cargo +nightly wasm simple
$ cargo +nightly svm simple

Dependencies

~1.5MB
~36K SLoC