1 /**********************************************************************
6 created at: Thu Jun 10 14:22:17 JST 1993
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
12 **********************************************************************/
14 #include "ruby/internal/config.h"
16 #ifdef HAVE_SYS_PRCTL_H
17 #include <sys/prctl.h>
20 #include "eval_intern.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"
36 #include "probes_helper.h"
39 #include "ractor_core.h"
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
);
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
);
48 VALUE rb_eLocalJumpError
;
49 VALUE rb_eSysStackError
;
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
55 #define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
57 #include "eval_error.c"
58 #include "eval_jump.c"
60 #define CLASS_OR_MODULE_P(obj) \
61 (!SPECIAL_CONST_P(obj) && \
62 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
67 enum ruby_tag_type state
;
72 ruby_init_stack((void *)&state
);
75 * Disable THP early before mallocs happen because we want this to
76 * affect as many future pages as possible for CoW-friendliness
78 #if defined(__linux__) && defined(PR_SET_THP_DISABLE)
79 prctl(PR_SET_THP_DISABLE
, 1, 0, 0, 0);
83 rb_vm_encoded_insn_data_table_init();
86 EC_PUSH_TAG(GET_EC());
87 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
90 GET_VM()->running
= 1;
100 int state
= ruby_setup();
102 if (RTEST(ruby_debug
))
103 error_print(GET_EC());
109 ruby_options(int argc
, char **argv
)
111 rb_execution_context_t
*ec
= GET_EC();
112 enum ruby_tag_type state
;
113 void *volatile iseq
= 0;
115 ruby_init_stack((void *)&iseq
);
117 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
118 SAVE_ROOT_JMPBUF(GET_THREAD(), iseq
= ruby_process_options(argc
, argv
));
121 rb_ec_clear_current_thread_trace_func(ec
);
122 state
= error_handle(ec
, state
);
123 iseq
= (void *)INT2FIX(state
);
130 rb_ec_fiber_scheduler_finalize(rb_execution_context_t
*ec
)
132 enum ruby_tag_type state
;
135 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
136 rb_fiber_scheduler_set(Qnil
);
139 state
= error_handle(ec
, state
);
145 rb_ec_teardown(rb_execution_context_t
*ec
)
147 // If the user code defined a scheduler for the top level thread, run it:
148 rb_ec_fiber_scheduler_finalize(ec
);
151 if (EC_EXEC_TAG() == TAG_NONE
) {
152 rb_vm_trap_exit(rb_ec_vm_ptr(ec
));
155 rb_ec_exec_end_proc(ec
);
156 rb_ec_clear_all_trace_func(ec
);
160 rb_ec_finalize(rb_execution_context_t
*ec
)
164 rb_objspace_call_finalizer(rb_ec_vm_ptr(ec
)->objspace
);
170 rb_execution_context_t
*ec
= GET_EC();
178 return rb_ec_cleanup(GET_EC(), ex
);
182 rb_ec_cleanup(rb_execution_context_t
*ec
, int ex0
)
185 volatile VALUE errs
[2] = { Qundef
, Qundef
};
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
;
193 rb_threadptr_interrupt(th
);
194 rb_threadptr_check_signal(th
);
197 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
198 SAVE_ROOT_JMPBUF(th
, { RUBY_VM_CHECK_INTS(ec
); });
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)]);
205 SAVE_ROOT_JMPBUF(th
, rb_ec_teardown(ec
));
208 /* protect from Thread#raise */
209 th
->status
= THREAD_KILLED
;
211 errs
[0] = ec
->errinfo
;
212 SAVE_ROOT_JMPBUF(th
, rb_ractor_terminate_all());
220 if (ex
== 0) ex
= state
;
222 ec
->errinfo
= errs
[1];
223 sysex
= error_handle(ec
, ex
);
226 for (nerr
= 0; nerr
< numberof(errs
); ++nerr
) {
227 VALUE err
= ATOMIC_VALUE_EXCHANGE(errs
[nerr
], Qnil
);
230 if (!RTEST(err
)) continue;
232 /* ec->errinfo contains a NODE while break'ing */
233 if (THROW_DATA_P(err
)) continue;
235 if (rb_obj_is_kind_of(err
, rb_eSystemExit
)) {
236 sysex
= sysexit_status(err
);
239 else if (rb_obj_is_kind_of(err
, rb_eSignal
)) {
240 VALUE sig
= rb_ivar_get(err
, id_signo
);
241 state
= NUM2INT(sig
);
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
);
249 else if (sysex
== EXIT_SUCCESS
) {
250 sysex
= EXIT_FAILURE
;
254 mjit_finish(true); // We still need ISeqs here.
258 /* unlock again if finalizer took mutexes. */
259 rb_threadptr_unlock_all_locking_mutexes(th
);
263 rb_thread_stop_timer_thread();
264 ruby_vm_destruct(th
->vm
);
265 if (state
) ruby_default_signal(state
);
271 rb_ec_exec_node(rb_execution_context_t
*ec
, void *n
)
274 rb_iseq_t
*iseq
= (rb_iseq_t
*)n
;
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
);
291 exit(ruby_cleanup(ex
));
295 ruby_executable_node(void *n
, int *status
)
301 case Qtrue
: s
= EXIT_SUCCESS
; break;
302 case Qfalse
: s
= EXIT_FAILURE
; break;
304 if (!FIXNUM_P(v
)) return TRUE
;
307 if (status
) *status
= s
;
312 ruby_run_node(void *n
)
314 rb_execution_context_t
*ec
= GET_EC();
316 if (!ruby_executable_node(n
, &status
)) {
317 rb_ec_cleanup(ec
, 0);
320 ruby_init_stack((void *)&status
);
321 return rb_ec_cleanup(ec
, rb_ec_exec_node(ec
, n
));
325 ruby_exec_node(void *n
)
327 ruby_init_stack((void *)&n
);
328 return rb_ec_exec_node(GET_EC(), n
);
333 * Module.nesting -> array
335 * Returns the list of +Modules+ nested at the point of call.
339 * $a = Module.nesting
342 * $a #=> [M1::M2, M1]
343 * $a[0].name #=> "M1::M2"
347 rb_mod_nesting(VALUE _
)
349 VALUE ary
= rb_ary_new();
350 const rb_cref_t
*cref
= rb_vm_cref();
352 while (cref
&& CREF_NEXT(cref
)) {
353 VALUE klass
= CREF_CLASS(cref
);
354 if (!CREF_PUSHED_BY_EVAL(cref
) &&
356 rb_ary_push(ary
, klass
);
358 cref
= CREF_NEXT(cref
);
365 * Module.constants -> array
366 * Module.constants(inherited) -> array
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.
373 * Module.constants.first(4)
374 * # => [:ARGF, :ARGV, :ArgumentError, :Array]
376 * Module.constants.include?(:SEEK_SET) # => false
379 * Module.constants.include?(:SEEK_SET) # => true
382 * The second form calls the instance method +constants+.
386 rb_mod_s_constants(int argc
, VALUE
*argv
, VALUE mod
)
388 const rb_cref_t
*cref
= rb_vm_cref();
393 if (argc
> 0 || mod
!= rb_cModule
) {
394 return rb_mod_constants(argc
, argv
, mod
);
398 klass
= CREF_CLASS(cref
);
399 if (!CREF_PUSHED_BY_EVAL(cref
) &&
401 data
= rb_mod_const_at(CREF_CLASS(cref
), data
);
406 cref
= CREF_NEXT(cref
);
410 data
= rb_mod_const_of(cbase
, data
);
412 return rb_const_list(data
);
416 * Asserts that \a klass is not a frozen class.
417 * \param[in] klass a \c Module object
418 * \exception RuntimeError if \a klass is not a class or frozen.
422 rb_class_modify_check(VALUE klass
)
424 if (SPECIAL_CONST_P(klass
)) {
425 Check_Type(klass
, T_CLASS
);
427 if (RB_TYPE_P(klass
, T_MODULE
)) {
428 rb_module_set_initialized(klass
);
430 if (OBJ_FROZEN(klass
)) {
433 if (FL_TEST(klass
, FL_SINGLETON
)) {
435 klass
= rb_ivar_get(klass
, id__attached__
);
436 if (!SPECIAL_CONST_P(klass
)) {
437 switch (BUILTIN_TYPE(klass
)) {
451 switch (BUILTIN_TYPE(klass
)) {
460 Check_Type(klass
, T_CLASS
);
464 rb_frozen_error_raise(klass
, "can't modify frozen %s: %"PRIsVALUE
, desc
, klass
);
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)
473 exc_setup_cause(VALUE exc
, VALUE cause
)
478 CONST_ID(id_true_cause
, "true_cause");
480 cause
= rb_attr_get(rb_eFatal
, id_true_cause
);
482 cause
= rb_exc_new_cstr(rb_eFatal
, "because using such Ruby");
483 rb_ivar_set(cause
, id_cause
, INT2FIX(42)); /* the answer */
485 rb_ivar_set(rb_eFatal
, id_true_cause
, cause
);
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
);
499 exc_setup_message(const rb_execution_context_t
*ec
, VALUE mesg
, VALUE
*cause
)
506 if (INTERNAL_EXCEPTION_P(mesg
)) EC_JUMP_TAG(ec
, TAG_FATAL
);
510 mesg
= rb_exc_new(rb_eRuntimeError
, 0, 0);
514 if (*cause
== Qundef
) {
519 else if (!rb_ivar_defined(mesg
, id_cause
)) {
520 *cause
= get_ec_errinfo(ec
);
526 else if (!NIL_P(*cause
) && !rb_obj_is_kind_of(*cause
, rb_eException
)) {
527 rb_raise(rb_eTypeError
, "exception object expected");
530 if (!nocircular
&& !NIL_P(*cause
) && *cause
!= Qundef
&& *cause
!= mesg
) {
532 while (!NIL_P(c
= rb_attr_get(c
, id_cause
))) {
534 rb_raise(rb_eArgError
, "circular causes");
542 setup_exception(rb_execution_context_t
*ec
, int tag
, volatile VALUE mesg
, VALUE cause
)
546 const char *file
= rb_source_location_cstr(&line
);
547 const char *const volatile file0
= file
;
549 if ((file
&& !NIL_P(mesg
)) || (cause
!= Qundef
)) {
550 volatile int state
= 0;
553 if (EC_EXEC_TAG() == TAG_NONE
&& !(state
= rb_ec_set_raised(ec
))) {
554 VALUE bt
= rb_get_backtrace(mesg
);
555 if (!NIL_P(bt
) || cause
== Qundef
) {
556 if (OBJ_FROZEN(mesg
)) {
557 mesg
= rb_obj_dup(mesg
);
560 if (cause
!= Qundef
&& !THROW_DATA_P(cause
)) {
561 exc_setup_cause(mesg
, cause
);
564 VALUE at
= rb_ec_backtrace_object(ec
);
565 rb_ivar_set(mesg
, idBt_locations
, at
);
566 set_backtrace(mesg
, at
);
568 rb_ec_reset_raised(ec
);
572 if (state
) goto fatal
;
579 if (RTEST(ruby_debug
) && !NIL_P(e
= ec
->errinfo
) &&
580 !rb_obj_is_kind_of(e
, rb_eSystemExit
)) {
581 enum ruby_tag_type state
;
585 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
587 e
= rb_obj_as_string(mesg
);
590 e
= rb_sprintf("Exception `%"PRIsVALUE
"' at %s:%d - %"PRIsVALUE
"\n",
591 rb_obj_class(mesg
), file
, line
, e
);
594 e
= rb_sprintf("Exception `%"PRIsVALUE
"' at %s - %"PRIsVALUE
"\n",
595 rb_obj_class(mesg
), file
, e
);
598 e
= rb_sprintf("Exception `%"PRIsVALUE
"' - %"PRIsVALUE
"\n",
599 rb_obj_class(mesg
), e
);
604 if (state
== TAG_FATAL
&& ec
->errinfo
== exception_error
) {
608 rb_ec_reset_raised(ec
);
609 EC_JUMP_TAG(ec
, state
);
613 if (rb_ec_set_raised(ec
)) {
617 if (tag
!= TAG_FATAL
) {
618 RUBY_DTRACE_HOOK(RAISE
, rb_obj_classname(ec
->errinfo
));
619 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_RAISE
, ec
->cfp
->self
, 0, 0, 0, mesg
);
624 ec
->errinfo
= exception_error
;
625 rb_ec_reset_raised(ec
);
626 EC_JUMP_TAG(ec
, TAG_FATAL
);
631 rb_ec_setup_exception(const rb_execution_context_t
*ec
, VALUE mesg
, VALUE cause
)
633 if (cause
== Qundef
) {
634 cause
= get_ec_errinfo(ec
);
637 rb_ivar_set(mesg
, id_cause
, cause
);
642 rb_longjmp(rb_execution_context_t
*ec
, int tag
, volatile VALUE mesg
, VALUE cause
)
644 mesg
= exc_setup_message(ec
, mesg
, &cause
);
645 setup_exception(ec
, tag
, mesg
, cause
);
646 rb_ec_raised_clear(ec
);
647 EC_JUMP_TAG(ec
, tag
);
650 static VALUE
make_exception(int argc
, const VALUE
*argv
, int isstr
);
652 NORETURN(static void rb_exc_exception(VALUE mesg
, int tag
, VALUE cause
));
655 rb_exc_exception(VALUE mesg
, int tag
, VALUE cause
)
658 mesg
= make_exception(1, &mesg
, FALSE
);
660 rb_longjmp(GET_EC(), tag
, mesg
, cause
);
664 * Raises an exception in the current thread.
665 * \param[in] mesg an Exception class or an \c Exception object.
666 * \exception always raises an instance of the given exception class or
667 * the given \c Exception object.
671 rb_exc_raise(VALUE mesg
)
673 rb_exc_exception(mesg
, TAG_RAISE
, Qundef
);
677 * Raises a fatal error in the current thread.
679 * Same as rb_exc_raise() but raises a fatal error, which Ruby codes
684 rb_exc_fatal(VALUE mesg
)
686 rb_exc_exception(mesg
, TAG_FATAL
, Qnil
);
692 rb_exc_raise(rb_exc_new(rb_eInterrupt
, 0, 0));
695 enum {raise_opt_cause
, raise_max_opt
}; /*< \private */
698 extract_raise_opts(int argc
, const VALUE
*argv
, VALUE
*opts
)
702 VALUE opt
= argv
[argc
-1];
703 if (RB_TYPE_P(opt
, T_HASH
)) {
704 if (!RHASH_EMPTY_P(opt
)) {
706 CONST_ID(keywords
[0], "cause");
707 rb_get_kwargs(opt
, keywords
, 0, -1-raise_max_opt
, opts
);
708 if (RHASH_EMPTY_P(opt
)) --argc
;
713 for (i
= 0; i
< raise_max_opt
; ++i
) {
720 rb_f_raise(int argc
, VALUE
*argv
)
723 VALUE opts
[raise_max_opt
], *const cause
= &opts
[raise_opt_cause
];
725 argc
= extract_raise_opts(argc
, argv
, opts
);
727 if (*cause
!= Qundef
) {
728 rb_raise(rb_eArgError
, "only cause is given with no arguments");
736 rb_raise_jump(rb_make_exception(argc
, argv
), *cause
);
738 UNREACHABLE_RETURN(Qnil
);
744 * raise(string, cause: $!)
745 * raise(exception [, string [, array]], cause: $!)
747 * fail(string, cause: $!)
748 * fail(exception [, string [, array]], cause: $!)
750 * With no arguments, raises the exception in <code>$!</code> or raises
751 * a RuntimeError if <code>$!</code> is +nil+. With a single +String+
752 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
753 * the first parameter should be an +Exception+ class (or another
754 * object that returns an +Exception+ object when sent an +exception+
755 * message). The optional second parameter sets the message associated with
756 * the exception (accessible via Exception#message), and the third parameter
757 * is an array of callback information (accessible via Exception#backtrace).
758 * The +cause+ of the generated exception (accessible via Exception#cause)
759 * is automatically set to the "current" exception (<code>$!</code>), if any.
760 * An alternative value, either an +Exception+ object or +nil+, can be
761 * specified via the +:cause+ argument.
763 * Exceptions are caught by the +rescue+ clause of
764 * <code>begin...end</code> blocks.
766 * raise "Failed to create socket"
767 * raise ArgumentError, "No parameters", caller
771 f_raise(int c
, VALUE
*v
, VALUE _
)
773 return rb_f_raise(c
, v
);
777 make_exception(int argc
, const VALUE
*argv
, int isstr
)
787 if (isstr
&&! NIL_P(exc
)) {
788 mesg
= rb_check_string_type(exc
);
790 return rb_exc_new3(rb_eRuntimeError
, mesg
);
798 rb_error_arity(argc
, 0, 3);
801 mesg
= rb_check_funcall(argv
[0], idException
, argc
!= 1, &argv
[1]);
803 if (mesg
== Qundef
) {
804 rb_raise(rb_eTypeError
, "exception class/object expected");
806 if (!rb_obj_is_kind_of(mesg
, rb_eException
)) {
807 rb_raise(rb_eTypeError
, "exception object expected");
810 set_backtrace(mesg
, argv
[2]);
817 rb_make_exception(int argc
, const VALUE
*argv
)
819 return make_exception(argc
, argv
, TRUE
);
825 rb_raise_jump(VALUE mesg
, VALUE cause
)
827 rb_execution_context_t
*ec
= GET_EC();
828 const rb_control_frame_t
*cfp
= ec
->cfp
;
829 const rb_callable_method_entry_t
*me
= rb_vm_frame_method_entry(cfp
);
830 VALUE klass
= me
->owner
;
831 VALUE self
= cfp
->self
;
832 ID mid
= me
->called_id
;
835 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_C_RETURN
, self
, me
->def
->original_id
, mid
, klass
, Qnil
);
837 rb_longjmp(ec
, TAG_RAISE
, mesg
, cause
);
843 if (UNLIKELY(tag
< TAG_RETURN
|| tag
> TAG_FATAL
)) {
844 unknown_longjmp_status(tag
);
846 EC_JUMP_TAG(GET_EC(), tag
);
850 rb_block_given_p(void)
852 if (rb_vm_frame_block_handler(GET_EC()->cfp
) == VM_BLOCK_HANDLER_NONE
) {
860 int rb_vm_cframe_keyword_p(const rb_control_frame_t
*cfp
);
863 rb_keyword_given_p(void)
865 return rb_vm_cframe_keyword_p(GET_EC()->cfp
);
868 VALUE rb_eThreadError
;
873 if (!rb_block_given_p()) {
874 rb_vm_localjump_error("no block given", Qnil
, 0);
879 rb_rescue2(VALUE (* b_proc
) (VALUE
), VALUE data1
,
880 VALUE (* r_proc
) (VALUE
, VALUE
), VALUE data2
, ...)
884 VALUE ret
= rb_vrescue2(b_proc
, data1
, r_proc
, data2
, ap
);
890 rb_vrescue2(VALUE (* b_proc
) (VALUE
), VALUE data1
,
891 VALUE (* r_proc
) (VALUE
, VALUE
), VALUE data2
,
894 enum ruby_tag_type state
;
895 rb_execution_context_t
* volatile ec
= GET_EC();
896 rb_control_frame_t
*volatile cfp
= ec
->cfp
;
897 volatile VALUE result
= Qfalse
;
898 volatile VALUE e_info
= ec
->errinfo
;
901 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
903 result
= (*b_proc
) (data1
);
906 /* escape from r_proc */
907 if (state
== TAG_RETRY
) {
915 rb_vm_rewind_cfp(ec
, cfp
);
917 if (state
== TAG_RAISE
) {
923 /* reuses args when raised again after retrying in r_proc */
925 while ((eclass
= va_arg(ap
, VALUE
)) != 0) {
926 if (rb_obj_is_kind_of(ec
->errinfo
, eclass
)) {
936 result
= (*r_proc
) (data2
, ec
->errinfo
);
938 ec
->errinfo
= e_info
;
944 EC_JUMP_TAG(ec
, state
);
950 rb_rescue(VALUE (* b_proc
)(VALUE
), VALUE data1
,
951 VALUE (* r_proc
)(VALUE
, VALUE
), VALUE data2
)
953 return rb_rescue2(b_proc
, data1
, r_proc
, data2
, rb_eStandardError
,
958 rb_protect(VALUE (* proc
) (VALUE
), VALUE data
, int *pstate
)
960 volatile VALUE result
= Qnil
;
961 volatile enum ruby_tag_type state
;
962 rb_execution_context_t
* volatile ec
= GET_EC();
963 rb_control_frame_t
*volatile cfp
= ec
->cfp
;
966 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
967 SAVE_ROOT_JMPBUF(rb_ec_thread_ptr(ec
), result
= (*proc
) (data
));
970 rb_vm_rewind_cfp(ec
, cfp
);
974 if (pstate
!= NULL
) *pstate
= state
;
979 rb_ensure(VALUE (*b_proc
)(VALUE
), VALUE data1
, VALUE (*e_proc
)(VALUE
), VALUE data2
)
982 volatile VALUE result
= Qnil
;
984 rb_execution_context_t
* volatile ec
= GET_EC();
985 rb_ensure_list_t ensure_list
;
986 ensure_list
.entry
.marker
= 0;
987 ensure_list
.entry
.e_proc
= e_proc
;
988 ensure_list
.entry
.data2
= data2
;
989 ensure_list
.next
= ec
->ensure_list
;
990 ec
->ensure_list
= &ensure_list
;
992 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
993 result
= (*b_proc
) (data1
);
996 errinfo
= ec
->errinfo
;
997 if (!NIL_P(errinfo
) && !RB_TYPE_P(errinfo
, T_OBJECT
)) {
1000 ec
->ensure_list
=ensure_list
.next
;
1001 (*ensure_list
.entry
.e_proc
)(ensure_list
.entry
.data2
);
1002 ec
->errinfo
= errinfo
;
1004 EC_JUMP_TAG(ec
, state
);
1009 frame_func_id(const rb_control_frame_t
*cfp
)
1011 const rb_callable_method_entry_t
*me
= rb_vm_frame_method_entry(cfp
);
1014 return me
->def
->original_id
;
1022 frame_called_id(rb_control_frame_t
*cfp
)
1024 const rb_callable_method_entry_t
*me
= rb_vm_frame_method_entry(cfp
);
1027 return me
->called_id
;
1035 rb_frame_this_func(void)
1037 return frame_func_id(GET_EC()->cfp
);
1041 rb_frame_callee(void)
1043 return frame_called_id(GET_EC()->cfp
);
1046 static rb_control_frame_t
*
1047 previous_frame(const rb_execution_context_t
*ec
)
1049 rb_control_frame_t
*prev_cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(ec
->cfp
);
1050 /* check if prev_cfp can be accessible */
1051 if ((void *)(ec
->vm_stack
+ ec
->vm_stack_size
) == (void *)(prev_cfp
)) {
1058 prev_frame_callee(void)
1060 rb_control_frame_t
*prev_cfp
= previous_frame(GET_EC());
1061 if (!prev_cfp
) return 0;
1062 return frame_called_id(prev_cfp
);
1066 prev_frame_func(void)
1068 rb_control_frame_t
*prev_cfp
= previous_frame(GET_EC());
1069 if (!prev_cfp
) return 0;
1070 return frame_func_id(prev_cfp
);
1075 * Returns the ID of the last method in the call stack.
1076 * \sa rb_frame_this_func
1077 * \ingroup defmethod
1080 rb_frame_last_func(void)
1082 const rb_execution_context_t
*ec
= GET_EC();
1083 const rb_control_frame_t
*cfp
= ec
->cfp
;
1086 while (!(mid
= frame_func_id(cfp
)) &&
1087 (cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
),
1088 !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec
, cfp
)));
1094 * append_features(mod) -> mod
1096 * When this module is included in another, Ruby calls
1097 * #append_features in this module, passing it the receiving module
1098 * in _mod_. Ruby's default implementation is to add the constants,
1099 * methods, and module variables of this module to _mod_ if this
1100 * module has not already been added to _mod_ or one of its
1101 * ancestors. See also Module#include.
1105 rb_mod_append_features(VALUE module
, VALUE include
)
1107 if (!CLASS_OR_MODULE_P(include
)) {
1108 Check_Type(include
, T_CLASS
);
1110 rb_include_module(include
, module
);
1117 * include(module, ...) -> self
1119 * Invokes Module.append_features on each parameter in reverse order.
1123 rb_mod_include(int argc
, VALUE
*argv
, VALUE module
)
1126 ID id_append_features
, id_included
;
1128 CONST_ID(id_append_features
, "append_features");
1129 CONST_ID(id_included
, "included");
1131 if (FL_TEST(module
, RMODULE_IS_REFINEMENT
)) {
1132 rb_raise(rb_eTypeError
, "Refinement#include has been removed");
1135 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1136 for (i
= 0; i
< argc
; i
++)
1137 Check_Type(argv
[i
], T_MODULE
);
1139 rb_funcall(argv
[argc
], id_append_features
, 1, module
);
1140 rb_funcall(argv
[argc
], id_included
, 1, module
);
1147 * prepend_features(mod) -> mod
1149 * When this module is prepended in another, Ruby calls
1150 * #prepend_features in this module, passing it the receiving module
1151 * in _mod_. Ruby's default implementation is to overlay the
1152 * constants, methods, and module variables of this module to _mod_
1153 * if this module has not already been added to _mod_ or one of its
1154 * ancestors. See also Module#prepend.
1158 rb_mod_prepend_features(VALUE module
, VALUE prepend
)
1160 if (!CLASS_OR_MODULE_P(prepend
)) {
1161 Check_Type(prepend
, T_CLASS
);
1163 rb_prepend_module(prepend
, module
);
1170 * prepend(module, ...) -> self
1172 * Invokes Module.prepend_features on each parameter in reverse order.
1176 rb_mod_prepend(int argc
, VALUE
*argv
, VALUE module
)
1179 ID id_prepend_features
, id_prepended
;
1181 if (FL_TEST(module
, RMODULE_IS_REFINEMENT
)) {
1182 rb_raise(rb_eTypeError
, "Refinement#prepend has been removed");
1185 CONST_ID(id_prepend_features
, "prepend_features");
1186 CONST_ID(id_prepended
, "prepended");
1188 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1189 for (i
= 0; i
< argc
; i
++)
1190 Check_Type(argv
[i
], T_MODULE
);
1192 rb_funcall(argv
[argc
], id_prepend_features
, 1, module
);
1193 rb_funcall(argv
[argc
], id_prepended
, 1, module
);
1199 ensure_class_or_module(VALUE obj
)
1201 if (!RB_TYPE_P(obj
, T_CLASS
) && !RB_TYPE_P(obj
, T_MODULE
)) {
1202 rb_raise(rb_eTypeError
,
1203 "wrong argument type %"PRIsVALUE
" (expected Class or Module)",
1209 hidden_identity_hash_new(void)
1211 VALUE hash
= rb_ident_hash_new();
1213 RBASIC_CLEAR_CLASS(hash
); /* hide from ObjectSpace */
1218 refinement_superclass(VALUE superclass
)
1220 if (RB_TYPE_P(superclass
, T_MODULE
)) {
1221 /* FIXME: Should ancestors of superclass be used here? */
1222 return rb_include_class_new(RCLASS_ORIGIN(superclass
), rb_cBasicObject
);
1233 rb_using_refinement(rb_cref_t
*cref
, VALUE klass
, VALUE module
)
1235 VALUE iclass
, c
, superclass
= klass
;
1237 ensure_class_or_module(klass
);
1238 Check_Type(module
, T_MODULE
);
1239 if (NIL_P(CREF_REFINEMENTS(cref
))) {
1240 CREF_REFINEMENTS_SET(cref
, hidden_identity_hash_new());
1243 if (CREF_OMOD_SHARED(cref
)) {
1244 CREF_REFINEMENTS_SET(cref
, rb_hash_dup(CREF_REFINEMENTS(cref
)));
1245 CREF_OMOD_SHARED_UNSET(cref
);
1247 if (!NIL_P(c
= rb_hash_lookup(CREF_REFINEMENTS(cref
), klass
))) {
1249 while (c
&& RB_TYPE_P(c
, T_ICLASS
)) {
1250 if (RBASIC(c
)->klass
== module
) {
1251 /* already used refinement */
1254 c
= RCLASS_SUPER(c
);
1258 FL_SET(module
, RMODULE_IS_OVERLAID
);
1259 superclass
= refinement_superclass(superclass
);
1260 c
= iclass
= rb_include_class_new(module
, superclass
);
1261 RB_OBJ_WRITE(c
, &RCLASS_REFINED_CLASS(c
), klass
);
1263 RCLASS_M_TBL(c
) = RCLASS_M_TBL(module
);
1265 module
= RCLASS_SUPER(module
);
1266 while (module
&& module
!= klass
) {
1267 FL_SET(module
, RMODULE_IS_OVERLAID
);
1268 c
= RCLASS_SET_SUPER(c
, rb_include_class_new(module
, RCLASS_SUPER(c
)));
1269 RB_OBJ_WRITE(c
, &RCLASS_REFINED_CLASS(c
), klass
);
1270 module
= RCLASS_SUPER(module
);
1272 rb_hash_aset(CREF_REFINEMENTS(cref
), klass
, iclass
);
1276 using_refinement(VALUE klass
, VALUE module
, VALUE arg
)
1278 rb_cref_t
*cref
= (rb_cref_t
*) arg
;
1280 rb_using_refinement(cref
, klass
, module
);
1285 using_module_recursive(const rb_cref_t
*cref
, VALUE klass
)
1288 VALUE super
, module
, refinements
;
1290 super
= RCLASS_SUPER(klass
);
1292 using_module_recursive(cref
, super
);
1294 switch (BUILTIN_TYPE(klass
)) {
1300 module
= RBASIC(klass
)->klass
;
1304 rb_raise(rb_eTypeError
, "wrong argument type %s (expected Module)",
1305 rb_obj_classname(klass
));
1308 CONST_ID(id_refinements
, "__refinements__");
1309 refinements
= rb_attr_get(module
, id_refinements
);
1310 if (NIL_P(refinements
)) return;
1311 rb_hash_foreach(refinements
, using_refinement
, (VALUE
) cref
);
1318 rb_using_module(const rb_cref_t
*cref
, VALUE module
)
1320 Check_Type(module
, T_MODULE
);
1321 using_module_recursive(cref
, module
);
1322 rb_clear_method_cache_all();
1327 rb_refinement_module_get_refined_class(VALUE module
)
1329 ID id_refined_class
;
1331 CONST_ID(id_refined_class
, "__refined_class__");
1332 return rb_attr_get(module
, id_refined_class
);
1336 add_activated_refinement(VALUE activated_refinements
,
1337 VALUE klass
, VALUE refinement
)
1339 VALUE iclass
, c
, superclass
= klass
;
1341 if (!NIL_P(c
= rb_hash_lookup(activated_refinements
, klass
))) {
1343 while (c
&& RB_TYPE_P(c
, T_ICLASS
)) {
1344 if (RBASIC(c
)->klass
== refinement
) {
1345 /* already used refinement */
1348 c
= RCLASS_SUPER(c
);
1351 FL_SET(refinement
, RMODULE_IS_OVERLAID
);
1352 superclass
= refinement_superclass(superclass
);
1353 c
= iclass
= rb_include_class_new(refinement
, superclass
);
1354 RB_OBJ_WRITE(c
, &RCLASS_REFINED_CLASS(c
), klass
);
1355 refinement
= RCLASS_SUPER(refinement
);
1356 while (refinement
&& refinement
!= klass
) {
1357 FL_SET(refinement
, RMODULE_IS_OVERLAID
);
1358 c
= RCLASS_SET_SUPER(c
, rb_include_class_new(refinement
, RCLASS_SUPER(c
)));
1359 RB_OBJ_WRITE(c
, &RCLASS_REFINED_CLASS(c
), klass
);
1360 refinement
= RCLASS_SUPER(refinement
);
1362 rb_hash_aset(activated_refinements
, klass
, iclass
);
1367 * refine(mod) { block } -> module
1369 * Refine <i>mod</i> in the receiver.
1371 * Returns a module, where refined methods are defined.
1375 rb_mod_refine(VALUE module
, VALUE klass
)
1378 ID id_refinements
, id_activated_refinements
,
1379 id_refined_class
, id_defined_at
;
1380 VALUE refinements
, activated_refinements
;
1381 rb_thread_t
*th
= GET_THREAD();
1382 VALUE block_handler
= rb_vm_frame_block_handler(th
->ec
->cfp
);
1384 if (block_handler
== VM_BLOCK_HANDLER_NONE
) {
1385 rb_raise(rb_eArgError
, "no block given");
1387 if (vm_block_handler_type(block_handler
) != block_handler_type_iseq
) {
1388 rb_raise(rb_eArgError
, "can't pass a Proc as a block to Module#refine");
1391 ensure_class_or_module(klass
);
1392 CONST_ID(id_refinements
, "__refinements__");
1393 refinements
= rb_attr_get(module
, id_refinements
);
1394 if (NIL_P(refinements
)) {
1395 refinements
= hidden_identity_hash_new();
1396 rb_ivar_set(module
, id_refinements
, refinements
);
1398 CONST_ID(id_activated_refinements
, "__activated_refinements__");
1399 activated_refinements
= rb_attr_get(module
, id_activated_refinements
);
1400 if (NIL_P(activated_refinements
)) {
1401 activated_refinements
= hidden_identity_hash_new();
1402 rb_ivar_set(module
, id_activated_refinements
,
1403 activated_refinements
);
1405 refinement
= rb_hash_lookup(refinements
, klass
);
1406 if (NIL_P(refinement
)) {
1407 VALUE superclass
= refinement_superclass(klass
);
1408 refinement
= rb_refinement_new();
1409 RCLASS_SET_SUPER(refinement
, superclass
);
1410 FL_SET(refinement
, RMODULE_IS_REFINEMENT
);
1411 CONST_ID(id_refined_class
, "__refined_class__");
1412 rb_ivar_set(refinement
, id_refined_class
, klass
);
1413 CONST_ID(id_defined_at
, "__defined_at__");
1414 rb_ivar_set(refinement
, id_defined_at
, module
);
1415 rb_hash_aset(refinements
, klass
, refinement
);
1416 add_activated_refinement(activated_refinements
, klass
, refinement
);
1418 rb_yield_refine_block(refinement
, activated_refinements
);
1423 ignored_block(VALUE module
, const char *klass
)
1425 const char *anon
= "";
1426 Check_Type(module
, T_MODULE
);
1427 if (!RTEST(rb_search_class_path(module
))) {
1428 anon
= ", maybe for Module.new";
1430 rb_warn("%s""using doesn't call the given block""%s.", klass
, anon
);
1435 * using(module) -> self
1437 * Import class refinements from <i>module</i> into the current class or
1438 * module definition.
1442 mod_using(VALUE self
, VALUE module
)
1444 rb_control_frame_t
*prev_cfp
= previous_frame(GET_EC());
1446 if (prev_frame_func()) {
1447 rb_raise(rb_eRuntimeError
,
1448 "Module#using is not permitted in methods");
1450 if (prev_cfp
&& prev_cfp
->self
!= self
) {
1451 rb_raise(rb_eRuntimeError
, "Module#using is not called on self");
1453 if (rb_block_given_p()) {
1454 ignored_block(module
, "Module#");
1456 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module
);
1461 used_modules_i(VALUE _
, VALUE mod
, VALUE ary
)
1464 CONST_ID(id_defined_at
, "__defined_at__");
1465 while (FL_TEST(rb_class_of(mod
), RMODULE_IS_REFINEMENT
)) {
1466 rb_ary_push(ary
, rb_attr_get(rb_class_of(mod
), id_defined_at
));
1467 mod
= RCLASS_SUPER(mod
);
1474 * used_modules -> array
1476 * Returns an array of all modules used in the current scope. The ordering
1477 * of modules in the resulting array is not defined.
1491 * p Module.used_modules
1493 * <em>produces:</em>
1498 rb_mod_s_used_modules(VALUE _
)
1500 const rb_cref_t
*cref
= rb_vm_cref();
1501 VALUE ary
= rb_ary_new();
1504 if (!NIL_P(CREF_REFINEMENTS(cref
))) {
1505 rb_hash_foreach(CREF_REFINEMENTS(cref
), used_modules_i
, ary
);
1507 cref
= CREF_NEXT(cref
);
1510 return rb_funcall(ary
, rb_intern("uniq"), 0);
1513 struct refinement_import_methods_arg
{
1520 rb_cref_t
*rb_vm_cref_dup_without_refinements(const rb_cref_t
*cref
);
1522 static enum rb_id_table_iterator_result
1523 refinement_import_methods_i(ID key
, VALUE value
, void *data
)
1525 const rb_method_entry_t
*me
= (const rb_method_entry_t
*)value
;
1526 struct refinement_import_methods_arg
*arg
= (struct refinement_import_methods_arg
*)data
;
1528 if (me
->def
->type
!= VM_METHOD_TYPE_ISEQ
) {
1529 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
));
1531 rb_cref_t
*new_cref
= rb_vm_cref_dup_without_refinements(me
->def
->body
.iseq
.cref
);
1532 CREF_REFINEMENTS_SET(new_cref
, CREF_REFINEMENTS(arg
->cref
));
1533 rb_add_method_iseq(arg
->refinement
, key
, me
->def
->body
.iseq
.iseqptr
, new_cref
, METHOD_ENTRY_VISI(me
));
1534 return ID_TABLE_CONTINUE
;
1538 * Note: docs for the method are in class.c
1542 refinement_import_methods(int argc
, VALUE
*argv
, VALUE refinement
)
1545 struct refinement_import_methods_arg arg
;
1547 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1548 for (i
= 0; i
< argc
; i
++) {
1549 Check_Type(argv
[i
], T_MODULE
);
1550 if (RCLASS_SUPER(argv
[i
])) {
1551 rb_warn("%"PRIsVALUE
" has ancestors, but Refinement#import_methods doesn't import their methods", rb_class_path(argv
[i
]));
1554 arg
.cref
= rb_vm_cref_replace_with_duplicated_cref();
1555 arg
.refinement
= refinement
;
1556 for (i
= 0; i
< argc
; i
++) {
1557 arg
.module
= argv
[i
];
1558 struct rb_id_table
*m_tbl
= RCLASS_M_TBL(argv
[i
]);
1559 if (!m_tbl
) continue;
1560 rb_id_table_foreach(m_tbl
, refinement_import_methods_i
, &arg
);
1566 rb_obj_call_init(VALUE obj
, int argc
, const VALUE
*argv
)
1568 rb_obj_call_init_kw(obj
, argc
, argv
, RB_NO_KEYWORDS
);
1572 rb_obj_call_init_kw(VALUE obj
, int argc
, const VALUE
*argv
, int kw_splat
)
1574 PASS_PASSED_BLOCK_HANDLER();
1575 rb_funcallv_kw(obj
, idInitialize
, argc
, argv
, kw_splat
);
1579 rb_extend_object(VALUE obj
, VALUE module
)
1581 rb_include_module(rb_singleton_class(obj
), module
);
1586 * extend_object(obj) -> obj
1588 * Extends the specified object by adding this module's constants and
1589 * methods (which are added as singleton methods). This is the callback
1590 * method used by Object#extend.
1593 * def Picky.extend_object(o)
1595 * puts "Can't add Picky to a String"
1597 * puts "Picky added to #{o.class}"
1602 * (s = Array.new).extend Picky # Call Object.extend
1603 * (s = "quick brown fox").extend Picky
1605 * <em>produces:</em>
1607 * Picky added to Array
1608 * Can't add Picky to a String
1612 rb_mod_extend_object(VALUE mod
, VALUE obj
)
1614 rb_extend_object(obj
, mod
);
1620 * obj.extend(module, ...) -> obj
1622 * Adds to _obj_ the instance methods from each module given as a
1627 * "Hello from Mod.\n"
1633 * "Hello from Klass.\n"
1638 * k.hello #=> "Hello from Klass.\n"
1639 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1640 * k.hello #=> "Hello from Mod.\n"
1644 rb_obj_extend(int argc
, VALUE
*argv
, VALUE obj
)
1647 ID id_extend_object
, id_extended
;
1649 CONST_ID(id_extend_object
, "extend_object");
1650 CONST_ID(id_extended
, "extended");
1652 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1653 for (i
= 0; i
< argc
; i
++)
1654 Check_Type(argv
[i
], T_MODULE
);
1656 rb_funcall(argv
[argc
], id_extend_object
, 1, obj
);
1657 rb_funcall(argv
[argc
], id_extended
, 1, obj
);
1664 * include(module, ...) -> self
1666 * Invokes Module.append_features on each parameter in turn.
1667 * Effectively adds the methods and constants in each module to the
1672 top_include(int argc
, VALUE
*argv
, VALUE self
)
1674 rb_thread_t
*th
= GET_THREAD();
1676 if (th
->top_wrapper
) {
1677 rb_warning("main.include in the wrapped load is effective only in wrapper module");
1678 return rb_mod_include(argc
, argv
, th
->top_wrapper
);
1680 return rb_mod_include(argc
, argv
, rb_cObject
);
1685 * using(module) -> self
1687 * Import class refinements from <i>module</i> into the scope where
1692 top_using(VALUE self
, VALUE module
)
1694 const rb_cref_t
*cref
= rb_vm_cref();
1695 rb_control_frame_t
*prev_cfp
= previous_frame(GET_EC());
1697 if (CREF_NEXT(cref
) || (prev_cfp
&& rb_vm_frame_method_entry(prev_cfp
))) {
1698 rb_raise(rb_eRuntimeError
, "main.using is permitted only at toplevel");
1700 if (rb_block_given_p()) {
1701 ignored_block(module
, "main.");
1703 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module
);
1707 static const VALUE
*
1708 errinfo_place(const rb_execution_context_t
*ec
)
1710 const rb_control_frame_t
*cfp
= ec
->cfp
;
1711 const rb_control_frame_t
*end_cfp
= RUBY_VM_END_CONTROL_FRAME(ec
);
1713 while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp
, end_cfp
)) {
1714 if (VM_FRAME_RUBYFRAME_P(cfp
)) {
1715 if (cfp
->iseq
->body
->type
== ISEQ_TYPE_RESCUE
) {
1716 return &cfp
->ep
[VM_ENV_INDEX_LAST_LVAR
];
1718 else if (cfp
->iseq
->body
->type
== ISEQ_TYPE_ENSURE
&&
1719 !THROW_DATA_P(cfp
->ep
[VM_ENV_INDEX_LAST_LVAR
]) &&
1720 !FIXNUM_P(cfp
->ep
[VM_ENV_INDEX_LAST_LVAR
])) {
1721 return &cfp
->ep
[VM_ENV_INDEX_LAST_LVAR
];
1724 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1730 rb_ec_get_errinfo(const rb_execution_context_t
*ec
)
1732 const VALUE
*ptr
= errinfo_place(ec
);
1744 return get_ec_errinfo(GET_EC());
1748 errinfo_getter(ID id
, VALUE
*_
)
1750 return get_errinfo();
1756 return GET_EC()->errinfo
;
1760 rb_set_errinfo(VALUE err
)
1762 if (!NIL_P(err
) && !rb_obj_is_kind_of(err
, rb_eException
)) {
1763 rb_raise(rb_eTypeError
, "assigning non-exception to $!");
1765 GET_EC()->errinfo
= err
;
1769 errat_getter(ID id
, VALUE
*_
)
1771 VALUE err
= get_errinfo();
1773 return rb_get_backtrace(err
);
1781 errat_setter(VALUE val
, ID id
, VALUE
*var
)
1783 VALUE err
= get_errinfo();
1785 rb_raise(rb_eArgError
, "$! not set");
1787 set_backtrace(err
, val
);
1792 * __method__ -> symbol
1794 * Returns the name at the definition of the current method as a
1796 * If called outside of a method, it returns <code>nil</code>.
1801 rb_f_method_name(VALUE _
)
1803 ID fname
= prev_frame_func(); /* need *method* ID */
1806 return ID2SYM(fname
);
1815 * __callee__ -> symbol
1817 * Returns the called name of the current method as a Symbol.
1818 * If called outside of a method, it returns <code>nil</code>.
1823 rb_f_callee_name(VALUE _
)
1825 ID fname
= prev_frame_callee(); /* need *callee* ID */
1828 return ID2SYM(fname
);
1839 * Returns the canonicalized absolute path of the directory of the file from
1840 * which this method is called. It means symlinks in the path is resolved.
1841 * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1842 * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1846 f_current_dirname(VALUE _
)
1848 VALUE base
= rb_current_realfilepath();
1852 base
= rb_file_dirname(base
);
1858 * global_variables -> array
1860 * Returns an array of the names of global variables. This includes
1861 * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>,
1862 * but does not include the numbered regexp global variables (<tt>$1</tt>,
1863 * <tt>$2</tt>, etc.).
1865 * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
1869 f_global_variables(VALUE _
)
1871 return rb_f_global_variables();
1876 * trace_var(symbol, cmd ) -> nil
1877 * trace_var(symbol) {|val| block } -> nil
1879 * Controls tracing of assignments to global variables. The parameter
1880 * +symbol+ identifies the variable (as either a string name or a
1881 * symbol identifier). _cmd_ (which may be a string or a
1882 * +Proc+ object) or block is executed whenever the variable
1883 * is assigned. The block or +Proc+ object receives the
1884 * variable's new value as a parameter. Also see
1885 * Kernel::untrace_var.
1887 * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
1891 * <em>produces:</em>
1894 * $_ is now ' there'
1898 f_trace_var(int c
, const VALUE
*a
, VALUE _
)
1900 return rb_f_trace_var(c
, a
);
1905 * untrace_var(symbol [, cmd] ) -> array or nil
1907 * Removes tracing for the specified command on the given global
1908 * variable and returns +nil+. If no command is specified,
1909 * removes all tracing for that variable and returns an array
1910 * containing the commands actually removed.
1914 f_untrace_var(int c
, const VALUE
*a
, VALUE _
)
1916 return rb_f_untrace_var(c
, a
);
1922 rb_define_virtual_variable("$@", errat_getter
, errat_setter
);
1923 rb_define_virtual_variable("$!", errinfo_getter
, 0);
1925 rb_gvar_ractor_local("$@");
1926 rb_gvar_ractor_local("$!");
1928 rb_define_global_function("raise", f_raise
, -1);
1929 rb_define_global_function("fail", f_raise
, -1);
1931 rb_define_global_function("global_variables", f_global_variables
, 0);
1933 rb_define_global_function("__method__", rb_f_method_name
, 0);
1934 rb_define_global_function("__callee__", rb_f_callee_name
, 0);
1935 rb_define_global_function("__dir__", f_current_dirname
, 0);
1937 rb_define_method(rb_cModule
, "include", rb_mod_include
, -1);
1938 rb_define_method(rb_cModule
, "prepend", rb_mod_prepend
, -1);
1940 rb_define_private_method(rb_cModule
, "append_features", rb_mod_append_features
, 1);
1941 rb_define_private_method(rb_cModule
, "extend_object", rb_mod_extend_object
, 1);
1942 rb_define_private_method(rb_cModule
, "prepend_features", rb_mod_prepend_features
, 1);
1943 rb_define_private_method(rb_cModule
, "refine", rb_mod_refine
, 1);
1944 rb_define_private_method(rb_cModule
, "using", mod_using
, 1);
1945 rb_define_singleton_method(rb_cModule
, "used_modules",
1946 rb_mod_s_used_modules
, 0);
1947 rb_undef_method(rb_cClass
, "refine");
1948 rb_define_private_method(rb_cRefinement
, "import_methods", refinement_import_methods
, -1);
1950 rb_undef_method(rb_cClass
, "module_function");
1955 rb_define_singleton_method(rb_cModule
, "nesting", rb_mod_nesting
, 0);
1956 rb_define_singleton_method(rb_cModule
, "constants", rb_mod_s_constants
, -1);
1958 rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
1959 "include", top_include
, -1);
1960 rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
1961 "using", top_using
, 1);
1963 rb_define_method(rb_mKernel
, "extend", rb_obj_extend
, -1);
1965 rb_define_global_function("trace_var", f_trace_var
, -1);
1966 rb_define_global_function("untrace_var", f_untrace_var
, -1);
1968 rb_vm_register_special_exception(ruby_error_reenter
, rb_eFatal
, "exception reentered");
1969 rb_vm_register_special_exception(ruby_error_stackfatal
, rb_eFatal
, "machine stack overflow in critical region");
1971 id_signo
= rb_intern_const("signo");
1972 id_status
= rb_intern_const("status");