1 /**********************************************************************
6 created at: Thu Jun 10 14:22:17 JST 1993
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 "eval_intern.h"
16 VALUE
proc_invoke(VALUE
, VALUE
, VALUE
, VALUE
);
17 VALUE
rb_binding_new();
19 VALUE
rb_f_block_given_p(void);
21 ID
rb_frame_callee(void);
22 static VALUE
rb_frame_self(void);
24 static ID removed
, singleton_removed
, undefined
, singleton_undefined
;
25 static ID init
, eqq
, each
, aref
, aset
, match
, missing
;
26 static ID added
, singleton_added
;
27 static ID object_id
, __send__
, respond_to
;
29 VALUE rb_eLocalJumpError
;
30 VALUE rb_eSysStackError
;
33 static VALUE exception_error
;
35 static VALUE
eval(VALUE
, VALUE
, VALUE
, const char *, int);
37 static inline VALUE
rb_yield_0(int argc
, VALUE
*argv
);
38 static VALUE
rb_call(VALUE
, VALUE
, ID
, int, const VALUE
*, int);
40 #include "eval_error.c"
41 #include "eval_method.c"
42 #include "eval_safe.c"
43 #include "eval_jump.c"
47 #if defined(__APPLE__)
48 #define environ (*_NSGetEnviron())
49 #elif !defined(_WIN32) && !defined(__MACOS__) || defined(_WIN32_WCE)
50 extern char **environ
;
52 char **rb_origenviron
;
54 void rb_clear_trace_func(void);
55 void rb_thread_stop_timer_thread(void);
57 void rb_call_inits(void);
58 void Init_stack(VALUE
*);
61 void Init_BareVM(void);
66 static int initialized
= 0;
76 rb_origenviron
= environ
;
79 Init_stack((void *)&state
);
84 if ((state
= EXEC_TAG()) == 0) {
105 extern void rb_clear_trace_func(void);
108 ruby_options(int argc
, char **argv
)
113 Init_stack((void *)&state
);
115 if ((state
= EXEC_TAG()) == 0) {
116 SAVE_ROOT_JMPBUF(GET_THREAD(), tree
= ruby_process_options(argc
, argv
));
119 rb_clear_trace_func();
120 state
= error_handle(state
);
121 tree
= (void *)INT2FIX(state
);
128 ruby_finalize_0(void)
130 rb_clear_trace_func();
132 if (EXEC_TAG() == 0) {
140 ruby_finalize_1(void)
143 GET_THREAD()->errinfo
= Qnil
;
144 rb_gc_call_finalizer_at_exit();
154 void rb_thread_stop_timer_thread(void);
160 volatile VALUE errs
[2];
161 rb_thread_t
*th
= GET_THREAD();
164 errs
[1] = th
->errinfo
;
166 Init_stack((void *)&state
);
168 errs
[0] = th
->errinfo
;
170 if ((state
= EXEC_TAG()) == 0) {
171 SAVE_ROOT_JMPBUF(th
, rb_thread_terminate_all());
176 th
->errinfo
= errs
[1];
177 ex
= error_handle(ex
);
180 rb_thread_stop_timer_thread();
182 for (nerr
= 0; nerr
< sizeof(errs
) / sizeof(errs
[0]); ++nerr
) {
183 VALUE err
= errs
[nerr
];
185 if (!RTEST(err
)) continue;
187 /* th->errinfo contains a NODE while break'ing */
188 if (TYPE(err
) == T_NODE
) continue;
190 if (rb_obj_is_kind_of(err
, rb_eSystemExit
)) {
191 return sysexit_status(err
);
193 else if (rb_obj_is_kind_of(err
, rb_eSignal
)) {
194 VALUE sig
= rb_iv_get(err
, "signo");
195 ruby_default_signal(NUM2INT(sig
));
202 #if EXIT_SUCCESS != 0 || EXIT_FAILURE != 1
204 #if EXIT_SUCCESS != 0
205 case 0: return EXIT_SUCCESS
;
207 #if EXIT_FAILURE != 1
208 case 1: return EXIT_FAILURE
;
217 ruby_exec_node(void *n
, const char *file
)
220 VALUE iseq
= (VALUE
)n
;
221 rb_thread_t
*th
= GET_THREAD();
226 if ((state
= EXEC_TAG()) == 0) {
227 SAVE_ROOT_JMPBUF(th
, {
239 exit(ruby_cleanup(ex
));
243 ruby_run_node(void *n
)
248 case Qtrue
: return EXIT_SUCCESS
;
249 case Qfalse
: return EXIT_FAILURE
;
254 Init_stack((void *)&n
);
255 return ruby_cleanup(ruby_exec_node(n
, 0));
259 rb_eval_string(const char *str
)
261 return eval(rb_vm_top_self(), rb_str_new2(str
), Qnil
, "(eval)", 1);
265 rb_eval_string_protect(const char *str
, int *state
)
267 return rb_protect((VALUE (*)(VALUE
))rb_eval_string
, (VALUE
)str
, state
);
271 rb_eval_string_wrap(const char *str
, int *state
)
274 rb_thread_t
*th
= GET_THREAD();
275 VALUE self
= th
->top_self
;
276 VALUE wrapper
= th
->top_wrapper
;
279 th
->top_wrapper
= rb_module_new();
280 th
->top_self
= rb_obj_clone(rb_vm_top_self());
281 rb_extend_object(th
->top_self
, th
->top_wrapper
);
283 val
= rb_eval_string_protect(str
, &status
);
286 th
->top_wrapper
= wrapper
;
298 rb_eval_cmd(VALUE cmd
, VALUE arg
, int level
)
301 VALUE val
= Qnil
; /* OK */
302 volatile int safe
= rb_safe_level();
304 if (OBJ_TAINTED(cmd
)) {
308 if (TYPE(cmd
) != T_STRING
) {
310 rb_set_safe_level_force(level
);
311 if ((state
= EXEC_TAG()) == 0) {
312 val
= rb_funcall2(cmd
, rb_intern("call"), RARRAY_LEN(arg
),
317 rb_set_safe_level_force(safe
);
325 if ((state
= EXEC_TAG()) == 0) {
326 val
= eval(rb_vm_top_self(), cmd
, Qnil
, 0, 0);
330 rb_set_safe_level_force(safe
);
331 if (state
) vm_jump_tag_but_local_jump(state
, val
);
337 * Module.nesting => array
339 * Returns the list of +Modules+ nested at the point of call.
343 * $a = Module.nesting
346 * $a #=> [M1::M2, M1]
347 * $a[0].name #=> "M1::M2"
353 VALUE ary
= rb_ary_new();
354 NODE
*cref
= ruby_cref();
356 while (cref
&& cref
->nd_next
) {
357 VALUE klass
= cref
->nd_clss
;
359 rb_ary_push(ary
, klass
);
361 cref
= cref
->nd_next
;
368 * Module.constants => array
370 * Returns an array of the names of all constants defined in the
371 * system. This list includes the names of all modules and classes.
373 * p Module.constants.sort[1..5]
377 * ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
381 rb_mod_s_constants(int argc
, VALUE
*argv
, VALUE mod
)
383 NODE
*cref
= ruby_cref();
389 return rb_mod_constants(argc
, argv
, rb_cModule
);
393 klass
= cref
->nd_clss
;
395 data
= rb_mod_const_at(cref
->nd_clss
, data
);
400 cref
= cref
->nd_next
;
404 data
= rb_mod_const_of(cbase
, data
);
406 return rb_const_list(data
);
410 rb_frozen_class_p(VALUE klass
)
412 char *desc
= "something(?!)";
414 if (OBJ_FROZEN(klass
)) {
415 if (FL_TEST(klass
, FL_SINGLETON
))
418 switch (TYPE(klass
)) {
428 rb_error_frozen(desc
);
434 * obj.respond_to?(symbol, include_private=false) => true or false
436 * Returns +true+> if _obj_ responds to the given
437 * method. Private methods are included in the search only if the
438 * optional second parameter evaluates to +true+.
441 static NODE
*basic_respond_to
= 0;
444 rb_obj_respond_to(VALUE obj
, ID id
, int priv
)
446 VALUE klass
= CLASS_OF(obj
);
448 if (rb_method_node(klass
, respond_to
) == basic_respond_to
) {
449 return rb_method_boundp(klass
, id
, !priv
);
454 args
[n
++] = ID2SYM(id
);
457 return RTEST(rb_funcall2(obj
, respond_to
, n
, args
));
462 rb_respond_to(VALUE obj
, ID id
)
464 return rb_obj_respond_to(obj
, id
, Qfalse
);
469 * obj.respond_to?(symbol, include_private=false) => true or false
471 * Returns +true+> if _obj_ responds to the given
472 * method. Private methods are included in the search only if the
473 * optional second parameter evaluates to +true+.
477 obj_respond_to(int argc
, VALUE
*argv
, VALUE obj
)
482 rb_scan_args(argc
, argv
, "11", &mid
, &priv
);
484 if (rb_method_boundp(CLASS_OF(obj
), id
, !RTEST(priv
))) {
492 * mod.method_defined?(symbol) => true or false
494 * Returns +true+ if the named method is defined by
495 * _mod_ (or its included modules and, if _mod_ is a class,
496 * its ancestors). Public and protected methods are matched.
509 * A.method_defined? :method1 #=> true
510 * C.method_defined? "method1" #=> true
511 * C.method_defined? "method2" #=> true
512 * C.method_defined? "method3" #=> true
513 * C.method_defined? "method4" #=> false
517 rb_mod_method_defined(VALUE mod
, VALUE mid
)
519 return rb_method_boundp(mod
, rb_to_id(mid
), 1);
522 #define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
526 * mod.public_method_defined?(symbol) => true or false
528 * Returns +true+ if the named public method is defined by
529 * _mod_ (or its included modules and, if _mod_ is a class,
544 * A.method_defined? :method1 #=> true
545 * C.public_method_defined? "method1" #=> true
546 * C.public_method_defined? "method2" #=> false
547 * C.method_defined? "method2" #=> true
551 rb_mod_public_method_defined(VALUE mod
, VALUE mid
)
553 ID id
= rb_to_id(mid
);
556 method
= rb_method_node(mod
, id
);
558 if (VISI_CHECK(method
->nd_noex
, NOEX_PUBLIC
))
566 * mod.private_method_defined?(symbol) => true or false
568 * Returns +true+ if the named private method is defined by
569 * _ mod_ (or its included modules and, if _mod_ is a class,
584 * A.method_defined? :method1 #=> true
585 * C.private_method_defined? "method1" #=> false
586 * C.private_method_defined? "method2" #=> true
587 * C.method_defined? "method2" #=> false
591 rb_mod_private_method_defined(VALUE mod
, VALUE mid
)
593 ID id
= rb_to_id(mid
);
596 method
= rb_method_node(mod
, id
);
598 if (VISI_CHECK(method
->nd_noex
, NOEX_PRIVATE
))
606 * mod.protected_method_defined?(symbol) => true or false
608 * Returns +true+ if the named protected method is defined
609 * by _mod_ (or its included modules and, if _mod_ is a
610 * class, its ancestors).
624 * A.method_defined? :method1 #=> true
625 * C.protected_method_defined? "method1" #=> false
626 * C.protected_method_defined? "method2" #=> true
627 * C.method_defined? "method2" #=> true
631 rb_mod_protected_method_defined(VALUE mod
, VALUE mid
)
633 ID id
= rb_to_id(mid
);
636 method
= rb_method_node(mod
, id
);
638 if (VISI_CHECK(method
->nd_noex
, NOEX_PROTECTED
))
644 NORETURN(static void rb_longjmp(int, VALUE
));
645 static VALUE
make_backtrace(void);
648 rb_longjmp(int tag
, VALUE mesg
)
652 rb_thread_t
*th
= GET_THREAD();
656 if (rb_thread_set_raised(th
)) {
657 th
->errinfo
= exception_error
;
664 mesg
= rb_exc_new(rb_eRuntimeError
, 0, 0);
667 file
= rb_sourcefile();
668 if (file
) line
= rb_sourceline();
669 if (file
&& !NIL_P(mesg
)) {
670 at
= get_backtrace(mesg
);
672 at
= make_backtrace();
673 set_backtrace(mesg
, at
);
680 if (RTEST(ruby_debug
) && !NIL_P(e
= th
->errinfo
) &&
681 !rb_obj_is_kind_of(e
, rb_eSystemExit
)) {
685 if ((status
= EXEC_TAG()) == 0) {
686 RB_GC_GUARD(e
) = rb_obj_as_string(e
);
688 warn_printf("Exception `%s' at %s:%d - %s\n",
689 rb_obj_classname(th
->errinfo
),
690 file
, line
, RSTRING_PTR(e
));
693 warn_printf("Exception `%s' - %s\n",
694 rb_obj_classname(th
->errinfo
),
699 if (status
== TAG_FATAL
&& th
->errinfo
== exception_error
) {
703 rb_thread_reset_raised(th
);
708 rb_trap_restore_mask();
710 if (tag
!= TAG_FATAL
) {
711 EXEC_EVENT_HOOK(th
, RUBY_EVENT_RAISE
, th
->cfp
->self
,
712 0 /* TODO: id */, 0 /* TODO: klass */);
715 rb_thread_raised_clear(th
);
720 rb_exc_raise(VALUE mesg
)
722 rb_longjmp(TAG_RAISE
, mesg
);
726 rb_exc_fatal(VALUE mesg
)
728 rb_longjmp(TAG_FATAL
, mesg
);
734 static const char fmt
[1] = {'\0'};
735 rb_raise(rb_eInterrupt
, fmt
);
738 static VALUE
get_errinfo(void);
744 * raise(exception [, string [, array]])
747 * fail(exception [, string [, array]])
749 * With no arguments, raises the exception in <code>$!</code> or raises
750 * a <code>RuntimeError</code> if <code>$!</code> is +nil+.
751 * With a single +String+ argument, raises a
752 * +RuntimeError+ with the string as a message. Otherwise,
753 * the first parameter should be the name of an +Exception+
754 * class (or an object that returns an +Exception+ object when sent
755 * an +exception+ message). The optional second parameter sets the
756 * message associated with the exception, and the third parameter is an
757 * array of callback information. Exceptions are caught by the
758 * +rescue+ clause of <code>begin...end</code> blocks.
760 * raise "Failed to create socket"
761 * raise ArgumentError, "No parameters", caller
765 rb_f_raise(int argc
, VALUE
*argv
)
775 rb_raise_jump(rb_make_exception(argc
, argv
));
776 return Qnil
; /* not reached */
780 rb_make_exception(int argc
, VALUE
*argv
)
794 if (TYPE(argv
[0]) == T_STRING
) {
795 mesg
= rb_exc_new3(rb_eRuntimeError
, argv
[0]);
805 exception
= rb_intern("exception");
806 if (!rb_respond_to(argv
[0], exception
)) {
807 rb_raise(rb_eTypeError
, "exception class/object expected");
809 mesg
= rb_funcall(argv
[0], exception
, n
, argv
[1]);
812 rb_raise(rb_eArgError
, "wrong number of arguments");
816 if (!rb_obj_is_kind_of(mesg
, rb_eException
))
817 rb_raise(rb_eTypeError
, "exception object expected");
819 set_backtrace(mesg
, argv
[2]);
826 rb_raise_jump(VALUE mesg
)
828 rb_thread_t
*th
= GET_THREAD();
829 th
->cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
);
831 rb_longjmp(TAG_RAISE
, mesg
);
841 rb_block_given_p(void)
843 rb_thread_t
*th
= GET_THREAD();
844 if (GC_GUARDED_PTR_REF(th
->cfp
->lfp
[0])) {
855 return rb_block_given_p();
860 * block_given? => true or false
861 * iterator? => true or false
863 * Returns <code>true</code> if <code>yield</code> would execute a
864 * block in the current context. The <code>iterator?</code> form
865 * is mildly deprecated.
875 * try { "hello" } #=> "hello"
876 * try do "hello" end #=> "hello"
883 rb_thread_t
*th
= GET_THREAD();
884 rb_control_frame_t
*cfp
= th
->cfp
;
885 cfp
= vm_get_ruby_level_cfp(th
, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
));
886 if (GC_GUARDED_PTR_REF(cfp
->lfp
[0])) {
894 VALUE rb_eThreadError
;
899 if (!rb_block_given_p()) {
900 vm_localjump_error("no block given", Qnil
, 0);
905 rb_yield_0(int argc
, VALUE
*argv
)
907 return vm_yield(GET_THREAD(), argc
, argv
);
913 volatile VALUE tmp
= val
;
915 tmp
= rb_yield_0(0, 0);
918 tmp
= rb_yield_0(1, &val
);
924 rb_yield_values(int n
, ...)
931 return rb_yield_0(0, 0);
934 argv
= ALLOCA_N(VALUE
, n
);
936 va_init_list(args
, n
);
937 for (i
=0; i
<n
; i
++) {
938 argv
[i
] = va_arg(args
, VALUE
);
942 return rb_yield_0(n
, argv
);
946 rb_yield_values2(int argc
, VALUE
*argv
)
948 return rb_yield_0(argc
, argv
);
952 rb_yield_splat(VALUE values
)
954 VALUE tmp
= rb_check_array_type(values
);
957 rb_raise(rb_eArgError
, "not an array");
959 v
= rb_yield_0(RARRAY_LEN(tmp
), RARRAY_PTR(tmp
));
976 * Repeatedly executes the block.
981 * break if !line or line =~ /^qQ/
985 * StopIteration raised in the block breaks the loop.
991 rb_rescue2(loop_i
, (VALUE
)0, 0, 0, rb_eStopIteration
, (VALUE
)0);
992 return Qnil
; /* dummy */
996 rb_iterate(VALUE (*it_proc
) (VALUE
), VALUE data1
,
997 VALUE (*bl_proc
) (ANYARGS
), VALUE data2
)
1000 volatile VALUE retval
= Qnil
;
1001 NODE
*node
= NEW_IFUNC(bl_proc
, data2
);
1002 rb_thread_t
*th
= GET_THREAD();
1003 rb_control_frame_t
*cfp
= th
->cfp
;
1006 state
= TH_EXEC_TAG();
1010 rb_block_t
*blockptr
= RUBY_VM_GET_BLOCK_PTR_IN_CFP(th
->cfp
);
1011 blockptr
->iseq
= (void *)node
;
1013 th
->passed_block
= blockptr
;
1015 retval
= (*it_proc
) (data1
);
1018 VALUE err
= th
->errinfo
;
1019 if (state
== TAG_BREAK
) {
1020 VALUE
*escape_dfp
= GET_THROWOBJ_CATCH_POINT(err
);
1021 VALUE
*cdfp
= cfp
->dfp
;
1023 if (cdfp
== escape_dfp
) {
1030 /* SDR(); printf("%p, %p\n", cdfp, escape_dfp); */
1033 else if (state
== TAG_RETRY
) {
1034 VALUE
*escape_dfp
= GET_THROWOBJ_CATCH_POINT(err
);
1035 VALUE
*cdfp
= cfp
->dfp
;
1037 if (cdfp
== escape_dfp
) {
1052 TH_JUMP_TAG(th
, state
);
1057 struct iter_method_arg
{
1065 iterate_method(VALUE obj
)
1067 struct iter_method_arg
*arg
;
1069 arg
= (struct iter_method_arg
*)obj
;
1070 return rb_call(CLASS_OF(arg
->obj
), arg
->obj
, arg
->mid
,
1071 arg
->argc
, arg
->argv
, CALL_FCALL
);
1075 rb_block_call(VALUE obj
, ID mid
, int argc
, VALUE
*argv
,
1076 VALUE (*bl_proc
) (ANYARGS
), VALUE data2
)
1078 struct iter_method_arg arg
;
1084 return rb_iterate(iterate_method
, (VALUE
)&arg
, bl_proc
, data2
);
1090 return rb_call(CLASS_OF(obj
), obj
, rb_intern("each"), 0, 0, CALL_FCALL
);
1094 rb_rescue2(VALUE (*b_proc
) (ANYARGS
), VALUE data1
, VALUE (*r_proc
) (ANYARGS
),
1098 rb_thread_t
*th
= GET_THREAD();
1099 rb_control_frame_t
*cfp
= th
->cfp
;
1100 volatile VALUE result
;
1101 volatile VALUE e_info
= th
->errinfo
;
1105 if ((state
= EXEC_TAG()) == 0) {
1107 result
= (*b_proc
) (data1
);
1110 th
->cfp
= cfp
; /* restore */
1112 if (state
== TAG_RAISE
) {
1113 int handle
= Qfalse
;
1116 va_init_list(args
, data2
);
1117 while ((eclass
= va_arg(args
, VALUE
)) != 0) {
1118 if (rb_obj_is_kind_of(th
->errinfo
, eclass
)) {
1128 if ((state
= EXEC_TAG()) == 0) {
1129 result
= (*r_proc
) (data2
, th
->errinfo
);
1132 if (state
== TAG_RETRY
) {
1143 th
->errinfo
= e_info
;
1156 rb_rescue(VALUE (*b_proc
)(ANYARGS
), VALUE data1
, VALUE (*r_proc
)(ANYARGS
), VALUE data2
)
1158 return rb_rescue2(b_proc
, data1
, r_proc
, data2
, rb_eStandardError
,
1163 rb_protect(VALUE (*proc
) (VALUE
), VALUE data
, int *state
)
1165 VALUE result
= Qnil
; /* OK */
1167 rb_thread_t
*th
= GET_THREAD();
1168 rb_control_frame_t
*cfp
= th
->cfp
;
1169 struct rb_vm_trap_tag trap_tag
;
1170 rb_jmpbuf_t org_jmpbuf
;
1172 trap_tag
.prev
= th
->trap_tag
;
1175 th
->trap_tag
= &trap_tag
;
1176 MEMCPY(&org_jmpbuf
, &(th
)->root_jmpbuf
, rb_jmpbuf_t
, 1);
1177 if ((status
= EXEC_TAG()) == 0) {
1178 SAVE_ROOT_JMPBUF(th
, result
= (*proc
) (data
));
1180 MEMCPY(&(th
)->root_jmpbuf
, &org_jmpbuf
, rb_jmpbuf_t
, 1);
1181 th
->trap_tag
= trap_tag
.prev
;
1196 rb_ensure(VALUE (*b_proc
)(ANYARGS
), VALUE data1
, VALUE (*e_proc
)(ANYARGS
), VALUE data2
)
1199 volatile VALUE result
= Qnil
;
1202 if ((state
= EXEC_TAG()) == 0) {
1203 result
= (*b_proc
) (data1
);
1207 /* retval = prot_tag ? prot_tag->retval : Qnil; */ /* save retval */
1215 rb_with_disable_interrupt(VALUE (*proc
)(ANYARGS
), VALUE data
)
1217 VALUE result
= Qnil
; /* OK */
1222 int thr_critical
= rb_thread_critical
;
1224 rb_thread_critical
= Qtrue
;
1226 if ((status
= EXEC_TAG()) == 0) {
1227 result
= (*proc
) (data
);
1230 rb_thread_critical
= thr_critical
;
1242 rb_thread_t
*th
= GET_THREAD();
1244 if (!rb_thread_raised_p(th
, RAISED_STACKOVERFLOW
) && ruby_stack_check()) {
1245 rb_thread_raised_set(th
, RAISED_STACKOVERFLOW
);
1246 rb_exc_raise(sysstack_error
);
1252 * obj.method_missing(symbol [, *args] ) => result
1254 * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
1255 * <i>symbol</i> is the symbol for the method called, and <i>args</i>
1256 * are any arguments that were passed to it. By default, the interpreter
1257 * raises an error when this method is called. However, it is possible
1258 * to override the method to provide more dynamic behavior.
1259 * If it is decided that a particular method should not be handled, then
1260 * <i>super</i> should be called, so that ancestors can pick up the
1262 * The example below creates
1263 * a class <code>Roman</code>, which responds to methods with names
1264 * consisting of roman numerals, returning the corresponding integer
1268 * def romanToInt(str)
1271 * def method_missing(methId)
1272 * str = methId.id2name
1284 rb_method_missing(int argc
, const VALUE
*argv
, VALUE obj
)
1287 VALUE exc
= rb_eNoMethodError
;
1289 rb_thread_t
*th
= GET_THREAD();
1290 int last_call_status
= th
->method_missing_reason
;
1291 if (argc
== 0 || !SYMBOL_P(argv
[0])) {
1292 rb_raise(rb_eArgError
, "no id given");
1297 id
= SYM2ID(argv
[0]);
1299 if (last_call_status
& NOEX_PRIVATE
) {
1300 format
= "private method `%s' called for %s";
1302 else if (last_call_status
& NOEX_PROTECTED
) {
1303 format
= "protected method `%s' called for %s";
1305 else if (last_call_status
& NOEX_VCALL
) {
1306 format
= "undefined local variable or method `%s' for %s";
1307 exc
= rb_eNameError
;
1309 else if (last_call_status
& NOEX_SUPER
) {
1310 format
= "super: no superclass method `%s' for %s";
1313 format
= "undefined method `%s' for %s";
1319 args
[n
++] = rb_funcall(rb_const_get(exc
, rb_intern("message")), '!',
1320 3, rb_str_new2(format
), obj
, argv
[0]);
1321 args
[n
++] = argv
[0];
1322 if (exc
== rb_eNoMethodError
) {
1323 args
[n
++] = rb_ary_new4(argc
- 1, argv
+ 1);
1325 exc
= rb_class_new_instance(n
, args
, exc
);
1327 th
->cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
);
1331 return Qnil
; /* not reached */
1335 method_missing(VALUE obj
, ID id
, int argc
, const VALUE
*argv
, int call_status
)
1338 GET_THREAD()->method_missing_reason
= call_status
;
1340 if (id
== missing
) {
1341 rb_method_missing(argc
, argv
, obj
);
1343 else if (id
== ID_ALLOCATOR
) {
1344 rb_raise(rb_eTypeError
, "allocator undefined for %s",
1345 rb_class2name(obj
));
1348 nargv
= ALLOCA_N(VALUE
, argc
+ 1);
1349 nargv
[0] = ID2SYM(id
);
1350 MEMCPY(nargv
+ 1, argv
, VALUE
, argc
);
1352 return rb_funcall2(obj
, missing
, argc
+ 1, nargv
);
1356 rb_call0(VALUE klass
, VALUE recv
, ID mid
, int argc
, const VALUE
*argv
, int scope
, VALUE self
)
1358 NODE
*body
, *method
;
1361 struct cache_entry
*ent
;
1362 rb_thread_t
*th
= GET_THREAD();
1365 rb_raise(rb_eNotImpError
,
1366 "method `%s' called on terminated object (%p)",
1367 rb_id2name(mid
), (void *)recv
);
1369 /* is it in the method cache? */
1370 ent
= cache
+ EXPR1(klass
, mid
);
1372 if (ent
->mid
== mid
&& ent
->klass
== klass
) {
1374 return method_missing(recv
, mid
, argc
, argv
,
1375 scope
== 2 ? NOEX_VCALL
: 0);
1377 noex
= ent
->method
->nd_noex
;
1378 klass
= ent
->method
->nd_clss
;
1379 body
= ent
->method
->nd_body
;
1381 else if ((method
= rb_get_method_body(klass
, id
, &id
)) != 0) {
1382 noex
= method
->nd_noex
;
1383 klass
= method
->nd_clss
;
1384 body
= method
->nd_body
;
1388 return method_missing(recv
, mid
, argc
, argv
, NOEX_SUPER
);
1390 return method_missing(recv
, mid
, argc
, argv
,
1391 scope
== 2 ? NOEX_VCALL
: 0);
1395 if (mid
!= missing
) {
1396 /* receiver specified form for private method */
1397 if (UNLIKELY(noex
)) {
1398 if (((noex
& NOEX_MASK
) & NOEX_PRIVATE
) && scope
== 0) {
1399 return method_missing(recv
, mid
, argc
, argv
, NOEX_PRIVATE
);
1402 /* self must be kind of a specified form for protected method */
1403 if (((noex
& NOEX_MASK
) & NOEX_PROTECTED
) && scope
== 0) {
1404 VALUE defined_class
= klass
;
1406 if (TYPE(defined_class
) == T_ICLASS
) {
1407 defined_class
= RBASIC(defined_class
)->klass
;
1410 if (!rb_obj_is_kind_of(self
, rb_class_real(defined_class
))) {
1411 return method_missing(recv
, mid
, argc
, argv
, NOEX_PROTECTED
);
1415 if (NOEX_SAFE(noex
) > th
->safe_level
) {
1416 rb_raise(rb_eSecurityError
, "calling insecure method: %s", rb_id2name(mid
));
1428 //for(i=0; i<level; i++){printf(" ");}
1429 //printf("invoke %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
1431 //printf("%s with %d args\n", rb_id2name(mid), argc);
1433 val
= vm_call0(th
, klass
, recv
, mid
, id
, argc
, argv
, body
,
1434 noex
& NOEX_NOSUPER
);
1437 //for(i=0; i<level; i++){printf(" ");}
1438 //printf("done %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
1445 rb_call(VALUE klass
, VALUE recv
, ID mid
, int argc
, const VALUE
*argv
, int scope
)
1447 return rb_call0(klass
, recv
, mid
, argc
, argv
, scope
, rb_frame_self());
1451 rb_apply(VALUE recv
, ID mid
, VALUE args
)
1456 argc
= RARRAY_LEN(args
); /* Assigns LONG, but argc is INT */
1457 argv
= ALLOCA_N(VALUE
, argc
);
1458 MEMCPY(argv
, RARRAY_PTR(args
), VALUE
, argc
);
1459 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_FCALL
);
1463 send_internal(int argc
, VALUE
*argv
, VALUE recv
, int scope
)
1466 VALUE self
= RUBY_VM_PREVIOUS_CONTROL_FRAME(GET_THREAD()->cfp
)->self
;
1469 rb_raise(rb_eArgError
, "no method name given");
1472 vid
= *argv
++; argc
--;
1473 PASS_PASSED_BLOCK();
1474 return rb_call0(CLASS_OF(recv
), recv
, rb_to_id(vid
), argc
, argv
, scope
, self
);
1479 * obj.send(symbol [, args...]) => obj
1480 * obj.__send__(symbol [, args...]) => obj
1482 * Invokes the method identified by _symbol_, passing it any
1483 * arguments specified. You can use <code>__send__</code> if the name
1484 * +send+ clashes with an existing method in _obj_.
1488 * "Hello " + args.join(' ')
1492 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
1496 rb_f_send(int argc
, VALUE
*argv
, VALUE recv
)
1498 return send_internal(argc
, argv
, recv
, NOEX_NOSUPER
| NOEX_PRIVATE
);
1504 * obj.public_send(symbol [, args...]) => obj
1506 * Invokes the method identified by _symbol_, passing it any
1507 * arguments specified. Unlike send, public_send calls public
1510 * 1.public_send(:puts, "hello") # causes NoMethodError
1514 rb_f_public_send(int argc
, VALUE
*argv
, VALUE recv
)
1516 return send_internal(argc
, argv
, recv
, NOEX_PUBLIC
);
1520 rb_funcall(VALUE recv
, ID mid
, int n
, ...)
1524 va_init_list(ar
, n
);
1529 argv
= ALLOCA_N(VALUE
, n
);
1531 for (i
= 0; i
< n
; i
++) {
1532 argv
[i
] = va_arg(ar
, VALUE
);
1539 return rb_call(CLASS_OF(recv
), recv
, mid
, n
, argv
, CALL_FCALL
);
1543 rb_funcall2(VALUE recv
, ID mid
, int argc
, const VALUE
*argv
)
1545 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_FCALL
);
1549 rb_funcall3(VALUE recv
, ID mid
, int argc
, const VALUE
*argv
)
1551 return rb_call(CLASS_OF(recv
), recv
, mid
, argc
, argv
, CALL_PUBLIC
);
1557 return vm_backtrace(GET_THREAD(), lev
);
1562 * caller(start=1) => array
1564 * Returns the current execution stack---an array containing strings in
1565 * the form ``<em>file:line</em>'' or ``<em>file:line: in
1566 * `method'</em>''. The optional _start_ parameter
1567 * determines the number of initial stack entries to omit from the
1579 * c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
1580 * c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
1581 * c(2) #=> ["prog:8:in `c'", "prog:12"]
1582 * c(3) #=> ["prog:13"]
1586 rb_f_caller(int argc
, VALUE
*argv
)
1591 rb_scan_args(argc
, argv
, "01", &level
);
1596 lev
= NUM2INT(level
);
1598 rb_raise(rb_eArgError
, "negative level (%d)", lev
);
1600 return backtrace(lev
);
1609 ary
= backtrace(-1);
1610 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
1611 printf("\tfrom %s\n", RSTRING_PTR(RARRAY_PTR(ary
)[i
]));
1616 make_backtrace(void)
1618 return backtrace(-1);
1622 frame_func_id(rb_control_frame_t
*cfp
)
1624 rb_iseq_t
*iseq
= cfp
->iseq
;
1626 return cfp
->method_id
;
1629 if (RUBY_VM_IFUNC_P(iseq
)) {
1630 return rb_intern("<ifunc>");
1632 if (iseq
->defined_method_id
) {
1633 return iseq
->defined_method_id
;
1635 if (iseq
->local_iseq
== iseq
) {
1638 iseq
= iseq
->parent_iseq
;
1644 rb_frame_this_func(void)
1646 return frame_func_id(GET_THREAD()->cfp
);
1650 rb_frame_callee(void)
1652 rb_thread_t
*th
= GET_THREAD();
1653 rb_control_frame_t
*prev_cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
);
1654 /* check if prev_cfp can be accessible */
1655 if ((void *)(th
->stack
+ th
->stack_size
) == (void *)(prev_cfp
)) {
1658 return frame_func_id(prev_cfp
);
1664 rb_thread_t
*th
= GET_THREAD();
1665 th
->cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
);
1671 return GET_THREAD()->cfp
->self
;
1675 eval(VALUE self
, VALUE src
, VALUE scope
, const char *file
, int line
)
1678 VALUE result
= Qundef
;
1680 rb_binding_t
*bind
= 0;
1681 rb_thread_t
*th
= GET_THREAD();
1682 rb_env_t
*env
= NULL
;
1683 NODE
*stored_cref_stack
= 0;
1686 file
= rb_sourcefile();
1687 line
= rb_sourceline();
1691 if ((state
= EXEC_TAG()) == 0) {
1693 volatile VALUE iseqval
;
1695 if (scope
!= Qnil
) {
1696 if (rb_obj_is_kind_of(scope
, rb_cBinding
)) {
1697 GetBindingPtr(scope
, bind
);
1699 stored_cref_stack
= bind
->cref_stack
;
1702 rb_raise(rb_eTypeError
,
1703 "wrong argument type %s (expected Binding)",
1704 rb_obj_classname(scope
));
1706 GetEnvPtr(envval
, env
);
1707 th
->base_block
= &env
->block
;
1710 rb_control_frame_t
*cfp
= vm_get_ruby_level_cfp(th
, th
->cfp
);
1711 th
->base_block
= RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp
);
1712 th
->base_block
->iseq
= cfp
->iseq
; /* TODO */
1715 /* make eval iseq */
1716 th
->parse_in_eval
++;
1717 iseqval
= rb_iseq_compile(src
, rb_str_new2(file
), INT2FIX(line
));
1718 th
->parse_in_eval
--;
1719 rb_vm_set_eval_stack(th
, iseqval
);
1722 if (0) { /* for debug */
1723 extern VALUE
ruby_iseq_disasm(VALUE
);
1724 printf("%s\n", RSTRING_PTR(ruby_iseq_disasm(iseqval
)));
1728 GetISeqPtr(iseqval
, iseq
);
1729 if (bind
&& iseq
->local_size
> 0) {
1730 bind
->env
= vm_make_env_object(th
, th
->cfp
);
1734 if (stored_cref_stack
) {
1736 vm_set_special_cref(th
, env
->block
.lfp
, stored_cref_stack
);
1740 CHECK_STACK_OVERFLOW(th
->cfp
, iseq
->stack_max
);
1741 result
= vm_eval_body(th
);
1745 if (stored_cref_stack
) {
1746 vm_set_special_cref(th
, env
->block
.lfp
, stored_cref_stack
);
1750 if (state
== TAG_RAISE
) {
1751 VALUE errinfo
= th
->errinfo
;
1752 if (strcmp(file
, "(eval)") == 0) {
1753 VALUE mesg
, errat
, bt2
;
1755 errat
= get_backtrace(errinfo
);
1756 mesg
= rb_attr_get(errinfo
, rb_intern("mesg"));
1757 if (!NIL_P(errat
) && TYPE(errat
) == T_ARRAY
&&
1758 (bt2
= backtrace(-2), RARRAY_LEN(bt2
) > 0)) {
1759 if (!NIL_P(mesg
) && TYPE(mesg
) == T_STRING
&& !RSTRING_LEN(mesg
)) {
1760 rb_str_update(mesg
, 0, 0, rb_str_new2(": "));
1761 rb_str_update(mesg
, 0, 0, RARRAY_PTR(errat
)[0]);
1763 RARRAY_PTR(errat
)[0] = RARRAY_PTR(bt2
)[0];
1766 rb_exc_raise(errinfo
);
1775 * eval(string [, binding [, filename [,lineno]]]) => obj
1777 * Evaluates the Ruby expression(s) in <em>string</em>. If
1778 * <em>binding</em> is given, the evaluation is performed in its
1779 * context. The binding may be a <code>Binding</code> object or a
1780 * <code>Proc</code> object. If the optional <em>filename</em> and
1781 * <em>lineno</em> parameters are present, they will be used when
1782 * reporting syntax errors.
1784 * def getBinding(str)
1788 * eval "str + ' Fred'" #=> "hello Fred"
1789 * eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
1793 rb_f_eval(int argc
, VALUE
*argv
, VALUE self
)
1795 VALUE src
, scope
, vfile
, vline
;
1796 char *file
= "(eval)";
1799 rb_scan_args(argc
, argv
, "13", &src
, &scope
, &vfile
, &vline
);
1800 if (rb_safe_level() >= 4) {
1802 if (!NIL_P(scope
) && !OBJ_TAINTED(scope
)) {
1803 rb_raise(rb_eSecurityError
,
1804 "Insecure: can't modify trusted binding");
1808 SafeStringValue(src
);
1814 line
= NUM2INT(vline
);
1818 file
= RSTRING_PTR(vfile
);
1819 return eval(self
, src
, scope
, file
, line
);
1822 VALUE
vm_cfp_svar_get(rb_thread_t
*th
, rb_control_frame_t
*cfp
, VALUE key
);
1823 void vm_cfp_svar_set(rb_thread_t
*th
, rb_control_frame_t
*cfp
, VALUE key
, VALUE val
);
1825 /* function to call func under the specified class/module context */
1827 exec_under(VALUE (*func
) (VALUE
), VALUE under
, VALUE self
, VALUE args
)
1829 VALUE val
= Qnil
; /* OK */
1830 rb_thread_t
*th
= GET_THREAD();
1831 rb_control_frame_t
*cfp
= th
->cfp
;
1832 rb_control_frame_t
*pcfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1833 VALUE stored_self
= pcfp
->self
;
1834 NODE
*stored_cref
= 0;
1837 rb_block_t
*blockptr
;
1840 /* replace environment */
1842 if ((blockptr
= GC_GUARDED_PTR_REF(*th
->cfp
->lfp
)) != 0) {
1843 /* copy block info */
1847 *th
->cfp
->lfp
= GC_GUARDED_PTR(&block
);
1850 while (!RUBY_VM_NORMAL_ISEQ_P(cfp
->iseq
)) {
1851 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1854 stored_cref
= (NODE
*)vm_cfp_svar_get(th
, cfp
, 2);
1855 vm_cfp_svar_set(th
, cfp
, 2, (VALUE
)vm_cref_push(th
, under
, NOEX_PUBLIC
));
1858 if ((state
= EXEC_TAG()) == 0) {
1859 val
= (*func
) (args
);
1863 /* restore environment */
1864 vm_cfp_svar_set(th
, cfp
, 2, (VALUE
)stored_cref
);
1865 pcfp
->self
= stored_self
;
1874 yield_under_i(VALUE arg
)
1876 if (arg
== Qundef
) {
1877 return rb_yield_0(0, 0);
1880 return rb_yield_0(RARRAY_LEN(arg
), RARRAY_PTR(arg
));
1884 /* block eval under the class/module context */
1886 yield_under(VALUE under
, VALUE self
, VALUE values
)
1888 return exec_under(yield_under_i
, under
, self
, values
);
1892 eval_under_i(VALUE arg
)
1894 VALUE
*args
= (VALUE
*)arg
;
1895 return eval(args
[0], args
[1], Qnil
, (char *)args
[2], (int)args
[3]);
1898 /* string eval under the class/module context */
1900 eval_under(VALUE under
, VALUE self
, VALUE src
, const char *file
, int line
)
1904 if (rb_safe_level() >= 4) {
1908 SafeStringValue(src
);
1912 args
[2] = (VALUE
)file
;
1913 args
[3] = (VALUE
)line
;
1914 return exec_under(eval_under_i
, under
, self
, (VALUE
)args
);
1918 specific_eval(int argc
, VALUE
*argv
, VALUE klass
, VALUE self
)
1920 if (rb_block_given_p()) {
1922 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 0)",
1925 return yield_under(klass
, self
, Qundef
);
1928 char *file
= "(eval)";
1932 rb_raise(rb_eArgError
, "block not supplied");
1935 if (rb_safe_level() >= 4) {
1936 StringValue(argv
[0]);
1939 SafeStringValue(argv
[0]);
1942 const char *name
= rb_id2name(rb_frame_callee());
1943 rb_raise(rb_eArgError
,
1944 "wrong number of arguments: %s(src) or %s{..}",
1948 line
= NUM2INT(argv
[2]);
1950 file
= StringValuePtr(argv
[1]);
1953 return eval_under(klass
, self
, argv
[0], file
, line
);
1959 * obj.instance_eval(string [, filename [, lineno]] ) => obj
1960 * obj.instance_eval {| | block } => obj
1962 * Evaluates a string containing Ruby source code, or the given block,
1963 * within the context of the receiver (_obj_). In order to set the
1964 * context, the variable +self+ is set to _obj_ while
1965 * the code is executing, giving the code access to _obj_'s
1966 * instance variables. In the version of <code>instance_eval</code>
1967 * that takes a +String+, the optional second and third
1968 * parameters supply a filename and starting line number that are used
1969 * when reporting compilation errors.
1971 * class KlassWithSecret
1976 * k = KlassWithSecret.new
1977 * k.instance_eval { @secret } #=> 99
1981 rb_obj_instance_eval(int argc
, VALUE
*argv
, VALUE self
)
1985 if (SPECIAL_CONST_P(self
)) {
1986 klass
= CLASS_OF(self
); /* klass = Qnil; */
1989 klass
= rb_singleton_class(self
);
1991 return specific_eval(argc
, argv
, klass
, self
);
1996 * obj.instance_exec(arg...) {|var...| block } => obj
1998 * Executes the given block within the context of the receiver
1999 * (_obj_). In order to set the context, the variable +self+ is set
2000 * to _obj_ while the code is executing, giving the code access to
2001 * _obj_'s instance variables. Arguments are passed as block parameters.
2003 * class KlassWithSecret
2008 * k = KlassWithSecret.new
2009 * k.instance_exec(5) {|x| @secret+x } #=> 104
2013 rb_obj_instance_exec(int argc
, VALUE
*argv
, VALUE self
)
2017 if (SPECIAL_CONST_P(self
)) {
2021 klass
= rb_singleton_class(self
);
2023 return yield_under(klass
, self
, rb_ary_new4(argc
, argv
));
2028 * mod.class_eval(string [, filename [, lineno]]) => obj
2029 * mod.module_eval {|| block } => obj
2031 * Evaluates the string or block in the context of _mod_. This can
2032 * be used to add methods to a class. <code>module_eval</code> returns
2033 * the result of evaluating its argument. The optional _filename_
2034 * and _lineno_ parameters set the text for error messages.
2038 * a = %q{def hello() "Hello there!" end}
2039 * Thing.module_eval(a)
2040 * puts Thing.new.hello()
2041 * Thing.module_eval("invalid code", "dummy", 123)
2043 * <em>produces:</em>
2046 * dummy:123:in `module_eval': undefined local variable
2047 * or method `code' for Thing:Class
2051 rb_mod_module_eval(int argc
, VALUE
*argv
, VALUE mod
)
2053 return specific_eval(argc
, argv
, mod
, mod
);
2058 * mod.module_exec(arg...) {|var...| block } => obj
2059 * mod.class_exec(arg...) {|var...| block } => obj
2061 * Evaluates the given block in the context of the class/module.
2062 * The method defined in the block will belong to the receiver.
2067 * def hello() "Hello there!" end
2069 * puts Thing.new.hello()
2071 * <em>produces:</em>
2077 rb_mod_module_exec(int argc
, VALUE
*argv
, VALUE mod
)
2079 return yield_under(mod
, mod
, rb_ary_new4(argc
, argv
));
2083 secure_visibility(VALUE self
)
2085 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self
)) {
2086 rb_raise(rb_eSecurityError
,
2087 "Insecure: can't change method visibility");
2092 set_method_visibility(VALUE self
, int argc
, VALUE
*argv
, ID ex
)
2095 secure_visibility(self
);
2096 for (i
= 0; i
< argc
; i
++) {
2097 rb_export_method(self
, rb_to_id(argv
[i
]), ex
);
2099 rb_clear_cache_by_class(self
);
2105 * public(symbol, ...) => self
2107 * With no arguments, sets the default visibility for subsequently
2108 * defined methods to public. With arguments, sets the named methods to
2109 * have public visibility.
2113 rb_mod_public(int argc
, VALUE
*argv
, VALUE module
)
2115 secure_visibility(module
);
2117 SCOPE_SET(NOEX_PUBLIC
);
2120 set_method_visibility(module
, argc
, argv
, NOEX_PUBLIC
);
2128 * protected(symbol, ...) => self
2130 * With no arguments, sets the default visibility for subsequently
2131 * defined methods to protected. With arguments, sets the named methods
2132 * to have protected visibility.
2136 rb_mod_protected(int argc
, VALUE
*argv
, VALUE module
)
2138 secure_visibility(module
);
2140 SCOPE_SET(NOEX_PROTECTED
);
2143 set_method_visibility(module
, argc
, argv
, NOEX_PROTECTED
);
2151 * private(symbol, ...) => self
2153 * With no arguments, sets the default visibility for subsequently
2154 * defined methods to private. With arguments, sets the named methods
2155 * to have private visibility.
2164 * Mod.private_instance_methods #=> [:a, :c]
2168 rb_mod_private(int argc
, VALUE
*argv
, VALUE module
)
2170 secure_visibility(module
);
2172 SCOPE_SET(NOEX_PRIVATE
);
2175 set_method_visibility(module
, argc
, argv
, NOEX_PRIVATE
);
2182 * mod.public_class_method(symbol, ...) => mod
2184 * Makes a list of existing class methods public.
2188 rb_mod_public_method(int argc
, VALUE
*argv
, VALUE obj
)
2190 set_method_visibility(CLASS_OF(obj
), argc
, argv
, NOEX_PUBLIC
);
2196 * mod.private_class_method(symbol, ...) => mod
2198 * Makes existing class methods private. Often used to hide the default
2199 * constructor <code>new</code>.
2201 * class SimpleSingleton # Not thread safe
2202 * private_class_method :new
2203 * def SimpleSingleton.create(*args, &block)
2204 * @me = new(*args, &block) if ! @me
2211 rb_mod_private_method(int argc
, VALUE
*argv
, VALUE obj
)
2213 set_method_visibility(CLASS_OF(obj
), argc
, argv
, NOEX_PRIVATE
);
2220 * public(symbol, ...)
2222 * With no arguments, sets the default visibility for subsequently
2223 * defined methods to public. With arguments, sets the named methods to
2224 * have public visibility.
2228 top_public(int argc
, VALUE
*argv
)
2230 return rb_mod_public(argc
, argv
, rb_cObject
);
2234 top_private(int argc
, VALUE
*argv
)
2236 return rb_mod_private(argc
, argv
, rb_cObject
);
2241 * module_function(symbol, ...) => self
2243 * Creates module functions for the named methods. These functions may
2244 * be called with the module as a receiver, and also become available
2245 * as instance methods to classes that mix in the module. Module
2246 * functions are copies of the original, and so may be changed
2247 * independently. The instance-method versions are made private. If
2248 * used with no arguments, subsequently defined methods become module
2255 * module_function :one
2263 * Mod.one #=> "This is one"
2265 * c.callOne #=> "This is one"
2268 * "This is the new one"
2271 * Mod.one #=> "This is one"
2272 * c.callOne #=> "This is the new one"
2276 rb_mod_modfunc(int argc
, VALUE
*argv
, VALUE module
)
2282 if (TYPE(module
) != T_MODULE
) {
2283 rb_raise(rb_eTypeError
, "module_function must be called for modules");
2286 secure_visibility(module
);
2288 SCOPE_SET(NOEX_MODFUNC
);
2292 set_method_visibility(module
, argc
, argv
, NOEX_PRIVATE
);
2294 for (i
= 0; i
< argc
; i
++) {
2297 id
= rb_to_id(argv
[i
]);
2299 fbody
= search_method(m
, id
, &m
);
2301 fbody
= search_method(rb_cObject
, id
, &m
);
2303 if (fbody
== 0 || fbody
->nd_body
== 0) {
2304 rb_bug("undefined method `%s'; can't happen", rb_id2name(id
));
2306 if (nd_type(fbody
->nd_body
->nd_body
) != NODE_ZSUPER
) {
2307 break; /* normal case: need not to follow 'super' link */
2309 m
= RCLASS_SUPER(m
);
2313 rb_add_method(rb_singleton_class(module
), id
, fbody
->nd_body
->nd_body
,
2321 * append_features(mod) => mod
2323 * When this module is included in another, Ruby calls
2324 * <code>append_features</code> in this module, passing it the
2325 * receiving module in _mod_. Ruby's default implementation is
2326 * to add the constants, methods, and module variables of this module
2327 * to _mod_ if this module has not already been added to
2328 * _mod_ or one of its ancestors. See also <code>Module#include</code>.
2332 rb_mod_append_features(VALUE module
, VALUE include
)
2334 switch (TYPE(include
)) {
2339 Check_Type(include
, T_CLASS
);
2342 rb_include_module(include
, module
);
2349 * include(module, ...) => self
2351 * Invokes <code>Module.append_features</code> on each parameter in turn.
2355 rb_mod_include(int argc
, VALUE
*argv
, VALUE module
)
2359 for (i
= 0; i
< argc
; i
++)
2360 Check_Type(argv
[i
], T_MODULE
);
2362 rb_funcall(argv
[argc
], rb_intern("append_features"), 1, module
);
2363 rb_funcall(argv
[argc
], rb_intern("included"), 1, module
);
2369 rb_obj_call_init(VALUE obj
, int argc
, VALUE
*argv
)
2371 PASS_PASSED_BLOCK();
2372 rb_funcall2(obj
, init
, argc
, argv
);
2376 rb_extend_object(VALUE obj
, VALUE module
)
2378 rb_include_module(rb_singleton_class(obj
), module
);
2383 * extend_object(obj) => obj
2385 * Extends the specified object by adding this module's constants and
2386 * methods (which are added as singleton methods). This is the callback
2387 * method used by <code>Object#extend</code>.
2390 * def Picky.extend_object(o)
2392 * puts "Can't add Picky to a String"
2394 * puts "Picky added to #{o.class}"
2399 * (s = Array.new).extend Picky # Call Object.extend
2400 * (s = "quick brown fox").extend Picky
2402 * <em>produces:</em>
2404 * Picky added to Array
2405 * Can't add Picky to a String
2409 rb_mod_extend_object(VALUE mod
, VALUE obj
)
2411 rb_extend_object(obj
, mod
);
2417 * obj.extend(module, ...) => obj
2419 * Adds to _obj_ the instance methods from each module given as a
2424 * "Hello from Mod.\n"
2430 * "Hello from Klass.\n"
2435 * k.hello #=> "Hello from Klass.\n"
2436 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
2437 * k.hello #=> "Hello from Mod.\n"
2441 rb_obj_extend(int argc
, VALUE
*argv
, VALUE obj
)
2446 rb_raise(rb_eArgError
, "wrong number of arguments (0 for 1)");
2448 for (i
= 0; i
< argc
; i
++)
2449 Check_Type(argv
[i
], T_MODULE
);
2451 rb_funcall(argv
[argc
], rb_intern("extend_object"), 1, obj
);
2452 rb_funcall(argv
[argc
], rb_intern("extended"), 1, obj
);
2459 * include(module, ...) => self
2461 * Invokes <code>Module.append_features</code>
2462 * on each parameter in turn. Effectively adds the methods and constants
2463 * in each module to the receiver.
2467 top_include(int argc
, VALUE
*argv
, VALUE self
)
2469 rb_thread_t
*th
= GET_THREAD();
2472 if (th
->top_wrapper
) {
2474 ("main#include in the wrapped load is effective only in wrapper module");
2475 return rb_mod_include(argc
, argv
, th
->top_wrapper
);
2477 return rb_mod_include(argc
, argv
, rb_cObject
);
2480 VALUE
rb_f_trace_var();
2481 VALUE
rb_f_untrace_var();
2486 rb_thread_t
*th
= GET_THREAD();
2487 rb_control_frame_t
*cfp
= th
->cfp
;
2488 rb_control_frame_t
*end_cfp
= RUBY_VM_END_CONTROL_FRAME(th
);
2490 while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp
, end_cfp
)) {
2491 if (RUBY_VM_NORMAL_ISEQ_P(cfp
->iseq
)) {
2492 if (cfp
->iseq
->type
== ISEQ_TYPE_RESCUE
) {
2493 return &cfp
->dfp
[-1];
2495 else if (cfp
->iseq
->type
== ISEQ_TYPE_ENSURE
&&
2496 TYPE(cfp
->dfp
[-1]) != T_NODE
&&
2497 !FIXNUM_P(cfp
->dfp
[-1])) {
2498 return &cfp
->dfp
[-1];
2501 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
2509 VALUE
*ptr
= errinfo_place();
2519 errinfo_getter(ID id
)
2521 return get_errinfo();
2526 errinfo_setter(VALUE val
, ID id
, VALUE
*var
)
2528 if (!NIL_P(val
) && !rb_obj_is_kind_of(val
, rb_eException
)) {
2529 rb_raise(rb_eTypeError
, "assigning non-exception to $!");
2532 VALUE
*ptr
= errinfo_place();
2537 rb_raise(rb_eRuntimeError
, "errinfo_setter: not in rescue clause.");
2546 rb_thread_t
*th
= GET_THREAD();
2551 rb_set_errinfo(VALUE err
)
2553 if (!NIL_P(err
) && !rb_obj_is_kind_of(err
, rb_eException
)) {
2554 rb_raise(rb_eTypeError
, "assigning non-exception to $!");
2556 GET_THREAD()->errinfo
= err
;
2560 rb_rubylevel_errinfo(void)
2562 return get_errinfo();
2568 VALUE err
= get_errinfo();
2570 return get_backtrace(err
);
2578 errat_setter(VALUE val
, ID id
, VALUE
*var
)
2580 VALUE err
= get_errinfo();
2582 rb_raise(rb_eArgError
, "$! not set");
2584 set_backtrace(err
, val
);
2587 int vm_collect_local_variables_in_heap(rb_thread_t
*th
, VALUE
*dfp
, VALUE ary
);
2591 * local_variables => array
2593 * Returns the names of the current local variables.
2599 * local_variables #=> ["fred", "i"]
2603 rb_f_local_variables(void)
2605 VALUE ary
= rb_ary_new();
2606 rb_thread_t
*th
= GET_THREAD();
2607 rb_control_frame_t
*cfp
=
2608 vm_get_ruby_level_cfp(th
, RUBY_VM_PREVIOUS_CONTROL_FRAME(th
->cfp
));
2613 for (i
= 0; i
< cfp
->iseq
->local_table_size
; i
++) {
2614 ID lid
= cfp
->iseq
->local_table
[i
];
2616 const char *vname
= rb_id2name(lid
);
2617 /* should skip temporary variable */
2619 rb_ary_push(ary
, ID2SYM(lid
));
2624 if (cfp
->lfp
!= cfp
->dfp
) {
2626 VALUE
*dfp
= GC_GUARDED_PTR_REF(cfp
->dfp
[0]);
2628 if (vm_collect_local_variables_in_heap(th
, dfp
, ary
)) {
2632 while (cfp
->dfp
!= dfp
) {
2633 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
2647 * __method__ => symbol
2648 * __callee__ => symbol
2650 * Returns the name of the current method as a Symbol.
2651 * If called outside of a method, it returns <code>nil</code>.
2656 rb_f_method_name(void)
2658 ID fname
= rb_frame_callee();
2661 return ID2SYM(fname
);
2671 /* TODO: fix position */
2672 GET_THREAD()->vm
->mark_object_ary
= rb_ary_new();
2674 init
= rb_intern("initialize");
2675 eqq
= rb_intern("===");
2676 each
= rb_intern("each");
2678 aref
= rb_intern("[]");
2679 aset
= rb_intern("[]=");
2680 match
= rb_intern("=~");
2681 missing
= rb_intern("method_missing");
2682 added
= rb_intern("method_added");
2683 singleton_added
= rb_intern("singleton_method_added");
2684 removed
= rb_intern("method_removed");
2685 singleton_removed
= rb_intern("singleton_method_removed");
2686 undefined
= rb_intern("method_undefined");
2687 singleton_undefined
= rb_intern("singleton_method_undefined");
2689 object_id
= rb_intern("object_id");
2690 __send__
= rb_intern("__send__");
2692 rb_define_virtual_variable("$@", errat_getter
, errat_setter
);
2693 rb_define_virtual_variable("$!", errinfo_getter
, 0);
2695 rb_define_private_method(rb_cBasicObject
, "method_missing", rb_method_missing
, -1);
2697 rb_define_global_function("eval", rb_f_eval
, -1);
2698 rb_define_global_function("iterator?", rb_f_block_given_p
, 0);
2699 rb_define_global_function("block_given?", rb_f_block_given_p
, 0);
2700 rb_define_global_function("loop", rb_f_loop
, 0);
2702 rb_define_method(rb_mKernel
, "respond_to?", obj_respond_to
, -1);
2703 respond_to
= rb_intern("respond_to?");
2704 basic_respond_to
= rb_method_node(rb_cObject
, respond_to
);
2705 rb_register_mark_object((VALUE
)basic_respond_to
);
2707 rb_define_global_function("raise", rb_f_raise
, -1);
2708 rb_define_global_function("fail", rb_f_raise
, -1);
2710 rb_define_global_function("caller", rb_f_caller
, -1);
2712 rb_define_global_function("global_variables", rb_f_global_variables
, 0); /* in variable.c */
2713 rb_define_global_function("local_variables", rb_f_local_variables
, 0);
2715 rb_define_global_function("__method__", rb_f_method_name
, 0);
2716 rb_define_global_function("__callee__", rb_f_method_name
, 0);
2718 rb_define_method(rb_cBasicObject
, "__send__", rb_f_send
, -1);
2719 rb_define_method(rb_mKernel
, "send", rb_f_send
, -1);
2720 rb_define_method(rb_mKernel
, "public_send", rb_f_public_send
, -1);
2722 rb_define_method(rb_cBasicObject
, "instance_eval", rb_obj_instance_eval
, -1);
2723 rb_define_method(rb_cBasicObject
, "instance_exec", rb_obj_instance_exec
, -1);
2725 rb_define_private_method(rb_cModule
, "append_features", rb_mod_append_features
, 1);
2726 rb_define_private_method(rb_cModule
, "extend_object", rb_mod_extend_object
, 1);
2727 rb_define_private_method(rb_cModule
, "include", rb_mod_include
, -1);
2728 rb_define_private_method(rb_cModule
, "public", rb_mod_public
, -1);
2729 rb_define_private_method(rb_cModule
, "protected", rb_mod_protected
, -1);
2730 rb_define_private_method(rb_cModule
, "private", rb_mod_private
, -1);
2731 rb_define_private_method(rb_cModule
, "module_function", rb_mod_modfunc
, -1);
2732 rb_define_method(rb_cModule
, "method_defined?", rb_mod_method_defined
, 1);
2733 rb_define_method(rb_cModule
, "public_method_defined?", rb_mod_public_method_defined
, 1);
2734 rb_define_method(rb_cModule
, "private_method_defined?", rb_mod_private_method_defined
, 1);
2735 rb_define_method(rb_cModule
, "protected_method_defined?", rb_mod_protected_method_defined
, 1);
2736 rb_define_method(rb_cModule
, "public_class_method", rb_mod_public_method
, -1);
2737 rb_define_method(rb_cModule
, "private_class_method", rb_mod_private_method
, -1);
2738 rb_define_method(rb_cModule
, "module_eval", rb_mod_module_eval
, -1);
2739 rb_define_method(rb_cModule
, "class_eval", rb_mod_module_eval
, -1);
2740 rb_define_method(rb_cModule
, "module_exec", rb_mod_module_exec
, -1);
2741 rb_define_method(rb_cModule
, "class_exec", rb_mod_module_exec
, -1);
2743 rb_undef_method(rb_cClass
, "module_function");
2747 rb_define_singleton_method(rb_cModule
, "nesting", rb_mod_nesting
, 0);
2748 rb_define_singleton_method(rb_cModule
, "constants", rb_mod_s_constants
, -1);
2750 rb_define_singleton_method(rb_vm_top_self(), "include", top_include
, -1);
2751 rb_define_singleton_method(rb_vm_top_self(), "public", top_public
, -1);
2752 rb_define_singleton_method(rb_vm_top_self(), "private", top_private
, -1);
2754 rb_define_method(rb_mKernel
, "extend", rb_obj_extend
, -1);
2756 rb_define_global_function("trace_var", rb_f_trace_var
, -1); /* in variable.c */
2757 rb_define_global_function("untrace_var", rb_f_untrace_var
, -1); /* in variable.c */
2759 rb_define_virtual_variable("$SAFE", safe_getter
, safe_setter
);
2761 exception_error
= rb_exc_new2(rb_eFatal
, "exception reentered");
2762 rb_ivar_set(exception_error
, idThrowState
, INT2FIX(TAG_FATAL
));
2763 rb_register_mark_object(exception_error
);
2770 rb_dvar_defined(ID id
)
2772 rb_thread_t
*th
= GET_THREAD();
2774 if (th
->base_block
&& (iseq
= th
->base_block
->iseq
)) {
2775 while (iseq
->type
== ISEQ_TYPE_BLOCK
||
2776 iseq
->type
== ISEQ_TYPE_RESCUE
||
2777 iseq
->type
== ISEQ_TYPE_ENSURE
||
2778 iseq
->type
== ISEQ_TYPE_EVAL
) {
2781 for (i
= 0; i
< iseq
->local_table_size
; i
++) {
2782 if (iseq
->local_table
[i
] == id
) {
2786 iseq
= iseq
->parent_iseq
;
2793 rb_local_defined(ID id
)
2795 rb_thread_t
*th
= GET_THREAD();
2798 if (th
->base_block
&& th
->base_block
->iseq
) {
2800 iseq
= th
->base_block
->iseq
->local_iseq
;
2802 for (i
=0; i
<iseq
->local_table_size
; i
++) {
2803 if (iseq
->local_table
[i
] == id
) {
2812 rb_parse_in_eval(void)
2814 return GET_THREAD()->parse_in_eval
!= 0;