Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
runtime.c
1#include "wasm/machine.h"
2#include "wasm/setjmp.h"
3#include "wasm/fiber.h"
4#include "wasm/asyncify.h"
5#include <stdlib.h>
6
7int rb_wasm_rt_start(int (main)(int argc, char **argv), int argc, char **argv) {
8 int result;
9 void *asyncify_buf;
10
11 bool new_fiber_started = false;
12 void *arg0 = NULL, *arg1 = NULL;
13 void (*fiber_entry_point)(void *, void *) = NULL;
14
15 while (1) {
16 if (fiber_entry_point) {
17 fiber_entry_point(arg0, arg1);
18 } else {
19 result = main(argc, argv);
20 }
21
22 // NOTE: it's important to call 'asyncify_stop_unwind' here instead in rb_wasm_handle_jmp_unwind
23 // because unless that, Asyncify inserts another unwind check here and it unwinds to the root frame.
24 asyncify_stop_unwind();
25
26 if ((asyncify_buf = rb_wasm_handle_jmp_unwind()) != NULL) {
27 asyncify_start_rewind(asyncify_buf);
28 continue;
29 }
30 if ((asyncify_buf = rb_wasm_handle_scan_unwind()) != NULL) {
31 asyncify_start_rewind(asyncify_buf);
32 continue;
33 }
34
35 asyncify_buf = rb_wasm_handle_fiber_unwind(&fiber_entry_point, &arg0, &arg1, &new_fiber_started);
36 // Newly starting fiber doesn't have asyncify buffer yet, so don't rewind it for the first time entry
37 if (asyncify_buf) {
38 asyncify_start_rewind(asyncify_buf);
39 continue;
40 } else if (new_fiber_started) {
41 continue;
42 }
43
44 break;
45 }
46 return result;
47}