WebAssembly / WASI

WebAssembly System Interface
Other
4.81k stars 249 forks source link

Separate initialization and finalization #441

Open tudor opened 3 years ago

tudor commented 3 years ago

Right now, _start initializes global state, calls main(), and then tears down global state.

This is fine for executable modules, but not for modules designed to be used as libraries.

For a library, I would like to initialize global state right after the library is loaded, and then tear down global state right before shutting the library down.

I am proposing separating the initialization part into a separate exported function (call it _init) and, similarly, splitting the finalization into a separate exported function (call it _fini).

Then _start becomes (pseudocode):

void _start() {
    _init();
    // set up arguments for main(), call main()
    _fini();
}

but people who intend to use the module as a library have the option to call _init() and _fini() directly.

devsnek commented 3 years ago

we already have this (to some degree), it is called a wasi reactor and it uses _initialize: https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md#current-unstable-abi

tudor commented 3 years ago

Nice, thank you. There should probably also be a finalization API (that would call, e.g. destructors of global C++ objects)