fix: add wasmtime flags! macro support for WIT flags types#1327
fix: add wasmtime flags! macro support for WIT flags types#1327jsturtevant wants to merge 1 commit into
Conversation
|
moved this to draft, I was integrating into hyperlight-wasm and found the usage of flags needs to be slightly different. than this. will push an update shortly |
f0c8e64 to
b68803b
Compare
b319154 to
564c59e
Compare
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
564c59e to
6e6617d
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates the Hyperlight component Rust code generator to correctly support WIT flags types when generating Wasmtime guest bindings, by emitting wasmtime::component::flags! (and updating marshal/unmarshal codegen accordingly) so the generated types satisfy Wasmtime’s Lift/Lower trait bounds. Non-Wasmtime generation paths remain unchanged.
Changes:
- Emit
::wasmtime::component::flags! { ... }for WITflagstypes whenis_wasmtime_guestis enabled (instead of apub structwithboolfields). - Update Hyperlight marshal/unmarshal codegen for Wasmtime guests to use the Wasmtime flags API (
empty(),|=,.contains(...)) rather than field access. - Add an integration test validating the Wasmtime flags macro emission and wire it into
just test-integration.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/hyperlight_component_util/tests/wasmtime_guest_codegen.rs | Adds an integration test to validate Wasmtime guest codegen emits wasmtime::component::flags! for WIT flags. |
| src/hyperlight_component_util/src/rtypes.rs | Switches Wasmtime-guest flags type emission from bool-field structs to the Wasmtime flags! macro, preserving original WIT names. |
| src/hyperlight_component_util/src/hl.rs | Updates marshal/unmarshal codegen for flags in the Wasmtime-guest path to use flags operations (empty, ` |
| src/hyperlight_component_util/src/emit.rs | Introduces kebab_to_flags_const helper for generating SCREAMING_SNAKE_CASE flag constant identifiers. |
| Justfile | Ensures test-integration runs after generating WIT-derived .wasm inputs and runs the new component-util integration test. |
| assert!(generated.contains("::wasmtime::component::flags! {")); | ||
| assert!(generated.contains("Smallflags {")); | ||
| assert!(generated.contains("\"flag-a\"")); | ||
| assert!(generated.contains("const FLAG_A;")); | ||
| assert!(generated.contains("\"flag-b\"")); | ||
| assert!(generated.contains("const FLAG_B;")); | ||
| assert!(generated.contains("\"flag-c\"")); | ||
| assert!(generated.contains("const FLAG_C;")); | ||
| assert!(!generated.contains("pub flag_a: bool")); | ||
| assert!(!generated.contains("pub flag_b: bool")); | ||
| assert!(!generated.contains("pub flag_c: bool")); |
There was a problem hiding this comment.
eh, I considered this but feels not quite right. There isn't really a great way to test this e2e in this repo since it really requires a whole wasmtime guest. What do others think?
When
is_wasmtime_guestis true, emitwasmtime::component::flags!macro invocations instead of plain structs with bool fields. This enables WIT flags types (e.g. fromwasi:filesystem) to satisfy thewasmtime::component::Liftandwasmtime::component::Lowertrait bounds. See https://docs.rs/wasmtime/latest/wasmtime/component/macro.flags.htmlThe non-Wasmtime code path used by
host_bindgen!and Hyperlightguest_bindgen!in this repo is unchanged. The Wasmtime path is used by downstream Wasmtime guest-bindgen consumers.WIT flags input
The tests use the existing WIT flags in
src/tests/rust_guests/witguest/guest.wit:Expected non-Wasmtime Rust output
host_bindgen!and Hyperlightguest_bindgen!keep generating a bool-field struct:Expected Wasmtime guest Rust output
The Wasmtime guest generator emits the real Wasmtime flags macro and preserves the original WIT names on each constant:
Expected marshal/unmarshal output
Because the type produced by
wasmtime::component::flags!is bitflag-style, the Hyperlight-generated Wasmtime guest marshal/unmarshal code cannot access fields likevalue.flag_a.For non-Wasmtime bindings, Hyperlight still generates code that reads and writes bool fields:
For Wasmtime guest bindings, Hyperlight now generates code that uses the Wasmtime flags API:
Closes #1318
Signed-off-by: James Sturtevant jsturtevant@gmail.com