Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
vm_method.c
Go to the documentation of this file.
1/*
2 * This file is included by vm.c
3 */
4
5#include "id_table.h"
6
7#define METHOD_DEBUG 0
8
9#if OPT_GLOBAL_METHOD_CACHE
10#ifndef GLOBAL_METHOD_CACHE_SIZE
11#define GLOBAL_METHOD_CACHE_SIZE 0x800
12#endif
13#define LSB_ONLY(x) ((x) & ~((x) - 1))
14#define POWER_OF_2_P(x) ((x) == LSB_ONLY(x))
15#if !POWER_OF_2_P(GLOBAL_METHOD_CACHE_SIZE)
16# error GLOBAL_METHOD_CACHE_SIZE must be power of 2
17#endif
18#ifndef GLOBAL_METHOD_CACHE_MASK
19#define GLOBAL_METHOD_CACHE_MASK (GLOBAL_METHOD_CACHE_SIZE-1)
20#endif
21
22#define GLOBAL_METHOD_CACHE_KEY(c,m) ((((c)>>3)^(m))&(global_method_cache.mask))
23#define GLOBAL_METHOD_CACHE(c,m) (global_method_cache.entries + GLOBAL_METHOD_CACHE_KEY(c,m))
24#else
25#define GLOBAL_METHOD_CACHE(c,m) (rb_bug("global method cache disabled improperly"), NULL)
26#endif
27
28static int vm_redefinition_check_flag(VALUE klass);
29static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass);
30
31#define object_id idObject_id
32#define added idMethod_added
33#define singleton_added idSingleton_method_added
34#define removed idMethod_removed
35#define singleton_removed idSingleton_method_removed
36#define undefined idMethod_undefined
37#define singleton_undefined idSingleton_method_undefined
38#define attached id__attached__
39
46};
47
48#if OPT_GLOBAL_METHOD_CACHE
49static struct {
50 unsigned int size;
51 unsigned int mask;
52 struct cache_entry *entries;
53} global_method_cache = {
54 GLOBAL_METHOD_CACHE_SIZE,
55 GLOBAL_METHOD_CACHE_MASK,
56};
57#endif
58
59#define ruby_running (GET_VM()->running)
60/* int ruby_running = 0; */
61
62static void
63rb_class_clear_method_cache(VALUE klass, VALUE arg)
64{
65 VALUE old_serial = *(rb_serial_t *)arg;
66 if (RCLASS_SERIAL(klass) > old_serial) {
67 return;
68 }
69
72
73 if (BUILTIN_TYPE(klass) == T_ICLASS) {
75 if (table) {
76 rb_id_table_clear(table);
77 }
78 }
79 else {
81 }
82
83 rb_class_foreach_subclass(klass, rb_class_clear_method_cache, arg);
84}
85
86void
88{
90}
91
92void
94{
95 if (klass && klass != Qundef) {
96 int global = klass == rb_cBasicObject || klass == rb_cObject || klass == rb_mKernel;
97
98 RUBY_DTRACE_HOOK(METHOD_CACHE_CLEAR, (global ? "global" : rb_class2name(klass)));
99
100 if (global) {
102 }
103 else {
104 rb_serial_t old_serial = PREV_CLASS_SERIAL();
105 rb_class_clear_method_cache(klass, (VALUE)&old_serial);
106 }
107 }
108
109 if (klass == rb_mKernel) {
110 rb_subclass_entry_t *entry = RCLASS_EXT(klass)->subclasses;
111
112 for (; entry != NULL; entry = entry->next) {
113 struct rb_id_table *table = RCLASS_CALLABLE_M_TBL(entry->klass);
114 if (table)rb_id_table_clear(table);
115 }
116 }
117}
118
119VALUE
121{
123
125}
126
127static void
128rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_visibility_t visi)
129{
130 rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, (void *)1, visi);
131}
132
133void
135{
136 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc);
137 if (func != rb_f_notimplement) {
139 opt.func = func;
140 opt.argc = argc;
141 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
142 }
143 else {
144 rb_define_notimplement_method_id(klass, mid, visi);
145 }
146}
147
148static void
149rb_method_definition_release(rb_method_definition_t *def, int complemented)
150{
151 if (def != NULL) {
152 const int alias_count = def->alias_count;
153 const int complemented_count = def->complemented_count;
154 VM_ASSERT(alias_count >= 0);
155 VM_ASSERT(complemented_count >= 0);
156
157 if (alias_count + complemented_count == 0) {
158 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d,%d (remove)\n", (void *)def,
159 rb_id2name(def->original_id), alias_count, complemented_count);
160 VM_ASSERT(def->type == VM_METHOD_TYPE_BMETHOD ? def->body.bmethod.hooks == NULL : TRUE);
161 xfree(def);
162 }
163 else {
164 if (complemented) def->complemented_count--;
165 else if (def->alias_count > 0) def->alias_count--;
166
167 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d,%d->%d (dec)\n", (void *)def, rb_id2name(def->original_id),
168 alias_count, def->alias_count, complemented_count, def->complemented_count);
169 }
170 }
171}
172
173void
175{
176 rb_method_definition_release(me->def, METHOD_ENTRY_COMPLEMENTED(me));
177}
178
179static inline rb_method_entry_t *search_method(VALUE klass, ID id, VALUE *defined_class_ptr);
181
182static inline rb_method_entry_t *
183lookup_method_table(VALUE klass, ID id)
184{
185 st_data_t body;
186 struct rb_id_table *m_tbl = RCLASS_M_TBL(klass);
187
188 if (rb_id_table_lookup(m_tbl, id, &body)) {
189 return (rb_method_entry_t *) body;
190 }
191 else {
192 return 0;
193 }
194}
195
196static VALUE
197(*call_cfunc_invoker_func(int argc))(VALUE recv, int argc, const VALUE *, VALUE (*func)(ANYARGS))
198{
199 switch (argc) {
200 case -2: return &call_cfunc_m2;
201 case -1: return &call_cfunc_m1;
202 case 0: return &call_cfunc_0;
203 case 1: return &call_cfunc_1;
204 case 2: return &call_cfunc_2;
205 case 3: return &call_cfunc_3;
206 case 4: return &call_cfunc_4;
207 case 5: return &call_cfunc_5;
208 case 6: return &call_cfunc_6;
209 case 7: return &call_cfunc_7;
210 case 8: return &call_cfunc_8;
211 case 9: return &call_cfunc_9;
212 case 10: return &call_cfunc_10;
213 case 11: return &call_cfunc_11;
214 case 12: return &call_cfunc_12;
215 case 13: return &call_cfunc_13;
216 case 14: return &call_cfunc_14;
217 case 15: return &call_cfunc_15;
218 default:
219 rb_bug("call_cfunc_func: unsupported length: %d", argc);
220 }
221}
222
223static void
224setup_method_cfunc_struct(rb_method_cfunc_t *cfunc, VALUE (*func)(), int argc)
225{
226 cfunc->func = func;
227 cfunc->argc = argc;
228 cfunc->invoker = call_cfunc_invoker_func(argc);
229}
230
233{
234 *(rb_method_definition_t **)&me->def = def;
235
236 if (opts != NULL) {
237 switch (def->type) {
239 {
240 rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
241 rb_cref_t *method_cref, *cref = iseq_body->cref;
242
243 /* setup iseq first (before invoking GC) */
244 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq_body->iseqptr);
245
246 if (0) vm_cref_dump("rb_method_definition_create", cref);
247
248 if (cref) {
249 method_cref = cref;
250 }
251 else {
252 method_cref = vm_cref_new_toplevel(GET_EC()); /* TODO: can we reuse? */
253 }
254
255 RB_OBJ_WRITE(me, &def->body.iseq.cref, method_cref);
256 return;
257 }
259 {
260 rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts;
261 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
262 return;
263 }
266 {
267 const rb_execution_context_t *ec = GET_EC();
269 int line;
270
271 def->body.attr.id = (ID)(VALUE)opts;
272
274
275 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
276 VALUE location = rb_ary_new3(2, rb_iseq_path(cfp->iseq), INT2FIX(line));
277 RB_OBJ_WRITE(me, &def->body.attr.location, rb_ary_freeze(location));
278 }
279 else {
280 VM_ASSERT(def->body.attr.location == 0);
281 }
282 return;
283 }
285 RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts);
286 return;
288 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), rb_f_notimplement, -1);
289 return;
291 def->body.optimize_type = (enum method_optimized_type)opts;
292 return;
294 {
295 const rb_method_refined_t *refined = (rb_method_refined_t *)opts;
296 RB_OBJ_WRITE(me, &def->body.refined.orig_me, refined->orig_me);
297 RB_OBJ_WRITE(me, &def->body.refined.owner, refined->owner);
298 return;
299 }
302 return;
306 return;
307 }
308 }
309}
310
311static void
312method_definition_reset(const rb_method_entry_t *me)
313{
315
316 switch(def->type) {
320 break;
324 break;
327 /* give up to check all in a list */
329 break;
333 break;
336 break;
343 break;
344 }
345}
346
349{
352 def->type = type;
353 def->original_id = mid;
354 static uintptr_t method_serial = 1;
355 def->method_serial = method_serial++;
356 return def;
357}
358
360method_definition_addref(rb_method_definition_t *def)
361{
362 def->alias_count++;
363 if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", (void *)def, rb_id2name(def->original_id), def->alias_count);
364 return def;
365}
366
368method_definition_addref_complement(rb_method_definition_t *def)
369{
370 def->complemented_count++;
371 if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", (void *)def, rb_id2name(def->original_id), def->complemented_count);
372 return def;
373}
374
375static rb_method_entry_t *
376rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, const rb_method_definition_t *def)
377{
378 rb_method_entry_t *me = (rb_method_entry_t *)rb_imemo_new(imemo_ment, (VALUE)def, (VALUE)called_id, owner, defined_class);
379 return me;
380}
381
382static VALUE
383filter_defined_class(VALUE klass)
384{
385 switch (BUILTIN_TYPE(klass)) {
386 case T_CLASS:
387 return klass;
388 case T_MODULE:
389 return 0;
390 case T_ICLASS:
391 break;
392 }
393 rb_bug("filter_defined_class: %s", rb_obj_info(klass));
394}
395
398{
399 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def);
400 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
401 if (def != NULL) method_definition_reset(me);
402 return me;
403}
404
405const rb_method_entry_t *
407{
408 rb_method_entry_t *me = rb_method_entry_alloc(src_me->called_id, src_me->owner, src_me->defined_class,
409 method_definition_addref(src_me->def));
410 METHOD_ENTRY_FLAGS_COPY(me, src_me);
411 return me;
412}
413
416{
417 rb_method_definition_t *def = src_me->def;
419 struct {
420 const struct rb_method_entry_struct *orig_me;
421 VALUE owner;
422 } refined = {0};
423
424 if (!src_me->defined_class &&
427 const rb_method_entry_t *orig_me =
429 RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
430 refined.orig_me = orig_me;
431 refined.owner = orig_me->owner;
432 def = NULL;
433 }
434 else {
435 def = method_definition_addref_complement(def);
436 }
437 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def);
438 METHOD_ENTRY_FLAGS_COPY(me, src_me);
440 if (!def) {
442 rb_method_definition_set(me, def, &refined);
443 }
444
446
448}
449
450void
452{
453 *(rb_method_definition_t **)&dst->def = method_definition_addref(src->def);
454 method_definition_reset(dst);
455 dst->called_id = src->called_id;
456 RB_OBJ_WRITE((VALUE)dst, &dst->owner, src->owner);
457 RB_OBJ_WRITE((VALUE)dst, &dst->defined_class, src->defined_class);
458 METHOD_ENTRY_FLAGS_COPY(dst, src);
459}
460
461static void
462make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
463{
465 return;
466 }
467 else {
468 struct {
469 struct rb_method_entry_struct *orig_me;
470 VALUE owner;
471 } refined;
473
474 rb_vm_check_redefinition_opt_method(me, me->owner);
475
476 refined.orig_me =
477 rb_method_entry_alloc(me->called_id, me->owner,
480 method_definition_addref(me->def));
481 METHOD_ENTRY_FLAGS_COPY(refined.orig_me, me);
482 refined.owner = owner;
483
485 rb_method_definition_set(me, def, (void *)&refined);
486 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
487 }
488}
489
490void
492{
493 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
494
495 if (me) {
496 make_method_entry_refined(refined_class, me);
497 rb_clear_method_cache_by_class(refined_class);
498 }
499 else {
501 }
502}
503
504static void
505check_override_opt_method_i(VALUE klass, VALUE arg)
506{
507 ID mid = (ID)arg;
508 const rb_method_entry_t *me, *newme;
509
510 if (vm_redefinition_check_flag(klass)) {
511 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
512 if (me) {
513 newme = rb_method_entry(klass, mid);
514 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
515 }
516 }
517 rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
518}
519
520static void
521check_override_opt_method(VALUE klass, VALUE mid)
522{
524 check_override_opt_method_i(klass, mid);
525 }
526}
527
528/*
529 * klass->method_table[mid] = method_entry(defined_class, visi, def)
530 *
531 * If def is given (!= NULL), then just use it and ignore original_id and otps.
532 * If not given, then make a new def with original_id and opts.
533 */
534static rb_method_entry_t *
535rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
536 rb_method_type_t type, rb_method_definition_t *def, ID original_id, void *opts)
537{
539 struct rb_id_table *mtbl;
540 st_data_t data;
541 int make_refined = 0;
542
543 if (NIL_P(klass)) {
545 }
546 if (!FL_TEST(klass, FL_SINGLETON) &&
549 switch (mid) {
550 case idInitialize:
553 case idInitialize_dup:
555 visi = METHOD_VISI_PRIVATE;
556 }
557 }
558
560
563 rb_add_refined_method_entry(refined_class, mid);
564 }
566 rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
567 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
568 }
569 else {
571 }
572 mtbl = RCLASS_M_TBL(klass);
573
574 /* check re-definition */
575 if (rb_id_table_lookup(mtbl, mid, &data)) {
576 rb_method_entry_t *old_me = (rb_method_entry_t *)data;
577 rb_method_definition_t *old_def = old_me->def;
578
579 if (rb_method_definition_eq(old_def, def)) return old_me;
580 rb_vm_check_redefinition_opt_method(old_me, klass);
581
582 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
583
584 if (RTEST(ruby_verbose) &&
586 (old_def->alias_count == 0) &&
587 (!old_def->no_redef_warning) &&
588 !make_refined &&
589 old_def->type != VM_METHOD_TYPE_UNDEF &&
590 old_def->type != VM_METHOD_TYPE_ZSUPER &&
591 old_def->type != VM_METHOD_TYPE_ALIAS) {
592 const rb_iseq_t *iseq = 0;
593
594 rb_warning("method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
595 switch (old_def->type) {
597 iseq = def_iseq_ptr(old_def);
598 break;
600 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
601 break;
602 default:
603 break;
604 }
605 if (iseq) {
608 "previous definition of %"PRIsVALUE" was here",
609 rb_id2str(old_def->original_id));
610 }
611 }
612 }
613
614 /* create method entry */
615 me = rb_method_entry_create(mid, defined_class, visi, NULL);
616 if (def == NULL) def = rb_method_definition_create(type, original_id);
617 rb_method_definition_set(me, def, opts);
618
620
621 /* check mid */
622 if (klass == rb_cObject) {
623 switch (mid) {
624 case idInitialize:
626 case idMethodMissing:
627 case idRespond_to:
628 rb_warn("redefining Object#%s may cause infinite loop", rb_id2name(mid));
629 }
630 }
631 /* check mid */
632 if (mid == object_id || mid == id__send__) {
633 if (type == VM_METHOD_TYPE_ISEQ && search_method(klass, mid, 0)) {
634 rb_warn("redefining `%s' may cause serious problems", rb_id2name(mid));
635 }
636 }
637
638 if (make_refined) {
639 make_method_entry_refined(klass, me);
640 }
641
642 rb_id_table_insert(mtbl, mid, (VALUE)me);
644
645 VM_ASSERT(me->def != NULL);
646
647 /* check optimized method override by a prepended module */
648 if (RB_TYPE_P(klass, T_MODULE)) {
649 check_override_opt_method(klass, (VALUE)mid);
650 }
651
652 return me;
653}
654
655#define CALL_METHOD_HOOK(klass, hook, mid) do { \
656 const VALUE arg = ID2SYM(mid); \
657 VALUE recv_class = (klass); \
658 ID hook_id = (hook); \
659 if (FL_TEST((klass), FL_SINGLETON)) { \
660 recv_class = rb_ivar_get((klass), attached); \
661 hook_id = singleton_##hook; \
662 } \
663 rb_funcallv(recv_class, hook_id, 1, &arg); \
664 } while (0)
665
666static void
667method_added(VALUE klass, ID mid)
668{
669 if (ruby_running) {
671 }
672}
673
674void
676{
677 rb_method_entry_make(klass, mid, klass, visi, type, NULL, mid, opts);
678
680 method_added(klass, mid);
681 }
682}
683
686{
687 struct { /* should be same fields with rb_method_iseq_struct */
688 const rb_iseq_t *iseqptr;
689 rb_cref_t *cref;
690 } iseq_body;
691
692 iseq_body.iseqptr = iseq;
693 iseq_body.cref = cref;
694 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
695}
696
697static rb_method_entry_t *
698method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
699 rb_method_visibility_t visi, VALUE defined_class)
700{
701 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
702 me->def->type, me->def, 0, NULL);
703 if (newme == me) {
705 }
706 else {
707 method_definition_addref(me->def);
708 }
709 method_added(klass, mid);
710 return newme;
711}
712
715{
716 return method_entry_set(klass, mid, me, visi, klass);
717}
718
719#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
720
721void
723{
725 RCLASS_EXT(klass)->allocator = func;
726}
727
728void
730{
732}
733
736{
738
739 for (; klass; klass = RCLASS_SUPER(klass)) {
740 rb_alloc_func_t allocator = RCLASS_EXT(klass)->allocator;
741 if (allocator == UNDEF_ALLOC_FUNC) break;
742 if (allocator) return allocator;
743 }
744 return 0;
745}
746
747static inline rb_method_entry_t*
748search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
749{
751
752 for (; klass; klass = RCLASS_SUPER(klass)) {
753 RB_DEBUG_COUNTER_INC(mc_search_super);
754 if ((me = lookup_method_table(klass, id)) != 0) {
755 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED) {
756 break;
757 }
758 }
759 }
760
761 if (defined_class_ptr)
762 *defined_class_ptr = klass;
763 return me;
764}
765
766const rb_method_entry_t *
768{
769 return lookup_method_table(klass, id);
770}
771
772/*
773 * search method entry without the method cache.
774 *
775 * if you need method entry with method cache (normal case), use
776 * rb_method_entry() simply.
777 */
778static rb_method_entry_t *
779method_entry_get_without_cache(VALUE klass, ID id,
780 VALUE *defined_class_ptr)
781{
782 VALUE defined_class;
783 rb_method_entry_t *me = search_method(klass, id, &defined_class);
784
785 if (ruby_running) {
787 struct cache_entry *ent;
788 ent = GLOBAL_METHOD_CACHE(klass, id);
792 ent->mid = id;
793
795 me = ent->me = NULL;
796 }
797 else {
798 ent->me = me;
799 }
800 }
801 else if (UNDEFINED_METHOD_ENTRY_P(me)) {
802 me = NULL;
803 }
804 }
805 else if (UNDEFINED_METHOD_ENTRY_P(me)) {
806 me = NULL;
807 }
808
809 if (defined_class_ptr)
810 *defined_class_ptr = defined_class;
811 return me;
812}
813
814static void
815verify_method_cache(VALUE klass, ID id, VALUE defined_class, rb_method_entry_t *me)
816{
817 if (!VM_DEBUG_VERIFY_METHOD_CACHE) return;
818 VALUE actual_defined_class;
819 rb_method_entry_t *actual_me =
820 method_entry_get_without_cache(klass, id, &actual_defined_class);
821
822 if (me != actual_me || defined_class != actual_defined_class) {
823 rb_bug("method cache verification failed");
824 }
825}
826
827static inline rb_method_entry_t*
828search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
829{
830 return search_method0(klass, id, defined_class_ptr, false);
831}
832
833static rb_method_entry_t *
834method_entry_get(VALUE klass, ID id, VALUE *defined_class_ptr)
835{
836 struct cache_entry *ent;
837 if (!OPT_GLOBAL_METHOD_CACHE) goto nocache;
838 ent = GLOBAL_METHOD_CACHE(klass, id);
841 ent->mid == id) {
842 verify_method_cache(klass, id, ent->defined_class, ent->me);
843 if (defined_class_ptr) *defined_class_ptr = ent->defined_class;
844 RB_DEBUG_COUNTER_INC(mc_global_hit);
845 return ent->me;
846 }
847
848 nocache:
849 RB_DEBUG_COUNTER_INC(mc_global_miss);
850 return method_entry_get_without_cache(klass, id, defined_class_ptr);
851}
852
855{
856 return method_entry_get(klass, id, NULL);
857}
858
859static const rb_callable_method_entry_t *
860prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_t *me)
861{
862 struct rb_id_table *mtbl;
864
865 if (me && me->defined_class == 0) {
866 RB_DEBUG_COUNTER_INC(mc_cme_complement);
867 VM_ASSERT(RB_TYPE_P(defined_class, T_ICLASS) || RB_TYPE_P(defined_class, T_MODULE));
869
870 mtbl = RCLASS_CALLABLE_M_TBL(defined_class);
871
872 if (mtbl && rb_id_table_lookup(mtbl, id, (VALUE *)&me)) {
873 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
875 VM_ASSERT(callable_method_entry_p(cme));
876 }
877 else {
878 if (!mtbl) {
879 mtbl = RCLASS_EXT(defined_class)->callable_m_tbl = rb_id_table_create(0);
880 }
882 rb_id_table_insert(mtbl, id, (VALUE)cme);
883 VM_ASSERT(callable_method_entry_p(cme));
884 }
885 }
886 else {
887 cme = (const rb_callable_method_entry_t *)me;
888 VM_ASSERT(callable_method_entry_p(cme));
889 }
890
891 return cme;
892}
893
896{
897 VALUE defined_class;
898 rb_method_entry_t *me = method_entry_get(klass, id, &defined_class);
899 return prepare_callable_method_entry(defined_class, id, me);
900}
901
902static const rb_method_entry_t *resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr);
903
904static const rb_method_entry_t *
905method_entry_resolve_refinement(VALUE klass, ID id, int with_refinement, VALUE *defined_class_ptr)
906{
907 const rb_method_entry_t *me = method_entry_get(klass, id, defined_class_ptr);
908
909 if (me) {
911 if (with_refinement) {
912 const rb_cref_t *cref = rb_vm_cref();
913 VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
914 me = resolve_refined_method(refinements, me, defined_class_ptr);
915 }
916 else {
917 me = resolve_refined_method(Qnil, me, defined_class_ptr);
918 }
919
921 }
922 }
923
924 return me;
925}
926
927const rb_method_entry_t *
929{
930 return method_entry_resolve_refinement(klass, id, TRUE, defined_class_ptr);
931}
932
935{
936 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
937 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, TRUE, dcp);
938 return prepare_callable_method_entry(*dcp, id, me);
939}
940
941const rb_method_entry_t *
943{
944 return method_entry_resolve_refinement(klass, id, FALSE, defined_class_ptr);
945}
946
949{
950 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
951 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, FALSE, dcp);
952 return prepare_callable_method_entry(*dcp, id, me);
953}
954
955static const rb_method_entry_t *
956resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
957{
958 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
959 VALUE refinement;
960 const rb_method_entry_t *tmp_me;
961 VALUE super;
962
963 refinement = find_refinement(refinements, me->owner);
964 if (!NIL_P(refinement)) {
965 tmp_me = method_entry_get(refinement, me->called_id, defined_class_ptr);
966
967 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
968 return tmp_me;
969 }
970 }
971
972 tmp_me = me->def->body.refined.orig_me;
973 if (tmp_me) {
974 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
975 return tmp_me;
976 }
977
978 super = RCLASS_SUPER(me->owner);
979 if (!super) {
980 return 0;
981 }
982
983 me = method_entry_get(super, me->called_id, defined_class_ptr);
984 }
985 return me;
986}
987
988const rb_method_entry_t *
990{
991 return resolve_refined_method(refinements, me, NULL);
992}
993
997{
998 VALUE defined_class = me->defined_class;
999 const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (const rb_method_entry_t *)me, &defined_class);
1000
1001 if (resolved_me && resolved_me->defined_class == 0) {
1002 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1003 }
1004 else {
1005 return (const rb_callable_method_entry_t *)resolved_me;
1006 }
1007}
1008
1009static void
1010remove_method(VALUE klass, ID mid)
1011{
1012 VALUE data;
1013 rb_method_entry_t *me = 0;
1014 VALUE self = klass;
1015
1018 if (mid == object_id || mid == id__send__ || mid == idInitialize) {
1019 rb_warn("removing `%s' may cause serious problems", rb_id2name(mid));
1020 }
1021
1022 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
1023 !(me = (rb_method_entry_t *)data) ||
1024 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
1026 rb_name_err_raise("method `%1$s' not defined in %2$s",
1027 klass, ID2SYM(mid));
1028 }
1029
1031
1032 rb_vm_check_redefinition_opt_method(me, klass);
1034
1035 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1037 }
1038
1039 CALL_METHOD_HOOK(self, removed, mid);
1040}
1041
1042void
1044{
1045 remove_method(klass, mid);
1046}
1047
1048void
1050{
1051 remove_method(klass, rb_intern(name));
1052}
1053
1054/*
1055 * call-seq:
1056 * remove_method(symbol) -> self
1057 * remove_method(string) -> self
1058 *
1059 * Removes the method identified by _symbol_ from the current
1060 * class. For an example, see Module#undef_method.
1061 * String arguments are converted to symbols.
1062 */
1063
1064static VALUE
1065rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
1066{
1067 int i;
1068
1069 for (i = 0; i < argc; i++) {
1070 VALUE v = argv[i];
1071 ID id = rb_check_id(&v);
1072 if (!id) {
1073 rb_name_err_raise("method `%1$s' not defined in %2$s",
1074 mod, v);
1075 }
1076 remove_method(mod, id);
1077 }
1078 return mod;
1079}
1080
1081static void
1082rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
1083{
1085 VALUE defined_class;
1086 VALUE origin_class = RCLASS_ORIGIN(klass);
1087
1088 me = search_method0(origin_class, name, &defined_class, true);
1089 if (!me && RB_TYPE_P(klass, T_MODULE)) {
1090 me = search_method(rb_cObject, name, &defined_class);
1091 }
1092
1096 }
1097
1098 if (METHOD_ENTRY_VISI(me) != visi) {
1099 rb_vm_check_redefinition_opt_method(me, klass);
1100
1101 if (klass == defined_class || origin_class == defined_class) {
1102 METHOD_ENTRY_VISI_SET(me, visi);
1103
1105 METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
1106 }
1108 }
1109 else {
1111 }
1112 }
1113}
1114
1115#define BOUND_PRIVATE 0x01
1116#define BOUND_RESPONDS 0x02
1117
1118int
1120{
1121 const rb_method_entry_t *me;
1122
1123 if (ex & BOUND_RESPONDS) {
1124 me = method_entry_resolve_refinement(klass, id, TRUE, NULL);
1125 }
1126 else {
1128 }
1129
1130 if (me != 0) {
1131 if ((ex & ~BOUND_RESPONDS) &&
1134 return 0;
1135 }
1136
1138 if (ex & BOUND_RESPONDS) return 2;
1139 return 0;
1140 }
1141 return 1;
1142 }
1143 return 0;
1144}
1145
1146static void
1147vm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func)
1148{
1150 scope_visi->method_visi = method_visi;
1151 scope_visi->module_func = module_func;
1152}
1153
1154void
1156{
1157 vm_cref_set_visibility(visi, FALSE);
1158}
1159
1160static void
1161scope_visibility_check(void)
1162{
1163 /* Check for public/protected/private/module_function called inside a method */
1164 rb_control_frame_t *cfp = rb_current_execution_context()->cfp+1;
1165 if (cfp && cfp->iseq && cfp->iseq->body->type == ISEQ_TYPE_METHOD) {
1166 rb_warn("calling %s without arguments inside a method may not have the intended effect",
1168 }
1169}
1170
1171static void
1172rb_scope_module_func_set(void)
1173{
1174 scope_visibility_check();
1175 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
1176}
1177
1179void
1180rb_attr(VALUE klass, ID id, int read, int write, int ex)
1181{
1182 ID attriv;
1184 const rb_execution_context_t *ec = GET_EC();
1186
1187 if (!ex || !cref) {
1188 visi = METHOD_VISI_PUBLIC;
1189 }
1190 else {
1191 switch (vm_scope_visibility_get(ec)) {
1193 if (vm_scope_module_func_check(ec)) {
1194 rb_warning("attribute accessor as module_function");
1195 }
1196 visi = METHOD_VISI_PRIVATE;
1197 break;
1199 visi = METHOD_VISI_PROTECTED;
1200 break;
1201 default:
1202 visi = METHOD_VISI_PUBLIC;
1203 break;
1204 }
1205 }
1206
1207 attriv = rb_intern_str(rb_sprintf("@%"PRIsVALUE, rb_id2str(id)));
1208 if (read) {
1209 rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, visi);
1210 }
1211 if (write) {
1212 rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, visi);
1213 }
1214}
1215
1216void
1218{
1219 const rb_method_entry_t *me;
1220
1221 if (NIL_P(klass)) {
1222 rb_raise(rb_eTypeError, "no class to undef method");
1223 }
1225 if (id == object_id || id == id__send__ || id == idInitialize) {
1226 rb_warn("undefining `%s' may cause serious problems", rb_id2name(id));
1227 }
1228
1229 me = search_method(klass, id, 0);
1230 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1232 }
1233
1237 }
1238
1240
1242}
1243
1244/*
1245 * call-seq:
1246 * undef_method(symbol) -> self
1247 * undef_method(string) -> self
1248 *
1249 * Prevents the current class from responding to calls to the named
1250 * method. Contrast this with <code>remove_method</code>, which deletes
1251 * the method from the particular class; Ruby will still search
1252 * superclasses and mixed-in modules for a possible receiver.
1253 * String arguments are converted to symbols.
1254 *
1255 * class Parent
1256 * def hello
1257 * puts "In parent"
1258 * end
1259 * end
1260 * class Child < Parent
1261 * def hello
1262 * puts "In child"
1263 * end
1264 * end
1265 *
1266 *
1267 * c = Child.new
1268 * c.hello
1269 *
1270 *
1271 * class Child
1272 * remove_method :hello # remove from child, still in parent
1273 * end
1274 * c.hello
1275 *
1276 *
1277 * class Child
1278 * undef_method :hello # prevent any calls to 'hello'
1279 * end
1280 * c.hello
1281 *
1282 * <em>produces:</em>
1283 *
1284 * In child
1285 * In parent
1286 * prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
1287 */
1288
1289static VALUE
1290rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
1291{
1292 int i;
1293 for (i = 0; i < argc; i++) {
1294 VALUE v = argv[i];
1295 ID id = rb_check_id(&v);
1296 if (!id) {
1298 }
1299 rb_undef(mod, id);
1300 }
1301 return mod;
1302}
1303
1305check_definition_visibility(VALUE mod, int argc, VALUE *argv)
1306{
1307 const rb_method_entry_t *me;
1308 VALUE mid, include_super, lookup_mod = mod;
1309 int inc_super;
1310 ID id;
1311
1312 rb_scan_args(argc, argv, "11", &mid, &include_super);
1313 id = rb_check_id(&mid);
1314 if (!id) return METHOD_VISI_UNDEF;
1315
1316 if (argc == 1) {
1317 inc_super = 1;
1318 }
1319 else {
1320 inc_super = RTEST(include_super);
1321 if (!inc_super) {
1322 lookup_mod = RCLASS_ORIGIN(mod);
1323 }
1324 }
1325
1326 me = rb_method_entry_without_refinements(lookup_mod, id, NULL);
1327 if (me) {
1329 if (!inc_super && me->owner != mod) return METHOD_VISI_UNDEF;
1330 return METHOD_ENTRY_VISI(me);
1331 }
1332 return METHOD_VISI_UNDEF;
1333}
1334
1335/*
1336 * call-seq:
1337 * mod.method_defined?(symbol, inherit=true) -> true or false
1338 * mod.method_defined?(string, inherit=true) -> true or false
1339 *
1340 * Returns +true+ if the named method is defined by
1341 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
1342 * ancestors. Public and protected methods are matched.
1343 * String arguments are converted to symbols.
1344 *
1345 * module A
1346 * def method1() end
1347 * def protected_method1() end
1348 * protected :protected_method1
1349 * end
1350 * class B
1351 * def method2() end
1352 * def private_method2() end
1353 * private :private_method2
1354 * end
1355 * class C < B
1356 * include A
1357 * def method3() end
1358 * end
1359 *
1360 * A.method_defined? :method1 #=> true
1361 * C.method_defined? "method1" #=> true
1362 * C.method_defined? "method2" #=> true
1363 * C.method_defined? "method2", true #=> true
1364 * C.method_defined? "method2", false #=> false
1365 * C.method_defined? "method3" #=> true
1366 * C.method_defined? "protected_method1" #=> true
1367 * C.method_defined? "method4" #=> false
1368 * C.method_defined? "private_method2" #=> false
1369 */
1370
1371static VALUE
1372rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
1373{
1374 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
1375 return (visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED) ? Qtrue : Qfalse;
1376}
1377
1378static VALUE
1379check_definition(VALUE mod, int argc, VALUE *argv, rb_method_visibility_t visi)
1380{
1381 return (check_definition_visibility(mod, argc, argv) == visi) ? Qtrue : Qfalse;
1382}
1383
1384/*
1385 * call-seq:
1386 * mod.public_method_defined?(symbol, inherit=true) -> true or false
1387 * mod.public_method_defined?(string, inherit=true) -> true or false
1388 *
1389 * Returns +true+ if the named public method is defined by
1390 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
1391 * ancestors.
1392 * String arguments are converted to symbols.
1393 *
1394 * module A
1395 * def method1() end
1396 * end
1397 * class B
1398 * protected
1399 * def method2() end
1400 * end
1401 * class C < B
1402 * include A
1403 * def method3() end
1404 * end
1405 *
1406 * A.method_defined? :method1 #=> true
1407 * C.public_method_defined? "method1" #=> true
1408 * C.public_method_defined? "method1", true #=> true
1409 * C.public_method_defined? "method1", false #=> true
1410 * C.public_method_defined? "method2" #=> false
1411 * C.method_defined? "method2" #=> true
1412 */
1413
1414static VALUE
1415rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
1416{
1417 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
1418}
1419
1420/*
1421 * call-seq:
1422 * mod.private_method_defined?(symbol, inherit=true) -> true or false
1423 * mod.private_method_defined?(string, inherit=true) -> true or false
1424 *
1425 * Returns +true+ if the named private method is defined by
1426 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
1427 * ancestors.
1428 * String arguments are converted to symbols.
1429 *
1430 * module A
1431 * def method1() end
1432 * end
1433 * class B
1434 * private
1435 * def method2() end
1436 * end
1437 * class C < B
1438 * include A
1439 * def method3() end
1440 * end
1441 *
1442 * A.method_defined? :method1 #=> true
1443 * C.private_method_defined? "method1" #=> false
1444 * C.private_method_defined? "method2" #=> true
1445 * C.private_method_defined? "method2", true #=> true
1446 * C.private_method_defined? "method2", false #=> false
1447 * C.method_defined? "method2" #=> false
1448 */
1449
1450static VALUE
1451rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
1452{
1453 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
1454}
1455
1456/*
1457 * call-seq:
1458 * mod.protected_method_defined?(symbol, inherit=true) -> true or false
1459 * mod.protected_method_defined?(string, inherit=true) -> true or false
1460 *
1461 * Returns +true+ if the named protected method is defined
1462 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
1463 * ancestors.
1464 * String arguments are converted to symbols.
1465 *
1466 * module A
1467 * def method1() end
1468 * end
1469 * class B
1470 * protected
1471 * def method2() end
1472 * end
1473 * class C < B
1474 * include A
1475 * def method3() end
1476 * end
1477 *
1478 * A.method_defined? :method1 #=> true
1479 * C.protected_method_defined? "method1" #=> false
1480 * C.protected_method_defined? "method2" #=> true
1481 * C.protected_method_defined? "method2", true #=> true
1482 * C.protected_method_defined? "method2", false #=> false
1483 * C.method_defined? "method2" #=> true
1484 */
1485
1486static VALUE
1487rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
1488{
1489 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
1490}
1491
1492int
1494{
1495 return rb_method_definition_eq(m1->def, m2->def);
1496}
1497
1498static const rb_method_definition_t *
1499original_method_definition(const rb_method_definition_t *def)
1500{
1501 again:
1502 if (def) {
1503 switch (def->type) {
1505 if (def->body.refined.orig_me) {
1506 def = def->body.refined.orig_me->def;
1507 goto again;
1508 }
1509 break;
1511 def = def->body.alias.original_me->def;
1512 goto again;
1513 default:
1514 break;
1515 }
1516 }
1517 return def;
1518}
1519
1522{
1523 d1 = original_method_definition(d1);
1524 d2 = original_method_definition(d2);
1525
1526 if (d1 == d2) return 1;
1527 if (!d1 || !d2) return 0;
1528 if (d1->type != d2->type) return 0;
1529
1530 switch (d1->type) {
1532 return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
1534 return
1535 d1->body.cfunc.func == d2->body.cfunc.func &&
1536 d1->body.cfunc.argc == d2->body.cfunc.argc;
1539 return d1->body.attr.id == d2->body.attr.id;
1541 return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
1543 return d1->original_id == d2->original_id;
1547 return 1;
1549 return d1->body.optimize_type == d2->body.optimize_type;
1552 break;
1553 }
1554 rb_bug("rb_method_definition_eq: unsupported type: %d\n", d1->type);
1555}
1556
1557static st_index_t
1558rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
1559{
1560 hash = rb_hash_uint(hash, def->type);
1561 def = original_method_definition(def);
1562
1563 if (!def) return hash;
1564
1565 switch (def->type) {
1567 return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr);
1569 hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
1570 return rb_hash_uint(hash, def->body.cfunc.argc);
1573 return rb_hash_uint(hash, def->body.attr.id);
1575 return rb_hash_proc(hash, def->body.bmethod.proc);
1577 return rb_hash_uint(hash, def->original_id);
1581 return hash;
1583 return rb_hash_uint(hash, def->body.optimize_type);
1586 break; /* unreachable */
1587 }
1588 rb_bug("rb_hash_method_definition: unsupported method type (%d)\n", def->type);
1589 }
1590
1593{
1594 return rb_hash_method_definition(hash, me->def);
1595}
1596
1597void
1598rb_alias(VALUE klass, ID alias_name, ID original_name)
1599{
1600 const VALUE target_klass = klass;
1601 VALUE defined_class;
1602 const rb_method_entry_t *orig_me;
1604
1605 if (NIL_P(klass)) {
1606 rb_raise(rb_eTypeError, "no class to make alias");
1607 }
1608
1610
1611 again:
1612 orig_me = search_method(klass, original_name, &defined_class);
1613 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
1614 orig_me = rb_resolve_refined_method(Qnil, orig_me);
1615 }
1616
1617 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
1618 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
1619 if ((!RB_TYPE_P(klass, T_MODULE)) ||
1620 (orig_me = search_method(rb_cObject, original_name, &defined_class),
1621 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
1622 rb_print_undef(klass, original_name, METHOD_VISI_UNDEF);
1623 }
1624 }
1625
1626 if (orig_me->def->type == VM_METHOD_TYPE_ZSUPER) {
1628 original_name = orig_me->def->original_id;
1629 visi = METHOD_ENTRY_VISI(orig_me);
1630 goto again;
1631 }
1632
1633 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
1634
1635 if (orig_me->defined_class == 0) {
1636 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
1638 (void *)rb_method_entry_clone(orig_me));
1639 method_added(target_klass, alias_name);
1640 }
1641 else {
1642 rb_method_entry_t *alias_me;
1643
1644 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
1645 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
1646 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, defined_class);
1647 }
1648}
1649
1650/*
1651 * call-seq:
1652 * alias_method(new_name, old_name) -> self
1653 *
1654 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
1655 * be used to retain access to methods that are overridden.
1656 *
1657 * module Mod
1658 * alias_method :orig_exit, :exit
1659 * def exit(code=0)
1660 * puts "Exiting with code #{code}"
1661 * orig_exit(code)
1662 * end
1663 * end
1664 * include Mod
1665 * exit(99)
1666 *
1667 * <em>produces:</em>
1668 *
1669 * Exiting with code 99
1670 */
1671
1672static VALUE
1673rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
1674{
1675 ID oldid = rb_check_id(&oldname);
1676 if (!oldid) {
1677 rb_print_undef_str(mod, oldname);
1678 }
1679 rb_alias(mod, rb_to_id(newname), oldid);
1680 return mod;
1681}
1682
1683static void
1684set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibility_t visi)
1685{
1686 int i;
1687
1688 rb_check_frozen(self);
1689 if (argc == 0) {
1690 rb_warning("%"PRIsVALUE" with no argument is just ignored",
1692 return;
1693 }
1694
1695 for (i = 0; i < argc; i++) {
1696 VALUE v = argv[i];
1697 ID id = rb_check_id(&v);
1698 if (!id) {
1699 rb_print_undef_str(self, v);
1700 }
1701 rb_export_method(self, id, visi);
1702 }
1703}
1704
1705static VALUE
1706set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi)
1707{
1708 if (argc == 0) {
1709 scope_visibility_check();
1711 }
1712 else {
1713 set_method_visibility(module, argc, argv, visi);
1714 }
1715 return module;
1716}
1717
1718/*
1719 * call-seq:
1720 * public -> self
1721 * public(symbol, ...) -> self
1722 * public(string, ...) -> self
1723 *
1724 * With no arguments, sets the default visibility for subsequently
1725 * defined methods to public. With arguments, sets the named methods to
1726 * have public visibility.
1727 * String arguments are converted to symbols.
1728 */
1729
1730static VALUE
1731rb_mod_public(int argc, VALUE *argv, VALUE module)
1732{
1733 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
1734}
1735
1736/*
1737 * call-seq:
1738 * protected -> self
1739 * protected(symbol, ...) -> self
1740 * protected(string, ...) -> self
1741 *
1742 * With no arguments, sets the default visibility for subsequently
1743 * defined methods to protected. With arguments, sets the named methods
1744 * to have protected visibility.
1745 * String arguments are converted to symbols.
1746 *
1747 * If a method has protected visibility, it is callable only where
1748 * <code>self</code> of the context is the same as the method.
1749 * (method definition or instance_eval). This behavior is different from
1750 * Java's protected method. Usually <code>private</code> should be used.
1751 *
1752 * Note that a protected method is slow because it can't use inline cache.
1753 *
1754 * To show a private method on RDoc, use <code>:doc:</code> instead of this.
1755 */
1756
1757static VALUE
1758rb_mod_protected(int argc, VALUE *argv, VALUE module)
1759{
1760 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
1761}
1762
1763/*
1764 * call-seq:
1765 * private -> self
1766 * private(symbol, ...) -> self
1767 * private(string, ...) -> self
1768 *
1769 * With no arguments, sets the default visibility for subsequently
1770 * defined methods to private. With arguments, sets the named methods
1771 * to have private visibility.
1772 * String arguments are converted to symbols.
1773 *
1774 * module Mod
1775 * def a() end
1776 * def b() end
1777 * private
1778 * def c() end
1779 * private :a
1780 * end
1781 * Mod.private_instance_methods #=> [:a, :c]
1782 *
1783 * Note that to show a private method on RDoc, use <code>:doc:</code>.
1784 */
1785
1786static VALUE
1787rb_mod_private(int argc, VALUE *argv, VALUE module)
1788{
1789 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
1790}
1791
1792/*
1793 * call-seq:
1794 * ruby2_keywords(method_name, ...) -> nil
1795 *
1796 * For the given method names, marks the method as passing keywords through
1797 * a normal argument splat. This should only be called on methods that
1798 * accept an argument splat (<tt>*args</tt>) but not explicit keywords or
1799 * a keyword splat. It marks the method such that if the method is called
1800 * with keyword arguments, the final hash argument is marked with a special
1801 * flag such that if it is the final element of a normal argument splat to
1802 * another method call, and that method call does not include explicit
1803 * keywords or a keyword splat, the final element is interpreted as keywords.
1804 * In other words, keywords will be passed through the method to other
1805 * methods.
1806 *
1807 * This should only be used for methods that delegate keywords to another
1808 * method, and only for backwards compatibility with Ruby versions before
1809 * 2.7.
1810 *
1811 * This method will probably be removed at some point, as it exists only
1812 * for backwards compatibility. As it does not exist in Ruby versions
1813 * before 2.7, check that the module responds to this method before calling
1814 * it. Also, be aware that if this method is removed, the behavior of the
1815 * method will change so that it does not pass through keywords.
1816 *
1817 * module Mod
1818 * def foo(meth, *args, &block)
1819 * send(:"do_#{meth}", *args, &block)
1820 * end
1821 * ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true)
1822 * end
1823 */
1824
1825static VALUE
1826rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module)
1827{
1828 int i;
1829 VALUE origin_class = RCLASS_ORIGIN(module);
1830
1831 rb_check_frozen(module);
1832
1833 for (i = 0; i < argc; i++) {
1834 VALUE v = argv[i];
1835 ID name = rb_check_id(&v);
1837 VALUE defined_class;
1838
1839 if (!name) {
1840 rb_print_undef_str(module, v);
1841 }
1842
1843 me = search_method(origin_class, name, &defined_class);
1844 if (!me && RB_TYPE_P(module, T_MODULE)) {
1845 me = search_method(rb_cObject, name, &defined_class);
1846 }
1847
1851 }
1852
1853 if (module == defined_class || origin_class == defined_class) {
1854 switch (me->def->type) {
1861 }
1862 else {
1863 rb_warn("Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
1864 }
1865 break;
1867 VALUE procval = me->def->body.bmethod.proc;
1868 if (vm_block_handler_type(procval) == block_handler_type_proc) {
1869 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
1870 }
1871
1872 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
1873 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval);
1874 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1875 if (iseq->body->param.flags.has_rest &&
1876 !iseq->body->param.flags.has_kw &&
1880 }
1881 else {
1882 rb_warn("Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
1883 }
1884 break;
1885 }
1886 }
1887 /* fallthrough */
1888 default:
1889 rb_warn("Skipping set of ruby2_keywords flag for %s (method not defined in Ruby)", rb_id2name(name));
1890 break;
1891 }
1892 }
1893 else {
1894 rb_warn("Skipping set of ruby2_keywords flag for %s (can only set in method defining module)", rb_id2name(name));
1895 }
1896 }
1897 return Qnil;
1898}
1899
1900/*
1901 * call-seq:
1902 * mod.public_class_method(symbol, ...) -> mod
1903 * mod.public_class_method(string, ...) -> mod
1904 *
1905 * Makes a list of existing class methods public.
1906 *
1907 * String arguments are converted to symbols.
1908 */
1909
1910static VALUE
1911rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
1912{
1913 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
1914 return obj;
1915}
1916
1917/*
1918 * call-seq:
1919 * mod.private_class_method(symbol, ...) -> mod
1920 * mod.private_class_method(string, ...) -> mod
1921 *
1922 * Makes existing class methods private. Often used to hide the default
1923 * constructor <code>new</code>.
1924 *
1925 * String arguments are converted to symbols.
1926 *
1927 * class SimpleSingleton # Not thread safe
1928 * private_class_method :new
1929 * def SimpleSingleton.create(*args, &block)
1930 * @me = new(*args, &block) if ! @me
1931 * @me
1932 * end
1933 * end
1934 */
1935
1936static VALUE
1937rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
1938{
1939 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
1940 return obj;
1941}
1942
1943/*
1944 * call-seq:
1945 * public
1946 * public(symbol, ...)
1947 * public(string, ...)
1948 *
1949 * With no arguments, sets the default visibility for subsequently
1950 * defined methods to public. With arguments, sets the named methods to
1951 * have public visibility.
1952 *
1953 * String arguments are converted to symbols.
1954 */
1955
1956static VALUE
1957top_public(int argc, VALUE *argv, VALUE _)
1958{
1959 return rb_mod_public(argc, argv, rb_cObject);
1960}
1961
1962/*
1963 * call-seq:
1964 * private
1965 * private(symbol, ...)
1966 * private(string, ...)
1967 *
1968 * With no arguments, sets the default visibility for subsequently
1969 * defined methods to private. With arguments, sets the named methods to
1970 * have private visibility.
1971 *
1972 * String arguments are converted to symbols.
1973 */
1974static VALUE
1975top_private(int argc, VALUE *argv, VALUE _)
1976{
1977 return rb_mod_private(argc, argv, rb_cObject);
1978}
1979
1980/*
1981 * call-seq:
1982 * ruby2_keywords(method_name, ...) -> self
1983 *
1984 * For the given method names, marks the method as passing keywords through
1985 * a normal argument splat. See Module#ruby2_keywords in detail.
1986 */
1987static VALUE
1988top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
1989{
1990 return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
1991}
1992
1993/*
1994 * call-seq:
1995 * module_function(symbol, ...) -> self
1996 * module_function(string, ...) -> self
1997 *
1998 * Creates module functions for the named methods. These functions may
1999 * be called with the module as a receiver, and also become available
2000 * as instance methods to classes that mix in the module. Module
2001 * functions are copies of the original, and so may be changed
2002 * independently. The instance-method versions are made private. If
2003 * used with no arguments, subsequently defined methods become module
2004 * functions.
2005 * String arguments are converted to symbols.
2006 *
2007 * module Mod
2008 * def one
2009 * "This is one"
2010 * end
2011 * module_function :one
2012 * end
2013 * class Cls
2014 * include Mod
2015 * def call_one
2016 * one
2017 * end
2018 * end
2019 * Mod.one #=> "This is one"
2020 * c = Cls.new
2021 * c.call_one #=> "This is one"
2022 * module Mod
2023 * def one
2024 * "This is the new one"
2025 * end
2026 * end
2027 * Mod.one #=> "This is one"
2028 * c.call_one #=> "This is the new one"
2029 */
2030
2031static VALUE
2032rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
2033{
2034 int i;
2035 ID id;
2036 const rb_method_entry_t *me;
2037
2038 if (!RB_TYPE_P(module, T_MODULE)) {
2039 rb_raise(rb_eTypeError, "module_function must be called for modules");
2040 }
2041
2042 if (argc == 0) {
2043 rb_scope_module_func_set();
2044 return module;
2045 }
2046
2047 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
2048
2049 for (i = 0; i < argc; i++) {
2050 VALUE m = module;
2051
2052 id = rb_to_id(argv[i]);
2053 for (;;) {
2054 me = search_method(m, id, 0);
2055 if (me == 0) {
2056 me = search_method(rb_cObject, id, 0);
2057 }
2059 rb_print_undef(module, id, METHOD_VISI_UNDEF);
2060 }
2061 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
2062 break; /* normal case: need not to follow 'super' link */
2063 }
2064 m = RCLASS_SUPER(m);
2065 if (!m)
2066 break;
2067 }
2069 }
2070 return module;
2071}
2072
2073bool
2075{
2076 if (cd->ci.mid != mid) {
2077 *cd = (struct rb_call_data) /* reset */ { .ci = { .mid = mid, }, };
2078 }
2079
2080 vm_search_method_fastpath(cd, klass);
2081 return cd->cc.me && METHOD_ENTRY_BASIC(cd->cc.me);
2082}
2083
2084#ifdef __GNUC__
2085#pragma push_macro("rb_method_basic_definition_p")
2086#undef rb_method_basic_definition_p
2087#endif
2088int
2090{
2091 const rb_method_entry_t *me;
2092 if (!klass) return TRUE; /* hidden object cannot be overridden */
2093 me = rb_method_entry(klass, id);
2094 return (me && METHOD_ENTRY_BASIC(me)) ? TRUE : FALSE;
2095}
2096#ifdef __GNUC__
2097#pragma pop_macro("rb_method_basic_definition_p")
2098#endif
2099
2100static VALUE
2101call_method_entry(rb_execution_context_t *ec, VALUE defined_class, VALUE obj, ID id,
2102 const rb_method_entry_t *me, int argc, const VALUE *argv, int kw_splat)
2103{
2104 const rb_callable_method_entry_t *cme =
2105 prepare_callable_method_entry(defined_class, id, me);
2106 VALUE passed_block_handler = vm_passed_block_handler(ec);
2107 VALUE result = rb_vm_call_kw(ec, obj, id, argc, argv, cme, kw_splat);
2108 vm_passed_block_handler_set(ec, passed_block_handler);
2109 return result;
2110}
2111
2112static VALUE
2113basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
2114 VALUE mid, VALUE priv)
2115{
2116 VALUE defined_class, args[2];
2117 const ID rtmid = idRespond_to_missing;
2118 const rb_method_entry_t *const me =
2119 method_entry_get(klass, rtmid, &defined_class);
2120
2121 if (!me || METHOD_ENTRY_BASIC(me)) return Qundef;
2122 args[0] = mid;
2123 args[1] = priv;
2124 return call_method_entry(ec, defined_class, obj, rtmid, me, 2, args, RB_NO_KEYWORDS);
2125}
2126
2127static inline int
2128basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
2129{
2131 VALUE ret;
2132
2133 switch (rb_method_boundp(klass, id, pub|BOUND_RESPONDS)) {
2134 case 2:
2135 return FALSE;
2136 case 0:
2137 ret = basic_obj_respond_to_missing(ec, klass, obj, ID2SYM(id),
2138 pub ? Qfalse : Qtrue);
2139 return RTEST(ret) && ret != Qundef;
2140 default:
2141 return TRUE;
2142 }
2143}
2144
2145static int
2146vm_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
2147{
2148 VALUE defined_class;
2149 const ID resid = idRespond_to;
2150 const rb_method_entry_t *const me =
2151 method_entry_get(klass, resid, &defined_class);
2152
2153 if (!me) return -1;
2154 if (METHOD_ENTRY_BASIC(me)) {
2155 return -1;
2156 }
2157 else {
2158 int argc = 1;
2159 VALUE args[2];
2160 VALUE result;
2161
2162 args[0] = ID2SYM(id);
2163 args[1] = Qtrue;
2164 if (priv) {
2166 if (argc > 2) {
2168 "respond_to? must accept 1 or 2 arguments (requires %d)",
2169 argc);
2170 }
2171 if (argc != 1) {
2172 argc = 2;
2173 }
2174 else if (!NIL_P(ruby_verbose)) {
2175 VALUE location = rb_method_entry_location(me);
2176 rb_warn("%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") uses"
2177 " the deprecated method signature, which takes one parameter",
2179 (FL_TEST(klass, FL_SINGLETON) ? '.' : '#'),
2180 QUOTE_ID(id));
2181 if (!NIL_P(location)) {
2182 VALUE path = RARRAY_AREF(location, 0);
2183 VALUE line = RARRAY_AREF(location, 1);
2184 if (!NIL_P(path)) {
2186 "respond_to? is defined here");
2187 }
2188 }
2189 }
2190 }
2191 result = call_method_entry(ec, defined_class, obj, resid, me, argc, args, RB_NO_KEYWORDS);
2192 return RTEST(result);
2193 }
2194}
2195
2196int
2198{
2201 int ret = vm_respond_to(ec, klass, obj, id, priv);
2202 if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
2203 return ret;
2204}
2205
2206int
2208{
2209 return rb_obj_respond_to(obj, id, FALSE);
2210}
2211
2212
2213/*
2214 * call-seq:
2215 * obj.respond_to?(symbol, include_all=false) -> true or false
2216 * obj.respond_to?(string, include_all=false) -> true or false
2217 *
2218 * Returns +true+ if _obj_ responds to the given method. Private and
2219 * protected methods are included in the search only if the optional
2220 * second parameter evaluates to +true+.
2221 *
2222 * If the method is not implemented,
2223 * as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
2224 * false is returned.
2225 *
2226 * If the method is not defined, <code>respond_to_missing?</code>
2227 * method is called and the result is returned.
2228 *
2229 * When the method name parameter is given as a string, the string is
2230 * converted to a symbol.
2231 */
2232
2233static VALUE
2234obj_respond_to(int argc, VALUE *argv, VALUE obj)
2235{
2236 VALUE mid, priv;
2237 ID id;
2239
2240 rb_scan_args(argc, argv, "11", &mid, &priv);
2241 if (!(id = rb_check_id(&mid))) {
2242 VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
2243 rb_to_symbol(mid), priv);
2244 if (ret == Qundef) ret = Qfalse;
2245 return ret;
2246 }
2247 if (basic_obj_respond_to(ec, obj, id, !RTEST(priv)))
2248 return Qtrue;
2249 return Qfalse;
2250}
2251
2252/*
2253 * call-seq:
2254 * obj.respond_to_missing?(symbol, include_all) -> true or false
2255 * obj.respond_to_missing?(string, include_all) -> true or false
2256 *
2257 * DO NOT USE THIS DIRECTLY.
2258 *
2259 * Hook method to return whether the _obj_ can respond to _id_ method
2260 * or not.
2261 *
2262 * When the method name parameter is given as a string, the string is
2263 * converted to a symbol.
2264 *
2265 * See #respond_to?, and the example of BasicObject.
2266 */
2267static VALUE
2268obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
2269{
2270 return Qfalse;
2271}
2272
2273void
2275{
2276 if (!OPT_GLOBAL_METHOD_CACHE) return;
2277 char *ptr = getenv("RUBY_GLOBAL_METHOD_CACHE_SIZE");
2278 int val;
2279
2280 if (ptr != NULL && (val = atoi(ptr)) > 0) {
2281 if ((val & (val - 1)) == 0) { /* ensure val is a power of 2 */
2282 global_method_cache.size = val;
2283 global_method_cache.mask = val - 1;
2284 }
2285 else {
2286 fprintf(stderr, "RUBY_GLOBAL_METHOD_CACHE_SIZE was set to %d but ignored because the value is not a power of 2.\n", val);
2287 }
2288 }
2289
2290 global_method_cache.entries = (struct cache_entry *)calloc(global_method_cache.size, sizeof(struct cache_entry));
2291 if (global_method_cache.entries == NULL) {
2292 fprintf(stderr, "[FATAL] failed to allocate memory\n");
2294 }
2295}
2296
2297void
2299{
2300#undef rb_intern
2301#define rb_intern(str) rb_intern_const(str)
2302
2303 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
2304 rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2);
2305
2306 rb_define_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
2307 rb_define_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
2308 rb_define_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
2309 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
2310 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
2311 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
2312 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
2313 rb_define_private_method(rb_cModule, "ruby2_keywords", rb_mod_ruby2_keywords, -1);
2314
2315 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
2316 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
2317 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
2318 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
2319 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
2320 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
2321
2323 "public", top_public, -1);
2325 "private", top_private, -1);
2327 "ruby2_keywords", top_ruby2_keywords, -1);
2328
2329 {
2330#define REPLICATE_METHOD(klass, id) do { \
2331 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
2332 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
2333 } while (0)
2334
2338 }
2339}
#define mod(x, y)
Definition: date_strftime.c:28
enum @73::@75::@76 mask
struct RIMemo * ptr
Definition: debug.c:65
#define d1
void rb_print_undef(VALUE klass, ID id, rb_method_visibility_t visi)
Definition: eval_error.c:383
void rb_print_undef_str(VALUE klass, VALUE name)
Definition: eval_error.c:398
#define rb_intern_str(string)
Definition: generator.h:16
void rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE, VALUE), VALUE)
Definition: class.c:116
VALUE rb_singleton_class(VALUE)
Returns the singleton class of obj.
Definition: class.c:1743
void rb_class_modify_check(VALUE)
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
VALUE rb_mKernel
Kernel module.
Definition: ruby.h:2000
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
@ RMODULE_IS_REFINEMENT
Definition: ruby.h:956
void rb_notimplement(void)
Definition: error.c:2714
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
void rb_bug(const char *fmt,...)
Definition: error.c:636
VALUE rb_eTypeError
Definition: error.c:924
void rb_warn(const char *fmt,...)
Definition: error.c:315
VALUE rb_eArgError
Definition: error.c:925
VALUE rb_eException
Definition: error.c:916
VALUE rb_equal(VALUE, VALUE)
Same as Object#===, case equality.
Definition: object.c:124
void rb_id_table_clear(struct rb_id_table *tbl)
Definition: id_table.c:109
int rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
Definition: id_table.c:256
int rb_id_table_lookup(struct rb_id_table *tbl, ID id, VALUE *valp)
Definition: id_table.c:226
struct rb_id_table * rb_id_table_create(size_t capa)
Definition: id_table.c:95
int rb_id_table_delete(struct rb_id_table *tbl, ID id)
Definition: id_table.c:262
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:39
const char * name
Definition: nkf.c:208
void rb_method_name_error(VALUE klass, VALUE str)
Definition: proc.c:1778
#define OPT_GLOBAL_METHOD_CACHE
#define NULL
VALUE rb_iseq_path(const rb_iseq_t *iseq)
Definition: iseq.c:1027
#define FL_SINGLETON
@ idRespond_to_missing
const char *void void rb_compile_warning(const char *, int, const char *,...) __attribute__((format(printf
#define _(args)
#define RTEST(v)
#define rb_hash_uint(h, i)
#define RCLASS_SUPER(c)
#define FL_TEST(x, f)
int rb_method_entry_arity(const rb_method_entry_t *me)
Definition: proc.c:2555
#define METHOD_ENTRY_COMPLEMENTED(me)
unsigned long st_data_t
#define RUBY_DTRACE_HOOK(name, arg)
void rb_compile_warn(const char *, int, const char *,...) __attribute__((format(printf
#define RCLASS_CALLABLE_M_TBL(c)
void rb_define_private_method(VALUE, const char *, VALUE(*)(), int)
rb_control_frame_t * cfp
#define xfree
int atoi(const char *__nptr)
#define QUOTE_ID(id)
#define Qundef
#define UNALIGNED_MEMBER_PTR(ptr, mem)
const VALUE VALUE obj
#define METHOD_ENTRY_COMPLEMENTED_SET(me)
#define rb_check_frozen(obj)
st_index_t rb_hash_proc(st_index_t hash, VALUE proc)
Definition: proc.c:1302
#define RSTRING_PTR(str)
#define RCLASS_SERIAL(c)
#define GET_EC()
#define INC_GLOBAL_METHOD_STATE()
#define NIL_P(v)
const rb_callable_method_entry_t * me
#define rb_name_err_raise(mesg, recv, name)
const rb_iseq_t * rb_proc_get_iseq(VALUE proc, int *is_proc)
Definition: proc.c:1194
#define VM_ASSERT(expr)
#define ID2SYM(x)
const char * rb_id2name(ID)
Definition: symbol.c:801
@ block_handler_type_proc
@ block_handler_type_iseq
#define VM_DEBUG_VERIFY_METHOD_CACHE
int fprintf(FILE *__restrict__, const char *__restrict__,...) __attribute__((__format__(__printf__
#define ruby_verbose
unsigned long VALUE
#define stderr
__inline__ const void *__restrict__ src
int rb_vm_get_sourceline(const rb_control_frame_t *)
Definition: vm_backtrace.c:68
void * calloc(size_t, size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1
@ METHOD_VISI_PROTECTED
int rb_vm_check_optimizable_mid(VALUE mid)
Definition: vm.c:1581
#define T_MODULE
#define UNDEFINED_REFINED_METHOD_P(def)
uint32_t i
#define EXIT_FAILURE
#define ZALLOC(type)
#define RB_OBJ_WRITE(a, slot, b)
#define T_ICLASS
#define INC_GLOBAL_CONSTANT_STATE()
#define NUM2INT(x)
@ VM_METHOD_TYPE_ATTRSET
@ VM_METHOD_TYPE_CFUNC
@ VM_METHOD_TYPE_OPTIMIZED
@ VM_METHOD_TYPE_REFINED
@ VM_METHOD_TYPE_NOTIMPLEMENTED
@ VM_METHOD_TYPE_MISSING
@ VM_METHOD_TYPE_BMETHOD
@ VM_METHOD_TYPE_ZSUPER
@ VM_METHOD_TYPE_ALIAS
@ VM_METHOD_TYPE_UNDEF
#define PRIsVALUE
unsigned long long rb_serial_t
#define rb_ary_new3
rb_serial_t rb_next_class_serial(void)
Definition: vm.c:358
#define RCLASS_ORIGIN(c)
#define FIX2INT(x)
int VALUE v
#define rb_scan_args(argc, argvp, fmt,...)
rb_control_frame_t * rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
Definition: vm.c:553
#define UNREACHABLE_RETURN(val)
const rb_iseq_t * iseq
VALUE rb_ary_freeze(VALUE)
Definition: array.c:648
#define RCLASS_EXT(c)
#define TRUE
#define FALSE
#define UNDEFINED_METHOD_ENTRY_P(me)
unsigned int size
#define Qtrue
#define PREV_CLASS_SERIAL()
#define RB_NO_KEYWORDS
#define RCLASS_M_TBL(c)
const char * rb_obj_info(VALUE obj)
Definition: gc.c:11713
__uintptr_t uintptr_t
void exit(int __status) __attribute__((__noreturn__))
#define Qnil
#define Qfalse
st_data_t st_index_t
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:2321
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:919
#define RB_TYPE_P(obj, type)
#define INT2FIX(i)
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1481
VALUE rb_to_symbol(VALUE name)
Definition: string.c:11156
#define MJIT_FUNC_EXPORTED
const VALUE * argv
VALUE rb_method_entry_location(const rb_method_entry_t *me)
Definition: proc.c:2725
#define T_CLASS
#define CLASS_OF(v)
const VALUE VALUE VALUE marker
void rb_gc_writebarrier_remember(VALUE obj)
Definition: gc.c:6891
rb_cref_t * rb_vm_cref(void)
Definition: vm.c:1384
if((__builtin_expect(!!(!me), 0)))
#define Check_Type(v, t)
void mjit_remove_class_serial(rb_serial_t class_serial)
ID rb_id_attrset(ID)
Definition: symbol.c:98
VALUE rb_sprintf(const char *,...) __attribute__((format(printf
unsigned long ID
const char *void rb_warning(const char *,...) __attribute__((format(printf
VALUE ID id
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
#define RARRAY_AREF(a, i)
#define BUILTIN_TYPE(x)
VALUE(* rb_alloc_func_t)(VALUE)
#define RB_OBJ_WRITTEN(a, oldv, b)
struct iseq_catch_table_entry entries[]
const char * rb_class2name(VALUE)
Definition: variable.c:280
#define ANYARGS
#define GET_GLOBAL_METHOD_STATE()
_ssize_t write(int __fd, const void *__buf, size_t __nbyte)
VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat)
Definition: vm_eval.c:265
_ssize_t read(int __fd, void *__buf, size_t __nbyte)
#define METHOD_ENTRY_BASIC(me)
#define METHOD_ENTRY_VISI(me)
ID rb_to_id(VALUE)
Definition: string.c:11146
#define RB_DEBUG_COUNTER_INC(type)
Definition: vm_method.c:40
rb_serial_t method_state
Definition: vm_method.c:41
rb_serial_t class_serial
Definition: vm_method.c:42
rb_method_entry_t * me
Definition: vm_method.c:44
ID mid
Definition: vm_method.c:43
VALUE defined_class
Definition: vm_method.c:45
const struct rb_callable_method_entry_struct * me
struct rb_call_info ci
struct rb_call_cache cc
ID called_id
struct rb_method_definition_struct *const def
const VALUE defined_class
const VALUE owner
union rb_captured_block::@53 code
CREF (Class REFerence)
const rb_scope_visibility_t scope_visi
enum rb_iseq_constant_body::iseq_type type
struct rb_iseq_constant_body::@45 param
struct rb_iseq_constant_body::@45::@47 flags
struct rb_iseq_constant_body * body
struct rb_method_entry_struct * original_me
struct rb_hook_list_struct * hooks
VALUE(* invoker)(VALUE recv, int argc, const VALUE *argv, VALUE(*func)())
enum method_optimized_type optimize_type
union rb_method_definition_struct::@41 body
struct rb_method_definition_struct *const def
ID called_id
VALUE defined_class
VALUE owner
rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
rb_cref_t * cref
class reference, should be marked
struct rb_method_entry_struct * orig_me
rb_method_visibility_t method_visi
rb_subclass_entry_t * next
VALUE klass
VALUE rb_vm_top_self(void)
Definition: vm.c:3349
#define rb_id2str(id)
Definition: vm_backtrace.c:30
int rb_method_basic_definition_p(VALUE klass, ID id)
Definition: vm_method.c:2089
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t * rb_callable_method_entry(VALUE klass, ID id)
Definition: vm_method.c:895
int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
Definition: vm_method.c:1493
#define CALL_METHOD_HOOK(klass, hook, mid)
Definition: vm_method.c:655
void rb_attr(VALUE klass, ID id, int read, int write, int ex)
Definition: vm_method.c:1180
int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
Definition: vm_method.c:1521
MJIT_FUNC_EXPORTED const rb_method_entry_t * rb_method_entry(VALUE klass, ID id)
Definition: vm_method.c:854
void rb_undef_alloc_func(VALUE klass)
Definition: vm_method.c:729
void rb_clear_method_cache_by_class(VALUE klass)
Definition: vm_method.c:93
const rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
Definition: vm_method.c:989
void Init_Method(void)
Definition: vm_method.c:2274
#define added
Definition: vm_method.c:32
void Init_eval_method(void)
Definition: vm_method.c:2298
#define METHOD_DEBUG
Definition: vm_method.c:7
void rb_clear_constant_cache(void)
Definition: vm_method.c:87
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
Definition: vm_method.c:451
void rb_undef(VALUE klass, ID id)
Definition: vm_method.c:1217
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:491
const rb_cref_t * rb_vm_cref_in_context(VALUE self, VALUE cbase)
Definition: vm.c:1400
bool rb_method_basic_definition_p_with_cc(struct rb_call_data *cd, VALUE klass, ID mid)
Definition: vm_method.c:2074
const rb_method_entry_t * rb_method_entry_at(VALUE klass, ID id)
Definition: vm_method.c:767
const rb_method_entry_t * rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
Definition: vm_method.c:928
void rb_remove_method(VALUE klass, const char *name)
Definition: vm_method.c:1049
MJIT_FUNC_EXPORTED rb_method_definition_t * rb_method_definition_create(rb_method_type_t type, ID mid)
Definition: vm_method.c:348
void rb_free_method_entry(const rb_method_entry_t *me)
Definition: vm_method.c:174
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Definition: vm_method.c:735
void rb_alias(VALUE klass, ID alias_name, ID original_name)
Definition: vm_method.c:1598
#define REPLICATE_METHOD(klass, id)
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *src_me)
Definition: vm_method.c:406
int rb_respond_to(VALUE obj, ID id)
Definition: vm_method.c:2207
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_visibility_t visi)
Definition: vm_method.c:714
#define UNDEF_ALLOC_FUNC
Definition: vm_method.c:719
#define removed
Definition: vm_method.c:34
MJIT_FUNC_EXPORTED void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
Definition: vm_method.c:685
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t * rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
Definition: vm_method.c:934
#define ruby_running
Definition: vm_method.c:59
#define object_id
Definition: vm_method.c:31
#define rb_intern(str)
int rb_obj_respond_to(VALUE obj, ID id, int priv)
Definition: vm_method.c:2197
void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
Definition: vm_method.c:675
void rb_remove_method_id(VALUE klass, ID mid)
Definition: vm_method.c:1043
MJIT_FUNC_EXPORTED void rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
Definition: vm_method.c:232
void rb_scope_visibility_set(rb_method_visibility_t visi)
Definition: vm_method.c:1155
#define GLOBAL_METHOD_CACHE(c, m)
Definition: vm_method.c:25
const rb_method_entry_t * rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
Definition: vm_method.c:942
#define undefined
Definition: vm_method.c:36
void rb_define_alloc_func(VALUE klass, VALUE(*func)(VALUE))
Definition: vm_method.c:722
VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
Definition: vm_method.c:120
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t * rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
Definition: vm_method.c:415
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t * rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
Definition: vm_method.c:996
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_visibility_t visi)
Definition: vm_method.c:134
st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
Definition: vm_method.c:1592
int rb_method_boundp(VALUE klass, ID id, int ex)
Definition: vm_method.c:1119
rb_method_entry_t * rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def)
Definition: vm_method.c:397
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t * rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
Definition: vm_method.c:948
#define BOUND_RESPONDS
Definition: vm_method.c:1116
#define getenv(name)
Definition: win32.c:73