Ruby 3.5.0dev (2025-08-16 revision 11c8bad64b31c125b903547bb3eed0ede8f0f8e2)
vm_method.c (11c8bad64b31c125b903547bb3eed0ede8f0f8e2)
1/*
2 * This file is included by vm.c
3 */
4
5#include "id_table.h"
6#include "yjit.h"
7
8#define METHOD_DEBUG 0
9
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);
13
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
22#define ruby_running (GET_VM()->running)
23/* int ruby_running = 0; */
24
25static enum rb_id_table_iterator_result
26mark_cc_entry_i(VALUE ccs_ptr, void *data)
27{
28 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
29
30 VM_ASSERT(vm_ccs_p(ccs));
31
32 if (METHOD_ENTRY_INVALIDATED(ccs->cme)) {
33 /* Before detaching the CCs from this class, we need to invalidate the cc
34 * since we will no longer be marking the cme on their behalf.
35 */
36 for (int i = 0; i < ccs->len; i++) {
37 const struct rb_callcache *cc = ccs->entries[i].cc;
38 if (cc->klass == Qundef) continue; // already invalidated
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));
41 vm_cc_invalidate(cc);
42 }
43 ruby_xfree(ccs);
44 return ID_TABLE_DELETE;
45 }
46 else {
47 rb_gc_mark_movable((VALUE)ccs->cme);
48
49 for (int i = 0; i < ccs->len; i++) {
50 const struct rb_callcache *cc = ccs->entries[i].cc;
51 VM_ASSERT(cc->klass == Qundef || vm_cc_check_cme(cc, ccs->cme));
52
53 rb_gc_mark_movable((VALUE)cc);
54 }
55 return ID_TABLE_CONTINUE;
56 }
57}
58
59static void
60vm_cc_table_mark(void *data)
61{
62 struct rb_id_table *tbl = (struct rb_id_table *)data;
63 if (tbl) {
64 rb_id_table_foreach_values(tbl, mark_cc_entry_i, NULL);
65 }
66}
67
68static enum rb_id_table_iterator_result
69cc_table_free_i(VALUE ccs_ptr, void *data)
70{
71 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
72 VM_ASSERT(vm_ccs_p(ccs));
73
74 ruby_xfree(ccs);
75
76 return ID_TABLE_CONTINUE;
77}
78
79static void
80vm_cc_table_free(void *data)
81{
82 struct rb_id_table *tbl = (struct rb_id_table *)data;
83
84 rb_id_table_foreach_values(tbl, cc_table_free_i, NULL);
85 rb_managed_id_table_type.function.dfree(data);
86}
87
88static enum rb_id_table_iterator_result
89cc_table_memsize_i(VALUE ccs_ptr, void *data_ptr)
90{
91 size_t *total_size = data_ptr;
92 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
93 *total_size += sizeof(*ccs);
94 *total_size += sizeof(ccs->entries[0]) * ccs->capa;
95 return ID_TABLE_CONTINUE;
96}
97
98static size_t
99vm_cc_table_memsize(const void *data)
100{
101 size_t memsize = rb_managed_id_table_type.function.dsize(data);
102 struct rb_id_table *tbl = (struct rb_id_table *)data;
103 rb_id_table_foreach_values(tbl, cc_table_memsize_i, &memsize);
104 return memsize;
105}
106
107static enum rb_id_table_iterator_result
108compact_cc_entry_i(VALUE ccs_ptr, void *data)
109{
110 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
111
112 ccs->cme = (const struct rb_callable_method_entry_struct *)rb_gc_location((VALUE)ccs->cme);
113 VM_ASSERT(vm_ccs_p(ccs));
114
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);
117 }
118
119 return ID_TABLE_CONTINUE;
120}
121
122static void
123vm_cc_table_compact(void *data)
124{
125 struct rb_id_table *tbl = (struct rb_id_table *)data;
126 rb_id_table_foreach_values(tbl, compact_cc_entry_i, NULL);
127}
128
129static const rb_data_type_t cc_table_type = {
130 .wrap_struct_name = "VM/cc_table",
131 .function = {
132 .dmark = vm_cc_table_mark,
133 .dfree = vm_cc_table_free,
134 .dsize = vm_cc_table_memsize,
135 .dcompact = vm_cc_table_compact,
136 },
137 .parent = &rb_managed_id_table_type,
138 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
139};
140
141VALUE
142rb_vm_cc_table_create(size_t capa)
143{
144 return rb_managed_id_table_create(&cc_table_type, capa);
145}
146
147static enum rb_id_table_iterator_result
148vm_cc_table_dup_i(ID key, VALUE old_ccs_ptr, void *data)
149{
150 VALUE new_table = (VALUE)data;
151 struct rb_class_cc_entries *old_ccs = (struct rb_class_cc_entries *)old_ccs_ptr;
152 size_t memsize = vm_ccs_alloc_size(old_ccs->capa);
153 struct rb_class_cc_entries *new_ccs = ruby_xcalloc(1, memsize);
154 rb_managed_id_table_insert(new_table, key, (VALUE)new_ccs);
155
156 memcpy(new_ccs, old_ccs, memsize);
157
158#if VM_CHECK_MODE > 0
159 new_ccs->debug_sig = ~(VALUE)new_ccs;
160#endif
161
162 RB_OBJ_WRITTEN(new_table, Qundef, (VALUE)new_ccs->cme);
163 for (int index = 0; index < new_ccs->len; index++) {
164 RB_OBJ_WRITTEN(new_table, Qundef, new_ccs->entries[index].cc);
165 }
166 return ID_TABLE_CONTINUE;
167}
168
169VALUE
170rb_vm_cc_table_dup(VALUE old_table)
171{
172 VALUE new_table = rb_vm_cc_table_create(rb_managed_id_table_size(old_table));
173 rb_managed_id_table_foreach(old_table, vm_cc_table_dup_i, (void *)new_table);
174 return new_table;
175}
176
177static void
178vm_ccs_invalidate(struct rb_class_cc_entries *ccs)
179{
180 for (int i=0; i<ccs->len; i++) {
181 const struct rb_callcache *cc = ccs->entries[i].cc;
182 VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
183 vm_cc_invalidate(cc);
184 }
185}
186
187void
188rb_vm_ccs_invalidate_and_free(struct rb_class_cc_entries *ccs)
189{
190 RB_DEBUG_COUNTER_INC(ccs_free);
191 vm_ccs_invalidate(ccs);
192 ruby_xfree(ccs);
193}
194
195void
196rb_vm_cc_table_delete(VALUE table, ID mid)
197{
198 struct rb_class_cc_entries *ccs;
199 if (rb_managed_id_table_lookup(table, mid, (VALUE *)&ccs)) {
200 rb_managed_id_table_delete(table, mid);
201 rb_vm_ccs_invalidate_and_free(ccs);
202 }
203}
204
205static enum rb_id_table_iterator_result
206vm_ccs_dump_i(ID mid, VALUE val, void *data)
207{
208 const struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)val;
209 fprintf(stderr, " | %s (len:%d) ", rb_id2name(mid), ccs->len);
210 rp(ccs->cme);
211
212 for (int i=0; i<ccs->len; i++) {
213 rp_m( " | \t", ccs->entries[i].cc);
214 }
215
216 return ID_TABLE_CONTINUE;
217}
218
219static void
220vm_ccs_dump(VALUE klass, ID target_mid)
221{
222 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
223 if (cc_tbl) {
224 VALUE ccs;
225 if (target_mid) {
226 if (rb_managed_id_table_lookup(cc_tbl, target_mid, &ccs)) {
227 fprintf(stderr, " [CCTB] %p\n", (void *)cc_tbl);
228 vm_ccs_dump_i(target_mid, ccs, NULL);
229 }
230 }
231 else {
232 fprintf(stderr, " [CCTB] %p\n", (void *)cc_tbl);
233 rb_managed_id_table_foreach(cc_tbl, vm_ccs_dump_i, (void *)target_mid);
234 }
235 }
236}
237
238static enum rb_id_table_iterator_result
239vm_cme_dump_i(ID mid, VALUE val, void *data)
240{
241 ID target_mid = (ID)data;
242 if (target_mid == 0 || mid == target_mid) {
243 rp_m(" > ", val);
244 }
245 return ID_TABLE_CONTINUE;
246}
247
248static VALUE
249vm_mtbl_dump(VALUE klass, ID target_mid)
250{
251 fprintf(stderr, "# vm_mtbl\n");
252 while (klass) {
253 rp_m(" -> ", klass);
254 VALUE me;
255
256 if (RCLASS_M_TBL(klass)) {
257 if (target_mid != 0) {
258 if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
259 rp_m(" [MTBL] ", me);
260 }
261 }
262 else {
263 fprintf(stderr, " ## RCLASS_M_TBL (%p)\n", (void *)RCLASS_M_TBL(klass));
264 rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
265 }
266 }
267 else {
268 fprintf(stderr, " MTBL: NULL\n");
269 }
270 if (RCLASS_WRITABLE_CALLABLE_M_TBL(klass)) {
271 if (target_mid != 0) {
272 if (rb_id_table_lookup(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), target_mid, &me)) {
273 rp_m(" [CM**] ", me);
274 }
275 }
276 else {
277 fprintf(stderr, " ## RCLASS_CALLABLE_M_TBL\n");
278 rb_id_table_foreach(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
279 }
280 }
281 if (RCLASS_WRITABLE_CC_TBL(klass)) {
282 vm_ccs_dump(klass, target_mid);
283 }
284 klass = RCLASS_SUPER(klass);
285 }
286 return Qnil;
287}
288
289void
290rb_vm_mtbl_dump(const char *msg, VALUE klass, ID target_mid)
291{
292 fprintf(stderr, "[%s] ", msg);
293 vm_mtbl_dump(klass, target_mid);
294}
295
296static inline void
297vm_cme_invalidate(rb_callable_method_entry_t *cme)
298{
299 VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment), "cme: %d", imemo_type((VALUE)cme));
300 VM_ASSERT(callable_method_entry_p(cme));
301 METHOD_ENTRY_INVALIDATED_SET(cme);
302 RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
303
304 rb_yjit_cme_invalidate(cme);
305 rb_zjit_cme_invalidate(cme);
306}
307
308static int
309rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t arg)
310{
311 ((IC) ic)->entry = NULL;
312 return ST_CONTINUE;
313}
314
315// Here for backward compat.
316void rb_clear_constant_cache(void) {}
317
318void
320{
321 VALUE lookup_result;
322 rb_vm_t *vm = GET_VM();
323
324 if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
325 set_table *ics = (set_table *)lookup_result;
326 set_table_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
327 ruby_vm_constant_cache_invalidations += ics->num_entries;
328 }
329
330 rb_yjit_constant_state_changed(id);
331 rb_zjit_constant_state_changed(id);
332}
333
334static void
335invalidate_negative_cache(ID mid)
336{
337 VALUE cme;
338 rb_vm_t *vm = GET_VM();
339
340 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
341 rb_id_table_delete(vm->negative_cme_table, mid);
342 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
343 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
344 }
345}
346
347const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *src_me);
348static const rb_callable_method_entry_t *complemented_callable_method_entry(VALUE klass, ID id);
349static const rb_callable_method_entry_t *lookup_overloaded_cme(const rb_callable_method_entry_t *cme);
350
351static void
352invalidate_method_cache_in_cc_table(VALUE tbl, ID mid)
353{
354 VALUE ccs_data;
355 if (tbl && rb_managed_id_table_lookup(tbl, mid, &ccs_data)) {
356 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
357 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
358 if (NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
359 rb_vm_ccs_invalidate_and_free(ccs);
360 rb_managed_id_table_delete(tbl, mid);
361 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
362 }
363}
364
365static void
366invalidate_callable_method_entry_in_callable_m_table(struct rb_id_table *tbl, ID mid)
367{
368 VALUE cme;
369 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
370 if (rb_yjit_enabled_p) {
371 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
372 }
373 rb_id_table_delete(tbl, mid);
374 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
375 }
376}
377
379 VALUE klass;
380 ID mid;
381 const rb_method_entry_t *cme;
382 const rb_method_entry_t *newer;
383};
384
385static void
386invalidate_callable_method_entry_in_every_m_table_i(rb_classext_t *ext, bool is_prime, VALUE namespace, void *data)
387{
388 st_data_t me;
390 struct rb_id_table *tbl = RCLASSEXT_M_TBL(ext);
391
392 if (rb_id_table_lookup(tbl, arg->mid, &me) && arg->cme == (const rb_method_entry_t *)me) {
393 rb_method_table_insert(arg->klass, tbl, arg->mid, arg->newer);
394 }
395}
396
397static void
398invalidate_callable_method_entry_in_every_m_table(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
399{
400 // The argument cme must be invalidated later in the caller side
401 const rb_method_entry_t *newer = rb_method_entry_clone((const rb_method_entry_t *)cme);
403 .klass = klass,
404 .mid = mid,
405 .cme = (const rb_method_entry_t *) cme,
406 .newer = newer,
407 };
408 rb_class_classext_foreach(klass, invalidate_callable_method_entry_in_every_m_table_i, (void *)&arg);
409}
410
411static void
412invalidate_complemented_method_entry_in_callable_m_table(struct rb_id_table *tbl, ID mid)
413{
414 VALUE cme;
415 if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
416 if (rb_yjit_enabled_p) {
417 rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
418 }
419 rb_id_table_delete(tbl, mid);
420 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
421 }
422}
423
424static void
425clear_method_cache_by_id_in_class(VALUE klass, ID mid)
426{
427 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
428 if (rb_objspace_garbage_object_p(klass)) return;
429
430 RB_VM_LOCKING() {
431 if (LIKELY(RCLASS_SUBCLASSES_FIRST(klass) == NULL)) {
432 // no subclasses
433 // check only current class
434
435 // invalidate CCs
436 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
437 invalidate_method_cache_in_cc_table(cc_tbl, mid);
438 if (RCLASS_CC_TBL_NOT_PRIME_P(klass, cc_tbl)) {
439 invalidate_method_cache_in_cc_table(RCLASS_PRIME_CC_TBL(klass), mid);
440 }
441
442 // remove from callable_m_tbl, if exists
443 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(klass);
444 invalidate_callable_method_entry_in_callable_m_table(cm_tbl, mid);
445 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(klass, cm_tbl)) {
446 invalidate_callable_method_entry_in_callable_m_table(RCLASS_PRIME_CALLABLE_M_TBL(klass), mid);
447 }
448
449 RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
450 }
451 else {
452 const rb_callable_method_entry_t *cme = complemented_callable_method_entry(klass, mid);
453
454 if (cme) {
455 // invalidate cme if found to invalidate the inline method cache.
456 if (METHOD_ENTRY_CACHED(cme)) {
457 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
458 // do nothing
459 }
460 else {
461 // invalidate cc by invalidating cc->cme
462 VALUE owner = cme->owner;
463 VM_ASSERT_TYPE(owner, T_CLASS);
464 VALUE klass_housing_cme;
465 if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
466 klass_housing_cme = owner;
467 }
468 else {
469 klass_housing_cme = RCLASS_ORIGIN(owner);
470 }
471
472 // replace the cme that will be invalid in the all classexts
473 invalidate_callable_method_entry_in_every_m_table(klass_housing_cme, mid, cme);
474 }
475
476 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
477 RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
478
479 // In case of refinement ME, also invalidate the wrapped ME that
480 // could be cached at some callsite and is unreachable from any
481 // RCLASS_WRITABLE_CC_TBL.
482 if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
483 vm_cme_invalidate((rb_callable_method_entry_t *)cme->def->body.refined.orig_me);
484 }
485
486 if (cme->def->iseq_overload) {
487 rb_callable_method_entry_t *monly_cme = (rb_callable_method_entry_t *)lookup_overloaded_cme(cme);
488 if (monly_cme) {
489 vm_cme_invalidate(monly_cme);
490 }
491 }
492 }
493
494 // invalidate complement tbl
495 if (METHOD_ENTRY_COMPLEMENTED(cme)) {
496 VALUE defined_class = cme->defined_class;
497 struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
498 invalidate_complemented_method_entry_in_callable_m_table(cm_tbl, mid);
499 if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(defined_class, cm_tbl)) {
500 struct rb_id_table *prime_cm_table = RCLASS_PRIME_CALLABLE_M_TBL(defined_class);
501 invalidate_complemented_method_entry_in_callable_m_table(prime_cm_table, mid);
502 }
503 }
504
505 RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
506 }
507 else {
508 invalidate_negative_cache(mid);
509 }
510 }
511
512 rb_gccct_clear_table(Qnil);
513}
514}
515
516static void
517clear_iclass_method_cache_by_id(VALUE iclass, VALUE d)
518{
519 VM_ASSERT_TYPE(iclass, T_ICLASS);
520 ID mid = (ID)d;
521 clear_method_cache_by_id_in_class(iclass, mid);
522}
523
524static void
525clear_iclass_method_cache_by_id_for_refinements(VALUE klass, VALUE d)
526{
527 if (RB_TYPE_P(klass, T_ICLASS)) {
528 ID mid = (ID)d;
529 clear_method_cache_by_id_in_class(klass, mid);
530 }
531}
532
533void
534rb_clear_method_cache(VALUE klass_or_module, ID mid)
535{
536 if (RB_TYPE_P(klass_or_module, T_MODULE)) {
537 VALUE module = klass_or_module; // alias
538
539 if (FL_TEST(module, RMODULE_IS_REFINEMENT)) {
540 VALUE refined_class = rb_refinement_module_get_refined_class(module);
541 rb_clear_method_cache(refined_class, mid);
542 rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
543 rb_clear_all_refinement_method_cache();
544 }
545 rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
546 }
547 else {
548 clear_method_cache_by_id_in_class(klass_or_module, mid);
549 }
550}
551
552static enum rb_id_table_iterator_result
553invalidate_method_entry_in_iclass_callable_m_tbl(VALUE cme, void *data)
554{
555 vm_cme_invalidate((rb_callable_method_entry_t *)cme);
556 return ID_TABLE_DELETE;
557}
558
559static enum rb_id_table_iterator_result
560invalidate_ccs_in_iclass_cc_tbl(VALUE value, void *data)
561{
562 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)value;
563 vm_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
564 return ID_TABLE_DELETE;
565}
566
567void
568rb_invalidate_method_caches(struct rb_id_table *cm_tbl, VALUE cc_tbl)
569{
570 if (cm_tbl) {
571 rb_id_table_foreach_values(cm_tbl, invalidate_method_entry_in_iclass_callable_m_tbl, NULL);
572 }
573 if (cc_tbl) {
574 rb_managed_id_table_foreach_values(cc_tbl, invalidate_ccs_in_iclass_cc_tbl, NULL);
575 }
576}
577
578static int
579invalidate_cc_refinement(st_data_t key, st_data_t data)
580{
581 VALUE v = (VALUE)key;
582 void *ptr = rb_asan_poisoned_object_p(v);
583 rb_asan_unpoison_object(v, false);
584
585 if (rb_gc_pointer_to_heap_p(v) &&
586 !rb_objspace_garbage_object_p(v) &&
587 RBASIC(v)->flags) { // liveness check
588 const struct rb_callcache *cc = (const struct rb_callcache *)v;
589
590 VM_ASSERT(vm_cc_refinement_p(cc));
591
592 if (vm_cc_valid(cc)) {
593 vm_cc_invalidate(cc);
594 }
595 }
596
597 if (ptr) {
598 rb_asan_poison_object(v);
599 }
600
601 return ST_CONTINUE;
602}
603
604static st_index_t
605vm_ci_hash(VALUE v)
606{
607 const struct rb_callinfo *ci = (const struct rb_callinfo *)v;
608 st_index_t h;
609 h = rb_hash_start(ci->mid);
610 h = rb_hash_uint(h, ci->flag);
611 h = rb_hash_uint(h, ci->argc);
612 if (ci->kwarg) {
613 for (int i = 0; i < ci->kwarg->keyword_len; i++) {
614 h = rb_hash_uint(h, ci->kwarg->keywords[i]);
615 }
616 }
617 return h;
618}
619
620static int
621vm_ci_hash_cmp(VALUE v1, VALUE v2)
622{
623 const struct rb_callinfo *ci1 = (const struct rb_callinfo *)v1;
624 const struct rb_callinfo *ci2 = (const struct rb_callinfo *)v2;
625 if (ci1->mid != ci2->mid) return 1;
626 if (ci1->flag != ci2->flag) return 1;
627 if (ci1->argc != ci2->argc) return 1;
628 if (ci1->kwarg != NULL) {
629 VM_ASSERT(ci2->kwarg != NULL); // implied by matching flags
630
631 if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
632 return 1;
633
634 for (int i = 0; i < ci1->kwarg->keyword_len; i++) {
635 if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
636 return 1;
637 }
638 }
639 }
640 else {
641 VM_ASSERT(ci2->kwarg == NULL); // implied by matching flags
642 }
643 return 0;
644}
645
646static const struct st_hash_type vm_ci_hashtype = {
647 vm_ci_hash_cmp,
648 vm_ci_hash
649};
650
651static int
652ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
653{
654 const struct rb_callinfo *ci = (const struct rb_callinfo *)*key;
655 st_data_t *ret = (st_data_t *)data;
656
657 if (existing) {
658 if (rb_objspace_garbage_object_p((VALUE)ci)) {
659 *ret = (st_data_t)NULL;
660 return ST_DELETE;
661 }
662 else {
663 *ret = *key;
664 return ST_STOP;
665 }
666 }
667 else {
668 *key = *value = *ret = (st_data_t)ci;
669 return ST_CONTINUE;
670 }
671}
672
673const struct rb_callinfo *
674rb_vm_ci_lookup(ID mid, unsigned int flag, unsigned int argc, const struct rb_callinfo_kwarg *kwarg)
675{
676 rb_vm_t *vm = GET_VM();
677 const struct rb_callinfo *ci = NULL;
678
679 if (kwarg) {
680 ((struct rb_callinfo_kwarg *)kwarg)->references++;
681 }
682
683 struct rb_callinfo *new_ci = IMEMO_NEW(struct rb_callinfo, imemo_callinfo, (VALUE)kwarg);
684 new_ci->mid = mid;
685 new_ci->flag = flag;
686 new_ci->argc = argc;
687
688 RB_VM_LOCKING() {
689 st_table *ci_table = vm->ci_table;
690 VM_ASSERT(ci_table);
691
692 do {
693 st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
694 } while (ci == NULL);
695 }
696
697 VM_ASSERT(ci);
698
699 return ci;
700}
701
702void
703rb_vm_ci_free(const struct rb_callinfo *ci)
704{
705 ASSERT_vm_locking();
706
707 rb_vm_t *vm = GET_VM();
708
709 st_data_t key = (st_data_t)ci;
710 st_delete(vm->ci_table, &key, NULL);
711}
712
713void
714rb_vm_insert_cc_refinement(const struct rb_callcache *cc)
715{
716 st_data_t key = (st_data_t)cc;
717
718 rb_vm_t *vm = GET_VM();
719 RB_VM_LOCK_ENTER();
720 {
721 rb_set_insert(vm->cc_refinement_table, key);
722 }
723 RB_VM_LOCK_LEAVE();
724}
725
726void
727rb_vm_delete_cc_refinement(const struct rb_callcache *cc)
728{
729 ASSERT_vm_locking();
730
731 rb_vm_t *vm = GET_VM();
732 st_data_t key = (st_data_t)cc;
733
734 rb_set_table_delete(vm->cc_refinement_table, &key);
735}
736
737void
738rb_clear_all_refinement_method_cache(void)
739{
740 rb_vm_t *vm = GET_VM();
741
742 RB_VM_LOCK_ENTER();
743 {
744 rb_set_table_foreach(vm->cc_refinement_table, invalidate_cc_refinement, (st_data_t)NULL);
745 rb_set_table_clear(vm->cc_refinement_table);
746 rb_set_compact_table(vm->cc_refinement_table);
747 }
748 RB_VM_LOCK_LEAVE();
749
750 rb_yjit_invalidate_all_method_lookup_assumptions();
751}
752
753void
754rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me)
755{
756 rb_method_table_insert0(klass, table, method_id, me, RB_TYPE_P(klass, T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass));
757}
758
759void
760rb_method_table_insert0(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me, bool iclass_shared_mtbl)
761{
762 VALUE table_owner = klass;
763 if (iclass_shared_mtbl) {
764 table_owner = RBASIC(table_owner)->klass;
765 }
766 VM_ASSERT_TYPE3(table_owner, T_CLASS, T_ICLASS, T_MODULE);
767 rb_id_table_insert(table, method_id, (VALUE)me);
768 RB_OBJ_WRITTEN(table_owner, Qundef, (VALUE)me);
769}
770
771// rb_f_notimplement has an extra trailing argument to distinguish it from other methods
772// at compile-time to override arity to be -1. But the trailing argument introduces a
773// signature mismatch between caller and callee, so rb_define_method family inserts a
774// method entry with rb_f_notimplement_internal, which has canonical arity=-1 signature,
775// instead of rb_f_notimplement.
776NORETURN(static VALUE rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj));
777
778static VALUE
779rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj)
780{
782
784}
785
786VALUE
787rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
788{
789 rb_f_notimplement_internal(argc, argv, obj);
790}
791
792static void
793rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_visibility_t visi)
794{
795 rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, (void *)1, visi);
796}
797
798void
799rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi)
800{
801 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc);
802 if (func != (VALUE(*)(ANYARGS))rb_f_notimplement) {
804 opt.func = func;
805 opt.argc = argc;
806 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
807 }
808 else {
809 rb_define_notimplement_method_id(klass, mid, visi);
810 }
811}
812
813void
814rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type opt_type, unsigned int index, rb_method_visibility_t visi)
815{
817 .type = opt_type,
818 .index = index,
819 };
820 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
821}
822
823static void
824rb_method_definition_release(rb_method_definition_t *def)
825{
826 if (def != NULL) {
827 const unsigned int reference_count_was = RUBY_ATOMIC_FETCH_SUB(def->reference_count, 1);
828
829 RUBY_ASSERT_ALWAYS(reference_count_was != 0);
830
831 if (reference_count_was == 1) {
832 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:1->0 (remove)\n", (void *)def,
833 rb_id2name(def->original_id));
834 if (def->type == VM_METHOD_TYPE_BMETHOD && def->body.bmethod.hooks) {
835 xfree(def->body.bmethod.hooks);
836 }
837 xfree(def);
838 }
839 else {
840 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d (dec)\n", (void *)def, rb_id2name(def->original_id),
841 reference_count_was, reference_count_was - 1);
842 }
843 }
844}
845
846static void delete_overloaded_cme(const rb_callable_method_entry_t *cme);
847
848void
849rb_free_method_entry_vm_weak_references(const rb_method_entry_t *me)
850{
851 if (me->def && me->def->iseq_overload) {
852 delete_overloaded_cme((const rb_callable_method_entry_t *)me);
853 }
854}
855
856void
857rb_free_method_entry(const rb_method_entry_t *me)
858{
859 rb_method_definition_release(me->def);
860}
861
862static inline rb_method_entry_t *search_method(VALUE klass, ID id, VALUE *defined_class_ptr);
863extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);
864
865static VALUE
866(*call_cfunc_invoker_func(int argc))(VALUE recv, int argc, const VALUE *, VALUE (*func)(ANYARGS))
867{
868 if (!GET_THREAD()->ext_config.ractor_safe) {
869 switch (argc) {
870 case -2: return &call_cfunc_m2;
871 case -1: return &call_cfunc_m1;
872 case 0: return &call_cfunc_0;
873 case 1: return &call_cfunc_1;
874 case 2: return &call_cfunc_2;
875 case 3: return &call_cfunc_3;
876 case 4: return &call_cfunc_4;
877 case 5: return &call_cfunc_5;
878 case 6: return &call_cfunc_6;
879 case 7: return &call_cfunc_7;
880 case 8: return &call_cfunc_8;
881 case 9: return &call_cfunc_9;
882 case 10: return &call_cfunc_10;
883 case 11: return &call_cfunc_11;
884 case 12: return &call_cfunc_12;
885 case 13: return &call_cfunc_13;
886 case 14: return &call_cfunc_14;
887 case 15: return &call_cfunc_15;
888 default:
889 rb_bug("unsupported length: %d", argc);
890 }
891 }
892 else {
893 switch (argc) {
894 case -2: return &ractor_safe_call_cfunc_m2;
895 case -1: return &ractor_safe_call_cfunc_m1;
896 case 0: return &ractor_safe_call_cfunc_0;
897 case 1: return &ractor_safe_call_cfunc_1;
898 case 2: return &ractor_safe_call_cfunc_2;
899 case 3: return &ractor_safe_call_cfunc_3;
900 case 4: return &ractor_safe_call_cfunc_4;
901 case 5: return &ractor_safe_call_cfunc_5;
902 case 6: return &ractor_safe_call_cfunc_6;
903 case 7: return &ractor_safe_call_cfunc_7;
904 case 8: return &ractor_safe_call_cfunc_8;
905 case 9: return &ractor_safe_call_cfunc_9;
906 case 10: return &ractor_safe_call_cfunc_10;
907 case 11: return &ractor_safe_call_cfunc_11;
908 case 12: return &ractor_safe_call_cfunc_12;
909 case 13: return &ractor_safe_call_cfunc_13;
910 case 14: return &ractor_safe_call_cfunc_14;
911 case 15: return &ractor_safe_call_cfunc_15;
912 default:
913 rb_bug("unsupported length: %d", argc);
914 }
915 }
916}
917
918static void
919setup_method_cfunc_struct(rb_method_cfunc_t *cfunc, VALUE (*func)(ANYARGS), int argc)
920{
921 cfunc->func = func;
922 cfunc->argc = argc;
923 cfunc->invoker = call_cfunc_invoker_func(argc);
924}
925
927method_definition_addref(rb_method_definition_t *def, bool complemented)
928{
929 unsigned int reference_count_was = RUBY_ATOMIC_FETCH_ADD(def->reference_count, 1);
930 if (!complemented && reference_count_was > 0) {
931 /* TODO: A Ractor can reach this via UnboundMethod#bind */
932 def->aliased = true;
933 }
934 if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d->%d\n", (void *)def, rb_id2name(def->original_id), reference_count_was, reference_count_was+1);
935
936 return def;
937}
938
939void
940rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
941{
942 rb_method_definition_release(me->def);
943 *(rb_method_definition_t **)&me->def = method_definition_addref(def, METHOD_ENTRY_COMPLEMENTED(me));
944
945 if (!ruby_running) add_opt_method_entry(me);
946
947 if (opts != NULL) {
948 switch (def->type) {
949 case VM_METHOD_TYPE_ISEQ:
950 {
951 rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
952 const rb_iseq_t *iseq = iseq_body->iseqptr;
953 rb_cref_t *method_cref, *cref = iseq_body->cref;
954
955 /* setup iseq first (before invoking GC) */
956 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq);
957
958 // Methods defined in `with_jit` should be considered METHOD_ENTRY_BASIC
959 if (rb_iseq_attr_p(iseq, BUILTIN_ATTR_C_TRACE)) {
960 METHOD_ENTRY_BASIC_SET((rb_method_entry_t *)me, TRUE);
961 }
962
963 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
964
965 if (0) vm_cref_dump("rb_method_definition_create", cref);
966
967 if (cref) {
968 method_cref = cref;
969 }
970 else {
971 method_cref = vm_cref_new_toplevel(GET_EC()); /* TODO: can we reuse? */
972 }
973
974 RB_OBJ_WRITE(me, &def->body.iseq.cref, method_cref);
975 return;
976 }
977 case VM_METHOD_TYPE_CFUNC:
978 {
979 rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts;
980 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
981 return;
982 }
983 case VM_METHOD_TYPE_ATTRSET:
984 case VM_METHOD_TYPE_IVAR:
985 {
986 const rb_execution_context_t *ec = GET_EC();
988 int line;
989
990 def->body.attr.id = (ID)(VALUE)opts;
991
992 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
993
994 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
995 VALUE location = rb_ary_new3(2, rb_iseq_path(cfp->iseq), INT2FIX(line));
996 RB_OBJ_WRITE(me, &def->body.attr.location, rb_ary_freeze(location));
997 }
998 else {
999 VM_ASSERT(def->body.attr.location == 0);
1000 }
1001 return;
1002 }
1003 case VM_METHOD_TYPE_BMETHOD:
1004 RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts);
1005 RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
1006 return;
1007 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1008 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (VALUE(*)(ANYARGS))rb_f_notimplement_internal, -1);
1009 return;
1010 case VM_METHOD_TYPE_OPTIMIZED:
1011 def->body.optimized = *(rb_method_optimized_t *)opts;
1012 return;
1013 case VM_METHOD_TYPE_REFINED:
1014 {
1015 RB_OBJ_WRITE(me, &def->body.refined.orig_me, (rb_method_entry_t *)opts);
1016 return;
1017 }
1018 case VM_METHOD_TYPE_ALIAS:
1019 RB_OBJ_WRITE(me, &def->body.alias.original_me, (rb_method_entry_t *)opts);
1020 return;
1021 case VM_METHOD_TYPE_ZSUPER:
1022 case VM_METHOD_TYPE_UNDEF:
1023 case VM_METHOD_TYPE_MISSING:
1024 return;
1025 }
1026 }
1027}
1028
1029static void
1030method_definition_reset(const rb_method_entry_t *me)
1031{
1032 rb_method_definition_t *def = me->def;
1033
1034 switch (def->type) {
1035 case VM_METHOD_TYPE_ISEQ:
1036 RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.iseqptr);
1037 RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.cref);
1038 break;
1039 case VM_METHOD_TYPE_ATTRSET:
1040 case VM_METHOD_TYPE_IVAR:
1041 RB_OBJ_WRITTEN(me, Qundef, def->body.attr.location);
1042 break;
1043 case VM_METHOD_TYPE_BMETHOD:
1044 RB_OBJ_WRITTEN(me, Qundef, def->body.bmethod.proc);
1045 RB_OBJ_WRITTEN(me, Qundef, def->body.bmethod.defined_ractor);
1046 /* give up to check all in a list */
1047 if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((VALUE)me);
1048 break;
1049 case VM_METHOD_TYPE_REFINED:
1050 RB_OBJ_WRITTEN(me, Qundef, def->body.refined.orig_me);
1051 break;
1052 case VM_METHOD_TYPE_ALIAS:
1053 RB_OBJ_WRITTEN(me, Qundef, def->body.alias.original_me);
1054 break;
1055 case VM_METHOD_TYPE_CFUNC:
1056 case VM_METHOD_TYPE_ZSUPER:
1057 case VM_METHOD_TYPE_MISSING:
1058 case VM_METHOD_TYPE_OPTIMIZED:
1059 case VM_METHOD_TYPE_UNDEF:
1060 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1061 break;
1062 }
1063}
1064
1065static rb_atomic_t method_serial = 1;
1066
1068rb_method_definition_create(rb_method_type_t type, ID mid)
1069{
1072 def->type = type;
1073 def->original_id = mid;
1074 def->method_serial = (uintptr_t)RUBY_ATOMIC_FETCH_ADD(method_serial, 1);
1075 def->ns = rb_current_namespace();
1076 return def;
1077}
1078
1079static rb_method_entry_t *
1080rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, rb_method_definition_t *def, bool complement)
1081{
1082 if (def) method_definition_addref(def, complement);
1083 if (RTEST(defined_class)) {
1084 // not negative cache
1085 VM_ASSERT_TYPE2(defined_class, T_CLASS, T_ICLASS);
1086 }
1087 rb_method_entry_t *me = IMEMO_NEW(rb_method_entry_t, imemo_ment, defined_class);
1088 *((rb_method_definition_t **)&me->def) = def;
1089 me->called_id = called_id;
1090 me->owner = owner;
1091
1092 return me;
1093}
1094
1095static VALUE
1096filter_defined_class(VALUE klass)
1097{
1098 switch (BUILTIN_TYPE(klass)) {
1099 case T_CLASS:
1100 return klass;
1101 case T_MODULE:
1102 return 0;
1103 case T_ICLASS:
1104 break;
1105 default:
1106 break;
1107 }
1108 rb_bug("filter_defined_class: %s", rb_obj_info(klass));
1109}
1110
1112rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def)
1113{
1114 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def, false);
1115 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
1116 if (def != NULL) method_definition_reset(me);
1117 return me;
1118}
1119
1120// Return a cloned ME that's not invalidated (MEs are disposable for caching).
1121const rb_method_entry_t *
1122rb_method_entry_clone(const rb_method_entry_t *src_me)
1123{
1124 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));
1125
1126 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1127
1128 // Also clone inner ME in case of refinement ME
1129 if (src_me->def &&
1130 src_me->def->type == VM_METHOD_TYPE_REFINED &&
1131 src_me->def->body.refined.orig_me) {
1132 const rb_method_entry_t *orig_me = src_me->def->body.refined.orig_me;
1133 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
1134
1135 rb_method_entry_t *orig_clone = rb_method_entry_alloc(orig_me->called_id,
1136 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
1137 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
1138
1139 // Clone definition, since writing a VALUE to a shared definition
1140 // can create reference edges we can't run WBs for.
1141 rb_method_definition_t *clone_def =
1142 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
1143 rb_method_definition_set(me, clone_def, orig_clone);
1144 }
1145 return me;
1146}
1147
1149rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
1150{
1151 rb_method_definition_t *def = src_me->def;
1153 const rb_method_entry_t *refined_orig_me = NULL;
1154
1155 if (!src_me->defined_class &&
1156 def->type == VM_METHOD_TYPE_REFINED &&
1157 def->body.refined.orig_me) {
1158 const rb_method_entry_t *orig_me =
1159 rb_method_entry_clone(def->body.refined.orig_me);
1160 RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
1161 refined_orig_me = orig_me;
1162 def = NULL;
1163 }
1164
1165 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def, true);
1166 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1167 METHOD_ENTRY_COMPLEMENTED_SET(me);
1168 if (!def) {
1169 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
1170 rb_method_definition_set(me, def, (void *)refined_orig_me);
1171 }
1172
1173 VM_ASSERT_TYPE(me->owner, T_MODULE);
1174
1175 return (rb_callable_method_entry_t *)me;
1176}
1177
1178void
1179rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
1180{
1181 rb_method_definition_release(dst->def);
1182 *(rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
1183 method_definition_reset(dst);
1184 dst->called_id = src->called_id;
1185 RB_OBJ_WRITE((VALUE)dst, &dst->owner, src->owner);
1186 RB_OBJ_WRITE((VALUE)dst, &dst->defined_class, src->defined_class);
1187 METHOD_ENTRY_FLAGS_COPY(dst, src);
1188}
1189
1190static void
1191make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
1192{
1193 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1194 return;
1195 }
1196 else {
1198
1199 rb_vm_check_redefinition_opt_method(me, me->owner);
1200
1201 struct rb_method_entry_struct *orig_me =
1202 rb_method_entry_alloc(me->called_id,
1203 me->owner,
1204 me->defined_class,
1205 me->def,
1206 true);
1207 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
1208
1209 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
1210 rb_method_definition_set(me, def, orig_me);
1211 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
1212 }
1213}
1214
1215static inline rb_method_entry_t *
1216lookup_method_table(VALUE klass, ID id)
1217{
1218 st_data_t body;
1219 struct rb_id_table *m_tbl = RCLASS_M_TBL(klass);
1220
1221 if (rb_id_table_lookup(m_tbl, id, &body)) {
1222 return (rb_method_entry_t *) body;
1223 }
1224 else {
1225 return 0;
1226 }
1227}
1228
1229void
1230rb_add_refined_method_entry(VALUE refined_class, ID mid)
1231{
1232 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
1233
1234 if (me) {
1235 make_method_entry_refined(refined_class, me);
1236 rb_clear_method_cache(refined_class, mid);
1237 }
1238 else {
1239 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
1240 }
1241}
1242
1243static void
1244check_override_opt_method_i(VALUE klass, VALUE arg)
1245{
1246 ID mid = (ID)arg;
1247 const rb_method_entry_t *me, *newme;
1248
1249 if (vm_redefinition_check_flag(klass)) {
1250 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1251 if (me) {
1252 newme = rb_method_entry(klass, mid);
1253 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1254 }
1255 }
1256 rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
1257}
1258
1259static void
1260check_override_opt_method(VALUE klass, VALUE mid)
1261{
1262 if (rb_vm_check_optimizable_mid(mid)) {
1263 check_override_opt_method_i(klass, mid);
1264 }
1265}
1266
1267/*
1268 * klass->method_table[mid] = method_entry(defined_class, visi, def)
1269 *
1270 * If def is given (!= NULL), then just use it and ignore original_id and otps.
1271 * If not given, then make a new def with original_id and opts.
1272 */
1273static rb_method_entry_t *
1274rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
1275 rb_method_type_t type, rb_method_definition_t *def, ID original_id, void *opts)
1276{
1278 struct rb_id_table *mtbl;
1279 st_data_t data;
1280 int make_refined = 0;
1281 VALUE orig_klass;
1282
1283 if (NIL_P(klass)) {
1284 klass = rb_cObject;
1285 }
1286 orig_klass = klass;
1287
1288 if (!RCLASS_SINGLETON_P(klass) &&
1289 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
1290 type != VM_METHOD_TYPE_ZSUPER) {
1291 switch (mid) {
1292 case idInitialize:
1293 case idInitialize_copy:
1294 case idInitialize_clone:
1295 case idInitialize_dup:
1296 case idRespond_to_missing:
1297 visi = METHOD_VISI_PRIVATE;
1298 }
1299 }
1300
1301 if (type != VM_METHOD_TYPE_REFINED) {
1302 rb_class_modify_check(klass);
1303 }
1304
1305 if (RB_TYPE_P(klass, T_MODULE) && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1306 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
1307 rb_add_refined_method_entry(refined_class, mid);
1308 }
1309 if (type == VM_METHOD_TYPE_REFINED) {
1310 rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1311 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1312 }
1313 else {
1314 klass = RCLASS_ORIGIN(klass);
1315 if (klass != orig_klass) {
1316 rb_clear_method_cache(orig_klass, mid);
1317 }
1318 }
1319 mtbl = RCLASS_WRITABLE_M_TBL(klass);
1320
1321 /* check re-definition */
1322 if (rb_id_table_lookup(mtbl, mid, &data)) {
1323 rb_method_entry_t *old_me = (rb_method_entry_t *)data;
1324 rb_method_definition_t *old_def = old_me->def;
1325
1326 if (rb_method_definition_eq(old_def, def)) return old_me;
1327 rb_vm_check_redefinition_opt_method(old_me, klass);
1328
1329 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1330
1331 if (RTEST(ruby_verbose) &&
1332 type != VM_METHOD_TYPE_UNDEF &&
1333 (old_def->aliased == false) &&
1334 (!old_def->no_redef_warning) &&
1335 !make_refined &&
1336 old_def->type != VM_METHOD_TYPE_UNDEF &&
1337 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1338 old_def->type != VM_METHOD_TYPE_ALIAS) {
1339 const rb_iseq_t *iseq = 0;
1340
1341 switch (old_def->type) {
1342 case VM_METHOD_TYPE_ISEQ:
1343 iseq = def_iseq_ptr(old_def);
1344 break;
1345 case VM_METHOD_TYPE_BMETHOD:
1346 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1347 break;
1348 default:
1349 break;
1350 }
1351 if (iseq) {
1352 rb_warning(
1353 "method redefined; discarding old %"PRIsVALUE"\n%s:%d: warning: previous definition of %"PRIsVALUE" was here",
1354 rb_id2str(mid),
1355 RSTRING_PTR(rb_iseq_path(iseq)),
1356 ISEQ_BODY(iseq)->location.first_lineno,
1357 rb_id2str(old_def->original_id)
1358 );
1359 }
1360 else {
1361 rb_warning("method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1362 }
1363 }
1364 }
1365
1366 /* create method entry */
1367 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1368 if (def == NULL) {
1369 def = rb_method_definition_create(type, original_id);
1370 }
1371 rb_method_definition_set(me, def, opts);
1372
1373 rb_clear_method_cache(klass, mid);
1374
1375 /* check mid */
1376 if (klass == rb_cObject) {
1377 switch (mid) {
1378 case idInitialize:
1379 case idRespond_to_missing:
1380 case idMethodMissing:
1381 case idRespond_to:
1382 rb_warn("redefining Object#%s may cause infinite loop", rb_id2name(mid));
1383 }
1384 }
1385 /* check mid */
1386 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1387 if (type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1388 rb_warn("redefining '%s' may cause serious problems", rb_id2name(mid));
1389 }
1390 }
1391
1392 if (make_refined) {
1393 make_method_entry_refined(klass, me);
1394 }
1395
1396 rb_method_table_insert(klass, mtbl, mid, me);
1397
1398 VM_ASSERT(me->def != NULL);
1399
1400 /* check optimized method override by a prepended module */
1401 if (RB_TYPE_P(orig_klass, T_MODULE)) {
1402 check_override_opt_method(klass, (VALUE)mid);
1403 }
1404
1405 return me;
1406}
1407
1408static st_table *
1409overloaded_cme_table(void)
1410{
1411 VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
1412 return GET_VM()->overloaded_cme_table;
1413}
1414
1415#if VM_CHECK_MODE > 0
1416static int
1417vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1418{
1419 fprintf(stderr, "key: "); rp(key);
1420 fprintf(stderr, "val: "); rp(val);
1421 return ST_CONTINUE;
1422}
1423
1424void
1425rb_vm_dump_overloaded_cme_table(void)
1426{
1427 fprintf(stderr, "== rb_vm_dump_overloaded_cme_table\n");
1428 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1429}
1430#endif
1431
1432static int
1433lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
1434{
1435 if (existing) {
1436 const rb_callable_method_entry_t *cme = (const rb_callable_method_entry_t *)*key;
1437 const rb_callable_method_entry_t *monly_cme = (const rb_callable_method_entry_t *)*value;
1438 const rb_callable_method_entry_t **ptr = (const rb_callable_method_entry_t **)data;
1439
1440 if (rb_objspace_garbage_object_p((VALUE)cme) ||
1441 rb_objspace_garbage_object_p((VALUE)monly_cme)) {
1442 *ptr = NULL;
1443 return ST_DELETE;
1444 }
1445 else {
1446 *ptr = monly_cme;
1447 }
1448 }
1449
1450 return ST_STOP;
1451}
1452
1453static const rb_callable_method_entry_t *
1454lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1455{
1456 ASSERT_vm_locking();
1457
1458 const rb_callable_method_entry_t *monly_cme = NULL;
1459 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1460 return monly_cme;
1461}
1462
1463#if VM_CHECK_MODE > 0
1465rb_vm_lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1466{
1467 return lookup_overloaded_cme(cme);
1468}
1469#endif
1470
1471static void
1472delete_overloaded_cme(const rb_callable_method_entry_t *cme)
1473{
1474 st_data_t cme_data = (st_data_t)cme;
1475 ASSERT_vm_locking();
1476 st_delete(overloaded_cme_table(), &cme_data, NULL);
1477}
1478
1479static const rb_callable_method_entry_t *
1480get_overloaded_cme(const rb_callable_method_entry_t *cme)
1481{
1482 const rb_callable_method_entry_t *monly_cme = lookup_overloaded_cme(cme);
1483
1484 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1485 return monly_cme;
1486 }
1487 else {
1488 // create
1489 rb_method_definition_t *def = rb_method_definition_create(VM_METHOD_TYPE_ISEQ, cme->def->original_id);
1490 rb_method_entry_t *me = rb_method_entry_alloc(cme->called_id,
1491 cme->owner,
1492 cme->defined_class,
1493 def,
1494 false);
1495
1496 RB_OBJ_WRITE(me, &def->body.iseq.cref, cme->def->body.iseq.cref);
1497 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, ISEQ_BODY(cme->def->body.iseq.iseqptr)->mandatory_only_iseq);
1498
1499 ASSERT_vm_locking();
1500 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1501
1502 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1503 return (rb_callable_method_entry_t *)me;
1504 }
1505}
1506
1508rb_check_overloaded_cme(const rb_callable_method_entry_t *cme, const struct rb_callinfo * const ci)
1509{
1510 if (UNLIKELY(cme->def->iseq_overload) &&
1511 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1512 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1513 (int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1514 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ, "type: %d", cme->def->type); // iseq_overload is marked only on ISEQ methods
1515
1516 cme = get_overloaded_cme(cme);
1517
1518 VM_ASSERT(cme != NULL);
1519 METHOD_ENTRY_CACHED_SET((struct rb_callable_method_entry_struct *)cme);
1520 }
1521
1522 return cme;
1523}
1524
1525#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1526 const VALUE arg = ID2SYM(mid); \
1527 VALUE recv_class = (klass); \
1528 ID hook_id = (hook); \
1529 if (RCLASS_SINGLETON_P((klass))) { \
1530 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1531 hook_id = singleton_##hook; \
1532 } \
1533 rb_funcallv(recv_class, hook_id, 1, &arg); \
1534 } while (0)
1535
1536static void
1537method_added(VALUE klass, ID mid)
1538{
1539 if (ruby_running) {
1540 CALL_METHOD_HOOK(klass, added, mid);
1541 }
1542}
1543
1544void
1545rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
1546{
1547 rb_method_entry_make(klass, mid, klass, visi, type, NULL, mid, opts);
1548
1549 if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) {
1550 method_added(klass, mid);
1551 }
1552}
1553
1554void
1555rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
1556{
1557 struct { /* should be same fields with rb_method_iseq_struct */
1558 const rb_iseq_t *iseqptr;
1559 rb_cref_t *cref;
1560 } iseq_body;
1561
1562 iseq_body.iseqptr = iseq;
1563 iseq_body.cref = cref;
1564
1565 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1566}
1567
1568static rb_method_entry_t *
1569method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
1570 rb_method_visibility_t visi, VALUE defined_class)
1571{
1572 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
1573 me->def->type, me->def, 0, NULL);
1574 if (newme == me) {
1575 me->def->no_redef_warning = TRUE;
1576 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1577 }
1578
1579 method_added(klass, mid);
1580 return newme;
1581}
1582
1584rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_visibility_t visi)
1585{
1586 return method_entry_set(klass, mid, me, visi, klass);
1587}
1588
1589#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1590
1591void
1592rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
1593{
1594 Check_Type(klass, T_CLASS);
1595 if (RCLASS_SINGLETON_P(klass)) {
1596 rb_raise(rb_eTypeError, "can't define an allocator for a singleton class");
1597 }
1598 RCLASS_SET_ALLOCATOR(klass, func);
1599}
1600
1601void
1603{
1604 rb_define_alloc_func(klass, UNDEF_ALLOC_FUNC);
1605}
1606
1609{
1610 Check_Type(klass, T_CLASS);
1611
1612 for (; klass; klass = RCLASS_SUPER(klass)) {
1613 rb_alloc_func_t allocator = RCLASS_ALLOCATOR(klass);
1614 if (allocator == UNDEF_ALLOC_FUNC) break;
1615 if (allocator) return allocator;
1616 }
1617 return 0;
1618}
1619
1620const rb_method_entry_t *
1621rb_method_entry_at(VALUE klass, ID id)
1622{
1623 return lookup_method_table(klass, id);
1624}
1625
1626static inline rb_method_entry_t*
1627search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
1628{
1629 rb_method_entry_t *me = NULL;
1630
1631 RB_DEBUG_COUNTER_INC(mc_search);
1632
1633 for (; klass; klass = RCLASS_SUPER(klass)) {
1634 RB_DEBUG_COUNTER_INC(mc_search_super);
1635 if ((me = lookup_method_table(klass, id)) != 0) {
1636 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1637 me->def->body.refined.orig_me) {
1638 break;
1639 }
1640 }
1641 }
1642
1643 if (defined_class_ptr) *defined_class_ptr = klass;
1644
1645 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1646
1647 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me),
1648 "invalid me, mid:%s, klass:%s(%s)",
1649 rb_id2name(id),
1650 RTEST(rb_mod_name(klass)) ? RSTRING_PTR(rb_mod_name(klass)) : "anonymous",
1651 rb_obj_info(klass));
1652 return me;
1653}
1654
1655static inline rb_method_entry_t*
1656search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
1657{
1658 return search_method0(klass, id, defined_class_ptr, false);
1659}
1660
1661static rb_method_entry_t *
1662search_method_protect(VALUE klass, ID id, VALUE *defined_class_ptr)
1663{
1664 rb_method_entry_t *me = search_method(klass, id, defined_class_ptr);
1665
1666 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1667 return me;
1668 }
1669 else {
1670 return NULL;
1671 }
1672}
1673
1674const rb_method_entry_t *
1675rb_method_entry(VALUE klass, ID id)
1676{
1677 return search_method_protect(klass, id, NULL);
1678}
1679
1680static inline const rb_callable_method_entry_t *
1681prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_t * const me, int create)
1682{
1683 struct rb_id_table *mtbl;
1684 const rb_callable_method_entry_t *cme;
1685 VALUE cme_data;
1686 int cme_found = 0;
1687
1688 if (me) {
1689 if (me->defined_class == 0) {
1690 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1691 VM_ASSERT_TYPE2(defined_class, T_ICLASS, T_MODULE);
1692
1693 mtbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
1694 if (mtbl && rb_id_table_lookup(mtbl, id, &cme_data)) {
1695 cme = (rb_callable_method_entry_t *)cme_data;
1696 cme_found = 1;
1697 }
1698 if (cme_found) {
1699 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1700 VM_ASSERT(callable_method_entry_p(cme));
1701 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1702 }
1703 else if (create) {
1704 if (!mtbl) {
1705 mtbl = rb_id_table_create(0);
1706 RCLASS_WRITE_CALLABLE_M_TBL(defined_class, mtbl);
1707 }
1708 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1709 rb_id_table_insert(mtbl, id, (VALUE)cme);
1710 RB_OBJ_WRITTEN(defined_class, Qundef, (VALUE)cme);
1711 VM_ASSERT(callable_method_entry_p(cme));
1712 }
1713 else {
1714 return NULL;
1715 }
1716 }
1717 else {
1718 cme = (const rb_callable_method_entry_t *)me;
1719 VM_ASSERT(callable_method_entry_p(cme));
1720 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1721 }
1722 return cme;
1723 }
1724 else {
1725 return NULL;
1726 }
1727}
1728
1729static const rb_callable_method_entry_t *
1730complemented_callable_method_entry(VALUE klass, ID id)
1731{
1732 VALUE defined_class;
1733 rb_method_entry_t *me = search_method(klass, id, &defined_class);
1734 return prepare_callable_method_entry(defined_class, id, me, FALSE);
1735}
1736
1737static const rb_callable_method_entry_t *
1738cached_callable_method_entry(VALUE klass, ID mid)
1739{
1740 ASSERT_vm_locking();
1741
1742 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1743 VALUE ccs_data;
1744
1745 if (cc_tbl && rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1746 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
1747 VM_ASSERT(vm_ccs_p(ccs));
1748
1749 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1750 VM_ASSERT(ccs->cme->called_id == mid);
1751 RB_DEBUG_COUNTER_INC(ccs_found);
1752 return ccs->cme;
1753 }
1754 else {
1755 rb_managed_id_table_delete(cc_tbl, mid);
1756 rb_vm_ccs_invalidate_and_free(ccs);
1757 }
1758 }
1759
1760 RB_DEBUG_COUNTER_INC(ccs_not_found);
1761 return NULL;
1762}
1763
1764static void
1765cache_callable_method_entry(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
1766{
1767 ASSERT_vm_locking();
1768 VM_ASSERT(cme != NULL);
1769
1770 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1771 VALUE ccs_data;
1772
1773 if (!cc_tbl) {
1774 cc_tbl = rb_vm_cc_table_create(2);
1775 RCLASS_WRITE_CC_TBL(klass, cc_tbl);
1776 }
1777
1778 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1779#if VM_CHECK_MODE > 0
1780 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
1781 VM_ASSERT(ccs->cme == cme);
1782#endif
1783 }
1784 else {
1785 vm_ccs_create(klass, cc_tbl, mid, cme);
1786 }
1787}
1788
1789static const rb_callable_method_entry_t *
1790negative_cme(ID mid)
1791{
1792 rb_vm_t *vm = GET_VM();
1793 const rb_callable_method_entry_t *cme;
1794 VALUE cme_data;
1795
1796 if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
1797 cme = (rb_callable_method_entry_t *)cme_data;
1798 }
1799 else {
1800 cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid, Qnil, Qnil, NULL, false);
1801 rb_id_table_insert(vm->negative_cme_table, mid, (VALUE)cme);
1802 }
1803
1804 VM_ASSERT(cme != NULL);
1805 return cme;
1806}
1807
1808static const rb_callable_method_entry_t *
1809callable_method_entry_or_negative(VALUE klass, ID mid, VALUE *defined_class_ptr)
1810{
1811 const rb_callable_method_entry_t *cme;
1812
1813 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
1814 RB_VM_LOCKING() {
1815 cme = cached_callable_method_entry(klass, mid);
1816
1817 if (cme) {
1818 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
1819 }
1820 else {
1821 VALUE defined_class;
1822 rb_method_entry_t *me = search_method(klass, mid, &defined_class);
1823 if (defined_class_ptr) *defined_class_ptr = defined_class;
1824
1825 if (me != NULL) {
1826 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
1827 }
1828 else {
1829 cme = negative_cme(mid);
1830 }
1831
1832 cache_callable_method_entry(klass, mid, cme);
1833 }
1834 }
1835
1836 return cme;
1837}
1838
1839// This is exposed for YJIT so that we can make assumptions that methods are
1840// not defined.
1842rb_callable_method_entry_or_negative(VALUE klass, ID mid)
1843{
1844 return callable_method_entry_or_negative(klass, mid, NULL);
1845}
1846
1847static const rb_callable_method_entry_t *
1848callable_method_entry(VALUE klass, ID mid, VALUE *defined_class_ptr)
1849{
1850 const rb_callable_method_entry_t *cme;
1851 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
1852 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
1853}
1854
1856rb_callable_method_entry(VALUE klass, ID mid)
1857{
1858 return callable_method_entry(klass, mid, NULL);
1859}
1860
1861static const rb_method_entry_t *resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr);
1862
1863static const rb_method_entry_t *
1864method_entry_resolve_refinement(VALUE klass, ID id, int with_refinement, VALUE *defined_class_ptr)
1865{
1866 const rb_method_entry_t *me = search_method_protect(klass, id, defined_class_ptr);
1867
1868 if (me) {
1869 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1870 if (with_refinement) {
1871 const rb_cref_t *cref = rb_vm_cref();
1872 VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
1873 me = resolve_refined_method(refinements, me, defined_class_ptr);
1874 }
1875 else {
1876 me = resolve_refined_method(Qnil, me, defined_class_ptr);
1877 }
1878
1879 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
1880 }
1881 }
1882
1883 return me;
1884}
1885
1886const rb_method_entry_t *
1887rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
1888{
1889 return method_entry_resolve_refinement(klass, id, TRUE, defined_class_ptr);
1890}
1891
1892static const rb_callable_method_entry_t *
1893callable_method_entry_refinements0(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements,
1894 const rb_callable_method_entry_t *cme)
1895{
1896 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
1897 return cme;
1898 }
1899 else {
1900 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1901 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, with_refinements, dcp);
1902 return prepare_callable_method_entry(*dcp, id, me, TRUE);
1903 }
1904}
1905
1906static const rb_callable_method_entry_t *
1907callable_method_entry_refinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
1908{
1909 const rb_callable_method_entry_t *cme = callable_method_entry(klass, id, defined_class_ptr);
1910 return callable_method_entry_refinements0(klass, id, defined_class_ptr, with_refinements, cme);
1911}
1912
1914rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
1915{
1916 return callable_method_entry_refinements(klass, id, defined_class_ptr, true);
1917}
1918
1919static const rb_callable_method_entry_t *
1920callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
1921{
1922 return callable_method_entry_refinements(klass, id, defined_class_ptr, false);
1923}
1924
1925const rb_method_entry_t *
1926rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
1927{
1928 return method_entry_resolve_refinement(klass, id, FALSE, defined_class_ptr);
1929}
1930
1932rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
1933{
1934 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
1935 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, FALSE, dcp);
1936 return prepare_callable_method_entry(*dcp, id, me, TRUE);
1937}
1938
1939static const rb_method_entry_t *
1940resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
1941{
1942 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1943 VALUE refinement;
1944 const rb_method_entry_t *tmp_me;
1945 VALUE super;
1946
1947 refinement = find_refinement(refinements, me->owner);
1948 if (!NIL_P(refinement)) {
1949 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
1950
1951 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
1952 return tmp_me;
1953 }
1954 }
1955
1956 tmp_me = me->def->body.refined.orig_me;
1957 if (tmp_me) {
1958 if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
1959 return tmp_me;
1960 }
1961
1962 super = RCLASS_SUPER(me->owner);
1963 if (!super) {
1964 return 0;
1965 }
1966
1967 me = search_method_protect(super, me->called_id, defined_class_ptr);
1968 }
1969 return me;
1970}
1971
1972const rb_method_entry_t *
1973rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
1974{
1975 return resolve_refined_method(refinements, me, NULL);
1976}
1977
1979rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
1980{
1981 VALUE defined_class = me->defined_class;
1982 const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (const rb_method_entry_t *)me, &defined_class);
1983
1984 if (resolved_me && resolved_me->defined_class == 0) {
1985 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
1986 }
1987 else {
1988 return (const rb_callable_method_entry_t *)resolved_me;
1989 }
1990}
1991
1992static void
1993remove_method(VALUE klass, ID mid)
1994{
1995 VALUE data;
1996 rb_method_entry_t *me = 0;
1997 VALUE self = klass;
1998
1999 rb_class_modify_check(klass);
2000 klass = RCLASS_ORIGIN(klass);
2001 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
2002 rb_warn("removing '%s' may cause serious problems", rb_id2name(mid));
2003 }
2004
2005 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
2006 !(me = (rb_method_entry_t *)data) ||
2007 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
2008 UNDEFINED_REFINED_METHOD_P(me->def)) {
2009 rb_name_err_raise("method '%1$s' not defined in %2$s",
2010 klass, ID2SYM(mid));
2011 }
2012
2013 if (klass != self) {
2014 rb_clear_method_cache(self, mid);
2015 }
2016 rb_clear_method_cache(klass, mid);
2017 rb_id_table_delete(RCLASS_WRITABLE_M_TBL(klass), mid);
2018
2019 rb_vm_check_redefinition_opt_method(me, klass);
2020
2021 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2022 rb_add_refined_method_entry(klass, mid);
2023 }
2024
2025 CALL_METHOD_HOOK(self, removed, mid);
2026}
2027
2028void
2030{
2031 remove_method(klass, mid);
2032}
2033
2034void
2035rb_remove_method(VALUE klass, const char *name)
2036{
2037 remove_method(klass, rb_intern(name));
2038}
2039
2040/*
2041 * call-seq:
2042 * remove_method(symbol) -> self
2043 * remove_method(string) -> self
2044 *
2045 * Removes the method identified by _symbol_ from the current
2046 * class. For an example, see Module#undef_method.
2047 * String arguments are converted to symbols.
2048 */
2049
2050static VALUE
2051rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
2052{
2053 int i;
2054
2055 for (i = 0; i < argc; i++) {
2056 VALUE v = argv[i];
2057 ID id = rb_check_id(&v);
2058 if (!id) {
2059 rb_name_err_raise("method '%1$s' not defined in %2$s",
2060 mod, v);
2061 }
2062 remove_method(mod, id);
2063 }
2064 return mod;
2065}
2066
2067static void
2068rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
2069{
2071 VALUE defined_class;
2072 VALUE origin_class = RCLASS_ORIGIN(klass);
2073
2074 me = search_method0(origin_class, name, &defined_class, true);
2075
2076 if (!me && RB_TYPE_P(klass, T_MODULE)) {
2077 me = search_method(rb_cObject, name, &defined_class);
2078 }
2079
2080 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2081 UNDEFINED_REFINED_METHOD_P(me->def)) {
2082 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
2083 }
2084
2085 if (METHOD_ENTRY_VISI(me) != visi) {
2086 rb_vm_check_redefinition_opt_method(me, klass);
2087
2088 if (klass == defined_class || origin_class == defined_class) {
2089 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2090 // Refinement method entries should always be public because the refinement
2091 // search is always performed.
2092 if (me->def->body.refined.orig_me) {
2093 METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
2094 }
2095 }
2096 else {
2097 METHOD_ENTRY_VISI_SET(me, visi);
2098 }
2099 rb_clear_method_cache(klass, name);
2100 }
2101 else {
2102 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
2103 }
2104 }
2105}
2106
2107#define BOUND_PRIVATE 0x01
2108#define BOUND_RESPONDS 0x02
2109
2110static int
2111method_boundp(VALUE klass, ID id, int ex)
2112{
2113 const rb_callable_method_entry_t *cme;
2114
2115 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2116
2117 if (ex & BOUND_RESPONDS) {
2118 cme = rb_callable_method_entry_with_refinements(klass, id, NULL);
2119 }
2120 else {
2121 cme = callable_method_entry_without_refinements(klass, id, NULL);
2122 }
2123
2124 if (cme != NULL) {
2125 if (ex & ~BOUND_RESPONDS) {
2126 switch (METHOD_ENTRY_VISI(cme)) {
2127 case METHOD_VISI_PRIVATE:
2128 return 0;
2129 case METHOD_VISI_PROTECTED:
2130 if (ex & BOUND_RESPONDS) return 0;
2131 default:
2132 break;
2133 }
2134 }
2135
2136 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
2137 if (ex & BOUND_RESPONDS) return 2;
2138 return 0;
2139 }
2140 return 1;
2141 }
2142 return 0;
2143}
2144
2145// deprecated
2146int
2147rb_method_boundp(VALUE klass, ID id, int ex)
2148{
2149 return method_boundp(klass, id, ex);
2150}
2151
2152static void
2153vm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func)
2154{
2155 rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t *)&rb_vm_cref()->scope_visi;
2156 scope_visi->method_visi = method_visi;
2157 scope_visi->module_func = module_func;
2158}
2159
2160void
2161rb_scope_visibility_set(rb_method_visibility_t visi)
2162{
2163 vm_cref_set_visibility(visi, FALSE);
2164}
2165
2166static void
2167scope_visibility_check(void)
2168{
2169 /* Check for public/protected/private/module_function called inside a method */
2170 rb_control_frame_t *cfp = GET_EC()->cfp+1;
2171 if (cfp && cfp->iseq && ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD) {
2172 rb_warn("calling %s without arguments inside a method may not have the intended effect",
2173 rb_id2name(rb_frame_this_func()));
2174 }
2175}
2176
2177static void
2178rb_scope_module_func_set(void)
2179{
2180 scope_visibility_check();
2181 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
2182}
2183
2184const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
2185void
2186rb_attr(VALUE klass, ID id, int read, int write, int ex)
2187{
2188 ID attriv;
2189 rb_method_visibility_t visi;
2190 const rb_execution_context_t *ec = GET_EC();
2191 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
2192
2193 if (!ex || !cref) {
2194 visi = METHOD_VISI_PUBLIC;
2195 }
2196 else {
2197 switch (vm_scope_visibility_get(ec)) {
2198 case METHOD_VISI_PRIVATE:
2199 if (vm_scope_module_func_check(ec)) {
2200 rb_warning("attribute accessor as module_function");
2201 }
2202 visi = METHOD_VISI_PRIVATE;
2203 break;
2204 case METHOD_VISI_PROTECTED:
2205 visi = METHOD_VISI_PROTECTED;
2206 break;
2207 default:
2208 visi = METHOD_VISI_PUBLIC;
2209 break;
2210 }
2211 }
2212
2213 attriv = rb_intern_str(rb_sprintf("@%"PRIsVALUE, rb_id2str(id)));
2214 if (read) {
2215 rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, visi);
2216 }
2217 if (write) {
2218 rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, visi);
2219 }
2220}
2221
2222void
2224{
2225 const rb_method_entry_t *me;
2226
2227 if (NIL_P(klass)) {
2228 rb_raise(rb_eTypeError, "no class to undef method");
2229 }
2230 rb_class_modify_check(klass);
2231 if (id == object_id || id == id__id__ || id == id__send__ || id == idInitialize) {
2232 rb_warn("undefining '%s' may cause serious problems", rb_id2name(id));
2233 }
2234
2235 me = search_method(klass, id, 0);
2236 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2237 me = rb_resolve_refined_method(Qnil, me);
2238 }
2239
2240 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2241 UNDEFINED_REFINED_METHOD_P(me->def)) {
2242 rb_method_name_error(klass, rb_id2str(id));
2243 }
2244
2245 rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
2246
2247 CALL_METHOD_HOOK(klass, undefined, id);
2248}
2249
2250/*
2251 * call-seq:
2252 * undef_method(symbol) -> self
2253 * undef_method(string) -> self
2254 *
2255 * Prevents the current class from responding to calls to the named
2256 * method. Contrast this with <code>remove_method</code>, which deletes
2257 * the method from the particular class; Ruby will still search
2258 * superclasses and mixed-in modules for a possible receiver.
2259 * String arguments are converted to symbols.
2260 *
2261 * class Parent
2262 * def hello
2263 * puts "In parent"
2264 * end
2265 * end
2266 * class Child < Parent
2267 * def hello
2268 * puts "In child"
2269 * end
2270 * end
2271 *
2272 *
2273 * c = Child.new
2274 * c.hello
2275 *
2276 *
2277 * class Child
2278 * remove_method :hello # remove from child, still in parent
2279 * end
2280 * c.hello
2281 *
2282 *
2283 * class Child
2284 * undef_method :hello # prevent any calls to 'hello'
2285 * end
2286 * c.hello
2287 *
2288 * <em>produces:</em>
2289 *
2290 * In child
2291 * In parent
2292 * prog.rb:23: undefined method 'hello' for #<Child:0x401b3bb4> (NoMethodError)
2293 */
2294
2295static VALUE
2296rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
2297{
2298 int i;
2299 for (i = 0; i < argc; i++) {
2300 VALUE v = argv[i];
2301 ID id = rb_check_id(&v);
2302 if (!id) {
2303 rb_method_name_error(mod, v);
2304 }
2305 rb_undef(mod, id);
2306 }
2307 return mod;
2308}
2309
2310static rb_method_visibility_t
2311check_definition_visibility(VALUE mod, int argc, VALUE *argv)
2312{
2313 const rb_method_entry_t *me;
2314 VALUE mid, include_super, lookup_mod = mod;
2315 int inc_super;
2316 ID id;
2317
2318 rb_scan_args(argc, argv, "11", &mid, &include_super);
2319 id = rb_check_id(&mid);
2320 if (!id) return METHOD_VISI_UNDEF;
2321
2322 if (argc == 1) {
2323 inc_super = 1;
2324 }
2325 else {
2326 inc_super = RTEST(include_super);
2327 if (!inc_super) {
2328 lookup_mod = RCLASS_ORIGIN(mod);
2329 }
2330 }
2331
2332 me = rb_method_entry_without_refinements(lookup_mod, id, NULL);
2333 if (me) {
2334 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) return METHOD_VISI_UNDEF;
2335 if (!inc_super && me->owner != mod) return METHOD_VISI_UNDEF;
2336 return METHOD_ENTRY_VISI(me);
2337 }
2338 return METHOD_VISI_UNDEF;
2339}
2340
2341/*
2342 * call-seq:
2343 * mod.method_defined?(symbol, inherit=true) -> true or false
2344 * mod.method_defined?(string, inherit=true) -> true or false
2345 *
2346 * Returns +true+ if the named method is defined by
2347 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2348 * ancestors. Public and protected methods are matched.
2349 * String arguments are converted to symbols.
2350 *
2351 * module A
2352 * def method1() end
2353 * def protected_method1() end
2354 * protected :protected_method1
2355 * end
2356 * class B
2357 * def method2() end
2358 * def private_method2() end
2359 * private :private_method2
2360 * end
2361 * class C < B
2362 * include A
2363 * def method3() end
2364 * end
2365 *
2366 * A.method_defined? :method1 #=> true
2367 * C.method_defined? "method1" #=> true
2368 * C.method_defined? "method2" #=> true
2369 * C.method_defined? "method2", true #=> true
2370 * C.method_defined? "method2", false #=> false
2371 * C.method_defined? "method3" #=> true
2372 * C.method_defined? "protected_method1" #=> true
2373 * C.method_defined? "method4" #=> false
2374 * C.method_defined? "private_method2" #=> false
2375 */
2376
2377static VALUE
2378rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
2379{
2380 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2381 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2382}
2383
2384static VALUE
2385check_definition(VALUE mod, int argc, VALUE *argv, rb_method_visibility_t visi)
2386{
2387 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2388}
2389
2390/*
2391 * call-seq:
2392 * mod.public_method_defined?(symbol, inherit=true) -> true or false
2393 * mod.public_method_defined?(string, inherit=true) -> true or false
2394 *
2395 * Returns +true+ if the named public method is defined by
2396 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2397 * ancestors.
2398 * String arguments are converted to symbols.
2399 *
2400 * module A
2401 * def method1() end
2402 * end
2403 * class B
2404 * protected
2405 * def method2() end
2406 * end
2407 * class C < B
2408 * include A
2409 * def method3() end
2410 * end
2411 *
2412 * A.method_defined? :method1 #=> true
2413 * C.public_method_defined? "method1" #=> true
2414 * C.public_method_defined? "method1", true #=> true
2415 * C.public_method_defined? "method1", false #=> true
2416 * C.public_method_defined? "method2" #=> false
2417 * C.method_defined? "method2" #=> true
2418 */
2419
2420static VALUE
2421rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
2422{
2423 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2424}
2425
2426/*
2427 * call-seq:
2428 * mod.private_method_defined?(symbol, inherit=true) -> true or false
2429 * mod.private_method_defined?(string, inherit=true) -> true or false
2430 *
2431 * Returns +true+ if the named private method is defined by
2432 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2433 * ancestors.
2434 * String arguments are converted to symbols.
2435 *
2436 * module A
2437 * def method1() end
2438 * end
2439 * class B
2440 * private
2441 * def method2() end
2442 * end
2443 * class C < B
2444 * include A
2445 * def method3() end
2446 * end
2447 *
2448 * A.method_defined? :method1 #=> true
2449 * C.private_method_defined? "method1" #=> false
2450 * C.private_method_defined? "method2" #=> true
2451 * C.private_method_defined? "method2", true #=> true
2452 * C.private_method_defined? "method2", false #=> false
2453 * C.method_defined? "method2" #=> false
2454 */
2455
2456static VALUE
2457rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
2458{
2459 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2460}
2461
2462/*
2463 * call-seq:
2464 * mod.protected_method_defined?(symbol, inherit=true) -> true or false
2465 * mod.protected_method_defined?(string, inherit=true) -> true or false
2466 *
2467 * Returns +true+ if the named protected method is defined
2468 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2469 * ancestors.
2470 * String arguments are converted to symbols.
2471 *
2472 * module A
2473 * def method1() end
2474 * end
2475 * class B
2476 * protected
2477 * def method2() end
2478 * end
2479 * class C < B
2480 * include A
2481 * def method3() end
2482 * end
2483 *
2484 * A.method_defined? :method1 #=> true
2485 * C.protected_method_defined? "method1" #=> false
2486 * C.protected_method_defined? "method2" #=> true
2487 * C.protected_method_defined? "method2", true #=> true
2488 * C.protected_method_defined? "method2", false #=> false
2489 * C.method_defined? "method2" #=> true
2490 */
2491
2492static VALUE
2493rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
2494{
2495 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2496}
2497
2498int
2499rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
2500{
2501 return rb_method_definition_eq(m1->def, m2->def);
2502}
2503
2504static const rb_method_definition_t *
2505original_method_definition(const rb_method_definition_t *def)
2506{
2507 again:
2508 if (def) {
2509 switch (def->type) {
2510 case VM_METHOD_TYPE_REFINED:
2511 if (def->body.refined.orig_me) {
2512 def = def->body.refined.orig_me->def;
2513 goto again;
2514 }
2515 break;
2516 case VM_METHOD_TYPE_ALIAS:
2517 def = def->body.alias.original_me->def;
2518 goto again;
2519 default:
2520 break;
2521 }
2522 }
2523 return def;
2524}
2525
2526int
2527rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
2528{
2529 d1 = original_method_definition(d1);
2530 d2 = original_method_definition(d2);
2531
2532 if (d1 == d2) return 1;
2533 if (!d1 || !d2) return 0;
2534 if (d1->type != d2->type) return 0;
2535
2536 switch (d1->type) {
2537 case VM_METHOD_TYPE_ISEQ:
2538 return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
2539 case VM_METHOD_TYPE_CFUNC:
2540 return
2541 d1->body.cfunc.func == d2->body.cfunc.func &&
2542 d1->body.cfunc.argc == d2->body.cfunc.argc;
2543 case VM_METHOD_TYPE_ATTRSET:
2544 case VM_METHOD_TYPE_IVAR:
2545 return d1->body.attr.id == d2->body.attr.id;
2546 case VM_METHOD_TYPE_BMETHOD:
2547 return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2548 case VM_METHOD_TYPE_MISSING:
2549 return d1->original_id == d2->original_id;
2550 case VM_METHOD_TYPE_ZSUPER:
2551 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2552 case VM_METHOD_TYPE_UNDEF:
2553 return 1;
2554 case VM_METHOD_TYPE_OPTIMIZED:
2555 return (d1->body.optimized.type == d2->body.optimized.type) &&
2556 (d1->body.optimized.index == d2->body.optimized.index);
2557 case VM_METHOD_TYPE_REFINED:
2558 case VM_METHOD_TYPE_ALIAS:
2559 break;
2560 }
2561 rb_bug("rb_method_definition_eq: unsupported type: %d", d1->type);
2562}
2563
2564static st_index_t
2565rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
2566{
2567 hash = rb_hash_uint(hash, def->type);
2568 def = original_method_definition(def);
2569
2570 if (!def) return hash;
2571
2572 switch (def->type) {
2573 case VM_METHOD_TYPE_ISEQ:
2574 return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr->body);
2575 case VM_METHOD_TYPE_CFUNC:
2576 hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2577 return rb_hash_uint(hash, def->body.cfunc.argc);
2578 case VM_METHOD_TYPE_ATTRSET:
2579 case VM_METHOD_TYPE_IVAR:
2580 return rb_hash_uint(hash, def->body.attr.id);
2581 case VM_METHOD_TYPE_BMETHOD:
2582 return rb_hash_proc(hash, def->body.bmethod.proc);
2583 case VM_METHOD_TYPE_MISSING:
2584 return rb_hash_uint(hash, def->original_id);
2585 case VM_METHOD_TYPE_ZSUPER:
2586 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2587 case VM_METHOD_TYPE_UNDEF:
2588 return hash;
2589 case VM_METHOD_TYPE_OPTIMIZED:
2590 hash = rb_hash_uint(hash, def->body.optimized.index);
2591 return rb_hash_uint(hash, def->body.optimized.type);
2592 case VM_METHOD_TYPE_REFINED:
2593 case VM_METHOD_TYPE_ALIAS:
2594 break; /* unreachable */
2595 }
2596 rb_bug("rb_hash_method_definition: unsupported method type (%d)", def->type);
2597}
2598
2599st_index_t
2600rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
2601{
2602 return rb_hash_method_definition(hash, me->def);
2603}
2604
2605void
2606rb_alias(VALUE klass, ID alias_name, ID original_name)
2607{
2608 const VALUE target_klass = klass;
2609 VALUE defined_class;
2610 const rb_method_entry_t *orig_me;
2611 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2612
2613 if (NIL_P(klass)) {
2614 rb_raise(rb_eTypeError, "no class to make alias");
2615 }
2616
2617 rb_class_modify_check(klass);
2618
2619 again:
2620 orig_me = search_method(klass, original_name, &defined_class);
2621
2622 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2623 orig_me = rb_resolve_refined_method(Qnil, orig_me);
2624 }
2625
2626 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2627 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2628 if ((!RB_TYPE_P(klass, T_MODULE)) ||
2629 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2630 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2631 rb_print_undef(target_klass, original_name, METHOD_VISI_UNDEF);
2632 }
2633 }
2634
2635 switch (orig_me->def->type) {
2636 case VM_METHOD_TYPE_ZSUPER:
2637 klass = RCLASS_SUPER(klass);
2638 original_name = orig_me->def->original_id;
2639 visi = METHOD_ENTRY_VISI(orig_me);
2640 goto again;
2641 case VM_METHOD_TYPE_ALIAS:
2642 visi = METHOD_ENTRY_VISI(orig_me);
2643 orig_me = orig_me->def->body.alias.original_me;
2644 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2645 break;
2646 default: break;
2647 }
2648
2649 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2650
2651 if (orig_me->defined_class == 0) {
2652 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2653 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2654 (void *)rb_method_entry_clone(orig_me));
2655 method_added(target_klass, alias_name);
2656 }
2657 else {
2658 rb_method_entry_t *alias_me;
2659
2660 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2661 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2662
2663 if (RB_TYPE_P(target_klass, T_MODULE)) {
2664 // defined_class should not be set
2665 }
2666 else {
2667 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2668 }
2669 }
2670}
2671
2672/*
2673 * call-seq:
2674 * alias_method(new_name, old_name) -> symbol
2675 *
2676 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
2677 * be used to retain access to methods that are overridden.
2678 *
2679 * module Mod
2680 * alias_method :orig_exit, :exit #=> :orig_exit
2681 * def exit(code=0)
2682 * puts "Exiting with code #{code}"
2683 * orig_exit(code)
2684 * end
2685 * end
2686 * include Mod
2687 * exit(99)
2688 *
2689 * <em>produces:</em>
2690 *
2691 * Exiting with code 99
2692 */
2693
2694static VALUE
2695rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
2696{
2697 ID oldid = rb_check_id(&oldname);
2698 if (!oldid) {
2699 rb_print_undef_str(mod, oldname);
2700 }
2701 VALUE id = rb_to_id(newname);
2702 rb_alias(mod, id, oldid);
2703 return ID2SYM(id);
2704}
2705
2706static void
2707check_and_export_method(VALUE self, VALUE name, rb_method_visibility_t visi)
2708{
2709 ID id = rb_check_id(&name);
2710 if (!id) {
2711 rb_print_undef_str(self, name);
2712 }
2713 rb_export_method(self, id, visi);
2714}
2715
2716static void
2717set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibility_t visi)
2718{
2719 int i;
2720
2721 rb_check_frozen(self);
2722 if (argc == 0) {
2723 rb_warning("%"PRIsVALUE" with no argument is just ignored",
2724 QUOTE_ID(rb_frame_callee()));
2725 return;
2726 }
2727
2728
2729 VALUE v;
2730
2731 if (argc == 1 && (v = rb_check_array_type(argv[0])) != Qnil) {
2732 long j;
2733
2734 for (j = 0; j < RARRAY_LEN(v); j++) {
2735 check_and_export_method(self, RARRAY_AREF(v, j), visi);
2736 }
2737 }
2738 else {
2739 for (i = 0; i < argc; i++) {
2740 check_and_export_method(self, argv[i], visi);
2741 }
2742 }
2743}
2744
2745static VALUE
2746set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi)
2747{
2748 if (argc == 0) {
2749 scope_visibility_check();
2750 rb_scope_visibility_set(visi);
2751 return Qnil;
2752 }
2753
2754 set_method_visibility(module, argc, argv, visi);
2755 if (argc == 1) {
2756 return argv[0];
2757 }
2758 return rb_ary_new_from_values(argc, argv);
2759}
2760
2761/*
2762 * call-seq:
2763 * public -> nil
2764 * public(method_name) -> method_name
2765 * public(method_name, method_name, ...) -> array
2766 * public(array) -> array
2767 *
2768 * With no arguments, sets the default visibility for subsequently
2769 * defined methods to public. With arguments, sets the named methods to
2770 * have public visibility.
2771 * String arguments are converted to symbols.
2772 * An Array of Symbols and/or Strings is also accepted.
2773 * If a single argument is passed, it is returned.
2774 * If no argument is passed, nil is returned.
2775 * If multiple arguments are passed, the arguments are returned as an array.
2776 */
2777
2778static VALUE
2779rb_mod_public(int argc, VALUE *argv, VALUE module)
2780{
2781 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
2782}
2783
2784/*
2785 * call-seq:
2786 * protected -> nil
2787 * protected(method_name) -> method_name
2788 * protected(method_name, method_name, ...) -> array
2789 * protected(array) -> array
2790 *
2791 * Sets the visibility of a section or of a list of method names as protected.
2792 * Accepts no arguments, a splat of method names (symbols or strings) or an
2793 * array of method names. Returns the arguments that it received.
2794 *
2795 * == Important difference between protected in other languages
2796 *
2797 * Protected methods in Ruby are different from other languages such as Java,
2798 * where methods are marked as protected to give access to subclasses. In Ruby,
2799 * subclasses <b>already have access to all methods defined in the parent
2800 * class</b>, even private ones.
2801 *
2802 * Marking a method as protected allows <b>different objects of the same
2803 * class</b> to call it.
2804 *
2805 * One use case is for comparison methods, such as <code>==</code>, if we want
2806 * to expose a method for comparison between objects of the same class without
2807 * making the method public to objects of other classes.
2808 *
2809 * == Performance considerations
2810 *
2811 * Protected methods are slower than others because they can't use inline
2812 * cache.
2813 *
2814 * == Example
2815 *
2816 * class Account
2817 * # Mark balance as protected, so that we can compare between accounts
2818 * # without making it public.
2819 * attr_reader :balance
2820 * protected :balance
2821 *
2822 * def initialize(balance)
2823 * @balance = balance
2824 * end
2825 *
2826 * def >(other)
2827 * # The invocation to `other.balance` is allowed because `other` is a
2828 * # different object of the same class (Account).
2829 * balance > other.balance
2830 * end
2831 * end
2832 *
2833 * account1 = Account.new(100)
2834 * account2 = Account.new(50)
2835 *
2836 * account1 > account2 # => true (works)
2837 * account1.balance # => NoMethodError (fails because balance is not public)
2838 *
2839 * To show a private method on RDoc, use <code>:doc:</code> instead of this.
2840 */
2841
2842static VALUE
2843rb_mod_protected(int argc, VALUE *argv, VALUE module)
2844{
2845 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
2846}
2847
2848/*
2849 * call-seq:
2850 * private -> nil
2851 * private(method_name) -> method_name
2852 * private(method_name, method_name, ...) -> array
2853 * private(array) -> array
2854 *
2855 * With no arguments, sets the default visibility for subsequently
2856 * defined methods to private. With arguments, sets the named methods
2857 * to have private visibility.
2858 * String arguments are converted to symbols.
2859 * An Array of Symbols and/or Strings is also accepted.
2860 * If a single argument is passed, it is returned.
2861 * If no argument is passed, nil is returned.
2862 * If multiple arguments are passed, the arguments are returned as an array.
2863 *
2864 * module Mod
2865 * def a() end
2866 * def b() end
2867 * private
2868 * def c() end
2869 * private :a
2870 * end
2871 * Mod.private_instance_methods #=> [:a, :c]
2872 *
2873 * Note that to show a private method on RDoc, use <code>:doc:</code>.
2874 */
2875
2876static VALUE
2877rb_mod_private(int argc, VALUE *argv, VALUE module)
2878{
2879 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
2880}
2881
2882/*
2883 * call-seq:
2884 * ruby2_keywords(method_name, ...) -> nil
2885 *
2886 * For the given method names, marks the method as passing keywords through
2887 * a normal argument splat. This should only be called on methods that
2888 * accept an argument splat (<tt>*args</tt>) but not explicit keywords or
2889 * a keyword splat. It marks the method such that if the method is called
2890 * with keyword arguments, the final hash argument is marked with a special
2891 * flag such that if it is the final element of a normal argument splat to
2892 * another method call, and that method call does not include explicit
2893 * keywords or a keyword splat, the final element is interpreted as keywords.
2894 * In other words, keywords will be passed through the method to other
2895 * methods.
2896 *
2897 * This should only be used for methods that delegate keywords to another
2898 * method, and only for backwards compatibility with Ruby versions before 3.0.
2899 * See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
2900 * for details on why +ruby2_keywords+ exists and when and how to use it.
2901 *
2902 * This method will probably be removed at some point, as it exists only
2903 * for backwards compatibility. As it does not exist in Ruby versions before
2904 * 2.7, check that the module responds to this method before calling it:
2905 *
2906 * module Mod
2907 * def foo(meth, *args, &block)
2908 * send(:"do_#{meth}", *args, &block)
2909 * end
2910 * ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true)
2911 * end
2912 *
2913 * However, be aware that if the +ruby2_keywords+ method is removed, the
2914 * behavior of the +foo+ method using the above approach will change so that
2915 * the method does not pass through keywords.
2916 */
2917
2918static VALUE
2919rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module)
2920{
2921 int i;
2922 VALUE origin_class = RCLASS_ORIGIN(module);
2923
2925 rb_check_frozen(module);
2926
2927 for (i = 0; i < argc; i++) {
2928 VALUE v = argv[i];
2929 ID name = rb_check_id(&v);
2931 VALUE defined_class;
2932
2933 if (!name) {
2934 rb_print_undef_str(module, v);
2935 }
2936
2937 me = search_method(origin_class, name, &defined_class);
2938 if (!me && RB_TYPE_P(module, T_MODULE)) {
2939 me = search_method(rb_cObject, name, &defined_class);
2940 }
2941
2942 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2943 UNDEFINED_REFINED_METHOD_P(me->def)) {
2944 rb_print_undef(module, name, METHOD_VISI_UNDEF);
2945 }
2946
2947 if (module == defined_class || origin_class == defined_class) {
2948 switch (me->def->type) {
2949 case VM_METHOD_TYPE_ISEQ:
2950 if (ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_rest &&
2951 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kw &&
2952 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kwrest) {
2953 ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.ruby2_keywords = 1;
2954 rb_clear_method_cache(module, name);
2955 }
2956 else {
2957 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name));
2958 }
2959 break;
2960 case VM_METHOD_TYPE_BMETHOD: {
2961 VALUE procval = me->def->body.bmethod.proc;
2962 if (vm_block_handler_type(procval) == block_handler_type_proc) {
2963 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
2964 }
2965
2966 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
2967 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval);
2968 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
2969 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
2970 !ISEQ_BODY(iseq)->param.flags.has_kw &&
2971 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
2972 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
2973 rb_clear_method_cache(module, name);
2974 }
2975 else {
2976 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name));
2977 }
2978 break;
2979 }
2980 }
2981 /* fallthrough */
2982 default:
2983 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method not defined in Ruby)", QUOTE_ID(name));
2984 break;
2985 }
2986 }
2987 else {
2988 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (can only set in method defining module)", QUOTE_ID(name));
2989 }
2990 }
2991 return Qnil;
2992}
2993
2994/*
2995 * call-seq:
2996 * mod.public_class_method(symbol, ...) -> mod
2997 * mod.public_class_method(string, ...) -> mod
2998 * mod.public_class_method(array) -> mod
2999 *
3000 * Makes a list of existing class methods public.
3001 *
3002 * String arguments are converted to symbols.
3003 * An Array of Symbols and/or Strings is also accepted.
3004 */
3005
3006static VALUE
3007rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
3008{
3009 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
3010 return obj;
3011}
3012
3013/*
3014 * call-seq:
3015 * mod.private_class_method(symbol, ...) -> mod
3016 * mod.private_class_method(string, ...) -> mod
3017 * mod.private_class_method(array) -> mod
3018 *
3019 * Makes existing class methods private. Often used to hide the default
3020 * constructor <code>new</code>.
3021 *
3022 * String arguments are converted to symbols.
3023 * An Array of Symbols and/or Strings is also accepted.
3024 *
3025 * class SimpleSingleton # Not thread safe
3026 * private_class_method :new
3027 * def SimpleSingleton.create(*args, &block)
3028 * @me = new(*args, &block) if ! @me
3029 * @me
3030 * end
3031 * end
3032 */
3033
3034static VALUE
3035rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
3036{
3037 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
3038 return obj;
3039}
3040
3041/*
3042 * call-seq:
3043 * public
3044 * public(symbol, ...)
3045 * public(string, ...)
3046 * public(array)
3047 *
3048 * With no arguments, sets the default visibility for subsequently
3049 * defined methods to public. With arguments, sets the named methods to
3050 * have public visibility.
3051 *
3052 * String arguments are converted to symbols.
3053 * An Array of Symbols and/or Strings is also accepted.
3054 */
3055
3056static VALUE
3057top_public(int argc, VALUE *argv, VALUE _)
3058{
3059 return rb_mod_public(argc, argv, rb_top_main_class("public"));
3060}
3061
3062/*
3063 * call-seq:
3064 * private
3065 * private(symbol, ...)
3066 * private(string, ...)
3067 * private(array)
3068 *
3069 * With no arguments, sets the default visibility for subsequently
3070 * defined methods to private. With arguments, sets the named methods to
3071 * have private visibility.
3072 *
3073 * String arguments are converted to symbols.
3074 * An Array of Symbols and/or Strings is also accepted.
3075 */
3076static VALUE
3077top_private(int argc, VALUE *argv, VALUE _)
3078{
3079 return rb_mod_private(argc, argv, rb_top_main_class("private"));
3080}
3081
3082/*
3083 * call-seq:
3084 * ruby2_keywords(method_name, ...) -> self
3085 *
3086 * For the given method names, marks the method as passing keywords through
3087 * a normal argument splat. See Module#ruby2_keywords in detail.
3088 */
3089static VALUE
3090top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
3091{
3092 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
3093}
3094
3095/*
3096 * call-seq:
3097 * module_function -> nil
3098 * module_function(method_name) -> method_name
3099 * module_function(method_name, method_name, ...) -> array
3100 *
3101 * Creates module functions for the named methods. These functions may
3102 * be called with the module as a receiver, and also become available
3103 * as instance methods to classes that mix in the module. Module
3104 * functions are copies of the original, and so may be changed
3105 * independently. The instance-method versions are made private. If
3106 * used with no arguments, subsequently defined methods become module
3107 * functions.
3108 * String arguments are converted to symbols.
3109 * If a single argument is passed, it is returned.
3110 * If no argument is passed, nil is returned.
3111 * If multiple arguments are passed, the arguments are returned as an array.
3112 *
3113 * module Mod
3114 * def one
3115 * "This is one"
3116 * end
3117 * module_function :one
3118 * end
3119 * class Cls
3120 * include Mod
3121 * def call_one
3122 * one
3123 * end
3124 * end
3125 * Mod.one #=> "This is one"
3126 * c = Cls.new
3127 * c.call_one #=> "This is one"
3128 * module Mod
3129 * def one
3130 * "This is the new one"
3131 * end
3132 * end
3133 * Mod.one #=> "This is one"
3134 * c.call_one #=> "This is the new one"
3135 */
3136
3137static VALUE
3138rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
3139{
3140 int i;
3141 ID id;
3142 const rb_method_entry_t *me;
3143
3144 if (!RB_TYPE_P(module, T_MODULE)) {
3145 rb_raise(rb_eTypeError, "module_function must be called for modules");
3146 }
3147
3148 if (argc == 0) {
3149 rb_scope_module_func_set();
3150 return Qnil;
3151 }
3152
3153 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
3154
3155 for (i = 0; i < argc; i++) {
3156 VALUE m = module;
3157
3158 id = rb_to_id(argv[i]);
3159 for (;;) {
3160 me = search_method(m, id, 0);
3161 if (me == 0) {
3162 me = search_method(rb_cObject, id, 0);
3163 }
3164 if (UNDEFINED_METHOD_ENTRY_P(me)) {
3165 rb_print_undef(module, id, METHOD_VISI_UNDEF);
3166 }
3167 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
3168 break; /* normal case: need not to follow 'super' link */
3169 }
3170 m = RCLASS_SUPER(m);
3171 if (!m)
3172 break;
3173 }
3174 rb_method_entry_set(rb_singleton_class(module), id, me, METHOD_VISI_PUBLIC);
3175 }
3176 if (argc == 1) {
3177 return argv[0];
3178 }
3179 return rb_ary_new_from_values(argc, argv);
3180}
3181
3182#ifdef __GNUC__
3183#pragma push_macro("rb_method_basic_definition_p")
3184#undef rb_method_basic_definition_p
3185#endif
3186int
3187rb_method_basic_definition_p(VALUE klass, ID id)
3188{
3189 const rb_callable_method_entry_t *cme;
3190 if (!klass) return TRUE; /* hidden object cannot be overridden */
3191 cme = rb_callable_method_entry(klass, id);
3192 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
3193}
3194#ifdef __GNUC__
3195#pragma pop_macro("rb_method_basic_definition_p")
3196#endif
3197
3198static VALUE
3199call_method_entry(rb_execution_context_t *ec, VALUE defined_class, VALUE obj, ID id,
3200 const rb_callable_method_entry_t *cme, int argc, const VALUE *argv, int kw_splat)
3201{
3202 VALUE passed_block_handler = vm_passed_block_handler(ec);
3203 VALUE result = rb_vm_call_kw(ec, obj, id, argc, argv, cme, kw_splat);
3204 vm_passed_block_handler_set(ec, passed_block_handler);
3205 return result;
3206}
3207
3208static VALUE
3209basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
3210 VALUE mid, VALUE priv)
3211{
3212 VALUE defined_class, args[2];
3213 const ID rtmid = idRespond_to_missing;
3214 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, rtmid, &defined_class);
3215
3216 if (!cme || METHOD_ENTRY_BASIC(cme)) return Qundef;
3217 args[0] = mid;
3218 args[1] = priv;
3219 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args, RB_NO_KEYWORDS);
3220}
3221
3222static inline int
3223basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
3224{
3225 VALUE klass = CLASS_OF(obj);
3226 VALUE ret;
3227
3228 switch (method_boundp(klass, id, pub|BOUND_RESPONDS)) {
3229 case 2:
3230 return FALSE;
3231 case 0:
3232 ret = basic_obj_respond_to_missing(ec, klass, obj, ID2SYM(id),
3233 RBOOL(!pub));
3234 return RTEST(ret) && !UNDEF_P(ret);
3235 default:
3236 return TRUE;
3237 }
3238}
3239
3240static int
3241vm_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
3242{
3243 VALUE defined_class;
3244 const ID resid = idRespond_to;
3245 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, resid, &defined_class);
3246
3247 if (!cme) return -1;
3248 if (METHOD_ENTRY_BASIC(cme)) {
3249 return -1;
3250 }
3251 else {
3252 int argc = 1;
3253 VALUE args[2];
3254 VALUE result;
3255
3256 args[0] = ID2SYM(id);
3257 args[1] = Qtrue;
3258 if (priv) {
3259 argc = rb_method_entry_arity((const rb_method_entry_t *)cme);
3260 if (argc > 2) {
3261 rb_raise(rb_eArgError,
3262 "respond_to? must accept 1 or 2 arguments (requires %d)",
3263 argc);
3264 }
3265 if (argc != 1) {
3266 argc = 2;
3267 }
3268 else if (!NIL_P(ruby_verbose)) {
3269 VALUE location = rb_method_entry_location((const rb_method_entry_t *)cme);
3271 "%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") uses"
3272 " the deprecated method signature, which takes one parameter",
3273 (RCLASS_SINGLETON_P(klass) ? obj : klass),
3274 (RCLASS_SINGLETON_P(klass) ? '.' : '#'),
3275 QUOTE_ID(id));
3276 if (!NIL_P(location)) {
3277 VALUE path = RARRAY_AREF(location, 0);
3278 VALUE line = RARRAY_AREF(location, 1);
3279 if (!NIL_P(path)) {
3281 RSTRING_PTR(path), NUM2INT(line),
3282 "respond_to? is defined here");
3283 }
3284 }
3285 }
3286 }
3287 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args, RB_NO_KEYWORDS);
3288 return RTEST(result);
3289 }
3290}
3291
3292int
3293rb_obj_respond_to(VALUE obj, ID id, int priv)
3294{
3295 rb_execution_context_t *ec = GET_EC();
3296 return rb_ec_obj_respond_to(ec, obj, id, priv);
3297}
3298
3299int
3300rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int priv)
3301{
3302 VALUE klass = CLASS_OF(obj);
3303 int ret = vm_respond_to(ec, klass, obj, id, priv);
3304 if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
3305 return ret;
3306}
3307
3308int
3310{
3311 return rb_obj_respond_to(obj, id, FALSE);
3312}
3313
3314
3315/*
3316 * call-seq:
3317 * obj.respond_to?(symbol, include_all=false) -> true or false
3318 * obj.respond_to?(string, include_all=false) -> true or false
3319 *
3320 * Returns +true+ if _obj_ responds to the given method. Private and
3321 * protected methods are included in the search only if the optional
3322 * second parameter evaluates to +true+.
3323 *
3324 * If the method is not implemented,
3325 * as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
3326 * false is returned.
3327 *
3328 * If the method is not defined, <code>respond_to_missing?</code>
3329 * method is called and the result is returned.
3330 *
3331 * When the method name parameter is given as a string, the string is
3332 * converted to a symbol.
3333 */
3334
3335static VALUE
3336obj_respond_to(int argc, VALUE *argv, VALUE obj)
3337{
3338 VALUE mid, priv;
3339 ID id;
3340 rb_execution_context_t *ec = GET_EC();
3341
3342 rb_scan_args(argc, argv, "11", &mid, &priv);
3343 if (!(id = rb_check_id(&mid))) {
3344 VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
3345 rb_to_symbol(mid), priv);
3346 if (UNDEF_P(ret)) ret = Qfalse;
3347 return ret;
3348 }
3349 return RBOOL(basic_obj_respond_to(ec, obj, id, !RTEST(priv)));
3350}
3351
3352/*
3353 * call-seq:
3354 * obj.respond_to_missing?(symbol, include_all) -> true or false
3355 * obj.respond_to_missing?(string, include_all) -> true or false
3356 *
3357 * DO NOT USE THIS DIRECTLY.
3358 *
3359 * Hook method to return whether the _obj_ can respond to _id_ method
3360 * or not.
3361 *
3362 * When the method name parameter is given as a string, the string is
3363 * converted to a symbol.
3364 *
3365 * See #respond_to?, and the example of BasicObject.
3366 */
3367static VALUE
3368obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
3369{
3370 return Qfalse;
3371}
3372
3373void
3374Init_eval_method(void)
3375{
3376 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
3377 rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2);
3378
3379 rb_define_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
3380 rb_define_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
3381 rb_define_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
3382 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
3383 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
3384 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
3385 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
3386 rb_define_private_method(rb_cModule, "ruby2_keywords", rb_mod_ruby2_keywords, -1);
3387
3388 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
3389 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
3390 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
3391 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
3392 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
3393 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
3394
3396 "public", top_public, -1);
3398 "private", top_private, -1);
3400 "ruby2_keywords", top_ruby2_keywords, -1);
3401
3402 {
3403#define REPLICATE_METHOD(klass, id) do { \
3404 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3405 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
3406 } while (0)
3407
3408 REPLICATE_METHOD(rb_eException, idMethodMissing);
3409 REPLICATE_METHOD(rb_eException, idRespond_to);
3410 REPLICATE_METHOD(rb_eException, idRespond_to_missing);
3411 }
3412}
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#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...
Definition atomic.h:118
#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...
Definition atomic.h:129
#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.
Definition class.c:2795
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:420
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.
Definition class.c:3133
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:206
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#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.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:130
void rb_notimplement(void)
Definition error.c:3839
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:475
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
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.
Definition error.c:439
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_eException
Mother of all exceptions.
Definition error.c:1422
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_mKernel
Kernel module.
Definition object.c:61
VALUE rb_cModule
Module class.
Definition object.c:63
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:175
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
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.
Definition vm_method.c:2223
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:942
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1762
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition variable.c:136
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3309
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:216
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1602
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2606
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition vm_method.c:2186
void rb_remove_method(VALUE klass, const char *name)
Removes a method.
Definition vm_method.c:2035
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1608
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:319
void rb_remove_method_id(VALUE klass, ID mid)
Identical to rb_remove_method(), except it accepts the method name as ID.
Definition vm_method.c:2029
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.
Definition vm_method.c:787
int rb_method_boundp(VALUE klass, ID id, int ex)
Queries if the klass has this method.
Definition vm_method.c:2147
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3293
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1145
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:12576
ID rb_to_id(VALUE str)
Definition string.c:12566
int capa
Designed capacity of the buffer.
Definition io.h:11
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
Definition vm_method.c:378
Definition method.h:63
CREF (Class REFerence)
Definition method.h:45
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:204
size_t(* dsize)(const void *)
This function is to query the size of the underlying memory regions.
Definition rtypeddata.h:244
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rtypeddata.h:234
struct rb_data_type_struct::@54 function
Function pointers.
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:211
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:313
Definition method.h:55
rb_cref_t * cref
class reference, should be marked
Definition method.h:137
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:136
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376