Commit 62cf274
authored
wasm-encoder: add method to allow fetching raw Function bytes. (#1630)
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` implements `Encode::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.1 parent 3e38098 commit 62cf274
1 file changed
Lines changed: 58 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
277 | 316 | | |
278 | 317 | | |
279 | 318 | | |
| |||
3753 | 3792 | | |
3754 | 3793 | | |
3755 | 3794 | | |
| 3795 | + | |
| 3796 | + | |
| 3797 | + | |
| 3798 | + | |
| 3799 | + | |
| 3800 | + | |
| 3801 | + | |
| 3802 | + | |
| 3803 | + | |
| 3804 | + | |
| 3805 | + | |
| 3806 | + | |
| 3807 | + | |
| 3808 | + | |
| 3809 | + | |
| 3810 | + | |
| 3811 | + | |
| 3812 | + | |
| 3813 | + | |
3756 | 3814 | | |
0 commit comments