最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebAssembly LinkError: function import requires a callable - Stack Overflow

programmeradmin15浏览0评论

I've recently started working with WebAssembly. I hit a problem trying to use log in my C code. I recreated the error in the simplest way I could. The error I get is

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

The error points to this function, specifically WebAsembly.Instance(module, imports)

function loadWebAssembly(filename, imports = {}) {
  return fetch(filename)
    .then((response) => response.arrayBuffer())
    .then((buffer) => WebAssemblypile(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)
    })
}

(I call this function with loadWebAssembly('/test.wasm'))

My C code is

#include <math.h>

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

and gets no errors when compiled with

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

I haven't been able to fix this error, I hope someone can help me out.

I've recently started working with WebAssembly. I hit a problem trying to use log in my C code. I recreated the error in the simplest way I could. The error I get is

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

The error points to this function, specifically 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)
    })
}

(I call this function with loadWebAssembly('/test.wasm'))

My C code is

#include <math.h>

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

and gets no errors when compiled with

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

I haven't been able to fix this error, I hope someone can help me out.

Share Improve this question asked May 21, 2017 at 13:41 craigmc08craigmc08 1951 gold badge2 silver badges5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

You aren't providing an implementation of 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,
})

I might be wrong but I think your C code is wrong

By default emscripten exports only main function others are as dead code

you need to tell the emscripten to keep alive the test function by using macro EMSCRIPTEN_KEEPALIVE

and don't forget to include header file emscripten/emscripten.h

For more info go to this link

test.c

#include <stdio.h>
#include <emscripten/emscripten.h>
#include <math.h>

EMSCRIPTEN_KEEPALIVE
double test(double x)
{
    return log(x);
}

and you can access your function in js

JavaScript

loadWebAssembly('test.wasm')
    .then((data) => {
        console.log(data.exports) // {__wasm_call_ctors: ƒ, log10: ƒ}
    }).
    catch((err) => {
        console.log(err);
    });
发布评论

评论列表(0)

  1. 暂无评论