Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 79 lines (69 sloc) 1.552 kb
9c1d230 committing experimental branch content
Laurent Sansonetti authored
1 /* -*-c-*- */
2 /*
3 * from eval.c
4 */
5
6 /* exit */
7
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
8 // TODO: move & lock me into RoxorCore
9 static VALUE at_exit_procs = Qnil;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
10
11 /*
12 * call-seq:
13 * at_exit { block } -> proc
14 *
15 * Converts _block_ to a +Proc+ object (and therefore
16 * binds it at the point of call) and registers it for execution when
17 * the program exits. If multiple handlers are registered, they are
18 * executed in reverse order of registration.
19 *
20 * def do_at_exit(str1)
21 * at_exit { print str1 }
22 * end
23 * at_exit { puts "cruel world" }
24 * do_at_exit("goodbye ")
25 * exit
26 *
27 * <em>produces:</em>
28 *
29 * goodbye cruel world
30 */
31
32 static VALUE
33 rb_f_at_exit(VALUE self, SEL sel)
34 {
35 if (!rb_block_given_p()) {
36 rb_raise(rb_eArgError, "called without a block");
37 }
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
38 VALUE proc = rb_block_proc();
39 rb_ary_push(at_exit_procs, proc);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
40 return proc;
41 }
42
d649cff call at_exit blocks within exception handlers
Laurent Sansonetti authored
43 static VALUE
44 rb_end_proc_call_try(VALUE proc)
45 {
46 return rb_proc_call2(proc, 0, NULL);
47 }
48
49 static VALUE
50 rb_end_proc_call_catch(VALUE data)
51 {
52 rb_vm_print_current_exception();
53 return Qnil;
54 }
55
9c1d230 committing experimental branch content
Laurent Sansonetti authored
56 void
57 rb_exec_end_proc(void)
58 {
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
59 while (true) {
60 const int count = RARRAY_LEN(at_exit_procs);
61 if (count > 0) {
62 VALUE proc = RARRAY_AT(at_exit_procs, count - 1);
63 rb_ary_delete_at(at_exit_procs, count - 1);
d649cff call at_exit blocks within exception handlers
Laurent Sansonetti authored
64 rb_rescue(rb_end_proc_call_try, proc, rb_end_proc_call_catch, 0);
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
65 continue;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
66 }
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
67 break;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
68 }
69 }
70
71 void
72 Init_jump(void)
73 {
cbe906e introduce rb_objc_define_module_function() which mimics the ruby spec
Laurent Sansonetti authored
74 rb_objc_define_module_function(rb_mKernel, "at_exit", rb_f_at_exit, 0);
ac3a5b4 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
Laurent Sansonetti authored
75
76 at_exit_procs = rb_ary_new();
77 GC_RETAIN(at_exit_procs);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
78 }
Something went wrong with that request. Please try again.