Skip to content
Newer
Older
100644 153 lines (137 sloc) 2.83 KB
511dc44 initial import
Laurent Sansonetti authored
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(void)
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 int safe;
54 struct end_proc_data *next;
55 };
56
57 static struct end_proc_data *end_procs, *ephemeral_end_procs, *tmp_end_procs;
58
59 void
60 rb_set_end_proc(void (*func)(VALUE), VALUE data)
61 {
62 struct end_proc_data *link = ALLOC(struct end_proc_data);
63 struct end_proc_data **list;
64 rb_thread_t *th = GET_THREAD();
65
66 if (th->top_wrapper) {
67 list = &ephemeral_end_procs;
68 }
69 else {
70 list = &end_procs;
71 }
72 link->next = *list;
73 link->func = func;
74 link->data = data;
75 link->safe = rb_safe_level();
76 *list = link;
77 }
78
79 void
80 rb_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 link = tmp_end_procs;
95 while (link) {
96 rb_gc_mark(link->data);
97 link = link->next;
98 }
99 }
100
101 void
102 rb_exec_end_proc(void)
103 {
104 struct end_proc_data *link, *tmp;
105 int status;
106 volatile int safe = rb_safe_level();
107
108 while (ephemeral_end_procs) {
109 tmp_end_procs = link = ephemeral_end_procs;
110 ephemeral_end_procs = 0;
111 while (link) {
112 PUSH_TAG();
113 if ((status = EXEC_TAG()) == 0) {
114 rb_set_safe_level_force(link->safe);
115 (*link->func) (link->data);
116 }
117 POP_TAG();
118 if (status) {
119 error_handle(status);
120 }
121 tmp = link;
122 tmp_end_procs = link = link->next;
123 free(tmp);
124 }
125 }
126 while (end_procs) {
127 tmp_end_procs = link = end_procs;
128 end_procs = 0;
129 while (link) {
130 PUSH_TAG();
131 if ((status = EXEC_TAG()) == 0) {
132 rb_set_safe_level_force(link->safe);
133 (*link->func) (link->data);
134 }
135 POP_TAG();
136 if (status) {
137 error_handle(status);
138 }
139 tmp = link;
140 tmp_end_procs = link = link->next;
141 xfree(tmp);
142 }
143 }
144 rb_set_safe_level_force(safe);
145 }
146
147 void
148 Init_jump(void)
149 {
150 rb_define_global_function("at_exit", rb_f_at_exit, 0);
151 GC_ROOT(&end_procs);
152 }
Something went wrong with that request. Please try again.