wasmy-macros

Crates.iowasmy-macros
lib.rswasmy-macros
version0.5.6
sourcesrc
created_at2022-02-26 11:50:43.978963
updated_at2022-03-15 12:06:26.282976
descriptionmacros of wasmy (easily customize my wasm app)
homepage
repositoryhttps://github.com/henrylee2cn/wasmy
max_upload_size
id539908
size9,494
Andeya (andeya)

documentation

README

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
Commit count: 90

cargo fmt