wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Error handling from within guest when calling host function #340

Open clarkmcc opened 2 years ago

clarkmcc commented 2 years ago

Summary

I've worked through the examples which illustrate how to deal with errors created by the guest function, but I need to deal with errors created by host functions. My host is Go and my guest is Rust. The host function looks like this

fn := wasmer.NewFunction(store, wasmer.NewFunctionType(wasmer.NewValueTypes(), wasmer.NewValueTypes()),
    func(values []wasmer.Value) ([]wasmer.Value, error) {
        return nil, fmt.Errorf("error in the host function")
    })
importObject.Register("env", map[string]wasmer.IntoExtern{
    "failable_function": fn,
})

And in my guest I'm doing this

extern "C" {
    pub fn failable_function();
}

#[no_mangle]
pub extern fn this_will_fail() {
    // ...even though I don't have an opportunity to deal with the error
    unsafe{failable_function()}

    // this doesn't work either because apparently its not a panic
    let _ = std::panic::catch_unwind(||{
        unsafe{failable_function()}
    });
}

The problem is that when I call the guest'sthis_will_fail function from the host, which then calls the host's failable_function from the guest, there is no opportunity for the guest to handle the error, it just returns the error right back to the host. Ideally, I'd be able to get some kind of Result wrapper around the function.