Disable traps by default in bindgen! imports#8310
Disable traps by default in bindgen! imports#8310alexcrichton merged 1 commit intobytecodealliance:mainfrom
bindgen! imports#8310Conversation
By default previously all return types were wrapped in `wasmtime::Result<T>` to enable any import to return a trap to the wasm guest. This is a fair bit of boilerplate, however, and it's easy to accidentally turn a normal error into a trap. This is additionally somewhat of a power-user method as many imports probably don't end up wanting to trap. This commit adds a new configuration option to `bindgen!`: `trappable_imports`, and switches the default to `false`. The previous behavior can be recovered with `trappable_imports: true`.
Subscribe to Label Actioncc @peterhuene DetailsThis issue or pull request has been labeled: "wasi", "wasmtime:api"Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
|
Hey guys, I'm fairly new to wasm/wasmtime/wasi, can you provide some per function trappable_imports example? [Nevermind, I found some example in this PR.] Also I'd like to ask how should one handle errors in a Host impl? Suppose I have some wit definition like this: resource example {
hello: func() -> result<string, error-type>,
}The host side impl in Rust with trappable-imports is something like this impl HostExample for ExampleContext {
fn hello(&mut self, self_: Resource<Example>) -> wasmtime::Result<Result<String, ErrorType>> {
// should I return a trap when resource table operations failed in here like this?
// When should I return a trap/normal result? My current understanding is if something is
// caused by wasmtime itself (like ResourceTable operation failed here), then I should
// return a trap, else normal result. Is my understanding correct/sane?
let ext: &mut Example = self.table.get_mut(&self_)?;
// let say Example::hello returns Result<String, ErrorType>
Ok(ext.hello())
}
fn drop(&mut self, self_: Resource<Example>) -> wasmtime::Result<()> {
// same here
self.table.delete(self_)?;
Ok(())
}
} |
|
Looks like you might have already found your answer @dearfl but I can try to fill in any possible gaps and/or confirm:
In any case the code you have gisted above looks reasonable to me! |
Thank you for your detailed reply. |
This PR upgrades the version of `wasmtime` and `wasmtime-wasi` in use to v21.0.1. We have to skip v20 because Tree-sitter also skipped it. Here are the changes that had to be made: ### v19 -> v20 After upgrading the `wasmtime` packages to v20, I also had to run `cargo update -p mach2` to pull in [v0.4.2](https://github.com/JohnTitor/mach2/releases/tag/0.4.2) to fix some compile errors. There were a few minor API changes in `wasmtime-wasi` from bytecodealliance/wasmtime#8228 that we needed to account for. ### v20 -> v21 Since there isn't a Tree-sitter version that depends on `wasmtime@v20`, we're jumping straight to v21. The published version of Tree-sitter (v0.22.6) still depends on `wasmtime@v19`, but there was a commit (tree-sitter/tree-sitter@7f4a578) later that month that upgrades the `wasmtime` dependency to v21. We're patching Tree-sitter to that commit so we can get the new `wasmtime` version. The main change in v21 is that imports generated by `bindgen!` are no longer automatically trapped (bytecodealliance/wasmtime#8310), so we need to add `trappable_imports: true` to our `bindgen!` calls. Release Notes: - N/A
This PR upgrades the version of `wasmtime` and `wasmtime-wasi` in use to v21.0.1. We have to skip v20 because Tree-sitter also skipped it. Here are the changes that had to be made: ### v19 -> v20 After upgrading the `wasmtime` packages to v20, I also had to run `cargo update -p mach2` to pull in [v0.4.2](https://github.com/JohnTitor/mach2/releases/tag/0.4.2) to fix some compile errors. There were a few minor API changes in `wasmtime-wasi` from bytecodealliance/wasmtime#8228 that we needed to account for. ### v20 -> v21 Since there isn't a Tree-sitter version that depends on `wasmtime@v20`, we're jumping straight to v21. The published version of Tree-sitter (v0.22.6) still depends on `wasmtime@v19`, but there was a commit (tree-sitter/tree-sitter@7f4a578) later that month that upgrades the `wasmtime` dependency to v21. We're patching Tree-sitter to that commit so we can get the new `wasmtime` version. The main change in v21 is that imports generated by `bindgen!` are no longer automatically trapped (bytecodealliance/wasmtime#8310), so we need to add `trappable_imports: true` to our `bindgen!` calls. Release Notes: - N/A
By default previously all return types were wrapped in
wasmtime::Result<T>to enable any import to return a trap to the wasm guest. This is a fair bit of boilerplate, however, and it's easy to accidentally turn a normal error into a trap. This is additionally somewhat of a power-user method as many imports probably don't end up wanting to trap.This commit adds a new configuration option to
bindgen!:trappable_imports, and switches the default tofalse. The previous behavior can be recovered withtrappable_imports: true.