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