emscripten-core / emscripten-core/emscripten

embind "table index is out of bounds" or "null function or function signature mismatch" when calling a bound function returning memory_view

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

Description

Here's an example of my bound function:

```cpp
static auto values_test = std::array{
1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u
};

static auto raw_obj_buffer() -> emscripten::memory_view {
return emscripten::memory_view{
values_test.size(),
values_test.data(),
};
}

// ...

emscripten::function("raw_obj_buffer", raw_obj_buffer);
```

Or this:

```cpp
struct my_handle {
std::uint32_t id;
};

static auto raw_obj_buffer(my_handle) -> emscripten::memory_view {
return emscripten::memory_view{
values_test.size(),
values_test.data(),
};
}

// ...

emscripten::value_object("my_handle")
.field("id", &my_handle::id);

emscripten::function("raw_obj_buffer", raw_obj_buffer);
```

The functions were called like this, respectively:
```js
import MyLib from './mylib.js'

fetch('mylib.wasm').then(async result => {
const wasm = await result.arrayBuffer();
const myLib = await MyLib({ wasm });
console.log(myLib.raw_obj_buffer());
});
```
```js
import MyLib from './mylib.js'

fetch('mylib.wasm').then(async result => {
const wasm = await result.arrayBuffer();
const myLib = await MyLib({ wasm });
console.log(myLib.raw_obj_buffer({id: 0}));
});
```

The errors are, respectively:
```
RuntimeError: null function or function signature mismatch
at emscripten::internal::Invoker>::invoke(emscripten::memory_view (*)()) (bind.h:353:13)
```
```
RuntimeError: table index is out of bounds
at emscripten::internal::Invoker, my_handle>::invoke(emscripten::memory_view (*)(my_handle), my_handle*) (bind.h:353:13)

```

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.17 (fbc532773d84d2bd7da876275671970e792ad1cd)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

**Failing command line in full:**
```
/Users/myself/emsdk/upstream/emscripten/em++ -g -gsource-map --source-map-base http://localhost:3004/ -s MODULARIZE -s EXPORT_NAME=MyLib -s EXPORT_ES6=1 -s ASSERTIONS=1 -s SAFE_HEAP=0 -s EVAL_CTORS=0 -s WASM=1 -s NO_EXIT_RUNTIME=1 -s STRICT=1 -s ENVIRONMENT=web -s MINIMAL_RUNTIME=1 -s TEXTDECODER=2 -s ABORTING_MALLOC=0 -s SUPPORT_ERRNO=0 -s MALLOC=emmalloc -s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=268435456 -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=0 -s INVOKE_RUN=0 --no-entry --output_eol=linux --bind -v CMakeFiles/mylib.dir/Debug/src/bindings.cpp.o -o Debug/mylib.js vcpkg_installed/wasm32-unknown-emscripten/debug/lib/libmylib.a vcpkg_installed/wasm32-unknown-emscripten/debug/lib/libmyotherlib.a
```

**Full link command and output with `-v` appended:**
```
"/Users/myself/emsdk/upstream/bin/wasm-ld" -o Debug/mylib.wasm CMakeFiles/mylib.dir/Debug/src/bindings.cpp.o vcpkg_installed/wasm32-unknown-emscripten/debug/lib/libmylib.a vcpkg_installed/wasm32-unknown-emscripten/debug/lib/libmyotherlib.a -L/Users/myself/.cache/emscripten/sysroot/lib/wasm32-emscripten --whole-archive -lembind-rtti --no-whole-archive -lnoexit -lc-debug -lemmalloc-noerrno -lcompiler_rt -lc++ -lc++abi -lsockets --fatal-warnings -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=malloc --export=free --export=__cxa_is_pointer_type --export=__cxa_can_catch --export=setThrew --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=268435456 --global-base=1024
"/Applications/Xcode.app/Contents/Developer/usr/bin/python3" -E /Users/myself/emsdk/upstream/emscripten/tools/wasm-sourcemap.py Debug/mylib.wasm --dwarfdump=/Users/myself/emsdk/upstream/bin/llvm-dwarfdump -o Debug/mylib.wasm.map --basepath=/Users/myself/mylib-js/build/emscripten/Debug
"/Users/myself/emsdk/upstream/bin/wasm-emscripten-finalize" --output-source-map-url=http://localhost:3004/mylib.wasm.map -g --dyncalls-i64 --dwarf Debug/mylib.wasm -o Debug/mylib.wasm --detect-features --input-source-map=Debug/mylib.wasm.map --output-source-map=Debug/mylib.wasm.map
"/Users/myself/emsdk/node/14.18.2_64bit/bin/node" /Users/myself/emsdk/upstream/emscripten/src/compiler.js /var/folders/0y/yjn07r7d7630x042vgk3xph00000gq/T/tmplutox_zq.json
em++: warning: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]
"/Users/myself/emsdk/upstream/bin/wasm-opt" --strip-producers Debug/mylib.wasm -o Debug/mylib.wasm -g --mvp-features --input-source-map=Debug/mylib.wasm.map --output-source-map=Debug/mylib.wasm.map
```

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.