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

时间:2017-05-21 13:41:28

标签: javascript c webassembly

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

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

错误指向此功能,特别是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)
    })
}

(我将此功能称为loadWebAssembly('/test.wasm')

我的C代码是

#include <math.h>

double test(v) {
  return log(v)
}
使用

编译时,

并没有错误

emcc test.c -Os -s WASM=1 -s SIDE_MODULE=1 -o test.wasm

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

1 个答案:

答案 0 :(得分:3)

您未在log()

中提供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,
})