【问题标题】:WebAssembly LinkError: function import requires a callableWebAssembly LinkError:函数导入需要可调用
【发布时间】:2017-10-21 05:01:26
【问题描述】:

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

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

我一直无法修复这个错误,希望有人能帮助我。

【问题讨论】:

    标签: javascript c webassembly


    【解决方案1】:

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

    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,
    })
    

    【讨论】:

    • 但是这样wasm代码只会运行JS函数而不是C实现......对吗?
    • @ShlomiSchwartz 你找到答案了吗?它会只运行 js 版本还是以某种方式使用 C 版本的日志?谢谢!
    • @NikitaPestrov 上面的日志运行的是 JS Math.log 版本,我还没有找到其他解决方案。
    • 这对我不起作用:(
    猜你喜欢
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多