-
Notifications
You must be signed in to change notification settings - Fork 215
Description
For Wasm components, there is no mechanism currently for a component's module, to have its initialization called, i.e for static c++ class constructors to run. Wasm tool chains put these in a __wasm_call_ctors
method, which is invoked (for libraries) via _initialize
. However the component model does not export _initialize
and the wasmtime (I've not investigated other hosts) host cannot/does not invoke it. We therefore have a problem when an exported method is called as the runtime is not initialized. We can take advantage of the fact that Wasm memory is zero initialized to do something like
#ifdef TARGET_WASI
static int m_initializeCalled = 0;
EXTERN_C void _initialize();
#endif
COOP_PINVOKE_HELPER(void, RhpReversePInvoke, (ReversePInvokeFrame * pFrame))
{
#ifdef TARGET_WASI
if (!m_initializeCalled)
{
_initialize();
m_initializeCalled = 1;
}
#endif
As a hack for components. It could probably be extended to cover wasm-wasi
programs , but as a temporary workaround until bytecodealliance/preview2-prototyping#99 is resolved, could there be a better solution?
Thanks