Wasm-bindgen: access wasm instance's memory buffer (from JS)

≯℡__Kan透↙ 提交于 2020-04-16 06:07:20

问题


According to this github comment, I can re-create an Uint8ClampedArray or Uint8Array returned from Rust/wasm by accessing the memory of the wasm instance directly:

const textureRaw = new Uint8ClampedArray(memory.buffer, texture.offset(), texture.size());

The thing is, the js files generated by wasm-bindgen already instantiate a wasm instance, and I'd want to access the memory of this particular instance but it doesn't seem to be exported:

// 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;

How would I access the current wasm instance's memory buffer ?

I've tried doing:

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()
);

The output is the expected size but every value is zero. Which makes me think I might be trying to load from a second wasm.memory instance ?

来源:https://stackoverflow.com/questions/60199785/wasm-bindgen-access-wasm-instances-memory-buffer-from-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!