How do I set setjmp / longjmp to WebAssembly?
I just started looking at WebAssembly MVP and noticed that there is no access to the stack pointer and stack pointer, or even any support for structured exception handling (throw / catch), etc.
Considering that it should be the target of C compilation, it should surely be possible to implement setjmp
and longjmp
, but I can't figure out how it can be done nicely. What does this construction look like in wast?
source to share
Zero cost exception handling is not supported in MVC WebAssembly.
C ++ exception handling and setjmp
/ longjmp
is currently implemented through Emscripten if each try
or "invoke" makes a JavaScript call with continuation of C ++ WebAssembly code. The throw then throws a JavaScript exception which unwinds the stack and processes the "handle" in which the life code (usually destructor calls and blocks catch
) is deployed . This means that each continuation gets a boolean value: an exclusion path or a regular path.
It's super expensive! If LLVM cannot prove that a function call cannot be thrown, then its IR contains an instruction invoke
and Emscripten relies on this to insert the exception handling code. The default in C ++ is that everything can be thrown, so if you look at LLVM IR, there are exceptions at compile time invoke
.
Zero cost of handling exception handling at the moment , so eventually this situation must be resolved. This will be used to implement setjmp
/ longjmp
. This can enable all the defined behavior of setjmp
/ longjmp
, namely: unwinding the stack without calling C ++ destructors. However, this prevents undefined behavior from jumping onto a stack that has already been unwound, which is sometimes used to implement coroutines.
source to share