emscripten-core / emscripten-core/emscripten

LinkError: WebAssembly.instantiate(): Import #0 module="env" function="_malloc" error: function import requires a callable

Open
#13,036 5 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Dear ,
I got a problem from running WASM file in chrome browser.
My EMScripten is v1.38.36

My C++ code as follows: math.c
```
#ifndef EM_PORT_API
# if defined(__EMSCRIPTEN__)
# include
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
# else
# define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
# endif
# else
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype
# else
# define EM_PORT_API(rettype) rettype
# endif
# endif
#endif

#include
#include
#include

extern "C"
{
EM_PORT_API(void*)create_input_buffer(int isize)
{
return malloc(isize);
}

//EM_PORT_API(int) free_input_buffer(void* buffer)
//{
//free(buffer);
// return 1;
//}

EM_PORT_API(int) add(int a,int b) {
int sum = 0;
sum = a + b;
return sum;
}

EM_PORT_API(int) delta(int a,int b) {
int delta = 0;
delta = a - b;
return delta;
}
}

```

My Complie command as follows:
```
emcc math.c -Os -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_FUNCTIONS=['_add','_delta','_create_input_buffer'] -s MODULARIZE=1 -o math.wasm
```

My Js code as follows:
```
function loadWebAssembly(filename, imports = {}) {
return fetch(filename)
.then(response => response.arrayBuffer())
.then(buffer => {
imports.env = imports.env || {}
Object.assign(imports.env, {
memoryBase: 0,
tableBase: 0,
__memory_base: 0,
__table_base: 0,
memory: new WebAssembly.Memory({ initial: 256, maximum: 256 }),
table: new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' }),
})
return WebAssembly.instantiate(buffer, imports)
})
.then(result => result.instance)
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.