Ruby 4.1.0dev (2026-09-05 revision a946c1bd8e5e7e03e938fe5bb4df942affa95ec6)
eval_jump.c (a946c1bd8e5e7e03e938fe5bb4df942affa95ec6)
1/* -*-c-*- */
2/*
3 * from eval.c
4 */
5
6#include "eval_intern.h"
7#include "internal/ractor.h"
8
9/* exit */
10
11void
12rb_call_end_proc(VALUE data)
13{
14 rb_proc_call(data, rb_ary_new());
15}
16
17/*
18 * call-seq:
19 * at_exit { block } -> proc
20 *
21 * Converts _block_ to a +Proc+ object (and therefore
22 * binds it at the point of call) and registers it for execution when
23 * the program exits. If multiple handlers are registered, they are
24 * executed in reverse order of registration.
25 *
26 * def do_at_exit(str1)
27 * at_exit { print str1 }
28 * end
29 * at_exit { puts "cruel world" }
30 * do_at_exit("goodbye ")
31 * exit
32 *
33 * <em>produces:</em>
34 *
35 * goodbye cruel world
36 */
37
38static VALUE
39rb_f_at_exit(VALUE _)
40{
41 VALUE proc;
42
43 if (!rb_block_given_p()) {
44 rb_raise(rb_eArgError, "called without a block");
45 }
46 rb_ractor_ensure_main_ractor("can not call at_exit from non-main Ractors");
47 proc = rb_block_proc();
48 rb_set_end_proc(rb_call_end_proc, proc);
49 return proc;
50}
51
53 void (*func) (VALUE);
54 VALUE data;
55 struct end_proc_data *next;
56};
57
58static struct end_proc_data *end_procs, *ephemeral_end_procs;
59
60void
61rb_set_end_proc(void (*func)(VALUE), VALUE data)
62{
63 struct end_proc_data *link = ALLOC(struct end_proc_data);
64 struct end_proc_data **list;
65 rb_thread_t *th = GET_THREAD();
66
67 if (th->top_wrapper) {
68 list = &ephemeral_end_procs;
69 }
70 else {
71 list = &end_procs;
72 }
73 link->next = *list;
74 link->func = func;
75 link->data = data;
76 *list = link;
77}
78
79void
80rb_mark_end_proc(void)
81{
82 struct end_proc_data *link;
83
84 link = end_procs;
85 while (link) {
86 rb_gc_mark(link->data);
87 link = link->next;
88 }
89 link = ephemeral_end_procs;
90 while (link) {
91 rb_gc_mark(link->data);
92 link = link->next;
93 }
94}
95
96static void
97exec_end_procs_chain(struct end_proc_data *volatile *procs, VALUE *errp)
98{
99 struct end_proc_data volatile endproc;
100 struct end_proc_data *link;
101 VALUE errinfo = *errp;
102
103 while ((link = *procs) != 0) {
104 *procs = link->next;
105 endproc = *link;
106 SIZED_FREE(link);
107 (*endproc.func) (endproc.data);
108 *errp = errinfo;
109 }
110}
111
112static void
113rb_ec_exec_end_proc(rb_execution_context_t * ec)
114{
115 enum ruby_tag_type state;
116 volatile VALUE errinfo = ec->errinfo;
117 /* Share only among END/at_exit handlers to avoid repeated causes. */
118 VALUE shown_causes = 0;
119 volatile bool finished = false;
120
121 while (!finished) {
122 EC_PUSH_TAG(ec);
123 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
124 exec_end_procs_chain(&ephemeral_end_procs, &ec->errinfo);
125 exec_end_procs_chain(&end_procs, &ec->errinfo);
126 finished = true;
127 }
128 EC_POP_TAG();
129 if (state != TAG_NONE) {
130 VALUE err = ec->errinfo;
131 add_shown_cause(errinfo, &shown_causes);
132 error_handle_with_shown_causes(ec, err, state, &shown_causes);
133 if (!NIL_P(err)) errinfo = err;
134 }
135 }
136
137 ec->errinfo = errinfo;
138}
139
140void
141Init_jump(void)
142{
143 rb_define_global_function("at_exit", rb_f_at_exit, 0);
144}
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1033
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define NIL_P
Old name of RB_NIL_P.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_proc_call(VALUE recv, VALUE args)
Evaluates the passed proc with the passed arguments.
Definition proc.c:1738
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:1575
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40