Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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(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;
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 }
95
96 void
97 rb_exec_end_proc(void)
98 {
99 struct end_proc_data *volatile link;
100 int status;
101 volatile int safe = rb_safe_level();
102 rb_thread_t *th = GET_THREAD();
103 volatile VALUE errinfo = th->errinfo;
104
105 while (ephemeral_end_procs) {
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);
113 }
114 POP_TAG();
115 if (status) {
116 error_handle(status);
117 if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
118 }
119 xfree(link);
120 }
121
122 while (end_procs) {
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);
134 if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
135 }
136 xfree(link);
137 }
138 rb_set_safe_level_force(safe);
139 th->errinfo = errinfo;
140 }
141
142 void
143 Init_jump(void)
144 {
145 rb_define_global_function("at_exit", rb_f_at_exit, 0);
146 }
data
VALUE data
Definition: tcltklib.c:3368
ALLOC
#define ALLOC(type)
th
static VALUE VALUE th
Definition: tcltklib.c:2948
proc
VALUE proc
Definition: tcltklib.c:2959
func
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
rb_exec_end_proc
void rb_exec_end_proc(void)
Definition: eval_jump.c:97
eval_intern.h
status
int status
Definition: tcltklib.c:2197
Init_jump
void Init_jump(void)
Definition: eval_jump.c:143
NIL_P
NIL_P(eventloop_thread)
Definition: tcltklib.c:4068
safe
int safe
Definition: tcltklib.c:6404
xfree
#define xfree
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
PUSH_TAG
#define PUSH_TAG()
Definition: eval_intern.h:108
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1526
rb_thread_struct
Definition: vm_core.h:493
rb_block_proc
VALUE rb_block_proc(void)
Definition: proc.c:479
rb_mark_end_proc
void rb_mark_end_proc(void)
Definition: eval_jump.c:80
error_handle
static int error_handle(int ex)
Definition: eval_error.c:239
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:672
EXEC_TAG
#define EXEC_TAG()
Definition: eval_intern.h:113
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:424
rb_gc_mark
void rb_gc_mark(VALUE)
Definition: gc.c:2598
ephemeral_end_procs
static struct end_proc_data * ephemeral_end_procs
Definition: eval_jump.c:57
POP_TAG
#define POP_TAG()
Definition: eval_intern.h:109
link
int link(const char *, const char *)
Definition: win32.c:4235
rb_call_end_proc
void rb_call_end_proc(VALUE data)
Definition: eval_jump.c:11
void
VP_EXPORT void
Definition: bigdecimal.c:5083
end_proc_data::data
VALUE data
Definition: eval_jump.c:52
rb_set_safe_level_force
void rb_set_safe_level_force(int)
Definition: safe.c:34
rb_set_end_proc
void rb_set_end_proc(void(*func)(VALUE), VALUE data)
Definition: eval_jump.c:60
end_procs
static struct end_proc_data * end_procs
Definition: eval_jump.c:57
end_proc_data
Definition: eval_jump.c:50
rb_thread_struct::top_wrapper
VALUE top_wrapper
Definition: vm_core.h:521
rb_proc_call
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:592
end_proc_data::safe
int safe
Definition: eval_jump.c:53
end_proc_data::func
void(* func)()
Definition: eval_jump.c:51
list
struct rb_encoding_entry * list
Definition: encoding.c:50
rb_safe_level
#define rb_safe_level()
Definition: tcltklib.c:94
VALUE
unsigned long VALUE
Definition: ripper.y:104
end_proc_data::next
struct end_proc_data * next
Definition: eval_jump.c:54
rb_thread_struct::errinfo
VALUE errinfo
Definition: vm_core.h:542
rb_f_at_exit
static VALUE rb_f_at_exit(void)
Definition: eval_jump.c:38
GET_THREAD
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:883
rb_eArgError
VALUE rb_eArgError
Definition: error.c:512