add a test for IA64 Debian GNU/Linux Etch.
[ruby-svn.git] / eval.c
blobe5c8d47fe225f1dc0c0320b747b7c4e49ee2896b
1 /**********************************************************************
3 eval.c -
5 $Author$
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;
31 VALUE sysstack_error;
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"
45 /* initialize ruby */
47 #if defined(__APPLE__)
48 #define environ (*_NSGetEnviron())
49 #elif !defined(_WIN32) && !defined(__MACOS__) || defined(_WIN32_WCE)
50 extern char **environ;
51 #endif
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 *);
59 void Init_heap(void);
60 void Init_ext(void);
61 void Init_BareVM(void);
63 void
64 ruby_init(void)
66 static int initialized = 0;
67 int state;
69 if (initialized)
70 return;
71 initialized = 1;
73 #ifdef __MACOS__
74 rb_origenviron = 0;
75 #else
76 rb_origenviron = environ;
77 #endif
79 Init_stack((void *)&state);
80 Init_BareVM();
81 Init_heap();
83 PUSH_TAG();
84 if ((state = EXEC_TAG()) == 0) {
85 rb_call_inits();
87 #ifdef __MACOS__
88 _macruby_init();
89 #elif defined(__VMS)
90 _vmsruby_init();
91 #endif
93 ruby_prog_init();
94 ALLOW_INTS;
96 POP_TAG();
98 if (state) {
99 error_print();
100 exit(EXIT_FAILURE);
102 ruby_running = 1;
105 extern void rb_clear_trace_func(void);
107 void *
108 ruby_options(int argc, char **argv)
110 int state;
111 void *tree = 0;
113 Init_stack((void *)&state);
114 PUSH_TAG();
115 if ((state = EXEC_TAG()) == 0) {
116 SAVE_ROOT_JMPBUF(GET_THREAD(), tree = ruby_process_options(argc, argv));
118 else {
119 rb_clear_trace_func();
120 state = error_handle(state);
121 tree = (void *)INT2FIX(state);
123 POP_TAG();
124 return tree;
127 static void
128 ruby_finalize_0(void)
130 rb_clear_trace_func();
131 PUSH_TAG();
132 if (EXEC_TAG() == 0) {
133 rb_trap_exit();
135 POP_TAG();
136 rb_exec_end_proc();
139 static void
140 ruby_finalize_1(void)
142 ruby_sig_finalize();
143 GET_THREAD()->errinfo = Qnil;
144 rb_gc_call_finalizer_at_exit();
147 void
148 ruby_finalize(void)
150 ruby_finalize_0();
151 ruby_finalize_1();
154 void rb_thread_stop_timer_thread(void);
157 ruby_cleanup(int ex)
159 int state;
160 volatile VALUE errs[2];
161 rb_thread_t *th = GET_THREAD();
162 int nerr;
164 errs[1] = th->errinfo;
165 th->safe_level = 0;
166 Init_stack((void *)&state);
167 ruby_finalize_0();
168 errs[0] = th->errinfo;
169 PUSH_TAG();
170 if ((state = EXEC_TAG()) == 0) {
171 SAVE_ROOT_JMPBUF(th, rb_thread_terminate_all());
173 else if (ex == 0) {
174 ex = state;
176 th->errinfo = errs[1];
177 ex = error_handle(ex);
178 ruby_finalize_1();
179 POP_TAG();
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));
197 else if (ex == 0) {
198 ex = 1;
202 #if EXIT_SUCCESS != 0 || EXIT_FAILURE != 1
203 switch (ex) {
204 #if EXIT_SUCCESS != 0
205 case 0: return EXIT_SUCCESS;
206 #endif
207 #if EXIT_FAILURE != 1
208 case 1: return EXIT_FAILURE;
209 #endif
211 #endif
213 return ex;
217 ruby_exec_node(void *n, const char *file)
219 int state;
220 VALUE iseq = (VALUE)n;
221 rb_thread_t *th = GET_THREAD();
223 if (!n) return 0;
225 PUSH_TAG();
226 if ((state = EXEC_TAG()) == 0) {
227 SAVE_ROOT_JMPBUF(th, {
228 th->base_block = 0;
229 rb_iseq_eval(iseq);
232 POP_TAG();
233 return state;
236 void
237 ruby_stop(int ex)
239 exit(ruby_cleanup(ex));
243 ruby_run_node(void *n)
245 VALUE v = (VALUE)n;
247 switch (v) {
248 case Qtrue: return EXIT_SUCCESS;
249 case Qfalse: return EXIT_FAILURE;
251 if (FIXNUM_P(v)) {
252 return FIX2INT(v);
254 Init_stack((void *)&n);
255 return ruby_cleanup(ruby_exec_node(n, 0));
258 VALUE
259 rb_eval_string(const char *str)
261 return eval(rb_vm_top_self(), rb_str_new2(str), Qnil, "(eval)", 1);
264 VALUE
265 rb_eval_string_protect(const char *str, int *state)
267 return rb_protect((VALUE (*)(VALUE))rb_eval_string, (VALUE)str, state);
270 VALUE
271 rb_eval_string_wrap(const char *str, int *state)
273 int status;
274 rb_thread_t *th = GET_THREAD();
275 VALUE self = th->top_self;
276 VALUE wrapper = th->top_wrapper;
277 VALUE val;
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);
285 th->top_self = self;
286 th->top_wrapper = wrapper;
288 if (state) {
289 *state = status;
291 else if (status) {
292 JUMP_TAG(status);
294 return val;
297 VALUE
298 rb_eval_cmd(VALUE cmd, VALUE arg, int level)
300 int state;
301 VALUE val = Qnil; /* OK */
302 volatile int safe = rb_safe_level();
304 if (OBJ_TAINTED(cmd)) {
305 level = 4;
308 if (TYPE(cmd) != T_STRING) {
309 PUSH_TAG();
310 rb_set_safe_level_force(level);
311 if ((state = EXEC_TAG()) == 0) {
312 val = rb_funcall2(cmd, rb_intern("call"), RARRAY_LEN(arg),
313 RARRAY_PTR(arg));
315 POP_TAG();
317 rb_set_safe_level_force(safe);
319 if (state)
320 JUMP_TAG(state);
321 return val;
324 PUSH_TAG();
325 if ((state = EXEC_TAG()) == 0) {
326 val = eval(rb_vm_top_self(), cmd, Qnil, 0, 0);
328 POP_TAG();
330 rb_set_safe_level_force(safe);
331 if (state) vm_jump_tag_but_local_jump(state, val);
332 return val;
336 * call-seq:
337 * Module.nesting => array
339 * Returns the list of +Modules+ nested at the point of call.
341 * module M1
342 * module M2
343 * $a = Module.nesting
344 * end
345 * end
346 * $a #=> [M1::M2, M1]
347 * $a[0].name #=> "M1::M2"
350 static VALUE
351 rb_mod_nesting(void)
353 VALUE ary = rb_ary_new();
354 NODE *cref = ruby_cref();
356 while (cref && cref->nd_next) {
357 VALUE klass = cref->nd_clss;
358 if (!NIL_P(klass)) {
359 rb_ary_push(ary, klass);
361 cref = cref->nd_next;
363 return ary;
367 * call-seq:
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]
375 * <em>produces:</em>
377 * ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
380 static VALUE
381 rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
383 NODE *cref = ruby_cref();
384 VALUE klass;
385 VALUE cbase = 0;
386 void *data = 0;
388 if (argc > 0) {
389 return rb_mod_constants(argc, argv, rb_cModule);
392 while (cref) {
393 klass = cref->nd_clss;
394 if (!NIL_P(klass)) {
395 data = rb_mod_const_at(cref->nd_clss, data);
396 if (!cbase) {
397 cbase = klass;
400 cref = cref->nd_next;
403 if (cbase) {
404 data = rb_mod_const_of(cbase, data);
406 return rb_const_list(data);
409 void
410 rb_frozen_class_p(VALUE klass)
412 char *desc = "something(?!)";
414 if (OBJ_FROZEN(klass)) {
415 if (FL_TEST(klass, FL_SINGLETON))
416 desc = "object";
417 else {
418 switch (TYPE(klass)) {
419 case T_MODULE:
420 case T_ICLASS:
421 desc = "module";
422 break;
423 case T_CLASS:
424 desc = "class";
425 break;
428 rb_error_frozen(desc);
433 * call-seq:
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);
451 else {
452 VALUE args[2];
453 int n = 0;
454 args[n++] = ID2SYM(id);
455 if (priv)
456 args[n++] = Qtrue;
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);
468 * call-seq:
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+.
476 static VALUE
477 obj_respond_to(int argc, VALUE *argv, VALUE obj)
479 VALUE mid, priv;
480 ID id;
482 rb_scan_args(argc, argv, "11", &mid, &priv);
483 id = rb_to_id(mid);
484 if (rb_method_boundp(CLASS_OF(obj), id, !RTEST(priv))) {
485 return Qtrue;
487 return Qfalse;
491 * call-seq:
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.
498 * module A
499 * def method1() end
500 * end
501 * class B
502 * def method2() end
503 * end
504 * class C < B
505 * include A
506 * def method3() end
507 * end
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
516 static VALUE
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))
525 * call-seq:
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,
530 * its ancestors).
532 * module A
533 * def method1() end
534 * end
535 * class B
536 * protected
537 * def method2() end
538 * end
539 * class C < B
540 * include A
541 * def method3() end
542 * end
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
550 static VALUE
551 rb_mod_public_method_defined(VALUE mod, VALUE mid)
553 ID id = rb_to_id(mid);
554 NODE *method;
556 method = rb_method_node(mod, id);
557 if (method) {
558 if (VISI_CHECK(method->nd_noex, NOEX_PUBLIC))
559 return Qtrue;
561 return Qfalse;
565 * call-seq:
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,
570 * its ancestors).
572 * module A
573 * def method1() end
574 * end
575 * class B
576 * private
577 * def method2() end
578 * end
579 * class C < B
580 * include A
581 * def method3() end
582 * end
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
590 static VALUE
591 rb_mod_private_method_defined(VALUE mod, VALUE mid)
593 ID id = rb_to_id(mid);
594 NODE *method;
596 method = rb_method_node(mod, id);
597 if (method) {
598 if (VISI_CHECK(method->nd_noex, NOEX_PRIVATE))
599 return Qtrue;
601 return Qfalse;
605 * call-seq:
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).
612 * module A
613 * def method1() end
614 * end
615 * class B
616 * protected
617 * def method2() end
618 * end
619 * class C < B
620 * include A
621 * def method3() end
622 * end
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
630 static VALUE
631 rb_mod_protected_method_defined(VALUE mod, VALUE mid)
633 ID id = rb_to_id(mid);
634 NODE *method;
636 method = rb_method_node(mod, id);
637 if (method) {
638 if (VISI_CHECK(method->nd_noex, NOEX_PROTECTED))
639 return Qtrue;
641 return Qfalse;
644 NORETURN(static void rb_longjmp(int, VALUE));
645 static VALUE make_backtrace(void);
647 static void
648 rb_longjmp(int tag, VALUE mesg)
650 VALUE at;
651 VALUE e;
652 rb_thread_t *th = GET_THREAD();
653 const char *file;
654 int line = 0;
656 if (rb_thread_set_raised(th)) {
657 th->errinfo = exception_error;
658 JUMP_TAG(TAG_FATAL);
661 if (NIL_P(mesg))
662 mesg = th->errinfo;
663 if (NIL_P(mesg)) {
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);
671 if (NIL_P(at)) {
672 at = make_backtrace();
673 set_backtrace(mesg, at);
676 if (!NIL_P(mesg)) {
677 th->errinfo = mesg;
680 if (RTEST(ruby_debug) && !NIL_P(e = th->errinfo) &&
681 !rb_obj_is_kind_of(e, rb_eSystemExit)) {
682 int status;
684 PUSH_TAG();
685 if ((status = EXEC_TAG()) == 0) {
686 RB_GC_GUARD(e) = rb_obj_as_string(e);
687 if (file) {
688 warn_printf("Exception `%s' at %s:%d - %s\n",
689 rb_obj_classname(th->errinfo),
690 file, line, RSTRING_PTR(e));
692 else {
693 warn_printf("Exception `%s' - %s\n",
694 rb_obj_classname(th->errinfo),
695 RSTRING_PTR(e));
698 POP_TAG();
699 if (status == TAG_FATAL && th->errinfo == exception_error) {
700 th->errinfo = mesg;
702 else if (status) {
703 rb_thread_reset_raised(th);
704 JUMP_TAG(status);
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);
716 JUMP_TAG(tag);
719 void
720 rb_exc_raise(VALUE mesg)
722 rb_longjmp(TAG_RAISE, mesg);
725 void
726 rb_exc_fatal(VALUE mesg)
728 rb_longjmp(TAG_FATAL, mesg);
731 void
732 rb_interrupt(void)
734 static const char fmt[1] = {'\0'};
735 rb_raise(rb_eInterrupt, fmt);
738 static VALUE get_errinfo(void);
741 * call-seq:
742 * raise
743 * raise(string)
744 * raise(exception [, string [, array]])
745 * fail
746 * fail(string)
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
764 static VALUE
765 rb_f_raise(int argc, VALUE *argv)
767 VALUE err;
768 if (argc == 0) {
769 err = get_errinfo();
770 if (!NIL_P(err)) {
771 argc = 1;
772 argv = &err;
775 rb_raise_jump(rb_make_exception(argc, argv));
776 return Qnil; /* not reached */
779 VALUE
780 rb_make_exception(int argc, VALUE *argv)
782 VALUE mesg;
783 ID exception;
784 int n;
786 mesg = Qnil;
787 switch (argc) {
788 case 0:
789 mesg = Qnil;
790 break;
791 case 1:
792 if (NIL_P(argv[0]))
793 break;
794 if (TYPE(argv[0]) == T_STRING) {
795 mesg = rb_exc_new3(rb_eRuntimeError, argv[0]);
796 break;
798 n = 0;
799 goto exception_call;
801 case 2:
802 case 3:
803 n = 1;
804 exception_call:
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]);
810 break;
811 default:
812 rb_raise(rb_eArgError, "wrong number of arguments");
813 break;
815 if (argc > 0) {
816 if (!rb_obj_is_kind_of(mesg, rb_eException))
817 rb_raise(rb_eTypeError, "exception object expected");
818 if (argc > 2)
819 set_backtrace(mesg, argv[2]);
822 return mesg;
825 void
826 rb_raise_jump(VALUE mesg)
828 rb_thread_t *th = GET_THREAD();
829 th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
830 /* TODO: fix me */
831 rb_longjmp(TAG_RAISE, mesg);
834 void
835 rb_jump_tag(int tag)
837 JUMP_TAG(tag);
841 rb_block_given_p(void)
843 rb_thread_t *th = GET_THREAD();
844 if (GC_GUARDED_PTR_REF(th->cfp->lfp[0])) {
845 return Qtrue;
847 else {
848 return Qfalse;
853 rb_iterator_p()
855 return rb_block_given_p();
859 * call-seq:
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.
867 * def try
868 * if block_given?
869 * yield
870 * else
871 * "no block"
872 * end
873 * end
874 * try #=> "no block"
875 * try { "hello" } #=> "hello"
876 * try do "hello" end #=> "hello"
880 VALUE
881 rb_f_block_given_p()
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])) {
887 return Qtrue;
889 else {
890 return Qfalse;
894 VALUE rb_eThreadError;
896 void
897 rb_need_block()
899 if (!rb_block_given_p()) {
900 vm_localjump_error("no block given", Qnil, 0);
904 static inline VALUE
905 rb_yield_0(int argc, VALUE *argv)
907 return vm_yield(GET_THREAD(), argc, argv);
910 VALUE
911 rb_yield(VALUE val)
913 volatile VALUE tmp = val;
914 if (val == Qundef) {
915 tmp = rb_yield_0(0, 0);
917 else {
918 tmp = rb_yield_0(1, &val);
920 return tmp;
923 VALUE
924 rb_yield_values(int n, ...)
926 int i;
927 VALUE *argv;
928 va_list args;
930 if (n == 0) {
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);
940 va_end(args);
942 return rb_yield_0(n, argv);
945 VALUE
946 rb_yield_values2(int argc, VALUE *argv)
948 return rb_yield_0(argc, argv);
951 VALUE
952 rb_yield_splat(VALUE values)
954 VALUE tmp = rb_check_array_type(values);
955 volatile VALUE v;
956 if (NIL_P(tmp)) {
957 rb_raise(rb_eArgError, "not an array");
959 v = rb_yield_0(RARRAY_LEN(tmp), RARRAY_PTR(tmp));
960 return v;
963 static VALUE
964 loop_i()
966 for (;;) {
967 rb_yield_0(0, 0);
969 return Qnil;
973 * call-seq:
974 * loop {|| block }
976 * Repeatedly executes the block.
978 * loop do
979 * print "Input: "
980 * line = gets
981 * break if !line or line =~ /^qQ/
982 * # ...
983 * end
985 * StopIteration raised in the block breaks the loop.
988 static VALUE
989 rb_f_loop(void)
991 rb_rescue2(loop_i, (VALUE)0, 0, 0, rb_eStopIteration, (VALUE)0);
992 return Qnil; /* dummy */
995 VALUE
996 rb_iterate(VALUE (*it_proc) (VALUE), VALUE data1,
997 VALUE (*bl_proc) (ANYARGS), VALUE data2)
999 int state;
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;
1005 TH_PUSH_TAG(th);
1006 state = TH_EXEC_TAG();
1007 if (state == 0) {
1008 iter_retry:
1010 rb_block_t *blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(th->cfp);
1011 blockptr->iseq = (void *)node;
1012 blockptr->proc = 0;
1013 th->passed_block = blockptr;
1015 retval = (*it_proc) (data1);
1017 else {
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) {
1024 state = 0;
1025 th->state = 0;
1026 th->errinfo = Qnil;
1027 th->cfp = cfp;
1029 else{
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) {
1038 state = 0;
1039 th->state = 0;
1040 th->errinfo = Qnil;
1041 th->cfp = cfp;
1042 goto iter_retry;
1046 TH_POP_TAG();
1048 switch (state) {
1049 case 0:
1050 break;
1051 default:
1052 TH_JUMP_TAG(th, state);
1054 return retval;
1057 struct iter_method_arg {
1058 VALUE obj;
1059 ID mid;
1060 int argc;
1061 VALUE *argv;
1064 static VALUE
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);
1074 VALUE
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;
1080 arg.obj = obj;
1081 arg.mid = mid;
1082 arg.argc = argc;
1083 arg.argv = argv;
1084 return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2);
1087 VALUE
1088 rb_each(VALUE obj)
1090 return rb_call(CLASS_OF(obj), obj, rb_intern("each"), 0, 0, CALL_FCALL);
1093 VALUE
1094 rb_rescue2(VALUE (*b_proc) (ANYARGS), VALUE data1, VALUE (*r_proc) (ANYARGS),
1095 VALUE data2, ...)
1097 int state;
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;
1102 va_list args;
1104 PUSH_TAG();
1105 if ((state = EXEC_TAG()) == 0) {
1106 retry_entry:
1107 result = (*b_proc) (data1);
1109 else {
1110 th->cfp = cfp; /* restore */
1112 if (state == TAG_RAISE) {
1113 int handle = Qfalse;
1114 VALUE eclass;
1116 va_init_list(args, data2);
1117 while ((eclass = va_arg(args, VALUE)) != 0) {
1118 if (rb_obj_is_kind_of(th->errinfo, eclass)) {
1119 handle = Qtrue;
1120 break;
1123 va_end(args);
1125 if (handle) {
1126 if (r_proc) {
1127 PUSH_TAG();
1128 if ((state = EXEC_TAG()) == 0) {
1129 result = (*r_proc) (data2, th->errinfo);
1131 POP_TAG();
1132 if (state == TAG_RETRY) {
1133 state = 0;
1134 th->errinfo = Qnil;
1135 goto retry_entry;
1138 else {
1139 result = Qnil;
1140 state = 0;
1142 if (state == 0) {
1143 th->errinfo = e_info;
1148 POP_TAG();
1149 if (state)
1150 JUMP_TAG(state);
1152 return result;
1155 VALUE
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,
1159 (VALUE)0);
1162 VALUE
1163 rb_protect(VALUE (*proc) (VALUE), VALUE data, int *state)
1165 VALUE result = Qnil; /* OK */
1166 int status;
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;
1174 PUSH_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;
1182 POP_TAG();
1184 if (state) {
1185 *state = status;
1187 if (status != 0) {
1188 th->cfp = cfp;
1189 return Qnil;
1192 return result;
1195 VALUE
1196 rb_ensure(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*e_proc)(ANYARGS), VALUE data2)
1198 int state;
1199 volatile VALUE result = Qnil;
1201 PUSH_TAG();
1202 if ((state = EXEC_TAG()) == 0) {
1203 result = (*b_proc) (data1);
1205 POP_TAG();
1206 /* TODO: fix me */
1207 /* retval = prot_tag ? prot_tag->retval : Qnil; */ /* save retval */
1208 (*e_proc) (data2);
1209 if (state)
1210 JUMP_TAG(state);
1211 return result;
1214 VALUE
1215 rb_with_disable_interrupt(VALUE (*proc)(ANYARGS), VALUE data)
1217 VALUE result = Qnil; /* OK */
1218 int status;
1220 DEFER_INTS;
1222 int thr_critical = rb_thread_critical;
1224 rb_thread_critical = Qtrue;
1225 PUSH_TAG();
1226 if ((status = EXEC_TAG()) == 0) {
1227 result = (*proc) (data);
1229 POP_TAG();
1230 rb_thread_critical = thr_critical;
1232 ENABLE_INTS;
1233 if (status)
1234 JUMP_TAG(status);
1236 return result;
1239 static inline void
1240 stack_check(void)
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);
1251 * call-seq:
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
1261 * missing method.
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
1265 * values.
1267 * class Roman
1268 * def romanToInt(str)
1269 * # ...
1270 * end
1271 * def method_missing(methId)
1272 * str = methId.id2name
1273 * romanToInt(str)
1274 * end
1275 * end
1277 * r = Roman.new
1278 * r.iv #=> 4
1279 * r.xxiii #=> 23
1280 * r.mm #=> 2000
1283 static VALUE
1284 rb_method_missing(int argc, const VALUE *argv, VALUE obj)
1286 ID id;
1287 VALUE exc = rb_eNoMethodError;
1288 char *format = 0;
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");
1295 stack_check();
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";
1312 if (!format) {
1313 format = "undefined method `%s' for %s";
1317 int n = 0;
1318 VALUE args[3];
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);
1328 rb_exc_raise(exc);
1331 return Qnil; /* not reached */
1334 static VALUE
1335 method_missing(VALUE obj, ID id, int argc, const VALUE *argv, int call_status)
1337 VALUE *nargv;
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);
1355 static VALUE
1356 rb_call0(VALUE klass, VALUE recv, ID mid, int argc, const VALUE *argv, int scope, VALUE self)
1358 NODE *body, *method;
1359 int noex;
1360 ID id = mid;
1361 struct cache_entry *ent;
1362 rb_thread_t *th = GET_THREAD();
1364 if (!klass) {
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) {
1373 if (!ent->method)
1374 return method_missing(recv, mid, argc, argv,
1375 scope == 2 ? NOEX_VCALL : 0);
1376 id = ent->mid0;
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;
1386 else {
1387 if (scope == 3) {
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));
1421 stack_check();
1424 VALUE val;
1426 //static int level;
1427 //int i;
1428 //for(i=0; i<level; i++){printf(" ");}
1429 //printf("invoke %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
1430 //level++;
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);
1436 //level--;
1437 //for(i=0; i<level; i++){printf(" ");}
1438 //printf("done %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
1440 return val;
1444 static VALUE
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());
1450 VALUE
1451 rb_apply(VALUE recv, ID mid, VALUE args)
1453 int argc;
1454 VALUE *argv;
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);
1462 static VALUE
1463 send_internal(int argc, VALUE *argv, VALUE recv, int scope)
1465 VALUE vid;
1466 VALUE self = RUBY_VM_PREVIOUS_CONTROL_FRAME(GET_THREAD()->cfp)->self;
1468 if (argc == 0) {
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);
1478 * call-seq:
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_.
1486 * class Klass
1487 * def hello(*args)
1488 * "Hello " + args.join(' ')
1489 * end
1490 * end
1491 * k = Klass.new
1492 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
1495 VALUE
1496 rb_f_send(int argc, VALUE *argv, VALUE recv)
1498 return send_internal(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE);
1503 * call-seq:
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
1508 * methods only.
1510 * 1.public_send(:puts, "hello") # causes NoMethodError
1513 VALUE
1514 rb_f_public_send(int argc, VALUE *argv, VALUE recv)
1516 return send_internal(argc, argv, recv, NOEX_PUBLIC);
1519 VALUE
1520 rb_funcall(VALUE recv, ID mid, int n, ...)
1522 VALUE *argv;
1523 va_list ar;
1524 va_init_list(ar, n);
1526 if (n > 0) {
1527 long i;
1529 argv = ALLOCA_N(VALUE, n);
1531 for (i = 0; i < n; i++) {
1532 argv[i] = va_arg(ar, VALUE);
1534 va_end(ar);
1536 else {
1537 argv = 0;
1539 return rb_call(CLASS_OF(recv), recv, mid, n, argv, CALL_FCALL);
1542 VALUE
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);
1548 VALUE
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);
1554 static VALUE
1555 backtrace(int lev)
1557 return vm_backtrace(GET_THREAD(), lev);
1561 * call-seq:
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
1568 * result.
1570 * def a(skip)
1571 * caller(skip)
1572 * end
1573 * def b(skip)
1574 * a(skip)
1575 * end
1576 * def c(skip)
1577 * b(skip)
1578 * end
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"]
1585 static VALUE
1586 rb_f_caller(int argc, VALUE *argv)
1588 VALUE level;
1589 int lev;
1591 rb_scan_args(argc, argv, "01", &level);
1593 if (NIL_P(level))
1594 lev = 1;
1595 else
1596 lev = NUM2INT(level);
1597 if (lev < 0)
1598 rb_raise(rb_eArgError, "negative level (%d)", lev);
1600 return backtrace(lev);
1603 void
1604 rb_backtrace(void)
1606 long i;
1607 VALUE ary;
1609 ary = backtrace(-1);
1610 for (i = 0; i < RARRAY_LEN(ary); i++) {
1611 printf("\tfrom %s\n", RSTRING_PTR(RARRAY_PTR(ary)[i]));
1615 static VALUE
1616 make_backtrace(void)
1618 return backtrace(-1);
1621 static ID
1622 frame_func_id(rb_control_frame_t *cfp)
1624 rb_iseq_t *iseq = cfp->iseq;
1625 if (!iseq) {
1626 return cfp->method_id;
1628 while (iseq) {
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) {
1636 break;
1638 iseq = iseq->parent_iseq;
1640 return 0;
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)) {
1656 return 0;
1658 return frame_func_id(prev_cfp);
1661 void
1662 rb_frame_pop(void)
1664 rb_thread_t *th = GET_THREAD();
1665 th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
1668 static VALUE
1669 rb_frame_self(void)
1671 return GET_THREAD()->cfp->self;
1674 static VALUE
1675 eval(VALUE self, VALUE src, VALUE scope, const char *file, int line)
1677 int state;
1678 VALUE result = Qundef;
1679 VALUE envval;
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;
1685 if (file == 0) {
1686 file = rb_sourcefile();
1687 line = rb_sourceline();
1690 PUSH_TAG();
1691 if ((state = EXEC_TAG()) == 0) {
1692 rb_iseq_t *iseq;
1693 volatile VALUE iseqval;
1695 if (scope != Qnil) {
1696 if (rb_obj_is_kind_of(scope, rb_cBinding)) {
1697 GetBindingPtr(scope, bind);
1698 envval = bind->env;
1699 stored_cref_stack = bind->cref_stack;
1701 else {
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;
1709 else {
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);
1720 th->base_block = 0;
1722 if (0) { /* for debug */
1723 extern VALUE ruby_iseq_disasm(VALUE);
1724 printf("%s\n", RSTRING_PTR(ruby_iseq_disasm(iseqval)));
1727 /* save new env */
1728 GetISeqPtr(iseqval, iseq);
1729 if (bind && iseq->local_size > 0) {
1730 bind->env = vm_make_env_object(th, th->cfp);
1733 /* push tag */
1734 if (stored_cref_stack) {
1735 stored_cref_stack =
1736 vm_set_special_cref(th, env->block.lfp, stored_cref_stack);
1739 /* kick */
1740 CHECK_STACK_OVERFLOW(th->cfp, iseq->stack_max);
1741 result = vm_eval_body(th);
1743 POP_TAG();
1745 if (stored_cref_stack) {
1746 vm_set_special_cref(th, env->block.lfp, stored_cref_stack);
1749 if (state) {
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);
1768 JUMP_TAG(state);
1770 return result;
1774 * call-seq:
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)
1785 * return binding
1786 * end
1787 * str = "hello"
1788 * eval "str + ' Fred'" #=> "hello Fred"
1789 * eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
1792 VALUE
1793 rb_f_eval(int argc, VALUE *argv, VALUE self)
1795 VALUE src, scope, vfile, vline;
1796 char *file = "(eval)";
1797 int line = 1;
1799 rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
1800 if (rb_safe_level() >= 4) {
1801 StringValue(src);
1802 if (!NIL_P(scope) && !OBJ_TAINTED(scope)) {
1803 rb_raise(rb_eSecurityError,
1804 "Insecure: can't modify trusted binding");
1807 else {
1808 SafeStringValue(src);
1810 if (argc >= 3) {
1811 StringValue(vfile);
1813 if (argc >= 4) {
1814 line = NUM2INT(vline);
1817 if (!NIL_P(vfile))
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 */
1826 static VALUE
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;
1836 rb_block_t block;
1837 rb_block_t *blockptr;
1838 int state;
1840 /* replace environment */
1841 pcfp->self = self;
1842 if ((blockptr = GC_GUARDED_PTR_REF(*th->cfp->lfp)) != 0) {
1843 /* copy block info */
1844 /* TODO: why? */
1845 block = *blockptr;
1846 block.self = self;
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));
1857 PUSH_TAG();
1858 if ((state = EXEC_TAG()) == 0) {
1859 val = (*func) (args);
1861 POP_TAG();
1863 /* restore environment */
1864 vm_cfp_svar_set(th, cfp, 2, (VALUE)stored_cref);
1865 pcfp->self = stored_self;
1867 if (state) {
1868 JUMP_TAG(state);
1870 return val;
1873 static VALUE
1874 yield_under_i(VALUE arg)
1876 if (arg == Qundef) {
1877 return rb_yield_0(0, 0);
1879 else {
1880 return rb_yield_0(RARRAY_LEN(arg), RARRAY_PTR(arg));
1884 /* block eval under the class/module context */
1885 static VALUE
1886 yield_under(VALUE under, VALUE self, VALUE values)
1888 return exec_under(yield_under_i, under, self, values);
1891 static VALUE
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 */
1899 static VALUE
1900 eval_under(VALUE under, VALUE self, VALUE src, const char *file, int line)
1902 VALUE args[4];
1904 if (rb_safe_level() >= 4) {
1905 StringValue(src);
1907 else {
1908 SafeStringValue(src);
1910 args[0] = self;
1911 args[1] = src;
1912 args[2] = (VALUE)file;
1913 args[3] = (VALUE)line;
1914 return exec_under(eval_under_i, under, self, (VALUE)args);
1917 static VALUE
1918 specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self)
1920 if (rb_block_given_p()) {
1921 if (argc > 0) {
1922 rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)",
1923 argc);
1925 return yield_under(klass, self, Qundef);
1927 else {
1928 char *file = "(eval)";
1929 int line = 1;
1931 if (argc == 0) {
1932 rb_raise(rb_eArgError, "block not supplied");
1934 else {
1935 if (rb_safe_level() >= 4) {
1936 StringValue(argv[0]);
1938 else {
1939 SafeStringValue(argv[0]);
1941 if (argc > 3) {
1942 const char *name = rb_id2name(rb_frame_callee());
1943 rb_raise(rb_eArgError,
1944 "wrong number of arguments: %s(src) or %s{..}",
1945 name, name);
1947 if (argc > 2)
1948 line = NUM2INT(argv[2]);
1949 if (argc > 1) {
1950 file = StringValuePtr(argv[1]);
1953 return eval_under(klass, self, argv[0], file, line);
1958 * call-seq:
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
1972 * def initialize
1973 * @secret = 99
1974 * end
1975 * end
1976 * k = KlassWithSecret.new
1977 * k.instance_eval { @secret } #=> 99
1980 VALUE
1981 rb_obj_instance_eval(int argc, VALUE *argv, VALUE self)
1983 VALUE klass;
1985 if (SPECIAL_CONST_P(self)) {
1986 klass = CLASS_OF(self); /* klass = Qnil; */
1988 else {
1989 klass = rb_singleton_class(self);
1991 return specific_eval(argc, argv, klass, self);
1995 * call-seq:
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
2004 * def initialize
2005 * @secret = 99
2006 * end
2007 * end
2008 * k = KlassWithSecret.new
2009 * k.instance_exec(5) {|x| @secret+x } #=> 104
2012 VALUE
2013 rb_obj_instance_exec(int argc, VALUE *argv, VALUE self)
2015 VALUE klass;
2017 if (SPECIAL_CONST_P(self)) {
2018 klass = Qnil;
2020 else {
2021 klass = rb_singleton_class(self);
2023 return yield_under(klass, self, rb_ary_new4(argc, argv));
2027 * call-seq:
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.
2036 * class Thing
2037 * end
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>
2045 * Hello there!
2046 * dummy:123:in `module_eval': undefined local variable
2047 * or method `code' for Thing:Class
2050 VALUE
2051 rb_mod_module_eval(int argc, VALUE *argv, VALUE mod)
2053 return specific_eval(argc, argv, mod, mod);
2057 * call-seq:
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.
2064 * class Thing
2065 * end
2066 * Thing.class_exec{
2067 * def hello() "Hello there!" end
2069 * puts Thing.new.hello()
2071 * <em>produces:</em>
2073 * Hello there!
2076 VALUE
2077 rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
2079 return yield_under(mod, mod, rb_ary_new4(argc, argv));
2082 static void
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");
2091 static void
2092 set_method_visibility(VALUE self, int argc, VALUE *argv, ID ex)
2094 int i;
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);
2103 * call-seq:
2104 * public => 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.
2112 static VALUE
2113 rb_mod_public(int argc, VALUE *argv, VALUE module)
2115 secure_visibility(module);
2116 if (argc == 0) {
2117 SCOPE_SET(NOEX_PUBLIC);
2119 else {
2120 set_method_visibility(module, argc, argv, NOEX_PUBLIC);
2122 return module;
2126 * call-seq:
2127 * protected => self
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.
2135 static VALUE
2136 rb_mod_protected(int argc, VALUE *argv, VALUE module)
2138 secure_visibility(module);
2139 if (argc == 0) {
2140 SCOPE_SET(NOEX_PROTECTED);
2142 else {
2143 set_method_visibility(module, argc, argv, NOEX_PROTECTED);
2145 return module;
2149 * call-seq:
2150 * private => self
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.
2157 * module Mod
2158 * def a() end
2159 * def b() end
2160 * private
2161 * def c() end
2162 * private :a
2163 * end
2164 * Mod.private_instance_methods #=> [:a, :c]
2167 static VALUE
2168 rb_mod_private(int argc, VALUE *argv, VALUE module)
2170 secure_visibility(module);
2171 if (argc == 0) {
2172 SCOPE_SET(NOEX_PRIVATE);
2174 else {
2175 set_method_visibility(module, argc, argv, NOEX_PRIVATE);
2177 return module;
2181 * call-seq:
2182 * mod.public_class_method(symbol, ...) => mod
2184 * Makes a list of existing class methods public.
2187 static VALUE
2188 rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
2190 set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
2191 return obj;
2195 * call-seq:
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
2205 * @me
2206 * end
2207 * end
2210 static VALUE
2211 rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
2213 set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
2214 return obj;
2218 * call-seq:
2219 * public
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.
2227 static VALUE
2228 top_public(int argc, VALUE *argv)
2230 return rb_mod_public(argc, argv, rb_cObject);
2233 static VALUE
2234 top_private(int argc, VALUE *argv)
2236 return rb_mod_private(argc, argv, rb_cObject);
2240 * call-seq:
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
2249 * functions.
2251 * module Mod
2252 * def one
2253 * "This is one"
2254 * end
2255 * module_function :one
2256 * end
2257 * class Cls
2258 * include Mod
2259 * def callOne
2260 * one
2261 * end
2262 * end
2263 * Mod.one #=> "This is one"
2264 * c = Cls.new
2265 * c.callOne #=> "This is one"
2266 * module Mod
2267 * def one
2268 * "This is the new one"
2269 * end
2270 * end
2271 * Mod.one #=> "This is one"
2272 * c.callOne #=> "This is the new one"
2275 static VALUE
2276 rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
2278 int i;
2279 ID id;
2280 NODE *fbody;
2282 if (TYPE(module) != T_MODULE) {
2283 rb_raise(rb_eTypeError, "module_function must be called for modules");
2286 secure_visibility(module);
2287 if (argc == 0) {
2288 SCOPE_SET(NOEX_MODFUNC);
2289 return module;
2292 set_method_visibility(module, argc, argv, NOEX_PRIVATE);
2294 for (i = 0; i < argc; i++) {
2295 VALUE m = module;
2297 id = rb_to_id(argv[i]);
2298 for (;;) {
2299 fbody = search_method(m, id, &m);
2300 if (fbody == 0) {
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);
2310 if (!m)
2311 break;
2313 rb_add_method(rb_singleton_class(module), id, fbody->nd_body->nd_body,
2314 NOEX_PUBLIC);
2316 return module;
2320 * call-seq:
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>.
2331 static VALUE
2332 rb_mod_append_features(VALUE module, VALUE include)
2334 switch (TYPE(include)) {
2335 case T_CLASS:
2336 case T_MODULE:
2337 break;
2338 default:
2339 Check_Type(include, T_CLASS);
2340 break;
2342 rb_include_module(include, module);
2344 return module;
2348 * call-seq:
2349 * include(module, ...) => self
2351 * Invokes <code>Module.append_features</code> on each parameter in turn.
2354 static VALUE
2355 rb_mod_include(int argc, VALUE *argv, VALUE module)
2357 int i;
2359 for (i = 0; i < argc; i++)
2360 Check_Type(argv[i], T_MODULE);
2361 while (argc--) {
2362 rb_funcall(argv[argc], rb_intern("append_features"), 1, module);
2363 rb_funcall(argv[argc], rb_intern("included"), 1, module);
2365 return module;
2368 void
2369 rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
2371 PASS_PASSED_BLOCK();
2372 rb_funcall2(obj, init, argc, argv);
2375 void
2376 rb_extend_object(VALUE obj, VALUE module)
2378 rb_include_module(rb_singleton_class(obj), module);
2382 * call-seq:
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>.
2389 * module Picky
2390 * def Picky.extend_object(o)
2391 * if String === o
2392 * puts "Can't add Picky to a String"
2393 * else
2394 * puts "Picky added to #{o.class}"
2395 * super
2396 * end
2397 * end
2398 * end
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
2408 static VALUE
2409 rb_mod_extend_object(VALUE mod, VALUE obj)
2411 rb_extend_object(obj, mod);
2412 return obj;
2416 * call-seq:
2417 * obj.extend(module, ...) => obj
2419 * Adds to _obj_ the instance methods from each module given as a
2420 * parameter.
2422 * module Mod
2423 * def hello
2424 * "Hello from Mod.\n"
2425 * end
2426 * end
2428 * class Klass
2429 * def hello
2430 * "Hello from Klass.\n"
2431 * end
2432 * end
2434 * k = Klass.new
2435 * k.hello #=> "Hello from Klass.\n"
2436 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
2437 * k.hello #=> "Hello from Mod.\n"
2440 static VALUE
2441 rb_obj_extend(int argc, VALUE *argv, VALUE obj)
2443 int i;
2445 if (argc == 0) {
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);
2450 while (argc--) {
2451 rb_funcall(argv[argc], rb_intern("extend_object"), 1, obj);
2452 rb_funcall(argv[argc], rb_intern("extended"), 1, obj);
2454 return obj;
2458 * call-seq:
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.
2466 static VALUE
2467 top_include(int argc, VALUE *argv, VALUE self)
2469 rb_thread_t *th = GET_THREAD();
2471 rb_secure(4);
2472 if (th->top_wrapper) {
2473 rb_warning
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();
2483 static VALUE *
2484 errinfo_place(void)
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);
2503 return 0;
2506 static VALUE
2507 get_errinfo(void)
2509 VALUE *ptr = errinfo_place();
2510 if (ptr) {
2511 return *ptr;
2513 else {
2514 return Qnil;
2518 static VALUE
2519 errinfo_getter(ID id)
2521 return get_errinfo();
2524 #if 0
2525 static void
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 $!");
2531 else {
2532 VALUE *ptr = errinfo_place();
2533 if (ptr) {
2534 *ptr = val;
2536 else {
2537 rb_raise(rb_eRuntimeError, "errinfo_setter: not in rescue clause.");
2541 #endif
2543 VALUE
2544 rb_errinfo(void)
2546 rb_thread_t *th = GET_THREAD();
2547 return th->errinfo;
2550 void
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;
2559 VALUE
2560 rb_rubylevel_errinfo(void)
2562 return get_errinfo();
2565 static VALUE
2566 errat_getter(ID id)
2568 VALUE err = get_errinfo();
2569 if (!NIL_P(err)) {
2570 return get_backtrace(err);
2572 else {
2573 return Qnil;
2577 static void
2578 errat_setter(VALUE val, ID id, VALUE *var)
2580 VALUE err = get_errinfo();
2581 if (NIL_P(err)) {
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);
2590 * call-seq:
2591 * local_variables => array
2593 * Returns the names of the current local variables.
2595 * fred = 1
2596 * for i in 1..10
2597 * # ...
2598 * end
2599 * local_variables #=> ["fred", "i"]
2602 static VALUE
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));
2609 int i;
2611 while (1) {
2612 if (cfp->iseq) {
2613 for (i = 0; i < cfp->iseq->local_table_size; i++) {
2614 ID lid = cfp->iseq->local_table[i];
2615 if (lid) {
2616 const char *vname = rb_id2name(lid);
2617 /* should skip temporary variable */
2618 if (vname) {
2619 rb_ary_push(ary, ID2SYM(lid));
2624 if (cfp->lfp != cfp->dfp) {
2625 /* block */
2626 VALUE *dfp = GC_GUARDED_PTR_REF(cfp->dfp[0]);
2628 if (vm_collect_local_variables_in_heap(th, dfp, ary)) {
2629 break;
2631 else {
2632 while (cfp->dfp != dfp) {
2633 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2637 else {
2638 break;
2641 return ary;
2646 * call-seq:
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>.
2655 static VALUE
2656 rb_f_method_name(void)
2658 ID fname = rb_frame_callee();
2660 if (fname) {
2661 return ID2SYM(fname);
2663 else {
2664 return Qnil;
2668 void
2669 Init_eval(void)
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");
2745 Init_eval_method();
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);
2767 /* for parser */
2770 rb_dvar_defined(ID id)
2772 rb_thread_t *th = GET_THREAD();
2773 rb_iseq_t *iseq;
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) {
2779 int i;
2781 for (i = 0; i < iseq->local_table_size; i++) {
2782 if (iseq->local_table[i] == id) {
2783 return 1;
2786 iseq = iseq->parent_iseq;
2789 return 0;
2793 rb_local_defined(ID id)
2795 rb_thread_t *th = GET_THREAD();
2796 rb_iseq_t *iseq;
2798 if (th->base_block && th->base_block->iseq) {
2799 int i;
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) {
2804 return 1;
2808 return 0;
2812 rb_parse_in_eval(void)
2814 return GET_THREAD()->parse_in_eval != 0;