- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想提供一个带有接受 Rust 函数指针的外部 JavaScript 函数的 WebAssembly 模块。
一旦这个 JS 模块被初始化,它将调用 .wasm
模块中的 run()
函数,然后调用 peekaboo
:
window.Module = {};
const imports = {
env: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({ initial: 256 }),
table: new WebAssembly.Table({ initial: 4, element: 'anyfunc' })
}
};
imports.env.peekaboo = function(f) {
const fn = imports.env.table.get(f);
return fn(2);
};
fetch('game.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.compile(bytes))
.then(mod => WebAssembly.instantiate(mod, imports))
.then(mod => {
mod.exports.run();
Module.memory = imports.env.memory;
Module.dealloc_str = mod.exports.dealloc_str;
});
我看到的例子表明,如果我以这种方式导入内存
,我应该能够使用表
来解析我的函数指针。这是 Rust 代码:
#![feature(wasm_import_memory)]
#![wasm_import_memory]
extern "C" {
fn peekaboo(f: fn(u32) -> u32);
}
fn main() {}
#[no_mangle]
pub fn run() {
let plus_one = |x: u32| -> u32 { x + 1 };
unsafe {
peekaboo(plus_one);
}
}
一切都编译正常,但是当我执行peekaboo
函数时,fn
变量为null
,表明该表无法找到函数指针。因此执行 fn(2)
会爆炸:
Uncaught (in promise) TypeError: fn is not a function
我或多或少关注了this example但由于我在 Rust 工作,翻译不是一对一的。我怀疑我忽略了一些对我来说不明显的东西,因为我是 Rust 和 WebAssembly 的新手。谁能发现我的错误?
最佳答案
到目前为止,Rust 中的 WebAssembly 后端似乎没有提供导入或导出(函数)table
的方法;索引 f
很好,但是 imports.env.table
与 wasm
实例(即空)使用的表不同。
此外,您应该在 FFI 中使用 extern fn
。
如果您想深入了解 Playground提供了一些不错的 WebAssembly 优化,看看这个例子:
#![crate_type = "cdylib"]
#![feature(link_args)]
#![allow(unused_attributes)] // link_args actually is used
#![link_args = "--import-memory"]
extern "C" {
fn peekaboo(f: extern "C" fn(u32) -> u32);
}
#[no_mangle]
pub fn run() {
extern "C" fn plus_one(x: u32) -> u32 {
x + 1
}
unsafe {
peekaboo(plus_one);
}
}
结果应该是这样的:
(module
(type $t0 (func))
(type $t1 (func (param i32) (result i32)))
(type $t2 (func (param i32)))
(import "env" "peekaboo" (func $peekaboo (type $t2)))
(import "env" "memory" (memory $env.memory 17))
(func $run (export "run") (type $t0)
(call $peekaboo
(i32.const 1)))
(func $playground::run::plus_one::h85275af105f0cc85 (type $t1) (param $p0 i32) (result i32)
(i32.add
(get_local $p0)
(i32.const 1)))
(table $T0 2 2 anyfunc)
(elem (i32.const 1) $playground::run::plus_one::h85275af105f0cc85))
如果你想在本地复制这个,将它添加到你的 Cargo.toml
中:
[lib]
crate-type = ["cdylib"]
[profile.release]
lto = true
并使用 cargo +nightly build --release --target wasm32-unknown-unknown
进行构建(假设 rustup 使用 nightly
工具链和 为
目标。nightly
工具链启用的 wasm32-unknown-unknown
关于javascript - 如何解析 Rust 和 WebAssembly 中的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47778074/
我在 WebAssembly 中看到的每个数据部分示例都使用字符串,即 (data (i32.const 16) "Hello World") 如何将这些部分与二进制数据一起使用?我怎么能做一些等同于
详细介绍 WebAssembly 的主要站点,https://webassembly.org/ ,自 1.0 版发布以来似乎没有更新,甚至是功能路线图。 docs on MDN好像也好不到哪里去,th
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
例如,C 没有与 Rust 相同的安全解决方案,但是用 Rust 编写的 WebAssembly 是否具有 Rust 的优点? 用 Rust 编写并转换为 WebAssembly 的程序会比用 C 编
垃圾收集语言,例如 Go 和 AssemblyScript,需要随应用程序一起提供运行时来处理垃圾收集。这导致比没有垃圾收集的语言更大的二进制大小。编译为 WebAssembly 的各种语言的二进制大
Webassembly 仅提供一种更高级的浮点运算:平方根。 它没有任何其他通常在 CPU 中实现的功能,例如幂和三角函数,官方常见问题解答中解释了这一基本原理: WebAssembly doesn’
有人可以分享语法来静态创建具有某些元素的表并引用它吗?我找不到这样做的任何 Web 程序集代码。 还有一些细节,比如我可以有多个列,如果索引不在表中会发生什么等会有所帮助吗? 最佳答案 (针对 202
有人可以分享语法来静态创建具有某些元素的表并引用它吗?我找不到这样做的任何 Web 程序集代码。 还有一些细节,比如我可以有多个列,如果索引不在表中会发生什么等会有所帮助吗? 最佳答案 (针对 202
即一方面,预编译代码更难阅读,因此更难以有意义地更改浏览器代码。 它如何比 JS 更“沙盒化”,这是否使它不那么容易被破解? “WebAssembly 被指定在安全的沙盒执行环境中运行。” - htt
https://webassembly.github.io/demo/说:“实现了完整的执行语义。”听起来 MVP 已经完成,但到底缺少什么,或者我做错了什么? 浪费: (module (me
我正在试验 WASM,我想将图片编码为 WebP 以学习 WASM。 我正在尝试编译 libwebp到单个 mjs 文件中。具体来说,encoding utility .我正在使用 Emscripte
我的问题类似于this one ,但不是属于 Blazor server 应用程序,我在 Blazor webassembly 应用程序的上下文中询问。我意识到在这个浏览器执行上下文中只有一个 (UI
我想使用这个很棒的包:https://github.com/hpcc-systems/hpcc-js-wasm它将 Webassembly (graphizlib.wasm) 与要使用的 Javasc
没有使用 indirect_call 的例子可在线使用。基于语义文档,我试过 (call_indirect (i32.const 0) (i32.const 0) ) 数字是随
我需要将 wasm 应用程序直接嵌入到 html 页面本身,而不是从单独的文件中加载它(例如通过 xhr)。到目前为止,我设法通过将字节直接“打印”到 Uint8Array 中来嵌入它(见下文),但它
我需要一个 WebAssembly 的“br_table”指令示例,因为我不知道如何直接以 WebAssembly 文本格式使用该指令。 就像下面的代码,我看不出它的构造结构对应的是哪一部分。哪一部分
是否有任何工具(目前)可以将 wasm 模块编译为 native 可执行文件(例如 ELF 或 .exe 文件)?类似 emscripten 的东西,但反过来。如果做不到这一点,有没有其他方法可以在浏
我尝试了 Blazor Webassembly。我在 Visual Studio 中创建了一个新的 Blazor Webassembly 项目。这为我们提供了一个基本的示例项目,其中包含可以通过单击增
我刚刚开始研究 WebAssembly MVP,并注意到无法访问堆栈和堆栈指针,或者实际上没有任何结构化异常处理支持(抛出/捕获)等。 鉴于它应该是一个 C 编译目标,它肯定可以实现 setjmp 和
在 Uno Platform for WebAssembly 中实现 URL 深度链接的任何线索或方向? 我看到它在以下项目中实现,可以在 https://platform.uno/showcases
我是一名优秀的程序员,十分优秀!