Ruby: eval.c Source File

Ruby 3.4.3p32 (2025-04-14 revision d0b7e5b6a04bde21ca483d20a1546b28b401c2d4)

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