Ruby  2.1.4p265(2014-10-27revision48166)
eval.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  eval.c -
4 
5  $Author: nagachika $
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 #include "probes_helper.h"
22 
24 
25 NODE *rb_vm_get_cref(const rb_iseq_t *, const VALUE *);
26 
29 
30 #define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
31 
32 #include "eval_error.c"
33 #include "eval_jump.c"
34 
35 #define CLASS_OR_MODULE_P(obj) \
36  (!SPECIAL_CONST_P(obj) && \
37  (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
38 
39 /* Initializes the Ruby VM and builtin libraries.
40  * @retval 0 if succeeded.
41  * @retval non-zero an error occurred.
42  */
43 int
45 {
46  static int initialized = 0;
47  int state;
48 
49  if (initialized)
50  return 0;
51  initialized = 1;
52 
53  ruby_init_stack((void *)&state);
54  Init_BareVM();
55  Init_heap();
56 
57  PUSH_TAG();
58  if ((state = EXEC_TAG()) == 0) {
59  rb_call_inits();
61  GET_VM()->running = 1;
62  }
63  POP_TAG();
64 
65  return state;
66 }
67 
68 /* Calls ruby_setup() and check error.
69  *
70  * Prints errors and calls exit(3) if an error occurred.
71  */
72 void
73 ruby_init(void)
74 {
75  int state = ruby_setup();
76  if (state) {
77  error_print();
78  exit(EXIT_FAILURE);
79  }
80 }
81 
92 void *
93 ruby_options(int argc, char **argv)
94 {
95  int state;
96  void *volatile iseq = 0;
97 
98  ruby_init_stack((void *)&iseq);
99  PUSH_TAG();
100  if ((state = EXEC_TAG()) == 0) {
101  SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv));
102  }
103  else {
105  state = error_handle(state);
106  iseq = (void *)INT2FIX(state);
107  }
108  POP_TAG();
109  return iseq;
110 }
111 
112 static void
114 {
115  PUSH_TAG();
116  if (EXEC_TAG() == 0) {
117  rb_trap_exit();
118  }
119  POP_TAG();
122 }
123 
124 static void
126 {
128  GET_THREAD()->errinfo = Qnil;
130 }
131 
139 void
141 {
142  ruby_finalize_0();
143  ruby_finalize_1();
144 }
145 
156 int
157 ruby_cleanup(volatile int ex)
158 {
159  int state;
160  volatile VALUE errs[2];
161  rb_thread_t *th = GET_THREAD();
162  int nerr;
163 
166  PUSH_TAG();
167  if ((state = EXEC_TAG()) == 0) {
168  SAVE_ROOT_JMPBUF(th, { RUBY_VM_CHECK_INTS(th); });
169  }
170  POP_TAG();
171 
172  errs[1] = th->errinfo;
173  th->safe_level = 0;
174  ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
175 
176  PUSH_TAG();
177  if ((state = EXEC_TAG()) == 0) {
179  }
180  POP_TAG();
181 
182  /* protect from Thread#raise */
183  th->status = THREAD_KILLED;
184 
185  errs[0] = th->errinfo;
186  PUSH_TAG();
187  if ((state = EXEC_TAG()) == 0) {
189  }
190  else if (ex == 0) {
191  ex = state;
192  }
193  th->errinfo = errs[1];
194  ex = error_handle(ex);
195 
196 #if EXIT_SUCCESS != 0 || EXIT_FAILURE != 1
197  switch (ex) {
198 #if EXIT_SUCCESS != 0
199  case 0: ex = EXIT_SUCCESS; break;
200 #endif
201 #if EXIT_FAILURE != 1
202  case 1: ex = EXIT_FAILURE; break;
203 #endif
204  }
205 #endif
206 
207  state = 0;
208  for (nerr = 0; nerr < numberof(errs); ++nerr) {
209  VALUE err = errs[nerr];
210 
211  if (!RTEST(err)) continue;
212 
213  /* th->errinfo contains a NODE while break'ing */
214  if (RB_TYPE_P(err, T_NODE)) continue;
215 
216  if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
217  ex = sysexit_status(err);
218  break;
219  }
220  else if (rb_obj_is_kind_of(err, rb_eSignal)) {
221  VALUE sig = rb_iv_get(err, "signo");
222  state = NUM2INT(sig);
223  break;
224  }
225  else if (ex == EXIT_SUCCESS) {
226  ex = EXIT_FAILURE;
227  }
228  }
229 
230  ruby_finalize_1();
231 
232  /* unlock again if finalizer took mutexes. */
234  POP_TAG();
237  if (state) ruby_default_signal(state);
238 
239  return ex;
240 }
241 
242 static int
244 {
245  volatile int state;
246  VALUE iseq = (VALUE)n;
247  rb_thread_t *th = GET_THREAD();
248 
249  if (!n) return 0;
250 
251  PUSH_TAG();
252  if ((state = EXEC_TAG()) == 0) {
253  SAVE_ROOT_JMPBUF(th, {
254  th->base_block = 0;
255  rb_iseq_eval_main(iseq);
256  });
257  }
258  POP_TAG();
259  return state;
260 }
261 
263 void
264 ruby_stop(int ex)
265 {
266  exit(ruby_cleanup(ex));
267 }
268 
281 int
282 ruby_executable_node(void *n, int *status)
283 {
284  VALUE v = (VALUE)n;
285  int s;
286 
287  switch (v) {
288  case Qtrue: s = EXIT_SUCCESS; break;
289  case Qfalse: s = EXIT_FAILURE; break;
290  default:
291  if (!FIXNUM_P(v)) return TRUE;
292  s = FIX2INT(v);
293  }
294  if (status) *status = s;
295  return FALSE;
296 }
297 
302 int
304 {
305  int status;
306  if (!ruby_executable_node(n, &status)) {
307  ruby_cleanup(0);
308  return status;
309  }
310  return ruby_cleanup(ruby_exec_node(n));
311 }
312 
314 int
316 {
317  ruby_init_stack((void *)&n);
318  return ruby_exec_internal(n);
319 }
320 
321 /*
322  * call-seq:
323  * Module.nesting -> array
324  *
325  * Returns the list of +Modules+ nested at the point of call.
326  *
327  * module M1
328  * module M2
329  * $a = Module.nesting
330  * end
331  * end
332  * $a #=> [M1::M2, M1]
333  * $a[0].name #=> "M1::M2"
334  */
335 
336 static VALUE
338 {
339  VALUE ary = rb_ary_new();
340  const NODE *cref = rb_vm_cref();
341 
342  while (cref && cref->nd_next) {
343  VALUE klass = cref->nd_clss;
344  if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
345  !NIL_P(klass)) {
346  rb_ary_push(ary, klass);
347  }
348  cref = cref->nd_next;
349  }
350  return ary;
351 }
352 
353 /*
354  * call-seq:
355  * Module.constants -> array
356  * Module.constants(inherited) -> array
357  *
358  * In the first form, returns an array of the names of all
359  * constants accessible from the point of call.
360  * This list includes the names of all modules and classes
361  * defined in the global scope.
362  *
363  * Module.constants.first(4)
364  * # => [:ARGF, :ARGV, :ArgumentError, :Array]
365  *
366  * Module.constants.include?(:SEEK_SET) # => false
367  *
368  * class IO
369  * Module.constants.include?(:SEEK_SET) # => true
370  * end
371  *
372  * The second form calls the instance method +constants+.
373  */
374 
375 static VALUE
377 {
378  const NODE *cref = rb_vm_cref();
379  VALUE klass;
380  VALUE cbase = 0;
381  void *data = 0;
382 
383  if (argc > 0 || mod != rb_cModule) {
384  return rb_mod_constants(argc, argv, mod);
385  }
386 
387  while (cref) {
388  klass = cref->nd_clss;
389  if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
390  !NIL_P(klass)) {
391  data = rb_mod_const_at(cref->nd_clss, data);
392  if (!cbase) {
393  cbase = klass;
394  }
395  }
396  cref = cref->nd_next;
397  }
398 
399  if (cbase) {
400  data = rb_mod_const_of(cbase, data);
401  }
402  return rb_const_list(data);
403 }
404 
405 void
407 {
408  if (SPECIAL_CONST_P(klass)) {
409  noclass:
410  Check_Type(klass, T_CLASS);
411  }
412  if (OBJ_FROZEN(klass)) {
413  const char *desc;
414 
415  if (FL_TEST(klass, FL_SINGLETON))
416  desc = "object";
417  else {
418  switch (BUILTIN_TYPE(klass)) {
419  case T_MODULE:
420  case T_ICLASS:
421  desc = "module";
422  break;
423  case T_CLASS:
424  desc = "class";
425  break;
426  default:
427  goto noclass;
428  }
429  }
430  rb_error_frozen(desc);
431  }
432 }
433 
434 NORETURN(static void rb_longjmp(int, volatile VALUE));
435 static VALUE get_errinfo(void);
437 
438 static VALUE
440 {
441  ID id_cause;
442  CONST_ID(id_cause, "cause");
443 
444 #if SUPPORT_JOKE
445  if (NIL_P(cause)) {
446  ID id_true_cause;
447  CONST_ID(id_true_cause, "true_cause");
448 
449  cause = rb_attr_get(rb_eFatal, id_true_cause);
450  if (NIL_P(cause)) {
451  cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby");
452  rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */
453  OBJ_FREEZE(cause);
454  rb_ivar_set(rb_eFatal, id_true_cause, cause);
455  }
456  }
457 #endif
458  if (!NIL_P(cause) && cause != exc) {
459  rb_ivar_set(exc, id_cause, cause);
460  }
461  return exc;
462 }
463 
464 static void
465 setup_exception(rb_thread_t *th, int tag, volatile VALUE mesg)
466 {
467  VALUE at;
468  VALUE e;
469  const char *file;
470  volatile int line = 0;
471 
472  if (NIL_P(mesg)) {
473  mesg = th->errinfo;
475  }
476  if (NIL_P(mesg)) {
477  mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
478  }
480 
481  file = rb_sourcefile();
482  if (file) line = rb_sourceline();
483  if (file && !NIL_P(mesg)) {
484  if (mesg == sysstack_error) {
485  at = rb_enc_sprintf(rb_usascii_encoding(), "%s:%d", file, line);
486  at = rb_ary_new3(1, at);
487  rb_iv_set(mesg, "bt", at);
488  }
489  else {
490  at = get_backtrace(mesg);
491  if (NIL_P(at)) {
492  at = rb_vm_backtrace_object();
493  if (OBJ_FROZEN(mesg)) {
494  mesg = rb_obj_dup(mesg);
495  }
496  rb_iv_set(mesg, "bt_locations", at);
497  set_backtrace(mesg, at);
498  }
499  }
500  }
501 
502  if (!NIL_P(mesg)) {
503  th->errinfo = mesg;
504  }
505 
506  if (RTEST(ruby_debug) && !NIL_P(e = th->errinfo) &&
508  int status;
509 
510  mesg = e;
511  PUSH_TAG();
512  if ((status = EXEC_TAG()) == 0) {
513  th->errinfo = Qnil;
514  e = rb_obj_as_string(mesg);
515  th->errinfo = mesg;
516  if (file && line) {
517  warn_printf("Exception `%s' at %s:%d - %"PRIsVALUE"\n",
518  rb_obj_classname(th->errinfo), file, line, e);
519  }
520  else if (file) {
521  warn_printf("Exception `%s' at %s - %"PRIsVALUE"\n",
522  rb_obj_classname(th->errinfo), file, e);
523  }
524  else {
525  warn_printf("Exception `%s' - %"PRIsVALUE"\n",
526  rb_obj_classname(th->errinfo), e);
527  }
528  }
529  POP_TAG();
530  if (status == TAG_FATAL && th->errinfo == exception_error) {
531  th->errinfo = mesg;
532  }
533  else if (status) {
535  JUMP_TAG(status);
536  }
537  }
538 
539  if (rb_threadptr_set_raised(th)) {
540  th->errinfo = exception_error;
543  }
544 
545  if (tag != TAG_FATAL) {
548  rb_sourcefile(),
549  rb_sourceline());
550  }
551  EXEC_EVENT_HOOK(th, RUBY_EVENT_RAISE, th->cfp->self, 0, 0, mesg);
552  }
553 }
554 
555 static void
556 rb_longjmp(int tag, volatile VALUE mesg)
557 {
558  rb_thread_t *th = GET_THREAD();
559  setup_exception(th, tag, mesg);
561  JUMP_TAG(tag);
562 }
563 
564 static VALUE make_exception(int argc, VALUE *argv, int isstr);
565 
566 void
568 {
569  if (!NIL_P(mesg)) {
570  mesg = make_exception(1, &mesg, FALSE);
571  }
572  rb_longjmp(TAG_RAISE, mesg);
573 }
574 
575 void
577 {
578  if (!NIL_P(mesg)) {
579  mesg = make_exception(1, &mesg, FALSE);
580  }
581  rb_longjmp(TAG_FATAL, mesg);
582 }
583 
584 void
586 {
587  rb_raise(rb_eInterrupt, "%s", "");
588 }
589 
590 /*
591  * call-seq:
592  * raise
593  * raise(string)
594  * raise(exception [, string [, array]])
595  * fail
596  * fail(string)
597  * fail(exception [, string [, array]])
598  *
599  * With no arguments, raises the exception in <code>$!</code> or raises
600  * a <code>RuntimeError</code> if <code>$!</code> is +nil+.
601  * With a single +String+ argument, raises a
602  * +RuntimeError+ with the string as a message. Otherwise,
603  * the first parameter should be the name of an +Exception+
604  * class (or an object that returns an +Exception+ object when sent
605  * an +exception+ message). The optional second parameter sets the
606  * message associated with the exception, and the third parameter is an
607  * array of callback information. Exceptions are caught by the
608  * +rescue+ clause of <code>begin...end</code> blocks.
609  *
610  * raise "Failed to create socket"
611  * raise ArgumentError, "No parameters", caller
612  */
613 
614 static VALUE
616 {
617  VALUE err;
618  if (argc == 0) {
619  err = get_errinfo();
620  if (!NIL_P(err)) {
621  argc = 1;
622  argv = &err;
623  }
624  }
625  rb_raise_jump(rb_make_exception(argc, argv));
626 
627  UNREACHABLE;
628 }
629 
630 static VALUE
631 make_exception(int argc, VALUE *argv, int isstr)
632 {
633  VALUE mesg, exc;
634  ID exception;
635  int n;
636 
637  mesg = Qnil;
638  switch (argc) {
639  case 0:
640  break;
641  case 1:
642  exc = argv[0];
643  if (NIL_P(exc))
644  break;
645  if (isstr) {
646  mesg = rb_check_string_type(exc);
647  if (!NIL_P(mesg)) {
648  mesg = rb_exc_new3(rb_eRuntimeError, mesg);
649  break;
650  }
651  }
652  n = 0;
653  goto exception_call;
654 
655  case 2:
656  case 3:
657  exc = argv[0];
658  n = 1;
659  exception_call:
660  if (exc == sysstack_error) return exc;
661  CONST_ID(exception, "exception");
662  mesg = rb_check_funcall(exc, exception, n, argv+1);
663  if (mesg == Qundef) {
664  rb_raise(rb_eTypeError, "exception class/object expected");
665  }
666  break;
667  default:
668  rb_check_arity(argc, 0, 3);
669  break;
670  }
671  if (argc > 0) {
672  if (!rb_obj_is_kind_of(mesg, rb_eException))
673  rb_raise(rb_eTypeError, "exception object expected");
674  if (argc > 2)
675  set_backtrace(mesg, argv[2]);
676  }
677 
678  return mesg;
679 }
680 
681 VALUE
683 {
684  return make_exception(argc, argv, TRUE);
685 }
686 
687 void
689 {
690  rb_thread_t *th = GET_THREAD();
691  rb_control_frame_t *cfp = th->cfp;
692  VALUE klass = cfp->me->klass;
693  VALUE self = cfp->self;
694  ID mid = cfp->me->called_id;
695 
697  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, self, mid, klass, Qnil);
698 
699  setup_exception(th, TAG_RAISE, mesg);
700 
703 }
704 
705 void
706 rb_jump_tag(int tag)
707 {
708  JUMP_TAG(tag);
709 }
710 
711 int
713 {
714  rb_thread_t *th = GET_THREAD();
715 
717  return TRUE;
718  }
719  else {
720  return FALSE;
721  }
722 }
723 
724 int
726 {
727  return rb_block_given_p();
728 }
729 
731 
732 void
734 {
735  if (!rb_block_given_p()) {
736  rb_vm_localjump_error("no block given", Qnil, 0);
737  }
738 }
739 
740 VALUE
741 rb_rescue2(VALUE (* b_proc) (ANYARGS), VALUE data1,
742  VALUE (* r_proc) (ANYARGS), VALUE data2, ...)
743 {
744  int state;
745  rb_thread_t *th = GET_THREAD();
746  rb_control_frame_t *cfp = th->cfp;
747  volatile VALUE result = Qfalse;
748  volatile VALUE e_info = th->errinfo;
749  va_list args;
750 
751  TH_PUSH_TAG(th);
752  if ((state = TH_EXEC_TAG()) == 0) {
753  retry_entry:
754  result = (*b_proc) (data1);
755  }
756  else if (result) {
757  /* escape from r_proc */
758  if (state == TAG_RETRY) {
759  state = 0;
760  th->errinfo = Qnil;
761  result = Qfalse;
762  goto retry_entry;
763  }
764  }
765  else {
766  rb_vm_rewind_cfp(th, cfp);
767 
768  if (state == TAG_RAISE) {
769  int handle = FALSE;
770  VALUE eclass;
771 
772  va_init_list(args, data2);
773  while ((eclass = va_arg(args, VALUE)) != 0) {
774  if (rb_obj_is_kind_of(th->errinfo, eclass)) {
775  handle = TRUE;
776  break;
777  }
778  }
779  va_end(args);
780 
781  if (handle) {
782  result = Qnil;
783  state = 0;
784  if (r_proc) {
785  result = (*r_proc) (data2, th->errinfo);
786  }
787  th->errinfo = e_info;
788  }
789  }
790  }
791  TH_POP_TAG();
792  if (state)
793  JUMP_TAG(state);
794 
795  return result;
796 }
797 
798 VALUE
799 rb_rescue(VALUE (* b_proc)(ANYARGS), VALUE data1,
800  VALUE (* r_proc)(ANYARGS), VALUE data2)
801 {
802  return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
803  (VALUE)0);
804 }
805 
806 VALUE
807 rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state)
808 {
809  volatile VALUE result = Qnil;
810  volatile int status;
811  rb_thread_t *th = GET_THREAD();
812  rb_control_frame_t *cfp = th->cfp;
813  struct rb_vm_protect_tag protect_tag;
814  rb_jmpbuf_t org_jmpbuf;
815 
816  protect_tag.prev = th->protect_tag;
817 
818  TH_PUSH_TAG(th);
819  th->protect_tag = &protect_tag;
820  MEMCPY(&org_jmpbuf, &(th)->root_jmpbuf, rb_jmpbuf_t, 1);
821  if ((status = TH_EXEC_TAG()) == 0) {
822  SAVE_ROOT_JMPBUF(th, result = (*proc) (data));
823  }
824  else {
825  rb_vm_rewind_cfp(th, cfp);
826  }
827  MEMCPY(&(th)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);
828  th->protect_tag = protect_tag.prev;
829  TH_POP_TAG();
830 
831  if (state) {
832  *state = status;
833  }
834 
835  return result;
836 }
837 
838 VALUE
839 rb_ensure(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*e_proc)(ANYARGS), VALUE data2)
840 {
841  int state;
842  volatile VALUE result = Qnil;
843  volatile VALUE errinfo;
844  rb_thread_t *const th = GET_THREAD();
845  rb_ensure_list_t ensure_list;
846  ensure_list.entry.marker = 0;
847  ensure_list.entry.e_proc = e_proc;
848  ensure_list.entry.data2 = data2;
849  ensure_list.next = th->ensure_list;
850  th->ensure_list = &ensure_list;
851  PUSH_TAG();
852  if ((state = EXEC_TAG()) == 0) {
853  result = (*b_proc) (data1);
854  }
855  POP_TAG();
856  /* TODO: fix me */
857  /* retval = prot_tag ? prot_tag->retval : Qnil; */ /* save retval */
858  errinfo = th->errinfo;
859  th->ensure_list=ensure_list.next;
860  (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
861  th->errinfo = errinfo;
862  if (state)
863  JUMP_TAG(state);
864  return result;
865 }
866 
867 static const rb_method_entry_t *
869 {
870  rb_thread_t *th = GET_THREAD();
871  rb_control_frame_t *cfp_limit;
872 
873  cfp_limit = (rb_control_frame_t *)(th->stack + th->stack_size);
874  while (cfp_limit > cfp) {
875  if (cfp->iseq == iseq)
876  return cfp->me;
878  }
879  return 0;
880 }
881 
882 static ID
884 {
885  const rb_method_entry_t *me_local;
886  rb_iseq_t *iseq = cfp->iseq;
887  if (cfp->me) {
888  return cfp->me->def->original_id;
889  }
890  while (iseq) {
891  if (RUBY_VM_IFUNC_P(iseq)) {
892  NODE *ifunc = (NODE *)iseq;
893  if (ifunc->nd_aid) return ifunc->nd_aid;
894  return idIFUNC;
895  }
896  me_local = method_entry_of_iseq(cfp, iseq);
897  if (me_local) {
898  cfp->me = me_local;
899  return me_local->def->original_id;
900  }
901  if (iseq->defined_method_id) {
902  return iseq->defined_method_id;
903  }
904  if (iseq->local_iseq == iseq) {
905  break;
906  }
907  iseq = iseq->parent_iseq;
908  }
909  return 0;
910 }
911 
912 static ID
914 {
915  const rb_method_entry_t *me_local;
916  rb_iseq_t *iseq = cfp->iseq;
917  if (cfp->me) {
918  return cfp->me->called_id;
919  }
920  while (iseq) {
921  if (RUBY_VM_IFUNC_P(iseq)) {
922  NODE *ifunc = (NODE *)iseq;
923  if (ifunc->nd_aid) return ifunc->nd_aid;
924  return idIFUNC;
925  }
926  me_local = method_entry_of_iseq(cfp, iseq);
927  if (me_local) {
928  cfp->me = me_local;
929  return me_local->called_id;
930  }
931  if (iseq->defined_method_id) {
932  return iseq->defined_method_id;
933  }
934  if (iseq->local_iseq == iseq) {
935  break;
936  }
937  iseq = iseq->parent_iseq;
938  }
939  return 0;
940 }
941 
942 ID
944 {
945  return frame_func_id(GET_THREAD()->cfp);
946 }
947 
948 ID
950 {
951  return frame_called_id(GET_THREAD()->cfp);
952 }
953 
954 static rb_control_frame_t *
956 {
958  /* check if prev_cfp can be accessible */
959  if ((void *)(th->stack + th->stack_size) == (void *)(prev_cfp)) {
960  return 0;
961  }
962  return prev_cfp;
963 }
964 
965 static ID
967 {
969  if (!prev_cfp) return 0;
970  return frame_called_id(prev_cfp);
971 }
972 
973 static ID
975 {
977  if (!prev_cfp) return 0;
978  return frame_func_id(prev_cfp);
979 }
980 
981 /*
982  * call-seq:
983  * append_features(mod) -> mod
984  *
985  * When this module is included in another, Ruby calls
986  * <code>append_features</code> in this module, passing it the
987  * receiving module in _mod_. Ruby's default implementation is
988  * to add the constants, methods, and module variables of this module
989  * to _mod_ if this module has not already been added to
990  * _mod_ or one of its ancestors. See also <code>Module#include</code>.
991  */
992 
993 static VALUE
995 {
996  if (!CLASS_OR_MODULE_P(include)) {
997  Check_Type(include, T_CLASS);
998  }
999  rb_include_module(include, module);
1000 
1001  return module;
1002 }
1003 
1004 /*
1005  * call-seq:
1006  * include(module, ...) -> self
1007  *
1008  * Invokes <code>Module.append_features</code> on each parameter in reverse order.
1009  */
1010 
1011 static VALUE
1013 {
1014  int i;
1015  ID id_append_features, id_included;
1016 
1017  CONST_ID(id_append_features, "append_features");
1018  CONST_ID(id_included, "included");
1019 
1020  for (i = 0; i < argc; i++)
1021  Check_Type(argv[i], T_MODULE);
1022  while (argc--) {
1023  rb_funcall(argv[argc], id_append_features, 1, module);
1024  rb_funcall(argv[argc], id_included, 1, module);
1025  }
1026  return module;
1027 }
1028 
1029 /*
1030  * call-seq:
1031  * prepend_features(mod) -> mod
1032  *
1033  * When this module is prepended in another, Ruby calls
1034  * <code>prepend_features</code> in this module, passing it the
1035  * receiving module in _mod_. Ruby's default implementation is
1036  * to overlay the constants, methods, and module variables of this module
1037  * to _mod_ if this module has not already been added to
1038  * _mod_ or one of its ancestors. See also <code>Module#prepend</code>.
1039  */
1040 
1041 static VALUE
1043 {
1044  if (!CLASS_OR_MODULE_P(prepend)) {
1045  Check_Type(prepend, T_CLASS);
1046  }
1047  rb_prepend_module(prepend, module);
1048 
1049  return module;
1050 }
1051 
1052 /*
1053  * call-seq:
1054  * prepend(module, ...) -> self
1055  *
1056  * Invokes <code>Module.prepend_features</code> on each parameter in reverse order.
1057  */
1058 
1059 static VALUE
1061 {
1062  int i;
1063  ID id_prepend_features, id_prepended;
1064 
1065  CONST_ID(id_prepend_features, "prepend_features");
1066  CONST_ID(id_prepended, "prepended");
1067  for (i = 0; i < argc; i++)
1068  Check_Type(argv[i], T_MODULE);
1069  while (argc--) {
1070  rb_funcall(argv[argc], id_prepend_features, 1, module);
1071  rb_funcall(argv[argc], id_prepended, 1, module);
1072  }
1073  return module;
1074 }
1075 
1076 static VALUE
1078 {
1079  VALUE hash = rb_hash_new();
1080 
1081  rb_funcall(hash, rb_intern("compare_by_identity"), 0);
1082  RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1083  return hash;
1084 }
1085 
1086 void
1087 rb_using_refinement(NODE *cref, VALUE klass, VALUE module)
1088 {
1089  VALUE iclass, c, superclass = klass;
1090 
1091  Check_Type(klass, T_CLASS);
1092  Check_Type(module, T_MODULE);
1093  if (NIL_P(cref->nd_refinements)) {
1094  RB_OBJ_WRITE(cref, &cref->nd_refinements, hidden_identity_hash_new());
1095  }
1096  else {
1097  if (cref->flags & NODE_FL_CREF_OMOD_SHARED) {
1098  RB_OBJ_WRITE(cref, &cref->nd_refinements, rb_hash_dup(cref->nd_refinements));
1099  cref->flags &= ~NODE_FL_CREF_OMOD_SHARED;
1100  }
1101  if (!NIL_P(c = rb_hash_lookup(cref->nd_refinements, klass))) {
1102  superclass = c;
1103  while (c && RB_TYPE_P(c, T_ICLASS)) {
1104  if (RBASIC(c)->klass == module) {
1105  /* already used refinement */
1106  return;
1107  }
1108  c = RCLASS_SUPER(c);
1109  }
1110  }
1111  }
1112  FL_SET(module, RMODULE_IS_OVERLAID);
1113  c = iclass = rb_include_class_new(module, superclass);
1114  RCLASS_REFINED_CLASS(c) = klass;
1115 
1118 
1119  module = RCLASS_SUPER(module);
1120  while (module && module != klass) {
1121  FL_SET(module, RMODULE_IS_OVERLAID);
1123  RCLASS_REFINED_CLASS(c) = klass;
1124  module = RCLASS_SUPER(module);
1125  }
1126  rb_hash_aset(cref->nd_refinements, klass, iclass);
1127 }
1128 
1129 static int
1130 using_refinement(VALUE klass, VALUE module, VALUE arg)
1131 {
1132  NODE *cref = (NODE *) arg;
1133 
1134  rb_using_refinement(cref, klass, module);
1135  return ST_CONTINUE;
1136 }
1137 
1138 static void
1140 {
1141  ID id_refinements;
1142  VALUE super, module, refinements;
1143 
1144  super = RCLASS_SUPER(klass);
1145  if (super) {
1146  using_module_recursive(cref, super);
1147  }
1148  switch (BUILTIN_TYPE(klass)) {
1149  case T_MODULE:
1150  module = klass;
1151  break;
1152 
1153  case T_ICLASS:
1154  module = RBASIC(klass)->klass;
1155  break;
1156 
1157  default:
1158  rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1159  rb_obj_classname(klass));
1160  break;
1161  }
1162  CONST_ID(id_refinements, "__refinements__");
1163  refinements = rb_attr_get(module, id_refinements);
1164  if (NIL_P(refinements)) return;
1165  rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1166 }
1167 
1168 void
1170 {
1171  Check_Type(module, T_MODULE);
1172  using_module_recursive(cref, module);
1174 }
1175 
1176 VALUE
1178 {
1179  ID id_refined_class;
1180 
1181  CONST_ID(id_refined_class, "__refined_class__");
1182  return rb_attr_get(module, id_refined_class);
1183 }
1184 
1185 static void
1186 add_activated_refinement(VALUE activated_refinements,
1187  VALUE klass, VALUE refinement)
1188 {
1189  VALUE iclass, c, superclass = klass;
1190 
1191  if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1192  superclass = c;
1193  while (c && RB_TYPE_P(c, T_ICLASS)) {
1194  if (RBASIC(c)->klass == refinement) {
1195  /* already used refinement */
1196  return;
1197  }
1198  c = RCLASS_SUPER(c);
1199  }
1200  }
1201  FL_SET(refinement, RMODULE_IS_OVERLAID);
1202  c = iclass = rb_include_class_new(refinement, superclass);
1203  RCLASS_REFINED_CLASS(c) = klass;
1204  refinement = RCLASS_SUPER(refinement);
1205  while (refinement) {
1206  FL_SET(refinement, RMODULE_IS_OVERLAID);
1207  c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1208  RCLASS_REFINED_CLASS(c) = klass;
1209  refinement = RCLASS_SUPER(refinement);
1210  }
1211  rb_hash_aset(activated_refinements, klass, iclass);
1212 }
1213 
1214 VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements);
1215 
1216 /*
1217  * call-seq:
1218  * refine(klass) { block } -> module
1219  *
1220  * Refine <i>klass</i> in the receiver.
1221  *
1222  * Returns an overlaid module.
1223  */
1224 
1225 static VALUE
1226 rb_mod_refine(VALUE module, VALUE klass)
1227 {
1228  VALUE refinement;
1229  ID id_refinements, id_activated_refinements,
1230  id_refined_class, id_defined_at;
1231  VALUE refinements, activated_refinements;
1232  rb_thread_t *th = GET_THREAD();
1234 
1235  if (!block) {
1236  rb_raise(rb_eArgError, "no block given");
1237  }
1238  if (block->proc) {
1240  "can't pass a Proc as a block to Module#refine");
1241  }
1242  Check_Type(klass, T_CLASS);
1243  CONST_ID(id_refinements, "__refinements__");
1244  refinements = rb_attr_get(module, id_refinements);
1245  if (NIL_P(refinements)) {
1246  refinements = hidden_identity_hash_new();
1247  rb_ivar_set(module, id_refinements, refinements);
1248  }
1249  CONST_ID(id_activated_refinements, "__activated_refinements__");
1250  activated_refinements = rb_attr_get(module, id_activated_refinements);
1251  if (NIL_P(activated_refinements)) {
1252  activated_refinements = hidden_identity_hash_new();
1253  rb_ivar_set(module, id_activated_refinements,
1254  activated_refinements);
1255  }
1256  refinement = rb_hash_lookup(refinements, klass);
1257  if (NIL_P(refinement)) {
1258  refinement = rb_module_new();
1259  RCLASS_SET_SUPER(refinement, klass);
1260  FL_SET(refinement, RMODULE_IS_REFINEMENT);
1261  CONST_ID(id_refined_class, "__refined_class__");
1262  rb_ivar_set(refinement, id_refined_class, klass);
1263  CONST_ID(id_defined_at, "__defined_at__");
1264  rb_ivar_set(refinement, id_defined_at, module);
1265  rb_hash_aset(refinements, klass, refinement);
1266  add_activated_refinement(activated_refinements, klass, refinement);
1267  }
1268  rb_yield_refine_block(refinement, activated_refinements);
1269  return refinement;
1270 }
1271 
1272 /*
1273  * call-seq:
1274  * using(module) -> self
1275  *
1276  * Import class refinements from <i>module</i> into the current class or
1277  * module definition.
1278  */
1279 
1280 static VALUE
1281 mod_using(VALUE self, VALUE module)
1282 {
1283  NODE *cref = rb_vm_cref();
1285 
1286  if (prev_frame_func()) {
1288  "Module#using is not permitted in methods");
1289  }
1290  if (prev_cfp && prev_cfp->self != self) {
1291  rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1292  }
1293  rb_using_module(cref, module);
1294  return self;
1295 }
1296 
1297 void
1299 {
1301  rb_funcall2(obj, idInitialize, argc, argv);
1302 }
1303 
1304 void
1306 {
1307  rb_include_module(rb_singleton_class(obj), module);
1308 }
1309 
1310 /*
1311  * call-seq:
1312  * extend_object(obj) -> obj
1313  *
1314  * Extends the specified object by adding this module's constants and
1315  * methods (which are added as singleton methods). This is the callback
1316  * method used by <code>Object#extend</code>.
1317  *
1318  * module Picky
1319  * def Picky.extend_object(o)
1320  * if String === o
1321  * puts "Can't add Picky to a String"
1322  * else
1323  * puts "Picky added to #{o.class}"
1324  * super
1325  * end
1326  * end
1327  * end
1328  * (s = Array.new).extend Picky # Call Object.extend
1329  * (s = "quick brown fox").extend Picky
1330  *
1331  * <em>produces:</em>
1332  *
1333  * Picky added to Array
1334  * Can't add Picky to a String
1335  */
1336 
1337 static VALUE
1339 {
1340  rb_extend_object(obj, mod);
1341  return obj;
1342 }
1343 
1344 /*
1345  * call-seq:
1346  * obj.extend(module, ...) -> obj
1347  *
1348  * Adds to _obj_ the instance methods from each module given as a
1349  * parameter.
1350  *
1351  * module Mod
1352  * def hello
1353  * "Hello from Mod.\n"
1354  * end
1355  * end
1356  *
1357  * class Klass
1358  * def hello
1359  * "Hello from Klass.\n"
1360  * end
1361  * end
1362  *
1363  * k = Klass.new
1364  * k.hello #=> "Hello from Klass.\n"
1365  * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1366  * k.hello #=> "Hello from Mod.\n"
1367  */
1368 
1369 static VALUE
1371 {
1372  int i;
1373  ID id_extend_object, id_extended;
1374 
1375  CONST_ID(id_extend_object, "extend_object");
1376  CONST_ID(id_extended, "extended");
1377 
1379  for (i = 0; i < argc; i++)
1380  Check_Type(argv[i], T_MODULE);
1381  while (argc--) {
1382  rb_funcall(argv[argc], id_extend_object, 1, obj);
1383  rb_funcall(argv[argc], id_extended, 1, obj);
1384  }
1385  return obj;
1386 }
1387 
1388 /*
1389  * call-seq:
1390  * include(module, ...) -> self
1391  *
1392  * Invokes <code>Module.append_features</code>
1393  * on each parameter in turn. Effectively adds the methods and constants
1394  * in each module to the receiver.
1395  */
1396 
1397 static VALUE
1399 {
1400  rb_thread_t *th = GET_THREAD();
1401 
1402  if (th->top_wrapper) {
1403  rb_warning("main.include in the wrapped load is effective only in wrapper module");
1404  return rb_mod_include(argc, argv, th->top_wrapper);
1405  }
1406  return rb_mod_include(argc, argv, rb_cObject);
1407 }
1408 
1409 /*
1410  * call-seq:
1411  * using(module) -> self
1412  *
1413  * Import class refinements from <i>module</i> into the scope where
1414  * <code>using</code> is called.
1415  */
1416 
1417 static VALUE
1418 top_using(VALUE self, VALUE module)
1419 {
1420  NODE *cref = rb_vm_cref();
1422 
1423  if (cref->nd_next || (prev_cfp && prev_cfp->me)) {
1425  "main.using is permitted only at toplevel");
1426  }
1427  rb_using_module(cref, module);
1428  return self;
1429 }
1430 
1431 static VALUE *
1433 {
1434  rb_control_frame_t *cfp = th->cfp;
1436 
1437  while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1438  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
1439  if (cfp->iseq->type == ISEQ_TYPE_RESCUE) {
1440  return &cfp->ep[-2];
1441  }
1442  else if (cfp->iseq->type == ISEQ_TYPE_ENSURE &&
1443  !RB_TYPE_P(cfp->ep[-2], T_NODE) &&
1444  !FIXNUM_P(cfp->ep[-2])) {
1445  return &cfp->ep[-2];
1446  }
1447  }
1448  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1449  }
1450  return 0;
1451 }
1452 
1453 static VALUE
1455 {
1456  VALUE *ptr = errinfo_place(th);
1457  if (ptr) {
1458  return *ptr;
1459  }
1460  else {
1461  return th->errinfo;
1462  }
1463 }
1464 
1465 static VALUE
1467 {
1468  return get_thread_errinfo(GET_THREAD());
1469 }
1470 
1471 static VALUE
1473 {
1474  return get_errinfo();
1475 }
1476 
1477 #if 0
1478 static void
1479 errinfo_setter(VALUE val, ID id, VALUE *var)
1480 {
1481  if (!NIL_P(val) && !rb_obj_is_kind_of(val, rb_eException)) {
1482  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1483  }
1484  else {
1485  VALUE *ptr = errinfo_place(GET_THREAD());
1486  if (ptr) {
1487  *ptr = val;
1488  }
1489  else {
1490  rb_raise(rb_eRuntimeError, "errinfo_setter: not in rescue clause.");
1491  }
1492  }
1493 }
1494 #endif
1495 
1496 VALUE
1498 {
1499  rb_thread_t *th = GET_THREAD();
1500  return th->errinfo;
1501 }
1502 
1503 void
1505 {
1506  if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1507  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1508  }
1509  GET_THREAD()->errinfo = err;
1510 }
1511 
1512 VALUE
1514 {
1515  return get_errinfo();
1516 }
1517 
1518 static VALUE
1520 {
1521  VALUE err = get_errinfo();
1522  if (!NIL_P(err)) {
1523  return get_backtrace(err);
1524  }
1525  else {
1526  return Qnil;
1527  }
1528 }
1529 
1530 static void
1531 errat_setter(VALUE val, ID id, VALUE *var)
1532 {
1533  VALUE err = get_errinfo();
1534  if (NIL_P(err)) {
1535  rb_raise(rb_eArgError, "$! not set");
1536  }
1537  set_backtrace(err, val);
1538 }
1539 
1540 /*
1541  * call-seq:
1542  * __method__ -> symbol
1543  *
1544  * Returns the name at the definition of the current method as a
1545  * Symbol.
1546  * If called outside of a method, it returns <code>nil</code>.
1547  *
1548  */
1549 
1550 static VALUE
1552 {
1553  ID fname = prev_frame_func(); /* need *method* ID */
1554 
1555  if (fname) {
1556  return ID2SYM(fname);
1557  }
1558  else {
1559  return Qnil;
1560  }
1561 }
1562 
1563 /*
1564  * call-seq:
1565  * __callee__ -> symbol
1566  *
1567  * Returns the called name of the current method as a Symbol.
1568  * If called outside of a method, it returns <code>nil</code>.
1569  *
1570  */
1571 
1572 static VALUE
1574 {
1575  ID fname = prev_frame_callee(); /* need *callee* ID */
1576 
1577  if (fname) {
1578  return ID2SYM(fname);
1579  }
1580  else {
1581  return Qnil;
1582  }
1583 }
1584 
1585 /*
1586  * call-seq:
1587  * __dir__ -> string
1588  *
1589  * Returns the canonicalized absolute path of the directory of the file from
1590  * which this method is called. It means symlinks in the path is resolved.
1591  * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1592  * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1593  *
1594  */
1595 static VALUE
1597 {
1598  VALUE base = rb_current_realfilepath();
1599  if (NIL_P(base)) {
1600  return Qnil;
1601  }
1602  base = rb_file_dirname(base);
1603  return base;
1604 }
1605 
1606 void
1608 {
1611 
1612  rb_define_global_function("raise", rb_f_raise, -1);
1614 
1615  rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */
1616 
1617  rb_define_global_function("__method__", rb_f_method_name, 0);
1618  rb_define_global_function("__callee__", rb_f_callee_name, 0);
1620 
1621  rb_define_method(rb_cModule, "include", rb_mod_include, -1);
1622  rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
1623 
1629  rb_undef_method(rb_cClass, "refine");
1630 
1631  rb_undef_method(rb_cClass, "module_function");
1632 
1633  Init_vm_eval();
1634  Init_eval_method();
1635 
1638 
1640  "include", top_include, -1);
1642  "using", top_using, 1);
1643 
1644  rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
1645 
1646  rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */
1647  rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */
1648 
1650  rb_obj_freeze(rb_str_new2("exception reentered")));
1653 }
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:607
static void ruby_finalize_0(void)
Definition: eval.c:113
int ruby_run_node(void *n)
Runs the given compiled source and exits this process.
Definition: eval.c:303
rb_control_frame_t * cfp
Definition: vm_core.h:531
void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
Definition: thread.c:404
VALUE rb_eStandardError
Definition: error.c:546
VALUE rb_eLocalJumpError
Definition: eval.c:27
#define RUBY_VM_CHECK_INTS(th)
Definition: vm_core.h:989
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Definition: error.c:573
int ruby_cleanup(volatile int ex)
Destructs the VM.
Definition: eval.c:157
struct rb_ensure_entry entry
Definition: vm_core.h:521
void rb_raise_jump(VALUE mesg)
Definition: eval.c:688
static const rb_method_entry_t * method_entry_of_iseq(rb_control_frame_t *cfp, rb_iseq_t *iseq)
Definition: eval.c:868
VALUE rb_vm_backtrace_object(void)
Definition: vm_backtrace.c:536
void rb_call_inits(void)
Definition: inits.c:18
void rb_interrupt(void)
Definition: eval.c:585
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:1713
#define FALSE
Definition: nkf.h:174
static VALUE rb_f_raise(int argc, VALUE *argv)
Definition: eval.c:615
#define rb_hash_lookup
Definition: tcltklib.c:269
struct rb_vm_protect_tag * protect_tag
Definition: vm_core.h:594
#define RUBY_VM_IFUNC_P(ptr)
Definition: vm_core.h:834
VALUE rb_make_exception(int argc, VALUE *argv)
Definition: eval.c:682
#define INTERNAL_EXCEPTION_P(exc)
Definition: eval_intern.h:175
VALUE rb_eSignal
Definition: error.c:544
void ruby_finalize(void)
Runs the VM finalization processes.
Definition: eval.c:140
void rb_define_virtual_variable(const char *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:616
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:320
#define NUM2INT(x)
Definition: ruby.h:630
VALUE rb_errinfo(void)
Definition: eval.c:1497
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1655
static VALUE exc_setup_cause(VALUE exc, VALUE cause)
Definition: eval.c:439
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:825
void Init_BareVM(void)
Definition: vm.c:2792
#define RUBY_VM_NORMAL_ISEQ_P(ptr)
Definition: vm_core.h:835
#define T_MODULE
Definition: ruby.h:480
#define RUBY_EVENT_RAISE
Definition: ruby.h:1714
void * ruby_options(int argc, char **argv)
Processes command line arguments and compiles the Ruby source to execute.
Definition: eval.c:93
#define Qtrue
Definition: ruby.h:426
VALUE rb_current_realfilepath(void)
Definition: vm_eval.c:1957
void rb_error_frozen(const char *what)
Definition: error.c:2077
void rb_exec_end_proc(void)
Definition: eval_jump.c:112
#define RUBY_DTRACE_RAISE(arg0, arg1, arg2)
Definition: probes.h:37
void rb_using_refinement(NODE *cref, VALUE klass, VALUE module)
Definition: eval.c:1087
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:35
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1177
static void set_backtrace(VALUE info, VALUE bt)
Definition: eval_error.c:63
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1500
VALUE data2
Definition: vm_core.h:516
ID rb_frame_this_func(void)
Definition: eval.c:943
#define sysstack_error
Definition: vm_core.h:899
VALUE rb_eTypeError
Definition: error.c:548
static int sysexit_status(VALUE err)
Definition: eval_error.c:238
static VALUE rb_mod_include(int argc, VALUE *argv, VALUE module)
Definition: eval.c:1012
#define rb_check_arity
Definition: intern.h:296
int ruby_exec_node(void *n)
Runs the given compiled source.
Definition: eval.c:315
#define UNREACHABLE
Definition: ruby.h:42
static ID frame_func_id(rb_control_frame_t *cfp)
Definition: eval.c:883
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
static void add_activated_refinement(VALUE activated_refinements, VALUE klass, VALUE refinement)
Definition: eval.c:1186
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:775
#define STACK_UPPER(x, a, b)
Definition: gc.h:74
VALUE rb_iv_set(VALUE, const char *, VALUE)
Definition: variable.c:2609
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:807
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:2601
struct rb_iseq_struct * local_iseq
Definition: vm_core.h:297
#define Check_Type(v, t)
Definition: ruby.h:532
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
struct rb_vm_protect_tag * prev
Definition: vm_core.h:496
void Init_vm_eval(void)
Definition: vm_eval.c:1967
NODE * rb_vm_cref(void)
Definition: vm.c:1015
ID called_id
Definition: method.h:101
#define TH_EXEC_TAG()
Definition: eval_intern.h:165
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:652
void Init_heap(void)
Definition: gc.c:1661
#define RCLASS_M_TBL_WRAPPER(c)
Definition: internal.h:294
static VALUE mod_using(VALUE self, VALUE module)
Definition: eval.c:1281
void rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:1094
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:827
ID defined_method_id
Definition: vm_core.h:319
static VALUE errinfo_getter(ID id)
Definition: eval.c:1472
#define TAG_RAISE
Definition: eval_intern.h:193
#define PUSH_TAG()
Definition: eval_intern.h:141
static VALUE rb_obj_extend(int argc, VALUE *argv, VALUE obj)
Definition: eval.c:1370
void Init_eval(void)
Definition: eval.c:1607
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1684
#define FIXNUM_P(f)
Definition: ruby.h:347
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1506
VALUE rb_rubylevel_errinfo(void)
Definition: eval.c:1513
VALUE rb_iseq_eval_main(VALUE iseqval)
Definition: vm.c:1655
void rb_thread_terminate_all(void)
Definition: thread.c:421
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
Definition: sprintf.c:1231
#define TAG_FATAL
Definition: eval_intern.h:195
Definition: node.h:239
int rb_iterator_p(void)
Definition: eval.c:725
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:264
void rb_exc_raise(VALUE mesg)
Definition: eval.c:567
#define FL_SINGLETON
Definition: ruby.h:1133
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:940
NORETURN(void rb_raise_jump(VALUE))
VALUE * stack
Definition: vm_core.h:529
VALUE rb_obj_dup(VALUE)
Definition: object.c:406
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1628
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
enum rb_iseq_struct::iseq_type type
#define TH_POP_TAG()
Definition: eval_intern.h:128
static VALUE rb_f_method_name(void)
Definition: eval.c:1551
#define FL_TEST(x, f)
Definition: ruby.h:1169
static VALUE rb_mod_prepend_features(VALUE module, VALUE prepend)
Definition: eval.c:1042
VALUE rb_rescue(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2)
Definition: eval.c:799
static ID prev_frame_callee(void)
Definition: eval.c:966
static VALUE get_backtrace(VALUE info)
Definition: eval_error.c:44
static VALUE f_current_dirname(void)
Definition: eval.c:1596
static ID frame_called_id(rb_control_frame_t *cfp)
Definition: eval.c:913
static int error_handle(int ex)
Definition: eval_error.c:245
int rb_block_given_p(void)
Definition: eval.c:712
void * rb_mod_const_at(VALUE, void *)
Definition: variable.c:2004
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
#define EXEC_TAG()
Definition: eval_intern.h:168
int rb_threadptr_set_raised(rb_thread_t *th)
Definition: thread.c:2096
void ruby_init(void)
Definition: eval.c:73
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
VALUE rb_eRuntimeError
Definition: error.c:547
#define RMODULE_IS_REFINEMENT
Definition: ruby.h:802
VALUE rb_eSysStackError
Definition: eval.c:28
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1011
VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements)
Definition: vm_eval.c:1527
VALUE rb_ary_new(void)
Definition: array.c:495
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1191
#define exception_error
Definition: eval.c:30
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1541
#define JUMP_TAG(st)
Definition: eval_intern.h:173
rb_iseq_t * iseq
Definition: vm_core.h:448
#define NIL_P(v)
Definition: ruby.h:438
#define RMODULE_IS_OVERLAID
Definition: ruby.h:801
static VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super)
Definition: internal.h:319
void rb_thread_stop_timer_thread(int close_anyway)
Definition: thread.c:3856
#define PASS_PASSED_BLOCK()
Definition: eval_intern.h:12
static VALUE rb_mod_prepend(int argc, VALUE *argv, VALUE module)
Definition: eval.c:1060
static VALUE rb_mod_extend_object(VALUE mod, VALUE obj)
Definition: eval.c:1338
#define OBJ_FROZEN(x)
Definition: ruby.h:1185
void rb_threadptr_check_signal(rb_thread_t *mth)
Definition: thread.c:3815
int argc
Definition: ruby.c:131
#define Qfalse
Definition: ruby.h:425
#define rb_sourcefile()
Definition: tcltklib.c:98
Definition: method.h:97
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1572
rb_block_t * base_block
Definition: vm_core.h:555
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1352
#define T_NODE
Definition: ruby.h:498
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition: eval.c:264
#define rb_str_new2
Definition: intern.h:840
int err
Definition: win32.c:114
#define OBJ_FREEZE(x)
Definition: ruby.h:1186
#define EXIT_FAILURE
Definition: eval_intern.h:24
VALUE rb_mod_constants(int, VALUE *, VALUE)
Definition: variable.c:2068
#define POP_TAG()
Definition: eval_intern.h:142
static VALUE hidden_identity_hash_new()
Definition: eval.c:1077
VALUE rb_vm_top_self()
Definition: vm.c:2826
VALUE klass
Definition: method.h:102
void rb_trap_exit(void)
Definition: signal.c:849
#define numberof(array)
Definition: etc.c:595
#define rb_thread_raised_clear(th)
Definition: eval_intern.h:226
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:298
void rb_need_block(void)
Definition: eval.c:733
#define TRUE
Definition: nkf.h:175
#define EXIT_SUCCESS
Definition: error.c:29
static VALUE rb_mod_nesting(void)
Definition: eval.c:337
Definition: id.h:99
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:792
static ID prev_frame_func(void)
Definition: eval.c:974
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:1913
VALUE rb_file_dirname(VALUE fname)
Definition: file.c:3910
VALUE rb_hash_new(void)
Definition: hash.c:298
static void ruby_finalize_1(void)
Definition: eval.c:125
void * rb_mod_const_of(VALUE, void *)
Definition: variable.c:2017
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1133
struct rb_iseq_struct * parent_iseq
Definition: vm_core.h:296
#define PRIsVALUE
Definition: ruby.h:137
VALUE rb_eInterrupt
Definition: error.c:543
unsigned long ID
Definition: ruby.h:89
rb_encoding * rb_usascii_encoding(void)
Definition: encoding.c:1257
#define Qnil
Definition: ruby.h:427
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:66
VALUE rb_const_list(void *)
Definition: variable.c:2039
#define BUILTIN_TYPE(x)
Definition: ruby.h:502
#define OBJ_TAINT(x)
Definition: ruby.h:1177
unsigned long VALUE
Definition: ruby.h:88
#define SAVE_ROOT_JMPBUF(th, stmt)
Definition: eval_intern.h:112
#define rb_funcall2
Definition: ruby.h:1456
static VALUE result
Definition: nkf.c:40
RUBY_JMP_BUF rb_jmpbuf_t
Definition: vm_core.h:482
int ruby_vm_destruct(ruby_vm_t *vm)
#define RBASIC(obj)
Definition: ruby.h:1116
static void setup_exception(rb_thread_t *th, int tag, volatile VALUE mesg)
Definition: eval.c:465
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:1305
int rb_threadptr_reset_raised(rb_thread_t *th)
Definition: thread.c:2106
#define FIX2INT(x)
Definition: ruby.h:632
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
Definition: eval.c:741
#define rb_ary_new3
Definition: intern.h:91
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:122
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:409
void ruby_init_stack(volatile VALUE *)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:839
static VALUE get_errinfo(void)
Definition: eval.c:1466
struct rb_ensure_list * next
Definition: vm_core.h:520
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1557
VALUE flags
Definition: node.h:240
void rb_jump_tag(int tag)
Definition: eval.c:706
#define RUBY_VM_END_CONTROL_FRAME(th)
Definition: vm_core.h:827
void rb_using_module(NODE *cref, VALUE module)
Definition: eval.c:1169
static void using_module_recursive(NODE *cref, VALUE klass)
Definition: eval.c:1139
static int ruby_exec_internal(void *n)
Definition: eval.c:243
enum rb_thread_status status
Definition: vm_core.h:562
#define RUBY_DTRACE_RAISE_ENABLED()
Definition: probes.h:36
#define va_init_list(a, b)
Definition: tcltklib.c:62
static void warn_printf(const char *fmt,...)
Definition: eval_error.c:7
#define rb_exc_new3
Definition: intern.h:248
static VALUE top_include(int argc, VALUE *argv, VALUE self)
Definition: eval.c:1398
static rb_control_frame_t * previous_frame(rb_thread_t *th)
Definition: eval.c:955
VALUE rb_exc_new_cstr(VALUE etype, const char *s)
Definition: error.c:579
static VALUE top_using(VALUE self, VALUE module)
Definition: eval.c:1418
#define INT2FIX(i)
Definition: ruby.h:231
VALUE top_wrapper
Definition: vm_core.h:552
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
#define RCLASS_SUPER(c)
Definition: classext.h:16
int rb_sourceline(void)
Definition: vm.c:1001
VALUE rb_module_new(void)
Definition: class.c:727
void * ruby_process_options(int, char **)
Definition: ruby.c:1960
ID rb_frame_callee(void)
Definition: eval.c:949
rb_method_definition_t * def
Definition: method.h:100
void rb_set_errinfo(VALUE err)
Definition: eval.c:1504
#define ANYARGS
Definition: defines.h:98
const rb_method_entry_t * me
Definition: vm_core.h:455
VALUE marker
Definition: vm_core.h:514
static VALUE * errinfo_place(rb_thread_t *th)
Definition: eval.c:1432
void ruby_sig_finalize(void)
Definition: signal.c:1232
VALUE rb_check_string_type(VALUE)
Definition: string.c:1679
#define RTEST(v)
Definition: ruby.h:437
rb_block_t * rb_vm_control_frame_block_ptr(rb_control_frame_t *cfp)
Definition: vm.c:59
VALUE rb_f_global_variables(void)
Definition: variable.c:853
#define NODE_FL_CREF_OMOD_SHARED
Definition: node.h:277
rb_ensure_list_t * ensure_list
Definition: vm_core.h:646
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
#define T_CLASS
Definition: ruby.h:478
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_)
Definition: vm_core.h:1034
void rb_frozen_class_p(VALUE klass)
Definition: eval.c:406
#define ruby_debug
Definition: ruby.h:1476
NODE * rb_vm_get_cref(const rb_iseq_t *, const VALUE *)
#define FL_SET(x, f)
Definition: ruby.h:1172
#define ID2SYM(x)
Definition: ruby.h:355
VALUE rb_eFatal
Definition: error.c:545
VALUE(* e_proc)(ANYARGS)
Definition: vm_core.h:515
size_t stack_size
Definition: vm_core.h:530
void * data
Definition: error.c:276
static VALUE rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
Definition: eval.c:376
void rb_vm_rewind_cfp(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:291
void rb_warning(const char *fmt,...)
Definition: error.c:236
VALUE rb_f_untrace_var(int, VALUE *)
Definition: variable.c:715
#define CONST_ID(var, str)
Definition: ruby.h:1428
static void rb_longjmp(int tag, volatile VALUE mesg)
Definition: eval.c:556
void rb_gc_call_finalizer_at_exit(void)
Definition: gc.c:2144
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1076
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1165
static void error_print(void)
Definition: eval_error.c:80
static VALUE get_thread_errinfo(rb_thread_t *th)
Definition: eval.c:1454
static int using_refinement(VALUE klass, VALUE module, VALUE arg)
Definition: eval.c:1130
#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp)
Definition: vm_core.h:829
static VALUE rb_mod_append_features(VALUE module, VALUE include)
Definition: eval.c:994
#define rb_intern(str)
#define mod(x, y)
Definition: date_strftime.c:28
VALUE rb_f_trace_var(int, VALUE *)
Definition: variable.c:656
static VALUE errat_getter(ID id)
Definition: eval.c:1519
void rb_clear_trace_func(void)
Definition: vm_trace.c:225
void rb_exc_fatal(VALUE mesg)
Definition: eval.c:576
VALUE rb_eSystemExit
Definition: error.c:542
#define Qundef
Definition: ruby.h:428
#define T_ICLASS
Definition: ruby.h:479
void ruby_default_signal(int)
Definition: signal.c:340
void rb_threadptr_interrupt(rb_thread_t *th)
Definition: thread.c:359
void rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
Definition: eval.c:1298
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:927
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
VALUE rb_eThreadError
Definition: eval.c:730
VALUE rb_eArgError
Definition: error.c:549
static void errat_setter(VALUE val, ID id, VALUE *var)
Definition: eval.c:1531
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition: eval.c:282
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1213
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1127
int ruby_setup(void)
Definition: eval.c:44
char ** argv
Definition: ruby.c:132
#define TAG_RETRY
Definition: eval_intern.h:191
static VALUE make_exception(int argc, VALUE *argv, int isstr)
Definition: eval.c:631
static VALUE rb_f_callee_name(void)
Definition: eval.c:1573
VALUE rb_eException
Definition: error.c:541
void Init_eval_method(void)
Definition: vm_method.c:1708
static VALUE rb_mod_refine(VALUE module, VALUE klass)
Definition: eval.c:1226
#define NODE_FL_CREF_PUSHED_BY_EVAL
Definition: node.h:276
#define GET_VM()
Definition: vm_core.h:920