Skip to content

Commit

Permalink
chore(say_hello): update to wasmedge-sys v0.14.0
Browse files Browse the repository at this point in the history
Signed-off-by: csh <458761603@qq.com>
  • Loading branch information
L-jasmine committed Jul 22, 2024
1 parent dca6142 commit 8cdf307
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"say_hello",
"async-wasi/run-wasm-app",
"async-wasi/wasm-app",
"async-wasi-socket-addr/main-app",
Expand Down
2 changes: 1 addition & 1 deletion say_hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ name = "say_hello"
version = "0.4.0"

[dependencies]
wasmedge-sdk = "0.13.0"
wasmedge-sdk.workspace = true
26 changes: 16 additions & 10 deletions say_hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
use std::collections::HashMap;

use wasmedge_sdk::{
error::HostFuncError, host_function, params, Caller, ImportObjectBuilder, NeverType, VmBuilder,
WasmValue,
error::CoreError, params, CallingFrame, ImportObjectBuilder, Instance, Store, Vm, WasmValue,
};

#[host_function]
fn hello(_caller: Caller, _args: Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> {
fn hello(
_data: &mut (),
_inst: &mut Instance,
_frame: &mut CallingFrame,
_args: Vec<WasmValue>,
) -> Result<Vec<WasmValue>, CoreError> {
println!("Hello, world!");

Ok(vec![])
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// create a new WasmEdge Vm instance
let mut vm = VmBuilder::new().build()?;

// create an import_object module with the host function
let import = ImportObjectBuilder::new()
.with_func::<(), (), NeverType>("say_hello", hello, None)?
.build::<NeverType>("extern", None)?;
let mut builder = ImportObjectBuilder::new("extern", ())?;
builder.with_func::<(), ()>("say_hello", hello)?;
let mut import = builder.build();

// register the import module into vm
vm.register_import_module(&import)?;
let mut instances = HashMap::new();
instances.insert("extern".into(), &mut import);
let store = Store::new(None, instances)?;
let mut vm = Vm::new(store);

let _ = vm.run_func(Some("extern"), "say_hello", params!())?;

Expand Down

0 comments on commit 8cdf307

Please sign in to comment.