Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

eval_jump.c
Go to the documentation of this file.
1 /* -*-c-*- */
2 /*
3 * from eval.c
4 */
5
6 #include "eval_intern.h"
7
8 /* exit */
9
10 void
11 rb_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
37 static VALUE
38 rb_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
50 struct end_proc_data {
51 void (*func) ();
52 VALUE data;
53 struct end_proc_data *next;
54 };
55
56 static struct end_proc_data *end_procs, *ephemeral_end_procs;
57
58 void
59 rb_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
77 void
78 rb_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
94 static void
95 exec_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
110 static void
111 rb_ec_exec_end_proc(rb_execution_context_t * ec)
112 {
113 enum ruby_tag_type state;
114 volatile VALUE errinfo = ec->errinfo;
115
116 EC_PUSH_TAG(ec);
117 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
118 again:
119 exec_end_procs_chain(&ephemeral_end_procs, &ec->errinfo);
120 exec_end_procs_chain(&end_procs, &ec->errinfo);
121 }
122 else {
123 EC_TMPPOP_TAG();
124 error_handle(ec, state);
125 if (!NIL_P(ec->errinfo)) errinfo = ec->errinfo;
126 EC_REPUSH_TAG();
127 goto again;
128 }
129 EC_POP_TAG();
130
131 ec->errinfo = errinfo;
132 }
133
134 void
135 Init_jump(void)
136 {
137 rb_define_global_function("at_exit", rb_f_at_exit, 0);
138 }
void
void
Definition: rb_mjit_min_header-2.7.0.h:13273
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:897
end_proc_data
Definition: eval_jump.c:50
rb_thread_struct::top_wrapper
VALUE top_wrapper
Definition: vm_core.h:924
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
Init_jump
void Init_jump(void)
Definition: eval_jump.c:135
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1787
rb_mark_end_proc
void rb_mark_end_proc(void)
Definition: eval_jump.c:78
EC_REPUSH_TAG
#define EC_REPUSH_TAG()
Definition: eval_intern.h:144
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
EC_POP_TAG
#define EC_POP_TAG()
Definition: eval_intern.h:137
rb_execution_context_struct::errinfo
VALUE errinfo
Definition: vm_core.h:875
EC_TMPPOP_TAG
#define EC_TMPPOP_TAG()
Definition: eval_intern.h:141
EC_EXEC_TAG
#define EC_EXEC_TAG()
Definition: eval_intern.h:181
ALLOC
#define ALLOC(type)
Definition: ruby.h:1664
end_proc_data::next
struct end_proc_data * next
Definition: eval_jump.c:53
end_proc_data::func
void(* func)()
Definition: eval_jump.c:51
end_proc_data::data
VALUE data
Definition: eval_jump.c:52
rb_set_end_proc
void rb_set_end_proc(void(*func)(VALUE), VALUE data)
Definition: eval_jump.c:59
list
struct rb_encoding_entry * list
Definition: encoding.c:56
link
int link(const char *, const char *)
Definition: win32.c:4931
rb_call_end_proc
void rb_call_end_proc(VALUE data)
Definition: eval_jump.c:11
GET_THREAD
#define GET_THREAD()
Definition: vm_core.h:1765
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
TAG_NONE
#define TAG_NONE
Definition: vm_core.h:197
xfree
#define xfree
Definition: defines.h:216
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:5212
_
#define _(args)
Definition: dln.h:28
EC_PUSH_TAG
#define EC_PUSH_TAG(ec)
Definition: eval_intern.h:130
eval_intern.h
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
rb_proc_call
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:966
rb_thread_struct
Definition: vm_core.h:910
ruby_tag_type
ruby_tag_type
Definition: vm_core.h:184
rb_execution_context_struct
Definition: vm_core.h:843
rb_block_proc
VALUE rb_block_proc(void)
Definition: proc.c:837