alexcrichton opened PR #11328 from alexcrichton:refactor-bindgen to bytecodealliance:main:
This commit is a redesign of how function-level configuration works in
Wasmtime'sbindgen!macro. The main goal of this redesign is to
better support WASIp3 and component model async functions. Prior to this
redesign there was a mish mash of mechanisms to configure behavior of
imports/exports:
The
asyncconfiguration could turn everything async, nothing async,
only some imports async, or everything except some imports async.The
concurrent_{imports,exports}keys were required to explicitly
opt-in to component model async signatures and applied to all
imports/exports.The
trappable_importsconfiguration would indicate a list of imports
allowed to trap and it had special configuration for everything,
nothing, and only a certain list.The
tracingandverbose_tracingkeys could be applied to either
nothing or all functions.Overall the previous state of configuration in
bindgen!was clearly a
hodgepodge of systems that organically grew over time. In my personal
opinion it was in dire need of a refresh to take into account how
component-model-async ended up being implemented as well as
consolidating the one-off systems amongst all of these configuration
keys. A major motivation of this redesign, for example, was to inherit
behavior from WIT files by default. Anasyncfunction in WIT should
not requireconcurrent_*keys to be configured, but rather it should
generate correct bindings by default.In this commit, all of the above keys were removed. All keys have been
replaced withimportsandexportsconfiguration keys. Each behaves
the same way and looks like so:bindgen!({ // ... imports: { // enable tracing for just this function "my:local/interface/func": tracing, // enable verbose tracing for just this function "my:local/interface/other-func": tracing | verbose_tracing, // this is blocking in WIT, but generate async bindings for // it "my:local/interface/[method]io.block": async, // like above, but use "concurrent" bindings which have // access to the store. "my:local/interface/[method]io.block-again": async | store, // everything else is, by default, trappable default: trappable, }, });Effectively all the function-level configuration items are now bitflags.
These bitflags are by default inherited from the WIT files itself (e.g.
asyncfunctions areasync | storeby default). Further configuration
is then layered on top at the desires of the embedder. Supported keys are:
async- this means that a Rust-levelasyncfunction should be
generated. This is eitherCallStyle::Asyncor
CallStyle::Concurrentas it was prior, depending on ...
store- this means that the generated function will have access to
the store on the host. This is only implemented right now forasync | storefunctions which map toCallStyle::Concurrent. In the future
I'd like to support just-storefunctions which means that you could
define a synchronous function with access to the store in addition to
an asynchronous function.
trappable- this means that the function returns a
wasmtime::Result<TheWitBindingType>. Iftrappable_errorsis
applicable then it means just aResult<TheWitOkType, TrappableErrorType>is returned (like before)
tracing- this enablestracing!integration for this function.
verbose_tracing- this logs all argument values for this function
(including lists).
ignore_wit- this ignores the WIT-level defaults of the function
(e.g. ignoring WITasync).The way this then works is all modeled is that for any WIT function
being generated there are a set of flags associated with that function.
To calculate the flags the algorithm looks like:
Find the first matching rule in the
importsorexportsmap
depending on if the function is imported or exported. If there is no
matching rule then use thedefaultrule if present. This is the
initial set of flags for the function (or empty if nothing was
found).If
ignore_witis present, return the flags from step 1. Otherwise
add inasync | storeif the function isasyncin WIT.The resulting set of flags are then used to control how everything is
generated. For example the same split traits of today are still
generated and it's controlled based on the flags. Note though that the
previousHostConcurrenttrait was renamed toHostWithStoreto make
space for synchronous functions in this trait in the future too.The end result of all these changes is that configuring imports/exports
now uses the exact same selection system as thewithreplacement map,
meaning there's only one system of selecting functions instead of 3.
WIT-levelasyncis now respected by default meaning that bindings work
by default without further need to configure anything (unless more
functionality is desired).One final minor change made here as well is that auto-generated
instantiatemethods are now always synchronous and an
instantiate_asyncmethod is unconditionally generated for async mode.
This means that bindings always generate both functions and it's up to
the embedder to choose the appropriate one.Closes https://github.com/bytecodealliance/wasmtime/issues/11246
Closes https://github.com/bytecodealliance/wasmtime/issues/11247
alexcrichton requested wasmtime-wasi-reviewers for a review on PR #11328.
alexcrichton requested pchickey for a review on PR #11328.
alexcrichton requested wasmtime-core-reviewers for a review on PR #11328.
alexcrichton requested wasmtime-default-reviewers for a review on PR #11328.
alexcrichton updated PR #11328.
alexcrichton commented on PR #11328:
One other large-ish change now here,
*WithStoreis always generated no matter what. That'll make the generated docs slightly more confusing but I think it's worth the tradeoff of enabling bindings to always assume the trait is there.
pchickey submitted PR review:
Thanks, this is a nice improvement!
pchickey submitted PR review:
Thanks, this is a nice improvement! In the future I'd probably like it to be
tracingortracing(verbose)instead of another keyword for verbose_tracing, but its not necessary to do that now.
dicej submitted PR review.
dicej created PR review comment:
Why is this one commented out?
dicej created PR review comment:
Did you intend to keep this comment-ed out code fragment?
dicej created PR review comment:
// it's otherwise not necessary to implement it manually.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Oops, meant to come back and dig in, but it's just unused so needed to be deleted.
alexcrichton updated PR #11328.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Oops nah just a forgotten deletion.
alexcrichton has enabled auto merge for PR #11328.
alexcrichton merged PR #11328.
Last updated: Dec 06 2025 at 06:05 UTC