根据这个 github 评论,我可以通过直接访问 wasm 实例的内存来重新创建从 Rust/wasm 返回的 Uint8ClampedArray 或 Uint8Array:
const textureRaw = new Uint8ClampedArray(memory.buffer, texture.offset(), texture.size());
问题是,由已经实例化一个 wasm 实例生成的 js 文件wasm-bindgen
,我想访问这个特定实例的内存,但它似乎没有被导出:
// XXXXX_bg.js
const path = require('path').join(__dirname, 'ed25519_sigs_bg.wasm');
const bytes = require('fs').readFileSync(path);
let imports = {};
imports['./ed25519_sigs.js'] = require('./ed25519_sigs.js');
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
module.exports = wasmInstance.exports;
我将如何访问当前 wasm 实例的内存缓冲区?
我试过做:
import { memory } from "XXXXXX_bg";
// say o is returned as an object with the right offset() and size() accessors. It represents an Uint8Array in memory
let outU8A: Uint8Array = new Uint8Array(
memory.buffer,
o.offset(),
o.size()
);
输出是预期的大小,但每个值都为零。这让我觉得我可能正在尝试从第二个 wasm.memory 实例加载?