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