This isn't clear to me from the documentation, although i believe the answer would be simple. How does one trigger a trap during an import? The imported functions that are provided to the import macro must return simple types (i32, u32, ...) and can't return a Result of either a Value or a Trap.
I would like to basically "throw" a custom error enumeration during import execution which would abort the module, and catch it outside of that context, where i can analyze the thrown error.
I think @MarkMcCaskey might be able to help you with that :)
Good question! This is something that should be documented more clearly, in Rust you can do it by returning a Result from a host function:
You can look at our WASI implementation for an example of this: https://github.com/wasmerio/wasmer/blob/master/lib/wasi/src/syscalls/mod.rs#L2506-L2509
We special cased it so that the failure value of a Result will trap with that value!
Oh, that's cool :D Thanks for the answer!
Can you also show me how you use the proc_exit function in the import!() macro, and how you catch the error later?
In Wasmer 0.16.* it'll be returned through RuntimeError and can be accessed by doing error.0.downcast::<Type>(...) or using other APIs that work on Box<dyn Any>; this is actually changing in the next release of Wasmer and will be stored in RuntimeError::User() (which contains a Box<dyn Any> that you can try to downcast to your type!
The runtime error will be returned at the site where you called into Wasm.
proc_exit should be a normal import, just func!() (https://github.com/wasmerio/wasmer/blob/master/lib/wasi/src/lib.rs#L218) nothing special needed to make this work
Thanks for the help on this issue!
This worked in the project I'm working on, so i think it's safe to close the issue now :)
The API changed a bit preparing to Wasmer 1.0. We will create an example soon
CC: @ethanfrey @webmaster128