11 static int vm_redefinition_check_flag(
VALUE klass);
15 #define object_id idObject_id
16 #define added idMethod_added
17 #define singleton_added idSingleton_method_added
18 #define removed idMethod_removed
19 #define singleton_removed idSingleton_method_removed
20 #define undefined idMethod_undefined
21 #define singleton_undefined idSingleton_method_undefined
23 #define ruby_running (GET_VM()->running)
26 static enum rb_id_table_iterator_result
27 vm_ccs_dump_i(
ID mid,
VALUE val,
void *data)
30 fprintf(stderr,
" | %s (len:%d) ",
rb_id2name(mid), ccs->len);
33 for (
int i=0; i<ccs->len; i++) {
34 rp_m(
" | \t", ccs->entries[i].cc);
37 return ID_TABLE_CONTINUE;
41 vm_ccs_dump(
VALUE klass,
ID target_mid)
47 if (rb_id_table_lookup(cc_tbl, target_mid, &ccs)) {
48 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
49 vm_ccs_dump_i(target_mid, ccs, NULL);
53 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
54 rb_id_table_foreach(cc_tbl, vm_ccs_dump_i, (
void *)target_mid);
59 static enum rb_id_table_iterator_result
60 vm_cme_dump_i(
ID mid,
VALUE val,
void *data)
62 ID target_mid = (
ID)data;
63 if (target_mid == 0 || mid == target_mid) {
66 return ID_TABLE_CONTINUE;
70 vm_mtbl_dump(
VALUE klass,
ID target_mid)
72 fprintf(stderr,
"# vm_mtbl\n");
77 if (RCLASS_M_TBL(klass)) {
78 if (target_mid != 0) {
79 if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
84 fprintf(stderr,
" ## RCLASS_M_TBL (%p)\n", (
void *)RCLASS_M_TBL(klass));
85 rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
89 fprintf(stderr,
" MTBL: NULL\n");
91 if (RCLASS_CALLABLE_M_TBL(klass)) {
92 if (target_mid != 0) {
93 if (rb_id_table_lookup(RCLASS_CALLABLE_M_TBL(klass), target_mid, &me)) {
98 fprintf(stderr,
" ## RCLASS_CALLABLE_M_TBL\n");
99 rb_id_table_foreach(RCLASS_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
102 if (RCLASS_CC_TBL(klass)) {
103 vm_ccs_dump(klass, target_mid);
111 rb_vm_mtbl_dump(
const char *msg,
VALUE klass,
ID target_mid)
113 fprintf(stderr,
"[%s] ", msg);
114 vm_mtbl_dump(klass, target_mid);
120 VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment),
"cme: %d", imemo_type((
VALUE)cme));
121 VM_ASSERT(callable_method_entry_p(cme));
122 METHOD_ENTRY_INVALIDATED_SET(cme);
123 RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
125 rb_yjit_cme_invalidate(cme);
126 rb_rjit_cme_invalidate(cme);
130 rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t idx, st_data_t arg)
132 ((
IC) ic)->entry = NULL;
137 void rb_clear_constant_cache(
void) {}
145 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
147 st_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
148 ruby_vm_constant_cache_invalidations += ics->num_entries;
151 rb_yjit_constant_state_changed(
id);
152 rb_rjit_constant_state_changed(
id);
156 invalidate_negative_cache(
ID mid)
161 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
162 rb_id_table_delete(vm->negative_cme_table, mid);
164 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
174 clear_method_cache_by_id_in_class(
VALUE klass,
ID mid)
177 if (rb_objspace_garbage_object_p(klass))
return;
180 if (LIKELY(RCLASS_SUBCLASSES(klass) == NULL)) {
188 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
192 if (
NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
194 rb_id_table_delete(cc_tbl, mid);
195 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
200 if ((cm_tbl = RCLASS_CALLABLE_M_TBL(klass)) != NULL) {
202 if (rb_yjit_enabled_p && rb_id_table_lookup(cm_tbl, mid, &cme)) {
205 if (rb_rjit_enabled && rb_id_table_lookup(cm_tbl, mid, &cme)) {
208 rb_id_table_delete(cm_tbl, mid);
209 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
211 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
218 if (METHOD_ENTRY_CACHED(cme)) {
219 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
224 VALUE owner = cme->owner;
225 VM_ASSERT_TYPE(owner,
T_CLASS);
226 VALUE klass_housing_cme;
227 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
228 klass_housing_cme = owner;
231 klass_housing_cme = RCLASS_ORIGIN(owner);
234 VM_ASSERT(lookup_method_table(klass_housing_cme, mid) == (
const rb_method_entry_t *)cme);
236 rb_method_table_insert(klass_housing_cme, RCLASS_M_TBL(klass_housing_cme), mid, new_cme);
240 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
245 if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
249 if (cme->def->iseq_overload) {
252 vm_cme_invalidate(monly_cme);
258 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
259 VALUE defined_class = cme->defined_class;
260 struct rb_id_table *cm_tbl = RCLASS_CALLABLE_M_TBL(defined_class);
261 VM_ASSERT(cm_tbl != NULL);
262 int r = rb_id_table_delete(cm_tbl, mid);
263 VM_ASSERT(r == TRUE); (void)r;
264 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
267 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
270 invalidate_negative_cache(mid);
277 clear_iclass_method_cache_by_id(
VALUE iclass,
VALUE d)
281 clear_method_cache_by_id_in_class(iclass, mid);
285 clear_iclass_method_cache_by_id_for_refinements(
VALUE klass,
VALUE d)
289 clear_method_cache_by_id_in_class(klass, mid);
294 rb_clear_method_cache(
VALUE klass_or_module,
ID mid)
297 VALUE module = klass_or_module;
299 if (
FL_TEST(module, RMODULE_IS_REFINEMENT)) {
300 VALUE refined_class = rb_refinement_module_get_refined_class(module);
301 rb_clear_method_cache(refined_class, mid);
302 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
303 rb_clear_all_refinement_method_cache();
305 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
308 clear_method_cache_by_id_in_class(klass_or_module, mid);
313 invalidate_all_refinement_cc(
void *vstart,
void *vend,
size_t stride,
void *data)
316 for (; v != (
VALUE)vend; v += stride) {
317 void *
ptr = asan_poisoned_object_p(v);
318 asan_unpoison_object(v,
false);
321 if (imemo_type_p(v, imemo_callcache)) {
323 if (vm_cc_refinement_p(cc) && cc->klass) {
324 vm_cc_invalidate(cc);
330 asan_poison_object(v);
345 for (
int i = 0; i < ci->kwarg->keyword_len; i++) {
357 if (ci1->mid != ci2->mid)
return 1;
358 if (ci1->flag != ci2->flag)
return 1;
359 if (ci1->argc != ci2->argc)
return 1;
360 if (ci1->kwarg != NULL) {
361 VM_ASSERT(ci2->kwarg != NULL);
363 if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
366 for (
int i = 0; i < ci1->kwarg->keyword_len; i++) {
367 if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
372 VM_ASSERT(ci2->kwarg == NULL);
383 ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
386 st_data_t *ret = (st_data_t *)data;
389 if (rb_objspace_garbage_object_p((
VALUE)ci)) {
390 *ret = (st_data_t)NULL;
398 *key = *value = *ret = (st_data_t)ci;
404 rb_vm_ci_lookup(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg)
424 st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
425 }
while (ci == NULL);
441 st_data_t key = (st_data_t)ci;
442 st_delete(vm->ci_table, &key, NULL);
446 rb_clear_all_refinement_method_cache(
void)
448 rb_objspace_each_objects(invalidate_all_refinement_cc, NULL);
449 rb_yjit_invalidate_all_method_lookup_assumptions();
455 VALUE table_owner = klass;
457 table_owner =
RBASIC(table_owner)->klass;
460 VM_ASSERT(table == RCLASS_M_TBL(table_owner));
461 rb_id_table_insert(table, method_id, (
VALUE)me);
470 NORETURN(
static VALUE rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj));
473 rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj)
483 rb_f_notimplement_internal(argc, argv, obj);
487 rb_define_notimplement_method_id(
VALUE mod,
ID id, rb_method_visibility_t visi)
489 rb_add_method(mod,
id, VM_METHOD_TYPE_NOTIMPLEMENTED, (
void *)1, visi);
493 rb_add_method_cfunc(
VALUE klass,
ID mid,
VALUE (*func)(
ANYARGS),
int argc, rb_method_visibility_t visi)
500 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
503 rb_define_notimplement_method_id(klass, mid, visi);
508 rb_add_method_optimized(
VALUE klass,
ID mid,
enum method_optimized_type opt_type,
unsigned int index, rb_method_visibility_t visi)
514 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
521 const int reference_count = def->reference_count;
522 def->reference_count--;
524 VM_ASSERT(reference_count >= 0);
526 if (def->reference_count == 0) {
527 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d (remove)\n", (
void *)def,
528 rb_id2name(def->original_id), def->reference_count);
529 if (def->type == VM_METHOD_TYPE_BMETHOD && def->body.bmethod.hooks) {
530 xfree(def->body.bmethod.hooks);
535 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d->%d (dec)\n", (
void *)def,
rb_id2name(def->original_id),
536 reference_count, def->reference_count);
546 if (me->def && me->def->iseq_overload) {
554 rb_method_definition_release(me->def);
563 if (!GET_THREAD()->ext_config.ractor_safe) {
565 case -2:
return &call_cfunc_m2;
566 case -1:
return &call_cfunc_m1;
567 case 0:
return &call_cfunc_0;
568 case 1:
return &call_cfunc_1;
569 case 2:
return &call_cfunc_2;
570 case 3:
return &call_cfunc_3;
571 case 4:
return &call_cfunc_4;
572 case 5:
return &call_cfunc_5;
573 case 6:
return &call_cfunc_6;
574 case 7:
return &call_cfunc_7;
575 case 8:
return &call_cfunc_8;
576 case 9:
return &call_cfunc_9;
577 case 10:
return &call_cfunc_10;
578 case 11:
return &call_cfunc_11;
579 case 12:
return &call_cfunc_12;
580 case 13:
return &call_cfunc_13;
581 case 14:
return &call_cfunc_14;
582 case 15:
return &call_cfunc_15;
584 rb_bug(
"unsupported length: %d", argc);
589 case -2:
return &ractor_safe_call_cfunc_m2;
590 case -1:
return &ractor_safe_call_cfunc_m1;
591 case 0:
return &ractor_safe_call_cfunc_0;
592 case 1:
return &ractor_safe_call_cfunc_1;
593 case 2:
return &ractor_safe_call_cfunc_2;
594 case 3:
return &ractor_safe_call_cfunc_3;
595 case 4:
return &ractor_safe_call_cfunc_4;
596 case 5:
return &ractor_safe_call_cfunc_5;
597 case 6:
return &ractor_safe_call_cfunc_6;
598 case 7:
return &ractor_safe_call_cfunc_7;
599 case 8:
return &ractor_safe_call_cfunc_8;
600 case 9:
return &ractor_safe_call_cfunc_9;
601 case 10:
return &ractor_safe_call_cfunc_10;
602 case 11:
return &ractor_safe_call_cfunc_11;
603 case 12:
return &ractor_safe_call_cfunc_12;
604 case 13:
return &ractor_safe_call_cfunc_13;
605 case 14:
return &ractor_safe_call_cfunc_14;
606 case 15:
return &ractor_safe_call_cfunc_15;
608 rb_bug(
"unsupported length: %d", argc);
618 cfunc->invoker = call_cfunc_invoker_func(argc);
624 if (!complemented && def->reference_count > 0) def->aliased =
true;
625 def->reference_count++;
626 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d\n", (
void *)def,
rb_id2name(def->original_id), def->reference_count);
633 rb_method_definition_release(me->def);
636 if (!ruby_running) add_opt_method_entry(me);
640 case VM_METHOD_TYPE_ISEQ:
650 if (rb_iseq_attr_p(iseq, BUILTIN_ATTR_C_TRACE)) {
654 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
656 if (0) vm_cref_dump(
"rb_method_definition_create", cref);
662 method_cref = vm_cref_new_toplevel(GET_EC());
668 case VM_METHOD_TYPE_CFUNC:
671 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
674 case VM_METHOD_TYPE_ATTRSET:
675 case VM_METHOD_TYPE_IVAR:
681 def->body.attr.id = (
ID)(
VALUE)opts;
683 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
685 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
690 VM_ASSERT(def->body.attr.location == 0);
694 case VM_METHOD_TYPE_BMETHOD:
696 RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
698 case VM_METHOD_TYPE_NOTIMPLEMENTED:
699 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (
VALUE(*)(
ANYARGS))rb_f_notimplement_internal, -1);
701 case VM_METHOD_TYPE_OPTIMIZED:
704 case VM_METHOD_TYPE_REFINED:
709 case VM_METHOD_TYPE_ALIAS:
712 case VM_METHOD_TYPE_ZSUPER:
713 case VM_METHOD_TYPE_UNDEF:
714 case VM_METHOD_TYPE_MISSING:
726 case VM_METHOD_TYPE_ISEQ:
730 case VM_METHOD_TYPE_ATTRSET:
731 case VM_METHOD_TYPE_IVAR:
734 case VM_METHOD_TYPE_BMETHOD:
738 if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((
VALUE)me);
740 case VM_METHOD_TYPE_REFINED:
743 case VM_METHOD_TYPE_ALIAS:
746 case VM_METHOD_TYPE_CFUNC:
747 case VM_METHOD_TYPE_ZSUPER:
748 case VM_METHOD_TYPE_MISSING:
749 case VM_METHOD_TYPE_OPTIMIZED:
750 case VM_METHOD_TYPE_UNDEF:
751 case VM_METHOD_TYPE_NOTIMPLEMENTED:
757 rb_method_definition_create(rb_method_type_t
type,
ID mid)
762 def->original_id = mid;
763 static uintptr_t method_serial = 1;
764 def->method_serial = method_serial++;
771 if (def) method_definition_addref(def, complement);
772 if (
RTEST(defined_class)) {
778 me->called_id = called_id;
785 filter_defined_class(
VALUE klass)
797 rb_bug(
"filter_defined_class: %s", rb_obj_info(klass));
803 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def,
false);
804 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
805 if (def != NULL) method_definition_reset(me);
813 rb_method_entry_t *me = rb_method_entry_alloc(src_me->called_id, src_me->owner, src_me->defined_class, src_me->def, METHOD_ENTRY_COMPLEMENTED(src_me));
815 METHOD_ENTRY_FLAGS_COPY(me, src_me);
819 src_me->def->type == VM_METHOD_TYPE_REFINED &&
820 src_me->def->body.refined.orig_me) {
822 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
825 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
826 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
831 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
832 rb_method_definition_set(me, clone_def, orig_clone);
844 if (!src_me->defined_class &&
845 def->type == VM_METHOD_TYPE_REFINED &&
846 def->body.refined.orig_me) {
848 rb_method_entry_clone(def->body.refined.orig_me);
850 refined_orig_me = orig_me;
854 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def,
true);
855 METHOD_ENTRY_FLAGS_COPY(me, src_me);
856 METHOD_ENTRY_COMPLEMENTED_SET(me);
858 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
859 rb_method_definition_set(me, def, (
void *)refined_orig_me);
862 VM_ASSERT_TYPE(me->owner,
T_MODULE);
870 rb_method_definition_release(dst->def);
871 *(
rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
872 method_definition_reset(dst);
873 dst->called_id = src->called_id;
876 METHOD_ENTRY_FLAGS_COPY(dst, src);
882 if (me->def->type == VM_METHOD_TYPE_REFINED) {
888 rb_vm_check_redefinition_opt_method(me, me->owner);
891 rb_method_entry_alloc(me->called_id,
896 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
898 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
899 rb_method_definition_set(me, def, orig_me);
900 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
905 lookup_method_table(
VALUE klass,
ID id)
910 if (rb_id_table_lookup(m_tbl,
id, &body)) {
919 rb_add_refined_method_entry(
VALUE refined_class,
ID mid)
924 make_method_entry_refined(refined_class, me);
925 rb_clear_method_cache(refined_class, mid);
928 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
933 check_override_opt_method_i(
VALUE klass,
VALUE arg)
938 if (vm_redefinition_check_flag(klass)) {
939 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
941 newme = rb_method_entry(klass, mid);
942 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
945 rb_class_foreach_subclass(klass, check_override_opt_method_i, (
VALUE)mid);
949 check_override_opt_method(
VALUE klass,
VALUE mid)
951 if (rb_vm_check_optimizable_mid(mid)) {
952 check_override_opt_method_i(klass, mid);
963 rb_method_entry_make(
VALUE klass,
ID mid,
VALUE defined_class, rb_method_visibility_t visi,
969 int make_refined = 0;
977 if (!RCLASS_SINGLETON_P(klass) &&
978 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
979 type != VM_METHOD_TYPE_ZSUPER) {
982 case idInitialize_copy:
983 case idInitialize_clone:
984 case idInitialize_dup:
985 case idRespond_to_missing:
986 visi = METHOD_VISI_PRIVATE;
990 if (
type != VM_METHOD_TYPE_REFINED) {
995 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
996 rb_add_refined_method_entry(refined_class, mid);
998 if (
type == VM_METHOD_TYPE_REFINED) {
1000 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1003 klass = RCLASS_ORIGIN(klass);
1004 if (klass != orig_klass) {
1005 rb_clear_method_cache(orig_klass, mid);
1008 mtbl = RCLASS_M_TBL(klass);
1011 if (rb_id_table_lookup(mtbl, mid, &data)) {
1015 if (rb_method_definition_eq(old_def, def))
return old_me;
1016 rb_vm_check_redefinition_opt_method(old_me, klass);
1018 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1021 type != VM_METHOD_TYPE_UNDEF &&
1022 (old_def->aliased ==
false) &&
1023 (!old_def->no_redef_warning) &&
1025 old_def->type != VM_METHOD_TYPE_UNDEF &&
1026 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1027 old_def->type != VM_METHOD_TYPE_ALIAS) {
1030 switch (old_def->type) {
1031 case VM_METHOD_TYPE_ISEQ:
1032 iseq = def_iseq_ptr(old_def);
1034 case VM_METHOD_TYPE_BMETHOD:
1035 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1042 "method redefined; discarding old %"PRIsVALUE
"\n%s:%d: warning: previous definition of %"PRIsVALUE
" was here",
1045 ISEQ_BODY(iseq)->location.first_lineno,
1056 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1058 def = rb_method_definition_create(
type, original_id);
1060 rb_method_definition_set(me, def, opts);
1062 rb_clear_method_cache(klass, mid);
1065 if (klass == rb_cObject) {
1068 case idRespond_to_missing:
1069 case idMethodMissing:
1075 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1076 if (
type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1082 make_method_entry_refined(klass, me);
1085 rb_method_table_insert(klass, mtbl, mid, me);
1087 VM_ASSERT(me->def != NULL);
1091 check_override_opt_method(klass, (
VALUE)mid);
1098 overloaded_cme_table(
void)
1100 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
1101 return GET_VM()->overloaded_cme_table;
1104 #if VM_CHECK_MODE > 0
1106 vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1108 fprintf(stderr,
"key: "); rp(key);
1109 fprintf(stderr,
"val: "); rp(val);
1114 rb_vm_dump_overloaded_cme_table(
void)
1116 fprintf(stderr,
"== rb_vm_dump_overloaded_cme_table\n");
1117 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1122 lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
1129 if (rb_objspace_garbage_object_p((
VALUE)cme) ||
1130 rb_objspace_garbage_object_p((
VALUE)monly_cme)) {
1145 ASSERT_vm_locking();
1148 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1152 #if VM_CHECK_MODE > 0
1156 return lookup_overloaded_cme(cme);
1163 st_data_t cme_data = (st_data_t)cme;
1164 ASSERT_vm_locking();
1165 st_delete(overloaded_cme_table(), &cme_data, NULL);
1173 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1188 ASSERT_vm_locking();
1189 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1191 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1199 if (UNLIKELY(cme->def->iseq_overload) &&
1200 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1201 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1202 (
int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1203 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ,
"type: %d", cme->def->type);
1205 cme = get_overloaded_cme(cme);
1207 VM_ASSERT(cme != NULL);
1214 #define CALL_METHOD_HOOK(klass, hook, mid) do { \
1215 const VALUE arg = ID2SYM(mid); \
1216 VALUE recv_class = (klass); \
1217 ID hook_id = (hook); \
1218 if (RCLASS_SINGLETON_P((klass))) { \
1219 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1220 hook_id = singleton_##hook; \
1222 rb_funcallv(recv_class, hook_id, 1, &arg); \
1226 method_added(
VALUE klass,
ID mid)
1229 CALL_METHOD_HOOK(klass, added, mid);
1234 rb_add_method(
VALUE klass,
ID mid, rb_method_type_t
type,
void *opts, rb_method_visibility_t visi)
1236 rb_method_entry_make(klass, mid, klass, visi,
type, NULL, mid, opts);
1238 if (
type != VM_METHOD_TYPE_UNDEF &&
type != VM_METHOD_TYPE_REFINED) {
1239 method_added(klass, mid);
1252 iseq_body.
cref = cref;
1254 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1259 rb_method_visibility_t visi,
VALUE defined_class)
1261 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
1262 me->def->type, me->def, 0, NULL);
1264 me->def->no_redef_warning = TRUE;
1265 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1268 method_added(klass, mid);
1275 return method_entry_set(klass, mid, me, visi, klass);
1278 #define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1284 if (RCLASS_SINGLETON_P(klass)) {
1287 RCLASS_SET_ALLOCATOR(klass, func);
1303 if (allocator == UNDEF_ALLOC_FUNC)
break;
1304 if (allocator)
return allocator;
1310 rb_method_entry_at(
VALUE klass,
ID id)
1312 return lookup_method_table(klass,
id);
1316 search_method0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool skip_refined)
1320 RB_DEBUG_COUNTER_INC(mc_search);
1323 RB_DEBUG_COUNTER_INC(mc_search_super);
1324 if ((me = lookup_method_table(klass,
id)) != 0) {
1325 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1326 me->def->body.refined.orig_me) {
1332 if (defined_class_ptr) *defined_class_ptr = klass;
1334 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1336 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me));
1341 search_method(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1343 return search_method0(klass,
id, defined_class_ptr,
false);
1347 search_method_protect(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1351 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1360 rb_method_entry(
VALUE klass,
ID id)
1362 return search_method_protect(klass,
id, NULL);
1373 if (me->defined_class == 0) {
1374 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1376 VM_ASSERT(me->defined_class == 0,
"me->defined_class: %s", rb_obj_info(me->defined_class));
1378 mtbl = RCLASS_CALLABLE_M_TBL(defined_class);
1380 if (mtbl && rb_id_table_lookup(mtbl,
id, &cme_data)) {
1382 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1383 VM_ASSERT(callable_method_entry_p(cme));
1384 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1388 mtbl = RCLASS_EXT(defined_class)->callable_m_tbl = rb_id_table_create(0);
1390 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1391 rb_id_table_insert(mtbl,
id, (
VALUE)cme);
1393 VM_ASSERT(callable_method_entry_p(cme));
1401 VM_ASSERT(callable_method_entry_p(cme));
1402 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1412 complemented_callable_method_entry(
VALUE klass,
ID id)
1414 VALUE defined_class;
1416 return prepare_callable_method_entry(defined_class,
id, me, FALSE);
1420 cached_callable_method_entry(
VALUE klass,
ID mid)
1422 ASSERT_vm_locking();
1424 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1427 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1429 VM_ASSERT(vm_ccs_p(ccs));
1431 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1432 VM_ASSERT(ccs->cme->called_id == mid);
1433 RB_DEBUG_COUNTER_INC(ccs_found);
1437 rb_vm_ccs_free(ccs);
1438 rb_id_table_delete(cc_tbl, mid);
1442 RB_DEBUG_COUNTER_INC(ccs_not_found);
1449 ASSERT_vm_locking();
1450 VM_ASSERT(cme != NULL);
1452 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1456 cc_tbl = RCLASS_CC_TBL(klass) = rb_id_table_create(2);
1459 if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1460 #if VM_CHECK_MODE > 0
1462 VM_ASSERT(ccs->cme == cme);
1466 vm_ccs_create(klass, cc_tbl, mid, cme);
1471 negative_cme(
ID mid)
1477 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1482 rb_id_table_insert(vm->negative_cme_table, mid, (
VALUE)cme);
1485 VM_ASSERT(cme != NULL);
1490 callable_method_entry_or_negative(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1497 cme = cached_callable_method_entry(klass, mid);
1500 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1503 VALUE defined_class;
1505 if (defined_class_ptr) *defined_class_ptr = defined_class;
1508 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1511 cme = negative_cme(mid);
1514 cache_callable_method_entry(klass, mid, cme);
1525 rb_callable_method_entry_or_negative(
VALUE klass,
ID mid)
1527 return callable_method_entry_or_negative(klass, mid, NULL);
1531 callable_method_entry(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1534 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
1535 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1539 rb_callable_method_entry(
VALUE klass,
ID mid)
1541 return callable_method_entry(klass, mid, NULL);
1547 method_entry_resolve_refinement(
VALUE klass,
ID id,
int with_refinement,
VALUE *defined_class_ptr)
1549 const rb_method_entry_t *me = search_method_protect(klass,
id, defined_class_ptr);
1552 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1553 if (with_refinement) {
1555 VALUE refinements = cref ? CREF_REFINEMENTS(cref) :
Qnil;
1556 me = resolve_refined_method(refinements, me, defined_class_ptr);
1559 me = resolve_refined_method(
Qnil, me, defined_class_ptr);
1562 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1570 rb_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1572 return method_entry_resolve_refinement(klass,
id, TRUE, defined_class_ptr);
1576 callable_method_entry_refinements0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements,
1579 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
1583 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1584 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, with_refinements, dcp);
1585 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1590 callable_method_entry_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements)
1593 return callable_method_entry_refinements0(klass,
id, defined_class_ptr, with_refinements, cme);
1597 rb_callable_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1599 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
true);
1603 callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1605 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
false);
1609 rb_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1611 return method_entry_resolve_refinement(klass,
id, FALSE, defined_class_ptr);
1615 rb_callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1617 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1618 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, FALSE, dcp);
1619 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1625 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1630 refinement = find_refinement(refinements, me->owner);
1631 if (!
NIL_P(refinement)) {
1632 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
1634 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
1639 tmp_me = me->def->body.refined.orig_me;
1641 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
1650 me = search_method_protect(super, me->called_id, defined_class_ptr);
1658 return resolve_refined_method(refinements, me, NULL);
1664 VALUE defined_class = me->defined_class;
1667 if (resolved_me && resolved_me->defined_class == 0) {
1668 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1676 remove_method(
VALUE klass,
ID mid)
1683 klass = RCLASS_ORIGIN(klass);
1684 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
1688 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
1690 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
1691 UNDEFINED_REFINED_METHOD_P(me->def)) {
1692 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
1696 if (klass !=
self) {
1697 rb_clear_method_cache(
self, mid);
1699 rb_clear_method_cache(klass, mid);
1700 rb_id_table_delete(RCLASS_M_TBL(klass), mid);
1702 rb_vm_check_redefinition_opt_method(me, klass);
1704 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1705 rb_add_refined_method_entry(klass, mid);
1708 CALL_METHOD_HOOK(
self, removed, mid);
1714 remove_method(klass, mid);
1734 rb_mod_remove_method(
int argc,
VALUE *argv,
VALUE mod)
1738 for (i = 0; i < argc; i++) {
1742 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
1745 remove_method(mod,
id);
1751 rb_export_method(
VALUE klass,
ID name, rb_method_visibility_t visi)
1754 VALUE defined_class;
1755 VALUE origin_class = RCLASS_ORIGIN(klass);
1757 me = search_method0(origin_class, name, &defined_class,
true);
1760 me = search_method(rb_cObject, name, &defined_class);
1763 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1764 UNDEFINED_REFINED_METHOD_P(me->def)) {
1765 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
1768 if (METHOD_ENTRY_VISI(me) != visi) {
1769 rb_vm_check_redefinition_opt_method(me, klass);
1771 if (klass == defined_class || origin_class == defined_class) {
1772 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1775 if (me->def->body.refined.orig_me) {
1776 METHOD_ENTRY_VISI_SET((
rb_method_entry_t *)me->def->body.refined.orig_me, visi);
1780 METHOD_ENTRY_VISI_SET(me, visi);
1782 rb_clear_method_cache(klass, name);
1785 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
1790 #define BOUND_PRIVATE 0x01
1791 #define BOUND_RESPONDS 0x02
1794 method_boundp(
VALUE klass,
ID id,
int ex)
1800 if (ex & BOUND_RESPONDS) {
1801 cme = rb_callable_method_entry_with_refinements(klass,
id, NULL);
1804 cme = callable_method_entry_without_refinements(klass,
id, NULL);
1808 if (ex & ~BOUND_RESPONDS) {
1809 switch (METHOD_ENTRY_VISI(cme)) {
1810 case METHOD_VISI_PRIVATE:
1812 case METHOD_VISI_PROTECTED:
1813 if (ex & BOUND_RESPONDS)
return 0;
1819 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
1820 if (ex & BOUND_RESPONDS)
return 2;
1832 return method_boundp(klass,
id, ex);
1836 vm_cref_set_visibility(rb_method_visibility_t method_visi,
int module_func)
1839 scope_visi->method_visi = method_visi;
1840 scope_visi->module_func = module_func;
1844 rb_scope_visibility_set(rb_method_visibility_t visi)
1846 vm_cref_set_visibility(visi, FALSE);
1850 scope_visibility_check(
void)
1854 if (cfp && cfp->iseq && ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD) {
1855 rb_warn(
"calling %s without arguments inside a method may not have the intended effect",
1861 rb_scope_module_func_set(
void)
1863 scope_visibility_check();
1864 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
1872 rb_method_visibility_t visi;
1874 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
1877 visi = METHOD_VISI_PUBLIC;
1880 switch (vm_scope_visibility_get(ec)) {
1881 case METHOD_VISI_PRIVATE:
1882 if (vm_scope_module_func_check(ec)) {
1883 rb_warning(
"attribute accessor as module_function");
1885 visi = METHOD_VISI_PRIVATE;
1887 case METHOD_VISI_PROTECTED:
1888 visi = METHOD_VISI_PROTECTED;
1891 visi = METHOD_VISI_PUBLIC;
1898 rb_add_method(klass,
id, VM_METHOD_TYPE_IVAR, (
void *)attriv, visi);
1901 rb_add_method(klass,
rb_id_attrset(
id), VM_METHOD_TYPE_ATTRSET, (
void *)attriv, visi);
1914 if (
id == object_id ||
id == id__id__ ||
id == id__send__ ||
id == idInitialize) {
1918 me = search_method(klass,
id, 0);
1919 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1920 me = rb_resolve_refined_method(
Qnil, me);
1923 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1924 UNDEFINED_REFINED_METHOD_P(me->def)) {
1925 rb_method_name_error(klass,
rb_id2str(
id));
1928 rb_add_method(klass,
id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
1930 CALL_METHOD_HOOK(klass, undefined,
id);
1979 rb_mod_undef_method(
int argc,
VALUE *argv,
VALUE mod)
1982 for (i = 0; i < argc; i++) {
1986 rb_method_name_error(mod, v);
1993 static rb_method_visibility_t
1994 check_definition_visibility(
VALUE mod,
int argc,
VALUE *argv)
1997 VALUE mid, include_super, lookup_mod = mod;
2003 if (!
id)
return METHOD_VISI_UNDEF;
2009 inc_super =
RTEST(include_super);
2011 lookup_mod = RCLASS_ORIGIN(mod);
2015 me = rb_method_entry_without_refinements(lookup_mod,
id, NULL);
2017 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED)
return METHOD_VISI_UNDEF;
2018 if (!inc_super && me->owner != mod)
return METHOD_VISI_UNDEF;
2019 return METHOD_ENTRY_VISI(me);
2021 return METHOD_VISI_UNDEF;
2061 rb_mod_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2063 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2064 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2068 check_definition(
VALUE mod,
int argc,
VALUE *argv, rb_method_visibility_t visi)
2070 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2104 rb_mod_public_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2106 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2140 rb_mod_private_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2142 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2176 rb_mod_protected_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2178 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2184 return rb_method_definition_eq(m1->def, m2->def);
2192 switch (def->type) {
2193 case VM_METHOD_TYPE_REFINED:
2194 if (def->body.refined.orig_me) {
2195 def = def->body.refined.orig_me->def;
2199 case VM_METHOD_TYPE_ALIAS:
2200 def = def->body.alias.original_me->def;
2212 d1 = original_method_definition(d1);
2213 d2 = original_method_definition(d2);
2215 if (d1 == d2)
return 1;
2216 if (!d1 || !d2)
return 0;
2217 if (d1->type != d2->type)
return 0;
2220 case VM_METHOD_TYPE_ISEQ:
2221 return d1->body.iseq.iseqptr == d2->body.iseq.
iseqptr;
2222 case VM_METHOD_TYPE_CFUNC:
2224 d1->body.cfunc.func == d2->body.cfunc.func &&
2225 d1->body.cfunc.argc == d2->body.cfunc.argc;
2226 case VM_METHOD_TYPE_ATTRSET:
2227 case VM_METHOD_TYPE_IVAR:
2228 return d1->body.attr.id == d2->body.attr.id;
2229 case VM_METHOD_TYPE_BMETHOD:
2230 return RTEST(
rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2231 case VM_METHOD_TYPE_MISSING:
2232 return d1->original_id == d2->original_id;
2233 case VM_METHOD_TYPE_ZSUPER:
2234 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2235 case VM_METHOD_TYPE_UNDEF:
2237 case VM_METHOD_TYPE_OPTIMIZED:
2238 return (d1->body.optimized.type == d2->body.optimized.type) &&
2239 (d1->body.optimized.index == d2->body.optimized.index);
2240 case VM_METHOD_TYPE_REFINED:
2241 case VM_METHOD_TYPE_ALIAS:
2244 rb_bug(
"rb_method_definition_eq: unsupported type: %d", d1->type);
2251 def = original_method_definition(def);
2253 if (!def)
return hash;
2255 switch (def->type) {
2256 case VM_METHOD_TYPE_ISEQ:
2258 case VM_METHOD_TYPE_CFUNC:
2259 hash =
rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2261 case VM_METHOD_TYPE_ATTRSET:
2262 case VM_METHOD_TYPE_IVAR:
2264 case VM_METHOD_TYPE_BMETHOD:
2265 return rb_hash_proc(hash, def->body.bmethod.proc);
2266 case VM_METHOD_TYPE_MISSING:
2268 case VM_METHOD_TYPE_ZSUPER:
2269 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2270 case VM_METHOD_TYPE_UNDEF:
2272 case VM_METHOD_TYPE_OPTIMIZED:
2275 case VM_METHOD_TYPE_REFINED:
2276 case VM_METHOD_TYPE_ALIAS:
2279 rb_bug(
"rb_hash_method_definition: unsupported method type (%d)", def->type);
2285 return rb_hash_method_definition(hash, me->def);
2291 const VALUE target_klass = klass;
2292 VALUE defined_class;
2294 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2303 orig_me = search_method(klass, original_name, &defined_class);
2305 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2306 orig_me = rb_resolve_refined_method(
Qnil, orig_me);
2309 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2310 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2312 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2313 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2314 rb_print_undef(klass, original_name, METHOD_VISI_UNDEF);
2318 switch (orig_me->def->type) {
2319 case VM_METHOD_TYPE_ZSUPER:
2321 original_name = orig_me->def->original_id;
2322 visi = METHOD_ENTRY_VISI(orig_me);
2324 case VM_METHOD_TYPE_ALIAS:
2325 visi = METHOD_ENTRY_VISI(orig_me);
2326 orig_me = orig_me->def->body.alias.original_me;
2327 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2332 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2334 if (orig_me->defined_class == 0) {
2335 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2336 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2337 (
void *)rb_method_entry_clone(orig_me));
2338 method_added(target_klass, alias_name);
2343 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2344 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2350 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2382 rb_print_undef_str(mod, oldname);
2390 check_and_export_method(
VALUE self,
VALUE name, rb_method_visibility_t visi)
2394 rb_print_undef_str(
self, name);
2396 rb_export_method(
self,
id, visi);
2400 set_method_visibility(
VALUE self,
int argc,
const VALUE *argv, rb_method_visibility_t visi)
2404 rb_check_frozen(
self);
2406 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
2418 check_and_export_method(
self,
RARRAY_AREF(v, j), visi);
2422 for (i = 0; i < argc; i++) {
2423 check_and_export_method(
self, argv[i], visi);
2429 set_visibility(
int argc,
const VALUE *argv,
VALUE module, rb_method_visibility_t visi)
2432 scope_visibility_check();
2433 rb_scope_visibility_set(visi);
2437 set_method_visibility(module, argc, argv, visi);
2462 rb_mod_public(
int argc,
VALUE *argv,
VALUE module)
2464 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2494 rb_mod_protected(
int argc,
VALUE *argv,
VALUE module)
2496 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2528 rb_mod_private(
int argc,
VALUE *argv,
VALUE module)
2530 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
2570 rb_mod_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2573 VALUE origin_class = RCLASS_ORIGIN(module);
2576 rb_check_frozen(module);
2578 for (i = 0; i < argc; i++) {
2582 VALUE defined_class;
2585 rb_print_undef_str(module, v);
2588 me = search_method(origin_class, name, &defined_class);
2590 me = search_method(rb_cObject, name, &defined_class);
2593 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2594 UNDEFINED_REFINED_METHOD_P(me->def)) {
2595 rb_print_undef(module, name, METHOD_VISI_UNDEF);
2598 if (module == defined_class || origin_class == defined_class) {
2599 switch (me->def->type) {
2600 case VM_METHOD_TYPE_ISEQ:
2601 if (ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_rest &&
2602 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kw &&
2603 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kwrest) {
2604 ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.ruby2_keywords = 1;
2605 rb_clear_method_cache(module, name);
2608 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)",
rb_id2name(name));
2611 case VM_METHOD_TYPE_BMETHOD: {
2612 VALUE procval = me->def->body.bmethod.proc;
2613 if (vm_block_handler_type(procval) == block_handler_type_proc) {
2614 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
2617 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
2619 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
2620 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
2621 !ISEQ_BODY(iseq)->param.flags.has_kw &&
2622 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
2623 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
2624 rb_clear_method_cache(module, name);
2627 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)",
rb_id2name(name));
2634 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method not defined in Ruby)",
rb_id2name(name));
2639 rb_warn(
"Skipping set of ruby2_keywords flag for %s (can only set in method defining module)",
rb_id2name(name));
2658 rb_mod_public_method(
int argc,
VALUE *argv,
VALUE obj)
2686 rb_mod_private_method(
int argc,
VALUE *argv,
VALUE obj)
2710 return rb_mod_public(argc, argv, rb_top_main_class(
"public"));
2730 return rb_mod_private(argc, argv, rb_top_main_class(
"private"));
2741 top_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2743 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class(
"ruby2_keywords"));
2789 rb_mod_modfunc(
int argc,
VALUE *argv,
VALUE module)
2800 rb_scope_module_func_set();
2804 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
2806 for (i = 0; i < argc; i++) {
2811 me = search_method(m,
id, 0);
2813 me = search_method(rb_cObject,
id, 0);
2815 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2816 rb_print_undef(module,
id, METHOD_VISI_UNDEF);
2818 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
2834 #pragma push_macro("rb_method_basic_definition_p")
2835 #undef rb_method_basic_definition_p
2841 if (!klass)
return TRUE;
2842 cme = rb_callable_method_entry(klass,
id);
2843 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
2846 #pragma pop_macro("rb_method_basic_definition_p")
2853 VALUE passed_block_handler = vm_passed_block_handler(ec);
2854 VALUE result = rb_vm_call_kw(ec, obj,
id, argc, argv, cme, kw_splat);
2855 vm_passed_block_handler_set(ec, passed_block_handler);
2863 VALUE defined_class, args[2];
2864 const ID rtmid = idRespond_to_missing;
2867 if (!cme || METHOD_ENTRY_BASIC(cme))
return Qundef;
2870 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args,
RB_NO_KEYWORDS);
2879 switch (method_boundp(klass,
id, pub|BOUND_RESPONDS)) {
2883 ret = basic_obj_respond_to_missing(ec, klass, obj,
ID2SYM(
id),
2885 return RTEST(ret) && !UNDEF_P(ret);
2894 VALUE defined_class;
2895 const ID resid = idRespond_to;
2898 if (!cme)
return -1;
2899 if (METHOD_ENTRY_BASIC(cme)) {
2913 "respond_to? must accept 1 or 2 arguments (requires %d)",
2922 "%"PRIsVALUE
"%c""respond_to?(:%"PRIsVALUE
") uses"
2923 " the deprecated method signature, which takes one parameter",
2924 (RCLASS_SINGLETON_P(klass) ? obj : klass),
2925 (RCLASS_SINGLETON_P(klass) ?
'.' :
'#'),
2927 if (!
NIL_P(location)) {
2933 "respond_to? is defined here");
2938 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args,
RB_NO_KEYWORDS);
2939 return RTEST(result);
2947 return rb_ec_obj_respond_to(ec, obj,
id, priv);
2954 int ret = vm_respond_to(ec, klass, obj,
id, priv);
2955 if (ret == -1) ret = basic_obj_respond_to(ec, obj,
id, !priv);
2987 obj_respond_to(
int argc,
VALUE *argv,
VALUE obj)
2995 VALUE ret = basic_obj_respond_to_missing(ec,
CLASS_OF(obj), obj,
2997 if (UNDEF_P(ret)) ret =
Qfalse;
3000 return RBOOL(basic_obj_respond_to(ec, obj,
id, !
RTEST(priv)));
3025 Init_eval_method(
void)
3047 "public", top_public, -1);
3049 "private", top_private, -1);
3051 "ruby2_keywords", top_ruby2_keywords, -1);
3054 #define REPLICATE_METHOD(klass, id) do { \
3055 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3056 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
void rb_notimplement(void)
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_raise(VALUE exc_class, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt,...)
Identical to rb_compile_warn(), except it also accepts category.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_eException
Mother of all exceptions.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_mKernel
Kernel module.
VALUE rb_cModule
Module class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
ID rb_frame_callee(void)
Identical to rb_frame_this_func(), except it returns the named used to call the method.
ID rb_frame_this_func(void)
Queries the name of the Ruby level method that is calling this function.
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well...
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
void rb_remove_method(VALUE klass, const char *name)
Removes a method.
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
void rb_remove_method_id(VALUE klass, ID mid)
Identical to rb_remove_method(), except it accepts the method name as ID.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
Raises rb_eNotImpError.
int rb_method_boundp(VALUE klass, ID id, int ex)
Queries if the klass has this method.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.