在 WebAssembly 实例中将嵌入文件读取为 Uint8Array
Read embedded file as Uint8Array in WebAssembly instance
我在 WebAssembly wasm 文件中嵌入了一个二进制文件。
考虑这个来源:
embed.ts(使用 AssemblyScript 构建为 embed.wasm):
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, ?!?!?
如何读出这个字节数组来重建嵌入文件?我需要重新创建 Uint8Array,以便在 worker.js 中我可以保存文件或将其流式传输到某处。
现阶段的WebAssembly只能在wasm模块和javascript主机之间传递数字,所以instance.text
只是数字(指针)或线性内存中的偏移量。为了从此内存中读取真实数据,您可以使用 __getUint8Array
或 __getArray
from loader. Also useful info
我在 WebAssembly wasm 文件中嵌入了一个二进制文件。
考虑这个来源:
embed.ts(使用 AssemblyScript 构建为 embed.wasm):
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, ?!?!?
如何读出这个字节数组来重建嵌入文件?我需要重新创建 Uint8Array,以便在 worker.js 中我可以保存文件或将其流式传输到某处。
现阶段的WebAssembly只能在wasm模块和javascript主机之间传递数字,所以instance.text
只是数字(指针)或线性内存中的偏移量。为了从此内存中读取真实数据,您可以使用 __getUint8Array
或 __getArray
from loader. Also useful info