Skip to content

Commit

Permalink
chore(define-async-host-func): update to wasmedge-sdk 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 76567ce commit 009e293
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"call-wasm-lib-from-host/wasm-lib",
"call-wasm-lib-with-external-deps/host-app",
"call-wasm-lib-with-external-deps/wasm-lib",
"define-async-host-func",
]


Expand Down
4 changes: 2 additions & 2 deletions define-async-host-func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ name = "define-async-host-func"
version = "0.4.0"

[dependencies]
tokio = { version = "1.28", features = ["full"] }
wasmedge-sdk = { version = "0.13.0", features = ["async"] }
wasmedge-sdk.workspace = true
tokio.workspace = true
6 changes: 4 additions & 2 deletions define-async-host-func/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ Now let's build and run this example.
# NOTICE that the installation script needs `sudo` access

# install wasmedge to the directory /usr/local/
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -v 0.13.5 -p /usr/local
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -v 0.14.0
source $HOME/.wasmedge/env
```

> For users in China mainland (中国大陆地区), try the following command to install WasmEdge Runtime if failed to run the command above
>
> ```bash
> # NOTICE that the installation script needs `sudo` access
>
> bash install_zh.sh -v 0.13.5 -p /usr/local
> bash install_zh.sh -v 0.14.0
> source $HOME/.wasmedge/env
> ```
- Download example
Expand Down
51 changes: 26 additions & 25 deletions define-async-host-func/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
use std::{collections::HashMap, future::Future};

use wasmedge_sdk::{
async_host_function, error::HostFuncError, params, wasi::r#async::AsyncState, Caller,
ImportObjectBuilder, NeverType, VmBuilder, WasmValue,
error::CoreError,
r#async::{import::ImportObjectBuilder, vm::Vm, AsyncInstance},
CallingFrame, Store, WasmValue,
};

#[async_host_function]
async fn async_hello(
_frame: CallingFrame,
fn async_hello(
_data: &mut (),
_inst: &mut AsyncInstance,
_frame: &mut CallingFrame,
_inputs: Vec<WasmValue>,
) -> Result<Vec<WasmValue>, HostFuncError> {
for _ in 0..10 {
println!("[async hello] say hello");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
) -> Box<dyn Future<Output = Result<Vec<WasmValue>, CoreError>> + Send> {
Box::new(async {
for _ in 0..10 {
println!("[async hello] say hello");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}

println!("[async hello] Done!");
println!("[async hello] Done!");

Ok(vec![])
Ok(vec![])
})
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// create an import module
let import = ImportObjectBuilder::new()
.with_async_func::<(), (), NeverType>("hello", async_hello, None)?
.build::<NeverType>("extern", None)?;
let mut builder = ImportObjectBuilder::new("extern", ()).unwrap();
builder.with_func::<(), ()>("hello", async_hello).unwrap();
let mut import = builder.build();

let mut instances = HashMap::new();
instances.insert("extern".into(), &mut import);
let store = Store::new(None, instances).unwrap();
// create a Vm
let mut vm = VmBuilder::new().build()?;

// register the import module
vm.register_import_module(&import)?;

// create an async state
let async_state = AsyncState::new();
let mut vm = Vm::new(store);

async fn tick() {
let mut i = 0;
Expand All @@ -45,9 +48,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::spawn(tick());

// run the async host function
let _ = vm
.run_func_async(&async_state, Some("extern"), "hello", params!())
.await?;
let _ = vm.run_func(Some("extern"), "hello", []).await?;

Ok(())
}

0 comments on commit 009e293

Please sign in to comment.