/********************************************************************** eval.c - $Author: matz $ $Date: 2006/08/07 03:43:39 $ created at: Thu Jun 10 14:22:17 JST 1993 Copyright (C) 1993-2003 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 "util.h" #include "rubysig.h" #ifdef HAVE_STDLIB_H #include #endif #ifndef EXIT_SUCCESS #define EXIT_SUCCESS 0 #endif #ifndef EXIT_FAILURE #define EXIT_FAILURE 1 #endif #include #if defined(HAVE_GETCONTEXT) && defined(HAVE_SETCONTEXT) #include #define USE_CONTEXT #endif #include #include "st.h" #include "dln.h" #ifdef __APPLE__ #include #endif /* Make alloca work the best possible way. */ #ifdef __GNUC__ # ifndef atarist # ifndef alloca # define alloca __builtin_alloca # endif # endif /* atarist */ #else # ifdef HAVE_ALLOCA_H # include # else # ifndef _AIX # ifndef alloca /* predefined by HP cc +Olibcalls */ void *alloca (); # endif # endif /* AIX */ # endif /* HAVE_ALLOCA_H */ #endif /* __GNUC__ */ #ifdef HAVE_STDARG_PROTOTYPES #include #define va_init_list(a,b) va_start(a,b) #else #include #define va_init_list(a,b) va_start(a) #endif #ifndef HAVE_STRING_H char *strrchr _((const char*,const char)); #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef __BEOS__ #include #endif #ifdef __MACOS__ #include "macruby_private.h" #endif #ifdef __VMS #include "vmsruby_private.h" #endif #ifdef USE_CONTEXT typedef struct { ucontext_t context; volatile int status; } rb_jmpbuf_t[1]; NORETURN(static void rb_jump_context(rb_jmpbuf_t, int)); static inline void rb_jump_context(env, val) rb_jmpbuf_t env; int val; { env->status = val; setcontext(&env->context); abort(); /* ensure noreturn */ } /* * PRE_GETCONTEXT and POST_GETCONTEXT is a magic for getcontext, gcc, * IA64 register stack and SPARC register window combination problem. * * Assume following code sequence. * * 1. set a register in the register stack/window such as r32/l0. * 2. call getcontext. * 3. use the register. * 4. update the register for other use. * 5. call setcontext indirectly (or directly). * * This code should be run as 1->2->3->4->5->3->4. * But after second getcontext return (second 3), * the register is broken (updated). * It's because getcontext/setcontext doesn't preserve the content of the * register stack/window. * * setjmp also doesn't preserve the content of the register stack/window. * But it has not the problem because gcc knows setjmp may return twice. * gcc detects setjmp and generates setjmp safe code. * * So setjmp calls before and after the getcontext call makes the code * somewhat safe. * It fix the problem on IA64. * It is not required that setjmp is called at run time, since the problem is * register usage. * * Since the magic setjmp is not enough for SPARC, * inline asm is used to prohibit registers in register windows. * * Since the problem is fixed at gcc 4.0.3, the magic is applied only for * prior versions of gcc. * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21957 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22127 */ # define GCC_VERSION_BEFORE(major, minor, patchlevel) \ (defined(__GNUC__) && !defined(__INTEL_COMPILER) && \ ((__GNUC__ < (major)) || \ (__GNUC__ == (major) && __GNUC_MINOR__ < (minor)) || \ (__GNUC__ == (major) && __GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ < (patchlevel)))) # if GCC_VERSION_BEFORE(4,0,3) && (defined(sparc) || defined(__sparc__)) # ifdef __pic__ /* * %l7 is excluded for PIC because it is PIC register. * http://lists.freebsd.org/pipermail/freebsd-sparc64/2006-January/003739.html */ # define PRE_GETCONTEXT \ ({ __asm__ volatile ("" : : : \ "%o0", "%o1", "%o2", "%o3", "%o4", "%o5", "%o7", \ "%l0", "%l1", "%l2", "%l3", "%l4", "%l5", "%l6", \ "%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i7"); }) # else # define PRE_GETCONTEXT \ ({ __asm__ volatile ("" : : : \ "%o0", "%o1", "%o2", "%o3", "%o4", "%o5", "%o7", \ "%l0", "%l1", "%l2", "%l3", "%l4", "%l5", "%l6", "%l7", \ "%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i7"); }) # endif # define POST_GETCONTEXT PRE_GETCONTEXT # elif GCC_VERSION_BEFORE(4,0,3) && defined(__ia64) static jmp_buf function_call_may_return_twice_jmp_buf; int function_call_may_return_twice_false_1 = 0; int function_call_may_return_twice_false_2 = 0; # define PRE_GETCONTEXT \ (function_call_may_return_twice_false_1 ? \ setjmp(function_call_may_return_twice_jmp_buf) : \ 0) # define POST_GETCONTEXT \ (function_call_may_return_twice_false_2 ? \ setjmp(function_call_may_return_twice_jmp_buf) : \ 0) # elif defined(__FreeBSD__) && __FreeBSD__ < 7 /* * workaround for FreeBSD/i386 getcontext/setcontext bug. * clear the carry flag by (0 ? ... : ...). * FreeBSD PR 92110 http://www.freebsd.org/cgi/query-pr.cgi?pr=92110 * [ruby-dev:28263] */ static int volatile freebsd_clear_carry_flag = 0; # define PRE_GETCONTEXT \ (freebsd_clear_carry_flag ? (freebsd_clear_carry_flag = 0) : 0) # endif # ifndef PRE_GETCONTEXT # define PRE_GETCONTEXT 0 # endif # ifndef POST_GETCONTEXT # define POST_GETCONTEXT 0 # endif # define ruby_longjmp(env, val) rb_jump_context(env, val) # define ruby_setjmp(j) ((j)->status = 0, \ PRE_GETCONTEXT, \ getcontext(&(j)->context), \ POST_GETCONTEXT, \ (j)->status) #else typedef jmp_buf rb_jmpbuf_t; # if !defined(setjmp) && defined(HAVE__SETJMP) # define ruby_setjmp(env) _setjmp(env) # define ruby_longjmp(env,val) _longjmp(env,val) # else # define ruby_setjmp(env) setjmp(env) # define ruby_longjmp(env,val) longjmp(env,val) # endif #endif #include #include #include #if defined(__VMS) #pragma nostandard #endif #ifdef HAVE_SYS_SELECT_H #include #endif #include #ifdef ENABLE_DTRACE #include "dtrace.h" #endif VALUE rb_cProc; VALUE rb_cBinding; static VALUE proc_invoke _((VALUE,VALUE,VALUE,VALUE)); static VALUE rb_f_binding _((VALUE)); static void rb_f_END _((void)); static VALUE rb_f_block_given_p _((void)); static VALUE block_pass _((VALUE,NODE*)); VALUE rb_cMethod; static VALUE method_call _((int, VALUE*, VALUE)); VALUE rb_cUnboundMethod; static VALUE umethod_bind _((VALUE, VALUE)); static VALUE rb_mod_define_method _((int, VALUE*, VALUE)); NORETURN(static void rb_raise_jump _((VALUE))); static VALUE rb_make_exception _((int argc, VALUE *argv)); 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) (scope_vmode=(f)) #define SCOPE_TEST(f) (scope_vmode&(f)) NODE* ruby_current_node; int ruby_safe_level = 0; /* safe-level: 0 - strings from streams/environment/ARGV are tainted (default) 1 - no dangerous operation by tainted value 2 - process/file operations prohibited 3 - all generated objects are tainted 4 - no global (non-tainted) variable modification/no direct output */ static VALUE safe_getter _((void)); static void safe_setter _((VALUE val)); void rb_secure(level) int level; { if (level <= ruby_safe_level) { if (ruby_frame->last_func) { rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d", rb_id2name(ruby_frame->last_func), ruby_safe_level); } else { rb_raise(rb_eSecurityError, "Insecure operation at level %d", ruby_safe_level); } } } void rb_secure_update(obj) VALUE obj; { if (!OBJ_TAINTED(obj)) rb_secure(4); } void rb_check_safe_obj(x) VALUE x; { if (ruby_safe_level > 0 && OBJ_TAINTED(x)){ if (ruby_frame->last_func) { rb_raise(rb_eSecurityError, "Insecure operation - %s", rb_id2name(ruby_frame->last_func)); } else { rb_raise(rb_eSecurityError, "Insecure operation: -r"); } } rb_secure(4); } void rb_check_safe_str(x) VALUE x; { rb_check_safe_obj(x); if (TYPE(x)!= T_STRING) { rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", rb_obj_classname(x)); } } NORETURN(static void print_undef _((VALUE, ID))); static void print_undef(klass, id) VALUE klass; ID id; { rb_name_error(id, "undefined method `%s' for %s `%s'", rb_id2name(id), (TYPE(klass) == T_MODULE) ? "module" : "class", rb_class2name(klass)); } static ID removed, singleton_removed, undefined, singleton_undefined; #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]; static int ruby_running = 0; void rb_clear_cache() { struct cache_entry *ent, *end; if (!ruby_running) return; ent = cache; end = ent + CACHE_SIZE; while (ent < end) { ent->mid = 0; ent++; } } static void rb_clear_cache_for_undef(klass, id) VALUE klass; ID id; { struct cache_entry *ent, *end; if (!ruby_running) return; ent = cache; end = ent + CACHE_SIZE; while (ent < end) { if (ent->mid == id && RCLASS(ent->origin)->m_tbl == RCLASS(klass)->m_tbl) { ent->mid = 0; } ent++; } } static void rb_clear_cache_by_id(id) ID id; { struct cache_entry *ent, *end; if (!ruby_running) return; ent = cache; end = ent + CACHE_SIZE; while (ent < end) { if (ent->mid == id) { ent->mid = 0; } ent++; } } void rb_clear_cache_by_class(klass) VALUE klass; { struct cache_entry *ent, *end; if (!ruby_running) return; ent = cache; end = ent + CACHE_SIZE; while (ent < end) { if (ent->klass == klass || ent->origin == klass) { ent->mid = 0; } ent++; } } static ID init, eqq, each, aref, aset, match, missing; static ID added, singleton_added; static ID __id__, __send__, respond_to; #define NOEX_TAINTED 8 #define NOEX_SAFE(n) ((n) >> 4) #define NOEX_WITH(n, v) ((n) | (v) << 4) #define NOEX_WITH_SAFE(n) NOEX_WITH(n, ruby_safe_level) 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 (ruby_safe_level >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) { rb_raise(rb_eSecurityError, "Insecure: can't define method"); } if (!FL_TEST(klass, FL_SINGLETON) && node && nd_type(node) != NODE_ZSUPER && (mid == rb_intern("initialize" )|| mid == rb_intern("initialize_copy"))) { noex = NOEX_PRIVATE | noex; } else if (FL_TEST(klass, FL_SINGLETON) && node && nd_type(node) == NODE_CFUNC && mid == rb_intern("allocate")) { rb_warn("defining %s.allocate is deprecated; use rb_define_alloc_func()", rb_class2name(rb_iv_get(klass, "__attached__"))); mid = ID_ALLOCATOR; } if (OBJ_FROZEN(klass)) rb_error_frozen("class/module"); rb_clear_cache_by_id(mid); body = NEW_METHOD(node, NOEX_WITH_SAFE(noex)); st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t)body); if (node && mid != ID_ALLOCATOR && ruby_running) { if (FL_TEST(klass, FL_SINGLETON)) { rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1, ID2SYM(mid)); } else { rb_funcall(klass, added, 1, ID2SYM(mid)); } } } void rb_define_alloc_func(klass, func) VALUE klass; VALUE (*func) _((VALUE)); { Check_Type(klass, T_CLASS); rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0), NOEX_PRIVATE); } void rb_undef_alloc_func(klass) VALUE klass; { Check_Type(klass, T_CLASS); rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, 0, NOEX_UNDEF); } static NODE* search_method(klass, id, origin) VALUE klass, *origin; ID id; { NODE *body; if (!klass) return 0; while (!st_lookup(RCLASS(klass)->m_tbl, id, (st_data_t *)&body)) { klass = RCLASS(klass)->super; if (!klass) return 0; } if (origin) *origin = klass; return body; } 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 || !body->nd_body) { /* store empty info in cache */ ent = cache + EXPR1(klass, id); ent->klass = klass; ent->origin = klass; ent->mid = ent->mid0 = id; ent->noex = 0; ent->method = 0; return 0; } if (ruby_running) { /* store in cache */ ent = cache + EXPR1(klass, id); ent->klass = klass; ent->noex = body->nd_noex; if (noexp) *noexp = 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; } } else { if (noexp) *noexp = body->nd_noex; body = body->nd_body; if (nd_type(body) == NODE_FBODY) { *klassp = body->nd_orig; *idp = body->nd_mid; body = body->nd_head; } else { *klassp = origin; } } return body; } NODE* rb_method_node(klass, id) VALUE klass; ID id; { int noex; return rb_get_method_body(&klass, &id, &noex); } static void remove_method(klass, mid) VALUE klass; ID mid; { NODE *body; if (klass == rb_cObject) { rb_secure(4); } if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) { rb_raise(rb_eSecurityError, "Insecure: can't remove method"); } if (OBJ_FROZEN(klass)) rb_error_frozen("class/module"); if (mid == __id__ || mid == __send__ || mid == init) { rb_warn("removing `%s' may cause serious problem", rb_id2name(mid)); } if (!st_delete(RCLASS(klass)->m_tbl, &mid, (st_data_t *)&body) || !body->nd_body) { rb_name_error(mid, "method `%s' not defined in %s", rb_id2name(mid), rb_class2name(klass)); } rb_clear_cache_for_undef(klass, mid); if (FL_TEST(klass, FL_SINGLETON)) { rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1, ID2SYM(mid)); } else { rb_funcall(klass, removed, 1, ID2SYM(mid)); } } void rb_remove_method(klass, name) VALUE klass; const char *name; { remove_method(klass, rb_intern(name)); } /* * call-seq: * remove_method(symbol) => self * * Removes the method identified by _symbol_ from the current * class. For an example, see Module.undef_method. */ static VALUE rb_mod_remove_method(argc, argv, mod) int argc; VALUE *argv; VALUE mod; { int i; for (i=0; ind_body) { print_undef(klass, name); } if (body->nd_noex != noex) { if (klass == origin) { body->nd_noex = noex; } else { rb_add_method(klass, name, NEW_ZSUPER(), noex); } } } int rb_method_boundp(klass, id, ex) VALUE klass; ID id; int ex; { struct cache_entry *ent; int noex; /* is it in the method cache? */ ent = cache + EXPR1(klass, id); if (ent->mid == id && ent->klass == klass) { if (ex && (ent->noex & NOEX_PRIVATE)) return Qfalse; if (!ent->method) return Qfalse; return Qtrue; } if (rb_get_method_body(&klass, &id, &noex)) { if (ex && (noex & NOEX_PRIVATE)) return Qfalse; return Qtrue; } return Qfalse; } void rb_attr(klass, id, read, write, ex) VALUE klass; ID id; int read, write, ex; { const char *name; char *buf; ID attriv; int noex; size_t len; if (!ex) noex = NOEX_PUBLIC; else { if (SCOPE_TEST(SCOPE_PRIVATE)) { noex = NOEX_PRIVATE; rb_warning((scope_vmode == SCOPE_MODFUNC) ? "attribute accessor as module_function" : "private attribute?"); } else if (SCOPE_TEST(SCOPE_PROTECTED)) { noex = NOEX_PROTECTED; } else { noex = NOEX_PUBLIC; } } if (!rb_is_local_id(id) && !rb_is_const_id(id)) { rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id)); } name = rb_id2name(id); if (!name) { rb_raise(rb_eArgError, "argument needs to be symbol or string"); } len = strlen(name)+2; buf = ALLOCA_N(char,len); snprintf(buf, len, "@%s", name); attriv = rb_intern(buf); if (read) { rb_add_method(klass, id, NEW_IVAR(attriv), noex); } if (write) { rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex); } } extern int ruby_in_compile; VALUE ruby_errinfo = Qnil; extern NODE *ruby_eval_tree_begin; extern NODE *ruby_eval_tree; extern int ruby_nerrs; VALUE rb_eLocalJumpError; 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; static unsigned long frame_unique = 0; #define PUSH_FRAME() do { \ struct FRAME _frame; \ _frame.prev = ruby_frame; \ _frame.tmp = 0; \ _frame.node = ruby_current_node; \ _frame.iter = ruby_iter->iter; \ _frame.argc = 0; \ _frame.flags = 0; \ _frame.uniq = frame_unique++; \ ruby_frame = &_frame #define POP_FRAME() \ ruby_current_node = _frame.node; \ ruby_frame = _frame.prev; \ } while (0) struct BLOCK { NODE *var; NODE *body; VALUE self; struct FRAME frame; struct SCOPE *scope; VALUE klass; NODE *cref; int iter; int vmode; int flags; int uniq; struct RVarmap *dyna_vars; VALUE orig_thread; VALUE wrapper; VALUE block_obj; struct BLOCK *outer; struct BLOCK *prev; }; #define BLOCK_D_SCOPE 1 #define BLOCK_LAMBDA 2 static struct BLOCK *ruby_block; static unsigned long block_unique = 1; #define PUSH_BLOCK(v,b) do { \ struct BLOCK _block; \ _block.var = (v); \ _block.body = (b); \ _block.self = self; \ _block.frame = *ruby_frame; \ _block.klass = ruby_class; \ _block.cref = ruby_cref; \ _block.frame.node = ruby_current_node;\ _block.scope = ruby_scope; \ _block.prev = ruby_block; \ _block.outer = ruby_block; \ _block.iter = ruby_iter->iter; \ _block.vmode = scope_vmode; \ _block.flags = BLOCK_D_SCOPE; \ _block.dyna_vars = ruby_dyna_vars; \ _block.wrapper = ruby_wrapper; \ _block.block_obj = 0; \ _block.uniq = (b)?block_unique++:0; \ if (b) { \ prot_tag->blkid = _block.uniq; \ } \ ruby_block = &_block #define POP_BLOCK() \ ruby_block = _block.prev; \ } while (0) struct RVarmap *ruby_dyna_vars; #define PUSH_VARS() do { \ struct RVarmap * volatile _old; \ _old = ruby_dyna_vars; \ ruby_dyna_vars = 0 #define POP_VARS() \ if (_old && (ruby_scope->flags & SCOPE_DONT_RECYCLE)) {\ if (RBASIC(_old)->flags) /* unless it's already recycled */ \ FL_SET(_old, DVAR_DONT_RECYCLE); \ }\ ruby_dyna_vars = _old; \ } while (0) #define DVAR_DONT_RECYCLE FL_USER2 #define DMETHOD_P() (ruby_frame->flags & FRAME_DMETH) static struct RVarmap* new_dvar(id, value, prev) ID id; VALUE value; struct RVarmap *prev; { NEWOBJ(vars, struct RVarmap); OBJSETUP(vars, 0, T_VARMAP); vars->id = id; vars->val = value; vars->next = prev; 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_curr(id) ID id; { struct RVarmap *vars = ruby_dyna_vars; while (vars) { if (vars->id == 0) break; 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, ruby_dyna_vars); } static void dvar_asgn_internal(id, value, curr) ID id; VALUE value; int curr; { int n = 0; struct RVarmap *vars = ruby_dyna_vars; while (vars) { if (curr && vars->id == 0) { /* first null is a dvar header */ n++; if (n == 2) break; } if (vars->id == id) { vars->val = value; return; } vars = vars->next; } if (!ruby_dyna_vars) { ruby_dyna_vars = new_dvar(id, value, 0); } else { vars = new_dvar(id, value, ruby_dyna_vars->next); ruby_dyna_vars->next = vars; } } static inline void dvar_asgn(id, value) ID id; VALUE value; { dvar_asgn_internal(id, value, 0); } static inline void dvar_asgn_curr(id, value) ID id; VALUE value; { dvar_asgn_internal(id, value, 1); } VALUE * rb_svar(cnt) int cnt; { struct RVarmap *vars = ruby_dyna_vars; ID id; if (!ruby_scope->local_tbl) return NULL; if (cnt >= ruby_scope->local_tbl[0]) return NULL; id = ruby_scope->local_tbl[cnt+1]; while (vars) { if (vars->id == id) return &vars->val; vars = vars->next; } if (ruby_scope->local_vars == 0) return NULL; return &ruby_scope->local_vars[cnt]; } 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 ITER_PAS 3 #define PUSH_ITER(i) do { \ struct iter _iter; \ _iter.prev = ruby_iter; \ _iter.iter = (i); \ ruby_iter = &_iter #define POP_ITER() \ ruby_iter = _iter.prev; \ } while (0) struct tag { rb_jmpbuf_t buf; struct FRAME *frame; struct iter *iter; VALUE tag; VALUE retval; struct SCOPE *scope; VALUE dst; struct tag *prev; int blkid; }; static struct tag *prot_tag; #define PUSH_TAG(ptag) do { \ struct tag _tag; \ _tag.retval = Qnil; \ _tag.frame = ruby_frame; \ _tag.iter = ruby_iter; \ _tag.prev = prot_tag; \ _tag.scope = ruby_scope; \ _tag.tag = ptag; \ _tag.dst = 0; \ _tag.blkid = 0; \ prot_tag = &_tag #define PROT_NONE Qfalse /* 0 */ #define PROT_THREAD Qtrue /* 2 */ #define PROT_FUNC INT2FIX(0) /* 1 */ #define PROT_LOOP INT2FIX(1) /* 3 */ #define PROT_LAMBDA INT2FIX(2) /* 5 */ #define PROT_YIELD INT2FIX(3) /* 7 */ #define EXEC_TAG() (FLUSH_REGISTER_WINDOWS, ruby_setjmp(prot_tag->buf)) #define JUMP_TAG(st) do { \ ruby_frame = prot_tag->frame; \ ruby_iter = prot_tag->iter; \ ruby_longjmp(prot_tag->buf,(st)); \ } while (0) #define POP_TAG() \ prot_tag = _tag.prev; \ } while (0) #define TAG_DST() (_tag.dst == (VALUE)ruby_frame->uniq) #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(c) do { \ VALUE _class = ruby_class; \ ruby_class = (c) #define POP_CLASS() ruby_class = _class; \ } while (0) static NODE *ruby_cref = 0; static NODE *top_cref; #define PUSH_CREF(c) ruby_cref = NEW_NODE(NODE_CREF,(c),0,ruby_cref) #define POP_CREF() ruby_cref = ruby_cref->nd_next #define PUSH_SCOPE() do { \ 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->flags = 0; \ _old = ruby_scope; \ ruby_scope = _scope; \ scope_vmode = SCOPE_PUBLIC typedef struct thread * rb_thread_t; static rb_thread_t curr_thread = 0; static rb_thread_t main_thread; static void scope_dup _((struct SCOPE *)); #define POP_SCOPE() \ if (ruby_scope->flags & SCOPE_DONT_RECYCLE) {\ if (_old) scope_dup(_old); \ } \ if (!(ruby_scope->flags & SCOPE_MALLOC)) {\ ruby_scope->local_vars = 0; \ ruby_scope->local_tbl = 0; \ if (!(ruby_scope->flags & SCOPE_DONT_RECYCLE) && \ ruby_scope != top_scope) { \ rb_gc_force_recycle((VALUE)ruby_scope);\ } \ } \ ruby_scope->flags |= SCOPE_NOSTACK; \ ruby_scope = _old; \ scope_vmode = _vmode; \ } while (0) static VALUE rb_eval _((VALUE,NODE*)); static VALUE eval _((VALUE,VALUE,VALUE,char*,int)); static NODE *compile _((VALUE, char*, int)); static VALUE rb_yield_0 _((VALUE, VALUE, VALUE, int, int)); #define YIELD_LAMBDA_CALL 1 #define YIELD_PROC_CALL 2 #define YIELD_PUBLIC_DEF 4 #define YIELD_FUNC_AVALUE 1 #define YIELD_FUNC_SVALUE 2 static VALUE rb_call _((VALUE,VALUE,ID,int,const VALUE*,int)); static VALUE module_setup _((VALUE,NODE*)); static VALUE massign _((VALUE,NODE*,VALUE,int)); static void assign _((VALUE,NODE*,VALUE,int)); typedef struct event_hook { rb_event_hook_func_t func; rb_event_t events; struct event_hook *next; } rb_event_hook_t; static rb_event_hook_t *event_hooks; #define EXEC_EVENT_HOOK(event, node, self, id, klass) \ do { \ rb_event_hook_t *hook = event_hooks; \ rb_event_hook_func_t hook_func; \ rb_event_t events; \ \ while (hook) { \ hook_func = hook->func; \ events = hook->events; \ hook = hook->next; \ if (events & event) \ (*hook_func)(event, node, self, id, klass); \ } \ } while (0) static VALUE trace_func = 0; static int tracing = 0; static void call_trace_func _((rb_event_t,NODE*,VALUE,ID,VALUE)); #if 0 #define SET_CURRENT_SOURCE() (ruby_sourcefile = ruby_current_node->nd_file, \ ruby_sourceline = nd_line(ruby_current_node)) #else #define SET_CURRENT_SOURCE() ((void)0) #endif void ruby_set_current_source() { if (ruby_current_node) { ruby_sourcefile = ruby_current_node->nd_file; ruby_sourceline = nd_line(ruby_current_node); } } static void #ifdef HAVE_STDARG_PROTOTYPES warn_printf(const char *fmt, ...) #else warn_printf(fmt, va_alist) const char *fmt; va_dcl #endif { char buf[BUFSIZ]; va_list args; va_init_list(args, fmt); vsnprintf(buf, BUFSIZ, fmt, args); va_end(args); rb_write_error(buf); } #define warn_print(x) rb_write_error(x) #define warn_print2(x,l) rb_write_error2(x,l) static void error_pos() { ruby_set_current_source(); if (ruby_sourcefile) { if (ruby_frame->last_func) { warn_printf("%s:%d:in `%s'", ruby_sourcefile, ruby_sourceline, rb_id2name(ruby_frame->orig_func)); } else if (ruby_sourceline == 0) { warn_printf("%s", ruby_sourcefile); } else { warn_printf("%s:%d", ruby_sourcefile, ruby_sourceline); } } } static VALUE get_backtrace(info) VALUE info; { if (NIL_P(info)) return Qnil; info = rb_funcall(info, rb_intern("backtrace"), 0); if (NIL_P(info)) return Qnil; return rb_check_array_type(info); } static void set_backtrace(info, bt) VALUE info, bt; { rb_funcall(info, rb_intern("set_backtrace"), 1, bt); } static void error_print() { VALUE errat = Qnil; /* OK */ volatile VALUE eclass, e; char *einfo; long elen; if (NIL_P(ruby_errinfo)) return; PUSH_TAG(PROT_NONE); if (EXEC_TAG() == 0) { errat = get_backtrace(ruby_errinfo); } else { errat = Qnil; } if (EXEC_TAG()) goto error; if (NIL_P(errat)){ ruby_set_current_source(); if (ruby_sourcefile) warn_printf("%s:%d", ruby_sourcefile, ruby_sourceline); else warn_printf("%d", ruby_sourceline); } else if (RARRAY(errat)->len == 0) { error_pos(); } else { VALUE mesg = RARRAY(errat)->ptr[0]; if (NIL_P(mesg)) error_pos(); else { warn_print2(RSTRING(mesg)->ptr, RSTRING(mesg)->len); } } eclass = CLASS_OF(ruby_errinfo); if (EXEC_TAG() == 0) { e = rb_funcall(ruby_errinfo, rb_intern("message"), 0, 0); StringValue(e); einfo = RSTRING(e)->ptr; elen = RSTRING(e)->len; } else { einfo = ""; elen = 0; } if (EXEC_TAG()) goto error; if (eclass == rb_eRuntimeError && elen == 0) { warn_print(": unhandled exception\n"); } else { VALUE epath; epath = rb_class_name(eclass); if (elen == 0) { warn_print(": "); warn_print2(RSTRING(epath)->ptr, RSTRING(epath)->len); warn_print("\n"); } else { char *tail = 0; long len = elen; if (RSTRING(epath)->ptr[0] == '#') epath = 0; if (tail = memchr(einfo, '\n', elen)) { len = tail - einfo; tail++; /* skip newline */ } warn_print(": "); warn_print2(einfo, len); if (epath) { warn_print(" ("); warn_print2(RSTRING(epath)->ptr, RSTRING(epath)->len); warn_print(")\n"); } if (tail) { warn_print2(tail, elen-len-1); } } } if (!NIL_P(errat)) { long 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; ilen; i++) { if (TYPE(ep->ptr[i]) == T_STRING) { warn_printf("\tfrom %s\n", RSTRING(ep->ptr[i])->ptr); } if (i == TRACE_HEAD && ep->len > TRACE_MAX) { warn_printf("\t ... %ld levels...\n", ep->len - TRACE_HEAD - TRACE_TAIL); i = ep->len - TRACE_TAIL; } } } error: POP_TAG(); } #if defined(__APPLE__) #define environ (*_NSGetEnviron()) #elif !defined(_WIN32) && !defined(__MACOS__) || defined(_WIN32_WCE) extern char **environ; #endif char **rb_origenviron; void rb_call_inits _((void)); void Init_stack _((VALUE*)); void Init_heap _((void)); void Init_ext _((void)); #ifdef HAVE_NATIVETHREAD static rb_nativethread_t ruby_thid; int is_ruby_native_thread() { return NATIVETHREAD_EQUAL(ruby_thid, NATIVETHREAD_CURRENT()); } # ifdef HAVE_NATIVETHREAD_KILL void ruby_native_thread_kill(sig) int sig; { NATIVETHREAD_KILL(ruby_thid, sig); } # endif #endif void ruby_init() { static int initialized = 0; static struct FRAME frame; static struct iter iter; int state; if (initialized) return; initialized = 1; #ifdef HAVE_NATIVETHREAD ruby_thid = NATIVETHREAD_CURRENT(); #endif ruby_frame = top_frame = &frame; ruby_iter = &iter; #ifdef __MACOS__ rb_origenviron = 0; #else rb_origenviron = environ; #endif Init_stack((void*)&state); 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; top_cref = rb_node_newnode(NODE_CREF,rb_cObject,0,0); ruby_cref = top_cref; rb_define_global_const("TOPLEVEL_BINDING", rb_f_binding(ruby_top_self)); #ifdef __MACOS__ _macruby_init(); #elif defined(__VMS) _vmsruby_init(); #endif ruby_prog_init(); ALLOW_INTS; } POP_TAG(); if (state) { error_print(); exit(EXIT_FAILURE); } POP_SCOPE(); ruby_scope = top_scope; top_scope->flags &= ~SCOPE_NOSTACK; ruby_running = 1; } static VALUE eval_node(self, node) VALUE self; NODE *node; { NODE *beg_tree = ruby_eval_tree_begin; ruby_eval_tree_begin = 0; if (beg_tree) { rb_eval(self, beg_tree); } if (!node) return Qnil; return rb_eval(self, node); } int ruby_in_eval; static void rb_thread_cleanup _((void)); static void rb_thread_wait_other_threads _((void)); static int thread_set_raised(); static int thread_reset_raised(); static VALUE exception_error; static VALUE sysstack_error; static int sysexit_status(err) VALUE err; { VALUE st = rb_iv_get(err, "status"); return NUM2INT(st); } static int error_handle(ex) int ex; { int status = EXIT_FAILURE; if (thread_set_raised()) return EXIT_FAILURE; switch (ex & TAG_MASK) { case 0: status = EXIT_SUCCESS; break; case TAG_RETURN: error_pos(); warn_print(": unexpected return\n"); break; case TAG_NEXT: error_pos(); warn_print(": unexpected next\n"); break; case TAG_BREAK: error_pos(); warn_print(": unexpected break\n"); break; case TAG_REDO: error_pos(); warn_print(": unexpected redo\n"); break; case TAG_RETRY: error_pos(); warn_print(": retry outside of rescue clause\n"); break; case TAG_THROW: if (prot_tag && prot_tag->frame && prot_tag->frame->node) { NODE *tag = prot_tag->frame->node; warn_printf("%s:%d: uncaught throw\n", tag->nd_file, nd_line(tag)); } else { error_pos(); warn_printf(": unexpected throw\n"); } break; case TAG_RAISE: case TAG_FATAL: if (rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) { status = sysexit_status(ruby_errinfo); } else { error_print(); } break; default: rb_bug("Unknown longjmp status %d", ex); break; } thread_reset_raised(); return status; } void ruby_options(argc, argv) int argc; char **argv; { int state; Init_stack((void*)&state); PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { ruby_process_options(argc, argv); } else { trace_func = 0; tracing = 0; exit(error_handle(state)); } POP_TAG(); } void rb_exec_end_proc _((void)); static void ruby_finalize_0() { PUSH_TAG(PROT_NONE); if (EXEC_TAG() == 0) { rb_trap_exit(); } POP_TAG(); rb_exec_end_proc(); } static void ruby_finalize_1() { signal(SIGINT, SIG_DFL); ruby_errinfo = 0; rb_gc_call_finalizer_at_exit(); trace_func = 0; tracing = 0; } void ruby_finalize() { ruby_finalize_0(); ruby_finalize_1(); } int ruby_cleanup(ex) int ex; { int state; volatile VALUE err = ruby_errinfo; ruby_safe_level = 0; Init_stack((void*)&state); ruby_finalize_0(); if (ruby_errinfo) err = ruby_errinfo; PUSH_TAG(PROT_NONE); PUSH_ITER(ITER_NOT); if ((state = EXEC_TAG()) == 0) { rb_thread_cleanup(); rb_thread_wait_other_threads(); } else if (ex == 0) { ex = state; } POP_ITER(); ruby_errinfo = err; ex = error_handle(ex); ruby_finalize_1(); POP_TAG(); if (err && rb_obj_is_kind_of(err, rb_eSystemExit)) { VALUE st = rb_iv_get(err, "status"); return NUM2INT(st); } return ex; } static int ruby_exec_internal() { int state; PUSH_TAG(PROT_NONE); PUSH_ITER(ITER_NOT); /* default visibility is private at toplevel */ SCOPE_SET(SCOPE_PRIVATE); if ((state = EXEC_TAG()) == 0) { eval_node(ruby_top_self, ruby_eval_tree); } POP_ITER(); POP_TAG(); return state; } void ruby_stop(ex) int ex; { exit(ruby_cleanup(ex)); } int ruby_exec() { volatile NODE *tmp; Init_stack((void*)&tmp); return ruby_exec_internal(); } void ruby_run() { int state; static int ex; if (ruby_nerrs > 0) exit(EXIT_FAILURE); state = ruby_exec(); if (state && !ex) ex = state; ruby_stop(ex); } static void compile_error(at) const char *at; { VALUE str; ruby_nerrs = 0; str = rb_str_buf_new2("compile error"); if (at) { rb_str_buf_cat2(str, " in "); rb_str_buf_cat2(str, at); } rb_str_buf_cat(str, "\n", 1); if (!NIL_P(ruby_errinfo)) { rb_str_append(str, rb_obj_as_string(ruby_errinfo)); } rb_exc_raise(rb_exc_new3(rb_eSyntaxError, str)); } VALUE rb_eval_string(str) const char *str; { VALUE v; NODE *oldsrc = ruby_current_node; ruby_current_node = 0; ruby_sourcefile = rb_source_filename("(eval)"); v = eval(ruby_top_self, rb_str_new2(str), Qnil, 0, 0); ruby_current_node = oldsrc; return v; } VALUE rb_eval_string_protect(str, state) const char *str; int *state; { return rb_protect((VALUE (*)_((VALUE)))rb_eval_string, (VALUE)str, state); } VALUE rb_eval_string_wrap(str, state) const char *str; int *state; { int status; VALUE self = ruby_top_self; VALUE wrapper = ruby_wrapper; VALUE val; PUSH_CLASS(ruby_wrapper = rb_module_new()); ruby_top_self = rb_obj_clone(ruby_top_self); rb_extend_object(ruby_top_self, ruby_wrapper); PUSH_FRAME(); ruby_frame->last_func = 0; ruby_frame->last_class = 0; ruby_frame->self = self; PUSH_CREF(ruby_wrapper); PUSH_SCOPE(); val = rb_eval_string_protect(str, &status); ruby_top_self = self; POP_SCOPE(); POP_FRAME(); POP_CLASS(); ruby_wrapper = wrapper; if (state) { *state = status; } else if (status) { JUMP_TAG(status); } return val; } NORETURN(static void localjump_error(const char*, VALUE, int)); static void localjump_error(mesg, value, reason) const char *mesg; VALUE value; int reason; { VALUE exc = rb_exc_new2(rb_eLocalJumpError, mesg); ID id; rb_iv_set(exc, "@exit_value", value); switch (reason) { case TAG_BREAK: id = rb_intern("break"); break; case TAG_REDO: id = rb_intern("redo"); break; case TAG_RETRY: id = rb_intern("retry"); break; case TAG_NEXT: id = rb_intern("next"); break; case TAG_RETURN: id = rb_intern("return"); break; default: id = rb_intern("noreason"); break; } rb_iv_set(exc, "@reason", ID2SYM(id)); rb_exc_raise(exc); } /* * call_seq: * local_jump_error.exit_value => obj * * Returns the exit value associated with this +LocalJumpError+. */ static VALUE localjump_xvalue(exc) VALUE exc; { return rb_iv_get(exc, "@exit_value"); } /* * call-seq: * local_jump_error.reason => symbol * * The reason this block was terminated: * :break, :redo, :retry, :next, :return, or :noreason. */ static VALUE localjump_reason(exc) VALUE exc; { return rb_iv_get(exc, "@reason"); } NORETURN(static void jump_tag_but_local_jump _((int,VALUE))); static void jump_tag_but_local_jump(state, val) int state; VALUE val; { if (val == Qundef) val = prot_tag->retval; switch (state) { case 0: break; case TAG_RETURN: localjump_error("unexpected return", val, state); break; case TAG_BREAK: localjump_error("unexpected break", val, state); break; case TAG_NEXT: localjump_error("unexpected next", val, state); break; case TAG_REDO: localjump_error("unexpected redo", Qnil, state); break; case TAG_RETRY: localjump_error("retry outside of rescue clause", Qnil, state); break; default: break; } JUMP_TAG(state); } VALUE rb_eval_cmd(cmd, arg, level) VALUE cmd, arg; int level; { int state; VALUE val = Qnil; /* OK */ struct SCOPE *saved_scope; volatile int safe = ruby_safe_level; if (OBJ_TAINTED(cmd)) { level = 4; } if (TYPE(cmd) != T_STRING) { PUSH_ITER(ITER_NOT); PUSH_TAG(PROT_NONE); ruby_safe_level = level; if ((state = EXEC_TAG()) == 0) { val = rb_funcall2(cmd, rb_intern("call"), RARRAY(arg)->len, RARRAY(arg)->ptr); } ruby_safe_level = safe; POP_TAG(); POP_ITER(); if (state) JUMP_TAG(state); return val; } saved_scope = ruby_scope; ruby_scope = top_scope; PUSH_FRAME(); ruby_frame->last_func = 0; ruby_frame->last_class = 0; ruby_frame->self = ruby_top_self; PUSH_CREF(ruby_wrapper ? ruby_wrapper : rb_cObject); ruby_safe_level = level; PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { val = eval(ruby_top_self, cmd, Qnil, 0, 0); } if (ruby_scope->flags & SCOPE_DONT_RECYCLE) scope_dup(saved_scope); ruby_scope = saved_scope; ruby_safe_level = safe; POP_TAG(); POP_FRAME(); jump_tag_but_local_jump(state, val); return val; } #define ruby_cbase (ruby_cref->nd_clss) static VALUE ev_const_defined(cref, id, self) NODE *cref; ID id; VALUE self; { NODE *cbase = cref; VALUE result; while (cbase && cbase->nd_next) { struct RClass *klass = RCLASS(cbase->nd_clss); if (!NIL_P(klass)) { if (klass->iv_tbl && st_lookup(klass->iv_tbl, id, &result)) { if (result == Qundef && NIL_P(rb_autoload_p((VALUE)klass, id))) { return Qfalse; } return Qtrue; } } cbase = cbase->nd_next; } return rb_const_defined(cref->nd_clss, id); } static VALUE ev_const_get(cref, id, self) NODE *cref; ID id; VALUE self; { NODE *cbase = cref; VALUE result; while (cbase && cbase->nd_next) { VALUE klass = cbase->nd_clss; if (!NIL_P(klass)) { while (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &result)) { if (result == Qundef) { if (!RTEST(rb_autoload_load(klass, id))) break; continue; } return result; } } cbase = cbase->nd_next; } return rb_const_get(NIL_P(cref->nd_clss) ? CLASS_OF(self): cref->nd_clss, id); } static VALUE cvar_cbase() { NODE *cref = ruby_cref; while (cref && cref->nd_next && (NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON))) { cref = cref->nd_next; if (!cref->nd_next) { rb_warn("class variable access from toplevel singleton method"); } } if (NIL_P(cref->nd_clss)) { rb_raise(rb_eTypeError, "no class variables available"); } return cref->nd_clss; } /* * call-seq: * Module.nesting => array * * Returns the list of +Modules+ nested at the point of call. * * module M1 * module M2 * $a = Module.nesting * end * end * $a #=> [M1::M2, M1] * $a[0].name #=> "M1::M2" */ static VALUE rb_mod_nesting() { NODE *cbase = ruby_cref; VALUE ary = rb_ary_new(); while (cbase && cbase->nd_next) { if (!NIL_P(cbase->nd_clss)) rb_ary_push(ary, cbase->nd_clss); cbase = cbase->nd_next; } if (ruby_wrapper && RARRAY(ary)->len == 0) { rb_ary_push(ary, ruby_wrapper); } return ary; } /* * call-seq: * Module.constants => array * * Returns an array of the names of all constants defined in the * system. This list includes the names of all modules and classes. * * p Module.constants.sort[1..5] * * produces: * * ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"] */ static VALUE rb_mod_s_constants() { NODE *cbase = ruby_cref; void *data = 0; while (cbase) { if (!NIL_P(cbase->nd_clss)) { data = rb_mod_const_at(cbase->nd_clss, data); } cbase = cbase->nd_next; } if (!NIL_P(ruby_cbase)) { data = rb_mod_const_of(ruby_cbase, data); } return rb_const_list(data); } void rb_frozen_class_p(klass) VALUE klass; { char *desc = "something(?!)"; if (OBJ_FROZEN(klass)) { if (FL_TEST(klass, FL_SINGLETON)) desc = "object"; else { switch (TYPE(klass)) { case T_MODULE: case T_ICLASS: desc = "module"; break; case T_CLASS: desc = "class"; break; } } rb_error_frozen(desc); } } void rb_undef(klass, id) VALUE klass; ID id; { VALUE origin; NODE *body; if (ruby_cbase == rb_cObject && klass == rb_cObject) { rb_secure(4); } if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) { rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'", rb_id2name(id)); } rb_frozen_class_p(klass); if (id == __id__ || id == __send__ || id == init) { rb_warn("undefining `%s' may cause serious problem", rb_id2name(id)); } body = search_method(klass, id, &origin); if (!body || !body->nd_body) { char *s0 = " class"; VALUE c = klass; if (FL_TEST(c, FL_SINGLETON)) { VALUE obj = rb_iv_get(klass, "__attached__"); switch (TYPE(obj)) { case T_MODULE: case T_CLASS: c = obj; s0 = ""; } } else if (TYPE(c) == T_MODULE) { s0 = " module"; } rb_name_error(id, "undefined method `%s' for%s `%s'", rb_id2name(id),s0,rb_class2name(c)); } rb_add_method(klass, id, 0, NOEX_PUBLIC); if (FL_TEST(klass, FL_SINGLETON)) { rb_funcall(rb_iv_get(klass, "__attached__"), singleton_undefined, 1, ID2SYM(id)); } else { rb_funcall(klass, undefined, 1, ID2SYM(id)); } } /* * call-seq: * undef_method(symbol) => self * * Prevents the current class from responding to calls to the named * method. Contrast this with remove_method, which deletes * the method from the particular class; Ruby will still search * superclasses and mixed-in modules for a possible receiver. * * class Parent * def hello * puts "In parent" * end * end * class Child < Parent * def hello * puts "In child" * end * end * * * c = Child.new * c.hello * * * class Child * remove_method :hello # remove from child, still in parent * end * c.hello * * * class Child * undef_method :hello # prevent any calls to 'hello' * end * c.hello * * produces: * * In child * In parent * prog.rb:23: undefined method `hello' for # (NoMethodError) */ static VALUE rb_mod_undef_method(argc, argv, mod) int argc; VALUE *argv; VALUE mod; { int i; for (i=0; ind_body) { if (TYPE(klass) == T_MODULE) { orig = search_method(rb_cObject, def, &origin); } } if (!orig || !orig->nd_body) { print_undef(klass, def); } if (FL_TEST(klass, FL_SINGLETON)) { singleton = rb_iv_get(klass, "__attached__"); } body = orig->nd_body; orig->nd_cnt++; if (nd_type(body) == NODE_FBODY) { /* was alias */ def = body->nd_mid; origin = body->nd_orig; body = body->nd_head; } rb_clear_cache_by_id(name); if (RTEST(ruby_verbose) && st_lookup(RCLASS(klass)->m_tbl, name, (st_data_t *)&node)) { if (node->nd_cnt == 0 && node->nd_body) { rb_warning("discarding old %s", rb_id2name(name)); } } st_insert(RCLASS(klass)->m_tbl, name, (st_data_t)NEW_METHOD(NEW_FBODY(body, def, origin), NOEX_WITH_SAFE(orig->nd_noex))); if (singleton) { rb_funcall(singleton, singleton_added, 1, ID2SYM(name)); } else { rb_funcall(klass, added, 1, ID2SYM(name)); } } /* * call-seq: * alias_method(new_name, old_name) => self * * Makes new_name a new copy of the method old_name. This can * be used to retain access to methods that are overridden. * * module Mod * alias_method :orig_exit, :exit * def exit(code=0) * puts "Exiting with code #{code}" * orig_exit(code) * end * end * include Mod * exit(99) * * produces: * * Exiting with code 99 */ static VALUE rb_mod_alias_method(mod, newname, oldname) VALUE mod, newname, oldname; { rb_alias(mod, rb_to_id(newname), rb_to_id(oldname)); return mod; } static NODE* copy_node_scope(node, rval) NODE *node; NODE *rval; { NODE *copy = NEW_NODE(NODE_SCOPE,0,rval,node->nd_next); if (node->nd_tbl) { copy->nd_tbl = ALLOC_N(ID, node->nd_tbl[0]+1); MEMCPY(copy->nd_tbl, node->nd_tbl, ID, node->nd_tbl[0]+1); } else { copy->nd_tbl = 0; } return copy; } #ifdef C_ALLOCA # define TMP_PROTECT NODE * volatile tmp__protect_tmp=0 # define TMP_ALLOC(n) \ (tmp__protect_tmp = NEW_NODE(NODE_ALLOCA, \ ALLOC_N(VALUE,n),tmp__protect_tmp,n), \ (void*)tmp__protect_tmp->nd_head) #else # define TMP_PROTECT typedef int foobazzz # define TMP_ALLOC(n) ALLOCA_N(VALUE,n) #endif #define SETUP_ARGS0(anode,extra) do {\ NODE *n = anode;\ if (!n) {\ argc = 0;\ argv = 0;\ }\ else if (nd_type(n) == NODE_ARRAY) {\ argc=anode->nd_alen;\ if (argc > 0) {\ int i;\ n = anode;\ argv = TMP_ALLOC(argc+extra);\ for (i=0;ind_head);\ n=n->nd_next;\ }\ }\ else {\ argc = 0;\ argv = 0;\ }\ }\ else {\ VALUE args = rb_eval(self,n);\ if (TYPE(args) != T_ARRAY)\ args = rb_ary_to_ary(args);\ argc = RARRAY(args)->len;\ argv = TMP_ALLOC(argc+extra);\ MEMCPY(argv, RARRAY(args)->ptr, VALUE, argc);\ }\ } while (0) #define SETUP_ARGS(anode) SETUP_ARGS0(anode,0) #define BEGIN_CALLARGS do {\ struct BLOCK *tmp_block = ruby_block;\ int tmp_iter = ruby_iter->iter;\ switch (tmp_iter) {\ case ITER_PRE:\ ruby_block = ruby_block->outer;\ case ITER_PAS:\ tmp_iter = ITER_NOT;\ }\ PUSH_ITER(tmp_iter) #define END_CALLARGS \ ruby_block = tmp_block;\ POP_ITER();\ } while (0) #define MATCH_DATA *rb_svar(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;ind_head, buf)) return 0; node = node->nd_next; } } } 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; again: if (!node) return "expression"; switch (nd_type(node)) { case NODE_SUPER: case NODE_ZSUPER: if (ruby_frame->last_func == 0) return 0; else if (ruby_frame->last_class == 0) return 0; val = ruby_frame->last_class; if (rb_method_boundp(RCLASS(val)->super, ruby_frame->orig_func, 0)) { 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 = self; goto check_bound; case NODE_ATTRASGN: val = self; if (node->nd_recv == (NODE *)1) goto check_bound; case NODE_CALL: PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { val = rb_eval(self, node->nd_recv); } POP_TAG(); if (state) { ruby_errinfo = Qnil; return 0; } check_bound: { int call = nd_type(node)==NODE_CALL; val = CLASS_OF(val); if (call) { int noex; ID id = node->nd_mid; if (!rb_get_method_body(&val, &id, &noex)) break; if ((noex & NOEX_PRIVATE)) break; if ((noex & NOEX_PROTECTED) && !rb_obj_is_kind_of(self, rb_class_real(val))) break; } else if (!rb_method_boundp(val, node->nd_mid, call)) break; return arg_defined(self, node->nd_args, buf, nd_type(node) == NODE_ATTRASGN ? "assignment" : "method"); } break; case NODE_MATCH2: case NODE_MATCH3: return "method"; case NODE_YIELD: if (rb_block_given_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_CURR: case NODE_GASGN: case NODE_IASGN: case NODE_CDECL: case NODE_CVDECL: case NODE_CVASGN: 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_CONST: if (ev_const_defined(ruby_cref, node->nd_vid, self)) { return "constant"; } break; case NODE_CVAR: if (rb_cvar_defined(cvar_cbase(), node->nd_vid)) { return "class variable"; } break; case NODE_COLON2: PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { val = rb_eval(self, node->nd_head); } POP_TAG(); if (state) { ruby_errinfo = Qnil; return 0; } else { switch (TYPE(val)) { case T_CLASS: case T_MODULE: if (rb_const_defined_from(val, node->nd_mid)) return "constant"; break; default: if (rb_method_boundp(CLASS_OF(val), node->nd_mid, 1)) { return "method"; } } } break; case NODE_COLON3: if (rb_const_defined_from(rb_cObject, node->nd_mid)) { return "constant"; } break; case NODE_NTH_REF: if (RTEST(rb_reg_nth_defined(node->nd_nth, MATCH_DATA))) { sprintf(buf, "$%d", (int)node->nd_nth); return buf; } break; case NODE_BACK_REF: if (RTEST(rb_reg_nth_defined(0, MATCH_DATA))) { sprintf(buf, "$%c", (char)node->nd_nth); return buf; } break; case NODE_NEWLINE: node = node->nd_next; goto again; default: PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { rb_eval(self, node); } POP_TAG(); if (!state) { return "expression"; } ruby_errinfo = Qnil; break; } return 0; } static int handle_rescue _((VALUE,NODE*)); static void blk_free(); static VALUE rb_obj_is_proc(proc) VALUE proc; { if (TYPE(proc) == T_DATA && RDATA(proc)->dfree == (RUBY_DATA_FUNC)blk_free) { return Qtrue; } return Qfalse; } void rb_add_event_hook(func, events) rb_event_hook_func_t func; rb_event_t events; { rb_event_hook_t *hook; hook = ALLOC(rb_event_hook_t); hook->func = func; hook->events = events; hook->next = event_hooks; event_hooks = hook; } int rb_remove_event_hook(func) rb_event_hook_func_t func; { rb_event_hook_t *prev, *hook; prev = NULL; hook = event_hooks; while (hook) { if (hook->func == func) { if (prev) { prev->next = hook->next; } else { event_hooks = hook->next; } xfree(hook); return 0; } prev = hook; hook = hook->next; } return -1; } /* * call-seq: * set_trace_func(proc) => proc * set_trace_func(nil) => nil * * Establishes _proc_ as the handler for tracing, or disables * tracing if the parameter is +nil+. _proc_ takes up * to six parameters: an event name, a filename, a line number, an * object id, a binding, and the name of a class. _proc_ is * invoked whenever an event occurs. Events are: c-call * (call a C-language routine), c-return (return from a * C-language routine), call (call a Ruby method), * class (start a class or module definition), * end (finish a class or module definition), * line (execute code on a new line), raise * (raise an exception), and return (return from a Ruby * method). Tracing is disabled within the context of _proc_. * * class Test * def test * a = 1 * b = 2 * end * end * * set_trace_func proc { |event, file, line, id, binding, classname| * printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname * } * t = Test.new * t.test * * line prog.rb:11 false * c-call prog.rb:11 new Class * c-call prog.rb:11 initialize Object * c-return prog.rb:11 initialize Object * c-return prog.rb:11 new Class * line prog.rb:12 false * call prog.rb:2 test Test * line prog.rb:3 test Test * line prog.rb:4 test Test * return prog.rb:4 test Test */ static VALUE set_trace_func(obj, trace) VALUE obj, trace; { rb_event_hook_t *hook; rb_secure(4); if (NIL_P(trace)) { trace_func = 0; rb_remove_event_hook(call_trace_func); return Qnil; } if (!rb_obj_is_proc(trace)) { rb_raise(rb_eTypeError, "trace_func needs to be Proc"); } trace_func = trace; for (hook = event_hooks; hook; hook = hook->next) { if (hook->func == call_trace_func) return trace; } rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL); return trace; } static char * get_event_name(rb_event_t event) { switch (event) { case RUBY_EVENT_LINE: return "line"; case RUBY_EVENT_CLASS: return "class"; case RUBY_EVENT_END: return "end"; case RUBY_EVENT_CALL: return "call"; case RUBY_EVENT_RETURN: return "return"; case RUBY_EVENT_C_CALL: return "c-call"; case RUBY_EVENT_C_RETURN: return "c-return"; case RUBY_EVENT_RAISE: return "raise"; default: return "unknown"; } } static void call_trace_func(event, node, self, id, klass) rb_event_t event; NODE *node; VALUE self; ID id; VALUE klass; /* OK */ { int state, raised; struct FRAME *prev; NODE *node_save; VALUE srcfile; char *event_name; if (!trace_func) return; if (tracing) return; if (ruby_in_compile) return; if (id == ID_ALLOCATOR) return; if (!(node_save = ruby_current_node)) { node_save = NEW_NEWLINE(0); } tracing = 1; prev = ruby_frame; PUSH_FRAME(); *ruby_frame = *prev; ruby_frame->prev = prev; ruby_frame->iter = 0; /* blocks not available anyway */ if (node) { ruby_current_node = node; ruby_frame->node = node; ruby_sourcefile = node->nd_file; ruby_sourceline = nd_line(node); } if (klass) { if (TYPE(klass) == T_ICLASS) { klass = RBASIC(klass)->klass; } else if (FL_TEST(klass, FL_SINGLETON)) { klass = rb_iv_get(klass, "__attached__"); } } PUSH_TAG(PROT_NONE); raised = thread_reset_raised(); if ((state = EXEC_TAG()) == 0) { srcfile = rb_str_new2(ruby_sourcefile?ruby_sourcefile:"(ruby)"); event_name = get_event_name(event); proc_invoke(trace_func, rb_ary_new3(6, rb_str_new2(event_name), srcfile, INT2FIX(ruby_sourceline), id?ID2SYM(id):Qnil, self?rb_f_binding(self):Qnil, klass), Qundef, 0); } if (raised) thread_set_raised(); POP_TAG(); POP_FRAME(); tracing = 0; ruby_current_node = node_save; SET_CURRENT_SOURCE(); if (state) JUMP_TAG(state); } static VALUE avalue_to_svalue(v) VALUE v; { VALUE tmp, top; tmp = rb_check_array_type(v); if (NIL_P(tmp)) { return v; } if (RARRAY(tmp)->len == 0) { return Qundef; } if (RARRAY(tmp)->len == 1) { top = rb_check_array_type(RARRAY(tmp)->ptr[0]); if (NIL_P(top)) { return RARRAY(tmp)->ptr[0]; } if (RARRAY(top)->len > 1) { return v; } return top; } return tmp; } static VALUE svalue_to_avalue(v) VALUE v; { VALUE tmp, top; if (v == Qundef) return rb_ary_new2(0); tmp = rb_check_array_type(v); if (NIL_P(tmp)) { return rb_ary_new3(1, v); } if (RARRAY(tmp)->len == 1) { top = rb_check_array_type(RARRAY(tmp)->ptr[0]); if (!NIL_P(top) && RARRAY(top)->len > 1) { return tmp; } return rb_ary_new3(1, v); } return tmp; } static VALUE svalue_to_mrhs(v, lhs) VALUE v; NODE *lhs; { VALUE tmp; if (v == Qundef) return rb_ary_new2(0); tmp = rb_check_array_type(v); if (NIL_P(tmp)) { return rb_ary_new3(1, v); } /* no lhs means splat lhs only */ if (!lhs) { return rb_ary_new3(1, v); } return tmp; } static VALUE avalue_splat(v) VALUE v; { if (RARRAY(v)->len == 0) { return Qundef; } if (RARRAY(v)->len == 1) { return RARRAY(v)->ptr[0]; } return v; } #if 1 VALUE rb_Array(val) VALUE val; { VALUE tmp = rb_check_array_type(val); if (NIL_P(tmp)) { /* hack to avoid invoke Object#to_a */ VALUE origin; ID id = rb_intern("to_a"); if (search_method(CLASS_OF(val), id, &origin) && RCLASS(origin)->m_tbl != RCLASS(rb_mKernel)->m_tbl) { /* exclude Kernel#to_a */ val = rb_funcall(val, id, 0); if (TYPE(val) != T_ARRAY) { rb_raise(rb_eTypeError, "`to_a' did not return Array"); } return val; } else { return rb_ary_new3(1, val); } } return tmp; } #endif static VALUE splat_value(v) VALUE v; { if (NIL_P(v)) return rb_ary_new3(1, Qnil); return rb_Array(v); } static VALUE class_prefix(self, cpath) VALUE self; NODE *cpath; { if (!cpath) { rb_bug("class path missing"); } if (cpath->nd_head) { VALUE c = rb_eval(self, cpath->nd_head); switch (TYPE(c)) { case T_CLASS: case T_MODULE: break; default: rb_raise(rb_eTypeError, "%s is not a class/module", RSTRING(rb_obj_as_string(c))->ptr); } return c; } else if (nd_type(cpath) == NODE_COLON2) { return ruby_cbase; } else if (ruby_wrapper) { return ruby_wrapper; } else { return rb_cObject; } } #define return_value(v) do {\ if ((prot_tag->retval = (v)) == Qundef) {\ prot_tag->retval = Qnil;\ }\ } while (0) NORETURN(static void return_jump _((VALUE))); NORETURN(static void break_jump _((VALUE))); NORETURN(static void next_jump _((VALUE))); NORETURN(static void unknown_node _((NODE * volatile))); static void unknown_node(node) NODE *volatile node; { ruby_current_node = 0; if (node->flags == 0) { rb_bug("terminated node (0x%lx)", node); } else if (BUILTIN_TYPE(node) != T_NODE) { rb_bug("not a node 0x%02lx (0x%lx)", BUILTIN_TYPE(node), node); } else { rb_bug("unknown node type %d (0x%lx)", nd_type(node), node); } } static VALUE rb_eval(self, n) VALUE self; NODE *n; { NODE * volatile contnode = 0; NODE * volatile node = n; int state; volatile VALUE result = Qnil; #define RETURN(v) do { \ result = (v); \ goto finish; \ } while (0) again: if (!node) RETURN(Qnil); ruby_current_node = node; switch (nd_type(node)) { case NODE_BLOCK: if (contnode) { result = rb_eval(self, node); break; } contnode = node->nd_next; node = node->nd_head; goto again; 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_lit); break; /* nodes for speed-up(literal match) */ case NODE_MATCH2: { VALUE l = rb_eval(self,node->nd_recv); VALUE r = rb_eval(self,node->nd_value); result = rb_reg_match(l, r); } 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(l) == T_STRING) { result = rb_reg_match(r, l); } else { result = rb_funcall(l, match, 1, r); } } break; /* node for speed-up(top-level loop for -n/-p) */ case NODE_OPT_N: PUSH_TAG(PROT_LOOP); switch (state = EXEC_TAG()) { case 0: opt_n_next: while (!NIL_P(rb_gets())) { opt_n_redo: rb_eval(self, node->nd_body); } break; case TAG_REDO: state = 0; goto opt_n_redo; case TAG_NEXT: state = 0; goto opt_n_next; case TAG_BREAK: state = 0; default: break; } POP_TAG(); if (state) JUMP_TAG(state); 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: #ifdef ENABLE_DTRACE if (RUBY_LINE_ENABLED()) if (ruby_current_node && ruby_current_node->nd_file) RUBY_LINE(ruby_current_node->nd_file, nd_line(ruby_current_node)); #endif EXEC_EVENT_HOOK(RUBY_EVENT_LINE, node, self, ruby_frame->last_func, ruby_frame->last_class); if (RTEST(rb_eval(self, node->nd_cond))) { node = node->nd_body; } else { node = node->nd_else; } goto again; case NODE_WHEN: while (node) { NODE *tag; if (nd_type(node) != NODE_WHEN) goto again; tag = node->nd_head; while (tag) { #ifdef ENABLE_DTRACE if (RUBY_LINE_ENABLED()) if (ruby_current_node && ruby_current_node->nd_file) RUBY_LINE(ruby_current_node->nd_file, nd_line(ruby_current_node)); #endif EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self, ruby_frame->last_func, ruby_frame->last_class); if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) { VALUE v = rb_eval(self, tag->nd_head->nd_head); long i; if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v); for (i=0; ilen; i++) { if (RTEST(RARRAY(v)->ptr[i])) { node = node->nd_body; goto again; } } tag = tag->nd_next; continue; } if (RTEST(rb_eval(self, tag->nd_head))) { node = node->nd_body; goto again; } tag = tag->nd_next; } node = node->nd_next; } RETURN(Qnil); case NODE_CASE: { VALUE val; val = rb_eval(self, node->nd_head); node = node->nd_body; while (node) { NODE *tag; if (nd_type(node) != NODE_WHEN) { goto again; } tag = node->nd_head; while (tag) { #ifdef ENABLE_DTRACE if (RUBY_LINE_ENABLED()) if (ruby_current_node && ruby_current_node->nd_file) RUBY_LINE(ruby_current_node->nd_file, nd_line(ruby_current_node)); #endif EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self, ruby_frame->last_func, ruby_frame->last_class); if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) { VALUE v = rb_eval(self, tag->nd_head->nd_head); long i; if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v); for (i=0; ilen; 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_LOOP); result = Qnil; switch (state = EXEC_TAG()) { case 0: 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: if (TAG_DST()) { state = 0; result = prot_tag->retval; } /* fall through */ default: break; } while_out: POP_TAG(); if (state) JUMP_TAG(state); RETURN(result); case NODE_UNTIL: PUSH_TAG(PROT_LOOP); result = Qnil; 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: if (TAG_DST()) { state = 0; result = prot_tag->retval; } /* fall through */ default: break; } until_out: POP_TAG(); if (state) JUMP_TAG(state); RETURN(result); case NODE_BLOCK_PASS: result = block_pass(self, node); break; case NODE_ITER: case NODE_FOR: { PUSH_TAG(PROT_LOOP); PUSH_BLOCK(node->nd_var, node->nd_body); state = EXEC_TAG(); if (state == 0) { iter_retry: PUSH_ITER(ITER_PRE); if (nd_type(node) == NODE_ITER) { result = rb_eval(self, node->nd_iter); } else { VALUE recv; _block.flags &= ~BLOCK_D_SCOPE; BEGIN_CALLARGS; recv = rb_eval(self, node->nd_iter); END_CALLARGS; ruby_current_node = node; SET_CURRENT_SOURCE(); result = rb_call(CLASS_OF(recv),recv,each,0,0,0); } POP_ITER(); } else if (state == TAG_BREAK && TAG_DST()) { result = prot_tag->retval; state = 0; } else if (state == TAG_RETRY) { state = 0; goto iter_retry; } POP_BLOCK(); POP_TAG(); switch (state) { case 0: break; default: JUMP_TAG(state); } } break; case NODE_BREAK: break_jump(rb_eval(self, node->nd_stts)); break; case NODE_NEXT: CHECK_INTS; next_jump(rb_eval(self, node->nd_stts)); break; case NODE_REDO: CHECK_INTS; JUMP_TAG(TAG_REDO); break; case NODE_RETRY: CHECK_INTS; JUMP_TAG(TAG_RETRY); break; case NODE_SPLAT: result = splat_value(rb_eval(self, node->nd_head)); break; case NODE_TO_ARY: result = rb_ary_to_ary(rb_eval(self, node->nd_head)); break; case NODE_SVALUE: result = avalue_splat(rb_eval(self, node->nd_head)); if (result == Qundef) result = Qnil; break; case NODE_YIELD: if (node->nd_head) { result = rb_eval(self, node->nd_head); ruby_current_node = node; } else { result = Qundef; /* no arg */ } SET_CURRENT_SOURCE(); result = rb_yield_0(result, 0, 0, 0, node->nd_state); break; case NODE_RESCUE: { volatile VALUE e_info = ruby_errinfo; volatile int rescuing = 0; PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { retry_entry: result = rb_eval(self, node->nd_head); } else if (rescuing) { if (rescuing < 0) { /* in rescue argument, just reraise */ } else if (state == TAG_RETRY) { rescuing = state = 0; ruby_errinfo = e_info; goto retry_entry; } else if (state != TAG_RAISE) { result = prot_tag->retval; } } else if (state == TAG_RAISE) { NODE *resq = node->nd_resq; rescuing = -1; while (resq) { ruby_current_node = resq; #ifdef ENABLE_DTRACE if (RUBY_RESCUE_ENABLED()) if (ruby_current_node && ruby_current_node->nd_file) RUBY_RESCUE(ruby_current_node->nd_file, nd_line(ruby_current_node)); #endif if (handle_rescue(self, resq)) { state = 0; rescuing = 1; result = rb_eval(self, resq->nd_body); break; } resq = resq->nd_head; /* next rescue */ } } else { result = prot_tag->retval; } POP_TAG(); if (state != TAG_RAISE) ruby_errinfo = e_info; if (state) { JUMP_TAG(state); } /* no exception raised */ if (!rescuing && (node = node->nd_else)) { /* else clause given */ goto again; } } 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 */ VALUE errinfo = ruby_errinfo; rb_eval(self, node->nd_ensr); return_value(retval); ruby_errinfo = errinfo; } 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: { VALUE beg = rb_eval(self, node->nd_beg); VALUE end = rb_eval(self, node->nd_end); result = rb_range_new(beg, end, nd_type(node) == NODE_DOT3); } break; case NODE_FLIP2: /* like AWK */ { VALUE *flip = rb_svar(node->nd_cnt); if (!flip) rb_bug("unexpected local variable"); if (!RTEST(*flip)) { if (RTEST(rb_eval(self, node->nd_beg))) { *flip = RTEST(rb_eval(self, node->nd_end))?Qfalse:Qtrue; result = Qtrue; } else { result = Qfalse; } } else { if (RTEST(rb_eval(self, node->nd_end))) { *flip = Qfalse; } result = Qtrue; } } break; case NODE_FLIP3: /* like SED */ { VALUE *flip = rb_svar(node->nd_cnt); if (!flip) rb_bug("unexpected local variable"); if (!RTEST(*flip)) { result = RTEST(rb_eval(self, node->nd_beg)) ? Qtrue : Qfalse; *flip = result; } else { if (RTEST(rb_eval(self, node->nd_end))) { *flip = Qfalse; } result = Qtrue; } } break; case NODE_RETURN: return_jump(rb_eval(self, node->nd_stts)); break; case NODE_ARGSCAT: { VALUE args = rb_eval(self, node->nd_head); result = rb_ary_concat(args, splat_value(rb_eval(self, node->nd_body))); } break; case NODE_ARGSPUSH: { VALUE args = rb_ary_dup(rb_eval(self, node->nd_head)); result = rb_ary_push(args, rb_eval(self, node->nd_body)); } break; case NODE_ATTRASGN: { VALUE recv; int argc; VALUE *argv; /* used in SETUP_ARGS */ int scope; TMP_PROTECT; BEGIN_CALLARGS; if (node->nd_recv == (NODE *)1) { recv = self; scope = 1; } else { recv = rb_eval(self, node->nd_recv); scope = 0; } SETUP_ARGS(node->nd_args); END_CALLARGS; ruby_current_node = node; SET_CURRENT_SOURCE(); rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,scope); result = argv[argc-1]; } break; case NODE_CALL: { VALUE recv; int argc; VALUE *argv; /* used in SETUP_ARGS */ TMP_PROTECT; BEGIN_CALLARGS; recv = rb_eval(self, node->nd_recv); SETUP_ARGS(node->nd_args); END_CALLARGS; ruby_current_node = node; SET_CURRENT_SOURCE(); 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; ruby_current_node = node; SET_CURRENT_SOURCE(); result = rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1); } break; case NODE_VCALL: SET_CURRENT_SOURCE(); 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) { if (ruby_frame->last_func) { rb_name_error(ruby_frame->last_func, "superclass method `%s' disabled", rb_id2name(ruby_frame->orig_func)); } else { rb_raise(rb_eNoMethodError, "super called outside of method"); } } if (nd_type(node) == NODE_ZSUPER) { argc = ruby_frame->argc; if (argc && DMETHOD_P()) { if (TYPE(RBASIC(ruby_scope)->klass) != T_ARRAY || RARRAY(RBASIC(ruby_scope)->klass)->len != argc) { rb_raise(rb_eRuntimeError, "super: specify arguments explicitly"); } argv = RARRAY(RBASIC(ruby_scope)->klass)->ptr; } else { argv = ruby_scope->local_vars + 2; } } else { BEGIN_CALLARGS; SETUP_ARGS(node->nd_args); END_CALLARGS; ruby_current_node = node; } SET_CURRENT_SOURCE(); result = rb_call_super(argc, argv); } break; case NODE_SCOPE: { struct FRAME frame; NODE *saved_cref = 0; frame = *ruby_frame; frame.tmp = ruby_frame; ruby_frame = &frame; PUSH_SCOPE(); PUSH_TAG(PROT_NONE); if (node->nd_rval) { saved_cref = ruby_cref; ruby_cref = (NODE*)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 = frame.tmp; if (saved_cref) ruby_cref = saved_cref; if (state) JUMP_TAG(state); } break; case NODE_OP_ASGN1: { int argc; VALUE *argv; /* used in SETUP_ARGS */ VALUE recv, val, tmp; NODE *rval; TMP_PROTECT; recv = rb_eval(self, node->nd_recv); rval = node->nd_args->nd_head; SETUP_ARGS0(node->nd_args->nd_body, 1); val = rb_funcall3(recv, aref, argc, argv); switch (node->nd_mid) { case 0: /* OR */ if (RTEST(val)) RETURN(val); val = rb_eval(self, rval); break; case 1: /* AND */ if (!RTEST(val)) RETURN(val); val = rb_eval(self, rval); break; default: tmp = rb_eval(self, rval); val = rb_funcall3(val, node->nd_mid, 1, &tmp); } argv[argc] = val; rb_funcall2(recv, aset, argc+1, argv); result = val; } break; case NODE_OP_ASGN2: { ID id = node->nd_next->nd_vid; VALUE recv, val, tmp; recv = rb_eval(self, node->nd_recv); val = rb_funcall3(recv, id, 0, 0); switch (node->nd_next->nd_mid) { case 0: /* OR */ if (RTEST(val)) RETURN(val); val = rb_eval(self, node->nd_value); break; case 1: /* AND */ if (!RTEST(val)) RETURN(val); val = rb_eval(self, node->nd_value); break; default: tmp = rb_eval(self, node->nd_value); val = rb_funcall3(val, node->nd_next->nd_mid, 1, &tmp); } 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)) break; node = node->nd_value; goto again; case NODE_OP_ASGN_OR: if ((node->nd_aid && !is_defined(self, node->nd_head, 0)) || !RTEST(result = rb_eval(self, node->nd_head))) { node = node->nd_value; goto again; } break; case NODE_MASGN: result = massign(self, node, rb_eval(self, node->nd_value), 0); 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); dvar_asgn(node->nd_vid, result); break; case NODE_DASGN_CURR: result = rb_eval(self, node->nd_value); dvar_asgn_curr(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_CDECL: result = rb_eval(self, node->nd_value); if (node->nd_vid == 0) { rb_const_set(class_prefix(self, node->nd_else), node->nd_else->nd_mid, result); } else { rb_const_set(ruby_cbase, node->nd_vid, result); } break; case NODE_CVDECL: if (NIL_P(ruby_cbase)) { rb_raise(rb_eTypeError, "no class/module to define class variable"); } result = rb_eval(self, node->nd_value); rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qtrue); break; case NODE_CVASGN: result = rb_eval(self, node->nd_value); rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qfalse); 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_CONST: result = ev_const_get(ruby_cref, node->nd_vid, self); break; case NODE_CVAR: result = rb_cvar_get(cvar_cbase(), node->nd_vid); break; case NODE_BLOCK_ARG: if (ruby_scope->local_vars == 0) rb_bug("unexpected block argument"); if (rb_block_given_p()) { result = rb_block_proc(); ruby_scope->local_vars[node->nd_cnt] = result; } else { result = Qnil; } break; case NODE_COLON2: { VALUE klass; klass = rb_eval(self, node->nd_head); if (rb_is_const_id(node->nd_mid)) { switch (TYPE(klass)) { case T_CLASS: case T_MODULE: result = rb_const_get_from(klass, node->nd_mid); break; default: rb_raise(rb_eTypeError, "%s is not a class/module", RSTRING(rb_obj_as_string(klass))->ptr); break; } } else { result = rb_funcall(klass, node->nd_mid, 0, 0); } } break; case NODE_COLON3: result = rb_const_get_from(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; long 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_EVSTR: result = rb_obj_as_string(rb_eval(self, node->nd_body)); break; case NODE_DSTR: case NODE_DXSTR: case NODE_DREGX: case NODE_DREGX_ONCE: case NODE_DSYM: { 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; default: str2 = rb_eval(self, list->nd_head); break; } rb_str_append(str, str2); OBJ_INFECT(str, str2); } 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_LIT: /* other thread may replace NODE_DREGX_ONCE to NODE_LIT */ goto again; case NODE_DXSTR: result = rb_funcall(self, '`', 1, str); break; case NODE_DSYM: result = rb_str_intern(str); break; default: result = str; break; } } break; case NODE_XSTR: result = rb_funcall(self, '`', 1, rb_str_new3(node->nd_lit)); break; case NODE_LIT: result = node->nd_lit; break; case NODE_DEFN: if (node->nd_defn) { NODE *body, *defn; VALUE origin; int noex; if (NIL_P(ruby_class)) { rb_raise(rb_eTypeError, "no class/module to add method"); } if (ruby_class == rb_cObject && node->nd_mid == init) { rb_warn("redefining Object#initialize may cause infinite loop"); } if (node->nd_mid == __id__ || node->nd_mid == __send__) { rb_warn("redefining `%s' may cause serious problem", rb_id2name(node->nd_mid)); } rb_frozen_class_p(ruby_class); body = search_method(ruby_class, node->nd_mid, &origin); if (body){ if (RTEST(ruby_verbose) && ruby_class == origin && body->nd_cnt == 0 && body->nd_body) { rb_warning("method redefined; discarding old %s", rb_id2name(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_body == 0) { noex |= NOEX_NOSUPER; } defn = copy_node_scope(node->nd_defn, ruby_cref); rb_add_method(ruby_class, node->nd_mid, defn, noex); if (scope_vmode == SCOPE_MODFUNC) { rb_add_method(rb_singleton_class(ruby_class), node->nd_mid, defn, NOEX_PUBLIC); } result = Qnil; } break; case NODE_DEFS: if (node->nd_defn) { VALUE recv = rb_eval(self, node->nd_recv); VALUE klass; NODE *body = 0, *defn; if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv)) { rb_raise(rb_eSecurityError, "Insecure: can't define singleton method"); } if (FIXNUM_P(recv) || SYMBOL_P(recv)) { rb_raise(rb_eTypeError, "can't define singleton method \"%s\" for %s", rb_id2name(node->nd_mid), rb_obj_classname(recv)); } if (OBJ_FROZEN(recv)) rb_error_frozen("object"); klass = rb_singleton_class(recv); if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, (st_data_t *)&body)) { if (ruby_safe_level >= 4) { rb_raise(rb_eSecurityError, "redefining method prohibited"); } if (RTEST(ruby_verbose)) { rb_warning("redefine %s", rb_id2name(node->nd_mid)); } } defn = copy_node_scope(node->nd_defn, ruby_cref); rb_add_method(klass, node->nd_mid, defn, NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0)); result = Qnil; } break; case NODE_UNDEF: if (NIL_P(ruby_class)) { rb_raise(rb_eTypeError, "no class to undef method"); } rb_undef(ruby_class, rb_to_id(rb_eval(self, node->u2.node))); result = Qnil; break; case NODE_ALIAS: if (NIL_P(ruby_class)) { rb_raise(rb_eTypeError, "no class to make alias"); } rb_alias(ruby_class, rb_to_id(rb_eval(self, node->u1.node)), rb_to_id(rb_eval(self, node->u2.node))); result = Qnil; break; case NODE_VALIAS: rb_alias_variable(node->u1.id, node->u2.id); result = Qnil; break; case NODE_CLASS: { VALUE super, klass, tmp, cbase; ID cname; int gen = Qfalse; cbase = class_prefix(self, node->nd_cpath); cname = node->nd_cpath->nd_mid; if (NIL_P(ruby_cbase)) { rb_raise(rb_eTypeError, "no outer class/module"); } if (node->nd_super) { super = rb_eval(self, node->nd_super); rb_check_inheritable(super); } else { super = 0; } if (rb_const_defined_at(cbase, cname)) { klass = rb_const_get_at(cbase, cname); if (TYPE(klass) != T_CLASS) { rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(cname)); } if (super) { tmp = rb_class_real(RCLASS(klass)->super); if (tmp != super) { rb_raise(rb_eTypeError, "superclass mismatch for class %s", rb_id2name(cname)); } super = 0; } if (ruby_safe_level >= 4) { rb_raise(rb_eSecurityError, "extending class prohibited"); } } else { if (!super) super = rb_cObject; klass = rb_define_class_id(cname, super); rb_set_class_path(klass, cbase, rb_id2name(cname)); rb_const_set(cbase, cname, klass); gen = Qtrue; } if (ruby_wrapper) { rb_extend_object(klass, ruby_wrapper); rb_include_module(klass, ruby_wrapper); } if (super && gen) { rb_class_inherited(super, klass); } result = module_setup(klass, node); } break; case NODE_MODULE: { VALUE module, cbase; ID cname; if (NIL_P(ruby_cbase)) { rb_raise(rb_eTypeError, "no outer class/module"); } cbase = class_prefix(self, node->nd_cpath); cname = node->nd_cpath->nd_mid; if (rb_const_defined_at(cbase, cname)) { module = rb_const_get_at(cbase, cname); if (TYPE(module) != T_MODULE) { rb_raise(rb_eTypeError, "%s is not a module", rb_id2name(cname)); } if (ruby_safe_level >= 4) { rb_raise(rb_eSecurityError, "extending module prohibited"); } } else { module = rb_define_module_id(cname); rb_set_class_path(module, cbase, rb_id2name(cname)); rb_const_set(cbase, cname, module); } if (ruby_wrapper) { rb_extend_object(module, ruby_wrapper); rb_include_module(module, ruby_wrapper); } result = module_setup(module, node); } break; case NODE_SCLASS: { VALUE klass; result = rb_eval(self, node->nd_recv); if (FIXNUM_P(result) || SYMBOL_P(result)) { rb_raise(rb_eTypeError, "no virtual class for %s", rb_obj_classname(result)); } if (ruby_safe_level >= 4 && !OBJ_TAINTED(result)) rb_raise(rb_eSecurityError, "Insecure: can't extend object"); klass = rb_singleton_class(result); if (ruby_wrapper) { rb_extend_object(klass, ruby_wrapper); rb_include_module(klass, ruby_wrapper); } result = module_setup(klass, node); } 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 = Qnil; } break; case NODE_NEWLINE: #ifdef ENABLE_DTRACE if (RUBY_LINE_ENABLED()) if (ruby_current_node && ruby_current_node->nd_file) RUBY_LINE(ruby_current_node->nd_file, nd_line(ruby_current_node)); #endif EXEC_EVENT_HOOK(RUBY_EVENT_LINE, node, self, ruby_frame->last_func, ruby_frame->last_class); node = node->nd_next; goto again; default: unknown_node(node); } finish: CHECK_INTS; if (contnode) { node = contnode; contnode = 0; goto again; } return result; } static VALUE module_setup(module, n) VALUE module; NODE *n; { NODE * volatile node = n->nd_body; int state; struct FRAME frame; VALUE result = Qnil; /* OK */ TMP_PROTECT; frame = *ruby_frame; frame.tmp = ruby_frame; ruby_frame = &frame; PUSH_CLASS(module); PUSH_SCOPE(); PUSH_VARS(); 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; } PUSH_CREF(module); PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { EXEC_EVENT_HOOK(RUBY_EVENT_CLASS, n, ruby_cbase, ruby_frame->last_func, ruby_frame->last_class); result = rb_eval(ruby_cbase, node->nd_next); } POP_TAG(); POP_CREF(); POP_VARS(); POP_SCOPE(); POP_CLASS(); ruby_frame = frame.tmp; EXEC_EVENT_HOOK(RUBY_EVENT_END, n, 0, ruby_frame->last_func, ruby_frame->last_class); if (state) JUMP_TAG(state); return result; } static NODE *basic_respond_to = 0; int rb_obj_respond_to(obj, id, priv) VALUE obj; ID id; int priv; { VALUE klass = CLASS_OF(obj); if (rb_method_node(klass, respond_to) == basic_respond_to) { return rb_method_boundp(klass, id, !priv); } else { VALUE args[2]; int n = 0; args[n++] = ID2SYM(id); if (priv) args[n++] = Qtrue; return rb_funcall2(obj, respond_to, n, args); } } int rb_respond_to(obj, id) VALUE obj; ID id; { return rb_obj_respond_to(obj, id, Qfalse); } /* * call-seq: * obj.respond_to?(symbol, include_private=false) => true or false * * Returns +true+> if _obj_ responds to the given * method. Private methods are included in the search only if the * optional second parameter evaluates to +true+. */ static VALUE 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; } /* * call-seq: * mod.method_defined?(symbol) => true or false * * Returns +true+ if the named method is defined by * _mod_ (or its included modules and, if _mod_ is a class, * its ancestors). Public and protected methods are matched. * * module A * def method1() end * end * class B * def method2() end * end * class C < B * include A * def method3() end * end * * A.method_defined? :method1 #=> true * C.method_defined? "method1" #=> true * C.method_defined? "method2" #=> true * C.method_defined? "method3" #=> true * C.method_defined? "method4" #=> false */ static VALUE rb_mod_method_defined(mod, mid) VALUE mod, mid; { return rb_method_boundp(mod, rb_to_id(mid), 1); } #define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f)) /* * call-seq: * mod.public_method_defined?(symbol) => true or false * * Returns +true+ if the named public method is defined by * _mod_ (or its included modules and, if _mod_ is a class, * its ancestors). * * module A * def method1() end * end * class B * protected * def method2() end * end * class C < B * include A * def method3() end * end * * A.method_defined? :method1 #=> true * C.public_method_defined? "method1" #=> true * C.public_method_defined? "method2" #=> false * C.method_defined? "method2" #=> true */ static VALUE rb_mod_public_method_defined(mod, mid) VALUE mod, mid; { ID id = rb_to_id(mid); int noex; if (rb_get_method_body(&mod, &id, &noex)) { if (VISI_CHECK(noex, NOEX_PUBLIC)) return Qtrue; } return Qfalse; } /* * call-seq: * mod.private_method_defined?(symbol) => true or false * * Returns +true+ if the named private method is defined by * _ mod_ (or its included modules and, if _mod_ is a class, * its ancestors). * * module A * def method1() end * end * class B * private * def method2() end * end * class C < B * include A * def method3() end * end * * A.method_defined? :method1 #=> true * C.private_method_defined? "method1" #=> false * C.private_method_defined? "method2" #=> true * C.method_defined? "method2" #=> false */ static VALUE rb_mod_private_method_defined(mod, mid) VALUE mod, mid; { ID id = rb_to_id(mid); int noex; if (rb_get_method_body(&mod, &id, &noex)) { if (VISI_CHECK(noex, NOEX_PRIVATE)) return Qtrue; } return Qfalse; } /* * call-seq: * mod.protected_method_defined?(symbol) => true or false * * Returns +true+ if the named protected method is defined * by _mod_ (or its included modules and, if _mod_ is a * class, its ancestors). * * module A * def method1() end * end * class B * protected * def method2() end * end * class C < B * include A * def method3() end * end * * A.method_defined? :method1 #=> true * C.protected_method_defined? "method1" #=> false * C.protected_method_defined? "method2" #=> true * C.method_defined? "method2" #=> true */ static VALUE rb_mod_protected_method_defined(mod, mid) VALUE mod, mid; { ID id = rb_to_id(mid); int noex; if (rb_get_method_body(&mod, &id, &noex)) { if (VISI_CHECK(noex, NOEX_PROTECTED)) return Qtrue; } return Qfalse; } NORETURN(static VALUE terminate_process _((int, VALUE))); static VALUE terminate_process(status, mesg) int status; VALUE mesg; { VALUE args[2]; args[0] = INT2NUM(status); args[1] = mesg; rb_exc_raise(rb_class_new_instance(2, args, rb_eSystemExit)); } void rb_exit(status) int status; { if (prot_tag) { terminate_process(status, rb_str_new("exit", 4)); } ruby_finalize(); exit(status); } /* * call-seq: * exit(integer=0) * Kernel::exit(integer=0) * Process::exit(integer=0) * * Initiates the termination of the Ruby script by raising the * SystemExit exception. This exception may be caught. The * optional parameter is used to return a status code to the invoking * environment. * * begin * exit * puts "never get here" * rescue SystemExit * puts "rescued a SystemExit exception" * end * puts "after begin block" * * produces: * * rescued a SystemExit exception * after begin block * * Just prior to termination, Ruby executes any at_exit functions * (see Kernel::at_exit) and runs any object finalizers (see * ObjectSpace::define_finalizer). * * at_exit { puts "at_exit function" } * ObjectSpace.define_finalizer("string", proc { puts "in finalizer" }) * exit * * produces: * * at_exit function * in finalizer */ VALUE rb_f_exit(argc, argv) int argc; VALUE *argv; { VALUE status; int istatus; rb_secure(4); if (rb_scan_args(argc, argv, "01", &status) == 1) { switch (status) { case Qtrue: istatus = EXIT_SUCCESS; break; case Qfalse: istatus = EXIT_FAILURE; break; default: istatus = NUM2INT(status); #if EXIT_SUCCESS != 0 if (istatus == 0) istatus = EXIT_SUCCESS; #endif break; } } else { istatus = EXIT_SUCCESS; } rb_exit(istatus); return Qnil; /* not reached */ } /* * call-seq: * abort * Kernel::abort * Process::abort * * Terminate execution immediately, effectively by calling * Kernel.exit(1). If _msg_ is given, it is written * to STDERR prior to terminating. */ VALUE rb_f_abort(argc, argv) int argc; VALUE *argv; { rb_secure(4); if (argc == 0) { if (!NIL_P(ruby_errinfo)) { error_print(); } rb_exit(EXIT_FAILURE); } else { VALUE mesg; rb_scan_args(argc, argv, "1", &mesg); StringValue(mesg); rb_io_puts(1, &mesg, rb_stderr); terminate_process(EXIT_FAILURE, mesg); } return Qnil; /* not reached */ } void rb_iter_break() { break_jump(Qnil); } NORETURN(static void rb_longjmp _((int, VALUE))); static VALUE make_backtrace _((void)); static void rb_longjmp(tag, mesg) int tag; VALUE mesg; { VALUE at; if (thread_set_raised()) { ruby_errinfo = exception_error; JUMP_TAG(TAG_FATAL); } if (NIL_P(mesg)) mesg = ruby_errinfo; if (NIL_P(mesg)) { mesg = rb_exc_new(rb_eRuntimeError, 0, 0); } ruby_set_current_source(); 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)) { ruby_errinfo = mesg; } if (RTEST(ruby_debug) && !NIL_P(ruby_errinfo) && !rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) { VALUE e = ruby_errinfo; int status; PUSH_TAG(PROT_NONE); if ((status = EXEC_TAG()) == 0) { StringValue(e); warn_printf("Exception `%s' at %s:%d - %s\n", rb_obj_classname(ruby_errinfo), ruby_sourcefile, ruby_sourceline, RSTRING(e)->ptr); } POP_TAG(); if (status == TAG_FATAL && ruby_errinfo == exception_error) { ruby_errinfo = mesg; } else if (status) { thread_reset_raised(); JUMP_TAG(status); } } rb_trap_restore_mask(); if (tag != TAG_FATAL) { #ifdef ENABLE_DTRACE if (RUBY_RAISE_ENABLED()) RUBY_RAISE(rb_obj_classname(ruby_errinfo), ruby_sourcefile, ruby_sourceline); #endif EXEC_EVENT_HOOK(RUBY_EVENT_RAISE, ruby_current_node, ruby_frame->self, ruby_frame->last_func, ruby_frame->last_class); } if (!prot_tag) { error_print(); } thread_reset_raised(); JUMP_TAG(tag); } void rb_exc_raise(mesg) VALUE mesg; { rb_longjmp(TAG_RAISE, mesg); } void rb_exc_fatal(mesg) VALUE mesg; { rb_longjmp(TAG_FATAL, mesg); } void rb_interrupt() { rb_raise(rb_eInterrupt, ""); } /* * call-seq: * raise * raise(string) * raise(exception [, string [, array]]) * fail * fail(string) * fail(exception [, string [, array]]) * * With no arguments, raises the exception in $! or raises * a RuntimeError if $! is +nil+. * With a single +String+ argument, raises a * +RuntimeError+ with the string as a message. Otherwise, * the first parameter should be the name of an +Exception+ * class (or an object that returns an +Exception+ object when sent * an +exception+ message). The optional second parameter sets the * message associated with the exception, and the third parameter is an * array of callback information. Exceptions are caught by the * +rescue+ clause of begin...end blocks. * * raise "Failed to create socket" * raise ArgumentError, "No parameters", caller */ static VALUE rb_f_raise(argc, argv) int argc; VALUE *argv; { rb_raise_jump(rb_make_exception(argc, argv)); return Qnil; /* not reached */ } static VALUE rb_make_exception(argc, argv) int argc; VALUE *argv; { VALUE mesg; ID exception; 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; } n = 0; goto exception_call; case 2: case 3: n = 1; exception_call: exception = rb_intern("exception"); if (!rb_respond_to(argv[0], exception)) { rb_raise(rb_eTypeError, "exception class/object expected"); } mesg = rb_funcall(argv[0], exception, n, argv[1]); break; default: rb_raise(rb_eArgError, "wrong number of arguments"); break; } if (argc > 0) { if (!rb_obj_is_kind_of(mesg, rb_eException)) rb_raise(rb_eTypeError, "exception object expected"); if (argc>2) set_backtrace(mesg, argv[2]); } return mesg; } static void rb_raise_jump(mesg) VALUE mesg; { if (ruby_frame != top_frame) { PUSH_FRAME(); /* fake frame */ *ruby_frame = *_frame.prev->prev; rb_longjmp(TAG_RAISE, mesg); POP_FRAME(); } rb_longjmp(TAG_RAISE, mesg); } void rb_jump_tag(tag) int tag; { JUMP_TAG(tag); } int rb_block_given_p() { if (ruby_frame->iter == ITER_CUR && ruby_block) return Qtrue; return Qfalse; } int rb_iterator_p() { return rb_block_given_p(); } /* * call-seq: * block_given? => true or false * iterator? => true or false * * Returns true if yield would execute a * block in the current context. The iterator? form * is mildly deprecated. * * def try * if block_given? * yield * else * "no block" * end * end * try #=> "no block" * try { "hello" } #=> "hello" * try do "hello" end #=> "hello" */ static VALUE rb_f_block_given_p() { if (ruby_frame->prev && ruby_frame->prev->iter == ITER_CUR && ruby_block) return Qtrue; return Qfalse; } static VALUE rb_eThreadError; NORETURN(static void proc_jump_error(int, VALUE)); static void proc_jump_error(state, result) int state; VALUE result; { char mesg[32]; char *statement; switch (state) { case TAG_BREAK: statement = "break"; break; case TAG_RETURN: statement = "return"; break; case TAG_RETRY: statement = "retry"; break; default: statement = "local-jump"; break; /* should not happen */ } snprintf(mesg, sizeof mesg, "%s from proc-closure", statement); localjump_error(mesg, result, state); } static void return_jump(retval) VALUE retval; { struct tag *tt = prot_tag; int yield = Qfalse; if (retval == Qundef) retval = Qnil; while (tt) { if (tt->tag == PROT_YIELD) { yield = Qtrue; tt = tt->prev; } if (tt->tag == PROT_FUNC && tt->frame->uniq == ruby_frame->uniq) { tt->dst = (VALUE)ruby_frame->uniq; tt->retval = retval; JUMP_TAG(TAG_RETURN); } if (tt->tag == PROT_LAMBDA && !yield) { tt->dst = (VALUE)tt->frame->uniq; tt->retval = retval; JUMP_TAG(TAG_RETURN); } if (tt->tag == PROT_THREAD) { rb_raise(rb_eThreadError, "return can't jump across threads"); } tt = tt->prev; } localjump_error("unexpected return", retval, TAG_RETURN); } static void break_jump(retval) VALUE retval; { struct tag *tt = prot_tag; if (retval == Qundef) retval = Qnil; while (tt) { switch (tt->tag) { case PROT_THREAD: case PROT_YIELD: case PROT_LOOP: case PROT_LAMBDA: tt->dst = (VALUE)tt->frame->uniq; tt->retval = retval; JUMP_TAG(TAG_BREAK); break; case PROT_FUNC: tt = 0; continue; default: break; } tt = tt->prev; } localjump_error("unexpected break", retval, TAG_BREAK); } static void next_jump(retval) VALUE retval; { struct tag *tt = prot_tag; if (retval == Qundef) retval = Qnil; while (tt) { switch (tt->tag) { case PROT_THREAD: case PROT_YIELD: case PROT_LOOP: case PROT_LAMBDA: case PROT_FUNC: tt->dst = (VALUE)tt->frame->uniq; tt->retval = retval; JUMP_TAG(TAG_NEXT); break; default: break; } tt = tt->prev; } localjump_error("unexpected next", retval, TAG_NEXT); } void rb_need_block() { if (!rb_block_given_p()) { localjump_error("no block given", Qnil, 0); } } static VALUE rb_yield_0(val, self, klass, flags, avalue) VALUE val, self, klass; /* OK */ int flags, avalue; { NODE *node; volatile VALUE result = Qnil; volatile VALUE old_cref; volatile VALUE old_wrapper; struct BLOCK * volatile block; struct SCOPE * volatile old_scope; int old_vmode; struct FRAME frame; NODE *cnode = ruby_current_node; int lambda = flags & YIELD_LAMBDA_CALL; int state; rb_need_block(); PUSH_VARS(); block = ruby_block; frame = block->frame; frame.prev = ruby_frame; frame.node = cnode; ruby_frame = &(frame); old_cref = (VALUE)ruby_cref; ruby_cref = block->cref; old_wrapper = ruby_wrapper; ruby_wrapper = block->wrapper; old_scope = ruby_scope; ruby_scope = block->scope; old_vmode = scope_vmode; scope_vmode = (flags & YIELD_PUBLIC_DEF) ? SCOPE_PUBLIC : block->vmode; ruby_block = block->prev; if (block->flags & BLOCK_D_SCOPE) { /* put place holder for dynamic (in-block) local variables */ ruby_dyna_vars = new_dvar(0, 0, block->dyna_vars); } else { /* FOR does not introduce new scope */ ruby_dyna_vars = block->dyna_vars; } PUSH_CLASS(klass ? klass : block->klass); if (!klass) { self = block->self; } node = block->body; if (block->var) { PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { if (block->var == (NODE*)1) { /* no parameter || */ if (lambda && RARRAY(val)->len != 0) { rb_raise(rb_eArgError, "wrong number of arguments (%ld for 0)", RARRAY(val)->len); } } else if (block->var == (NODE*)2) { if (TYPE(val) == T_ARRAY && RARRAY(val)->len != 0) { rb_raise(rb_eArgError, "wrong number of arguments (%ld for 0)", RARRAY(val)->len); } } else if (nd_type(block->var) == NODE_MASGN) { if (!avalue) { val = svalue_to_mrhs(val, block->var->nd_head); } massign(self, block->var, val, lambda); } else { int len = 0; if (avalue) { len = RARRAY(val)->len; if (len == 0) { goto zero_arg; } if (len == 1) { val = RARRAY(val)->ptr[0]; } else { goto multi_values; } } else if (val == Qundef) { zero_arg: val = Qnil; multi_values: { ruby_current_node = block->var; rb_warn("multiple values for a block parameter (%d for 1)\n\tfrom %s:%d", len, cnode->nd_file, nd_line(cnode)); ruby_current_node = cnode; } } assign(self, block->var, val, lambda); } } POP_TAG(); if (state) goto pop_state; } if (!node) { state = 0; goto pop_state; } ruby_current_node = node; PUSH_ITER(block->iter); PUSH_TAG(lambda ? PROT_NONE : PROT_YIELD); if ((state = EXEC_TAG()) == 0) { redo: if (nd_type(node) == NODE_CFUNC || nd_type(node) == NODE_IFUNC) { if (node->nd_state == YIELD_FUNC_AVALUE) { if (!avalue) { val = svalue_to_avalue(val); } } else { if (avalue) { val = avalue_to_svalue(val); } if (val == Qundef && node->nd_state != YIELD_FUNC_SVALUE) val = Qnil; } result = (*node->nd_cfnc)(val, node->nd_tval, self); } else { result = rb_eval(self, node); } } else { switch (state) { case TAG_REDO: state = 0; CHECK_INTS; goto redo; case TAG_NEXT: state = 0; result = prot_tag->retval; break; case TAG_BREAK: if (TAG_DST()) { result = prot_tag->retval; } else { lambda = Qtrue; /* just pass TAG_BREAK */ } break; default: break; } } POP_TAG(); POP_ITER(); pop_state: POP_CLASS(); if (ruby_dyna_vars && (block->flags & BLOCK_D_SCOPE) && !FL_TEST(ruby_dyna_vars, DVAR_DONT_RECYCLE)) { struct RVarmap *vars = ruby_dyna_vars; if (ruby_dyna_vars->id == 0) { vars = ruby_dyna_vars->next; rb_gc_force_recycle((VALUE)ruby_dyna_vars); while (vars && vars->id != 0 && vars != block->dyna_vars) { struct RVarmap *tmp = vars->next; rb_gc_force_recycle((VALUE)vars); vars = tmp; } } } POP_VARS(); ruby_block = block; ruby_frame = ruby_frame->prev; ruby_cref = (NODE*)old_cref; ruby_wrapper = old_wrapper; if (ruby_scope->flags & SCOPE_DONT_RECYCLE) scope_dup(old_scope); ruby_scope = old_scope; scope_vmode = old_vmode; switch (state) { case 0: break; case TAG_BREAK: if (!lambda) { struct tag *tt = prot_tag; while (tt) { if (tt->tag == PROT_LOOP && tt->blkid == ruby_block->uniq) { tt->dst = (VALUE)tt->frame->uniq; tt->retval = result; JUMP_TAG(TAG_BREAK); } tt = tt->prev; } proc_jump_error(TAG_BREAK, result); } /* fall through */ default: JUMP_TAG(state); break; } ruby_current_node = cnode; return result; } VALUE rb_yield(val) VALUE val; { return rb_yield_0(val, 0, 0, 0, Qfalse); } VALUE #ifdef HAVE_STDARG_PROTOTYPES rb_yield_values(int n, ...) #else rb_yield_values(n, va_alist) int n; va_dcl #endif { va_list args; VALUE ary; if (n == 0) { return rb_yield_0(Qundef, 0, 0, 0, Qfalse); } ary = rb_ary_new2(n); va_init_list(args, n); while (n--) { rb_ary_push(ary, va_arg(args, VALUE)); } va_end(args); return rb_yield_0(ary, 0, 0, 0, Qtrue); } VALUE rb_yield_splat(values) VALUE values; { int avalue = Qfalse; if (TYPE(values) == T_ARRAY) { if (RARRAY(values)->len == 0) { values = Qundef; } else { avalue = Qtrue; } } return rb_yield_0(values, 0, 0, 0, avalue); } /* * call-seq: * loop {|| block } * * Repeatedly executes the block. * * loop do * print "Input: " * line = gets * break if !line or line =~ /^qQ/ * # ... * end */ static VALUE rb_f_loop() { for (;;) { rb_yield_0(Qundef, 0, 0, 0, Qfalse); CHECK_INTS; } return Qnil; /* dummy */ } static VALUE massign(self, node, val, pcall) VALUE self; NODE *node; VALUE val; int pcall; { NODE *list; long i = 0, len; len = RARRAY(val)->len; list = node->nd_head; for (; list && ind_head, RARRAY(val)->ptr[i], pcall); list = list->nd_next; } if (pcall && list) goto arg_error; if (node->nd_args) { if ((long)(node->nd_args) == -1) { /* no check for mere `*' */ } else if (!list && ind_args, rb_ary_new4(len-i, RARRAY(val)->ptr+i), pcall); } else { assign(self, node->nd_args, rb_ary_new2(0), pcall); } } else if (pcall && i < len) { goto arg_error; } while (list) { i++; assign(self, list->nd_head, Qnil, pcall); list = list->nd_next; } return val; arg_error: while (list) { i++; list = list->nd_next; } rb_raise(rb_eArgError, "wrong number of arguments (%ld for %ld)", len, i); } static void assign(self, lhs, val, pcall) VALUE self; NODE *lhs; VALUE val; int pcall; { ruby_current_node = lhs; if (val == Qundef) { rb_warning("assigning void value"); val = Qnil; } 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 local variable assignment"); ruby_scope->local_vars[lhs->nd_cnt] = val; break; case NODE_DASGN: dvar_asgn(lhs->nd_vid, val); break; case NODE_DASGN_CURR: dvar_asgn_curr(lhs->nd_vid, val); break; case NODE_CDECL: if (lhs->nd_vid == 0) { rb_const_set(class_prefix(self, lhs->nd_else), lhs->nd_else->nd_mid, val); } else { rb_const_set(ruby_cbase, lhs->nd_vid, val); } break; case NODE_CVDECL: if (RTEST(ruby_verbose) && FL_TEST(ruby_cbase, FL_SINGLETON)) { rb_warn("declaring singleton class variable"); } rb_cvar_set(cvar_cbase(), lhs->nd_vid, val, Qtrue); break; case NODE_CVASGN: rb_cvar_set(cvar_cbase(), lhs->nd_vid, val, Qfalse); break; case NODE_MASGN: massign(self, lhs, svalue_to_mrhs(val, lhs->nd_head), pcall); break; case NODE_CALL: case NODE_ATTRASGN: { VALUE recv; int scope; if (lhs->nd_recv == (NODE *)1) { recv = self; scope = 1; } else { recv = rb_eval(self, lhs->nd_recv); scope = 0; } if (!lhs->nd_args) { /* attr set */ ruby_current_node = lhs; SET_CURRENT_SOURCE(); rb_call(CLASS_OF(recv), recv, lhs->nd_mid, 1, &val, scope); } else { /* array set */ VALUE args; args = rb_eval(self, lhs->nd_args); rb_ary_push(args, val); ruby_current_node = lhs; SET_CURRENT_SOURCE(); rb_call(CLASS_OF(recv), recv, lhs->nd_mid, RARRAY(args)->len, RARRAY(args)->ptr, scope); } } break; default: rb_bug("bug in variable assignment"); break; } } VALUE rb_iterate(it_proc, data1, bl_proc, data2) VALUE (*it_proc) _((VALUE)), (*bl_proc)(ANYARGS); VALUE data1, data2; { int state; volatile VALUE retval = Qnil; NODE *node = NEW_IFUNC(bl_proc, data2); VALUE self = ruby_top_self; PUSH_ITER(ITER_PRE); PUSH_TAG(PROT_LOOP); PUSH_BLOCK(0, node); state = EXEC_TAG(); if (state == 0) { iter_retry: retval = (*it_proc)(data1); } else if (state == TAG_BREAK && TAG_DST()) { retval = prot_tag->retval; state = 0; } else if (state == TAG_RETRY) { state = 0; goto iter_retry; } POP_BLOCK(); POP_TAG(); POP_ITER(); switch (state) { case 0: break; default: JUMP_TAG(state); } return retval; } 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(ruby_errinfo, rb_eStandardError); } 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 (RTEST(rb_funcall(*argv, eqq, 1, ruby_errinfo))) return 1; argv++; } return 0; } VALUE #ifdef HAVE_STDARG_PROTOTYPES rb_rescue2(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*r_proc)(ANYARGS), VALUE data2, ...) #else rb_rescue2(b_proc, data1, r_proc, data2, va_alist) VALUE (*b_proc)(ANYARGS), (*r_proc)(ANYARGS); VALUE data1, data2; va_dcl #endif { int state; volatile VALUE result; volatile VALUE e_info = ruby_errinfo; volatile int handle = Qfalse; VALUE eclass; va_list args; PUSH_TAG(PROT_NONE); switch (state = EXEC_TAG()) { case TAG_RETRY: if (!handle) break; handle = Qfalse; state = 0; ruby_errinfo = Qnil; case 0: result = (*b_proc)(data1); break; case TAG_RAISE: if (handle) break; handle = Qfalse; va_init_list(args, data2); while (eclass = va_arg(args, VALUE)) { if (rb_obj_is_kind_of(ruby_errinfo, eclass)) { handle = Qtrue; break; } } va_end(args); if (handle) { state = 0; if (r_proc) { result = (*r_proc)(data2, ruby_errinfo); } else { result = Qnil; } ruby_errinfo = e_info; } } POP_TAG(); if (state) JUMP_TAG(state); return result; } VALUE rb_rescue(b_proc, data1, r_proc, data2) VALUE (*b_proc)(), (*r_proc)(); VALUE data1, data2; { return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError, (VALUE)0); } static VALUE cont_protect; VALUE rb_protect(proc, data, state) VALUE (*proc) _((VALUE)); VALUE data; int *state; { VALUE result = Qnil; /* OK */ int status; PUSH_TAG(PROT_NONE); cont_protect = (VALUE)rb_node_newnode(NODE_MEMO, cont_protect, 0, 0); if ((status = EXEC_TAG()) == 0) { result = (*proc)(data); } cont_protect = ((NODE *)cont_protect)->u1.value; POP_TAG(); if (state) { *state = status; } if (status != 0) { return Qnil; } return result; } VALUE rb_ensure(b_proc, data1, e_proc, data2) VALUE (*b_proc)(); VALUE data1; VALUE (*e_proc)(); VALUE data2; { int state; volatile VALUE result = Qnil; VALUE retval; PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { result = (*b_proc)(data1); } POP_TAG(); retval = prot_tag ? prot_tag->retval : Qnil; /* save retval */ (*e_proc)(data2); if (prot_tag) return_value(retval); if (state) JUMP_TAG(state); return result; } VALUE rb_with_disable_interrupt(proc, data) VALUE (*proc)(); VALUE data; { VALUE result = Qnil; /* OK */ int status; DEFER_INTS; { int thr_critical = rb_thread_critical; rb_thread_critical = Qtrue; PUSH_TAG(PROT_NONE); if ((status = EXEC_TAG()) == 0) { result = (*proc)(data); } POP_TAG(); rb_thread_critical = thr_critical; } ENABLE_INTS; if (status) JUMP_TAG(status); return result; } static inline void stack_check() { static int overflowing = 0; if (!overflowing && ruby_stack_check()) { int state; overflowing = 1; PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { rb_exc_raise(sysstack_error); } POP_TAG(); overflowing = 0; JUMP_TAG(state); } } static int last_call_status; #define CSTAT_PRIV 1 #define CSTAT_PROT 2 #define CSTAT_VCALL 4 #define CSTAT_SUPER 8 /* * call-seq: * obj.method_missing(symbol [, *args] ) => result * * Invoked by Ruby when obj is sent a message it cannot handle. * symbol is the symbol for the method called, and args * are any arguments that were passed to it. By default, the interpreter * raises an error when this method is called. However, it is possible * to override the method to provide more dynamic behavior. * The example below creates * a class Roman, which responds to methods with names * consisting of roman numerals, returning the corresponding integer * values. * * class Roman * def romanToInt(str) * # ... * end * def method_missing(methId) * str = methId.id2name * romanToInt(str) * end * end * * r = Roman.new * r.iv #=> 4 * r.xxiii #=> 23 * r.mm #=> 2000 */ static VALUE rb_method_missing(argc, argv, obj) int argc; VALUE *argv; VALUE obj; { ID id; VALUE exc = rb_eNoMethodError; char *format = 0; NODE *cnode = ruby_current_node; if (argc == 0 || !SYMBOL_P(argv[0])) { rb_raise(rb_eArgError, "no id given"); } stack_check(); id = SYM2ID(argv[0]); if (last_call_status & CSTAT_PRIV) { format = "private method `%s' called for %s"; } else if (last_call_status & CSTAT_PROT) { format = "protected method `%s' called for %s"; } else if (last_call_status & CSTAT_VCALL) { format = "undefined local variable or method `%s' for %s"; exc = rb_eNameError; } else if (last_call_status & CSTAT_SUPER) { format = "super: no superclass method `%s'"; } if (!format) { format = "undefined method `%s' for %s"; } ruby_current_node = cnode; { int n = 0; VALUE args[3]; args[n++] = rb_funcall(rb_const_get(exc, rb_intern("message")), '!', 3, rb_str_new2(format), obj, argv[0]); args[n++] = argv[0]; if (exc == rb_eNoMethodError) { args[n++] = rb_ary_new4(argc-1, argv+1); } exc = rb_class_new_instance(n, args, exc); ruby_frame = ruby_frame->prev; /* pop frame for "method_missing" */ rb_exc_raise(exc); } return Qnil; /* not reached */ } static VALUE method_missing(obj, id, argc, argv, call_status) VALUE obj; ID id; int argc; const VALUE *argv; int call_status; { VALUE *nargv; last_call_status = call_status; if (id == missing) { PUSH_FRAME(); rb_method_missing(argc, argv, obj); POP_FRAME(); } else if (id == ID_ALLOCATOR) { rb_raise(rb_eTypeError, "allocator undefined for %s", rb_class2name(obj)); } if (argc < 0) { VALUE tmp; argc = -argc-1; tmp = splat_value(argv[argc]); nargv = ALLOCA_N(VALUE, argc + RARRAY(tmp)->len + 1); MEMCPY(nargv+1, argv, VALUE, argc); MEMCPY(nargv+1+argc, RARRAY(tmp)->ptr, VALUE, RARRAY(tmp)->len); argc += RARRAY(tmp)->len; } else { nargv = ALLOCA_N(VALUE, argc+1); MEMCPY(nargv+1, argv, VALUE, argc); } nargv[0] = ID2SYM(id); return rb_funcall2(obj, missing, argc+1, nargv); } static inline 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 number of arguments (%d for %d)", argc, len); } switch (len) { case -2: return (*func)(recv, rb_ary_new4(argc, argv)); break; case -1: return (*func)(argc, argv, recv); break; case 0: return (*func)(recv); break; case 1: return (*func)(recv, argv[0]); break; 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; default: rb_raise(rb_eArgError, "too many arguments (%d)", len); break; } return Qnil; /* not reached */ } static VALUE rb_call0(klass, recv, id, oid, argc, argv, body, flags) VALUE klass, recv; ID id; ID oid; int argc; /* OK */ VALUE *argv; /* OK */ NODE * volatile body; int flags; { NODE *b2; /* OK */ volatile VALUE result = Qnil; int itr; static int tick; TMP_PROTECT; volatile int safe = -1; if (NOEX_SAFE(flags) > ruby_safe_level && !(flags&NOEX_TAINTED) && ruby_safe_level == 0 && NOEX_SAFE(flags) > 2) { rb_raise(rb_eSecurityError, "calling insecure method: %s", rb_id2name(id)); } switch (ruby_iter->iter) { case ITER_PRE: case ITER_PAS: itr = ITER_CUR; break; case ITER_CUR: default: itr = ITER_NOT; break; } if ((++tick & 0xff) == 0) { CHECK_INTS; /* better than nothing */ stack_check(); rb_gc_finalize_deferred(); } if (argc < 0) { VALUE tmp; VALUE *nargv; argc = -argc-1; tmp = splat_value(argv[argc]); nargv = TMP_ALLOC(argc + RARRAY(tmp)->len); MEMCPY(nargv, argv, VALUE, argc); MEMCPY(nargv+argc, RARRAY(tmp)->ptr, VALUE, RARRAY(tmp)->len); argc += RARRAY(tmp)->len; argv = nargv; } PUSH_ITER(itr); PUSH_FRAME(); ruby_frame->last_func = id; ruby_frame->orig_func = oid; ruby_frame->last_class = (flags & NOEX_NOSUPER)?0:klass; ruby_frame->self = recv; ruby_frame->argc = argc; ruby_frame->flags = 0; 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)); } #ifdef ENABLE_DTRACE if (RUBY_FUNCTION_ENTRY_ENABLED()) { char *classname = rb_class2name(klass), *methodname = rb_id2name(id); if (ruby_current_node && ruby_current_node->nd_file && classname && methodname) RUBY_FUNCTION_ENTRY(classname, methodname, ruby_current_node->nd_file, nd_line(ruby_current_node)); } #endif if (event_hooks) { int state; EXEC_EVENT_HOOK(RUBY_EVENT_C_CALL, ruby_current_node, recv, id, klass); PUSH_TAG(PROT_FUNC); if ((state = EXEC_TAG()) == 0) { result = call_cfunc(body->nd_cfnc, recv, len, argc, argv); } POP_TAG(); ruby_current_node = ruby_frame->node; EXEC_EVENT_HOOK(RUBY_EVENT_C_RETURN, ruby_current_node, recv, id, klass); if (state) JUMP_TAG(state); } else { result = call_cfunc(body->nd_cfnc, recv, len, argc, argv); } #ifdef ENABLE_DTRACE if (RUBY_FUNCTION_RETURN_ENABLED()) { char *classname = rb_class2name(klass), *methodname = rb_id2name(id); if (ruby_current_node && ruby_current_node->nd_file && classname && methodname) RUBY_FUNCTION_RETURN(classname, methodname, ruby_current_node->nd_file, nd_line(ruby_current_node)); } #endif } break; /* for attr get/set */ case NODE_IVAR: if (argc != 0) { rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); } result = rb_attr_get(recv, body->nd_vid); break; case NODE_ATTRSET: if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); result = rb_ivar_set(recv, body->nd_vid, argv[0]); break; case NODE_ZSUPER: result = rb_call_super(argc, argv); break; case NODE_DMETHOD: result = method_call(argc, argv, umethod_bind(body->nd_cval, recv)); break; case NODE_BMETHOD: ruby_frame->flags |= FRAME_DMETH; if (event_hooks) { struct BLOCK *data; Data_Get_Struct(body->nd_cval, struct BLOCK, data); EXEC_EVENT_HOOK(RUBY_EVENT_CALL, data->body, recv, id, klass); } result = proc_invoke(body->nd_cval, rb_ary_new4(argc, argv), recv, klass); if (event_hooks) { EXEC_EVENT_HOOK(RUBY_EVENT_RETURN, body, recv, id, klass); } break; case NODE_SCOPE: { int state; VALUE *local_vars; /* OK */ NODE *saved_cref = 0; PUSH_SCOPE(); if (body->nd_rval) { saved_cref = ruby_cref; ruby_cref = (NODE*)body->nd_rval; } PUSH_CLASS(ruby_cbase); 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; if (NOEX_SAFE(flags) > ruby_safe_level) { safe = ruby_safe_level; ruby_safe_level = NOEX_SAFE(flags); } PUSH_VARS(); PUSH_TAG(PROT_FUNC); if ((state = EXEC_TAG()) == 0) { NODE *node = 0; int i, nopt = 0; 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 number of arguments (%d for %d)", argc, i); } if (!node->nd_rest) { NODE *optnode = node->nd_opt; nopt = i; while (optnode) { nopt++; optnode = optnode->nd_next; } if (nopt < argc) {