emscripten-core / emscripten-core/emscripten

`emscripten::val::new_` fails in js code at runtime / during instantiation of Module when compiled as MAIN_MODULE

Open
#16,451 3 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

When I have a main with bindings and use `emscripten::val::.new_()` I get a js error at runtime.

```c++
#include
#include
#include

void hello_world()
{
emscripten::val xhr = emscripten::val::global("XMLHttpRequest").new_();
}

EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("hello_world", &hello_world);
}
```
the error I see in the browsers console output is:
```
bindings.js:39028 Uncaught (in promise) RuntimeError: Aborted(Assertion failed: Missing signature argument to addFunction: function __emval_new(handle, argCount, argTypes, args) {
handle = Emval.toValue(handle);

var newer = emval_newers[argCount];
if (!newer) {
newer = craftEmvalAllocator(argCount);
emval_newers[argCount] = newer;
}

return newer(handle, argTypes, args);
})
at abort (bindings.js:39028)
at assert (bindings.js:38192)
at addFunction (bindings.js:38067)
at reportUndefinedSymbols (bindings.js:39926)
at preloadDylibs (bindings.js:39938)
at run (bindings.js:90244)
at runCaller (bindings.js:90178)
at removeRunDependency (bindings.js:39000)
at receiveInstance (bindings.js:39161)
at receiveInstantiationResult (bindings.js:39176)
abort @ bindings.js:39028
assert @ bindings.js:38192
addFunction @ bindings.js:38067
reportUndefinedSymbols @ bindings.js:39926
preloadDylibs @ bindings.js:39938
run @ bindings.js:90244
runCaller @ bindings.js:90178
removeRunDependency @ bindings.js:39000
receiveInstance @ bindings.js:39161
receiveInstantiationResult @ bindings.js:39176
Promise.then (async)
(anonymous) @ loader.html:12
```

removing the `new_` call I get no error. I can use other `emscripten::val` functions without any trouble.

I use cmake as build system with the following CMakeLists.txt:

```cmake
cmake_minimum_required(VERSION 3.9)
project(bindings VERSION 1.0.1 DESCRIPTION "bindings description")
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
message( FATAL_ERROR "This example works ONLY with emscripten")
endif()

# link and compile flags ()
set(EM_FLAGS "--bind -s MAIN_MODULE=1 -s EXPORT_NAME=\"createMyModule\" -s FORCE_FILESYSTEM=1 -s MODULARIZE=1 -s EXPORT_ES6=0 -s USE_ES6_IMPORT_META=0 -v")

# compile flags
set(EM_CXX_FLAGS "${EM_CXX_FLAGS} ${EM_FLAGS} ")

# link flags
set(EM_LINKER_FLAGS "${EM_LINKER_FLAGS} ${EM_FLAGS}")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EM_CXX_FLAGS}")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${EM_LINKER_FLAGS}")

add_executable(bindings main.cpp)
```
I attached a zip file with the `main.cpp` the `CMakeLists.txt` , `loader.html` and a minimal Docker based build system to reproduce the error
[reproduce.zip](https://github.com/emscripten-core/emscripten/files/8205745/reproduce.zip)

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.2 (a77b559a8b40b7e89fc8c17e41034128df9543e4)
clang version 14.0.0 (https://github.com/llvm/llvm-project 782c0dd1a1c235afb09a34e7da4a1267ead14765)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/derthorsten/src/emsdk/upstream/bin

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.