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