webassembly - 如何在 WebAssembly 中使用 `indirect_call`?

标签 webassembly

没有使用 indirect_call 的例子可在线使用。基于语义文档,我试过

(call_indirect 
    (i32.const 0)
    (i32.const 0)
    )

数字是随机的,但不会给出我所期望的运行时错误。我收到解析错误。
call_indirect 的正确语法是什么? ?

最佳答案

call_indirect 的正确语法似乎

(call_indirect $fsig
   (i32.const 0)
)

哪里$fsigtype 中定义的预期函数签名部分和参数是函数的地址(或者更确切地说是它在 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/

相关文章:

assembly - 在 ARMv7a 和 Neon 上通过 64 位签名比较来支持 CMGT 的最有效方法是什么?

rust - 如何使用 Rust 和 web-sys 将 Clamped<Vec<u8>> 转换为 Clamped<&mut [u8]> ?

io - WebAssembly 可以返回什么?

javascript - 有没有办法在浏览器中本地运行 C 程序?

c - EM_JS 无法导出 emscripten 中的函数

go - CGO 库构建到 JS WASM 文件

c++ - 使用 emscripten 从 C++ webassembly 代码将输出写入 json

typescript - 如何在 typescript 文件中导入由 wasm-pack 生成的 wasm 文件?

没有 Blazor 的 C# WASM

go - 是否可以从 Go 调用 WebAssembly 函数?