3 ms·
I think the easiest way to write webassembly is to write it in webassembly text format: https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_
by TekMol 6y ago
I think the easiest way to write webassembly is to write it in webassembly text format:
https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format https://developer.mozilla.org/en-US/docs/WebAssembly/Underst...
And then load that code via
WebAssembly.instantiateStreaming(fetch('mycode.wasm'))
It would be even nicer if it could be done without a pass through the network. But I think there is nothing built into browsers to support inline webassembly.
- stagas 6y agoI was also disappointed that this isn't included in the browsers given that it was designed to be very simple to parse and compile. So I tried as an exercise to build such a compiler[0] and indeed it was much easier than I expected (with a few shortcuts of course, being a POC). It is just 5kb gzipped and it compiles to binary most of the WAT code out there and also quite fast, just a few ms. That said, I think writing WAT by hand is only helpful for very small critical hot code, anything more complex and IMO you need an abstraction of some sort. [0]: https://github.com/stagas/wel https://github.com/stagas/wel
- TekMol 6y agoWow, all that code is needed to feed a string to WebAssembly.instantiateStreaming()? What does the code do? I was hoping there could be a trick like feeding a "blob:..." url to instantiateStreaming() or something. Since the bottleneck of a codebase typically is a single loop, I think it often would be a good approach to rewrite that one in Webassembly by hand.
- stagas 6y agoYou can inline the binary as base64 or simply a Uint8Array and instantiate it without having to fetch a wasm file. Many choose that approach as it also makes the library `import`-able in sync. The code I was pointing can be used to compile the WebAssembly Text format to binary and instantiate it afterwards on-the-fly, which is useful if you don't want a build step, so skipping the tooling(outside the browser) and also make edits which will then be compiled directly when the file executes. It's an option for experimentation mostly.
- TekMol 6y agoOoohh shit! Now I see that instantiateStreaming() does not accept webassembly in text format. So we need to ship a whole compiler to avoid a build step. Count me out :)
- stagas 6y agoYes, my point was that it's so trivial (and negligible in byte count size) to implement a compiler for the Webassembly text format, as the POC demonstrates, that it's curious why the browser isn't including one so that there isn't the need for extra tooling.
- coldtea 6y ago>I think the easiest way to write webassembly is to write it in webassembly text format: It's the simplest, but not the easiest.