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

Exporting a host function with a callback as parameter #1587

Closed
jovanivanovic opened this issue Sep 1, 2020 · 4 comments
Closed

Exporting a host function with a callback as parameter #1587

jovanivanovic opened this issue Sep 1, 2020 · 4 comments
Labels
1.0 Wasmer at 1.0 🧞 needs example The issue needs an example on how to do it ❓ question I've a question!

Comments

@jovanivanovic
Copy link

Summary

Hello, I am trying to export a function from the host that has a callback function as a parameter. I would like to somehow call that "guest" function in the host, but I didn't have any luck with it. I've seen a PR that added some features regarding the issue and an example, but at this point the example is gone.

Additional details

This is the function that I am exporting from the host:

fn SubscribeEvent(ctx: &mut Ctx, func: u32)
{
    logi!("SubscribeEvent Start");
    let func_typed = unsafe { std::mem::transmute(func) };
    
    logi!("Trying to call callback function...");
    ctx.call_with_table_index(func_typed, &[]);
}

I am using AssemblyScript to get the wasm file. This is how I use the exported function:

@external('SubscribeEvent')
export declare function subscribeEvent(callback: () => void): void;

subscribeEvent(() => {
    Console.log('Test'); // This is another host-exported function, but this works fine outside of the callback
});

It always fails with this message:

Error when calling invoke: An unknown trap (`segmentation violation`) occured
@jovanivanovic jovanivanovic added the ❓ question I've a question! label Sep 1, 2020
@syrusakbary
Copy link
Member

HI @jovanivanovic, this should be feasible in the refactor work that landed in master a few weeks ago (although you will need a custom environment rather than using Ctx).

@syrusakbary syrusakbary added the 🧞 needs example The issue needs an example on how to do it label Sep 1, 2020
@jovanivanovic
Copy link
Author

Is it possible to get more info on how that custom environment should work or something please?

@MarkMcCaskey MarkMcCaskey added the 1.0 Wasmer at 1.0 label Sep 21, 2020
@webmaster128
Copy link
Contributor

In #1364 and #638 it was discussed how to call an exported funtion from an import implementation. This is currently possible and in the process of getting a nicer API in #1739.

But what you describe here is calling a non-exported Wasm internal function (the callback) from the host, right? Is this something that can work, @syrusakbary?

@Hywan
Copy link
Contributor

Hywan commented Jul 16, 2021

In Wasmer 2.0, it is now possible to declare a host function with, or without, an environment. An environment can hold any kind of data. There is even a WasmerEnv trait to help on that. Please see the documentation of Function.

@Hywan Hywan closed this as completed Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Wasmer at 1.0 🧞 needs example The issue needs an example on how to do it ❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

5 participants