10static int vm_redefinition_check_flag(VALUE klass);
11static void rb_vm_check_redefinition_opt_method(
const rb_method_entry_t *me, 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
21#define attached id__attached__
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((VALUE)cme);
132 rb_yjit_constant_state_changed();
133 INC_GLOBAL_CONSTANT_STATE();
137invalidate_negative_cache(ID mid)
142 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
143 rb_id_table_delete(vm->negative_cme_table, mid);
145 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
156clear_method_cache_by_id_in_class(VALUE klass, ID mid)
159 if (rb_objspace_garbage_object_p(klass))
return;
162 if (LIKELY(RCLASS_SUBCLASSES(klass) == NULL)) {
170 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
172 if (
NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
174 rb_id_table_delete(cc_tbl, mid);
175 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
180 if ((cm_tbl = RCLASS_CALLABLE_M_TBL(klass)) != NULL) {
181 rb_id_table_delete(cm_tbl, mid);
182 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
184 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
191 if (METHOD_ENTRY_CACHED(cme)) {
192 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
197 VALUE owner = cme->owner;
199 VALUE klass_housing_cme;
200 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
201 klass_housing_cme = owner;
204 klass_housing_cme = RCLASS_ORIGIN(owner);
207 VM_ASSERT(lookup_method_table(klass_housing_cme, mid) == (
const rb_method_entry_t *)cme);
209 rb_method_table_insert(klass_housing_cme, RCLASS_M_TBL(klass_housing_cme), mid, new_cme);
213 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
215 if (cme->def->iseq_overload) {
218 vm_cme_invalidate(monly_cme);
224 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
225 VALUE defined_class = cme->defined_class;
226 struct rb_id_table *cm_tbl = RCLASS_CALLABLE_M_TBL(defined_class);
227 VM_ASSERT(cm_tbl != NULL);
228 int r = rb_id_table_delete(cm_tbl, mid);
229 VM_ASSERT(r == TRUE); (void)r;
230 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
233 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
236 invalidate_negative_cache(mid);
241 rb_yjit_method_lookup_change(klass, mid);
245clear_iclass_method_cache_by_id(VALUE iclass, VALUE d)
249 clear_method_cache_by_id_in_class(iclass, mid);
253clear_iclass_method_cache_by_id_for_refinements(VALUE klass, VALUE d)
257 clear_method_cache_by_id_in_class(klass, mid);
262rb_clear_method_cache(VALUE klass_or_module, ID mid)
265 VALUE module = klass_or_module;
267 if (
FL_TEST(module, RMODULE_IS_REFINEMENT)) {
268 VALUE refined_class = rb_refinement_module_get_refined_class(module);
269 rb_clear_method_cache(refined_class, mid);
270 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
272 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
275 clear_method_cache_by_id_in_class(klass_or_module, mid);
280void rb_cc_table_free(VALUE klass);
283invalidate_all_cc(
void *vstart,
void *vend,
size_t stride,
void *data)
285 VALUE v = (
VALUE)vstart;
286 for (; v != (
VALUE)vend; v += stride) {
287 void *ptr = asan_poisoned_object_p(v);
288 asan_unpoison_object(v,
false);
292 if (RCLASS_CC_TBL(v)) {
295 RCLASS_CC_TBL(v) = NULL;
299 asan_poison_object(v);
306rb_clear_method_cache_all(
void)
308 rb_objspace_each_objects(invalidate_all_cc, NULL);
310 rb_yjit_invalidate_all_method_lookup_assumptions();
316 VALUE table_owner = klass;
318 table_owner =
RBASIC(table_owner)->klass;
321 VM_ASSERT(table == RCLASS_M_TBL(table_owner));
322 rb_id_table_insert(table, method_id, (VALUE)me);
335rb_define_notimplement_method_id(VALUE mod, ID
id, rb_method_visibility_t visi)
337 rb_add_method(mod,
id, VM_METHOD_TYPE_NOTIMPLEMENTED, (
void *)1, visi);
341rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(
ANYARGS),
int argc, rb_method_visibility_t visi)
343 if (argc < -2 || 15 < argc)
rb_raise(rb_eArgError,
"arity out of range: %d for -2..15", argc);
348 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
351 rb_define_notimplement_method_id(klass, mid, visi);
356rb_add_method_optimized(VALUE klass, ID mid,
enum method_optimized_type opt_type,
unsigned int index, rb_method_visibility_t visi)
362 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
369 const int alias_count = def->alias_count;
370 const int complemented_count = def->complemented_count;
371 VM_ASSERT(alias_count >= 0);
372 VM_ASSERT(complemented_count >= 0);
374 if (alias_count + complemented_count == 0) {
375 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d,%d (remove)\n", (
void *)def,
376 rb_id2name(def->original_id), alias_count, complemented_count);
377 VM_ASSERT(def->type == VM_METHOD_TYPE_BMETHOD ? def->body.bmethod.hooks == NULL : TRUE);
382 VM_ASSERT(def->complemented_count > 0);
383 def->complemented_count--;
385 else if (def->alias_count > 0) {
389 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d->%d,%d->%d (dec)\n", (
void *)def,
rb_id2name(def->original_id),
390 alias_count, def->alias_count, complemented_count, def->complemented_count);
400 if (me->def && me->def->iseq_overload) {
403 rb_method_definition_release(me->def, METHOD_ENTRY_COMPLEMENTED(me));
406static inline rb_method_entry_t *search_method(VALUE klass, ID
id, VALUE *defined_class_ptr);
410(*call_cfunc_invoker_func(
int argc))(VALUE recv,
int argc,
const VALUE *,
VALUE (*func)(
ANYARGS))
412 if (!GET_THREAD()->ext_config.ractor_safe) {
414 case -2:
return &call_cfunc_m2;
415 case -1:
return &call_cfunc_m1;
416 case 0:
return &call_cfunc_0;
417 case 1:
return &call_cfunc_1;
418 case 2:
return &call_cfunc_2;
419 case 3:
return &call_cfunc_3;
420 case 4:
return &call_cfunc_4;
421 case 5:
return &call_cfunc_5;
422 case 6:
return &call_cfunc_6;
423 case 7:
return &call_cfunc_7;
424 case 8:
return &call_cfunc_8;
425 case 9:
return &call_cfunc_9;
426 case 10:
return &call_cfunc_10;
427 case 11:
return &call_cfunc_11;
428 case 12:
return &call_cfunc_12;
429 case 13:
return &call_cfunc_13;
430 case 14:
return &call_cfunc_14;
431 case 15:
return &call_cfunc_15;
433 rb_bug(
"unsupported length: %d", argc);
438 case -2:
return &ractor_safe_call_cfunc_m2;
439 case -1:
return &ractor_safe_call_cfunc_m1;
440 case 0:
return &ractor_safe_call_cfunc_0;
441 case 1:
return &ractor_safe_call_cfunc_1;
442 case 2:
return &ractor_safe_call_cfunc_2;
443 case 3:
return &ractor_safe_call_cfunc_3;
444 case 4:
return &ractor_safe_call_cfunc_4;
445 case 5:
return &ractor_safe_call_cfunc_5;
446 case 6:
return &ractor_safe_call_cfunc_6;
447 case 7:
return &ractor_safe_call_cfunc_7;
448 case 8:
return &ractor_safe_call_cfunc_8;
449 case 9:
return &ractor_safe_call_cfunc_9;
450 case 10:
return &ractor_safe_call_cfunc_10;
451 case 11:
return &ractor_safe_call_cfunc_11;
452 case 12:
return &ractor_safe_call_cfunc_12;
453 case 13:
return &ractor_safe_call_cfunc_13;
454 case 14:
return &ractor_safe_call_cfunc_14;
455 case 15:
return &ractor_safe_call_cfunc_15;
457 rb_bug(
"unsupported length: %d", argc);
467 cfunc->invoker = call_cfunc_invoker_func(argc);
470MJIT_FUNC_EXPORTED
void
477 case VM_METHOD_TYPE_ISEQ:
486 if (iseq->body->mandatory_only_iseq) def->iseq_overload = 1;
488 if (0) vm_cref_dump(
"rb_method_definition_create", cref);
494 method_cref = vm_cref_new_toplevel(GET_EC());
500 case VM_METHOD_TYPE_CFUNC:
503 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
506 case VM_METHOD_TYPE_ATTRSET:
507 case VM_METHOD_TYPE_IVAR:
513 def->body.attr.id = (
ID)(VALUE)opts;
515 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
517 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
522 VM_ASSERT(def->body.attr.location == 0);
526 case VM_METHOD_TYPE_BMETHOD:
528 RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
530 case VM_METHOD_TYPE_NOTIMPLEMENTED:
531 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc),
rb_f_notimplement, -1);
533 case VM_METHOD_TYPE_OPTIMIZED:
536 case VM_METHOD_TYPE_REFINED:
539 RB_OBJ_WRITE(me, &def->body.refined.orig_me, refined->orig_me);
540 RB_OBJ_WRITE(me, &def->body.refined.owner, refined->owner);
543 case VM_METHOD_TYPE_ALIAS:
546 case VM_METHOD_TYPE_ZSUPER:
547 case VM_METHOD_TYPE_UNDEF:
548 case VM_METHOD_TYPE_MISSING:
560 case VM_METHOD_TYPE_ISEQ:
564 case VM_METHOD_TYPE_ATTRSET:
565 case VM_METHOD_TYPE_IVAR:
568 case VM_METHOD_TYPE_BMETHOD:
572 if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((VALUE)me);
574 case VM_METHOD_TYPE_REFINED:
578 case VM_METHOD_TYPE_ALIAS:
581 case VM_METHOD_TYPE_CFUNC:
582 case VM_METHOD_TYPE_ZSUPER:
583 case VM_METHOD_TYPE_MISSING:
584 case VM_METHOD_TYPE_OPTIMIZED:
585 case VM_METHOD_TYPE_UNDEF:
586 case VM_METHOD_TYPE_NOTIMPLEMENTED:
592rb_method_definition_create(rb_method_type_t
type, ID mid)
597 def->original_id = mid;
598 static uintptr_t method_serial = 1;
599 def->method_serial = method_serial++;
607 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d\n", (
void *)def,
rb_id2name(def->original_id), def->alias_count);
614 def->complemented_count++;
615 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d\n", (
void *)def,
rb_id2name(def->original_id), def->complemented_count);
627filter_defined_class(VALUE klass)
639 rb_bug(
"filter_defined_class: %s", rb_obj_info(klass));
643rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi,
const rb_method_definition_t *def)
645 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def);
646 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
647 if (def != NULL) method_definition_reset(me);
654 rb_method_entry_t *me = rb_method_entry_alloc(src_me->called_id, src_me->owner, src_me->defined_class,
655 method_definition_addref(src_me->def));
656 if (METHOD_ENTRY_COMPLEMENTED(src_me)) {
657 method_definition_addref_complement(src_me->def);
660 METHOD_ENTRY_FLAGS_COPY(me, src_me);
665rb_method_entry_complement_defined_class(
const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
674 if (!src_me->defined_class &&
675 def->type == VM_METHOD_TYPE_REFINED &&
676 def->body.refined.orig_me) {
678 rb_method_entry_clone(def->body.refined.orig_me);
679 RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
680 refined.orig_me = orig_me;
681 refined.owner = orig_me->owner;
685 def = method_definition_addref_complement(def);
687 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def);
688 METHOD_ENTRY_FLAGS_COPY(me, src_me);
689 METHOD_ENTRY_COMPLEMENTED_SET(me);
691 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
692 rb_method_definition_set(me, def, &refined);
704 method_definition_reset(dst);
705 dst->called_id = src->called_id;
707 RB_OBJ_WRITE((VALUE)dst, &dst->defined_class, src->defined_class);
708 METHOD_ENTRY_FLAGS_COPY(dst, src);
714 if (me->def->type == VM_METHOD_TYPE_REFINED) {
724 rb_vm_check_redefinition_opt_method(me, me->owner);
727 rb_method_entry_alloc(me->called_id, me->owner,
729 me->defined_class : owner,
730 method_definition_addref(me->def));
731 METHOD_ENTRY_FLAGS_COPY(refined.orig_me, me);
732 refined.owner = owner;
734 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
735 rb_method_definition_set(me, def, (
void *)&refined);
736 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
741lookup_method_table(VALUE klass, ID
id)
746 if (rb_id_table_lookup(m_tbl,
id, &body)) {
755rb_add_refined_method_entry(VALUE refined_class, ID mid)
760 make_method_entry_refined(refined_class, me);
761 rb_clear_method_cache(refined_class, mid);
764 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
769check_override_opt_method_i(VALUE klass, VALUE arg)
774 if (vm_redefinition_check_flag(klass)) {
775 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
777 newme = rb_method_entry(klass, mid);
778 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
781 rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
785check_override_opt_method(VALUE klass, VALUE mid)
787 if (rb_vm_check_optimizable_mid(mid)) {
788 check_override_opt_method_i(klass, mid);
799rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
805 int make_refined = 0;
814 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
815 type != VM_METHOD_TYPE_ZSUPER) {
818 case idInitialize_copy:
819 case idInitialize_clone:
820 case idInitialize_dup:
821 case idRespond_to_missing:
822 visi = METHOD_VISI_PRIVATE;
826 if (
type != VM_METHOD_TYPE_REFINED) {
830 if (
FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
831 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
832 rb_add_refined_method_entry(refined_class, mid);
834 if (
type == VM_METHOD_TYPE_REFINED) {
836 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
839 klass = RCLASS_ORIGIN(klass);
840 if (klass != orig_klass) {
841 rb_clear_method_cache(orig_klass, mid);
844 mtbl = RCLASS_M_TBL(klass);
847 if (rb_id_table_lookup(mtbl, mid, &data)) {
851 if (rb_method_definition_eq(old_def, def))
return old_me;
852 rb_vm_check_redefinition_opt_method(old_me, klass);
854 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
857 type != VM_METHOD_TYPE_UNDEF &&
858 (old_def->alias_count == 0) &&
859 (!old_def->no_redef_warning) &&
861 old_def->type != VM_METHOD_TYPE_UNDEF &&
862 old_def->type != VM_METHOD_TYPE_ZSUPER &&
863 old_def->type != VM_METHOD_TYPE_ALIAS) {
867 switch (old_def->type) {
868 case VM_METHOD_TYPE_ISEQ:
869 iseq = def_iseq_ptr(old_def);
871 case VM_METHOD_TYPE_BMETHOD:
872 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
879 FIX2INT(iseq->body->location.first_lineno),
880 "previous definition of %"PRIsVALUE
" was here",
887 me = rb_method_entry_create(mid, defined_class, visi, NULL);
888 if (def == NULL) def = rb_method_definition_create(
type, original_id);
889 rb_method_definition_set(me, def, opts);
891 rb_clear_method_cache(klass, mid);
894 if (klass == rb_cObject) {
897 case idRespond_to_missing:
898 case idMethodMissing:
904 if (mid == object_id || mid == id__send__) {
905 if (
type == VM_METHOD_TYPE_ISEQ && search_method(klass, mid, 0)) {
911 make_method_entry_refined(klass, me);
914 rb_method_table_insert(klass, mtbl, mid, me);
916 VM_ASSERT(me->def != NULL);
920 check_override_opt_method(klass, (VALUE)mid);
929overloaded_cme_table(
void)
931 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
932 return GET_VM()->overloaded_cme_table;
937vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
939 fprintf(stderr,
"key: "); rp(key);
940 fprintf(stderr,
"val: "); rp(val);
945rb_vm_dump_overloaded_cme_table(
void)
947 fprintf(stderr,
"== rb_vm_dump_overloaded_cme_table\n");
948 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
953lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
960 if (rb_objspace_garbage_object_p((VALUE)cme) ||
961 rb_objspace_garbage_object_p((VALUE)monly_cme)) {
979 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
987 return lookup_overloaded_cme(cme);
995 st_delete(overloaded_cme_table(), (st_data_t *)&cme, NULL);
1003 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1009 def->body.iseq.
cref = cme->def->body.iseq.
cref;
1010 def->body.iseq.
iseqptr = cme->def->body.iseq.
iseqptr->body->mandatory_only_iseq;
1017 ASSERT_vm_locking();
1018 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1020 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1028 if (UNLIKELY(cme->def->iseq_overload) &&
1029 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1030 (
int)vm_ci_argc(ci) == method_entry_iseqptr(cme)->body->param.lead_num) {
1031 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ);
1033 cme = get_overloaded_cme(cme);
1035 VM_ASSERT(cme != NULL);
1042#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1043 const VALUE arg = ID2SYM(mid); \
1044 VALUE recv_class = (klass); \
1045 ID hook_id = (hook); \
1046 if (FL_TEST((klass), FL_SINGLETON)) { \
1047 recv_class = rb_ivar_get((klass), attached); \
1048 hook_id = singleton_##hook; \
1050 rb_funcallv(recv_class, hook_id, 1, &arg); \
1054method_added(VALUE klass, ID mid)
1057 CALL_METHOD_HOOK(klass, added, mid);
1062rb_add_method(VALUE klass, ID mid, rb_method_type_t
type,
void *opts, rb_method_visibility_t visi)
1064 rb_method_entry_make(klass, mid, klass, visi,
type, NULL, mid, opts);
1066 if (
type != VM_METHOD_TYPE_UNDEF &&
type != VM_METHOD_TYPE_REFINED) {
1067 method_added(klass, mid);
1071MJIT_FUNC_EXPORTED
void
1072rb_add_method_iseq(VALUE klass, ID mid,
const rb_iseq_t *iseq,
rb_cref_t *cref, rb_method_visibility_t visi)
1080 iseq_body.
cref = cref;
1082 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1087 rb_method_visibility_t visi, VALUE defined_class)
1089 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
1090 me->def->type, me->def, 0, NULL);
1092 me->def->no_redef_warning = TRUE;
1095 method_definition_addref(me->def);
1097 method_added(klass, mid);
1102rb_method_entry_set(VALUE klass, ID mid,
const rb_method_entry_t *me, rb_method_visibility_t visi)
1104 return method_entry_set(klass, mid, me, visi, klass);
1107#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1113 RCLASS_ALLOCATOR(klass) = func;
1129 if (allocator == UNDEF_ALLOC_FUNC)
break;
1130 if (allocator)
return allocator;
1136rb_method_entry_at(VALUE klass, ID
id)
1138 return lookup_method_table(klass,
id);
1142search_method0(VALUE klass, ID
id, VALUE *defined_class_ptr,
bool skip_refined)
1146 RB_DEBUG_COUNTER_INC(mc_search);
1149 RB_DEBUG_COUNTER_INC(mc_search_super);
1150 if ((me = lookup_method_table(klass,
id)) != 0) {
1151 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1152 me->def->body.refined.orig_me) {
1158 if (defined_class_ptr) *defined_class_ptr = klass;
1160 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1162 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me));
1167search_method(VALUE klass, ID
id, VALUE *defined_class_ptr)
1169 return search_method0(klass,
id, defined_class_ptr,
false);
1173search_method_protect(VALUE klass, ID
id, VALUE *defined_class_ptr)
1177 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1186rb_method_entry(VALUE klass, ID
id)
1188 return search_method_protect(klass,
id, NULL);
1192prepare_callable_method_entry(VALUE defined_class, ID
id,
const rb_method_entry_t *
const me,
int create)
1199 if (me->defined_class == 0) {
1200 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1202 VM_ASSERT(me->defined_class == 0);
1204 mtbl = RCLASS_CALLABLE_M_TBL(defined_class);
1206 if (mtbl && rb_id_table_lookup(mtbl,
id, &cme_data)) {
1208 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1209 VM_ASSERT(callable_method_entry_p(cme));
1210 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1214 mtbl = RCLASS_EXT(defined_class)->callable_m_tbl = rb_id_table_create(0);
1216 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1217 rb_id_table_insert(mtbl,
id, (VALUE)cme);
1219 VM_ASSERT(callable_method_entry_p(cme));
1227 VM_ASSERT(callable_method_entry_p(cme));
1228 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1238complemented_callable_method_entry(VALUE klass, ID
id)
1240 VALUE defined_class;
1242 return prepare_callable_method_entry(defined_class,
id, me, FALSE);
1246cached_callable_method_entry(VALUE klass, ID mid)
1248 ASSERT_vm_locking();
1250 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1253 if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1255 VM_ASSERT(vm_ccs_p(ccs));
1257 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1258 VM_ASSERT(ccs->cme->called_id == mid);
1259 RB_DEBUG_COUNTER_INC(ccs_found);
1263 rb_vm_ccs_free(ccs);
1264 rb_id_table_delete(cc_tbl, mid);
1268 RB_DEBUG_COUNTER_INC(ccs_not_found);
1275 ASSERT_vm_locking();
1276 VM_ASSERT(cme != NULL);
1278 struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
1283 cc_tbl = RCLASS_CC_TBL(klass) = rb_id_table_create(2);
1286 if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1288 VM_ASSERT(ccs->cme == cme);
1291 ccs = vm_ccs_create(klass, cme);
1292 rb_id_table_insert(cc_tbl, mid, (VALUE)ccs);
1303 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1308 rb_id_table_insert(vm->negative_cme_table, mid, (VALUE)cme);
1311 VM_ASSERT(cme != NULL);
1316callable_method_entry(VALUE klass, ID mid, VALUE *defined_class_ptr)
1323 cme = cached_callable_method_entry(klass, mid);
1326 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1329 VALUE defined_class;
1331 if (defined_class_ptr) *defined_class_ptr = defined_class;
1334 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1337 cme = negative_cme(mid);
1340 cache_callable_method_entry(klass, mid, cme);
1345 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1349rb_callable_method_entry(VALUE klass, ID mid)
1351 return callable_method_entry(klass, mid, NULL);
1357method_entry_resolve_refinement(VALUE klass, ID
id,
int with_refinement, VALUE *defined_class_ptr)
1359 const rb_method_entry_t *me = search_method_protect(klass,
id, defined_class_ptr);
1362 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1363 if (with_refinement) {
1365 VALUE refinements = cref ? CREF_REFINEMENTS(cref) :
Qnil;
1366 me = resolve_refined_method(refinements, me, defined_class_ptr);
1369 me = resolve_refined_method(
Qnil, me, defined_class_ptr);
1372 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1380rb_method_entry_with_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr)
1382 return method_entry_resolve_refinement(klass,
id, TRUE, defined_class_ptr);
1386callable_method_entry_refeinements0(VALUE klass, ID
id, VALUE *defined_class_ptr,
bool with_refinements,
1389 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
1393 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1394 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, with_refinements, dcp);
1395 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1400callable_method_entry_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr,
bool with_refinements)
1403 return callable_method_entry_refeinements0(klass,
id, defined_class_ptr, with_refinements, cme);
1407rb_callable_method_entry_with_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr)
1409 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
true);
1413callable_method_entry_without_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr)
1415 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
false);
1419rb_method_entry_without_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr)
1421 return method_entry_resolve_refinement(klass,
id, FALSE, defined_class_ptr);
1425rb_callable_method_entry_without_refinements(VALUE klass, ID
id, VALUE *defined_class_ptr)
1427 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1428 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, FALSE, dcp);
1429 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
1433resolve_refined_method(VALUE refinements,
const rb_method_entry_t *me, VALUE *defined_class_ptr)
1435 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1440 refinement = find_refinement(refinements, me->owner);
1441 if (!
NIL_P(refinement)) {
1442 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
1444 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
1449 tmp_me = me->def->body.refined.orig_me;
1451 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
1460 me = search_method_protect(super, me->called_id, defined_class_ptr);
1468 return resolve_refined_method(refinements, me, NULL);
1475 VALUE defined_class = me->defined_class;
1478 if (resolved_me && resolved_me->defined_class == 0) {
1479 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1487remove_method(VALUE klass, ID mid)
1493 klass = RCLASS_ORIGIN(klass);
1495 if (mid == object_id || mid == id__send__ || mid == idInitialize) {
1499 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
1501 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
1502 UNDEFINED_REFINED_METHOD_P(me->def)) {
1503 rb_name_err_raise(
"method `%1$s' not defined in %2$s",
1507 if (klass !=
self) {
1508 rb_clear_method_cache(
self, mid);
1510 rb_clear_method_cache(klass, mid);
1511 rb_id_table_delete(RCLASS_M_TBL(klass), mid);
1513 rb_vm_check_redefinition_opt_method(me, klass);
1515 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1516 rb_add_refined_method_entry(klass, mid);
1519 CALL_METHOD_HOOK(
self, removed, mid);
1525 remove_method(klass, mid);
1545rb_mod_remove_method(
int argc, VALUE *argv, VALUE mod)
1549 for (i = 0; i < argc; i++) {
1553 rb_name_err_raise(
"method `%1$s' not defined in %2$s",
1556 remove_method(mod,
id);
1562rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
1565 VALUE defined_class;
1566 VALUE origin_class = RCLASS_ORIGIN(klass);
1568 me = search_method0(origin_class, name, &defined_class,
true);
1571 me = search_method(rb_cObject, name, &defined_class);
1574 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1575 UNDEFINED_REFINED_METHOD_P(me->def)) {
1576 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
1579 if (METHOD_ENTRY_VISI(me) != visi) {
1580 rb_vm_check_redefinition_opt_method(me, klass);
1582 if (klass == defined_class || origin_class == defined_class) {
1583 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1586 if (me->def->body.refined.orig_me) {
1587 METHOD_ENTRY_VISI_SET((
rb_method_entry_t *)me->def->body.refined.orig_me, visi);
1591 METHOD_ENTRY_VISI_SET(me, visi);
1593 rb_clear_method_cache(klass, name);
1596 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
1601#define BOUND_PRIVATE 0x01
1602#define BOUND_RESPONDS 0x02
1605method_boundp(VALUE klass, ID
id,
int ex)
1611 if (ex & BOUND_RESPONDS) {
1612 cme = rb_callable_method_entry_with_refinements(klass,
id, NULL);
1615 cme = callable_method_entry_without_refinements(klass,
id, NULL);
1619 if (ex & ~BOUND_RESPONDS) {
1620 switch (METHOD_ENTRY_VISI(cme)) {
1621 case METHOD_VISI_PRIVATE:
1623 case METHOD_VISI_PROTECTED:
1624 if (ex & BOUND_RESPONDS)
return 0;
1630 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
1631 if (ex & BOUND_RESPONDS)
return 2;
1643 return method_boundp(klass,
id, ex);
1647vm_cref_set_visibility(rb_method_visibility_t method_visi,
int module_func)
1650 scope_visi->method_visi = method_visi;
1651 scope_visi->module_func = module_func;
1655rb_scope_visibility_set(rb_method_visibility_t visi)
1657 vm_cref_set_visibility(visi, FALSE);
1661scope_visibility_check(
void)
1665 if (cfp && cfp->iseq && cfp->iseq->body->type == ISEQ_TYPE_METHOD) {
1666 rb_warn(
"calling %s without arguments inside a method may not have the intended effect",
1672rb_scope_module_func_set(
void)
1674 scope_visibility_check();
1675 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
1678const rb_cref_t *rb_vm_cref_in_context(VALUE
self, VALUE cbase);
1680rb_attr(VALUE klass, ID
id,
int read,
int write,
int ex)
1683 rb_method_visibility_t visi;
1685 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
1688 visi = METHOD_VISI_PUBLIC;
1691 switch (vm_scope_visibility_get(ec)) {
1692 case METHOD_VISI_PRIVATE:
1693 if (vm_scope_module_func_check(ec)) {
1694 rb_warning(
"attribute accessor as module_function");
1696 visi = METHOD_VISI_PRIVATE;
1698 case METHOD_VISI_PROTECTED:
1699 visi = METHOD_VISI_PROTECTED;
1702 visi = METHOD_VISI_PUBLIC;
1709 rb_add_method(klass,
id, VM_METHOD_TYPE_IVAR, (
void *)attriv, visi);
1712 rb_add_method(klass,
rb_id_attrset(
id), VM_METHOD_TYPE_ATTRSET, (
void *)attriv, visi);
1722 rb_raise(rb_eTypeError,
"no class to undef method");
1725 if (
id == object_id ||
id == id__send__ ||
id == idInitialize) {
1729 me = search_method(klass,
id, 0);
1730 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1731 me = rb_resolve_refined_method(
Qnil, me);
1734 if (UNDEFINED_METHOD_ENTRY_P(me) ||
1735 UNDEFINED_REFINED_METHOD_P(me->def)) {
1736 rb_method_name_error(klass,
rb_id2str(
id));
1739 rb_add_method(klass,
id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
1741 CALL_METHOD_HOOK(klass, undefined,
id);
1790rb_mod_undef_method(
int argc, VALUE *argv, VALUE mod)
1793 for (i = 0; i < argc; i++) {
1797 rb_method_name_error(mod, v);
1804static rb_method_visibility_t
1805check_definition_visibility(VALUE mod,
int argc, VALUE *argv)
1808 VALUE mid, include_super, lookup_mod = mod;
1814 if (!
id)
return METHOD_VISI_UNDEF;
1820 inc_super =
RTEST(include_super);
1822 lookup_mod = RCLASS_ORIGIN(mod);
1826 me = rb_method_entry_without_refinements(lookup_mod,
id, NULL);
1828 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED)
return METHOD_VISI_UNDEF;
1829 if (!inc_super && me->owner != mod)
return METHOD_VISI_UNDEF;
1830 return METHOD_ENTRY_VISI(me);
1832 return METHOD_VISI_UNDEF;
1872rb_mod_method_defined(
int argc, VALUE *argv, VALUE mod)
1874 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
1875 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
1879check_definition(VALUE mod,
int argc, VALUE *argv, rb_method_visibility_t visi)
1881 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
1915rb_mod_public_method_defined(
int argc, VALUE *argv, VALUE mod)
1917 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
1951rb_mod_private_method_defined(
int argc, VALUE *argv, VALUE mod)
1953 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
1987rb_mod_protected_method_defined(
int argc, VALUE *argv, VALUE mod)
1989 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
1995 return rb_method_definition_eq(m1->def, m2->def);
2003 switch (def->type) {
2004 case VM_METHOD_TYPE_REFINED:
2005 if (def->body.refined.orig_me) {
2006 def = def->body.refined.orig_me->def;
2010 case VM_METHOD_TYPE_ALIAS:
2011 def = def->body.alias.original_me->def;
2020MJIT_FUNC_EXPORTED
int
2023 d1 = original_method_definition(d1);
2024 d2 = original_method_definition(d2);
2026 if (d1 == d2)
return 1;
2027 if (!d1 || !d2)
return 0;
2028 if (d1->type != d2->type)
return 0;
2031 case VM_METHOD_TYPE_ISEQ:
2032 return d1->body.iseq.iseqptr == d2->body.iseq.
iseqptr;
2033 case VM_METHOD_TYPE_CFUNC:
2035 d1->body.cfunc.func == d2->body.cfunc.func &&
2036 d1->body.cfunc.argc == d2->body.cfunc.argc;
2037 case VM_METHOD_TYPE_ATTRSET:
2038 case VM_METHOD_TYPE_IVAR:
2039 return d1->body.attr.id == d2->body.attr.id;
2040 case VM_METHOD_TYPE_BMETHOD:
2041 return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2042 case VM_METHOD_TYPE_MISSING:
2043 return d1->original_id == d2->original_id;
2044 case VM_METHOD_TYPE_ZSUPER:
2045 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2046 case VM_METHOD_TYPE_UNDEF:
2048 case VM_METHOD_TYPE_OPTIMIZED:
2049 return (d1->body.optimized.type == d2->body.optimized.type) &&
2050 (d1->body.optimized.index == d2->body.optimized.index);
2051 case VM_METHOD_TYPE_REFINED:
2052 case VM_METHOD_TYPE_ALIAS:
2055 rb_bug(
"rb_method_definition_eq: unsupported type: %d\n", d1->type);
2062 def = original_method_definition(def);
2064 if (!def)
return hash;
2066 switch (def->type) {
2067 case VM_METHOD_TYPE_ISEQ:
2069 case VM_METHOD_TYPE_CFUNC:
2070 hash =
rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2072 case VM_METHOD_TYPE_ATTRSET:
2073 case VM_METHOD_TYPE_IVAR:
2075 case VM_METHOD_TYPE_BMETHOD:
2076 return rb_hash_proc(hash, def->body.bmethod.proc);
2077 case VM_METHOD_TYPE_MISSING:
2079 case VM_METHOD_TYPE_ZSUPER:
2080 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2081 case VM_METHOD_TYPE_UNDEF:
2083 case VM_METHOD_TYPE_OPTIMIZED:
2086 case VM_METHOD_TYPE_REFINED:
2087 case VM_METHOD_TYPE_ALIAS:
2090 rb_bug(
"rb_hash_method_definition: unsupported method type (%d)\n", def->type);
2096 return rb_hash_method_definition(hash, me->def);
2102 const VALUE target_klass = klass;
2103 VALUE defined_class;
2105 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2108 rb_raise(rb_eTypeError,
"no class to make alias");
2114 orig_me = search_method(klass, original_name, &defined_class);
2116 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2117 orig_me = rb_resolve_refined_method(
Qnil, orig_me);
2120 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2121 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2123 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2124 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2125 rb_print_undef(klass, original_name, METHOD_VISI_UNDEF);
2129 switch (orig_me->def->type) {
2130 case VM_METHOD_TYPE_ZSUPER:
2132 original_name = orig_me->def->original_id;
2133 visi = METHOD_ENTRY_VISI(orig_me);
2135 case VM_METHOD_TYPE_ALIAS:
2136 visi = METHOD_ENTRY_VISI(orig_me);
2137 orig_me = orig_me->def->body.alias.original_me;
2138 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2143 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2145 if (orig_me->defined_class == 0) {
2146 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2147 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2148 (
void *)rb_method_entry_clone(orig_me));
2149 method_added(target_klass, alias_name);
2154 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2155 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2156 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2183rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
2187 rb_print_undef_str(mod, oldname);
2195check_and_export_method(VALUE
self, VALUE name, rb_method_visibility_t visi)
2199 rb_print_undef_str(
self, name);
2201 rb_export_method(
self,
id, visi);
2205set_method_visibility(VALUE
self,
int argc,
const VALUE *argv, rb_method_visibility_t visi)
2211 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
2223 check_and_export_method(
self,
RARRAY_AREF(v, j), visi);
2227 for (i = 0; i < argc; i++) {
2228 check_and_export_method(
self, argv[i], visi);
2234set_visibility(
int argc,
const VALUE *argv, VALUE module, rb_method_visibility_t visi)
2237 scope_visibility_check();
2238 rb_scope_visibility_set(visi);
2242 set_method_visibility(module, argc, argv, visi);
2267rb_mod_public(
int argc, VALUE *argv, VALUE module)
2269 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2299rb_mod_protected(
int argc, VALUE *argv, VALUE module)
2301 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2333rb_mod_private(
int argc, VALUE *argv, VALUE module)
2335 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
2375rb_mod_ruby2_keywords(
int argc, VALUE *argv, VALUE module)
2378 VALUE origin_class = RCLASS_ORIGIN(module);
2383 for (i = 0; i < argc; i++) {
2387 VALUE defined_class;
2390 rb_print_undef_str(module, v);
2393 me = search_method(origin_class, name, &defined_class);
2395 me = search_method(rb_cObject, name, &defined_class);
2398 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2399 UNDEFINED_REFINED_METHOD_P(me->def)) {
2400 rb_print_undef(module, name, METHOD_VISI_UNDEF);
2403 if (module == defined_class || origin_class == defined_class) {
2404 switch (me->def->type) {
2405 case VM_METHOD_TYPE_ISEQ:
2406 if (me->def->body.iseq.
iseqptr->body->param.flags.has_rest &&
2407 !me->def->body.iseq.
iseqptr->body->param.flags.has_kw &&
2408 !me->def->body.iseq.
iseqptr->body->param.flags.has_kwrest) {
2409 me->def->body.iseq.
iseqptr->body->param.flags.ruby2_keywords = 1;
2410 rb_clear_method_cache(module, name);
2413 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)",
rb_id2name(name));
2416 case VM_METHOD_TYPE_BMETHOD: {
2417 VALUE procval = me->def->body.bmethod.proc;
2418 if (vm_block_handler_type(procval) == block_handler_type_proc) {
2419 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
2422 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
2424 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
2425 if (iseq->body->param.flags.has_rest &&
2426 !iseq->body->param.flags.has_kw &&
2427 !iseq->body->param.flags.has_kwrest) {
2428 iseq->body->param.flags.ruby2_keywords = 1;
2429 rb_clear_method_cache(module, name);
2432 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)",
rb_id2name(name));
2439 rb_warn(
"Skipping set of ruby2_keywords flag for %s (method not defined in Ruby)",
rb_id2name(name));
2444 rb_warn(
"Skipping set of ruby2_keywords flag for %s (can only set in method defining module)",
rb_id2name(name));
2463rb_mod_public_method(
int argc, VALUE *argv, VALUE obj)
2465 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
2491rb_mod_private_method(
int argc, VALUE *argv, VALUE obj)
2493 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
2513top_public(
int argc, VALUE *argv, VALUE
_)
2515 return rb_mod_public(argc, argv, rb_cObject);
2533top_private(
int argc, VALUE *argv, VALUE
_)
2535 return rb_mod_private(argc, argv, rb_cObject);
2546top_ruby2_keywords(
int argc, VALUE *argv, VALUE module)
2548 return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
2594rb_mod_modfunc(
int argc, VALUE *argv, VALUE module)
2601 rb_raise(rb_eTypeError,
"module_function must be called for modules");
2605 rb_scope_module_func_set();
2609 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
2611 for (i = 0; i < argc; i++) {
2616 me = search_method(m,
id, 0);
2618 me = search_method(rb_cObject,
id, 0);
2620 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2621 rb_print_undef(module,
id, METHOD_VISI_UNDEF);
2623 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
2630 rb_method_entry_set(rb_singleton_class(module),
id, me, METHOD_VISI_PUBLIC);
2639#pragma push_macro("rb_method_basic_definition_p")
2640#undef rb_method_basic_definition_p
2646 if (!klass)
return TRUE;
2647 cme = rb_callable_method_entry(klass,
id);
2648 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
2651#pragma pop_macro("rb_method_basic_definition_p")
2658 VALUE passed_block_handler = vm_passed_block_handler(ec);
2659 VALUE result = rb_vm_call_kw(ec, obj,
id, argc, argv, cme, kw_splat);
2660 vm_passed_block_handler_set(ec, passed_block_handler);
2666 VALUE mid, VALUE priv)
2668 VALUE defined_class, args[2];
2669 const ID rtmid = idRespond_to_missing;
2672 if (!cme || METHOD_ENTRY_BASIC(cme))
return Qundef;
2675 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args,
RB_NO_KEYWORDS);
2684 switch (method_boundp(klass,
id, pub|BOUND_RESPONDS)) {
2688 ret = basic_obj_respond_to_missing(ec, klass, obj,
ID2SYM(
id),
2699 VALUE defined_class;
2700 const ID resid = idRespond_to;
2703 if (!cme)
return -1;
2704 if (METHOD_ENTRY_BASIC(cme)) {
2718 "respond_to? must accept 1 or 2 arguments (requires %d)",
2727 "%"PRIsVALUE
"%c""respond_to?(:%"PRIsVALUE
") uses"
2728 " the deprecated method signature, which takes one parameter",
2732 if (!
NIL_P(location)) {
2738 "respond_to? is defined here");
2743 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args,
RB_NO_KEYWORDS);
2744 return RTEST(result);
2752 return rb_ec_obj_respond_to(ec, obj,
id, priv);
2759 int ret = vm_respond_to(ec, klass, obj,
id, priv);
2760 if (ret == -1) ret = basic_obj_respond_to(ec, obj,
id, !priv);
2792obj_respond_to(
int argc, VALUE *argv, VALUE obj)
2800 VALUE ret = basic_obj_respond_to_missing(ec,
CLASS_OF(obj), obj,
2805 return RBOOL(basic_obj_respond_to(ec, obj,
id, !
RTEST(priv)));
2824obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
2836Init_eval_method(
void)
2839 rb_define_method(rb_mKernel,
"respond_to_missing?", obj_respond_to_missing, 2);
2850 rb_define_method(rb_cModule,
"method_defined?", rb_mod_method_defined, -1);
2851 rb_define_method(rb_cModule,
"public_method_defined?", rb_mod_public_method_defined, -1);
2852 rb_define_method(rb_cModule,
"private_method_defined?", rb_mod_private_method_defined, -1);
2853 rb_define_method(rb_cModule,
"protected_method_defined?", rb_mod_protected_method_defined, -1);
2854 rb_define_method(rb_cModule,
"public_class_method", rb_mod_public_method, -1);
2855 rb_define_method(rb_cModule,
"private_class_method", rb_mod_private_method, -1);
2858 "public", top_public, -1);
2860 "private", top_private, -1);
2862 "ruby2_keywords", top_ruby2_keywords, -1);
2865#define REPLICATE_METHOD(klass, id) do { \
2866 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
2867 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
2870 REPLICATE_METHOD(rb_eException, idMethodMissing);
2871 REPLICATE_METHOD(rb_eException, idRespond_to);
2872 REPLICATE_METHOD(rb_eException, idRespond_to_missing);
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
void rb_class_modify_check(VALUE)
Asserts that klass is not a frozen class.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define 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 FIX2INT
Old name of RB_FIX2INT.
#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 cat, const char *fmt,...)
Identical to rb_category_warning(), except it reports always regardless of runtime -W flag.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
void rb_category_compile_warn(rb_warning_category_t cat, 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 always regardless of runtime -W flag.
void rb_compile_warning(const char *file, int line, const char *fmt,...)
Issues a compile-time warning that happens at __file__:__line__.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
#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)
Just another name of rb_obj_freeze.
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.
ID rb_frame_callee(void)
Identical to rb_frame_this_func(), except it returns the named used to call the method.
ID rb_frame_this_func(void)
Queries the name of the Ruby level method that is calling this function.
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
void rb_clear_constant_cache(void)
Clears the constant cache.
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_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.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
rb_cref_t * cref
class reference, should be marked
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.