Skip to content

Commit 5ee81b5

Browse files
committed
provide __wasm_setjmp/__wasm_setjmp_test for emscripten sjlj as well
also, replace __wasm_longjmp with my version as well.
1 parent d5d878b commit 5ee81b5

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

system/lib/compiler-rt/emscripten_setjmp.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,20 @@ void emscripten_longjmp(uintptr_t env, int val) {
7474
#endif
7575

7676
#ifdef __USING_WASM_SJLJ__
77-
7877
struct __WasmLongjmpArgs {
7978
void *env;
8079
int val;
8180
};
82-
83-
thread_local struct __WasmLongjmpArgs __wasm_longjmp_args;
84-
85-
// llvm uses `1` for the __c_longjmp tag.
86-
// See https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
87-
#define C_LONGJMP 1
88-
89-
// Wasm EH allows us to throw and catch multiple values, but that requires
90-
// multivalue support in the toolchain, whch is not reliable at the time.
91-
// TODO Consider switching to throwing two values at the same time later.
92-
void __wasm_longjmp(void *env, int val) {
93-
__wasm_longjmp_args.env = env;
94-
/*
95-
 * C standard:
96-
 * The longjmp function cannot cause the setjmp macro to return
97-
 * the value 0; if val is 0, the setjmp macro returns the value 1.
98-
 */
99-
if (val == 0) {
100-
val = 1;
101-
}
102-
__wasm_longjmp_args.val = val;
103-
__builtin_wasm_throw(C_LONGJMP, &__wasm_longjmp_args);
104-
}
81+
#endif
10582

10683
// jmp_buf should have large enough size and alignment to contain
10784
// this structure.
10885
struct jmp_buf_impl {
10986
void* func_invocation_id;
11087
uint32_t label;
88+
#ifdef __USING_WASM_SJLJ__
89+
struct __WasmLongjmpArgs arg;
90+
#endif
11191
};
11292

11393
void __wasm_setjmp(void* env, uint32_t label, void* func_invocation_id) {
@@ -127,4 +107,23 @@ uint32_t __wasm_setjmp_test(void* env, void* func_invocation_id) {
127107
}
128108
return 0;
129109
}
110+
111+
#ifdef __USING_WASM_SJLJ__
112+
void
113+
__wasm_longjmp(void *env, int val)
114+
{
115+
struct jmp_buf_impl *jb = env;
116+
struct __WasmLongjmpArgs *arg = &jb->arg;
117+
/*
118+
* C standard says:
119+
* The longjmp function cannot cause the setjmp macro to return
120+
* the value 0; if val is 0, the setjmp macro returns the value 1.
121+
*/
122+
if (val == 0) {
123+
val = 1;
124+
}
125+
arg->env = env;
126+
arg->val = val;
127+
__builtin_wasm_throw(1, arg); /* 1 == C_LONGJMP */
128+
}
130129
#endif

0 commit comments

Comments
 (0)