Skip to content
This repository
Newer
Older
100644 86 lines (76 sloc) 1.875 kb
95957256 » Laurent Sansonetti
2011-01-15 update copyrights to 2011
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
3 *
4 * Copyright (C) 2007-2011, Apple Inc. All rights reserved.
5 * Copyright (C) 1993-2007 Yukihiro Matsumoto
6 * Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
7 * Copyright (C) 2000 Information-technology Promotion Agency, Japan
8 */
9
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
10 /*
11 * from eval.c
12 */
13
14 /* exit */
15
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
16 // TODO: move & lock me into RoxorCore
17 static VALUE at_exit_procs = Qnil;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
18
19 /*
20 * call-seq:
21 * at_exit { block } -> proc
22 *
23 * Converts _block_ to a +Proc+ object (and therefore
24 * binds it at the point of call) and registers it for execution when
25 * the program exits. If multiple handlers are registered, they are
26 * executed in reverse order of registration.
27 *
28 * def do_at_exit(str1)
29 * at_exit { print str1 }
30 * end
31 * at_exit { puts "cruel world" }
32 * do_at_exit("goodbye ")
33 * exit
34 *
35 * <em>produces:</em>
36 *
37 * goodbye cruel world
38 */
39
40 static VALUE
41 rb_f_at_exit(VALUE self, SEL sel)
42 {
43 if (!rb_block_given_p()) {
44 rb_raise(rb_eArgError, "called without a block");
45 }
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
46 VALUE proc = rb_block_proc();
47 rb_ary_push(at_exit_procs, proc);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
48 return proc;
49 }
50
d649cff3 » Laurent Sansonetti
2010-01-18 call at_exit blocks within exception handlers
51 static VALUE
52 rb_end_proc_call_try(VALUE proc)
53 {
54 return rb_proc_call2(proc, 0, NULL);
55 }
56
57 static VALUE
e297457c » Laurent Sansonetti
2011-01-28 fix a bug in #at_exit when the process would abort if a block raised …
58 rb_end_proc_call_catch(VALUE data, VALUE exc)
d649cff3 » Laurent Sansonetti
2010-01-18 call at_exit blocks within exception handlers
59 {
e297457c » Laurent Sansonetti
2011-01-28 fix a bug in #at_exit when the process would abort if a block raised …
60 rb_vm_print_exception(exc);
d649cff3 » Laurent Sansonetti
2010-01-18 call at_exit blocks within exception handlers
61 return Qnil;
62 }
63
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
64 void
65 rb_exec_end_proc(void)
66 {
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
67 while (true) {
68 const int count = RARRAY_LEN(at_exit_procs);
69 if (count > 0) {
70 VALUE proc = RARRAY_AT(at_exit_procs, count - 1);
71 rb_ary_delete_at(at_exit_procs, count - 1);
d649cff3 » Laurent Sansonetti
2010-01-18 call at_exit blocks within exception handlers
72 rb_rescue(rb_end_proc_call_try, proc, rb_end_proc_call_catch, 0);
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
73 continue;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
74 }
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
75 break;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
76 }
77 }
78
79 void
80 Init_jump(void)
81 {
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
82 rb_objc_define_module_function(rb_mKernel, "at_exit", rb_f_at_exit, 0);
ac3a5b45 » Laurent Sansonetti
2009-10-07 fix for <rdar://problem/7281043> Adding frameworks after requiring a …
83
84 at_exit_procs = rb_ary_new();
85 GC_RETAIN(at_exit_procs);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
86 }
Something went wrong with that request. Please try again.