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