WASM Support #8
Replies: 4 comments 4 replies
-
No WASM support yet, but let's think about it how it could be done. You've laid out the current landscape well: there seems to be two ways to use WASM in a Cloudflare Worker today
This is wrong, see below
Style 1 (global variable binding) should be fairly straightforward to implement in Denoflare. Injected global would be a Style 2 (module import) is trickier, since this syntax is not valid Deno. Maybe some tricks with import maps or providing override sources to Do you have existing code you're trying to get working with Denoflare? If so, I could prioritize getting that style working. Otherwise, I think the implementation approach might be:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick and detailed response!
Interesting, I actually was not aware that their implementation differs from the ESM proposal.
Thanks for that offer, no there‘s no urgent usecase actually, I‘m just creating some architecture concepts and am interested in general CF Worker/Deno interopability in that regard.
I agree, this seems to be more straigtforward to implement and would not require manual build steps. I‘m not sure however wether that global binding approach is superseded by the module imports or if both should coexist. I wonder if another approach might be feasible, which, just like my original option B., would involve an additional rewrite of imports at build time, but works with valid syntax: Using a local |
Beta Was this translation helpful? Give feedback.
-
Ok, just pushed initial wasm support for module-based workers for This is using the magic function approach kind of along the lines of what you suggested. Ended up being really clean, no extra module graph evaluations or special compiler options or temporary stub files. Just works as a standard Deno function when reusing code outside of Denoflare, since it builds on Deno's local file fetch and WebAssembly support. See the Can finally start doing wasm scenarios using Deno! So excited. Thanks for prompting me on this. |
Beta Was this translation helpful? Give feedback.
-
Alright, shipped in Release 0.4.0 New doc page up: https://denoflare.dev/reference/wasm |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So far I was unable to load wasm from a local file, is this possible at the moment?
Workers recently started supporting a not-yet standardized way of importing wasm like an es module, which seems way more flexible than the previous bindings.
So one can just do it like this:
import module from "./file.wasm"
Deno used to support a similar approach but removed it eventually with the longer term intent to reintroduce it when some outstanding questions are sorted out. Instead,
Deno.readFile
has to be used (or local file fetch) - However, the global Deno namespace seems to not be available from within the worker code.I wonder how this could be worked around in an interoperable way that fits both CF Workers and Deno.
A. Use the import syntax and somehow make Deno.emit ignore that import, since pratically no further processing is required. Longer term the ES import would IMHO be the preferable syntax, however I don’t know if it is technically feasible, also another issue would be that at this point, the result would not be valid Deno code anymore, since that one would not know how to process this import outside denoflare.
B. Use the default Deno.readFile approach and thereby keep things the current Deno way. A very dirty workaround could be to as soon as such a readFile of a file with .wasm extension comes up, convert that line to an import. That way the result could run both on Deno and CF. However, partially stubbing Deno seems messy, likewise converting these reads of .wasm files to imports seems messy if not potentially dangerous too, since I might want to read some file with wasm extension for whatever other reason than to actually run its code.
Both options don’t really seem good at the moment, I wonder if there’s any better possibly that I missed?
An unrelated option is to embed wasm into js a base64, however I would assume that might have some performance implications, also I prefer to not have added build steps.
Beta Was this translation helpful? Give feedback.
All reactions