Skip to content

Commit 2ffe231

Browse files
authored
Minor cleanup of emscripten_setjmp.c. NFC (#21631)
- Move `emscripten_longjmp` down alongside `__wasm_longjmp` where it can live in and `#else` block. - Move `C_LONGJMP` down to where it is used. - Declare `setThrew` in emscripten_internal.h
1 parent d72d722 commit 2ffe231

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

system/lib/compiler-rt/emscripten_exception_builtins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <stdint.h>
1313
#include <threads.h>
1414

15+
#include "emscripten_internal.h"
16+
1517
thread_local uintptr_t __THREW__ = 0;
1618
thread_local int __threwValue = 0;
1719

system/lib/compiler-rt/emscripten_setjmp.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,13 @@
1111
#include <setjmp.h>
1212
#include <threads.h>
1313

14-
extern void setThrew(uintptr_t threw, int value);
15-
16-
#if !defined(__USING_WASM_SJLJ__)
17-
1814
#include "emscripten_internal.h"
1915

20-
void emscripten_longjmp(uintptr_t env, int val) {
21-
// C standard:
22-
// The longjmp function cannot cause the setjmp macro to return
23-
// the value 0; if val is 0, the setjmp macro returns the value 1.
24-
if (val == 0) {
25-
val = 1;
26-
}
27-
setThrew(env, val);
28-
_emscripten_throw_longjmp();
29-
}
30-
#endif
31-
3216
#ifdef __USING_WASM_SJLJ__
3317
struct __WasmLongjmpArgs {
3418
void *env;
3519
int val;
3620
};
37-
38-
// llvm uses `1` for the __c_longjmp tag.
39-
// See https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
40-
#define C_LONGJMP 1
4121
#endif
4222

4323
// jmp_buf should have large enough size and alignment to contain
@@ -69,6 +49,10 @@ uint32_t __wasm_setjmp_test(void* env, void* func_invocation_id) {
6949
}
7050

7151
#ifdef __USING_WASM_SJLJ__
52+
// llvm uses `1` for the __c_longjmp tag.
53+
// See https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
54+
#define C_LONGJMP 1
55+
7256
// Wasm EH allows us to throw and catch multiple values, but that requires
7357
// multivalue support in the toolchain, whch is not reliable at the time.
7458
// TODO Consider switching to throwing two values at the same time later.
@@ -85,4 +69,15 @@ void __wasm_longjmp(void* env, int val) {
8569
arg->val = val;
8670
__builtin_wasm_throw(C_LONGJMP, arg);
8771
}
72+
#else
73+
void emscripten_longjmp(uintptr_t env, int val) {
74+
// C standard:
75+
// The longjmp function cannot cause the setjmp macro to return
76+
// the value 0; if val is 0, the setjmp macro returns the value 1.
77+
if (val == 0) {
78+
val = 1;
79+
}
80+
setThrew(env, val);
81+
_emscripten_throw_longjmp();
82+
}
8883
#endif

system/lib/libc/emscripten_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
extern "C" {
2727
#endif
2828

29+
void setThrew(uintptr_t threw, int value);
30+
2931
// An external JS implementation that is efficient for very large copies, using
3032
// HEAPU8.set()
3133
void _emscripten_memcpy_js(void* __restrict__ dest,

0 commit comments

Comments
 (0)