Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: lateau
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 142 lines (123 sloc) 2.566 kb
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* -*-c-*- */
/*
* from eval.c
*/

#include "eval_intern.h"

/* exit */

void
rb_call_end_proc(VALUE data)
{
    rb_proc_call(data, rb_ary_new());
}

/*
* 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(void)
{
    VALUE proc;

    if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "called without a block");
    }
    proc = rb_block_proc();
    rb_set_end_proc(rb_call_end_proc, proc);
    return proc;
}

struct end_proc_data {
    void (*func) ();
    VALUE data;
    int safe;
    struct end_proc_data *next;
};

static struct end_proc_data *end_procs, *ephemeral_end_procs;

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;
    rb_thread_t *th = GET_THREAD();

    if (th->top_wrapper) {
list = &ephemeral_end_procs;
    }
    else {
list = &end_procs;
    }
    link->next = *list;
    link->func = func;
    link->data = data;
    link->safe = rb_safe_level();
    *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;
    }
}

void
rb_exec_end_proc(void)
{
    struct end_proc_data *volatile link;
    int status;
    volatile int safe = rb_safe_level();

    while (ephemeral_end_procs) {
link = ephemeral_end_procs;
ephemeral_end_procs = link->next;

PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
rb_set_safe_level_force(link->safe);
(*link->func) (link->data);
}
POP_TAG();
if (status) {
error_handle(status);
}
xfree(link);
    }

    while (end_procs) {
link = end_procs;
end_procs = link->next;

PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
rb_set_safe_level_force(link->safe);
(*link->func) (link->data);
}
POP_TAG();
if (status) {
error_handle(status);
}
xfree(link);
    }
    rb_set_safe_level_force(safe);
}

void
Init_jump(void)
{
    rb_define_global_function("at_exit", rb_f_at_exit, 0);
}
Something went wrong with that request. Please try again.