forked from MacRuby/MacRuby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_jump.c
87 lines (77 loc) · 1.9 KB
/
eval_jump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012-2013, The MacRuby Team. All rights reserved.
* Copyright (C) 2007-2011, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*/
/*
* from eval.c
*/
/* exit */
// TODO: move & lock me into RoxorCore
static VALUE at_exit_procs = Qnil;
/*
* call-seq:
* at_exit { block } -> proc
*
* Converts _block_ to a +Proc+ object (and therefore
* binds it at the point of call) and registers it for execution when
* the program exits. If multiple handlers are registered, they are
* executed in reverse order of registration.
*
* def do_at_exit(str1)
* at_exit { print str1 }
* end
* at_exit { puts "cruel world" }
* do_at_exit("goodbye ")
* exit
*
* <em>produces:</em>
*
* goodbye cruel world
*/
static VALUE
rb_f_at_exit(VALUE self, SEL sel)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "called without a block");
}
VALUE proc = rb_block_proc();
rb_ary_push(at_exit_procs, proc);
return proc;
}
static VALUE
rb_end_proc_call_try(VALUE proc)
{
return rb_proc_call2(proc, 0, NULL);
}
static VALUE
rb_end_proc_call_catch(VALUE data, VALUE exc)
{
rb_vm_print_exception(exc);
return Qnil;
}
void
rb_exec_end_proc(void)
{
while (true) {
const int count = RARRAY_LEN(at_exit_procs);
if (count > 0) {
VALUE proc = RARRAY_AT(at_exit_procs, count - 1);
rb_ary_delete_at(at_exit_procs, count - 1);
rb_rescue(rb_end_proc_call_try, proc, rb_end_proc_call_catch, 0);
continue;
}
break;
}
}
void
Init_jump(void)
{
rb_objc_define_module_function(rb_mKernel, "at_exit", rb_f_at_exit, 0);
at_exit_procs = rb_ary_new();
GC_RETAIN(at_exit_procs);
}