11static 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)
26static enum rb_id_table_iterator_result
27vm_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 fprintf(stderr,
" | [%d]\t", i); vm_ci_dump(ccs->entries[i].ci);
35 rp_m(
" | \t", ccs->entries[i].cc);
38 return ID_TABLE_CONTINUE;
42vm_ccs_dump(
VALUE klass,
ID target_mid)
48 if (rb_id_table_lookup(cc_tbl, target_mid, &ccs)) {
49 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
50 vm_ccs_dump_i(target_mid, ccs, NULL);
54 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
55 rb_id_table_foreach(cc_tbl, vm_ccs_dump_i, (
void *)target_mid);
60static enum rb_id_table_iterator_result
61vm_cme_dump_i(
ID mid,
VALUE val,
void *data)
63 ID target_mid = (
ID)data;
64 if (target_mid == 0 || mid == target_mid) {
67 return ID_TABLE_CONTINUE;
71vm_mtbl_dump(
VALUE klass,
ID target_mid)
73 fprintf(stderr,
"# vm_mtbl\n");
78 if (RCLASS_M_TBL(klass)) {
79 if (target_mid != 0) {
80 if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
85 fprintf(stderr,
" ## RCLASS_M_TBL (%p)\n", (
void *)RCLASS_M_TBL(klass));
86 rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
90 fprintf(stderr,
" MTBL: NULL\n");
92 if (RCLASS_CALLABLE_M_TBL(klass)) {
93 if (target_mid != 0) {
94 if (rb_id_table_lookup(RCLASS_CALLABLE_M_TBL(klass), target_mid, &me)) {
99 fprintf(stderr,
" ## RCLASS_CALLABLE_M_TBL\n");
100 rb_id_table_foreach(RCLASS_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
103 if (RCLASS_CC_TBL(klass)) {
104 vm_ccs_dump(klass, target_mid);
112rb_vm_mtbl_dump(
const char *msg,
VALUE klass,
ID target_mid)
114 fprintf(stderr,
"[%s] ", msg);
115 vm_mtbl_dump(klass, target_mid);
121 VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment));
122 VM_ASSERT(callable_method_entry_p(cme));
123 METHOD_ENTRY_INVALIDATED_SET(cme);
124 RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
126 rb_yjit_cme_invalidate(cme);
127 rb_rjit_cme_invalidate(cme);
131rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t idx, st_data_t arg)
133 ((
IC) ic)->entry = NULL;
138void rb_clear_constant_cache(
void) {}
146 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
148 st_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
149 ruby_vm_constant_cache_invalidations += ics->num_entries;
152 rb_yjit_constant_state_changed(
id);
153 rb_rjit_constant_state_changed(
id);
157invalidate_negative_cache(
ID mid)
162 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
163 rb_id_table_delete(vm->negative_cme_table, mid);
165 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
175clear_method_cache_by_id_in_class(
VALUE klass,
ID mid)
178 if (rb_objspace_garbage_object_p(klass))
return;
181 if (LIKELY(RCLASS_SUBCLASSES(klass) == NULL)) {
189 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
193 if (
NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
195 rb_id_table_delete(cc_tbl, mid);
196 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
201 if ((cm_tbl = RCLASS_CALLABLE_M_TBL(klass)) != NULL) {
203 if (rb_yjit_enabled_p && rb_id_table_lookup(cm_tbl, mid, &cme)) {
206 if (rb_rjit_enabled && rb_id_table_lookup(cm_tbl, mid, &cme)) {
209 rb_id_table_delete(cm_tbl, mid);
210 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
212 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
219 if (METHOD_ENTRY_CACHED(cme)) {
220 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
225 VALUE owner = cme->owner;
227 VALUE klass_housing_cme;
228 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
229 klass_housing_cme = owner;
232 klass_housing_cme = RCLASS_ORIGIN(owner);
235 VM_ASSERT(lookup_method_table(klass_housing_cme, mid) == (
const rb_method_entry_t *)cme);
237 rb_method_table_insert(klass_housing_cme, RCLASS_M_TBL(klass_housing_cme), mid, new_cme);
241 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
246 if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
250 if (cme->def->iseq_overload) {
253 vm_cme_invalidate(monly_cme);
259 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
260 VALUE defined_class = cme->defined_class;
261 struct rb_id_table *cm_tbl = RCLASS_CALLABLE_M_TBL(defined_class);
262 VM_ASSERT(cm_tbl != NULL);
263 int r = rb_id_table_delete(cm_tbl, mid);
264 VM_ASSERT(r == TRUE); (void)r;
265 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
268 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
271 invalidate_negative_cache(mid);
278clear_iclass_method_cache_by_id(
VALUE iclass,
VALUE d)
280 VM_ASSERT(RB_TYPE_P(iclass,
T_ICLASS));
282 clear_method_cache_by_id_in_class(iclass, mid);
286clear_iclass_method_cache_by_id_for_refinements(
VALUE klass,
VALUE d)
290 clear_method_cache_by_id_in_class(klass, mid);
295rb_clear_method_cache(
VALUE klass_or_module,
ID mid)
297 if (RB_TYPE_P(klass_or_module,
T_MODULE)) {
298 VALUE module = klass_or_module;
300 if (
FL_TEST(module, RMODULE_IS_REFINEMENT)) {
301 VALUE refined_class = rb_refinement_module_get_refined_class(module);
302 rb_clear_method_cache(refined_class, mid);
303 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
304 rb_clear_all_refinement_method_cache();
306 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
309 clear_method_cache_by_id_in_class(klass_or_module, mid);
314void rb_cc_table_free(
VALUE klass);
317invalidate_all_refinement_cc(
void *vstart,
void *vend,
size_t stride,
void *data)
320 for (; v != (
VALUE)vend; v += stride) {
321 void *ptr = asan_poisoned_object_p(v);
322 asan_unpoison_object(v,
false);
325 if (imemo_type_p(v, imemo_callcache)) {
327 if (vm_cc_refinement_p(cc) && cc->klass) {
328 vm_cc_invalidate(cc);
334 asan_poison_object(v);
349 for (
int i = 0; i < ci->kwarg->keyword_len; i++) {
361 if (ci1->mid != ci2->mid)
return 1;
362 if (ci1->flag != ci2->flag)
return 1;
363 if (ci1->argc != ci2->argc)
return 1;
364 if (ci1->kwarg != NULL) {
365 VM_ASSERT(ci2->kwarg != NULL);
367 if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
370 for (
int i = 0; i < ci1->kwarg->keyword_len; i++) {
371 if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
376 VM_ASSERT(ci2->kwarg == NULL);
387ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
390 st_data_t *ret = (st_data_t *)data;
393 if (rb_objspace_garbage_object_p((
VALUE)ci)) {
394 *ret = (st_data_t)NULL;
402 *key = *value = *ret = (st_data_t)ci;
408rb_vm_ci_lookup(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg)
430 st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
431 }
while (ci == NULL);
436 VM_ASSERT(vm_ci_markable(ci));
448 st_data_t key = (st_data_t)ci;
449 st_delete(vm->ci_table, &key, NULL);
455rb_clear_all_refinement_method_cache(
void)
457 rb_objspace_each_objects(invalidate_all_refinement_cc, NULL);
458 rb_yjit_invalidate_all_method_lookup_assumptions();
464 VALUE table_owner = klass;
465 if (RB_TYPE_P(klass,
T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass)) {
466 table_owner =
RBASIC(table_owner)->klass;
468 VM_ASSERT(RB_TYPE_P(table_owner,
T_CLASS) || RB_TYPE_P(table_owner,
T_ICLASS) || RB_TYPE_P(table_owner,
T_MODULE));
469 VM_ASSERT(table == RCLASS_M_TBL(table_owner));
470 rb_id_table_insert(table, method_id, (
VALUE)me);
479NORETURN(
static VALUE rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj));
482rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj)
492 rb_f_notimplement_internal(argc, argv, obj);
496rb_define_notimplement_method_id(
VALUE mod,
ID id, rb_method_visibility_t visi)
498 rb_add_method(mod,
id, VM_METHOD_TYPE_NOTIMPLEMENTED, (
void *)1, visi);
502rb_add_method_cfunc(
VALUE klass,
ID mid,
VALUE (*func)(
ANYARGS),
int argc, rb_method_visibility_t visi)
504 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError,
"arity out of range: %d for -2..15", argc);
509 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
512 rb_define_notimplement_method_id(klass, mid, visi);
517rb_add_method_optimized(
VALUE klass,
ID mid,
enum method_optimized_type opt_type,
unsigned int index, rb_method_visibility_t visi)
523 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
530 const int reference_count = def->reference_count;
531 def->reference_count--;
533 VM_ASSERT(reference_count >= 0);
535 if (def->reference_count == 0) {
536 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d (remove)\n", (
void *)def,
537 rb_id2name(def->original_id), def->reference_count);
538 if (def->type == VM_METHOD_TYPE_BMETHOD && def->body.bmethod.hooks) {
539 xfree(def->body.bmethod.hooks);
544 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d->%d (dec)\n", (
void *)def, rb_id2name(def->original_id),
545 reference_count, def->reference_count);
555 if (me->def && me->def->iseq_overload) {
558 rb_method_definition_release(me->def);
567 if (!GET_THREAD()->ext_config.ractor_safe) {
569 case -2:
return &call_cfunc_m2;
570 case -1:
return &call_cfunc_m1;
571 case 0:
return &call_cfunc_0;
572 case 1:
return &call_cfunc_1;
573 case 2:
return &call_cfunc_2;
574 case 3:
return &call_cfunc_3;
575 case 4:
return &call_cfunc_4;
576 case 5:
return &call_cfunc_5;
577 case 6:
return &call_cfunc_6;
578 case 7:
return &call_cfunc_7;
579 case 8:
return &call_cfunc_8;
580 case 9:
return &call_cfunc_9;
581 case 10:
return &call_cfunc_10;
582 case 11:
return &call_cfunc_11;
583 case 12:
return &call_cfunc_12;
584 case 13:
return &call_cfunc_13;
585 case 14:
return &call_cfunc_14;
586 case 15:
return &call_cfunc_15;
588 rb_bug(
"unsupported length: %d", argc);
593 case -2:
return &ractor_safe_call_cfunc_m2;
594 case -1:
return &ractor_safe_call_cfunc_m1;
595 case 0:
return &ractor_safe_call_cfunc_0;
596 case 1:
return &ractor_safe_call_cfunc_1;
597 case 2:
return &ractor_safe_call_cfunc_2;
598 case 3:
return &ractor_safe_call_cfunc_3;
599 case 4:
return &ractor_safe_call_cfunc_4;
600 case 5:
return &ractor_safe_call_cfunc_5;
601 case 6:
return &ractor_safe_call_cfunc_6;
602 case 7:
return &ractor_safe_call_cfunc_7;
603 case 8:
return &ractor_safe_call_cfunc_8;
604 case 9:
return &ractor_safe_call_cfunc_9;
605 case 10:
return &ractor_safe_call_cfunc_10;
606 case 11:
return &ractor_safe_call_cfunc_11;
607 case 12:
return &ractor_safe_call_cfunc_12;
608 case 13:
return &ractor_safe_call_cfunc_13;
609 case 14:
return &ractor_safe_call_cfunc_14;
610 case 15:
return &ractor_safe_call_cfunc_15;
612 rb_bug(
"unsupported length: %d", argc);
622 cfunc->invoker = call_cfunc_invoker_func(argc);
628 if (!complemented && def->reference_count > 0) def->aliased =
true;
629 def->reference_count++;
630 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d\n", (
void *)def, rb_id2name(def->original_id), def->reference_count);
637 rb_method_definition_release(me->def);
642 case VM_METHOD_TYPE_ISEQ:
651 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
653 if (0) vm_cref_dump(
"rb_method_definition_create", cref);
659 method_cref = vm_cref_new_toplevel(GET_EC());
665 case VM_METHOD_TYPE_CFUNC:
668 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
671 case VM_METHOD_TYPE_ATTRSET:
672 case VM_METHOD_TYPE_IVAR:
678 def->body.attr.id = (
ID)(
VALUE)opts;
680 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
682 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
684 RB_OBJ_WRITE(me, &def->body.attr.location, rb_ary_freeze(location));
687 VM_ASSERT(def->body.attr.location == 0);
691 case VM_METHOD_TYPE_BMETHOD:
693 RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
695 case VM_METHOD_TYPE_NOTIMPLEMENTED:
696 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (
VALUE(*)(
ANYARGS))rb_f_notimplement_internal, -1);
698 case VM_METHOD_TYPE_OPTIMIZED:
701 case VM_METHOD_TYPE_REFINED:
706 case VM_METHOD_TYPE_ALIAS:
709 case VM_METHOD_TYPE_ZSUPER:
710 case VM_METHOD_TYPE_UNDEF:
711 case VM_METHOD_TYPE_MISSING:
723 case VM_METHOD_TYPE_ISEQ:
727 case VM_METHOD_TYPE_ATTRSET:
728 case VM_METHOD_TYPE_IVAR:
731 case VM_METHOD_TYPE_BMETHOD:
735 if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((
VALUE)me);
737 case VM_METHOD_TYPE_REFINED:
740 case VM_METHOD_TYPE_ALIAS:
743 case VM_METHOD_TYPE_CFUNC:
744 case VM_METHOD_TYPE_ZSUPER:
745 case VM_METHOD_TYPE_MISSING:
746 case VM_METHOD_TYPE_OPTIMIZED:
747 case VM_METHOD_TYPE_UNDEF:
748 case VM_METHOD_TYPE_NOTIMPLEMENTED:
754rb_method_definition_create(rb_method_type_t
type,
ID mid)
759 def->original_id = mid;
760 static uintptr_t method_serial = 1;
761 def->method_serial = method_serial++;
768 if (def) method_definition_addref(def, complement);
774filter_defined_class(
VALUE klass)
786 rb_bug(
"filter_defined_class: %s", rb_obj_info(klass));
792 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def,
false);
793 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
794 if (def != NULL) method_definition_reset(me);
802 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));
804 METHOD_ENTRY_FLAGS_COPY(me, src_me);
808 src_me->def->type == VM_METHOD_TYPE_REFINED &&
809 src_me->def->body.refined.orig_me) {
811 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
814 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
815 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
820 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
821 rb_method_definition_set(me, clone_def, orig_clone);
833 if (!src_me->defined_class &&
834 def->type == VM_METHOD_TYPE_REFINED &&
835 def->body.refined.orig_me) {
837 rb_method_entry_clone(def->body.refined.orig_me);
839 refined_orig_me = orig_me;
843 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def,
true);
844 METHOD_ENTRY_FLAGS_COPY(me, src_me);
845 METHOD_ENTRY_COMPLEMENTED_SET(me);
847 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
848 rb_method_definition_set(me, def, (
void *)refined_orig_me);
851 VM_ASSERT(RB_TYPE_P(me->owner,
T_MODULE));
859 rb_method_definition_release(dst->def);
860 *(
rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
861 method_definition_reset(dst);
862 dst->called_id = src->called_id;
865 METHOD_ENTRY_FLAGS_COPY(dst, src);
871 if (me->def->type == VM_METHOD_TYPE_REFINED) {
877 rb_vm_check_redefinition_opt_method(me, me->owner);
880 rb_method_entry_alloc(me->called_id, me->owner,
881 me->defined_class ? me->defined_class : owner,
884 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
886 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
887 rb_method_definition_set(me, def, orig_me);
888 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
893lookup_method_table(
VALUE klass,
ID id)
898 if (rb_id_table_lookup(m_tbl,
id, &body)) {
907rb_add_refined_method_entry(
VALUE refined_class,
ID mid)
912 make_method_entry_refined(refined_class, me);
913 rb_clear_method_cache(refined_class, mid);
916 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
921check_override_opt_method_i(
VALUE klass,
VALUE arg)
926 if (vm_redefinition_check_flag(klass)) {
927 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
929 newme = rb_method_entry(klass, mid);
930 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
933 rb_class_foreach_subclass(klass, check_override_opt_method_i, (
VALUE)mid);
937check_override_opt_method(
VALUE klass,
VALUE mid)
939 if (rb_vm_check_optimizable_mid(mid)) {
940 check_override_opt_method_i(klass, mid);
951rb_method_entry_make(
VALUE klass,
ID mid,
VALUE defined_class, rb_method_visibility_t visi,
957 int make_refined = 0;
966 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
967 type != VM_METHOD_TYPE_ZSUPER) {
970 case idInitialize_copy:
971 case idInitialize_clone:
972 case idInitialize_dup:
973 case idRespond_to_missing:
974 visi = METHOD_VISI_PRIVATE;
978 if (
type != VM_METHOD_TYPE_REFINED) {
982 if (RB_TYPE_P(klass,
T_MODULE) &&
FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
983 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
984 rb_add_refined_method_entry(refined_class, mid);
986 if (
type == VM_METHOD_TYPE_REFINED) {
988 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
991 klass = RCLASS_ORIGIN(klass);
992 if (klass != orig_klass) {
993 rb_clear_method_cache(orig_klass, mid);
996 mtbl = RCLASS_M_TBL(klass);
999 if (rb_id_table_lookup(mtbl, mid, &data)) {
1003 if (rb_method_definition_eq(old_def, def))
return old_me;
1004 rb_vm_check_redefinition_opt_method(old_me, klass);
1006 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1009 type != VM_METHOD_TYPE_UNDEF &&
1010 (old_def->aliased ==
false) &&
1011 (!old_def->no_redef_warning) &&
1013 old_def->type != VM_METHOD_TYPE_UNDEF &&
1014 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1015 old_def->type != VM_METHOD_TYPE_ALIAS) {
1018 rb_warning(
"method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1019 switch (old_def->type) {
1020 case VM_METHOD_TYPE_ISEQ:
1021 iseq = def_iseq_ptr(old_def);
1023 case VM_METHOD_TYPE_BMETHOD:
1024 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1030 rb_compile_warning(RSTRING_PTR(rb_iseq_path(iseq)),
1031 ISEQ_BODY(iseq)->location.first_lineno,
1032 "previous definition of %"PRIsVALUE
" was here",
1033 rb_id2str(old_def->original_id));
1039 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1041 def = rb_method_definition_create(
type, original_id);
1043 rb_method_definition_set(me, def, opts);
1045 rb_clear_method_cache(klass, mid);
1048 if (klass == rb_cObject) {
1051 case idRespond_to_missing:
1052 case idMethodMissing:
1054 rb_warn(
"redefining Object#%s may cause infinite loop", rb_id2name(mid));
1058 if (mid == object_id || mid == id__send__) {
1059 if (
type == VM_METHOD_TYPE_ISEQ && search_method(klass, mid, 0)) {
1060 rb_warn(
"redefining `%s' may cause serious problems", rb_id2name(mid));
1065 make_method_entry_refined(klass, me);
1068 rb_method_table_insert(klass, mtbl, mid, me);
1070 VM_ASSERT(me->def != NULL);
1073 if (RB_TYPE_P(orig_klass,
T_MODULE)) {
1074 check_override_opt_method(klass, (
VALUE)mid);
1083overloaded_cme_table(
void)
1085 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
1086 return GET_VM()->overloaded_cme_table;
1089#if VM_CHECK_MODE > 0
1091vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1093 fprintf(stderr,
"key: "); rp(key);
1094 fprintf(stderr,
"val: "); rp(val);
1099rb_vm_dump_overloaded_cme_table(
void)
1101 fprintf(stderr,
"== rb_vm_dump_overloaded_cme_table\n");
1102 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1107lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
1114 if (rb_objspace_garbage_object_p((
VALUE)cme) ||
1115 rb_objspace_garbage_object_p((
VALUE)monly_cme)) {
1130 ASSERT_vm_locking();
1133 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1137#if VM_CHECK_MODE > 0
1141 return lookup_overloaded_cme(cme);
1148 st_data_t cme_data = (st_data_t)cme;
1149 ASSERT_vm_locking();
1150 st_delete(overloaded_cme_table(), &cme_data, NULL);
1158 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1173 ASSERT_vm_locking();
1174 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1176 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1184 if (UNLIKELY(cme->def->iseq_overload) &&
1185 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1186 (
int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1187 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ);
1189 cme = get_overloaded_cme(cme);
1191 VM_ASSERT(cme != NULL);
1198#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1199 const VALUE arg = ID2SYM(mid); \
1200 VALUE recv_class = (klass); \
1201 ID hook_id = (hook); \
1202 if (FL_TEST((klass), FL_SINGLETON)) { \
1203 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1204 hook_id = singleton_##hook; \
1206 rb_funcallv(recv_class, hook_id, 1, &arg); \
1210method_added(
VALUE klass,
ID mid)
1213 CALL_METHOD_HOOK(klass, added, mid);
1218rb_add_method(
VALUE klass,
ID mid, rb_method_type_t
type,
void *opts, rb_method_visibility_t visi)
1220 rb_method_entry_make(klass, mid, klass, visi,
type, NULL, mid, opts);
1222 if (
type != VM_METHOD_TYPE_UNDEF &&
type != VM_METHOD_TYPE_REFINED) {
1223 method_added(klass, mid);
1236 iseq_body.
cref = cref;
1238 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1243 rb_method_visibility_t visi,
VALUE defined_class)
1245 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
1246 me->def->type, me->def, 0, NULL);
1248 me->def->no_redef_warning = TRUE;
1251 method_added(klass, mid);
1258 return method_entry_set(klass, mid, me, visi, klass);
1261#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1268 rb_raise(
rb_eTypeError,
"can't define an allocator for a singleton class");
1270 RCLASS_SET_ALLOCATOR(klass, func);
1286 if (allocator == UNDEF_ALLOC_FUNC)
break;
1287 if (allocator)
return allocator;
1293rb_method_entry_at(
VALUE klass,
ID id)
1295 return lookup_method_table(klass,
id);
1299search_method0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool skip_refined)
1303 RB_DEBUG_COUNTER_INC(mc_search);
1306 RB_DEBUG_COUNTER_INC(mc_search_super);
1307 if ((me = lookup_method_table(klass,
id)) != 0) {
1308 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1309 me->def->body.refined.orig_me) {
1315 if (defined_class_ptr) *defined_class_ptr = klass;
1317 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1319 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me));
1324search_method(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1326 return search_method0(klass,
id, defined_class_ptr,
false);
1330search_method_protect(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1334 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1343rb_method_entry(
VALUE klass,
ID id)
1345 return search_method_protect(klass,
id, NULL);
1356 if (me->defined_class == 0) {
1357 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1358 VM_ASSERT(RB_TYPE_P(defined_class,
T_ICLASS) || RB_TYPE_P(defined_class,
T_MODULE));
1359 VM_ASSERT(me->defined_class == 0);
1361 mtbl = RCLASS_CALLABLE_M_TBL(defined_class);
1363 if (mtbl && rb_id_table_lookup(mtbl,
id, &cme_data)) {
1365 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1366 VM_ASSERT(callable_method_entry_p(cme));
1367 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1371 mtbl = RCLASS_EXT(defined_class)->callable_m_tbl = rb_id_table_create(0);
1373 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1374 rb_id_table_insert(mtbl,
id, (
VALUE)cme);
1376 VM_ASSERT(callable_method_entry_p(cme));
1384 VM_ASSERT(callable_method_entry_p(cme));
1385 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1395complemented_callable_method_entry(
VALUE klass,
ID id)
1397 VALUE defined_class;
1399 return prepare_callable_method_entry(defined_class,
id, me, FALSE);
1403cached_callable_method_entry(
VALUE klass,
ID mid)
1405 ASSERT_vm_locking();
1407 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1410 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1412 VM_ASSERT(vm_ccs_p(ccs));
1414 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1415 VM_ASSERT(ccs->cme->called_id == mid);
1416 RB_DEBUG_COUNTER_INC(ccs_found);
1420 rb_vm_ccs_free(ccs);
1421 rb_id_table_delete(cc_tbl, mid);
1425 RB_DEBUG_COUNTER_INC(ccs_not_found);
1432 ASSERT_vm_locking();
1433 VM_ASSERT(cme != NULL);
1435 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1439 cc_tbl = RCLASS_CC_TBL(klass) = rb_id_table_create(2);
1442 if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1443#if VM_CHECK_MODE > 0
1445 VM_ASSERT(ccs->cme == cme);
1449 vm_ccs_create(klass, cc_tbl, mid, cme);
1460 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1465 rb_id_table_insert(vm->negative_cme_table, mid, (
VALUE)cme);
1468 VM_ASSERT(cme != NULL);
1473callable_method_entry_or_negative(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1480 cme = cached_callable_method_entry(klass, mid);
1483 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1486 VALUE defined_class;
1488 if (defined_class_ptr) *defined_class_ptr = defined_class;
1491 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1494 cme = negative_cme(mid);
1497 cache_callable_method_entry(klass, mid, cme);
1508rb_callable_method_entry_or_negative(
VALUE klass,
ID mid)
1510 return callable_method_entry_or_negative(klass, mid, NULL);
1514callable_method_entry(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1517 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
1518 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1522rb_callable_method_entry(
VALUE klass,
ID mid)
1524 return callable_method_entry(klass, mid, NULL);
1530method_entry_resolve_refinement(
VALUE klass,
ID id,
int with_refinement,
VALUE *defined_class_ptr)
1532 const rb_method_entry_t *me = search_method_protect(klass,
id, defined_class_ptr);
1535 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1536 if (with_refinement) {
1538 VALUE refinements = cref ? CREF_REFINEMENTS(cref) :
Qnil;
1539 me = resolve_refined_method(refinements, me, defined_class_ptr);
1542 me = resolve_refined_method(
Qnil, me, defined_class_ptr);
1545 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1553rb_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1555 return method_entry_resolve_refinement(klass,
id, TRUE, defined_class_ptr);
1559callable_method_entry_refinements0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements,
1562 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
1566 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1567 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, with_refinements, dcp);
1568 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1573callable_method_entry_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements)
1576 return callable_method_entry_refinements0(klass,
id, defined_class_ptr, with_refinements, cme);
1580rb_callable_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1582 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
true);
1586callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1588 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
false);
1592rb_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1594 return method_entry_resolve_refinement(klass,
id, FALSE, defined_class_ptr);
1598rb_callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1600 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1601 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, FALSE, dcp);
1602 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1608 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1613 refinement = find_refinement(refinements, me->owner);
1614 if (!
NIL_P(refinement)) {
1615 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
1617 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
1622 tmp_me = me->def->body.refined.orig_me;
1624 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
1633 me = search_method_protect(super, me->called_id, defined_class_ptr);
1641 return resolve_refined_method(refinements, me, NULL);
1647 VALUE defined_class = me->defined_class;
1650 if (resolved_me && resolved_me->defined_class == 0) {
1651 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1659remove_method(
VALUE klass,
ID mid)
1666 klass = RCLASS_ORIGIN(klass);
1667 if (mid == object_id || mid == id__send__ || mid == idInitialize) {
1668 rb_warn(
"removing `%s' may cause serious problems", rb_id2name(mid));
1671 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
1673 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
1674 UNDEFINED_REFINED_METHOD_P(me->def)) {
1675 rb_name_err_raise(
"method `%1$s' not defined in %2$s",
1679 if (klass != self) {
1680 rb_clear_method_cache(self, mid);
1682 rb_clear_method_cache(klass, mid);
1683 rb_id_table_delete(RCLASS_M_TBL(klass), mid);
1685 rb_vm_check_redefinition_opt_method(me, klass);
1687 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1688 rb_add_refined_method_entry(klass, mid);
1691 CALL_METHOD_HOOK(self, removed, mid);
1697 remove_method(klass, mid);
1703 remove_method(klass, rb_intern(name));
1717rb_mod_remove_method(
int argc,
VALUE *argv,
VALUE mod)
1721 for (i = 0; i < argc; i++) {
1725 rb_name_err_raise(
"method `%1$s' not defined in %2$s",
1728 remove_method(mod,
id);
1734rb_export_method(
VALUE klass,
ID name, rb_method_visibility_t visi)
1737 VALUE defined_class;
1738 VALUE origin_class = RCLASS_ORIGIN(klass);
1740 me = search_method0(origin_class, name, &defined_class,
true);
1742 if (!me && RB_TYPE_P(klass,
T_MODULE)) {
1743 me = search_method(rb_cObject, name, &defined_class);
1746 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1747 UNDEFINED_REFINED_METHOD_P(me->def)) {
1748 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
1751 if (METHOD_ENTRY_VISI(me) != visi) {
1752 rb_vm_check_redefinition_opt_method(me, klass);
1754 if (klass == defined_class || origin_class == defined_class) {
1755 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1758 if (me->def->body.refined.orig_me) {
1759 METHOD_ENTRY_VISI_SET((
rb_method_entry_t *)me->def->body.refined.orig_me, visi);
1763 METHOD_ENTRY_VISI_SET(me, visi);
1765 rb_clear_method_cache(klass, name);
1768 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
1773#define BOUND_PRIVATE 0x01
1774#define BOUND_RESPONDS 0x02
1777method_boundp(
VALUE klass,
ID id,
int ex)
1783 if (ex & BOUND_RESPONDS) {
1784 cme = rb_callable_method_entry_with_refinements(klass,
id, NULL);
1787 cme = callable_method_entry_without_refinements(klass,
id, NULL);
1791 if (ex & ~BOUND_RESPONDS) {
1792 switch (METHOD_ENTRY_VISI(cme)) {
1793 case METHOD_VISI_PRIVATE:
1795 case METHOD_VISI_PROTECTED:
1796 if (ex & BOUND_RESPONDS)
return 0;
1802 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
1803 if (ex & BOUND_RESPONDS)
return 2;
1815 return method_boundp(klass,
id, ex);
1819vm_cref_set_visibility(rb_method_visibility_t method_visi,
int module_func)
1822 scope_visi->method_visi = method_visi;
1823 scope_visi->module_func = module_func;
1827rb_scope_visibility_set(rb_method_visibility_t visi)
1829 vm_cref_set_visibility(visi, FALSE);
1833scope_visibility_check(
void)
1837 if (cfp && cfp->iseq && ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD) {
1838 rb_warn(
"calling %s without arguments inside a method may not have the intended effect",
1839 rb_id2name(rb_frame_this_func()));
1844rb_scope_module_func_set(
void)
1846 scope_visibility_check();
1847 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
1855 rb_method_visibility_t visi;
1857 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
1860 visi = METHOD_VISI_PUBLIC;
1863 switch (vm_scope_visibility_get(ec)) {
1864 case METHOD_VISI_PRIVATE:
1865 if (vm_scope_module_func_check(ec)) {
1866 rb_warning(
"attribute accessor as module_function");
1868 visi = METHOD_VISI_PRIVATE;
1870 case METHOD_VISI_PROTECTED:
1871 visi = METHOD_VISI_PROTECTED;
1874 visi = METHOD_VISI_PUBLIC;
1879 attriv = rb_intern_str(rb_sprintf(
"@%"PRIsVALUE, rb_id2str(
id)));
1881 rb_add_method(klass,
id, VM_METHOD_TYPE_IVAR, (
void *)attriv, visi);
1884 rb_add_method(klass, rb_id_attrset(
id), VM_METHOD_TYPE_ATTRSET, (
void *)attriv, visi);
1897 if (
id == object_id ||
id == id__send__ ||
id == idInitialize) {
1898 rb_warn(
"undefining `%s' may cause serious problems", rb_id2name(
id));
1901 me = search_method(klass,
id, 0);
1902 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1903 me = rb_resolve_refined_method(
Qnil, me);
1906 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1907 UNDEFINED_REFINED_METHOD_P(me->def)) {
1908 rb_method_name_error(klass, rb_id2str(
id));
1911 rb_add_method(klass,
id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
1913 CALL_METHOD_HOOK(klass, undefined,
id);
1962rb_mod_undef_method(
int argc,
VALUE *argv,
VALUE mod)
1965 for (i = 0; i < argc; i++) {
1969 rb_method_name_error(mod, v);
1976static rb_method_visibility_t
1977check_definition_visibility(
VALUE mod,
int argc,
VALUE *argv)
1980 VALUE mid, include_super, lookup_mod = mod;
1986 if (!
id)
return METHOD_VISI_UNDEF;
1992 inc_super =
RTEST(include_super);
1994 lookup_mod = RCLASS_ORIGIN(mod);
1998 me = rb_method_entry_without_refinements(lookup_mod,
id, NULL);
2000 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED)
return METHOD_VISI_UNDEF;
2001 if (!inc_super && me->owner != mod)
return METHOD_VISI_UNDEF;
2002 return METHOD_ENTRY_VISI(me);
2004 return METHOD_VISI_UNDEF;
2044rb_mod_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2046 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2047 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2051check_definition(
VALUE mod,
int argc,
VALUE *argv, rb_method_visibility_t visi)
2053 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2087rb_mod_public_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2089 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2123rb_mod_private_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2125 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2159rb_mod_protected_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2161 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2167 return rb_method_definition_eq(m1->def, m2->def);
2175 switch (def->type) {
2176 case VM_METHOD_TYPE_REFINED:
2177 if (def->body.refined.orig_me) {
2178 def = def->body.refined.orig_me->def;
2182 case VM_METHOD_TYPE_ALIAS:
2183 def = def->body.alias.original_me->def;
2195 d1 = original_method_definition(d1);
2196 d2 = original_method_definition(d2);
2198 if (d1 == d2)
return 1;
2199 if (!d1 || !d2)
return 0;
2200 if (d1->type != d2->type)
return 0;
2203 case VM_METHOD_TYPE_ISEQ:
2204 return d1->body.iseq.iseqptr == d2->body.iseq.
iseqptr;
2205 case VM_METHOD_TYPE_CFUNC:
2207 d1->body.cfunc.func == d2->body.cfunc.func &&
2208 d1->body.cfunc.argc == d2->body.cfunc.argc;
2209 case VM_METHOD_TYPE_ATTRSET:
2210 case VM_METHOD_TYPE_IVAR:
2211 return d1->body.attr.id == d2->body.attr.id;
2212 case VM_METHOD_TYPE_BMETHOD:
2213 return RTEST(
rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2214 case VM_METHOD_TYPE_MISSING:
2215 return d1->original_id == d2->original_id;
2216 case VM_METHOD_TYPE_ZSUPER:
2217 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2218 case VM_METHOD_TYPE_UNDEF:
2220 case VM_METHOD_TYPE_OPTIMIZED:
2221 return (d1->body.optimized.type == d2->body.optimized.type) &&
2222 (d1->body.optimized.index == d2->body.optimized.index);
2223 case VM_METHOD_TYPE_REFINED:
2224 case VM_METHOD_TYPE_ALIAS:
2227 rb_bug(
"rb_method_definition_eq: unsupported type: %d", d1->type);
2234 def = original_method_definition(def);
2236 if (!def)
return hash;
2238 switch (def->type) {
2239 case VM_METHOD_TYPE_ISEQ:
2241 case VM_METHOD_TYPE_CFUNC:
2242 hash =
rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2244 case VM_METHOD_TYPE_ATTRSET:
2245 case VM_METHOD_TYPE_IVAR:
2247 case VM_METHOD_TYPE_BMETHOD:
2248 return rb_hash_proc(hash, def->body.bmethod.proc);
2249 case VM_METHOD_TYPE_MISSING:
2251 case VM_METHOD_TYPE_ZSUPER:
2252 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2253 case VM_METHOD_TYPE_UNDEF:
2255 case VM_METHOD_TYPE_OPTIMIZED:
2258 case VM_METHOD_TYPE_REFINED:
2259 case VM_METHOD_TYPE_ALIAS:
2262 rb_bug(
"rb_hash_method_definition: unsupported method type (%d)", def->type);
2268 return rb_hash_method_definition(hash, me->def);
2274 const VALUE target_klass = klass;
2275 VALUE defined_class;
2277 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2286 orig_me = search_method(klass, original_name, &defined_class);
2288 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2289 orig_me = rb_resolve_refined_method(
Qnil, orig_me);
2292 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2293 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2294 if ((!RB_TYPE_P(klass,
T_MODULE)) ||
2295 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2296 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2297 rb_print_undef(klass, original_name, METHOD_VISI_UNDEF);
2301 switch (orig_me->def->type) {
2302 case VM_METHOD_TYPE_ZSUPER:
2304 original_name = orig_me->def->original_id;
2305 visi = METHOD_ENTRY_VISI(orig_me);
2307 case VM_METHOD_TYPE_ALIAS:
2308 visi = METHOD_ENTRY_VISI(orig_me);
2309 orig_me = orig_me->def->body.alias.original_me;
2310 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2315 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2317 if (orig_me->defined_class == 0) {
2318 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2319 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2320 (
void *)rb_method_entry_clone(orig_me));
2321 method_added(target_klass, alias_name);
2326 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2327 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2328 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2359 rb_print_undef_str(mod, oldname);
2367check_and_export_method(
VALUE self,
VALUE name, rb_method_visibility_t visi)
2371 rb_print_undef_str(self, name);
2373 rb_export_method(self,
id, visi);
2377set_method_visibility(
VALUE self,
int argc,
const VALUE *argv, rb_method_visibility_t visi)
2383 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
2384 QUOTE_ID(rb_frame_callee()));
2391 if (argc == 1 && (v = rb_check_array_type(argv[0])) !=
Qnil) {
2395 check_and_export_method(self,
RARRAY_AREF(v, j), visi);
2399 for (i = 0; i < argc; i++) {
2400 check_and_export_method(self, argv[i], visi);
2406set_visibility(
int argc,
const VALUE *argv,
VALUE module, rb_method_visibility_t visi)
2409 scope_visibility_check();
2410 rb_scope_visibility_set(visi);
2414 set_method_visibility(module, argc, argv, visi);
2418 return rb_ary_new_from_values(argc, argv);
2439rb_mod_public(
int argc,
VALUE *argv,
VALUE module)
2441 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2471rb_mod_protected(
int argc,
VALUE *argv,
VALUE module)
2473 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2505rb_mod_private(
int argc,
VALUE *argv,
VALUE module)
2507 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
2547rb_mod_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2550 VALUE origin_class = RCLASS_ORIGIN(module);
2555 for (i = 0; i < argc; i++) {
2559 VALUE defined_class;
2562 rb_print_undef_str(module, v);
2565 me = search_method(origin_class, name, &defined_class);
2566 if (!me && RB_TYPE_P(module,
T_MODULE)) {
2567 me = search_method(rb_cObject, name, &defined_class);
2570 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2571 UNDEFINED_REFINED_METHOD_P(me->def)) {
2572 rb_print_undef(module, name, METHOD_VISI_UNDEF);
2575 if (module == defined_class || origin_class == defined_class) {
2576 switch (me->def->type) {
2577 case VM_METHOD_TYPE_ISEQ:
2578 if (ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_rest &&
2579 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kw &&
2580 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kwrest) {
2581 ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.ruby2_keywords = 1;
2582 rb_clear_method_cache(module, name);
2585 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
2588 case VM_METHOD_TYPE_BMETHOD: {
2589 VALUE procval = me->def->body.bmethod.proc;
2590 if (vm_block_handler_type(procval) == block_handler_type_proc) {
2591 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
2594 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
2596 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
2597 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
2598 !ISEQ_BODY(iseq)->param.flags.has_kw &&
2599 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
2600 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
2601 rb_clear_method_cache(module, name);
2604 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
2611 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method not defined in Ruby)", rb_id2name(name));
2616 rb_warn(
"Skipping set of ruby2_keywords flag for %s (can only set in method defining module)", rb_id2name(name));
2635rb_mod_public_method(
int argc,
VALUE *argv,
VALUE obj)
2663rb_mod_private_method(
int argc,
VALUE *argv,
VALUE obj)
2687 return rb_mod_public(argc, argv, rb_top_main_class(
"public"));
2707 return rb_mod_private(argc, argv, rb_top_main_class(
"private"));
2718top_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
2720 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class(
"ruby2_keywords"));
2766rb_mod_modfunc(
int argc,
VALUE *argv,
VALUE module)
2772 if (!RB_TYPE_P(module,
T_MODULE)) {
2773 rb_raise(
rb_eTypeError,
"module_function must be called for modules");
2777 rb_scope_module_func_set();
2781 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
2783 for (i = 0; i < argc; i++) {
2788 me = search_method(m,
id, 0);
2790 me = search_method(rb_cObject,
id, 0);
2792 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2793 rb_print_undef(module,
id, METHOD_VISI_UNDEF);
2795 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
2807 return rb_ary_new_from_values(argc, argv);
2811#pragma push_macro("rb_method_basic_definition_p")
2812#undef rb_method_basic_definition_p
2818 if (!klass)
return TRUE;
2819 cme = rb_callable_method_entry(klass,
id);
2820 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
2823#pragma pop_macro("rb_method_basic_definition_p")
2830 VALUE passed_block_handler = vm_passed_block_handler(ec);
2831 VALUE result = rb_vm_call_kw(ec, obj,
id, argc, argv, cme, kw_splat);
2832 vm_passed_block_handler_set(ec, passed_block_handler);
2840 VALUE defined_class, args[2];
2841 const ID rtmid = idRespond_to_missing;
2844 if (!cme || METHOD_ENTRY_BASIC(cme))
return Qundef;
2847 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args,
RB_NO_KEYWORDS);
2856 switch (method_boundp(klass,
id, pub|BOUND_RESPONDS)) {
2860 ret = basic_obj_respond_to_missing(ec, klass, obj,
ID2SYM(
id),
2862 return RTEST(ret) && !UNDEF_P(ret);
2871 VALUE defined_class;
2872 const ID resid = idRespond_to;
2875 if (!cme)
return -1;
2876 if (METHOD_ENTRY_BASIC(cme)) {
2889 rb_raise(rb_eArgError,
2890 "respond_to? must accept 1 or 2 arguments (requires %d)",
2899 "%"PRIsVALUE
"%c""respond_to?(:%"PRIsVALUE
") uses"
2900 " the deprecated method signature, which takes one parameter",
2904 if (!
NIL_P(location)) {
2909 RSTRING_PTR(path),
NUM2INT(line),
2910 "respond_to? is defined here");
2915 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args,
RB_NO_KEYWORDS);
2916 return RTEST(result);
2924 return rb_ec_obj_respond_to(ec, obj,
id, priv);
2931 int ret = vm_respond_to(ec, klass, obj,
id, priv);
2932 if (ret == -1) ret = basic_obj_respond_to(ec, obj,
id, !priv);
2964obj_respond_to(
int argc,
VALUE *argv,
VALUE obj)
2972 VALUE ret = basic_obj_respond_to_missing(ec,
CLASS_OF(obj), obj,
2974 if (UNDEF_P(ret)) ret =
Qfalse;
2977 return RBOOL(basic_obj_respond_to(ec, obj,
id, !
RTEST(priv)));
3002Init_eval_method(
void)
3024 "public", top_public, -1);
3026 "private", top_private, -1);
3028 "ruby2_keywords", top_ruby2_keywords, -1);
3031#define REPLICATE_METHOD(klass, id) do { \
3032 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3033 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
#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 FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#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 FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#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.
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().
#define rb_check_frozen
Just another name of rb_check_frozen
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.
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.