10static int vm_redefinition_check_flag(
VALUE klass);
11static void rb_vm_check_redefinition_opt_method(
const rb_method_entry_t *me,
VALUE klass);
12static inline rb_method_entry_t *lookup_method_table(
VALUE klass,
ID id);
14#define object_id idObject_id
15#define added idMethod_added
16#define singleton_added idSingleton_method_added
17#define removed idMethod_removed
18#define singleton_removed idSingleton_method_removed
19#define undefined idMethod_undefined
20#define singleton_undefined idSingleton_method_undefined
22#define ruby_running (GET_VM()->running)
25static enum rb_id_table_iterator_result
26mark_cc_entry_i(
VALUE ccs_ptr,
void *data)
30 VM_ASSERT(vm_ccs_p(ccs));
32 if (METHOD_ENTRY_INVALIDATED(ccs->cme)) {
36 for (
int i = 0; i < ccs->len; i++) {
38 if (cc->klass ==
Qundef)
continue;
39 VM_ASSERT(cc->klass ==
Qundef || vm_cc_check_cme(cc, ccs->cme));
40 VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
44 return ID_TABLE_DELETE;
47 rb_gc_mark_movable((
VALUE)ccs->cme);
49 for (
int i = 0; i < ccs->len; i++) {
51 VM_ASSERT(cc->klass ==
Qundef || vm_cc_check_cme(cc, ccs->cme));
53 rb_gc_mark_movable((
VALUE)cc);
55 return ID_TABLE_CONTINUE;
60vm_cc_table_mark(
void *data)
64 rb_id_table_foreach_values(tbl, mark_cc_entry_i, NULL);
68static enum rb_id_table_iterator_result
69cc_table_free_i(
VALUE ccs_ptr,
void *data)
72 VM_ASSERT(vm_ccs_p(ccs));
76 return ID_TABLE_CONTINUE;
80vm_cc_table_free(
void *data)
84 rb_id_table_foreach_values(tbl, cc_table_free_i, NULL);
85 rb_managed_id_table_type.function.dfree(data);
88static enum rb_id_table_iterator_result
89cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
91 size_t *total_size = data_ptr;
93 *total_size +=
sizeof(*ccs);
94 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
95 return ID_TABLE_CONTINUE;
99vm_cc_table_memsize(
const void *data)
101 size_t memsize = rb_managed_id_table_type.function.dsize(data);
103 rb_id_table_foreach_values(tbl, cc_table_memsize_i, &memsize);
107static enum rb_id_table_iterator_result
108compact_cc_entry_i(
VALUE ccs_ptr,
void *data)
113 VM_ASSERT(vm_ccs_p(ccs));
115 for (
int i=0; i<ccs->len; i++) {
116 ccs->entries[i].cc = (
const struct rb_callcache *)rb_gc_location((
VALUE)ccs->entries[i].cc);
119 return ID_TABLE_CONTINUE;
123vm_cc_table_compact(
void *data)
126 rb_id_table_foreach_values(tbl, compact_cc_entry_i, NULL);
130 .wrap_struct_name =
"VM/cc_table",
132 .dmark = vm_cc_table_mark,
133 .dfree = vm_cc_table_free,
134 .dsize = vm_cc_table_memsize,
135 .dcompact = vm_cc_table_compact,
137 .parent = &rb_managed_id_table_type,
138 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
142rb_vm_cc_table_create(
size_t capa)
144 return rb_managed_id_table_create(&cc_table_type,
capa);
147static enum rb_id_table_iterator_result
148vm_cc_table_dup_i(
ID key,
VALUE old_ccs_ptr,
void *data)
153 if (METHOD_ENTRY_INVALIDATED(old_ccs->cme)) {
156 return ID_TABLE_CONTINUE;
159 size_t memsize = vm_ccs_alloc_size(old_ccs->capa);
161 rb_managed_id_table_insert(new_table, key, (
VALUE)new_ccs);
165 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(old_ccs->cme));
167 memcpy(new_ccs, old_ccs, memsize);
170 new_ccs->debug_sig = ~(
VALUE)new_ccs;
174 for (
int index = 0; index < new_ccs->len; index++) {
177 return ID_TABLE_CONTINUE;
181rb_vm_cc_table_dup(
VALUE old_table)
184 VALUE new_table = rb_vm_cc_table_create(rb_managed_id_table_size(old_table));
185 rb_managed_id_table_foreach(old_table, vm_cc_table_dup_i, (
void *)new_table);
192 for (
int i=0; i<ccs->len; i++) {
194 VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
195 vm_cc_invalidate(cc);
202 RB_DEBUG_COUNTER_INC(ccs_free);
203 vm_ccs_invalidate(ccs);
208rb_vm_cc_table_delete(
VALUE table,
ID mid)
211 if (rb_managed_id_table_lookup(table, mid, &ccs_obj)) {
213 rb_managed_id_table_delete(table, mid);
214 rb_vm_ccs_invalidate_and_free(ccs);
218static enum rb_id_table_iterator_result
219vm_ccs_dump_i(
ID mid,
VALUE val,
void *data)
222 fprintf(stderr,
" | %s (len:%d) ", rb_id2name(mid), ccs->len);
225 for (
int i=0; i<ccs->len; i++) {
226 rp_m(
" | \t", ccs->entries[i].cc);
229 return ID_TABLE_CONTINUE;
233vm_ccs_dump(
VALUE klass,
ID target_mid)
235 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
239 if (rb_managed_id_table_lookup(cc_tbl, target_mid, &ccs)) {
240 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
241 vm_ccs_dump_i(target_mid, ccs, NULL);
245 fprintf(stderr,
" [CCTB] %p\n", (
void *)cc_tbl);
246 rb_managed_id_table_foreach(cc_tbl, vm_ccs_dump_i, (
void *)target_mid);
251static enum rb_id_table_iterator_result
252vm_cme_dump_i(
ID mid,
VALUE val,
void *data)
254 ID target_mid = (
ID)data;
255 if (target_mid == 0 || mid == target_mid) {
258 return ID_TABLE_CONTINUE;
262vm_mtbl_dump(
VALUE klass,
ID target_mid)
264 fprintf(stderr,
"# vm_mtbl\n");
269 if (RCLASS_M_TBL(klass)) {
270 if (target_mid != 0) {
271 if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
272 rp_m(
" [MTBL] ", me);
276 fprintf(stderr,
" ## RCLASS_M_TBL (%p)\n", (
void *)RCLASS_M_TBL(klass));
277 rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
281 fprintf(stderr,
" MTBL: NULL\n");
283 if (RCLASS_WRITABLE_CALLABLE_M_TBL(klass)) {
284 if (target_mid != 0) {
285 if (rb_id_table_lookup(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), target_mid, &me)) {
286 rp_m(
" [CM**] ", me);
290 fprintf(stderr,
" ## RCLASS_CALLABLE_M_TBL\n");
291 rb_id_table_foreach(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
294 if (RCLASS_WRITABLE_CC_TBL(klass)) {
295 vm_ccs_dump(klass, target_mid);
303rb_vm_mtbl_dump(
const char *msg,
VALUE klass,
ID target_mid)
305 fprintf(stderr,
"[%s] ", msg);
306 vm_mtbl_dump(klass, target_mid);
310vm_cme_invalidate(rb_callable_method_entry_t *cme)
312 VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment),
"cme: %d", imemo_type((
VALUE)cme));
313 VM_ASSERT(callable_method_entry_p(cme));
314 METHOD_ENTRY_INVALIDATED_SET(cme);
315 RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
317 rb_yjit_cme_invalidate(cme);
318 rb_zjit_cme_invalidate(cme);
322rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t arg)
324 ((IC) ic)->entry = NULL;
329void rb_clear_constant_cache(
void) {}
335 rb_vm_t *vm = GET_VM();
337 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
339 set_table_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
340 ruby_vm_constant_cache_invalidations += ics->num_entries;
343 rb_yjit_constant_state_changed(
id);
344 rb_zjit_constant_state_changed(
id);
348invalidate_negative_cache(
ID mid)
351 rb_vm_t *vm = GET_VM();
353 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
354 rb_id_table_delete(vm->negative_cme_table, mid);
355 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
356 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
360const rb_method_entry_t * rb_method_entry_clone(
const rb_method_entry_t *src_me);
361static const rb_callable_method_entry_t *complemented_callable_method_entry(
VALUE klass,
ID id);
362static const rb_callable_method_entry_t *lookup_overloaded_cme(
const rb_callable_method_entry_t *cme);
365invalidate_method_cache_in_cc_table(
VALUE tbl,
ID mid)
368 if (tbl && rb_managed_id_table_lookup(tbl, mid, &ccs_data)) {
370 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
371 rb_zjit_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
372 if (
NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
373 rb_vm_ccs_invalidate_and_free(ccs);
374 rb_managed_id_table_delete(tbl, mid);
375 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
380invalidate_callable_method_entry_in_callable_m_table(
struct rb_id_table *tbl,
ID mid)
383 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
384 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
385 rb_zjit_cme_invalidate((rb_callable_method_entry_t *)cme);
386 rb_id_table_delete(tbl, mid);
387 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
394 const rb_method_entry_t *cme;
395 const rb_method_entry_t *newer;
399invalidate_callable_method_entry_in_every_m_table_i(rb_classext_t *ext,
bool is_prime,
VALUE box_value,
void *data)
405 if (rb_id_table_lookup(tbl, arg->mid, &me) && arg->cme == (
const rb_method_entry_t *)me) {
406 rb_method_table_insert(arg->klass, tbl, arg->mid, arg->newer);
411invalidate_callable_method_entry_in_every_m_table(
VALUE klass,
ID mid,
const rb_callable_method_entry_t *cme)
414 const rb_method_entry_t *newer = rb_method_entry_clone((
const rb_method_entry_t *)cme);
418 .cme = (
const rb_method_entry_t *) cme,
421 rb_class_classext_foreach(klass, invalidate_callable_method_entry_in_every_m_table_i, (
void *)&arg);
425invalidate_complemented_method_entry_in_callable_m_table(
struct rb_id_table *tbl,
ID mid)
428 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
429 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
430 rb_zjit_cme_invalidate((rb_callable_method_entry_t *)cme);
431 rb_id_table_delete(tbl, mid);
432 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
437clear_method_cache_by_id_in_class(
VALUE klass,
ID mid)
440 if (rb_objspace_garbage_object_p(klass))
return;
445 if (LIKELY(RCLASS_SUBCLASSES_FIRST(klass) == NULL) &&
454 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
455 invalidate_method_cache_in_cc_table(cc_tbl, mid);
456 if (RCLASS_CC_TBL_NOT_PRIME_P(klass, cc_tbl)) {
457 invalidate_method_cache_in_cc_table(RCLASS_PRIME_CC_TBL(klass), mid);
461 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(klass);
462 invalidate_callable_method_entry_in_callable_m_table(cm_tbl, mid);
463 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(klass, cm_tbl)) {
464 invalidate_callable_method_entry_in_callable_m_table(RCLASS_PRIME_CALLABLE_M_TBL(klass), mid);
467 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
470 const rb_callable_method_entry_t *cme = complemented_callable_method_entry(klass, mid);
474 if (METHOD_ENTRY_CACHED(cme)) {
475 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
480 VALUE owner = cme->owner;
481 VM_ASSERT_TYPE(owner,
T_CLASS);
482 VALUE klass_housing_cme;
483 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
484 klass_housing_cme = owner;
487 klass_housing_cme = RCLASS_ORIGIN(owner);
491 invalidate_callable_method_entry_in_every_m_table(klass_housing_cme, mid, cme);
494 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
495 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
500 if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
501 vm_cme_invalidate((rb_callable_method_entry_t *)cme->def->body.refined.orig_me);
504 if (cme->def->iseq_overload) {
505 rb_callable_method_entry_t *monly_cme = (rb_callable_method_entry_t *)lookup_overloaded_cme(cme);
507 vm_cme_invalidate(monly_cme);
513 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
514 VALUE defined_class = cme->defined_class;
515 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
516 invalidate_complemented_method_entry_in_callable_m_table(cm_tbl, mid);
517 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(defined_class, cm_tbl)) {
518 struct rb_id_table *prime_cm_table = RCLASS_PRIME_CALLABLE_M_TBL(defined_class);
519 invalidate_complemented_method_entry_in_callable_m_table(prime_cm_table, mid);
523 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
526 invalidate_negative_cache(mid);
530 rb_gccct_clear_table(
Qnil);
535clear_iclass_method_cache_by_id(
VALUE iclass,
VALUE d)
539 clear_method_cache_by_id_in_class(iclass, mid);
543clear_iclass_method_cache_by_id_for_refinements(
VALUE klass,
VALUE d)
547 clear_method_cache_by_id_in_class(klass, mid);
552rb_clear_method_cache(
VALUE klass_or_module,
ID mid)
555 VALUE module = klass_or_module;
557 if (
FL_TEST(module, RMODULE_IS_REFINEMENT)) {
558 VALUE refined_class = rb_refinement_module_get_refined_class(module);
559 rb_clear_method_cache(refined_class, mid);
560 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
561 rb_clear_all_refinement_method_cache();
563 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
566 clear_method_cache_by_id_in_class(klass_or_module, mid);
570static enum rb_id_table_iterator_result
571invalidate_method_entry_in_iclass_callable_m_tbl(
VALUE cme,
void *data)
573 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
574 return ID_TABLE_DELETE;
577static enum rb_id_table_iterator_result
578invalidate_ccs_in_iclass_cc_tbl(
VALUE value,
void *data)
581 vm_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
583 return ID_TABLE_DELETE;
590 rb_id_table_foreach_values(cm_tbl, invalidate_method_entry_in_iclass_callable_m_tbl, NULL);
593 rb_managed_id_table_foreach_values(cc_tbl, invalidate_ccs_in_iclass_cc_tbl, NULL);
598invalidate_cc_refinement(st_data_t key, st_data_t data)
601 void *ptr = rb_asan_poisoned_object_p(v);
602 rb_asan_unpoison_object(v,
false);
604 if (rb_gc_pointer_to_heap_p(v) &&
605 !rb_objspace_garbage_object_p(v) &&
609 VM_ASSERT(vm_cc_refinement_p(cc));
611 if (vm_cc_valid(cc)) {
612 vm_cc_invalidate(cc);
617 rb_asan_poison_object(v);
632 for (
int i = 0; i < ci->kwarg->keyword_len; i++) {
644 if (ci1->mid != ci2->mid)
return 1;
645 if (ci1->flag != ci2->flag)
return 1;
646 if (ci1->argc != ci2->argc)
return 1;
647 if (ci1->kwarg != NULL) {
648 VM_ASSERT(ci2->kwarg != NULL);
650 if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
653 for (
int i = 0; i < ci1->kwarg->keyword_len; i++) {
654 if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
660 VM_ASSERT(ci2->kwarg == NULL);
665static const struct st_hash_type vm_ci_hashtype = {
671ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
674 st_data_t *ret = (st_data_t *)data;
677 if (rb_objspace_garbage_object_p((
VALUE)ci)) {
678 *ret = (st_data_t)NULL;
687 *key = *value = *ret = (st_data_t)ci;
693rb_vm_ci_lookup(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg)
695 rb_vm_t *vm = GET_VM();
708 st_table *ci_table = vm->ci_table;
712 st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
713 }
while (ci == NULL);
726 rb_vm_t *vm = GET_VM();
728 st_data_t key = (st_data_t)ci;
729 st_delete(vm->ci_table, &key, NULL);
733rb_vm_insert_cc_refinement(
const struct rb_callcache *cc)
735 st_data_t key = (st_data_t)cc;
737 rb_vm_t *vm = GET_VM();
740 rb_set_insert(vm->cc_refinement_table, key);
746rb_vm_delete_cc_refinement(
const struct rb_callcache *cc)
750 rb_vm_t *vm = GET_VM();
751 st_data_t key = (st_data_t)cc;
753 rb_set_table_delete(vm->cc_refinement_table, &key);
757rb_clear_all_refinement_method_cache(
void)
759 rb_vm_t *vm = GET_VM();
763 rb_set_table_foreach(vm->cc_refinement_table, invalidate_cc_refinement, (st_data_t)NULL);
764 rb_set_table_clear(vm->cc_refinement_table);
765 rb_set_compact_table(vm->cc_refinement_table);
769 rb_yjit_invalidate_all_method_lookup_assumptions();
773rb_method_table_insert(
VALUE klass,
struct rb_id_table *table,
ID method_id,
const rb_method_entry_t *me)
776 rb_method_table_insert0(klass, table, method_id, me,
RB_TYPE_P(klass,
T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass));
781rb_method_table_insert0(
VALUE klass,
struct rb_id_table *table,
ID method_id,
const rb_method_entry_t *me,
bool iclass_shared_mtbl)
783 VALUE table_owner = klass;
784 if (iclass_shared_mtbl) {
785 table_owner =
RBASIC(table_owner)->klass;
788 rb_id_table_insert(table, method_id, (
VALUE)me);
797NORETURN(
static VALUE rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj));
800rb_f_notimplement_internal(
int argc,
const VALUE *argv,
VALUE obj)
810 rb_f_notimplement_internal(argc, argv, obj);
814rb_define_notimplement_method_id(
VALUE mod,
ID id, rb_method_visibility_t visi)
816 rb_add_method(mod,
id, VM_METHOD_TYPE_NOTIMPLEMENTED, (
void *)1, visi);
820rb_add_method_cfunc(
VALUE klass,
ID mid,
VALUE (*func)(
ANYARGS),
int argc, rb_method_visibility_t visi)
822 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError,
"arity out of range: %d for -2..15", argc);
824 rb_method_cfunc_t opt;
827 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
830 rb_define_notimplement_method_id(klass, mid, visi);
835rb_add_method_optimized(
VALUE klass,
ID mid,
enum method_optimized_type opt_type,
unsigned int index, rb_method_visibility_t visi)
837 rb_method_optimized_t opt = {
841 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
845method_definition_release(rb_method_definition_t *def)
852 if (reference_count_was == 1) {
853 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:1->0 (remove)\n", (
void *)def,
854 rb_id2name(def->original_id));
858 if (METHOD_DEBUG) fprintf(stderr,
"-%p-%s:%d->%d (dec)\n", (
void *)def, rb_id2name(def->original_id),
859 reference_count_was, reference_count_was - 1);
865rb_method_definition_release(rb_method_definition_t *def)
867 method_definition_release(def);
870static void delete_overloaded_cme(
const rb_callable_method_entry_t *cme);
873rb_free_method_entry_vm_weak_references(
const rb_method_entry_t *me)
875 if (me->def && me->def->iseq_overload) {
876 delete_overloaded_cme((
const rb_callable_method_entry_t *)me);
881rb_free_method_entry(
const rb_method_entry_t *me)
884 if (METHOD_ENTRY_CACHED(me)) {
885 rb_zjit_cme_free((
const rb_callable_method_entry_t *)me);
894 method_definition_release(me->def);
897static inline rb_method_entry_t *search_method(
VALUE klass,
ID id,
VALUE *defined_class_ptr);
898extern int rb_method_definition_eq(
const rb_method_definition_t *d1,
const rb_method_definition_t *d2);
903 if (!GET_THREAD()->ext_config.ractor_safe) {
905 case -2:
return &call_cfunc_m2;
906 case -1:
return &call_cfunc_m1;
907 case 0:
return &call_cfunc_0;
908 case 1:
return &call_cfunc_1;
909 case 2:
return &call_cfunc_2;
910 case 3:
return &call_cfunc_3;
911 case 4:
return &call_cfunc_4;
912 case 5:
return &call_cfunc_5;
913 case 6:
return &call_cfunc_6;
914 case 7:
return &call_cfunc_7;
915 case 8:
return &call_cfunc_8;
916 case 9:
return &call_cfunc_9;
917 case 10:
return &call_cfunc_10;
918 case 11:
return &call_cfunc_11;
919 case 12:
return &call_cfunc_12;
920 case 13:
return &call_cfunc_13;
921 case 14:
return &call_cfunc_14;
922 case 15:
return &call_cfunc_15;
924 rb_bug(
"unsupported length: %d", argc);
929 case -2:
return &ractor_safe_call_cfunc_m2;
930 case -1:
return &ractor_safe_call_cfunc_m1;
931 case 0:
return &ractor_safe_call_cfunc_0;
932 case 1:
return &ractor_safe_call_cfunc_1;
933 case 2:
return &ractor_safe_call_cfunc_2;
934 case 3:
return &ractor_safe_call_cfunc_3;
935 case 4:
return &ractor_safe_call_cfunc_4;
936 case 5:
return &ractor_safe_call_cfunc_5;
937 case 6:
return &ractor_safe_call_cfunc_6;
938 case 7:
return &ractor_safe_call_cfunc_7;
939 case 8:
return &ractor_safe_call_cfunc_8;
940 case 9:
return &ractor_safe_call_cfunc_9;
941 case 10:
return &ractor_safe_call_cfunc_10;
942 case 11:
return &ractor_safe_call_cfunc_11;
943 case 12:
return &ractor_safe_call_cfunc_12;
944 case 13:
return &ractor_safe_call_cfunc_13;
945 case 14:
return &ractor_safe_call_cfunc_14;
946 case 15:
return &ractor_safe_call_cfunc_15;
948 rb_bug(
"unsupported length: %d", argc);
954setup_method_cfunc_struct(rb_method_cfunc_t *cfunc,
VALUE (*func)(
ANYARGS),
int argc)
958 cfunc->invoker = call_cfunc_invoker_func(argc);
962static rb_method_definition_t *
963method_definition_addref(rb_method_definition_t *def,
bool complemented)
966 if (!complemented && reference_count_was > 0) {
970 if (METHOD_DEBUG) fprintf(stderr,
"+%p-%s:%d->%d\n", (
void *)def, rb_id2name(def->original_id), reference_count_was, reference_count_was+1);
976rb_method_definition_addref(rb_method_definition_t *def)
978 method_definition_addref(def,
false);
982rb_method_definition_set(
const rb_method_entry_t *me, rb_method_definition_t *def,
void *opts)
984 method_definition_release(me->def);
985 *(rb_method_definition_t **)&me->def = method_definition_addref(def, METHOD_ENTRY_COMPLEMENTED(me));
987 if (!ruby_running) add_opt_method_entry(me);
991 case VM_METHOD_TYPE_ISEQ:
993 rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
994 const rb_iseq_t *iseq = iseq_body->
iseqptr;
995 rb_cref_t *method_cref, *cref = iseq_body->
cref;
1001 if (rb_iseq_attr_p(iseq, BUILTIN_ATTR_C_TRACE)) {
1002 METHOD_ENTRY_BASIC_SET((rb_method_entry_t *)me, TRUE);
1005 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
1007 if (0) vm_cref_dump(
"rb_method_definition_create", cref);
1013 method_cref = vm_cref_new_toplevel(GET_EC());
1019 case VM_METHOD_TYPE_CFUNC:
1021 rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts;
1022 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
1025 case VM_METHOD_TYPE_ATTRSET:
1026 case VM_METHOD_TYPE_IVAR:
1028 const rb_execution_context_t *ec = GET_EC();
1029 rb_control_frame_t *cfp;
1032 def->body.attr.id = (
ID)(
VALUE)opts;
1034 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1036 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
1039 RB_OBJ_SET_SHAREABLE(location);
1043 VM_ASSERT(def->body.attr.location == 0);
1047 case VM_METHOD_TYPE_BMETHOD:
1049 def->body.bmethod.defined_ractor_id = rb_ec_ractor_id(GET_EC());
1051 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1052 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (
VALUE(*)(
ANYARGS))rb_f_notimplement_internal, -1);
1054 case VM_METHOD_TYPE_OPTIMIZED:
1055 def->body.optimized = *(rb_method_optimized_t *)opts;
1057 case VM_METHOD_TYPE_REFINED:
1059 RB_OBJ_WRITE(me, &def->body.refined.orig_me, (rb_method_entry_t *)opts);
1062 case VM_METHOD_TYPE_ALIAS:
1063 RB_OBJ_WRITE(me, &def->body.alias.original_me, (rb_method_entry_t *)opts);
1065 case VM_METHOD_TYPE_ZSUPER:
1066 case VM_METHOD_TYPE_UNDEF:
1067 case VM_METHOD_TYPE_MISSING:
1074method_definition_reset(
const rb_method_entry_t *me)
1076 rb_method_definition_t *def = me->def;
1078 switch (def->type) {
1079 case VM_METHOD_TYPE_ISEQ:
1083 case VM_METHOD_TYPE_ATTRSET:
1084 case VM_METHOD_TYPE_IVAR:
1087 case VM_METHOD_TYPE_BMETHOD:
1090 case VM_METHOD_TYPE_REFINED:
1093 case VM_METHOD_TYPE_ALIAS:
1096 case VM_METHOD_TYPE_CFUNC:
1097 case VM_METHOD_TYPE_ZSUPER:
1098 case VM_METHOD_TYPE_MISSING:
1099 case VM_METHOD_TYPE_OPTIMIZED:
1100 case VM_METHOD_TYPE_UNDEF:
1101 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1108rb_method_definition_t *
1109rb_method_definition_create(rb_method_type_t
type,
ID mid)
1111 rb_method_definition_t *def;
1112 def =
ZALLOC(rb_method_definition_t);
1114 def->original_id = mid;
1116 def->box = rb_current_box();
1120static rb_method_entry_t *
1121rb_method_entry_alloc(
ID called_id,
VALUE owner,
VALUE defined_class, rb_method_definition_t *def,
bool complement)
1123 if (def) method_definition_addref(def, complement);
1124 if (
RTEST(defined_class)) {
1128 rb_method_entry_t *me = SHAREABLE_IMEMO_NEW(rb_method_entry_t, imemo_ment, defined_class);
1129 *((rb_method_definition_t **)&me->def) = def;
1130 me->called_id = called_id;
1137filter_defined_class(
VALUE klass)
1149 rb_bug(
"filter_defined_class: %s", rb_obj_info(klass));
1153rb_method_entry_create(
ID called_id,
VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def)
1155 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def,
false);
1156 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
1157 if (def != NULL) method_definition_reset(me);
1162const rb_method_entry_t *
1163rb_method_entry_clone(
const rb_method_entry_t *src_me)
1165 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));
1167 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1171 src_me->def->type == VM_METHOD_TYPE_REFINED &&
1172 src_me->def->body.refined.orig_me) {
1173 const rb_method_entry_t *orig_me = src_me->def->body.refined.orig_me;
1174 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
1176 rb_method_entry_t *orig_clone = rb_method_entry_alloc(orig_me->called_id,
1177 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
1178 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
1182 rb_method_definition_t *clone_def =
1183 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
1184 rb_method_definition_set(me, clone_def, orig_clone);
1189const rb_callable_method_entry_t *
1190rb_method_entry_complement_defined_class(
const rb_method_entry_t *src_me,
ID called_id,
VALUE defined_class)
1192 rb_method_definition_t *def = src_me->def;
1193 rb_method_entry_t *me;
1194 const rb_method_entry_t *refined_orig_me = NULL;
1196 if (!src_me->defined_class &&
1197 def->type == VM_METHOD_TYPE_REFINED &&
1198 def->body.refined.orig_me) {
1199 const rb_method_entry_t *orig_me =
1200 rb_method_entry_clone(def->body.refined.orig_me);
1202 refined_orig_me = orig_me;
1206 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def,
true);
1207 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1208 METHOD_ENTRY_COMPLEMENTED_SET(me);
1210 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
1211 rb_method_definition_set(me, def, (
void *)refined_orig_me);
1214 VM_ASSERT_TYPE(me->owner,
T_MODULE);
1216 return (rb_callable_method_entry_t *)me;
1220rb_method_entry_copy(rb_method_entry_t *dst,
const rb_method_entry_t *src)
1222 method_definition_release(dst->def);
1223 *(rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
1224 method_definition_reset(dst);
1225 dst->called_id = src->called_id;
1228 METHOD_ENTRY_FLAGS_COPY(dst, src);
1232make_method_entry_refined(
VALUE owner, rb_method_entry_t *me)
1234 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1238 rb_method_definition_t *def;
1240 rb_vm_check_redefinition_opt_method(me, me->owner);
1243 rb_method_entry_alloc(me->called_id,
1248 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
1250 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
1251 rb_method_definition_set(me, def, orig_me);
1252 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
1256static inline rb_method_entry_t *
1257lookup_method_table(
VALUE klass,
ID id)
1262 if (rb_id_table_lookup(m_tbl,
id, &body)) {
1263 return (rb_method_entry_t *) body;
1271rb_add_refined_method_entry(
VALUE refined_class,
ID mid)
1273 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
1276 make_method_entry_refined(refined_class, me);
1277 rb_clear_method_cache(refined_class, mid);
1280 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
1285check_override_opt_method_i(
VALUE klass,
VALUE arg)
1290 VALUE includer = RCLASS_INCLUDER(klass);
1291 if (!UNDEF_P(includer) && includer) {
1292 check_override_opt_method_i(includer, arg);
1298 const rb_method_entry_t *me, *newme;
1300 if (vm_redefinition_check_flag(klass)) {
1301 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1303 newme = rb_method_entry(klass, mid);
1304 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1307 rb_class_foreach_subclass(klass, check_override_opt_method_i, (
VALUE)mid);
1311check_override_opt_method(
VALUE klass,
VALUE mid)
1313 if (rb_vm_check_optimizable_mid(mid)) {
1314 check_override_opt_method_i(klass, mid);
1318static inline rb_method_entry_t* search_method0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool skip_refined);
1325static rb_method_entry_t *
1326rb_method_entry_make(
VALUE klass,
ID mid,
VALUE defined_class, rb_method_visibility_t visi,
1327 rb_method_type_t
type, rb_method_definition_t *def,
ID original_id,
void *opts)
1329 rb_method_entry_t *me;
1332 int make_refined = 0;
1340 if (!RCLASS_SINGLETON_P(klass) &&
1341 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
1342 type != VM_METHOD_TYPE_ZSUPER) {
1345 case idInitialize_copy:
1346 case idInitialize_clone:
1347 case idInitialize_dup:
1348 case idRespond_to_missing:
1349 visi = METHOD_VISI_PRIVATE;
1353 if (
type != VM_METHOD_TYPE_REFINED) {
1358 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
1359 bool search_superclass =
type == VM_METHOD_TYPE_ZSUPER && !lookup_method_table(refined_class, mid);
1360 rb_add_refined_method_entry(refined_class, mid);
1361 if (search_superclass) {
1362 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
1363 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, search_method0(refined_class, mid, NULL,
true));
1366 if (
type == VM_METHOD_TYPE_REFINED) {
1367 rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1368 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1371 klass = RCLASS_ORIGIN(klass);
1372 if (klass != orig_klass) {
1373 rb_clear_method_cache(orig_klass, mid);
1376 mtbl = RCLASS_WRITABLE_M_TBL(klass);
1379 if (rb_id_table_lookup(mtbl, mid, &data)) {
1380 rb_method_entry_t *old_me = (rb_method_entry_t *)data;
1381 rb_method_definition_t *old_def = old_me->def;
1383 if (rb_method_definition_eq(old_def, def))
return old_me;
1384 rb_vm_check_redefinition_opt_method(old_me, klass);
1386 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1389 type != VM_METHOD_TYPE_UNDEF &&
1390 (old_def->aliased ==
false) &&
1391 (!old_def->no_redef_warning) &&
1393 old_def->type != VM_METHOD_TYPE_UNDEF &&
1394 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1395 old_def->type != VM_METHOD_TYPE_ALIAS) {
1396 const rb_iseq_t *iseq = 0;
1398 switch (old_def->type) {
1399 case VM_METHOD_TYPE_ISEQ:
1400 iseq = def_iseq_ptr(old_def);
1402 case VM_METHOD_TYPE_BMETHOD:
1403 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1410 "method redefined; discarding old %"PRIsVALUE
"\n%s:%d: warning: previous definition of %"PRIsVALUE
" was here",
1412 RSTRING_PTR(rb_iseq_path(iseq)),
1413 ISEQ_BODY(iseq)->location.first_lineno,
1414 rb_id2str(old_def->original_id)
1418 rb_warning(
"method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1424 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1426 def = rb_method_definition_create(
type, original_id);
1428 rb_method_definition_set(me, def, opts);
1430 rb_clear_method_cache(klass, mid);
1433 if (klass == rb_cObject) {
1436 case idRespond_to_missing:
1437 case idMethodMissing:
1439 rb_warn(
"redefining Object#%s may cause infinite loop", rb_id2name(mid));
1443 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1444 if (
type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1445 rb_warn(
"redefining '%s' may cause serious problems", rb_id2name(mid));
1450 make_method_entry_refined(klass, me);
1453 rb_method_table_insert(klass, mtbl, mid, me);
1455 VM_ASSERT(me->def != NULL);
1459 check_override_opt_method(klass, (
VALUE)mid);
1466overloaded_cme_table(
void)
1468 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
1469 return GET_VM()->overloaded_cme_table;
1472#if VM_CHECK_MODE > 0
1474vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1476 fprintf(stderr,
"key: "); rp(key);
1477 fprintf(stderr,
"val: "); rp(val);
1482rb_vm_dump_overloaded_cme_table(
void)
1484 fprintf(stderr,
"== rb_vm_dump_overloaded_cme_table\n");
1485 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1490lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
1493 const rb_callable_method_entry_t *cme = (
const rb_callable_method_entry_t *)*key;
1494 const rb_callable_method_entry_t *monly_cme = (
const rb_callable_method_entry_t *)*value;
1495 const rb_callable_method_entry_t **ptr = (
const rb_callable_method_entry_t **)data;
1497 if (rb_objspace_garbage_object_p((
VALUE)cme) ||
1498 rb_objspace_garbage_object_p((
VALUE)monly_cme)) {
1510static const rb_callable_method_entry_t *
1511lookup_overloaded_cme(
const rb_callable_method_entry_t *cme)
1513 ASSERT_vm_locking();
1515 const rb_callable_method_entry_t *monly_cme = NULL;
1516 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1520#if VM_CHECK_MODE > 0
1521const rb_callable_method_entry_t *
1522rb_vm_lookup_overloaded_cme(
const rb_callable_method_entry_t *cme)
1524 return lookup_overloaded_cme(cme);
1529delete_overloaded_cme(
const rb_callable_method_entry_t *cme)
1531 st_data_t cme_data = (st_data_t)cme;
1532 ASSERT_vm_locking();
1533 st_delete(overloaded_cme_table(), &cme_data, NULL);
1536static const rb_callable_method_entry_t *
1537get_overloaded_cme(
const rb_callable_method_entry_t *cme)
1539 const rb_callable_method_entry_t *monly_cme = lookup_overloaded_cme(cme);
1541 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1546 rb_method_definition_t *def = rb_method_definition_create(VM_METHOD_TYPE_ISEQ, cme->def->original_id);
1547 rb_method_entry_t *me = rb_method_entry_alloc(cme->called_id,
1556 ASSERT_vm_locking();
1557 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1559 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1560 return (rb_callable_method_entry_t *)me;
1564const rb_callable_method_entry_t *
1565rb_check_overloaded_cme(
const rb_callable_method_entry_t *cme,
const struct rb_callinfo *
const ci)
1567 if (UNLIKELY(cme->def->iseq_overload) &&
1568 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1569 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1570 (
int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1571 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ,
"type: %d", cme->def->type);
1573 cme = get_overloaded_cme(cme);
1575 VM_ASSERT(cme != NULL);
1582#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1583 const VALUE arg = ID2SYM(mid); \
1584 VALUE recv_class = (klass); \
1585 ID hook_id = (hook); \
1586 if (RCLASS_SINGLETON_P((klass))) { \
1587 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1588 hook_id = singleton_##hook; \
1590 rb_funcallv(recv_class, hook_id, 1, &arg); \
1594method_added(
VALUE klass,
ID mid)
1597 CALL_METHOD_HOOK(klass, added, mid);
1602rb_add_method(
VALUE klass,
ID mid, rb_method_type_t
type,
void *opts, rb_method_visibility_t visi)
1605 rb_method_entry_make(klass, mid, klass, visi,
type, NULL, mid, opts);
1608 if (
type != VM_METHOD_TYPE_UNDEF &&
type != VM_METHOD_TYPE_REFINED) {
1609 method_added(klass, mid);
1614rb_add_method_iseq(
VALUE klass,
ID mid,
const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
1617 const rb_iseq_t *iseqptr;
1622 iseq_body.
cref = cref;
1624 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1627static rb_method_entry_t *
1628method_entry_set(
VALUE klass,
ID mid,
const rb_method_entry_t *me,
1629 rb_method_visibility_t visi,
VALUE defined_class)
1631 rb_method_entry_t *newme;
1633 newme = rb_method_entry_make(klass, mid, defined_class, visi,
1634 me->def->type, me->def, 0, NULL);
1636 me->def->no_redef_warning = TRUE;
1637 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1641 method_added(klass, mid);
1646rb_method_entry_set(
VALUE klass,
ID mid,
const rb_method_entry_t *me, rb_method_visibility_t visi)
1648 return method_entry_set(klass, mid, me, visi, klass);
1651#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1657 if (RCLASS_SINGLETON_P(klass)) {
1658 rb_raise(
rb_eTypeError,
"can't define an allocator for a singleton class");
1660 RCLASS_SET_ALLOCATOR(klass, func);
1672 RBIMPL_ASSERT_TYPE(klass,
T_CLASS);
1675 if (allocator == UNDEF_ALLOC_FUNC)
return 0;
1676 if (allocator)
return allocator;
1678 VALUE *superclasses = RCLASS_SUPERCLASSES(klass);
1679 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
1681 for (
size_t i = depth; i > 0; i--) {
1682 klass = superclasses[i - 1];
1683 RBIMPL_ASSERT_TYPE(klass,
T_CLASS);
1685 allocator = RCLASS_ALLOCATOR(klass);
1686 if (allocator == UNDEF_ALLOC_FUNC)
break;
1687 if (allocator)
return allocator;
1692const rb_method_entry_t *
1693rb_method_entry_at(
VALUE klass,
ID id)
1695 return lookup_method_table(klass,
id);
1698static inline rb_method_entry_t*
1699search_method0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool skip_refined)
1701 rb_method_entry_t *me = NULL;
1703 RB_DEBUG_COUNTER_INC(mc_search);
1706 RB_DEBUG_COUNTER_INC(mc_search_super);
1707 if ((me = lookup_method_table(klass,
id)) != 0) {
1708 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1709 me->def->body.refined.orig_me) {
1715 if (defined_class_ptr) *defined_class_ptr = klass;
1717 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1719 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me),
1720 "invalid me, mid:%s, klass:%s(%s)",
1723 rb_obj_info(klass));
1727static inline rb_method_entry_t*
1728search_method(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1730 return search_method0(klass,
id, defined_class_ptr,
false);
1733static rb_method_entry_t *
1734search_method_protect(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1736 rb_method_entry_t *me = search_method(klass,
id, defined_class_ptr);
1738 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1746const rb_method_entry_t *
1747rb_method_entry(
VALUE klass,
ID id)
1749 return search_method_protect(klass,
id, NULL);
1752static inline const rb_callable_method_entry_t *
1753prepare_callable_method_entry(
VALUE defined_class,
ID id,
const rb_method_entry_t *
const me,
int create)
1756 const rb_callable_method_entry_t *cme;
1761 if (me->defined_class == 0) {
1762 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1765 mtbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
1766 if (mtbl && rb_id_table_lookup(mtbl,
id, &cme_data)) {
1767 cme = (rb_callable_method_entry_t *)cme_data;
1771 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1772 VM_ASSERT(callable_method_entry_p(cme));
1773 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1777 mtbl = rb_id_table_create(0);
1778 RCLASS_WRITE_CALLABLE_M_TBL(defined_class, mtbl);
1780 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1781 rb_id_table_insert(mtbl,
id, (
VALUE)cme);
1783 VM_ASSERT(callable_method_entry_p(cme));
1790 cme = (
const rb_callable_method_entry_t *)me;
1791 VM_ASSERT(callable_method_entry_p(cme));
1792 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1801static const rb_callable_method_entry_t *
1802complemented_callable_method_entry(
VALUE klass,
ID id)
1804 VALUE defined_class;
1805 rb_method_entry_t *me = search_method(klass,
id, &defined_class);
1806 return prepare_callable_method_entry(defined_class,
id, me, FALSE);
1809static const rb_callable_method_entry_t *
1810cached_callable_method_entry(
VALUE klass,
ID mid)
1812 ASSERT_vm_locking();
1814 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1817 if (cc_tbl && rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1819 VM_ASSERT(vm_ccs_p(ccs));
1821 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1822 VM_ASSERT(ccs->cme->called_id == mid);
1823 RB_DEBUG_COUNTER_INC(ccs_found);
1829 rb_managed_id_table_delete(cc_tbl, mid);
1830 rb_vm_ccs_invalidate_and_free(ccs);
1834 RB_DEBUG_COUNTER_INC(ccs_not_found);
1839cache_callable_method_entry(
VALUE klass,
ID mid,
const rb_callable_method_entry_t *cme)
1841 ASSERT_vm_locking();
1842 VM_ASSERT(cme != NULL);
1844 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1848 cc_tbl = rb_vm_cc_table_create(2);
1849 RCLASS_WRITE_CC_TBL(klass, cc_tbl);
1852 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1853#if VM_CHECK_MODE > 0
1855 VM_ASSERT(ccs->cme == cme);
1859 if (rb_multi_ractor_p()) {
1860 VALUE new_cc_tbl = rb_vm_cc_table_dup(cc_tbl);
1861 vm_ccs_create(klass, new_cc_tbl, mid, cme);
1862 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), new_cc_tbl);
1865 vm_ccs_create(klass, cc_tbl, mid, cme);
1870static const rb_callable_method_entry_t *
1873 rb_vm_t *vm = GET_VM();
1874 const rb_callable_method_entry_t *cme;
1877 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1878 cme = (rb_callable_method_entry_t *)cme_data;
1881 cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid,
Qnil,
Qnil, NULL,
false);
1882 rb_id_table_insert(vm->negative_cme_table, mid, (
VALUE)cme);
1885 VM_ASSERT(cme != NULL);
1889static const rb_callable_method_entry_t *
1890callable_method_entry_or_negative(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1892 const rb_callable_method_entry_t *cme;
1897 VALUE cc_tbl = RUBY_ATOMIC_VALUE_LOAD(RCLASS_WRITABLE_CC_TBL(klass));
1900 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1902 VM_ASSERT(vm_ccs_p(ccs));
1904 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1905 VM_ASSERT(ccs->cme->called_id == mid);
1906 if (defined_class_ptr != NULL) *defined_class_ptr = ccs->cme->defined_class;
1907 RB_DEBUG_COUNTER_INC(ccs_found);
1915 cme = cached_callable_method_entry(klass, mid);
1918 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1921 VALUE defined_class;
1922 rb_method_entry_t *me = search_method(klass, mid, &defined_class);
1923 if (defined_class_ptr) *defined_class_ptr = defined_class;
1926 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1929 cme = negative_cme(mid);
1932 cache_callable_method_entry(klass, mid, cme);
1941const rb_callable_method_entry_t *
1942rb_callable_method_entry_or_negative(
VALUE klass,
ID mid)
1944 return callable_method_entry_or_negative(klass, mid, NULL);
1947static const rb_callable_method_entry_t *
1948callable_method_entry(
VALUE klass,
ID mid,
VALUE *defined_class_ptr)
1950 const rb_callable_method_entry_t *cme;
1951 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
1952 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1955const rb_callable_method_entry_t *
1956rb_callable_method_entry(
VALUE klass,
ID mid)
1958 return callable_method_entry(klass, mid, NULL);
1961static const rb_method_entry_t *resolve_refined_method(
VALUE refinements,
const rb_method_entry_t *me,
VALUE *defined_class_ptr);
1963static const rb_method_entry_t *
1964method_entry_resolve_refinement(
VALUE klass,
ID id,
int with_refinement,
VALUE *defined_class_ptr)
1966 const rb_method_entry_t *me = search_method_protect(klass,
id, defined_class_ptr);
1969 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1970 if (with_refinement) {
1971 const rb_cref_t *cref = rb_vm_cref();
1972 VALUE refinements = cref ? CREF_REFINEMENTS(cref) :
Qnil;
1973 me = resolve_refined_method(refinements, me, defined_class_ptr);
1976 me = resolve_refined_method(
Qnil, me, defined_class_ptr);
1979 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1986const rb_method_entry_t *
1987rb_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
1989 return method_entry_resolve_refinement(klass,
id, TRUE, defined_class_ptr);
1992static const rb_callable_method_entry_t *
1993callable_method_entry_refinements0(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements,
1994 const rb_callable_method_entry_t *cme)
1996 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
2000 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2001 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, with_refinements, dcp);
2002 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
2006static const rb_callable_method_entry_t *
2007callable_method_entry_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr,
bool with_refinements)
2009 const rb_callable_method_entry_t *cme = callable_method_entry(klass,
id, defined_class_ptr);
2010 return callable_method_entry_refinements0(klass,
id, defined_class_ptr, with_refinements, cme);
2013const rb_callable_method_entry_t *
2014rb_callable_method_entry_with_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
2016 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
true);
2019static const rb_callable_method_entry_t *
2020callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
2022 return callable_method_entry_refinements(klass,
id, defined_class_ptr,
false);
2025const rb_method_entry_t *
2026rb_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
2028 return method_entry_resolve_refinement(klass,
id, FALSE, defined_class_ptr);
2031const rb_callable_method_entry_t *
2032rb_callable_method_entry_without_refinements(
VALUE klass,
ID id,
VALUE *defined_class_ptr)
2034 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2035 const rb_method_entry_t *me = method_entry_resolve_refinement(klass,
id, FALSE, dcp);
2036 return prepare_callable_method_entry(*dcp,
id, me, TRUE);
2039static const rb_method_entry_t *
2040resolve_refined_method(
VALUE refinements,
const rb_method_entry_t *me,
VALUE *defined_class_ptr)
2042 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2044 const rb_method_entry_t *tmp_me;
2047 refinement = find_refinement(refinements, me->owner);
2048 if (!
NIL_P(refinement)) {
2049 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
2051 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
2056 tmp_me = me->def->body.refined.orig_me;
2058 if (!tmp_me->defined_class) {
2059 VM_ASSERT_TYPE(tmp_me->owner,
T_MODULE);
2061 else if (defined_class_ptr) {
2062 *defined_class_ptr = tmp_me->defined_class;
2072 me = search_method_protect(super, me->called_id, defined_class_ptr);
2077const rb_method_entry_t *
2078rb_resolve_refined_method(
VALUE refinements,
const rb_method_entry_t *me)
2080 return resolve_refined_method(refinements, me, NULL);
2083const rb_callable_method_entry_t *
2084rb_resolve_refined_method_callable(
VALUE refinements,
const rb_callable_method_entry_t *me)
2086 VALUE defined_class = me->defined_class;
2087 const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (
const rb_method_entry_t *)me, &defined_class);
2089 if (resolved_me && resolved_me->defined_class == 0) {
2090 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
2093 return (
const rb_callable_method_entry_t *)resolved_me;
2098remove_method(
VALUE klass,
ID mid)
2101 rb_method_entry_t *me = 0;
2105 klass = RCLASS_ORIGIN(klass);
2106 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
2107 rb_warn(
"removing '%s' may cause serious problems", rb_id2name(mid));
2110 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
2111 !(me = (rb_method_entry_t *)data) ||
2112 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
2113 UNDEFINED_REFINED_METHOD_P(me->def)) {
2114 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
2118 if (klass != self) {
2119 rb_clear_method_cache(self, mid);
2121 rb_clear_method_cache(klass, mid);
2122 rb_id_table_delete(RCLASS_WRITABLE_M_TBL(klass), mid);
2124 rb_vm_check_redefinition_opt_method(me, klass);
2126 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2127 rb_add_refined_method_entry(klass, mid);
2130 CALL_METHOD_HOOK(self, removed, mid);
2136 remove_method(klass, mid);
2142 remove_method(klass, rb_intern(name));
2156rb_mod_remove_method(
int argc,
VALUE *argv,
VALUE mod)
2160 for (i = 0; i < argc; i++) {
2164 rb_name_err_raise(
"method '%1$s' not defined in %2$s",
2167 remove_method(mod,
id);
2173rb_export_method(
VALUE klass,
ID name, rb_method_visibility_t visi)
2175 rb_method_entry_t *me;
2176 VALUE defined_class;
2177 VALUE origin_class = RCLASS_ORIGIN(klass);
2179 me = search_method0(origin_class, name, &defined_class,
true);
2182 me = search_method(rb_cObject, name, &defined_class);
2185 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2186 UNDEFINED_REFINED_METHOD_P(me->def)) {
2187 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
2190 if (METHOD_ENTRY_VISI(me) != visi) {
2191 rb_vm_check_redefinition_opt_method(me, klass);
2193 if (klass == defined_class || origin_class == defined_class) {
2194 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2197 if (me->def->body.refined.orig_me) {
2198 METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
2202 METHOD_ENTRY_VISI_SET(me, visi);
2204 rb_clear_method_cache(klass, name);
2207 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
2212#define BOUND_PRIVATE 0x01
2213#define BOUND_RESPONDS 0x02
2216method_boundp(
VALUE klass,
ID id,
int ex)
2218 const rb_callable_method_entry_t *cme;
2222 if (ex & BOUND_RESPONDS) {
2223 cme = rb_callable_method_entry_with_refinements(klass,
id, NULL);
2226 cme = callable_method_entry_without_refinements(klass,
id, NULL);
2230 if (ex & ~BOUND_RESPONDS) {
2231 switch (METHOD_ENTRY_VISI(cme)) {
2232 case METHOD_VISI_PRIVATE:
2234 case METHOD_VISI_PROTECTED:
2235 if (ex & BOUND_RESPONDS)
return 0;
2241 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
2242 if (ex & BOUND_RESPONDS)
return 2;
2254 return method_boundp(klass,
id, ex);
2258vm_cref_set_visibility(rb_method_visibility_t method_visi,
int module_func)
2260 rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t *)&rb_vm_cref()->scope_visi;
2261 scope_visi->method_visi = method_visi;
2262 scope_visi->module_func = module_func;
2266rb_scope_visibility_set(rb_method_visibility_t visi)
2268 vm_cref_set_visibility(visi, FALSE);
2272scope_visibility_check(
void)
2275 rb_control_frame_t *cfp = GET_EC()->cfp+1;
2276 if (cfp && cfp->iseq && ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD) {
2277 rb_warn(
"calling %s without arguments inside a method may not have the intended effect",
2283rb_scope_module_func_set(
void)
2285 scope_visibility_check();
2286 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
2289const rb_cref_t *rb_vm_cref_in_context(
VALUE self,
VALUE cbase);
2294 rb_method_visibility_t visi;
2295 const rb_execution_context_t *ec = GET_EC();
2296 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
2299 visi = METHOD_VISI_PUBLIC;
2302 switch (vm_scope_visibility_get(ec)) {
2303 case METHOD_VISI_PRIVATE:
2304 if (vm_scope_module_func_check(ec)) {
2305 rb_warning(
"attribute accessor as module_function");
2307 visi = METHOD_VISI_PRIVATE;
2309 case METHOD_VISI_PROTECTED:
2310 visi = METHOD_VISI_PROTECTED;
2313 visi = METHOD_VISI_PUBLIC;
2318 attriv = rb_intern_str(rb_sprintf(
"@%"PRIsVALUE, rb_id2str(
id)));
2320 rb_add_method(klass,
id, VM_METHOD_TYPE_IVAR, (
void *)attriv, visi);
2323 rb_add_method(klass, rb_id_attrset(
id), VM_METHOD_TYPE_ATTRSET, (
void *)attriv, visi);
2330 const rb_method_entry_t *me;
2336 if (
id == object_id ||
id == id__id__ ||
id == id__send__ ||
id == idInitialize) {
2337 rb_warn(
"undefining '%s' may cause serious problems", rb_id2name(
id));
2340 me = search_method(klass,
id, 0);
2341 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2342 me = rb_resolve_refined_method(
Qnil, me);
2345 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2346 UNDEFINED_REFINED_METHOD_P(me->def)) {
2347 rb_method_name_error(klass, rb_id2str(
id));
2350 rb_add_method(klass,
id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
2352 CALL_METHOD_HOOK(klass, undefined,
id);
2401rb_mod_undef_method(
int argc,
VALUE *argv,
VALUE mod)
2404 for (i = 0; i < argc; i++) {
2408 rb_method_name_error(mod, v);
2415static rb_method_visibility_t
2416check_definition_visibility(
VALUE mod,
int argc,
VALUE *argv)
2418 const rb_method_entry_t *me;
2419 VALUE mid, include_super, lookup_mod = mod;
2425 if (!
id)
return METHOD_VISI_UNDEF;
2431 inc_super =
RTEST(include_super);
2433 lookup_mod = RCLASS_ORIGIN(mod);
2437 me = rb_method_entry_without_refinements(lookup_mod,
id, NULL);
2439 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED)
return METHOD_VISI_UNDEF;
2440 if (!inc_super && me->owner != mod)
return METHOD_VISI_UNDEF;
2441 return METHOD_ENTRY_VISI(me);
2443 return METHOD_VISI_UNDEF;
2483rb_mod_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2485 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2486 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2490check_definition(
VALUE mod,
int argc,
VALUE *argv, rb_method_visibility_t visi)
2492 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2526rb_mod_public_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2528 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2562rb_mod_private_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2564 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2598rb_mod_protected_method_defined(
int argc,
VALUE *argv,
VALUE mod)
2600 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2604rb_method_entry_eq(
const rb_method_entry_t *m1,
const rb_method_entry_t *m2)
2606 return rb_method_definition_eq(m1->def, m2->def);
2609static const rb_method_definition_t *
2610original_method_definition(
const rb_method_definition_t *def)
2614 switch (def->type) {
2615 case VM_METHOD_TYPE_REFINED:
2616 if (def->body.refined.orig_me) {
2617 def = def->body.refined.orig_me->def;
2621 case VM_METHOD_TYPE_ALIAS:
2622 def = def->body.alias.original_me->def;
2632rb_method_definition_eq(
const rb_method_definition_t *d1,
const rb_method_definition_t *d2)
2634 d1 = original_method_definition(d1);
2635 d2 = original_method_definition(d2);
2637 if (d1 == d2)
return 1;
2638 if (!d1 || !d2)
return 0;
2639 if (d1->type != d2->type)
return 0;
2642 case VM_METHOD_TYPE_ISEQ:
2643 return d1->body.iseq.iseqptr == d2->body.iseq.
iseqptr;
2644 case VM_METHOD_TYPE_CFUNC:
2646 d1->body.cfunc.func == d2->body.cfunc.func &&
2647 d1->body.cfunc.argc == d2->body.cfunc.argc;
2648 case VM_METHOD_TYPE_ATTRSET:
2649 case VM_METHOD_TYPE_IVAR:
2650 return d1->body.attr.id == d2->body.attr.id;
2651 case VM_METHOD_TYPE_BMETHOD:
2652 return RTEST(
rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2653 case VM_METHOD_TYPE_MISSING:
2654 return d1->original_id == d2->original_id;
2655 case VM_METHOD_TYPE_ZSUPER:
2656 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2657 case VM_METHOD_TYPE_UNDEF:
2659 case VM_METHOD_TYPE_OPTIMIZED:
2660 return (d1->body.optimized.type == d2->body.optimized.type) &&
2661 (d1->body.optimized.index == d2->body.optimized.index);
2662 case VM_METHOD_TYPE_REFINED:
2663 case VM_METHOD_TYPE_ALIAS:
2666 rb_bug(
"rb_method_definition_eq: unsupported type: %d", d1->type);
2670rb_hash_method_definition(st_index_t hash,
const rb_method_definition_t *def)
2673 def = original_method_definition(def);
2675 if (!def)
return hash;
2677 switch (def->type) {
2678 case VM_METHOD_TYPE_ISEQ:
2680 case VM_METHOD_TYPE_CFUNC:
2681 hash =
rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2683 case VM_METHOD_TYPE_ATTRSET:
2684 case VM_METHOD_TYPE_IVAR:
2686 case VM_METHOD_TYPE_BMETHOD:
2687 return rb_hash_proc(hash, def->body.bmethod.proc);
2688 case VM_METHOD_TYPE_MISSING:
2690 case VM_METHOD_TYPE_ZSUPER:
2691 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2692 case VM_METHOD_TYPE_UNDEF:
2694 case VM_METHOD_TYPE_OPTIMIZED:
2697 case VM_METHOD_TYPE_REFINED:
2698 case VM_METHOD_TYPE_ALIAS:
2701 rb_bug(
"rb_hash_method_definition: unsupported method type (%d)", def->type);
2705rb_hash_method_entry(st_index_t hash,
const rb_method_entry_t *me)
2707 return rb_hash_method_definition(hash, me->def);
2713 const VALUE target_klass = klass;
2714 VALUE defined_class;
2715 const rb_method_entry_t *orig_me;
2716 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2725 orig_me = search_method(klass, original_name, &defined_class);
2727 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2728 orig_me = rb_resolve_refined_method(
Qnil, orig_me);
2731 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2732 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2734 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2735 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2736 rb_print_undef(target_klass, original_name, METHOD_VISI_UNDEF);
2740 switch (orig_me->def->type) {
2741 case VM_METHOD_TYPE_ZSUPER:
2743 original_name = orig_me->def->original_id;
2744 visi = METHOD_ENTRY_VISI(orig_me);
2746 case VM_METHOD_TYPE_ALIAS:
2747 visi = METHOD_ENTRY_VISI(orig_me);
2748 orig_me = orig_me->def->body.alias.original_me;
2749 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2754 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2756 if (orig_me->defined_class == 0) {
2757 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2758 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2759 (
void *)rb_method_entry_clone(orig_me));
2760 method_added(target_klass, alias_name);
2763 rb_method_entry_t *alias_me;
2765 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2766 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2772 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2804 rb_print_undef_str(mod, oldname);
2812check_and_export_method(
VALUE self,
VALUE name, rb_method_visibility_t visi)
2816 rb_print_undef_str(self, name);
2818 rb_export_method(self,
id, visi);
2822set_method_visibility(
VALUE self,
int argc,
const VALUE *argv, rb_method_visibility_t visi)
2826 rb_check_frozen(self);
2828 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
2840 check_and_export_method(self,
RARRAY_AREF(v, j), visi);
2844 for (i = 0; i < argc; i++) {
2845 check_and_export_method(self, argv[i], visi);
2851set_visibility(
int argc,
const VALUE *argv,
VALUE module, rb_method_visibility_t visi)
2854 scope_visibility_check();
2855 rb_scope_visibility_set(visi);
2859 set_method_visibility(module, argc, argv, visi);
2884rb_mod_public(
int argc,
VALUE *argv,
VALUE module)
2886 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2948rb_mod_protected(
int argc,
VALUE *argv,
VALUE module)
2950 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2982rb_mod_private(
int argc,
VALUE *argv,
VALUE module)
2984 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
3024rb_mod_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
3027 VALUE origin_class = RCLASS_ORIGIN(module);
3030 rb_check_frozen(module);
3032 for (i = 0; i < argc; i++) {
3035 rb_method_entry_t *me;
3036 VALUE defined_class;
3039 rb_print_undef_str(module, v);
3042 me = search_method(origin_class, name, &defined_class);
3044 me = search_method(rb_cObject, name, &defined_class);
3047 if (UNDEFINED_METHOD_ENTRY_P(me) ||
3048 UNDEFINED_REFINED_METHOD_P(me->def)) {
3049 rb_print_undef(module, name, METHOD_VISI_UNDEF);
3052 if (module == defined_class || origin_class == defined_class) {
3053 switch (me->def->type) {
3054 case VM_METHOD_TYPE_ISEQ:
3055 if (ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_rest &&
3056 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_post &&
3057 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kw &&
3058 !ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.has_kwrest) {
3059 ISEQ_BODY(me->def->body.iseq.
iseqptr)->param.flags.ruby2_keywords = 1;
3060 rb_clear_method_cache(module, name);
3063 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method accepts keywords or post arguments or method does not accept argument splat)", QUOTE_ID(name));
3066 case VM_METHOD_TYPE_BMETHOD: {
3067 VALUE procval = me->def->body.bmethod.proc;
3068 if (vm_block_handler_type(procval) == block_handler_type_proc) {
3069 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
3072 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
3074 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
3075 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
3076 !ISEQ_BODY(iseq)->param.flags.has_post &&
3077 !ISEQ_BODY(iseq)->param.flags.has_kw &&
3078 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
3079 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
3080 rb_clear_method_cache(module, name);
3083 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method accepts keywords or post arguments or method does not accept argument splat)", QUOTE_ID(name));
3090 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (method not defined in Ruby)", QUOTE_ID(name));
3095 rb_warn(
"Skipping set of ruby2_keywords flag for %"PRIsVALUE
" (can only set in method defining module)", QUOTE_ID(name));
3114rb_mod_public_method(
int argc,
VALUE *argv,
VALUE obj)
3142rb_mod_private_method(
int argc,
VALUE *argv,
VALUE obj)
3166 return rb_mod_public(argc, argv, rb_top_main_class(
"public"));
3186 return rb_mod_private(argc, argv, rb_top_main_class(
"private"));
3197top_ruby2_keywords(
int argc,
VALUE *argv,
VALUE module)
3199 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class(
"ruby2_keywords"));
3245rb_mod_modfunc(
int argc,
VALUE *argv,
VALUE module)
3249 const rb_method_entry_t *me;
3252 rb_raise(
rb_eTypeError,
"module_function must be called for modules");
3256 rb_scope_module_func_set();
3260 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
3262 for (i = 0; i < argc; i++) {
3267 me = search_method(m,
id, 0);
3269 me = search_method(rb_cObject,
id, 0);
3271 if (UNDEFINED_METHOD_ENTRY_P(me)) {
3272 rb_print_undef(module,
id, METHOD_VISI_UNDEF);
3274 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
3290#pragma push_macro("rb_method_basic_definition_p")
3291#undef rb_method_basic_definition_p
3296 const rb_callable_method_entry_t *cme;
3297 if (!klass)
return TRUE;
3298 cme = rb_callable_method_entry(klass,
id);
3299 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
3302#pragma pop_macro("rb_method_basic_definition_p")
3306call_method_entry(rb_execution_context_t *ec,
VALUE defined_class,
VALUE obj,
ID id,
3307 const rb_callable_method_entry_t *cme,
int argc,
const VALUE *argv,
int kw_splat)
3309 VALUE passed_block_handler = vm_passed_block_handler(ec);
3310 VALUE result = rb_vm_call_kw(ec, obj,
id, argc, argv, cme, kw_splat);
3311 vm_passed_block_handler_set(ec, passed_block_handler);
3316basic_obj_respond_to_missing(rb_execution_context_t *ec,
VALUE klass,
VALUE obj,
3319 VALUE defined_class, args[2];
3320 const ID rtmid = idRespond_to_missing;
3321 const rb_callable_method_entry_t *
const cme = callable_method_entry(klass, rtmid, &defined_class);
3323 if (!cme || METHOD_ENTRY_BASIC(cme))
return Qundef;
3326 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args,
RB_NO_KEYWORDS);
3330basic_obj_respond_to(rb_execution_context_t *ec,
VALUE obj,
ID id,
int pub)
3335 switch (method_boundp(klass,
id, pub|BOUND_RESPONDS)) {
3339 ret = basic_obj_respond_to_missing(ec, klass, obj,
ID2SYM(
id),
3341 return RTEST(ret) && !UNDEF_P(ret);
3348vm_respond_to(rb_execution_context_t *ec,
VALUE klass,
VALUE obj,
ID id,
int priv)
3350 VALUE defined_class;
3351 const ID resid = idRespond_to;
3352 const rb_callable_method_entry_t *
const cme = callable_method_entry(klass, resid, &defined_class);
3354 if (!cme)
return -1;
3355 if (METHOD_ENTRY_BASIC(cme)) {
3366 argc = rb_method_entry_arity((
const rb_method_entry_t *)cme);
3368 rb_raise(rb_eArgError,
3369 "respond_to? must accept 1 or 2 arguments (requires %d)",
3376 VALUE location = rb_method_entry_location((
const rb_method_entry_t *)cme);
3378 "%"PRIsVALUE
"%c""respond_to?(:%"PRIsVALUE
") uses"
3379 " the deprecated method signature, which takes one parameter",
3380 (RCLASS_SINGLETON_P(klass) ? obj : klass),
3381 (RCLASS_SINGLETON_P(klass) ?
'.' :
'#'),
3383 if (!
NIL_P(location)) {
3388 RSTRING_PTR(path),
NUM2INT(line),
3389 "respond_to? is defined here");
3394 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args,
RB_NO_KEYWORDS);
3395 return RTEST(result);
3402 rb_execution_context_t *ec = GET_EC();
3403 return rb_ec_obj_respond_to(ec, obj,
id, priv);
3407rb_ec_obj_respond_to(rb_execution_context_t *ec,
VALUE obj,
ID id,
int priv)
3410 int ret = vm_respond_to(ec, klass, obj,
id, priv);
3411 if (ret == -1) ret = basic_obj_respond_to(ec, obj,
id, !priv);
3443obj_respond_to(
int argc,
VALUE *argv,
VALUE obj)
3447 rb_execution_context_t *ec = GET_EC();
3451 VALUE ret = basic_obj_respond_to_missing(ec,
CLASS_OF(obj), obj,
3453 if (UNDEF_P(ret)) ret =
Qfalse;
3456 return RBOOL(basic_obj_respond_to(ec, obj,
id, !
RTEST(priv)));
3481Init_eval_method(
void)
3503 "public", top_public, -1);
3505 "private", top_private, -1);
3507 "ruby2_keywords", top_ruby2_keywords, -1);
3510#define REPLICATE_METHOD(klass, id) do { \
3511 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3512 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RUBY_ATOMIC_FETCH_SUB(var, val)
Atomically replaces the value pointed by var with the result of subtraction of val to the old value o...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
void rb_notimplement(void)
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt,...)
Identical to rb_compile_warn(), except it also accepts category.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eException
Mother of all exceptions.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_mKernel
Kernel module.
VALUE rb_cModule
Module class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
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.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
void rb_remove_method(VALUE klass, const char *name)
Removes a method.
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
void rb_remove_method_id(VALUE klass, ID mid)
Identical to rb_remove_method(), except it accepts the method name as ID.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
Raises rb_eNotImpError.
int rb_method_boundp(VALUE klass, ID id, int ex)
Queries if the klass has this method.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
int capa
Designed capacity of the buffer.
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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.