wasm-encoder: add method to allow fetching raw Function bytes. #1630
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When generating function bodies with
wasm-encoder
, it is sometimes useful to take the function bytecode, cache it, then reuse it later. (For my specific use-case, in weval, I would like to cache weval-specialized function bodies and reuse them when creating new modules.)Unfortunately the existing API of
wasm-encoder
makes this almost but not quite easy:Function
implementsEncode::encode
, but this will produce a function body with the length prefixed. However there's no way to use this unmodified with the next level up the entity hierarchy:CodeSection::raw
wants the function bytes without the length prefixed.This is a fairly annoying if small API gap, and otherwise requires manually stripping the length prefix, a leb128-encoded integer. It's also a small footgun: I naively did not realize this mismatch and tried to do the above, only to get perplexing type errors with locals.
This PR adds one method on
wasm_encoder::Function
to return the inner bytes directly, and the doc-comment contains an example showing the intended use-case.