-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
emscripten::val fails in global scope constructors #23170
Comments
Hmm, IIRC we do all the embind type registration because user-level constructors are called, but I looks like the ordering is wrong here. Actually looking at the code it looks like we do use a C++ constructor to register the types: emscripten/system/include/emscripten/bind.h Lines 2267 to 2280 in dfe3356
|
To be clear, |
Sorry I mean "static constructor" in the sense of "constructors that run before main runs". The reason is that embind is itself initialized by such a "static constructor", and you have no way of ensuring that it happens before you one does. You could get lucky, or try to play with object file ordering on the command line.. but that super fragile. |
Yeah, it's best to avoid using emval during static constructors. We should probably add a note in the documentation or I suppose we could add an assert. |
We used to use a C static constructor to try to work around this, but it turned out that had its own issues. See #17182. |
Tested with 3.1.73.
Attempting to use
val
in constructors executed at global scope (beforemain
begins) fails withemval::as has unknown type
errors.Minimal example:
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<std::string>()
:and for the
.as<unsigned int>()
: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.The text was updated successfully, but these errors were encountered: