Ruby: wasm/setjmp.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

setjmp.h
1#ifndef RB_WASM_SUPPORT_SETJMP_H
2#define RB_WASM_SUPPORT_SETJMP_H
3
4#include "ruby/internal/config.h"
5#include <stdbool.h>
6
7#ifndef WASM_SETJMP_STACK_BUFFER_SIZE
8# define WASM_SETJMP_STACK_BUFFER_SIZE 8192
9#endif
10
11 struct __rb_wasm_asyncify_jmp_buf {
12 void* top;
13 void* end;
14 char buffer[WASM_SETJMP_STACK_BUFFER_SIZE];
15};
16
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;
31
32// noinline to avoid breaking Asyncify assumption
33NOINLINE(int _rb_wasm_setjmp(rb_wasm_jmp_buf *env));
34NOINLINE(void _rb_wasm_longjmp(rb_wasm_jmp_buf *env, int payload));
35
36#define rb_wasm_setjmp(env) ((env).state = 0, _rb_wasm_setjmp(&(env)))
37
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())
46
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
49void *rb_wasm_handle_jmp_unwind(void);
50
51
52//
53// POSIX-compatible declarations
54//
55
56typedef rb_wasm_jmp_buf jmp_buf;
57
58#define setjmp(env) rb_wasm_setjmp(env)
59#define longjmp(env, payload) rb_wasm_longjmp(env, payload)
60
61
62typedef void (*rb_wasm_try_catch_func_t)(void *ctx);
63
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 void *stack_pointer;
69 int state;
70};
71
72//
73// Lightweight try-catch API without unwinding to root frame.
74//
75
76void
77rb_wasm_try_catch_init(struct rb_wasm_try_catch *try_catch,
78 rb_wasm_try_catch_func_t try_f,
79 rb_wasm_try_catch_func_t catch_f,
80 void *context);
81
82// Run, catch longjmp thrown by run, and re-catch longjmp thrown by catch, ...
83//
84// 1. run try_f of try_catch struct
85// 2. catch longjmps with the given target jmp_buf or exit
86// 3. run catch_f if not NULL, otherwise exit
87// 4. catch longjmps with the given target jmp_buf or exit
88// 5. repeat from step 3
89//
90// NOTICE: This API assumes that all longjmp targeting the given jmp_buf are NOT called
91// after the function that called this function has exited.
92//
93void
94rb_wasm_try_catch_loop_run(struct rb_wasm_try_catch *try_catch, rb_wasm_jmp_buf *target);
95
96#endif
stdbool.h
C99 shim for <stdbool.h>.
__rb_wasm_asyncify_jmp_buf
Definition setjmp.h:11
rb_wasm_jmp_buf
Definition setjmp.h:17
rb_wasm_try_catch
Definition setjmp.h:64