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

Passing fn as parameter in host or guest #1302

Closed
Alphapage opened this issue Mar 14, 2020 · 1 comment
Closed

Passing fn as parameter in host or guest #1302

Alphapage opened this issue Mar 14, 2020 · 1 comment
Labels
❓ question I've a question!

Comments

@Alphapage
Copy link

Hello,

I'd like to know if it is possible to do something like this in host or guest:

extern fn afunction(

    param1: fn(data1: Option<Vec<u8>>, data2: Option<Vec<u8>>),

    param2: Vec<u8>

) -> Option<Vec<u8>> {

    param1(mydata1,mydata2);
...
}

Thank you in advance for your help.

@Alphapage Alphapage added the ❓ question I've a question! label Mar 14, 2020
@MarkMcCaskey
Copy link
Contributor

Yes, it is possible to call guest functions from the host with Ctx::call_with_table_index. When compiling to Wasm, Rust will instead insert the function into a table and pass a table index instead of a function pointer, so using it as a table index with that method on Ctx should work!

The guest cannot call functions on the host currently, other than through imports. If you wanted something more dynamic, you could do the equivalent of inserting function pointers into data on the Ctx and passing table indices into the guest and having a dedicated import from the host that allows invoking functions.

Let me know if you run into any issues with it and I'll help you fix it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants