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