简体   繁体   English

如何从 wasm (rust) 到 js 获取 static mut 的地址?

[英]How can I get the adress of a static mut from wasm (rust) to js?

I have a rust-wasm project where I need to fill a buffer in webassembly before passing it to my js script and then display it with a canvas.我有一个 rust-wasm 项目,我需要在将它传递给我的 js 脚本之前在 webassembly 中填充一个缓冲区,然后用 canvas 显示它。

(Inspired by this article ) (灵感来自这篇文章

static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT];

But a static mut in rust is unsafe, so I can't declare it with #[wasm_bindgen]但是 rust 中的 static mut 是不安全的,所以我不能用#[wasm_bindgen]声明它

How can I get the address of this static buffer in my js script?我如何在我的js脚本中获取这个static缓冲区的地址?

EDIT:编辑:

I have seen this solution:我见过这个解决方案:

const { instance } = await WebAssembly.instantiateStreaming(
          fetch("./demo.wasm")
        );

const buffer_address = instance.exports.BUFFER.value;

I don't really understand what is happening in this code, and I can't use that because I use the wasm-pack buildtool: it creates for me the.js file corresponding to the wasm file, and I still can't get the Buffer address.我真的不明白这段代码中发生了什么,我不能使用它,因为我使用了 wasm-pack 构建工具:它为我创建了与 wasm 文件对应的 .js 文件,但我仍然无法获取缓冲区地址。

Any help would be appreciated任何帮助,将不胜感激

Your linked article uses #[no_mangle] , which makes BUFFER exported.您的链接文章使用#[no_mangle] ,这使得BUFFER导出。

#[no_mangle]
static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT];

The exported BUFFER can then be found on the instance, via instance.exports.BUFFER.value;然后可以通过 instance.exports.BUFFER.value 在实例上找到导出的BUFFER instance.exports.BUFFER.value; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ArrayBuffer从JS传递给AssemblyScript / Wasm? - How can I pass an ArrayBuffer from JS to AssemblyScript/Wasm? Rust wasm:如何访问当前实例的 Webassembly.Memory(来自 Rust)? - Rust wasm: How to access Webassembly.Memory of current instance (from Rust)? 如何从 Wasm 调用外部 js function? - How to call an external js function from Wasm? 使用rust+webassembly进行web开发,如何解决wasm和js交互的额外开销 - Using rust+webassembly for web development, how to solve the extra cost of wasm and js interaction 如何协调主机 JS 和 Wasm 模块之间的内存访问? - How can I coordinate Memory access between the host JS and the Wasm module? 如何通过jquery load()函数获取地址栏中content(home.html)的URL? - how can i get the url of the content( home.html) in adress bar by jquery load() function? 如何在 JS 中获取 IP 地址 - Firefox Addon Devloment - How to get the IP Adress in JS - Firefox Addon Devloment 如何在html页面中从静态URL获取JSON - How can I get a JSON from static url in html page 我如何从js获取数据? - how can i get data from js? 我如何在没有地址栏或键盘的情况下从Rails Kiosk错误页面返回家 - How can I return home from an a Rails Kiosk Error page without an adress bar or keyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM