WebAssembly LinkError: function import requires a callable
WebAssembly LinkError: function import requires a callable
我最近开始使用 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
我一直没能修复这个错误,希望有人能帮助我。
您没有在 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,
})
我最近开始使用 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
我一直没能修复这个错误,希望有人能帮助我。
您没有在 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,
})