Ruby  2.0.0p648(2015-12-16revision53162)
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
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
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();
47  return proc;
48 }
49 
50 struct end_proc_data {
51  void (*func) ();
53  int safe;
55 };
56 
58 
59 void
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) {
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
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  }
90  while (link) {
91  rb_gc_mark(link->data);
92  link = link->next;
93  }
94 }
95 
96 void
98 {
99  struct end_proc_data volatile endproc;
100  struct end_proc_data volatile *link;
101  int status;
102  volatile int safe = rb_safe_level();
103  rb_thread_t *th = GET_THREAD();
104  volatile VALUE errinfo = th->errinfo;
105 
106  while (ephemeral_end_procs) {
108  ephemeral_end_procs = link->next;
109  endproc = *link;
110  xfree((void *)link);
111  link = &endproc;
112 
113  PUSH_TAG();
114  if ((status = EXEC_TAG()) == 0) {
116  (*link->func) (link->data);
117  }
118  POP_TAG();
119  if (status) {
120  error_handle(status);
121  if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
122  }
123  }
124 
125  while (end_procs) {
126  link = end_procs;
127  end_procs = link->next;
128  endproc = *link;
129  xfree((void *)link);
130  link = &endproc;
131 
132  PUSH_TAG();
133  if ((status = EXEC_TAG()) == 0) {
135  (*link->func) (link->data);
136  }
137  POP_TAG();
138  if (status) {
139  error_handle(status);
140  if (!NIL_P(th->errinfo)) errinfo = th->errinfo;
141  }
142  }
143 
145  th->errinfo = errinfo;
146 }
147 
148 void
150 {
152 }
void rb_exec_end_proc(void)
Definition: eval_jump.c:97
void Init_jump(void)
Definition: eval_jump.c:149
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
void rb_gc_mark(VALUE ptr)
Definition: gc.c:2600
#define PUSH_TAG()
Definition: eval_intern.h:136
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1526
void rb_mark_end_proc(void)
Definition: eval_jump.c:80
static int error_handle(int ex)
Definition: eval_error.c:240
int rb_block_given_p(void)
Definition: eval.c:672
#define EXEC_TAG()
Definition: eval_intern.h:141
VALUE rb_ary_new(void)
Definition: array.c:424
#define NIL_P(v)
Definition: ruby.h:446
static struct end_proc_data * ephemeral_end_procs
Definition: eval_jump.c:57
#define POP_TAG()
Definition: eval_intern.h:137
int link(const char *, const char *)
Definition: win32.c:4245
#define ALLOC(type)
Definition: ruby.h:1224
void rb_call_end_proc(VALUE data)
Definition: eval_jump.c:11
void(* func)()
Definition: eval_jump.c:51
VALUE data
Definition: eval_jump.c:52
unsigned long VALUE
Definition: ruby.h:104
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:571
void xfree(void *)
void rb_set_end_proc(void(*func)(VALUE), VALUE data)
Definition: eval_jump.c:60
void rb_set_safe_level_force(int)
Definition: safe.c:34
static struct end_proc_data * end_procs
Definition: eval_jump.c:57
VALUE top_wrapper
Definition: vm_core.h:521
VALUE rb_block_proc(void)
Definition: proc.c:458
struct rb_encoding_entry * list
Definition: encoding.c:50
#define rb_safe_level()
Definition: tcltklib.c:94
struct end_proc_data * next
Definition: eval_jump.c:54
static VALUE rb_f_at_exit(void)
Definition: eval_jump.c:38
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:890
VALUE rb_eArgError
Definition: error.c:517