10static int vm_redefinition_check_flag(
VALUE klass);
14#define object_id idObject_id
15#define added idMethod_added
16#define singleton_added idSingleton_method_added
17#define removed idMethod_removed
18#define singleton_removed idSingleton_method_removed
19#define undefined idMethod_undefined
20#define singleton_undefined idSingleton_method_undefined
22#define ruby_running (GET_VM()->running)
25static enum rb_id_table_iterator_result
26vm_ccs_dump_i(
ID mid,
VALUE val,
void *data)
29 fprintf(stderr,
" | %s (len:%d) ", rb_id2name(mid), ccs->len);
32 for (
int i=0; i<ccs->len; i++) {
33 rp_m(
" | \t", ccs->entries[i].cc);
36 return ID_TABLE_CONTINUE;
40vm_ccs_dump(
VALUE klass,
ID target_mid)
42 struct rb_id_table *cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
46 if (rb_id_table_lookup(cc_tbl, target_mid, &ccs)) {
47 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
48 vm_ccs_dump_i(target_mid, ccs, NULL);
52 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
53 rb_id_table_foreach(cc_tbl, vm_ccs_dump_i, (
void *)target_mid);
58static enum rb_id_table_iterator_result
59vm_cme_dump_i(
ID mid,
VALUE val,
void *data)
61 ID target_mid = (
ID)data;
62 if (target_mid == 0 || mid == target_mid) {
65 return ID_TABLE_CONTINUE;
69vm_mtbl_dump(
VALUE klass,
ID target_mid)
71 fprintf(stderr,
"# vm_mtbl\n");
76 if (RCLASS_M_TBL(klass)) {
77 if (target_mid != 0) {
78 if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
83 fprintf(stderr,
" ## RCLASS_M_TBL (%p)\n", (
void *)RCLASS_M_TBL(klass));
84 rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
88 fprintf(stderr,
" MTBL: NULL\n");
90 if (RCLASS_WRITABLE_CALLABLE_M_TBL(klass)) {
91 if (target_mid != 0) {
92 if (rb_id_table_lookup(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), target_mid, &me)) {
97 fprintf(stderr,
" ## RCLASS_CALLABLE_M_TBL\n");
98 rb_id_table_foreach(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
101 if (RCLASS_WRITABLE_CC_TBL(klass)) {
102 vm_ccs_dump(klass, target_mid);
110rb_vm_mtbl_dump(
const char *msg,
VALUE klass,
ID target_mid)
112 fprintf(stderr,
"[%s] ", msg);
113 vm_mtbl_dump(klass, target_mid);
119 VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment),
"cme: %d", imemo_type((
VALUE)cme));
120 VM_ASSERT(callable_method_entry_p(cme));
121 METHOD_ENTRY_INVALIDATED_SET(cme);
122 RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
124 rb_yjit_cme_invalidate(cme);
128rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t arg)
130 ((
IC) ic)->entry = NULL;
135void rb_clear_constant_cache(
void) {}
143 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
145 set_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
146 ruby_vm_constant_cache_invalidations += ics->num_entries;
149 rb_yjit_constant_state_changed(
id);
153invalidate_negative_cache(
ID mid)
158 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
159 rb_id_table_delete(vm->negative_cme_table, mid);
161 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
170invalidate_method_cache_in_cc_table(
struct rb_id_table *tbl,
ID mid)
173 if (tbl && rb_id_table_lookup(tbl, mid, &ccs_data)) {
176 if (
NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
178 rb_id_table_delete(tbl, mid);
179 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
184invalidate_callable_method_entry_in_callable_m_table(
struct rb_id_table *tbl,
ID mid)
187 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
188 if (rb_yjit_enabled_p) {
191 rb_id_table_delete(tbl, mid);
192 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
204invalidate_callable_method_entry_in_every_m_table_i(
rb_classext_t *ext,
bool is_prime,
VALUE namespace,
void *data)
210 if (rb_id_table_lookup(tbl, arg->mid, &me) && arg->cme == (
const rb_method_entry_t *)me) {
211 rb_method_table_insert(arg->klass, tbl, arg->mid, arg->newer);
226 rb_class_classext_foreach(klass, invalidate_callable_method_entry_in_every_m_table_i, (
void *)&arg);
230invalidate_complemented_method_entry_in_callable_m_table(
struct rb_id_table *tbl,
ID mid)
233 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
234 if (rb_yjit_enabled_p) {
237 rb_id_table_delete(tbl, mid);
238 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
243clear_method_cache_by_id_in_class(
VALUE klass,
ID mid)
246 if (rb_objspace_garbage_object_p(klass))
return;
248 RB_VM_LOCKING() {
if (LIKELY(RCLASS_SUBCLASSES_FIRST(klass) == NULL)) {
253 struct rb_id_table *cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
254 invalidate_method_cache_in_cc_table(cc_tbl, mid);
255 if (RCLASS_CC_TBL_NOT_PRIME_P(klass, cc_tbl)) {
256 invalidate_method_cache_in_cc_table(RCLASS_PRIME_CC_TBL(klass), mid);
260 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(klass);
261 invalidate_callable_method_entry_in_callable_m_table(cm_tbl, mid);
262 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(klass, cm_tbl)) {
263 invalidate_callable_method_entry_in_callable_m_table(RCLASS_PRIME_CALLABLE_M_TBL(klass), mid);
266 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
273 if (METHOD_ENTRY_CACHED(cme)) {
274 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
279 VALUE owner = cme->owner;
280 VM_ASSERT_TYPE(owner,
T_CLASS);
281 VALUE klass_housing_cme;
282 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
283 klass_housing_cme = owner;
286 klass_housing_cme = RCLASS_ORIGIN(owner);
290 invalidate_callable_method_entry_in_every_m_table(klass_housing_cme, mid, cme);
294 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
299 if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
303 if (cme->def->iseq_overload) {
306 vm_cme_invalidate(monly_cme);
312 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
313 VALUE defined_class = cme->defined_class;
314 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
315 invalidate_complemented_method_entry_in_callable_m_table(cm_tbl, mid);
316 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(defined_class, cm_tbl)) {
317 struct rb_id_table *prime_cm_table = RCLASS_PRIME_CALLABLE_M_TBL(defined_class);
318 invalidate_complemented_method_entry_in_callable_m_table(prime_cm_table, mid);
322 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
325 invalidate_negative_cache(mid);
329 rb_gccct_clear_table(
Qnil);
334clear_iclass_method_cache_by_id(
VALUE iclass,
VALUE d)
338 clear_method_cache_by_id_in_class(iclass, mid);
342clear_iclass_method_cache_by_id_for_refinements(
VALUE klass,
VALUE d)
346 clear_method_cache_by_id_in_class(klass, mid);
351rb_clear_method_cache(
VALUE klass_or_module,
ID mid)
354 VALUE module = klass_or_module;
356 if (
FL_TEST(module, RMODULE_IS_REFINEMENT)) {
357 VALUE refined_class = rb_refinement_module_get_refined_class(module);
358 rb_clear_method_cache(refined_class, mid);
359 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
360 rb_clear_all_refinement_method_cache();
362 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
365 clear_method_cache_by_id_in_class(klass_or_module, mid);
369static enum rb_id_table_iterator_result
370invalidate_method_entry_in_iclass_callable_m_tbl(
VALUE cme,
void *data)
373 return ID_TABLE_DELETE;
376static enum rb_id_table_iterator_result
377invalidate_ccs_in_iclass_cc_tbl(
VALUE value,
void *data)
381 return ID_TABLE_DELETE;
388 rb_id_table_foreach_values(cm_tbl, invalidate_method_entry_in_iclass_callable_m_tbl, NULL);
391 rb_id_table_foreach_values(cc_tbl, invalidate_ccs_in_iclass_cc_tbl, NULL);
396invalidate_cc_refinement(st_data_t key, st_data_t data)
399 void *ptr = rb_asan_poisoned_object_p(v);
400 rb_asan_unpoison_object(v,
false);
402 if (rb_gc_pointer_to_heap_p(v) &&
403 !rb_objspace_garbage_object_p(v) &&
407 VM_ASSERT(vm_cc_refinement_p(cc));
410 vm_cc_invalidate(cc);
415 rb_asan_poison_object(v);
430 for (
int i = 0; i < ci->kwarg->keyword_len; i++) {
442 if (ci1->mid != ci2->mid)
return 1;
443 if (ci1->flag != ci2->flag)
return 1;
444 if (ci1->argc != ci2->argc)
return 1;
445 if (ci1->kwarg != NULL) {
446 VM_ASSERT(ci2->kwarg != NULL);
448 if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
451 for (
int i = 0; i < ci1->kwarg->keyword_len; i++) {
452 if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
458 VM_ASSERT(ci2->kwarg == NULL);
469ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
472 st_data_t *ret = (st_data_t *)data;
475 if (rb_objspace_garbage_object_p((
VALUE)ci)) {
476 *ret = (st_data_t)NULL;
485 *key = *value = *ret = (st_data_t)ci;
491rb_vm_ci_lookup(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg)
510 st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
511 }
while (ci == NULL);
526 st_data_t key = (st_data_t)ci;
527 st_delete(vm->ci_table, &key, NULL);
531rb_vm_insert_cc_refinement(
const struct rb_callcache *cc)
533 st_data_t key = (st_data_t)cc;
538 rb_set_insert(vm->cc_refinement_table, key);
544rb_vm_delete_cc_refinement(
const struct rb_callcache *cc)
549 st_data_t key = (st_data_t)cc;
551 rb_set_delete(vm->cc_refinement_table, &key);
555rb_clear_all_refinement_method_cache(
void)
561 rb_set_foreach(vm->cc_refinement_table, invalidate_cc_refinement, (st_data_t)NULL);
562 rb_set_clear(vm->cc_refinement_table);
563 rb_set_compact_table(vm->cc_refinement_table);
567 rb_yjit_invalidate_all_method_lookup_assumptions();
573 rb_method_table_insert0(klass, table, method_id, me,
RB_TYPE_P(klass,
T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass));
579 VALUE table_owner = klass;
580 if (iclass_shared_mtbl) {
581 table_owner =
RBASIC(table_owner)->klass;
584 rb_id_table_insert(table, method_id, (
VALUE)me);
593NORETURN(
static VALUE rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj));
596rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj)
606 rb_f_notimplement_internal(argc, argv, obj);
610rb_define_notimplement_method_id(
VALUE mod,
ID id, rb_method_visibility_t visi)
612 rb_add_method(mod,
id, VM_METHOD_TYPE_NOTIMPLEMENTED, (
void *)1, visi);
616rb_add_method_cfunc(
VALUE klass,
ID mid,
VALUE (*func)(
ANYARGS),
int argc, rb_method_visibility_t visi)
618 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError,
"arity out of range: %d for -2..15", argc);
623 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
626 rb_define_notimplement_method_id(klass, mid, visi);
631rb_add_method_optimized(
VALUE klass,
ID mid,
enum method_optimized_type opt_type,
unsigned int index, rb_method_visibility_t visi)
637 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
648 if (reference_count_was == 1) {
649 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:1->0 (remove)\n", (
void *)def,
650 rb_id2name(def->original_id));
651 if (def->type == VM_METHOD_TYPE_BMETHOD && def->body.bmethod.hooks) {
652 xfree(def->body.bmethod.hooks);
657 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d->%d (dec)\n", (
void *)def, rb_id2name(def->original_id),
658 reference_count_was, reference_count_was - 1);
668 if (me->def && me->def->iseq_overload) {
676 rb_method_definition_release(me->def);
685 if (!GET_THREAD()->ext_config.ractor_safe) {
687 case -2:
return &call_cfunc_m2;
688 case -1:
return &call_cfunc_m1;
689 case 0:
return &call_cfunc_0;
690 case 1:
return &call_cfunc_1;
691 case 2:
return &call_cfunc_2;
692 case 3:
return &call_cfunc_3;
693 case 4:
return &call_cfunc_4;
694 case 5:
return &call_cfunc_5;
695 case 6:
return &call_cfunc_6;
696 case 7:
return &call_cfunc_7;
697 case 8:
return &call_cfunc_8;
698 case 9:
return &call_cfunc_9;
699 case 10:
return &call_cfunc_10;
700 case 11:
return &call_cfunc_11;
701 case 12:
return &call_cfunc_12;
702 case 13:
return &call_cfunc_13;
703 case 14:
return &call_cfunc_14;
704 case 15:
return &call_cfunc_15;
706 rb_bug(
"unsupported length: %d", argc);
711 case -2:
return &ractor_safe_call_cfunc_m2;
712 case -1:
return &ractor_safe_call_cfunc_m1;
713 case 0:
return &ractor_safe_call_cfunc_0;
714 case 1:
return &ractor_safe_call_cfunc_1;
715 case 2:
return &ractor_safe_call_cfunc_2;
716 case 3:
return &ractor_safe_call_cfunc_3;
717 case 4:
return &ractor_safe_call_cfunc_4;
718 case 5:
return &ractor_safe_call_cfunc_5;
719 case 6:
return &ractor_safe_call_cfunc_6;
720 case 7:
return &ractor_safe_call_cfunc_7;
721 case 8:
return &ractor_safe_call_cfunc_8;
722 case 9:
return &ractor_safe_call_cfunc_9;
723 case 10:
return &ractor_safe_call_cfunc_10;
724 case 11:
return &ractor_safe_call_cfunc_11;
725 case 12:
return &ractor_safe_call_cfunc_12;
726 case 13:
return &ractor_safe_call_cfunc_13;
727 case 14:
return &ractor_safe_call_cfunc_14;
728 case 15:
return &ractor_safe_call_cfunc_15;
730 rb_bug(
"unsupported length: %d", argc);
740 cfunc->invoker = call_cfunc_invoker_func(argc);
747 if (!complemented && reference_count_was > 0) {
751 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d->%d\n", (
void *)def, rb_id2name(def->original_id), reference_count_was, reference_count_was+1);
759 rb_method_definition_release(me->def);
762 if (!ruby_running) add_opt_method_entry(me);
766 case VM_METHOD_TYPE_ISEQ:
776 if (rb_iseq_attr_p(iseq, BUILTIN_ATTR_C_TRACE)) {
780 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
782 if (0) vm_cref_dump(
"rb_method_definition_create", cref);
788 method_cref = vm_cref_new_toplevel(GET_EC());
794 case VM_METHOD_TYPE_CFUNC:
797 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
800 case VM_METHOD_TYPE_ATTRSET:
801 case VM_METHOD_TYPE_IVAR:
807 def->body.attr.id = (
ID)(
VALUE)opts;
809 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
811 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
816 VM_ASSERT(def->body.attr.location == 0);
820 case VM_METHOD_TYPE_BMETHOD:
822 RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
824 case VM_METHOD_TYPE_NOTIMPLEMENTED:
825 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (
VALUE(*)(
ANYARGS))rb_f_notimplement_internal, -1);
827 case VM_METHOD_TYPE_OPTIMIZED:
830 case VM_METHOD_TYPE_REFINED:
835 case VM_METHOD_TYPE_ALIAS:
838 case VM_METHOD_TYPE_ZSUPER:
839 case VM_METHOD_TYPE_UNDEF:
840 case VM_METHOD_TYPE_MISSING:
852 case VM_METHOD_TYPE_ISEQ:
856 case VM_METHOD_TYPE_ATTRSET:
857 case VM_METHOD_TYPE_IVAR:
860 case VM_METHOD_TYPE_BMETHOD:
864 if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((
VALUE)me);
866 case VM_METHOD_TYPE_REFINED:
869 case VM_METHOD_TYPE_ALIAS:
872 case VM_METHOD_TYPE_CFUNC:
873 case VM_METHOD_TYPE_ZSUPER:
874 case VM_METHOD_TYPE_MISSING:
875 case VM_METHOD_TYPE_OPTIMIZED:
876 case VM_METHOD_TYPE_UNDEF:
877 case VM_METHOD_TYPE_NOTIMPLEMENTED:
885rb_method_definition_create(rb_method_type_t
type,
ID mid)
890 def->original_id = mid;
892 def->ns = rb_current_namespace();
899 if (def) method_definition_addref(def, complement);
900 if (
RTEST(defined_class)) {
906 me->called_id = called_id;
913filter_defined_class(
VALUE klass)
925 rb_bug(
"filter_defined_class: %s", rb_obj_info(klass));
931 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def,
false);
932 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
933 if (def != NULL) method_definition_reset(me);
941 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));
943 METHOD_ENTRY_FLAGS_COPY(me, src_me);
947 src_me->def->type == VM_METHOD_TYPE_REFINED &&
948 src_me->def->body.refined.orig_me) {
950 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
953 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
954 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
959 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
960 rb_method_definition_set(me, clone_def, orig_clone);
972 if (!src_me->defined_class &&
973 def->type == VM_METHOD_TYPE_REFINED &&
974 def->body.refined.orig_me) {
976 rb_method_entry_clone(def->body.refined.orig_me);
978 refined_orig_me = orig_me;
982 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def,
true);
983 METHOD_ENTRY_FLAGS_COPY(me, src_me);
984 METHOD_ENTRY_COMPLEMENTED_SET(me);
986 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
987 rb_method_definition_set(me, def, (
void *)refined_orig_me);
990 VM_ASSERT_TYPE(me->owner,
T_MODULE);
998 rb_method_definition_release(dst->def);
999 *(
rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
1000 method_definition_reset(dst);
1001 dst->called_id = src->called_id;
1004 METHOD_ENTRY_FLAGS_COPY(dst, src);
1010 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1016 rb_vm_check_redefinition_opt_method(me, me->owner);
1019 rb_method_entry_alloc(me->called_id,
1024 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
1026 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
1027 rb_method_definition_set(me, def, orig_me);
1028 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
1033lookup_method_table(
VALUE klass,
ID id)
1038 if (rb_id_table_lookup(m_tbl,
id, &body)) {
1047rb_add_refined_method_entry(
VALUE refined_class,
ID mid)
1052 make_method_entry_refined(refined_class, me);
1053 rb_clear_method_cache(refined_class, mid);
1056 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
1061check_override_opt_method_i(
VALUE klass,
VALUE arg)
1066 if (vm_redefinition_check_flag(klass)) {
1067 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1069 newme = rb_method_entry(klass, mid);
1070 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1073 rb_class_foreach_subclass(klass, check_override_opt_method_i, (
VALUE)mid);
1077check_override_opt_method(
VALUE klass,
VALUE mid)
1079 if (rb_vm_check_optimizable_mid(mid)) {
1080 check_override_opt_method_i(klass, mid);
1091rb_method_entry_make(
VALUE klass,
ID mid,
VALUE defined_class, rb_method_visibility_t visi,
1097 int make_refined = 0;
1105 if (!RCLASS_SINGLETON_P(klass) &&
1106 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
1107 type != VM_METHOD_TYPE_ZSUPER) {
1110 case idInitialize_copy:
1111 case idInitialize_clone:
1112 case idInitialize_dup:
1113 case idRespond_to_missing:
1114 visi = METHOD_VISI_PRIVATE;
1118 if (
type != VM_METHOD_TYPE_REFINED) {
1123 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
1124 rb_add_refined_method_entry(refined_class, mid);
1126 if (
type == VM_METHOD_TYPE_REFINED) {
1128 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1131 klass = RCLASS_ORIGIN(klass);
1132 if (klass != orig_klass) {
1133 rb_clear_method_cache(orig_klass, mid);
1136 mtbl = RCLASS_WRITABLE_M_TBL(klass);
1139 if (rb_id_table_lookup(mtbl, mid, &data)) {
1143 if (rb_method_definition_eq(old_def, def))
return old_me;
1144 rb_vm_check_redefinition_opt_method(old_me, klass);
1146 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1149 type != VM_METHOD_TYPE_UNDEF &&
1150 (old_def->aliased ==
false) &&
1151 (!old_def->no_redef_warning) &&
1153 old_def->type != VM_METHOD_TYPE_UNDEF &&
1154 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1155 old_def->type != VM_METHOD_TYPE_ALIAS) {
1158 switch (old_def->type) {
1159 case VM_METHOD_TYPE_ISEQ:
1160 iseq = def_iseq_ptr(old_def);
1162 case VM_METHOD_TYPE_BMETHOD:
1163 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1170 "method redefined; discarding old %"PRIsVALUE
"\n%s:%d: warning: previous definition of %"PRIsVALUE
" was here",
1172 RSTRING_PTR(rb_iseq_path(iseq)),
1173 ISEQ_BODY(iseq)->location.first_lineno,
1174 rb_id2str(old_def->original_id)
1178 rb_warning(
"method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1184 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1186 def = rb_method_definition_create(
type, original_id);
1188 rb_method_definition_set(me, def, opts);
1190 rb_clear_method_cache(klass, mid);
1193 if (klass == rb_cObject) {
1196 case idRespond_to_missing:
1197 case idMethodMissing:
1199 rb_warn(
"redefining Object#%s may cause infinite loop", rb_id2name(mid));
1203 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1204 if (
type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1205 rb_warn(
"redefining '%s' may cause serious problems", rb_id2name(mid));
1210 make_method_entry_refined(klass, me);
1213 rb_method_table_insert(klass, mtbl, mid, me);
1215 VM_ASSERT(me->def != NULL);
1219 check_override_opt_method(klass, (
VALUE)mid);
1226overloaded_cme_table(
void)
1228 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
1229 return GET_VM()->overloaded_cme_table;
1232#if VM_CHECK_MODE > 0
1234vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1236 fprintf(stderr,
"key: "); rp(key);
1237 fprintf(stderr,
"val: "); rp(val);
1242rb_vm_dump_overloaded_cme_table(
void)
1244 fprintf(stderr,
"== rb_vm_dump_overloaded_cme_table\n");
1245 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1250lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
1257 if (rb_objspace_garbage_object_p((
VALUE)cme) ||
1258 rb_objspace_garbage_object_p((
VALUE)monly_cme)) {
1273 ASSERT_vm_locking();
1276 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1280#if VM_CHECK_MODE > 0
1284 return lookup_overloaded_cme(cme);
1291 st_data_t cme_data = (st_data_t)cme;
1292 ASSERT_vm_locking();
1293 st_delete(overloaded_cme_table(), &cme_data, NULL);
1301 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1316 ASSERT_vm_locking();
1317 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1319 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1327 if (UNLIKELY(cme->def->iseq_overload) &&
1328 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1329 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1330 (
int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1331 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ,
"type: %d", cme->def->type);
1333 cme = get_overloaded_cme(cme);
1335 VM_ASSERT(cme != NULL);
1342#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1343 const VALUE arg = ID2SYM(mid); \
1344 VALUE recv_class = (klass); \
1345 ID hook_id = (hook); \
1346 if (RCLASS_SINGLETON_P((klass))) { \
1347 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1348 hook_id = singleton_##hook; \
1350 rb_funcallv(recv_class, hook_id, 1, &arg); \
1354method_added(
VALUE klass,
ID mid)
1357 CALL_METHOD_HOOK(klass, added, mid);
1362rb_add_method(
VALUE klass,
ID mid, rb_method_type_t
type,
void *opts, rb_method_visibility_t visi)
1364 rb_method_entry_make(klass, mid, klass, visi,
type, NULL, mid, opts);
1366 if (
type != VM_METHOD_TYPE_UNDEF &&
type != VM_METHOD_TYPE_REFINED) {
1367 method_added(klass, mid);
1380 iseq_body.
cref = cref;
1382 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1387 rb_method_visibility_t visi,
VALUE defined_class)
1389 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
1390 me->def->type, me->def, 0, NULL);
1392 me->def->no_redef_warning = TRUE;
1393 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1396 method_added(klass, mid);
1403 return method_entry_set(klass, mid, me, visi, klass);
1406#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1412 if (RCLASS_SINGLETON_P(klass)) {
1413 rb_raise(
rb_eTypeError,
"can't define an allocator for a singleton class");
1415 RCLASS_SET_ALLOCATOR(klass, func);
1431 if (allocator == UNDEF_ALLOC_FUNC)
break;
1432 if (allocator)
return allocator;
1438rb_method_entry_at(
VALUE klass,
ID id)
1440 return lookup_method_table(klass,
id);
1444search_method0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool skip_refined)
1448 RB_DEBUG_COUNTER_INC(mc_search);
1451 RB_DEBUG_COUNTER_INC(mc_search_super);
1452 if ((me = lookup_method_table(klass,
id)) != 0) {
1453 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1454 me->def->body.refined.orig_me) {
1460 if (defined_class_ptr) *defined_class_ptr = klass;
1462 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1464 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me),
1465 "invalid me, mid:%s, klass:%s(%s)",
1468 rb_obj_info(klass));
1473search_method(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1475 return search_method0(klass,
id, defined_class_ptr,
false);
1479search_method_protect(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1483 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1492rb_method_entry(
VALUE klass,
ID id)
1494 return search_method_protect(klass,
id, NULL);
1506 if (me->defined_class == 0) {
1507 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1510 mtbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
1511 if (mtbl && rb_id_table_lookup(mtbl,
id, &cme_data)) {
1516 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1517 VM_ASSERT(callable_method_entry_p(cme));
1518 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1522 mtbl = rb_id_table_create(0);
1523 RCLASS_WRITE_CALLABLE_M_TBL(defined_class, mtbl);
1525 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1526 rb_id_table_insert(mtbl,
id, (
VALUE)cme);
1528 VM_ASSERT(callable_method_entry_p(cme));
1536 VM_ASSERT(callable_method_entry_p(cme));
1537 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1547complemented_callable_method_entry(
VALUE klass,
ID id)
1549 VALUE defined_class;
1551 return prepare_callable_method_entry(defined_class,
id, me, FALSE);
1555cached_callable_method_entry(
VALUE klass,
ID mid)
1557 ASSERT_vm_locking();
1559 struct rb_id_table *cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1562 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1564 VM_ASSERT(vm_ccs_p(ccs));
1566 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1567 VM_ASSERT(ccs->cme->called_id == mid);
1568 RB_DEBUG_COUNTER_INC(ccs_found);
1572 rb_vm_ccs_free(ccs);
1573 rb_id_table_delete(cc_tbl, mid);
1577 RB_DEBUG_COUNTER_INC(ccs_not_found);
1584 ASSERT_vm_locking();
1585 VM_ASSERT(cme != NULL);
1587 struct rb_id_table *cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1591 cc_tbl = rb_id_table_create(2);
1592 RCLASS_WRITE_CC_TBL(klass, cc_tbl);
1595 if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1596#if VM_CHECK_MODE > 0
1598 VM_ASSERT(ccs->cme == cme);
1602 vm_ccs_create(klass, cc_tbl, mid, cme);
1613 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1618 rb_id_table_insert(vm->negative_cme_table, mid, (
VALUE)cme);
1621 VM_ASSERT(cme != NULL);
1626callable_method_entry_or_negative(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1632 cme = cached_callable_method_entry(klass, mid);
1635 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1638 VALUE defined_class;
1640 if (defined_class_ptr) *defined_class_ptr = defined_class;
1643 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1646 cme = negative_cme(mid);
1649 cache_callable_method_entry(klass, mid, cme);
1659rb_callable_method_entry_or_negative(
VALUE klass,
ID mid)
1661 return callable_method_entry_or_negative(klass, mid, NULL);
1665callable_method_entry(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1668 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
1669 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1673rb_callable_method_entry(
VALUE klass,
ID mid)
1675 return callable_method_entry(klass, mid, NULL);
1681method_entry_resolve_refinement(
VALUE klass,
ID id,
int with_refinement,
VALUE *defined_class_ptr)
1683 const rb_method_entry_t *me = search_method_protect(klass,
id, defined_class_ptr);
1686 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1687 if (with_refinement) {
1689 VALUE refinements = cref ? CREF_REFINEMENTS(cref) :
Qnil;
1690 me = resolve_refined_method(refinements, me, defined_class_ptr);
1693 me = resolve_refined_method(
Qnil, me, defined_class_ptr);
1696 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1704rb_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1706 return method_entry_resolve_refinement(klass,
id, TRUE, defined_class_ptr);
1710callable_method_entry_refinements0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements,
1713 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
1717 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1718 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, with_refinements, dcp);
1719 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1724callable_method_entry_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements)
1727 return callable_method_entry_refinements0(klass,
id, defined_class_ptr, with_refinements, cme);
1731rb_callable_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1733 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
true);
1737callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1739 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
false);
1743rb_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1745 return method_entry_resolve_refinement(klass,
id, FALSE, defined_class_ptr);
1749rb_callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1751 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1752 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, FALSE, dcp);
1753 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1759 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1764 refinement = find_refinement(refinements, me->owner);
1765 if (!
NIL_P(refinement)) {
1766 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
1768 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
1773 tmp_me = me->def->body.refined.orig_me;
1775 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
1784 me = search_method_protect(super, me->called_id, defined_class_ptr);
1792 return resolve_refined_method(refinements, me, NULL);
1798 VALUE defined_class = me->defined_class;
1801 if (resolved_me && resolved_me->defined_class == 0) {
1802 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1810remove_method(
VALUE klass,
ID mid)
1817 klass = RCLASS_ORIGIN(klass);
1818 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
1819 rb_warn(
"removing '%s' may cause serious problems", rb_id2name(mid));
1822 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
1824 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
1825 UNDEFINED_REFINED_METHOD_P(me->def)) {
1826 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
1830 if (klass != self) {
1831 rb_clear_method_cache(self, mid);
1833 rb_clear_method_cache(klass, mid);
1834 rb_id_table_delete(RCLASS_WRITABLE_M_TBL(klass), mid);
1836 rb_vm_check_redefinition_opt_method(me, klass);
1838 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1839 rb_add_refined_method_entry(klass, mid);
1842 CALL_METHOD_HOOK(self, removed, mid);
1848 remove_method(klass, mid);
1854 remove_method(klass, rb_intern(name));
1868rb_mod_remove_method(
int argc,
VALUE *argv,
VALUE mod)
1872 for (i = 0; i < argc; i++) {
1876 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
1879 remove_method(mod,
id);
1885rb_export_method(
VALUE klass,
ID name, rb_method_visibility_t visi)
1888 VALUE defined_class;
1889 VALUE origin_class = RCLASS_ORIGIN(klass);
1891 me = search_method0(origin_class, name, &defined_class,
true);
1894 me = search_method(rb_cObject, name, &defined_class);
1897 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1898 UNDEFINED_REFINED_METHOD_P(me->def)) {
1899 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
1902 if (METHOD_ENTRY_VISI(me) != visi) {
1903 rb_vm_check_redefinition_opt_method(me, klass);
1905 if (klass == defined_class || origin_class == defined_class) {
1906 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1909 if (me->def->body.refined.orig_me) {
1910 METHOD_ENTRY_VISI_SET((
rb_method_entry_t *)me->def->body.refined.orig_me, visi);
1914 METHOD_ENTRY_VISI_SET(me, visi);
1916 rb_clear_method_cache(klass, name);
1919 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
1924#define BOUND_PRIVATE 0x01
1925#define BOUND_RESPONDS 0x02
1928method_boundp(
VALUE klass,
ID id,
int ex)
1934 if (ex & BOUND_RESPONDS) {
1935 cme = rb_callable_method_entry_with_refinements(klass,
id, NULL);
1938 cme = callable_method_entry_without_refinements(klass,
id, NULL);
1942 if (ex & ~BOUND_RESPONDS) {
1943 switch (METHOD_ENTRY_VISI(cme)) {
1944 case METHOD_VISI_PRIVATE:
1946 case METHOD_VISI_PROTECTED:
1947 if (ex & BOUND_RESPONDS)
return 0;
1953 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
1954 if (ex & BOUND_RESPONDS)
return 2;
1966 return method_boundp(klass,
id, ex);
1970vm_cref_set_visibility(rb_method_visibility_t method_visi,
int module_func)
1973 scope_visi->method_visi = method_visi;
1974 scope_visi->module_func = module_func;
1978rb_scope_visibility_set(rb_method_visibility_t visi)
1980 vm_cref_set_visibility(visi, FALSE);
1984scope_visibility_check(
void)
1988 if (cfp && cfp->iseq && ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD) {
1989 rb_warn(
"calling %s without arguments inside a method may not have the intended effect",
1990 rb_id2name(rb_frame_this_func()));
1995rb_scope_module_func_set(
void)
1997 scope_visibility_check();
1998 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
2006 rb_method_visibility_t visi;
2008 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
2011 visi = METHOD_VISI_PUBLIC;
2014 switch (vm_scope_visibility_get(ec)) {
2015 case METHOD_VISI_PRIVATE:
2016 if (vm_scope_module_func_check(ec)) {
2017 rb_warning(
"attribute accessor as module_function");
2019 visi = METHOD_VISI_PRIVATE;
2021 case METHOD_VISI_PROTECTED:
2022 visi = METHOD_VISI_PROTECTED;
2025 visi = METHOD_VISI_PUBLIC;
2030 attriv = rb_intern_str(rb_sprintf(
"@%"PRIsVALUE, rb_id2str(
id)));
2032 rb_add_method(klass,
id, VM_METHOD_TYPE_IVAR, (
void *)attriv, visi);
2035 rb_add_method(klass, rb_id_attrset(
id), VM_METHOD_TYPE_ATTRSET, (
void *)attriv, visi);
2048 if (
id == object_id ||
id == id__id__ ||
id == id__send__ ||
id == idInitialize) {
2049 rb_warn(
"undefining '%s' may cause serious problems", rb_id2name(
id));
2052 me = search_method(klass,
id, 0);
2053 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2054 me = rb_resolve_refined_method(
Qnil, me);
2057 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2058 UNDEFINED_REFINED_METHOD_P(me->def)) {
2059 rb_method_name_error(klass, rb_id2str(
id));
2062 rb_add_method(klass,
id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
2064 CALL_METHOD_HOOK(klass, undefined,
id);
2113rb_mod_undef_method(
int argc,
VALUE *argv,
VALUE mod)
2116 for (i = 0; i < argc; i++) {
2120 rb_method_name_error(mod, v);
2127static rb_method_visibility_t
2128check_definition_visibility(
VALUE mod,
int argc,
VALUE *argv)
2131 VALUE mid, include_super, lookup_mod = mod;
2137 if (!
id)
return METHOD_VISI_UNDEF;
2143 inc_super =
RTEST(include_super);
2145 lookup_mod = RCLASS_ORIGIN(mod);
2149 me = rb_method_entry_without_refinements(lookup_mod,
id, NULL);
2151 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED)
return METHOD_VISI_UNDEF;
2152 if (!inc_super && me->owner != mod)
return METHOD_VISI_UNDEF;
2153 return METHOD_ENTRY_VISI(me);
2155 return METHOD_VISI_UNDEF;
2195rb_mod_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2197 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2198 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2202check_definition(
VALUE mod,
int argc,
VALUE *argv, rb_method_visibility_t visi)
2204 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2238rb_mod_public_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2240 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2274rb_mod_private_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2276 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2310rb_mod_protected_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2312 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2318 return rb_method_definition_eq(m1->def, m2->def);
2326 switch (def->type) {
2327 case VM_METHOD_TYPE_REFINED:
2328 if (def->body.refined.orig_me) {
2329 def = def->body.refined.orig_me->def;
2333 case VM_METHOD_TYPE_ALIAS:
2334 def = def->body.alias.original_me->def;
2346 d1 = original_method_definition(d1);
2347 d2 = original_method_definition(d2);
2349 if (d1 == d2)
return 1;
2350 if (!d1 || !d2)
return 0;
2351 if (d1->type != d2->type)
return 0;
2354 case VM_METHOD_TYPE_ISEQ:
2355 return d1->body.iseq.iseqptr == d2->body.iseq.
iseqptr;
2356 case VM_METHOD_TYPE_CFUNC:
2358 d1->body.cfunc.func == d2->body.cfunc.func &&
2359 d1->body.cfunc.argc == d2->body.cfunc.argc;
2360 case VM_METHOD_TYPE_ATTRSET:
2361 case VM_METHOD_TYPE_IVAR:
2362 return d1->body.attr.id == d2->body.attr.id;
2363 case VM_METHOD_TYPE_BMETHOD:
2364 return RTEST(
rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2365 case VM_METHOD_TYPE_MISSING:
2366 return d1->original_id == d2->original_id;
2367 case VM_METHOD_TYPE_ZSUPER:
2368 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2369 case VM_METHOD_TYPE_UNDEF:
2371 case VM_METHOD_TYPE_OPTIMIZED:
2372 return (d1->body.optimized.type == d2->body.optimized.type) &&
2373 (d1->body.optimized.index == d2->body.optimized.index);
2374 case VM_METHOD_TYPE_REFINED:
2375 case VM_METHOD_TYPE_ALIAS:
2378 rb_bug(
"rb_method_definition_eq: unsupported type: %d", d1->type);
2385 def = original_method_definition(def);
2387 if (!def)
return hash;
2389 switch (def->type) {
2390 case VM_METHOD_TYPE_ISEQ:
2392 case VM_METHOD_TYPE_CFUNC:
2393 hash =
rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2395 case VM_METHOD_TYPE_ATTRSET:
2396 case VM_METHOD_TYPE_IVAR:
2398 case VM_METHOD_TYPE_BMETHOD:
2399 return rb_hash_proc(hash, def->body.bmethod.proc);
2400 case VM_METHOD_TYPE_MISSING:
2402 case VM_METHOD_TYPE_ZSUPER:
2403 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2404 case VM_METHOD_TYPE_UNDEF:
2406 case VM_METHOD_TYPE_OPTIMIZED:
2409 case VM_METHOD_TYPE_REFINED:
2410 case VM_METHOD_TYPE_ALIAS:
2413 rb_bug(
"rb_hash_method_definition: unsupported method type (%d)", def->type);
2419 return rb_hash_method_definition(hash, me->def);
2425 const VALUE target_klass = klass;
2426 VALUE defined_class;
2428 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2437 orig_me = search_method(klass, original_name, &defined_class);
2439 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2440 orig_me = rb_resolve_refined_method(
Qnil, orig_me);
2443 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2444 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2446 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2447 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2448 rb_print_undef(target_klass, original_name, METHOD_VISI_UNDEF);
2452 switch (orig_me->def->type) {
2453 case VM_METHOD_TYPE_ZSUPER:
2455 original_name = orig_me->def->original_id;
2456 visi = METHOD_ENTRY_VISI(orig_me);
2458 case VM_METHOD_TYPE_ALIAS:
2459 visi = METHOD_ENTRY_VISI(orig_me);
2460 orig_me = orig_me->def->body.alias.original_me;
2461 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2466 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2468 if (orig_me->defined_class == 0) {
2469 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2470 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2471 (
void *)rb_method_entry_clone(orig_me));
2472 method_added(target_klass, alias_name);
2477 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2478 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2484 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2516 rb_print_undef_str(mod, oldname);
2524check_and_export_method(
VALUE self,
VALUE name, rb_method_visibility_t visi)
2528 rb_print_undef_str(self, name);
2530 rb_export_method(self,
id, visi);
2534set_method_visibility(
VALUE self,
int argc,
const VALUE *argv, rb_method_visibility_t visi)
2538 rb_check_frozen(self);
2540 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
2541 QUOTE_ID(rb_frame_callee()));
2552 check_and_export_method(self,
RARRAY_AREF(v, j), visi);
2556 for (i = 0; i < argc; i++) {
2557 check_and_export_method(self, argv[i], visi);
2563set_visibility(
int argc,
const VALUE *argv,
VALUE module, rb_method_visibility_t visi)
2566 scope_visibility_check();
2567 rb_scope_visibility_set(visi);
2571 set_method_visibility(module, argc, argv, visi);
2596rb_mod_public(
int argc,
VALUE *argv,
VALUE module)
2598 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2628rb_mod_protected(
int argc,
VALUE *argv,
VALUE module)
2630 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2662rb_mod_private(
int argc,
VALUE *argv,
VALUE module)
2664 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
2704rb_mod_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2707 VALUE origin_class = RCLASS_ORIGIN(module);
2710 rb_check_frozen(module);
2712 for (i = 0; i < argc; i++) {
2716 VALUE defined_class;
2719 rb_print_undef_str(module, v);
2722 me = search_method(origin_class, name, &defined_class);
2724 me = search_method(rb_cObject, name, &defined_class);
2727 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2728 UNDEFINED_REFINED_METHOD_P(me->def)) {
2729 rb_print_undef(module, name, METHOD_VISI_UNDEF);
2732 if (module == defined_class || origin_class == defined_class) {
2733 switch (me->def->type) {
2734 case VM_METHOD_TYPE_ISEQ:
2735 if (ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_rest &&
2736 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kw &&
2737 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kwrest) {
2738 ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.ruby2_keywords = 1;
2739 rb_clear_method_cache(module, name);
2742 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name));
2745 case VM_METHOD_TYPE_BMETHOD: {
2746 VALUE procval = me->def->body.bmethod.proc;
2747 if (vm_block_handler_type(procval) == block_handler_type_proc) {
2748 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
2751 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
2753 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
2754 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
2755 !ISEQ_BODY(iseq)->param.flags.has_kw &&
2756 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
2757 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
2758 rb_clear_method_cache(module, name);
2761 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name));
2768 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method not defined in Ruby)", QUOTE_ID(name));
2773 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (can only set in method defining module)", QUOTE_ID(name));
2792rb_mod_public_method(
int argc,
VALUE *argv,
VALUE obj)
2820rb_mod_private_method(
int argc,
VALUE *argv,
VALUE obj)
2844 return rb_mod_public(argc, argv, rb_top_main_class(
"public"));
2864 return rb_mod_private(argc, argv, rb_top_main_class(
"private"));
2875top_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2877 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class(
"ruby2_keywords"));
2923rb_mod_modfunc(
int argc,
VALUE *argv,
VALUE module)
2930 rb_raise(
rb_eTypeError,
"module_function must be called for modules");
2934 rb_scope_module_func_set();
2938 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
2940 for (i = 0; i < argc; i++) {
2945 me = search_method(m,
id, 0);
2947 me = search_method(rb_cObject,
id, 0);
2949 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2950 rb_print_undef(module,
id, METHOD_VISI_UNDEF);
2952 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
2968#pragma push_macro("rb_method_basic_definition_p")
2969#undef rb_method_basic_definition_p
2975 if (!klass)
return TRUE;
2976 cme = rb_callable_method_entry(klass,
id);
2977 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
2980#pragma pop_macro("rb_method_basic_definition_p")
2987 VALUE passed_block_handler = vm_passed_block_handler(ec);
2988 VALUE result = rb_vm_call_kw(ec, obj,
id, argc, argv, cme, kw_splat);
2989 vm_passed_block_handler_set(ec, passed_block_handler);
2997 VALUE defined_class, args[2];
2998 const ID rtmid = idRespond_to_missing;
3001 if (!cme || METHOD_ENTRY_BASIC(cme))
return Qundef;
3004 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args,
RB_NO_KEYWORDS);
3013 switch (method_boundp(klass,
id, pub|BOUND_RESPONDS)) {
3017 ret = basic_obj_respond_to_missing(ec, klass, obj,
ID2SYM(
id),
3019 return RTEST(ret) && !UNDEF_P(ret);
3028 VALUE defined_class;
3029 const ID resid = idRespond_to;
3032 if (!cme)
return -1;
3033 if (METHOD_ENTRY_BASIC(cme)) {
3046 rb_raise(rb_eArgError,
3047 "respond_to? must accept 1 or 2 arguments (requires %d)",
3056 "%"PRIsVALUE
"%c""respond_to?(:%"PRIsVALUE
") uses"
3057 " the deprecated method signature, which takes one parameter",
3058 (RCLASS_SINGLETON_P(klass) ? obj : klass),
3059 (RCLASS_SINGLETON_P(klass) ?
'.' :
'#'),
3061 if (!
NIL_P(location)) {
3066 RSTRING_PTR(path),
NUM2INT(line),
3067 "respond_to? is defined here");
3072 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args,
RB_NO_KEYWORDS);
3073 return RTEST(result);
3081 return rb_ec_obj_respond_to(ec, obj,
id, priv);
3088 int ret = vm_respond_to(ec, klass, obj,
id, priv);
3089 if (ret == -1) ret = basic_obj_respond_to(ec, obj,
id, !priv);
3121obj_respond_to(
int argc,
VALUE *argv,
VALUE obj)
3129 VALUE ret = basic_obj_respond_to_missing(ec,
CLASS_OF(obj), obj,
3131 if (UNDEF_P(ret)) ret =
Qfalse;
3134 return RBOOL(basic_obj_respond_to(ec, obj,
id, !
RTEST(priv)));
3159Init_eval_method(
void)
3181 "public", top_public, -1);
3183 "private", top_private, -1);
3185 "ruby2_keywords", top_ruby2_keywords, -1);
3188#define REPLICATE_METHOD(klass, id) do { \
3189 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3190 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RUBY_ATOMIC_FETCH_SUB(var, val)
Atomically replaces the value pointed by var with the result of subtraction of val to the old value o...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#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.
#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.
#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_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.
#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.
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
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.
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.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#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.
#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.