WebAssembly LinkError:函数导入需要可调用

cra*_*c08 5 javascript c webassembly

我最近开始使用WebAssembly。我在尝试使用C代码中的登录时遇到问题。我以最简单的方法重新创建了错误。我得到的错误是

Uncaught (in promise) LinkError: WebAssembly.Instance(): Import #1 module="env" function="_log" error: function import requires a callable
Run Code Online (Sandbox Code Playgroud)

错误指向此功能,特别是 WebAsembly.Instance(module, imports)

function loadWebAssembly(filename, imports = {}) {
  return fetch(filename)
    .then((response) => response.arrayBuffer())
    .then((buffer) => WebAssembly.compile(buffer))
    .then((module) => {
      imports.env = imports.env || {}
      Object.assign(imports.env, {
        memoryBase: 0,
        tableBase: 0,
        memory: new WebAssembly.Memory({
          initial: 256,
          maximum: 512,
        }),
        table: new WebAssembly.Table({
          initial: 0,
          maximum: 0,
          element: 'anyfunc',
        }),
      })
      return new WebAssembly.Instance(module, imports)
    })
}
Run Code Online (Sandbox Code Playgroud)

(我将此函数称为loadWebAssembly('/test.wasm'))

我的C代码是

#include <math.h>

double test(v) {
  return log(v)
}
Run Code Online (Sandbox Code Playgroud)

并且在编译时没有错误

emcc test.c -Os -s WASM=1 -s SIDE_MODULE=1 -o test.wasm
Run Code Online (Sandbox Code Playgroud)

我无法解决此错误,希望有人可以帮助我。

Ghi*_*lie 5

您没有提供log()in的实现imports.env

Object.assign(imports.env, {
    memoryBase: 0,
    tableBase: 0,
    memory: new WebAssembly.Memory({
        initial: 256,
        maximum: 512,
    }),
    table: new WebAssembly.Table({
        initial: 0,
        maximum: 0,
        element: 'anyfunc',
    }),
    _log: Math.log,
})
Run Code Online (Sandbox Code Playgroud)