Reputation: 2935
Is it possible to terminate running WebAssembly code and cleanup any remaining resources from JavaScript?
In general the scenario is this. JS initializes a WASM module and calls an exported function. At a later point in time WASM code calls an imported function f
transferring control back to JS. f
does some stuff and based on the result decides whether to keep WASM running or not.
Two cases:
f
simply returns.f
decides to terminate the WASM function and possibly cleanup any WASM related resources. Can it? How?If it would make it easier to analyze a more concrete example think how execve
works. If the call fails it simply returns indicating failure. However if it succeeds it does not return - a broad oversimplification would be to say it terminates the caller and starts a new one in it's place (keeping some of the environment unchanged).
Upvotes: 1
Views: 813
Reputation: 36098
The only way to possibly terminate a caller -- no matter whether the caller is Wasm or JavaScript -- is to throw an exception. That leaves it up to the caller to react adequately. In particular, if it needs to do cleanup then it should be written such that it catches possible exceptions, performs the cleanup, and rethrows (or, equivalently, use a try-finally construct when available).
Wasm does not currently support exception handling directly, but that can be emulated by thunking out to JavaScript. Future versions of Wasm will have proper exception handling support.
Upvotes: 2