loke
12/31/2023, 9:00 AMdocument
internally, and my code runs in a webworker. What is the correct way to instantiate and run the .wasm
file that I compiled? I'm getting error that suggest that I need a specific importObject
when I call WebAssembly.instantiateStreaming
, but it's not clear what that should contain.andylamax
12/31/2023, 9:41 PMloke
01/01/2024, 2:59 PMloke
01/01/2024, 2:59 PMloke
01/01/2024, 5:48 PMloke
01/01/2024, 5:49 PMandylamax
01/01/2024, 5:51 PMloke
01/02/2024, 4:11 AMloke
01/02/2024, 4:11 AMandylamax
01/02/2024, 4:17 AMloke
01/02/2024, 4:27 AMimportObject
that doesn't appear to be documented.andylamax
01/02/2024, 4:38 AMandylamax
01/02/2024, 4:38 AMloke
01/02/2024, 4:43 AMloke
01/02/2024, 4:44 AMandylamax
01/02/2024, 6:09 AMloke
01/02/2024, 12:25 PMloke
01/02/2024, 12:26 PMinstantiateStreaming
is optional, but that:
"There must be one matching property for each declared import of the compiled module or else a WebAssembly.LinkError is thrown".
This is what happens when I skip the importObject argument altogether:loke
01/02/2024, 12:26 PMWebAssembly.instantiateStreaming(fetch("array-clientweb2-webworker-wasmjs.wasm")).then(
(results) => {
console.log("Result = " + results)
},
);
This gives me this error:
TypeError: second argument must be an object
OK, fine, let's pass an empty object to see if it tells us anything else. Now we're getting this:
TypeError: import object field 'js_code' is not an Object
loke
01/02/2024, 12:30 PMjs_code
somewhere. Turns out that it does, and it's a lot. One of the intermediary files is called array-clientweb2-webworker-wasmjs.uninstantiated.mjs
and contains the definition for this argument. It's a huge list of functions, listing every entry point into normal js code that can be called from the wasm). It actually makes sense, since wasm doesn't havr arbitrary access to the surrounding js environment. So the compiler prepares entry points for everything that will need to be called.loke
01/02/2024, 12:33 PMcompute-queue-worker-wasm.js
in my case, which handles all of this magic. The issue is that this generated file refers to `document`because it assumes it's being loaded in a regular browser environment. I don't fully understand what it's trying to do, but it does check the document for a <script>
tag and some other things that is completely unnecessary in a webworker.loke
01/03/2024, 5:38 AMdocuemnt
which is not available in a webworker. Do you know if there is a way to use the wrapper in such a was that document
is not used, so I can load it from a webworker?ephemient
01/03/2024, 5:41 AMloke
01/03/2024, 5:42 AMdocument
that returns empty results for whatever it is that the wrapper expects?ephemient
01/03/2024, 5:44 AMdocument
? possiblyephemient
01/03/2024, 5:45 AMloke
01/03/2024, 5:46 AMloke
01/03/2024, 5:48 AMdocument
if it's not defined. Perhaps a better approach would be to file a bug report and wait for things to be fixed. 🙂loke
01/03/2024, 6:01 AMdocument
, but it's complicated. It tried to call document.createElement
to create a new <script>
element and then put stuff in there. It's a mess, and not very easy.loke
01/03/2024, 6:02 AMloke
01/05/2024, 7:26 AM