Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
eval.c
Go to the documentation of this file.
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 "internal.h"
15#include "eval_intern.h"
16#include "iseq.h"
17#include "gc.h"
18#include "ruby/vm.h"
19#include "vm_core.h"
20#include "mjit.h"
21#include "probes.h"
22#include "probes_helper.h"
23#ifdef HAVE_SYS_PRCTL_H
24#include <sys/prctl.h>
25#endif
26
27NORETURN(void rb_raise_jump(VALUE, VALUE));
30
31static int rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex);
32static int rb_ec_exec_node(rb_execution_context_t *ec, void *n);
33
36
39#define id_cause ruby_static_id_cause
40
41#define exception_error GET_VM()->special_exceptions[ruby_error_reenter]
42
43#include "eval_error.c"
44#include "eval_jump.c"
45
46#define CLASS_OR_MODULE_P(obj) \
47 (!SPECIAL_CONST_P(obj) && \
48 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
49
55int
57{
58 enum ruby_tag_type state;
59
60 if (GET_VM())
61 return 0;
62
63 ruby_init_stack((void *)&state);
64
65 /*
66 * Disable THP early before mallocs happen because we want this to
67 * affect as many future pages as possible for CoW-friendliness
68 */
69#if defined(__linux__) && defined(PR_SET_THP_DISABLE)
70 prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0);
71#endif
73 Init_heap();
76
78 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
81 GET_VM()->running = 1;
82 }
83 EC_POP_TAG();
84
85 return state;
86}
87
93void
95{
96 int state = ruby_setup();
97 if (state) {
98 if (RTEST(ruby_debug))
99 error_print(GET_EC());
101 }
102}
103
114void *
116{
118 enum ruby_tag_type state;
119 void *volatile iseq = 0;
120
121 ruby_init_stack((void *)&iseq);
122 EC_PUSH_TAG(ec);
123 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
125 }
126 else {
128 state = error_handle(ec, state);
129 iseq = (void *)INT2FIX(state);
130 }
131 EC_POP_TAG();
132 return iseq;
133}
134
135static void
136rb_ec_teardown(rb_execution_context_t *ec)
137{
138 EC_PUSH_TAG(ec);
139 if (EC_EXEC_TAG() == TAG_NONE) {
140 rb_vm_trap_exit(rb_ec_vm_ptr(ec));
141 }
142 EC_POP_TAG();
143 rb_ec_exec_end_proc(ec);
145}
146
147static void
148rb_ec_finalize(rb_execution_context_t *ec)
149{
151 ec->errinfo = Qnil;
152 rb_objspace_call_finalizer(rb_ec_vm_ptr(ec)->objspace);
153}
154
162void
164{
166 rb_ec_teardown(ec);
167 rb_ec_finalize(ec);
168}
169
180int
181ruby_cleanup(volatile int ex)
182{
183 return rb_ec_cleanup(GET_EC(), ex);
184}
185
186static int
187rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex)
188{
189 int state;
190 volatile VALUE errs[2] = { Qundef, Qundef };
191 int nerr;
192 rb_thread_t *th = rb_ec_thread_ptr(ec);
193 rb_thread_t *const volatile th0 = th;
194 volatile int sysex = EXIT_SUCCESS;
195 volatile int step = 0;
196
199 EC_PUSH_TAG(ec);
200 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
201 th = th0;
203
204 step_0: step++;
205 th = th0;
206 errs[1] = ec->errinfo;
207 if (THROW_DATA_P(ec->errinfo)) ec->errinfo = Qnil;
208 ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]);
209
210 SAVE_ROOT_JMPBUF(th, rb_ec_teardown(ec));
211
212 step_1: step++;
213 th = th0;
214 /* protect from Thread#raise */
215 th->status = THREAD_KILLED;
216
217 errs[0] = ec->errinfo;
219 }
220 else {
221 switch (step) {
222 case 0: goto step_0;
223 case 1: goto step_1;
224 }
225 if (ex == 0) ex = state;
226 }
227 th = th0;
228 ec->errinfo = errs[1];
229 sysex = error_handle(ec, ex);
230
231 state = 0;
232 for (nerr = 0; nerr < numberof(errs); ++nerr) {
233 VALUE err = ATOMIC_VALUE_EXCHANGE(errs[nerr], Qnil);
234
235 if (!RTEST(err)) continue;
236
237 /* ec->errinfo contains a NODE while break'ing */
238 if (THROW_DATA_P(err)) continue;
239
241 sysex = sysexit_status(err);
242 break;
243 }
244 else if (rb_obj_is_kind_of(err, rb_eSignal)) {
246 state = NUM2INT(sig);
247 break;
248 }
249 else if (sysex == EXIT_SUCCESS) {
250 sysex = EXIT_FAILURE;
251 }
252 }
253
254 mjit_finish(true); // We still need ISeqs here.
255
256 rb_ec_finalize(ec);
257
258 /* unlock again if finalizer took mutexes. */
260 EC_POP_TAG();
262 ruby_vm_destruct(th->vm);
263 if (state) ruby_default_signal(state);
264
265 return sysex;
266}
267
268static int
269rb_ec_exec_node(rb_execution_context_t *ec, void *n)
270{
271 volatile int state;
273 if (!n) return 0;
274
275 EC_PUSH_TAG(ec);
276 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
277 rb_thread_t *const th = rb_ec_thread_ptr(ec);
278 SAVE_ROOT_JMPBUF(th, {
280 });
281 }
282 EC_POP_TAG();
283 return state;
284}
285
287void
289{
290 exit(ruby_cleanup(ex));
291}
292
305int
306ruby_executable_node(void *n, int *status)
307{
308 VALUE v = (VALUE)n;
309 int s;
310
311 switch (v) {
312 case Qtrue: s = EXIT_SUCCESS; break;
313 case Qfalse: s = EXIT_FAILURE; break;
314 default:
315 if (!FIXNUM_P(v)) return TRUE;
316 s = FIX2INT(v);
317 }
318 if (status) *status = s;
319 return FALSE;
320}
321
326int
328{
330 int status;
331 if (!ruby_executable_node(n, &status)) {
332 rb_ec_cleanup(ec, 0);
333 return status;
334 }
335 ruby_init_stack((void *)&status);
336 return rb_ec_cleanup(ec, rb_ec_exec_node(ec, n));
337}
338
340int
342{
343 ruby_init_stack((void *)&n);
344 return rb_ec_exec_node(GET_EC(), n);
345}
346
347/*
348 * call-seq:
349 * Module.nesting -> array
350 *
351 * Returns the list of +Modules+ nested at the point of call.
352 *
353 * module M1
354 * module M2
355 * $a = Module.nesting
356 * end
357 * end
358 * $a #=> [M1::M2, M1]
359 * $a[0].name #=> "M1::M2"
360 */
361
362static VALUE
363rb_mod_nesting(VALUE _)
364{
365 VALUE ary = rb_ary_new();
366 const rb_cref_t *cref = rb_vm_cref();
367
368 while (cref && CREF_NEXT(cref)) {
369 VALUE klass = CREF_CLASS(cref);
370 if (!CREF_PUSHED_BY_EVAL(cref) &&
371 !NIL_P(klass)) {
372 rb_ary_push(ary, klass);
373 }
374 cref = CREF_NEXT(cref);
375 }
376 return ary;
377}
378
379/*
380 * call-seq:
381 * Module.constants -> array
382 * Module.constants(inherited) -> array
383 *
384 * In the first form, returns an array of the names of all
385 * constants accessible from the point of call.
386 * This list includes the names of all modules and classes
387 * defined in the global scope.
388 *
389 * Module.constants.first(4)
390 * # => [:ARGF, :ARGV, :ArgumentError, :Array]
391 *
392 * Module.constants.include?(:SEEK_SET) # => false
393 *
394 * class IO
395 * Module.constants.include?(:SEEK_SET) # => true
396 * end
397 *
398 * The second form calls the instance method +constants+.
399 */
400
401static VALUE
402rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
403{
404 const rb_cref_t *cref = rb_vm_cref();
405 VALUE klass;
406 VALUE cbase = 0;
407 void *data = 0;
408
409 if (argc > 0 || mod != rb_cModule) {
410 return rb_mod_constants(argc, argv, mod);
411 }
412
413 while (cref) {
414 klass = CREF_CLASS(cref);
415 if (!CREF_PUSHED_BY_EVAL(cref) &&
416 !NIL_P(klass)) {
417 data = rb_mod_const_at(CREF_CLASS(cref), data);
418 if (!cbase) {
419 cbase = klass;
420 }
421 }
422 cref = CREF_NEXT(cref);
423 }
424
425 if (cbase) {
426 data = rb_mod_const_of(cbase, data);
427 }
428 return rb_const_list(data);
429}
430
437void
439{
440 if (SPECIAL_CONST_P(klass)) {
441 noclass:
443 }
444 if (OBJ_FROZEN(klass)) {
445 const char *desc;
446
447 if (FL_TEST(klass, FL_SINGLETON)) {
448 desc = "object";
450 if (!SPECIAL_CONST_P(klass)) {
451 switch (BUILTIN_TYPE(klass)) {
452 case T_MODULE:
453 case T_ICLASS:
454 desc = "Module";
455 break;
456 case T_CLASS:
457 desc = "Class";
458 break;
459 }
460 }
461 }
462 else {
463 switch (BUILTIN_TYPE(klass)) {
464 case T_MODULE:
465 case T_ICLASS:
466 desc = "module";
467 break;
468 case T_CLASS:
469 desc = "class";
470 break;
471 default:
472 goto noclass;
473 }
474 }
475 rb_frozen_error_raise(klass, "can't modify frozen %s: %"PRIsVALUE, desc, klass);
476 }
477}
478
479NORETURN(static void rb_longjmp(rb_execution_context_t *, int, volatile VALUE, VALUE));
480static VALUE get_errinfo(void);
481#define get_ec_errinfo(ec) rb_ec_get_errinfo(ec)
482
483static VALUE
484exc_setup_cause(VALUE exc, VALUE cause)
485{
486#if OPT_SUPPORT_JOKE
487 if (NIL_P(cause)) {
488 ID id_true_cause;
489 CONST_ID(id_true_cause, "true_cause");
490
491 cause = rb_attr_get(rb_eFatal, id_true_cause);
492 if (NIL_P(cause)) {
493 cause = rb_exc_new_cstr(rb_eFatal, "because using such Ruby");
494 rb_ivar_set(cause, id_cause, INT2FIX(42)); /* the answer */
495 OBJ_FREEZE(cause);
496 rb_ivar_set(rb_eFatal, id_true_cause, cause);
497 }
498 }
499#endif
500 if (!NIL_P(cause) && cause != exc) {
501 rb_ivar_set(exc, id_cause, cause);
502 if (!rb_ivar_defined(cause, id_cause)) {
503 rb_ivar_set(cause, id_cause, Qnil);
504 }
505 }
506 return exc;
507}
508
509static inline VALUE
510exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause)
511{
512 int nocause = 0;
513 int nocircular = 0;
514
515 if (NIL_P(mesg)) {
516 mesg = ec->errinfo;
518 nocause = 1;
519 }
520 if (NIL_P(mesg)) {
521 mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
522 nocause = 0;
523 nocircular = 1;
524 }
525 if (*cause == Qundef) {
526 if (nocause) {
527 *cause = Qnil;
528 nocircular = 1;
529 }
530 else if (!rb_ivar_defined(mesg, id_cause)) {
531 *cause = get_ec_errinfo(ec);
532 }
533 else {
534 nocircular = 1;
535 }
536 }
537 else if (!NIL_P(*cause) && !rb_obj_is_kind_of(*cause, rb_eException)) {
538 rb_raise(rb_eTypeError, "exception object expected");
539 }
540
541 if (!nocircular && !NIL_P(*cause) && *cause != Qundef && *cause != mesg) {
542 VALUE c = *cause;
543 while (!NIL_P(c = rb_attr_get(c, id_cause))) {
544 if (c == mesg) {
545 rb_raise(rb_eArgError, "circular causes");
546 }
547 }
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)) || (cause != Qundef)) {
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) || cause == Qundef) {
567 if (OBJ_FROZEN(mesg)) {
568 mesg = rb_obj_dup(mesg);
569 }
570 }
571 if (cause != Qundef && !THROW_DATA_P(cause)) {
572 exc_setup_cause(mesg, cause);
573 }
574 if (NIL_P(bt)) {
576 rb_ivar_set(mesg, idBt_locations, at);
577 set_backtrace(mesg, at);
578 }
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) &&
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 }
613 }
614 EC_POP_TAG();
615 if (state == TAG_FATAL && ec->errinfo == exception_error) {
616 ec->errinfo = mesg;
617 }
618 else if (state) {
620 EC_JUMP_TAG(ec, state);
621 }
622 }
623
624 if (rb_ec_set_raised(ec)) {
625 fatal:
629 }
630
631 if (tag != TAG_FATAL) {
633 EXEC_EVENT_HOOK(ec, RUBY_EVENT_RAISE, ec->cfp->self, 0, 0, 0, mesg);
634 }
635}
636
638void
640{
641 if (cause == Qundef) {
642 cause = get_ec_errinfo(ec);
643 }
644 if (cause != mesg) {
645 rb_ivar_set(mesg, id_cause, cause);
646 }
647}
648
649static void
650rb_longjmp(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
651{
652 mesg = exc_setup_message(ec, mesg, &cause);
653 setup_exception(ec, tag, mesg, cause);
655 EC_JUMP_TAG(ec, tag);
656}
657
658static VALUE make_exception(int argc, const VALUE *argv, int isstr);
659
667void
669{
670 if (!NIL_P(mesg)) {
671 mesg = make_exception(1, &mesg, FALSE);
672 }
673 rb_longjmp(GET_EC(), TAG_RAISE, mesg, Qundef);
674}
675
683void
685{
686 if (!NIL_P(mesg)) {
687 mesg = make_exception(1, &mesg, FALSE);
688 }
689 rb_longjmp(GET_EC(), TAG_FATAL, mesg, Qnil);
690}
691
696void
698{
700}
701
702enum {raise_opt_cause, raise_max_opt}; /*< \private */
703
704static int
705extract_raise_opts(int argc, const VALUE *argv, VALUE *opts)
706{
707 int i;
708 if (argc > 0) {
709 VALUE opt = argv[argc-1];
710 if (RB_TYPE_P(opt, T_HASH)) {
711 if (!RHASH_EMPTY_P(opt)) {
712 ID keywords[1];
713 CONST_ID(keywords[0], "cause");
714 rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
715 if (RHASH_EMPTY_P(opt)) --argc;
716 return argc;
717 }
718 }
719 }
720 for (i = 0; i < raise_max_opt; ++i) {
721 opts[i] = Qundef;
722 }
723 return argc;
724}
725
726VALUE
728{
729 VALUE err;
730 VALUE opts[raise_max_opt], *const cause = &opts[raise_opt_cause];
731
732 argc = extract_raise_opts(argc, argv, opts);
733 if (argc == 0) {
734 if (*cause != Qundef) {
735 rb_raise(rb_eArgError, "only cause is given with no arguments");
736 }
737 err = get_errinfo();
738 if (!NIL_P(err)) {
739 argc = 1;
740 argv = &err;
741 }
742 }
743 rb_raise_jump(rb_make_exception(argc, argv), *cause);
744
746}
747
748/*
749 * call-seq:
750 * raise
751 * raise(string, cause: $!)
752 * raise(exception [, string [, array]], cause: $!)
753 * fail
754 * fail(string, cause: $!)
755 * fail(exception [, string [, array]], cause: $!)
756 *
757 * With no arguments, raises the exception in <code>$!</code> or raises
758 * a RuntimeError if <code>$!</code> is +nil+. With a single +String+
759 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
760 * the first parameter should be an +Exception+ class (or another
761 * object that returns an +Exception+ object when sent an +exception+
762 * message). The optional second parameter sets the message associated with
763 * the exception (accessible via Exception#message), and the third parameter
764 * is an array of callback information (accessible via Exception#backtrace).
765 * The +cause+ of the generated exception (accessible via Exception#cause)
766 * is automatically set to the "current" exception (<code>$!</code>), if any.
767 * An alternative value, either an +Exception+ object or +nil+, can be
768 * specified via the +:cause+ argument.
769 *
770 * Exceptions are caught by the +rescue+ clause of
771 * <code>begin...end</code> blocks.
772 *
773 * raise "Failed to create socket"
774 * raise ArgumentError, "No parameters", caller
775 */
776
777static VALUE
778f_raise(int c, VALUE *v, VALUE _)
779{
780 return rb_f_raise(c, v);
781}
782
783static VALUE
784make_exception(int argc, const VALUE *argv, int isstr)
785{
786 VALUE mesg, exc;
787 int n;
788
789 mesg = Qnil;
790 switch (argc) {
791 case 0:
792 break;
793 case 1:
794 exc = argv[0];
795 if (NIL_P(exc))
796 break;
797 if (isstr) {
799 if (!NIL_P(mesg)) {
800 mesg = rb_exc_new3(rb_eRuntimeError, mesg);
801 break;
802 }
803 }
804 n = 0;
805 goto exception_call;
806
807 case 2:
808 case 3:
809 exc = argv[0];
810 n = 1;
811 exception_call:
813 if (mesg == Qundef) {
814 rb_raise(rb_eTypeError, "exception class/object expected");
815 }
816 break;
817 default:
818 rb_error_arity(argc, 0, 3);
819 }
820 if (argc > 0) {
822 rb_raise(rb_eTypeError, "exception object expected");
823 if (argc > 2)
824 set_backtrace(mesg, argv[2]);
825 }
826
827 return mesg;
828}
829
850VALUE
852{
853 return make_exception(argc, argv, TRUE);
854}
855
859void
860rb_raise_jump(VALUE mesg, VALUE cause)
861{
863 const rb_control_frame_t *cfp = ec->cfp;
865 VALUE klass = me->owner;
866 VALUE self = cfp->self;
867 ID mid = me->called_id;
868
869 rb_vm_pop_frame(ec);
871
872 rb_longjmp(ec, TAG_RAISE, mesg, cause);
873}
874
883void
885{
886 if (UNLIKELY(tag < TAG_RETURN || tag > TAG_FATAL)) {
888 }
889 EC_JUMP_TAG(GET_EC(), tag);
890}
891
897int
899{
901 return FALSE;
902 }
903 else {
904 return TRUE;
905 }
906}
907
909
910int
912{
914}
915
916/* -- Remove In 3.0 -- */
918int
920{
922}
923
925
931void
933{
934 if (!rb_block_given_p()) {
935 rb_vm_localjump_error("no block given", Qnil, 0);
936 }
937}
938
961VALUE
962rb_rescue2(VALUE (* b_proc) (VALUE), VALUE data1,
963 VALUE (* r_proc) (VALUE, VALUE), VALUE data2, ...)
964{
965 va_list ap;
966 va_start(ap, data2);
967 VALUE ret = rb_vrescue2(b_proc, data1, r_proc, data2, ap);
968 va_end(ap);
969 return ret;
970}
971
976VALUE
977rb_vrescue2(VALUE (* b_proc) (VALUE), VALUE data1,
978 VALUE (* r_proc) (VALUE, VALUE), VALUE data2,
979 va_list args)
980{
981 enum ruby_tag_type state;
982 rb_execution_context_t * volatile ec = GET_EC();
983 rb_control_frame_t *volatile cfp = ec->cfp;
984 volatile VALUE result = Qfalse;
985 volatile VALUE e_info = ec->errinfo;
986
987 EC_PUSH_TAG(ec);
988 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
989 retry_entry:
990 result = (*b_proc) (data1);
991 }
992 else if (result) {
993 /* escape from r_proc */
994 if (state == TAG_RETRY) {
995 state = 0;
996 ec->errinfo = Qnil;
997 result = Qfalse;
998 goto retry_entry;
999 }
1000 }
1001 else {
1002 rb_vm_rewind_cfp(ec, cfp);
1003
1004 if (state == TAG_RAISE) {
1005 int handle = FALSE;
1006 VALUE eclass;
1007
1008 while ((eclass = va_arg(args, VALUE)) != 0) {
1009 if (rb_obj_is_kind_of(ec->errinfo, eclass)) {
1010 handle = TRUE;
1011 break;
1012 }
1013 }
1014
1015 if (handle) {
1016 result = Qnil;
1017 state = 0;
1018 if (r_proc) {
1019 result = (*r_proc) (data2, ec->errinfo);
1020 }
1021 ec->errinfo = e_info;
1022 }
1023 }
1024 }
1025 EC_POP_TAG();
1026 if (state)
1027 EC_JUMP_TAG(ec, state);
1028
1029 return result;
1030}
1031
1046VALUE
1047rb_rescue(VALUE (* b_proc)(VALUE), VALUE data1,
1048 VALUE (* r_proc)(VALUE, VALUE), VALUE data2)
1049{
1050 return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
1051 (VALUE)0);
1052}
1053
1071VALUE
1072rb_protect(VALUE (* proc) (VALUE), VALUE data, int *pstate)
1073{
1074 volatile VALUE result = Qnil;
1075 volatile enum ruby_tag_type state;
1076 rb_execution_context_t * volatile ec = GET_EC();
1077 rb_control_frame_t *volatile cfp = ec->cfp;
1078 struct rb_vm_protect_tag protect_tag;
1079 rb_jmpbuf_t org_jmpbuf;
1080
1081 protect_tag.prev = ec->protect_tag;
1082
1083 EC_PUSH_TAG(ec);
1084 ec->protect_tag = &protect_tag;
1085 MEMCPY(&org_jmpbuf, &rb_ec_thread_ptr(ec)->root_jmpbuf, rb_jmpbuf_t, 1);
1086 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1087 SAVE_ROOT_JMPBUF(rb_ec_thread_ptr(ec), result = (*proc) (data));
1088 }
1089 else {
1090 rb_vm_rewind_cfp(ec, cfp);
1091 }
1092 MEMCPY(&rb_ec_thread_ptr(ec)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);
1093 ec->protect_tag = protect_tag.prev;
1094 EC_POP_TAG();
1095
1096 if (pstate != NULL) *pstate = state;
1097 return result;
1098}
1099
1114VALUE
1115rb_ensure(VALUE (*b_proc)(VALUE), VALUE data1, VALUE (*e_proc)(VALUE), VALUE data2)
1116{
1117 int state;
1118 volatile VALUE result = Qnil;
1119 VALUE errinfo;
1120 rb_execution_context_t * volatile ec = GET_EC();
1121 rb_ensure_list_t ensure_list;
1122 ensure_list.entry.marker = 0;
1123 ensure_list.entry.e_proc = e_proc;
1124 ensure_list.entry.data2 = data2;
1125 ensure_list.next = ec->ensure_list;
1126 ec->ensure_list = &ensure_list;
1127 EC_PUSH_TAG(ec);
1128 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1129 result = (*b_proc) (data1);
1130 }
1131 EC_POP_TAG();
1132 errinfo = ec->errinfo;
1133 if (!NIL_P(errinfo) && !RB_TYPE_P(errinfo, T_OBJECT)) {
1134 ec->errinfo = Qnil;
1135 }
1136 ec->ensure_list=ensure_list.next;
1137 (*ensure_list.entry.e_proc)(ensure_list.entry.data2);
1138 ec->errinfo = errinfo;
1139 if (state)
1140 EC_JUMP_TAG(ec, state);
1141 return result;
1142}
1143
1144static ID
1145frame_func_id(const rb_control_frame_t *cfp)
1146{
1148
1149 if (me) {
1150 return me->def->original_id;
1151 }
1152 else {
1153 return 0;
1154 }
1155}
1156
1157static ID
1158frame_called_id(rb_control_frame_t *cfp)
1159{
1161
1162 if (me) {
1163 return me->called_id;
1164 }
1165 else {
1166 return 0;
1167 }
1168}
1169
1182ID
1184{
1185 return frame_func_id(GET_EC()->cfp);
1186}
1187
1199ID
1201{
1202 return frame_called_id(GET_EC()->cfp);
1203}
1204
1205static rb_control_frame_t *
1206previous_frame(const rb_execution_context_t *ec)
1207{
1209 /* check if prev_cfp can be accessible */
1210 if ((void *)(ec->vm_stack + ec->vm_stack_size) == (void *)(prev_cfp)) {
1211 return 0;
1212 }
1213 return prev_cfp;
1214}
1215
1216static ID
1217prev_frame_callee(void)
1218{
1219 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1220 if (!prev_cfp) return 0;
1221 return frame_called_id(prev_cfp);
1222}
1223
1224static ID
1225prev_frame_func(void)
1226{
1227 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1228 if (!prev_cfp) return 0;
1229 return frame_func_id(prev_cfp);
1230}
1231
1238ID
1240{
1241 const rb_execution_context_t *ec = GET_EC();
1242 const rb_control_frame_t *cfp = ec->cfp;
1243 ID mid;
1244
1245 while (!(mid = frame_func_id(cfp)) &&
1247 !RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)));
1248 return mid;
1249}
1250
1251/*
1252 * call-seq:
1253 * append_features(mod) -> mod
1254 *
1255 * When this module is included in another, Ruby calls
1256 * #append_features in this module, passing it the receiving module
1257 * in _mod_. Ruby's default implementation is to add the constants,
1258 * methods, and module variables of this module to _mod_ if this
1259 * module has not already been added to _mod_ or one of its
1260 * ancestors. See also Module#include.
1261 */
1262
1263static VALUE
1264rb_mod_append_features(VALUE module, VALUE include)
1265{
1266 if (!CLASS_OR_MODULE_P(include)) {
1267 Check_Type(include, T_CLASS);
1268 }
1269 rb_include_module(include, module);
1270
1271 return module;
1272}
1273
1274/*
1275 * call-seq:
1276 * include(module, ...) -> self
1277 *
1278 * Invokes Module.append_features on each parameter in reverse order.
1279 */
1280
1281static VALUE
1282rb_mod_include(int argc, VALUE *argv, VALUE module)
1283{
1284 int i;
1285 ID id_append_features, id_included;
1286
1287 CONST_ID(id_append_features, "append_features");
1288 CONST_ID(id_included, "included");
1289
1291 for (i = 0; i < argc; i++)
1293 while (argc--) {
1294 rb_funcall(argv[argc], id_append_features, 1, module);
1295 rb_funcall(argv[argc], id_included, 1, module);
1296 }
1297 return module;
1298}
1299
1300/*
1301 * call-seq:
1302 * prepend_features(mod) -> mod
1303 *
1304 * When this module is prepended in another, Ruby calls
1305 * #prepend_features in this module, passing it the receiving module
1306 * in _mod_. Ruby's default implementation is to overlay the
1307 * constants, methods, and module variables of this module to _mod_
1308 * if this module has not already been added to _mod_ or one of its
1309 * ancestors. See also Module#prepend.
1310 */
1311
1312static VALUE
1313rb_mod_prepend_features(VALUE module, VALUE prepend)
1314{
1315 if (!CLASS_OR_MODULE_P(prepend)) {
1316 Check_Type(prepend, T_CLASS);
1317 }
1318 rb_prepend_module(prepend, module);
1319
1320 return module;
1321}
1322
1323/*
1324 * call-seq:
1325 * prepend(module, ...) -> self
1326 *
1327 * Invokes Module.prepend_features on each parameter in reverse order.
1328 */
1329
1330static VALUE
1331rb_mod_prepend(int argc, VALUE *argv, VALUE module)
1332{
1333 int i;
1334 ID id_prepend_features, id_prepended;
1335
1336 CONST_ID(id_prepend_features, "prepend_features");
1337 CONST_ID(id_prepended, "prepended");
1338
1340 for (i = 0; i < argc; i++)
1342 while (argc--) {
1343 rb_funcall(argv[argc], id_prepend_features, 1, module);
1344 rb_funcall(argv[argc], id_prepended, 1, module);
1345 }
1346 return module;
1347}
1348
1349static void
1350ensure_class_or_module(VALUE obj)
1351{
1352 if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
1354 "wrong argument type %"PRIsVALUE" (expected Class or Module)",
1355 rb_obj_class(obj));
1356 }
1357}
1358
1359static VALUE
1360hidden_identity_hash_new(void)
1361{
1362 VALUE hash = rb_ident_hash_new();
1363
1364 RBASIC_CLEAR_CLASS(hash); /* hide from ObjectSpace */
1365 return hash;
1366}
1367
1368static VALUE
1369refinement_superclass(VALUE superclass)
1370{
1371 if (RB_TYPE_P(superclass, T_MODULE)) {
1372 /* FIXME: Should ancestors of superclass be used here? */
1373 return rb_include_class_new(superclass, rb_cBasicObject);
1374 }
1375 else {
1376 return superclass;
1377 }
1378}
1379
1384void
1385rb_using_refinement(rb_cref_t *cref, VALUE klass, VALUE module)
1386{
1387 VALUE iclass, c, superclass = klass;
1388
1389 ensure_class_or_module(klass);
1390 Check_Type(module, T_MODULE);
1391 if (NIL_P(CREF_REFINEMENTS(cref))) {
1392 CREF_REFINEMENTS_SET(cref, hidden_identity_hash_new());
1393 }
1394 else {
1395 if (CREF_OMOD_SHARED(cref)) {
1396 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(cref)));
1397 CREF_OMOD_SHARED_UNSET(cref);
1398 }
1399 if (!NIL_P(c = rb_hash_lookup(CREF_REFINEMENTS(cref), klass))) {
1400 superclass = c;
1401 while (c && RB_TYPE_P(c, T_ICLASS)) {
1402 if (RBASIC(c)->klass == module) {
1403 /* already used refinement */
1404 return;
1405 }
1406 c = RCLASS_SUPER(c);
1407 }
1408 }
1409 }
1410 FL_SET(module, RMODULE_IS_OVERLAID);
1411 superclass = refinement_superclass(superclass);
1412 c = iclass = rb_include_class_new(module, superclass);
1414
1416 RCLASS_M_TBL(OBJ_WB_UNPROTECT(module)); /* TODO: check unprotecting */
1417
1418 module = RCLASS_SUPER(module);
1419 while (module && module != klass) {
1420 FL_SET(module, RMODULE_IS_OVERLAID);
1421 c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
1423 module = RCLASS_SUPER(module);
1424 }
1425 rb_hash_aset(CREF_REFINEMENTS(cref), klass, iclass);
1426}
1427
1428static int
1429using_refinement(VALUE klass, VALUE module, VALUE arg)
1430{
1431 rb_cref_t *cref = (rb_cref_t *) arg;
1432
1433 rb_using_refinement(cref, klass, module);
1434 return ST_CONTINUE;
1435}
1436
1437static void
1438using_module_recursive(const rb_cref_t *cref, VALUE klass)
1439{
1440 ID id_refinements;
1441 VALUE super, module, refinements;
1442
1443 super = RCLASS_SUPER(klass);
1444 if (super) {
1445 using_module_recursive(cref, super);
1446 }
1447 switch (BUILTIN_TYPE(klass)) {
1448 case T_MODULE:
1449 module = klass;
1450 break;
1451
1452 case T_ICLASS:
1453 module = RBASIC(klass)->klass;
1454 break;
1455
1456 default:
1457 rb_raise(rb_eTypeError, "wrong argument type %s (expected Module)",
1459 break;
1460 }
1461 CONST_ID(id_refinements, "__refinements__");
1462 refinements = rb_attr_get(module, id_refinements);
1463 if (NIL_P(refinements)) return;
1464 rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
1465}
1466
1471void
1472rb_using_module(const rb_cref_t *cref, VALUE module)
1473{
1474 Check_Type(module, T_MODULE);
1475 using_module_recursive(cref, module);
1477}
1478
1480VALUE
1482{
1483 ID id_refined_class;
1484
1485 CONST_ID(id_refined_class, "__refined_class__");
1486 return rb_attr_get(module, id_refined_class);
1487}
1488
1489static void
1490add_activated_refinement(VALUE activated_refinements,
1491 VALUE klass, VALUE refinement)
1492{
1493 VALUE iclass, c, superclass = klass;
1494
1495 if (!NIL_P(c = rb_hash_lookup(activated_refinements, klass))) {
1496 superclass = c;
1497 while (c && RB_TYPE_P(c, T_ICLASS)) {
1498 if (RBASIC(c)->klass == refinement) {
1499 /* already used refinement */
1500 return;
1501 }
1502 c = RCLASS_SUPER(c);
1503 }
1504 }
1505 FL_SET(refinement, RMODULE_IS_OVERLAID);
1506 superclass = refinement_superclass(superclass);
1507 c = iclass = rb_include_class_new(refinement, superclass);
1509 refinement = RCLASS_SUPER(refinement);
1510 while (refinement && refinement != klass) {
1511 FL_SET(refinement, RMODULE_IS_OVERLAID);
1512 c = RCLASS_SET_SUPER(c, rb_include_class_new(refinement, RCLASS_SUPER(c)));
1514 refinement = RCLASS_SUPER(refinement);
1515 }
1516 rb_hash_aset(activated_refinements, klass, iclass);
1517}
1518
1519/*
1520 * call-seq:
1521 * refine(mod) { block } -> module
1522 *
1523 * Refine <i>mod</i> in the receiver.
1524 *
1525 * Returns a module, where refined methods are defined.
1526 */
1527
1528static VALUE
1529rb_mod_refine(VALUE module, VALUE klass)
1530{
1531 VALUE refinement;
1532 ID id_refinements, id_activated_refinements,
1533 id_refined_class, id_defined_at;
1534 VALUE refinements, activated_refinements;
1535 rb_thread_t *th = GET_THREAD();
1537
1539 rb_raise(rb_eArgError, "no block given");
1540 }
1541 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
1542 rb_raise(rb_eArgError, "can't pass a Proc as a block to Module#refine");
1543 }
1544
1545 ensure_class_or_module(klass);
1546 if (RB_TYPE_P(klass, T_MODULE)) {
1548 }
1549 CONST_ID(id_refinements, "__refinements__");
1550 refinements = rb_attr_get(module, id_refinements);
1551 if (NIL_P(refinements)) {
1552 refinements = hidden_identity_hash_new();
1553 rb_ivar_set(module, id_refinements, refinements);
1554 }
1555 CONST_ID(id_activated_refinements, "__activated_refinements__");
1556 activated_refinements = rb_attr_get(module, id_activated_refinements);
1557 if (NIL_P(activated_refinements)) {
1558 activated_refinements = hidden_identity_hash_new();
1559 rb_ivar_set(module, id_activated_refinements,
1560 activated_refinements);
1561 }
1562 refinement = rb_hash_lookup(refinements, klass);
1563 if (NIL_P(refinement)) {
1564 VALUE superclass = refinement_superclass(klass);
1565 refinement = rb_module_new();
1566 RCLASS_SET_SUPER(refinement, superclass);
1567 FL_SET(refinement, RMODULE_IS_REFINEMENT);
1568 CONST_ID(id_refined_class, "__refined_class__");
1569 rb_ivar_set(refinement, id_refined_class, klass);
1570 CONST_ID(id_defined_at, "__defined_at__");
1571 rb_ivar_set(refinement, id_defined_at, module);
1572 rb_hash_aset(refinements, klass, refinement);
1573 add_activated_refinement(activated_refinements, klass, refinement);
1574 }
1575 rb_yield_refine_block(refinement, activated_refinements);
1576 return refinement;
1577}
1578
1579static void
1580ignored_block(VALUE module, const char *klass)
1581{
1582 const char *anon = "";
1583 Check_Type(module, T_MODULE);
1584 if (!RTEST(rb_search_class_path(module))) {
1585 anon = ", maybe for Module.new";
1586 }
1587 rb_warn("%s""using doesn't call the given block""%s.", klass, anon);
1588}
1589
1590/*
1591 * call-seq:
1592 * using(module) -> self
1593 *
1594 * Import class refinements from <i>module</i> into the current class or
1595 * module definition.
1596 */
1597
1598static VALUE
1599mod_using(VALUE self, VALUE module)
1600{
1601 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1602
1603 if (prev_frame_func()) {
1605 "Module#using is not permitted in methods");
1606 }
1607 if (prev_cfp && prev_cfp->self != self) {
1608 rb_raise(rb_eRuntimeError, "Module#using is not called on self");
1609 }
1610 if (rb_block_given_p()) {
1611 ignored_block(module, "Module#");
1612 }
1613 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1614 return self;
1615}
1616
1617static int
1618used_modules_i(VALUE _, VALUE mod, VALUE ary)
1619{
1620 ID id_defined_at;
1621 CONST_ID(id_defined_at, "__defined_at__");
1622 while (FL_TEST(rb_class_of(mod), RMODULE_IS_REFINEMENT)) {
1623 rb_ary_push(ary, rb_attr_get(rb_class_of(mod), id_defined_at));
1624 mod = RCLASS_SUPER(mod);
1625 }
1626 return ST_CONTINUE;
1627}
1628
1629/*
1630 * call-seq:
1631 * used_modules -> array
1632 *
1633 * Returns an array of all modules used in the current scope. The ordering
1634 * of modules in the resulting array is not defined.
1635 *
1636 * module A
1637 * refine Object do
1638 * end
1639 * end
1640 *
1641 * module B
1642 * refine Object do
1643 * end
1644 * end
1645 *
1646 * using A
1647 * using B
1648 * p Module.used_modules
1649 *
1650 * <em>produces:</em>
1651 *
1652 * [B, A]
1653 */
1654static VALUE
1655rb_mod_s_used_modules(VALUE _)
1656{
1657 const rb_cref_t *cref = rb_vm_cref();
1658 VALUE ary = rb_ary_new();
1659
1660 while (cref) {
1661 if (!NIL_P(CREF_REFINEMENTS(cref))) {
1662 rb_hash_foreach(CREF_REFINEMENTS(cref), used_modules_i, ary);
1663 }
1664 cref = CREF_NEXT(cref);
1665 }
1666
1667 return rb_funcall(ary, rb_intern("uniq"), 0);
1668}
1669
1680void
1682{
1685}
1686
1687void
1688rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
1689{
1691 rb_funcallv_kw(obj, idInitialize, argc, argv, kw_splat);
1692}
1693
1700void
1702{
1704}
1705
1706/*
1707 * call-seq:
1708 * extend_object(obj) -> obj
1709 *
1710 * Extends the specified object by adding this module's constants and
1711 * methods (which are added as singleton methods). This is the callback
1712 * method used by Object#extend.
1713 *
1714 * module Picky
1715 * def Picky.extend_object(o)
1716 * if String === o
1717 * puts "Can't add Picky to a String"
1718 * else
1719 * puts "Picky added to #{o.class}"
1720 * super
1721 * end
1722 * end
1723 * end
1724 * (s = Array.new).extend Picky # Call Object.extend
1725 * (s = "quick brown fox").extend Picky
1726 *
1727 * <em>produces:</em>
1728 *
1729 * Picky added to Array
1730 * Can't add Picky to a String
1731 */
1732
1733static VALUE
1734rb_mod_extend_object(VALUE mod, VALUE obj)
1735{
1737 return obj;
1738}
1739
1740/*
1741 * call-seq:
1742 * obj.extend(module, ...) -> obj
1743 *
1744 * Adds to _obj_ the instance methods from each module given as a
1745 * parameter.
1746 *
1747 * module Mod
1748 * def hello
1749 * "Hello from Mod.\n"
1750 * end
1751 * end
1752 *
1753 * class Klass
1754 * def hello
1755 * "Hello from Klass.\n"
1756 * end
1757 * end
1758 *
1759 * k = Klass.new
1760 * k.hello #=> "Hello from Klass.\n"
1761 * k.extend(Mod) #=> #<Klass:0x401b3bc8>
1762 * k.hello #=> "Hello from Mod.\n"
1763 */
1764
1765static VALUE
1766rb_obj_extend(int argc, VALUE *argv, VALUE obj)
1767{
1768 int i;
1769 ID id_extend_object, id_extended;
1770
1771 CONST_ID(id_extend_object, "extend_object");
1772 CONST_ID(id_extended, "extended");
1773
1775 for (i = 0; i < argc; i++)
1777 while (argc--) {
1778 rb_funcall(argv[argc], id_extend_object, 1, obj);
1779 rb_funcall(argv[argc], id_extended, 1, obj);
1780 }
1781 return obj;
1782}
1783
1784/*
1785 * call-seq:
1786 * include(module, ...) -> self
1787 *
1788 * Invokes Module.append_features on each parameter in turn.
1789 * Effectively adds the methods and constants in each module to the
1790 * receiver.
1791 */
1792
1793static VALUE
1794top_include(int argc, VALUE *argv, VALUE self)
1795{
1796 rb_thread_t *th = GET_THREAD();
1797
1798 if (th->top_wrapper) {
1799 rb_warning("main.include in the wrapped load is effective only in wrapper module");
1800 return rb_mod_include(argc, argv, th->top_wrapper);
1801 }
1802 return rb_mod_include(argc, argv, rb_cObject);
1803}
1804
1805/*
1806 * call-seq:
1807 * using(module) -> self
1808 *
1809 * Import class refinements from <i>module</i> into the scope where
1810 * #using is called.
1811 */
1812
1813static VALUE
1814top_using(VALUE self, VALUE module)
1815{
1816 const rb_cref_t *cref = rb_vm_cref();
1817 rb_control_frame_t *prev_cfp = previous_frame(GET_EC());
1818
1819 if (CREF_NEXT(cref) || (prev_cfp && rb_vm_frame_method_entry(prev_cfp))) {
1820 rb_raise(rb_eRuntimeError, "main.using is permitted only at toplevel");
1821 }
1822 if (rb_block_given_p()) {
1823 ignored_block(module, "main.");
1824 }
1825 rb_using_module(rb_vm_cref_replace_with_duplicated_cref(), module);
1826 return self;
1827}
1828
1829static const VALUE *
1830errinfo_place(const rb_execution_context_t *ec)
1831{
1832 const rb_control_frame_t *cfp = ec->cfp;
1833 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1834
1835 while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
1836 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1837 if (cfp->iseq->body->type == ISEQ_TYPE_RESCUE) {
1838 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1839 }
1840 else if (cfp->iseq->body->type == ISEQ_TYPE_ENSURE &&
1843 return &cfp->ep[VM_ENV_INDEX_LAST_LVAR];
1844 }
1845 }
1847 }
1848 return 0;
1849}
1850
1851VALUE
1853{
1854 const VALUE *ptr = errinfo_place(ec);
1855 if (ptr) {
1856 return *ptr;
1857 }
1858 else {
1859 return ec->errinfo;
1860 }
1861}
1862
1863static VALUE
1864get_errinfo(void)
1865{
1866 return get_ec_errinfo(GET_EC());
1867}
1868
1869static VALUE
1870errinfo_getter(ID id, VALUE *_)
1871{
1872 return get_errinfo();
1873}
1874
1881VALUE
1883{
1884 return GET_EC()->errinfo;
1885}
1886
1895void
1897{
1899 rb_raise(rb_eTypeError, "assigning non-exception to $!");
1900 }
1901 GET_EC()->errinfo = err;
1902}
1903
1904static VALUE
1905errat_getter(ID id, VALUE *_)
1906{
1907 VALUE err = get_errinfo();
1908 if (!NIL_P(err)) {
1909 return rb_get_backtrace(err);
1910 }
1911 else {
1912 return Qnil;
1913 }
1914}
1915
1916static void
1917errat_setter(VALUE val, ID id, VALUE *var)
1918{
1919 VALUE err = get_errinfo();
1920 if (NIL_P(err)) {
1921 rb_raise(rb_eArgError, "$! not set");
1922 }
1923 set_backtrace(err, val);
1924}
1925
1926/*
1927 * call-seq:
1928 * __method__ -> symbol
1929 *
1930 * Returns the name at the definition of the current method as a
1931 * Symbol.
1932 * If called outside of a method, it returns <code>nil</code>.
1933 *
1934 */
1935
1936static VALUE
1937rb_f_method_name(VALUE _)
1938{
1939 ID fname = prev_frame_func(); /* need *method* ID */
1940
1941 if (fname) {
1942 return ID2SYM(fname);
1943 }
1944 else {
1945 return Qnil;
1946 }
1947}
1948
1949/*
1950 * call-seq:
1951 * __callee__ -> symbol
1952 *
1953 * Returns the called name of the current method as a Symbol.
1954 * If called outside of a method, it returns <code>nil</code>.
1955 *
1956 */
1957
1958static VALUE
1959rb_f_callee_name(VALUE _)
1960{
1961 ID fname = prev_frame_callee(); /* need *callee* ID */
1962
1963 if (fname) {
1964 return ID2SYM(fname);
1965 }
1966 else {
1967 return Qnil;
1968 }
1969}
1970
1971/*
1972 * call-seq:
1973 * __dir__ -> string
1974 *
1975 * Returns the canonicalized absolute path of the directory of the file from
1976 * which this method is called. It means symlinks in the path is resolved.
1977 * If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1978 * The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.
1979 *
1980 */
1981static VALUE
1982f_current_dirname(VALUE _)
1983{
1985 if (NIL_P(base)) {
1986 return Qnil;
1987 }
1988 base = rb_file_dirname(base);
1989 return base;
1990}
1991
1992/*
1993 * call-seq:
1994 * global_variables -> array
1995 *
1996 * Returns an array of the names of global variables.
1997 *
1998 * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
1999 */
2000
2001static VALUE
2002f_global_variables(VALUE _)
2003{
2004 return rb_f_global_variables();
2005}
2006
2007/*
2008 * call-seq:
2009 * trace_var(symbol, cmd ) -> nil
2010 * trace_var(symbol) {|val| block } -> nil
2011 *
2012 * Controls tracing of assignments to global variables. The parameter
2013 * +symbol+ identifies the variable (as either a string name or a
2014 * symbol identifier). _cmd_ (which may be a string or a
2015 * +Proc+ object) or block is executed whenever the variable
2016 * is assigned. The block or +Proc+ object receives the
2017 * variable's new value as a parameter. Also see
2018 * Kernel::untrace_var.
2019 *
2020 * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
2021 * $_ = "hello"
2022 * $_ = ' there'
2023 *
2024 * <em>produces:</em>
2025 *
2026 * $_ is now 'hello'
2027 * $_ is now ' there'
2028 */
2029
2030static VALUE
2031f_trace_var(int c, const VALUE *a, VALUE _)
2032{
2033 return rb_f_trace_var(c, a);
2034}
2035
2036/*
2037 * call-seq:
2038 * untrace_var(symbol [, cmd] ) -> array or nil
2039 *
2040 * Removes tracing for the specified command on the given global
2041 * variable and returns +nil+. If no command is specified,
2042 * removes all tracing for that variable and returns an array
2043 * containing the commands actually removed.
2044 */
2045
2046static VALUE
2047f_untrace_var(int c, const VALUE *a, VALUE _)
2048{
2049 return rb_f_untrace_var(c, a);
2050}
2051
2052void
2054{
2055 rb_define_virtual_variable("$@", errat_getter, errat_setter);
2056 rb_define_virtual_variable("$!", errinfo_getter, 0);
2057
2058 rb_define_global_function("raise", f_raise, -1);
2059 rb_define_global_function("fail", f_raise, -1);
2060
2061 rb_define_global_function("global_variables", f_global_variables, 0);
2062
2063 rb_define_global_function("__method__", rb_f_method_name, 0);
2064 rb_define_global_function("__callee__", rb_f_callee_name, 0);
2065 rb_define_global_function("__dir__", f_current_dirname, 0);
2066
2067 rb_define_method(rb_cModule, "include", rb_mod_include, -1);
2068 rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
2069
2070 rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
2071 rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
2072 rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
2073 rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
2074 rb_define_private_method(rb_cModule, "using", mod_using, 1);
2075 rb_define_singleton_method(rb_cModule, "used_modules",
2076 rb_mod_s_used_modules, 0);
2077 rb_undef_method(rb_cClass, "refine");
2078
2079 rb_undef_method(rb_cClass, "module_function");
2080
2081 Init_vm_eval();
2083
2084 rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
2085 rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
2086
2088 "include", top_include, -1);
2090 "using", top_using, 1);
2091
2092 rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
2093
2094 rb_define_global_function("trace_var", f_trace_var, -1);
2095 rb_define_global_function("untrace_var", f_untrace_var, -1);
2096
2098 rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region");
2099
2100 id_signo = rb_intern_const("signo");
2101 id_status = rb_intern_const("status");
2102}
VALUE e_proc(VALUE)
Definition: cont.c:1529
#define mod(x, y)
Definition: date_strftime.c:28
struct RIMemo * ptr
Definition: debug.c:65
void rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
Definition: eval.c:1688
VALUE rb_eLocalJumpError
Definition: eval.c:34
int rb_vm_cframe_empty_keyword_p(const rb_control_frame_t *cfp)
Definition: vm.c:109
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition: eval.c:288
VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec)
Definition: eval.c:1852
void Init_eval(void)
Definition: eval.c:2053
@ raise_opt_cause
Definition: eval.c:702
@ raise_max_opt
Definition: eval.c:702
#define get_ec_errinfo(ec)
Definition: eval.c:481
void rb_ec_clear_all_trace_func(const rb_execution_context_t *ec)
Definition: vm_trace.c:280
#define exception_error
Definition: eval.c:41
VALUE rb_f_raise(int argc, VALUE *argv)
Definition: eval.c:727
void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec)
Definition: vm_trace.c:274
VALUE rb_eSysStackError
Definition: eval.c:35
ID ruby_static_id_signo
Definition: eval.c:37
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:46
#define id_cause
Definition: eval.c:39
VALUE rb_eThreadError
Definition: eval.c:924
int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp)
Definition: vm.c:102
ID ruby_static_id_status
Definition: eval.c:37
NORETURN(void rb_raise_jump(VALUE, VALUE))
#define warn_print_str(x)
Definition: eval_error.c:23
#define unknown_longjmp_status(status)
Definition: eval_error.c:432
void rb_include_module(VALUE, VALUE)
Definition: class.c:882
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition: eval.c:1701
void rb_prepend_module(VALUE, VALUE)
Definition: class.c:1017
VALUE rb_singleton_class(VALUE)
Returns the singleton class of obj.
Definition: class.c:1743
VALUE rb_include_class_new(VALUE, VALUE)
Definition: class.c:838
VALUE rb_module_new(void)
Definition: class.c:771
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:438
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1200
ID rb_frame_this_func(void)
The original name of the current method.
Definition: eval.c:1183
void rb_need_block(void)
Declares that the current method needs a block.
Definition: eval.c:932
ID rb_frame_last_func(void)
Returns the ID of the last method in the call stack.
Definition: eval.c:1239
void rb_undef_method(VALUE, const char *)
Definition: class.c:1593
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:898
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *)
Definition: class.c:1904
int rb_empty_keyword_given_p(void)
Definition: eval.c:919
VALUE rb_mKernel
Kernel module.
Definition: ruby.h:2000
VALUE rb_vrescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2, va_list args)
An equivalent of rescue clause.
Definition: eval.c:977
VALUE rb_cClass
Class class.
Definition: ruby.h:2018
VALUE rb_cBasicObject
BasicObject class.
Definition: ruby.h:2011
VALUE rb_cObject
Object class.
Definition: ruby.h:2012
VALUE rb_cModule
Module class.
Definition: ruby.h:2036
int rb_keyword_given_p(void)
Definition: eval.c:911
@ RMODULE_IS_REFINEMENT
Definition: ruby.h:956
@ RMODULE_IS_OVERLAID
Definition: ruby.h:955
int ruby_exec_node(void *n)
Runs the given compiled source.
Definition: eval.c:341
int ruby_setup(void)
Initializes the VM and builtin libraries.
Definition: eval.c:56
void ruby_finalize(void)
Runs the VM finalization processes.
Definition: eval.c:163
int ruby_cleanup(volatile int ex)
Destructs the VM.
Definition: eval.c:181
VALUE rb_get_backtrace(VALUE info)
Definition: error.c:1231
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
Definition: eval.c:962
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:668
VALUE rb_eSystemExit
Definition: error.c:917
VALUE rb_eStandardError
Definition: error.c:921
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1896
void rb_interrupt(void)
Raises an Interrupt exception.
Definition: eval.c:697
VALUE rb_eTypeError
Definition: error.c:924
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Definition: error.c:2982
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1072
VALUE rb_eFatal
Definition: error.c:920
VALUE rb_eInterrupt
Definition: error.c:918
VALUE rb_make_exception(int argc, const VALUE *argv)
Make an Exception object from the list of arguments in a manner similar to Kernel#raise.
Definition: eval.c:851
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition: eval.c:684
VALUE rb_eRuntimeError
Definition: error.c:922
void rb_warn(const char *fmt,...)
Definition: error.c:315
VALUE rb_exc_new(VALUE, const char *, long)
Definition: error.c:961
VALUE rb_eArgError
Definition: error.c:925
VALUE rb_eException
Definition: error.c:916
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
An equivalent of rescue clause.
Definition: eval.c:1047
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1115
VALUE rb_errinfo(void)
The current exception in the current thread.
Definition: eval.c:1882
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:884
ID ruby_static_id_cause
Definition: error.c:947
VALUE rb_eSignal
Definition: error.c:919
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Calls #initialize method of obj with the given arguments.
Definition: eval.c:1681
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
VALUE rb_obj_dup(VALUE)
Equivalent to Object#dup in Ruby.
Definition: object.c:420
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:692
int ruby_run_node(void *n)
Runs the given compiled source and exits this process.
Definition: eval.c:327
void ruby_init(void)
Calls ruby_setup() and check error.
Definition: eval.c:94
void * ruby_options(int argc, char **argv)
Processes command line arguments and compiles the Ruby source to execute.
Definition: eval.c:115
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition: eval.c:306
void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause)
Definition: eval.c:639
#define ruby_debug
int rb_ec_set_raised(rb_execution_context_t *ec)
Definition: thread.c:2343
VALUE rb_hash_lookup(VALUE, VALUE)
Definition: hash.c:2063
#define MEMCPY(p1, p2, type, n)
void rb_hash_foreach(VALUE, int(*)(VALUE, VALUE, VALUE), VALUE)
VALUE rb_f_trace_var(int, const VALUE *)
Definition: variable.c:528
#define NULL
#define FL_SINGLETON
#define ATOMIC_VALUE_EXCHANGE(var, val)
#define RBASIC_CLEAR_CLASS(obj)
#define TAG_RAISE
#define UNLIMITED_ARGUMENTS
#define _(args)
#define OBJ_WB_UNPROTECT(x)
#define RTEST(v)
void rb_define_virtual_variable(const char *, rb_gvar_getter_t *, rb_gvar_setter_t *)
Definition: variable.c:511
void mjit_finish(_Bool close_handle_p)
#define RUBY_EVENT_RAISE
VALUE rb_iseq_eval_main(const rb_iseq_t *iseq)
Definition: vm.c:2173
#define TAG_NONE
rb_control_frame_t struct rb_calling_info const struct rb_call_info VALUE block_handler
#define RCLASS_SUPER(c)
#define FL_TEST(x, f)
#define RBASIC(obj)
#define RUBY_DTRACE_HOOK(name, arg)
void rb_threadptr_check_signal(rb_thread_t *mth)
Definition: thread.c:4317
#define VM_ENV_INDEX_LAST_LVAR
void rb_define_private_method(VALUE, const char *, VALUE(*)(), int)
#define TAG_RETRY
void Init_heap(void)
Definition: gc.c:2924
rb_control_frame_t * cfp
void Init_vm_eval(void)
Definition: vm_eval.c:2472
void rb_define_global_function(const char *, VALUE(*)(), int)
VALUE rb_const_list(void *)
Definition: variable.c:2598
#define Qundef
void rb_vm_encoded_insn_data_table_init(void)
Definition: iseq.c:3085
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1070
const VALUE VALUE obj
void Init_eval_method(void)
Definition: vm_method.c:2298
#define FL_SET(x, f)
VALUE rb_funcallv_kw(VALUE, ID, int, const VALUE *, int)
Definition: vm_eval.c:962
void rb_objspace_call_finalizer(struct rb_objspace *)
Definition: gc.c:3456
VALUE rb_ec_backtrace_object(const rb_execution_context_t *ec)
Definition: vm_backtrace.c:557
#define INTERNAL_EXCEPTION_P(exc)
#define rb_vm_register_special_exception(sp, e, m)
#define GET_EC()
VALUE rb_ident_hash_new(void)
Definition: hash.c:4278
#define NIL_P(v)
#define id_status
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:505
const rb_callable_method_entry_t * me
#define numberof(array)
#define ID2SYM(x)
#define EC_EXEC_TAG()
@ block_handler_type_iseq
VALUE rb_f_untrace_var(int, const VALUE *)
Definition: variable.c:574
const char size_t n
#define rb_exc_new_cstr(klass, ptr)
#define rb_intern_const(str)
@ ruby_error_stackfatal
const char const char *typedef unsigned long VALUE
VALUE rb_ary_push(VALUE, VALUE)
Definition: array.c:1195
#define EC_PUSH_TAG(ec)
#define EC_JUMP_TAG(ec, st)
int ruby_vm_destruct(ruby_vm_t *vm)
Definition: vm.c:2329
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_)
#define EXIT_SUCCESS
void * rb_jmpbuf_t[5]
VALUE rb_hash_dup(VALUE)
Definition: hash.c:1564
#define T_MODULE
#define GET_VM()
uint32_t i
#define OBJ_FROZEN(x)
#define EXIT_FAILURE
#define TAG_FATAL
const char * rb_obj_classname(VALUE)
Definition: variable.c:289
#define OBJ_FREEZE(x)
#define RUBY_VM_CHECK_INTS(ec)
#define RB_OBJ_WRITE(a, slot, b)
#define va_end(v)
#define T_ICLASS
#define T_HASH
#define rb_ec_raised_clear(ec)
#define THROW_DATA_P(err)
VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements)
Definition: vm_eval.c:1885
__gnuc_va_list va_list
#define NUM2INT(x)
void rb_define_singleton_method(VALUE, const char *, VALUE(*)(), int)
#define GET_THREAD()
#define PRIsVALUE
void rb_thread_terminate_all(void)
Definition: thread.c:554
#define rb_funcall(recv, mid, argc,...)
#define FIX2INT(x)
int VALUE v
VALUE rb_ary_new(void)
Definition: array.c:723
#define rb_exc_new3
#define EC_POP_TAG()
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1317
#define rb_intern(str)
#define va_arg(v, l)
#define PASS_PASSED_BLOCK_HANDLER()
#define UNREACHABLE_RETURN(val)
#define va_start(v, l)
const rb_iseq_t * iseq
void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
Definition: thread.c:537
#define CONST_ID(var, str)
#define TRUE
#define RUBY_EVENT_C_RETURN
#define FALSE
#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp)
#define Qtrue
void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp)
Definition: vm.c:604
#define UNLIKELY(x)
#define RB_NO_KEYWORDS
#define RCLASS_M_TBL(c)
void rb_thread_stop_timer_thread(void)
Definition: thread.c:4416
VALUE rb_mod_constants(int, const VALUE *, VALUE)
Definition: variable.c:2630
#define id_signo
void ruby_init_stack(volatile VALUE *)
void exit(int __status) __attribute__((__noreturn__))
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1084
#define Qnil
#define Qfalse
void * rb_mod_const_of(VALUE, void *)
Definition: variable.c:2576
const char * rb_source_location_cstr(int *pline)
Definition: vm.c:1376
#define T_OBJECT
void * rb_mod_const_at(VALUE, void *)
Definition: variable.c:2563
#define RCLASS_REFINED_CLASS(c)
void rb_threadptr_interrupt(rb_thread_t *th)
Definition: thread.c:505
#define RB_TYPE_P(obj, type)
void * ruby_process_options(int, char **)
Definition: ruby.c:2412
#define INT2FIX(i)
#define SPECIAL_CONST_P(x)
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1481
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1440
const VALUE * argv
#define VM_BLOCK_HANDLER_NONE
int rb_ec_reset_raised(rb_execution_context_t *ec)
Definition: thread.c:2353
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1300
#define FIXNUM_P(f)
#define T_CLASS
rb_cref_t * rb_vm_cref_replace_with_duplicated_cref(void)
Definition: vm.c:1391
void Init_vm_objects(void)
Definition: vm.c:3326
rb_cref_t * rb_vm_cref(void)
Definition: vm.c:1384
#define Check_Type(v, t)
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
Definition: hash.c:2852
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:2366
#define STACK_UPPER(x, a, b)
#define rb_check_arity
void rb_call_inits(void)
Definition: inits.c:19
#define SAVE_ROOT_JMPBUF(th, stmt)
void ruby_sig_finalize(void)
Definition: signal.c:1470
void ruby_default_signal(int)
Definition: signal.c:402
VALUE rb_f_global_variables(void)
Definition: variable.c:728
VALUE rb_sprintf(const char *,...) __attribute__((format(printf
unsigned long ID
void rb_vm_trap_exit(rb_vm_t *vm)
Definition: signal.c:1060
#define RHASH_EMPTY_P(h)
const char *void rb_warning(const char *,...) __attribute__((format(printf
const rb_iseq_t const VALUE exc
void Init_BareVM(void)
Definition: vm.c:3303
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:93
#define BUILTIN_TYPE(x)
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
VALUE rb_current_realfilepath(void)
Definition: vm_eval.c:2462
#define RCLASS_REFINED_BY_ANY
VALUE rb_search_class_path(VALUE)
Definition: variable.c:175
VALUE rb_file_dirname(VALUE fname)
Definition: file.c:4706
unsigned long VALUE
Definition: ruby.h:102
ID called_id
struct rb_method_definition_struct *const def
const VALUE owner
CREF (Class REFerence)
VALUE(* e_proc)(VALUE)
VALUE marker
VALUE data2
struct rb_ensure_entry entry
struct rb_ensure_list * next
struct rb_vm_protect_tag * protect_tag
enum rb_iseq_constant_body::iseq_type type
struct rb_iseq_constant_body * body
rb_execution_context_t * ec
enum rb_thread_status status
struct rb_vm_protect_tag * prev
VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
Definition: vm.c:115
VALUE rb_vm_top_self(void)
Definition: vm.c:3349
MJIT_FUNC_EXPORTED void rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:1471
MJIT_STATIC void rb_vm_pop_frame(rb_execution_context_t *ec)
MJIT_STATIC const rb_callable_method_entry_t * rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
MJIT_STATIC void rb_error_arity(int argc, int min, int max)