Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 146 lines (128 sloc) 2.777 kb
ace2b68a » nobu
2007-06-25 * eval_error.ci, eval_jump.ci, eval_method.ci, eval_safe.ci: c-mode.
1 /* -*-c-*- */
a3e1b1ce » ko1
2006-12-31 * Merge YARV
2 /*
3 * from eval.c
4 */
5
6 #include "eval_intern.h"
7
8 /* exit */
9
314bef7e » ko1
2007-12-20 * insnhelper.ci, vm.c, vm_core.h: change interface of
10 void
11 rb_call_end_proc(VALUE data)
a3e1b1ce » ko1
2006-12-31 * Merge YARV
12 {
ed4139e3 » ko1
2008-06-10 * include/ruby/intern.h, proc.c: revert rb_proc_call() and
13 rb_proc_call(data, rb_ary_new());
a3e1b1ce » ko1
2006-12-31 * Merge YARV
14 }
15
16 /*
17 * call-seq:
18 * at_exit { block } -> proc
99d65b14 » nobu
2007-06-05 * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
19 *
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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.
99d65b14 » nobu
2007-06-05 * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
24 *
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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
99d65b14 » nobu
2007-06-05 * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
31 *
a3e1b1ce » ko1
2006-12-31 * Merge YARV
32 * <em>produces:</em>
99d65b14 » nobu
2007-06-05 * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
33 *
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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();
314bef7e » ko1
2007-12-20 * insnhelper.ci, vm.c, vm_core.h: change interface of
46 rb_set_end_proc(rb_call_end_proc, proc);
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
57 static struct end_proc_data *end_procs, *ephemeral_end_procs;
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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;
9987d53e » ko1
2007-02-25 * yarvcore.h: add rb_thread_t#top_wrapper, top_self.
64 rb_thread_t *th = GET_THREAD();
a3e1b1ce » ko1
2006-12-31 * Merge YARV
65
9987d53e » ko1
2007-02-25 * yarvcore.h: add rb_thread_t#top_wrapper, top_self.
66 if (th->top_wrapper) {
a3e1b1ce » ko1
2006-12-31 * Merge YARV
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 }
95
96 void
97 rb_exec_end_proc(void)
98 {
cfe9dcb8 » akr
2009-02-28 * eval_error.c (error_print): use volatile to suppress warnings.
99 struct end_proc_data *volatile link;
a3e1b1ce » ko1
2006-12-31 * Merge YARV
100 int status;
101 volatile int safe = rb_safe_level();
b19f3a8a » nobu
2012-03-12 * eval_jump.c (rb_exec_end_proc): remember the latest exit status.
102 rb_thread_t *th = GET_THREAD();
bd377449 » nobu
2012-03-18 volatile errinfo
103 volatile VALUE errinfo = th->errinfo;
a3e1b1ce » ko1
2006-12-31 * Merge YARV
104
105 while (ephemeral_end_procs) {
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
106 link = ephemeral_end_procs;
107 ephemeral_end_procs = link->next;
108
109 PUSH_TAG();
110 if ((status = EXEC_TAG()) == 0) {
111 rb_set_safe_level_force(link->safe);
112 (*link->func) (link->data);
a3e1b1ce » ko1
2006-12-31 * Merge YARV
113 }
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
114 POP_TAG();
115 if (status) {
116 error_handle(status);
b19f3a8a » nobu
2012-03-12 * eval_jump.c (rb_exec_end_proc): remember the latest exit status.
117 if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
118 }
119 xfree(link);
a3e1b1ce » ko1
2006-12-31 * Merge YARV
120 }
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
121
a3e1b1ce » ko1
2006-12-31 * Merge YARV
122 while (end_procs) {
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
123 link = end_procs;
124 end_procs = link->next;
125
126 PUSH_TAG();
127 if ((status = EXEC_TAG()) == 0) {
128 rb_set_safe_level_force(link->safe);
129 (*link->func) (link->data);
130 }
131 POP_TAG();
132 if (status) {
133 error_handle(status);
b19f3a8a » nobu
2012-03-12 * eval_jump.c (rb_exec_end_proc): remember the latest exit status.
134 if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
a3e1b1ce » ko1
2006-12-31 * Merge YARV
135 }
36a0a1a3 » kosaki
2011-02-16 * eval_jump.c (rb_exec_end_proc): changed at_exit and END proc
136 xfree(link);
a3e1b1ce » ko1
2006-12-31 * Merge YARV
137 }
138 rb_set_safe_level_force(safe);
b19f3a8a » nobu
2012-03-12 * eval_jump.c (rb_exec_end_proc): remember the latest exit status.
139 th->errinfo = errinfo;
a3e1b1ce » ko1
2006-12-31 * Merge YARV
140 }
141
142 void
c4941852 » aamine
2007-03-08 * compile.c: iseq_compile -> rb_iseq_compile.
143 Init_jump(void)
a3e1b1ce » ko1
2006-12-31 * Merge YARV
144 {
145 rb_define_global_function("at_exit", rb_f_at_exit, 0);
146 }
Something went wrong with that request. Please try again.