Ruby: eval.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

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