Description
There should be a way to emit custom wasm sections from inside Rust code.
There are many potential uses for custom sections. For example, currently the the js!
macro in stdweb
takes JavaScript code, stringifies it, and assigns it to a normal constant. Then cargo-web
scans the .wasm
file, extracts those JavaScript snippets, and deletes the (now useless) corresponding data section entries.
Ideally instead of mucking around with the data section (which often creates holes in memory at runtime since you can't really move the entries' offsets around, and is harder to process) it'd just read a custom section which stdweb
would emit for it, and then easily nuke it afterwards.
One potential way we could support custom sections would be like this: have a built-in macro, say, append_to_custom_section!
, which would take two arguments: (1) a string with the name of the section, (2) a blob of bytes it would append to that section. It would also be a purely compile-time construct and would expand into nothing after macro expansion.
For example this Rust code:
append_to_custom_section!("js-snippets", b"\x12\x00\x00\x00console.log('...')")
append_to_custom_section!("js-snippets", b"\x0c\x00\x00\x00alert('...')")
would result in a custom wasm section with the name js-snippets
containing \x12\x00\x00\x00console.log('...')\x0c\x00\x00\x00alert('...')
. The blobs of bytes would be concatenated in the order of their appearance within one .rs
file, and with an unspecified order between different .rs
files.
This would allow a procedural macro to put arbitrary information in a custom section of its choice, which then could be easily consumed by external tools.