1 /**********************************************************************
6 created at: Sat May 24 16:02:32 JST 2008
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/node.h"
18 #include "vm_method.c"
20 static inline VALUE
method_missing(VALUE obj
, ID id
, int argc
, const VALUE
*argv
, int call_status
);
21 static inline VALUE
rb_vm_set_finish_env(rb_thread_t
* th
);
22 static inline VALUE
vm_yield_with_cref(rb_thread_t
*th
, int argc
, const VALUE
*argv
, const NODE
*cref
);
23 static inline VALUE
vm_yield(rb_thread_t
*th
, int argc
, const VALUE
*argv
);
24 static inline VALUE
vm_backtrace(rb_thread_t
*th
, int lev
);
25 static NODE
*vm_cref_push(rb_thread_t
*th
, VALUE klass
, int noex
);
26 static VALUE
vm_eval_body(rb_thread_t
*th
);
27 static void vm_set_eval_stack(rb_thread_t
* th
, VALUE iseqval
, const NODE
*cref
);
30 vm_call0(rb_thread_t
* th
, VALUE klass
, VALUE recv
, VALUE id
, ID oid
,
31 int argc
, const VALUE
*argv
, const NODE
*body
, int nosuper
)
34 rb_block_t
*blockptr
= 0;
36 if (0) printf("id: %s, nd: %s, argc: %d, passed: %p\n",
37 rb_id2name(id
), ruby_node_name(nd_type(body
)),
38 argc
, th
->passed_block
);
40 if (th
->passed_block
) {
41 blockptr
= th
->passed_block
;
44 switch (nd_type(body
)) {
45 case RUBY_VM_METHOD_NODE
:{
46 rb_control_frame_t
*reg_cfp
;
47 VALUE iseqval
= (VALUE
)body
->nd_body
;
50 rb_vm_set_finish_env(th
);
53 CHECK_STACK_OVERFLOW(reg_cfp
, argc
+ 1);
55 *reg_cfp
->sp
++ = recv
;
56 for (i
= 0; i
< argc
; i
++) {
57 *reg_cfp
->sp
++ = argv
[i
];
60 vm_setup_method(th
, reg_cfp
, argc
, blockptr
, 0, iseqval
, recv
, klass
);
61 val
= vm_eval_body(th
);
65 EXEC_EVENT_HOOK(th
, RUBY_EVENT_C_CALL
, recv
, id
, klass
);
67 rb_control_frame_t
*reg_cfp
= th
->cfp
;
68 rb_control_frame_t
*cfp
=
69 vm_push_frame(th
, 0, VM_FRAME_MAGIC_CFUNC
,
70 recv
, (VALUE
)blockptr
, 0, reg_cfp
->sp
, 0, 1);
73 cfp
->method_class
= klass
;
75 val
= call_cfunc(body
->nd_cfnc
, recv
, body
->nd_argc
, argc
, argv
);
77 if (reg_cfp
!= th
->cfp
+ 1) {
80 rb_bug("cfp consistency error - call0");
85 EXEC_EVENT_HOOK(th
, RUBY_EVENT_C_RETURN
, recv
, id
, klass
);
90 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 1)", argc
);
92 val
= rb_ivar_set(recv
, body
->nd_vid
, argv
[0]);
97 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 0)",
100 val
= rb_attr_get(recv
, body
->nd_vid
);
104 val
= vm_call_bmethod(th
, id
, body
->nd_cval
,
105 recv
, klass
, argc
, (VALUE
*)argv
, blockptr
);
109 rb_bug("unsupported: vm_call0(%s)", ruby_node_name(nd_type(body
)));
111 RUBY_VM_CHECK_INTS();
116 rb_vm_call(rb_thread_t
* th
, VALUE klass
, VALUE recv
, VALUE id
, ID oid
,
117 int argc
, const VALUE
*argv
, const NODE
*body
, int nosuper
)
119 return vm_call0(th
, klass
, recv
, id
, oid
, argc
, argv
, body
, nosuper
);
123 vm_call_super(rb_thread_t
* const th
, const int argc
, const VALUE
* const argv
)
125 VALUE recv
= th
->cfp
->self
;
129 rb_control_frame_t
*cfp
= th
->cfp
;
132 klass
= cfp
->method_class
;
133 klass
= RCLASS_SUPER(klass
);
136 klass
= vm_search_normal_superclass(cfp
->method_class
, recv
);
142 rb_bug("vm_call_super: should not be reached");
145 body
= rb_method_node(klass
, id
); /* this returns NODE_METHOD */
148 body
= body
->nd_body
;
151 VALUE
*argv_m
= ALLOCA_N(VALUE
, argc
+1);
152 MEMCPY(argv_m
+ 1, argv
, VALUE
, argc
);
153 argv_m
[0] = ID2SYM(id
);
154 th
->method_missing_reason
= 0;
155 th
->passed_block
= 0;
156 return rb_funcall2(recv
, idMethodMissing
, argc
+ 1, argv
);
159 return vm_call0(th
, klass
, recv
, id
, id
, argc
, argv
, body
, CALL_SUPER
);
163 rb_call_super(int argc
, const VALUE
*argv
)
166 return vm_call_super(GET_THREAD(), argc
, argv
);
172 rb_thread_t
*th
= GET_THREAD();
174 if (!rb_thread_raised_p(th
, RAISED_STACKOVERFLOW
) && ruby_stack_check()) {
175 rb_thread_raised_set(th
, RAISED_STACKOVERFLOW
);
176 rb_exc_raise(sysstack_error
);
181 rb_call0(VALUE klass
, VALUE recv
, ID mid
, int argc
, const VALUE
*argv
,
182 int scope
, VALUE self
)
187 struct cache_entry
*ent
;
188 rb_thread_t
*th
= GET_THREAD();
191 rb_raise(rb_eNotImpError
,
192 "method `%s' called on terminated object (%p)",
193 rb_id2name(mid
), (void *)recv
);
195 /* is it in the method cache? */
196 ent
= cache
+ EXPR1(klass
, mid
);
198 if (ent
->mid
== mid
&& ent
->klass
== klass
) {
200 return method_missing(recv
, mid
, argc
, argv
,
201 scope
== 2 ? NOEX_VCALL
: 0);
203 noex
= ent
->method
->nd_noex
;
204 klass
= ent
->method
->nd_clss
;
205 body
= ent
->method
->nd_body
;
207 else if ((method
= rb_get_method_body(klass
, id
, &id
)) != 0) {
208 noex
= method
->nd_noex
;
209 klass
= method
->nd_clss
;
210 body
= method
->nd_body
;
214 return method_missing(recv
, mid
, argc
, argv
, NOEX_SUPER
);
216 return method_missing(recv
, mid
, argc
, argv
,
217 scope
== 2 ? NOEX_VCALL
: 0);
221 if (mid
!= missing
) {
222 /* receiver specified form for private method */
223 if (UNLIKELY(noex
)) {
224 if (((noex
& NOEX_MASK
) & NOEX_PRIVATE
) && scope
== 0) {
225 return method_missing(recv
, mid
, argc
, argv
, NOEX_PRIVATE
);
228 /* self must be kind of a specified form for protected method */
229 if (((noex
& NOEX_MASK
) & NOEX_PROTECTED
) && scope
== 0) {
230 VALUE defined_class
= klass
;
232 if (TYPE(defined_class
) == T_ICLASS
) {
233 defined_class
= RBASIC(defined_class
)->klass
;
236 if (self
== Qundef
) {
237 self
= th
->cfp
->self
;
239 if (!rb_obj_is_kind_of(self
, rb_class_real(defined_class
))) {
240 return method_missing(recv
, mid
, argc
, argv
, NOEX_PROTECTED
);
244 if (NOEX_SAFE(noex
) > th
->safe_level
) {
245 rb_raise(rb_eSecurityError
, "calling insecure method: %s", rb_id2name(mid
));
251 return vm_call0(th
, klass
, recv
, mid
, id
, argc
, argv
, body
, noex
& NOEX_NOSUPER
);
255 rb_call(VALUE klass
, VALUE recv
, ID mid
, int argc
, const VALUE
*argv
, int scope
)
257 return rb_call0(klass
, recv
, mid
, argc
, argv
, scope
, Qundef
);
262 * obj.method_missing(symbol [, *args] ) => result
264 * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
265 * <i>symbol</i> is the symbol for the method called, and <i>args</i>
266 * are any arguments that were passed to it. By default, the interpreter
267 * raises an error when this method is called. However, it is possible
268 * to override the method to provide more dynamic behavior.
269 * If it is decided that a particular method should not be handled, then
270 * <i>super</i> should be called, so that ancestors can pick up the
272 * The example below creates
273 * a class <code>Roman</code>, which responds to methods with names
274 * consisting of roman numerals, returning the corresponding integer
278 * def romanToInt(str)
281 * def method_missing(methId)
282 * str = methId.id2name
294 rb_method_missing(int argc
, const VALUE
*argv
, VALUE obj
)
297 VALUE exc
= rb_eNoMethodError
;
298 const char *format
= 0;
299 rb_thread_t
*th
= GET_THREAD();
300 int last_call_status
= th
->method_missing_reason
;
301 if (argc
== 0 || !SYMBOL_P(argv
[0])) {
302 rb_raise(rb_eArgError
, "no id given");
307 id
= SYM2ID(argv
[0]);
309 if (last_call_status
& NOEX_PRIVATE
) {
310 format
= "private method `%s' called for %s";
312 else if (last_call_status
& NOEX_PROTECTED
) {
313 format
= "protected method `%s' called for %s";
315 else if (last_call_status
& NOEX_VCALL
) {
316 format
= "undefined local variable or method `%s' for %s";
319 else if (last_call_status
& NOEX_SUPER
) {
320 format
= "super: no superclass method `%s' for %s";
323 format
= "undefined method `%s' for %s";
329 args
[n
++] = rb_funcall(rb_const_get(exc
, rb_intern("message")), '!',
330 3, rb_str_new2(format
), obj
, argv
[0]);
332 if (exc
== rb_eNoMethodError
) {
333 args
[n
++] = rb_ary_new4(argc
- 1, argv
+ 1);
335 exc
= rb_class_new_instance(n
, args
, exc
);
337 th
->cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
);
341 return Qnil
; /* not reached */
345 method_missing(VALUE obj
, ID id
, int argc
, const VALUE
*argv
, int call_status
)
348 GET_THREAD()->method_missing_reason
= call_status
;
351 rb_method_missing(argc
, argv
, obj
);
353 else if (id
== ID_ALLOCATOR
) {
354 rb_raise(rb_eTypeError
, "allocator undefined for %s",
358 nargv
= ALLOCA_N(VALUE
, argc
+ 1);
359 nargv
[0] = ID2SYM(id
);
360 MEMCPY(nargv
+ 1, argv
, VALUE
, argc
);
362 return rb_funcall2(obj
, missing
, argc
+ 1, nargv
);
366 rb_apply(VALUE recv
, ID mid
, VALUE args
)
371 argc
= RARRAY_LEN(args
); /* Assigns LONG, but argc is INT */
372 argv
= ALLOCA_N(VALUE
, argc
);
373 MEMCPY(argv
, RARRAY_PTR(args
), VALUE
, argc
);
374 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_FCALL
);
378 rb_funcall(VALUE recv
, ID mid
, int n
, ...)
387 argv
= ALLOCA_N(VALUE
, n
);
389 for (i
= 0; i
< n
; i
++) {
390 argv
[i
] = va_arg(ar
, VALUE
);
397 return rb_call(CLASS_OF(recv
), recv
, mid
, n
, argv
, CALL_FCALL
);
401 rb_funcall2(VALUE recv
, ID mid
, int argc
, const VALUE
*argv
)
403 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_FCALL
);
407 rb_funcall3(VALUE recv
, ID mid
, int argc
, const VALUE
*argv
)
409 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_PUBLIC
);
413 send_internal(int argc
, VALUE
*argv
, VALUE recv
, int scope
)
416 VALUE self
= RUBY_VM_PREVIOUS_CONTROL_FRAME(GET_THREAD()->cfp
)->self
;
417 rb_thread_t
*th
= GET_THREAD();
420 rb_raise(rb_eArgError
, "no method name given");
423 vid
= *argv
++; argc
--;
424 PASS_PASSED_BLOCK_TH(th
);
426 return rb_call0(CLASS_OF(recv
), recv
, rb_to_id(vid
), argc
, argv
, scope
, self
);
431 * obj.send(symbol [, args...]) => obj
432 * obj.__send__(symbol [, args...]) => obj
434 * Invokes the method identified by _symbol_, passing it any
435 * arguments specified. You can use <code>__send__</code> if the name
436 * +send+ clashes with an existing method in _obj_.
440 * "Hello " + args.join(' ')
444 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
448 rb_f_send(int argc
, VALUE
*argv
, VALUE recv
)
450 return send_internal(argc
, argv
, recv
, NOEX_NOSUPER
| NOEX_PRIVATE
);
455 * obj.public_send(symbol [, args...]) => obj
457 * Invokes the method identified by _symbol_, passing it any
458 * arguments specified. Unlike send, public_send calls public
461 * 1.public_send(:puts, "hello") # causes NoMethodError
465 rb_f_public_send(int argc
, VALUE
*argv
, VALUE recv
)
467 return send_internal(argc
, argv
, recv
, NOEX_PUBLIC
);
473 rb_yield_0(int argc
, const VALUE
* argv
)
475 return vm_yield(GET_THREAD(), argc
, argv
);
482 return rb_yield_0(0, 0);
485 return rb_yield_0(1, &val
);
490 rb_yield_values(int n
, ...)
493 return rb_yield_0(0, 0);
499 argv
= ALLOCA_N(VALUE
, n
);
501 va_init_list(args
, n
);
502 for (i
=0; i
<n
; i
++) {
503 argv
[i
] = va_arg(args
, VALUE
);
507 return rb_yield_0(n
, argv
);
512 rb_yield_values2(int argc
, const VALUE
*argv
)
514 return rb_yield_0(argc
, argv
);
518 rb_yield_splat(VALUE values
)
520 VALUE tmp
= rb_check_array_type(values
);
523 rb_raise(rb_eArgError
, "not an array");
525 v
= rb_yield_0(RARRAY_LEN(tmp
), RARRAY_PTR(tmp
));
542 * Repeatedly executes the block.
547 * break if !line or line =~ /^qQ/
551 * StopIteration raised in the block breaks the loop.
557 rb_rescue2(loop_i
, (VALUE
)0, 0, 0, rb_eStopIteration
, (VALUE
)0);
558 return Qnil
; /* dummy */
562 rb_iterate(VALUE (* it_proc
) (VALUE
), VALUE data1
,
563 VALUE (* bl_proc
) (ANYARGS
), VALUE data2
)
566 volatile VALUE retval
= Qnil
;
567 NODE
*node
= NEW_IFUNC(bl_proc
, data2
);
568 rb_thread_t
*th
= GET_THREAD();
569 rb_control_frame_t
*cfp
= th
->cfp
;
572 state
= TH_EXEC_TAG();
576 rb_block_t
*blockptr
= RUBY_VM_GET_BLOCK_PTR_IN_CFP(th
->cfp
);
577 blockptr
->iseq
= (void *)node
;
579 th
->passed_block
= blockptr
;
581 retval
= (*it_proc
) (data1
);
584 VALUE err
= th
->errinfo
;
585 if (state
== TAG_BREAK
) {
586 VALUE
*escape_dfp
= GET_THROWOBJ_CATCH_POINT(err
);
587 VALUE
*cdfp
= cfp
->dfp
;
589 if (cdfp
== escape_dfp
) {
596 /* SDR(); printf("%p, %p\n", cdfp, escape_dfp); */
599 else if (state
== TAG_RETRY
) {
600 VALUE
*escape_dfp
= GET_THROWOBJ_CATCH_POINT(err
);
601 VALUE
*cdfp
= cfp
->dfp
;
603 if (cdfp
== escape_dfp
) {
618 TH_JUMP_TAG(th
, state
);
623 struct iter_method_arg
{
631 iterate_method(VALUE obj
)
633 const struct iter_method_arg
* arg
=
634 (struct iter_method_arg
*) obj
;
636 return rb_call(CLASS_OF(arg
->obj
), arg
->obj
, arg
->mid
,
637 arg
->argc
, arg
->argv
, CALL_FCALL
);
641 rb_block_call(VALUE obj
, ID mid
, int argc
, VALUE
* argv
,
642 VALUE (*bl_proc
) (ANYARGS
), VALUE data2
)
644 struct iter_method_arg arg
;
650 return rb_iterate(iterate_method
, (VALUE
)&arg
, bl_proc
, data2
);
656 return rb_call(CLASS_OF(obj
), obj
, idEach
, 0, 0, CALL_FCALL
);
660 eval_string_with_cref(VALUE self
, VALUE src
, VALUE scope
, NODE
*cref
, const char *file
, int line
)
663 VALUE result
= Qundef
;
665 rb_binding_t
*bind
= 0;
666 rb_thread_t
*th
= GET_THREAD();
667 rb_env_t
*env
= NULL
;
669 volatile int parse_in_eval
;
670 volatile int mild_compile_error
;
673 file
= rb_sourcefile();
674 line
= rb_sourceline();
677 parse_in_eval
= th
->parse_in_eval
;
678 mild_compile_error
= th
->mild_compile_error
;
680 if ((state
= EXEC_TAG()) == 0) {
682 volatile VALUE iseqval
;
685 if (rb_obj_is_kind_of(scope
, rb_cBinding
)) {
686 GetBindingPtr(scope
, bind
);
690 rb_raise(rb_eTypeError
,
691 "wrong argument type %s (expected Binding)",
692 rb_obj_classname(scope
));
694 GetEnvPtr(envval
, env
);
695 th
->base_block
= &env
->block
;
698 rb_control_frame_t
*cfp
= vm_get_ruby_level_caller_cfp(th
, th
->cfp
);
701 block
= *RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp
);
702 th
->base_block
= &block
;
703 th
->base_block
->self
= self
;
704 th
->base_block
->iseq
= cfp
->iseq
; /* TODO */
707 rb_raise(rb_eRuntimeError
, "Can't eval on top of Fiber or Thread");
713 th
->mild_compile_error
++;
714 iseqval
= rb_iseq_compile(src
, rb_str_new2(file
), INT2FIX(line
));
715 th
->mild_compile_error
--;
718 vm_set_eval_stack(th
, iseqval
, cref
);
721 if (0) { /* for debug */
722 extern VALUE
ruby_iseq_disasm(VALUE
);
723 printf("%s\n", RSTRING_PTR(ruby_iseq_disasm(iseqval
)));
727 GetISeqPtr(iseqval
, iseq
);
728 if (bind
&& iseq
->local_size
> 0) {
729 bind
->env
= vm_make_env_object(th
, th
->cfp
);
733 CHECK_STACK_OVERFLOW(th
->cfp
, iseq
->stack_max
);
734 result
= vm_eval_body(th
);
737 th
->mild_compile_error
= mild_compile_error
;
738 th
->parse_in_eval
= parse_in_eval
;
741 if (state
== TAG_RAISE
) {
742 VALUE errinfo
= th
->errinfo
;
743 if (strcmp(file
, "(eval)") == 0) {
744 VALUE mesg
, errat
, bt2
;
745 extern VALUE
rb_get_backtrace(VALUE info
);
747 errat
= rb_get_backtrace(errinfo
);
748 mesg
= rb_attr_get(errinfo
, rb_intern("mesg"));
749 if (!NIL_P(errat
) && TYPE(errat
) == T_ARRAY
&&
750 (bt2
= vm_backtrace(th
, -2), RARRAY_LEN(bt2
) > 0)) {
751 if (!NIL_P(mesg
) && TYPE(mesg
) == T_STRING
&& !RSTRING_LEN(mesg
)) {
752 rb_str_update(mesg
, 0, 0, rb_str_new2(": "));
753 rb_str_update(mesg
, 0, 0, RARRAY_PTR(errat
)[0]);
755 RARRAY_PTR(errat
)[0] = RARRAY_PTR(bt2
)[0];
758 rb_exc_raise(errinfo
);
766 eval_string(VALUE self
, VALUE src
, VALUE scope
, const char *file
, int line
)
768 return eval_string_with_cref(self
, src
, scope
, 0, file
, line
);
773 * eval(string [, binding [, filename [,lineno]]]) => obj
775 * Evaluates the Ruby expression(s) in <em>string</em>. If
776 * <em>binding</em> is given, the evaluation is performed in its
777 * context. The binding may be a <code>Binding</code> object or a
778 * <code>Proc</code> object. If the optional <em>filename</em> and
779 * <em>lineno</em> parameters are present, they will be used when
780 * reporting syntax errors.
782 * def getBinding(str)
786 * eval "str + ' Fred'" #=> "hello Fred"
787 * eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
791 rb_f_eval(int argc
, VALUE
*argv
, VALUE self
)
793 VALUE src
, scope
, vfile
, vline
;
794 const char *file
= "(eval)";
797 rb_scan_args(argc
, argv
, "13", &src
, &scope
, &vfile
, &vline
);
798 if (rb_safe_level() >= 4) {
800 if (!NIL_P(scope
) && !OBJ_TAINTED(scope
)) {
801 rb_raise(rb_eSecurityError
,
802 "Insecure: can't modify trusted binding");
806 SafeStringValue(src
);
812 line
= NUM2INT(vline
);
816 file
= RSTRING_PTR(vfile
);
817 return eval_string(self
, src
, scope
, file
, line
);
821 rb_eval_string(const char *str
)
823 return eval_string(rb_vm_top_self(), rb_str_new2(str
), Qnil
, "(eval)", 1);
827 rb_eval_string_protect(const char *str
, int *state
)
829 return rb_protect((VALUE (*)(VALUE
))rb_eval_string
, (VALUE
)str
, state
);
833 rb_eval_string_wrap(const char *str
, int *state
)
836 rb_thread_t
*th
= GET_THREAD();
837 VALUE self
= th
->top_self
;
838 VALUE wrapper
= th
->top_wrapper
;
841 th
->top_wrapper
= rb_module_new();
842 th
->top_self
= rb_obj_clone(rb_vm_top_self());
843 rb_extend_object(th
->top_self
, th
->top_wrapper
);
845 val
= rb_eval_string_protect(str
, &status
);
848 th
->top_wrapper
= wrapper
;
860 rb_eval_cmd(VALUE cmd
, VALUE arg
, int level
)
863 VALUE val
= Qnil
; /* OK */
864 volatile int safe
= rb_safe_level();
866 if (OBJ_TAINTED(cmd
)) {
870 if (TYPE(cmd
) != T_STRING
) {
872 rb_set_safe_level_force(level
);
873 if ((state
= EXEC_TAG()) == 0) {
874 val
= rb_funcall2(cmd
, rb_intern("call"), RARRAY_LEN(arg
),
879 rb_set_safe_level_force(safe
);
887 if ((state
= EXEC_TAG()) == 0) {
888 val
= eval_string(rb_vm_top_self(), cmd
, Qnil
, 0, 0);
892 rb_set_safe_level_force(safe
);
893 if (state
) vm_jump_tag_but_local_jump(state
, val
);
897 /* block eval under the class/module context */
900 yield_under(VALUE under
, VALUE self
, VALUE values
)
902 rb_thread_t
*th
= GET_THREAD();
903 rb_block_t block
, *blockptr
;
904 NODE
*cref
= vm_cref_push(th
, under
, NOEX_PUBLIC
);
906 if ((blockptr
= GC_GUARDED_PTR_REF(th
->cfp
->lfp
[0])) != 0) {
909 th
->cfp
->lfp
[0] = GC_GUARDED_PTR(&block
);
912 if (values
== Qundef
) {
913 return vm_yield_with_cref(th
, 0, 0, cref
);
916 return vm_yield_with_cref(th
, RARRAY_LEN(values
), RARRAY_PTR(values
), cref
);
920 /* string eval under the class/module context */
922 eval_under(VALUE under
, VALUE self
, VALUE src
, const char *file
, int line
)
924 NODE
*cref
= vm_cref_push(GET_THREAD(), under
, NOEX_PUBLIC
);
926 if (rb_safe_level() >= 4) {
930 SafeStringValue(src
);
933 return eval_string_with_cref(self
, src
, Qnil
, cref
, file
, line
);
937 specific_eval(int argc
, VALUE
*argv
, VALUE klass
, VALUE self
)
939 if (rb_block_given_p()) {
941 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 0)", argc
);
943 return yield_under(klass
, self
, Qundef
);
946 const char *file
= "(eval)";
950 rb_raise(rb_eArgError
, "block not supplied");
953 if (rb_safe_level() >= 4) {
954 StringValue(argv
[0]);
957 SafeStringValue(argv
[0]);
960 const char *name
= rb_id2name(rb_frame_callee());
961 rb_raise(rb_eArgError
,
962 "wrong number of arguments: %s(src) or %s{..}",
966 line
= NUM2INT(argv
[2]);
968 file
= StringValuePtr(argv
[1]);
971 return eval_under(klass
, self
, argv
[0], file
, line
);
977 * obj.instance_eval(string [, filename [, lineno]] ) => obj
978 * obj.instance_eval {| | block } => obj
980 * Evaluates a string containing Ruby source code, or the given block,
981 * within the context of the receiver (_obj_). In order to set the
982 * context, the variable +self+ is set to _obj_ while
983 * the code is executing, giving the code access to _obj_'s
984 * instance variables. In the version of <code>instance_eval</code>
985 * that takes a +String+, the optional second and third
986 * parameters supply a filename and starting line number that are used
987 * when reporting compilation errors.
989 * class KlassWithSecret
994 * k = KlassWithSecret.new
995 * k.instance_eval { @secret } #=> 99
999 rb_obj_instance_eval(int argc
, VALUE
*argv
, VALUE self
)
1003 if (SPECIAL_CONST_P(self
)) {
1007 klass
= rb_singleton_class(self
);
1009 return specific_eval(argc
, argv
, klass
, self
);
1014 * obj.instance_exec(arg...) {|var...| block } => obj
1016 * Executes the given block within the context of the receiver
1017 * (_obj_). In order to set the context, the variable +self+ is set
1018 * to _obj_ while the code is executing, giving the code access to
1019 * _obj_'s instance variables. Arguments are passed as block parameters.
1021 * class KlassWithSecret
1026 * k = KlassWithSecret.new
1027 * k.instance_exec(5) {|x| @secret+x } #=> 104
1031 rb_obj_instance_exec(int argc
, VALUE
*argv
, VALUE self
)
1035 if (SPECIAL_CONST_P(self
)) {
1039 klass
= rb_singleton_class(self
);
1041 return yield_under(klass
, self
, rb_ary_new4(argc
, argv
));
1046 * mod.class_eval(string [, filename [, lineno]]) => obj
1047 * mod.module_eval {|| block } => obj
1049 * Evaluates the string or block in the context of _mod_. This can
1050 * be used to add methods to a class. <code>module_eval</code> returns
1051 * the result of evaluating its argument. The optional _filename_
1052 * and _lineno_ parameters set the text for error messages.
1056 * a = %q{def hello() "Hello there!" end}
1057 * Thing.module_eval(a)
1058 * puts Thing.new.hello()
1059 * Thing.module_eval("invalid code", "dummy", 123)
1061 * <em>produces:</em>
1064 * dummy:123:in `module_eval': undefined local variable
1065 * or method `code' for Thing:Class
1069 rb_mod_module_eval(int argc
, VALUE
*argv
, VALUE mod
)
1071 return specific_eval(argc
, argv
, mod
, mod
);
1076 * mod.module_exec(arg...) {|var...| block } => obj
1077 * mod.class_exec(arg...) {|var...| block } => obj
1079 * Evaluates the given block in the context of the class/module.
1080 * The method defined in the block will belong to the receiver.
1085 * def hello() "Hello there!" end
1087 * puts Thing.new.hello()
1089 * <em>produces:</em>
1095 rb_mod_module_exec(int argc
, VALUE
*argv
, VALUE mod
)
1097 return yield_under(mod
, mod
, rb_ary_new4(argc
, argv
));
1100 NORETURN(static VALUE rb_f_throw
_((int, VALUE
*)));
1104 * throw(symbol [, obj])
1106 * Transfers control to the end of the active +catch+ block
1107 * waiting for _symbol_. Raises +NameError+ if there
1108 * is no +catch+ block for the symbol. The optional second
1109 * parameter supplies a return value for the +catch+ block,
1110 * which otherwise defaults to +nil+. For examples, see
1111 * <code>Kernel::catch</code>.
1115 rb_f_throw(int argc
, VALUE
*argv
)
1118 rb_thread_t
*th
= GET_THREAD();
1119 struct rb_vm_tag
*tt
= th
->tag
;
1121 rb_scan_args(argc
, argv
, "11", &tag
, &value
);
1123 if (tt
->tag
== tag
) {
1130 VALUE desc
= rb_inspect(tag
);
1131 rb_raise(rb_eArgError
, "uncaught throw %s", RSTRING_PTR(desc
));
1133 rb_trap_restore_mask();
1134 th
->errinfo
= NEW_THROW_OBJECT(tag
, 0, TAG_THROW
);
1136 JUMP_TAG(TAG_THROW
);
1138 return Qnil
; /* not reached */
1143 rb_throw(const char *tag
, VALUE val
)
1147 argv
[0] = ID2SYM(rb_intern(tag
));
1149 rb_f_throw(2, argv
);
1153 rb_throw_obj(VALUE tag
, VALUE val
)
1159 rb_f_throw(2, argv
);
1164 * catch(symbol) {| | block } > obj
1166 * +catch+ executes its block. If a +throw+ is
1167 * executed, Ruby searches up its stack for a +catch+ block
1168 * with a tag corresponding to the +throw+'s
1169 * _symbol_. If found, that block is terminated, and
1170 * +catch+ returns the value given to +throw+. If
1171 * +throw+ is not called, the block terminates normally, and
1172 * the value of +catch+ is the value of the last expression
1173 * evaluated. +catch+ expressions may be nested, and the
1174 * +throw+ call need not be in lexical scope.
1178 * throw :done if n <= 0
1183 * catch(:done) { routine(3) }
1185 * <em>produces:</em>
1194 rb_f_catch(int argc
, VALUE
*argv
)
1198 VALUE val
= Qnil
; /* OK */
1199 rb_thread_t
*th
= GET_THREAD();
1200 rb_control_frame_t
*saved_cfp
= th
->cfp
;
1203 tag
= rb_obj_alloc(rb_cObject
);
1206 rb_scan_args(argc
, argv
, "01", &tag
);
1212 if ((state
= EXEC_TAG()) == 0) {
1213 val
= rb_yield_0(1, &tag
);
1215 else if (state
== TAG_THROW
&& RNODE(th
->errinfo
)->u1
.value
== tag
) {
1216 th
->cfp
= saved_cfp
;
1217 val
= th
->tag
->retval
;
1229 catch_null_i(VALUE dmy
)
1231 return rb_funcall(Qnil
, rb_intern("catch"), 0, 0);
1237 return rb_funcall(Qnil
, rb_intern("catch"), 1, tag
);
1241 rb_catch(const char *tag
, VALUE (*func
)(), VALUE data
)
1244 return rb_iterate(catch_null_i
, 0, func
, data
);
1246 return rb_iterate(catch_i
, ID2SYM(rb_intern(tag
)), func
, data
);
1250 rb_catch_obj(VALUE tag
, VALUE (*func
)(), VALUE data
)
1252 return rb_iterate((VALUE (*)_((VALUE
)))catch_i
, tag
, func
, data
);
1257 * caller(start=1) => array
1259 * Returns the current execution stack---an array containing strings in
1260 * the form ``<em>file:line</em>'' or ``<em>file:line: in
1261 * `method'</em>''. The optional _start_ parameter
1262 * determines the number of initial stack entries to omit from the
1274 * c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
1275 * c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
1276 * c(2) #=> ["prog:8:in `c'", "prog:12"]
1277 * c(3) #=> ["prog:13"]
1281 rb_f_caller(int argc
, VALUE
*argv
)
1286 rb_scan_args(argc
, argv
, "01", &level
);
1291 lev
= NUM2INT(level
);
1293 rb_raise(rb_eArgError
, "negative level (%d)", lev
);
1295 return vm_backtrace(GET_THREAD(), lev
);
1304 ary
= vm_backtrace(GET_THREAD(), -1);
1305 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
1306 printf("\tfrom %s\n", RSTRING_PTR(RARRAY_PTR(ary
)[i
]));
1311 rb_make_backtrace(void)
1313 return vm_backtrace(GET_THREAD(), -1);
1319 rb_define_global_function("catch", rb_f_catch
, -1);
1320 rb_define_global_function("throw", rb_f_throw
, -1);
1322 rb_define_global_function("loop", rb_f_loop
, 0);
1324 rb_define_method(rb_cBasicObject
, "instance_eval", rb_obj_instance_eval
, -1);
1325 rb_define_method(rb_cBasicObject
, "instance_exec", rb_obj_instance_exec
, -1);
1326 rb_define_private_method(rb_cBasicObject
, "method_missing", rb_method_missing
, -1);
1328 rb_define_method(rb_cBasicObject
, "__send__", rb_f_send
, -1);
1329 rb_define_method(rb_mKernel
, "send", rb_f_send
, -1);
1330 rb_define_method(rb_mKernel
, "public_send", rb_f_public_send
, -1);
1332 rb_define_method(rb_cModule
, "module_exec", rb_mod_module_exec
, -1);
1333 rb_define_method(rb_cModule
, "class_exec", rb_mod_module_exec
, -1);
1335 rb_define_global_function("caller", rb_f_caller
, -1);