Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks between Wasm and Native code #670

Closed
tprk77 opened this issue Aug 13, 2019 · 4 comments
Closed

Callbacks between Wasm and Native code #670

tprk77 opened this issue Aug 13, 2019 · 4 comments
Labels
🎉 enhancement New feature!

Comments

@tprk77
Copy link

tprk77 commented Aug 13, 2019

Motivation

I would like to use callbacks between Wasm and native code. I would like to implement a callback on the Wasm-side, pass it as an argument, and later invoke it from the native-side.

I basically want to do what's in this StackOverflow question, but with Wasmer and Rust.

Proposed solution

On the native-side, which is embedding the VM, I want to receive a pointer to a function implemented on the Wasm-side, and somehow lookup and dispatch that function. I would like to be able to do this without any shims or exported functions.

It might look something like this:

// set_callback: (callback: extern fn(x: u32) -> ()) -> ()
pub fn set_callback(ctx: &mut Ctx, callback_ptr: u32) {
  // Somehow lookup callback_ptr in a table?
  ctx.get_func(callback_ptr).call(123);
}

Additional context

I've attempted #245, but I haven't made any progress there. (Help please!)

I've implemented callbacks in my own demo project, but it required some pretty undesirable work-arounds, like a export function shim.

There was also some discussion on Spectrum.

@MarkMcCaskey
Copy link
Contributor

I believe this is the same as or very similar to #638 ;

Thanks for the report -- I agree that this is an important thing to be able to do!

bors bot added a commit that referenced this issue Sep 19, 2019
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 { 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


# 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]>
@MarkMcCaskey
Copy link
Contributor

I got this working in #803 with an example!!

bors bot added a commit that referenced this issue Sep 19, 2019
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]>
@MarkMcCaskey
Copy link
Contributor

Closing because #803 merged in. If you have any issues, don't hesitate to reopen this issue or file a new one!

@tprk77
Copy link
Author

tprk77 commented Sep 25, 2019

This weekend I'll have time to integrate this change into my project. I'll let you know how it goes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature!
Projects
None yet
Development

No branches or pull requests

2 participants