Ruby: eval.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

eval.c
Go to the documentation of this file.
1/**********************************************************************
2
3 eval.c -
4
5 $Author$
6 created at: Thu Jun 10 14:22:17 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "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 "internal/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(void rb_raise_jump(VALUE, VALUE));
42void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec);
43void rb_ec_clear_all_trace_func(const rb_execution_context_t *ec);
44
45static int rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex);
46static int rb_ec_exec_node(rb_execution_context_t *ec, void *n);
47
48 VALUE rb_eLocalJumpError;
49 VALUE rb_eSysStackError;
50
51 ID ruby_static_id_signo, ruby_static_id_status;
52extern 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
69int
70ruby_setup(void)
71{
72 enum ruby_tag_type state;
73
74 if (GET_VM())
75 return 0;
76
77 ruby_init_stack((void *)&state);
78
79 /*
80 * Disable THP early before mallocs happen because we want this to
81 * affect as many future pages as possible for CoW-friendliness
82 */
83#if defined(__linux__) && defined(PR_SET_THP_DISABLE)
84 prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0);
85#endif
86 Init_BareVM();
87 Init_heap();
88 rb_vm_encoded_insn_data_table_init();
89 Init_vm_objects();
90
91 EC_PUSH_TAG(GET_EC());
92 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
93 rb_call_inits();
94 ruby_prog_init();
95 GET_VM()->running = 1;
96 }
97 EC_POP_TAG();
98
99 return state;
100}
101
107void
108ruby_init(void)
109{
110 int state = ruby_setup();
111 if (state) {
112 if (RTEST(ruby_debug))
113 error_print(GET_EC());
114 exit(EXIT_FAILURE);
115 }
116}
117
128void *
129 ruby_options(int argc, char **argv)
130{
131 rb_execution_context_t *ec = GET_EC();
132 enum ruby_tag_type state;
133 void *volatile iseq = 0;
134
135 ruby_init_stack((void *)&iseq);
136 EC_PUSH_TAG(ec);
137 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
138 SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv));
139 }
140 else {
141 rb_ec_clear_current_thread_trace_func(ec);
142 state = error_handle(ec, state);
143 iseq = (void *)INT2FIX(state);
144 }
145 EC_POP_TAG();
146 return iseq;
147}
148
149static void
150rb_ec_scheduler_finalize(rb_execution_context_t *ec)
151{
152 enum ruby_tag_type state;
153
154 EC_PUSH_TAG(ec);
155 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
156 rb_scheduler_set(Qnil);
157 }
158 else {
159 state = error_handle(ec, state);
160 }
161 EC_POP_TAG();
162}
163
164static void
165rb_ec_teardown(rb_execution_context_t *ec)
166{
167 // If the user code defined a scheduler for the top level thread, run it:
168 rb_ec_scheduler_finalize(ec);
169
170 EC_PUSH_TAG(ec);
171 if (EC_EXEC_TAG() == TAG_NONE) {
172 rb_vm_trap_exit(rb_ec_vm_ptr(ec));
173 }
174 EC_POP_TAG();
175 rb_ec_exec_end_proc(ec);
176 rb_ec_clear_all_trace_func(ec);
177}
178
179static void
180rb_ec_finalize(rb_execution_context_t *ec)
181{
182 ruby_sig_finalize();
183 ec->errinfo = Qnil;
184 rb_objspace_call_finalizer(rb_ec_vm_ptr(ec)->objspace);
185}
186
194void
195ruby_finalize(void)
196{
197 rb_execution_context_t *ec = GET_EC();
198 rb_ec_teardown(ec);
199 rb_ec_finalize(ec);
200}
201
212int
213ruby_cleanup(volatile int ex)
214{
215 return rb_ec_cleanup(GET_EC(), ex);
216}
217
218static int
219rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex)
220{
221 int state;
222 volatile VALUE errs[2] = { Qundef, Qundef };
223 int nerr;
224 rb_thread_t *th = rb_ec_thread_ptr(ec);
225 rb_thread_t *const volatile th0 = th;
226 volatile int sysex = EXIT_SUCCESS;
227 volatile int step = 0;
228
229 rb_threadptr_interrupt(th);
230 rb_threadptr_check_signal(th);
231
232 EC_PUSH_TAG(ec);
233 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
234 th = th0;
235 SAVE_ROOT_JMPBUF(th, { RUBY_VM_CHECK_INTS(ec); });
236
237 step_0: step++;
238 th = th0;
239 errs[1] = ec->errinfo;
240 if (THROW_DATA_P(ec->errinfo)) ec->errinfo = Qnil;
241 ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
242
243 SAVE_ROOT_JMPBUF(th, rb_ec_teardown(ec));
244
245 step_1: step++;
246 th = th0;
247 /* protect from Thread#raise */
248 th->status = THREAD_KILLED;
249
250 errs[0] = ec->errinfo;
251 SAVE_ROOT_JMPBUF(th, rb_ractor_terminate_all());
252 }
253 else {
254 switch (step) {
255 case 0: goto step_0;
256 case 1: goto step_1;
257 }
258 if (ex == 0) ex = state;
259 }
260 th = th0;
261 ec->errinfo = errs[1];
262 sysex = error_handle(ec, ex);
263
264 state = 0;
265 for (nerr = 0; nerr < numberof(errs); ++nerr) {
266 VALUE err = ATOMIC_VALUE_EXCHANGE(errs[nerr], Qnil);
267 VALUE sig;
268
269 if (!RTEST(err)) continue;
270
271 /* ec->errinfo contains a NODE while break'ing */
272 if (THROW_DATA_P(err)) continue;
273
274 if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
275 sysex = sysexit_status(err);
276 break;
277 }
278 else if (rb_obj_is_kind_of(err, rb_eSignal)) {
279 VALUE sig = rb_ivar_get(err, id_signo);
280 state = NUM2INT(sig);
281 break;
282 }
283 else if (rb_obj_is_kind_of(err, rb_eSystemCallError) &&
284 FIXNUM_P(sig = rb_attr_get(err, id_signo))) {
285 state = NUM2INT(sig);
286 break;
287 }
288 else if (sysex == EXIT_SUCCESS) {
289 sysex = EXIT_FAILURE;
290 }
291 }
292
293 mjit_finish(true); // We still need ISeqs here.
294
295 rb_ec_finalize(ec);
296
297 /* unlock again if finalizer took mutexes. */
298 rb_threadptr_unlock_all_locking_mutexes(th);
299 EC_POP_TAG();
300 rb_thread_stop_timer_thread();
301 ruby_vm_destruct(th->vm);
302 if (state) ruby_default_signal(state);
303
304 return sysex;
305}
306
307static int
308rb_ec_exec_node(rb_execution_context_t *ec, void *n)
309{
310 volatile int state;
311 rb_iseq_t *iseq = (rb_iseq_t *)n;
312 if (!n) return 0;
313
314 EC_PUSH_TAG(ec);
315 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
316 rb_thread_t *const th = rb_ec_thread_ptr(ec);
317 SAVE_ROOT_JMPBUF(th, {
318 rb_iseq_eval_main(iseq);
319 });
320 }
321 EC_POP_TAG();
322 return state;
323}
324
326void
327ruby_stop(int ex)
328{
329 exit(ruby_cleanup(ex));
330}
331
344int
345ruby_executable_node(void *n, int *status)
346{
347 VALUE v = (VALUE)n;
348 int s;
349
350 switch (v) {
351 case Qtrue: s = EXIT_SUCCESS; break;
352 case Qfalse: s = EXIT_FAILURE; break;
353 default:
354 if (!FIXNUM_P(v)) return TRUE;
355 s = FIX2INT(v);
356 }
357 if (status) *status = s;
358 return FALSE;
359}
360
365int
366 ruby_run_node(void *n)
367{
368 rb_execution_context_t *ec = GET_EC();
369 int status;
370 if (!ruby_executable_node(n, &status)) {
371 rb_ec_cleanup(ec, 0);
372 return status;
373 }
374 ruby_init_stack((void *)&status);
375 return rb_ec_cleanup(ec, rb_ec_exec_node(ec, n));
376}
377
379int
380ruby_exec_node(void *n)
381{
382 ruby_init_stack((void *)&n);
383 return rb_ec_exec_node(GET_EC(), n);
384}
385
386/*
387 * call-seq:
388 * Module.nesting -> array
389 *
390 * Returns the list of +Modules+ nested at the point of call.
391 *
392 * module M1
393 * module M2
394 * $a = Module.nesting
395 * end
396 * end
397 * $a #=> [M1::M2, M1]
398 * $a[0].name #=> "M1::M2"
399 */
400
401static VALUE
402rb_mod_nesting(VALUE _)
403{
404 VALUE ary = rb_ary_new();
405 const rb_cref_t *cref = rb_vm_cref();
406
407 while (cref && CREF_NEXT(cref)) {
408 VALUE klass = CREF_CLASS(cref);
409 if (!CREF_PUSHED_BY_EVAL(cref) &&
410 !NIL_P(klass)) {
411 rb_ary_push(ary, klass);
412 }
413 cref = CREF_NEXT(cref);
414 }
415 return ary;
416}
417
418/*
419 * call-seq:
420 * Module.constants -> array
421 * Module.constants(inherited) -> array
422 *
423 * In the first form, returns an array of the names of all
424 * constants accessible from the point of call.
425 * This list includes the names of all modules and classes
426 * defined in the global scope.
427 *
428 * Module.constants.first(4)
429 * # => [:ARGF, :ARGV, :ArgumentError, :Array]
430 *
431 * Module.constants.include?(:SEEK_SET) # => false
432 *
433 * class IO
434 * Module.constants.include?(:SEEK_SET) # => true
435 * end
436 *
437 * The second form calls the instance method +constants+.
438 */
439
440static VALUE
441rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
442{
443 const rb_cref_t *cref = rb_vm_cref();
444 VALUE klass;
445 VALUE cbase = 0;
446 void *data = 0;
447
448 if (argc > 0 || mod != rb_cModule) {
449 return rb_mod_constants(argc, argv, mod);
450 }
451
452 while (cref) {
453 klass = CREF_CLASS(cref);
454 if (!CREF_PUSHED_BY_EVAL(cref) &&
455 !NIL_P(klass)) {
456 data = rb_mod_const_at(CREF_CLASS(cref), data);
457 if (!cbase) {
458 cbase = klass;
459 }
460 }
461 cref = CREF_NEXT(cref);
462 }
463
464 if (cbase) {
465 data = rb_mod_const_of(cbase, data);
466 }
467 return rb_const_list(data);
468}
469
476void
477rb_class_modify_check(VALUE klass)
478{
479 if (SPECIAL_CONST_P(klass)) {
480 Check_Type(klass, T_CLASS);
481 }
482 if (OBJ_FROZEN(klass)) {
483 const char *desc;
484
485 if (FL_TEST(klass, FL_SINGLETON)) {
486 desc = "object";
487 klass = rb_ivar_get(klass, id__attached__);
488 if (!SPECIAL_CONST_P(klass)) {
489 switch (BUILTIN_TYPE(klass)) {
490 case T_MODULE:
491 case T_ICLASS:
492 desc = "Module";
493 break;
494 case T_CLASS:
495 desc = "Class";
496 break;
497 default:
498 break;
499 }
500 }
501 }
502 else {
503 switch (BUILTIN_TYPE(klass)) {
504 case T_MODULE:
505 case T_ICLASS:
506 desc = "module";
507 break;
508 case T_CLASS:
509 desc = "class";
510 break;
511 default:
512 Check_Type(klass, T_CLASS);
513 UNREACHABLE;
514 }
515 }
516 rb_frozen_error_raise(klass, "can't modify frozen %s: %"PRIsVALUE, desc, klass);
517 }
518}
519
520 NORETURN(static void rb_longjmp(rb_execution_context_t *, int, volatile VALUE, VALUE));
521static VALUE get_errinfo(void);
522#define get_ec_errinfo(ec) rb_ec_get_errinfo(ec)
523
524static VALUE
525exc_setup_cause(VALUE exc, VALUE cause)
526{
527#if OPT_SUPPORT_JOKE
528 if (NIL_P(cause)) {
529 ID id_true_cause;
530 CONST_ID(id_true_cause, "true_cause");
531
532 cause = rb_attr_get(rb_eFatal, id_true_cause);
533 if (NIL_P(cause)) {
534 cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby");
535 rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */
536 OBJ_FREEZE(cause);
537 rb_ivar_set(rb_eFatal, id_true_cause, cause);
538 }
539 }
540#endif
541 if (!NIL_P(cause) && cause != exc) {
542 rb_ivar_set(exc, id_cause, cause);
543 if (!rb_ivar_defined(cause, id_cause)) {
544 rb_ivar_set(cause, id_cause, Qnil);
545 }
546 }
547 return exc;
548}
549
550static inline VALUE
551exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause)
552{
553 int nocause = 0;
554 int nocircular = 0;
555
556 if (NIL_P(mesg)) {
557 mesg = ec->errinfo;
558 if (INTERNAL_EXCEPTION_P(mesg)) EC_JUMP_TAG(ec, TAG_FATAL);
559 nocause = 1;
560 }
561 if (NIL_P(mesg)) {
562 mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
563 nocause = 0;
564 nocircular = 1;
565 }
566 if (*cause == Qundef) {
567 if (nocause) {
568 *cause = Qnil;
569 nocircular = 1;
570 }
571 else if (!rb_ivar_defined(mesg, id_cause)) {
572 *cause = get_ec_errinfo(ec);
573 }
574 else {
575 nocircular = 1;
576 }
577 }
578 else if (!NIL_P(*cause) && !rb_obj_is_kind_of(*cause, rb_eException)) {
579 rb_raise(rb_eTypeError, "exception object expected");
580 }
581
582 if (!nocircular && !NIL_P(*cause) && *cause != Qundef && *cause != mesg) {
583 VALUE c = *cause;
584 while (!NIL_P(c = rb_attr_get(c, id_cause))) {
585 if (c == mesg) {
586 rb_raise(rb_eArgError, "circular causes");
587 }
588 }
589 }
590 return mesg;
591}
592
593static void
594setup_exception(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
595{
596 VALUE e;
597 int line;
598 const char *file = rb_source_location_cstr(&line);
599 const char *const volatile file0 = file;
600
601 if ((file && !NIL_P(mesg)) || (cause != Qundef)) {
602 volatile int state = 0;
603
604 EC_PUSH_TAG(ec);
605 if (EC_EXEC_TAG() == TAG_NONE && !(state = rb_ec_set_raised(ec))) {
606 VALUE bt = rb_get_backtrace(mesg);
607 if (!NIL_P(bt) || cause == Qundef) {
608 if (OBJ_FROZEN(mesg)) {
609 mesg = rb_obj_dup(mesg);
610 }
611 }
612 if (cause != Qundef && !THROW_DATA_P(cause)) {
613 exc_setup_cause(mesg, cause);
614 }
615 if (NIL_P(bt)) {
616 VALUE at = rb_ec_backtrace_object(ec);
617 rb_ivar_set(mesg, idBt_locations, at);
618 set_backtrace(mesg, at);
619 }
620 rb_ec_reset_raised(ec);
621 }
622 EC_POP_TAG();
623 file = file0;
624 if (state) goto fatal;
625 }
626
627 if (!NIL_P(mesg)) {
628 ec->errinfo = mesg;
629 }
630
631 if (RTEST(ruby_debug) && !NIL_P(e = ec->errinfo) &&
632 !rb_obj_is_kind_of(e, rb_eSystemExit)) {
633 enum ruby_tag_type state;
634
635 mesg = e;
636 EC_PUSH_TAG(ec);
637 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
638 ec->errinfo = Qnil;
639 e = rb_obj_as_string(mesg);
640 ec->errinfo = mesg;
641 if (file && line) {
642 e = rb_sprintf("Exception `%"PRIsVALUE"' at %s:%d - %"PRIsVALUE"\n",
643 rb_obj_class(mesg), file, line, e);
644 }
645 else if (file) {
646 e = rb_sprintf("Exception `%"PRIsVALUE"' at %s - %"PRIsVALUE"\n",
647 rb_obj_class(mesg), file, e);
648 }
649 else {
650 e = rb_sprintf("Exception `%"PRIsVALUE"' - %"PRIsVALUE"\n",
651 rb_obj_class(mesg), e);
652 }
653 warn_print_str(e);
654 }
655 EC_POP_TAG();
656 if (state == TAG_FATAL && ec->errinfo == exception_error) {
657 ec->errinfo = mesg;
658 }
659 else if (state) {
660 rb_ec_reset_raised(ec);
661 EC_JUMP_TAG(ec, state);
662 }
663 }
664
665 if (rb_ec_set_raised(ec)) {
666 goto fatal;
667 }
668
669 if (tag != TAG_FATAL) {
670 RUBY_DTRACE_HOOK(RAISE, rb_obj_classname(ec->errinfo));
671 EXEC_EVENT_HOOK(ec, RUBY_EVENT_RAISE, ec->cfp->self, 0, 0, 0, mesg);
672 }
673 return;
674
675 fatal:
676 ec->errinfo = exception_error;
677 rb_ec_reset_raised(ec);
678 EC_JUMP_TAG(ec, TAG_FATAL);
679}
680
682void
683rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause)
684{
685 if (cause == Qundef) {
686 cause = get_ec_errinfo(ec);
687 }
688 if (cause != mesg) {
689 rb_ivar_set(mesg, id_cause, cause);
690 }
691}
692
693static void
694rb_longjmp(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
695{
696 mesg = exc_setup_message(ec, mesg, &cause);
697 setup_exception(ec, tag, mesg, cause);
698 rb_ec_raised_clear(ec);
699 EC_JUMP_TAG(ec, tag);
700}
701
702static VALUE make_exception(int argc, const VALUE *argv, int isstr);
703
711void
712rb_exc_raise(VALUE mesg)
713{
714 if (!NIL_P(mesg)) {
715 mesg = make_exception(1, &mesg, FALSE);
716 }
717 rb_longjmp(GET_EC(), TAG_RAISE, mesg, Qundef);
718}
719
727void
728rb_exc_fatal(VALUE mesg)
729{
730 if (!NIL_P(mesg)) {
731 mesg = make_exception(1, &mesg, FALSE);
732 }
733 rb_longjmp(GET_EC(), TAG_FATAL, mesg, Qnil);
734}
735
740void
741rb_interrupt(void)
742{
743 rb_exc_raise(rb_exc_new(rb_eInterrupt, 0, 0));
744}
745
746 enum {raise_opt_cause, raise_max_opt}; /*< \private */
747
748static int
749extract_raise_opts(int argc, const VALUE *argv, VALUE *opts)
750{
751 int i;
752 if (argc > 0) {
753 VALUE opt = argv[argc-1];
754 if (RB_TYPE_P(opt, T_HASH)) {
755 if (!RHASH_EMPTY_P(opt)) {
756 ID keywords[1];
757 CONST_ID(keywords[0], "cause");
758 rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
759 if (RHASH_EMPTY_P(opt)) --argc;
760 return argc;
761 }
762 }
763 }
764 for (i = 0; i < raise_max_opt; ++i) {
765 opts[i] = Qundef;
766 }
767 return argc;
768}
769
770 VALUE
771rb_f_raise(int argc, VALUE *argv)
772{
773 VALUE err;
774 VALUE opts[raise_max_opt], *const cause = &opts[raise_opt_cause];
775
776 argc = extract_raise_opts(argc, argv, opts);
777 if (argc == 0) {
778 if (*cause != Qundef) {
779 rb_raise(rb_eArgError, "only cause is given with no arguments");
780 }
781 err = get_errinfo();
782 if (!NIL_P(err)) {
783 argc = 1;
784 argv = &err;
785 }
786 }
787 rb_raise_jump(rb_make_exception(argc, argv), *cause);
788
789 UNREACHABLE_RETURN(Qnil);
790}
791
792/*
793 * call-seq:
794 * raise
795 * raise(string, cause: $!)
796 * raise(exception [, string [, array]], cause: $!)
797 * fail
798 * fail(string, cause: $!)
799 * fail(exception [, string [, array]], cause: $!)
800 *
801 * With no arguments, raises the exception in <code>$!</code> or raises
802 * a RuntimeError if <code>$!</code> is +nil+. With a single +String+
803 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
804 * the first parameter should be an +Exception+ class (or another
805 * object that returns an +Exception+ object when sent an +exception+
806 * message). The optional second parameter sets the message associated with
807 * the exception (accessible via Exception#message), and the third parameter
808 * is an array of callback information (accessible via Exception#backtrace).
809 * The +cause+ of the generated exception (accessible via Exception#cause)
810 * is automatically set to the "current" exception (<code>$!</code>), if any.
811 * An alternative value, either an +Exception+ object or +nil+, can be
812 * specified via the +:cause+ argument.
813 *
814 * Exceptions are caught by the +rescue+ clause of
815 * <code>begin...end</code> blocks.
816 *
817 * raise "Failed to create socket"
818 * raise ArgumentError, "No parameters", caller
819 */
820
821static VALUE
822f_raise(int c, VALUE *v, VALUE _)
823{
824 return rb_f_raise(c, v);
825}
826
827static VALUE
828make_exception(int argc, const VALUE *argv, int isstr)
829{
830 VALUE mesg, exc;
831
832 mesg = Qnil;
833 switch (argc) {
834 case 0:
835 return Qnil;
836 case 1:
837 exc = argv[0];
838 if (isstr &&! NIL_P(exc)) {
839 mesg = rb_check_string_type(exc);
840 if (!NIL_P(mesg)) {
841 return rb_exc_new3(rb_eRuntimeError, mesg);
842 }
843 }
844
845 case 2:
846 case 3:
847 break;
848 default:
849 rb_error_arity(argc, 0, 3);
850 }
851 if (NIL_P(mesg)) {
852 mesg = rb_check_funcall(argv[0], idException, argc != 1, &argv[1]);
853 }
854 if (mesg == Qundef) {
855 rb_raise(rb_eTypeError, "exception class/object expected");
856 }
857 if (!rb_obj_is_kind_of(mesg, rb_eException)) {
858 rb_raise(rb_eTypeError, "exception object expected");
859 }
860 if (argc == 3) {
861 set_backtrace(mesg, argv[2]);
862 }
863
864 return mesg;
865}
866
887 VALUE
888rb_make_exception(int argc, const VALUE *argv)
889{
890 return make_exception(argc, argv, TRUE);
891}
892
896void
897rb_raise_jump(VALUE mesg, VALUE cause)
898{
899 rb_execution_context_t *ec = GET_EC();
900 const rb_control_frame_t *cfp = ec->cfp;
901 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
902 VALUE klass = me->owner;
903 VALUE self = cfp->self;
904 ID mid = me->called_id;
905
906 rb_vm_pop_frame(ec);
907 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, self, me->def->original_id, mid, klass, Qnil);
908
909 rb_longjmp(ec, TAG_RAISE, mesg, cause);
910}
911
920void
921rb_jump_tag(int tag)
922{
923 if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) {
924 unknown_longjmp_status(tag);
925 }
926 EC_JUMP_TAG(GET_EC(), tag);
927}
928
934int
935rb_block_given_p(void)
936{
937 if (rb_vm_frame_block_handler(GET_EC()->cfp) == VM_BLOCK_HANDLER_NONE) {
938 return FALSE;
939 }
940 else {
941 return TRUE;
942 }
943}
944
945int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp);
946
947int
948rb_keyword_given_p(void)
949{
950 return rb_vm_cframe_keyword_p(GET_EC()->cfp);
951}
952
953 VALUE rb_eThreadError;
954
960void
961rb_need_block(void)
962{
963 if (!rb_block_given_p()) {
964 rb_vm_localjump_error("no block given", Qnil, 0);
965 }
966}
967
990 VALUE
991rb_rescue2(VALUE (* b_proc) (VALUE), VALUE data1,
992 VALUE (* r_proc) (VALUE, VALUE), VALUE data2, ...)
993{
994 va_list ap;
995 va_start(ap, data2);
996 VALUE ret = rb_vrescue2(b_proc, data1, r_proc, data2, ap);
997 va_end(ap);
998 return ret;
999}
1000
1005 VALUE
1006rb_vrescue2(VALUE (* b_proc) (VALUE), VALUE data1,
1007 VALUE (* r_proc) (VALUE, VALUE), VALUE data2,
1008 va_list args)
1009{
1010 enum ruby_tag_type state;
1011 rb_execution_context_t * volatile ec = GET_EC();
1012 rb_control_frame_t *volatile cfp = ec->cfp;
1013 volatile VALUE result = Qfalse;
1014 volatile VALUE e_info = ec->errinfo;
1015
1016 EC_PUSH_TAG(ec);
1017 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1018 retry_entry:
1019 result = (*b_proc) (data1);
1020 }
1021 else if (result) {
1022 /* escape from r_proc */
1023 if (state == TAG_RETRY) {
1024 state = TAG_NONE;
1025 ec->errinfo = Qnil;
1026 result = Qfalse;
1027 goto retry_entry;
1028 }
1029 }
1030 else {
1031 rb_vm_rewind_cfp(ec, cfp);
1032
1033 if (state == TAG_RAISE) {
1034 int handle = FALSE;
1035 VALUE eclass;
1036 va_list ap;
1037
1038 result = Qnil;
1039 /* reuses args when raised again after retrying in r_proc */
1040 va_copy(ap, args);
1041 while ((eclass = va_arg(ap, VALUE)) != 0) {
1042 if (rb_obj_is_kind_of(ec->errinfo, eclass)) {
1043 handle = TRUE;
1044 break;
1045 }
1046 }
1047 va_end(ap);
1048
1049 if (handle) {
1050 state = TAG_NONE;
1051 if (r_proc) {
1052 result = (*r_proc) (data2, ec->errinfo);
1053 }
1054 ec->errinfo = e_info;
1055 }
1056 }
1057 }
1058 EC_POP_TAG();
1059 if (state)
1060 EC_JUMP_TAG(ec, state);
1061
1062 return result;
1063}
1064
1079 VALUE
1080rb_rescue(VALUE (* b_proc)(VALUE), VALUE data1,
1081 VALUE (* r_proc)(VALUE, VALUE), VALUE data2)
1082{
1083 return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
1084 (VALUE)0);
1085}
1086
1104 VALUE
1105rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate)
1106{
1107 volatile VALUE result = Qnil;
1108 volatile enum ruby_tag_type state;
1109 rb_execution_context_t * volatile ec = GET_EC();
1110 rb_control_frame_t *volatile cfp = ec->cfp;
1111 struct rb_vm_protect_tag protect_tag;
1112 rb_jmpbuf_t org_jmpbuf;
1113
1114 protect_tag.prev = ec->protect_tag;
1115
1116 EC_PUSH_TAG(ec);
1117 ec->protect_tag = &protect_tag;
1118 MEMCPY(&org_jmpbuf, &rb_ec_thread_ptr(ec)->root_jmpbuf, rb_jmpbuf_t, 1);
1119 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1120 SAVE_ROOT_JMPBUF(rb_ec_thread_ptr(ec), result = (*proc) (data));
1121 }
1122 else {
1123 rb_vm_rewind_cfp(ec, cfp);
1124 }
1125 MEMCPY(&rb_ec_thread_ptr(ec)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);
1126 ec->protect_tag = protect_tag.prev;
1127 EC_POP_TAG();
1128
1129 if (pstate != NULL) *pstate = state;
1130 return result;
1131}
1132
1147 VALUE
1148rb_ensure(VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2)
1149{
1150 int state;
1151 volatile VALUE result = Qnil;
1152 VALUE errinfo;
1153 rb_execution_context_t * volatile ec = GET_EC();
1154 rb_ensure_list_t ensure_list;
1155 ensure_list.entry.marker = 0;
1156 ensure_list.entry.e_proc = e_proc;
1157 ensure_list.entry.data2 = data2;
1158 ensure_list.next = ec->ensure_list;
1159 ec->ensure_list = &ensure_list;
1160 EC_PUSH_TAG(ec);
1161 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1162 result = (*b_proc) (data1);
1163 }
1164 EC_POP_TAG();
1165 errinfo = ec->errinfo;
1166 if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) {
1167 ec->errinfo = Qnil;
1168 }
1169 ec->ensure_list=ensure_list.next;
1170 (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
1171 ec->errinfo = errinfo;
1172 if (state)
1173 EC_JUMP_TAG(ec, state);
1174 return result;
1175}
1176
1177static ID
1178frame_func_id(const rb_control_frame_t *cfp)
1179{
1180 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1181
1182 if (me) {
1183 return me->def->original_id;
1184 }
1185 else {
1186 return 0;
1187 }
1188}
1189
1190static ID
1191frame_called_id(rb_control_frame_t *cfp)
1192{
1193 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1194
1195 if (me) {
1196 return me->called_id;
1197 }
1198 else {
1199 return 0;
1200 }
1201}
1202
1215 ID
1216rb_frame_this_func(void)
1217{
1218 return frame_func_id(GET_EC()->cfp);
1219}
1220
1232 ID
1233rb_frame_callee(void)
1234{
1235 return frame_called_id(GET_EC()->cfp);
1236}
1237
1238static rb_control_frame_t *
1239previous_frame(const rb_execution_context_t *ec)
1240{
1241 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1242 /* check if prev_cfp can be accessible */
1243 if ((void *)(ec->vm_stack + ec->vm_stack_size) == (void *)(prev_cfp)) {
1244 return 0;
1245 }
1246 return prev_cfp;
1247}
1248
1249static ID
1250prev_frame_callee(void)
1251{
1252 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1253 if (!prev_cfp) return 0;
1254 return frame_called_id(prev_cfp);
1255}
1256
1257static ID
1258prev_frame_func(void)
1259{
1260 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1261 if (!prev_cfp) return 0;
1262 return frame_func_id(prev_cfp);
1263}
1264
1271 ID
1272rb_frame_last_func(void)
1273{
1274 const rb_execution_context_t *ec = GET_EC();
1275 const rb_control_frame_t *cfp = ec->cfp;
1276 ID mid;
1277
1278 while (!(mid = frame_func_id(cfp)) &&
1279 (cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp),
1280 !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)));
1281 return mid;
1282}
1283
1284/*
1285 * call-seq:
1286 * append_features(mod) -> mod
1287 *
1288 * When this module is included in another, Ruby calls
1289 * #append_features in this module, passing it the receiving module
1290 * in _mod_. Ruby's default implementation is to add the constants,
1291 * methods, and module variables of this module to _mod_ if this
1292 * module has not already been added to _mod_ or one of its
1293 * ancestors. See also Module#include.
1294 */
1295
1296static VALUE
1297rb_mod_append_features(VALUE module, VALUE include)
1298{
1299 if (!CLASS_OR_MODULE_P(include)) {
1300 Check_Type(include, T_CLASS);
1301 }
1302 rb_include_module(include, module);
1303
1304 return module;
1305}
1306
1307/*
1308 * call-seq:
1309 * include(module, ...) -> self
1310 *
1311 * Invokes Module.append_features on each parameter in reverse order.
1312 */
1313
1314static VALUE
1315rb_mod_include(int argc, VALUE *argv, VALUE module)
1316{
1317 int i;
1318 ID id_append_features, id_included;
1319
1320 CONST_ID(id_append_features, "append_features");
1321 CONST_ID(id_included, "included");
1322
1323 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1324 for (i = 0; i < argc; i++)
1325 Check_Type(argv[i], T_MODULE);
1326 while (argc--) {
1327 rb_funcall(argv[argc], id_append_features, 1, module);
1328 rb_funcall(argv[argc], id_included, 1, module);
1329 }
1330 return module;
1331}
1332
1333/*
1334 * call-seq:
1335 * prepend_features(mod) -> mod
1336 *
1337 * When this module is prepended in another, Ruby calls
1338 * #prepend_features in this module, passing it the receiving module
1339 * in _mod_. Ruby's default implementation is to overlay the
1340 * constants, methods, and module variables of this module to _mod_
1341 * if this module has not already been added to _mod_ or one of its
1342 * ancestors. See also Module#prepend.
1343 */
1344
1345static VALUE
1346rb_mod_prepend_features(VALUE module, VALUE prepend)
1347{
1348 if (!CLASS_OR_MODULE_P(prepend)) {
1349 Check_Type(prepend, T_CLASS);
1350 }
1351 rb_prepend_module(prepend, module);
1352
1353 return module;
1354}
1355
1356/*
1357 * call-seq:
1358 * prepend(module, ...) -> self
1359 *
1360 * Invokes Module.prepend_features on each parameter in reverse order.
1361 */
1362
1363static VALUE
1364rb_mod_prepend(int argc, VALUE *argv, VALUE module)
1365{
1366 int i;
1367 ID id_prepend_features, id_prepended;
1368
1369 CONST_ID(id_prepend_features, "prepend_features");
1370 CONST_ID(id_prepended, "prepended");
1371
1372 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1373 for (i = 0; i < argc; i++)
1374 Check_Type(argv[i], T_MODULE);
1375 while (argc--) {
1376 rb_funcall(argv[argc], id_prepend_features, 1, module);
1377 rb_funcall(argv[argc], id_prepended, 1, module);
1378 }
1379 return module;
1380}
1381
1382static void
1383ensure_class_or_module(VALUE obj)
1384{
1385 if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
1386 rb_raise(rb_eTypeError,
1387 "wrong argument type %"PRIsVALUE" (expected Class or Module)",
1388 rb_obj_class(obj));
1389 }
1390}
1391
1392static VALUE
1393hidden_identity_hash_new(void)
1394{
1395 VALUE hash = rb_ident_hash_new();
1396
1397 RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1398 return hash;
1399}
1400
1401static VALUE
1402refinement_superclass(VALUE superclass)
1403{
1404 if (RB_TYPE_P(superclass, T_MODULE)) {
1405 /* FIXME: Should ancestors of superclass be used here? */
1406 return rb_include_class_new(RCLASS_ORIGIN(superclass), rb_cBasicObject);
1407 }
1408 else {
1409 return superclass;
1410 }
1411}
1412
1417void
1418rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module)
1419{
1420 VALUE iclass, c, superclass = klass;
1421
1422 ensure_class_or_module(klass);
1423 Check_Type(module, T_MODULE);
1424 if (NIL_P(CREF_REFINEMENTS(cref))) {
1425 CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new());
1426 }
1427 else {
1428 if (CREF_OMOD_SHARED(cref)) {
1429 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref)));
1430 CREF_OMOD_SHARED_UNSET(cref);
1431 }
1432 if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) {
1433 superclass = c;
1434 while (c && RB_TYPE_P(c, T_ICLASS)) {
1435 if (RBASIC(c)->klass == module) {
1436 /* already used refinement */
1437 return;
1438 }
1439 c = RCLASS_SUPER(c);
1440 }
1441 }
1442 }
1443 FL_SET(module, RMODULE_IS_OVERLAID);
1444 superclass = refinement_superclass(superclass);
1445 c = iclass = rb_include_class_new(module, superclass);
1446 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1447
1448 RCLASS_M_TBL(c) = RCLASS_M_TBL(module);
1449
1450 module = RCLASS_SUPER(module);
1451 while (module && module != klass) {
1452 FL_SET(module, RMODULE_IS_OVERLAID);
1453 c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
1454 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1455 module = RCLASS_SUPER(module);
1456 }
1457 rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass);
1458}
1459
1460static int
1461using_refinement(VALUE klass, VALUE module, VALUE arg)
1462{
1463 rb_cref_t *cref = (rb_cref_t *) arg;
1464
1465 rb_using_refinement(cref, klass, module);
1466 return ST_CONTINUE;
1467}
1468
1469static void
1470using_module_recursive(const rb_cref_t *cref, VALUE klass)
1471{
1472 ID id_refinements;
1473 VALUE super, module, refinements;
1474
1475 super = RCLASS_SUPER(klass);
1476 if (super) {
1477 using_module_recursive(cref, super);
1478 }
1479 switch (BUILTIN_TYPE(klass)) {
1480 case T_MODULE:
1481 module = klass;
1482 break;
1483
1484 case T_ICLASS:
1485 module = RBASIC(klass)->klass;
1486 break;
1487
1488 default:
1489 rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1490 rb_obj_classname(klass));
1491 break;
1492 }
1493 CONST_ID(id_refinements, "__refinements__");
1494 refinements = rb_attr_get(module, id_refinements);
1495 if (NIL_P(refinements)) return;
1496 rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1497}
1498
1503void
1504rb_using_module(const rb_cref_t *cref, VALUE module)
1505{
1506 Check_Type(module, T_MODULE);
1507 using_module_recursive(cref, module);
1508 rb_clear_method_cache_all();
1509}
1510
1512 VALUE
1513rb_refinement_module_get_refined_class(VALUE module)
1514{
1515 ID id_refined_class;
1516
1517 CONST_ID(id_refined_class, "__refined_class__");
1518 return rb_attr_get(module, id_refined_class);
1519}
1520
1521static void
1522add_activated_refinement(VALUE activated_refinements,
1523 VALUE klass, VALUE refinement)
1524{
1525 VALUE iclass, c, superclass = klass;
1526
1527 if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1528 superclass = c;
1529 while (c && RB_TYPE_P(c, T_ICLASS)) {
1530 if (RBASIC(c)->klass == refinement) {
1531 /* already used refinement */
1532 return;
1533 }
1534 c = RCLASS_SUPER(c);
1535 }
1536 }
1537 FL_SET(refinement, RMODULE_IS_OVERLAID);
1538 superclass = refinement_superclass(superclass);
1539 c = iclass = rb_include_class_new(refinement, superclass);
1540 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1541 refinement = RCLASS_SUPER(refinement);
1542 while (refinement && refinement != klass) {
1543 FL_SET(refinement, RMODULE_IS_OVERLAID);
1544 c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1545 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1546 refinement = RCLASS_SUPER(refinement);
1547 }
1548 rb_hash_aset(activated_refinements, klass, iclass);
1549}
1550
1551/*
1552 * call-seq:
1553 * refine(mod) { block } -> module
1554 *
1555 * Refine <i>mod</i> in the receiver.
1556 *
1557 * Returns a module, where refined methods are defined.
1558 */
1559
1560static VALUE
1561rb_mod_refine(VALUE module, VALUE klass)
1562{
1563 VALUE refinement;
1564 ID id_refinements, id_activated_refinements,
1565 id_refined_class, id_defined_at;
1566 VALUE refinements, activated_refinements;
1567 rb_thread_t *th = GET_THREAD();
1568 VALUE block_handler = rb_vm_frame_block_handler(th->ec->cfp);
1569
1570 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1571 rb_raise(rb_eArgError, "no block given");
1572 }
1573 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
1574 rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine");
1575 }
1576
1577 ensure_class_or_module(klass);
1578 CONST_ID(id_refinements, "__refinements__");
1579 refinements = rb_attr_get(module, id_refinements);
1580 if (NIL_P(refinements)) {
1581 refinements = hidden_identity_hash_new();
1582 rb_ivar_set(module, id_refinements, refinements);
1583 }
1584 CONST_ID(id_activated_refinements, "__activated_refinements__");
1585 activated_refinements = rb_attr_get(module, id_activated_refinements);
1586 if (NIL_P(activated_refinements)) {
1587 activated_refinements = hidden_identity_hash_new();
1588 rb_ivar_set(module, id_activated_refinements,
1589 activated_refinements);
1590 }
1591 refinement = rb_hash_lookup(refinements, klass);
1592 if (NIL_P(refinement)) {
1593 VALUE superclass = refinement_superclass(klass);
1594 refinement = rb_module_new();
1595 RCLASS_SET_SUPER(refinement, superclass);
1596 FL_SET(refinement, RMODULE_IS_REFINEMENT);
1597 CONST_ID(id_refined_class, "__refined_class__");
1598 rb_ivar_set(refinement, id_refined_class, klass);
1599 CONST_ID(id_defined_at, "__defined_at__");
1600 rb_ivar_set(refinement, id_defined_at, module);
1601 rb_hash_aset(refinements, klass, refinement);
1602 add_activated_refinement(activated_refinements, klass, refinement);
1603 }
1604 rb_yield_refine_block(refinement, activated_refinements);
1605 return refinement;
1606}
1607
1608static void
1609ignored_block(VALUE module, const char *klass)
1610{
1611 const char *anon = "";
1612 Check_Type(module, T_MODULE);
1613 if (!RTEST(rb_search_class_path(module))) {
1614 anon = ", maybe for Module.new";
1615 }
1616 rb_warn("%s""using doesn't call the given block""%s.", klass, anon);
1617}
1618
1619/*
1620 * call-seq:
1621 * using(module) -> self
1622 *
1623 * Import class refinements from <i>module</i> into the current class or
1624 * module definition.
1625 */
1626
1627static VALUE
1628mod_using(VALUE self, VALUE module)
1629{
1630 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1631
1632 if (prev_frame_func()) {
1633 rb_raise(rb_eRuntimeError,
1634 "Module#using is not permitted in methods");
1635 }
1636 if (prev_cfp && prev_cfp->self != self) {
1637 rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1638 }
1639 if (rb_block_given_p()) {
1640 ignored_block(module, "Module#");
1641 }
1642 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1643 return self;
1644}
1645
1646static int
1647used_modules_i(VALUE _, VALUE mod, VALUE ary)
1648{
1649 ID id_defined_at;
1650 CONST_ID(id_defined_at, "__defined_at__");
1651 while (FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1652 rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at));
1653 mod = RCLASS_SUPER(mod);
1654 }
1655 return ST_CONTINUE;
1656}
1657
1658/*
1659 * call-seq:
1660 * used_modules -> array
1661 *
1662 * Returns an array of all modules used in the current scope. The ordering
1663 * of modules in the resulting array is not defined.
1664 *
1665 * module A
1666 * refine Object do
1667 * end
1668 * end
1669 *
1670 * module B
1671 * refine Object do
1672 * end
1673 * end
1674 *
1675 * using A
1676 * using B
1677 * p Module.used_modules
1678 *
1679 * <em>produces:</em>
1680 *
1681 * [B, A]
1682 */
1683static VALUE
1684rb_mod_s_used_modules(VALUE _)
1685{
1686 const rb_cref_t *cref = rb_vm_cref();
1687 VALUE ary = rb_ary_new();
1688
1689 while (cref) {
1690 if (!NIL_P(CREF_REFINEMENTS(cref))) {
1691 rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary);
1692 }
1693 cref = CREF_NEXT(cref);
1694 }
1695
1696 return rb_funcall(ary, rb_intern("uniq"), 0);
1697}
1698
1709void
1710rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
1711{
1712 PASS_PASSED_BLOCK_HANDLER();
1713 rb_funcallv_kw(obj, idInitialize, argc, argv, RB_NO_KEYWORDS);
1714}
1715
1716void
1717rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
1718{
1719 PASS_PASSED_BLOCK_HANDLER();
1720 rb_funcallv_kw(obj, idInitialize, argc, argv, kw_splat);
1721}
1722
1729void
1730rb_extend_object(VALUE obj, VALUE module)
1731{
1732 rb_include_module(rb_singleton_class(obj), module);
1733}
1734
1735/*
1736 * call-seq:
1737 * extend_object(obj) -> obj
1738 *
1739 * Extends the specified object by adding this module's constants and
1740 * methods (which are added as singleton methods). This is the callback
1741 * method used by Object#extend.
1742 *
1743 * module Picky
1744 * def Picky.extend_object(o)
1745 * if String === o
1746 * puts "Can't add Picky to a String"
1747 * else
1748 * puts "Picky added to #{o.class}"
1749 * super
1750 * end
1751 * end
1752 * end
1753 * (s = Array.new).extend Picky # Call Object.extend
1754 * (s = "quick brown fox").extend Picky
1755 *
1756 * <em>produces:</em>
1757 *
1758 * Picky added to Array
1759 * Can't add Picky to a String
1760 */
1761
1762static VALUE
1763rb_mod_extend_object(VALUE mod, VALUE obj)
1764{
1765 rb_extend_object(obj, mod);
1766 return obj;
1767}
1768
1769/*
1770 * call-seq:
1771 * obj.extend(module, ...) -> obj
1772 *
1773 * Adds to _obj_ the instance methods from each module given as a
1774 * parameter.
1775 *
1776 * module Mod
1777 * def hello
1778 * "Hello from Mod.\n"
1779 * end
1780 * end
1781 *
1782 * class Klass
1783 * def hello
1784 * "Hello from Klass.\n"
1785 * end
1786 * end
1787 *
1788 * k = Klass.new
1789 * k.hello #=> "Hello from Klass.\n"
1790 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1791 * k.hello #=> "Hello from Mod.\n"
1792 */
1793
1794static VALUE
1795rb_obj_extend(int argc, VALUE *argv, VALUE obj)
1796{
1797 int i;
1798 ID id_extend_object, id_extended;
1799
1800 CONST_ID(id_extend_object, "extend_object");
1801 CONST_ID(id_extended, "extended");
1802
1803 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1804 for (i = 0; i < argc; i++)
1805 Check_Type(argv[i], T_MODULE);
1806 while (argc--) {
1807 rb_funcall(argv[argc], id_extend_object, 1, obj);
1808 rb_funcall(argv[argc], id_extended, 1, obj);
1809 }
1810 return obj;
1811}
1812
1813/*
1814 * call-seq:
1815 * include(module, ...) -> self
1816 *
1817 * Invokes Module.append_features on each parameter in turn.
1818 * Effectively adds the methods and constants in each module to the
1819 * receiver.
1820 */
1821
1822static VALUE
1823top_include(int argc, VALUE *argv, VALUE self)
1824{
1825 rb_thread_t *th = GET_THREAD();
1826
1827 if (th->top_wrapper) {
1828 rb_warning("main.include in the wrapped load is effective only in wrapper module");
1829 return rb_mod_include(argc, argv, th->top_wrapper);
1830 }
1831 return rb_mod_include(argc, argv, rb_cObject);
1832}
1833
1834/*
1835 * call-seq:
1836 * using(module) -> self
1837 *
1838 * Import class refinements from <i>module</i> into the scope where
1839 * #using is called.
1840 */
1841
1842static VALUE
1843top_using(VALUE self, VALUE module)
1844{
1845 const rb_cref_t *cref = rb_vm_cref();
1846 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1847
1848 if (CREF_NEXT(cref) || (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) {
1849 rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel");
1850 }
1851 if (rb_block_given_p()) {
1852 ignored_block(module, "main.");
1853 }
1854 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1855 return self;
1856}
1857
1858static const VALUE *
1859errinfo_place(const rb_execution_context_t *ec)
1860{
1861 const rb_control_frame_t *cfp = ec->cfp;
1862 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1863
1864 while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1865 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1866 if (cfp->iseq->body->type == ISEQ_TYPE_RESCUE) {
1867 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1868 }
1869 else if (cfp->iseq->body->type == ISEQ_TYPE_ENSURE &&
1870 !THROW_DATA_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR]) &&
1871 !FIXNUM_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR])) {
1872 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1873 }
1874 }
1875 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1876 }
1877 return 0;
1878}
1879
1880 VALUE
1881rb_ec_get_errinfo(const rb_execution_context_t *ec)
1882{
1883 const VALUE *ptr = errinfo_place(ec);
1884 if (ptr) {
1885 return *ptr;
1886 }
1887 else {
1888 return ec->errinfo;
1889 }
1890}
1891
1892static VALUE
1893get_errinfo(void)
1894{
1895 return get_ec_errinfo(GET_EC());
1896}
1897
1898static VALUE
1899errinfo_getter(ID id, VALUE *_)
1900{
1901 return get_errinfo();
1902}
1903
1910 VALUE
1911rb_errinfo(void)
1912{
1913 return GET_EC()->errinfo;
1914}
1915
1924void
1925rb_set_errinfo(VALUE err)
1926{
1927 if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1928 rb_raise(rb_eTypeError, "assigning non-exception to $!");
1929 }
1930 GET_EC()->errinfo = err;
1931}
1932
1933static VALUE
1934errat_getter(ID id, VALUE *_)
1935{
1936 VALUE err = get_errinfo();
1937 if (!NIL_P(err)) {
1938 return rb_get_backtrace(err);
1939 }
1940 else {
1941 return Qnil;
1942 }
1943}
1944
1945static void
1946errat_setter(VALUE val, ID id, VALUE *var)
1947{
1948 VALUE err = get_errinfo();
1949 if (NIL_P(err)) {
1950 rb_raise(rb_eArgError, "$! not set");
1951 }
1952 set_backtrace(err, val);
1953}
1954
1955/*
1956 * call-seq:
1957 * __method__ -> symbol
1958 *
1959 * Returns the name at the definition of the current method as a
1960 * Symbol.
1961 * If called outside of a method, it returns <code>nil</code>.
1962 *
1963 */
1964
1965static VALUE
1966rb_f_method_name(VALUE _)
1967{
1968 ID fname = prev_frame_func(); /* need *method* ID */
1969
1970 if (fname) {
1971 return ID2SYM(fname);
1972 }
1973 else {
1974 return Qnil;
1975 }
1976}
1977
1978/*
1979 * call-seq:
1980 * __callee__ -> symbol
1981 *
1982 * Returns the called name of the current method as a Symbol.
1983 * If called outside of a method, it returns <code>nil</code>.
1984 *
1985 */
1986
1987static VALUE
1988rb_f_callee_name(VALUE _)
1989{
1990 ID fname = prev_frame_callee(); /* need *callee* ID */
1991
1992 if (fname) {
1993 return ID2SYM(fname);
1994 }
1995 else {
1996 return Qnil;
1997 }
1998}
1999
2000/*
2001 * call-seq:
2002 * __dir__ -> string
2003 *
2004 * Returns the canonicalized absolute path of the directory of the file from
2005 * which this method is called. It means symlinks in the path is resolved.
2006 * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
2007 * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
2008 *
2009 */
2010static VALUE
2011f_current_dirname(VALUE _)
2012{
2013 VALUE base = rb_current_realfilepath();
2014 if (NIL_P(base)) {
2015 return Qnil;
2016 }
2017 base = rb_file_dirname(base);
2018 return base;
2019}
2020
2021/*
2022 * call-seq:
2023 * global_variables -> array
2024 *
2025 * Returns an array of the names of global variables. This includes
2026 * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>,
2027 * but does not include the numbered regexp global variables (<tt>$1</tt>,
2028 * <tt>$2</tt>, etc.).
2029 *
2030 * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
2031 */
2032
2033static VALUE
2034f_global_variables(VALUE _)
2035{
2036 return rb_f_global_variables();
2037}
2038
2039/*
2040 * call-seq:
2041 * trace_var(symbol, cmd ) -> nil
2042 * trace_var(symbol) {|val| block } -> nil
2043 *
2044 * Controls tracing of assignments to global variables. The parameter
2045 * +symbol+ identifies the variable (as either a string name or a
2046 * symbol identifier). _cmd_ (which may be a string or a
2047 * +Proc+ object) or block is executed whenever the variable
2048 * is assigned. The block or +Proc+ object receives the
2049 * variable's new value as a parameter. Also see
2050 * Kernel::untrace_var.
2051 *
2052 * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
2053 * $_ = "hello"
2054 * $_ = ' there'
2055 *
2056 * <em>produces:</em>
2057 *
2058 * $_ is now 'hello'
2059 * $_ is now ' there'
2060 */
2061
2062static VALUE
2063f_trace_var(int c, const VALUE *a, VALUE _)
2064{
2065 return rb_f_trace_var(c, a);
2066}
2067
2068/*
2069 * call-seq:
2070 * untrace_var(symbol [, cmd] ) -> array or nil
2071 *
2072 * Removes tracing for the specified command on the given global
2073 * variable and returns +nil+. If no command is specified,
2074 * removes all tracing for that variable and returns an array
2075 * containing the commands actually removed.
2076 */
2077
2078static VALUE
2079f_untrace_var(int c, const VALUE *a, VALUE _)
2080{
2081 return rb_f_untrace_var(c, a);
2082}
2083
2084void
2085Init_eval(void)
2086{
2087 rb_define_virtual_variable("$@", errat_getter, errat_setter);
2088 rb_define_virtual_variable("$!", errinfo_getter, 0);
2089
2090 rb_gvar_ractor_local("$@");
2091 rb_gvar_ractor_local("$!");
2092
2093 rb_define_global_function("raise", f_raise, -1);
2094 rb_define_global_function("fail", f_raise, -1);
2095
2096 rb_define_global_function("global_variables", f_global_variables, 0);
2097
2098 rb_define_global_function("__method__", rb_f_method_name, 0);
2099 rb_define_global_function("__callee__", rb_f_callee_name, 0);
2100 rb_define_global_function("__dir__", f_current_dirname, 0);
2101
2102 rb_define_method(rb_cModule, "include", rb_mod_include, -1);
2103 rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
2104
2105 rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
2106 rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
2107 rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
2108 rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
2109 rb_define_private_method(rb_cModule, "using", mod_using, 1);
2110 rb_define_singleton_method(rb_cModule, "used_modules",
2111 rb_mod_s_used_modules, 0);
2112 rb_undef_method(rb_cClass, "refine");
2113
2114 rb_undef_method(rb_cClass, "module_function");
2115
2116 Init_vm_eval();
2117 Init_eval_method();
2118
2119 rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
2120 rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
2121
2122 rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
2123 "include", top_include, -1);
2124 rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
2125 "using", top_using, 1);
2126
2127 rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
2128
2129 rb_define_global_function("trace_var", f_trace_var, -1);
2130 rb_define_global_function("untrace_var", f_untrace_var, -1);
2131
2132 rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered");
2133 rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");
2134
2135 id_signo = rb_intern_const("signo");
2136 id_status = rb_intern_const("status");
2137}
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1301
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:749
NORETURN
#define NORETURN(x)
Definition: attributes.h:152
UNREACHABLE
#define UNREACHABLE
Definition: assume.h:30
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Definition: assume.h:31
e_proc
VALUE e_proc(VALUE)
Definition: cont.c:1558
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
rb_define_private_method
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:660
rb_define_global_function
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition: cxxanyargs.hpp:678
mod
#define mod(x, y)
Definition: date_strftime.c:28
ptr
struct RIMemo * ptr
Definition: debug.c:88
numberof
#define numberof(array)
Definition: etc.c:649
rb_obj_call_init_kw
void rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
Definition: eval.c:1717
rb_eLocalJumpError
VALUE rb_eLocalJumpError
Definition: eval.c:48
rb_ec_get_errinfo
VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec)
Definition: eval.c:1881
Init_eval
void Init_eval(void)
Definition: eval.c:2085
raise_opt_cause
@ raise_opt_cause
Definition: eval.c:746
raise_max_opt
@ raise_max_opt
Definition: eval.c:746
rb_raise_jump
void rb_raise_jump(VALUE, VALUE)
Definition: eval.c:897
get_ec_errinfo
#define get_ec_errinfo(ec)
Definition: eval.c:522
rb_ec_clear_all_trace_func
void rb_ec_clear_all_trace_func(const rb_execution_context_t *ec)
Definition: vm_trace.c:283
exception_error
#define exception_error
Definition: eval.c:55
rb_f_raise
VALUE rb_f_raise(int argc, VALUE *argv)
Definition: eval.c:771
rb_ec_clear_current_thread_trace_func
void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec)
Definition: vm_trace.c:277
rb_vrescue2
VALUE rb_vrescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2, va_list args)
An equivalent of rescue clause.
Definition: eval.c:1006
rb_eSysStackError
VALUE rb_eSysStackError
Definition: eval.c:49
ruby_static_id_signo
ID ruby_static_id_signo
Definition: eval.c:51
CLASS_OR_MODULE_P
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:60
rb_keyword_given_p
int rb_keyword_given_p(void)
Definition: eval.c:948
id_cause
#define id_cause
Definition: eval.c:53
rb_eThreadError
VALUE rb_eThreadError
Definition: eval.c:953
rb_vm_cframe_keyword_p
int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp)
Definition: vm.c:120
ruby_static_id_status
ID ruby_static_id_status
Definition: eval.c:51
eval_error.c
warn_print_str
#define warn_print_str(x)
Definition: eval_error.c:23
unknown_longjmp_status
#define unknown_longjmp_status(status)
Definition: eval_error.c:446
eval_intern.h
rb_ec_set_raised
int rb_ec_set_raised(rb_execution_context_t *ec)
Definition: thread.c:2587
rb_ec_backtrace_object
VALUE rb_ec_backtrace_object(const rb_execution_context_t *ec)
Definition: vm_backtrace.c:770
INTERNAL_EXCEPTION_P
#define INTERNAL_EXCEPTION_P(exc)
Definition: eval_intern.h:198
EC_EXEC_TAG
#define EC_EXEC_TAG()
Definition: eval_intern.h:193
EC_PUSH_TAG
#define EC_PUSH_TAG(ec)
Definition: eval_intern.h:130
EC_JUMP_TAG
#define EC_JUMP_TAG(ec, st)
Definition: eval_intern.h:196
rb_vm_localjump_error
void rb_vm_localjump_error(const char *, VALUE, int)
Definition: vm.c:1711
EXIT_FAILURE
#define EXIT_FAILURE
Definition: eval_intern.h:32
rb_ec_raised_clear
#define rb_ec_raised_clear(ec)
Definition: eval_intern.h:273
EC_POP_TAG
#define EC_POP_TAG()
Definition: eval_intern.h:138
PASS_PASSED_BLOCK_HANDLER
#define PASS_PASSED_BLOCK_HANDLER()
Definition: eval_intern.h:23
rb_ec_reset_raised
int rb_ec_reset_raised(rb_execution_context_t *ec)
Definition: thread.c:2597
rb_vm_cref_replace_with_duplicated_cref
rb_cref_t * rb_vm_cref_replace_with_duplicated_cref(void)
Definition: vm.c:1631
rb_vm_cref
rb_cref_t * rb_vm_cref(void)
Definition: vm.c:1624
SAVE_ROOT_JMPBUF
#define SAVE_ROOT_JMPBUF(th, stmt)
Definition: eval_intern.h:120
eval_jump.c
RUBY_EVENT_RAISE
#define RUBY_EVENT_RAISE
Definition: event.h:37
RUBY_EVENT_C_RETURN
#define RUBY_EVENT_C_RETURN
Definition: event.h:36
UNLIKELY
#define UNLIKELY(x)
Definition: ffi_common.h:126
rb_file_dirname
VALUE rb_file_dirname(VALUE fname)
Definition: file.c:4732
FL_SINGLETON
#define FL_SINGLETON
Definition: fl_type.h:49
PRIsVALUE
#define PRIsVALUE
Definition: function.c:10
Init_heap
void Init_heap(void)
Definition: gc.c:3158
rb_objspace_call_finalizer
void rb_objspace_call_finalizer(rb_objspace_t *objspace)
Definition: gc.c:3771
STACK_UPPER
#define STACK_UPPER(x, a, b)
Definition: gc.h:92
ruby_run_node
#define ruby_run_node
Definition: goruby.c:3
ruby_options
#define ruby_options
Definition: goruby.c:2
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:962
rb_extend_object
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition: eval.c:1730
rb_prepend_module
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:1170
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1924
rb_include_class_new
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:923
rb_module_new
VALUE rb_module_new(void)
Definition: class.c:856
rb_class_modify_check
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:477
rb_frame_callee
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1233
rb_frame_this_func
ID rb_frame_this_func(void)
The original name of the current method.
Definition: eval.c:1216
rb_need_block
void rb_need_block(void)
Declares that the current method needs a block.
Definition: eval.c:961
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1777
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:935
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:2085
OBJ_FROZEN
#define OBJ_FROZEN
Definition: fl_type.h:136
OBJ_FREEZE
#define OBJ_FREEZE
Definition: fl_type.h:134
FL_SET
#define FL_SET
Definition: fl_type.h:128
FL_TEST
#define FL_TEST
Definition: fl_type.h:130
ruby_stop
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition: eval.c:327
ruby_exec_node
int ruby_exec_node(void *n)
Runs the given compiled source.
Definition: eval.c:380
ruby_setup
int ruby_setup(void)
Initializes the VM and builtin libraries.
Definition: eval.c:70
ruby_finalize
void ruby_finalize(void)
Runs the VM finalization processes.
Definition: eval.c:195
ruby_init_stack
void ruby_init_stack(volatile VALUE *)
ruby_process_options
void * ruby_process_options(int, char **)
Definition: ruby.c:2595
ruby_prog_init
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:2542
ruby_cleanup
int ruby_cleanup(volatile int ex)
Destructs the VM.
Definition: eval.c:213
ruby_sig_finalize
void ruby_sig_finalize(void)
Definition: signal.c:1496
rb_get_backtrace
VALUE rb_get_backtrace(VALUE exc)
Definition: error.c:1365
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_rescue2
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
Definition: eval.c:991
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
rb_eSystemExit
VALUE rb_eSystemExit
Definition: error.c:1050
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1054
rb_set_errinfo
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1925
rb_ident_hash_new
VALUE rb_ident_hash_new(void)
Definition: hash.c:4443
rb_interrupt
void rb_interrupt(void)
Raises an Interrupt exception.
Definition: eval.c:741
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:1057
EXIT_SUCCESS
#define EXIT_SUCCESS
Definition: error.c:52
rb_frozen_error_raise
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Definition: error.c:3234
rb_protect
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1105
rb_eFatal
VALUE rb_eFatal
Definition: error.c:1053
rb_eInterrupt
VALUE rb_eInterrupt
Definition: error.c:1051
rb_make_exception
VALUE rb_make_exception(int argc, const VALUE *argv)
Make an Exception object from the list of arguments in a manner similar to Kernel#raise.
Definition: eval.c:888
rb_exc_fatal
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition: eval.c:728
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1055
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:408
rb_exc_new
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Definition: error.c:1094
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
rb_eException
VALUE rb_eException
Definition: error.c:1049
rb_rescue
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
An equivalent of rescue clause.
Definition: eval.c:1080
rb_ensure
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1148
rb_errinfo
VALUE rb_errinfo(void)
The current exception in the current thread.
Definition: eval.c:1911
rb_jump_tag
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:921
ruby_static_id_cause
ID ruby_static_id_cause
Definition: error.c:1080
rb_eSystemCallError
VALUE rb_eSystemCallError
Definition: error.c:1076
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:439
rb_eSignal
VALUE rb_eSignal
Definition: error.c:1052
rb_cClass
VALUE rb_cClass
Class class.
Definition: object.c:51
rb_mKernel
VALUE rb_mKernel
Kernel module.
Definition: object.c:48
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
rb_obj_call_init
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Calls initialize method of obj with the given arguments.
Definition: eval.c:1710
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:245
rb_obj_dup
VALUE rb_obj_dup(VALUE)
Equivalent to Object#dup in Ruby.
Definition: object.c:467
rb_cBasicObject
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:47
rb_cModule
VALUE rb_cModule
Module class.
Definition: object.c:50
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:724
ruby_init
void ruby_init(void)
Calls ruby_setup() and check error.
Definition: eval.c:108
ruby_executable_node
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition: eval.c:345
rb_hash_foreach
void rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
Definition: hash.c:1498
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:2901
rb_hash_lookup
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:2072
rb_hash_dup
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:1579
THROW_DATA_P
#define THROW_DATA_P(err)
Definition: imemo.h:120
config.h
Thin wrapper to ruby/config.h.
ruby_debug
#define ruby_debug
Definition: error.h:69
rb_funcallv_kw
VALUE rb_funcallv_kw(VALUE, ID, int, const VALUE *, int)
Definition: vm_eval.c:1030
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1077
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
Definition: error.h:29
rb_exc_new3
#define rb_exc_new3
Definition: error.h:31
rb_error_arity
void rb_error_arity(int, int, int)
Definition: vm_insnhelper.c:442
rb_check_arity
#define rb_check_arity
Definition: error.h:34
ruby_default_signal
void ruby_default_signal(int)
Definition: signal.c:407
rb_exc_new_cstr
#define rb_exc_new_cstr(exc, str)
Definition: string.h:271
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2462
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1529
rb_f_trace_var
VALUE rb_f_trace_var(int, const VALUE *)
Definition: variable.c:610
rb_const_list
VALUE rb_const_list(void *)
Definition: variable.c:2840
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1234
rb_f_untrace_var
VALUE rb_f_untrace_var(int, const VALUE *)
Definition: variable.c:656
rb_ivar_defined
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1510
rb_mod_constants
VALUE rb_mod_constants(int, const VALUE *, VALUE)
Definition: variable.c:2872
rb_attr_get
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1242
rb_mod_const_of
void * rb_mod_const_of(VALUE, void *)
Definition: variable.c:2818
rb_mod_const_at
void * rb_mod_const_at(VALUE, void *)
Definition: variable.c:2801
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1493
rb_f_global_variables
VALUE rb_f_global_variables(void)
Definition: variable.c:811
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:619
ID2SYM
#define ID2SYM
Definition: symbol.h:44
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:785
CONST_ID
#define CONST_ID
Definition: symbol.h:47
vm.h
ruby_vm_destruct
int ruby_vm_destruct(ruby_vm_t *vm)
Definition: vm.c:2639
rb_call_inits
void rb_call_inits(void)
Definition: inits.c:21
inits.h
Internal header aggregating init functions.
Init_vm_eval
void Init_vm_eval(void)
Definition: vm_eval.c:2579
Init_eval_method
void Init_eval_method(void)
Definition: vm_method.c:2634
Init_vm_objects
void Init_vm_objects(void)
Definition: vm.c:3731
Init_BareVM
void Init_BareVM(void)
Definition: vm.c:3702
FIX2INT
#define FIX2INT
Definition: int.h:41
NUM2INT
#define NUM2INT
Definition: int.h:44
class.h
Internal header for Class.
RCLASS_ORIGIN
#define RCLASS_ORIGIN(c)
Definition: class.h:87
RCLASS_M_TBL
#define RCLASS_M_TBL(c)
Definition: class.h:80
RCLASS_REFINED_CLASS
#define RCLASS_REFINED_CLASS(c)
Definition: class.h:88
id_status
#define id_status
Definition: eval.h:17
id_signo
#define id_signo
Definition: eval.h:16
gc.h
Internal header for GC.
hash.h
Internal header for Hash.
io.h
Internal header for IO.
object.h
Internal header for Object.
thread.h
Internal header for Thread.
rb_gvar_ractor_local
void rb_gvar_ractor_local(const char *name)
Definition: variable.c:359
rb_search_class_path
VALUE rb_search_class_path(VALUE)
Definition: variable.c:191
RUBY_DTRACE_HOOK
#define RUBY_DTRACE_HOOK(name, arg)
Definition: vm.h:122
rb_vm_top_self
VALUE rb_vm_top_self(void)
Definition: vm.c:3752
rb_yield_refine_block
VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements)
Definition: vm_eval.c:1980
rb_source_location_cstr
const char * rb_source_location_cstr(int *pline)
Definition: vm.c:1616
rb_current_realfilepath
VALUE rb_current_realfilepath(void)
Definition: vm_eval.c:2569
internal.h
rb_vm_encoded_insn_data_table_init
void rb_vm_encoded_insn_data_table_init(void)
Definition: iseq.c:3139
iseq.h
INT2FIX
#define INT2FIX
Definition: long.h:48
MEMCPY
#define MEMCPY(p1, p2, type, n)
Definition: memory.h:129
rb_clear_method_cache_all
void rb_clear_method_cache_all(void)
Definition: vm_method.c:280
mjit.h
ruby::backward::cxxanyargs::rb_define_virtual_variable
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
Definition: cxxanyargs.hpp:73
TRUE
#define TRUE
Definition: nkf.h:175
FALSE
#define FALSE
Definition: nkf.h:174
probes.h
probes_helper.h
rb_ractor_terminate_all
void rb_ractor_terminate_all(void)
Definition: ractor.c:1946
ractor_core.h
RBASIC
#define RBASIC(obj)
Definition: rbasic.h:34
RMODULE_IS_REFINEMENT
#define RMODULE_IS_REFINEMENT
Definition: rclass.h:28
RCLASS_SUPER
#define RCLASS_SUPER
Definition: rclass.h:33
RMODULE_IS_OVERLAID
#define RMODULE_IS_OVERLAID
Definition: rclass.h:27
NULL
#define NULL
Definition: regenc.h:69
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Definition: rhash.h:51
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:308
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
ATOMIC_VALUE_EXCHANGE
#define ATOMIC_VALUE_EXCHANGE(var, val)
Definition: ruby_atomic.h:23
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Definition: scan_args.h:46
scheduler.h
Internal header for Scheduler.
rb_scheduler_set
VALUE rb_scheduler_set(VALUE scheduler)
Definition: scheduler.c:73
rb_vm_trap_exit
void rb_vm_trap_exit(rb_vm_t *vm)
Definition: signal.c:1077
Qundef
#define Qundef
Definition: special_consts.h:53
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Definition: special_consts.h:47
Qtrue
#define Qtrue
Definition: special_consts.h:52
RTEST
#define RTEST
Definition: special_consts.h:42
Qnil
#define Qnil
Definition: special_consts.h:51
Qfalse
#define Qfalse
Definition: special_consts.h:50
NIL_P
#define NIL_P
Definition: special_consts.h:46
FIXNUM_P
#define FIXNUM_P
Definition: special_consts.h:44
rb_sprintf
VALUE rb_sprintf(const char *,...)
Definition: sprintf.c:1203
ST_CONTINUE
@ ST_CONTINUE
Definition: st.h:99
_
#define _(args)
Definition: stdarg.h:31
file
Definition: gzappend.c:170
rb_callable_method_entry_struct
Definition: method.h:62
rb_callable_method_entry_struct::called_id
ID called_id
Definition: method.h:66
rb_callable_method_entry_struct::owner
const VALUE owner
Definition: method.h:67
rb_callable_method_entry_struct::def
struct rb_method_definition_struct *const def
Definition: method.h:65
rb_control_frame_struct
Definition: vm_core.h:769
rb_control_frame_struct::ep
const VALUE * ep
Definition: vm_core.h:774
rb_control_frame_struct::self
VALUE self
Definition: vm_core.h:773
rb_control_frame_struct::iseq
const rb_iseq_t * iseq
Definition: vm_core.h:772
rb_cref_struct
CREF (Class REFerence)
Definition: method.h:44
rb_ensure_entry::e_proc
VALUE(* e_proc)(VALUE)
Definition: vm_core.h:835
rb_ensure_entry::marker
VALUE marker
Definition: vm_core.h:834
rb_ensure_entry::data2
VALUE data2
Definition: vm_core.h:836
rb_ensure_list
Definition: vm_core.h:839
rb_ensure_list::next
struct rb_ensure_list * next
Definition: vm_core.h:840
rb_ensure_list::entry
struct rb_ensure_entry entry
Definition: vm_core.h:841
rb_execution_context_struct
Definition: vm_core.h:854
rb_execution_context_struct::vm_stack
VALUE * vm_stack
Definition: vm_core.h:856
rb_execution_context_struct::errinfo
VALUE errinfo
Definition: vm_core.h:889
rb_execution_context_struct::vm_stack_size
size_t vm_stack_size
Definition: vm_core.h:857
rb_execution_context_struct::ensure_list
rb_ensure_list_t * ensure_list
Definition: vm_core.h:883
rb_execution_context_struct::protect_tag
struct rb_vm_protect_tag * protect_tag
Definition: vm_core.h:861
rb_execution_context_struct::cfp
rb_control_frame_t * cfp
Definition: vm_core.h:858
rb_iseq_constant_body::type
enum rb_iseq_constant_body::iseq_type type
rb_iseq_struct
Definition: vm_core.h:444
rb_iseq_struct::body
struct rb_iseq_constant_body * body
Definition: vm_core.h:448
rb_method_definition_struct::original_id
ID original_id
Definition: method.h:189
rb_thread_struct
Definition: vm_core.h:935
rb_thread_struct::ec
rb_execution_context_t * ec
Definition: vm_core.h:941
rb_thread_struct::vm
rb_vm_t * vm
Definition: vm_core.h:939
rb_thread_struct::top_wrapper
VALUE top_wrapper
Definition: vm_core.h:950
rb_vm_protect_tag
Definition: vm_core.h:822
rb_vm_protect_tag::prev
struct rb_vm_protect_tag * prev
Definition: vm_core.h:823
state
Definition: blast.c:41
rb_threadptr_check_signal
void rb_threadptr_check_signal(rb_thread_t *mth)
Definition: thread.c:4598
rb_threadptr_unlock_all_locking_mutexes
void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
Definition: thread.c:567
rb_thread_stop_timer_thread
void rb_thread_stop_timer_thread(void)
Definition: thread.c:4688
rb_threadptr_interrupt
void rb_threadptr_interrupt(rb_thread_t *th)
Definition: thread.c:508
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39
T_MODULE
#define T_MODULE
Definition: value_type.h:69
T_ICLASS
#define T_ICLASS
Definition: value_type.h:65
T_HASH
#define T_HASH
Definition: value_type.h:64
T_OBJECT
#define T_OBJECT
Definition: value_type.h:74
T_CLASS
#define T_CLASS
Definition: value_type.h:57
BUILTIN_TYPE
#define BUILTIN_TYPE
Definition: value_type.h:84
rb_iseq_eval_main
VALUE rb_iseq_eval_main(const rb_iseq_t *iseq)
Definition: vm.c:2413
rb_vm_frame_block_handler
VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
Definition: vm.c:126
rb_vm_rewind_cfp
void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp)
Definition: vm.c:639
vm_core.h
THREAD_KILLED
@ THREAD_KILLED
Definition: vm_core.h:795
TAG_RAISE
#define TAG_RAISE
Definition: vm_core.h:204
rb_vm_frame_method_entry
const rb_callable_method_entry_t * rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
Definition: vm_insnhelper.c:667
TAG_NONE
#define TAG_NONE
Definition: vm_core.h:198
rb_vm_pop_frame
void rb_vm_pop_frame(rb_execution_context_t *ec)
Definition: vm_insnhelper.c:419
VM_ENV_INDEX_LAST_LVAR
#define VM_ENV_INDEX_LAST_LVAR
Definition: vm_core.h:1213
TAG_RETRY
#define TAG_RETRY
Definition: vm_core.h:202
ruby_tag_type
ruby_tag_type
Definition: vm_core.h:185
rb_vm_register_special_exception
#define rb_vm_register_special_exception(sp, e, m)
Definition: vm_core.h:1720
block_handler_type_iseq
@ block_handler_type_iseq
Definition: vm_core.h:747
ruby_error_stackfatal
@ ruby_error_stackfatal
Definition: vm_core.h:498
ruby_error_reenter
@ ruby_error_reenter
Definition: vm_core.h:495
EXEC_EVENT_HOOK
#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_)
Definition: vm_core.h:2001
rb_jmpbuf_t
void * rb_jmpbuf_t[5]
Definition: vm_core.h:801
TAG_FATAL
#define TAG_FATAL
Definition: vm_core.h:206
RUBY_VM_CHECK_INTS
#define RUBY_VM_CHECK_INTS(ec)
Definition: vm_core.h:1921
RUBY_VM_VALID_CONTROL_FRAME_P
#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp)
Definition: vm_core.h:1395
VM_BLOCK_HANDLER_NONE
#define VM_BLOCK_HANDLER_NONE
Definition: vm_core.h:1299
RUBY_VM_PREVIOUS_CONTROL_FRAME
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:1392
err
int err
Definition: win32.c:142