Ruby  1.9.3p551(2014-11-13revision48407)
eval.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  eval.c -
4 
5  $Author: usa $
6  created at: Thu Jun 10 14:22:17 JST 1993
7 
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
11 
12 **********************************************************************/
13 
14 #include "eval_intern.h"
15 #include "iseq.h"
16 #include "gc.h"
17 #include "ruby/vm.h"
18 #include "ruby/encoding.h"
19 #include "internal.h"
20 #include "vm_core.h"
21 
22 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
23 
25 
28 
29 #define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
30 
31 #include "eval_error.c"
32 #include "eval_jump.c"
33 
34 /* initialize ruby */
35 
36 void
37 ruby_init(void)
38 {
39  static int initialized = 0;
40  int state;
41 
42  if (initialized)
43  return;
44  initialized = 1;
45 
46  ruby_init_stack((void *)&state);
47  Init_BareVM();
48  Init_heap();
49 
50  PUSH_TAG();
51  if ((state = EXEC_TAG()) == 0) {
52  rb_call_inits();
54  }
55  POP_TAG();
56 
57  if (state) {
58  error_print();
59  exit(EXIT_FAILURE);
60  }
61  GET_VM()->running = 1;
62 }
63 
64 void *
65 ruby_options(int argc, char **argv)
66 {
67  int state;
68  void *volatile iseq = 0;
69 
70  ruby_init_stack((void *)&iseq);
71  PUSH_TAG();
72  if ((state = EXEC_TAG()) == 0) {
73  SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv));
74  }
75  else {
77  state = error_handle(state);
78  iseq = (void *)INT2FIX(state);
79  }
80  POP_TAG();
81  return iseq;
82 }
83 
84 static void
86 {
87  PUSH_TAG();
88  if (EXEC_TAG() == 0) {
89  rb_trap_exit();
90  }
91  POP_TAG();
94 }
95 
96 static void
98 {
100  GET_THREAD()->errinfo = Qnil;
102 }
103 
104 void
106 {
107  ruby_finalize_0();
108  ruby_finalize_1();
109 }
110 
111 int
112 ruby_cleanup(volatile int ex)
113 {
114  int state;
115  volatile VALUE errs[2];
116  rb_thread_t *th = GET_THREAD();
117  int nerr;
118 
121  PUSH_TAG();
122  if ((state = EXEC_TAG()) == 0) {
124  }
125  POP_TAG();
126 
127  errs[1] = th->errinfo;
128  th->safe_level = 0;
129  ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
130 
131  PUSH_TAG();
132  if ((state = EXEC_TAG()) == 0) {
134  }
135  POP_TAG();
136 
137  errs[0] = th->errinfo;
138  PUSH_TAG();
139  if ((state = EXEC_TAG()) == 0) {
141  }
142  else if (ex == 0) {
143  ex = state;
144  }
145  th->errinfo = errs[1];
146  ex = error_handle(ex);
147  ruby_finalize_1();
148 
149  /* unlock again if finalizer took mutexes. */
151  POP_TAG();
153 
154 #if EXIT_SUCCESS != 0 || EXIT_FAILURE != 1
155  switch (ex) {
156 #if EXIT_SUCCESS != 0
157  case 0: ex = EXIT_SUCCESS; break;
158 #endif
159 #if EXIT_FAILURE != 1
160  case 1: ex = EXIT_FAILURE; break;
161 #endif
162  }
163 #endif
164 
165  state = 0;
166  for (nerr = 0; nerr < numberof(errs); ++nerr) {
167  VALUE err = errs[nerr];
168 
169  if (!RTEST(err)) continue;
170 
171  /* th->errinfo contains a NODE while break'ing */
172  if (TYPE(err) == T_NODE) continue;
173 
174  if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
175  ex = sysexit_status(err);
176  break;
177  }
178  else if (rb_obj_is_kind_of(err, rb_eSignal)) {
179  VALUE sig = rb_iv_get(err, "signo");
180  state = NUM2INT(sig);
181  break;
182  }
183  else if (ex == EXIT_SUCCESS) {
184  ex = EXIT_FAILURE;
185  }
186  }
188  if (state) ruby_default_signal(state);
189 
190  return ex;
191 }
192 
193 static int
195 {
196  volatile int state;
197  VALUE iseq = (VALUE)n;
198  rb_thread_t *th = GET_THREAD();
199 
200  if (!n) return 0;
201 
202  PUSH_TAG();
203  if ((state = EXEC_TAG()) == 0) {
204  SAVE_ROOT_JMPBUF(th, {
205  th->base_block = 0;
206  rb_iseq_eval_main(iseq);
207  });
208  }
209  POP_TAG();
210  return state;
211 }
212 
213 void
214 ruby_stop(int ex)
215 {
216  exit(ruby_cleanup(ex));
217 }
218 
219 int
220 ruby_executable_node(void *n, int *status)
221 {
222  VALUE v = (VALUE)n;
223  int s;
224 
225  switch (v) {
226  case Qtrue: s = EXIT_SUCCESS; break;
227  case Qfalse: s = EXIT_FAILURE; break;
228  default:
229  if (!FIXNUM_P(v)) return TRUE;
230  s = FIX2INT(v);
231  }
232  if (status) *status = s;
233  return FALSE;
234 }
235 
236 int
238 {
239  int status;
240  if (!ruby_executable_node(n, &status)) {
241  ruby_cleanup(0);
242  return status;
243  }
244  return ruby_cleanup(ruby_exec_node(n));
245 }
246 
247 int
249 {
250  ruby_init_stack((void *)&n);
251  return ruby_exec_internal(n);
252 }
253 
254 /*
255  * call-seq:
256  * Module.nesting -> array
257  *
258  * Returns the list of +Modules+ nested at the point of call.
259  *
260  * module M1
261  * module M2
262  * $a = Module.nesting
263  * end
264  * end
265  * $a #=> [M1::M2, M1]
266  * $a[0].name #=> "M1::M2"
267  */
268 
269 static VALUE
271 {
272  VALUE ary = rb_ary_new();
273  const NODE *cref = rb_vm_cref();
274 
275  while (cref && cref->nd_next) {
276  VALUE klass = cref->nd_clss;
277  if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
278  !NIL_P(klass)) {
279  rb_ary_push(ary, klass);
280  }
281  cref = cref->nd_next;
282  }
283  return ary;
284 }
285 
286 /*
287  * call-seq:
288  * Module.constants -> array
289  * Module.constants(inherited) -> array
290  *
291  * In the first form, returns an array of the names of all
292  * constants accessible from the point of call.
293  * This list includes the names of all modules and classes
294  * defined in the global scope.
295  *
296  * Module.constants.first(4)
297  * # => [:ARGF, :ARGV, :ArgumentError, :Array]
298  *
299  * Module.constants.include?(:SEEK_SET) # => false
300  *
301  * class IO
302  * Module.constants.include?(:SEEK_SET) # => true
303  * end
304  *
305  * The second form calls the instance method +constants+.
306  */
307 
308 static VALUE
310 {
311  const NODE *cref = rb_vm_cref();
312  VALUE klass;
313  VALUE cbase = 0;
314  void *data = 0;
315 
316  if (argc > 0 || mod != rb_cModule) {
317  return rb_mod_constants(argc, argv, mod);
318  }
319 
320  while (cref) {
321  klass = cref->nd_clss;
322  if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
323  !NIL_P(klass)) {
324  data = rb_mod_const_at(cref->nd_clss, data);
325  if (!cbase) {
326  cbase = klass;
327  }
328  }
329  cref = cref->nd_next;
330  }
331 
332  if (cbase) {
333  data = rb_mod_const_of(cbase, data);
334  }
335  return rb_const_list(data);
336 }
337 
338 void
340 {
341  const char *desc = "something(?!)";
342 
343  if (OBJ_FROZEN(klass)) {
344  if (FL_TEST(klass, FL_SINGLETON))
345  desc = "object";
346  else {
347  switch (TYPE(klass)) {
348  case T_MODULE:
349  case T_ICLASS:
350  desc = "module";
351  break;
352  case T_CLASS:
353  desc = "class";
354  break;
355  }
356  }
357  rb_error_frozen(desc);
358  }
359 }
360 
361 NORETURN(static void rb_longjmp(int, volatile VALUE));
362 
363 static void
364 setup_exception(rb_thread_t *th, int tag, volatile VALUE mesg)
365 {
366  VALUE at;
367  VALUE e;
368  const char *file;
369  volatile int line = 0;
370 
371  if (NIL_P(mesg)) {
372  mesg = th->errinfo;
374  }
375  if (NIL_P(mesg)) {
376  mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
377  }
378 
379  file = rb_sourcefile();
380  if (file) line = rb_sourceline();
381  if (file && !NIL_P(mesg)) {
382  if (mesg == sysstack_error) {
383  at = rb_enc_sprintf(rb_usascii_encoding(), "%s:%d", file, line);
384  at = rb_ary_new3(1, at);
385  rb_iv_set(mesg, "bt", at);
386  }
387  else {
388  at = get_backtrace(mesg);
389  if (NIL_P(at)) {
390  at = rb_make_backtrace();
391  if (OBJ_FROZEN(mesg)) {
392  mesg = rb_obj_dup(mesg);
393  }
394  set_backtrace(mesg, at);
395  }
396  }
397  }
398  if (!NIL_P(mesg)) {
399  th->errinfo = mesg;
400  }
401 
402  if (RTEST(ruby_debug) && !NIL_P(e = th->errinfo) &&
404  int status;
405 
406  PUSH_TAG();
407  if ((status = EXEC_TAG()) == 0) {
409  if (file && line) {
410  warn_printf("Exception `%s' at %s:%d - %s\n",
412  file, line, RSTRING_PTR(e));
413  }
414  else if (file) {
415  warn_printf("Exception `%s' at %s - %s\n",
417  file, RSTRING_PTR(e));
418  }
419  else {
420  warn_printf("Exception `%s' - %s\n",
422  RSTRING_PTR(e));
423  }
424  }
425  POP_TAG();
426  if (status == TAG_FATAL && th->errinfo == exception_error) {
427  th->errinfo = mesg;
428  }
429  else if (status) {
431  JUMP_TAG(status);
432  }
433  }
434 
435  if (rb_threadptr_set_raised(th)) {
436  th->errinfo = exception_error;
439  }
440 
442 
443  if (tag != TAG_FATAL) {
444  EXEC_EVENT_HOOK(th, RUBY_EVENT_RAISE, th->cfp->self, 0, 0);
445  }
446 }
447 
448 static void
449 rb_longjmp(int tag, volatile VALUE mesg)
450 {
451  rb_thread_t *th = GET_THREAD();
452  setup_exception(th, tag, mesg);
454  JUMP_TAG(tag);
455 }
456 
457 static VALUE make_exception(int argc, VALUE *argv, int isstr);
458 
459 void
461 {
462  if (!NIL_P(mesg)) {
463  mesg = make_exception(1, &mesg, FALSE);
464  }
465  rb_longjmp(TAG_RAISE, mesg);
466 }
467 
468 void
470 {
471  if (!NIL_P(mesg)) {
472  mesg = make_exception(1, &mesg, FALSE);
473  }
474  rb_longjmp(TAG_FATAL, mesg);
475 }
476 
477 void
479 {
480  rb_raise(rb_eInterrupt, "%s", "");
481 }
482 
483 static VALUE get_errinfo(void);
484 
485 /*
486  * call-seq:
487  * raise
488  * raise(string)
489  * raise(exception [, string [, array]])
490  * fail
491  * fail(string)
492  * fail(exception [, string [, array]])
493  *
494  * With no arguments, raises the exception in <code>$!</code> or raises
495  * a <code>RuntimeError</code> if <code>$!</code> is +nil+.
496  * With a single +String+ argument, raises a
497  * +RuntimeError+ with the string as a message. Otherwise,
498  * the first parameter should be the name of an +Exception+
499  * class (or an object that returns an +Exception+ object when sent
500  * an +exception+ message). The optional second parameter sets the
501  * message associated with the exception, and the third parameter is an
502  * array of callback information. Exceptions are caught by the
503  * +rescue+ clause of <code>begin...end</code> blocks.
504  *
505  * raise "Failed to create socket"
506  * raise ArgumentError, "No parameters", caller
507  */
508 
509 static VALUE
511 {
512  VALUE err;
513  if (argc == 0) {
514  err = get_errinfo();
515  if (!NIL_P(err)) {
516  argc = 1;
517  argv = &err;
518  }
519  }
520  rb_raise_jump(rb_make_exception(argc, argv));
521  return Qnil; /* not reached */
522 }
523 
524 static VALUE
525 make_exception(int argc, VALUE *argv, int isstr)
526 {
527  VALUE mesg;
528  ID exception;
529  int n;
530 
531  mesg = Qnil;
532  switch (argc) {
533  case 0:
534  break;
535  case 1:
536  if (NIL_P(argv[0]))
537  break;
538  if (isstr) {
539  mesg = rb_check_string_type(argv[0]);
540  if (!NIL_P(mesg)) {
541  mesg = rb_exc_new3(rb_eRuntimeError, mesg);
542  break;
543  }
544  }
545  n = 0;
546  goto exception_call;
547 
548  case 2:
549  case 3:
550  n = 1;
551  exception_call:
552  if (argv[0] == sysstack_error) return argv[0];
553  CONST_ID(exception, "exception");
554  mesg = rb_check_funcall(argv[0], exception, n, argv+1);
555  if (mesg == Qundef) {
556  rb_raise(rb_eTypeError, "exception class/object expected");
557  }
558  break;
559  default:
560  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..3)", argc);
561  break;
562  }
563  if (argc > 0) {
564  if (!rb_obj_is_kind_of(mesg, rb_eException))
565  rb_raise(rb_eTypeError, "exception object expected");
566  if (argc > 2)
567  set_backtrace(mesg, argv[2]);
568  }
569 
570  return mesg;
571 }
572 
573 VALUE
575 {
576  return make_exception(argc, argv, TRUE);
577 }
578 
579 void
581 {
582  rb_thread_t *th = GET_THREAD();
583  rb_control_frame_t *cfp = th->cfp;
584  VALUE klass = cfp->me->klass;
585  VALUE self = cfp->self;
586  ID mid = cfp->me->called_id;
587 
589 
590  setup_exception(th, TAG_RAISE, mesg);
591 
592  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, self, mid, klass);
595 }
596 
597 void
598 rb_jump_tag(int tag)
599 {
600  JUMP_TAG(tag);
601 }
602 
603 int
605 {
606  rb_thread_t *th = GET_THREAD();
607 
608  if ((th->cfp->lfp[0] & 0x02) == 0 &&
609  GC_GUARDED_PTR_REF(th->cfp->lfp[0])) {
610  return TRUE;
611  }
612  else {
613  return FALSE;
614  }
615 }
616 
617 int
619 {
620  return rb_block_given_p();
621 }
622 
624 
625 void
627 {
628  if (!rb_block_given_p()) {
629  rb_vm_localjump_error("no block given", Qnil, 0);
630  }
631 }
632 
633 VALUE
634 rb_rescue2(VALUE (* b_proc) (ANYARGS), VALUE data1,
635  VALUE (* r_proc) (ANYARGS), VALUE data2, ...)
636 {
637  int state;
638  rb_thread_t *th = GET_THREAD();
639  rb_control_frame_t *cfp = th->cfp;
640  volatile VALUE result;
641  volatile VALUE e_info = th->errinfo;
642  va_list args;
643 
644  PUSH_TAG();
645  if ((state = EXEC_TAG()) == 0) {
646  retry_entry:
647  result = (*b_proc) (data1);
648  }
649  else {
650  th->cfp = cfp; /* restore */
651 
652  if (state == TAG_RAISE) {
653  int handle = FALSE;
654  VALUE eclass;
655 
656  va_init_list(args, data2);
657  while ((eclass = va_arg(args, VALUE)) != 0) {
658  if (rb_obj_is_kind_of(th->errinfo, eclass)) {
659  handle = TRUE;
660  break;
661  }
662  }
663  va_end(args);
664 
665  if (handle) {
666  if (r_proc) {
667  PUSH_TAG();
668  if ((state = EXEC_TAG()) == 0) {
669  result = (*r_proc) (data2, th->errinfo);
670  }
671  POP_TAG();
672  if (state == TAG_RETRY) {
673  state = 0;
674  th->errinfo = Qnil;
675  goto retry_entry;
676  }
677  }
678  else {
679  result = Qnil;
680  state = 0;
681  }
682  if (state == 0) {
683  th->errinfo = e_info;
684  }
685  }
686  }
687  }
688  POP_TAG();
689  if (state)
690  JUMP_TAG(state);
691 
692  return result;
693 }
694 
695 VALUE
696 rb_rescue(VALUE (* b_proc)(ANYARGS), VALUE data1,
697  VALUE (* r_proc)(ANYARGS), VALUE data2)
698 {
699  return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
700  (VALUE)0);
701 }
702 
703 VALUE
704 rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state)
705 {
706  volatile VALUE result = Qnil;
707  int status;
708  rb_thread_t *th = GET_THREAD();
709  rb_control_frame_t *cfp = th->cfp;
710  struct rb_vm_protect_tag protect_tag;
711  rb_jmpbuf_t org_jmpbuf;
712 
713  protect_tag.prev = th->protect_tag;
714 
715  PUSH_TAG();
716  th->protect_tag = &protect_tag;
717  MEMCPY(&org_jmpbuf, &(th)->root_jmpbuf, rb_jmpbuf_t, 1);
718  if ((status = EXEC_TAG()) == 0) {
719  SAVE_ROOT_JMPBUF(th, result = (*proc) (data));
720  }
721  MEMCPY(&(th)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);
722  th->protect_tag = protect_tag.prev;
723  POP_TAG();
724 
725  if (state) {
726  *state = status;
727  }
728  if (status != 0) {
729  th->cfp = cfp;
730  return Qnil;
731  }
732 
733  return result;
734 }
735 
736 VALUE
737 rb_ensure(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*e_proc)(ANYARGS), VALUE data2)
738 {
739  int state;
740  volatile VALUE result = Qnil;
741 
742  PUSH_TAG();
743  if ((state = EXEC_TAG()) == 0) {
744  result = (*b_proc) (data1);
745  }
746  POP_TAG();
747  /* TODO: fix me */
748  /* retval = prot_tag ? prot_tag->retval : Qnil; */ /* save retval */
749  (*e_proc) (data2);
750  if (state)
751  JUMP_TAG(state);
752  return result;
753 }
754 
755 static const rb_method_entry_t *
757 {
758  rb_thread_t *th = GET_THREAD();
759  rb_control_frame_t *cfp_limit;
760 
761  cfp_limit = (rb_control_frame_t *)(th->stack + th->stack_size);
762  while (cfp_limit > cfp) {
763  if (cfp->iseq == iseq)
764  return cfp->me;
766  }
767  return 0;
768 }
769 
770 static ID
772 {
773  const rb_method_entry_t *me_local;
774  rb_iseq_t *iseq = cfp->iseq;
775  if (cfp->me) {
776  return cfp->me->def->original_id;
777  }
778  while (iseq) {
779  if (RUBY_VM_IFUNC_P(iseq)) {
780  NODE *ifunc = (NODE *)iseq;
781  if (ifunc->nd_aid) return ifunc->nd_aid;
782  return rb_intern("<ifunc>");
783  }
784  me_local = method_entry_of_iseq(cfp, iseq);
785  if (me_local) {
786  cfp->me = me_local;
787  return me_local->def->original_id;
788  }
789  if (iseq->defined_method_id) {
790  return iseq->defined_method_id;
791  }
792  if (iseq->local_iseq == iseq) {
793  break;
794  }
795  iseq = iseq->parent_iseq;
796  }
797  return 0;
798 }
799 
800 ID
802 {
803  return frame_func_id(GET_THREAD()->cfp);
804 }
805 
806 ID
808 {
809  return frame_func_id(GET_THREAD()->cfp);
810 }
811 
812 static ID
814 {
815  rb_thread_t *th = GET_THREAD();
817  /* check if prev_cfp can be accessible */
818  if ((void *)(th->stack + th->stack_size) == (void *)(prev_cfp)) {
819  return 0;
820  }
821  return frame_func_id(prev_cfp);
822 }
823 
824 void
826 {
827  rb_thread_t *th = GET_THREAD();
829 }
830 
831 /*
832  * call-seq:
833  * append_features(mod) -> mod
834  *
835  * When this module is included in another, Ruby calls
836  * <code>append_features</code> in this module, passing it the
837  * receiving module in _mod_. Ruby's default implementation is
838  * to add the constants, methods, and module variables of this module
839  * to _mod_ if this module has not already been added to
840  * _mod_ or one of its ancestors. See also <code>Module#include</code>.
841  */
842 
843 static VALUE
845 {
846  switch (TYPE(include)) {
847  case T_CLASS:
848  case T_MODULE:
849  break;
850  default:
851  Check_Type(include, T_CLASS);
852  break;
853  }
854  rb_include_module(include, module);
855 
856  return module;
857 }
858 
859 /*
860  * call-seq:
861  * include(module, ...) -> self
862  *
863  * Invokes <code>Module.append_features</code> on each parameter in reverse order.
864  */
865 
866 static VALUE
868 {
869  int i;
870 
871  for (i = 0; i < argc; i++)
872  Check_Type(argv[i], T_MODULE);
873  while (argc--) {
874  rb_funcall(argv[argc], rb_intern("append_features"), 1, module);
875  rb_funcall(argv[argc], rb_intern("included"), 1, module);
876  }
877  return module;
878 }
879 
880 void
882 {
884  rb_funcall2(obj, idInitialize, argc, argv);
885 }
886 
887 void
889 {
891 }
892 
893 /*
894  * call-seq:
895  * extend_object(obj) -> obj
896  *
897  * Extends the specified object by adding this module's constants and
898  * methods (which are added as singleton methods). This is the callback
899  * method used by <code>Object#extend</code>.
900  *
901  * module Picky
902  * def Picky.extend_object(o)
903  * if String === o
904  * puts "Can't add Picky to a String"
905  * else
906  * puts "Picky added to #{o.class}"
907  * super
908  * end
909  * end
910  * end
911  * (s = Array.new).extend Picky # Call Object.extend
912  * (s = "quick brown fox").extend Picky
913  *
914  * <em>produces:</em>
915  *
916  * Picky added to Array
917  * Can't add Picky to a String
918  */
919 
920 static VALUE
922 {
923  rb_extend_object(obj, mod);
924  return obj;
925 }
926 
927 /*
928  * call-seq:
929  * obj.extend(module, ...) -> obj
930  *
931  * Adds to _obj_ the instance methods from each module given as a
932  * parameter.
933  *
934  * module Mod
935  * def hello
936  * "Hello from Mod.\n"
937  * end
938  * end
939  *
940  * class Klass
941  * def hello
942  * "Hello from Klass.\n"
943  * end
944  * end
945  *
946  * k = Klass.new
947  * k.hello #=> "Hello from Klass.\n"
948  * k.extend(Mod) #=> #<Klass:0x401b3bc8>
949  * k.hello #=> "Hello from Mod.\n"
950  */
951 
952 static VALUE
954 {
955  int i;
956 
957  if (argc == 0) {
958  rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
959  }
960  for (i = 0; i < argc; i++)
961  Check_Type(argv[i], T_MODULE);
962  while (argc--) {
963  rb_funcall(argv[argc], rb_intern("extend_object"), 1, obj);
964  rb_funcall(argv[argc], rb_intern("extended"), 1, obj);
965  }
966  return obj;
967 }
968 
969 /*
970  * call-seq:
971  * include(module, ...) -> self
972  *
973  * Invokes <code>Module.append_features</code>
974  * on each parameter in turn. Effectively adds the methods and constants
975  * in each module to the receiver.
976  */
977 
978 static VALUE
980 {
981  rb_thread_t *th = GET_THREAD();
982 
983  rb_secure(4);
984  if (th->top_wrapper) {
985  rb_warning
986  ("main#include in the wrapped load is effective only in wrapper module");
987  return rb_mod_include(argc, argv, th->top_wrapper);
988  }
989  return rb_mod_include(argc, argv, rb_cObject);
990 }
991 
992 static VALUE *
994 {
995  rb_control_frame_t *cfp = th->cfp;
997 
998  while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
999  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
1000  if (cfp->iseq->type == ISEQ_TYPE_RESCUE) {
1001  return &cfp->dfp[-2];
1002  }
1003  else if (cfp->iseq->type == ISEQ_TYPE_ENSURE &&
1004  TYPE(cfp->dfp[-2]) != T_NODE &&
1005  !FIXNUM_P(cfp->dfp[-2])) {
1006  return &cfp->dfp[-2];
1007  }
1008  }
1009  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1010  }
1011  return 0;
1012 }
1013 
1014 static VALUE
1016 {
1017  VALUE *ptr = errinfo_place(th);
1018  if (ptr) {
1019  return *ptr;
1020  }
1021  else {
1022  return th->errinfo;
1023  }
1024 }
1025 
1026 static VALUE
1028 {
1029  return get_thread_errinfo(GET_THREAD());
1030 }
1031 
1032 static VALUE
1034 {
1035  return get_errinfo();
1036 }
1037 
1038 #if 0
1039 static void
1040 errinfo_setter(VALUE val, ID id, VALUE *var)
1041 {
1042  if (!NIL_P(val) && !rb_obj_is_kind_of(val, rb_eException)) {
1043  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1044  }
1045  else {
1046  VALUE *ptr = errinfo_place(GET_THREAD());
1047  if (ptr) {
1048  *ptr = val;
1049  }
1050  else {
1051  rb_raise(rb_eRuntimeError, "errinfo_setter: not in rescue clause.");
1052  }
1053  }
1054 }
1055 #endif
1056 
1057 VALUE
1059 {
1060  rb_thread_t *th = GET_THREAD();
1061  return th->errinfo;
1062 }
1063 
1064 void
1066 {
1067  if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1068  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1069  }
1070  GET_THREAD()->errinfo = err;
1071 }
1072 
1073 VALUE
1075 {
1076  return get_errinfo();
1077 }
1078 
1079 static VALUE
1081 {
1082  VALUE err = get_errinfo();
1083  if (!NIL_P(err)) {
1084  return get_backtrace(err);
1085  }
1086  else {
1087  return Qnil;
1088  }
1089 }
1090 
1091 static void
1092 errat_setter(VALUE val, ID id, VALUE *var)
1093 {
1094  VALUE err = get_errinfo();
1095  if (NIL_P(err)) {
1096  rb_raise(rb_eArgError, "$! not set");
1097  }
1098  set_backtrace(err, val);
1099 }
1100 
1101 /*
1102  * call-seq:
1103  * __method__ -> symbol
1104  * __callee__ -> symbol
1105  *
1106  * Returns the name of the current method as a Symbol.
1107  * If called outside of a method, it returns <code>nil</code>.
1108  *
1109  */
1110 
1111 static VALUE
1113 {
1114  ID fname = rb_frame_caller(); /* need *caller* ID */
1115 
1116  if (fname) {
1117  return ID2SYM(fname);
1118  }
1119  else {
1120  return Qnil;
1121  }
1122 }
1123 
1124 void
1126 {
1129 
1130  rb_define_global_function("raise", rb_f_raise, -1);
1132 
1133  rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */
1134 
1135  rb_define_global_function("__method__", rb_f_method_name, 0);
1136  rb_define_global_function("__callee__", rb_f_method_name, 0);
1137 
1141 
1142  rb_undef_method(rb_cClass, "module_function");
1143 
1144  Init_vm_eval();
1145  Init_eval_method();
1146 
1149 
1151 
1152  rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
1153 
1154  rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */
1155  rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */
1156 
1158  rb_obj_freeze(rb_str_new2("exception reentered")));
1161 }
1162