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

wasmy-macros

wasmy的宏(轻松自定义我的wasm应用)

12个不稳定版本 (3个重大更改)

0.5.6 2022年3月15日
0.5.5 2022年3月9日
0.4.2 2022年3月3日
0.3.3 2022年2月28日
0.2.2 2022年2月26日

#6 in #customize

Download history 9/week @ 2024-04-20 4/week @ 2024-04-27 3/week @ 2024-05-11 2/week @ 2024-05-18 5/week @ 2024-05-25 4/week @ 2024-06-01 3/week @ 2024-06-08 1/week @ 2024-06-15 4/week @ 2024-06-22 2/week @ 2024-06-29 13/week @ 2024-07-06 6/week @ 2024-07-13 1/week @ 2024-07-20 81/week @ 2024-07-27 2/week @ 2024-08-03

每月 90 次下载
4 个crate中使用 2 个直接使用

Apache-2.0

10KB
161

wasmy WasmGroup-QQ42726807

wasmy,轻松自定义我的wasm应用!

特性

  • 完全屏蔽vm-wasm交互细节
  • 简单灵活的ABI,支持通过属性宏(#[vm_handle(0)] /#[wasm_handle(0)])自由添加vm和wasm处理器
  • 提供属性宏 #[wasm_onload] 支持初始化wasm
  • 支持多线程并发
  • 提供上下文,分层友好
  • 具有安全沙盒功能
  • 使用protobuf作为交互协议
  • 支持自定义ABI

crate

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

示例

  • wasm代码(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代码
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 ...
}

测试简单示例

  • 原始cargo命令
$ rustup target add wasm32-wasi

$ cargo +nightly build --target=wasm32-wasi --example=simple
$ cargo +nightly run --example=svm -- ../../wasm32-wasi/debug/examples/simple.wasm
  • 别名cargo命令
$ rustup target add wasm32-wasi

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

依赖

~1.5MB
~36K SLoC