没有使用 indirect_call
的例子可在线使用。基于语义文档,我试过
(call_indirect
(i32.const 0)
(i32.const 0)
)
数字是随机的,但不会给出我所期望的运行时错误。我收到解析错误。
call_indirect
的正确语法是什么? ?
最佳答案
call_indirect
的正确语法似乎
(call_indirect $fsig
(i32.const 0)
)
哪里
$fsig
是 type
中定义的预期函数签名部分和参数是函数的地址(或者更确切地说是它在 table
中的索引)。以调用函数指针的以下 C 代码示例为例:
typedef void(*fp)();
void dispatch(fp x) {
x();
}
它compiles到
(module
(type $FUNCSIG$v (func))
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "dispatch" (func $dispatch))
(func $dispatch (param $0 i32)
(call_indirect $FUNCSIG$v
(get_local $0)
)
)
)
这是一个更完整的例子,我们实际调用了一个函数
test
返回一个值:(module
(type $FUNCSIG$i (func (result i32)))
(table 1 anyfunc)
(elem (i32.const 0) $test)
(memory $0 1)
(func $test (type $FUNCSIG$i) (result i32)
(i32.const 42)
)
(func $main (result i32)
(call_indirect $FUNCSIG$i
(i32.const 0)
)
)
)
关于webassembly - 如何在 WebAssembly 中使用 `indirect_call`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713841/