Ruby  2.0.0p576(2014-09-19revision47628)
vm_eval.c
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   vm_eval.c -
00004 
00005   $Author: usa $
00006   created at: Sat May 24 16:02:32 JST 2008
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
00010   Copyright (C) 2000  Information-technology Promotion Agency, Japan
00011 
00012 **********************************************************************/
00013 
00014 static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, int call_status);
00015 static inline VALUE vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const NODE *cref);
00016 static inline VALUE vm_yield(rb_thread_t *th, int argc, const VALUE *argv);
00017 static NODE *vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr);
00018 static VALUE vm_exec(rb_thread_t *th);
00019 static void vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const NODE *cref, rb_block_t *base_block);
00020 static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, VALUE ary);
00021 
00022 /* vm_backtrace.c */
00023 VALUE vm_backtrace_str_ary(rb_thread_t *th, int lev, int n);
00024 
00025 typedef enum call_type {
00026     CALL_PUBLIC,
00027     CALL_FCALL,
00028     CALL_VCALL,
00029     CALL_TYPE_MAX
00030 } call_type;
00031 
00032 static VALUE send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope);
00033 
00034 static VALUE vm_call0_body(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv);
00035 
00036 static VALUE
00037 vm_call0(rb_thread_t* th, VALUE recv, ID id, int argc, const VALUE *argv,
00038          const rb_method_entry_t *me, VALUE defined_class)
00039 {
00040     rb_call_info_t ci_entry, *ci = &ci_entry;
00041 
00042     ci->flag = 0;
00043     ci->mid = id;
00044     ci->recv = recv;
00045     ci->defined_class = defined_class;
00046     ci->argc = argc;
00047     ci->me = me;
00048 
00049     return vm_call0_body(th, ci, argv);
00050 }
00051 
00052 #if OPT_CALL_CFUNC_WITHOUT_FRAME
00053 static VALUE
00054 vm_call0_cfunc(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv)
00055 {
00056     VALUE val;
00057 
00058     RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, ci->defined_class, ci->mid);
00059     EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, ci->recv, ci->mid, ci->defined_class, Qnil);
00060     {
00061         rb_control_frame_t *reg_cfp = th->cfp;
00062         const rb_method_entry_t *me = ci->me;
00063         const rb_method_cfunc_t *cfunc = &me->def->body.cfunc;
00064         int len = cfunc->argc;
00065 
00066         if (len >= 0) rb_check_arity(ci->argc, len, len);
00067 
00068         th->passed_ci = ci;
00069         ci->aux.inc_sp = 0;
00070         VM_PROFILE_UP(2);
00071         val = (*cfunc->invoker)(cfunc->func, ci, argv);
00072 
00073         if (reg_cfp == th->cfp) {
00074             if (UNLIKELY(th->passed_ci != ci)) {
00075                 rb_bug("vm_call0_cfunc: passed_ci error (ci: %p, passed_ci: %p)", ci, th->passed_ci);
00076             }
00077             th->passed_ci = 0;
00078         }
00079         else {
00080             if (reg_cfp != th->cfp + 1) {
00081                 rb_bug("vm_call0_cfunc: cfp consistency error");
00082             }
00083             VM_PROFILE_UP(3);
00084             vm_pop_frame(th);
00085         }
00086     }
00087     EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, ci->recv, ci->mid, ci->defined_class, val);
00088     RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, ci->defined_class, ci->mid);
00089 
00090     return val;
00091 }
00092 #else
00093 static VALUE
00094 vm_call0_cfunc_with_frame(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv)
00095 {
00096     VALUE val;
00097     const rb_method_entry_t *me = ci->me;
00098     const rb_method_cfunc_t *cfunc = &me->def->body.cfunc;
00099     int len = cfunc->argc;
00100     VALUE recv = ci->recv;
00101     VALUE defined_class = ci->defined_class;
00102     int argc = ci->argc;
00103     ID mid = ci->mid;
00104     rb_block_t *blockptr = ci->blockptr;
00105 
00106     RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, defined_class, mid);
00107     EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, mid, defined_class, Qnil);
00108     {
00109         rb_control_frame_t *reg_cfp = th->cfp;
00110 
00111         vm_push_frame(th, 0, VM_FRAME_MAGIC_CFUNC, recv, defined_class,
00112                       VM_ENVVAL_BLOCK_PTR(blockptr), 0, reg_cfp->sp, 1, me);
00113 
00114         if (len >= 0) rb_check_arity(argc, len, len);
00115 
00116         VM_PROFILE_UP(2);
00117         val = (*cfunc->invoker)(cfunc->func, recv, argc, argv);
00118 
00119         if (UNLIKELY(reg_cfp != th->cfp + 1)) {
00120                 rb_bug("vm_call0_cfunc_with_frame: cfp consistency error");
00121         }
00122         VM_PROFILE_UP(3);
00123         vm_pop_frame(th);
00124     }
00125     EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, mid, defined_class, val);
00126     RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, defined_class, mid);
00127 
00128     return val;
00129 }
00130 
00131 static VALUE
00132 vm_call0_cfunc(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv)
00133 {
00134     return vm_call0_cfunc_with_frame(th, ci, argv);
00135 }
00136 #endif
00137 
00138 /* `ci' should point temporal value (on stack value) */
00139 static VALUE
00140 vm_call0_body(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv)
00141 {
00142     VALUE ret;
00143 
00144     if (!ci->me->def) return Qnil;
00145 
00146     if (th->passed_block) {
00147         ci->blockptr = (rb_block_t *)th->passed_block;
00148         th->passed_block = 0;
00149     }
00150     else {
00151         ci->blockptr = 0;
00152     }
00153 
00154   again:
00155     switch (ci->me->def->type) {
00156       case VM_METHOD_TYPE_ISEQ:
00157         {
00158             rb_control_frame_t *reg_cfp = th->cfp;
00159             int i;
00160 
00161             CHECK_VM_STACK_OVERFLOW(reg_cfp, ci->argc + 1);
00162 
00163             *reg_cfp->sp++ = ci->recv;
00164             for (i = 0; i < ci->argc; i++) {
00165                 *reg_cfp->sp++ = argv[i];
00166             }
00167 
00168             vm_call_iseq_setup(th, reg_cfp, ci);
00169             th->cfp->flag |= VM_FRAME_FLAG_FINISH;
00170             return vm_exec(th); /* CHECK_INTS in this function */
00171         }
00172       case VM_METHOD_TYPE_NOTIMPLEMENTED:
00173       case VM_METHOD_TYPE_CFUNC:
00174         ret = vm_call0_cfunc(th, ci, argv);
00175         goto success;
00176       case VM_METHOD_TYPE_ATTRSET:
00177         rb_check_arity(ci->argc, 1, 1);
00178         ret = rb_ivar_set(ci->recv, ci->me->def->body.attr.id, argv[0]);
00179         goto success;
00180       case VM_METHOD_TYPE_IVAR:
00181         rb_check_arity(ci->argc, 0, 0);
00182         ret = rb_attr_get(ci->recv, ci->me->def->body.attr.id);
00183         goto success;
00184       case VM_METHOD_TYPE_BMETHOD:
00185         ret = vm_call_bmethod_body(th, ci, argv);
00186         goto success;
00187       case VM_METHOD_TYPE_ZSUPER:
00188       case VM_METHOD_TYPE_REFINED:
00189         {
00190             if (ci->me->def->type == VM_METHOD_TYPE_REFINED &&
00191                 ci->me->def->body.orig_me) {
00192                 ci->me = ci->me->def->body.orig_me;
00193                 goto again;
00194             }
00195 
00196             ci->defined_class = RCLASS_SUPER(ci->defined_class);
00197 
00198             if (!ci->defined_class || !(ci->me = rb_method_entry(ci->defined_class, ci->mid, &ci->defined_class))) {
00199                 ret = method_missing(ci->recv, ci->mid, ci->argc, argv, NOEX_SUPER);
00200                 goto success;
00201             }
00202             RUBY_VM_CHECK_INTS(th);
00203             if (!ci->me->def) return Qnil;
00204             goto again;
00205         }
00206       case VM_METHOD_TYPE_MISSING:
00207         {
00208             VALUE new_args = rb_ary_new4(ci->argc, argv);
00209 
00210             RB_GC_GUARD(new_args);
00211             rb_ary_unshift(new_args, ID2SYM(ci->mid));
00212             th->passed_block = ci->blockptr;
00213             return rb_funcall2(ci->recv, idMethodMissing, ci->argc+1, RARRAY_PTR(new_args));
00214         }
00215       case VM_METHOD_TYPE_OPTIMIZED:
00216         switch (ci->me->def->body.optimize_type) {
00217           case OPTIMIZED_METHOD_TYPE_SEND:
00218             ret = send_internal(ci->argc, argv, ci->recv, CALL_FCALL);
00219             goto success;
00220           case OPTIMIZED_METHOD_TYPE_CALL:
00221             {
00222                 rb_proc_t *proc;
00223                 GetProcPtr(ci->recv, proc);
00224                 ret = rb_vm_invoke_proc(th, proc, ci->argc, argv, ci->blockptr);
00225                 goto success;
00226             }
00227           default:
00228             rb_bug("vm_call0: unsupported optimized method type (%d)", ci->me->def->body.optimize_type);
00229         }
00230         break;
00231       case VM_METHOD_TYPE_UNDEF:
00232         break;
00233     }
00234     rb_bug("vm_call0: unsupported method type (%d)", ci->me->def->type);
00235     return Qundef;
00236 
00237   success:
00238     RUBY_VM_CHECK_INTS(th);
00239     return ret;
00240 }
00241 
00242 VALUE
00243 rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc, const VALUE *argv,
00244            const rb_method_entry_t *me, VALUE defined_class)
00245 {
00246     return vm_call0(th, recv, id, argc, argv, me, defined_class);
00247 }
00248 
00249 static inline VALUE
00250 vm_call_super(rb_thread_t *th, int argc, const VALUE *argv)
00251 {
00252     VALUE recv = th->cfp->self;
00253     VALUE klass;
00254     ID id;
00255     rb_method_entry_t *me;
00256     rb_control_frame_t *cfp = th->cfp;
00257 
00258     if (cfp->iseq || NIL_P(cfp->klass)) {
00259         rb_bug("vm_call_super: should not be reached");
00260     }
00261 
00262     klass = RCLASS_SUPER(cfp->klass);
00263     id = cfp->me->def->original_id;
00264     me = rb_method_entry(klass, id, &klass);
00265     if (!me) {
00266         return method_missing(recv, id, argc, argv, NOEX_SUPER);
00267     }
00268 
00269     return vm_call0(th, recv, id, argc, argv, me, klass);
00270 }
00271 
00272 VALUE
00273 rb_call_super(int argc, const VALUE *argv)
00274 {
00275     PASS_PASSED_BLOCK();
00276     return vm_call_super(GET_THREAD(), argc, argv);
00277 }
00278 
00279 static inline void
00280 stack_check(void)
00281 {
00282     rb_thread_t *th = GET_THREAD();
00283 
00284     if (!rb_thread_raised_p(th, RAISED_STACKOVERFLOW) && ruby_stack_check()) {
00285         rb_thread_raised_set(th, RAISED_STACKOVERFLOW);
00286         rb_exc_raise(sysstack_error);
00287     }
00288 }
00289 
00290 static inline rb_method_entry_t *
00291     rb_search_method_entry(VALUE recv, ID mid, VALUE *defined_class_ptr);
00292 static inline int rb_method_call_status(rb_thread_t *th, const rb_method_entry_t *me, call_type scope, VALUE self);
00293 #define NOEX_OK NOEX_NOSUPER
00294 
00310 static inline VALUE
00311 rb_call0(VALUE recv, ID mid, int argc, const VALUE *argv,
00312          call_type scope, VALUE self)
00313 {
00314     VALUE defined_class;
00315     rb_method_entry_t *me =
00316         rb_search_method_entry(recv, mid, &defined_class);
00317     rb_thread_t *th = GET_THREAD();
00318     int call_status = rb_method_call_status(th, me, scope, self);
00319 
00320     if (call_status != NOEX_OK) {
00321         return method_missing(recv, mid, argc, argv, call_status);
00322     }
00323     stack_check();
00324     return vm_call0(th, recv, mid, argc, argv, me, defined_class);
00325 }
00326 
00327 struct rescue_funcall_args {
00328     VALUE recv;
00329     VALUE sym;
00330     int argc;
00331     VALUE *argv;
00332 };
00333 
00334 static VALUE
00335 check_funcall_exec(struct rescue_funcall_args *args)
00336 {
00337     VALUE new_args = rb_ary_new4(args->argc, args->argv);
00338 
00339     RB_GC_GUARD(new_args);
00340     rb_ary_unshift(new_args, args->sym);
00341     return rb_funcall2(args->recv, idMethodMissing,
00342                        args->argc+1, RARRAY_PTR(new_args));
00343 }
00344 
00345 static VALUE
00346 check_funcall_failed(struct rescue_funcall_args *args, VALUE e)
00347 {
00348     if (rb_respond_to(args->recv, SYM2ID(args->sym))) {
00349         rb_exc_raise(e);
00350     }
00351     return Qundef;
00352 }
00353 
00354 static int
00355 check_funcall_respond_to(rb_thread_t *th, VALUE klass, VALUE recv, ID mid)
00356 {
00357     VALUE defined_class;
00358     const rb_method_entry_t *me = rb_method_entry(klass, idRespond_to, &defined_class);
00359 
00360     if (me && !(me->flag & NOEX_BASIC)) {
00361         const rb_block_t *passed_block = th->passed_block;
00362         VALUE args[2], result;
00363         int arity = rb_method_entry_arity(me);
00364 
00365         if (arity > 2)
00366             rb_raise(rb_eArgError, "respond_to? must accept 1 or 2 arguments (requires %d)", arity);
00367 
00368         if (arity < 1) arity = 2;
00369 
00370         args[0] = ID2SYM(mid);
00371         args[1] = Qtrue;
00372         result = vm_call0(th, recv, idRespond_to, arity, args, me, defined_class);
00373         th->passed_block = passed_block;
00374         if (!RTEST(result)) {
00375             return FALSE;
00376         }
00377     }
00378     return TRUE;
00379 }
00380 
00381 static int
00382 check_funcall_callable(rb_thread_t *th, const rb_method_entry_t *me)
00383 {
00384     return rb_method_call_status(th, me, CALL_FCALL, th->cfp->self) == NOEX_OK;
00385 }
00386 
00387 static VALUE
00388 check_funcall_missing(rb_thread_t *th, VALUE klass, VALUE recv, ID mid, int argc, VALUE *argv)
00389 {
00390     if (rb_method_basic_definition_p(klass, idMethodMissing)) {
00391         return Qundef;
00392     }
00393     else {
00394         struct rescue_funcall_args args;
00395 
00396         th->method_missing_reason = 0;
00397         args.recv = recv;
00398         args.sym = ID2SYM(mid);
00399         args.argc = argc;
00400         args.argv = argv;
00401         return rb_rescue2(check_funcall_exec, (VALUE)&args,
00402                           check_funcall_failed, (VALUE)&args,
00403                           rb_eNoMethodError, (VALUE)0);
00404     }
00405 }
00406 
00407 VALUE
00408 rb_check_funcall(VALUE recv, ID mid, int argc, VALUE *argv)
00409 {
00410     VALUE klass = CLASS_OF(recv);
00411     const rb_method_entry_t *me;
00412     rb_thread_t *th = GET_THREAD();
00413     VALUE defined_class;
00414 
00415     if (!check_funcall_respond_to(th, klass, recv, mid))
00416         return Qundef;
00417 
00418     me = rb_search_method_entry(recv, mid, &defined_class);
00419     if (check_funcall_callable(th, me) != NOEX_OK) {
00420         return check_funcall_missing(th, klass, recv, mid, argc, argv);
00421     }
00422     stack_check();
00423     return vm_call0(th, recv, mid, argc, argv, me, defined_class);
00424 }
00425 
00426 VALUE
00427 rb_check_funcall_with_hook(VALUE recv, ID mid, int argc, VALUE *argv,
00428                            rb_check_funcall_hook *hook, VALUE arg)
00429 {
00430     VALUE klass = CLASS_OF(recv);
00431     const rb_method_entry_t *me;
00432     rb_thread_t *th = GET_THREAD();
00433     VALUE defined_class;
00434 
00435     if (!check_funcall_respond_to(th, klass, recv, mid))
00436         return Qundef;
00437 
00438     me = rb_search_method_entry(recv, mid, &defined_class);
00439     if (check_funcall_callable(th, me) != NOEX_OK) {
00440         (*hook)(FALSE, recv, mid, argc, argv, arg);
00441         return check_funcall_missing(th, klass, recv, mid, argc, argv);
00442     }
00443     stack_check();
00444     (*hook)(TRUE, recv, mid, argc, argv, arg);
00445     return vm_call0(th, recv, mid, argc, argv, me, defined_class);
00446 }
00447 
00448 static const char *
00449 rb_type_str(enum ruby_value_type type)
00450 {
00451 #define type_case(t) case t: return #t;
00452     switch (type) {
00453       type_case(T_NONE)
00454       type_case(T_OBJECT)
00455       type_case(T_CLASS)
00456       type_case(T_MODULE)
00457       type_case(T_FLOAT)
00458       type_case(T_STRING)
00459       type_case(T_REGEXP)
00460       type_case(T_ARRAY)
00461       type_case(T_HASH)
00462       type_case(T_STRUCT)
00463       type_case(T_BIGNUM)
00464       type_case(T_FILE)
00465       type_case(T_DATA)
00466       type_case(T_MATCH)
00467       type_case(T_COMPLEX)
00468       type_case(T_RATIONAL)
00469       type_case(T_NIL)
00470       type_case(T_TRUE)
00471       type_case(T_FALSE)
00472       type_case(T_SYMBOL)
00473       type_case(T_FIXNUM)
00474       type_case(T_UNDEF)
00475       type_case(T_NODE)
00476       type_case(T_ICLASS)
00477       type_case(T_ZOMBIE)
00478       default: return NULL;
00479     }
00480 #undef type_case
00481 }
00482 
00483 static inline rb_method_entry_t *
00484 rb_search_method_entry(VALUE recv, ID mid, VALUE *defined_class_ptr)
00485 {
00486     VALUE klass = CLASS_OF(recv);
00487 
00488     if (!klass) {
00489         VALUE flags;
00490         if (SPECIAL_CONST_P(recv)) {
00491             rb_raise(rb_eNotImpError,
00492                      "method `%"PRIsVALUE"' called on unexpected immediate object (%p)",
00493                      rb_id2str(mid), (void *)recv);
00494         }
00495         flags = RBASIC(recv)->flags;
00496         if (flags == 0) {
00497             rb_raise(rb_eNotImpError,
00498                      "method `%"PRIsVALUE"' called on terminated object"
00499                      " (%p flags=0x%"PRIxVALUE")",
00500                      rb_id2str(mid), (void *)recv, flags);
00501         }
00502         else {
00503             int type = BUILTIN_TYPE(recv);
00504             const char *typestr = rb_type_str(type);
00505             if (typestr && T_OBJECT <= type && type < T_NIL)
00506                 rb_raise(rb_eNotImpError,
00507                          "method `%"PRIsVALUE"' called on hidden %s object"
00508                          " (%p flags=0x%"PRIxVALUE")",
00509                          rb_id2str(mid), typestr, (void *)recv, flags);
00510             if (typestr)
00511                 rb_raise(rb_eNotImpError,
00512                          "method `%"PRIsVALUE"' called on unexpected %s object"
00513                          " (%p flags=0x%"PRIxVALUE")",
00514                          rb_id2str(mid), typestr, (void *)recv, flags);
00515             else
00516                 rb_raise(rb_eNotImpError,
00517                          "method `%"PRIsVALUE"' called on broken T_???" "(0x%02x) object"
00518                          " (%p flags=0x%"PRIxVALUE")",
00519                          rb_id2str(mid), type, (void *)recv, flags);
00520         }
00521     }
00522     return rb_method_entry(klass, mid, defined_class_ptr);
00523 }
00524 
00525 static inline int
00526 rb_method_call_status(rb_thread_t *th, const rb_method_entry_t *me, call_type scope, VALUE self)
00527 {
00528     VALUE klass;
00529     ID oid;
00530     int noex;
00531 
00532     if (UNDEFINED_METHOD_ENTRY_P(me)) {
00533         return scope == CALL_VCALL ? NOEX_VCALL : 0;
00534     }
00535     klass = me->klass;
00536     oid = me->def->original_id;
00537     noex = me->flag;
00538 
00539     if (oid != idMethodMissing) {
00540         /* receiver specified form for private method */
00541         if (UNLIKELY(noex)) {
00542             if (((noex & NOEX_MASK) & NOEX_PRIVATE) && scope == CALL_PUBLIC) {
00543                 return NOEX_PRIVATE;
00544             }
00545 
00546             /* self must be kind of a specified form for protected method */
00547             if (((noex & NOEX_MASK) & NOEX_PROTECTED) && scope == CALL_PUBLIC) {
00548                 VALUE defined_class = klass;
00549 
00550                 if (RB_TYPE_P(defined_class, T_ICLASS)) {
00551                     defined_class = RBASIC(defined_class)->klass;
00552                 }
00553 
00554                 if (self == Qundef || !rb_obj_is_kind_of(self, defined_class)) {
00555                     return NOEX_PROTECTED;
00556                 }
00557             }
00558 
00559             if (NOEX_SAFE(noex) > th->safe_level) {
00560                 rb_raise(rb_eSecurityError, "calling insecure method: %s",
00561                          rb_id2name(me->called_id));
00562             }
00563         }
00564     }
00565     return NOEX_OK;
00566 }
00567 
00568 
00580 static inline VALUE
00581 rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
00582 {
00583     rb_thread_t *th = GET_THREAD();
00584     return rb_call0(recv, mid, argc, argv, scope, th->cfp->self);
00585 }
00586 
00587 NORETURN(static void raise_method_missing(rb_thread_t *th, int argc, const VALUE *argv,
00588                                           VALUE obj, int call_status));
00589 
00590 /*
00591  *  call-seq:
00592  *     obj.method_missing(symbol [, *args] )   -> result
00593  *
00594  *  Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
00595  *  <i>symbol</i> is the symbol for the method called, and <i>args</i>
00596  *  are any arguments that were passed to it. By default, the interpreter
00597  *  raises an error when this method is called. However, it is possible
00598  *  to override the method to provide more dynamic behavior.
00599  *  If it is decided that a particular method should not be handled, then
00600  *  <i>super</i> should be called, so that ancestors can pick up the
00601  *  missing method.
00602  *  The example below creates
00603  *  a class <code>Roman</code>, which responds to methods with names
00604  *  consisting of roman numerals, returning the corresponding integer
00605  *  values.
00606  *
00607  *     class Roman
00608  *       def roman_to_int(str)
00609  *         # ...
00610  *       end
00611  *       def method_missing(methId)
00612  *         str = methId.id2name
00613  *         roman_to_int(str)
00614  *       end
00615  *     end
00616  *
00617  *     r = Roman.new
00618  *     r.iv      #=> 4
00619  *     r.xxiii   #=> 23
00620  *     r.mm      #=> 2000
00621  */
00622 
00623 static VALUE
00624 rb_method_missing(int argc, const VALUE *argv, VALUE obj)
00625 {
00626     rb_thread_t *th = GET_THREAD();
00627     raise_method_missing(th, argc, argv, obj, th->method_missing_reason);
00628     UNREACHABLE;
00629 }
00630 
00631 #define NOEX_MISSING   0x80
00632 
00633 static VALUE
00634 make_no_method_exception(VALUE exc, const char *format, VALUE obj, int argc, const VALUE *argv)
00635 {
00636     int n = 0;
00637     VALUE mesg;
00638     VALUE args[3];
00639 
00640     if (!format) {
00641         format = "undefined method `%s' for %s";
00642     }
00643     mesg = rb_const_get(exc, rb_intern("message"));
00644     if (rb_method_basic_definition_p(CLASS_OF(mesg), '!')) {
00645         args[n++] = rb_name_err_mesg_new(mesg, rb_str_new2(format), obj, argv[0]);
00646     }
00647     else {
00648         args[n++] = rb_funcall(mesg, '!', 3, rb_str_new2(format), obj, argv[0]);
00649     }
00650     args[n++] = argv[0];
00651     if (exc == rb_eNoMethodError) {
00652         args[n++] = rb_ary_new4(argc - 1, argv + 1);
00653     }
00654     return rb_class_new_instance(n, args, exc);
00655 }
00656 
00657 static void
00658 raise_method_missing(rb_thread_t *th, int argc, const VALUE *argv, VALUE obj,
00659                      int last_call_status)
00660 {
00661     VALUE exc = rb_eNoMethodError;
00662     const char *format = 0;
00663 
00664     if (argc == 0 || !SYMBOL_P(argv[0])) {
00665         rb_raise(rb_eArgError, "no id given");
00666     }
00667 
00668     stack_check();
00669 
00670     if (last_call_status & NOEX_PRIVATE) {
00671         format = "private method `%s' called for %s";
00672     }
00673     else if (last_call_status & NOEX_PROTECTED) {
00674         format = "protected method `%s' called for %s";
00675     }
00676     else if (last_call_status & NOEX_VCALL) {
00677         format = "undefined local variable or method `%s' for %s";
00678         exc = rb_eNameError;
00679     }
00680     else if (last_call_status & NOEX_SUPER) {
00681         format = "super: no superclass method `%s' for %s";
00682     }
00683 
00684     {
00685         exc = make_no_method_exception(exc, format, obj, argc, argv);
00686         if (!(last_call_status & NOEX_MISSING)) {
00687             rb_vm_pop_cfunc_frame();
00688         }
00689         rb_exc_raise(exc);
00690     }
00691 }
00692 
00693 static inline VALUE
00694 method_missing(VALUE obj, ID id, int argc, const VALUE *argv, int call_status)
00695 {
00696     VALUE *nargv, result, argv_ary = 0;
00697     rb_thread_t *th = GET_THREAD();
00698     const rb_block_t *blockptr = th->passed_block;
00699 
00700     th->method_missing_reason = call_status;
00701     th->passed_block = 0;
00702 
00703     if (id == idMethodMissing) {
00704         raise_method_missing(th, argc, argv, obj, call_status | NOEX_MISSING);
00705     }
00706 
00707     if (argc < 0x100) {
00708         nargv = ALLOCA_N(VALUE, argc + 1);
00709     }
00710     else {
00711         argv_ary = rb_ary_tmp_new(argc + 1);
00712         nargv = RARRAY_PTR(argv_ary);
00713     }
00714     nargv[0] = ID2SYM(id);
00715     MEMCPY(nargv + 1, argv, VALUE, argc);
00716     if (argv_ary) rb_ary_set_len(argv_ary, argc + 1);
00717 
00718     if (rb_method_basic_definition_p(CLASS_OF(obj) , idMethodMissing)) {
00719         raise_method_missing(th, argc+1, nargv, obj, call_status | NOEX_MISSING);
00720     }
00721     th->passed_block = blockptr;
00722     result = rb_funcall2(obj, idMethodMissing, argc + 1, nargv);
00723     if (argv_ary) rb_ary_clear(argv_ary);
00724     return result;
00725 }
00726 
00727 void
00728 rb_raise_method_missing(rb_thread_t *th, int argc, VALUE *argv,
00729                         VALUE obj, int call_status)
00730 {
00731     th->passed_block = 0;
00732     raise_method_missing(th, argc, argv, obj, call_status | NOEX_MISSING);
00733 }
00734 
00743 VALUE
00744 rb_apply(VALUE recv, ID mid, VALUE args)
00745 {
00746     int argc;
00747     VALUE *argv, ret;
00748 
00749     argc = RARRAY_LENINT(args);
00750     if (argc >= 0x100) {
00751         args = rb_ary_subseq(args, 0, argc);
00752         RBASIC(args)->klass = 0;
00753         OBJ_FREEZE(args);
00754         ret = rb_call(recv, mid, argc, RARRAY_PTR(args), CALL_FCALL);
00755         RB_GC_GUARD(args);
00756         return ret;
00757     }
00758     argv = ALLOCA_N(VALUE, argc);
00759     MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
00760     return rb_call(recv, mid, argc, argv, CALL_FCALL);
00761 }
00762 
00772 VALUE
00773 rb_funcall(VALUE recv, ID mid, int n, ...)
00774 {
00775     VALUE *argv;
00776     va_list ar;
00777 
00778     if (n > 0) {
00779         long i;
00780 
00781         va_init_list(ar, n);
00782 
00783         argv = ALLOCA_N(VALUE, n);
00784 
00785         for (i = 0; i < n; i++) {
00786             argv[i] = va_arg(ar, VALUE);
00787         }
00788         va_end(ar);
00789     }
00790     else {
00791         argv = 0;
00792     }
00793     return rb_call(recv, mid, n, argv, CALL_FCALL);
00794 }
00795 
00803 VALUE
00804 rb_funcall2(VALUE recv, ID mid, int argc, const VALUE *argv)
00805 {
00806     return rb_call(recv, mid, argc, argv, CALL_FCALL);
00807 }
00808 
00818 VALUE
00819 rb_funcall3(VALUE recv, ID mid, int argc, const VALUE *argv)
00820 {
00821     return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
00822 }
00823 
00824 VALUE
00825 rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
00826 {
00827     PASS_PASSED_BLOCK_TH(GET_THREAD());
00828 
00829     return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
00830 }
00831 
00832 VALUE
00833 rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE pass_procval)
00834 {
00835     if (!NIL_P(pass_procval)) {
00836         rb_thread_t *th = GET_THREAD();
00837         rb_block_t *block = 0;
00838 
00839         rb_proc_t *pass_proc;
00840         GetProcPtr(pass_procval, pass_proc);
00841         block = &pass_proc->block;
00842 
00843         th->passed_block = block;
00844     }
00845 
00846     return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
00847 }
00848 
00849 static VALUE
00850 send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
00851 {
00852     ID id;
00853     VALUE vid;
00854     VALUE self;
00855     rb_thread_t *th = GET_THREAD();
00856 
00857     if (scope == CALL_PUBLIC) {
00858         self = Qundef;
00859     }
00860     else {
00861         self = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp)->self;
00862     }
00863 
00864     if (argc == 0) {
00865         rb_raise(rb_eArgError, "no method name given");
00866     }
00867 
00868     vid = *argv++; argc--;
00869 
00870     id = rb_check_id(&vid);
00871     if (!id) {
00872         if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) {
00873             VALUE exc = make_no_method_exception(rb_eNoMethodError, NULL,
00874                                                  recv, ++argc, --argv);
00875             rb_exc_raise(exc);
00876         }
00877         id = rb_to_id(vid);
00878     }
00879     PASS_PASSED_BLOCK_TH(th);
00880     return rb_call0(recv, id, argc, argv, scope, self);
00881 }
00882 
00883 /*
00884  *  call-seq:
00885  *     foo.send(symbol [, args...])        -> obj
00886  *     foo.__send__(symbol [, args...])      -> obj
00887  *
00888  *  Invokes the method identified by _symbol_, passing it any
00889  *  arguments specified. You can use <code>__send__</code> if the name
00890  *  +send+ clashes with an existing method in _obj_.
00891  *
00892  *     class Klass
00893  *       def hello(*args)
00894  *         "Hello " + args.join(' ')
00895  *       end
00896  *     end
00897  *     k = Klass.new
00898  *     k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"
00899  */
00900 
00901 VALUE
00902 rb_f_send(int argc, VALUE *argv, VALUE recv)
00903 {
00904     return send_internal(argc, argv, recv, CALL_FCALL);
00905 }
00906 
00907 /*
00908  *  call-seq:
00909  *     obj.public_send(symbol [, args...])  -> obj
00910  *
00911  *  Invokes the method identified by _symbol_, passing it any
00912  *  arguments specified. Unlike send, public_send calls public
00913  *  methods only.
00914  *
00915  *     1.public_send(:puts, "hello")  # causes NoMethodError
00916  */
00917 
00918 VALUE
00919 rb_f_public_send(int argc, VALUE *argv, VALUE recv)
00920 {
00921     return send_internal(argc, argv, recv, CALL_PUBLIC);
00922 }
00923 
00924 /* yield */
00925 
00926 static inline VALUE
00927 rb_yield_0(int argc, const VALUE * argv)
00928 {
00929     return vm_yield(GET_THREAD(), argc, argv);
00930 }
00931 
00932 VALUE
00933 rb_yield(VALUE val)
00934 {
00935     if (val == Qundef) {
00936         return rb_yield_0(0, 0);
00937     }
00938     else {
00939         return rb_yield_0(1, &val);
00940     }
00941 }
00942 
00943 VALUE
00944 rb_yield_values(int n, ...)
00945 {
00946     if (n == 0) {
00947         return rb_yield_0(0, 0);
00948     }
00949     else {
00950         int i;
00951         VALUE *argv;
00952         va_list args;
00953         argv = ALLOCA_N(VALUE, n);
00954 
00955         va_init_list(args, n);
00956         for (i=0; i<n; i++) {
00957             argv[i] = va_arg(args, VALUE);
00958         }
00959         va_end(args);
00960 
00961         return rb_yield_0(n, argv);
00962     }
00963 }
00964 
00965 VALUE
00966 rb_yield_values2(int argc, const VALUE *argv)
00967 {
00968     return rb_yield_0(argc, argv);
00969 }
00970 
00971 VALUE
00972 rb_yield_splat(VALUE values)
00973 {
00974     VALUE tmp = rb_check_array_type(values);
00975     volatile VALUE v;
00976     if (NIL_P(tmp)) {
00977         rb_raise(rb_eArgError, "not an array");
00978     }
00979     v = rb_yield_0(RARRAY_LENINT(tmp), RARRAY_PTR(tmp));
00980     return v;
00981 }
00982 
00983 static VALUE
00984 loop_i(void)
00985 {
00986     for (;;) {
00987         rb_yield_0(0, 0);
00988     }
00989     return Qnil;
00990 }
00991 
00992 static VALUE
00993 rb_f_loop_size(VALUE self, VALUE args)
00994 {
00995     return DBL2NUM(INFINITY);
00996 }
00997 
00998 /*
00999  *  call-seq:
01000  *     loop { block }
01001  *     loop            -> an_enumerator
01002  *
01003  *  Repeatedly executes the block.
01004  *
01005  *  If no block is given, an enumerator is returned instead.
01006  *
01007  *     loop do
01008  *       print "Input: "
01009  *       line = gets
01010  *       break if !line or line =~ /^qQ/
01011  *       # ...
01012  *     end
01013  *
01014  *  StopIteration raised in the block breaks the loop.
01015  */
01016 
01017 static VALUE
01018 rb_f_loop(VALUE self)
01019 {
01020     RETURN_SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size);
01021     rb_rescue2(loop_i, (VALUE)0, 0, 0, rb_eStopIteration, (VALUE)0);
01022     return Qnil;                /* dummy */
01023 }
01024 
01025 #if VMDEBUG
01026 static const char *
01027 vm_frametype_name(const rb_control_frame_t *cfp);
01028 #endif
01029 
01030 VALUE
01031 rb_iterate(VALUE (* it_proc) (VALUE), VALUE data1,
01032            VALUE (* bl_proc) (ANYARGS), VALUE data2)
01033 {
01034     int state;
01035     volatile VALUE retval = Qnil;
01036     NODE *node = NEW_IFUNC(bl_proc, data2);
01037     rb_thread_t *th = GET_THREAD();
01038     rb_control_frame_t *volatile cfp = th->cfp;
01039 
01040     node->nd_aid = rb_frame_this_func();
01041     TH_PUSH_TAG(th);
01042     state = TH_EXEC_TAG();
01043     if (state == 0) {
01044       iter_retry:
01045         {
01046             rb_block_t *blockptr;
01047             if (bl_proc) {
01048                 blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(th->cfp);
01049                 blockptr->iseq = (void *)node;
01050                 blockptr->proc = 0;
01051             }
01052             else {
01053                 blockptr = VM_CF_BLOCK_PTR(th->cfp);
01054             }
01055             th->passed_block = blockptr;
01056         }
01057         retval = (*it_proc) (data1);
01058     }
01059     else {
01060         VALUE err = th->errinfo;
01061         if (state == TAG_BREAK) {
01062             VALUE *escape_ep = GET_THROWOBJ_CATCH_POINT(err);
01063             VALUE *cep = cfp->ep;
01064 
01065             if (cep == escape_ep) {
01066                 state = 0;
01067                 th->state = 0;
01068                 th->errinfo = Qnil;
01069                 retval = GET_THROWOBJ_VAL(err);
01070 
01071                 rb_vm_rewind_cfp(th, cfp);
01072             }
01073             else{
01074                 /* SDR(); printf("%p, %p\n", cdfp, escape_dfp); */
01075             }
01076         }
01077         else if (state == TAG_RETRY) {
01078             VALUE *escape_ep = GET_THROWOBJ_CATCH_POINT(err);
01079             VALUE *cep = cfp->ep;
01080 
01081             if (cep == escape_ep) {
01082                 rb_vm_rewind_cfp(th, cfp);
01083 
01084                 state = 0;
01085                 th->state = 0;
01086                 th->errinfo = Qnil;
01087                 goto iter_retry;
01088             }
01089         }
01090     }
01091     TH_POP_TAG();
01092 
01093     switch (state) {
01094       case 0:
01095         break;
01096       default:
01097         TH_JUMP_TAG(th, state);
01098     }
01099     return retval;
01100 }
01101 
01102 struct iter_method_arg {
01103     VALUE obj;
01104     ID mid;
01105     int argc;
01106     VALUE *argv;
01107 };
01108 
01109 static VALUE
01110 iterate_method(VALUE obj)
01111 {
01112     const struct iter_method_arg * arg =
01113       (struct iter_method_arg *) obj;
01114 
01115     return rb_call(arg->obj, arg->mid, arg->argc, arg->argv, CALL_FCALL);
01116 }
01117 
01118 VALUE
01119 rb_block_call(VALUE obj, ID mid, int argc, VALUE * argv,
01120               VALUE (*bl_proc) (ANYARGS), VALUE data2)
01121 {
01122     struct iter_method_arg arg;
01123 
01124     arg.obj = obj;
01125     arg.mid = mid;
01126     arg.argc = argc;
01127     arg.argv = argv;
01128     return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2);
01129 }
01130 
01131 static VALUE
01132 iterate_check_method(VALUE obj)
01133 {
01134     const struct iter_method_arg * arg =
01135       (struct iter_method_arg *) obj;
01136 
01137     return rb_check_funcall(arg->obj, arg->mid, arg->argc, arg->argv);
01138 }
01139 
01140 VALUE
01141 rb_check_block_call(VALUE obj, ID mid, int argc, VALUE * argv,
01142                     VALUE (*bl_proc) (ANYARGS), VALUE data2)
01143 {
01144     struct iter_method_arg arg;
01145 
01146     arg.obj = obj;
01147     arg.mid = mid;
01148     arg.argc = argc;
01149     arg.argv = argv;
01150     return rb_iterate(iterate_check_method, (VALUE)&arg, bl_proc, data2);
01151 }
01152 
01153 VALUE
01154 rb_each(VALUE obj)
01155 {
01156     return rb_call(obj, idEach, 0, 0, CALL_FCALL);
01157 }
01158 
01159 static VALUE
01160 eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, volatile VALUE file, volatile int line)
01161 {
01162     int state;
01163     VALUE result = Qundef;
01164     VALUE envval;
01165     rb_thread_t *th = GET_THREAD();
01166     rb_env_t *env = NULL;
01167     rb_block_t block, *base_block;
01168     volatile int parse_in_eval;
01169     volatile int mild_compile_error;
01170 
01171     if (file == 0) {
01172         file = rb_sourcefilename();
01173         line = rb_sourceline();
01174     }
01175 
01176     parse_in_eval = th->parse_in_eval;
01177     mild_compile_error = th->mild_compile_error;
01178     TH_PUSH_TAG(th);
01179     if ((state = TH_EXEC_TAG()) == 0) {
01180         rb_binding_t *bind = 0;
01181         rb_iseq_t *iseq;
01182         volatile VALUE iseqval;
01183         VALUE absolute_path = Qnil;
01184         VALUE fname;
01185 
01186         if (file != Qundef) {
01187             absolute_path = file;
01188         }
01189 
01190         if (scope != Qnil) {
01191             if (rb_obj_is_kind_of(scope, rb_cBinding)) {
01192                 GetBindingPtr(scope, bind);
01193                 envval = bind->env;
01194                 if (NIL_P(absolute_path) && !NIL_P(bind->path)) {
01195                     file = bind->path;
01196                     line = bind->first_lineno;
01197                     absolute_path = rb_current_realfilepath();
01198                 }
01199             }
01200             else {
01201                 rb_raise(rb_eTypeError,
01202                          "wrong argument type %s (expected Binding)",
01203                          rb_obj_classname(scope));
01204             }
01205             GetEnvPtr(envval, env);
01206             base_block = &env->block;
01207         }
01208         else {
01209             rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp);
01210 
01211             if (cfp != 0) {
01212                 block = *RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp);
01213                 base_block = &block;
01214                 base_block->self = self;
01215                 base_block->iseq = cfp->iseq;   /* TODO */
01216             }
01217             else {
01218                 rb_raise(rb_eRuntimeError, "Can't eval on top of Fiber or Thread");
01219             }
01220         }
01221 
01222         if ((fname = file) == Qundef) {
01223             fname = rb_usascii_str_new_cstr("(eval)");
01224         }
01225 
01226         /* make eval iseq */
01227         th->parse_in_eval++;
01228         th->mild_compile_error++;
01229         iseqval = rb_iseq_compile_with_option(src, fname, absolute_path, INT2FIX(line), base_block, Qnil);
01230         th->mild_compile_error--;
01231         th->parse_in_eval--;
01232 
01233         vm_set_eval_stack(th, iseqval, cref, base_block);
01234 
01235         if (0) {                /* for debug */
01236             VALUE disasm = rb_iseq_disasm(iseqval);
01237             printf("%s\n", StringValuePtr(disasm));
01238         }
01239 
01240         /* save new env */
01241         GetISeqPtr(iseqval, iseq);
01242         if (bind && iseq->local_table_size > 0) {
01243             bind->env = rb_vm_make_env_object(th, th->cfp);
01244         }
01245 
01246         /* kick */
01247         CHECK_VM_STACK_OVERFLOW(th->cfp, iseq->stack_max);
01248         result = vm_exec(th);
01249     }
01250     TH_POP_TAG();
01251     th->mild_compile_error = mild_compile_error;
01252     th->parse_in_eval = parse_in_eval;
01253 
01254     if (state) {
01255         if (state == TAG_RAISE) {
01256             VALUE errinfo = th->errinfo;
01257             if (file == Qundef) {
01258                 VALUE mesg, errat, bt2;
01259                 ID id_mesg;
01260 
01261                 CONST_ID(id_mesg, "mesg");
01262                 errat = rb_get_backtrace(errinfo);
01263                 mesg = rb_attr_get(errinfo, id_mesg);
01264                 if (!NIL_P(errat) && RB_TYPE_P(errat, T_ARRAY) &&
01265                     (bt2 = vm_backtrace_str_ary(th, 0, 0), RARRAY_LEN(bt2) > 0)) {
01266                     if (!NIL_P(mesg) && RB_TYPE_P(mesg, T_STRING) && !RSTRING_LEN(mesg)) {
01267                         if (OBJ_FROZEN(mesg)) {
01268                             VALUE m = rb_str_cat(rb_str_dup(RARRAY_PTR(errat)[0]), ": ", 2);
01269                             rb_ivar_set(errinfo, id_mesg, rb_str_append(m, mesg));
01270                         }
01271                         else {
01272                             rb_str_update(mesg, 0, 0, rb_str_new2(": "));
01273                             rb_str_update(mesg, 0, 0, RARRAY_PTR(errat)[0]);
01274                         }
01275                     }
01276                     RARRAY_PTR(errat)[0] = RARRAY_PTR(bt2)[0];
01277                 }
01278             }
01279             rb_exc_raise(errinfo);
01280         }
01281         JUMP_TAG(state);
01282     }
01283     return result;
01284 }
01285 
01286 static VALUE
01287 eval_string(VALUE self, VALUE src, VALUE scope, VALUE file, int line)
01288 {
01289     return eval_string_with_cref(self, src, scope, 0, file, line);
01290 }
01291 
01292 /*
01293  *  call-seq:
01294  *     eval(string [, binding [, filename [,lineno]]])  -> obj
01295  *
01296  *  Evaluates the Ruby expression(s) in <em>string</em>. If
01297  *  <em>binding</em> is given, which must be a <code>Binding</code>
01298  *  object, the evaluation is performed in its context. If the
01299  *  optional <em>filename</em> and <em>lineno</em> parameters are
01300  *  present, they will be used when reporting syntax errors.
01301  *
01302  *     def get_binding(str)
01303  *       return binding
01304  *     end
01305  *     str = "hello"
01306  *     eval "str + ' Fred'"                      #=> "hello Fred"
01307  *     eval "str + ' Fred'", get_binding("bye")  #=> "bye Fred"
01308  */
01309 
01310 VALUE
01311 rb_f_eval(int argc, VALUE *argv, VALUE self)
01312 {
01313     VALUE src, scope, vfile, vline;
01314     VALUE file = Qundef;
01315     int line = 1;
01316 
01317     rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
01318     if (rb_safe_level() >= 4) {
01319         StringValue(src);
01320         if (!NIL_P(scope) && !OBJ_TAINTED(scope)) {
01321             rb_raise(rb_eSecurityError,
01322                      "Insecure: can't modify trusted binding");
01323         }
01324     }
01325     else {
01326         SafeStringValue(src);
01327     }
01328     if (argc >= 3) {
01329         StringValue(vfile);
01330     }
01331     if (argc >= 4) {
01332         line = NUM2INT(vline);
01333     }
01334 
01335     if (!NIL_P(vfile))
01336         file = vfile;
01337     return eval_string(self, src, scope, file, line);
01338 }
01339 
01341 VALUE
01342 ruby_eval_string_from_file(const char *str, const char *filename)
01343 {
01344     VALUE file = filename ? rb_str_new_cstr(filename) : 0;
01345     return eval_string(rb_vm_top_self(), rb_str_new2(str), Qnil, file, 1);
01346 }
01347 
01348 struct eval_string_from_file_arg {
01349     VALUE str;
01350     VALUE filename;
01351 };
01352 
01353 static VALUE
01354 eval_string_from_file_helper(void *data)
01355 {
01356     const struct eval_string_from_file_arg *const arg = (struct eval_string_from_file_arg*)data;
01357     return eval_string(rb_vm_top_self(), arg->str, Qnil, arg->filename, 1);
01358 }
01359 
01360 VALUE
01361 ruby_eval_string_from_file_protect(const char *str, const char *filename, int *state)
01362 {
01363     struct eval_string_from_file_arg arg;
01364     arg.str = rb_str_new_cstr(str);
01365     arg.filename = filename ? rb_str_new_cstr(filename) : 0;
01366     return rb_protect((VALUE (*)(VALUE))eval_string_from_file_helper, (VALUE)&arg, state);
01367 }
01368 
01381 VALUE
01382 rb_eval_string(const char *str)
01383 {
01384     return ruby_eval_string_from_file(str, "eval");
01385 }
01386 
01397 VALUE
01398 rb_eval_string_protect(const char *str, int *state)
01399 {
01400     return rb_protect((VALUE (*)(VALUE))rb_eval_string, (VALUE)str, state);
01401 }
01402 
01414 VALUE
01415 rb_eval_string_wrap(const char *str, int *state)
01416 {
01417     int status;
01418     rb_thread_t *th = GET_THREAD();
01419     VALUE self = th->top_self;
01420     VALUE wrapper = th->top_wrapper;
01421     VALUE val;
01422 
01423     th->top_wrapper = rb_module_new();
01424     th->top_self = rb_obj_clone(rb_vm_top_self());
01425     rb_extend_object(th->top_self, th->top_wrapper);
01426 
01427     val = rb_eval_string_protect(str, &status);
01428 
01429     th->top_self = self;
01430     th->top_wrapper = wrapper;
01431 
01432     if (state) {
01433         *state = status;
01434     }
01435     else if (status) {
01436         JUMP_TAG(status);
01437     }
01438     return val;
01439 }
01440 
01441 VALUE
01442 rb_eval_cmd(VALUE cmd, VALUE arg, int level)
01443 {
01444     int state;
01445     VALUE val = Qnil;           /* OK */
01446     volatile int safe = rb_safe_level();
01447 
01448     if (OBJ_TAINTED(cmd)) {
01449         level = 4;
01450     }
01451 
01452     if (!RB_TYPE_P(cmd, T_STRING)) {
01453         PUSH_TAG();
01454         rb_set_safe_level_force(level);
01455         if ((state = EXEC_TAG()) == 0) {
01456             val = rb_funcall2(cmd, rb_intern("call"), RARRAY_LENINT(arg),
01457                               RARRAY_PTR(arg));
01458         }
01459         POP_TAG();
01460 
01461         rb_set_safe_level_force(safe);
01462 
01463         if (state)
01464             JUMP_TAG(state);
01465         return val;
01466     }
01467 
01468     PUSH_TAG();
01469     if ((state = EXEC_TAG()) == 0) {
01470         val = eval_string(rb_vm_top_self(), cmd, Qnil, 0, 0);
01471     }
01472     POP_TAG();
01473 
01474     rb_set_safe_level_force(safe);
01475     if (state) JUMP_TAG(state);
01476     return val;
01477 }
01478 
01479 /* block eval under the class/module context */
01480 
01481 static VALUE
01482 yield_under(VALUE under, VALUE self, VALUE values)
01483 {
01484     rb_thread_t *th = GET_THREAD();
01485     rb_block_t block, *blockptr;
01486     NODE *cref;
01487 
01488     if ((blockptr = VM_CF_BLOCK_PTR(th->cfp)) != 0) {
01489         block = *blockptr;
01490         block.self = self;
01491         VM_CF_LEP(th->cfp)[0] = VM_ENVVAL_BLOCK_PTR(&block);
01492     }
01493     cref = vm_cref_push(th, under, NOEX_PUBLIC, blockptr);
01494     cref->flags |= NODE_FL_CREF_PUSHED_BY_EVAL;
01495 
01496     if (values == Qundef) {
01497         return vm_yield_with_cref(th, 1, &self, cref);
01498     }
01499     else {
01500         return vm_yield_with_cref(th, RARRAY_LENINT(values), RARRAY_PTR(values), cref);
01501     }
01502 }
01503 
01504 VALUE
01505 rb_yield_refine_block(VALUE refinement, VALUE refinements)
01506 {
01507     rb_thread_t *th = GET_THREAD();
01508     rb_block_t block, *blockptr;
01509     NODE *cref;
01510 
01511     if ((blockptr = VM_CF_BLOCK_PTR(th->cfp)) != 0) {
01512         block = *blockptr;
01513         block.self = refinement;
01514         VM_CF_LEP(th->cfp)[0] = VM_ENVVAL_BLOCK_PTR(&block);
01515     }
01516     cref = vm_cref_push(th, refinement, NOEX_PUBLIC, blockptr);
01517     cref->flags |= NODE_FL_CREF_PUSHED_BY_EVAL;
01518     cref->nd_refinements = refinements;
01519 
01520     return vm_yield_with_cref(th, 0, NULL, cref);
01521 }
01522 
01523 /* string eval under the class/module context */
01524 static VALUE
01525 eval_under(VALUE under, VALUE self, VALUE src, VALUE file, int line)
01526 {
01527     NODE *cref = vm_cref_push(GET_THREAD(), under, NOEX_PUBLIC, NULL);
01528 
01529     if (SPECIAL_CONST_P(self) && !NIL_P(under)) {
01530         cref->flags |= NODE_FL_CREF_PUSHED_BY_EVAL;
01531     }
01532     if (rb_safe_level() >= 4) {
01533         StringValue(src);
01534     }
01535     else {
01536         SafeStringValue(src);
01537     }
01538 
01539     return eval_string_with_cref(self, src, Qnil, cref, file, line);
01540 }
01541 
01542 static VALUE
01543 specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self)
01544 {
01545     if (rb_block_given_p()) {
01546         rb_check_arity(argc, 0, 0);
01547         return yield_under(klass, self, Qundef);
01548     }
01549     else {
01550         VALUE file = Qundef;
01551         int line = 1;
01552 
01553         rb_check_arity(argc, 1, 3);
01554         if (rb_safe_level() >= 4) {
01555             StringValue(argv[0]);
01556         }
01557         else {
01558             SafeStringValue(argv[0]);
01559         }
01560         if (argc > 2)
01561             line = NUM2INT(argv[2]);
01562         if (argc > 1) {
01563             file = argv[1];
01564             if (!NIL_P(file)) StringValue(file);
01565         }
01566         return eval_under(klass, self, argv[0], file, line);
01567     }
01568 }
01569 
01570 /*
01571  *  call-seq:
01572  *     obj.instance_eval(string [, filename [, lineno]] )   -> obj
01573  *     obj.instance_eval {| | block }                       -> obj
01574  *
01575  *  Evaluates a string containing Ruby source code, or the given block,
01576  *  within the context of the receiver (_obj_). In order to set the
01577  *  context, the variable +self+ is set to _obj_ while
01578  *  the code is executing, giving the code access to _obj_'s
01579  *  instance variables. In the version of <code>instance_eval</code>
01580  *  that takes a +String+, the optional second and third
01581  *  parameters supply a filename and starting line number that are used
01582  *  when reporting compilation errors.
01583  *
01584  *     class KlassWithSecret
01585  *       def initialize
01586  *         @secret = 99
01587  *       end
01588  *     end
01589  *     k = KlassWithSecret.new
01590  *     k.instance_eval { @secret }   #=> 99
01591  */
01592 
01593 VALUE
01594 rb_obj_instance_eval(int argc, VALUE *argv, VALUE self)
01595 {
01596     VALUE klass;
01597 
01598     if (SPECIAL_CONST_P(self)) {
01599         klass = rb_special_singleton_class(self);
01600     }
01601     else {
01602         klass = rb_singleton_class(self);
01603     }
01604     return specific_eval(argc, argv, klass, self);
01605 }
01606 
01607 /*
01608  *  call-seq:
01609  *     obj.instance_exec(arg...) {|var...| block }                       -> obj
01610  *
01611  *  Executes the given block within the context of the receiver
01612  *  (_obj_). In order to set the context, the variable +self+ is set
01613  *  to _obj_ while the code is executing, giving the code access to
01614  *  _obj_'s instance variables.  Arguments are passed as block parameters.
01615  *
01616  *     class KlassWithSecret
01617  *       def initialize
01618  *         @secret = 99
01619  *       end
01620  *     end
01621  *     k = KlassWithSecret.new
01622  *     k.instance_exec(5) {|x| @secret+x }   #=> 104
01623  */
01624 
01625 VALUE
01626 rb_obj_instance_exec(int argc, VALUE *argv, VALUE self)
01627 {
01628     VALUE klass;
01629 
01630     if (SPECIAL_CONST_P(self)) {
01631         klass = rb_special_singleton_class(self);
01632     }
01633     else {
01634         klass = rb_singleton_class(self);
01635     }
01636     return yield_under(klass, self, rb_ary_new4(argc, argv));
01637 }
01638 
01639 /*
01640  *  call-seq:
01641  *     mod.class_eval(string [, filename [, lineno]])  -> obj
01642  *     mod.module_eval {|| block }                     -> obj
01643  *
01644  *  Evaluates the string or block in the context of _mod_, except that when
01645  *  a block is given, constant/class variable lookup is not affected. This
01646  *  can be used to add methods to a class. <code>module_eval</code> returns
01647  *  the result of evaluating its argument. The optional _filename_ and
01648  *  _lineno_ parameters set the text for error messages.
01649  *
01650  *     class Thing
01651  *     end
01652  *     a = %q{def hello() "Hello there!" end}
01653  *     Thing.module_eval(a)
01654  *     puts Thing.new.hello()
01655  *     Thing.module_eval("invalid code", "dummy", 123)
01656  *
01657  *  <em>produces:</em>
01658  *
01659  *     Hello there!
01660  *     dummy:123:in `module_eval': undefined local variable
01661  *         or method `code' for Thing:Class
01662  */
01663 
01664 VALUE
01665 rb_mod_module_eval(int argc, VALUE *argv, VALUE mod)
01666 {
01667     return specific_eval(argc, argv, mod, mod);
01668 }
01669 
01670 /*
01671  *  call-seq:
01672  *     mod.module_exec(arg...) {|var...| block }       -> obj
01673  *     mod.class_exec(arg...) {|var...| block }        -> obj
01674  *
01675  *  Evaluates the given block in the context of the class/module.
01676  *  The method defined in the block will belong to the receiver.
01677  *
01678  *     class Thing
01679  *     end
01680  *     Thing.class_exec{
01681  *       def hello() "Hello there!" end
01682  *     }
01683  *     puts Thing.new.hello()
01684  *
01685  *  <em>produces:</em>
01686  *
01687  *     Hello there!
01688  */
01689 
01690 VALUE
01691 rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
01692 {
01693     return yield_under(mod, mod, rb_ary_new4(argc, argv));
01694 }
01695 
01696 /*
01697  *  call-seq:
01698  *     throw(tag [, obj])
01699  *
01700  *  Transfers control to the end of the active +catch+ block
01701  *  waiting for _tag_. Raises +ArgumentError+ if there
01702  *  is no +catch+ block for the _tag_. The optional second
01703  *  parameter supplies a return value for the +catch+ block,
01704  *  which otherwise defaults to +nil+. For examples, see
01705  *  <code>Kernel::catch</code>.
01706  */
01707 
01708 static VALUE
01709 rb_f_throw(int argc, VALUE *argv)
01710 {
01711     VALUE tag, value;
01712 
01713     rb_scan_args(argc, argv, "11", &tag, &value);
01714     rb_throw_obj(tag, value);
01715     UNREACHABLE;
01716 }
01717 
01718 void
01719 rb_throw_obj(VALUE tag, VALUE value)
01720 {
01721     rb_thread_t *th = GET_THREAD();
01722     struct rb_vm_tag *tt = th->tag;
01723 
01724     while (tt) {
01725         if (tt->tag == tag) {
01726             tt->retval = value;
01727             break;
01728         }
01729         tt = tt->prev;
01730     }
01731     if (!tt) {
01732         VALUE desc = rb_inspect(tag);
01733         RB_GC_GUARD(desc);
01734         rb_raise(rb_eArgError, "uncaught throw %s", RSTRING_PTR(desc));
01735     }
01736     th->errinfo = NEW_THROW_OBJECT(tag, 0, TAG_THROW);
01737 
01738     JUMP_TAG(TAG_THROW);
01739 }
01740 
01741 void
01742 rb_throw(const char *tag, VALUE val)
01743 {
01744     rb_throw_obj(ID2SYM(rb_intern(tag)), val);
01745 }
01746 
01747 static VALUE
01748 catch_i(VALUE tag, VALUE data)
01749 {
01750     return rb_yield_0(1, &tag);
01751 }
01752 
01753 /*
01754  *  call-seq:
01755  *     catch([arg]) {|tag| block }  -> obj
01756  *
01757  *  +catch+ executes its block. If a +throw+ is
01758  *  executed, Ruby searches up its stack for a +catch+ block
01759  *  with a tag corresponding to the +throw+'s
01760  *  _tag_. If found, that block is terminated, and
01761  *  +catch+ returns the value given to +throw+. If
01762  *  +throw+ is not called, the block terminates normally, and
01763  *  the value of +catch+ is the value of the last expression
01764  *  evaluated. +catch+ expressions may be nested, and the
01765  *  +throw+ call need not be in lexical scope.
01766  *
01767  *     def routine(n)
01768  *       puts n
01769  *       throw :done if n <= 0
01770  *       routine(n-1)
01771  *     end
01772  *
01773  *
01774  *     catch(:done) { routine(3) }
01775  *
01776  *  <em>produces:</em>
01777  *
01778  *     3
01779  *     2
01780  *     1
01781  *     0
01782  *
01783  *  when _arg_ is given, +catch+ yields it as is, or when no
01784  *  _arg_ is given, +catch+ assigns a new unique object to
01785  *  +throw+.  this is useful for nested +catch+.  _arg_ can
01786  *  be an arbitrary object, not only Symbol.
01787  *
01788  */
01789 
01790 static VALUE
01791 rb_f_catch(int argc, VALUE *argv)
01792 {
01793     VALUE tag;
01794 
01795     if (argc == 0) {
01796         tag = rb_obj_alloc(rb_cObject);
01797     }
01798     else {
01799         rb_scan_args(argc, argv, "01", &tag);
01800     }
01801     return rb_catch_obj(tag, catch_i, 0);
01802 }
01803 
01804 VALUE
01805 rb_catch(const char *tag, VALUE (*func)(), VALUE data)
01806 {
01807     VALUE vtag = tag ? ID2SYM(rb_intern(tag)) : rb_obj_alloc(rb_cObject);
01808     return rb_catch_obj(vtag, func, data);
01809 }
01810 
01811 VALUE
01812 rb_catch_obj(VALUE tag, VALUE (*func)(), VALUE data)
01813 {
01814     int state;
01815     volatile VALUE val = Qnil;          /* OK */
01816     rb_thread_t *th = GET_THREAD();
01817     rb_control_frame_t *saved_cfp = th->cfp;
01818 
01819     TH_PUSH_TAG(th);
01820 
01821     th->tag->tag = tag;
01822 
01823     if ((state = TH_EXEC_TAG()) == 0) {
01824         /* call with argc=1, argv = [tag], block = Qnil to insure compatibility */
01825         val = (*func)(tag, data, 1, &tag, Qnil);
01826     }
01827     else if (state == TAG_THROW && RNODE(th->errinfo)->u1.value == tag) {
01828         rb_vm_rewind_cfp(th, saved_cfp);
01829         val = th->tag->retval;
01830         th->errinfo = Qnil;
01831         state = 0;
01832     }
01833     TH_POP_TAG();
01834     if (state)
01835         JUMP_TAG(state);
01836 
01837     return val;
01838 }
01839 
01840 /*
01841  *  call-seq:
01842  *     local_variables    -> array
01843  *
01844  *  Returns the names of the current local variables.
01845  *
01846  *     fred = 1
01847  *     for i in 1..10
01848  *        # ...
01849  *     end
01850  *     local_variables   #=> [:fred, :i]
01851  */
01852 
01853 static VALUE
01854 rb_f_local_variables(void)
01855 {
01856     VALUE ary = rb_ary_new();
01857     rb_thread_t *th = GET_THREAD();
01858     rb_control_frame_t *cfp =
01859         vm_get_ruby_level_caller_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp));
01860     int i;
01861 
01862     while (cfp) {
01863         if (cfp->iseq) {
01864             for (i = 0; i < cfp->iseq->local_table_size; i++) {
01865                 ID lid = cfp->iseq->local_table[i];
01866                 if (lid) {
01867                     const char *vname = rb_id2name(lid);
01868                     /* should skip temporary variable */
01869                     if (vname) {
01870                         rb_ary_push(ary, ID2SYM(lid));
01871                     }
01872                 }
01873             }
01874         }
01875         if (!VM_EP_LEP_P(cfp->ep)) {
01876             /* block */
01877             VALUE *ep = VM_CF_PREV_EP(cfp);
01878 
01879             if (vm_collect_local_variables_in_heap(th, ep, ary)) {
01880                 break;
01881             }
01882             else {
01883                 while (cfp->ep != ep) {
01884                     cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
01885                 }
01886             }
01887         }
01888         else {
01889             break;
01890         }
01891     }
01892     return ary;
01893 }
01894 
01895 /*
01896  *  call-seq:
01897  *     block_given?   -> true or false
01898  *     iterator?      -> true or false
01899  *
01900  *  Returns <code>true</code> if <code>yield</code> would execute a
01901  *  block in the current context. The <code>iterator?</code> form
01902  *  is mildly deprecated.
01903  *
01904  *     def try
01905  *       if block_given?
01906  *         yield
01907  *       else
01908  *         "no block"
01909  *       end
01910  *     end
01911  *     try                  #=> "no block"
01912  *     try { "hello" }      #=> "hello"
01913  *     try do "hello" end   #=> "hello"
01914  */
01915 
01916 
01917 VALUE
01918 rb_f_block_given_p(void)
01919 {
01920     rb_thread_t *th = GET_THREAD();
01921     rb_control_frame_t *cfp = th->cfp;
01922     cfp = vm_get_ruby_level_caller_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
01923 
01924     if (cfp != 0 && VM_CF_BLOCK_PTR(cfp)) {
01925         return Qtrue;
01926     }
01927     else {
01928         return Qfalse;
01929     }
01930 }
01931 
01932 VALUE
01933 rb_current_realfilepath(void)
01934 {
01935     rb_thread_t *th = GET_THREAD();
01936     rb_control_frame_t *cfp = th->cfp;
01937     cfp = vm_get_ruby_level_caller_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
01938     if (cfp != 0) return cfp->iseq->location.absolute_path;
01939     return Qnil;
01940 }
01941 
01942 void
01943 Init_vm_eval(void)
01944 {
01945     rb_define_global_function("eval", rb_f_eval, -1);
01946     rb_define_global_function("local_variables", rb_f_local_variables, 0);
01947     rb_define_global_function("iterator?", rb_f_block_given_p, 0);
01948     rb_define_global_function("block_given?", rb_f_block_given_p, 0);
01949 
01950     rb_define_global_function("catch", rb_f_catch, -1);
01951     rb_define_global_function("throw", rb_f_throw, -1);
01952 
01953     rb_define_global_function("loop", rb_f_loop, 0);
01954 
01955     rb_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval, -1);
01956     rb_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec, -1);
01957     rb_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
01958 
01959 #if 1
01960     rb_add_method(rb_cBasicObject, rb_intern("__send__"),
01961                   VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, 0);
01962     rb_add_method(rb_mKernel, rb_intern("send"),
01963                   VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, 0);
01964 #else
01965     rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
01966     rb_define_method(rb_mKernel, "send", rb_f_send, -1);
01967 #endif
01968     rb_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
01969 
01970     rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec, -1);
01971     rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec, -1);
01972     rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1);
01973     rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval, -1);
01974 }
01975