2007-06-25 04:45:48 +04:00
|
|
|
/* -*-c-*- */
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* from eval.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "eval_intern.h"
|
|
|
|
|
|
|
|
/* exit */
|
|
|
|
|
2007-12-20 10:07:35 +03:00
|
|
|
void
|
|
|
|
rb_call_end_proc(VALUE data)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-06-10 20:33:51 +04:00
|
|
|
rb_proc_call(data, rb_ary_new());
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* at_exit { block } -> proc
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* 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.
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* def do_at_exit(str1)
|
|
|
|
* at_exit { print str1 }
|
|
|
|
* end
|
|
|
|
* at_exit { puts "cruel world" }
|
|
|
|
* do_at_exit("goodbye ")
|
|
|
|
* exit
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* goodbye cruel world
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2019-08-28 12:19:11 +03:00
|
|
|
rb_f_at_exit(VALUE _)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
VALUE proc;
|
|
|
|
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_raise(rb_eArgError, "called without a block");
|
|
|
|
}
|
|
|
|
proc = rb_block_proc();
|
2007-12-20 10:07:35 +03:00
|
|
|
rb_set_end_proc(rb_call_end_proc, proc);
|
2006-12-31 18:02:22 +03:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct end_proc_data {
|
2021-05-06 18:04:36 +03:00
|
|
|
void (*func) (VALUE);
|
2006-12-31 18:02:22 +03:00
|
|
|
VALUE data;
|
|
|
|
struct end_proc_data *next;
|
|
|
|
};
|
|
|
|
|
2011-02-16 14:42:03 +03:00
|
|
|
static struct end_proc_data *end_procs, *ephemeral_end_procs;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_set_end_proc(void (*func)(VALUE), VALUE data)
|
|
|
|
{
|
|
|
|
struct end_proc_data *link = ALLOC(struct end_proc_data);
|
|
|
|
struct end_proc_data **list;
|
2007-02-25 19:29:26 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-02-25 19:29:26 +03:00
|
|
|
if (th->top_wrapper) {
|
2006-12-31 18:02:22 +03:00
|
|
|
list = &ephemeral_end_procs;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list = &end_procs;
|
|
|
|
}
|
|
|
|
link->next = *list;
|
|
|
|
link->func = func;
|
|
|
|
link->data = data;
|
|
|
|
*list = link;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_mark_end_proc(void)
|
|
|
|
{
|
|
|
|
struct end_proc_data *link;
|
|
|
|
|
|
|
|
link = end_procs;
|
|
|
|
while (link) {
|
|
|
|
rb_gc_mark(link->data);
|
|
|
|
link = link->next;
|
|
|
|
}
|
|
|
|
link = ephemeral_end_procs;
|
|
|
|
while (link) {
|
|
|
|
rb_gc_mark(link->data);
|
|
|
|
link = link->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:01:10 +04:00
|
|
|
static void
|
2016-04-22 09:09:29 +03:00
|
|
|
exec_end_procs_chain(struct end_proc_data *volatile *procs, VALUE *errp)
|
2013-11-19 21:01:10 +04:00
|
|
|
{
|
|
|
|
struct end_proc_data volatile endproc;
|
|
|
|
struct end_proc_data *link;
|
2016-04-22 09:09:29 +03:00
|
|
|
VALUE errinfo = *errp;
|
2013-11-19 21:01:10 +04:00
|
|
|
|
|
|
|
while ((link = *procs) != 0) {
|
|
|
|
*procs = link->next;
|
|
|
|
endproc = *link;
|
|
|
|
xfree(link);
|
|
|
|
(*endproc.func) (endproc.data);
|
2016-04-22 09:09:29 +03:00
|
|
|
*errp = errinfo;
|
2013-11-19 21:01:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:44:30 +03:00
|
|
|
static void
|
|
|
|
rb_ec_exec_end_proc(rb_execution_context_t * ec)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2017-06-23 10:25:52 +03:00
|
|
|
enum ruby_tag_type state;
|
2017-10-29 18:41:53 +03:00
|
|
|
volatile VALUE errinfo = ec->errinfo;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2017-10-29 18:41:53 +03:00
|
|
|
EC_PUSH_TAG(ec);
|
2017-12-06 06:16:08 +03:00
|
|
|
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
|
2013-11-19 21:01:10 +04:00
|
|
|
again:
|
2017-10-29 18:41:53 +03:00
|
|
|
exec_end_procs_chain(&ephemeral_end_procs, &ec->errinfo);
|
|
|
|
exec_end_procs_chain(&end_procs, &ec->errinfo);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2013-11-19 21:01:10 +04:00
|
|
|
else {
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_TMPPOP_TAG();
|
2019-08-13 03:50:34 +03:00
|
|
|
error_handle(ec, state);
|
2017-10-29 18:41:53 +03:00
|
|
|
if (!NIL_P(ec->errinfo)) errinfo = ec->errinfo;
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_REPUSH_TAG();
|
2013-11-19 21:01:10 +04:00
|
|
|
goto again;
|
2013-11-15 19:29:23 +04:00
|
|
|
}
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_POP_TAG();
|
2013-11-15 19:29:23 +04:00
|
|
|
|
2017-10-29 18:41:53 +03:00
|
|
|
ec->errinfo = errinfo;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-03-08 03:23:14 +03:00
|
|
|
Init_jump(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
rb_define_global_function("at_exit", rb_f_at_exit, 0);
|
|
|
|
}
|