Crates.io | wasmy-vm |
lib.rs | wasmy-vm |
version | 0.5.6 |
source | src |
created_at | 2022-02-26 11:55:43.500589 |
updated_at | 2022-03-15 12:09:41.183179 |
description | virtual machine of wasmy (easily customize my wasm app) |
homepage | |
repository | https://github.com/henrylee2cn/wasmy |
max_upload_size | |
id | 539912 |
size | 29,679 |
wasmy, easily customize my wasm app!
#[vm_handle(0)]
/#[wasm_handle(0)]
)#[wasm_onload]
support to initialize wasm[dependencies]
wasmy-vm = "0.5"
[dependencies]
wasmy-abi = "0.5"
wasmy-macros = "0.5"
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)
}
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 ...
}
$ rustup target add wasm32-wasi
$ cargo +nightly build --target=wasm32-wasi --example=simple
$ cargo +nightly run --example=svm -- ../../wasm32-wasi/debug/examples/simple.wasm
$ rustup target add wasm32-wasi
$ cargo +nightly wasm simple
$ cargo +nightly svm simple