diff options
Diffstat (limited to 'eval.c')
| -rw-r--r-- | eval.c | 8352 |
1 files changed, 1702 insertions, 6650 deletions
@@ -1,7248 +1,2300 @@ -/************************************************ +/********************************************************************** eval.c - $Author$ - $Date$ created at: Thu Jun 10 14:22:17 JST 1993 - Copyright (C) 1993-1999 Yukihiro Matsumoto + Copyright (C) 1993-2007 Yukihiro Matsumoto + Copyright (C) 2000 Network Applied Communication Laboratory, Inc. + Copyright (C) 2000 Information-technology Promotion Agency, Japan -************************************************/ +**********************************************************************/ -#include "ruby.h" -#include "node.h" -#include "env.h" -#include "rubysig.h" +#include "ruby/internal/config.h" -#include <stdio.h> -#include <setjmp.h> -#include "st.h" -#include "dln.h" - -#ifndef HAVE_STRING_H -char *strrchr _((char*,char)); -#endif - -#ifdef HAVE_UNISTD_H -#include <unistd.h> -#endif - -#ifdef __BEOS__ -#include <net/socket.h> -#endif - -#ifdef USE_CWGUSI -#include <sys/stat.h> -#include <sys/errno.h> -#include <compat.h> -#endif - -#ifdef __MACOS__ -#include "macruby_private.h" +#ifdef HAVE_SYS_PRCTL_H +#include <sys/prctl.h> #endif -#ifndef setjmp -#ifdef HAVE__SETJMP -#define setjmp(env) _setjmp(env) -#define longjmp(env,val) _longjmp(env,val) -#endif -#endif +#include "eval_intern.h" +#include "internal.h" +#include "internal/class.h" +#include "internal/cont.h" +#include "internal/error.h" +#include "internal/eval.h" +#include "internal/gc.h" +#include "internal/hash.h" +#include "internal/inits.h" +#include "internal/io.h" +#include "internal/object.h" +#include "internal/thread.h" +#include "internal/variable.h" +#include "ruby/fiber/scheduler.h" +#include "iseq.h" +#include "probes.h" +#include "probes_helper.h" +#include "ruby/vm.h" +#include "vm_core.h" +#include "ractor_core.h" +#include "zjit.h" + +NORETURN(static void rb_raise_jump(VALUE, VALUE)); +void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec); +void rb_ec_clear_all_trace_func(const rb_execution_context_t *ec); + +static int rb_ec_cleanup(rb_execution_context_t *ec, enum ruby_tag_type ex); +static int rb_ec_exec_node(rb_execution_context_t *ec, void *n); + +VALUE rb_eLocalJumpError; +VALUE rb_eSysStackError; + +ID ruby_static_id_signo, ruby_static_id_status; +extern ID ruby_static_id_cause; +#define id_cause ruby_static_id_cause + +#define exception_error GET_VM()->special_exceptions[ruby_error_reenter] + +#include "eval_error.c" +#include "eval_jump.c" + +#define CLASS_OR_MODULE_P(obj) \ + (!SPECIAL_CONST_P(obj) && \ + (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE)) -VALUE rb_cProc; -static VALUE rb_cBinding; -static VALUE proc_call _((VALUE,VALUE)); -static VALUE rb_f_binding _((VALUE)); -static void rb_f_END _((void)); -static VALUE rb_f_iterator_p _((void)); -static VALUE block_pass _((VALUE,NODE*)); -static VALUE rb_cMethod; -static VALUE method_proc _((VALUE)); - -static int scope_vmode; -#define SCOPE_PUBLIC 0 -#define SCOPE_PRIVATE 1 -#define SCOPE_PROTECTED 2 -#define SCOPE_MODFUNC 5 -#define SCOPE_MASK 7 -#define SCOPE_SET(f) do {scope_vmode=(f);} while(0) -#define SCOPE_TEST(f) (scope_vmode&(f)) - -#define CACHE_SIZE 0x800 -#define CACHE_MASK 0x7ff -#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK) - -struct cache_entry { /* method hash table. */ - ID mid; /* method's id */ - ID mid0; /* method's original id */ - VALUE klass; /* receiver's class */ - VALUE origin; /* where method defined */ - NODE *method; - int noex; -}; - -static struct cache_entry cache[CACHE_SIZE]; - -void -rb_clear_cache() -{ - struct cache_entry *ent, *end; - - ent = cache; end = ent + CACHE_SIZE; - while (ent < end) { - ent->mid = 0; - ent++; - } -} - -static void -rb_clear_cache_by_id(id) - ID id; -{ - struct cache_entry *ent, *end; - - ent = cache; end = ent + CACHE_SIZE; - while (ent < end) { - if (ent->mid == id) { - ent->mid = 0; - } - ent++; - } -} - -void -rb_add_method(klass, mid, node, noex) - VALUE klass; - ID mid; - NODE *node; - int noex; -{ - NODE *body; - - if (NIL_P(klass)) klass = rb_cObject; - if (klass == rb_cObject) { - rb_secure(4); - } - body = NEW_METHOD(node, noex); - st_insert(RCLASS(klass)->m_tbl, mid, body); -} - -static NODE* -search_method(klass, id, origin) - VALUE klass, *origin; - ID id; +int +ruby_setup(void) { - NODE *body; + enum ruby_tag_type state; - while (!st_lookup(RCLASS(klass)->m_tbl, id, &body)) { - klass = RCLASS(klass)->super; - if (!klass) return 0; - } + if (GET_VM()) + return 0; - if (origin) *origin = klass; - return body; -} + /* + * Disable THP early before mallocs happen because we want this to + * affect as many future pages as possible for CoW-friendliness + */ +#if defined(__linux__) && defined(PR_SET_THP_DISABLE) + prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0); +#endif + Init_BareVM(); + rb_vm_encoded_insn_data_table_init(); + Init_enable_box(); + Init_vm_objects(); + Init_master_box(); + Init_fstring_table(); -static NODE* -rb_get_method_body(klassp, idp, noexp) - VALUE *klassp; - ID *idp; - int *noexp; -{ - ID id = *idp; - VALUE klass = *klassp; - VALUE origin; - NODE * volatile body; - struct cache_entry *ent; - - if ((body = search_method(klass, id, &origin)) == 0) { - return 0; - } - if (!body->nd_body) return 0; - - /* store in cache */ - ent = cache + EXPR1(klass, id); - ent->klass = klass; - ent->noex = body->nd_noex; - body = body->nd_body; - if (nd_type(body) == NODE_FBODY) { - ent->mid = id; - *klassp = body->nd_orig; - ent->origin = body->nd_orig; - *idp = ent->mid0 = body->nd_mid; - body = ent->method = body->nd_head; - } - else { - *klassp = origin; - ent->origin = origin; - ent->mid = ent->mid0 = id; - ent->method = body; + EC_PUSH_TAG(GET_EC()); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + rb_call_inits(); + ruby_prog_init(); + GET_VM()->running = 1; } + EC_POP_TAG(); - if (noexp) *noexp = ent->noex; - return body; + return state; } void -rb_alias(klass, name, def) - VALUE klass; - ID name, def; -{ - VALUE origin; - NODE *orig, *body; - - if (name == def) return; - if (klass == rb_cObject) { - rb_secure(4); - } - orig = search_method(klass, def, &origin); - if (!orig || !orig->nd_body) { - if (TYPE(klass) == T_MODULE) { - orig = search_method(rb_cObject, def, &origin); - } - } - if (!orig || !orig->nd_body) { - rb_raise(rb_eNameError, "undefined method `%s' for `%s'", - rb_id2name(def), rb_class2name(klass)); - } - body = orig->nd_body; - if (nd_type(body) == NODE_FBODY) { /* was alias */ - def = body->nd_mid; - origin = body->nd_orig; - body = body->nd_head; - } - - st_insert(RCLASS(klass)->m_tbl, name, - NEW_METHOD(NEW_FBODY(body, def, origin), orig->nd_noex)); -} - -static void -remove_method(klass, mid) - VALUE klass; - ID mid; +ruby_init(void) { - NODE *body; - - if (klass == rb_cObject) { - rb_secure(4); - } - if (!st_delete(RCLASS(klass)->m_tbl, &mid, &body)) { - rb_raise(rb_eNameError, "method `%s' not defined in %s", - rb_id2name(mid), rb_class2name(klass)); + int state = ruby_setup(); + if (state) { + if (RTEST(ruby_debug)) { + rb_execution_context_t *ec = GET_EC(); + rb_ec_error_print(ec, ec->errinfo); + } + exit(EXIT_FAILURE); } - rb_clear_cache_by_id(mid); } -void -rb_remove_method(klass, name) - VALUE klass; - char *name; +void * +ruby_options(int argc, char **argv) { - remove_method(klass, rb_intern(name)); -} + rb_execution_context_t *ec = GET_EC(); + enum ruby_tag_type state; + void *volatile iseq = 0; -void -rb_disable_super(klass, name) - VALUE klass; - char *name; -{ - VALUE origin; - NODE *body; - ID mid = rb_intern(name); - - body = search_method(klass, mid, &origin); - if (!body || !body->nd_body) { - rb_raise(rb_eNameError, "undefined method `%s' for `%s'", - rb_id2name(mid), rb_class2name(klass)); - } - if (origin == klass) { - body->nd_noex |= NOEX_UNDEF; + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + iseq = ruby_process_options(argc, argv); } else { - rb_clear_cache_by_id(mid); - rb_add_method(ruby_class, mid, 0, NOEX_UNDEF); + rb_ec_clear_current_thread_trace_func(ec); + int exitcode = error_handle(ec, ec->errinfo, state); + ec->errinfo = Qnil; /* just been handled */ + iseq = (void *)INT2FIX(exitcode); } -} - -void -rb_enable_super(klass, name) - VALUE klass; - char *name; -{ - VALUE origin; - NODE *body; - ID mid = rb_intern(name); - - body = search_method(klass, mid, &origin); - if (!body || !body->nd_body || origin != klass) { - rb_raise(rb_eNameError, "undefined method `%s' for `%s'", - rb_id2name(mid), rb_class2name(klass)); - } - body->nd_noex &= ~NOEX_UNDEF; + EC_POP_TAG(); + return iseq; } static void -rb_export_method(klass, name, noex) - VALUE klass; - ID name; - ID noex; -{ - NODE *body; - VALUE origin; - - if (klass == rb_cObject) { - rb_secure(4); - } - body = search_method(klass, name, &origin); - if (!body && TYPE(klass) == T_MODULE) { - body = search_method(rb_cObject, name, &origin); - } - if (!body) { - rb_raise(rb_eNameError, "undefined method `%s' for `%s'", - rb_id2name(name), rb_class2name(klass)); - } - if (body->nd_noex != noex) { - if (klass == origin) { - body->nd_noex = noex; - } - else { - rb_clear_cache_by_id(name); - rb_add_method(klass, name, NEW_ZSUPER(), noex); - } - } -} - -int -rb_method_boundp(klass, id, ex) - VALUE klass; - ID id; - int ex; +rb_ec_fiber_scheduler_finalize(rb_execution_context_t *ec) { - int noex; + enum ruby_tag_type state; - if (rb_get_method_body(&klass, &id, &noex)) { - if (ex && noex & NOEX_PRIVATE) - return Qfalse; - return Qtrue; + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + rb_fiber_scheduler_set(Qnil); } - return Qfalse; -} - -void -rb_attr(klass, id, read, write, ex) - VALUE klass; - ID id; - int read, write, ex; -{ - char *name; - char *buf; - ID attriv; - int noex; - - if (!ex) noex = NOEX_PUBLIC; else { - if (SCOPE_TEST(SCOPE_PRIVATE)) { - noex = NOEX_PRIVATE; - rb_warning("private attribute?"); - } - else if (SCOPE_TEST(SCOPE_PROTECTED)) { - noex = NOEX_PROTECTED; - } - else { - noex = NOEX_PUBLIC; - } - } - - name = rb_id2name(id); - if (!name) { - rb_raise(rb_eArgError, "argument needs to be symbol or string"); + state = error_handle(ec, ec->errinfo, state); } - buf = ALLOCA_N(char,strlen(name)+2); - sprintf(buf, "@%s", name); - attriv = rb_intern(buf); - if (read) { - rb_add_method(klass, id, NEW_IVAR(attriv), noex); - } - sprintf(buf, "%s=", name); - id = rb_intern(buf); - if (write) { - rb_add_method(klass, id, NEW_ATTRSET(attriv), noex); - } -} - -static ID init, eqq, each, aref, aset, match; -VALUE rb_errinfo = Qnil; -extern NODE *ruby_eval_tree_begin; -extern NODE *ruby_eval_tree; -extern int ruby_nerrs; - -static VALUE rb_eLocalJumpError; -static VALUE rb_eSysStackError; - -extern VALUE ruby_top_self; - -struct FRAME *ruby_frame; -struct SCOPE *ruby_scope; -static struct FRAME *top_frame; -static struct SCOPE *top_scope; - -#define PUSH_FRAME() { \ - struct FRAME _frame; \ - _frame.prev = ruby_frame; \ - _frame.file = ruby_sourcefile; \ - _frame.line = ruby_sourceline; \ - _frame.iter = ruby_iter->iter; \ - _frame.cbase = ruby_frame->cbase; \ - _frame.argc = 0; \ - ruby_frame = &_frame; \ - -#define POP_FRAME() \ - ruby_sourcefile = _frame.file; \ - ruby_sourceline = _frame.line; \ - ruby_frame = _frame.prev; } - -struct BLOCK { - NODE *var; - NODE *body; - VALUE self; - struct FRAME frame; - struct SCOPE *scope; - VALUE klass; - struct tag *tag; - int iter; - int vmode; - struct RVarmap *d_vars; -#ifdef USE_THREAD - VALUE orig_thread; -#endif - struct BLOCK *prev; -}; -static struct BLOCK *ruby_block; -static struct BLOCK *ruby_calling_block; - -#define PUSH_BLOCK(v,b) { \ - struct BLOCK _block; \ - _block.tag = prot_tag; \ - _block.var = v; \ - _block.body = b; \ - _block.self = self; \ - _block.frame = *ruby_frame; \ - _block.klass = ruby_class; \ - _block.frame.file = ruby_sourcefile;\ - _block.frame.line = ruby_sourceline;\ - _block.scope = ruby_scope; \ - _block.d_vars = ruby_dyna_vars; \ - _block.prev = ruby_block; \ - _block.iter = ruby_iter->iter; \ - _block.vmode = scope_vmode; \ - ruby_block = &_block; - -#define POP_BLOCK() \ - ruby_block = _block.prev; \ -} - -#define PUSH_BLOCK2(b) { \ - struct BLOCK * volatile _old; \ - struct BLOCK * volatile _old_call; \ - _old = ruby_block; \ - _old_call = ruby_calling_block; \ - ruby_calling_block = b; \ - ruby_block = b; - -#define POP_BLOCK2() \ - ruby_calling_block = _old_call; \ - ruby_block = _old; \ -} - -struct RVarmap *ruby_dyna_vars; -#define PUSH_VARS() { \ - struct RVarmap * volatile _oldvmap; \ - _oldvmap = ruby_dyna_vars; \ - ruby_dyna_vars = 0; - -#define POP_VARS() \ - ruby_dyna_vars = _oldvmap; \ -} - -static struct RVarmap* -new_dvar(id, value) - ID id; - VALUE value; -{ - NEWOBJ(vars, struct RVarmap); - OBJSETUP(vars, 0, T_VARMAP); - vars->id = id; - vars->val = value; - vars->next = ruby_dyna_vars; - - return vars; -} - -VALUE -rb_dvar_defined(id) - ID id; -{ - struct RVarmap *vars = ruby_dyna_vars; - - while (vars) { - if (vars->id == id) return Qtrue; - vars = vars->next; - } - return Qfalse; -} - -VALUE -rb_dvar_ref(id) - ID id; -{ - struct RVarmap *vars = ruby_dyna_vars; - - while (vars) { - if (vars->id == id) { - return vars->val; - } - vars = vars->next; - } - return Qnil; -} - -void -rb_dvar_push(id, value) - ID id; - VALUE value; -{ - ruby_dyna_vars = new_dvar(id, value); -} - -void -rb_dvar_asgn(id, value) - ID id; - VALUE value; -{ - struct RVarmap *vars = ruby_dyna_vars; - - while (vars) { - if (vars->id == id) { - vars->val = value; - return; - } - vars = vars->next; - } - rb_dvar_push(id, value); - return; + EC_POP_TAG(); } static void -dvar_asgn_push(id, value) - ID id; - VALUE value; -{ - rb_dvar_asgn(id, value); - if (ruby_calling_block) { - ruby_calling_block->d_vars = ruby_dyna_vars; - } -} - -struct iter { - int iter; - struct iter *prev; -}; -static struct iter *ruby_iter; - -#define ITER_NOT 0 -#define ITER_PRE 1 -#define ITER_CUR 2 - -#define PUSH_ITER(i) { \ - struct iter _iter; \ - _iter.prev = ruby_iter; \ - _iter.iter = (i); \ - ruby_iter = &_iter; \ - -#define POP_ITER() \ - ruby_iter = _iter.prev; \ -} - -struct tag { - jmp_buf buf; - struct FRAME *frame; - struct iter *iter; - ID tag; - VALUE retval; - int dst; - struct tag *prev; -}; -static struct tag *prot_tag; - -#define PUSH_TAG(ptag) { \ - struct tag _tag; \ - _tag.retval = Qnil; \ - _tag.frame = ruby_frame; \ - _tag.iter = ruby_iter; \ - _tag.prev = prot_tag; \ - _tag.retval = Qnil; \ - _tag.tag = ptag; \ - _tag.dst = 0; \ - prot_tag = &_tag; - -#define PROT_NONE 0 -#define PROT_FUNC -1 -#define PROT_THREAD -2 - -#define EXEC_TAG() setjmp(prot_tag->buf) - -#define JUMP_TAG(st) { \ - ruby_frame = prot_tag->frame; \ - ruby_iter = prot_tag->iter; \ - longjmp(prot_tag->buf,(st)); \ -} - -#define POP_TAG() \ - if (_tag.prev) \ - _tag.prev->retval = _tag.retval;\ - prot_tag = _tag.prev; \ -} - -#define TAG_RETURN 0x1 -#define TAG_BREAK 0x2 -#define TAG_NEXT 0x3 -#define TAG_RETRY 0x4 -#define TAG_REDO 0x5 -#define TAG_RAISE 0x6 -#define TAG_THROW 0x7 -#define TAG_FATAL 0x8 -#define TAG_MASK 0xf - -VALUE ruby_class; -static VALUE ruby_wrapper; /* security wrapper */ - -#define PUSH_CLASS() { \ - VALUE _class = ruby_class; \ - -#define POP_CLASS() ruby_class = _class; } - -#define PUSH_SCOPE() { \ - volatile int _vmode = scope_vmode; \ - struct SCOPE * volatile _old; \ - NEWOBJ(_scope, struct SCOPE); \ - OBJSETUP(_scope, 0, T_SCOPE); \ - _scope->local_tbl = 0; \ - _scope->local_vars = 0; \ - _scope->flag = 0; \ - _old = ruby_scope; \ - ruby_scope = _scope; \ - scope_vmode = SCOPE_PUBLIC; - -#ifdef USE_THREAD -#define SCOPE_DONT_RECYCLE FL_USER2 -#define POP_SCOPE() \ - if (FL_TEST(ruby_scope, SCOPE_DONT_RECYCLE)) {\ - FL_SET(_old, SCOPE_DONT_RECYCLE);\ - }\ - else {\ - if (ruby_scope->flag == SCOPE_ALLOCA) {\ - ruby_scope->local_vars = 0;\ - ruby_scope->local_tbl = 0;\ - if (ruby_scope != top_scope)\ - rb_gc_force_recycle((VALUE)ruby_scope);\ - }\ - else {\ - ruby_scope->flag |= SCOPE_NOSTACK;\ - }\ - }\ - ruby_scope = _old;\ - scope_vmode = _vmode;\ -} -#else /* not USE_THREAD */ -#define POP_SCOPE() \ - if (ruby_scope->flag == SCOPE_ALLOCA) {\ - ruby_scope->local_vars = 0;\ - ruby_scope->local_tbl = 0;\ - if (ruby_scope != top_scope)\ - rb_gc_force_recycle((VALUE)ruby_scope);\ - }\ - else {\ - ruby_scope->flag |= SCOPE_NOSTACK;\ - }\ - ruby_scope = _old;\ - scope_vmode = _vmode;\ -} -#endif /* USE_THREAD */ - -static VALUE rb_eval _((VALUE,NODE*)); -static VALUE eval _((VALUE,VALUE,VALUE,char*,int)); -static NODE *compile _((VALUE,char*)); -static VALUE rb_yield_0 _((VALUE, VALUE, VALUE)); - -static VALUE rb_call _((VALUE,VALUE,ID,int,VALUE*,int)); -static VALUE module_setup _((VALUE,NODE*)); - -static VALUE massign _((VALUE,NODE*,VALUE)); -static void assign _((VALUE,NODE*,VALUE)); - -static int safe_level = 0; -/* safe-level: - 0 - strings from streams/environment/ARGV are tainted (default) - 1 - no dangerous operation by tainted string - 2 - process/file operations prohibited - 3 - all genetated strings are tainted - 4 - no global (non-tainted) variable modification/no direct output -*/ - -int -rb_safe_level() +rb_ec_teardown(rb_execution_context_t *ec) { - return safe_level; -} + // If the user code defined a scheduler for the top level thread, run it: + rb_ec_fiber_scheduler_finalize(ec); -void -rb_set_safe_level(level) - int level; -{ - if (level > safe_level) { - safe_level = level; + EC_PUSH_TAG(ec); + if (EC_EXEC_TAG() == TAG_NONE) { + rb_vm_trap_exit(rb_ec_vm_ptr(ec)); } -} - -static VALUE -safe_getter() -{ - return INT2FIX(safe_level); + EC_POP_TAG(); + rb_ec_exec_end_proc(ec); + rb_ec_clear_all_trace_func(ec); } static void -safe_setter(val) - VALUE val; +rb_ec_finalize(rb_execution_context_t *ec) { - int level = NUM2INT(val); - - if (level < safe_level) { - rb_raise(rb_eSecurityError, "tried to downgrade safe level from %d to %d", - safe_level, level); - } - safe_level = level; + ruby_sig_finalize(); + ec->errinfo = Qnil; + rb_objspace_call_finalizer(); } void -rb_check_safe_str(x) - VALUE x; +ruby_finalize(void) { - if (TYPE(x)!= T_STRING) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", - rb_class2name(CLASS_OF(x))); - } - if (OBJ_TAINTED(x)) { - if (safe_level > 0){ - rb_raise(rb_eSecurityError, "Insecure operation - %s", - rb_id2name(ruby_frame->last_func)); - } - } + rb_execution_context_t *ec = GET_EC(); + rb_ec_teardown(ec); + rb_ec_finalize(ec); } -void -rb_secure(level) - int level; +int +ruby_cleanup(int ex) { - if (level <= safe_level) { - rb_raise(rb_eSecurityError, "Insecure operation `%s' for level %d", - rb_id2name(ruby_frame->last_func), safe_level); - } + return rb_ec_cleanup(GET_EC(), (enum ruby_tag_type)ex); } -static VALUE trace_func = 0; -static void call_trace_func _((char*,char*,int,VALUE,ID,VALUE)); - -static void -error_pos() +static int +rb_ec_cleanup(rb_execution_context_t *ec, enum ruby_tag_type ex) { - if (ruby_sourcefile) { - if (ruby_frame->last_func) { - fprintf(stderr, "%s:%d:in `%s'", ruby_sourcefile, ruby_sourceline, - rb_id2name(ruby_frame->last_func)); - } - else { - fprintf(stderr, "%s:%d", ruby_sourcefile, ruby_sourceline); - } - } -} + int state; + volatile VALUE save_error = Qundef; + volatile int sysex = EXIT_SUCCESS; + volatile int signaled = 0; + rb_thread_t *th = rb_ec_thread_ptr(ec); + rb_thread_t *const volatile th0 = th; + volatile int step = 0; + volatile VALUE message = Qnil; + VALUE buf; + + rb_threadptr_interrupt(th); + rb_threadptr_check_signal(th); + + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + RUBY_VM_CHECK_INTS(ec); + + step_0: step++; + save_error = ec->errinfo; + if (THROW_DATA_P(ec->errinfo)) ec->errinfo = Qnil; + + /* exits with failure but silently when an exception raised + * here */ + rb_ec_teardown(ec); + + step_1: step++; + VALUE err = ec->errinfo; + volatile int mode0 = 0, mode1 = 0; + if (err != save_error && !NIL_P(err)) { + mode0 = exiting_split(err, &sysex, &signaled); + } -static VALUE -get_backtrace(info) - VALUE info; -{ - if (NIL_P(info)) return Qnil; - return rb_funcall(info, rb_intern("backtrace"), 0); -} + /* exceptions after here will be ignored */ -static void -set_backtrace(info, bt) - VALUE info, bt; -{ - rb_funcall(info, rb_intern("set_backtrace"), 1, bt); -} + /* build error message including causes */ + err = ATOMIC_VALUE_EXCHANGE(save_error, Qnil); -static void -error_print() -{ - VALUE errat; - VALUE eclass; - VALUE einfo; - volatile int safe = safe_level; - - if (NIL_P(rb_errinfo)) return; + if (!NIL_P(err) && !THROW_DATA_P(err)) { + mode1 = exiting_split(err, (mode0 & EXITING_WITH_STATUS) ? NULL : &sysex, &signaled); + if (mode1 & EXITING_WITH_MESSAGE) { + buf = rb_str_new(NULL, 0); + rb_ec_error_print_detailed(ec, err, buf, Qundef); + message = buf; + } + } - errat = get_backtrace(rb_errinfo); - if (!NIL_P(errat)) { - VALUE mesg = RARRAY(errat)->ptr[0]; + step_2: step++; + /* protect from Thread#raise */ + th->status = THREAD_KILLED; - if (NIL_P(mesg)) error_pos(); - else { - fwrite(RSTRING(mesg)->ptr, 1, RSTRING(mesg)->len, stderr); - } - } + rb_ractor_terminate_all(); - eclass = CLASS_OF(rb_errinfo); - einfo = rb_obj_as_string(rb_errinfo); - if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) { - fprintf(stderr, ": unhandled exception\n"); + step_3: step++; + if (!NIL_P(buf = message)) { + warn_print_str(buf); + } + else if (!NIL_OR_UNDEF_P(err = save_error) || + (ex != TAG_NONE && !((mode0|mode1) & EXITING_WITH_STATUS))) { + sysex = error_handle(ec, err, ex); + } } else { - VALUE epath; - - epath = rb_class_path(eclass); - if (RSTRING(einfo)->len == 0) { - fprintf(stderr, ": "); - fwrite(RSTRING(epath)->ptr, 1, RSTRING(epath)->len, stderr); - putc('\n', stderr); - } - else { - char *tail = 0; - int len = RSTRING(einfo)->len; - - if (RSTRING(epath)->ptr[0] == '#') epath = 0; - if (tail = strchr(RSTRING(einfo)->ptr, '\n')) { - len = tail - RSTRING(einfo)->ptr; - tail++; /* skip newline */ - } - fprintf(stderr, ": "); - fwrite(RSTRING(einfo)->ptr, 1, len, stderr); - if (epath) { - fprintf(stderr, " ("); - fwrite(RSTRING(epath)->ptr, 1, RSTRING(epath)->len, stderr); - fprintf(stderr, ")\n"); - } - if (tail) { - fwrite(tail, 1, RSTRING(einfo)->len-len-1, stderr); - putc('\n', stderr); - } - } + th = th0; + switch (step) { + case 0: goto step_0; + case 1: goto step_1; + case 2: goto step_2; + case 3: goto step_3; + } } - if (!NIL_P(errat)) { - int i; - struct RArray *ep = RARRAY(errat); - -#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5) -#define TRACE_HEAD 8 -#define TRACE_TAIL 5 - - ep = RARRAY(errat); - for (i=1; i<ep->len; i++) { - if (TYPE(ep->ptr[i]) == T_STRING) { - fprintf(stderr, "\tfrom %s\n", RSTRING(ep->ptr[i])->ptr); - } - if (i == TRACE_HEAD && ep->len > TRACE_MAX) { - fprintf(stderr, "\t ... %d levels...\n", - ep->len - TRACE_HEAD - TRACE_TAIL); - i = ep->len - TRACE_TAIL; - } - } - } - safe_level = safe; -} + rb_ec_finalize(ec); -#if !defined(NT) && !defined(__MACOS__) -extern char **environ; -#endif -char **rb_origenviron; + /* unlock again if finalizer took mutexes. */ + rb_threadptr_unlock_all_locking_mutexes(th); + th = th0; + EC_POP_TAG(); + th = th0; + rb_thread_stop_timer_thread(); + ruby_vm_destruct(th->vm); + // For YJIT, call this after ruby_vm_destruct() frees jit_cont for the root fiber. + rb_jit_cont_finish(); -void rb_call_inits _((void)); -void Init_stack _((void)); -void Init_heap _((void)); -void Init_ext _((void)); + if (signaled) ruby_default_signal(signaled); -void -ruby_init() -{ - static struct FRAME frame; - static struct iter iter; - int state; - - ruby_frame = top_frame = &frame; - ruby_iter = &iter; - -#ifdef __MACOS__ - rb_origenviron = 0; -#else - rb_origenviron = environ; -#endif - - Init_heap(); - PUSH_SCOPE(); - ruby_scope->local_vars = 0; - ruby_scope->local_tbl = 0; - top_scope = ruby_scope; - /* default visibility is private at toplevel */ - SCOPE_SET(SCOPE_PRIVATE); - - PUSH_TAG(PROT_NONE) - if ((state = EXEC_TAG()) == 0) { - rb_call_inits(); - ruby_class = rb_cObject; - ruby_frame->self = ruby_top_self; - ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,rb_cObject,0,0); - rb_define_global_const("TOPLEVEL_BINDING", rb_f_binding(ruby_top_self)); -#ifdef __MACOS__ - _macruby_init(); -#endif - ruby_prog_init(); - } - POP_TAG(); - if (state) error_print(); - POP_SCOPE(); - ruby_scope = top_scope; + return sysex; } -static int ext_init = 0; - -void -ruby_options(argc, argv) - int argc; - char **argv; +static int +rb_ec_exec_node(rb_execution_context_t *ec, void *n) { - int state; + volatile int state; + rb_iseq_t *iseq = (rb_iseq_t *)n; + if (!n) return 0; - PUSH_TAG(PROT_NONE) - if ((state = EXEC_TAG()) == 0) { - NODE *save; - - ruby_process_options(argc, argv); - ext_init = 1; /* Init_ext() called in ruby_process_options */ - save = ruby_eval_tree; - ruby_require_modules(); - ruby_eval_tree = save; - } - POP_TAG(); - if (state) { - trace_func = 0; - error_print(); - exit(1); + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + rb_iseq_eval_main(iseq); } + EC_POP_TAG(); + return state; } -static VALUE -eval_node(self) - VALUE self; +void +ruby_stop(int ex) { - VALUE result = Qnil; - NODE *tree; - - if (ruby_eval_tree_begin) { - tree = ruby_eval_tree_begin; - ruby_eval_tree_begin = 0; - rb_eval(self, tree); - } - - if (!ruby_eval_tree) return Qnil; - - tree = ruby_eval_tree; - ruby_eval_tree = 0; - - result = rb_eval(self, tree); - return result; + exit(ruby_cleanup(ex)); } -int rb_in_eval; - -#ifdef USE_THREAD -static void rb_thread_cleanup _((void)); -static void rb_thread_wait_other_threads _((void)); -#endif - -static int exit_status; - -static void exec_end_proc _((void)); - -void -ruby_run() +int +ruby_executable_node(void *n, int *status) { - int state; - static int ex; - - if (ruby_nerrs > 0) exit(ruby_nerrs); - - Init_stack(); - - PUSH_TAG(PROT_NONE); - PUSH_ITER(ITER_NOT); - if ((state = EXEC_TAG()) == 0) { - if (!ext_init) Init_ext(); - eval_node(ruby_top_self); - } - POP_ITER(); - POP_TAG(); - - if (state && !ex) ex = state; - PUSH_TAG(PROT_NONE); - PUSH_ITER(ITER_NOT); - if ((state = EXEC_TAG()) == 0) { - rb_trap_exit(); -#ifdef USE_THREAD - rb_thread_cleanup(); - rb_thread_wait_other_threads(); -#endif - } - else { - ex = state; - } - POP_ITER(); - POP_TAG(); + VALUE v = (VALUE)n; + int s; - switch (ex & 0xf) { - case 0: - ex = 0; - break; - - case TAG_RETURN: - error_pos(); - fprintf(stderr, ": unexpected return\n"); - ex = 1; - break; - case TAG_NEXT: - error_pos(); - fprintf(stderr, ": unexpected next\n"); - ex = 1; - break; - case TAG_BREAK: - error_pos(); - fprintf(stderr, ": unexpected break\n"); - ex = 1; - break; - case TAG_REDO: - error_pos(); - fprintf(stderr, ": unexpected redo\n"); - ex = 1; - break; - case TAG_RETRY: - error_pos(); - fprintf(stderr, ": retry outside of rescue clause\n"); - ex = 1; - break; - case TAG_RAISE: - case TAG_FATAL: - if (rb_obj_is_kind_of(rb_errinfo, rb_eSystemExit)) { - exit(exit_status); - } - error_print(); - ex = 1; - break; + switch (v) { + case Qtrue: s = EXIT_SUCCESS; break; + case Qfalse: s = EXIT_FAILURE; break; default: - rb_bug("Unknown longjmp status %d", ex); - break; + if (!FIXNUM_P(v)) return TRUE; + s = FIX2INT(v); } - exec_end_proc(); - rb_gc_call_finalizer_at_exit(); - exit(ex); + if (status) *status = s; + return FALSE; } -static void -compile_error(at) - char *at; -{ - VALUE str; - char *mesg; - int len; - - mesg = str2cstr(rb_errinfo, &len); - ruby_nerrs = 0; - str = rb_str_new2("compile error"); - if (at) { - rb_str_cat(str, " in ", 4); - rb_str_cat(str, at, strlen(at)); - } - rb_str_cat(str, "\n", 1); - rb_str_cat(str, mesg, len); - rb_exc_raise(rb_exc_new3(rb_eSyntaxError, str)); -} - -VALUE -rb_eval_string(str) - char *str; -{ - VALUE v; - char *oldsrc = ruby_sourcefile; - - ruby_sourcefile = "(eval)"; - v = eval(ruby_top_self, rb_str_new2(str), Qnil, 0, 0); - ruby_sourcefile = oldsrc; - - return v; -} - -VALUE -rb_eval_string_protect(str, state) - char *str; - int *state; +int +ruby_run_node(void *n) { - VALUE result; /* OK */ + rb_execution_context_t *ec = GET_EC(); int status; - - PUSH_TAG(PROT_NONE); - if ((status = EXEC_TAG()) == 0) { - result = rb_eval_string(str); - } - POP_TAG(); - if (state) { - *state = status; - } - if (status != 0) { - return Qnil; + if (!ruby_executable_node(n, &status)) { + rb_ec_cleanup(ec, (NIL_P(ec->errinfo) ? TAG_NONE : TAG_RAISE)); + return status; } - - return result; -} - -VALUE -rb_eval_cmd(cmd, arg) - VALUE cmd, arg; -{ - int state; - VALUE val; /* OK */ - struct SCOPE *saved_scope; - volatile int safe = safe_level; - - if (TYPE(cmd) != T_STRING) { - return rb_funcall2(cmd, rb_intern("call"), - RARRAY(arg)->len, RARRAY(arg)->ptr); - } - - PUSH_CLASS(); - PUSH_TAG(PROT_NONE); - saved_scope = ruby_scope; - ruby_scope = top_scope; - - ruby_class = rb_cObject; - if (OBJ_TAINTED(cmd)) { - safe_level = 4; - } - - if ((state = EXEC_TAG()) == 0) { - val = eval(ruby_top_self, cmd, Qnil, 0, 0); - } - -#ifdef USE_THREAD - if (FL_TEST(ruby_scope, SCOPE_DONT_RECYCLE)) - FL_SET(saved_scope, SCOPE_DONT_RECYCLE); -#endif - ruby_scope = saved_scope; - safe_level = safe; - POP_TAG(); - POP_CLASS(); - - switch (state) { - case 0: - break; - case TAG_RETURN: - rb_raise(rb_eLocalJumpError, "unexpected return"); - break; - case TAG_NEXT: - rb_raise(rb_eLocalJumpError, "unexpected next"); - break; - case TAG_BREAK: - rb_raise(rb_eLocalJumpError, "unexpected break"); - break; - case TAG_REDO: - rb_raise(rb_eLocalJumpError, "unexpected redo"); - break; - case TAG_RETRY: - rb_raise(rb_eLocalJumpError, "retry outside of rescue clause"); - break; - default: - JUMP_TAG(state); - break; - } - return val; + return rb_ec_cleanup(ec, rb_ec_exec_node(ec, n)); } -VALUE -rb_trap_eval(cmd, sig) - VALUE cmd; - int sig; +int +ruby_exec_node(void *n) +{ + return rb_ec_exec_node(GET_EC(), n); +} + +/* + * call-seq: + * Module.nesting -> array + * + * Returns nested module as an array of Module objects: + * + * module M0 + * def self.speak = Module.nesting + * module M1 + * def self.speak = Module.nesting + * module M2 + * def self.speak = Module.nesting + * end + * end + * end + * M0.speak # => [M0] + * M0.speak.first.class # => Module + * M0::M1.speak # => [M0::M1, M0] + * M0::M1::M2.speak # => [M0::M1::M2, M0::M1, M0] + * + */ + +static VALUE +rb_mod_nesting(VALUE _) { - int state; - VALUE val; /* OK */ + VALUE ary = rb_ary_new(); + const rb_cref_t *cref = rb_vm_cref(); - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - val = rb_eval_cmd(cmd, rb_ary_new3(1, INT2FIX(sig))); - } - POP_TAG(); - if (state) { - rb_trap_immediate = 0; - JUMP_TAG(state); + while (cref && CREF_NEXT(cref)) { + VALUE klass = CREF_CLASS(cref); + if (!CREF_PUSHED_BY_EVAL(cref) && + !NIL_P(klass)) { + rb_ary_push(ary, klass); + } + cref = CREF_NEXT(cref); } - return val; + return ary; } -static VALUE -superclass(self, node) - VALUE self; - NODE *node; -{ - VALUE val; /* OK */ - int state; +/* + * call-seq: + * Module.constants -> array + * Module.constants(inherited) -> array + * + * In the first form, returns an array of the names of all + * constants accessible from the point of call. + * This list includes the names of all modules and classes + * defined in the global scope. + * + * Module.constants.first(4) + * # => [:ARGF, :ARGV, :ArgumentError, :Array] + * + * Module.constants.include?(:SEEK_SET) # => false + * + * class IO + * Module.constants.include?(:SEEK_SET) # => true + * end + * + * The second form calls the instance method +constants+. + */ + +static VALUE +rb_mod_s_constants(int argc, VALUE *argv, VALUE mod) +{ + const rb_cref_t *cref = rb_vm_cref(); + VALUE klass; + VALUE cbase = 0; + void *data = 0; - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - val = rb_eval(self, node); - } - POP_TAG(); - if (state) { - superclass_error: - switch (nd_type(node)) { - case NODE_COLON2: - rb_raise(rb_eTypeError, "undefined superclass `%s'", - rb_id2name(node->nd_mid)); - case NODE_CVAR: - rb_raise(rb_eTypeError, "undefined superclass `%s'", - rb_id2name(node->nd_vid)); - default: - rb_raise(rb_eTypeError, "superclass undefined"); - } - JUMP_TAG(state); + if (argc > 0 || mod != rb_cModule) { + return rb_mod_constants(argc, argv, mod); } - if (TYPE(val) != T_CLASS) goto superclass_error; - if (FL_TEST(val, FL_SINGLETON)) { - rb_raise(rb_eTypeError, "can't make subclass of virtual class"); - } - - return val; -} - -static VALUE -ev_const_defined(cref, id) - NODE *cref; - ID id; -{ - NODE *cbase = cref; - while (cbase && cbase->nd_clss != rb_cObject) { - struct RClass *klass = RCLASS(cbase->nd_clss); - - if (klass->iv_tbl && - st_lookup(klass->iv_tbl, id, 0)) { - return Qtrue; - } - cbase = cbase->nd_next; + while (cref) { + klass = CREF_CLASS(cref); + if (!CREF_PUSHED_BY_EVAL(cref) && + !NIL_P(klass)) { + data = rb_mod_const_at(CREF_CLASS(cref), data); + if (!cbase) { + cbase = klass; + } + } + cref = CREF_NEXT(cref); } - return rb_const_defined(cref->nd_clss, id); -} -static VALUE -ev_const_get(cref, id) - NODE *cref; - ID id; -{ - NODE *cbase = cref; - VALUE result; - - while (cbase && cbase->nd_clss != rb_cObject) { - struct RClass *klass = RCLASS(cbase->nd_clss); - - if (klass->iv_tbl && - st_lookup(klass->iv_tbl, id, &result)) { - return result; - } - cbase = cbase->nd_next; + if (cbase) { + data = rb_mod_const_of(cbase, data); } - return rb_const_get(cref->nd_clss, id); + return rb_const_list(data); } -static VALUE -rb_mod_nesting() +/** + * Asserts that `klass` is not a frozen class. + * @param[in] klass a `Module` object + * @exception RuntimeError if `klass` is not a class or frozen. + * @ingroup class + */ +void +rb_class_modify_check(VALUE klass) { - NODE *cbase = (NODE*)ruby_frame->cbase; - VALUE ary = rb_ary_new(); - - while (cbase && cbase->nd_clss != rb_cObject) { - rb_ary_push(ary, cbase->nd_clss); - cbase = cbase->nd_next; + if (SPECIAL_CONST_P(klass)) { + Check_Type(klass, T_CLASS); } - return ary; -} - -static VALUE -rb_mod_s_constants() -{ - NODE *cbase = (NODE*)ruby_frame->cbase; - VALUE ary = rb_ary_new(); - - while (cbase && cbase->nd_clss != rb_cObject) { - rb_mod_const_at(cbase->nd_clss, ary); - cbase = cbase->nd_next; + if (RB_TYPE_P(klass, T_MODULE)) { + // TODO: shouldn't this only happen in a few places? + rb_class_set_initialized(klass); } - - rb_mod_const_of(((NODE*)ruby_frame->cbase)->nd_clss, ary); - return ary; -} - -static VALUE -rb_mod_remove_method(mod, name) - VALUE mod, name; -{ - remove_method(mod, rb_to_id(name)); - return mod; -} - -static VALUE -rb_mod_undef_method(mod, name) - VALUE mod, name; -{ - ID id = rb_to_id(name); - - rb_add_method(mod, id, 0, NOEX_PUBLIC); - rb_clear_cache_by_id(id); - return mod; -} - -static VALUE -rb_mod_alias_method(mod, newname, oldname) - VALUE mod, newname, oldname; -{ - ID id = rb_to_id(newname); - - rb_alias(mod, id, rb_to_id(oldname)); - rb_clear_cache_by_id(id); - return mod; -} - -#ifdef C_ALLOCA -# define TMP_PROTECT NODE * volatile __protect_tmp=0 -# define TMP_ALLOC(n) \ - (__protect_tmp = rb_node_newnode(NODE_ALLOCA, \ - ALLOC_N(VALUE,n),__protect_tmp,n), \ - (void*)__protect_tmp->nd_head) -#else -# define TMP_PROTECT typedef int foobazzz -# define TMP_ALLOC(n) ALLOCA_N(VALUE,n) -#endif - -#define SETUP_ARGS(anode) {\ - NODE *n = anode;\ - if (!n) {\ - argc = 0;\ - argv = 0;\ - }\ - else if (nd_type(n) == NODE_ARRAY) {\ - argc=n->nd_alen;\ - if (argc > 0) {\ - char *file = ruby_sourcefile;\ - int line = ruby_sourceline;\ - int i;\ - n = anode;\ - argv = TMP_ALLOC(argc);\ - for (i=0;i<argc;i++) {\ - argv[i] = rb_eval(self,n->nd_head);\ - n=n->nd_next;\ - }\ - ruby_sourcefile = file;\ - ruby_sourceline = line;\ - }\ - else {\ - argc = 0;\ - argv = 0;\ - }\ - }\ - else {\ - VALUE args = rb_eval(self,n);\ - char *file = ruby_sourcefile;\ - int line = ruby_sourceline;\ - if (TYPE(args) != T_ARRAY)\ - args = rb_Array(args);\ - argc = RARRAY(args)->len;\ - argv = ALLOCA_N(VALUE, argc);\ - MEMCPY(argv, RARRAY(args)->ptr, VALUE, argc);\ - ruby_sourcefile = file;\ - ruby_sourceline = line;\ - }\ -} - -#define BEGIN_CALLARGS {\ - struct BLOCK *tmp_block = ruby_block;\ - if (ruby_iter->iter == ITER_PRE) {\ - ruby_block = ruby_block->prev;\ - }\ - PUSH_ITER(ITER_NOT); - -#define END_CALLARGS \ - ruby_block = tmp_block;\ - POP_ITER();\ -} - -#define MATCH_DATA ruby_scope->local_vars[node->nd_cnt] - -static char* is_defined _((VALUE, NODE*, char*)); - -static char* -arg_defined(self, node, buf, type) - VALUE self; - NODE *node; - char *buf; - char *type; -{ - int argc; - int i; - - if (!node) return type; /* no args */ - if (nd_type(node) == NODE_ARRAY) { - argc=node->nd_alen; - if (argc > 0) { - for (i=0;i<argc;i++) { - if (!is_defined(self, node->nd_head, buf)) - return 0; - node = node->nd_next; - } + if (OBJ_FROZEN(klass)) { + if (RCLASS_SINGLETON_P(klass)) { + klass = RCLASS_ATTACHED_OBJECT(klass); } + rb_error_frozen_object(klass); } - else if (!is_defined(self, node, buf)) { - return 0; - } - return type; } - -static char* -is_defined(self, node, buf) - VALUE self; - NODE *node; /* OK */ - char *buf; -{ - VALUE val; /* OK */ - int state; - - switch (nd_type(node)) { - case NODE_SUPER: - case NODE_ZSUPER: - if (ruby_frame->last_func == 0) return 0; - else if (rb_method_boundp(RCLASS(ruby_frame->last_class)->super, - ruby_frame->last_func, 1)) { - if (nd_type(node) == NODE_SUPER) { - return arg_defined(self, node->nd_args, buf, "super"); - } - return "super"; - } - break; - - case NODE_VCALL: - case NODE_FCALL: - val = CLASS_OF(self); - goto check_bound; - - case NODE_CALL: - if (!is_defined(self, node->nd_recv, buf)) return 0; - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - val = rb_eval(self, node->nd_recv); - val = CLASS_OF(val); - } - POP_TAG(); - if (state) return 0; - check_bound: - if (rb_method_boundp(val, node->nd_mid, nd_type(node)== NODE_CALL)) { - return arg_defined(self, node->nd_args, buf, "method"); - } - break; - - case NODE_MATCH2: - case NODE_MATCH3: - return "method"; - - case NODE_YIELD: - if (rb_iterator_p()) { - return "yield"; - } - break; - - case NODE_SELF: - return "self"; - - case NODE_NIL: - return "nil"; - - case NODE_TRUE: - return "true"; - - case NODE_FALSE: - return "false"; - - case NODE_ATTRSET: - case NODE_OP_ASGN1: - case NODE_OP_ASGN2: - case NODE_MASGN: - case NODE_LASGN: - case NODE_DASGN: - case NODE_DASGN_PUSH: - case NODE_GASGN: - case NODE_IASGN: - case NODE_CASGN: - return "assignment"; - - case NODE_LVAR: - return "local-variable"; - case NODE_DVAR: - return "local-variable(in-block)"; - - case NODE_GVAR: - if (rb_gvar_defined(node->nd_entry)) { - return "global-variable"; - } - break; - - case NODE_IVAR: - if (rb_ivar_defined(self, node->nd_vid)) { - return "instance-variable"; - } - break; - - case NODE_CVAR: - if (ev_const_defined((NODE*)ruby_frame->cbase, node->nd_vid)) { - return "constant"; - } - break; - - case NODE_COLON2: - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - val = rb_eval(self, node->nd_head); - } - POP_TAG(); - if (state) return 0; - else { - switch (TYPE(val)) { - case T_CLASS: - case T_MODULE: - if (rb_const_defined_at(val, node->nd_mid)) - return "constant"; - default: - if (rb_method_boundp(val, node->nd_mid, 1)) { - return "method"; - } - } - } - break; - - case NODE_NTH_REF: - if (rb_reg_nth_defined(node->nd_nth, MATCH_DATA)) { - sprintf(buf, "$%d", node->nd_nth); - return buf; - } - break; - - case NODE_BACK_REF: - if (rb_reg_nth_defined(0, MATCH_DATA)) { - sprintf(buf, "$%c", node->nd_nth); - return buf; - } - break; - - default: - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - rb_eval(self, node); - } - POP_TAG(); - if (!state) { - return "expression"; - } - break; - } - return 0; -} - -static int handle_rescue _((VALUE,NODE*)); - -static void blk_free(); -static VALUE -rb_obj_is_block(block) - VALUE block; -{ - if (TYPE(block) == T_DATA && RDATA(block)->dfree == blk_free) { - return Qtrue; - } - return Qfalse; -} +NORETURN(static void rb_longjmp(rb_execution_context_t *, enum ruby_tag_type, volatile VALUE, VALUE)); +static VALUE get_errinfo(void); +#define get_ec_errinfo(ec) rb_ec_get_errinfo(ec) static VALUE -rb_obj_is_proc(proc) - VALUE proc; +exc_setup_cause(VALUE exc, VALUE cause) { - if (rb_obj_is_block(proc) && rb_obj_is_kind_of(proc, rb_cProc)) { - return Qtrue; - } - return Qfalse; -} +#if OPT_SUPPORT_JOKE + if (NIL_P(cause)) { + ID id_true_cause; + CONST_ID(id_true_cause, "true_cause"); -static VALUE -set_trace_func(obj, trace) - VALUE obj, trace; -{ - if (NIL_P(trace)) { - trace_func = 0; - return Qnil; + cause = rb_attr_get(rb_eFatal, id_true_cause); + if (NIL_P(cause)) { + cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby"); + rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */ + OBJ_FREEZE(cause); + rb_ivar_set(rb_eFatal, id_true_cause, cause); + } } - if (!rb_obj_is_proc(trace)) { - rb_raise(rb_eTypeError, "trace_func needs to be Proc"); +#endif + if (!NIL_P(cause) && cause != exc) { + rb_ivar_set(exc, id_cause, cause); + if (!rb_ivar_defined(cause, id_cause)) { + rb_ivar_set(cause, id_cause, Qnil); + } } - return trace_func = trace; + return exc; } -static void -call_trace_func(event, file, line, self, id, klass) - char *event; - char *file; - int line; - VALUE self; - ID id; - VALUE klass; /* OK */ +static inline VALUE +exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause) { - int state; - volatile VALUE trace; - struct FRAME *prev; - char *file_save = ruby_sourcefile; - int line_save = ruby_sourceline; - - if (!trace_func) return; - - trace = trace_func; - trace_func = 0; -#ifdef USE_THREAD - rb_thread_critical++; -#endif + int nocause = 0; + int nocircular = 0; - prev = ruby_frame; - PUSH_FRAME(); - *ruby_frame = *_frame.prev; - ruby_frame->prev = prev; - - if (file) { - ruby_frame->line = ruby_sourceline = line; - ruby_frame->file = ruby_sourcefile = file; + if (NIL_P(mesg)) { + mesg = ec->errinfo; + if (INTERNAL_EXCEPTION_P(mesg)) EC_JUMP_TAG(ec, TAG_FATAL); + nocause = 1; } - if (klass) { - if (TYPE(klass) == T_ICLASS || FL_TEST(klass, FL_SINGLETON)) { - klass = self; - } + if (NIL_P(mesg)) { + mesg = rb_exc_new(rb_eRuntimeError, 0, 0); + nocause = 0; + nocircular = 1; + } + if (UNDEF_P(*cause)) { + if (nocause) { + *cause = Qnil; + nocircular = 1; + } + else if (!rb_ivar_defined(mesg, id_cause)) { + *cause = get_ec_errinfo(ec); + } + else { + nocircular = 1; + } } - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - proc_call(trace, rb_ary_new3(6, rb_str_new2(event), - rb_str_new2(ruby_sourcefile), - INT2FIX(ruby_sourceline), - INT2FIX(id), - self?rb_f_binding(self):Qnil, - klass)); + else if (!NIL_P(*cause) && !rb_obj_is_kind_of(*cause, rb_eException)) { + rb_raise(rb_eTypeError, "exception object expected"); } - POP_TAG(); - POP_FRAME(); -#ifdef USE_THREAD - rb_thread_critical--; -#endif - if (!trace_func) trace_func = trace; - ruby_sourceline = line_save; - ruby_sourcefile = file_save; - if (state) JUMP_TAG(state); -} - -static void return_check _((void)); -#define return_value(v) prot_tag->retval = (v) - -static VALUE -rb_eval(self, node) - VALUE self; - NODE * volatile node; -{ - int state; - volatile VALUE result = Qnil; -#ifdef NOBLOCK_RECUR - NODE * volatile next = 0; - NODE * volatile nstack = 0; -#endif - -#define RETURN(v) { result = (v); goto finish; } - - again: - if (!node) RETURN(Qnil); - - switch (nd_type(node)) { - case NODE_BLOCK: -#ifndef NOBLOCK_RECUR - if (!node->nd_next) { - node = node->nd_head; - goto again; - } - while (node) { - result = rb_eval(self, node->nd_head); - node = node->nd_next; - } - break; + if (!nocircular && !NIL_P(*cause) && !UNDEF_P(*cause) && *cause != mesg) { +#if 0 /* maybe critical for some cases */ + rb_exc_check_circular_cause(*cause); #else - if (next) { - nstack = rb_node_newnode(NODE_CREF,next,0,nstack); - } - next = node->nd_next; - node = node->nd_head; - goto again; -#endif - case NODE_POSTEXE: - rb_f_END(); - nd_set_type(node, NODE_NIL); /* exec just once */ - result = Qnil; - break; - - /* begin .. end without clauses */ - case NODE_BEGIN: - node = node->nd_body; - goto again; - - /* nodes for speed-up(default match) */ - case NODE_MATCH: - result = rb_reg_match2(node->nd_head->nd_lit); - break; - - /* nodes for speed-up(literal match) */ - case NODE_MATCH2: - result = rb_reg_match(rb_eval(self,node->nd_recv), - rb_eval(self,node->nd_value)); - break; - - /* nodes for speed-up(literal match) */ - case NODE_MATCH3: - { - VALUE r = rb_eval(self,node->nd_recv); - VALUE l = rb_eval(self,node->nd_value); - if (TYPE(r) == T_STRING) { - result = rb_reg_match(l, r); - } - else { - result = rb_funcall(r, match, 1, l); - } - } - break; - - /* nodes for speed-up(top-level loop for -n/-p) */ - case NODE_OPT_N: - while (!NIL_P(rb_gets())) { - rb_eval(self, node->nd_body); - } - RETURN(Qnil); - - case NODE_SELF: - RETURN(self); - - case NODE_NIL: - RETURN(Qnil); - - case NODE_TRUE: - RETURN(Qtrue); - - case NODE_FALSE: - RETURN(Qfalse); - - case NODE_IF: - ruby_sourceline = nd_line(node); -#ifdef NOBLOCK_RECUR - if (RTEST(result)){ -#else - if (RTEST(rb_eval(self, node->nd_cond))) { -#endif - node = node->nd_body; - } - else { - node = node->nd_else; - } - goto again; - - case NODE_CASE: - { - VALUE val; - -#ifdef NOBLOCK_RECUR - val = result; -#else - val = rb_eval(self, node->nd_head); -#endif - node = node->nd_body; - while (node) { - NODE *tag; - - if (nd_type(node) != NODE_WHEN) { - goto again; - } - tag = node->nd_head; - while (tag) { - if (trace_func) { - call_trace_func("line", tag->nd_file, nd_line(tag), - self, ruby_frame->last_func, 0); - } - ruby_sourcefile = tag->nd_file; - ruby_sourceline = nd_line(tag); - if (nd_type(tag->nd_head) == NODE_WHEN) { - VALUE v = rb_eval(self, tag->nd_head->nd_head); - int i; - - if (TYPE(v) != T_ARRAY) v = rb_Array(v); - for (i=0; i<RARRAY(v)->len; i++) { - if (RTEST(rb_funcall2(RARRAY(v)->ptr[i], eqq, 1, &val))){ - node = node->nd_body; - goto again; - } - } - tag = tag->nd_next; - continue; - } - if (RTEST(rb_funcall2(rb_eval(self, tag->nd_head), eqq, 1, &val))) { - node = node->nd_body; - goto again; - } - tag = tag->nd_next; - } - node = node->nd_next; - } - } - RETURN(Qnil); - - case NODE_WHILE: - PUSH_TAG(PROT_NONE); - switch (state = EXEC_TAG()) { - case 0: - ruby_sourceline = nd_line(node); - if (node->nd_state && !RTEST(rb_eval(self, node->nd_cond))) - goto while_out; - do { - while_redo: - rb_eval(self, node->nd_body); - while_next: - ; - } while (RTEST(rb_eval(self, node->nd_cond))); - break; - - case TAG_REDO: - state = 0; - goto while_redo; - case TAG_NEXT: - state = 0; - goto while_next; - case TAG_BREAK: - state = 0; - default: - break; - } - while_out: - POP_TAG(); - if (state) JUMP_TAG(state); - RETURN(Qnil); - - case NODE_UNTIL: - PUSH_TAG(PROT_NONE); - switch (state = EXEC_TAG()) { - case 0: - if (node->nd_state && RTEST(rb_eval(self, node->nd_cond))) - goto until_out; - do { - until_redo: - rb_eval(self, node->nd_body); - until_next: - ; - } while (!RTEST(rb_eval(self, node->nd_cond))); - break; - - case TAG_REDO: - state = 0; - goto until_redo; - case TAG_NEXT: - state = 0; - goto until_next; - case TAG_BREAK: - state = 0; - default: - break; - } - until_out: - POP_TAG(); - if (state) JUMP_TAG(state); - RETURN(Qnil); - - case NODE_BLOCK_PASS: - result = block_pass(self, node); - break; - - case NODE_ITER: - case NODE_FOR: - { - iter_retry: - PUSH_BLOCK(node->nd_var, node->nd_body); - PUSH_TAG(PROT_FUNC); - - state = EXEC_TAG(); - if (state == 0) { - if (nd_type(node) == NODE_ITER) { - PUSH_ITER(ITER_PRE); - result = rb_eval(self, node->nd_iter); - POP_ITER(); - } - else { - VALUE recv; - char *file = ruby_sourcefile; - int line = ruby_sourceline; - - recv = rb_eval(self, node->nd_iter); - PUSH_ITER(ITER_PRE); - ruby_sourcefile = file; - ruby_sourceline = line; - result = rb_call(CLASS_OF(recv),recv,each,0,0,0); - POP_ITER(); - } - } - else if (_block.tag->dst == state) { - state &= TAG_MASK; - if (state == TAG_RETURN) { - result = prot_tag->retval; - } - } - POP_TAG(); - POP_BLOCK(); - switch (state) { - case 0: - break; - - case TAG_RETRY: - goto iter_retry; - - case TAG_BREAK: - result = Qnil; - break; - case TAG_RETURN: - return_value(result); - /* fall through */ - default: - JUMP_TAG(state); - } - } - break; - - case NODE_BREAK: - JUMP_TAG(TAG_BREAK); - break; - - case NODE_NEXT: - JUMP_TAG(TAG_NEXT); - break; - - case NODE_REDO: - JUMP_TAG(TAG_REDO); - break; - - case NODE_RETRY: - JUMP_TAG(TAG_RETRY); - break; - - case NODE_RESTARGS: - result = rb_eval(self, node->nd_head); - if (TYPE(result) != T_ARRAY) { - result = rb_Array(result); - } - break; - - case NODE_YIELD: - if (node->nd_stts) { - result = rb_eval(self, node->nd_stts); - if (nd_type(node->nd_stts) == NODE_RESTARGS && - RARRAY(result)->len == 1) - { - result = RARRAY(result)->ptr[0]; - } - } - else { - result = Qnil; - } - result = rb_yield_0(result, 0, 0); - break; - - case NODE_RESCUE: - retry_entry: - { - volatile VALUE e_info = rb_errinfo; - - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - result = rb_eval(self, node->nd_head); - } - POP_TAG(); - if (state == TAG_RAISE) { - NODE * volatile resq = node->nd_resq; - - while (resq) { - if (handle_rescue(self, resq)) { - state = 0; - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - result = rb_eval(self, resq->nd_body); - } - POP_TAG(); - if (state == 0) { - rb_errinfo = e_info; - } - else if (state == TAG_RETRY) { - state = 0; - goto retry_entry; - } - break; - } - resq = resq->nd_head; /* next rescue */ - } - } - if (state) JUMP_TAG(state); - if (node->nd_else) { /* no exception raised, else clause given */ - result = rb_eval(self, node->nd_else); - } - } - break; - - case NODE_ENSURE: - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - result = rb_eval(self, node->nd_head); - } - POP_TAG(); - if (node->nd_ensr) { - VALUE retval = prot_tag->retval; /* save retval */ - - rb_eval(self, node->nd_ensr); - return_value(retval); - } - if (state) JUMP_TAG(state); - break; - - case NODE_AND: - result = rb_eval(self, node->nd_1st); - if (!RTEST(result)) break; - node = node->nd_2nd; - goto again; - - case NODE_OR: - result = rb_eval(self, node->nd_1st); - if (RTEST(result)) break; - node = node->nd_2nd; - goto again; - - case NODE_NOT: - if (RTEST(rb_eval(self, node->nd_body))) result = Qfalse; - else result = Qtrue; - break; - - case NODE_DOT2: - case NODE_DOT3: - result = rb_range_new(rb_eval(self, node->nd_beg), rb_eval(self, node->nd_end)); -#if 0 - break; -#else - result = rb_range_new(rb_eval(self, node->nd_beg), rb_eval(self, node->nd_end)); - if (node->nd_state) break; - if (nd_type(node->nd_beg) == NODE_LIT && FIXNUM_P(node->nd_beg->nd_lit) && - nd_type(node->nd_end) == NODE_LIT && FIXNUM_P(node->nd_end->nd_lit)) - { - nd_set_type(node, NODE_LIT); - node->nd_lit = result; - } - else { - node->nd_state = 1; - } -#endif - break; - - case NODE_FLIP2: /* like AWK */ - if (ruby_scope->local_vars == 0) { - rb_bug("unexpected local variable"); - } - if (!RTEST(ruby_scope->local_vars[node->nd_cnt])) { - if (RTEST(rb_eval(self, node->nd_beg))) { - ruby_scope->local_vars[node->nd_cnt] = - RTEST(rb_eval(self, node->nd_end))?Qfalse:Qtrue; - result = Qtrue; - } - else { - result = Qfalse; - } - } - else { - if (RTEST(rb_eval(self, node->nd_end))) { - ruby_scope->local_vars[node->nd_cnt] = Qfalse; - } - result = Qtrue; - } - break; - - case NODE_FLIP3: /* like SED */ - if (ruby_scope->local_vars == 0) { - rb_bug("unexpected local variable"); - } - if (!RTEST(ruby_scope->local_vars[node->nd_cnt])) { - result = RTEST(rb_eval(self, node->nd_beg)); - ruby_scope->local_vars[node->nd_cnt] = result; - } - else { - if (RTEST(rb_eval(self, node->nd_end))) { - ruby_scope->local_vars[node->nd_cnt] = Qfalse; - } - result = Qtrue; - } - break; - - case NODE_RETURN: - if (node->nd_stts) { - return_value(rb_eval(self, node->nd_stts)); - } - return_check(); - JUMP_TAG(TAG_RETURN); - break; - - case NODE_ARGSCAT: - result = rb_ary_concat(rb_eval(self, node->nd_head), - rb_eval(self, node->nd_body)); - break; - - case NODE_CALL: - { - VALUE recv; - int argc; VALUE *argv; /* used in SETUP_ARGS */ - TMP_PROTECT; - - BEGIN_CALLARGS; -#ifdef NOBLOCK_RECUR_incomplete - printf("mid %s recv: ", rb_id2name(node->nd_mid)); - rb_p(result); - recv = result; -#else - recv = rb_eval(self, node->nd_recv); + VALUE c = *cause; + while (!NIL_P(c)) { + if (c == mesg) { + rb_raise(rb_eArgError, "circular causes"); + } + if (THROW_DATA_P(c)) { + break; + } + c = rb_attr_get(c, id_cause); + } #endif - SETUP_ARGS(node->nd_args); - END_CALLARGS; - - result = rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,0); - } - break; - - case NODE_FCALL: - { - int argc; VALUE *argv; /* used in SETUP_ARGS */ - TMP_PROTECT; - - BEGIN_CALLARGS; - SETUP_ARGS(node->nd_args); - END_CALLARGS; - - result = rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1); - } - break; - - case NODE_VCALL: - result = rb_call(CLASS_OF(self),self,node->nd_mid,0,0,2); - break; - - case NODE_SUPER: - case NODE_ZSUPER: - { - int argc; VALUE *argv; /* used in SETUP_ARGS */ - TMP_PROTECT; - - if (ruby_frame->last_class == 0) { - rb_raise(rb_eNameError, "superclass method `%s' disabled", - rb_id2name(ruby_frame->last_func)); - } - if (nd_type(node) == NODE_ZSUPER) { - argc = ruby_frame->argc; - argv = ruby_frame->argv; - } - else { - BEGIN_CALLARGS; - SETUP_ARGS(node->nd_args); - END_CALLARGS; - } - - PUSH_ITER(ruby_iter->iter?ITER_PRE:ITER_NOT); - result = rb_call(RCLASS(ruby_frame->last_class)->super, - ruby_frame->self, ruby_frame->last_func, - argc, argv, 3); - POP_ITER(); - } - break; - - case NODE_SCOPE: - { - VALUE save = ruby_frame->cbase; - - PUSH_SCOPE(); - PUSH_TAG(PROT_NONE); - if (node->nd_rval) ruby_frame->cbase = node->nd_rval; - if (node->nd_tbl) { - VALUE *vars = ALLOCA_N(VALUE, node->nd_tbl[0]+1); - *vars++ = (VALUE)node; - ruby_scope->local_vars = vars; - rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]); - ruby_scope->local_tbl = node->nd_tbl; - } - else { - ruby_scope->local_vars = 0; - ruby_scope->local_tbl = 0; - } - if ((state = EXEC_TAG()) == 0) { - result = rb_eval(self, node->nd_next); - } - POP_TAG(); - POP_SCOPE(); - ruby_frame->cbase = save; - if (state) JUMP_TAG(state); - } - break; - - case NODE_OP_ASGN1: - { - int argc; VALUE *argv; /* used in SETUP_ARGS */ - VALUE recv, val; - NODE *rval; - TMP_PROTECT; - - recv = rb_eval(self, node->nd_recv); - rval = node->nd_args->nd_head; - SETUP_ARGS(node->nd_args->nd_next); - val = rb_funcall2(recv, aref, argc-1, argv); - if (node->nd_mid == 0) { /* OR */ - if (RTEST(val)) break; - val = rb_eval(self, rval); - } - else if (node->nd_mid == 1) { /* AND */ - if (!RTEST(val)) break; - val = rb_eval(self, rval); - } - else { - val = rb_funcall(val, node->nd_mid, 1, rb_eval(self, rval)); - } - argv[argc-1] = val; - val = rb_funcall2(recv, aset, argc, argv); - result = val; - } - break; - - case NODE_OP_ASGN2: - { - ID id = node->nd_next->nd_vid; - VALUE recv, val; - - recv = rb_eval(self, node->nd_recv); - val = rb_funcall(recv, id, 0); - if (node->nd_next->nd_mid == 0) { /* OR */ - if (RTEST(val)) break; - val = rb_eval(self, node->nd_value); - } - else if (node->nd_next->nd_mid == 1) { /* AND */ - if (!RTEST(val)) break; - val = rb_eval(self, node->nd_value); - } - else { - val = rb_funcall(val, node->nd_next->nd_mid, 1, - rb_eval(self, node->nd_value)); - } - - rb_funcall2(recv, node->nd_next->nd_aid, 1, &val); - result = val; - } - break; - - case NODE_OP_ASGN_AND: - result = rb_eval(self, node->nd_head); - if (RTEST(result)) { - result = rb_eval(self, node->nd_value); - } - break; - - case NODE_OP_ASGN_OR: - result = rb_eval(self, node->nd_head); - if (!RTEST(result)) { - result = rb_eval(self, node->nd_value); - } - break; - - case NODE_MASGN: - result = massign(self, node, rb_eval(self, node->nd_value)); - break; - - case NODE_LASGN: - if (ruby_scope->local_vars == 0) - rb_bug("unexpected local variable assignment"); - result = rb_eval(self, node->nd_value); - ruby_scope->local_vars[node->nd_cnt] = result; - break; - - case NODE_DASGN: - result = rb_eval(self, node->nd_value); - rb_dvar_asgn(node->nd_vid, result); - break; - - case NODE_DASGN_PUSH: - result = rb_eval(self, node->nd_value); - dvar_asgn_push(node->nd_vid, result); - break; - - case NODE_GASGN: - result = rb_eval(self, node->nd_value); - rb_gvar_set(node->nd_entry, result); - break; - - case NODE_IASGN: - result = rb_eval(self, node->nd_value); - rb_ivar_set(self, node->nd_vid, result); - break; - - case NODE_CASGN: - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no class/module to define constant"); - } - result = rb_eval(self, node->nd_value); - /* check for static scope constants */ - if (RTEST(rb_verbose) && - ev_const_defined((NODE*)ruby_frame->cbase, node->nd_vid)) { - if (RTEST(rb_verbose)) { - rb_warning("already initialized constant %s", - rb_id2name(node->nd_vid)); - } - } - rb_const_set(ruby_class, node->nd_vid, result); - break; - - case NODE_LVAR: - if (ruby_scope->local_vars == 0) { - rb_bug("unexpected local variable"); - } - result = ruby_scope->local_vars[node->nd_cnt]; - break; - - case NODE_DVAR: - result = rb_dvar_ref(node->nd_vid); - break; - - case NODE_GVAR: - result = rb_gvar_get(node->nd_entry); - break; - - case NODE_IVAR: - result = rb_ivar_get(self, node->nd_vid); - break; - - case NODE_CVAR: - result = ev_const_get((NODE*)ruby_frame->cbase, node->nd_vid); - break; - - case NODE_BLOCK_ARG: - if (ruby_scope->local_vars == 0) - rb_bug("unexpected block argument"); - if (rb_iterator_p()) { - result = rb_f_lambda(); - ruby_scope->local_vars[node->nd_cnt] = result; - } - else { - result = Qnil; - } - break; - - case NODE_COLON2: - { - VALUE klass; - - klass = rb_eval(self, node->nd_head); - switch (TYPE(klass)) { - case T_CLASS: - case T_MODULE: - break; - default: - return rb_funcall(klass, node->nd_mid, 0, 0); - } - result = rb_const_get_at(klass, node->nd_mid); - } - break; - - case NODE_COLON3: - result = rb_const_get_at(rb_cObject, node->nd_mid); - break; - - case NODE_NTH_REF: - result = rb_reg_nth_match(node->nd_nth, MATCH_DATA); - break; - - case NODE_BACK_REF: - switch (node->nd_nth) { - case '&': - result = rb_reg_last_match(MATCH_DATA); - break; - case '`': - result = rb_reg_match_pre(MATCH_DATA); - break; - case '\'': - result = rb_reg_match_post(MATCH_DATA); - break; - case '+': - result = rb_reg_match_last(MATCH_DATA); - break; - default: - rb_bug("unexpected back-ref"); - } - break; - - case NODE_HASH: - { - NODE *list; - VALUE hash = rb_hash_new(); - VALUE key, val; - - list = node->nd_head; - while (list) { - key = rb_eval(self, list->nd_head); - list = list->nd_next; - if (list == 0) - rb_bug("odd number list for Hash"); - val = rb_eval(self, list->nd_head); - list = list->nd_next; - rb_hash_aset(hash, key, val); - } - result = hash; - } - break; - - case NODE_ZARRAY: /* zero length list */ - result = rb_ary_new(); - break; - - case NODE_ARRAY: - { - VALUE ary; - int i; - - i = node->nd_alen; - ary = rb_ary_new2(i); - for (i=0;node;node=node->nd_next) { - RARRAY(ary)->ptr[i++] = rb_eval(self, node->nd_head); - RARRAY(ary)->len = i; - } - - result = ary; - } - break; - - case NODE_STR: - result = rb_str_new3(node->nd_lit); - break; - - case NODE_DSTR: - case NODE_DXSTR: - case NODE_DREGX: - case NODE_DREGX_ONCE: - { - VALUE str, str2; - NODE *list = node->nd_next; - - str = rb_str_new3(node->nd_lit); - while (list) { - if (list->nd_head) { - switch (nd_type(list->nd_head)) { - case NODE_STR: - str2 = list->nd_head->nd_lit; - break; - case NODE_EVSTR: - rb_in_eval++; - list->nd_head = compile(list->nd_head->nd_lit,0); - ruby_eval_tree = 0; - rb_in_eval--; - if (ruby_nerrs > 0) { - compile_error("string expansion"); - } - /* fall through */ - default: - str2 = rb_eval(self, list->nd_head); - str2 = rb_obj_as_string(str2); - break; - } - rb_str_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len); - if (OBJ_TAINTED(str2)) OBJ_TAINT(str); - } - list = list->nd_next; - } - switch (nd_type(node)) { - case NODE_DREGX: - result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len, - node->nd_cflag); - break; - case NODE_DREGX_ONCE: /* regexp expand once */ - result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len, - node->nd_cflag); - nd_set_type(node, NODE_LIT); - node->nd_lit = result; - break; - case NODE_DXSTR: - result = rb_funcall(self, '`', 1, str); - break; - default: - result = str; - break; - } - } - break; - - case NODE_XSTR: - result = rb_funcall(self, '`', 1, node->nd_lit); - break; - - case NODE_LIT: - result = node->nd_lit; - break; - - case NODE_ATTRSET: - if (ruby_frame->argc != 1) - rb_raise(rb_eArgError, "Wrong # of arguments(%d for 1)", - ruby_frame->argc); - result = rb_ivar_set(self, node->nd_vid, ruby_frame->argv[0]); - break; - - case NODE_DEFN: - if (node->nd_defn) { - NODE *body; - VALUE origin; - int noex; - - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no class to add method"); - } - if (ruby_class == rb_cObject && node->nd_mid == init) { - rb_warn("re-defining Object#initialize may cause infinite loop"); - } - body = search_method(ruby_class, node->nd_mid, &origin); - if (body) { - if (origin == ruby_class) { - if (safe_level >= 3) { - rb_raise(rb_eSecurityError, "re-defining method prohibited"); - } - if (RTEST(rb_verbose)) { - rb_warning("discarding old %s", rb_id2name(node->nd_mid)); - } - } - rb_clear_cache_by_id(node->nd_mid); - } - - if (SCOPE_TEST(SCOPE_PRIVATE) || node->nd_mid == init) { - noex = NOEX_PRIVATE; - } - else if (SCOPE_TEST(SCOPE_PROTECTED)) { - noex = NOEX_PROTECTED; - } - else { - noex = NOEX_PUBLIC; - } - if (body && origin == ruby_class && body->nd_noex & NOEX_UNDEF) { - noex |= NOEX_UNDEF; - } - rb_add_method(ruby_class, node->nd_mid, node->nd_defn, noex); - if (scope_vmode == SCOPE_MODFUNC) { - rb_add_method(rb_singleton_class(ruby_class), - node->nd_mid, node->nd_defn, NOEX_PUBLIC); - rb_funcall(ruby_class, rb_intern("singleton_method_added"), - 1, INT2FIX(node->nd_mid)); - } - if (FL_TEST(ruby_class, FL_SINGLETON)) { - rb_funcall(rb_iv_get(ruby_class, "__attached__"), - rb_intern("singleton_method_added"), - 1, INT2FIX(node->nd_mid)); - } - else { - rb_funcall(ruby_class, rb_intern("method_added"), - 1, INT2FIX(node->nd_mid)); - } - result = Qnil; - } - break; - - case NODE_DEFS: - if (node->nd_defn) { - VALUE recv = rb_eval(self, node->nd_recv); - VALUE klass; - NODE *body = 0; - - if (FIXNUM_P(recv)) { - rb_raise(rb_eTypeError, "Can't define method \"%s\" for Fixnum", - rb_id2name(node->nd_mid)); - } - if (NIL_P(recv)) { - rb_raise(rb_eTypeError, "Can't define method \"%s\" for nil", - rb_id2name(node->nd_mid)); - } - if (rb_special_const_p(recv)) { - rb_raise(rb_eTypeError, - "Can't define method \"%s\" for special constants", - rb_id2name(node->nd_mid)); - } - - if (rb_safe_level() >= 4 && !FL_TEST(recv, FL_TAINT)) { - rb_raise(rb_eSecurityError, "can't define singleton method"); - } - klass = rb_singleton_class(recv); - if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &body)) { - if (safe_level >= 3) { - rb_raise(rb_eSecurityError, "re-defining method prohibited"); - } - if (RTEST(rb_verbose)) { - rb_warning("redefine %s", rb_id2name(node->nd_mid)); - } - } - rb_clear_cache_by_id(node->nd_mid); - rb_add_method(klass, node->nd_mid, node->nd_defn, - NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0)); - rb_funcall(recv, rb_intern("singleton_method_added"), - 1, INT2FIX(node->nd_mid)); - result = Qnil; - } - break; - - case NODE_UNDEF: - { - VALUE origin; - NODE *body; - - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no class to undef method"); - } - if (ruby_class == rb_cObject) { - rb_secure(4); - } - body = search_method(ruby_class, node->nd_mid, &origin); - if (!body || !body->nd_body) { - char *s0 = " class"; - VALUE klass = ruby_class; - - if (FL_TEST(ruby_class, FL_SINGLETON)) { - VALUE obj = rb_iv_get(ruby_class, "__attached__"); - switch (TYPE(obj)) { - case T_MODULE: - case T_CLASS: - klass = obj; - s0 = ""; - } - } - rb_raise(rb_eNameError, "undefined method `%s' for%s `%s'", - rb_id2name(node->nd_mid),s0,rb_class2name(klass)); - } - rb_clear_cache_by_id(node->nd_mid); - rb_add_method(ruby_class, node->nd_mid, 0, NOEX_PUBLIC); - result = Qnil; - } - break; - - case NODE_ALIAS: - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no class to make alias"); - } - rb_alias(ruby_class, node->nd_new, node->nd_old); - rb_funcall(ruby_class, rb_intern("method_added"), - 1, INT2FIX(node->nd_mid)); - result = Qnil; - break; - - case NODE_VALIAS: - rb_alias_variable(node->nd_new, node->nd_old); - result = Qnil; - break; - - case NODE_CLASS: - { - VALUE super, klass, tmp; - - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no outer class/module"); - } - if (node->nd_super) { - super = superclass(self, node->nd_super); - } - else { - super = 0; - } - - klass = 0; - if (rb_const_defined_at(ruby_class, node->nd_cname) && - (ruby_class != rb_cObject || !rb_autoload_defined(node->nd_cname))) { - klass = rb_const_get_at(ruby_class, node->nd_cname); - } - if (ruby_wrapper && rb_const_defined_at(rb_cObject, node->nd_cname)) { - klass = rb_const_get_at(rb_cObject, node->nd_cname); - } - if (klass) { - if (TYPE(klass) != T_CLASS) { - rb_raise(rb_eTypeError, "%s is not a class", - rb_id2name(node->nd_cname)); - } - if (super) { - tmp = RCLASS(klass)->super; - if (FL_TEST(tmp, FL_SINGLETON)) { - tmp = RCLASS(tmp)->super; - } - while (TYPE(tmp) == T_ICLASS) { - tmp = RCLASS(tmp)->super; - } - if (tmp != super) { - rb_raise(rb_eTypeError, "superclass mismatch for %s", - rb_id2name(node->nd_cname)); - } - } - if (safe_level >= 3) { - rb_raise(rb_eSecurityError, "extending class prohibited"); - } - rb_clear_cache(); - } - else { - if (!super) super = rb_cObject; - klass = rb_define_class_id(node->nd_cname, super); - rb_const_set(ruby_class, node->nd_cname, klass); - rb_set_class_path(klass,ruby_class,rb_id2name(node->nd_cname)); - rb_obj_call_init(klass); - } - if (ruby_wrapper) { - rb_extend_object(klass, ruby_wrapper); - rb_include_module(klass, ruby_wrapper); - } - - result = module_setup(klass, node->nd_body); - } - break; - - case NODE_MODULE: - { - VALUE module; - - if (NIL_P(ruby_class)) { - rb_raise(rb_eTypeError, "no outer class/module"); - } - module = 0; - if (rb_const_defined_at(ruby_class, node->nd_cname) && - (ruby_class != rb_cObject || - !rb_autoload_defined(node->nd_cname))) { - module = rb_const_get_at(ruby_class, node->nd_cname); - } - if (ruby_wrapper && rb_const_defined_at(rb_cObject, node->nd_cname)) { - module = rb_const_get_at(rb_cObject, node->nd_cname); - } - if (module) { - if (TYPE(module) != T_MODULE) { - rb_raise(rb_eTypeError, "%s is not a module", - rb_id2name(node->nd_cname)); - } - if (safe_level >= 3) { - rb_raise(rb_eSecurityError, "extending module prohibited"); - } - } - else { - module = rb_define_module_id(node->nd_cname); - rb_const_set(ruby_class, node->nd_cname, module); - rb_set_class_path(module,ruby_class,rb_id2name(node->nd_cname)); - rb_obj_call_init(module); - } - if (ruby_wrapper) { - rb_extend_object(module, ruby_wrapper); - rb_include_module(module, ruby_wrapper); - } - - result = module_setup(module, node->nd_body); - } - break; - - case NODE_SCLASS: - { - VALUE klass; - - klass = rb_eval(self, node->nd_recv); - if (FIXNUM_P(klass)) { - rb_raise(rb_eTypeError, "No virtual class for Fixnums"); - } - if (NIL_P(klass)) { - rb_raise(rb_eTypeError, "No virtual class for nil"); - } - if (rb_special_const_p(klass)) { - rb_raise(rb_eTypeError, "No virtual class for special constants"); - } - if (FL_TEST(CLASS_OF(klass), FL_SINGLETON)) { - rb_clear_cache(); - } - klass = rb_singleton_class(klass); - if (ruby_wrapper) { - rb_extend_object(klass, ruby_wrapper); - rb_include_module(klass, ruby_wrapper); - } - - result = module_setup(klass, node->nd_body); - } - break; - - case NODE_DEFINED: - { - char buf[20]; - char *desc = is_defined(self, node->nd_head, buf); - - if (desc) result = rb_str_new2(desc); - else result = Qfalse; - } - break; - - case NODE_NEWLINE: - ruby_sourcefile = node->nd_file; - ruby_sourceline = node->nd_nth; - if (trace_func) { - call_trace_func("line", ruby_sourcefile, ruby_sourceline, - self, ruby_frame->last_func, 0); - } - node = node->nd_next; - goto again; - - default: - rb_bug("unknown node type %d", nd_type(node)); - } - finish: - CHECK_INTS; -#ifdef NOBLOCK_RECUR - if (next) { - node = next; - next = 0; - goto again; } - if (nstack) { - node = nstack->nd_head; - nstack = nstack->nd_next; - goto again; - } -#endif - return result; + return mesg; } -static VALUE -module_setup(module, node) - VALUE module; - NODE * volatile node; +static void +setup_exception(rb_execution_context_t *ec, enum ruby_tag_type tag, volatile VALUE mesg, VALUE cause) { - int state; - VALUE save = ruby_frame->cbase; - VALUE result; /* OK */ - char *file = ruby_sourcefile; - int line = ruby_sourceline; - TMP_PROTECT; - - /* fill c-ref */ - node->nd_clss = module; - node = node->nd_body; - - PUSH_CLASS(); - ruby_class = module; - PUSH_SCOPE(); - - if (node->nd_rval) ruby_frame->cbase = node->nd_rval; - if (node->nd_tbl) { - VALUE *vars = TMP_ALLOC(node->nd_tbl[0]+1); - *vars++ = (VALUE)node; - ruby_scope->local_vars = vars; - rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]); - ruby_scope->local_tbl = node->nd_tbl; - } - else { - ruby_scope->local_vars = 0; - ruby_scope->local_tbl = 0; + VALUE e; + int line; + const char *file = rb_source_location_cstr(&line); + const char *const volatile file0 = file; + + if ((file && !NIL_P(mesg)) || !UNDEF_P(cause)) { + volatile int state = 0; + + EC_PUSH_TAG(ec); + if (EC_EXEC_TAG() == TAG_NONE && !(state = rb_ec_set_raised(ec))) { + VALUE bt = rb_get_backtrace(mesg); + if (!NIL_P(bt) || UNDEF_P(cause)) { + if (OBJ_FROZEN(mesg)) { + mesg = rb_obj_dup(mesg); + } + } + if (!UNDEF_P(cause) && !THROW_DATA_P(cause)) { + exc_setup_cause(mesg, cause); + } + if (NIL_P(bt)) { + VALUE at = rb_ec_backtrace_object(ec); + rb_ivar_set(mesg, idBt_locations, at); + set_backtrace(mesg, at); + } + rb_ec_reset_raised(ec); + } + EC_POP_TAG(); + file = file0; + if (state) goto fatal; } - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - if (trace_func) { - call_trace_func("class", file, line, - ruby_class, ruby_frame->last_func, 0); - } - result = rb_eval(ruby_class, node->nd_next); + if (!NIL_P(mesg)) { + ec->errinfo = mesg; + } + + if (RTEST(ruby_debug) && !NIL_P(e = ec->errinfo) && + !rb_obj_is_kind_of(e, rb_eSystemExit)) { + enum ruby_tag_type state; + + mesg = e; + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + ec->errinfo = Qnil; + e = rb_obj_as_string(mesg); + ec->errinfo = mesg; + if (file && line) { + e = rb_sprintf("Exception '%"PRIsVALUE"' at %s:%d - %"PRIsVALUE"\n", + rb_obj_class(mesg), file, line, e); + } + else if (file) { + e = rb_sprintf("Exception '%"PRIsVALUE"' at %s - %"PRIsVALUE"\n", + rb_obj_class(mesg), file, e); + } + else { + e = rb_sprintf("Exception '%"PRIsVALUE"' - %"PRIsVALUE"\n", + rb_obj_class(mesg), e); + } + warn_print_str(e); + } + EC_POP_TAG(); + if (state == TAG_FATAL && ec->errinfo == exception_error) { + ec->errinfo = mesg; + } + else if (state) { + rb_ec_reset_raised(ec); + EC_JUMP_TAG(ec, state); + } } - POP_TAG(); - POP_SCOPE(); - POP_CLASS(); - ruby_frame->cbase = save; - if (trace_func) { - call_trace_func("end", file, line, 0, ruby_frame->last_func, 0); + if (rb_ec_set_raised(ec)) { + goto fatal; } - if (state) JUMP_TAG(state); - - return result; -} -int -rb_respond_to(obj, id) - VALUE obj; - ID id; -{ - if (rb_method_boundp(CLASS_OF(obj), id, 0)) { - return Qtrue; + if (tag != TAG_FATAL) { + RUBY_DTRACE_HOOK(RAISE, rb_obj_classname(ec->errinfo)); + EXEC_EVENT_HOOK(ec, RUBY_EVENT_RAISE, ec->cfp->self, 0, 0, 0, mesg); } - return Qfalse; -} - -static VALUE -rb_obj_respond_to(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE mid, priv; - ID id; - - rb_scan_args(argc, argv, "11", &mid, &priv); - id = rb_to_id(mid); - if (rb_method_boundp(CLASS_OF(obj), id, !RTEST(priv))) { - return Qtrue; - } - return Qfalse; -} + return; -static VALUE -rb_mod_method_defined(mod, mid) - VALUE mod, mid; -{ - if (rb_method_boundp(mod, rb_to_id(mid), 1)) { - return Qtrue; - } - return Qfalse; + fatal: + ec->errinfo = exception_error; + rb_ec_reset_raised(ec); + EC_JUMP_TAG(ec, TAG_FATAL); } +/*! \private */ void -rb_exit(status) - int status; +rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause) { - if (prot_tag) { - exit_status = status; - rb_exc_raise(rb_exc_new(rb_eSystemExit, 0, 0)); + if (UNDEF_P(cause)) { + cause = get_ec_errinfo(ec); } - exit(status); -} - -static VALUE -rb_f_exit(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE status; + if (cause != mesg) { + if (THROW_DATA_P(cause)) { + cause = Qnil; + } - rb_secure(4); - if (rb_scan_args(argc, argv, "01", &status) == 1) { - status = NUM2INT(status); + rb_ivar_set(mesg, id_cause, cause); } - else { - status = 0; - } - rb_exit(status); - return Qnil; /* not reached */ } static void -rb_abort() -{ - if (rb_errinfo) { - error_print(); - } - rb_exit(1); -} - -static VALUE -rb_f_abort() +rb_longjmp(rb_execution_context_t *ec, enum ruby_tag_type tag, volatile VALUE mesg, VALUE cause) { - rb_secure(4); - rb_abort(); - return Qnil; /* not reached */ + mesg = exc_setup_message(ec, mesg, &cause); + setup_exception(ec, tag, mesg, cause); + rb_ec_raised_clear(ec); + EC_JUMP_TAG(ec, tag); } -void -rb_iter_break() -{ - JUMP_TAG(TAG_BREAK); -} +static VALUE make_exception(int argc, const VALUE *argv, int isstr); -static void rb_longjmp _((int, VALUE)) NORETURN; -static VALUE make_backtrace _((void)); +NORETURN(static void rb_exc_exception(VALUE mesg, enum ruby_tag_type tag, VALUE cause)); static void -rb_longjmp(tag, mesg) - int tag; - VALUE mesg; +rb_exc_exception(VALUE mesg, enum ruby_tag_type tag, VALUE cause) { - VALUE at; - - if (NIL_P(mesg)) mesg = rb_errinfo; - if (NIL_P(mesg)) { - mesg = rb_exc_new(rb_eRuntimeError, 0, 0); - } - - if (ruby_sourcefile && !NIL_P(mesg)) { - at = get_backtrace(mesg); - if (NIL_P(at)) { - at = make_backtrace(); - set_backtrace(mesg, at); - } - } if (!NIL_P(mesg)) { - rb_errinfo = mesg; - } - - if (RTEST(rb_debug) && !NIL_P(rb_errinfo) - && !rb_obj_is_kind_of(rb_errinfo, rb_eSystemExit)) { - fprintf(stderr, "Exception `%s' at %s:%d\n", - rb_class2name(CLASS_OF(rb_errinfo)), - ruby_sourcefile, ruby_sourceline); - } - - rb_trap_restore_mask(); - if (trace_func && tag != TAG_FATAL) { - call_trace_func("raise", ruby_sourcefile, ruby_sourceline, - ruby_frame->self, ruby_frame->last_func, 0); + mesg = make_exception(1, &mesg, FALSE); } - if (!prot_tag) { - error_print(); - } - JUMP_TAG(tag); + rb_longjmp(GET_EC(), tag, mesg, cause); } +/** + * Raises an exception in the current thread. + * @param[in] mesg an Exception class or an `Exception` object. + * @exception always raises an instance of the given exception class or + * the given `Exception` object. + * @ingroup exception + */ void -rb_exc_raise(mesg) - VALUE mesg; +rb_exc_raise(VALUE mesg) { - rb_longjmp(TAG_RAISE, mesg); + rb_exc_exception(mesg, TAG_RAISE, Qundef); } +/*! + * Raises a fatal error in the current thread. + * + * Same as rb_exc_raise() but raises a fatal error, which Ruby codes + * cannot rescue. + * \ingroup exception + */ void -rb_exc_fatal(mesg) - VALUE mesg; +rb_exc_fatal(VALUE mesg) { - rb_longjmp(TAG_FATAL, mesg); + rb_exc_exception(mesg, TAG_FATAL, Qnil); } void -rb_interrupt() +rb_interrupt(void) { - rb_raise(rb_eInterrupt, ""); + rb_exc_raise(rb_exc_new(rb_eInterrupt, 0, 0)); } -static VALUE -rb_f_raise(argc, argv) - int argc; - VALUE *argv; +static int +extract_raise_options(int argc, VALUE *argv, VALUE *cause) { - VALUE mesg; - int n; - - mesg = Qnil; - switch (argc) { - case 0: - mesg = Qnil; - break; - case 1: - if (NIL_P(argv[0])) break; - if (TYPE(argv[0]) == T_STRING) { - mesg = rb_exc_new3(rb_eRuntimeError, argv[0]); - break; - } - mesg = rb_funcall(argv[0], rb_intern("exception"), 0, 0); - break; - case 3: - case 2: - mesg = rb_funcall(argv[0], rb_intern("exception"), 1, argv[1]); - break; - default: - rb_raise(rb_eArgError, "wrong # of arguments"); - break; - } - if (!NIL_P(mesg)) { - if (!rb_obj_is_kind_of(mesg, rb_eException)) - rb_raise(rb_eTypeError, "exception object expected"); - set_backtrace(mesg, (argc>2)?argv[2]:Qnil); + // Keyword arguments: + static ID keywords[1] = {0}; + if (!keywords[0]) { + CONST_ID(keywords[0], "cause"); } - PUSH_FRAME(); /* fake frame */ - *ruby_frame = *_frame.prev->prev; - rb_longjmp(TAG_RAISE, mesg); - POP_FRAME(); + if (argc > 0) { + VALUE options; + argc = rb_scan_args(argc, argv, "*:", NULL, &options); - return Qnil; /* not reached */ -} + if (!NIL_P(options)) { + if (!RHASH_EMPTY_P(options)) { + // Extract optional cause keyword argument, leaving any other options alone: + rb_get_kwargs(options, keywords, 0, -2, cause); -void -rb_jump_tag(tag) - int tag; -{ - JUMP_TAG(tag); -} - -int -rb_iterator_p() -{ - if (ruby_frame->iter) return Qtrue; - return Qfalse; -} - -static VALUE -rb_f_iterator_p() -{ - if (ruby_frame->prev && ruby_frame->prev->iter) return Qtrue; - return Qfalse; -} - -static VALUE -rb_yield_0(val, self, klass) - VALUE val, self, klass; /* OK */ -{ - NODE *node; - volatile VALUE result = Qnil; - struct BLOCK *block; - struct SCOPE *old_scope; - struct FRAME frame; - int state; - static unsigned serial = 1; - - if (!ruby_frame->iter || !ruby_block) { - rb_raise(rb_eLocalJumpError, "yield called out of iterator"); + // If there were any other options, add them back to the arguments: + if (!RHASH_EMPTY_P(options)) argv[argc++] = options; + } + } } - PUSH_VARS(); - PUSH_CLASS(); - block = ruby_block; - frame = block->frame; - frame.prev = ruby_frame; - ruby_frame = &(frame); - old_scope = ruby_scope; - ruby_scope = block->scope; - ruby_block = block->prev; - ruby_dyna_vars = block->d_vars; - ruby_class = klass?klass:block->klass; - if (!self) self = block->self; - node = block->body; - if (block->var) { - if (nd_type(block->var) == NODE_MASGN) - massign(self, block->var, val); - else - assign(self, block->var, val); - } - PUSH_ITER(block->iter); - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - redo: - if (!node) { - result = Qnil; - } - else if (nd_type(node) == NODE_CFUNC) { - result = (*node->nd_cfnc)(val, node->nd_tval, self); - } - else { - result = rb_eval(self, node); - } - } - else { - switch (state) { - case TAG_REDO: - state = 0; - goto redo; - case TAG_NEXT: - state = 0; - result = Qnil; - break; - case TAG_BREAK: - case TAG_RETURN: - state |= (serial++ << 8); - state |= 0x10; - block->tag->dst = state; - break; - default: - break; - } - } - POP_TAG(); - POP_ITER(); - POP_CLASS(); - POP_VARS(); - ruby_block = block; - ruby_frame = ruby_frame->prev; -#ifdef USE_THREAD - if (FL_TEST(ruby_scope, SCOPE_DONT_RECYCLE)) - FL_SET(old_scope, SCOPE_DONT_RECYCLE); -#endif - ruby_scope = old_scope; - if (state) JUMP_TAG(state); - return result; + return argc; } +/** + * Complete exception setup for cross-context raises (Thread#raise, Fiber#raise). + * Handles keyword extraction, validation, exception creation, and cause assignment. + * + * @param[in] argc Number of arguments + * @param[in] argv Argument array (will be modified for keyword extraction) + * @return Prepared exception object with cause applied + */ VALUE -rb_yield(val) - VALUE val; -{ - return rb_yield_0(val, 0, 0); -} - -static VALUE -rb_f_loop() +rb_exception_setup(int argc, VALUE *argv) { - for (;;) { rb_yield_0(Qnil, 0, 0); } -} - -static VALUE -massign(self, node, val) - VALUE self; - NODE *node; - VALUE val; -{ - NODE *list; - int i, len; - - list = node->nd_head; - - if (val) { - if (TYPE(val) != T_ARRAY) { - val = rb_Array(val); - } - len = RARRAY(val)->len; - for (i=0; list && i<len; i++) { - assign(self, list->nd_head, RARRAY(val)->ptr[i]); - list = list->nd_next; - } - if (node->nd_args) { - if (!list && i<len) { - assign(self, node->nd_args, rb_ary_new4(len-i, RARRAY(val)->ptr+i)); - } - else { - assign(self, node->nd_args, rb_ary_new2(0)); - } - } - } - else if (node->nd_args) { - assign(self, node->nd_args, Qnil); - } - while (list) { - assign(self, list->nd_head, Qnil); - list = list->nd_next; - } - return val; -} + rb_execution_context_t *ec = GET_EC(); -static void -assign(self, lhs, val) - VALUE self; - NODE *lhs; - VALUE val; -{ - switch (nd_type(lhs)) { - case NODE_GASGN: - rb_gvar_set(lhs->nd_entry, val); - break; - - case NODE_IASGN: - rb_ivar_set(self, lhs->nd_vid, val); - break; - - case NODE_LASGN: - if (ruby_scope->local_vars == 0) - rb_bug("unexpected iterator variable assignment"); - ruby_scope->local_vars[lhs->nd_cnt] = val; - break; - - case NODE_DASGN: - rb_dvar_asgn(lhs->nd_vid, val); - break; - - case NODE_DASGN_PUSH: - dvar_asgn_push(lhs->nd_vid, val); - break; - - case NODE_CASGN: - rb_const_set(ruby_class, lhs->nd_vid, val); - break; - - case NODE_MASGN: - massign(self, lhs, val); - break; - - case NODE_CALL: - { - VALUE recv; - recv = rb_eval(self, lhs->nd_recv); - if (!lhs->nd_args->nd_head) { - /* attr set */ - rb_call(CLASS_OF(recv), recv, lhs->nd_mid, 1, &val, 0); - } - else { - /* array set */ - VALUE args; - - args = rb_eval(self, lhs->nd_args); - RARRAY(args)->ptr[RARRAY(args)->len-1] = val; - rb_call(CLASS_OF(recv), recv, lhs->nd_mid, - RARRAY(args)->len, RARRAY(args)->ptr, 0); - } - } - break; + // Extract cause keyword argument: + VALUE cause = Qundef; + argc = extract_raise_options(argc, argv, &cause); - default: - rb_bug("bug in variable assignment"); - break; + // Validate cause-only case: + if (argc == 0 && !UNDEF_P(cause)) { + rb_raise(rb_eArgError, "only cause is given with no arguments"); } -} -VALUE -rb_iterate(it_proc, data1, bl_proc, data2) - VALUE (*it_proc)(), (*bl_proc)(); - VALUE data1, data2; -{ - int state; - volatile VALUE retval = Qnil; - NODE *node = NEW_CFUNC(bl_proc, data2); - VALUE self = ruby_top_self; - - iter_retry: - PUSH_ITER(ITER_PRE); - PUSH_BLOCK(0, node); - PUSH_TAG(PROT_NONE); - - state = EXEC_TAG(); - if (state == 0) { - retval = (*it_proc)(data1); + // Create exception: + VALUE exception; + if (argc == 0) { + exception = rb_exc_new(rb_eRuntimeError, 0, 0); } - if (ruby_block->tag->dst == state) { - state &= TAG_MASK; - if (state == TAG_RETURN) { - retval = prot_tag->retval; - } + else { + exception = rb_make_exception(argc, argv); } - POP_TAG(); - POP_BLOCK(); - POP_ITER(); - switch (state) { - case 0: - break; + VALUE resolved_cause = Qnil; - case TAG_RETRY: - goto iter_retry; - - case TAG_BREAK: - retval = Qnil; - break; - - case TAG_RETURN: - return_value(retval); - /* fall through */ - default: - JUMP_TAG(state); - } - return retval; -} + // Resolve cause with validation: + if (UNDEF_P(cause)) { + // No explicit cause - use automatic cause chaining from calling context: + resolved_cause = rb_ec_get_errinfo(ec); -static int -handle_rescue(self, node) - VALUE self; - NODE *node; -{ - int argc; VALUE *argv; /* used in SETUP_ARGS */ - TMP_PROTECT; - - if (!node->nd_args) { - return rb_obj_is_kind_of(rb_errinfo, rb_eStandardError); + // Prevent self-referential cause (e.g. `raise $!`): + if (resolved_cause == exception) { + resolved_cause = Qnil; + } } - - BEGIN_CALLARGS; - SETUP_ARGS(node->nd_args); - END_CALLARGS; - - while (argc--) { - if (!rb_obj_is_kind_of(argv[0], rb_cModule)) { - rb_raise(rb_eTypeError, "class or module required for rescue clause"); - } - if (rb_obj_is_kind_of(rb_errinfo, argv[0])) return 1; - argv++; + else if (NIL_P(cause)) { + // Explicit nil cause - prevent chaining: + resolved_cause = Qnil; } - return 0; -} - -VALUE -rb_rescue(b_proc, data1, r_proc, data2) - VALUE (*b_proc)(), (*r_proc)(); - VALUE data1, data2; -{ - int state; - volatile VALUE result; - volatile VALUE e_info = rb_errinfo; + else { + // Explicit cause - validate and assign: + if (!rb_obj_is_kind_of(cause, rb_eException)) { + rb_raise(rb_eTypeError, "exception object expected"); + } - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - retry_entry: - result = (*b_proc)(data1); - } - else if (state == TAG_RAISE && rb_obj_is_kind_of(rb_errinfo, rb_eStandardError)) { - if (r_proc) { - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - result = (*r_proc)(data2, rb_errinfo); - } - POP_TAG(); - if (state == TAG_RETRY) { - state = 0; - goto retry_entry; - } - } - else { - result = Qnil; - state = 0; - } - if (state == 0) { - rb_errinfo = e_info; - } + if (cause == exception) { + // Prevent self-referential cause (e.g. `raise error, cause: error`) - although I'm not sure this is good behaviour, it's inherited from `Kernel#raise`. + resolved_cause = Qnil; + } + else { + // Check for circular causes: + VALUE current_cause = cause; + while (!NIL_P(current_cause)) { + // We guarantee that the cause chain is always terminated. Then, creating an exception with an existing cause is not circular as long as exception is not an existing cause of any other exception. + if (current_cause == exception) { + rb_raise(rb_eArgError, "circular causes"); + } + if (THROW_DATA_P(current_cause)) { + break; + } + current_cause = rb_attr_get(current_cause, id_cause); + } + resolved_cause = cause; + } } - POP_TAG(); - if (state) JUMP_TAG(state); - - return result; -} -VALUE -rb_protect(proc, data, state) - VALUE (*proc)(); - VALUE data; - int *state; -{ - VALUE result; /* OK */ - int status; - - PUSH_TAG(PROT_NONE); - if ((status = EXEC_TAG()) == 0) { - result = (*proc)(data); - } - POP_TAG(); - if (state) { - *state = status; - } - if (status != 0) { - return Qnil; + // Apply cause to exception object (duplicate if frozen): + if (!UNDEF_P(resolved_cause)) { + if (OBJ_FROZEN(exception)) { + exception = rb_obj_dup(exception); + } + rb_ivar_set(exception, id_cause, resolved_cause); } - return result; + return exception; } VALUE -rb_ensure(b_proc, data1, e_proc, data2) - VALUE (*b_proc)(); - VALUE (*e_proc)(); - VALUE data1, data2; +rb_f_raise(int argc, VALUE *argv) { - int state; - volatile VALUE result = Qnil; - VALUE retval; + VALUE cause = Qundef; + argc = extract_raise_options(argc, argv, &cause); - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - result = (*b_proc)(data1); - } - POP_TAG(); - retval = prot_tag->retval; /* save retval */ - (*e_proc)(data2); - return_value(retval); - - if (state) JUMP_TAG(state); - return result; -} - -static int last_call_status; + VALUE exception; -#define CSTAT_PRIV 1 -#define CSTAT_PROT 2 -#define CSTAT_VCALL 4 + // Bare re-raise case: + if (argc == 0) { + // Cause was extracted, but no arguments were provided: + if (!UNDEF_P(cause)) { + rb_raise(rb_eArgError, "only cause is given with no arguments"); + } -static VALUE -rb_f_missing(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - ID id; - VALUE desc = 0; - char *format = 0; - char *file = ruby_sourcefile; - int line = ruby_sourceline; - - id = FIX2INT(argv[0]); - argc--; argv++; - - switch (TYPE(obj)) { - case T_NIL: - format = "undefined method `%s' for nil"; - break; - case T_TRUE: - format = "undefined method `%s' for Qtrue"; - break; - case T_FALSE: - format = "undefined method `%s' for Qfalse"; - break; - case T_OBJECT: - desc = rb_any_to_s(obj); - break; - default: - desc = rb_inspect(obj); - break; - } - if (desc) { - if (last_call_status & CSTAT_PRIV) { - format = "private method `%s' called for %s"; - } - if (last_call_status & CSTAT_PROT) { - format = "protected method `%s' called for %s"; - } - else if (rb_iterator_p()) { - format = "undefined iterator `%s' for %s"; - } - else if (last_call_status & CSTAT_VCALL) { - char *mname = rb_id2name(id); - - if (('a' <= mname[0] && mname[0] <= 'z') || mname[0] == '_') { - format = "undefined local variable or method `%s' for %s"; - } - } - if (!format) { - format = "undefined method `%s' for %s"; - } - if (RSTRING(desc)->len > 65) { - desc = rb_any_to_s(obj); - } + // Otherwise, re-raise the current exception: + exception = get_errinfo(); + if (!NIL_P(exception)) { + argc = 1; + argv = &exception; + } } - ruby_sourcefile = file; - ruby_sourceline = line; - PUSH_FRAME(); /* fake frame */ - *ruby_frame = *_frame.prev->prev; - - rb_raise(rb_eNameError, format, - rb_id2name(id), - desc?(char*)RSTRING(desc)->ptr:""); - POP_FRAME(); - - return Qnil; /* not reached */ -} - -static VALUE -rb_undefined(obj, id, argc, argv, call_status) - VALUE obj; - ID id; - int argc; - VALUE*argv; - int call_status; -{ - VALUE *nargv; - - nargv = ALLOCA_N(VALUE, argc+1); - nargv[0] = INT2FIX(id); - MEMCPY(nargv+1, argv, VALUE, argc); - - last_call_status = call_status; - - return rb_funcall2(obj, rb_intern("method_missing"), argc+1, nargv); -} - -#ifdef DJGPP -# define STACK_LEVEL_MAX 65535 -#else -#ifdef __human68k__ -extern int _stacksize; -# define STACK_LEVEL_MAX (_stacksize - 4096) -#else -# define STACK_LEVEL_MAX 655300 -#endif -#endif -extern VALUE *rb_gc_stack_start; -static int -stack_length() -{ - VALUE pos; - -#ifdef sparc - return rb_gc_stack_start - &pos + 0x80; -#else - return (&pos < rb_gc_stack_start) ? rb_gc_stack_start - &pos - : &pos - rb_gc_stack_start; -#endif -} - -static VALUE -call_cfunc(func, recv, len, argc, argv) - VALUE (*func)(); - VALUE recv; - int len, argc; - VALUE *argv; -{ - if (len >= 0 && argc != len) { - rb_raise(rb_eArgError, "Wrong # of arguments(%d for %d)", - argc, len); - } + rb_raise_jump(rb_make_exception(argc, argv), cause); + + UNREACHABLE_RETURN(Qnil); +} + +/* + * call-seq: + * raise(exception, message = exception.to_s, backtrace = nil, cause: $!) + * raise(message = nil, cause: $!) + * + * Raises an exception; + * see {Exceptions}[rdoc-ref:exceptions.md]. + * + * Argument +exception+ sets the class of the new exception; + * it should be class Exception or one of its subclasses + * (most commonly, RuntimeError or StandardError), + * or an instance of one of those classes: + * + * begin + * raise(StandardError) + * rescue => x + * p x.class + * end + * # => StandardError + * + * Argument +message+ sets the stored message in the new exception, + * which may be retrieved by method Exception#message; + * the message must be + * a {string-convertible object}[rdoc-ref:implicit_conversion.rdoc@String-Convertible+Objects] + * or +nil+: + * + * begin + * raise(StandardError, 'Boom') + * rescue => x + * p x.message + * end + * # => "Boom" + * + * If argument +message+ is not given, + * the message is the exception class name. + * + * See {Messages}[rdoc-ref:exceptions.md@Messages]. + * + * Argument +backtrace+ might be used to modify the backtrace of the new exception, + * as reported by Exception#backtrace and Exception#backtrace_locations; + * the backtrace must be an array of Thread::Backtrace::Location, an array of + * strings, a single string, or +nil+. + * + * Using the array of Thread::Backtrace::Location instances is the most consistent option + * and should be preferred when possible. The necessary value might be obtained + * from #caller_locations, or copied from Exception#backtrace_locations of another + * error: + * + * begin + * do_some_work() + * rescue ZeroDivisionError => ex + * raise(LogicalError, "You have an error in your math", ex.backtrace_locations) + * end + * + * The ways, both Exception#backtrace and Exception#backtrace_locations of the + * raised error are set to the same backtrace. + * + * When the desired stack of locations is not available and should + * be constructed from scratch, an array of strings or a singular + * string can be used. In this case, only Exception#backtrace is set: + * + * begin + * raise(StandardError, 'Boom', %w[dsl.rb:3 framework.rb:1]) + * rescue => ex + * p ex.backtrace + * # => ["dsl.rb:3", "framework.rb:1"] + * p ex.backtrace_locations + * # => nil + * end + * + * If argument +backtrace+ is not given, + * the backtrace is set according to an array of Thread::Backtrace::Location objects, + * as derived from the call stack. + * + * See {Backtraces}[rdoc-ref:exceptions.md@Backtraces]. + * + * Keyword argument +cause+ sets the stored cause in the new exception, + * which may be retrieved by method Exception#cause; + * the cause must be an exception object (Exception or one of its subclasses), + * or +nil+: + * + * begin + * raise(StandardError, cause: RuntimeError.new) + * rescue => x + * p x.cause + * end + * # => #<RuntimeError: RuntimeError> + * + * If keyword argument +cause+ is not given, + * the cause is the value of <tt>$!</tt>. + * + * See {Cause}[rdoc-ref:exceptions.md@Cause]. + * + * In the alternate calling sequence, + * where argument +exception+ _not_ given, + * raises a new exception of the class given by <tt>$!</tt>, + * or of class RuntimeError if <tt>$!</tt> is +nil+: + * + * begin + * raise + * rescue => x + * p x + * end + * # => RuntimeError + * + * With argument +exception+ not given, + * argument +message+ and keyword argument +cause+ may be given, + * but argument +backtrace+ may not be given. + * + * +cause+ can not be given as an only argument. + * + */ + +static VALUE +f_raise(int c, VALUE *v, VALUE _) +{ + return rb_f_raise(c, v); +} + +static VALUE +make_exception(int argc, const VALUE *argv, int isstr) +{ + VALUE mesg, exc; - switch (len) { - case -2: - return (*func)(recv, rb_ary_new4(argc, argv)); - break; - case -1: - return (*func)(argc, argv, recv); - break; + mesg = Qnil; + switch (argc) { case 0: - return (*func)(recv); - break; + return Qnil; case 1: - return (*func)(recv, argv[0]); - break; + exc = argv[0]; + if (isstr &&! NIL_P(exc)) { + mesg = rb_check_string_type(exc); + if (!NIL_P(mesg)) { + return rb_exc_new3(rb_eRuntimeError, mesg); + } + } + case 2: - return (*func)(recv, argv[0], argv[1]); - break; case 3: - return (*func)(recv, argv[0], argv[1], argv[2]); - break; - case 4: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3]); - break; - case 5: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]); - break; - case 6: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5]); - break; - case 7: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6]); - break; - case 8: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7]); - break; - case 9: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8]); - break; - case 10: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9]); - break; - case 11: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]); - break; - case 12: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9], - argv[10], argv[11]); - break; - case 13: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], - argv[11], argv[12]); - break; - case 14: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], - argv[11], argv[12], argv[13]); - break; - case 15: - return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], - argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], - argv[11], argv[12], argv[13], argv[14]); - break; + break; default: - rb_raise(rb_eArgError, "too many arguments(%d)", len); - break; + rb_error_arity(argc, 0, 3); } - return Qnil; /* not reached */ -} - -static VALUE -rb_call0(klass, recv, id, argc, argv, body, nosuper) - VALUE klass, recv; - ID id; - int argc; /* OK */ - VALUE *argv; /* OK */ - NODE *body; /* OK */ - int nosuper; -{ - NODE *b2; /* OK */ - volatile VALUE result = Qnil; - int itr; - static int tick; - TMP_PROTECT; - - switch (ruby_iter->iter) { - case ITER_PRE: - itr = ITER_CUR; - break; - case ITER_CUR: - default: - itr = ITER_NOT; - break; + if (NIL_P(mesg)) { + mesg = rb_check_funcall(argv[0], idException, argc != 1, &argv[1]); } - - if ((++tick & 0x3ff) == 0) { - CHECK_INTS; /* better than nothing */ - if (stack_length() > STACK_LEVEL_MAX) { - rb_raise(rb_eSysStackError, "stack level too deep"); - } + if (UNDEF_P(mesg)) { + rb_raise(rb_eTypeError, "exception class/object expected"); } - PUSH_ITER(itr); - PUSH_FRAME(); - - ruby_frame->last_func = id; - ruby_frame->last_class = nosuper?0:klass; - ruby_frame->self = recv; - ruby_frame->argc = argc; - ruby_frame->argv = argv; - - switch (nd_type(body)) { - case NODE_CFUNC: - { - int len = body->nd_argc; - - if (len < -2) { - rb_bug("bad argc(%d) specified for `%s(%s)'", - len, rb_class2name(klass), rb_id2name(id)); - } - if (trace_func) { - int state; - char *file = ruby_frame->prev->file; - int line = ruby_frame->prev->line; - if (!file) { - file = ruby_sourcefile; - line = ruby_sourceline; - } - - call_trace_func("c-call", 0, 0, 0, id, 0); - PUSH_TAG(PROT_FUNC); - if ((state = EXEC_TAG()) == 0) { - result = call_cfunc(body->nd_cfnc, recv, len, argc, argv); - } - POP_TAG(); - call_trace_func("c-return", 0, 0, recv, id, klass); - if (state) JUMP_TAG(state); - } - else { - result = call_cfunc(body->nd_cfnc, recv, len, argc, argv); - } - } - break; - - /* for re-scoped/renamed method */ - case NODE_ZSUPER: - /* for attr get/set */ - case NODE_ATTRSET: - case NODE_IVAR: - result = rb_eval(recv, body); - break; - - default: - { - int state; - VALUE *local_vars; /* OK */ - - PUSH_SCOPE(); - - if (body->nd_rval) ruby_frame->cbase = body->nd_rval; - if (body->nd_tbl) { - local_vars = TMP_ALLOC(body->nd_tbl[0]+1); - *local_vars++ = (VALUE)body; - rb_mem_clear(local_vars, body->nd_tbl[0]); - ruby_scope->local_tbl = body->nd_tbl; - ruby_scope->local_vars = local_vars; - } - else { - local_vars = ruby_scope->local_vars = 0; - ruby_scope->local_tbl = 0; - } - b2 = body = body->nd_next; - - PUSH_VARS(); - PUSH_TAG(PROT_FUNC); - - if ((state = EXEC_TAG()) == 0) { - NODE *node = 0; - int i; - - if (nd_type(body) == NODE_ARGS) { - node = body; - body = 0; - } - else if (nd_type(body) == NODE_BLOCK) { - node = body->nd_head; - body = body->nd_next; - } - if (node) { - if (nd_type(node) != NODE_ARGS) { - rb_bug("no argument-node"); - } - - i = node->nd_cnt; - if (i > argc) { - rb_raise(rb_eArgError, "Wrong # of arguments(%d for %d)", - argc, i); - } - if (node->nd_rest == -1) { - int opt = argc - i; - NODE *optnode = node->nd_opt; - - while (optnode) { - opt--; - optnode = optnode->nd_next; - } - if (opt > 0) { - rb_raise(rb_eArgError, "Wrong # of arguments(%d for %d)", - argc, argc-opt); - } - } - - if (local_vars) { - if (i > 0) { - /* +2 for $_ and $~ */ - MEMCPY(local_vars+2, argv, VALUE, i); - } - argv += i; argc -= i; - if (node->nd_opt) { - NODE *opt = node->nd_opt; - - while (opt && argc) { - assign(recv, opt->nd_head, *argv); - argv++; argc--; - opt = opt->nd_next; - } - rb_eval(recv, opt); - } - if (node->nd_rest >= 0) { - if (argc > 0) - local_vars[node->nd_rest]=rb_ary_new4(argc,argv); - else - local_vars[node->nd_rest]=rb_ary_new2(0); - } - } - } - - if (trace_func) { - call_trace_func("call", b2->nd_file, nd_line(b2), - recv, ruby_frame->last_func, 0); - } - result = rb_eval(recv, body); - } - else if (state == TAG_RETURN) { - result = prot_tag->retval; - state = 0; - } - POP_TAG(); - POP_VARS(); - POP_SCOPE(); - if (trace_func) { - char *file = ruby_frame->prev->file; - int line = ruby_frame->prev->line; - if (!file) { - file = ruby_sourcefile; - line = ruby_sourceline; - } - call_trace_func("return", file, line, recv, - ruby_frame->last_func, klass); - } - switch (state) { - case 0: - break; - - case TAG_NEXT: - rb_raise(rb_eLocalJumpError, "unexpected next"); - break; - case TAG_BREAK: - rb_raise(rb_eLocalJumpError, "unexpected break"); - break; - case TAG_REDO: - rb_raise(rb_eLocalJumpError, "unexpected redo"); - break; - case TAG_RETRY: - if (!rb_iterator_p()) { - rb_raise(rb_eLocalJumpError, "retry outside of rescue clause"); - } - default: - JUMP_TAG(state); - } - } + if (!rb_obj_is_kind_of(mesg, rb_eException)) { + rb_raise(rb_eTypeError, "exception object expected"); } - POP_FRAME(); - POP_ITER(); - return result; -} - -static VALUE -rb_call(klass, recv, mid, argc, argv, scope) - VALUE klass, recv; - ID mid; - int argc; /* OK */ - VALUE *argv; /* OK */ - int scope; -{ - NODE *body; /* OK */ - int noex; - ID id = mid; - struct cache_entry *ent; - - /* is it in the method cache? */ - ent = cache + EXPR1(klass, mid); - if (ent->mid == mid && ent->klass == klass) { - klass = ent->origin; - id = ent->mid0; - noex = ent->noex; - body = ent->method; - } - else if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) { - if (scope == 3) { - rb_raise(rb_eNameError, "super: no superclass method `%s'", - rb_id2name(mid)); - } - return rb_undefined(recv, mid, argc, argv, scope==2?CSTAT_VCALL:0); + if (argc == 3) { + set_backtrace(mesg, argv[2]); } - /* receiver specified form for private method */ - if ((noex & NOEX_PRIVATE) && scope == 0) - return rb_undefined(recv, mid, argc, argv, CSTAT_PRIV); - - /* self must be kind of a specified form for private method */ - if ((noex & NOEX_PROTECTED) && !rb_obj_is_kind_of(ruby_frame->self, klass)) - return rb_undefined(recv, mid, argc, argv, CSTAT_PROT); - - return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF); + return mesg; } VALUE -rb_apply(recv, mid, args) - VALUE recv; - ID mid; - VALUE args; +rb_make_exception(int argc, const VALUE *argv) { - int argc; - VALUE *argv; - - argc = RARRAY(args)->len; - argv = ALLOCA_N(VALUE, argc); - MEMCPY(argv, RARRAY(args)->ptr, VALUE, argc); - return rb_call(CLASS_OF(recv), recv, mid, argc, argv, 1); + return make_exception(argc, argv, TRUE); } -static VALUE -rb_f_send(argc, argv, recv) - int argc; - VALUE *argv; - VALUE recv; +/*! \private + */ +static void +rb_raise_jump(VALUE mesg, VALUE cause) { - VALUE vid; - - if (argc == 0) rb_raise(rb_eArgError, "no method name given"); + rb_execution_context_t *ec = GET_EC(); + const rb_control_frame_t *cfp = ec->cfp; + const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp); + VALUE klass = me->owner; + VALUE self = cfp->self; + ID mid = me->called_id; - vid = *argv++; argc--; - PUSH_ITER(rb_iterator_p()?ITER_PRE:ITER_NOT); - vid = rb_call(CLASS_OF(recv), recv, rb_to_id(vid), argc, argv, 1); - POP_ITER(); + rb_vm_pop_frame(ec); + EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, self, me->def->original_id, mid, klass, Qnil); - return vid; + rb_longjmp(ec, TAG_RAISE, mesg, cause); } - -#ifdef HAVE_STDARG_PROTOTYPES -#include <stdarg.h> -#define va_init_list(a,b) va_start(a,b) -#else -#include <varargs.h> -#define va_init_list(a,b) va_start(a) -#endif - -VALUE -#ifdef HAVE_STDARG_PROTOTYPES -rb_funcall(VALUE recv, ID mid, int n, ...) -#else -rb_funcall(recv, mid, n, va_alist) - VALUE recv; - ID mid; - int n; - va_dcl -#endif +void +rb_jump_tag(int tag) { - va_list ar; - VALUE *argv; - - if (n > 0) { - int i; - - argv = ALLOCA_N(VALUE, n); - - va_init_list(ar, n); - for (i=0;i<n;i++) { - argv[i] = va_arg(ar, VALUE); - } - va_end(ar); + if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) { + unknown_longjmp_status(tag); } - else { - argv = 0; - } - - return rb_call(CLASS_OF(recv), recv, mid, n, argv, 1); -} - -VALUE -rb_funcall2(recv, mid, argc, argv) - VALUE recv; - ID mid; - int argc; - VALUE *argv; -{ - return rb_call(CLASS_OF(recv), recv, mid, argc, argv, 1); + EC_JUMP_TAG(GET_EC(), tag); } -static VALUE -backtrace(lev) - int lev; +int +rb_block_given_p(void) { - struct FRAME *frame = ruby_frame; - char buf[BUFSIZ]; - VALUE ary; - int slev = safe_level; - - safe_level = 0; - ary = rb_ary_new(); - if (lev < 0) { - if (frame->last_func) { - snprintf(buf, BUFSIZ, "%s:%d:in `%s'", - ruby_sourcefile, ruby_sourceline, - rb_id2name(frame->last_func)); - } - else { - snprintf(buf, BUFSIZ, "%s:%d", ruby_sourcefile, ruby_sourceline); - } - rb_ary_push(ary, rb_str_new2(buf)); + if (rb_vm_frame_block_handler(GET_EC()->cfp) == VM_BLOCK_HANDLER_NONE) { + return FALSE; } else { - while (lev-- > 0) { - frame = frame->prev; - if (!frame) return Qnil; - } + return TRUE; } - while (frame && frame->file) { - if (frame->prev && frame->prev->last_func) { - snprintf(buf, BUFSIZ, "%s:%d:in `%s'", - frame->file, frame->line, - rb_id2name(frame->prev->last_func)); - } - else { - snprintf(buf, BUFSIZ, "%s:%d", frame->file, frame->line); - } - rb_ary_push(ary, rb_str_new2(buf)); - frame = frame->prev; - } - safe_level = slev; - return ary; } -static VALUE -rb_f_caller(argc, argv) - int argc; - VALUE *argv; -{ - VALUE level; - int lev; - - rb_scan_args(argc, argv, "01", &level); - - if (NIL_P(level)) lev = 1; - else lev = NUM2INT(level); - if (lev < 0) rb_raise(rb_eArgError, "negative level(%d)", lev); +int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp); - return backtrace(lev); -} - -void -rb_backtrace() +int +rb_keyword_given_p(void) { - int i, lev; - VALUE ary; - - lev = INT2FIX(0); - ary = backtrace(-1); - for (i=0; i<RARRAY(ary)->len; i++) { - printf("\tfrom %s\n", RSTRING(RARRAY(ary)->ptr[i])->ptr); - } + return rb_vm_cframe_keyword_p(GET_EC()->cfp); } -static VALUE -make_backtrace() -{ - VALUE lev; +VALUE rb_eThreadError; - lev = INT2FIX(0); - return backtrace(-1); -} - -ID -rb_frame_last_func() +void +rb_need_block(void) { - return ruby_frame->last_func; + if (!rb_block_given_p()) { + rb_vm_localjump_error("no block given", Qnil, 0); + } } -static NODE* -compile(src, place) - VALUE src; - char *place; +VALUE +rb_rescue2(VALUE (* b_proc) (VALUE), VALUE data1, + VALUE (* r_proc) (VALUE, VALUE), VALUE data2, ...) { - NODE *node; - - Check_Type(src, T_STRING); - if (place == 0) place = ruby_sourcefile; - node = rb_compile_string(place, src); - - if (ruby_nerrs == 0) return node; - return 0; + va_list ap; + va_start(ap, data2); + VALUE ret = rb_vrescue2(b_proc, data1, r_proc, data2, ap); + va_end(ap); + return ret; } -static VALUE -eval(self, src, scope, file, line) - VALUE self, src, scope; - char *file; - int line; -{ - struct BLOCK *data; - volatile VALUE result = Qnil; - struct SCOPE * volatile old_scope; - struct BLOCK * volatile old_block; - struct BLOCK * volatile old_call_block; - struct RVarmap * volatile old_d_vars; - int volatile old_vmode; - struct FRAME frame; - char *filesave = ruby_sourcefile; - int linesave = ruby_sourceline; - volatile int iter = ruby_frame->iter; - int state; - - if (file == 0) { - file = ruby_sourcefile; - line = ruby_sourceline; - } - if (!NIL_P(scope)) { - if (!rb_obj_is_block(scope)) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc/Binding)", - rb_class2name(CLASS_OF(scope))); - } - - Data_Get_Struct(scope, struct BLOCK, data); - - /* PUSH BLOCK from data */ - frame = data->frame; - frame.prev = ruby_frame; - ruby_frame = &(frame); - old_scope = ruby_scope; - ruby_scope = data->scope; - old_call_block = ruby_calling_block; - ruby_calling_block = data; - old_block = ruby_block; - ruby_block = data->prev; - old_d_vars = ruby_dyna_vars; - ruby_dyna_vars = data->d_vars; - old_vmode = scope_vmode; - scope_vmode = data->vmode; - - self = data->self; - ruby_frame->iter = data->iter; - } - else { - if (ruby_frame->prev) { - ruby_frame->iter = ruby_frame->prev->iter; - } - } - PUSH_CLASS(); - ruby_class = ((NODE*)ruby_frame->cbase)->nd_clss; - - rb_in_eval++; - if (TYPE(ruby_class) == T_ICLASS) { - ruby_class = RBASIC(ruby_class)->klass; - } - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - ruby_sourcefile = file; - ruby_sourceline = line; - compile(src, file); - if (ruby_nerrs > 0) { - compile_error(0); - } - result = eval_node(self); - } - POP_TAG(); - POP_CLASS(); - rb_in_eval--; - if (!NIL_P(scope)) { - ruby_frame = ruby_frame->prev; -#ifdef USE_THREAD - if (FL_TEST(ruby_scope, SCOPE_DONT_RECYCLE)) - FL_SET(old_scope, SCOPE_DONT_RECYCLE); -#endif - ruby_scope = old_scope; - ruby_block = old_block; - ruby_calling_block = old_call_block; - data->d_vars = ruby_dyna_vars; - ruby_dyna_vars = old_d_vars; - data->vmode = scope_vmode; /* write back visibility mode */ - scope_vmode = old_vmode; +VALUE +rb_vrescue2(VALUE (* b_proc) (VALUE), VALUE data1, + VALUE (* r_proc) (VALUE, VALUE), VALUE data2, + va_list args) +{ + enum ruby_tag_type state; + rb_execution_context_t * volatile ec = GET_EC(); + rb_control_frame_t *volatile cfp = ec->cfp; + volatile VALUE result = Qfalse; + volatile VALUE e_info = ec->errinfo; + + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + retry_entry: + result = (*b_proc) (data1); + } + else if (result) { + /* escape from r_proc */ + if (state == TAG_RETRY) { + state = TAG_NONE; + ec->errinfo = Qnil; + result = Qfalse; + goto retry_entry; + } } else { - ruby_frame->iter = iter; - } - ruby_sourcefile = filesave; - ruby_sourceline = linesave; - if (state) { - if (state == TAG_RAISE) { - VALUE err; - VALUE errat; - - errat = get_backtrace(rb_errinfo); - if (strcmp(file, "(eval)") == 0) { - if (ruby_sourceline > 1) { - err = RARRAY(errat)->ptr[0]; - rb_str_cat(err, ": ", 2); - rb_str_concat(err, rb_errinfo); - } - else { - err = rb_str_dup(rb_errinfo); - } - errat = Qnil; - rb_exc_raise(rb_exc_new3(CLASS_OF(rb_errinfo), err)); - } - rb_exc_raise(rb_errinfo); - } - JUMP_TAG(state); + rb_vm_rewind_cfp(ec, cfp); + + if (state == TAG_RAISE) { + int handle = FALSE; + VALUE eclass; + va_list ap; + + result = Qnil; + /* reuses args when raised again after retrying in r_proc */ + va_copy(ap, args); + while ((eclass = va_arg(ap, VALUE)) != 0) { + if (rb_obj_is_kind_of(ec->errinfo, eclass)) { + handle = TRUE; + break; + } + } + va_end(ap); + + if (handle) { + state = TAG_NONE; + if (r_proc) { + result = (*r_proc) (data2, ec->errinfo); + } + ec->errinfo = e_info; + } + } } + EC_POP_TAG(); + if (state) + EC_JUMP_TAG(ec, state); return result; } -static VALUE -rb_f_eval(argc, argv, self) - int argc; - VALUE *argv; - VALUE self; -{ - VALUE src, scope, vfile, vline; - char *file = "(eval)"; - int line = 0; - - rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline); - if (argc >= 3) { - Check_Type(vfile, T_STRING); - file = RSTRING(vfile)->ptr; - } - if (argc >= 4) { - line = NUM2INT(vline); - } - - Check_SafeStr(src); - return eval(self, src, scope, file, line); -} - -static VALUE -exec_under(func, under, args) - VALUE (*func)(); - VALUE under; - void *args; -{ - VALUE val; /* OK */ - int state; - int mode; - VALUE cbase = ruby_frame->cbase; - - PUSH_CLASS(); - ruby_class = under; - PUSH_FRAME(); - ruby_frame->last_func = _frame.prev->last_func; - ruby_frame->last_class = _frame.prev->last_class; - ruby_frame->argc = _frame.prev->argc; - ruby_frame->argv = _frame.prev->argv; - ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,under,0,cbase); - mode = scope_vmode; - SCOPE_SET(SCOPE_PUBLIC); - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { - val = (*func)(args); - } - POP_TAG(); - SCOPE_SET(mode); - POP_FRAME(); - POP_CLASS(); - if (state) JUMP_TAG(state); - - return val; -} - -static VALUE -eval_under_i(args) - VALUE *args; -{ - return eval(args[0], args[1], Qnil, (char*)args[2], (int)args[3]); -} - -static VALUE -eval_under(under, self, src, file, line) - VALUE under, self, src; - char *file; - int line; -{ - VALUE args[4]; - - Check_SafeStr(src); - args[0] = self; - args[1] = src; - args[2] = (VALUE)file; - args[3] = (VALUE)line; - return exec_under(eval_under_i, under, args); -} - -static VALUE -yield_under_i(self) - VALUE self; -{ - return rb_yield_0(self, self, ruby_class); -} - -static VALUE -yield_under(under, self) - VALUE under, self; +VALUE +rb_rescue(VALUE (* b_proc)(VALUE), VALUE data1, + VALUE (* r_proc)(VALUE, VALUE), VALUE data2) { - if (rb_safe_level() >= 4 && !FL_TEST(self, FL_TAINT)) - rb_raise(rb_eSecurityError, "Insecure: can't eval"); - return exec_under(yield_under_i, under, self); + return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError, + (VALUE)0); } VALUE -rb_obj_instance_eval(argc, argv, self) - int argc; - VALUE *argv; - VALUE self; +rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate) { - char *file = 0; - int line = 0; - VALUE klass; + volatile VALUE result = Qnil; + volatile enum ruby_tag_type state; + rb_execution_context_t * volatile ec = GET_EC(); + rb_control_frame_t *volatile cfp = ec->cfp; - if (argc == 0) { - if (!rb_iterator_p()) { - rb_raise(rb_eArgError, "block not supplied"); - } - } - else if (argc < 4) { - Check_SafeStr(argv[0]); - if (argc > 1) file = STR2CSTR(argv[1]); - if (argc > 2) line = NUM2INT(argv[2]); + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + result = (*proc)(data); } else { - rb_raise(rb_eArgError, "Wrong # of arguments: %s(src) or %s{..}", - rb_id2name(ruby_frame->last_func), - rb_id2name(ruby_frame->last_func)); + rb_vm_rewind_cfp(ec, cfp); } + EC_POP_TAG(); - if (rb_special_const_p(self)) { - klass = Qnil; - } - else { - klass = rb_singleton_class(self); - } - if (argc == 0) { - return yield_under(klass, self); - } - else { - return eval_under(klass, self, argv[0], file, line); - } + if (pstate != NULL) *pstate = state; + return result; } -static VALUE -rb_mod_module_eval(argc, argv, mod) - int argc; - VALUE *argv; - VALUE mod; +VALUE +rb_ec_ensure(rb_execution_context_t *ec, VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2) { - char *file = 0; - int line = 0; - - if (argc == 0) { - if (!rb_iterator_p()) { - rb_raise(rb_eArgError, "block not supplied"); - } - } - else if (argc < 4) { - Check_SafeStr(argv[0]); - if (argc > 1) file = STR2CSTR(argv[1]); - if (argc > 2) line = NUM2INT(argv[2]); - } - else { - rb_raise(rb_eArgError, "Wrong # of arguments: %s(src) or %s{..}", - rb_id2name(ruby_frame->last_func), - rb_id2name(ruby_frame->last_func)); - } - - if (argc == 0) { - return yield_under(mod, mod); + enum ruby_tag_type state; + volatile VALUE result = Qnil; + VALUE errinfo; + EC_PUSH_TAG(ec); + if ((state = EC_EXEC_TAG()) == TAG_NONE) { + result = (*b_proc) (data1); } - else { - return eval_under(mod, mod, argv[0], file, line); + EC_POP_TAG(); + errinfo = ec->errinfo; + if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) { + ec->errinfo = Qnil; } + (*e_proc)(data2); + ec->errinfo = errinfo; + if (state) + EC_JUMP_TAG(ec, state); + return result; } -VALUE rb_load_path; - -static int -is_absolute_path(path) - char *path; -{ - if (path[0] == '/') return 1; -# if defined(MSDOS) || defined(NT) || defined(__human68k__) - if (path[0] == '\\') return 1; - if (strlen(path) > 2 && path[1] == ':') return 1; -# endif - return 0; -} - -#ifdef __MACOS__ -static int -is_macos_native_path(path) - char *path; +VALUE +rb_ensure(VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2) { - if (strchr(path, ':')) return 1; - return 0; + return rb_ec_ensure(GET_EC(), b_proc, data1, e_proc, data2); } -#endif -static char* -find_file(file) - char *file; +static ID +frame_func_id(const rb_control_frame_t *cfp) { - extern VALUE rb_load_path; - volatile VALUE vpath; - char *path; - -#ifdef __MACOS__ - if (is_macos_native_path(file)) { - FILE *f = fopen(file, "r"); - - if (f == NULL) return 0; - fclose(f); - return file; - } -#endif - - if (is_absolute_path(file)) { - FILE *f = fopen(file, "r"); - - if (f == NULL) return 0; - fclose(f); - return file; - } + const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp); - if (rb_load_path) { - int i; - - Check_Type(rb_load_path, T_ARRAY); - for (i=0;i<RARRAY(rb_load_path)->len;i++) { - Check_SafeStr(RARRAY(rb_load_path)->ptr[i]); - } - vpath = rb_ary_join(rb_load_path, rb_str_new2(RUBY_PATH_SEP)); - path = STR2CSTR(vpath); - if (safe_level >= 2 && !rb_path_check(path)) { - rb_raise(rb_eSecurityError, "loading from unsefe path %s", path); - } + if (me) { + return me->def->original_id; } else { - path = 0; + return 0; } - - return dln_find_file(file, path); } -void -rb_load(fname, wrap) - VALUE fname; - int wrap; +static ID +frame_called_id(rb_control_frame_t *cfp) { - int state; - char *file; - volatile ID last_func; - VALUE self = ruby_top_self; - TMP_PROTECT; + const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp); - if (wrap) { - Check_Type(fname, T_STRING); + if (me) { + return me->called_id; } else { - Check_SafeStr(fname); - } - if (RSTRING(fname)->ptr[0] == '~') { - fname = rb_file_s_expand_path(1, &fname); - } - file = find_file(RSTRING(fname)->ptr); - if (!file) { - rb_raise(rb_eLoadError, "No such file to load -- %s", RSTRING(fname)->ptr); - } - - PUSH_VARS(); - PUSH_TAG(PROT_NONE); - PUSH_CLASS(); - if (!wrap) { - rb_secure(4); /* should alter global state */ - ruby_class = rb_cObject; - } - else { - /* load in anonymous module as toplevel */ - ruby_class = ruby_wrapper = rb_module_new(); - self = rb_obj_clone(ruby_top_self); - rb_extend_object(self, ruby_class); - } - PUSH_FRAME(); - ruby_frame->last_func = 0; - ruby_frame->self = ruby_top_self; - ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,ruby_class,0,0); - PUSH_SCOPE(); - if (ruby_class == rb_cObject && top_scope->local_tbl) { - int len = top_scope->local_tbl[0]+1; - ID *tbl = ALLOC_N(ID, len); - VALUE *vars = TMP_ALLOC(len); - *vars++ = 0; - MEMCPY(tbl, top_scope->local_tbl, ID, len); - MEMCPY(vars, top_scope->local_vars, ID, len-1); - ruby_scope->local_tbl = tbl; /* copy toplevel scope */ - ruby_scope->local_vars = vars; /* will not alter toplevel variables */ + return 0; } - /* default visibility is private at loading toplevel */ - SCOPE_SET(SCOPE_PRIVATE); - - state = EXEC_TAG(); - last_func = ruby_frame->last_func; - if (state == 0) { - rb_in_eval++; - rb_load_file(file); - rb_in_eval--; - if (ruby_nerrs == 0) { - eval_node(self); - } - } - ruby_frame->last_func = last_func; - if (ruby_scope->flag == SCOPE_ALLOCA && ruby_class == rb_cObject) { - if (ruby_scope->local_tbl) /* toplevel was empty */ - free(ruby_scope->local_tbl); - } - POP_SCOPE(); - POP_FRAME(); - POP_CLASS(); - POP_TAG(); - POP_VARS(); - ruby_wrapper = 0; - if (ruby_nerrs > 0) { - rb_exc_raise(rb_errinfo); - } - if (state) JUMP_TAG(state); } -void -rb_load_protect(fname, wrap, state) - VALUE fname; - int wrap; - int *state; +ID +rb_frame_this_func(void) { - int status; - - PUSH_TAG(PROT_NONE); - if ((status = EXEC_TAG()) == 0) { - rb_load(fname, wrap); - } - POP_TAG(); - if (state) *state = status; + return frame_func_id(GET_EC()->cfp); } -static VALUE -rb_f_load(argc, argv) - int argc; - VALUE *argv; +ID +rb_frame_callee(void) { - VALUE fname, wrap; - - rb_scan_args(argc, argv, "11", &fname, &wrap); - rb_load(fname, RTEST(wrap)); - return Qtrue; + return frame_called_id(GET_EC()->cfp); } -static VALUE rb_features; - -static int -rb_provided(feature) - char *feature; +static rb_control_frame_t * +previous_frame(const rb_execution_context_t *ec) { - VALUE *p, *pend; - char *f; - int len; - - p = RARRAY(rb_features)->ptr; - pend = p + RARRAY(rb_features)->len; - while (p < pend) { - f = STR2CSTR(*p); - if (strcmp(f, feature) == 0) return Qtrue; - len = strlen(feature); - if (strncmp(f, feature, len) == 0 - && (strcmp(f+len, ".rb") == 0 ||strcmp(f+len, ".so") == 0)) { - return Qtrue; - } - p++; + rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp); + /* check if prev_cfp can be accessible */ + if ((void *)(ec->vm_stack + ec->vm_stack_size) == (void *)(prev_cfp)) { + return 0; } - return Qfalse; + return prev_cfp; } -#ifdef USE_THREAD -static int rb_thread_loading _((char*)); -static void rb_thread_loading_done _((void)); -#endif - -void -rb_provide(feature) - char *feature; +static ID +prev_frame_callee(void) { - char *buf, *ext; - - if (!rb_provided(feature)) { - ext = strrchr(feature, '.'); - if (ext && strcmp(DLEXT, ext) == 0) { - buf = ALLOCA_N(char, strlen(feature)+4); - strcpy(buf, feature); - ext = strrchr(buf, '.'); - strcpy(ext, ".so"); - feature = buf; - } - rb_ary_push(rb_features, rb_str_new2(feature)); - } + rb_control_frame_t *prev_cfp = previous_frame(GET_EC()); + if (!prev_cfp) return 0; + return frame_called_id(prev_cfp); } -VALUE -rb_f_require(obj, fname) - VALUE obj, fname; +static ID +prev_frame_func(void) { - char *ext, *file, *feature, *buf; /* OK */ - volatile VALUE load; - - rb_secure(4); - Check_SafeStr(fname); - if (rb_provided(RSTRING(fname)->ptr)) - return Qfalse; - - ext = strrchr(RSTRING(fname)->ptr, '.'); - if (ext) { - if (strcmp(".rb", ext) == 0) { - feature = file = RSTRING(fname)->ptr; - file = find_file(file); - if (file) goto load_rb; - } - else if (strcmp(".so", ext) == 0 || strcmp(".o", ext) == 0) { - file = feature = RSTRING(fname)->ptr; - if (strcmp(ext, DLEXT) != 0) { - buf = ALLOCA_N(char, strlen(file)+sizeof(DLEXT)+1); - strcpy(buf, feature); - ext = strrchr(buf, '.'); - strcpy(ext, DLEXT); - file = feature = buf; - } - file = find_file(file); - if (file) goto load_dyna; - } - else if (strcmp(DLEXT, ext) == 0) { - feature = RSTRING(fname)->ptr; - file = find_file(feature); - if (file) goto load_dyna; - } - } - buf = ALLOCA_N(char, strlen(RSTRING(fname)->ptr) + 5); - strcpy(buf, RSTRING(fname)->ptr); - strcat(buf, ".rb"); - file = find_file(buf); - if (file) { - fname = rb_str_new2(file); - feature = buf; - goto load_rb; - } - strcpy(buf, RSTRING(fname)->ptr); - strcat(buf, DLEXT); - file = find_file(buf); - if (file) { - feature = buf; - goto load_dyna; - } - rb_raise(rb_eLoadError, "No such file to load -- %s", - RSTRING(fname)->ptr); - - load_dyna: -#ifdef USE_THREAD - if (rb_thread_loading(feature)) return Qfalse; - else { - int state; - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { -#endif - load = rb_str_new2(file); - file = RSTRING(load)->ptr; - dln_load(file); - rb_provide(feature); -#ifdef USE_THREAD - } - POP_TAG(); - rb_thread_loading_done(); - if (state) JUMP_TAG(state); - } -#endif - return Qtrue; - - load_rb: -#ifdef USE_THREAD - if (rb_thread_loading(feature)) return Qfalse; - else { - int state; - PUSH_TAG(PROT_NONE); - if ((state = EXEC_TAG()) == 0) { -#endif - rb_load(fname, 0); - rb_provide(feature); -#ifdef USE_THREAD - } - POP_TAG(); - rb_thread_loading_done(); - if (state) JUMP_TAG(state); - } -#endif - return Qtrue; + rb_control_frame_t *prev_cfp = previous_frame(GET_EC()); + if (!prev_cfp) return 0; + return frame_func_id(prev_cfp); } -static void -set_method_visibility(self, argc, argv, ex) - VALUE self; - int argc; - VALUE *argv; - ID ex; +/*! + * \private + * Returns the ID of the last method in the call stack. + * \sa rb_frame_this_func + * \ingroup defmethod + */ +ID +rb_frame_last_func(void) { - int i; + const rb_execution_context_t *ec = GET_EC(); + const rb_control_frame_t *cfp = ec->cfp; + ID mid; - for (i=0; i<argc; i++) { - rb_export_method(self, rb_to_id(argv[i]), ex); - } + while (!(mid = frame_func_id(cfp)) && + (cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp), + !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp))); + return mid; } -static VALUE -rb_mod_public(argc, argv, module) - int argc; - VALUE *argv; - VALUE module; -{ - if (argc == 0) { - SCOPE_SET(SCOPE_PUBLIC); - } - else { - set_method_visibility(module, argc, argv, NOEX_PUBLIC); - } - return module; -} +/* + * call-seq: + * append_features(mod) -> mod + * + * When this module is included in another, Ruby calls + * #append_features in this module, passing it the receiving module + * in _mod_. Ruby's default implementation is to add the constants, + * methods, and module variables of this module to _mod_ if this + * module has not already been added to _mod_ or one of its + * ancestors. See also Module#include. + */ static VALUE -rb_mod_protected(argc, argv, module) - int argc; - VALUE *argv; - VALUE module; +rb_mod_append_features(VALUE module, VALUE include) { - if (argc == 0) { - SCOPE_SET(SCOPE_PROTECTED); - } - else { - set_method_visibility(module, argc, argv, NOEX_PROTECTED); + if (!CLASS_OR_MODULE_P(include)) { + Check_Type(include, T_CLASS); } - return module; -} + rb_include_module(include, module); -static VALUE -rb_mod_private(argc, argv, module) - int argc; - VALUE *argv; - VALUE module; -{ - if (argc == 0) { - SCOPE_SET(SCOPE_PRIVATE); - } - else { - set_method_visibility(module, argc, argv, NOEX_PRIVATE); - } return module; } -static VALUE -rb_mod_public_method(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC); - return obj; -} +static VALUE refinement_import_methods(int argc, VALUE *argv, VALUE refinement); -static VALUE -rb_mod_private_method(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE); - return obj; -} +/* + * call-seq: + * include(module, ...) -> self + * + * Invokes Module.append_features on each parameter in reverse order. + */ static VALUE -top_public(argc, argv) - int argc; - VALUE *argv; -{ - return rb_mod_public(argc, argv, rb_cObject); -} - -static VALUE -top_private(argc, argv) - int argc; - VALUE *argv; -{ - return rb_mod_private(argc, argv, rb_cObject); -} - -static VALUE -rb_mod_modfunc(argc, argv, module) - int argc; - VALUE *argv; - VALUE module; +rb_mod_include(int argc, VALUE *argv, VALUE module) { int i; - ID id; - NODE *body; + ID id_append_features, id_included; - if (argc == 0) { - SCOPE_SET(SCOPE_MODFUNC); - return module; + CONST_ID(id_append_features, "append_features"); + CONST_ID(id_included, "included"); + + if (BUILTIN_TYPE(module) == T_MODULE && FL_TEST(module, RMODULE_IS_REFINEMENT)) { + rb_raise(rb_eTypeError, "Refinement#include has been removed"); } - set_method_visibility(module, argc, argv, NOEX_PRIVATE); - for (i=0; i<argc; i++) { - id = rb_to_id(argv[i]); - body = search_method(module, id, 0); - if (body == 0 || body->nd_body == 0) { - rb_raise(rb_eNameError, "undefined method `%s' for module `%s'", - rb_id2name(id), rb_class2name(module)); - } - rb_clear_cache_by_id(id); - rb_add_method(rb_singleton_class(module), id, body->nd_body, NOEX_PUBLIC); + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); + for (i = 0; i < argc; i++) { + Check_Type(argv[i], T_MODULE); + if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) { + rb_raise(rb_eTypeError, "Cannot include refinement"); + } + } + while (argc--) { + rb_funcall(argv[argc], id_append_features, 1, module); + rb_funcall(argv[argc], id_included, 1, module); } return module; } +/* + * call-seq: + * prepend_features(mod) -> mod + * + * When this module is prepended in another, Ruby calls + * #prepend_features in this module, passing it the receiving module + * in _mod_. Ruby's default implementation is to overlay the + * constants, methods, and module variables of this module to _mod_ + * if this module has not already been added to _mod_ or one of its + * ancestors. See also Module#prepend. + */ + static VALUE -rb_mod_append_features(module, include) - VALUE module, include; +rb_mod_prepend_features(VALUE module, VALUE prepend) { - switch (TYPE(include)) { - case T_CLASS: - case T_MODULE: - break; - default: - Check_Type(include, T_CLASS); - break; + if (!CLASS_OR_MODULE_P(prepend)) { + Check_Type(prepend, T_CLASS); } - rb_include_module(include, module); + rb_prepend_module(prepend, module); return module; } +/* + * call-seq: + * prepend(module, ...) -> self + * + * Invokes Module.prepend_features on each parameter in reverse order. + */ + static VALUE -rb_mod_include(argc, argv, module) - int argc; - VALUE *argv; - VALUE module; +rb_mod_prepend(int argc, VALUE *argv, VALUE module) { int i; + ID id_prepend_features, id_prepended; - for (i=0; i<argc; i++) { - Check_Type(argv[i], T_MODULE); - rb_funcall(argv[i], rb_intern("append_features"), 1, module); + if (BUILTIN_TYPE(module) == T_MODULE && FL_TEST(module, RMODULE_IS_REFINEMENT)) { + rb_raise(rb_eTypeError, "Refinement#prepend has been removed"); } - return module; -} - -void -rb_obj_call_init(obj) - VALUE obj; -{ - PUSH_ITER(rb_iterator_p()?ITER_PRE:ITER_NOT); - rb_funcall2(obj, init, ruby_frame->argc, ruby_frame->argv); - POP_ITER(); -} -VALUE -rb_class_new_instance(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - VALUE obj; + CONST_ID(id_prepend_features, "prepend_features"); + CONST_ID(id_prepended, "prepended"); - if (FL_TEST(klass, FL_SINGLETON)) { - rb_raise(rb_eTypeError, "can't create instance of virtual class"); + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); + for (i = 0; i < argc; i++) { + Check_Type(argv[i], T_MODULE); + if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) { + rb_raise(rb_eTypeError, "Cannot prepend refinement"); + } } - obj = rb_obj_alloc(klass); - rb_obj_call_init(obj); - - return obj; -} - -static VALUE -top_include(argc, argv) - int argc; - VALUE *argv; -{ - rb_secure(4); - return rb_mod_include(argc, argv, rb_cObject); -} - -void -rb_extend_object(obj, module) - VALUE obj, module; -{ - rb_include_module(rb_singleton_class(obj), module); -} - -static VALUE -rb_mod_extend_object(mod, obj) - VALUE mod, obj; -{ - rb_extend_object(obj, mod); - return obj; -} - -static VALUE -rb_obj_extend(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - int i; - - for (i=0; i<argc; i++) Check_Type(argv[i], T_MODULE); - for (i=0; i<argc; i++) { - rb_funcall(argv[i], rb_intern("extend_object"), 1, obj); + while (argc--) { + rb_funcall(argv[argc], id_prepend_features, 1, module); + rb_funcall(argv[argc], id_prepended, 1, module); } - return obj; + return module; } -VALUE rb_f_trace_var(); -VALUE rb_f_untrace_var(); - static void -errinfo_setter(val, id, var) - VALUE val; - ID id; - VALUE *var; +ensure_class_or_module(VALUE obj) { - if (!rb_obj_is_kind_of(val, rb_eException)) { - rb_raise(rb_eTypeError, "assigning non-exception to $!"); + if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) { + rb_raise(rb_eTypeError, + "wrong argument type %"PRIsVALUE" (expected Class or Module)", + rb_obj_class(obj)); } - *var = val; } static VALUE -errat_getter(id) - ID id; +hidden_identity_hash_new(void) { - return get_backtrace(rb_errinfo); -} + VALUE hash = rb_ident_hash_new(); -static void -errat_setter(val, id, var) - VALUE val; - ID id; - VALUE *var; -{ - if (NIL_P(rb_errinfo)) { - rb_raise(rb_eArgError, "$! not set"); - } - set_backtrace(rb_errinfo, val); + RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */ + return hash; } -VALUE rb_f_global_variables(); -VALUE f_instance_variables(); - static VALUE -rb_f_local_variables() +refinement_superclass(VALUE superclass) { - ID *tbl; - int n, i; - VALUE ary = rb_ary_new(); - struct RVarmap *vars; - - tbl = ruby_scope->local_tbl; - if (tbl) { - n = *tbl++; - for (i=2; i<n; i++) { /* skip first 2 ($_ and $~) */ - if (tbl[i] == 0) continue; /* skip flip states */ - rb_ary_push(ary, rb_str_new2(rb_id2name(tbl[i]))); - } + if (RB_TYPE_P(superclass, T_MODULE)) { + /* FIXME: Should ancestors of superclass be used here? */ + return rb_include_class_new(RCLASS_ORIGIN(superclass), rb_cBasicObject); } - - vars = ruby_dyna_vars; - while (vars) { - if (vars->id) { - rb_ary_push(ary, rb_str_new2(rb_id2name(vars->id))); - } - vars = vars->next; + else { + return superclass; } - - return ary; -} - -static VALUE rb_f_catch _((VALUE,VALUE)); -static VALUE rb_f_throw _((int,VALUE*)) NORETURN; - -struct end_proc_data { - void (*func)(); - VALUE data; - struct end_proc_data *next; -}; -static struct end_proc_data *end_proc_data; - -void -rb_set_end_proc(func, data) - void (*func)(); - VALUE data; -{ - struct end_proc_data *link = ALLOC(struct end_proc_data); - - link->next = end_proc_data; - link->func = func; - link->data = data; - rb_global_variable(&link->data); - end_proc_data = link; -} - -static void -call_end_proc(data) - VALUE data; -{ - proc_call(data, Qnil); } +/*! + * \private + */ static void -rb_f_END() -{ - PUSH_FRAME(); - ruby_frame->argc = 0; - rb_set_end_proc(call_end_proc, rb_f_lambda()); - POP_FRAME(); -} - -static VALUE -rb_f_at_exit() -{ - VALUE proc; - - proc = rb_f_lambda(); - - rb_set_end_proc(call_end_proc, proc); - return proc; -} - -static void -exec_end_proc() -{ - struct end_proc_data *link = end_proc_data; - - while (link) { - (*link->func)(link->data); - link = link->next; - } -} - -void -Init_eval() -{ - init = rb_intern("initialize"); - eqq = rb_intern("==="); - each = rb_intern("each"); - - aref = rb_intern("[]"); - aset = rb_intern("[]="); - match = rb_intern("=~"); - - rb_global_variable((VALUE*)&top_scope); - rb_global_variable((VALUE*)&ruby_eval_tree_begin); - - rb_global_variable((VALUE*)&ruby_eval_tree); - rb_global_variable((VALUE*)&ruby_dyna_vars); - - rb_define_virtual_variable("$@", errat_getter, errat_setter); - rb_define_hooked_variable("$!", &rb_errinfo, 0, errinfo_setter); - - rb_define_global_function("eval", rb_f_eval, -1); - rb_define_global_function("iterator?", rb_f_iterator_p, 0); - rb_define_global_function("method_missing", rb_f_missing, -1); - rb_define_global_function("loop", rb_f_loop, 0); - - rb_define_method(rb_mKernel, "respond_to?", rb_obj_respond_to, -1); - - rb_define_global_function("raise", rb_f_raise, -1); - rb_define_global_function("fail", rb_f_raise, -1); - - rb_define_global_function("caller", rb_f_caller, -1); - - rb_define_global_function("exit", rb_f_exit, -1); - rb_define_global_function("abort", rb_f_abort, 0); - - rb_define_global_function("at_exit", rb_f_at_exit, 0); - - rb_define_global_function("catch", rb_f_catch, 1); - rb_define_global_function("throw", rb_f_throw, -1); - rb_define_global_function("global_variables", rb_f_global_variables, 0); - rb_define_global_function("local_variables", rb_f_local_variables, 0); - - rb_define_method(rb_mKernel, "send", rb_f_send, -1); - rb_define_method(rb_mKernel, "__send__", rb_f_send, -1); - rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -1); - - rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1); - rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1); - rb_define_private_method(rb_cModule, "include", rb_mod_include, -1); - rb_define_private_method(rb_cModule, "public", rb_mod_public, -1); - rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1); - rb_define_private_method(rb_cModule, "private", rb_mod_private, -1); - rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1); - rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1); - rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1); - rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1); - rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1); - rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval, -1); - - rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, 1); - rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, 1); - rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2); - - rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0); - rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, 0); - - rb_define_singleton_method(ruby_top_self, "include", top_include, -1); - rb_define_singleton_method(ruby_top_self, "public", top_public, -1); - rb_define_singleton_method(ruby_top_self, "private", top_private, -1); - - rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1); - - rb_define_global_function("trace_var", rb_f_trace_var, -1); - rb_define_global_function("untrace_var", rb_f_untrace_var, -1); - - rb_define_global_function("set_trace_func", set_trace_func, 1); - - rb_define_virtual_variable("$SAFE", safe_getter, safe_setter); -} - -VALUE rb_f_autoload(); - -void -Init_load() +rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module) { - rb_load_path = rb_ary_new(); - rb_define_readonly_variable("$:", &rb_load_path); - rb_define_readonly_variable("$-I", &rb_load_path); - rb_define_readonly_variable("$LOAD_PATH", &rb_load_path); - - rb_features = rb_ary_new(); - rb_define_readonly_variable("$\"", &rb_features); - - rb_define_global_function("load", rb_f_load, -1); - rb_define_global_function("require", rb_f_require, 1); - rb_define_global_function("autoload", rb_f_autoload, 2); - rb_global_variable(&ruby_wrapper); -} + VALUE iclass, c, superclass = klass; -static void -scope_dup(scope) - struct SCOPE *scope; -{ - ID *tbl; - VALUE *vars; - - if (scope->flag & SCOPE_MALLOC) return; - - if (scope->local_tbl) { - tbl = scope->local_tbl; - vars = ALLOC_N(VALUE, tbl[0]+1); - *vars++ = scope->local_vars[-1]; - MEMCPY(vars, scope->local_vars, VALUE, tbl[0]); - scope->local_vars = vars; - scope->flag = SCOPE_MALLOC; + ensure_class_or_module(klass); + Check_Type(module, T_MODULE); + if (NIL_P(CREF_REFINEMENTS(cref))) { + CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new()); } else { - scope->flag = SCOPE_NOSTACK; + if (CREF_OMOD_SHARED(cref)) { + CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref))); + CREF_OMOD_SHARED_UNSET(cref); + } + if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) { + superclass = c; + while (c && RB_TYPE_P(c, T_ICLASS)) { + if (RBASIC(c)->klass == module) { + /* already used refinement */ + return; + } + c = RCLASS_SUPER(c); + } + } } -} + superclass = refinement_superclass(superclass); + c = iclass = rb_include_class_new(module, superclass); + RCLASS_SET_REFINED_CLASS(c, klass); -static void -blk_mark(data) - struct BLOCK *data; -{ - while (data) { - rb_gc_mark_frame(&data->frame); - rb_gc_mark(data->scope); - rb_gc_mark(data->var); - rb_gc_mark(data->body); - rb_gc_mark(data->self); - rb_gc_mark(data->d_vars); - rb_gc_mark(data->klass); - data = data->prev; - } -} + RCLASS_WRITE_M_TBL(c, RCLASS_M_TBL(module)); -static void -blk_free(data) - struct BLOCK *data; -{ - struct BLOCK *tmp; + rb_class_subclass_add(klass, iclass); - while (data) { - free(data->frame.argv); - tmp = data; - data = data->prev; - free(tmp); - } -} - -static void -blk_copy_prev(block) - struct BLOCK *block; -{ - struct BLOCK *tmp; - - while (block->prev) { - tmp = ALLOC_N(struct BLOCK, 1); - MEMCPY(tmp, block->prev, struct BLOCK, 1); - tmp->frame.argv = ALLOC_N(VALUE, tmp->frame.argc); - MEMCPY(tmp->frame.argv, block->frame.argv, VALUE, tmp->frame.argc); - block->prev = tmp; - block = tmp; - } + rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass); } - -static VALUE -bind_clone(self) - VALUE self; +static int +using_refinement(VALUE klass, VALUE module, VALUE arg) { - struct BLOCK *orig, *data; - VALUE bind; - - Data_Get_Struct(self, struct BLOCK, orig); - bind = Data_Make_Struct(self,struct BLOCK,blk_mark,blk_free,data); - MEMCPY(data, orig, struct BLOCK, 1); - data->frame.argv = ALLOC_N(VALUE, orig->frame.argc); - MEMCPY(data->frame.argv, orig->frame.argv, VALUE, orig->frame.argc); - - if (data->iter) { - blk_copy_prev(data); - } - else { - data->prev = 0; - } + rb_cref_t *cref = (rb_cref_t *) arg; - return bind; + rb_using_refinement(cref, klass, module); + return ST_CONTINUE; } -static VALUE -rb_f_binding(self) - VALUE self; +static void +using_module_recursive(const rb_cref_t *cref, VALUE klass) { - struct BLOCK *data; - VALUE bind; - - PUSH_BLOCK(0,0); - bind = Data_Make_Struct(rb_cBinding,struct BLOCK,blk_mark,blk_free,data); - *data = *ruby_block; + ID id_refinements; + VALUE super, module, refinements; -#ifdef USE_THREAD - data->orig_thread = rb_thread_current(); -#endif - data->iter = rb_f_iterator_p(); - if (ruby_frame->prev) { - data->frame.last_func = ruby_frame->prev->last_func; - } - data->frame.argv = ALLOC_N(VALUE, data->frame.argc); - MEMCPY(data->frame.argv, ruby_block->frame.argv, VALUE, data->frame.argc); - - if (data->iter) { - blk_copy_prev(data); - } - else { - data->prev = 0; + super = RCLASS_SUPER(klass); + if (super) { + using_module_recursive(cref, super); } + switch (BUILTIN_TYPE(klass)) { + case T_MODULE: + module = klass; + break; - scope_dup(data->scope); - POP_BLOCK(); - - return bind; -} - -#define PROC_T3 FL_USER1 -#define PROC_T4 FL_USER2 -#define PROC_T5 (FL_USER1|FL_USER2) -#define PROC_TMASK (FL_USER1|FL_USER2) + case T_ICLASS: + module = RBASIC(klass)->klass; + break; -static void -proc_save_safe_level(data) - VALUE data; -{ - if (FL_TEST(data, FL_TAINT)) { - switch (safe_level) { - case 3: - FL_SET(data, PROC_T3); - break; - case 4: - FL_SET(data, PROC_T4); - break; - case 5: - FL_SET(data, PROC_T5); - break; - } + default: + rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)", + rb_obj_classname(klass)); + break; } + CONST_ID(id_refinements, "__refinements__"); + refinements = rb_attr_get(module, id_refinements); + if (NIL_P(refinements)) return; + rb_hash_foreach(refinements, using_refinement, (VALUE) cref); } +/*! + * \private + */ static void -proc_set_safe_level(data) - VALUE data; +rb_using_module(const rb_cref_t *cref, VALUE module) { - if (FL_TEST(data, FL_TAINT)) { - switch (RBASIC(data)->flags & PROC_TMASK) { - case PROC_T3: - safe_level = 3; - break; - case PROC_T4: - safe_level = 4; - break; - case PROC_T5: - safe_level = 5; - break; - } - } -} - -static VALUE -proc_s_new(klass) - VALUE klass; -{ - volatile VALUE proc; - struct BLOCK *data; - - if (!rb_iterator_p() && !rb_f_iterator_p()) { - rb_raise(rb_eArgError, "tried to create Procedure-Object out of iterator"); - } - - proc = Data_Make_Struct(klass, struct BLOCK, blk_mark, blk_free, data); - *data = *ruby_block; - -#ifdef USE_THREAD - data->orig_thread = rb_thread_current(); -#endif - data->iter = data->prev?Qtrue:Qfalse; - data->frame.argv = ALLOC_N(VALUE, data->frame.argc); - MEMCPY(data->frame.argv, ruby_block->frame.argv, VALUE, data->frame.argc); - if (data->iter) { - blk_copy_prev(data); - } - else { - data->prev = 0; - } - - scope_dup(data->scope); - proc_save_safe_level(proc); - rb_obj_call_init(proc); - - return proc; + Check_Type(module, T_MODULE); + using_module_recursive(cref, module); + rb_clear_all_refinement_method_cache(); } +void +rb_vm_using_module(VALUE module) +{ + rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module); +} + +/* + * call-seq: + * target -> class_or_module + * + * Return the class or module refined by the receiver. + * + * module M + * refine String do + * end + * end + * + * M.refinements[0].target # => String + */ VALUE -rb_f_lambda() +rb_refinement_module_get_refined_class(VALUE module) { - return proc_s_new(rb_cProc); + ID id_refined_class; + + CONST_ID(id_refined_class, "__refined_class__"); + return rb_attr_get(module, id_refined_class); } -static int -blk_orphan(data) - struct BLOCK *data; -{ - if (data->scope && data->scope != top_scope && - (data->scope->flag & SCOPE_NOSTACK)) { - return 1; +static void +add_activated_refinement(VALUE activated_refinements, + VALUE klass, VALUE refinement) +{ + VALUE iclass, c, superclass = klass; + + if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) { + superclass = c; + while (c && RB_TYPE_P(c, T_ICLASS)) { + if (RBASIC(c)->klass == refinement) { + /* already used refinement */ + return; + } + c = RCLASS_SUPER(c); + } } -#ifdef USE_THREAD - if (data->orig_thread != rb_thread_current()) { - return 1; + superclass = refinement_superclass(superclass); + c = iclass = rb_include_class_new(refinement, superclass); + RCLASS_SET_REFINED_CLASS(c, klass); + rb_class_subclass_add(klass, iclass); + refinement = RCLASS_SUPER(refinement); + while (refinement && refinement != klass) { + c = rb_class_set_super(c, rb_include_class_new(refinement, RCLASS_SUPER(c))); + RCLASS_SET_REFINED_CLASS(c, klass); + rb_class_subclass_add(klass, c); + refinement = RCLASS_SUPER(refinement); } -#endif - return 0; + rb_hash_aset(activated_refinements, klass, iclass); } -static VALUE -proc_call(proc, args) - VALUE proc, args; /* OK */ +void +rb_refinement_setup(struct rb_refinements_data *data, VALUE module, VALUE klass) { - struct BLOCK * volatile old_block; - struct BLOCK *data; - volatile VALUE result = Qnil; - int state; - volatile int orphan; - volatile int safe = safe_level; - - if (TYPE(args) == T_ARRAY) { - switch (RARRAY(args)->len) { - case 0: - args = Qnil; - break; - case 1: - args = RARRAY(args)->ptr[0]; - break; - } - } + VALUE refinement; + ID id_refinements, id_activated_refinements, + id_refined_class, id_defined_at; + VALUE refinements, activated_refinements; - Data_Get_Struct(proc, struct BLOCK, data); - orphan = blk_orphan(data); - - /* PUSH BLOCK from data */ - old_block = ruby_block; - ruby_block = data; - PUSH_ITER(ITER_CUR); - ruby_frame->iter = ITER_CUR; - - if (orphan) {/* orphan procedure */ - if (rb_iterator_p()) { - ruby_block->frame.iter = ITER_CUR; - } - else { - ruby_block->frame.iter = ITER_NOT; - } + CONST_ID(id_refinements, "__refinements__"); + refinements = rb_attr_get(module, id_refinements); + if (NIL_P(refinements)) { + refinements = hidden_identity_hash_new(); + rb_ivar_set(module, id_refinements, refinements); } - - PUSH_TAG(PROT_NONE); - state = EXEC_TAG(); - if (state == 0) { - proc_set_safe_level(proc); - result = rb_yield_0(args, 0, 0); + CONST_ID(id_activated_refinements, "__activated_refinements__"); + activated_refinements = rb_attr_get(module, id_activated_refinements); + if (NIL_P(activated_refinements)) { + activated_refinements = hidden_identity_hash_new(); + rb_ivar_set(module, id_activated_refinements, + activated_refinements); } - POP_TAG(); - - POP_ITER(); - if (ruby_block->tag->dst == state) { - state &= TAG_MASK; + refinement = rb_hash_lookup(refinements, klass); + if (NIL_P(refinement)) { + VALUE superclass = refinement_superclass(klass); + refinement = rb_refinement_new(); + rb_class_set_super(refinement, superclass); + RUBY_ASSERT(BUILTIN_TYPE(refinement) == T_MODULE); + FL_SET(refinement, RMODULE_IS_REFINEMENT); + CONST_ID(id_refined_class, "__refined_class__"); + rb_ivar_set(refinement, id_refined_class, klass); + CONST_ID(id_defined_at, "__defined_at__"); + rb_ivar_set(refinement, id_defined_at, module); + rb_hash_aset(refinements, klass, refinement); + add_activated_refinement(activated_refinements, klass, refinement); } - ruby_block = old_block; - safe_level = safe; - if (state) { - if (orphan) {/* orphan procedure */ - switch (state) { - case TAG_BREAK: - rb_raise(rb_eLocalJumpError, "break from proc-closure"); - break; - case TAG_RETRY: - rb_raise(rb_eLocalJumpError, "retry from proc-closure"); - break; - case TAG_RETURN: - rb_raise(rb_eLocalJumpError, "return from proc-closure"); - break; - } - } - JUMP_TAG(state); - } - return result; + data->refinement = refinement; + data->refinements = activated_refinements; } +/* + * call-seq: + * refine(mod) { block } -> module + * + * Refine <i>mod</i> in the receiver. + * + * Returns a module, where refined methods are defined. + */ + static VALUE -block_pass(self, node) - VALUE self; - NODE *node; +rb_mod_refine(VALUE module, VALUE klass) { - VALUE block = rb_eval(self, node->nd_body); - struct BLOCK * volatile old_block; - struct BLOCK *data; - volatile VALUE result = Qnil; - int state; - volatile int orphan; - volatile int safe = safe_level; + /* module is the receiver of #refine, klass is a module to be refined (`mod` in the doc) */ + rb_thread_t *th = GET_THREAD(); + VALUE block_handler = rb_vm_frame_block_handler(th->ec->cfp); + struct rb_refinements_data data; - if (NIL_P(block)) { - return rb_eval(self, node->nd_iter); - } - if (rb_obj_is_kind_of(block, rb_cMethod)) { - block = method_proc(block); + if (block_handler == VM_BLOCK_HANDLER_NONE) { + rb_raise(rb_eArgError, "no block given"); } - else if (!rb_obj_is_proc(block)) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc)", - rb_class2name(CLASS_OF(block))); + if (vm_block_handler_type(block_handler) != block_handler_type_iseq) { + rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine"); } - Data_Get_Struct(block, struct BLOCK, data); - orphan = blk_orphan(data); + ensure_class_or_module(klass); - /* PUSH BLOCK from data */ - old_block = ruby_block; - ruby_block = data; - PUSH_ITER(ITER_PRE); - ruby_frame->iter = ITER_PRE; - - PUSH_TAG(PROT_NONE); - state = EXEC_TAG(); - if (state == 0) { - proc_set_safe_level(block); - result = rb_eval(self, node->nd_iter); - } - POP_TAG(); - POP_ITER(); - if (ruby_block->tag->dst == state) { - state &= TAG_MASK; - orphan = 2; - } - ruby_block = old_block; - safe_level = safe; + rb_refinement_setup(&data, module, klass); - if (state) { - if (orphan == 2) {/* escape from orphan procedure */ - switch (state) { - case TAG_BREAK: - rb_raise(rb_eLocalJumpError, "break from proc-closure"); - break; - case TAG_RETRY: - rb_raise(rb_eLocalJumpError, "retry from proc-closure"); - break; - case TAG_RETURN: - rb_raise(rb_eLocalJumpError, "return from proc-closure"); - break; - } - } - JUMP_TAG(state); - } - return result; + rb_yield_refine_block(data.refinement, data.refinements); + return data.refinement; } -struct METHOD { - VALUE klass, oklass; - VALUE recv; - ID id, oid; - NODE *body; -}; - static void -bm_mark(data) - struct METHOD *data; +ignored_block(VALUE module, const char *klass) { - rb_gc_mark(data->oklass); - rb_gc_mark(data->klass); - rb_gc_mark(data->recv); - rb_gc_mark(data->body); + const char *anon = ""; + Check_Type(module, T_MODULE); + if (!RTEST(rb_search_class_path(module))) { + anon = ", maybe for Module.new"; + } + rb_warn("%s""using doesn't call the given block""%s.", klass, anon); } +/* + * call-seq: + * using(module) -> self + * + * Import class refinements from <i>module</i> into the current class or + * module definition. + */ + static VALUE -rb_obj_method(obj, vid) - VALUE obj; - VALUE vid; +mod_using(VALUE self, VALUE module) { - VALUE method; - VALUE klass = CLASS_OF(obj); - ID id; - NODE *body; - int noex; - struct METHOD *data; - - id = rb_to_id(vid); - - again: - if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) { - return rb_undefined(obj, rb_to_id(vid), 0, 0, 0); - } + rb_control_frame_t *prev_cfp = previous_frame(GET_EC()); - if (nd_type(body) == NODE_ZSUPER) { - klass = RCLASS(klass)->super; - goto again; + if (prev_frame_func()) { + rb_raise(rb_eRuntimeError, + "Module#using is not permitted in methods"); } - - method = Data_Make_Struct(rb_cMethod, struct METHOD, bm_mark, free, data); - data->klass = klass; - data->recv = obj; - data->id = id; - data->body = body; - data->oklass = CLASS_OF(obj); - data->oid = rb_to_id(vid); - if (FL_TEST(obj, FL_TAINT)) { - FL_SET(method, FL_TAINT); - } - - return method; -} - -static VALUE -method_call(argc, argv, method) - int argc; - VALUE *argv; - VALUE method; -{ - VALUE result; - struct METHOD *data; - int state; - volatile int safe = safe_level; - - Data_Get_Struct(method, struct METHOD, data); - PUSH_ITER(rb_iterator_p()?ITER_PRE:ITER_NOT); - PUSH_TAG(PROT_NONE); - if (FL_TEST(data->recv, FL_TAINT) || FL_TEST(method, FL_TAINT)) { - FL_SET(method, FL_TAINT); - if (safe_level < 4) safe_level = 4; + if (prev_cfp && prev_cfp->self != self) { + rb_raise(rb_eRuntimeError, "Module#using is not called on self"); } - if ((state = EXEC_TAG()) == 0) { - result = rb_call0(data->klass, data->recv, data->id, - argc, argv, data->body, 0); + if (rb_block_given_p()) { + ignored_block(module, "Module#"); } - POP_TAG(); - POP_ITER(); - safe_level = safe; - if (state) JUMP_TAG(state); - return result; + rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module); + return self; } -static VALUE -method_inspect(method) - VALUE method; -{ - struct METHOD *data; - VALUE str; - char *s; - - Data_Get_Struct(method, struct METHOD, data); - str = rb_str_new2("#<"); - s = rb_class2name(CLASS_OF(method)); - rb_str_cat(str, s, strlen(s)); - rb_str_cat(str, ": ", 2); - s = rb_class2name(data->oklass); - rb_str_cat(str, s, strlen(s)); - rb_str_cat(str, "#", 1); - s = rb_id2name(data->oid); - rb_str_cat(str, s, strlen(s)); - rb_str_cat(str, ">", 1); - - return str; -} +/* + * call-seq: + * refinements -> array + * + * Returns an array of +Refinement+ defined within the receiver. + * + * module A + * refine Integer do + * end + * + * refine String do + * end + * end + * + * p A.refinements + * + * <em>produces:</em> + * + * [#<refinement:Integer@A>, #<refinement:String@A>] + */ static VALUE -mproc() +mod_refinements(VALUE self) { - VALUE proc; - - /* emulate ruby's method call */ - PUSH_ITER(ITER_CUR); - PUSH_FRAME(); - proc = rb_f_lambda(); - POP_FRAME(); - POP_ITER(); + ID id_refinements; + VALUE refinements; - return proc; -} - -static VALUE -mcall(args, method) - VALUE args, method; -{ - if (TYPE(args) == T_ARRAY) { - return method_call(RARRAY(args)->len, RARRAY(args)->ptr, method); + CONST_ID(id_refinements, "__refinements__"); + refinements = rb_attr_get(self, id_refinements); + if (NIL_P(refinements)) { + return rb_ary_new(); } - return method_call(1, &args, method); -} - -static VALUE -method_proc(method) - VALUE method; -{ - return rb_iterate(mproc, 0, mcall, method); -} - -void -Init_Proc() -{ - rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError); - rb_eSysStackError = rb_define_class("SystemStackError", rb_eStandardError); - - rb_cProc = rb_define_class("Proc", rb_cObject); - rb_define_singleton_method(rb_cProc, "new", proc_s_new, 0); - - rb_define_method(rb_cProc, "call", proc_call, -2); - rb_define_method(rb_cProc, "[]", proc_call, -2); - rb_define_global_function("proc", rb_f_lambda, 0); - rb_define_global_function("lambda", rb_f_lambda, 0); - rb_define_global_function("binding", rb_f_binding, 0); - rb_cBinding = rb_define_class("Binding", rb_cObject); - rb_undef_method(CLASS_OF(rb_cMethod), "new"); - rb_define_method(rb_cBinding, "clone", bind_clone, 0); - - rb_cMethod = rb_define_class("Method", rb_cObject); - rb_undef_method(CLASS_OF(rb_cMethod), "new"); - rb_define_method(rb_cMethod, "call", method_call, -1); - rb_define_method(rb_cMethod, "[]", method_call, -1); - rb_define_method(rb_cMethod, "inspect", method_inspect, 0); - rb_define_method(rb_cMethod, "to_s", method_inspect, 0); - rb_define_method(rb_cMethod, "to_proc", method_proc, 0); - rb_define_method(rb_mKernel, "method", rb_obj_method, 1); + return rb_hash_values(refinements); } -#ifdef USE_THREAD - -static VALUE rb_eThreadError; - -int rb_thread_pending = 0; - -VALUE rb_cThread; - -#include <sys/types.h> -#ifdef HAVE_SYS_TIME_H -# include <sys/time.h> -#else -#ifndef NT -struct timeval { - long tv_sec; /* seconds */ - long tv_usec; /* and microseconds */ -}; -#endif /* NT */ -#endif -#include <signal.h> -#include <errno.h> - -#ifdef HAVE_SYS_SELECT_H -#include <sys/select.h> -#endif - -extern VALUE rb_last_status; - -enum thread_status { - THREAD_RUNNABLE, - THREAD_STOPPED, - THREAD_TO_KILL, - THREAD_KILLED, -}; - -#define WAIT_FD (1<<0) -#define WAIT_TIME (1<<1) -#define WAIT_JOIN (1<<2) - -/* +infty, for this purpose */ -#define DELAY_INFTY 1E30 - -typedef struct thread * thread_t; - -struct thread { - struct thread *next, *prev; - jmp_buf context; - - VALUE result; - - int stk_len; - int stk_max; - VALUE*stk_ptr; - VALUE*stk_pos; - - struct FRAME *frame; - struct SCOPE *scope; - struct RVarmap *dyna_vars; - struct BLOCK *block; - struct BLOCK *cblock; - struct iter *iter; - struct tag *tag; - VALUE klass; - VALUE wrapper; - - VALUE trace; - int misc; /* misc. states (vmode/rb_trap_immediate) */ - - char *file; - int line; - - VALUE rb_errinfo; - VALUE last_status; - VALUE last_line; - VALUE last_match; - - int safe; - - enum thread_status status; - int wait_for; - int fd; - double delay; - thread_t join; - - int abort; - - st_table *locals; - - VALUE thread; -}; - -static thread_t curr_thread; -static int num_waiting_on_fd; -static int num_waiting_on_timer; -static int num_waiting_on_join; - -#define FOREACH_THREAD_FROM(f,x) x = f; do { x = x->next; -#define END_FOREACH_FROM(f,x) } while (x != f) - -#define FOREACH_THREAD(x) FOREACH_THREAD_FROM(curr_thread,x) -#define END_FOREACH(x) END_FOREACH_FROM(curr_thread,x) - -/* Return the current time as a floating-point number */ -static double -timeofday() -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6; -} - -static thread_t main_thread; - -#define ADJ(addr) (void*)(((VALUE*)(addr)-th->stk_pos)+th->stk_ptr) -#define STACK(addr) (th->stk_pos<(addr) && (addr)<th->stk_pos+th->stk_len) +static int +used_modules_i(VALUE _, VALUE mod, VALUE ary) +{ + ID id_defined_at; + CONST_ID(id_defined_at, "__defined_at__"); + while (BUILTIN_TYPE(rb_class_of(mod)) == T_MODULE && FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) { + rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at)); + mod = RCLASS_SUPER(mod); + } + return ST_CONTINUE; +} + +/* + * call-seq: + * used_modules -> array + * + * Returns an array of all modules used in the current scope. The ordering + * of modules in the resulting array is not defined. + * + * module A + * refine Object do + * end + * end + * + * module B + * refine Object do + * end + * end + * + * using A + * using B + * p Module.used_modules + * + * <em>produces:</em> + * + * [B, A] + */ +static VALUE +rb_mod_s_used_modules(VALUE _) +{ + const rb_cref_t *cref = rb_vm_cref(); + VALUE ary = rb_ary_new(); -static void -thread_mark(th) - thread_t th; -{ - struct FRAME *frame; - struct BLOCK *block; - - rb_gc_mark(th->result); - rb_gc_mark(th->thread); - if (th->join) rb_gc_mark(th->join->thread); - - rb_gc_mark(th->klass); - rb_gc_mark(th->wrapper); - - rb_gc_mark(th->scope); - rb_gc_mark(th->dyna_vars); - rb_gc_mark(th->rb_errinfo); - rb_gc_mark(th->last_line); - rb_gc_mark(th->last_match); - - /* mark data in copied stack */ - if (th->status == THREAD_KILLED) return; - if (th->stk_len == 0) return; /* stack not active, no need to mark. */ - if (th->stk_ptr) { - rb_gc_mark_locations(th->stk_ptr, th->stk_ptr+th->stk_len); -#if defined(THINK_C) || defined(__human68k__) - rb_gc_mark_locations(th->stk_ptr+2, th->stk_ptr+th->stk_len+2); -#endif - } - frame = th->frame; - while (frame && frame != top_frame) { - frame = ADJ(frame); - if (frame->argv && !STACK(frame->argv)) { - rb_gc_mark_frame(frame); - } - frame = frame->prev; - } - block = th->block; - while (block) { - block = ADJ(block); - if (block->frame.argv && !STACK(block->frame.argv)) { - rb_gc_mark_frame(&block->frame); - } - block = block->prev; + while (cref) { + if (!NIL_P(CREF_REFINEMENTS(cref))) { + rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary); + } + cref = CREF_NEXT(cref); } - rb_mark_tbl(th->locals); -} -void -rb_gc_mark_threads() -{ - thread_t th; - - if (!curr_thread) return; - FOREACH_THREAD(th) { - rb_gc_mark(th->thread); - } END_FOREACH(th); + return rb_funcall(ary, rb_intern("uniq"), 0); } -static void -thread_free(th) - thread_t th; -{ - if (th->stk_ptr) free(th->stk_ptr); - th->stk_ptr = 0; - if (th->locals) st_free_table(th->locals); - if (th != main_thread) free(th); -} +static int +used_refinements_i(VALUE _, VALUE mod, VALUE ary) +{ + while (BUILTIN_TYPE(rb_class_of(mod)) == T_MODULE && FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) { + rb_ary_push(ary, rb_class_of(mod)); + mod = RCLASS_SUPER(mod); + } + return ST_CONTINUE; +} + +/* + * call-seq: + * used_refinements -> array + * + * Returns an array of all modules used in the current scope. The ordering + * of modules in the resulting array is not defined. + * + * module A + * refine Object do + * end + * end + * + * module B + * refine Object do + * end + * end + * + * using A + * using B + * p Module.used_refinements + * + * <em>produces:</em> + * + * [#<refinement:Object@B>, #<refinement:Object@A>] + */ +static VALUE +rb_mod_s_used_refinements(VALUE _) +{ + const rb_cref_t *cref = rb_vm_cref(); + VALUE ary = rb_ary_new(); -static thread_t -rb_thread_check(data) - VALUE data; -{ - if (TYPE(data) != T_DATA || RDATA(data)->dfree != thread_free) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected Thread)", - rb_class2name(CLASS_OF(data))); + while (cref) { + if (!NIL_P(CREF_REFINEMENTS(cref))) { + rb_hash_foreach(CREF_REFINEMENTS(cref), used_refinements_i, ary); + } + cref = CREF_NEXT(cref); } - return (thread_t)RDATA(data)->data; -} -static void -rb_thread_save_context(th) - thread_t th; -{ - VALUE v; - - int len = stack_length(); - th->stk_len = 0; - th->stk_pos = (rb_gc_stack_start<(VALUE*)&v)?rb_gc_stack_start - :rb_gc_stack_start - len; - if (len > th->stk_max) { - REALLOC_N(th->stk_ptr, VALUE, len); - th->stk_max = len; - } - th->stk_len = len; - FLUSH_REGISTER_WINDOWS; - MEMCPY(th->stk_ptr, th->stk_pos, VALUE, th->stk_len); - - th->frame = ruby_frame; - th->scope = ruby_scope; - th->klass = ruby_class; - th->wrapper = ruby_wrapper; - th->dyna_vars = ruby_dyna_vars; - th->block = ruby_block; - th->cblock = ruby_calling_block; - th->misc = scope_vmode | (rb_trap_immediate<<8); - th->iter = ruby_iter; - th->tag = prot_tag; - th->rb_errinfo = rb_errinfo; - th->last_status = rb_last_status; - th->last_line = rb_lastline_get(); - th->last_match = rb_backref_get(); - th->safe = safe_level; - - th->trace = trace_func; - th->file = ruby_sourcefile; - th->line = ruby_sourceline; - - th->locals = 0; + return ary; } -static void rb_thread_restore_context _((thread_t,int)); - -static void -stack_extend(th, exit) - thread_t th; - int exit; -{ - VALUE space[1024]; - - memset(space, 0, 1); /* prevent array from optimization */ - rb_thread_restore_context(th, exit); -} +struct refinement_import_methods_arg { + rb_cref_t *cref; + VALUE refinement; + VALUE module; +}; -static int th_raise_argc; -static VALUE th_raise_argv[2]; -static char *th_raise_file; -static int th_raise_line; -static VALUE th_cmd; -static int th_sig; +/* vm.c */ +rb_cref_t *rb_vm_cref_dup_without_refinements(const rb_cref_t *cref); -static void -rb_thread_restore_context(th, exit) - thread_t th; - int exit; +static enum rb_id_table_iterator_result +refinement_import_methods_i(ID key, VALUE value, void *data) { - VALUE v; - static thread_t tmp; - static int ex; - - if (!th->stk_ptr) rb_bug("unsaved context"); + const rb_method_entry_t *me = (const rb_method_entry_t *)value; + struct refinement_import_methods_arg *arg = (struct refinement_import_methods_arg *)data; - if (&v < rb_gc_stack_start) { - /* Stack grows downward */ - if (&v > th->stk_pos) stack_extend(th, exit); + if (me->def->type != VM_METHOD_TYPE_ISEQ) { + rb_raise(rb_eArgError, "Can't import method which is not defined with Ruby code: %"PRIsVALUE"#%"PRIsVALUE, rb_class_path(arg->module), rb_id2str(key)); } - else { - /* Stack grows upward */ - if (&v < th->stk_pos + th->stk_len) stack_extend(th, exit); - } - - ruby_frame = th->frame; - ruby_scope = th->scope; - ruby_class = th->klass; - ruby_wrapper = th->wrapper; - ruby_dyna_vars = th->dyna_vars; - ruby_block = th->block; - ruby_calling_block = th->cblock; - scope_vmode = th->misc&SCOPE_MASK; - rb_trap_immediate = th->misc>>8; - ruby_iter = th->iter; - prot_tag = th->tag; - rb_errinfo = th->rb_errinfo; - rb_last_status = th->last_status; - safe_level = th->safe; - - trace_func = th->trace; - ruby_sourcefile = th->file; - ruby_sourceline = th->line; - - tmp = th; - ex = exit; - FLUSH_REGISTER_WINDOWS; - MEMCPY(tmp->stk_pos, tmp->stk_ptr, VALUE, tmp->stk_len); - - rb_lastline_set(tmp->last_line); - rb_backref_set(tmp->last_match); - - switch (ex) { - case 1: - JUMP_TAG(TAG_FATAL); - break; - - case 2: - rb_interrupt(); - break; - - case 3: - rb_trap_eval(th_cmd, th_sig); - errno = EINTR; - break; - - case 4: - ruby_frame->last_func = 0; - ruby_sourcefile = th_raise_file; - ruby_sourceline = th_raise_line; - rb_f_raise(th_raise_argc, th_raise_argv); - break; - - default: - longjmp(tmp->context, 1); - } -} - -static void -rb_thread_ready(th) - thread_t th; -{ - /* The thread is no longer waiting on anything */ - if (th->wait_for & WAIT_FD) { - num_waiting_on_fd--; - } - if (th->wait_for & WAIT_TIME) { - num_waiting_on_timer--; - } - if (th->wait_for & WAIT_JOIN) { - num_waiting_on_join--; - } - th->wait_for = 0; - th->status = THREAD_RUNNABLE; + rb_cref_t *new_cref = rb_vm_cref_dup_without_refinements(me->def->body.iseq.cref); + CREF_REFINEMENTS_SET(new_cref, CREF_REFINEMENTS(arg->cref)); + rb_add_method_iseq(arg->refinement, key, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me)); + return ID_TABLE_CONTINUE; } -static void -rb_thread_remove() -{ - rb_thread_ready(curr_thread); - curr_thread->status = THREAD_KILLED; - curr_thread->prev->next = curr_thread->next; - curr_thread->next->prev = curr_thread->prev; -} +/* + * Note: docs for the method are in class.c + */ -static int -rb_thread_dead(th) - thread_t th; -{ - return th->status == THREAD_KILLED; -} - -static void -rb_thread_deadlock() -{ - curr_thread = main_thread; - th_raise_argc = 1; - th_raise_argv[0] = rb_exc_new2(rb_eFatal, "Thread: deadlock"); - th_raise_file = ruby_sourcefile; - th_raise_line = ruby_sourceline; - rb_abort(); -} - -void -rb_thread_schedule() +static VALUE +refinement_import_methods(int argc, VALUE *argv, VALUE refinement) { - thread_t next; /* OK */ - thread_t th; - thread_t curr; - - select_err: - rb_thread_pending = 0; - if (curr_thread == curr_thread->next) return; - - next = 0; - curr = curr_thread; /* starting thread */ - - while (curr->status == THREAD_KILLED) { - curr = curr->prev; - } - - FOREACH_THREAD_FROM(curr, th) { - if (th->status != THREAD_STOPPED && th->status != THREAD_KILLED) { - next = th; - break; - } - } - END_FOREACH_FROM(curr, th); - - if (num_waiting_on_join) { - FOREACH_THREAD_FROM(curr, th) { - if ((th->wait_for&WAIT_JOIN) && rb_thread_dead(th->join)) { - th->join = 0; - th->wait_for &= ~WAIT_JOIN; - th->status = THREAD_RUNNABLE; - num_waiting_on_join--; - if (!next) next = th; - } - } - END_FOREACH_FROM(curr, th); - } - - if (num_waiting_on_fd > 0 || num_waiting_on_timer > 0) { - fd_set readfds; - struct timeval delay_tv, *delay_ptr; - double delay, now; /* OK */ - - int n, max; - - do { - max = 0; - FD_ZERO(&readfds); - if (num_waiting_on_fd > 0) { - FOREACH_THREAD_FROM(curr, th) { - if (th->wait_for & WAIT_FD) { - FD_SET(th->fd, &readfds); - if (th->fd > max) max = th->fd; - } - } - END_FOREACH_FROM(curr, th); - } - - delay = DELAY_INFTY; - if (num_waiting_on_timer > 0) { - now = timeofday(); - FOREACH_THREAD_FROM(curr, th) { - if (th->wait_for & WAIT_TIME) { - if (th->delay <= now) { - th->delay = 0.0; - th->wait_for &= ~WAIT_TIME; - th->status = THREAD_RUNNABLE; - num_waiting_on_timer--; - next = th; - } else if (th->delay < delay) { - delay = th->delay; - } - } - } - END_FOREACH_FROM(curr, th); - } - /* Do the select if needed */ - if (num_waiting_on_fd > 0 || !next) { - /* Convert delay to a timeval */ - /* If a thread is runnable, just poll */ - if (next) { - delay_tv.tv_sec = 0; - delay_tv.tv_usec = 0; - delay_ptr = &delay_tv; - } - else if (delay == DELAY_INFTY) { - delay_ptr = 0; - } - else { - delay -= now; - delay_tv.tv_sec = (unsigned int)delay; - delay_tv.tv_usec = (long)((delay-(double)delay_tv.tv_sec)*1e6); - delay_ptr = &delay_tv; - } - - n = select(max+1, &readfds, 0, 0, delay_ptr); - if (n < 0) { - if (rb_trap_pending) rb_trap_exec(); - goto select_err; - } - if (n > 0) { - /* Some descriptors are ready. - Make the corresponding threads runnable. */ - FOREACH_THREAD_FROM(curr, th) { - if ((th->wait_for&WAIT_FD) - && FD_ISSET(th->fd, &readfds)) { - /* Wake up only one thread per fd. */ - FD_CLR(th->fd, &readfds); - th->status = THREAD_RUNNABLE; - th->fd = 0; - th->wait_for &= ~WAIT_FD; - num_waiting_on_fd--; - if (!next) next = th; /* Found one. */ - } - } - END_FOREACH_FROM(curr, th); - } - } - /* The delays for some of the threads should have expired. - Go through the loop once more, to check the delays. */ - } while (!next && delay != DELAY_INFTY); - } - - if (!next) { - curr_thread->file = ruby_sourcefile; - curr_thread->line = ruby_sourceline; - FOREACH_THREAD_FROM(curr, th) { - fprintf(stderr, "%s:%d:deadlock 0x%x: %d:%d %s\n", - th->file, th->line, th->thread, th->status, - th->wait_for, th==main_thread?"(main)":""); - } - END_FOREACH_FROM(curr, th); - /* raise fatal error to main thread */ - rb_thread_deadlock(); - } - if (next->status == THREAD_RUNNABLE && next == curr_thread) { - return; - } + int i; + struct refinement_import_methods_arg arg; - /* context switch */ - if (curr == curr_thread) { - rb_thread_save_context(curr); - if (setjmp(curr->context)) { - return; - } + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); + for (i = 0; i < argc; i++) { + Check_Type(argv[i], T_MODULE); + if (RCLASS_SUPER(argv[i])) { + rb_warn("%"PRIsVALUE" has ancestors, but Refinement#import_methods doesn't import their methods", rb_class_path(argv[i])); + } } - - curr_thread = next; - if (next->status == THREAD_TO_KILL) { - /* execute ensure-clause if any */ - rb_thread_restore_context(next, 1); + arg.cref = rb_vm_cref_replace_with_duplicated_cref(); + arg.refinement = refinement; + for (i = 0; i < argc; i++) { + arg.module = argv[i]; + struct rb_id_table *m_tbl = RCLASS_M_TBL(argv[i]); + if (!m_tbl) continue; + rb_id_table_foreach(m_tbl, refinement_import_methods_i, &arg); } - rb_thread_restore_context(next, 0); + return refinement; } void -rb_thread_wait_fd(fd) - int fd; +rb_obj_call_init(VALUE obj, int argc, const VALUE *argv) { - if (curr_thread == curr_thread->next) return; - - curr_thread->status = THREAD_STOPPED; - curr_thread->fd = fd; - num_waiting_on_fd++; - curr_thread->wait_for |= WAIT_FD; - rb_thread_schedule(); + rb_obj_call_init_kw(obj, argc, argv, RB_NO_KEYWORDS); } void -rb_thread_fd_writable(fd) - int fd; +rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat) { - struct timeval zero; - fd_set fds; - - if (curr_thread == curr_thread->next) return; - - zero.tv_sec = zero.tv_usec = 0; - for (;;) { - FD_ZERO(&fds); - FD_SET(fd, &fds); - if (select(fd+1, 0, &fds, 0, &zero) == 1) break; - rb_thread_schedule(); - } + PASS_PASSED_BLOCK_HANDLER(); + rb_funcallv_kw(obj, idInitialize, argc, argv, kw_splat); } void -rb_thread_wait_for(time) - struct timeval time; +rb_extend_object(VALUE obj, VALUE module) { - double date; - - if (curr_thread == curr_thread->next) { - int n; -#ifndef linux - double d, limit; - limit = timeofday()+(double)time.tv_sec+(double)time.tv_usec*1e-6; -#endif - for (;;) { - TRAP_BEG; - n = select(0, 0, 0, 0, &time); - TRAP_END; - if (n == 0) return; - -#ifndef linux - d = limit - timeofday(); - - time.tv_sec = (int)d; - time.tv_usec = (int)((d - (int)d)*1e6); - if (time.tv_usec < 0) { - time.tv_usec += (long)1e6; - time.tv_sec -= 1; - } - if (time.tv_sec < 0) return; -#endif - } - } - - date = timeofday() + (double)time.tv_sec + (double)time.tv_usec*1e-6; - curr_thread->status = THREAD_STOPPED; - curr_thread->delay = date; - num_waiting_on_timer++; - curr_thread->wait_for |= WAIT_TIME; - rb_thread_schedule(); + rb_include_module(rb_singleton_class(obj), module); } -void rb_thread_sleep_forever _((void)); - -int -rb_thread_alone() +/* + * call-seq: + * extend_object(obj) -> obj + * + * Extends the specified object by adding this module's constants and + * methods (which are added as singleton methods). This is the callback + * method used by Object#extend. + * + * module Picky + * def Picky.extend_object(o) + * if String === o + * puts "Can't add Picky to a String" + * else + * puts "Picky added to #{o.class}" + * super + * end + * end + * end + * (s = Array.new).extend Picky # Call Object.extend + * (s = "quick brown fox").extend Picky + * + * <em>produces:</em> + * + * Picky added to Array + * Can't add Picky to a String + */ + +static VALUE +rb_mod_extend_object(VALUE mod, VALUE obj) { - return curr_thread == curr_thread->next; + rb_extend_object(obj, mod); + return obj; } -int -rb_thread_select(max, read, write, except, timeout) - int max; - fd_set *read, *write, *except; - struct timeval *timeout; +/* + * call-seq: + * obj.extend(module, ...) -> obj + * + * Adds to _obj_ the instance methods from each module given as a + * parameter. + * + * module Mod + * def hello + * "Hello from Mod.\n" + * end + * end + * + * class Klass + * def hello + * "Hello from Klass.\n" + * end + * end + * + * k = Klass.new + * k.hello #=> "Hello from Klass.\n" + * k.extend(Mod) #=> #<Klass:0x401b3bc8> + * k.hello #=> "Hello from Mod.\n" + */ + +static VALUE +rb_obj_extend(int argc, VALUE *argv, VALUE obj) { - double limit; - struct timeval zero; - fd_set r, *rp, w, *wp, x, *xp; - int n; - - if (!read && !write && !except) { - if (!timeout) { - rb_thread_sleep_forever(); - return 0; - } - rb_thread_wait_for(*timeout); - return 0; - } - - if (timeout) { - limit = timeofday()+ - (double)timeout->tv_sec+(double)timeout->tv_usec*1e-6; - } + int i; + ID id_extend_object, id_extended; - if (curr_thread == curr_thread->next) { /* no other thread */ -#ifndef linux - struct timeval tv, *tvp = timeout; - - if (timeout) { - tv = *timeout; - tvp = &tv; - } - for (;;) { - TRAP_BEG; - n = select(max, read, write, except, tvp); - TRAP_END; - if (n < 0 && errno == EINTR) { - if (timeout) { - double d = timeofday() - limit; - - tv.tv_sec = (unsigned int)d; - tv.tv_usec = (long)((d-(double)tv.tv_sec)*1e6); - } - continue; - } - return n; - } -#else - for (;;) { - TRAP_BEG; - n = select(max, read, write, except, timeout); - TRAP_END; - if (n < 0 && errno == EINTR) { - continue; - } - return n; - } -#endif + CONST_ID(id_extend_object, "extend_object"); + CONST_ID(id_extended, "extended"); + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); + for (i = 0; i < argc; i++) { + Check_Type(argv[i], T_MODULE); + if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) { + rb_raise(rb_eTypeError, "Cannot extend object with refinement"); + } } - - for (;;) { - zero.tv_sec = zero.tv_usec = 0; - if (read) {rp = &r; r = *read;} else {rp = 0;} - if (write) {wp = &w; w = *write;} else {wp = 0;} - if (except) {xp = &x; x = *except;} else {xp = 0;} - n = select(max, rp, wp, xp, &zero); - if (n > 0) { - /* write back fds */ - if (read) {*read = r;} - if (write) {*write = w;} - if (except) {*except = x;} - return n; - } - if (n < 0 && errno != EINTR) { - return n; - } - if (timeout) { - if (timeout->tv_sec == 0 && timeout->tv_usec == 0) return 0; - if (limit <= timeofday()) return 0; - } - - rb_thread_schedule(); - CHECK_INTS; + while (argc--) { + rb_funcall(argv[argc], id_extend_object, 1, obj); + rb_funcall(argv[argc], id_extended, 1, obj); } -} - -static VALUE -rb_thread_join(thread) - VALUE thread; -{ - thread_t th = rb_thread_check(thread); - - if (rb_thread_dead(th)) return thread; - if ((th->wait_for & WAIT_JOIN) && th->join == curr_thread) - rb_raise(rb_eThreadError, "Thread#join: deadlock"); - curr_thread->status = THREAD_STOPPED; - curr_thread->join = th; - num_waiting_on_join++; - curr_thread->wait_for |= WAIT_JOIN; - rb_thread_schedule(); - - return thread; -} - -static VALUE -rb_thread_s_join(dmy, thread) /* will be removed in 1.4 */ - VALUE dmy; - VALUE thread; -{ - rb_warn("Thread::join is obsolete; use Thread#join instead"); - return rb_thread_join(thread); -} - -VALUE -rb_thread_current() -{ - return curr_thread->thread; + return obj; } VALUE -rb_thread_main() -{ - return main_thread->thread; -} - -static VALUE -rb_thread_wakeup(thread) - VALUE thread; -{ - thread_t th = rb_thread_check(thread); - - if (th->status == THREAD_KILLED) - rb_raise(rb_eThreadError, "killed thread"); - rb_thread_ready(th); - - return thread; -} - -static VALUE -rb_thread_run(thread) - VALUE thread; +rb_top_main_class(const char *method) { - rb_thread_wakeup(thread); - if (!rb_thread_critical) rb_thread_schedule(); + VALUE klass = GET_THREAD()->top_wrapper; - return thread; + if (!klass) return rb_cObject; + rb_warning("main.%s in the wrapped load is effective only in wrapper module", method); + return klass; } -static VALUE -rb_thread_kill(thread) - VALUE thread; -{ - thread_t th = rb_thread_check(thread); - - if (th->status == THREAD_TO_KILL || th->status == THREAD_KILLED) - return thread; - if (th == th->next || th == main_thread) rb_exit(0); - - rb_thread_ready(th); - th->status = THREAD_TO_KILL; - rb_thread_schedule(); - return Qnil; /* not reached */ -} - -static VALUE -rb_thread_s_kill(obj, th) - VALUE obj, th; -{ - return rb_thread_kill(th); -} +/* + * call-seq: + * include(module, ...) -> self + * + * Invokes Module.append_features on each parameter in turn. + * Effectively adds the methods and constants in each module to the + * receiver. + */ static VALUE -rb_thread_exit() +top_include(int argc, VALUE *argv, VALUE self) { - return rb_thread_kill(curr_thread->thread); + return rb_mod_include(argc, argv, rb_top_main_class("include")); } -static VALUE -rb_thread_pass() -{ - rb_thread_schedule(); - return Qnil; -} +/* + * call-seq: + * using(module) -> self + * + * Import class refinements from <i>module</i> into the scope where + * #using is called. + */ static VALUE -rb_thread_stop() +top_using(VALUE self, VALUE module) { - rb_thread_critical = 0; - curr_thread->status = THREAD_STOPPED; - if (curr_thread == curr_thread->next) { - rb_raise(rb_eThreadError, "stopping only thread"); - } - rb_thread_schedule(); - - return Qnil; -} + const rb_cref_t *cref = CREF_NEXT(rb_vm_cref()); + rb_control_frame_t *prev_cfp = previous_frame(GET_EC()); + rb_thread_t *th = GET_THREAD(); -struct timeval rb_time_timeval(); - -void -rb_thread_sleep(sec) - int sec; -{ - if (curr_thread == curr_thread->next) { - TRAP_BEG; - sleep(sec); - TRAP_END; - return; + if ((th->top_wrapper ? CREF_NEXT(cref) : cref) || + (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) { + rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel"); } - rb_thread_wait_for(rb_time_timeval(INT2FIX(sec))); -} - -void -rb_thread_sleep_forever() -{ - if (curr_thread == curr_thread->next) { - TRAP_BEG; - sleep((32767L<<16)+32767); - TRAP_END; - return; + if (rb_block_given_p()) { + ignored_block(module, "main."); } - - num_waiting_on_timer++; - curr_thread->delay = DELAY_INFTY; - curr_thread->wait_for |= WAIT_TIME; - curr_thread->status = THREAD_STOPPED; - rb_thread_schedule(); -} - -static int rb_thread_abort; - -static VALUE -rb_thread_s_abort_exc() -{ - return rb_thread_abort?Qtrue:Qfalse; + rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module); + return self; } -static VALUE -rb_thread_s_abort_exc_set(self, val) - VALUE self, val; +static const VALUE * +errinfo_place(const rb_execution_context_t *ec) { - rb_thread_abort = RTEST(val); - return val; -} + const rb_control_frame_t *cfp = ec->cfp; + const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec); -static VALUE -rb_thread_abort_exc(thread) - VALUE thread; -{ - thread_t th = rb_thread_check(thread); - - return th->abort?Qtrue:Qfalse; -} - -static VALUE -rb_thread_abort_exc_set(thread, val) - VALUE thread, val; -{ - thread_t th = rb_thread_check(thread); - - th->abort = RTEST(val); - return val; -} - -#define THREAD_ALLOC(th) {\ - th = ALLOC(struct thread);\ -\ - th->status = 0;\ - th->result = 0;\ - th->rb_errinfo = Qnil;\ -\ - th->stk_ptr = 0;\ - th->stk_len = 0;\ - th->stk_max = 0;\ - th->wait_for = 0;\ - th->fd = 0;\ - th->delay = 0.0;\ - th->join = 0;\ -\ - th->frame = 0;\ - th->scope = 0;\ - th->klass = 0;\ - th->dyna_vars = 0;\ - th->block = 0;\ - th->iter = 0;\ - th->tag = 0;\ - th->rb_errinfo = 0;\ - th->last_status = 0;\ - th->last_line = 0;\ - th->last_match = 0;\ - th->abort = 0;\ + while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) { + if (VM_FRAME_RUBYFRAME_P(cfp)) { + if (ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_RESCUE) { + return &cfp->ep[VM_ENV_INDEX_LAST_LVAR]; + } + else if (ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_ENSURE && + !THROW_DATA_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR]) && + !FIXNUM_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR])) { + return &cfp->ep[VM_ENV_INDEX_LAST_LVAR]; + } + } + cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); + } + return 0; } -static thread_t -rb_thread_alloc(klass) - VALUE klass; +VALUE +rb_ec_get_errinfo(const rb_execution_context_t *ec) { - thread_t th; - - THREAD_ALLOC(th); - th->thread = Data_Wrap_Struct(klass, thread_mark, thread_free, th); - - if (curr_thread) { - th->prev = curr_thread; - curr_thread->next->prev = th; - th->next = curr_thread->next; - curr_thread->next = th; + const VALUE *ptr = errinfo_place(ec); + if (ptr) { + return *ptr; } else { - curr_thread = th->prev = th->next = th; - th->status = THREAD_RUNNABLE; - } - - return th; -} - -#if defined(HAVE_SETITIMER) && !defined(__BOW__) -static void -catch_timer(sig) - int sig; -{ -#if !defined(POSIX_SIGNAL) && !defined(BSD_SIGNAL) - signal(sig, catch_timer); -#endif - if (!rb_thread_critical) { - if (rb_trap_immediate) { - rb_thread_schedule(); - } - else rb_thread_pending = 1; + return ec->errinfo; } } -#else -int rb_thread_tick = THREAD_TICK; -#endif - -static VALUE rb_thread_raise _((int, VALUE*, VALUE)); - -#define SCOPE_SHARED FL_USER1 - -#if defined(HAVE_SETITIMER) && !defined(__BOW__) -static int thread_init = 0; - -void -rb_thread_start_timer() -{ - struct itimerval tval; - - if (!thread_init) return; - tval.it_interval.tv_sec = 0; - tval.it_interval.tv_usec = 50000; - tval.it_value = tval.it_interval; - setitimer(ITIMER_VIRTUAL, &tval, NULL); -} - -void -rb_thread_stop_timer() -{ - struct itimerval tval; - - if (!thread_init) return; - tval.it_interval.tv_sec = 0; - tval.it_interval.tv_usec = 0; - tval.it_value = tval.it_interval; - setitimer(ITIMER_VIRTUAL, &tval, NULL); -} -#endif static VALUE -rb_thread_create_0(fn, arg, klass) - VALUE (*fn)(); - void *arg; - VALUE klass; +get_errinfo(void) { - thread_t th = rb_thread_alloc(klass); - enum thread_status status; - int state; - -#if defined(HAVE_SETITIMER) && !defined(__BOW__) - if (!thread_init) { -#ifdef POSIX_SIGNAL - posix_signal(SIGVTALRM, catch_timer); -#else - signal(SIGVTALRM, catch_timer); -#endif - - thread_init = 1; - rb_thread_start_timer(); - } -#endif - - FL_SET(ruby_scope, SCOPE_SHARED); - rb_thread_save_context(curr_thread); - if (setjmp(curr_thread->context)) { - return th->thread; - } - - PUSH_TAG(PROT_THREAD); - if ((state = EXEC_TAG()) == 0) { - rb_thread_save_context(th); - if (setjmp(th->context) == 0) { - curr_thread = th; - th->result = (*fn)(arg, th); - } - } - POP_TAG(); - status = th->status; - rb_thread_remove(); - if (state && status != THREAD_TO_KILL && !NIL_P(rb_errinfo)) { - if (state == TAG_FATAL) { - /* fatal error within this thread, need to stop whole script */ - main_thread->rb_errinfo = rb_errinfo; - rb_thread_cleanup(); - } - else if (rb_obj_is_kind_of(rb_errinfo, rb_eSystemExit)) { - /* delegate exception to main_thread */ - rb_thread_raise(1, &rb_errinfo, main_thread->thread); - } - else if (rb_thread_abort || curr_thread->abort || RTEST(rb_debug)) { - VALUE err = rb_exc_new(rb_eSystemExit, 0, 0); - error_print(); - /* exit on main_thread */ - rb_thread_raise(1, &err, main_thread->thread); - } - else { - curr_thread->rb_errinfo = rb_errinfo; - } - } - rb_thread_schedule(); - return 0; /* not reached */ -} - -VALUE -rb_thread_create(fn, arg) - VALUE (*fn)(); - void *arg; -{ - return rb_thread_create_0(fn, arg, rb_cThread); -} - -int -rb_thread_scope_shared_p() -{ - return FL_TEST(ruby_scope, SCOPE_SHARED); + return get_ec_errinfo(GET_EC()); } static VALUE -rb_thread_yield(arg, th) - int arg; - thread_t th; +errinfo_getter(ID id, VALUE *_) { - scope_dup(ruby_block->scope); - return rb_yield_0(th->thread, 0, 0); + return get_errinfo(); } -static VALUE -rb_thread_start(klass) - VALUE klass; +VALUE +rb_errinfo(void) { - if (!rb_iterator_p()) { - rb_raise(rb_eThreadError, "must be called as iterator"); - } - return rb_thread_create_0(rb_thread_yield, 0, klass); + return GET_EC()->errinfo; } -static VALUE -rb_thread_value(thread) - VALUE thread; +void +rb_set_errinfo(VALUE err) { - thread_t th = rb_thread_check(thread); - - rb_thread_join(thread); - if (!NIL_P(th->rb_errinfo)) { - VALUE oldbt = get_backtrace(th->rb_errinfo); - VALUE errat = make_backtrace(); - - rb_ary_unshift(errat, rb_ary_entry(oldbt, 0)); - set_backtrace(th->rb_errinfo, errat); - rb_exc_raise(th->rb_errinfo); + if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) { + rb_raise(rb_eTypeError, "assigning non-exception to $!"); } - - return th->result; + GET_EC()->errinfo = err; } static VALUE -rb_thread_status(thread) - VALUE thread; +errat_getter(ID id, VALUE *_) { - thread_t th = rb_thread_check(thread); - - if (rb_thread_dead(th)) { - if (NIL_P(th->rb_errinfo)) return Qfalse; - return Qnil; + VALUE err = get_errinfo(); + if (!NIL_P(err)) { + return rb_get_backtrace(err); } - - return Qtrue; -} - -static VALUE -rb_thread_stop_p(thread) - VALUE thread; -{ - thread_t th = rb_thread_check(thread); - - if (rb_thread_dead(th)) return Qtrue; - if (th->status == THREAD_STOPPED) return Qtrue; - return Qfalse; -} - -static void -rb_thread_wait_other_threads() -{ - /* wait other threads to terminate */ - while (curr_thread != curr_thread->next) { - rb_thread_schedule(); + else { + return Qnil; } } static void -rb_thread_cleanup() +errat_setter(VALUE val, ID id, VALUE *var) { - thread_t th; - - if (curr_thread != curr_thread->next->prev) { - curr_thread = curr_thread->prev; + VALUE err = get_errinfo(); + if (NIL_P(err)) { + rb_raise(rb_eArgError, "$! not set"); } - - FOREACH_THREAD(th) { - if (th != curr_thread && th->status != THREAD_KILLED) { - th->status = THREAD_TO_KILL; - th->wait_for = 0; - } - } - END_FOREACH(th); + set_backtrace(err, val); } -int rb_thread_critical; - -static VALUE -rb_thread_get_critical() -{ - return rb_thread_critical?Qtrue:Qfalse; -} +/* + * call-seq: + * __method__ -> symbol + * + * Returns the name at the definition of the current method as a + * Symbol. + * If called outside of a method, it returns <code>nil</code>. + * + */ static VALUE -rb_thread_set_critical(obj, val) - VALUE obj, val; +rb_f_method_name(VALUE _) { - rb_thread_critical = RTEST(val); - return val; -} + ID fname = prev_frame_func(); /* need *method* ID */ -void -rb_thread_interrupt() -{ - rb_thread_critical = 0; - rb_thread_ready(main_thread); - if (curr_thread == main_thread) { - rb_interrupt(); + if (fname) { + return ID2SYM(fname); } - rb_thread_save_context(curr_thread); - if (setjmp(curr_thread->context)) { - return; + else { + return Qnil; } - curr_thread = main_thread; - rb_thread_restore_context(curr_thread, 2); } -void -rb_thread_trap_eval(cmd, sig) - VALUE cmd; - int sig; -{ - rb_thread_critical = 0; - if (!rb_thread_dead(curr_thread)) { - rb_thread_ready(curr_thread); - rb_trap_eval(cmd, sig); - return; - } - rb_thread_ready(main_thread); - rb_thread_save_context(curr_thread); - if (setjmp(curr_thread->context)) { - return; - } - th_cmd = cmd; - th_sig = sig; - curr_thread = main_thread; - rb_thread_restore_context(curr_thread, 3); -} +/* + * call-seq: + * __callee__ -> symbol + * + * Returns the called name of the current method as a Symbol. + * If called outside of a method, it returns <code>nil</code>. + * + */ static VALUE -rb_thread_raise(argc, argv, thread) - int argc; - VALUE *argv; - VALUE thread; +rb_f_callee_name(VALUE _) { - thread_t th = rb_thread_check(thread); + ID fname = prev_frame_callee(); /* need *callee* ID */ - if (rb_thread_dead(th)) return thread; - if (curr_thread == th) { - rb_f_raise(argc, argv); + if (fname) { + return ID2SYM(fname); } - - if (curr_thread->status != THREAD_KILLED) - rb_thread_save_context(curr_thread); - if (setjmp(curr_thread->context)) { - return thread; + else { + return Qnil; } - - rb_scan_args(argc, argv, "11", &th_raise_argv[0], &th_raise_argv[1]); - rb_thread_ready(th); - curr_thread = th; - - th_raise_argc = argc; - th_raise_file = ruby_sourcefile; - th_raise_line = ruby_sourceline; - rb_thread_restore_context(curr_thread, 4); - return Qnil; /* not reached */ } -static thread_t loading_thread; -static int loading_nest; - -static int -rb_thread_loading(feature) - char *feature; +/* + * call-seq: + * __dir__ -> string + * + * Returns the canonicalized absolute path of the directory of the file from + * which this method is called. It means symlinks in the path is resolved. + * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>. + * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>. + * + */ +static VALUE +f_current_dirname(VALUE _) { - if (curr_thread != curr_thread->next && loading_thread) { - while (loading_thread != curr_thread) { - rb_thread_schedule(); - CHECK_INTS; - } - if (rb_provided(feature)) return Qtrue; /* no need to load */ + VALUE base = rb_current_realfilepath(); + if (NIL_P(base)) { + return Qnil; } - - loading_thread = curr_thread; - loading_nest++; - - return Qfalse; + base = rb_file_dirname(base); + return base; } -static void -rb_thread_loading_done() -{ - if (--loading_nest == 0) { - loading_thread = 0; - } -} +/* + * call-seq: + * global_variables -> array + * + * Returns an array of the names of global variables. This includes + * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>, + * but does not include the numbered regexp global variables (<tt>$1</tt>, + * <tt>$2</tt>, etc.). + * + * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr] + */ -VALUE -rb_thread_local_aref(thread, id) - VALUE thread; - ID id; +static VALUE +f_global_variables(VALUE _) { - thread_t th; - VALUE val; - - th = rb_thread_check(thread); - if (!th->locals) return Qnil; - if (st_lookup(th->locals, id, &val)) { - return val; - } - return Qnil; + return rb_f_global_variables(); } +/* + * call-seq: + * trace_var(symbol, cmd ) -> nil + * trace_var(symbol) {|val| block } -> nil + * + * Controls tracing of assignments to global variables. The parameter + * +symbol+ identifies the variable (as either a string name or a + * symbol identifier). _cmd_ (which may be a string or a + * +Proc+ object) or block is executed whenever the variable + * is assigned. The block or +Proc+ object receives the + * variable's new value as a parameter. Also see + * #untrace_var. + * + * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" } + * $_ = "hello" + * $_ = ' there' + * + * <em>produces:</em> + * + * $_ is now 'hello' + * $_ is now ' there' + */ + static VALUE -rb_thread_aref(thread, id) - VALUE thread, id; +f_trace_var(int c, const VALUE *a, VALUE _) { - return rb_thread_local_aref(thread, rb_to_id(id)); + return rb_f_trace_var(c, a); } -VALUE -rb_thread_local_aset(thread, id, val) - VALUE thread; - ID id; - VALUE val; -{ - thread_t th; - - - if (safe_level >= 4 && !FL_TEST(thread, FL_TAINT)) - rb_raise(rb_eSecurityError, "Insecure: can't modify thread values"); - - th = rb_thread_check(thread); - if (!th->locals) { - th->locals = st_init_numtable(); - } - if (NIL_P(val)) { - st_delete(th->locals, &id, 0); - return Qnil; - } - st_insert(th->locals, id, val); - - return val; -} +/* + * call-seq: + * untrace_var(symbol [, cmd] ) -> array or nil + * + * Removes tracing for the specified command on the given global + * variable and returns +nil+. If no command is specified, + * removes all tracing for that variable and returns an array + * containing the commands actually removed. + */ static VALUE -rb_thread_aset(thread, id, val) - VALUE thread, id, val; +f_untrace_var(int c, const VALUE *a, VALUE _) { - return rb_thread_local_aset(thread, rb_to_id(id), val); + return rb_f_untrace_var(c, a); } -static VALUE -rb_thread_key_p(thread, id) - VALUE thread, id; +void +Init_eval(void) { - thread_t th = rb_thread_check(thread); + rb_define_virtual_variable("$@", errat_getter, errat_setter); + rb_define_virtual_variable("$!", errinfo_getter, 0); - if (!th->locals) return Qfalse; - if (st_lookup(th->locals, rb_to_id(id), 0)) - return Qtrue; - return Qfalse; -} + rb_gvar_ractor_local("$@"); + rb_gvar_ractor_local("$!"); -static VALUE rb_cContinuation; + rb_gvar_box_dynamic("$@"); + rb_gvar_box_dynamic("$!"); -static VALUE -rb_callcc(self) - VALUE self; -{ - volatile VALUE cont; - thread_t th; + rb_define_global_function("raise", f_raise, -1); + rb_define_global_function("fail", f_raise, -1); - THREAD_ALLOC(th); - th->thread = cont = Data_Wrap_Struct(rb_cContinuation, thread_mark, - thread_free, th); + rb_define_global_function("global_variables", f_global_variables, 0); - FL_SET(ruby_scope, SCOPE_DONT_RECYCLE); - rb_thread_save_context(th); - if (setjmp(th->context)) { - return th->result; - } - else { - return rb_yield(th->thread); - } -} + rb_define_global_function("__method__", rb_f_method_name, 0); + rb_define_global_function("__callee__", rb_f_callee_name, 0); + rb_define_global_function("__dir__", f_current_dirname, 0); -static VALUE -rb_continuation_call(argc, argv, cont) - int argc; - VALUE *argv; - VALUE cont; -{ - thread_t th = rb_thread_check(cont); + rb_define_method(rb_cModule, "include", rb_mod_include, -1); + rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1); - switch (argc) { - case 0: - th->result = Qnil; - break; - case 1: - th->result = *argv; - break; - default: - th->result = rb_ary_new4(argc, argv); - break; - } - rb_thread_restore_context(th, 0); - return Qnil; -} + rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1); + rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1); + rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1); + rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1); + rb_define_private_method(rb_cModule, "using", mod_using, 1); + rb_define_method(rb_cModule, "refinements", mod_refinements, 0); + rb_define_singleton_method(rb_cModule, "used_modules", + rb_mod_s_used_modules, 0); + rb_define_singleton_method(rb_cModule, "used_refinements", + rb_mod_s_used_refinements, 0); + rb_undef_method(rb_cClass, "refine"); + rb_define_private_method(rb_cRefinement, "import_methods", refinement_import_methods, -1); + rb_define_method(rb_cRefinement, "target", rb_refinement_module_get_refined_class, 0); + rb_undef_method(rb_cRefinement, "append_features"); + rb_undef_method(rb_cRefinement, "prepend_features"); + rb_undef_method(rb_cRefinement, "extend_object"); + + rb_undef_method(rb_cClass, "module_function"); + + Init_vm_eval(); + Init_eval_method(); -void -Init_Thread() -{ - rb_eThreadError = rb_define_class("ThreadError", rb_eStandardError); - rb_cThread = rb_define_class("Thread", rb_cObject); - - rb_define_singleton_method(rb_cThread, "new", rb_thread_start, 0); - rb_define_singleton_method(rb_cThread, "start", rb_thread_start, 0); - rb_define_singleton_method(rb_cThread, "fork", rb_thread_start, 0); - - rb_define_singleton_method(rb_cThread, "stop", rb_thread_stop, 0); - rb_define_singleton_method(rb_cThread, "kill", rb_thread_s_kill, 1); - rb_define_singleton_method(rb_cThread, "exit", rb_thread_exit, 0); - rb_define_singleton_method(rb_cThread, "pass", rb_thread_pass, 0); - rb_define_singleton_method(rb_cThread, "join", rb_thread_s_join, 1); - rb_define_singleton_method(rb_cThread, "current", rb_thread_current, 0); - rb_define_singleton_method(rb_cThread, "main", rb_thread_main, 0); - - rb_define_singleton_method(rb_cThread, "critical", rb_thread_get_critical, 0); - rb_define_singleton_method(rb_cThread, "critical=", rb_thread_set_critical, 1); - - rb_define_singleton_method(rb_cThread, "abort_on_exception", rb_thread_s_abort_exc, 0); - rb_define_singleton_method(rb_cThread, "abort_on_exception=", rb_thread_s_abort_exc_set, 1); - - rb_define_method(rb_cThread, "run", rb_thread_run, 0); - rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0); - rb_define_method(rb_cThread, "exit", rb_thread_kill, 0); - rb_define_method(rb_cThread, "value", rb_thread_value, 0); - rb_define_method(rb_cThread, "status", rb_thread_status, 0); - rb_define_method(rb_cThread, "join", rb_thread_join, 0); - rb_define_method(rb_cThread, "alive?", rb_thread_status, 0); - rb_define_method(rb_cThread, "stop?", rb_thread_stop_p, 0); - rb_define_method(rb_cThread, "raise", rb_thread_raise, -1); - - rb_define_method(rb_cThread, "abort_on_exception", rb_thread_abort_exc, 0); - rb_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1); - - rb_define_method(rb_cThread, "[]", rb_thread_aref, 1); - rb_define_method(rb_cThread, "[]=", rb_thread_aset, 2); - rb_define_method(rb_cThread, "key?", rb_thread_key_p, 1); - - /* allocate main thread */ - main_thread = rb_thread_alloc(rb_cThread); - - rb_cContinuation = rb_define_class("Continuation", rb_cObject); - rb_undef_method(CLASS_OF(rb_cContinuation), "new"); - rb_define_method(rb_cContinuation, "call", rb_continuation_call, -1); - rb_define_method(rb_mKernel, "callcc", rb_callcc, 0); -} -#endif + rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0); + rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1); -static VALUE -rb_f_catch(dmy, tag) - VALUE dmy, tag; -{ - int state; - ID t; - VALUE val; /* OK */ + rb_define_private_method(rb_singleton_class(rb_vm_top_self()), + "include", top_include, -1); + rb_define_private_method(rb_singleton_class(rb_vm_top_self()), + "using", top_using, 1); - t = rb_to_id(tag); - PUSH_TAG(t); - if ((state = EXEC_TAG()) == 0) { - val = rb_yield_0(tag, 0, 0); - } - else if (state == TAG_THROW && t == prot_tag->dst) { - val = prot_tag->retval; - state = 0; - } - POP_TAG(); - if (state) JUMP_TAG(state); + rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1); - return val; -} + rb_define_global_function("trace_var", f_trace_var, -1); + rb_define_global_function("untrace_var", f_untrace_var, -1); -static VALUE -catch_i(tag) - ID tag; -{ - return rb_f_catch(0, FIX2INT(tag)); -} + rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered"); + rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region"); -VALUE -rb_catch(tag, proc, data) - char *tag; - VALUE (*proc)(); - VALUE data; -{ - return rb_iterate(catch_i, rb_intern(tag), proc, data); + id_signo = rb_intern_const("signo"); + id_status = rb_intern_const("status"); } -static VALUE -rb_f_throw(argc, argv) - int argc; - VALUE *argv; +int +rb_errno(void) { - VALUE tag, value; - ID t; - struct tag *tt = prot_tag; - - rb_scan_args(argc, argv, "11", &tag, &value); - t = rb_to_id(tag); - - while (tt) { - if (tt->tag == t) { - tt->dst = t; - break; - } -#ifdef USE_THREAD - if (tt->tag == PROT_THREAD) { - rb_raise(rb_eThreadError, "uncaught throw `%s' in thread 0x%x", - rb_id2name(t), - curr_thread); - } -#endif - tt = tt->prev; - } - if (!tt) { - rb_raise(rb_eNameError, "uncaught throw `%s'", rb_id2name(t)); - } - return_value(value); - rb_trap_restore_mask(); - JUMP_TAG(TAG_THROW); - /* not reached */ + return *rb_orig_errno_ptr(); } void -rb_throw(tag, val) - char *tag; - VALUE val; +rb_errno_set(int e) { - VALUE argv[2]; - ID t = rb_intern(tag); - - argv[0] = FIX2INT(t); - argv[1] = val; - rb_f_throw(2, argv); + *rb_orig_errno_ptr() = e; } -static void -return_check() +int * +rb_errno_ptr(void) { -#ifdef USE_THREAD - struct tag *tt = prot_tag; - - while (tt) { - if (tt->tag == PROT_FUNC) { - break; - } - if (tt->tag == PROT_THREAD) { - rb_raise(rb_eThreadError, "return from within thread 0x%x", - curr_thread); - } - tt = tt->prev; - } -#endif + return rb_orig_errno_ptr(); } - |
