mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* vm_core.h (rb_vm_t): move `rb_execution_context_t::safe_level` to
  `rb_vm_t::safe_level_` because `$SAFE` is a process (VM) global state.
* vm_core.h (rb_proc_t): remove `rb_proc_t::safe_level` because `Proc`
  objects don't need to keep `$SAFE` at the creation.
  Also make `is_from_method` and `is_lambda` as 1 bit fields.
* cont.c (cont_restore_thread): no need to keep `$SAFE` for Continuation.
* eval.c (ruby_cleanup): use `rb_set_safe_level_force()` instead of access
  `vm->safe_level_` directly.
* eval_jump.c: End procs `END{}` doesn't keep `$SAFE`.
* proc.c (proc_dup): removed and introduce `rb_proc_dup` in vm.c.
* safe.c (rb_set_safe_level): don't check `$SAFE` 1 -> 0 changes.
* safe.c (safe_setter): use `rb_set_safe_level()`.
* thread.c (rb_thread_safe_level): `Thread#safe_level` returns `$SAFE`.
  It should be obsolete.
* transcode.c (load_transcoder_entry): `rb_safe_level()` only returns
  0 or 1 so that this check is not needed.
* vm.c (vm_proc_create_from_captured): don't need to keep `$SAFE` for Proc.
* vm.c (rb_proc_create): renamed to `proc_create`.
* vm.c (rb_proc_dup): moved from proc.c.
* vm.c (vm_invoke_proc): do not need to set and restore `$SAFE`
  for `Proc#call`.
* vm_eval.c (rb_eval_cmd): rename a local variable to represent clearer
  meaning.
* lib/drb/drb.rb: restore `$SAFE`.
* lib/erb.rb: restore `$SAFE`, too.
* test/lib/leakchecker.rb: check `$SAFE == 0` at the end of tests.
* test/rubygems/test_gem.rb: do not set `$SAFE = 1`.
* bootstraptest/test_proc.rb: catch up this change.
* spec/ruby/optional/capi/string_spec.rb: ditto.
* test/bigdecimal/test_bigdecimal.rb: ditto.
* test/fiddle/test_func.rb: ditto.
* test/fiddle/test_handle.rb: ditto.
* test/net/imap/test_imap_response_parser.rb: ditto.
* test/pathname/test_pathname.rb: ditto.
* test/readline/test_readline.rb: ditto.
* test/ruby/test_file.rb: ditto.
* test/ruby/test_optimization.rb: ditto.
* test/ruby/test_proc.rb: ditto.
* test/ruby/test_require.rb: ditto.
* test/ruby/test_thread.rb: ditto.
* test/rubygems/test_gem_specification.rb: ditto.
* test/test_tempfile.rb: ditto.
* test/test_tmpdir.rb: ditto.
* test/win32ole/test_win32ole.rb: ditto.
* test/win32ole/test_win32ole_event.rb: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61510 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
		
	
			
		
			
				
	
	
		
			139 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			139 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* -*-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;
 | 
						|
    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;
 | 
						|
    *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;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
static void
 | 
						|
exec_end_procs_chain(struct end_proc_data *volatile *procs, VALUE *errp)
 | 
						|
{
 | 
						|
    struct end_proc_data volatile endproc;
 | 
						|
    struct end_proc_data *link;
 | 
						|
    VALUE errinfo = *errp;
 | 
						|
 | 
						|
    while ((link = *procs) != 0) {
 | 
						|
	*procs = link->next;
 | 
						|
	endproc = *link;
 | 
						|
	xfree(link);
 | 
						|
	(*endproc.func) (endproc.data);
 | 
						|
	*errp = errinfo;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void
 | 
						|
rb_exec_end_proc(void)
 | 
						|
{
 | 
						|
    enum ruby_tag_type state;
 | 
						|
    rb_execution_context_t * volatile ec = GET_EC();
 | 
						|
    volatile VALUE errinfo = ec->errinfo;
 | 
						|
 | 
						|
    EC_PUSH_TAG(ec);
 | 
						|
    if ((state = EC_EXEC_TAG()) == TAG_NONE) {
 | 
						|
      again:
 | 
						|
	exec_end_procs_chain(&ephemeral_end_procs, &ec->errinfo);
 | 
						|
	exec_end_procs_chain(&end_procs, &ec->errinfo);
 | 
						|
    }
 | 
						|
    else {
 | 
						|
	EC_TMPPOP_TAG();
 | 
						|
	error_handle(state);
 | 
						|
	if (!NIL_P(ec->errinfo)) errinfo = ec->errinfo;
 | 
						|
	EC_REPUSH_TAG();
 | 
						|
	goto again;
 | 
						|
    }
 | 
						|
    EC_POP_TAG();
 | 
						|
 | 
						|
    ec->errinfo = errinfo;
 | 
						|
}
 | 
						|
 | 
						|
void
 | 
						|
Init_jump(void)
 | 
						|
{
 | 
						|
    rb_define_global_function("at_exit", rb_f_at_exit, 0);
 | 
						|
}
 |