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