emscripten-core / emscripten-core/emscripten
emscripten::val fails in global scope constructors
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Tested with 3.1.73.
Attempting to use `val` in constructors executed at global scope (before `main` begins) fails with `emval::as has unknown type` errors.
Minimal example:
```cpp
#include
#include
#include
struct test_struct {
test_struct() {
std::cout << "nagivator.userAgent " << emscripten::val::global("navigator")["userAgent"].as() << std::endl;
std::cout << "window.innerWidth " << emscripten::val::global("window")["innerWidth"].as() << std::endl;
}
};
// fails when constructed in global scope:
test_struct test;
auto main()->int {
// OK :
//test_struct test;
// also OK:
//static test_struct test;
std::cout << "Hello world" << std::endl;
return EXIT_SUCCESS;
}
```
Build with `--bind` and `-sENVIRONMENT=web`.
Expected output, and output when test_struct is initialised inside `main`:

Output when trying to initialise at global scope, for the `.as()`:

and for the `.as()`:

I'm guessing that something gets initialised before `main` runs, but after static construction completes. Whereas ideally, everything should be ready before any C++ objects are constructed at static scope too.
Contributor guide
Assessment
This issue has not been assessed yet.