Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
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 rb_ivar_set(mesg, id_cause, cause);
651 }
652}
653
654static void
655rb_longjmp(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
656{
657 mesg = exc_setup_message(ec, mesg, &cause);
658 setup_exception(ec, tag, mesg, cause);
659 rb_ec_raised_clear(ec);
660 EC_JUMP_TAG(ec, tag);
661}
662
663static VALUE make_exception(int argc, const VALUE *argv, int isstr);
664
665NORETURN(static void rb_exc_exception(VALUE mesg, int tag, VALUE cause));
666
667static void
668rb_exc_exception(VALUE mesg, int tag, VALUE cause)
669{
670 if (!NIL_P(mesg)) {
671 mesg = make_exception(1, &mesg, FALSE);
672 }
673 rb_longjmp(GET_EC(), tag, mesg, cause);
674}
675
683void
685{
686 rb_exc_exception(mesg, TAG_RAISE, Qundef);
687}
688
696void
698{
699 rb_exc_exception(mesg, TAG_FATAL, Qnil);
700}
701
702void
703rb_interrupt(void)
704{
706}
707
708enum {raise_opt_cause, raise_max_opt}; /*< \private */
709
710static int
711extract_raise_opts(int argc, VALUE *argv, VALUE *opts)
712{
713 int i;
714 if (argc > 0) {
715 VALUE opt;
716 argc = rb_scan_args(argc, argv, "*:", NULL, &opt);
717 if (!NIL_P(opt)) {
718 if (!RHASH_EMPTY_P(opt)) {
719 ID keywords[1];
720 CONST_ID(keywords[0], "cause");
721 rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
722 if (!RHASH_EMPTY_P(opt)) argv[argc++] = opt;
723 return argc;
724 }
725 }
726 }
727 for (i = 0; i < raise_max_opt; ++i) {
728 opts[i] = Qundef;
729 }
730 return argc;
731}
732
733VALUE
734rb_f_raise(int argc, VALUE *argv)
735{
736 VALUE err;
737 VALUE opts[raise_max_opt], *const cause = &opts[raise_opt_cause];
738
739 argc = extract_raise_opts(argc, argv, opts);
740 if (argc == 0) {
741 if (!UNDEF_P(*cause)) {
742 rb_raise(rb_eArgError, "only cause is given with no arguments");
743 }
744 err = get_errinfo();
745 if (!NIL_P(err)) {
746 argc = 1;
747 argv = &err;
748 }
749 }
750 rb_raise_jump(rb_make_exception(argc, argv), *cause);
751
753}
754
755/*
756 * call-seq:
757 * raise
758 * raise(string, cause: $!)
759 * raise(exception [, string [, array]], cause: $!)
760 * fail
761 * fail(string, cause: $!)
762 * fail(exception [, string [, array]], cause: $!)
763 *
764 * With no arguments, raises the exception in <code>$!</code> or raises
765 * a RuntimeError if <code>$!</code> is +nil+. With a single +String+
766 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
767 * the first parameter should be an +Exception+ class (or another
768 * object that returns an +Exception+ object when sent an +exception+
769 * message). The optional second parameter sets the message associated with
770 * the exception (accessible via Exception#message), and the third parameter
771 * is an array of callback information (accessible via Exception#backtrace).
772 * The +cause+ of the generated exception (accessible via Exception#cause)
773 * is automatically set to the "current" exception (<code>$!</code>), if any.
774 * An alternative value, either an +Exception+ object or +nil+, can be
775 * specified via the +:cause+ argument.
776 *
777 * Exceptions are caught by the +rescue+ clause of
778 * <code>begin...end</code> blocks.
779 *
780 * raise "Failed to create socket"
781 * raise ArgumentError, "No parameters", caller
782 */
783
784static VALUE
785f_raise(int c, VALUE *v, VALUE _)
786{
787 return rb_f_raise(c, v);
788}
789
790static VALUE
791make_exception(int argc, const VALUE *argv, int isstr)
792{
793 VALUE mesg, exc;
794
795 mesg = Qnil;
796 switch (argc) {
797 case 0:
798 return Qnil;
799 case 1:
800 exc = argv[0];
801 if (isstr &&! NIL_P(exc)) {
802 mesg = rb_check_string_type(exc);
803 if (!NIL_P(mesg)) {
804 return rb_exc_new3(rb_eRuntimeError, mesg);
805 }
806 }
807
808 case 2:
809 case 3:
810 break;
811 default:
812 rb_error_arity(argc, 0, 3);
813 }
814 if (NIL_P(mesg)) {
815 mesg = rb_check_funcall(argv[0], idException, argc != 1, &argv[1]);
816 }
817 if (UNDEF_P(mesg)) {
818 rb_raise(rb_eTypeError, "exception class/object expected");
819 }
820 if (!rb_obj_is_kind_of(mesg, rb_eException)) {
821 rb_raise(rb_eTypeError, "exception object expected");
822 }
823 if (argc == 3) {
824 set_backtrace(mesg, argv[2]);
825 }
826
827 return mesg;
828}
829
830VALUE
831rb_make_exception(int argc, const VALUE *argv)
832{
833 return make_exception(argc, argv, TRUE);
834}
835
838static void
839rb_raise_jump(VALUE mesg, VALUE cause)
840{
841 rb_execution_context_t *ec = GET_EC();
842 const rb_control_frame_t *cfp = ec->cfp;
843 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
844 VALUE klass = me->owner;
845 VALUE self = cfp->self;
846 ID mid = me->called_id;
847
848 rb_vm_pop_frame(ec);
849 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, self, me->def->original_id, mid, klass, Qnil);
850
851 rb_longjmp(ec, TAG_RAISE, mesg, cause);
852}
853
854void
855rb_jump_tag(int tag)
856{
857 if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) {
858 unknown_longjmp_status(tag);
859 }
860 EC_JUMP_TAG(GET_EC(), tag);
861}
862
863int
865{
866 if (rb_vm_frame_block_handler(GET_EC()->cfp) == VM_BLOCK_HANDLER_NONE) {
867 return FALSE;
868 }
869 else {
870 return TRUE;
871 }
872}
873
874int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp);
875
876int
878{
879 return rb_vm_cframe_keyword_p(GET_EC()->cfp);
880}
881
883
884void
886{
887 if (!rb_block_given_p()) {
888 rb_vm_localjump_error("no block given", Qnil, 0);
889 }
890}
891
892VALUE
893rb_rescue2(VALUE (* b_proc) (VALUE), VALUE data1,
894 VALUE (* r_proc) (VALUE, VALUE), VALUE data2, ...)
895{
896 va_list ap;
897 va_start(ap, data2);
898 VALUE ret = rb_vrescue2(b_proc, data1, r_proc, data2, ap);
899 va_end(ap);
900 return ret;
901}
902
903VALUE
904rb_vrescue2(VALUE (* b_proc) (VALUE), VALUE data1,
905 VALUE (* r_proc) (VALUE, VALUE), VALUE data2,
906 va_list args)
907{
908 enum ruby_tag_type state;
909 rb_execution_context_t * volatile ec = GET_EC();
910 rb_control_frame_t *volatile cfp = ec->cfp;
911 volatile VALUE result = Qfalse;
912 volatile VALUE e_info = ec->errinfo;
913
914 EC_PUSH_TAG(ec);
915 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
916 retry_entry:
917 result = (*b_proc) (data1);
918 }
919 else if (result) {
920 /* escape from r_proc */
921 if (state == TAG_RETRY) {
922 state = TAG_NONE;
923 ec->errinfo = Qnil;
924 result = Qfalse;
925 goto retry_entry;
926 }
927 }
928 else {
929 rb_vm_rewind_cfp(ec, cfp);
930
931 if (state == TAG_RAISE) {
932 int handle = FALSE;
933 VALUE eclass;
934 va_list ap;
935
936 result = Qnil;
937 /* reuses args when raised again after retrying in r_proc */
938 va_copy(ap, args);
939 while ((eclass = va_arg(ap, VALUE)) != 0) {
940 if (rb_obj_is_kind_of(ec->errinfo, eclass)) {
941 handle = TRUE;
942 break;
943 }
944 }
945 va_end(ap);
946
947 if (handle) {
948 state = TAG_NONE;
949 if (r_proc) {
950 result = (*r_proc) (data2, ec->errinfo);
951 }
952 ec->errinfo = e_info;
953 }
954 }
955 }
956 EC_POP_TAG();
957 if (state)
958 EC_JUMP_TAG(ec, state);
959
960 return result;
961}
962
963VALUE
964rb_rescue(VALUE (* b_proc)(VALUE), VALUE data1,
965 VALUE (* r_proc)(VALUE, VALUE), VALUE data2)
966{
967 return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
968 (VALUE)0);
969}
970
971VALUE
972rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate)
973{
974 volatile VALUE result = Qnil;
975 volatile enum ruby_tag_type state;
976 rb_execution_context_t * volatile ec = GET_EC();
977 rb_control_frame_t *volatile cfp = ec->cfp;
978
979 EC_PUSH_TAG(ec);
980 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
981 SAVE_ROOT_JMPBUF(rb_ec_thread_ptr(ec), result = (*proc) (data));
982 }
983 else {
984 rb_vm_rewind_cfp(ec, cfp);
985 }
986 EC_POP_TAG();
987
988 if (pstate != NULL) *pstate = state;
989 return result;
990}
991
992VALUE
993rb_ensure(VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2)
994{
995 int state;
996 volatile VALUE result = Qnil;
997 VALUE errinfo;
998 rb_execution_context_t * volatile ec = GET_EC();
999 rb_ensure_list_t ensure_list;
1000 ensure_list.entry.marker = 0;
1001 ensure_list.entry.e_proc = e_proc;
1002 ensure_list.entry.data2 = data2;
1003 ensure_list.next = ec->ensure_list;
1004 ec->ensure_list = &ensure_list;
1005 EC_PUSH_TAG(ec);
1006 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1007 result = (*b_proc) (data1);
1008 }
1009 EC_POP_TAG();
1010 errinfo = ec->errinfo;
1011 if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) {
1012 ec->errinfo = Qnil;
1013 }
1014 ec->ensure_list=ensure_list.next;
1015 (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
1016 ec->errinfo = errinfo;
1017 if (state)
1018 EC_JUMP_TAG(ec, state);
1019 return result;
1020}
1021
1022static ID
1023frame_func_id(const rb_control_frame_t *cfp)
1024{
1025 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1026
1027 if (me) {
1028 return me->def->original_id;
1029 }
1030 else {
1031 return 0;
1032 }
1033}
1034
1035static ID
1036frame_called_id(rb_control_frame_t *cfp)
1037{
1038 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1039
1040 if (me) {
1041 return me->called_id;
1042 }
1043 else {
1044 return 0;
1045 }
1046}
1047
1048ID
1049rb_frame_this_func(void)
1050{
1051 return frame_func_id(GET_EC()->cfp);
1052}
1053
1054ID
1055rb_frame_callee(void)
1056{
1057 return frame_called_id(GET_EC()->cfp);
1058}
1059
1060static rb_control_frame_t *
1061previous_frame(const rb_execution_context_t *ec)
1062{
1063 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1064 /* check if prev_cfp can be accessible */
1065 if ((void *)(ec->vm_stack + ec->vm_stack_size) == (void *)(prev_cfp)) {
1066 return 0;
1067 }
1068 return prev_cfp;
1069}
1070
1071static ID
1072prev_frame_callee(void)
1073{
1074 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1075 if (!prev_cfp) return 0;
1076 return frame_called_id(prev_cfp);
1077}
1078
1079static ID
1080prev_frame_func(void)
1081{
1082 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1083 if (!prev_cfp) return 0;
1084 return frame_func_id(prev_cfp);
1085}
1086
1093ID
1094rb_frame_last_func(void)
1095{
1096 const rb_execution_context_t *ec = GET_EC();
1097 const rb_control_frame_t *cfp = ec->cfp;
1098 ID mid;
1099
1100 while (!(mid = frame_func_id(cfp)) &&
1101 (cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp),
1102 !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)));
1103 return mid;
1104}
1105
1106/*
1107 * call-seq:
1108 * append_features(mod) -> mod
1109 *
1110 * When this module is included in another, Ruby calls
1111 * #append_features in this module, passing it the receiving module
1112 * in _mod_. Ruby's default implementation is to add the constants,
1113 * methods, and module variables of this module to _mod_ if this
1114 * module has not already been added to _mod_ or one of its
1115 * ancestors. See also Module#include.
1116 */
1117
1118static VALUE
1119rb_mod_append_features(VALUE module, VALUE include)
1120{
1121 if (!CLASS_OR_MODULE_P(include)) {
1122 Check_Type(include, T_CLASS);
1123 }
1124 rb_include_module(include, module);
1125
1126 return module;
1127}
1128
1129/*
1130 * call-seq:
1131 * include(module, ...) -> self
1132 *
1133 * Invokes Module.append_features on each parameter in reverse order.
1134 */
1135
1136static VALUE
1137rb_mod_include(int argc, VALUE *argv, VALUE module)
1138{
1139 int i;
1140 ID id_append_features, id_included;
1141
1142 CONST_ID(id_append_features, "append_features");
1143 CONST_ID(id_included, "included");
1144
1145 if (BUILTIN_TYPE(module) == T_MODULE && FL_TEST(module, RMODULE_IS_REFINEMENT)) {
1146 rb_raise(rb_eTypeError, "Refinement#include has been removed");
1147 }
1148
1149 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1150 for (i = 0; i < argc; i++) {
1151 Check_Type(argv[i], T_MODULE);
1152 if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) {
1153 rb_raise(rb_eTypeError, "Cannot include refinement");
1154 }
1155 }
1156 while (argc--) {
1157 rb_funcall(argv[argc], id_append_features, 1, module);
1158 rb_funcall(argv[argc], id_included, 1, module);
1159 }
1160 return module;
1161}
1162
1163/*
1164 * call-seq:
1165 * prepend_features(mod) -> mod
1166 *
1167 * When this module is prepended in another, Ruby calls
1168 * #prepend_features in this module, passing it the receiving module
1169 * in _mod_. Ruby's default implementation is to overlay the
1170 * constants, methods, and module variables of this module to _mod_
1171 * if this module has not already been added to _mod_ or one of its
1172 * ancestors. See also Module#prepend.
1173 */
1174
1175static VALUE
1176rb_mod_prepend_features(VALUE module, VALUE prepend)
1177{
1178 if (!CLASS_OR_MODULE_P(prepend)) {
1179 Check_Type(prepend, T_CLASS);
1180 }
1181 rb_prepend_module(prepend, module);
1182
1183 return module;
1184}
1185
1186/*
1187 * call-seq:
1188 * prepend(module, ...) -> self
1189 *
1190 * Invokes Module.prepend_features on each parameter in reverse order.
1191 */
1192
1193static VALUE
1194rb_mod_prepend(int argc, VALUE *argv, VALUE module)
1195{
1196 int i;
1197 ID id_prepend_features, id_prepended;
1198
1199 if (BUILTIN_TYPE(module) == T_MODULE && FL_TEST(module, RMODULE_IS_REFINEMENT)) {
1200 rb_raise(rb_eTypeError, "Refinement#prepend has been removed");
1201 }
1202
1203 CONST_ID(id_prepend_features, "prepend_features");
1204 CONST_ID(id_prepended, "prepended");
1205
1206 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1207 for (i = 0; i < argc; i++) {
1208 Check_Type(argv[i], T_MODULE);
1209 if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) {
1210 rb_raise(rb_eTypeError, "Cannot prepend refinement");
1211 }
1212 }
1213 while (argc--) {
1214 rb_funcall(argv[argc], id_prepend_features, 1, module);
1215 rb_funcall(argv[argc], id_prepended, 1, module);
1216 }
1217 return module;
1218}
1219
1220static void
1221ensure_class_or_module(VALUE obj)
1222{
1223 if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
1225 "wrong argument type %"PRIsVALUE" (expected Class or Module)",
1226 rb_obj_class(obj));
1227 }
1228}
1229
1230static VALUE
1231hidden_identity_hash_new(void)
1232{
1233 VALUE hash = rb_ident_hash_new();
1234
1235 RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1236 return hash;
1237}
1238
1239static VALUE
1240refinement_superclass(VALUE superclass)
1241{
1242 if (RB_TYPE_P(superclass, T_MODULE)) {
1243 /* FIXME: Should ancestors of superclass be used here? */
1244 return rb_include_class_new(RCLASS_ORIGIN(superclass), rb_cBasicObject);
1245 }
1246 else {
1247 return superclass;
1248 }
1249}
1250
1254static void
1255rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module)
1256{
1257 VALUE iclass, c, superclass = klass;
1258
1259 ensure_class_or_module(klass);
1260 Check_Type(module, T_MODULE);
1261 if (NIL_P(CREF_REFINEMENTS(cref))) {
1262 CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new());
1263 }
1264 else {
1265 if (CREF_OMOD_SHARED(cref)) {
1266 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref)));
1267 CREF_OMOD_SHARED_UNSET(cref);
1268 }
1269 if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) {
1270 superclass = c;
1271 while (c && RB_TYPE_P(c, T_ICLASS)) {
1272 if (RBASIC(c)->klass == module) {
1273 /* already used refinement */
1274 return;
1275 }
1276 c = RCLASS_SUPER(c);
1277 }
1278 }
1279 }
1280 superclass = refinement_superclass(superclass);
1281 c = iclass = rb_include_class_new(module, superclass);
1282 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1283
1284 RCLASS_M_TBL(c) = RCLASS_M_TBL(module);
1285
1286 module = RCLASS_SUPER(module);
1287 while (module && module != klass) {
1288 c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
1289 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1290 module = RCLASS_SUPER(module);
1291 }
1292 rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass);
1293}
1294
1295static int
1296using_refinement(VALUE klass, VALUE module, VALUE arg)
1297{
1298 rb_cref_t *cref = (rb_cref_t *) arg;
1299
1300 rb_using_refinement(cref, klass, module);
1301 return ST_CONTINUE;
1302}
1303
1304static void
1305using_module_recursive(const rb_cref_t *cref, VALUE klass)
1306{
1307 ID id_refinements;
1308 VALUE super, module, refinements;
1309
1310 super = RCLASS_SUPER(klass);
1311 if (super) {
1312 using_module_recursive(cref, super);
1313 }
1314 switch (BUILTIN_TYPE(klass)) {
1315 case T_MODULE:
1316 module = klass;
1317 break;
1318
1319 case T_ICLASS:
1320 module = RBASIC(klass)->klass;
1321 break;
1322
1323 default:
1324 rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1325 rb_obj_classname(klass));
1326 break;
1327 }
1328 CONST_ID(id_refinements, "__refinements__");
1329 refinements = rb_attr_get(module, id_refinements);
1330 if (NIL_P(refinements)) return;
1331 rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1332}
1333
1337static void
1338rb_using_module(const rb_cref_t *cref, VALUE module)
1339{
1340 Check_Type(module, T_MODULE);
1341 using_module_recursive(cref, module);
1342 rb_clear_method_cache_all();
1343}
1344
1345/*
1346 * call-seq:
1347 * refined_class -> class
1348 *
1349 * Return the class refined by the receiver.
1350 */
1351VALUE
1352rb_refinement_module_get_refined_class(VALUE module)
1353{
1354 ID id_refined_class;
1355
1356 CONST_ID(id_refined_class, "__refined_class__");
1357 return rb_attr_get(module, id_refined_class);
1358}
1359
1360static void
1361add_activated_refinement(VALUE activated_refinements,
1362 VALUE klass, VALUE refinement)
1363{
1364 VALUE iclass, c, superclass = klass;
1365
1366 if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1367 superclass = c;
1368 while (c && RB_TYPE_P(c, T_ICLASS)) {
1369 if (RBASIC(c)->klass == refinement) {
1370 /* already used refinement */
1371 return;
1372 }
1373 c = RCLASS_SUPER(c);
1374 }
1375 }
1376 superclass = refinement_superclass(superclass);
1377 c = iclass = rb_include_class_new(refinement, superclass);
1378 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1379 refinement = RCLASS_SUPER(refinement);
1380 while (refinement && refinement != klass) {
1381 c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1382 RB_OBJ_WRITE(c, &RCLASS_REFINED_CLASS(c), klass);
1383 refinement = RCLASS_SUPER(refinement);
1384 }
1385 rb_hash_aset(activated_refinements, klass, iclass);
1386}
1387
1388/*
1389 * call-seq:
1390 * refine(mod) { block } -> module
1391 *
1392 * Refine <i>mod</i> in the receiver.
1393 *
1394 * Returns a module, where refined methods are defined.
1395 */
1396
1397static VALUE
1398rb_mod_refine(VALUE module, VALUE klass)
1399{
1400 VALUE refinement;
1401 ID id_refinements, id_activated_refinements,
1402 id_refined_class, id_defined_at;
1403 VALUE refinements, activated_refinements;
1404 rb_thread_t *th = GET_THREAD();
1405 VALUE block_handler = rb_vm_frame_block_handler(th->ec->cfp);
1406
1407 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1408 rb_raise(rb_eArgError, "no block given");
1409 }
1410 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
1411 rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine");
1412 }
1413
1414 ensure_class_or_module(klass);
1415 CONST_ID(id_refinements, "__refinements__");
1416 refinements = rb_attr_get(module, id_refinements);
1417 if (NIL_P(refinements)) {
1418 refinements = hidden_identity_hash_new();
1419 rb_ivar_set(module, id_refinements, refinements);
1420 }
1421 CONST_ID(id_activated_refinements, "__activated_refinements__");
1422 activated_refinements = rb_attr_get(module, id_activated_refinements);
1423 if (NIL_P(activated_refinements)) {
1424 activated_refinements = hidden_identity_hash_new();
1425 rb_ivar_set(module, id_activated_refinements,
1426 activated_refinements);
1427 }
1428 refinement = rb_hash_lookup(refinements, klass);
1429 if (NIL_P(refinement)) {
1430 VALUE superclass = refinement_superclass(klass);
1431 refinement = rb_refinement_new();
1432 RCLASS_SET_SUPER(refinement, superclass);
1433 RUBY_ASSERT(BUILTIN_TYPE(refinement) == T_MODULE);
1434 FL_SET(refinement, RMODULE_IS_REFINEMENT);
1435 CONST_ID(id_refined_class, "__refined_class__");
1436 rb_ivar_set(refinement, id_refined_class, klass);
1437 CONST_ID(id_defined_at, "__defined_at__");
1438 rb_ivar_set(refinement, id_defined_at, module);
1439 rb_hash_aset(refinements, klass, refinement);
1440 add_activated_refinement(activated_refinements, klass, refinement);
1441 }
1442 rb_yield_refine_block(refinement, activated_refinements);
1443 return refinement;
1444}
1445
1446static void
1447ignored_block(VALUE module, const char *klass)
1448{
1449 const char *anon = "";
1450 Check_Type(module, T_MODULE);
1451 if (!RTEST(rb_search_class_path(module))) {
1452 anon = ", maybe for Module.new";
1453 }
1454 rb_warn("%s""using doesn't call the given block""%s.", klass, anon);
1455}
1456
1457/*
1458 * call-seq:
1459 * using(module) -> self
1460 *
1461 * Import class refinements from <i>module</i> into the current class or
1462 * module definition.
1463 */
1464
1465static VALUE
1466mod_using(VALUE self, VALUE module)
1467{
1468 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1469
1470 if (prev_frame_func()) {
1472 "Module#using is not permitted in methods");
1473 }
1474 if (prev_cfp && prev_cfp->self != self) {
1475 rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1476 }
1477 if (rb_block_given_p()) {
1478 ignored_block(module, "Module#");
1479 }
1480 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1481 return self;
1482}
1483
1484
1485/*
1486 * call-seq:
1487 * refinements -> array
1488 *
1489 * Returns an array of modules defined within the receiver.
1490 *
1491 * module A
1492 * refine Integer do
1493 * end
1494 *
1495 * refine String do
1496 * end
1497 * end
1498 *
1499 * p A.refinements
1500 *
1501 * <em>produces:</em>
1502 *
1503 * [#<refinement:Integer@A>, #<refinement:String@A>]
1504 */
1505static VALUE
1506mod_refinements(VALUE self)
1507{
1508 ID id_refinements;
1509 VALUE refinements;
1510
1511 CONST_ID(id_refinements, "__refinements__");
1512 refinements = rb_attr_get(self, id_refinements);
1513 if (NIL_P(refinements)) {
1514 return rb_ary_new();
1515 }
1516 return rb_hash_values(refinements);
1517}
1518
1519static int
1520used_modules_i(VALUE _, VALUE mod, VALUE ary)
1521{
1522 ID id_defined_at;
1523 CONST_ID(id_defined_at, "__defined_at__");
1524 while (BUILTIN_TYPE(rb_class_of(mod)) == T_MODULE && FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1525 rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at));
1526 mod = RCLASS_SUPER(mod);
1527 }
1528 return ST_CONTINUE;
1529}
1530
1531/*
1532 * call-seq:
1533 * used_modules -> array
1534 *
1535 * Returns an array of all modules used in the current scope. The ordering
1536 * of modules in the resulting array is not defined.
1537 *
1538 * module A
1539 * refine Object do
1540 * end
1541 * end
1542 *
1543 * module B
1544 * refine Object do
1545 * end
1546 * end
1547 *
1548 * using A
1549 * using B
1550 * p Module.used_modules
1551 *
1552 * <em>produces:</em>
1553 *
1554 * [B, A]
1555 */
1556static VALUE
1557rb_mod_s_used_modules(VALUE _)
1558{
1559 const rb_cref_t *cref = rb_vm_cref();
1560 VALUE ary = rb_ary_new();
1561
1562 while (cref) {
1563 if (!NIL_P(CREF_REFINEMENTS(cref))) {
1564 rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary);
1565 }
1566 cref = CREF_NEXT(cref);
1567 }
1568
1569 return rb_funcall(ary, rb_intern("uniq"), 0);
1570}
1571
1572static int
1573used_refinements_i(VALUE _, VALUE mod, VALUE ary)
1574{
1575 while (BUILTIN_TYPE(rb_class_of(mod)) == T_MODULE && FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1576 rb_ary_push(ary, rb_class_of(mod));
1577 mod = RCLASS_SUPER(mod);
1578 }
1579 return ST_CONTINUE;
1580}
1581
1582/*
1583 * call-seq:
1584 * used_refinements -> array
1585 *
1586 * Returns an array of all modules used in the current scope. The ordering
1587 * of modules in the resulting array is not defined.
1588 *
1589 * module A
1590 * refine Object do
1591 * end
1592 * end
1593 *
1594 * module B
1595 * refine Object do
1596 * end
1597 * end
1598 *
1599 * using A
1600 * using B
1601 * p Module.used_refinements
1602 *
1603 * <em>produces:</em>
1604 *
1605 * [#<refinement:Object@B>, #<refinement:Object@A>]
1606 */
1607static VALUE
1608rb_mod_s_used_refinements(VALUE _)
1609{
1610 const rb_cref_t *cref = rb_vm_cref();
1611 VALUE ary = rb_ary_new();
1612
1613 while (cref) {
1614 if (!NIL_P(CREF_REFINEMENTS(cref))) {
1615 rb_hash_foreach(CREF_REFINEMENTS(cref), used_refinements_i, ary);
1616 }
1617 cref = CREF_NEXT(cref);
1618 }
1619
1620 return ary;
1621}
1622
1624 rb_cref_t *cref;
1625 VALUE refinement;
1626 VALUE module;
1627};
1628
1629/* vm.c */
1630rb_cref_t *rb_vm_cref_dup_without_refinements(const rb_cref_t *cref);
1631
1632static enum rb_id_table_iterator_result
1633refinement_import_methods_i(ID key, VALUE value, void *data)
1634{
1635 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1637
1638 if (me->def->type != VM_METHOD_TYPE_ISEQ) {
1639 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));
1640 }
1641 rb_cref_t *new_cref = rb_vm_cref_dup_without_refinements(me->def->body.iseq.cref);
1642 CREF_REFINEMENTS_SET(new_cref, CREF_REFINEMENTS(arg->cref));
1643 rb_add_method_iseq(arg->refinement, key, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
1644 return ID_TABLE_CONTINUE;
1645}
1646
1647/*
1648 * Note: docs for the method are in class.c
1649 */
1650
1651static VALUE
1652refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1653{
1654 int i;
1656
1657 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1658 for (i = 0; i < argc; i++) {
1659 Check_Type(argv[i], T_MODULE);
1660 if (RCLASS_SUPER(argv[i])) {
1661 rb_warn("%"PRIsVALUE" has ancestors, but Refinement#import_methods doesn't import their methods", rb_class_path(argv[i]));
1662 }
1663 }
1664 arg.cref = rb_vm_cref_replace_with_duplicated_cref();
1665 arg.refinement = refinement;
1666 for (i = 0; i < argc; i++) {
1667 arg.module = argv[i];
1668 struct rb_id_table *m_tbl = RCLASS_M_TBL(argv[i]);
1669 if (!m_tbl) continue;
1670 rb_id_table_foreach(m_tbl, refinement_import_methods_i, &arg);
1671 }
1672 return refinement;
1673}
1674
1675void
1676rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
1677{
1678 rb_obj_call_init_kw(obj, argc, argv, RB_NO_KEYWORDS);
1679}
1680
1681void
1682rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
1683{
1684 PASS_PASSED_BLOCK_HANDLER();
1685 rb_funcallv_kw(obj, idInitialize, argc, argv, kw_splat);
1686}
1687
1688void
1690{
1692}
1693
1694/*
1695 * call-seq:
1696 * extend_object(obj) -> obj
1697 *
1698 * Extends the specified object by adding this module's constants and
1699 * methods (which are added as singleton methods). This is the callback
1700 * method used by Object#extend.
1701 *
1702 * module Picky
1703 * def Picky.extend_object(o)
1704 * if String === o
1705 * puts "Can't add Picky to a String"
1706 * else
1707 * puts "Picky added to #{o.class}"
1708 * super
1709 * end
1710 * end
1711 * end
1712 * (s = Array.new).extend Picky # Call Object.extend
1713 * (s = "quick brown fox").extend Picky
1714 *
1715 * <em>produces:</em>
1716 *
1717 * Picky added to Array
1718 * Can't add Picky to a String
1719 */
1720
1721static VALUE
1722rb_mod_extend_object(VALUE mod, VALUE obj)
1723{
1724 rb_extend_object(obj, mod);
1725 return obj;
1726}
1727
1728/*
1729 * call-seq:
1730 * obj.extend(module, ...) -> obj
1731 *
1732 * Adds to _obj_ the instance methods from each module given as a
1733 * parameter.
1734 *
1735 * module Mod
1736 * def hello
1737 * "Hello from Mod.\n"
1738 * end
1739 * end
1740 *
1741 * class Klass
1742 * def hello
1743 * "Hello from Klass.\n"
1744 * end
1745 * end
1746 *
1747 * k = Klass.new
1748 * k.hello #=> "Hello from Klass.\n"
1749 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1750 * k.hello #=> "Hello from Mod.\n"
1751 */
1752
1753static VALUE
1754rb_obj_extend(int argc, VALUE *argv, VALUE obj)
1755{
1756 int i;
1757 ID id_extend_object, id_extended;
1758
1759 CONST_ID(id_extend_object, "extend_object");
1760 CONST_ID(id_extended, "extended");
1761
1762 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1763 for (i = 0; i < argc; i++) {
1764 Check_Type(argv[i], T_MODULE);
1765 if (FL_TEST(argv[i], RMODULE_IS_REFINEMENT)) {
1766 rb_raise(rb_eTypeError, "Cannot extend object with refinement");
1767 }
1768 }
1769 while (argc--) {
1770 rb_funcall(argv[argc], id_extend_object, 1, obj);
1771 rb_funcall(argv[argc], id_extended, 1, obj);
1772 }
1773 return obj;
1774}
1775
1776/*
1777 * call-seq:
1778 * include(module, ...) -> self
1779 *
1780 * Invokes Module.append_features on each parameter in turn.
1781 * Effectively adds the methods and constants in each module to the
1782 * receiver.
1783 */
1784
1785static VALUE
1786top_include(int argc, VALUE *argv, VALUE self)
1787{
1788 rb_thread_t *th = GET_THREAD();
1789
1790 if (th->top_wrapper) {
1791 rb_warning("main.include in the wrapped load is effective only in wrapper module");
1792 return rb_mod_include(argc, argv, th->top_wrapper);
1793 }
1794 return rb_mod_include(argc, argv, rb_cObject);
1795}
1796
1797/*
1798 * call-seq:
1799 * using(module) -> self
1800 *
1801 * Import class refinements from <i>module</i> into the scope where
1802 * #using is called.
1803 */
1804
1805static VALUE
1806top_using(VALUE self, VALUE module)
1807{
1808 const rb_cref_t *cref = CREF_NEXT(rb_vm_cref());;
1809 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1810 rb_thread_t *th = GET_THREAD();
1811
1812 if ((th->top_wrapper ? CREF_NEXT(cref) : cref) ||
1813 (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) {
1814 rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel");
1815 }
1816 if (rb_block_given_p()) {
1817 ignored_block(module, "main.");
1818 }
1819 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1820 return self;
1821}
1822
1823static const VALUE *
1824errinfo_place(const rb_execution_context_t *ec)
1825{
1826 const rb_control_frame_t *cfp = ec->cfp;
1827 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1828
1829 while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1830 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1831 if (ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_RESCUE) {
1832 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1833 }
1834 else if (ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_ENSURE &&
1835 !THROW_DATA_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR]) &&
1836 !FIXNUM_P(cfp->ep[VM_ENV_INDEX_LAST_LVAR])) {
1837 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1838 }
1839 }
1840 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1841 }
1842 return 0;
1843}
1844
1845VALUE
1846rb_ec_get_errinfo(const rb_execution_context_t *ec)
1847{
1848 const VALUE *ptr = errinfo_place(ec);
1849 if (ptr) {
1850 return *ptr;
1851 }
1852 else {
1853 return ec->errinfo;
1854 }
1855}
1856
1857static VALUE
1858get_errinfo(void)
1859{
1860 return get_ec_errinfo(GET_EC());
1861}
1862
1863static VALUE
1864errinfo_getter(ID id, VALUE *_)
1865{
1866 return get_errinfo();
1867}
1868
1869VALUE
1871{
1872 return GET_EC()->errinfo;
1873}
1874
1875void
1877{
1878 if (!NIL_P(err) && !rb_obj_is_kind_of(err, rb_eException)) {
1879 rb_raise(rb_eTypeError, "assigning non-exception to $!");
1880 }
1881 GET_EC()->errinfo = err;
1882}
1883
1884static VALUE
1885errat_getter(ID id, VALUE *_)
1886{
1887 VALUE err = get_errinfo();
1888 if (!NIL_P(err)) {
1889 return rb_get_backtrace(err);
1890 }
1891 else {
1892 return Qnil;
1893 }
1894}
1895
1896static void
1897errat_setter(VALUE val, ID id, VALUE *var)
1898{
1899 VALUE err = get_errinfo();
1900 if (NIL_P(err)) {
1901 rb_raise(rb_eArgError, "$! not set");
1902 }
1903 set_backtrace(err, val);
1904}
1905
1906/*
1907 * call-seq:
1908 * __method__ -> symbol
1909 *
1910 * Returns the name at the definition of the current method as a
1911 * Symbol.
1912 * If called outside of a method, it returns <code>nil</code>.
1913 *
1914 */
1915
1916static VALUE
1917rb_f_method_name(VALUE _)
1918{
1919 ID fname = prev_frame_func(); /* need *method* ID */
1920
1921 if (fname) {
1922 return ID2SYM(fname);
1923 }
1924 else {
1925 return Qnil;
1926 }
1927}
1928
1929/*
1930 * call-seq:
1931 * __callee__ -> symbol
1932 *
1933 * Returns the called name of the current method as a Symbol.
1934 * If called outside of a method, it returns <code>nil</code>.
1935 *
1936 */
1937
1938static VALUE
1939rb_f_callee_name(VALUE _)
1940{
1941 ID fname = prev_frame_callee(); /* need *callee* ID */
1942
1943 if (fname) {
1944 return ID2SYM(fname);
1945 }
1946 else {
1947 return Qnil;
1948 }
1949}
1950
1951/*
1952 * call-seq:
1953 * __dir__ -> string
1954 *
1955 * Returns the canonicalized absolute path of the directory of the file from
1956 * which this method is called. It means symlinks in the path is resolved.
1957 * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1958 * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1959 *
1960 */
1961static VALUE
1962f_current_dirname(VALUE _)
1963{
1964 VALUE base = rb_current_realfilepath();
1965 if (NIL_P(base)) {
1966 return Qnil;
1967 }
1968 base = rb_file_dirname(base);
1969 return base;
1970}
1971
1972/*
1973 * call-seq:
1974 * global_variables -> array
1975 *
1976 * Returns an array of the names of global variables. This includes
1977 * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>,
1978 * but does not include the numbered regexp global variables (<tt>$1</tt>,
1979 * <tt>$2</tt>, etc.).
1980 *
1981 * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
1982 */
1983
1984static VALUE
1985f_global_variables(VALUE _)
1986{
1987 return rb_f_global_variables();
1988}
1989
1990/*
1991 * call-seq:
1992 * trace_var(symbol, cmd ) -> nil
1993 * trace_var(symbol) {|val| block } -> nil
1994 *
1995 * Controls tracing of assignments to global variables. The parameter
1996 * +symbol+ identifies the variable (as either a string name or a
1997 * symbol identifier). _cmd_ (which may be a string or a
1998 * +Proc+ object) or block is executed whenever the variable
1999 * is assigned. The block or +Proc+ object receives the
2000 * variable's new value as a parameter. Also see
2001 * Kernel::untrace_var.
2002 *
2003 * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
2004 * $_ = "hello"
2005 * $_ = ' there'
2006 *
2007 * <em>produces:</em>
2008 *
2009 * $_ is now 'hello'
2010 * $_ is now ' there'
2011 */
2012
2013static VALUE
2014f_trace_var(int c, const VALUE *a, VALUE _)
2015{
2016 return rb_f_trace_var(c, a);
2017}
2018
2019/*
2020 * call-seq:
2021 * untrace_var(symbol [, cmd] ) -> array or nil
2022 *
2023 * Removes tracing for the specified command on the given global
2024 * variable and returns +nil+. If no command is specified,
2025 * removes all tracing for that variable and returns an array
2026 * containing the commands actually removed.
2027 */
2028
2029static VALUE
2030f_untrace_var(int c, const VALUE *a, VALUE _)
2031{
2032 return rb_f_untrace_var(c, a);
2033}
2034
2035void
2036Init_eval(void)
2037{
2038 rb_define_virtual_variable("$@", errat_getter, errat_setter);
2039 rb_define_virtual_variable("$!", errinfo_getter, 0);
2040
2041 rb_gvar_ractor_local("$@");
2042 rb_gvar_ractor_local("$!");
2043
2044 rb_define_global_function("raise", f_raise, -1);
2045 rb_define_global_function("fail", f_raise, -1);
2046
2047 rb_define_global_function("global_variables", f_global_variables, 0);
2048
2049 rb_define_global_function("__method__", rb_f_method_name, 0);
2050 rb_define_global_function("__callee__", rb_f_callee_name, 0);
2051 rb_define_global_function("__dir__", f_current_dirname, 0);
2052
2053 rb_define_method(rb_cModule, "include", rb_mod_include, -1);
2054 rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
2055
2056 rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
2057 rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
2058 rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
2059 rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
2060 rb_define_private_method(rb_cModule, "using", mod_using, 1);
2061 rb_define_method(rb_cModule, "refinements", mod_refinements, 0);
2062 rb_define_singleton_method(rb_cModule, "used_modules",
2063 rb_mod_s_used_modules, 0);
2064 rb_define_singleton_method(rb_cModule, "used_refinements",
2065 rb_mod_s_used_refinements, 0);
2066 rb_undef_method(rb_cClass, "refine");
2067 rb_define_private_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
2068 rb_define_method(rb_cRefinement, "refined_class", rb_refinement_module_get_refined_class, 0);
2069 rb_undef_method(rb_cRefinement, "append_features");
2070 rb_undef_method(rb_cRefinement, "prepend_features");
2071 rb_undef_method(rb_cRefinement, "extend_object");
2072
2073 rb_undef_method(rb_cClass, "module_function");
2074
2075 Init_vm_eval();
2076 Init_eval_method();
2077
2078 rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
2079 rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
2080
2082 "include", top_include, -1);
2084 "using", top_using, 1);
2085
2086 rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
2087
2088 rb_define_global_function("trace_var", f_trace_var, -1);
2089 rb_define_global_function("untrace_var", f_untrace_var, -1);
2090
2091 rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered");
2092 rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");
2093
2094 id_signo = rb_intern_const("signo");
2095 id_status = rb_intern_const("status");
2096}
#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:1090
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:985
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition eval.c:1689
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:1345
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2201
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:885
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2073
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
Definition eval.c:877
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:864
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2328
#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:3148
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
Definition eval.c:893
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:684
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:1876
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:904
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Raises an instance of rb_eFrozenError.
Definition error.c:3470
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:697
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:964
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition eval.c:993
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
Definition eval.c:1870
VALUE rb_eSysStackError
SystemStackError exception.
Definition eval.c:50
VALUE rb_eThreadError
ThreadError exception.
Definition eval.c:882
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_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:1070
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
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:2639
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:670
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
Definition variable.c:3103
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition variable.c:1223
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:1593
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
Definition variable.c:624
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition variable.c:3135
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:1215
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
Definition variable.c:3081
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
Definition variable.c:3064
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:1610
VALUE rb_f_global_variables(void)
Queries the list of global variables.
Definition variable.c:824
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:185
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:665
int ruby_vm_destruct(ruby_vm_t *vm)
Destructs the passed VM.
Definition vm.c:2860
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition sprintf.c:1219
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:322
#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
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:134
rb_cref_t * cref
class reference, should be marked
Definition method.h:135
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52