Fix typos [ci skip]
[ruby-80x24.org.git] / eval_jump.c
blob2ea73b0da32608e2423795d1d0a34f970f406ccd
1 /* -*-c-*- */
2 /*
3 * from eval.c
4 */
6 #include "eval_intern.h"
8 /* exit */
10 void
11 rb_call_end_proc(VALUE data)
13 rb_proc_call(data, rb_ary_new());
17 * call-seq:
18 * at_exit { block } -> proc
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.
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
32 * <em>produces:</em>
34 * goodbye cruel world
37 static VALUE
38 rb_f_at_exit(VALUE _)
40 VALUE proc;
42 if (!rb_block_given_p()) {
43 rb_raise(rb_eArgError, "called without a block");
45 proc = rb_block_proc();
46 rb_set_end_proc(rb_call_end_proc, proc);
47 return proc;
50 struct end_proc_data {
51 void (*func) (VALUE);
52 VALUE data;
53 struct end_proc_data *next;
56 static struct end_proc_data *end_procs, *ephemeral_end_procs;
58 void
59 rb_set_end_proc(void (*func)(VALUE), VALUE data)
61 struct end_proc_data *link = ALLOC(struct end_proc_data);
62 struct end_proc_data **list;
63 rb_thread_t *th = GET_THREAD();
65 if (th->top_wrapper) {
66 list = &ephemeral_end_procs;
68 else {
69 list = &end_procs;
71 link->next = *list;
72 link->func = func;
73 link->data = data;
74 *list = link;
77 void
78 rb_mark_end_proc(void)
80 struct end_proc_data *link;
82 link = end_procs;
83 while (link) {
84 rb_gc_mark(link->data);
85 link = link->next;
87 link = ephemeral_end_procs;
88 while (link) {
89 rb_gc_mark(link->data);
90 link = link->next;
94 static void
95 exec_end_procs_chain(struct end_proc_data *volatile *procs, VALUE *errp)
97 struct end_proc_data volatile endproc;
98 struct end_proc_data *link;
99 VALUE errinfo = *errp;
101 while ((link = *procs) != 0) {
102 *procs = link->next;
103 endproc = *link;
104 xfree(link);
105 (*endproc.func) (endproc.data);
106 *errp = errinfo;
110 static void
111 rb_ec_exec_end_proc(rb_execution_context_t * ec)
113 enum ruby_tag_type state;
114 volatile VALUE errinfo = ec->errinfo;
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);
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;
129 EC_POP_TAG();
131 ec->errinfo = errinfo;
134 void
135 Init_jump(void)
137 rb_define_global_function("at_exit", rb_f_at_exit, 0);