simbolo
simbolo

Reputation: 7494

Read embedded file as Uint8Array in WebAssembly instance

I'm embedding a binary file within a WebAssembly wasm file.

Consider this source:

embed.ts (gets built to embed.wasm using AssemblyScript):

export const text: u8[] = [83,65,77,80,76,69,10]; // The text "SAMPLE" in UTF-8 encoding

export const textLength: i32 = text.length;

worker.js:

const instance = new WebAssembly.Instance(/* read embed.wasm */).exports;
instance.textLength // prints 7, correct
instance.text // prints, 10232 of type number, ?!?!?

How can I read out this byte array to enable reconstruction of the embedded file? I need to recreate the Uint8Array so in worker.js I can save the file or stream it somewhere.

Upvotes: 0

Views: 563

Answers (1)

MaxGraey
MaxGraey

Reputation: 361

WebAssembly in current stage can pass only numbers between wasm module and javascript host, so instance.text is just number (pointer) or offset in linear memory. In order to read real data from this memory you could use __getUint8Array or __getArray from loader. Also useful info

Upvotes: 1

Related Questions