-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
803: Add method to call function at index on Ctx r=MarkMcCaskey a=MarkMcCaskey For #638 and #670 ```Rust fn call_guest_fn(ctx: &mut Ctx, guest_fn: u32) -> u32 { println!("{}", guest_fn); let guest_fn_typed = unsafe { std::mem::transmute(guest_fn) }; let result = ctx.call_with_table_index(guest_fn_typed, &[]).unwrap(); println!(" -> {:?}", result); 0 } ``` is what this looks like from the Host side See `examples/callback.rs` for an example that doesn't require `transmute` # Review - [x] Create a short description of the the change in the CHANGELOG.md file Co-authored-by: Mark McCaskey <[email protected]> Co-authored-by: Mark McCaskey <[email protected]>
- Loading branch information
Showing
12 changed files
with
165 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Call back guest | ||
|
||
This is part of the `callback` example. This Wasm module passes host imports and its own functions to the Wasm host to execute. | ||
|
||
See `examples/callback.rs` for the host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern "C" { | ||
fn call_guest_fn(f: u32) -> u32; | ||
fn call_guest_fn2(f: u32) -> u32; | ||
fn host_callback() -> u32; | ||
} | ||
|
||
#[no_mangle] | ||
fn test_callback() -> u32 { | ||
42 | ||
} | ||
|
||
#[no_mangle] | ||
fn test_callback2() -> u32 { | ||
45 | ||
} | ||
|
||
fn main() { | ||
unsafe { call_guest_fn(test_callback as usize as u32) }; | ||
unsafe { call_guest_fn(host_callback as usize as u32) }; | ||
unsafe { call_guest_fn(test_callback2 as usize as u32) }; | ||
unsafe { call_guest_fn2(test_callback2 as usize as u32) }; | ||
unsafe { call_guest_fn2(test_callback as usize as u32) }; | ||
unsafe { call_guest_fn2(host_callback as usize as u32) }; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/// This example demonstrates the use of callbacks: calling functions (Host and Wasm) | ||
/// passed to us from the Wasm via hostcall | ||
use wasmer_runtime::{compile_with, compiler_for_backend, func, imports, Backend, Ctx}; | ||
use wasmer_runtime_core::{structures::TypedIndex, types::TableIndex}; | ||
|
||
static WASM: &'static str = "examples/callback-guest/callback-guest.wasm"; | ||
|
||
/// This function matches our arbitrarily decided callback signature | ||
/// in this example we'll only call functions that take no arguments and return one value | ||
fn host_callback(_ctx: &mut Ctx) -> u32 { | ||
55 | ||
} | ||
|
||
fn call_guest_fn(ctx: &mut Ctx, guest_fn: u32) -> u32 { | ||
// We get a TableIndex from our raw value passed in | ||
let guest_fn_typed = TableIndex::new(guest_fn as usize); | ||
// and use it to call the corresponding function | ||
let result = ctx.call_with_table_index(guest_fn_typed, &[]).unwrap(); | ||
|
||
println!("Guest fn {} returned {:?}", guest_fn, result); | ||
|
||
0 | ||
} | ||
|
||
fn main() { | ||
let wasm_bytes = | ||
std::fs::read(WASM).expect(&format!("Could not read in WASM plugin at {}", WASM)); | ||
|
||
let imports = imports! { | ||
"env" => { | ||
"call_guest_fn" => func!(call_guest_fn), | ||
"call_guest_fn2" => func!(call_guest_fn), | ||
"host_callback" => func!(host_callback), | ||
}, | ||
}; | ||
|
||
let compiler = compiler_for_backend(Backend::default()).unwrap(); | ||
let module = compile_with(&wasm_bytes[..], compiler.as_ref()).unwrap(); | ||
let instance = module | ||
.instantiate(&imports) | ||
.expect("failed to instantiate wasm module"); | ||
|
||
let entry_point = instance.func::<(u32, u32), u32>("main").unwrap(); | ||
|
||
entry_point.call(0, 0).expect("START"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters