Ruby  3.1.4p223 (2023-03-30 revision HEAD)
eval.c
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 "ruby/internal/config.h"
15 
16 #ifdef HAVE_SYS_PRCTL_H
17 #include <sys/prctl.h>
18 #endif
19 
20 #include "eval_intern.h"
21 #include "gc.h"
22 #include "internal.h"
23 #include "internal/class.h"
24 #include "internal/error.h"
25 #include "internal/eval.h"
26 #include "internal/hash.h"
27 #include "internal/inits.h"
28 #include "internal/io.h"
29 #include "internal/object.h"
30 #include "internal/thread.h"
31 #include "internal/variable.h"
32 #include "ruby/fiber/scheduler.h"
33 #include "iseq.h"
34 #include "mjit.h"
35 #include "probes.h"
36 #include "probes_helper.h"
37 #include "ruby/vm.h"
38 #include "vm_core.h"
39 #include "ractor_core.h"
40 
41 NORETURN(static void rb_raise_jump(VALUE, VALUE));
42 void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec);
43 void rb_ec_clear_all_trace_func(const rb_execution_context_t *ec);
44 
45 static int rb_ec_cleanup(rb_execution_context_t *ec, int ex);
46 static int rb_ec_exec_node(rb_execution_context_t *ec, void *n);
47 
50 
51 ID ruby_static_id_signo, ruby_static_id_status;
52 extern ID ruby_static_id_cause;
53 #define id_cause ruby_static_id_cause
54 
55 #define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
56 
57 #include "eval_error.c"
58 #include "eval_jump.c"
59 
60 #define CLASS_OR_MODULE_P(obj) \
61  (!SPECIAL_CONST_P(obj) && \
62  (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
63 
64 int
66 {
67  enum ruby_tag_type state;
68 
69  if (GET_VM())
70  return 0;
71 
72  ruby_init_stack((void *)&state);
73 
74  /*
75  * Disable THP early before mallocs happen because we want this to
76  * affect as many future pages as possible for CoW-friendliness
77  */
78 #if defined(__linux__) && defined(PR_SET_THP_DISABLE)
79  prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0);
80 #endif
81  Init_BareVM();
82  Init_heap();
83  rb_vm_encoded_insn_data_table_init();
84  Init_vm_objects();
85 
86  EC_PUSH_TAG(GET_EC());
87  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
88  rb_call_inits();
90  GET_VM()->running = 1;
91  }
92  EC_POP_TAG();
93 
94  return state;
95 }
96 
97 void
98 ruby_init(void)
99 {
100  int state = ruby_setup();
101  if (state) {
102  if (RTEST(ruby_debug))
103  error_print(GET_EC());
104  exit(EXIT_FAILURE);
105  }
106 }
107 
108 void *
109 ruby_options(int argc, char **argv)
110 {
111  rb_execution_context_t *ec = GET_EC();
112  enum ruby_tag_type state;
113  void *volatile iseq = 0;
114 
115  ruby_init_stack((void *)&iseq);
116  EC_PUSH_TAG(ec);
117  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
118  SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv));
119  }
120  else {
121  rb_ec_clear_current_thread_trace_func(ec);
122  state = error_handle(ec, state);
123  iseq = (void *)INT2FIX(state);
124  }
125  EC_POP_TAG();
126  return iseq;
127 }
128 
129 static void
130 rb_ec_fiber_scheduler_finalize(rb_execution_context_t *ec)
131 {
132  enum ruby_tag_type state;
133 
134  EC_PUSH_TAG(ec);
135  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
137  }
138  else {
139  state = error_handle(ec, state);
140  }
141  EC_POP_TAG();
142 }
143 
144 static void
145 rb_ec_teardown(rb_execution_context_t *ec)
146 {
147  // If the user code defined a scheduler for the top level thread, run it:
148  rb_ec_fiber_scheduler_finalize(ec);
149 
150  EC_PUSH_TAG(ec);
151  if (EC_EXEC_TAG() == TAG_NONE) {
152  rb_vm_trap_exit(rb_ec_vm_ptr(ec));
153  }
154  EC_POP_TAG();
155  rb_ec_exec_end_proc(ec);
156  rb_ec_clear_all_trace_func(ec);
157 }
158 
159 static void
160 rb_ec_finalize(rb_execution_context_t *ec)
161 {
163  ec->errinfo = Qnil;
164  rb_objspace_call_finalizer(rb_ec_vm_ptr(ec)->objspace);
165 }
166 
167 void
169 {
170  rb_execution_context_t *ec = GET_EC();
171  rb_ec_teardown(ec);
172  rb_ec_finalize(ec);
173 }
174 
175 int
177 {
178  return rb_ec_cleanup(GET_EC(), ex);
179 }
180 
181 static int
182 rb_ec_cleanup(rb_execution_context_t *ec, int ex0)
183 {
184  int state;
185  volatile VALUE errs[2] = { Qundef, Qundef };
186  int nerr;
187  rb_thread_t *th = rb_ec_thread_ptr(ec);
188  rb_thread_t *const volatile th0 = th;
189  volatile int sysex = EXIT_SUCCESS;
190  volatile int step = 0;
191  volatile int ex = ex0;
192 
193  rb_threadptr_interrupt(th);
194  rb_threadptr_check_signal(th);
195 
196  EC_PUSH_TAG(ec);
197  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
198  SAVE_ROOT_JMPBUF(th, { RUBY_VM_CHECK_INTS(ec); });
199 
200  step_0: step++;
201  errs[1] = ec->errinfo;
202  if (THROW_DATA_P(ec->errinfo)) ec->errinfo = Qnil;
203  ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
204 
205  SAVE_ROOT_JMPBUF(th, rb_ec_teardown(ec));
206 
207  step_1: step++;
208  /* protect from Thread#raise */
209  th->status = THREAD_KILLED;
210 
211  errs[0] = ec->errinfo;
212  SAVE_ROOT_JMPBUF(th, rb_ractor_terminate_all());
213  }
214  else {
215  th = th0;
216  switch (step) {
217  case 0: goto step_0;
218  case 1: goto step_1;
219  }
220  if (ex == 0) ex = state;
221  }
222  ec->errinfo = errs[1];
223  sysex = error_handle(ec, ex);
224 
225  state = 0;
226  for (nerr = 0; nerr < numberof(errs); ++nerr) {
227  VALUE err = ATOMIC_VALUE_EXCHANGE(errs[nerr], Qnil);
228  VALUE sig;
229 
230  if (!RTEST(err)) continue;
231 
232  /* ec->errinfo contains a NODE while break'ing */
233  if (THROW_DATA_P(err)) continue;
234 
235  if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
236  sysex = sysexit_status(err);
237  break;
238  }
239  else if (rb_obj_is_kind_of(err, rb_eSignal)) {
240  VALUE sig = rb_ivar_get(err, id_signo);
241  state = NUM2INT(sig);
242  break;
243  }
244  else if (rb_obj_is_kind_of(err, rb_eSystemCallError) &&
245  FIXNUM_P(sig = rb_attr_get(err, id_signo))) {
246  state = NUM2INT(sig);
247  break;
248  }
249  else if (sysex == EXIT_SUCCESS) {
250  sysex = EXIT_FAILURE;
251  }
252  }
253 
254  mjit_finish(true); // We still need ISeqs here.
255 
256  rb_ec_finalize(ec);
257 
258  /* unlock again if finalizer took mutexes. */
259  rb_threadptr_unlock_all_locking_mutexes(th);
260  th = th0;
261  EC_POP_TAG();
262  th = th0;
263  rb_thread_stop_timer_thread();
264  ruby_vm_destruct(th->vm);
265  if (state) ruby_default_signal(state);
266 
267  return sysex;
268 }
269 
270 static int
271 rb_ec_exec_node(rb_execution_context_t *ec, void *n)
272 {
273  volatile int state;
274  rb_iseq_t *iseq = (rb_iseq_t *)n;
275  if (!n) return 0;
276 
277  EC_PUSH_TAG(ec);
278  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
279  rb_thread_t *const th = rb_ec_thread_ptr(ec);
280  SAVE_ROOT_JMPBUF(th, {
281  rb_iseq_eval_main(iseq);
282  });
283  }
284  EC_POP_TAG();
285  return state;
286 }
287 
288 void
289 ruby_stop(int ex)
290 {
291  exit(ruby_cleanup(ex));
292 }
293 
294 int
295 ruby_executable_node(void *n, int *status)
296 {
297  VALUE v = (VALUE)n;
298  int s;
299 
300  switch (v) {
301  case Qtrue: s = EXIT_SUCCESS; break;
302  case Qfalse: s = EXIT_FAILURE; break;
303  default:
304  if (!FIXNUM_P(v)) return TRUE;
305  s = FIX2INT(v);
306  }
307  if (status) *status = s;
308  return FALSE;
309 }
310 
311 int
313 {
314  rb_execution_context_t *ec = GET_EC();
315  int status;
316  if (!ruby_executable_node(n, &status)) {
317  rb_ec_cleanup(ec, 0);
318  return status;
319  }
320  ruby_init_stack((void *)&status);
321  return rb_ec_cleanup(ec, rb_ec_exec_node(ec, n));
322 }
323 
324 int
326 {
327  ruby_init_stack((void *)&n);
328  return rb_ec_exec_node(GET_EC(), n);
329 }
330 
331 /*
332  * call-seq:
333  * Module.nesting -> array
334  *
335  * Returns the list of +Modules+ nested at the point of call.
336  *
337  * module M1
338  * module M2
339  * $a = Module.nesting
340  * end
341  * end
342  * $a #=> [M1::M2, M1]
343  * $a[0].name #=> "M1::M2"
344  */
345 
346 static VALUE
347 rb_mod_nesting(VALUE _)
348 {
349  VALUE ary = rb_ary_new();
350  const rb_cref_t *cref = rb_vm_cref();
351 
352  while (cref && CREF_NEXT(cref)) {
353  VALUE klass = CREF_CLASS(cref);
354  if (!CREF_PUSHED_BY_EVAL(cref) &&
355  !NIL_P(klass)) {
356  rb_ary_push(ary, klass);
357  }
358  cref = CREF_NEXT(cref);
359  }
360  return ary;
361 }
362 
363 /*
364  * call-seq:
365  * Module.constants -> array
366  * Module.constants(inherited) -> array
367  *
368  * In the first form, returns an array of the names of all
369  * constants accessible from the point of call.
370  * This list includes the names of all modules and classes
371  * defined in the global scope.
372  *
373  * Module.constants.first(4)
374  * # => [:ARGF, :ARGV, :ArgumentError, :Array]
375  *
376  * Module.constants.include?(:SEEK_SET) # => false
377  *
378  * class IO
379  * Module.constants.include?(:SEEK_SET) # => true
380  * end
381  *
382  * The second form calls the instance method +constants+.
383  */
384 
385 static VALUE
386 rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
387 {
388  const rb_cref_t *cref = rb_vm_cref();
389  VALUE klass;
390  VALUE cbase = 0;
391  void *data = 0;
392 
393  if (argc > 0 || mod != rb_cModule) {
394  return rb_mod_constants(argc, argv, mod);
395  }
396 
397  while (cref) {
398  klass = CREF_CLASS(cref);
399  if (!CREF_PUSHED_BY_EVAL(cref) &&
400  !NIL_P(klass)) {
401  data = rb_mod_const_at(CREF_CLASS(cref), data);
402  if (!cbase) {
403  cbase = klass;
404  }
405  }
406  cref = CREF_NEXT(cref);
407  }
408 
409  if (cbase) {
410  data = rb_mod_const_of(cbase, data);
411  }
412  return rb_const_list(data);
413 }
414 
421 void
423 {
424  if (SPECIAL_CONST_P(klass)) {
425  Check_Type(klass, T_CLASS);
426  }
427  if (RB_TYPE_P(klass, T_MODULE)) {
428  rb_module_set_initialized(klass);
429  }
430  if (OBJ_FROZEN(klass)) {
431  const char *desc;
432 
433  if (FL_TEST(klass, FL_SINGLETON)) {
434  desc = "object";
435  klass = rb_ivar_get(klass, id__attached__);
436  if (!SPECIAL_CONST_P(klass)) {
437  switch (BUILTIN_TYPE(klass)) {
438  case T_MODULE:
439  case T_ICLASS:
440  desc = "Module";
441  break;
442  case T_CLASS:
443  desc = "Class";
444  break;
445  default:
446  break;
447  }
448  }
449  }
450  else {
451  switch (BUILTIN_TYPE(klass)) {
452  case T_MODULE:
453  case T_ICLASS:
454  desc = "module";
455  break;
456  case T_CLASS:
457  desc = "class";
458  break;
459  default:
460  Check_Type(klass, T_CLASS);
461  UNREACHABLE;
462  }
463  }
464  rb_frozen_error_raise(klass, "can't modify frozen %s: %"PRIsVALUE, desc, klass);
465  }
466 }
467 
468 NORETURN(static void rb_longjmp(rb_execution_context_t *, int, volatile VALUE, VALUE));
469 static VALUE get_errinfo(void);
470 #define get_ec_errinfo(ec) rb_ec_get_errinfo(ec)
471 
472 static VALUE
473 exc_setup_cause(VALUE exc, VALUE cause)
474 {
475 #if OPT_SUPPORT_JOKE
476  if (NIL_P(cause)) {
477  ID id_true_cause;
478  CONST_ID(id_true_cause, "true_cause");
479 
480  cause = rb_attr_get(rb_eFatal, id_true_cause);
481  if (NIL_P(cause)) {
482  cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby");
483  rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */
484  OBJ_FREEZE(cause);
485  rb_ivar_set(rb_eFatal, id_true_cause, cause);
486  }
487  }
488 #endif
489  if (!NIL_P(cause) && cause != exc) {
490  rb_ivar_set(exc, id_cause, cause);
491  if (!rb_ivar_defined(cause, id_cause)) {
492  rb_ivar_set(cause, id_cause, Qnil);
493  }
494  }
495  return exc;
496 }
497 
498 static inline VALUE
499 exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause)
500 {
501  int nocause = 0;
502  int nocircular = 0;
503 
504  if (NIL_P(mesg)) {
505  mesg = ec->errinfo;
506  if (INTERNAL_EXCEPTION_P(mesg)) EC_JUMP_TAG(ec, TAG_FATAL);
507  nocause = 1;
508  }
509  if (NIL_P(mesg)) {
510  mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
511  nocause = 0;
512  nocircular = 1;
513  }
514  if (*cause == Qundef) {
515  if (nocause) {
516  *cause = Qnil;
517  nocircular = 1;
518  }
519  else if (!rb_ivar_defined(mesg, id_cause)) {
520  *cause = get_ec_errinfo(ec);
521  }
522  else {
523  nocircular = 1;
524  }
525  }
526  else if (!NIL_P(*cause) && !rb_obj_is_kind_of(*cause, rb_eException)) {
527  rb_raise(rb_eTypeError, "exception object expected");
528  }
529 
530  if (!nocircular && !NIL_P(*cause) && *cause != Qundef && *cause != mesg) {
531 #if 0 /* maybe critical for some cases */
532  rb_exc_check_circular_cause(*cause);
533 #else
534  VALUE c = *cause;
535  while (!NIL_P(c = rb_attr_get(c, id_cause))) {
536  if (c == mesg) {
537  rb_raise(rb_eArgError, "circular causes");
538  }
539  }
540 #endif
541  }
542  return mesg;
543 }
544 
545 static void
546 setup_exception(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
547 {
548  VALUE e;
549  int line;
550  const char *file = rb_source_location_cstr(&line);
551  const char *const volatile file0 = file;
552 
553  if ((file && !NIL_P(mesg)) || (cause != Qundef)) {
554  volatile int state = 0;
555 
556  EC_PUSH_TAG(ec);
557  if (EC_EXEC_TAG() == TAG_NONE && !(state = rb_ec_set_raised(ec))) {
558  VALUE bt = rb_get_backtrace(mesg);
559  if (!NIL_P(bt) || cause == Qundef) {
560  if (OBJ_FROZEN(mesg)) {
561  mesg = rb_obj_dup(mesg);
562  }
563  }
564  if (cause != Qundef && !THROW_DATA_P(cause)) {
565  exc_setup_cause(mesg, cause);
566  }
567  if (NIL_P(bt)) {
568  VALUE at = rb_ec_backtrace_object(ec);
569  rb_ivar_set(mesg, idBt_locations, at);
570  set_backtrace(mesg, at);
571  }
572  rb_ec_reset_raised(ec);
573  }
574  EC_POP_TAG();
575  file = file0;
576  if (state) goto fatal;
577  }
578 
579  if (!NIL_P(mesg)) {
580  ec->errinfo = mesg;
581  }
582 
583  if (RTEST(ruby_debug) && !NIL_P(e = ec->errinfo) &&
585  enum ruby_tag_type state;
586 
587  mesg = e;
588  EC_PUSH_TAG(ec);
589  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
590  ec->errinfo = Qnil;
591  e = rb_obj_as_string(mesg);
592  ec->errinfo = mesg;
593  if (file && line) {
594  e = rb_sprintf("Exception `%"PRIsVALUE"' at %s:%d - %"PRIsVALUE"\n",
595  rb_obj_class(mesg), file, line, e);
596  }
597  else if (file) {
598  e = rb_sprintf("Exception `%"PRIsVALUE"' at %s - %"PRIsVALUE"\n",
599  rb_obj_class(mesg), file, e);
600  }
601  else {
602  e = rb_sprintf("Exception `%"PRIsVALUE"' - %"PRIsVALUE"\n",
603  rb_obj_class(mesg), e);
604  }
605  warn_print_str(e);
606  }
607  EC_POP_TAG();
608  if (state == TAG_FATAL && ec->errinfo == exception_error) {
609  ec->errinfo = mesg;
610  }
611  else if (state) {
612  rb_ec_reset_raised(ec);
613  EC_JUMP_TAG(ec, state);
614  }
615  }
616 
617  if (rb_ec_set_raised(ec)) {
618  goto fatal;
619  }
620 
621  if (tag != TAG_FATAL) {
622  RUBY_DTRACE_HOOK(RAISE, rb_obj_classname(ec->errinfo));
623  EXEC_EVENT_HOOK(ec, RUBY_EVENT_RAISE, ec->cfp->self, 0, 0, 0, mesg);
624  }
625  return;
626 
627  fatal:
628  ec->errinfo = exception_error;
629  rb_ec_reset_raised(ec);
630  EC_JUMP_TAG(ec, TAG_FATAL);
631 }
632 
634 void
635 rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause)
636 {
637  if (cause == Qundef) {
638  cause = get_ec_errinfo(ec);
639  }
640  if (cause != mesg) {
641  rb_ivar_set(mesg, id_cause, cause);
642  }
643 }
644 
645 static void
646 rb_longjmp(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
647 {
648  mesg = exc_setup_message(ec, mesg, &cause);
649  setup_exception(ec, tag, mesg, cause);
650  rb_ec_raised_clear(ec);
651  EC_JUMP_TAG(ec, tag);
652 }
653 
654 static VALUE make_exception(int argc, const VALUE *argv, int isstr);
655 
656 NORETURN(static void rb_exc_exception(VALUE mesg, int tag, VALUE cause));
657 
658 static void
659 rb_exc_exception(VALUE mesg, int tag, VALUE cause)
660 {
661  if (!NIL_P(mesg)) {
662  mesg = make_exception(1, &mesg, FALSE);
663  }
664  rb_longjmp(GET_EC(), tag, mesg, cause);
665 }
666 
674 void
676 {
677  rb_exc_exception(mesg, TAG_RAISE, Qundef);
678 }
679 
687 void
689 {
690  rb_exc_exception(mesg, TAG_FATAL, Qnil);
691 }
692 
693 void
695 {
697 }
698 
699 enum {raise_opt_cause, raise_max_opt}; /*< \private */
700 
701 static int
702 extract_raise_opts(int argc, const VALUE *argv, VALUE *opts)
703 {
704  int i;
705  if (argc > 0) {
706  VALUE opt = argv[argc-1];
707  if (RB_TYPE_P(opt, T_HASH)) {
708  if (!RHASH_EMPTY_P(opt)) {
709  ID keywords[1];
710  CONST_ID(keywords[0], "cause");
711  rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
712  if (RHASH_EMPTY_P(opt)) --argc;
713  return argc;
714  }
715  }
716  }
717  for (i = 0; i < raise_max_opt; ++i) {
718  opts[i] = Qundef;
719  }
720  return argc;
721 }
722 
723 VALUE
724 rb_f_raise(int argc, VALUE *argv)
725 {
726  VALUE err;
727  VALUE opts[raise_max_opt], *const cause = &opts[raise_opt_cause];
728 
729  argc = extract_raise_opts(argc, argv, opts);
730  if (argc == 0) {
731  if (*cause != Qundef) {
732  rb_raise(rb_eArgError, "only cause is given with no arguments");
733  }
734  err = get_errinfo();
735  if (!NIL_P(err)) {
736  argc = 1;
737  argv = &err;
738  }
739  }
740  rb_raise_jump(rb_make_exception(argc, argv), *cause);
741 
743 }
744 
745 /*
746  * call-seq:
747  * raise
748  * raise(string, cause: $!)
749  * raise(exception [, string [, array]], cause: $!)
750  * fail
751  * fail(string, cause: $!)
752  * fail(exception [, string [, array]], cause: $!)
753  *
754  * With no arguments, raises the exception in <code>$!</code> or raises
755  * a RuntimeError if <code>$!</code> is +nil+. With a single +String+
756  * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
757  * the first parameter should be an +Exception+ class (or another
758  * object that returns an +Exception+ object when sent an +exception+
759  * message). The optional second parameter sets the message associated with
760  * the exception (accessible via Exception#message), and the third parameter
761  * is an array of callback information (accessible via Exception#backtrace).
762  * The +cause+ of the generated exception (accessible via Exception#cause)
763  * is automatically set to the "current" exception (<code>$!</code>), if any.
764  * An alternative value, either an +Exception+ object or +nil+, can be
765  * specified via the +:cause+ argument.
766  *
767  * Exceptions are caught by the +rescue+ clause of
768  * <code>begin...end</code> blocks.
769  *
770  * raise "Failed to create socket"
771  * raise ArgumentError, "No parameters", caller
772  */
773 
774 static VALUE
775 f_raise(int c, VALUE *v, VALUE _)
776 {
777  return rb_f_raise(c, v);
778 }
779 
780 static VALUE
781 make_exception(int argc, const VALUE *argv, int isstr)
782 {
783  VALUE mesg, exc;
784 
785  mesg = Qnil;
786  switch (argc) {
787  case 0:
788  return Qnil;
789  case 1:
790  exc = argv[0];
791  if (isstr &&! NIL_P(exc)) {
792  mesg = rb_check_string_type(exc);
793  if (!NIL_P(mesg)) {
794  return rb_exc_new3(rb_eRuntimeError, mesg);
795  }
796  }
797 
798  case 2:
799  case 3:
800  break;
801  default:
802  rb_error_arity(argc, 0, 3);
803  }
804  if (NIL_P(mesg)) {
805  mesg = rb_check_funcall(argv[0], idException, argc != 1, &argv[1]);
806  }
807  if (mesg == Qundef) {
808  rb_raise(rb_eTypeError, "exception class/object expected");
809  }
810  if (!rb_obj_is_kind_of(mesg, rb_eException)) {
811  rb_raise(rb_eTypeError, "exception object expected");
812  }
813  if (argc == 3) {
814  set_backtrace(mesg, argv[2]);
815  }
816 
817  return mesg;
818 }
819 
820 VALUE
821 rb_make_exception(int argc, const VALUE *argv)
822 {
823  return make_exception(argc, argv, TRUE);
824 }
825 
828 static void
829 rb_raise_jump(VALUE mesg, VALUE cause)
830 {
831  rb_execution_context_t *ec = GET_EC();
832  const rb_control_frame_t *cfp = ec->cfp;
833  const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
834  VALUE klass = me->owner;
835  VALUE self = cfp->self;
836  ID mid = me->called_id;
837 
838  rb_vm_pop_frame(ec);
839  EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, self, me->def->original_id, mid, klass, Qnil);
840 
841  rb_longjmp(ec, TAG_RAISE, mesg, cause);
842 }
843 
844 void
845 rb_jump_tag(int tag)
846 {
847  if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) {
848  unknown_longjmp_status(tag);
849  }
850  EC_JUMP_TAG(GET_EC(), tag);
851 }
852 
853 int
855 {
856  if (rb_vm_frame_block_handler(GET_EC()->cfp) == VM_BLOCK_HANDLER_NONE) {
857  return FALSE;
858  }
859  else {
860  return TRUE;
861  }
862 }
863 
864 int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp);
865 
866 int
868 {
869  return rb_vm_cframe_keyword_p(GET_EC()->cfp);
870 }
871 
873 
874 void
876 {
877  if (!rb_block_given_p()) {
878  rb_vm_localjump_error("no block given", Qnil, 0);
879  }
880 }
881 
882 VALUE
883 rb_rescue2(VALUE (* b_proc) (VALUE), VALUE data1,
884  VALUE (* r_proc) (VALUE, VALUE), VALUE data2, ...)
885 {
886  va_list ap;
887  va_start(ap, data2);
888  VALUE ret = rb_vrescue2(b_proc, data1, r_proc, data2, ap);
889  va_end(ap);
890  return ret;
891 }
892 
893 VALUE
894 rb_vrescue2(VALUE (* b_proc) (VALUE), VALUE data1,
895  VALUE (* r_proc) (VALUE, VALUE), VALUE data2,
896  va_list args)
897 {
898  enum ruby_tag_type state;
899  rb_execution_context_t * volatile ec = GET_EC();
900  rb_control_frame_t *volatile cfp = ec->cfp;
901  volatile VALUE result = Qfalse;
902  volatile VALUE e_info = ec->errinfo;
903 
904  EC_PUSH_TAG(ec);
905  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
906  retry_entry:
907  result = (*b_proc) (data1);
908  }
909  else if (result) {
910  /* escape from r_proc */
911  if (state == TAG_RETRY) {
912  state = TAG_NONE;
913  ec->errinfo = Qnil;
914  result = Qfalse;
915  goto retry_entry;
916  }
917  }
918  else {
919  rb_vm_rewind_cfp(ec, cfp);
920 
921  if (state == TAG_RAISE) {
922  int handle = FALSE;
923  VALUE eclass;
924  va_list ap;
925 
926  result = Qnil;
927  /* reuses args when raised again after retrying in r_proc */
928  va_copy(ap, args);
929  while ((eclass = va_arg(ap, VALUE)) != 0) {
930  if (rb_obj_is_kind_of(ec->errinfo, eclass)) {
931  handle = TRUE;
932  break;
933  }
934  }
935  va_end(ap);
936 
937  if (handle) {
938  state = TAG_NONE;
939  if (r_proc) {
940  result = (*r_proc) (data2, ec->errinfo);
941  }
942  ec->errinfo = e_info;
943  }
944  }
945  }
946  EC_POP_TAG();
947  if (state)
948  EC_JUMP_TAG(ec, state);
949 
950  return result;
951 }
952 
953 VALUE
954 rb_rescue(VALUE (* b_proc)(VALUE), VALUE data1,
955  VALUE (* r_proc)(VALUE, VALUE), VALUE data2)
956 {
957  return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
958  (VALUE)0);
959 }
960 
961 VALUE
962 rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate)
963 {
964  volatile VALUE result = Qnil;
965  volatile enum ruby_tag_type state;
966  rb_execution_context_t * volatile ec = GET_EC();
967  rb_control_frame_t *volatile cfp = ec->cfp;
968 
969  EC_PUSH_TAG(ec);
970  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
971  SAVE_ROOT_JMPBUF(rb_ec_thread_ptr(ec), result = (*proc) (data));
972  }
973  else {
974  rb_vm_rewind_cfp(ec, cfp);
975  }
976  EC_POP_TAG();
977 
978  if (pstate != NULL) *pstate = state;
979  return result;
980 }
981 
982 VALUE
983 rb_ensure(VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2)
984 {
985  int state;
986  volatile VALUE result = Qnil;
987  VALUE errinfo;
988  rb_execution_context_t * volatile ec = GET_EC();
989  rb_ensure_list_t ensure_list;
990  ensure_list.entry.marker = 0;
991  ensure_list.entry.e_proc = e_proc;
992  ensure_list.entry.data2 = data2;
993  ensure_list.next = ec->ensure_list;
994  ec->ensure_list = &ensure_list;
995  EC_PUSH_TAG(ec);
996  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
997  result = (*b_proc) (data1);
998  }
999  EC_POP_TAG();
1000  errinfo = ec->errinfo;
1001  if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) {
1002  ec->errinfo = Qnil;
1003  }
1004  ec->ensure_list=ensure_list.next;
1005  (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
1006  ec->errinfo = errinfo;
1007  if (state)
1008  EC_JUMP_TAG(ec, state);
1009  return result;
1010 }
1011 
1012 static ID
1013 frame_func_id(const rb_control_frame_t *cfp)
1014 {
1015  const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1016 
1017  if (me) {
1018  return me->def->original_id;
1019  }
1020  else {
1021  return 0;
1022  }
1023 }
1024 
1025 static ID
1026 frame_called_id(rb_control_frame_t *cfp)
1027 {
1028  const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1029 
1030  if (me) {
1031  return me->called_id;
1032  }
1033  else {
1034  return 0;
1035  }
1036 }
1037 
1038 ID
1040 {
1041  return frame_func_id(GET_EC()->cfp);
1042 }
1043 
1044 ID
1046 {
1047  return frame_called_id(GET_EC()->cfp);
1048 }
1049 
1050 static rb_control_frame_t *
1051 previous_frame(const rb_execution_context_t *ec)
1052 {
1053  rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1054  /* check if prev_cfp can be accessible */
1055  if ((void *)(ec->vm_stack + ec->vm_stack_size) == (void *)(prev_cfp)) {
1056  return 0;
1057  }
1058  return prev_cfp;
1059 }
1060 
1061 static ID
1062 prev_frame_callee(void)
1063 {
1064  rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1065  if (!prev_cfp) return 0;
1066  return frame_called_id(prev_cfp);
1067 }
1068 
1069 static ID
1070 prev_frame_func(void)
1071 {
1072  rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1073  if (!prev_cfp) return 0;
1074  return frame_func_id(prev_cfp);
1075 }
1076 
1083 ID
1085 {
1086  const rb_execution_context_t *ec = GET_EC();
1087  const rb_control_frame_t *cfp = ec->cfp;
1088  ID mid;
1089 
1090  while (!(mid = frame_func_id(cfp)) &&
1091  (cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp),
1092  !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)));
1093  return mid;
1094 }
1095 
1096 /*
1097  * call-seq:
1098  * append_features(mod) -> mod
1099  *
1100  * When this module is included in another, Ruby calls
1101  * #append_features in this module, passing it the receiving module
1102  * in _mod_. Ruby's default implementation is to add the constants,
1103  * methods, and module variables of this module to _mod_ if this
1104  * module has not already been added to _mod_ or one of its
1105  * ancestors. See also Module#include.
1106  */
1107 
1108 static VALUE
1109 rb_mod_append_features(VALUE module, VALUE include)
1110 {
1111  if (!CLASS_OR_MODULE_P(include)) {
1112  Check_Type(include, T_CLASS);
1113  }
1114  rb_include_module(include, module);
1115 
1116  return module;
1117 }
1118 
1119 /*
1120  * call-seq:
1121  * include(module, ...) -> self
1122  *
1123  * Invokes Module.append_features on each parameter in reverse order.
1124  */
1125 
1126 static VALUE
1127 rb_mod_include(int argc, VALUE *argv, VALUE module)
1128 {
1129  int i;
1130  ID id_append_features, id_included;
1131 
1132  CONST_ID(id_append_features, "append_features");
1133  CONST_ID(id_included, "included");
1134 
1135  if (FL_TEST(module, RMODULE_IS_REFINEMENT)) {
1136  rb_warn_deprecated_to_remove_at(3.2, "Refinement#include", NULL);
1137  }
1138 
1140  for (i = 0; i < argc; i++)
1141  Check_Type(argv[i], T_MODULE);
1142  while (argc--) {
1143  rb_funcall(argv[argc], id_append_features, 1, module);
1144  rb_funcall(argv[argc], id_included, 1, module);
1145  }
1146  return module;
1147 }
1148 
1149 /*
1150  * call-seq:
1151  * prepend_features(mod) -> mod
1152  *
1153  * When this module is prepended in another, Ruby calls
1154  * #prepend_features in this module, passing it the receiving module
1155  * in _mod_. Ruby's default implementation is to overlay the
1156  * constants, methods, and module variables of this module to _mod_
1157  * if this module has not already been added to _mod_ or one of its
1158  * ancestors. See also Module#prepend.
1159  */
1160 
1161 static VALUE
1162 rb_mod_prepend_features(VALUE module, VALUE prepend)
1163 {
1164  if (!CLASS_OR_MODULE_P(prepend)) {
1165  Check_Type(prepend, T_CLASS);
1166  }
1167  rb_prepend_module(prepend, module);
1168 
1169  return module;
1170 }
1171 
1172 /*
1173  * call-seq:
1174  * prepend(module, ...) -> self
1175  *
1176  * Invokes Module.prepend_features on each parameter in reverse order.
1177  */
1178 
1179 static VALUE
1180 rb_mod_prepend(int argc, VALUE *argv, VALUE module)
1181 {
1182  int i;
1183  ID id_prepend_features, id_prepended;
1184 
1185  if (FL_TEST(module, RMODULE_IS_REFINEMENT)) {
1186  rb_warn_deprecated_to_remove_at(3.2, "Refinement#prepend", NULL);
1187  }
1188 
1189  CONST_ID(id_prepend_features, "prepend_features");
1190  CONST_ID(id_prepended, "prepended");
1191 
1193  for (i = 0; i < argc; i++)
1194  Check_Type(argv[i], T_MODULE);
1195  while (argc--) {
1196  rb_funcall(argv[argc], id_prepend_features, 1, module);
1197  rb_funcall(argv[argc], id_prepended, 1, module);
1198  }
1199  return module;
1200 }
1201 
1202 static void
1203 ensure_class_or_module(VALUE obj)
1204 {
1205  if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
1207  "wrong argument type %"PRIsVALUE" (expected Class or Module)",
1208  rb_obj_class(obj));
1209  }
1210 }
1211 
1212 static VALUE
1213 hidden_identity_hash_new(void)
1214 {
1215  VALUE hash = rb_ident_hash_new();
1216 
1217  RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1218  return hash;
1219 }
1220 
1221 static VALUE
1222 refinement_superclass(VALUE superclass)
1223 {
1224  if (RB_TYPE_P(superclass, T_MODULE)) {
1225  /* FIXME: Should ancestors of superclass be used here? */
1226  return rb_include_class_new(RCLASS_ORIGIN(superclass), rb_cBasicObject);
1227  }
1228  else {
1229  return superclass;
1230  }
1231 }
1232 
1236 static void
1237 rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module)
1238 {
1239  VALUE iclass, c, superclass = klass;
1240 
1241  ensure_class_or_module(klass);
1242  Check_Type(module, T_MODULE);
1243  if (NIL_P(CREF_REFINEMENTS(cref))) {
1244  CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new());
1245  }
1246  else {
1247  if (CREF_OMOD_SHARED(cref)) {
1248  CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref)));
1249  CREF_OMOD_SHARED_UNSET(cref);
1250  }
1251  if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) {
1252  superclass = c;
1253  while (c && RB_TYPE_P(c, T_ICLASS)) {
1254  if (RBASIC(c)->klass == module) {
1255  /* already used refinement */
1256  return;
1257  }
1258  c = RCLASS_SUPER(c);
1259  }
1260  }
1261  }
1262  FL_SET(module, RMODULE_IS_OVERLAID);
1263  superclass = refinement_superclass(superclass);
1264  c = iclass = rb_include_class_new(module, superclass);
1265  RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1266 
1267  RCLASS_M_TBL(c) = RCLASS_M_TBL(module);
1268 
1269  module = RCLASS_SUPER(module);
1270  while (module && module != klass) {
1271  FL_SET(module, RMODULE_IS_OVERLAID);
1272  c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
1273  RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1274  module = RCLASS_SUPER(module);
1275  }
1276  rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass);
1277 }
1278 
1279 static int
1280 using_refinement(VALUE klass, VALUE module, VALUE arg)
1281 {
1282  rb_cref_t *cref = (rb_cref_t *) arg;
1283 
1284  rb_using_refinement(cref, klass, module);
1285  return ST_CONTINUE;
1286 }
1287 
1288 static void
1289 using_module_recursive(const rb_cref_t *cref, VALUE klass)
1290 {
1291  ID id_refinements;
1292  VALUE super, module, refinements;
1293 
1294  super = RCLASS_SUPER(klass);
1295  if (super) {
1296  using_module_recursive(cref, super);
1297  }
1298  switch (BUILTIN_TYPE(klass)) {
1299  case T_MODULE:
1300  module = klass;
1301  break;
1302 
1303  case T_ICLASS:
1304  module = RBASIC(klass)->klass;
1305  break;
1306 
1307  default:
1308  rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1309  rb_obj_classname(klass));
1310  break;
1311  }
1312  CONST_ID(id_refinements, "__refinements__");
1313  refinements = rb_attr_get(module, id_refinements);
1314  if (NIL_P(refinements)) return;
1315  rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1316 }
1317 
1321 static void
1322 rb_using_module(const rb_cref_t *cref, VALUE module)
1323 {
1324  Check_Type(module, T_MODULE);
1325  using_module_recursive(cref, module);
1326  rb_clear_method_cache_all();
1327 }
1328 
1330 VALUE
1331 rb_refinement_module_get_refined_class(VALUE module)
1332 {
1333  ID id_refined_class;
1334 
1335  CONST_ID(id_refined_class, "__refined_class__");
1336  return rb_attr_get(module, id_refined_class);
1337 }
1338 
1339 static void
1340 add_activated_refinement(VALUE activated_refinements,
1341  VALUE klass, VALUE refinement)
1342 {
1343  VALUE iclass, c, superclass = klass;
1344 
1345  if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1346  superclass = c;
1347  while (c && RB_TYPE_P(c, T_ICLASS)) {
1348  if (RBASIC(c)->klass == refinement) {
1349  /* already used refinement */
1350  return;
1351  }
1352  c = RCLASS_SUPER(c);
1353  }
1354  }
1355  FL_SET(refinement, RMODULE_IS_OVERLAID);
1356  superclass = refinement_superclass(superclass);
1357  c = iclass = rb_include_class_new(refinement, superclass);
1358  RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1359  refinement = RCLASS_SUPER(refinement);
1360  while (refinement && refinement != klass) {
1361  FL_SET(refinement, RMODULE_IS_OVERLAID);
1362  c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1363  RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1364  refinement = RCLASS_SUPER(refinement);
1365  }
1366  rb_hash_aset(activated_refinements, klass, iclass);
1367 }
1368 
1369 /*
1370  * call-seq:
1371  * refine(mod) { block } -> module
1372  *
1373  * Refine <i>mod</i> in the receiver.
1374  *
1375  * Returns a module, where refined methods are defined.
1376  */
1377 
1378 static VALUE
1379 rb_mod_refine(VALUE module, VALUE klass)
1380 {
1381  VALUE refinement;
1382  ID id_refinements, id_activated_refinements,
1383  id_refined_class, id_defined_at;
1384  VALUE refinements, activated_refinements;
1385  rb_thread_t *th = GET_THREAD();
1386  VALUE block_handler = rb_vm_frame_block_handler(th->ec->cfp);
1387 
1388  if (block_handler == VM_BLOCK_HANDLER_NONE) {
1389  rb_raise(rb_eArgError, "no block given");
1390  }
1391  if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
1392  rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine");
1393  }
1394 
1395  ensure_class_or_module(klass);
1396  CONST_ID(id_refinements, "__refinements__");
1397  refinements = rb_attr_get(module, id_refinements);
1398  if (NIL_P(refinements)) {
1399  refinements = hidden_identity_hash_new();
1400  rb_ivar_set(module, id_refinements, refinements);
1401  }
1402  CONST_ID(id_activated_refinements, "__activated_refinements__");
1403  activated_refinements = rb_attr_get(module, id_activated_refinements);
1404  if (NIL_P(activated_refinements)) {
1405  activated_refinements = hidden_identity_hash_new();
1406  rb_ivar_set(module, id_activated_refinements,
1407  activated_refinements);
1408  }
1409  refinement = rb_hash_lookup(refinements, klass);
1410  if (NIL_P(refinement)) {
1411  VALUE superclass = refinement_superclass(klass);
1412  refinement = rb_refinement_new();
1413  RCLASS_SET_SUPER(refinement, superclass);
1414  FL_SET(refinement, RMODULE_IS_REFINEMENT);
1415  CONST_ID(id_refined_class, "__refined_class__");
1416  rb_ivar_set(refinement, id_refined_class, klass);
1417  CONST_ID(id_defined_at, "__defined_at__");
1418  rb_ivar_set(refinement, id_defined_at, module);
1419  rb_hash_aset(refinements, klass, refinement);
1420  add_activated_refinement(activated_refinements, klass, refinement);
1421  }
1422  rb_yield_refine_block(refinement, activated_refinements);
1423  return refinement;
1424 }
1425 
1426 static void
1427 ignored_block(VALUE module, const char *klass)
1428 {
1429  const char *anon = "";
1430  Check_Type(module, T_MODULE);
1431  if (!RTEST(rb_search_class_path(module))) {
1432  anon = ", maybe for Module.new";
1433  }
1434  rb_warn("%s""using doesn't call the given block""%s.", klass, anon);
1435 }
1436 
1437 /*
1438  * call-seq:
1439  * using(module) -> self
1440  *
1441  * Import class refinements from <i>module</i> into the current class or
1442  * module definition.
1443  */
1444 
1445 static VALUE
1446 mod_using(VALUE self, VALUE module)
1447 {
1448  rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1449 
1450  if (prev_frame_func()) {
1452  "Module#using is not permitted in methods");
1453  }
1454  if (prev_cfp && prev_cfp->self != self) {
1455  rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1456  }
1457  if (rb_block_given_p()) {
1458  ignored_block(module, "Module#");
1459  }
1460  rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1461  return self;
1462 }
1463 
1464 static int
1465 used_modules_i(VALUE _, VALUE mod, VALUE ary)
1466 {
1467  ID id_defined_at;
1468  CONST_ID(id_defined_at, "__defined_at__");
1469  while (FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1470  rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at));
1471  mod = RCLASS_SUPER(mod);
1472  }
1473  return ST_CONTINUE;
1474 }
1475 
1476 /*
1477  * call-seq:
1478  * used_modules -> array
1479  *
1480  * Returns an array of all modules used in the current scope. The ordering
1481  * of modules in the resulting array is not defined.
1482  *
1483  * module A
1484  * refine Object do
1485  * end
1486  * end
1487  *
1488  * module B
1489  * refine Object do
1490  * end
1491  * end
1492  *
1493  * using A
1494  * using B
1495  * p Module.used_modules
1496  *
1497  * <em>produces:</em>
1498  *
1499  * [B, A]
1500  */
1501 static VALUE
1502 rb_mod_s_used_modules(VALUE _)
1503 {
1504  const rb_cref_t *cref = rb_vm_cref();
1505  VALUE ary = rb_ary_new();
1506 
1507  while (cref) {
1508  if (!NIL_P(CREF_REFINEMENTS(cref))) {
1509  rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary);
1510  }
1511  cref = CREF_NEXT(cref);
1512  }
1513 
1514  return rb_funcall(ary, rb_intern("uniq"), 0);
1515 }
1516 
1518  rb_cref_t *cref;
1519  VALUE refinement;
1520  VALUE module;
1521 };
1522 
1523 /* vm.c */
1524 rb_cref_t *rb_vm_cref_dup_without_refinements(const rb_cref_t *cref);
1525 
1526 static enum rb_id_table_iterator_result
1527 refinement_import_methods_i(ID key, VALUE value, void *data)
1528 {
1529  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1530  struct refinement_import_methods_arg *arg = (struct refinement_import_methods_arg *)data;
1531 
1532  if (me->def->type != VM_METHOD_TYPE_ISEQ) {
1533  rb_raise(rb_eArgError, "Can't import method which is not defined with Ruby code: %"PRIsVALUE"#%"PRIsVALUE, rb_class_path(arg->module), rb_id2str(key));
1534  }
1535  rb_cref_t *new_cref = rb_vm_cref_dup_without_refinements(me->def->body.iseq.cref);
1536  CREF_REFINEMENTS_SET(new_cref, CREF_REFINEMENTS(arg->cref));
1537  rb_add_method_iseq(arg->refinement, key, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
1538  return ID_TABLE_CONTINUE;
1539 }
1540 
1541 /*
1542  * Note: docs for the method are in class.c
1543  */
1544 
1545 static VALUE
1546 refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1547 {
1548  int i;
1549  struct refinement_import_methods_arg arg;
1550 
1552  for (i = 0; i < argc; i++) {
1553  Check_Type(argv[i], T_MODULE);
1554  if (RCLASS_SUPER(argv[i])) {
1555  rb_warn("%"PRIsVALUE" has ancestors, but Refinement#import_methods doesn't import their methods", rb_class_path(argv[i]));
1556  }
1557  }
1558  arg.cref = rb_vm_cref_replace_with_duplicated_cref();
1559  arg.refinement = refinement;
1560  for (i = 0; i < argc; i++) {
1561  arg.module = argv[i];
1562  struct rb_id_table *m_tbl = RCLASS_M_TBL(argv[i]);
1563  if (!m_tbl) continue;
1564  rb_id_table_foreach(m_tbl, refinement_import_methods_i, &arg);
1565  }
1566  return refinement;
1567 }
1568 
1569 void
1570 rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
1571 {
1572  rb_obj_call_init_kw(obj, argc, argv, RB_NO_KEYWORDS);
1573 }
1574 
1575 void
1576 rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
1577 {
1578  PASS_PASSED_BLOCK_HANDLER();
1579  rb_funcallv_kw(obj, idInitialize, argc, argv, kw_splat);
1580 }
1581 
1582 void
1584 {
1585  rb_include_module(rb_singleton_class(obj), module);
1586 }
1587 
1588 /*
1589  * call-seq:
1590  * extend_object(obj) -> obj
1591  *
1592  * Extends the specified object by adding this module's constants and
1593  * methods (which are added as singleton methods). This is the callback
1594  * method used by Object#extend.
1595  *
1596  * module Picky
1597  * def Picky.extend_object(o)
1598  * if String === o
1599  * puts "Can't add Picky to a String"
1600  * else
1601  * puts "Picky added to #{o.class}"
1602  * super
1603  * end
1604  * end
1605  * end
1606  * (s = Array.new).extend Picky # Call Object.extend
1607  * (s = "quick brown fox").extend Picky
1608  *
1609  * <em>produces:</em>
1610  *
1611  * Picky added to Array
1612  * Can't add Picky to a String
1613  */
1614 
1615 static VALUE
1616 rb_mod_extend_object(VALUE mod, VALUE obj)
1617 {
1618  rb_extend_object(obj, mod);
1619  return obj;
1620 }
1621 
1622 /*
1623  * call-seq:
1624  * obj.extend(module, ...) -> obj
1625  *
1626  * Adds to _obj_ the instance methods from each module given as a
1627  * parameter.
1628  *
1629  * module Mod
1630  * def hello
1631  * "Hello from Mod.\n"
1632  * end
1633  * end
1634  *
1635  * class Klass
1636  * def hello
1637  * "Hello from Klass.\n"
1638  * end
1639  * end
1640  *
1641  * k = Klass.new
1642  * k.hello #=> "Hello from Klass.\n"
1643  * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1644  * k.hello #=> "Hello from Mod.\n"
1645  */
1646 
1647 static VALUE
1648 rb_obj_extend(int argc, VALUE *argv, VALUE obj)
1649 {
1650  int i;
1651  ID id_extend_object, id_extended;
1652 
1653  CONST_ID(id_extend_object, "extend_object");
1654  CONST_ID(id_extended, "extended");
1655 
1657  for (i = 0; i < argc; i++)
1658  Check_Type(argv[i], T_MODULE);
1659  while (argc--) {
1660  rb_funcall(argv[argc], id_extend_object, 1, obj);
1661  rb_funcall(argv[argc], id_extended, 1, obj);
1662  }
1663  return obj;
1664 }
1665 
1666 /*
1667  * call-seq:
1668  * include(module, ...) -> self
1669  *
1670  * Invokes Module.append_features on each parameter in turn.
1671  * Effectively adds the methods and constants in each module to the
1672  * receiver.
1673  */
1674 
1675 static VALUE
1676 top_include(int argc, VALUE *argv, VALUE self)
1677 {
1678  rb_thread_t *th = GET_THREAD();
1679 
1680  if (th->top_wrapper) {
1681  rb_warning("main.include in the wrapped load is effective only in wrapper module");
1682  return rb_mod_include(argc, argv, th->top_wrapper);
1683  }
1684  return rb_mod_include(argc, argv, rb_cObject);
1685 }
1686 
1687 /*
1688  * call-seq:
1689  * using(module) -> self
1690  *
1691  * Import class refinements from <i>module</i> into the scope where
1692  * #using is called.
1693  */
1694 
1695 static VALUE
1696 top_using(VALUE self, VALUE module)
1697 {
1698  const rb_cref_t *cref = rb_vm_cref();
1699  rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1700 
1701  if (CREF_NEXT(cref) || (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) {
1702  rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel");
1703  }
1704  if (rb_block_given_p()) {
1705  ignored_block(module, "main.");
1706  }
1707  rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1708  return self;
1709 }
1710 
1711 static const VALUE *
1712 errinfo_place(const rb_execution_context_t *ec)
1713 {
1714  const rb_control_frame_t *cfp = ec->cfp;
1715  const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1716 
1717  while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1718  if (VM_FRAME_RUBYFRAME_P(cfp)) {
1719  if (cfp->iseq->body->type == ISEQ_TYPE_RESCUE) {
1720  return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1721  }
1722  else if (cfp->iseq->body->type == ISEQ_TYPE_ENSURE &&
1723  !THROW_DATA_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR]) &&
1724  !FIXNUM_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR])) {
1725  return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1726  }
1727  }
1728  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1729  }
1730  return 0;
1731 }
1732 
1733 VALUE
1734 rb_ec_get_errinfo(const rb_execution_context_t *ec)
1735 {
1736  const VALUE *ptr = errinfo_place(ec);
1737  if (ptr) {
1738  return *ptr;
1739  }
1740  else {
1741  return ec->errinfo;
1742  }
1743 }
1744 
1745 static VALUE
1746 get_errinfo(void)
1747 {
1748  return get_ec_errinfo(GET_EC());
1749 }
1750 
1751 static VALUE
1752 errinfo_getter(ID id, VALUE *_)
1753 {
1754  return get_errinfo();
1755 }
1756 
1757 VALUE
1759 {
1760  return GET_EC()->errinfo;
1761 }
1762 
1763 void
1765 {
1766  if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1767  rb_raise(rb_eTypeError, "assigning non-exception to $!");
1768  }
1769  GET_EC()->errinfo = err;
1770 }
1771 
1772 static VALUE
1773 errat_getter(ID id, VALUE *_)
1774 {
1775  VALUE err = get_errinfo();
1776  if (!NIL_P(err)) {
1777  return rb_get_backtrace(err);
1778  }
1779  else {
1780  return Qnil;
1781  }
1782 }
1783 
1784 static void
1785 errat_setter(VALUE val, ID id, VALUE *var)
1786 {
1787  VALUE err = get_errinfo();
1788  if (NIL_P(err)) {
1789  rb_raise(rb_eArgError, "$! not set");
1790  }
1791  set_backtrace(err, val);
1792 }
1793 
1794 /*
1795  * call-seq:
1796  * __method__ -> symbol
1797  *
1798  * Returns the name at the definition of the current method as a
1799  * Symbol.
1800  * If called outside of a method, it returns <code>nil</code>.
1801  *
1802  */
1803 
1804 static VALUE
1805 rb_f_method_name(VALUE _)
1806 {
1807  ID fname = prev_frame_func(); /* need *method* ID */
1808 
1809  if (fname) {
1810  return ID2SYM(fname);
1811  }
1812  else {
1813  return Qnil;
1814  }
1815 }
1816 
1817 /*
1818  * call-seq:
1819  * __callee__ -> symbol
1820  *
1821  * Returns the called name of the current method as a Symbol.
1822  * If called outside of a method, it returns <code>nil</code>.
1823  *
1824  */
1825 
1826 static VALUE
1827 rb_f_callee_name(VALUE _)
1828 {
1829  ID fname = prev_frame_callee(); /* need *callee* ID */
1830 
1831  if (fname) {
1832  return ID2SYM(fname);
1833  }
1834  else {
1835  return Qnil;
1836  }
1837 }
1838 
1839 /*
1840  * call-seq:
1841  * __dir__ -> string
1842  *
1843  * Returns the canonicalized absolute path of the directory of the file from
1844  * which this method is called. It means symlinks in the path is resolved.
1845  * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1846  * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1847  *
1848  */
1849 static VALUE
1850 f_current_dirname(VALUE _)
1851 {
1852  VALUE base = rb_current_realfilepath();
1853  if (NIL_P(base)) {
1854  return Qnil;
1855  }
1856  base = rb_file_dirname(base);
1857  return base;
1858 }
1859 
1860 /*
1861  * call-seq:
1862  * global_variables -> array
1863  *
1864  * Returns an array of the names of global variables. This includes
1865  * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>,
1866  * but does not include the numbered regexp global variables (<tt>$1</tt>,
1867  * <tt>$2</tt>, etc.).
1868  *
1869  * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
1870  */
1871 
1872 static VALUE
1873 f_global_variables(VALUE _)
1874 {
1875  return rb_f_global_variables();
1876 }
1877 
1878 /*
1879  * call-seq:
1880  * trace_var(symbol, cmd ) -> nil
1881  * trace_var(symbol) {|val| block } -> nil
1882  *
1883  * Controls tracing of assignments to global variables. The parameter
1884  * +symbol+ identifies the variable (as either a string name or a
1885  * symbol identifier). _cmd_ (which may be a string or a
1886  * +Proc+ object) or block is executed whenever the variable
1887  * is assigned. The block or +Proc+ object receives the
1888  * variable's new value as a parameter. Also see
1889  * Kernel::untrace_var.
1890  *
1891  * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
1892  * $_ = "hello"
1893  * $_ = ' there'
1894  *
1895  * <em>produces:</em>
1896  *
1897  * $_ is now 'hello'
1898  * $_ is now ' there'
1899  */
1900 
1901 static VALUE
1902 f_trace_var(int c, const VALUE *a, VALUE _)
1903 {
1904  return rb_f_trace_var(c, a);
1905 }
1906 
1907 /*
1908  * call-seq:
1909  * untrace_var(symbol [, cmd] ) -> array or nil
1910  *
1911  * Removes tracing for the specified command on the given global
1912  * variable and returns +nil+. If no command is specified,
1913  * removes all tracing for that variable and returns an array
1914  * containing the commands actually removed.
1915  */
1916 
1917 static VALUE
1918 f_untrace_var(int c, const VALUE *a, VALUE _)
1919 {
1920  return rb_f_untrace_var(c, a);
1921 }
1922 
1923 void
1924 Init_eval(void)
1925 {
1926  rb_define_virtual_variable("$@", errat_getter, errat_setter);
1927  rb_define_virtual_variable("$!", errinfo_getter, 0);
1928 
1929  rb_gvar_ractor_local("$@");
1930  rb_gvar_ractor_local("$!");
1931 
1932  rb_define_global_function("raise", f_raise, -1);
1933  rb_define_global_function("fail", f_raise, -1);
1934 
1935  rb_define_global_function("global_variables", f_global_variables, 0);
1936 
1937  rb_define_global_function("__method__", rb_f_method_name, 0);
1938  rb_define_global_function("__callee__", rb_f_callee_name, 0);
1939  rb_define_global_function("__dir__", f_current_dirname, 0);
1940 
1941  rb_define_method(rb_cModule, "include", rb_mod_include, -1);
1942  rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
1943 
1944  rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
1945  rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
1946  rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
1947  rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
1948  rb_define_private_method(rb_cModule, "using", mod_using, 1);
1949  rb_define_singleton_method(rb_cModule, "used_modules",
1950  rb_mod_s_used_modules, 0);
1951  rb_undef_method(rb_cClass, "refine");
1952  rb_define_private_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1953 
1954  rb_undef_method(rb_cClass, "module_function");
1955 
1956  Init_vm_eval();
1957  Init_eval_method();
1958 
1959  rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
1960  rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
1961 
1962  rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
1963  "include", top_include, -1);
1964  rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
1965  "using", top_using, 1);
1966 
1967  rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
1968 
1969  rb_define_global_function("trace_var", f_trace_var, -1);
1970  rb_define_global_function("untrace_var", f_untrace_var, -1);
1971 
1972  rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered");
1973  rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");
1974 
1975  id_signo = rb_intern_const("signo");
1976  id_status = rb_intern_const("status");
1977 }
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:685
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:677
#define RUBY_EVENT_RAISE
Encountered a raise statement.
Definition: event.h:41
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition: event.h:40
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition: class.c:1043
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition: class.c:935
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition: eval.c:1583
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
Definition: class.c:1289
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition: class.c:2068
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:422
void rb_need_block(void)
Declares that the current method needs a block.
Definition: eval.c:875
ID rb_frame_last_func(void)
Returns the ID of the last method in the call stack.
Definition: eval.c:1084
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition: class.c:1938
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:1914
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
Definition: eval.c:867
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:854
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition: class.c:2195
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:2110
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition: fl_type.h:58
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition: fl_type.h:145
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition: assume.h:30
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition: fl_type.h:143
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition: assume.h:31
#define FIX2INT
Old name of RB_FIX2INT.
Definition: int.h:41
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition: value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition: value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition: value_type.h:65
#define FL_SET
Old name of RB_FL_SET.
Definition: fl_type.h:137
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition: error.h:38
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition: int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition: value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition: value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition: value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition: fl_type.h:139
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition: symbol.h:47
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition: eval.c:289
int ruby_exec_node(void *n)
Identical to ruby_run_node(), except it returns an opaque execution status.
Definition: eval.c:325
int ruby_setup(void)
Initializes the VM and builtin libraries.
Definition: eval.c:65
void ruby_finalize(void)
Runs the VM finalization processes.
Definition: eval.c:168
void ruby_init_stack(volatile VALUE *addr)
Set stack bottom of Ruby implementation.
int ruby_cleanup(int ex)
Destructs the VM.
Definition: eval.c:176
void * ruby_process_options(int argc, char **argv)
Identical to ruby_options(), except it raises ruby-level exceptions on failure.
Definition: ruby.c:2730
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:2683
void ruby_sig_finalize(void)
Clear signal handlers.
Definition: signal.c:1497
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition: error.h:470
VALUE rb_eLocalJumpError
LocalJumpError exception.
Definition: eval.c:48
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3025
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
Definition: eval.c:883
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:675
VALUE rb_eSystemExit
SystemExit exception.
Definition: error.c:1092
VALUE rb_eStandardError
StandardError exception.
Definition: error.c:1096
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1764
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1099
VALUE rb_vrescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2, va_list args)
Identical to rb_rescue2(), except it takes va_list instead of variadic number of arguments.
Definition: eval.c:894
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Raises an instance of rb_eFrozenError.
Definition: error.c:3347
VALUE rb_eFatal
fatal exception.
Definition: error.c:1095
VALUE rb_eInterrupt
Interrupt exception.
Definition: error.c:1093
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition: eval.c:688
VALUE rb_eRuntimeError
RuntimeError exception.
Definition: error.c:1097
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
Definition: error.c:418
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition: error.c:1137
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1100
VALUE rb_eException
Mother of all exceptions.
Definition: error.c:1091
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
Identical to rb_rescue2(), except it does not take a list of exception classes.
Definition: eval.c:954
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:983
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
Definition: eval.c:1758
VALUE rb_eSysStackError
SystemStackError exception.
Definition: eval.c:49
VALUE rb_eThreadError
ThreadError exception.
Definition: eval.c:872
VALUE rb_eSystemCallError
SystemCallError exception.
Definition: error.c:1119
void rb_warning(const char *fmt,...)
Issues a warning.
Definition: error.c:449
VALUE rb_eSignal
SignalException exception.
Definition: error.c:1094
VALUE rb_cClass
Class class.
Definition: object.c:52
VALUE rb_mKernel
Kernel module.
Definition: object.c:49
VALUE rb_cRefinement
Refinement class.
Definition: object.c:53
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
Definition: globals.h:172
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:188
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition: object.c:451
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:48
VALUE rb_cModule
Module class.
Definition: object.c:51
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition: object.c:731
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition: rgengc.h:220
int ruby_run_node(void *n)
Runs the given compiled source and exits this process.
Definition: eval.c:312
void ruby_init(void)
Calls ruby_setup() and check error.
Definition: eval.c:98
void * ruby_options(int argc, char **argv)
Processes command line arguments and compiles the Ruby source to execute.
Definition: eval.c:109
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition: eval.c:295
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1102
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
Definition: vm_eval.c:1069
VALUE rb_ary_new(void)
Allocates a new, empty array.
Definition: array.c:750
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
Definition: array.c:1308
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition: error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition: error.h:294
ID rb_frame_callee(void)
Identical to rb_frame_this_func(), except it returns the named used to call the method.
Definition: eval.c:1045
ID rb_frame_this_func(void)
Queries the name of the Ruby level method that is calling this function.
Definition: eval.c:1039
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Calls initialize method of the passed object with the passed arguments.
Definition: eval.c:1570
void rb_interrupt(void)
Raises an instance of rb_eInterrupt.
Definition: eval.c:694
VALUE rb_make_exception(int argc, const VALUE *argv)
Constructs an exception object from the list of arguments, in a manner similar to Ruby's raise.
Definition: eval.c:821
void rb_jump_tag(int state)
This function is to re-throw global escapes.
Definition: eval.c:845
void rb_obj_call_init_kw(VALUE, int, const VALUE *, int)
Identical to rb_obj_call_init(), except you can specify how to handle the last element of the given a...
Definition: eval.c:1576
VALUE rb_file_dirname(VALUE fname)
Strips a file path's last component (and trailing separators if any).
Definition: file.c:4785
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
Definition: hash.c:2903
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
Definition: hash.c:2108
VALUE rb_hash_dup(VALUE hash)
Duplicates a hash.
Definition: hash.c:1585
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
void ruby_default_signal(int sig)
Pretends as if there was no custom signal handler.
Definition: signal.c:407
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.h:1733
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition: string.c:2659
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition: string.c:1657
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Deletes the passed tracer from the passed global variable, or if omitted, deletes everything.
Definition: variable.c:657
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
Definition: variable.c:2949
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition: variable.c:1293
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1575
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
Definition: variable.c:611
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
Definition: variable.c:2910
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition: variable.c:2981
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1285
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition: variable.c:1592
VALUE rb_f_global_variables(void)
Queries the list of global variables.
Definition: variable.c:811
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition: variable.c:172
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
Definition: variable.c:2927
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition: vm_eval.c:664
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition: symbol.h:276
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:782
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
Definition: symbol.c:935
void rb_define_virtual_variable(const char *name, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter)
Defines a global variable that is purely function-backended.
Definition: variable.c:594
int ruby_vm_destruct(ruby_vm_t *vm)
Destructs the passed VM.
Definition: vm.c:2696
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1201
#define RBASIC(obj)
Convenient casting macro.
Definition: rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition: rclass.h:46
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition: rhash.h:92
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition: variable.c:309
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition: scan_args.h:69
Scheduler APIs.
VALUE rb_fiber_scheduler_set(VALUE scheduler)
Destructively assigns the passed scheduler to that of the current thread that is calling this functio...
Definition: scheduler.c:91
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition: stdarg.h:35
Definition: method.h:62
CREF (Class REFerence)
Definition: method.h:44
Definition: method.h:54
rb_cref_t * cref
class reference, should be marked
Definition: method.h:136
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:135
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition: value_type.h:432
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:375