[Bug #20566] Mention out-of-range argument cases in `String#<<`
[ruby.git] / wasm / setjmp.h
blobcc14df33be11400c12b4540926a215fca408ae4f
1 #ifndef RB_WASM_SUPPORT_SETJMP_H
2 #define RB_WASM_SUPPORT_SETJMP_H
4 #include "ruby/internal/config.h"
5 #include <stdbool.h>
7 #ifndef WASM_SETJMP_STACK_BUFFER_SIZE
8 # define WASM_SETJMP_STACK_BUFFER_SIZE 6144
9 #endif
11 struct __rb_wasm_asyncify_jmp_buf {
12 void* top;
13 void* end;
14 char buffer[WASM_SETJMP_STACK_BUFFER_SIZE];
17 typedef struct {
18 // Internal Asyncify buffer space to save execution context
19 struct __rb_wasm_asyncify_jmp_buf setjmp_buf;
20 // Internal Asyncify buffer space used while unwinding from longjmp
21 // but never used for rewinding.
22 struct __rb_wasm_asyncify_jmp_buf *longjmp_buf_ptr;
23 // Used to save top address of Asyncify stack `setjmp_buf`, which is
24 // overwritten during first rewind.
25 void *dst_buf_top;
26 // A payload value given by longjmp and returned by setjmp for the second time
27 int payload;
28 // Internal state field
29 int state;
30 } rb_wasm_jmp_buf;
32 // noinline to avoid breaking Asyncify assumption
33 NOINLINE(int _rb_wasm_setjmp(rb_wasm_jmp_buf *env));
34 NOINLINE(void _rb_wasm_longjmp(rb_wasm_jmp_buf *env, int payload));
36 #define rb_wasm_setjmp(env) ((env).state = 0, _rb_wasm_setjmp(&(env)))
38 // NOTE: Why is `_rb_wasm_longjmp` not `noreturn`? Why put `unreachable` in the call site?
39 // Asyncify expects that `_rb_wasm_longjmp` returns its control, and Asyncify inserts a return
40 // for unwinding after the call. This means that "`_rb_wasm_longjmp` returns its control but the
41 // next line in the caller (C level) won't be executed".
42 // On the other hand, `noreturn` means the callee won't return its control to the caller,
43 // so compiler can assume that a function with the attribute won't reach the end of the function.
44 // Therefore `_rb_wasm_longjmp`'s semantics is not exactly same as `noreturn`.
45 #define rb_wasm_longjmp(env, payload) (_rb_wasm_longjmp(&env, payload), __builtin_unreachable())
47 // Returns the Asyncify buffer of next rewinding if unwound for setjmp capturing or longjmp.
48 // Used by the top level Asyncify handling in wasm/runtime.c
49 void *rb_wasm_handle_jmp_unwind(void);
53 // POSIX-compatible declarations
56 typedef rb_wasm_jmp_buf jmp_buf;
58 #define setjmp(env) rb_wasm_setjmp(env)
59 #define longjmp(env, payload) rb_wasm_longjmp(env, payload)
62 typedef void (*rb_wasm_try_catch_func_t)(void *ctx);
64 struct rb_wasm_try_catch {
65 rb_wasm_try_catch_func_t try_f;
66 rb_wasm_try_catch_func_t catch_f;
67 void *context;
68 int state;
72 // Lightweight try-catch API without unwinding to root frame.
75 void
76 rb_wasm_try_catch_init(struct rb_wasm_try_catch *try_catch,
77 rb_wasm_try_catch_func_t try_f,
78 rb_wasm_try_catch_func_t catch_f,
79 void *context);
81 // Run, catch longjmp thrown by run, and re-catch longjmp thrown by catch, ...
83 // 1. run try_f of try_catch struct
84 // 2. catch longjmps with the given target jmp_buf or exit
85 // 3. run catch_f if not NULL, otherwise exit
86 // 4. catch longjmps with the given target jmp_buf or exit
87 // 5. repeat from step 3
89 // NOTICE: This API assumes that all longjmp targeting the given jmp_buf are NOT called
90 // after the function that called this function has exited.
92 void
93 rb_wasm_try_catch_loop_run(struct rb_wasm_try_catch *try_catch, rb_wasm_jmp_buf *target);
95 #endif