WebAssembly.instantiate(): Wasm code generation disallowed by embedder

Hey folks, I'm trying to get a Wasm library to work in a Pages function, but I'm having the hardest time instantiating it. At first I was fetching the .wasm binary to instantiate, but someone in #pages-help told me it had to be bundled within the _worker.js script. Okay, now it's a UInt8Array directly in the _worker.js script but I still get this error. Is there more information on how the wasm binary should be bundled? Is it a specific format that will make the CF worker accept it?
9 Replies
zegevlier
zegevlier•11mo ago
It should be imported, like in this example: https://developers.cloudflare.com/workers/runtime-apis/webassembly/javascript/#use-from-javascript Anything else will not work. You cannot create a WebAssembly object from anything that could chance at runtime
xeon06
xeon06OP•11mo ago
How does that get bundled in the end? I am using Astro here so maybe that's my problem, Vite can't bundle my wasm as expected Is there any more info on the format that this is to be expected to be in for the upload? I could always create a vite plugin I imagine wrangler is doing the bundling directly in that instance? Ah yes scrolling up reveals that
zegevlier
zegevlier•11mo ago
I believe they would be uploaded seperately (so the .wasm binary file would be uploaded, along with the code), although I'm having a hard time finding documentation for it. I'm sure the requests are made somewhere in the wrangler source (https://github.com/cloudflare/workers-sdk), but I wouldn't be able to tell you where.
xeon06
xeon06OP•11mo ago
All good I appreciate it. Push comes to shove I can always try bundling a minimal example with wrangler and see what comes out. Thanks! It turns out the Astro adapter has already done the work of the vite plugin for this and it works great if you configure it properly 🙃 I am curious about the impact of this external wasm file on bundle size though. The wrangler deploy --dry run option doesn't seem to take it into consideration, although it does show it in an "additional modules" table?
xeon06
xeon06OP•11mo ago
No description
xeon06
xeon06OP•11mo ago
Ah I tried taking it out of the bundle and it does count the file in the table in that total
zegevlier
zegevlier•11mo ago
Yea, it's definitely something to consider when using wasm. If the entire worker goes over 1mb you'll have longer cold start times
xeon06
xeon06OP•11mo ago
Ah, I had read that the bigger the size the slower the cold start but is there like a different code path taken > 1 mb?
zegevlier
zegevlier•11mo ago
Yea. Above 1mb they are not stored at every datacenter around the world, lower they are (on paid plan at least)
Want results from more Discord servers?
Add your server