Ruby  2.5.0dev(2017-10-22revision60238)
eval.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  eval.c -
4 
5  $Author$
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 "internal.h"
15 #include "eval_intern.h"
16 #include "iseq.h"
17 #include "gc.h"
18 #include "ruby/vm.h"
19 #include "vm_core.h"
20 #include "probes_helper.h"
21 
22 NORETURN(void rb_raise_jump(VALUE, VALUE));
23 
26 
28 static ID id_cause;
29 #define id_signo ruby_static_id_signo
30 #define id_status ruby_static_id_status
31 
32 #define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
33 
34 #include "eval_error.c"
35 #include "eval_jump.c"
36 
37 #define CLASS_OR_MODULE_P(obj) \
38  (!SPECIAL_CONST_P(obj) && \
39  (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
40 
46 int
48 {
49  enum ruby_tag_type state;
50 
51  if (GET_VM())
52  return 0;
53 
54  ruby_init_stack((void *)&state);
55  Init_BareVM();
56  Init_heap();
58 
59  PUSH_TAG();
60  if ((state = EXEC_TAG()) == TAG_NONE) {
61  rb_call_inits();
63  GET_VM()->running = 1;
64  }
65  POP_TAG();
66 
67  return state;
68 }
69 
75 void
76 ruby_init(void)
77 {
78  int state = ruby_setup();
79  if (state) {
80  if (RTEST(ruby_debug))
81  error_print(GET_THREAD());
82  exit(EXIT_FAILURE);
83  }
84 }
85 
96 void *
97 ruby_options(int argc, char **argv)
98 {
99  enum ruby_tag_type state;
100  void *volatile iseq = 0;
101 
102  ruby_init_stack((void *)&iseq);
103  PUSH_TAG();
104  if ((state = EXEC_TAG()) == TAG_NONE) {
105  SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv));
106  }
107  else {
109  state = error_handle(state);
110  iseq = (void *)INT2FIX(state);
111  }
112  POP_TAG();
113  return iseq;
114 }
115 
116 static void
117 ruby_finalize_0(void)
118 {
119  PUSH_TAG();
120  if (EXEC_TAG() == TAG_NONE) {
121  rb_trap_exit();
122  }
123  POP_TAG();
126 }
127 
128 static void
129 ruby_finalize_1(void)
130 {
132  GET_THREAD()->ec.errinfo = Qnil;
134 }
135 
143 void
145 {
146  ruby_finalize_0();
147  ruby_finalize_1();
148 }
149 
160 int
161 ruby_cleanup(volatile int ex)
162 {
163  int state;
164  volatile VALUE errs[2];
165  rb_thread_t *th = GET_THREAD();
166  int nerr;
167  volatile int sysex = EXIT_SUCCESS;
168  volatile int step = 0;
169 
172  TH_PUSH_TAG(th);
173  if ((state = EXEC_TAG()) == TAG_NONE) {
174  SAVE_ROOT_JMPBUF(th, { RUBY_VM_CHECK_INTS(th); });
175 
176  step_0: step++;
177  errs[1] = th->ec.errinfo;
178  th->ec.safe_level = 0;
179  ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
180 
181  SAVE_ROOT_JMPBUF(th, ruby_finalize_0());
182 
183  step_1: step++;
184  /* protect from Thread#raise */
185  th->status = THREAD_KILLED;
186 
187  errs[0] = th->ec.errinfo;
189  }
190  else {
191  switch (step) {
192  case 0: goto step_0;
193  case 1: goto step_1;
194  }
195  if (ex == 0) ex = state;
196  }
197  th->ec.errinfo = errs[1];
198  sysex = error_handle(ex);
199 
200  state = 0;
201  for (nerr = 0; nerr < numberof(errs); ++nerr) {
202  VALUE err = ATOMIC_VALUE_EXCHANGE(errs[nerr], Qnil);
203 
204  if (!RTEST(err)) continue;
205 
206  /* th->ec.errinfo contains a NODE while break'ing */
207  if (THROW_DATA_P(err)) continue;
208 
209  if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
210  sysex = sysexit_status(err);
211  break;
212  }
213  else if (rb_obj_is_kind_of(err, rb_eSignal)) {
214  VALUE sig = rb_ivar_get(err, id_signo);
215  state = NUM2INT(sig);
216  break;
217  }
218  else if (sysex == EXIT_SUCCESS) {
219  sysex = EXIT_FAILURE;
220  }
221  }
222 
223  ruby_finalize_1();
224 
225  /* unlock again if finalizer took mutexes. */
227  TH_POP_TAG();
230  if (state) ruby_default_signal(state);
231 
232  return sysex;
233 }
234 
235 static int
236 ruby_exec_internal(void *n)
237 {
238  volatile int state;
239  rb_iseq_t *iseq = (rb_iseq_t *)n;
240  rb_thread_t *th = GET_THREAD();
241 
242  if (!n) return 0;
243 
244  TH_PUSH_TAG(th);
245  if ((state = EXEC_TAG()) == TAG_NONE) {
246  SAVE_ROOT_JMPBUF(th, {
247  rb_iseq_eval_main(iseq);
248  });
249  }
250  TH_POP_TAG();
251  return state;
252 }
253 
255 void
256 ruby_stop(int ex)
257 {
258  exit(ruby_cleanup(ex));
259 }
260 
273 int
274 ruby_executable_node(void *n, int *status)
275 {
276  VALUE v = (VALUE)n;
277  int s;
278 
279  switch (v) {
280  case Qtrue: s = EXIT_SUCCESS; break;
281  case Qfalse: s = EXIT_FAILURE; break;
282  default:
283  if (!FIXNUM_P(v)) return TRUE;
284  s = FIX2INT(v);
285  }
286  if (status) *status = s;
287  return FALSE;
288 }
289 
294 int
296 {
297  int status;
298  if (!ruby_executable_node(n, &status)) {
299  ruby_cleanup(0);
300  return status;
301  }
302  return ruby_cleanup(ruby_exec_node(n));
303 }
304 
306 int
308 {
309  ruby_init_stack((void *)&n);
310  return ruby_exec_internal(n);
311 }
312 
313 /*
314  * call-seq:
315  * Module.nesting -> array
316  *
317  * Returns the list of +Modules+ nested at the point of call.
318  *
319  * module M1
320  * module M2
321  * $a = Module.nesting
322  * end
323  * end
324  * $a #=> [M1::M2, M1]
325  * $a[0].name #=> "M1::M2"
326  */
327 
328 static VALUE
329 rb_mod_nesting(void)
330 {
331  VALUE ary = rb_ary_new();
332  const rb_cref_t *cref = rb_vm_cref();
333 
334  while (cref && CREF_NEXT(cref)) {
335  VALUE klass = CREF_CLASS(cref);
336  if (!CREF_PUSHED_BY_EVAL(cref) &&
337  !NIL_P(klass)) {
338  rb_ary_push(ary, klass);
339  }
340  cref = CREF_NEXT(cref);
341  }
342  return ary;
343 }
344 
345 /*
346  * call-seq:
347  * Module.constants -> array
348  * Module.constants(inherited) -> array
349  *
350  * In the first form, returns an array of the names of all
351  * constants accessible from the point of call.
352  * This list includes the names of all modules and classes
353  * defined in the global scope.
354  *
355  * Module.constants.first(4)
356  * # => [:ARGF, :ARGV, :ArgumentError, :Array]
357  *
358  * Module.constants.include?(:SEEK_SET) # => false
359  *
360  * class IO
361  * Module.constants.include?(:SEEK_SET) # => true
362  * end
363  *
364  * The second form calls the instance method +constants+.
365  */
366 
367 static VALUE
368 rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
369 {
370  const rb_cref_t *cref = rb_vm_cref();
371  VALUE klass;
372  VALUE cbase = 0;
373  void *data = 0;
374 
375  if (argc > 0 || mod != rb_cModule) {
376  return rb_mod_constants(argc, argv, mod);
377  }
378 
379  while (cref) {
380  klass = CREF_CLASS(cref);
381  if (!CREF_PUSHED_BY_EVAL(cref) &&
382  !NIL_P(klass)) {
383  data = rb_mod_const_at(CREF_CLASS(cref), data);
384  if (!cbase) {
385  cbase = klass;
386  }
387  }
388  cref = CREF_NEXT(cref);
389  }
390 
391  if (cbase) {
392  data = rb_mod_const_of(cbase, data);
393  }
394  return rb_const_list(data);
395 }
396 
403 void
405 {
406  if (SPECIAL_CONST_P(klass)) {
407  noclass:
408  Check_Type(klass, T_CLASS);
409  }
410  if (OBJ_FROZEN(klass)) {
411  const char *desc;
412 
413  if (FL_TEST(klass, FL_SINGLETON)) {
414  desc = "object";
415  klass = rb_ivar_get(klass, id__attached__);
416  if (!SPECIAL_CONST_P(klass)) {
417  switch (BUILTIN_TYPE(klass)) {
418  case T_MODULE:
419  case T_ICLASS:
420  desc = "Module";
421  break;
422  case T_CLASS:
423  desc = "Class";
424  break;
425  }
426  }
427  }
428  else {
429  switch (BUILTIN_TYPE(klass)) {
430  case T_MODULE:
431  case T_ICLASS:
432  desc = "module";
433  break;
434  case T_CLASS:
435  desc = "class";
436  break;
437  default:
438  goto noclass;
439  }
440  }
441  rb_error_frozen(desc);
442  }
443 }
444 
445 NORETURN(static void rb_longjmp(rb_thread_t *, int, volatile VALUE, VALUE));
446 static VALUE get_errinfo(void);
447 static VALUE get_thread_errinfo(rb_thread_t *th);
448 
449 static VALUE
450 exc_setup_cause(VALUE exc, VALUE cause)
451 {
452 #if SUPPORT_JOKE
453  if (NIL_P(cause)) {
454  ID id_true_cause;
455  CONST_ID(id_true_cause, "true_cause");
456 
457  cause = rb_attr_get(rb_eFatal, id_true_cause);
458  if (NIL_P(cause)) {
459  cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby");
460  rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */
461  OBJ_FREEZE(cause);
462  rb_ivar_set(rb_eFatal, id_true_cause, cause);
463  }
464  }
465 #endif
466  if (!NIL_P(cause) && cause != exc) {
467  rb_ivar_set(exc, id_cause, cause);
468  if (!rb_ivar_defined(cause, id_cause)) {
469  rb_ivar_set(cause, id_cause, Qnil);
470  }
471  }
472  return exc;
473 }
474 
475 static inline VALUE
476 exc_setup_message(rb_thread_t *th, VALUE mesg, VALUE *cause)
477 {
478  int nocause = 0;
479 
480  if (NIL_P(mesg)) {
481  mesg = th->ec.errinfo;
483  nocause = 1;
484  }
485  if (NIL_P(mesg)) {
486  mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
487  nocause = 0;
488  }
489  if (*cause == Qundef) {
490  if (nocause) {
491  *cause = Qnil;
492  }
493  else if (!rb_ivar_defined(mesg, id_cause)) {
494  *cause = get_thread_errinfo(th);
495  }
496  }
497  return mesg;
498 }
499 
500 static void
501 setup_exception(rb_thread_t *th, int tag, volatile VALUE mesg, VALUE cause)
502 {
503  VALUE e;
504  const char *file = 0;
505  int line;
506 
507  file = rb_source_loc(&line);
508  if ((file && !NIL_P(mesg)) || (cause != Qundef)) {
509  volatile int state = 0;
510 
511  TH_PUSH_TAG(th);
512  if (EXEC_TAG() == TAG_NONE && !(state = rb_threadptr_set_raised(th))) {
513  VALUE bt = rb_get_backtrace(mesg);
514  if (!NIL_P(bt) || cause == Qundef) {
515  if (OBJ_FROZEN(mesg)) {
516  mesg = rb_obj_dup(mesg);
517  }
518  }
519  if (cause != Qundef) {
520  exc_setup_cause(mesg, cause);
521  }
522  if (NIL_P(bt)) {
524  rb_ivar_set(mesg, idBt_locations, at);
525  set_backtrace(mesg, at);
526  }
528  }
529  TH_POP_TAG();
530  if (state) goto fatal;
531  }
532 
533  if (!NIL_P(mesg)) {
534  th->ec.errinfo = mesg;
535  }
536 
537  if (RTEST(ruby_debug) && !NIL_P(e = th->ec.errinfo) &&
539  enum ruby_tag_type state;
540 
541  mesg = e;
542  TH_PUSH_TAG(th);
543  if ((state = EXEC_TAG()) == TAG_NONE) {
544  th->ec.errinfo = Qnil;
545  e = rb_obj_as_string(mesg);
546  th->ec.errinfo = mesg;
547  if (file && line) {
548  e = rb_sprintf("Exception `%"PRIsVALUE"' at %s:%d - %"PRIsVALUE"\n",
549  rb_obj_class(mesg), file, line, e);
550  }
551  else if (file) {
552  e = rb_sprintf("Exception `%"PRIsVALUE"' at %s - %"PRIsVALUE"\n",
553  rb_obj_class(mesg), file, e);
554  }
555  else {
556  e = rb_sprintf("Exception `%"PRIsVALUE"' - %"PRIsVALUE"\n",
557  rb_obj_class(mesg), e);
558  }
559  warn_print_str(e);
560  }
561  TH_POP_TAG();
562  if (state == TAG_FATAL && th->ec.errinfo == exception_error) {
563  th->ec.errinfo = mesg;
564  }
565  else if (state) {
567  TH_JUMP_TAG(th, state);
568  }
569  }
570 
571  if (rb_threadptr_set_raised(th)) {
572  fatal:
573  th->ec.errinfo = exception_error;
575  TH_JUMP_TAG(th, TAG_FATAL);
576  }
577 
578  if (tag != TAG_FATAL) {
580  EXEC_EVENT_HOOK(th, RUBY_EVENT_RAISE, th->ec.cfp->self, 0, 0, 0, mesg);
581  }
582 }
583 
585 void
587 {
588  if (cause == Qundef) {
589  cause = get_thread_errinfo(th);
590  }
591  if (cause != mesg) {
592  rb_ivar_set(mesg, id_cause, cause);
593  }
594 }
595 
596 static void
597 rb_longjmp(rb_thread_t *th, int tag, volatile VALUE mesg, VALUE cause)
598 {
599  mesg = exc_setup_message(th, mesg, &cause);
600  setup_exception(th, tag, mesg, cause);
602  TH_JUMP_TAG(th, tag);
603 }
604 
605 static VALUE make_exception(int argc, const VALUE *argv, int isstr);
606 
614 void
616 {
617  if (!NIL_P(mesg)) {
618  mesg = make_exception(1, &mesg, FALSE);
619  }
620  rb_longjmp(GET_THREAD(), TAG_RAISE, mesg, Qundef);
621 }
622 
630 void
632 {
633  if (!NIL_P(mesg)) {
634  mesg = make_exception(1, &mesg, FALSE);
635  }
636  rb_longjmp(GET_THREAD(), TAG_FATAL, mesg, Qnil);
637 }
638 
643 void
645 {
646  rb_raise(rb_eInterrupt, "%s", "");
647 }
648 
649 enum {raise_opt_cause, raise_max_opt}; /*< \private */
650 
651 static int
652 extract_raise_opts(int argc, const VALUE *argv, VALUE *opts)
653 {
654  int i;
655  if (argc > 0) {
656  VALUE opt = argv[argc-1];
657  if (RB_TYPE_P(opt, T_HASH)) {
658  if (!RHASH_EMPTY_P(opt)) {
659  ID keywords[1];
660  CONST_ID(keywords[0], "cause");
661  rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
662  if (RHASH_EMPTY_P(opt)) --argc;
663  return argc;
664  }
665  }
666  }
667  for (i = 0; i < raise_max_opt; ++i) {
668  opts[i] = Qundef;
669  }
670  return argc;
671 }
672 
673 /*
674  * call-seq:
675  * raise
676  * raise(string)
677  * raise(exception [, string [, array]])
678  * fail
679  * fail(string)
680  * fail(exception [, string [, array]])
681  *
682  * With no arguments, raises the exception in <code>$!</code> or raises
683  * a <code>RuntimeError</code> if <code>$!</code> is +nil+.
684  * With a single +String+ argument, raises a
685  * +RuntimeError+ with the string as a message. Otherwise,
686  * the first parameter should be the name of an +Exception+
687  * class (or an object that returns an +Exception+ object when sent
688  * an +exception+ message). The optional second parameter sets the
689  * message associated with the exception, and the third parameter is an
690  * array of callback information. Exceptions are caught by the
691  * +rescue+ clause of <code>begin...end</code> blocks.
692  *
693  * raise "Failed to create socket"
694  * raise ArgumentError, "No parameters", caller
695  */
696 
697 static VALUE
698 rb_f_raise(int argc, VALUE *argv)
699 {
700  VALUE err;
701  VALUE opts[raise_max_opt], *const cause = &opts[raise_opt_cause];
702 
703  argc = extract_raise_opts(argc, argv, opts);
704  if (argc == 0) {
705  if (*cause != Qundef) {
706  rb_raise(rb_eArgError, "only cause is given with no arguments");
707  }
708  err = get_errinfo();
709  if (!NIL_P(err)) {
710  argc = 1;
711  argv = &err;
712  }
713  }
714  rb_raise_jump(rb_make_exception(argc, argv), *cause);
715 
716  UNREACHABLE;
717 }
718 
719 static VALUE
720 make_exception(int argc, const VALUE *argv, int isstr)
721 {
722  VALUE mesg, exc;
723  int n;
724 
725  mesg = Qnil;
726  switch (argc) {
727  case 0:
728  break;
729  case 1:
730  exc = argv[0];
731  if (NIL_P(exc))
732  break;
733  if (isstr) {
734  mesg = rb_check_string_type(exc);
735  if (!NIL_P(mesg)) {
736  mesg = rb_exc_new3(rb_eRuntimeError, mesg);
737  break;
738  }
739  }
740  n = 0;
741  goto exception_call;
742 
743  case 2:
744  case 3:
745  exc = argv[0];
746  n = 1;
747  exception_call:
748  mesg = rb_check_funcall(exc, idException, n, argv+1);
749  if (mesg == Qundef) {
750  rb_raise(rb_eTypeError, "exception class/object expected");
751  }
752  break;
753  default:
754  rb_check_arity(argc, 0, 3);
755  break;
756  }
757  if (argc > 0) {
758  if (!rb_obj_is_kind_of(mesg, rb_eException))
759  rb_raise(rb_eTypeError, "exception object expected");
760  if (argc > 2)
761  set_backtrace(mesg, argv[2]);
762  }
763 
764  return mesg;
765 }
766 
787 VALUE
788 rb_make_exception(int argc, const VALUE *argv)
789 {
790  return make_exception(argc, argv, TRUE);
791 }
792 
796 void
797 rb_raise_jump(VALUE mesg, VALUE cause)
798 {
799  rb_thread_t *th = GET_THREAD();
800  const rb_control_frame_t *cfp = th->ec.cfp;
802  VALUE klass = me->owner;
803  VALUE self = cfp->self;
804  ID mid = me->called_id;
805 
806  rb_vm_pop_frame(th);
807  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, self, me->def->original_id, mid, klass, Qnil);
808 
809  rb_longjmp(th, TAG_RAISE, mesg, cause);
810 }
811 
820 void
821 rb_jump_tag(int tag)
822 {
823  if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) {
825  }
826  JUMP_TAG(tag);
827 }
828 
834 int
836 {
837  rb_thread_t *th = GET_THREAD();
839  return FALSE;
840  }
841  else {
842  return TRUE;
843  }
844 }
845 
851 int
853 {
854  return rb_block_given_p();
855 }
856 
858 
864 void
866 {
867  if (!rb_block_given_p()) {
868  rb_vm_localjump_error("no block given", Qnil, 0);
869  }
870 }
871 
894 VALUE
895 rb_rescue2(VALUE (* b_proc) (ANYARGS), VALUE data1,
896  VALUE (* r_proc) (ANYARGS), VALUE data2, ...)
897 {
898  enum ruby_tag_type state;
899  rb_thread_t *th = GET_THREAD();
900  rb_control_frame_t *volatile cfp = th->ec.cfp;
901  volatile VALUE result = Qfalse;
902  volatile VALUE e_info = th->ec.errinfo;
903  va_list args;
904 
905  TH_PUSH_TAG(th);
906  if ((state = TH_EXEC_TAG()) == TAG_NONE) {
907  retry_entry:
908  result = (*b_proc) (data1);
909  }
910  else if (result) {
911  /* escape from r_proc */
912  if (state == TAG_RETRY) {
913  state = 0;
914  th->ec.errinfo = Qnil;
915  result = Qfalse;
916  goto retry_entry;
917  }
918  }
919  else {
920  rb_vm_rewind_cfp(th, cfp);
921 
922  if (state == TAG_RAISE) {
923  int handle = FALSE;
924  VALUE eclass;
925 
926  va_init_list(args, data2);
927  while ((eclass = va_arg(args, VALUE)) != 0) {
928  if (rb_obj_is_kind_of(th->ec.errinfo, eclass)) {
929  handle = TRUE;
930  break;
931  }
932  }
933  va_end(args);
934 
935  if (handle) {
936  result = Qnil;
937  state = 0;
938  if (r_proc) {
939  result = (*r_proc) (data2, th->ec.errinfo);
940  }
941  th->ec.errinfo = e_info;
942  }
943  }
944  }
945  TH_POP_TAG();
946  if (state)
947  TH_JUMP_TAG(th, state);
948 
949  return result;
950 }
951 
966 VALUE
967 rb_rescue(VALUE (* b_proc)(ANYARGS), VALUE data1,
968  VALUE (* r_proc)(ANYARGS), VALUE data2)
969 {
970  return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
971  (VALUE)0);
972 }
973 
991 VALUE
992 rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate)
993 {
994  volatile VALUE result = Qnil;
995  volatile enum ruby_tag_type state;
996  rb_thread_t *th = GET_THREAD();
997  rb_control_frame_t *volatile cfp = th->ec.cfp;
998  struct rb_vm_protect_tag protect_tag;
999  rb_jmpbuf_t org_jmpbuf;
1000 
1001  protect_tag.prev = th->ec.protect_tag;
1002 
1003  TH_PUSH_TAG(th);
1004  th->ec.protect_tag = &protect_tag;
1005  MEMCPY(&org_jmpbuf, &(th)->root_jmpbuf, rb_jmpbuf_t, 1);
1006  if ((state = TH_EXEC_TAG()) == TAG_NONE) {
1007  SAVE_ROOT_JMPBUF(th, result = (*proc) (data));
1008  }
1009  else {
1010  rb_vm_rewind_cfp(th, cfp);
1011  }
1012  MEMCPY(&(th)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);
1013  th->ec.protect_tag = protect_tag.prev;
1014  TH_POP_TAG();
1015 
1016  if (pstate != NULL) *pstate = state;
1017  return result;
1018 }
1019 
1034 VALUE
1035 rb_ensure(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*e_proc)(ANYARGS), VALUE data2)
1036 {
1037  int state;
1038  volatile VALUE result = Qnil;
1039  VALUE errinfo;
1040  rb_thread_t *const th = GET_THREAD();
1041  rb_ensure_list_t ensure_list;
1042  ensure_list.entry.marker = 0;
1043  ensure_list.entry.e_proc = e_proc;
1044  ensure_list.entry.data2 = data2;
1045  ensure_list.next = th->ec.ensure_list;
1046  th->ec.ensure_list = &ensure_list;
1047  TH_PUSH_TAG(th);
1048  if ((state = EXEC_TAG()) == TAG_NONE) {
1049  result = (*b_proc) (data1);
1050  }
1051  TH_POP_TAG();
1052  errinfo = th->ec.errinfo;
1053  if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) {
1054  th->ec.errinfo = Qnil;
1055  }
1056  th->ec.ensure_list=ensure_list.next;
1057  (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
1058  th->ec.errinfo = errinfo;
1059  if (state)
1060  TH_JUMP_TAG(th, state);
1061  return result;
1062 }
1063 
1064 static ID
1065 frame_func_id(rb_control_frame_t *cfp)
1066 {
1068 
1069  if (me) {
1070  return me->def->original_id;
1071  }
1072  else {
1073  return 0;
1074  }
1075 }
1076 
1077 static ID
1078 frame_called_id(rb_control_frame_t *cfp)
1079 {
1081 
1082  if (me) {
1083  return me->called_id;
1084  }
1085  else {
1086  return 0;
1087  }
1088 }
1089 
1102 ID
1104 {
1105  return frame_func_id(GET_THREAD()->ec.cfp);
1106 }
1107 
1119 ID
1121 {
1122  return frame_called_id(GET_THREAD()->ec.cfp);
1123 }
1124 
1125 static rb_control_frame_t *
1126 previous_frame(rb_thread_t *th)
1127 {
1129  /* check if prev_cfp can be accessible */
1130  if ((void *)(th->ec.vm_stack + th->ec.vm_stack_size) == (void *)(prev_cfp)) {
1131  return 0;
1132  }
1133  return prev_cfp;
1134 }
1135 
1136 static ID
1137 prev_frame_callee(void)
1138 {
1139  rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());
1140  if (!prev_cfp) return 0;
1141  return frame_called_id(prev_cfp);
1142 }
1143 
1144 static ID
1145 prev_frame_func(void)
1146 {
1147  rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());
1148  if (!prev_cfp) return 0;
1149  return frame_func_id(prev_cfp);
1150 }
1151 
1158 ID
1160 {
1161  rb_thread_t *th = GET_THREAD();
1162  rb_control_frame_t *cfp = th->ec.cfp;
1163  ID mid;
1164 
1165  while (!(mid = frame_func_id(cfp)) &&
1166  (cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp),
1168  return mid;
1169 }
1170 
1171 /*
1172  * call-seq:
1173  * append_features(mod) -> mod
1174  *
1175  * When this module is included in another, Ruby calls
1176  * <code>append_features</code> in this module, passing it the
1177  * receiving module in _mod_. Ruby's default implementation is
1178  * to add the constants, methods, and module variables of this module
1179  * to _mod_ if this module has not already been added to
1180  * _mod_ or one of its ancestors. See also <code>Module#include</code>.
1181  */
1182 
1183 static VALUE
1184 rb_mod_append_features(VALUE module, VALUE include)
1185 {
1186  if (!CLASS_OR_MODULE_P(include)) {
1187  Check_Type(include, T_CLASS);
1188  }
1189  rb_include_module(include, module);
1190 
1191  return module;
1192 }
1193 
1194 /*
1195  * call-seq:
1196  * include(module, ...) -> self
1197  *
1198  * Invokes <code>Module.append_features</code> on each parameter in reverse order.
1199  */
1200 
1201 static VALUE
1202 rb_mod_include(int argc, VALUE *argv, VALUE module)
1203 {
1204  int i;
1205  ID id_append_features, id_included;
1206 
1207  CONST_ID(id_append_features, "append_features");
1208  CONST_ID(id_included, "included");
1209 
1211  for (i = 0; i < argc; i++)
1212  Check_Type(argv[i], T_MODULE);
1213  while (argc--) {
1214  rb_funcall(argv[argc], id_append_features, 1, module);
1215  rb_funcall(argv[argc], id_included, 1, module);
1216  }
1217  return module;
1218 }
1219 
1220 /*
1221  * call-seq:
1222  * prepend_features(mod) -> mod
1223  *
1224  * When this module is prepended in another, Ruby calls
1225  * <code>prepend_features</code> in this module, passing it the
1226  * receiving module in _mod_. Ruby's default implementation is
1227  * to overlay the constants, methods, and module variables of this module
1228  * to _mod_ if this module has not already been added to
1229  * _mod_ or one of its ancestors. See also <code>Module#prepend</code>.
1230  */
1231 
1232 static VALUE
1233 rb_mod_prepend_features(VALUE module, VALUE prepend)
1234 {
1235  if (!CLASS_OR_MODULE_P(prepend)) {
1236  Check_Type(prepend, T_CLASS);
1237  }
1238  rb_prepend_module(prepend, module);
1239 
1240  return module;
1241 }
1242 
1243 /*
1244  * call-seq:
1245  * prepend(module, ...) -> self
1246  *
1247  * Invokes <code>Module.prepend_features</code> on each parameter in reverse order.
1248  */
1249 
1250 static VALUE
1251 rb_mod_prepend(int argc, VALUE *argv, VALUE module)
1252 {
1253  int i;
1254  ID id_prepend_features, id_prepended;
1255 
1256  CONST_ID(id_prepend_features, "prepend_features");
1257  CONST_ID(id_prepended, "prepended");
1258 
1260  for (i = 0; i < argc; i++)
1261  Check_Type(argv[i], T_MODULE);
1262  while (argc--) {
1263  rb_funcall(argv[argc], id_prepend_features, 1, module);
1264  rb_funcall(argv[argc], id_prepended, 1, module);
1265  }
1266  return module;
1267 }
1268 
1269 static void
1270 ensure_class_or_module(VALUE obj)
1271 {
1272  if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
1274  "wrong argument type %"PRIsVALUE" (expected Class or Module)",
1275  rb_obj_class(obj));
1276  }
1277 }
1278 
1279 static VALUE
1280 hidden_identity_hash_new(void)
1281 {
1282  VALUE hash = rb_ident_hash_new();
1283 
1284  RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1285  return hash;
1286 }
1287 
1292 void
1293 rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module)
1294 {
1295  VALUE iclass, c, superclass = klass;
1296 
1297  ensure_class_or_module(klass);
1298  Check_Type(module, T_MODULE);
1299  if (NIL_P(CREF_REFINEMENTS(cref))) {
1300  CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new());
1301  }
1302  else {
1303  if (CREF_OMOD_SHARED(cref)) {
1304  CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref)));
1305  CREF_OMOD_SHARED_UNSET(cref);
1306  }
1307  if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) {
1308  superclass = c;
1309  while (c && RB_TYPE_P(c, T_ICLASS)) {
1310  if (RBASIC(c)->klass == module) {
1311  /* already used refinement */
1312  return;
1313  }
1314  c = RCLASS_SUPER(c);
1315  }
1316  }
1317  }
1318  FL_SET(module, RMODULE_IS_OVERLAID);
1319  c = iclass = rb_include_class_new(module, superclass);
1320  RCLASS_REFINED_CLASS(c) = klass;
1321 
1323  RCLASS_M_TBL(OBJ_WB_UNPROTECT(module)); /* TODO: check unprotecting */
1324 
1325  module = RCLASS_SUPER(module);
1326  while (module && module != klass) {
1327  FL_SET(module, RMODULE_IS_OVERLAID);
1328  c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
1329  RCLASS_REFINED_CLASS(c) = klass;
1330  module = RCLASS_SUPER(module);
1331  }
1332  rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass);
1333 }
1334 
1335 static int
1336 using_refinement(VALUE klass, VALUE module, VALUE arg)
1337 {
1338  rb_cref_t *cref = (rb_cref_t *) arg;
1339 
1340  rb_using_refinement(cref, klass, module);
1341  return ST_CONTINUE;
1342 }
1343 
1344 static void
1345 using_module_recursive(const rb_cref_t *cref, VALUE klass)
1346 {
1347  ID id_refinements;
1348  VALUE super, module, refinements;
1349 
1350  super = RCLASS_SUPER(klass);
1351  if (super) {
1352  using_module_recursive(cref, super);
1353  }
1354  switch (BUILTIN_TYPE(klass)) {
1355  case T_MODULE:
1356  module = klass;
1357  break;
1358 
1359  case T_ICLASS:
1360  module = RBASIC(klass)->klass;
1361  break;
1362 
1363  default:
1364  rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1365  rb_obj_classname(klass));
1366  break;
1367  }
1368  CONST_ID(id_refinements, "__refinements__");
1369  refinements = rb_attr_get(module, id_refinements);
1370  if (NIL_P(refinements)) return;
1371  rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1372 }
1373 
1378 void
1379 rb_using_module(const rb_cref_t *cref, VALUE module)
1380 {
1381  Check_Type(module, T_MODULE);
1382  using_module_recursive(cref, module);
1384 }
1385 
1387 VALUE
1389 {
1390  ID id_refined_class;
1391 
1392  CONST_ID(id_refined_class, "__refined_class__");
1393  return rb_attr_get(module, id_refined_class);
1394 }
1395 
1396 static void
1397 add_activated_refinement(VALUE activated_refinements,
1398  VALUE klass, VALUE refinement)
1399 {
1400  VALUE iclass, c, superclass = klass;
1401 
1402  if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1403  superclass = c;
1404  while (c && RB_TYPE_P(c, T_ICLASS)) {
1405  if (RBASIC(c)->klass == refinement) {
1406  /* already used refinement */
1407  return;
1408  }
1409  c = RCLASS_SUPER(c);
1410  }
1411  }
1412  FL_SET(refinement, RMODULE_IS_OVERLAID);
1413  c = iclass = rb_include_class_new(refinement, superclass);
1414  RCLASS_REFINED_CLASS(c) = klass;
1415  refinement = RCLASS_SUPER(refinement);
1416  while (refinement && refinement != klass) {
1417  FL_SET(refinement, RMODULE_IS_OVERLAID);
1418  c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1419  RCLASS_REFINED_CLASS(c) = klass;
1420  refinement = RCLASS_SUPER(refinement);
1421  }
1422  rb_hash_aset(activated_refinements, klass, iclass);
1423 }
1424 
1425 /*
1426  * call-seq:
1427  * refine(mod) { block } -> module
1428  *
1429  * Refine <i>mod</i> in the receiver.
1430  *
1431  * Returns a module, where refined methods are defined.
1432  */
1433 
1434 static VALUE
1435 rb_mod_refine(VALUE module, VALUE klass)
1436 {
1437  VALUE refinement;
1438  ID id_refinements, id_activated_refinements,
1439  id_refined_class, id_defined_at;
1440  VALUE refinements, activated_refinements;
1441  rb_thread_t *th = GET_THREAD();
1442  VALUE block_handler = rb_vm_frame_block_handler(th->ec.cfp);
1443 
1444  if (block_handler == VM_BLOCK_HANDLER_NONE) {
1445  rb_raise(rb_eArgError, "no block given");
1446  }
1447  if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
1448  rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine");
1449  }
1450 
1451  ensure_class_or_module(klass);
1452  CONST_ID(id_refinements, "__refinements__");
1453  refinements = rb_attr_get(module, id_refinements);
1454  if (NIL_P(refinements)) {
1455  refinements = hidden_identity_hash_new();
1456  rb_ivar_set(module, id_refinements, refinements);
1457  }
1458  CONST_ID(id_activated_refinements, "__activated_refinements__");
1459  activated_refinements = rb_attr_get(module, id_activated_refinements);
1460  if (NIL_P(activated_refinements)) {
1461  activated_refinements = hidden_identity_hash_new();
1462  rb_ivar_set(module, id_activated_refinements,
1463  activated_refinements);
1464  }
1465  refinement = rb_hash_lookup(refinements, klass);
1466  if (NIL_P(refinement)) {
1467  refinement = rb_module_new();
1468  RCLASS_SET_SUPER(refinement, klass);
1469  FL_SET(refinement, RMODULE_IS_REFINEMENT);
1470  CONST_ID(id_refined_class, "__refined_class__");
1471  rb_ivar_set(refinement, id_refined_class, klass);
1472  CONST_ID(id_defined_at, "__defined_at__");
1473  rb_ivar_set(refinement, id_defined_at, module);
1474  rb_hash_aset(refinements, klass, refinement);
1475  add_activated_refinement(activated_refinements, klass, refinement);
1476  }
1477  rb_yield_refine_block(refinement, activated_refinements);
1478  return refinement;
1479 }
1480 
1481 static void
1482 ignored_block(VALUE module, const char *klass)
1483 {
1484  const char *anon = "";
1485  Check_Type(module, T_MODULE);
1486  if (!RTEST(rb_search_class_path(module))) {
1487  anon = ", maybe for Module.new";
1488  }
1489  rb_warn("%s""using doesn't call the given block""%s.", klass, anon);
1490 }
1491 
1492 /*
1493  * call-seq:
1494  * using(module) -> self
1495  *
1496  * Import class refinements from <i>module</i> into the current class or
1497  * module definition.
1498  */
1499 
1500 static VALUE
1501 mod_using(VALUE self, VALUE module)
1502 {
1503  rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());
1504 
1505  if (prev_frame_func()) {
1507  "Module#using is not permitted in methods");
1508  }
1509  if (prev_cfp && prev_cfp->self != self) {
1510  rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1511  }
1512  if (rb_block_given_p()) {
1513  ignored_block(module, "Module#");
1514  }
1515  rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1516  return self;
1517 }
1518 
1519 static int
1520 used_modules_i(VALUE _, VALUE mod, VALUE ary)
1521 {
1522  ID id_defined_at;
1523  CONST_ID(id_defined_at, "__defined_at__");
1524  while (FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1525  rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at));
1526  mod = RCLASS_SUPER(mod);
1527  }
1528  return ST_CONTINUE;
1529 }
1530 
1531 /*
1532  * call-seq:
1533  * used_modules -> array
1534  *
1535  * Returns an array of all modules used in the current scope. The ordering
1536  * of modules in the resulting array is not defined.
1537  *
1538  * module A
1539  * refine Object do
1540  * end
1541  * end
1542  *
1543  * module B
1544  * refine Object do
1545  * end
1546  * end
1547  *
1548  * using A
1549  * using B
1550  * p Module.used_modules
1551  *
1552  * <em>produces:</em>
1553  *
1554  * [B, A]
1555  */
1556 static VALUE
1557 rb_mod_s_used_modules(void)
1558 {
1559  const rb_cref_t *cref = rb_vm_cref();
1560  VALUE ary = rb_ary_new();
1561 
1562  while(cref) {
1563  if(!NIL_P(CREF_REFINEMENTS(cref))) {
1564  rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary);
1565  }
1566  cref = CREF_NEXT(cref);
1567  }
1568 
1569  return rb_funcall(ary, rb_intern("uniq"), 0);
1570 }
1571 
1582 void
1583 rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
1584 {
1586  rb_funcallv(obj, idInitialize, argc, argv);
1587 }
1588 
1595 void
1597 {
1598  rb_include_module(rb_singleton_class(obj), module);
1599 }
1600 
1601 /*
1602  * call-seq:
1603  * extend_object(obj) -> obj
1604  *
1605  * Extends the specified object by adding this module's constants and
1606  * methods (which are added as singleton methods). This is the callback
1607  * method used by <code>Object#extend</code>.
1608  *
1609  * module Picky
1610  * def Picky.extend_object(o)
1611  * if String === o
1612  * puts "Can't add Picky to a String"
1613  * else
1614  * puts "Picky added to #{o.class}"
1615  * super
1616  * end
1617  * end
1618  * end
1619  * (s = Array.new).extend Picky # Call Object.extend
1620  * (s = "quick brown fox").extend Picky
1621  *
1622  * <em>produces:</em>
1623  *
1624  * Picky added to Array
1625  * Can't add Picky to a String
1626  */
1627 
1628 static VALUE
1629 rb_mod_extend_object(VALUE mod, VALUE obj)
1630 {
1631  rb_extend_object(obj, mod);
1632  return obj;
1633 }
1634 
1635 /*
1636  * call-seq:
1637  * obj.extend(module, ...) -> obj
1638  *
1639  * Adds to _obj_ the instance methods from each module given as a
1640  * parameter.
1641  *
1642  * module Mod
1643  * def hello
1644  * "Hello from Mod.\n"
1645  * end
1646  * end
1647  *
1648  * class Klass
1649  * def hello
1650  * "Hello from Klass.\n"
1651  * end
1652  * end
1653  *
1654  * k = Klass.new
1655  * k.hello #=> "Hello from Klass.\n"
1656  * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1657  * k.hello #=> "Hello from Mod.\n"
1658  */
1659 
1660 static VALUE
1661 rb_obj_extend(int argc, VALUE *argv, VALUE obj)
1662 {
1663  int i;
1664  ID id_extend_object, id_extended;
1665 
1666  CONST_ID(id_extend_object, "extend_object");
1667  CONST_ID(id_extended, "extended");
1668 
1670  for (i = 0; i < argc; i++)
1671  Check_Type(argv[i], T_MODULE);
1672  while (argc--) {
1673  rb_funcall(argv[argc], id_extend_object, 1, obj);
1674  rb_funcall(argv[argc], id_extended, 1, obj);
1675  }
1676  return obj;
1677 }
1678 
1679 /*
1680  * call-seq:
1681  * include(module, ...) -> self
1682  *
1683  * Invokes <code>Module.append_features</code>
1684  * on each parameter in turn. Effectively adds the methods and constants
1685  * in each module to the receiver.
1686  */
1687 
1688 static VALUE
1689 top_include(int argc, VALUE *argv, VALUE self)
1690 {
1691  rb_thread_t *th = GET_THREAD();
1692 
1693  if (th->top_wrapper) {
1694  rb_warning("main.include in the wrapped load is effective only in wrapper module");
1695  return rb_mod_include(argc, argv, th->top_wrapper);
1696  }
1697  return rb_mod_include(argc, argv, rb_cObject);
1698 }
1699 
1700 /*
1701  * call-seq:
1702  * using(module) -> self
1703  *
1704  * Import class refinements from <i>module</i> into the scope where
1705  * <code>using</code> is called.
1706  */
1707 
1708 static VALUE
1709 top_using(VALUE self, VALUE module)
1710 {
1711  const rb_cref_t *cref = rb_vm_cref();
1712  rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());
1713 
1714  if (CREF_NEXT(cref) || (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) {
1715  rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel");
1716  }
1717  if (rb_block_given_p()) {
1718  ignored_block(module, "main.");
1719  }
1720  rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1721  return self;
1722 }
1723 
1724 static const VALUE *
1725 errinfo_place(rb_thread_t *th)
1726 {
1727  rb_control_frame_t *cfp = th->ec.cfp;
1729 
1730  while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1731  if (VM_FRAME_RUBYFRAME_P(cfp)) {
1732  if (cfp->iseq->body->type == ISEQ_TYPE_RESCUE) {
1733  return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1734  }
1735  else if (cfp->iseq->body->type == ISEQ_TYPE_ENSURE &&
1738  return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1739  }
1740  }
1741  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1742  }
1743  return 0;
1744 }
1745 
1746 static VALUE
1747 get_thread_errinfo(rb_thread_t *th)
1748 {
1749  const VALUE *ptr = errinfo_place(th);
1750  if (ptr) {
1751  return *ptr;
1752  }
1753  else {
1754  return th->ec.errinfo;
1755  }
1756 }
1757 
1758 static VALUE
1759 get_errinfo(void)
1760 {
1761  return get_thread_errinfo(GET_THREAD());
1762 }
1763 
1764 static VALUE
1765 errinfo_getter(ID id)
1766 {
1767  return get_errinfo();
1768 }
1769 
1776 VALUE
1778 {
1779  rb_thread_t *th = GET_THREAD();
1780  return th->ec.errinfo;
1781 }
1782 
1791 void
1793 {
1794  if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1795  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1796  }
1797  GET_THREAD()->ec.errinfo = err;
1798 }
1799 
1800 static VALUE
1801 errat_getter(ID id)
1802 {
1803  VALUE err = get_errinfo();
1804  if (!NIL_P(err)) {
1805  return rb_get_backtrace(err);
1806  }
1807  else {
1808  return Qnil;
1809  }
1810 }
1811 
1812 static void
1813 errat_setter(VALUE val, ID id, VALUE *var)
1814 {
1815  VALUE err = get_errinfo();
1816  if (NIL_P(err)) {
1817  rb_raise(rb_eArgError, "$! not set");
1818  }
1819  set_backtrace(err, val);
1820 }
1821 
1822 /*
1823  * call-seq:
1824  * __method__ -> symbol
1825  *
1826  * Returns the name at the definition of the current method as a
1827  * Symbol.
1828  * If called outside of a method, it returns <code>nil</code>.
1829  *
1830  */
1831 
1832 static VALUE
1833 rb_f_method_name(void)
1834 {
1835  ID fname = prev_frame_func(); /* need *method* ID */
1836 
1837  if (fname) {
1838  return ID2SYM(fname);
1839  }
1840  else {
1841  return Qnil;
1842  }
1843 }
1844 
1845 /*
1846  * call-seq:
1847  * __callee__ -> symbol
1848  *
1849  * Returns the called name of the current method as a Symbol.
1850  * If called outside of a method, it returns <code>nil</code>.
1851  *
1852  */
1853 
1854 static VALUE
1855 rb_f_callee_name(void)
1856 {
1857  ID fname = prev_frame_callee(); /* need *callee* ID */
1858 
1859  if (fname) {
1860  return ID2SYM(fname);
1861  }
1862  else {
1863  return Qnil;
1864  }
1865 }
1866 
1867 /*
1868  * call-seq:
1869  * __dir__ -> string
1870  *
1871  * Returns the canonicalized absolute path of the directory of the file from
1872  * which this method is called. It means symlinks in the path is resolved.
1873  * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1874  * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1875  *
1876  */
1877 static VALUE
1878 f_current_dirname(void)
1879 {
1880  VALUE base = rb_current_realfilepath();
1881  if (NIL_P(base)) {
1882  return Qnil;
1883  }
1884  base = rb_file_dirname(base);
1885  return base;
1886 }
1887 
1888 void
1890 {
1891  rb_define_virtual_variable("$@", errat_getter, errat_setter);
1892  rb_define_virtual_variable("$!", errinfo_getter, 0);
1893 
1894  rb_define_global_function("raise", rb_f_raise, -1);
1895  rb_define_global_function("fail", rb_f_raise, -1);
1896 
1897  rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */
1898 
1899  rb_define_global_function("__method__", rb_f_method_name, 0);
1900  rb_define_global_function("__callee__", rb_f_callee_name, 0);
1901  rb_define_global_function("__dir__", f_current_dirname, 0);
1902 
1903  rb_define_method(rb_cModule, "include", rb_mod_include, -1);
1904  rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
1905 
1906  rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
1907  rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
1908  rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
1909  rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
1910  rb_define_private_method(rb_cModule, "using", mod_using, 1);
1911  rb_define_singleton_method(rb_cModule, "used_modules",
1912  rb_mod_s_used_modules, 0);
1913  rb_undef_method(rb_cClass, "refine");
1914 
1915  rb_undef_method(rb_cClass, "module_function");
1916 
1917  Init_vm_eval();
1918  Init_eval_method();
1919 
1920  rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
1921  rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
1922 
1924  "include", top_include, -1);
1926  "using", top_using, 1);
1927 
1928  rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
1929 
1930  rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */
1931  rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */
1932 
1934  rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");
1935 
1936  id_signo = rb_intern_const("signo");
1937  id_status = rb_intern_const("status");
1938  id_cause = rb_intern_const("cause");
1939 }
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:1469
int ruby_run_node(void *n)
Runs the given compiled source and exits this process.
Definition: eval.c:295
void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
Definition: thread.c:462
#define T_OBJECT
Definition: ruby.h:491
VALUE rb_eLocalJumpError
Definition: eval.c:24
#define RUBY_VM_CHECK_INTS(th)
Definition: vm_core.h:1627
const VALUE * ep
Definition: vm_core.h:667
int ruby_cleanup(volatile int ex)
Destructs the VM.
Definition: eval.c:161
struct rb_ensure_entry entry
Definition: vm_core.h:733
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:992
void rb_warn(const char *fmt,...)
Definition: error.c:246
void rb_call_inits(void)
Definition: inits.c:17
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:2086
#define FALSE
Definition: nkf.h:174
ruby_tag_type
Definition: vm_core.h:151
#define va_init_list(a, b)
Definition: win32ole.h:34
#define INTERNAL_EXCEPTION_P(exc)
Definition: eval_intern.h:208
#define VM_ENV_INDEX_LAST_LVAR
Definition: vm_core.h:1055
ID ruby_static_id_signo
Definition: eval.c:27
void ruby_finalize(void)
Runs the VM finalization processes.
Definition: eval.c:144
void rb_define_virtual_variable(const char *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:648
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:449
VALUE rb_threadptr_backtrace_object(rb_thread_t *th)
Definition: vm_backtrace.c:515
#define NUM2INT(x)
Definition: ruby.h:684
const VALUE owner
Definition: method.h:64
rb_control_frame_t * cfp
Definition: vm_core.h:744
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:1238
#define TAG_NONE
Definition: vm_core.h:164
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
void Init_BareVM(void)
Definition: vm.c:3122
VALUE rb_iseq_eval_main(const rb_iseq_t *iseq)
Definition: vm.c:2037
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define T_MODULE
Definition: ruby.h:494
#define RUBY_EVENT_RAISE
Definition: ruby.h:2087
ID rb_frame_last_func(void)
Returns the ID of the last method in the call stack.
Definition: eval.c:1159
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:821
void * ruby_options(int argc, char **argv)
Processes command line arguments and compiles the Ruby source to execute.
Definition: eval.c:97
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1847
#define Qtrue
Definition: ruby.h:437
VALUE rb_current_realfilepath(void)
Definition: vm_eval.c:2145
void rb_exec_end_proc(void)
Definition: eval_jump.c:114
struct rb_method_definition_struct *const def
Definition: method.h:62
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:37
Definition: st.h:99
#define OBJ_FREEZE(x)
Definition: ruby.h:1306
const rb_callable_method_entry_t * rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Definition: error.c:836
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
VALUE data2
Definition: vm_core.h:728
#define rb_vm_register_special_exception(sp, e, m)
Definition: vm_core.h:1543
#define TH_JUMP_TAG(th, st)
Definition: eval_intern.h:204
#define rb_check_arity
Definition: intern.h:298
int ruby_exec_node(void *n)
Runs the given compiled source.
Definition: eval.c:307
#define UNREACHABLE
Definition: ruby.h:46
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
#define VM_BLOCK_HANDLER_NONE
Definition: vm_core.h:1135
#define EXIT_SUCCESS
Definition: error.c:37
struct rb_iseq_constant_body * body
Definition: vm_core.h:423
void rb_threadptr_setup_exception(rb_thread_t *th, VALUE mesg, VALUE cause)
Definition: eval.c:586
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_ensure_list_t * ensure_list
Definition: vm_core.h:767
#define STACK_UPPER(x, a, b)
Definition: gc.h:77
#define Check_Type(v, t)
Definition: ruby.h:562
struct rb_vm_protect_tag * prev
Definition: vm_core.h:710
VALUE rb_obj_dup(VALUE)
call-seq: obj.dup -> an_object
Definition: object.c:526
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1210
void Init_vm_eval(void)
Definition: vm_eval.c:2155
void Init_vm_objects(void)
Definition: vm.c:3145
VALUE rb_rescue(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2)
An equivalent of rescue clause.
Definition: eval.c:967
#define TH_EXEC_TAG()
Definition: eval_intern.h:198
#define T_HASH
Definition: ruby.h:499
void Init_heap(void)
Definition: gc.c:2381
void rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:1414
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1388
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:853
#define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)
Definition: vm_core.h:1244
#define PUSH_TAG()
Definition: eval_intern.h:147
void Init_eval(void)
Definition: eval.c:1889
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1745
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
#define RUBY_DTRACE_HOOK(name, arg)
Definition: internal.h:1934
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Calls #initialize method of obj with the given arguments.
Definition: eval.c:1583
#define FIXNUM_P(f)
Definition: ruby.h:365
#define ATOMIC_VALUE_EXCHANGE(var, val)
Definition: ruby_atomic.h:206
const char * rb_source_loc(int *pline)
Definition: vm.c:1313
#define FL_TEST(x, f)
Definition: ruby.h:1282
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1374
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
An equivalent of rescue clause.
Definition: eval.c:895
void rb_thread_terminate_all(void)
Definition: thread.c:479
const char * rb_obj_classname(VALUE)
Definition: variable.c:459
#define THROW_DATA_P(err)
Definition: internal.h:903
#define GET_THREAD()
Definition: vm_core.h:1583
VALUE rb_eArgError
Definition: error.c:802
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:385
#define FL_SINGLETON
Definition: ruby.h:1208
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:973
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1689
#define id_signo
Definition: eval.c:29
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
Definition: object.c:277
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
Definition: object.c:842
#define TH_POP_TAG()
Definition: eval_intern.h:138
#define UNLIKELY(x)
Definition: internal.h:43
VALUE rb_eSignal
Definition: error.c:797
const rb_iseq_t * iseq
Definition: vm_core.h:665
void * rb_mod_const_at(VALUE, void *)
Definition: variable.c:2414
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
#define EXEC_TAG()
Definition: eval_intern.h:201
int rb_threadptr_set_raised(rb_thread_t *th)
Definition: thread.c:2173
void ruby_init(void)
Calls ruby_setup() and check error.
Definition: eval.c:76
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
VALUE rb_eSysStackError
Definition: eval.c:25
VALUE(* e_proc)(ANYARGS)
Definition: vm_core.h:727
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1410
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1792
#define FL_SET(x, f)
Definition: ruby.h:1288
VALUE rb_ary_new(void)
Definition: array.c:499
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1424
#define exception_error
Definition: eval.c:32
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1881
#define JUMP_TAG(st)
Definition: eval_intern.h:206
#define NIL_P(v)
Definition: ruby.h:451
void rb_frozen_class_p(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:404
#define warn_print_str(x)
Definition: eval_error.c:16
void rb_threadptr_check_signal(rb_thread_t *mth)
Definition: thread.c:4029
int argc
Definition: ruby.c:187
void rb_thread_stop_timer_thread(void)
Definition: thread.c:4070
char ary[RSTRING_EMBED_LEN_MAX+1]
Definition: ruby.h:965
#define Qfalse
Definition: ruby.h:436
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1916
Definition: method.h:59
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition: eval.c:256
int err
Definition: win32.c:135
#define EXIT_FAILURE
Definition: eval_intern.h:33
void rb_error_frozen(const char *what)
Definition: error.c:2584
#define POP_TAG()
Definition: eval_intern.h:148
void rb_trap_exit(void)
Definition: signal.c:1024
#define numberof(array)
Definition: etc.c:618
VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements)
Definition: vm_eval.c:1584
#define rb_thread_raised_clear(th)
Definition: eval_intern.h:288
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:795
rb_cref_t * rb_vm_cref_replace_with_duplicated_cref(void)
Definition: vm.c:1334
void rb_vm_pop_frame(rb_thread_t *th)
VALUE rb_eException
Definition: error.c:794
#define RCLASS_M_TBL(c)
Definition: internal.h:791
#define TRUE
Definition: nkf.h:175
void rb_need_block(void)
Declares that the current method needs a block.
Definition: eval.c:865
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:818
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:2190
VALUE rb_file_dirname(VALUE fname)
Definition: file.c:4317
void * rb_mod_const_of(VALUE, void *)
Definition: variable.c:2427
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1315
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
#define TAG_FATAL
Definition: vm_core.h:172
VALUE rb_make_exception(int argc, const VALUE *argv)
Make an Exception object from the list of arguments in a manner similar to Kernel#raise.
Definition: eval.c:788
#define Qnil
Definition: ruby.h:438
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:90
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:615
struct rb_vm_protect_tag * protect_tag
Definition: vm_core.h:747
VALUE rb_const_list(void *)
Definition: variable.c:2449
VALUE rb_eStandardError
Definition: error.c:799
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
unsigned long VALUE
Definition: ruby.h:85
VALUE rb_vm_top_self(void)
Definition: vm.c:3166
#define SAVE_ROOT_JMPBUF(th, stmt)
Definition: eval_intern.h:121
VALUE rb_f_trace_var(int, const VALUE *)
Definition: variable.c:688
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, called_id_, klass_, data_)
Definition: vm_core.h:1686
RUBY_JMP_BUF rb_jmpbuf_t
Definition: vm_core.h:690
int ruby_vm_destruct(ruby_vm_t *vm)
#define RBASIC(obj)
Definition: ruby.h:1197
VALUE rb_eTypeError
Definition: error.c:801
#define id_status
Definition: eval.c:30
int rb_threadptr_reset_raised(rb_thread_t *th)
Definition: thread.c:2183
#define FIX2INT(x)
Definition: ruby.h:686
ID called_id
Definition: method.h:63
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:131
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:389
void ruby_init_stack(volatile VALUE *)
CREF (Class REFerence)
Definition: method.h:41
struct rb_ensure_list * next
Definition: vm_core.h:732
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1899
enum rb_iseq_constant_body::iseq_type type
#define RUBY_VM_END_CONTROL_FRAME(th)
Definition: vm_core.h:1240
#define _(args)
Definition: dln.h:28
#define rb_funcallv
Definition: console.c:21
VALUE rb_mod_constants(int, const VALUE *, VALUE)
Definition: variable.c:2481
enum rb_thread_status status
Definition: vm_core.h:812
#define rb_exc_new3
Definition: intern.h:244
VALUE rb_eInterrupt
Definition: error.c:796
VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
Definition: vm.c:82
VALUE rb_f_untrace_var(int, const VALUE *)
Definition: variable.c:747
VALUE rb_ident_hash_new(void)
Definition: hash.c:2924
#define TAG_RETRY
Definition: vm_core.h:168
VALUE rb_get_backtrace(VALUE exc)
Definition: error.c:1004
#define INT2FIX(i)
Definition: ruby.h:232
VALUE top_wrapper
Definition: vm_core.h:805
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
#define RCLASS_SUPER(c)
Definition: classext.h:16
VALUE rb_module_new(void)
Definition: class.c:749
void * ruby_process_options(int, char **)
Definition: ruby.c:2236
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1120
void rb_interrupt(void)
Raises an Interrupt exception.
Definition: eval.c:644
#define ANYARGS
Definition: defines.h:173
VALUE marker
Definition: vm_core.h:726
VALUE rb_eRuntimeError
Definition: error.c:800
void ruby_sig_finalize(void)
Definition: signal.c:1438
ID rb_frame_this_func(void)
The original name of the current method.
Definition: eval.c:1103
VALUE rb_eFatal
Definition: error.c:798
VALUE rb_eSystemExit
Definition: error.c:795
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition: eval.c:1596
VALUE rb_check_string_type(VALUE)
Definition: string.c:2246
ID ruby_static_id_status
Definition: eval.c:27
#define RTEST(v)
Definition: ruby.h:450
void rb_warning(const char *fmt,...)
Definition: error.c:267
#define OBJ_FROZEN(x)
Definition: ruby.h:1304
VALUE rb_f_global_variables(void)
Definition: variable.c:884
VALUE rb_errinfo(void)
The current exception in the current thread.
Definition: eval.c:1777
#define T_CLASS
Definition: ruby.h:492
int rb_iterator_p(void)
Determines if the current method is an interator.
Definition: eval.c:852
rb_execution_context_t ec
Definition: vm_core.h:790
#define ruby_debug
Definition: ruby.h:1814
#define ID2SYM(x)
Definition: ruby.h:383
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition: eval.c:631
void rb_vm_rewind_cfp(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:544
#define CONST_ID(var, str)
Definition: ruby.h:1743
void rb_gc_call_finalizer_at_exit(void)
Definition: gc.c:2908
#define rb_intern_const(str)
Definition: ruby.h:1777
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1242
#define RHASH_EMPTY_P(h)
Definition: ruby.h:1060
#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp)
Definition: vm_core.h:1242
#define rb_intern(str)
#define mod(x, y)
Definition: date_strftime.c:28
#define TAG_RAISE
Definition: vm_core.h:170
void rb_clear_trace_func(void)
Definition: vm_trace.c:206
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
VALUE rb_exc_new_cstr(VALUE etype, const char *s)
Definition: error.c:842
void ruby_default_signal(int)
Definition: signal.c:360
VALUE rb_search_class_path(VALUE)
Definition: variable.c:337
void rb_threadptr_interrupt(rb_thread_t *th)
Definition: thread.c:433
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
#define unknown_longjmp_status(status)
Definition: eval_error.c:265
rb_cref_t * rb_vm_cref(void)
Definition: vm.c:1321
VALUE rb_eThreadError
Definition: eval.c:857
NORETURN(void rb_raise_jump(VALUE, VALUE))
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition: eval.c:274
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1224
int ruby_setup(void)
Initializes the Ruby VM and builtin libraries.
Definition: eval.c:47
char ** argv
Definition: ruby.c:188
#define PASS_PASSED_BLOCK_HANDLER()
Definition: eval_intern.h:24
void Init_eval_method(void)
Definition: vm_method.c:2086
#define GET_VM()
Definition: vm_core.h:1582