Ruby 4.1.0dev (2026-10-02 revision bac5fe37358bdf2c0dc72c676eec8ca34392b970)
vm_method.c (bac5fe37358bdf2c0dc72c676eec8ca34392b970)
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 long len = RARRAY_LEN(arg->origins);
427 for (long 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 (long 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
1386void
1387rb_add_refined_method_entry(VALUE refined_class, ID mid)
1388{
1389 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
1390
1391 if (me) {
1392 make_method_entry_refined(refined_class, me);
1393 rb_clear_method_cache(refined_class, mid);
1394 }
1395 else {
1396 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
1397 }
1398}
1399
1400static void
1401check_override_opt_method_i(VALUE klass, VALUE arg)
1402{
1403 if (RB_TYPE_P(klass, T_ICLASS)) {
1404 // ICLASS from a module's subclass list: check the includer and
1405 // recurse into the includer's T_CLASS subclasses.
1406 VALUE includer = RCLASS_INCLUDER(klass);
1407 if (!UNDEF_P(includer) && includer) {
1408 check_override_opt_method_i(includer, arg);
1409 }
1410 return;
1411 }
1412
1413 ID mid = (ID)arg;
1414 const rb_method_entry_t *me, *newme;
1415
1416 if (vm_redefinition_check_flag(klass)) {
1417 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1418 if (me) {
1419 newme = rb_method_entry(klass, mid);
1420 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1421 }
1422 }
1423 rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
1424}
1425
1426static void
1427check_override_opt_method(VALUE klass, VALUE mid)
1428{
1429 if (rb_vm_check_optimizable_mid(mid)) {
1430 check_override_opt_method_i(klass, mid);
1431 }
1432}
1433
1434VALUE
1435rb_zsuper_to_super(int argc, VALUE *argv, VALUE self)
1436{
1437 return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
1438}
1439
1440static inline rb_method_entry_t* search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined);
1441static void
1442method_entry_modify_check(VALUE klass, rb_method_type_t type)
1443{
1444 ASSERT_vm_unlocking();
1445 if (type != VM_METHOD_TYPE_REFINED) {
1446 rb_class_modify_check(NIL_P(klass) ? rb_cObject : klass);
1447 }
1448}
1449
1450/* rb_method_entry_make() runs under the VM lock, where it must not warn:
1451 * rb_warn() dispatches Warning.warn and writes to $stderr, either of which can
1452 * check for interrupts. It formats the message instead, and the caller emits
1453 * it once the lock is released. */
1455 VALUE redefined; /* $VERBOSE only */
1456 VALUE problem;
1457};
1458
1459static void
1460method_entry_warnings_emit(const struct method_entry_warnings *warnings)
1461{
1462 if (warnings->redefined) rb_warning("%"PRIsVALUE, warnings->redefined);
1463 if (warnings->problem) rb_warn("%"PRIsVALUE, warnings->problem);
1464}
1465
1466/*
1467 * klass->method_table[mid] = method_entry(defined_class, visi, def)
1468 *
1469 * If def is given (!= NULL), then just use it and ignore original_id and otps.
1470 * If not given, then make a new def with original_id and opts.
1471 */
1472static rb_method_entry_t *
1473rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
1474 rb_method_type_t type, rb_method_definition_t *def, ID original_id, void *opts,
1475 struct method_entry_warnings *warnings)
1476{
1478 struct rb_id_table *mtbl;
1479 st_data_t data;
1480 int make_refined = 0;
1481 VALUE orig_klass;
1482 bool turn_zsuper_to_super = false;
1483
1484 if (NIL_P(klass)) {
1485 klass = rb_cObject;
1486 }
1487 orig_klass = klass;
1488
1489 if (!RCLASS_SINGLETON_P(klass) &&
1490 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
1491 type != VM_METHOD_TYPE_ZSUPER) {
1492 switch (mid) {
1493 case idInitialize:
1494 case idInitialize_copy:
1495 case idInitialize_clone:
1496 case idInitialize_dup:
1497 case idRespond_to_missing:
1498 visi = METHOD_VISI_PRIVATE;
1499 }
1500 }
1501
1502 if (RB_TYPE_P(klass, T_MODULE) && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1503 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
1504 if (type == VM_METHOD_TYPE_ZSUPER) {
1505 turn_zsuper_to_super = true;
1506 }
1507 rb_add_refined_method_entry(refined_class, mid);
1508 }
1509 if (type == VM_METHOD_TYPE_REFINED) {
1510 rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1511 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1512 }
1513 else {
1514 klass = RCLASS_ORIGIN(klass);
1515 if (klass != orig_klass) {
1516 rb_clear_method_cache(orig_klass, mid);
1517 }
1518 }
1519 mtbl = RCLASS_WRITABLE_M_TBL(klass);
1520
1521 /* check re-definition */
1522 if (rb_id_table_lookup(mtbl, mid, &data)) {
1523 rb_method_entry_t *old_me = (rb_method_entry_t *)data;
1524 rb_method_definition_t *old_def = old_me->def;
1525
1526 if (rb_method_definition_eq(old_def, def)) return old_me;
1527 rb_vm_check_redefinition_opt_method(old_me, klass);
1528
1529 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1530
1531 if (RTEST(ruby_verbose) &&
1532 type != VM_METHOD_TYPE_UNDEF &&
1533 (old_def->aliased == false) &&
1534 (!old_def->no_redef_warning) &&
1535 !make_refined &&
1536 old_def->type != VM_METHOD_TYPE_UNDEF &&
1537 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1538 old_def->type != VM_METHOD_TYPE_ALIAS) {
1539 const rb_iseq_t *iseq = 0;
1540
1541 switch (old_def->type) {
1542 case VM_METHOD_TYPE_ISEQ:
1543 iseq = def_iseq_ptr(old_def);
1544 break;
1545 case VM_METHOD_TYPE_BMETHOD:
1546 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1547 break;
1548 default:
1549 break;
1550 }
1551 if (iseq) {
1552 warnings->redefined = rb_sprintf(
1553 "method redefined; discarding old %"PRIsVALUE"\n%"PRIsVALUE":%d: warning: previous definition of %"PRIsVALUE" was here",
1554 rb_id2str(mid),
1555 rb_iseq_path(iseq),
1556 ISEQ_BODY(iseq)->location.first_lineno,
1557 rb_id2str(old_def->original_id)
1558 );
1559 }
1560 else {
1561 warnings->redefined = rb_sprintf("method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1562 }
1563 }
1564 }
1565
1566 /* create method entry */
1567 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1568 if (def == NULL) {
1569 def = rb_method_definition_create(type, original_id);
1570 if (turn_zsuper_to_super) {
1571 def->type = VM_METHOD_TYPE_CFUNC;
1572 def->body.cfunc.func = (rb_cfunc_t)rb_zsuper_to_super;
1573 def->body.cfunc.invoker = ractor_safe_call_cfunc_m1;
1574 def->body.cfunc.argc = -1;
1575 }
1576 RB_OBJ_WRITE(me, &def->original_module, me->owner);
1577 }
1578 rb_method_definition_set(me, def, opts);
1579
1580 rb_clear_method_cache(klass, mid);
1581
1582 /* check mid */
1583 if (klass == rb_cObject) {
1584 switch (mid) {
1585 case idInitialize:
1586 case idRespond_to_missing:
1587 case idMethodMissing:
1588 case idRespond_to:
1589 warnings->problem = rb_sprintf("redefining Object#%s may cause infinite loop", rb_id2name(mid));
1590 }
1591 }
1592 /* check mid */
1593 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1594 if (type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1595 warnings->problem = rb_sprintf("redefining '%s' may cause serious problems", rb_id2name(mid));
1596 }
1597 }
1598
1599 if (make_refined) {
1600 make_method_entry_refined(klass, me);
1601 }
1602
1603 rb_method_table_insert(klass, mtbl, mid, me);
1604
1605 VM_ASSERT(me->def != NULL);
1606
1607 /* check optimized method override by a prepended module */
1608 if (RB_TYPE_P(orig_klass, T_MODULE)) {
1609 check_override_opt_method(klass, (VALUE)mid);
1610 }
1611
1612 return me;
1613}
1614
1615static st_table *
1616overloaded_cme_table(void)
1617{
1618 return &GET_VM()->overloaded_cme_table;
1619}
1620
1621#if VM_CHECK_MODE > 0
1622static int
1623vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1624{
1625 fprintf(stderr, "key: "); rp(key);
1626 fprintf(stderr, "val: "); rp(val);
1627 return ST_CONTINUE;
1628}
1629
1630void
1631rb_vm_dump_overloaded_cme_table(void)
1632{
1633 fprintf(stderr, "== rb_vm_dump_overloaded_cme_table\n");
1634 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1635}
1636#endif
1637
1638static int
1639lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
1640{
1641 if (existing) {
1642 const rb_callable_method_entry_t *cme = (const rb_callable_method_entry_t *)*key;
1643 const rb_callable_method_entry_t *monly_cme = (const rb_callable_method_entry_t *)*value;
1644 const rb_callable_method_entry_t **ptr = (const rb_callable_method_entry_t **)data;
1645
1646 if (rb_objspace_garbage_object_p((VALUE)cme) ||
1647 rb_objspace_garbage_object_p((VALUE)monly_cme)) {
1648 *ptr = NULL;
1649 return ST_DELETE;
1650 }
1651 else {
1652 *ptr = monly_cme;
1653 }
1654 }
1655
1656 return ST_STOP;
1657}
1658
1659static const rb_callable_method_entry_t *
1660lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1661{
1662 ASSERT_vm_locking();
1663
1664 const rb_callable_method_entry_t *monly_cme = NULL;
1665 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1666 return monly_cme;
1667}
1668
1669#if VM_CHECK_MODE > 0
1671rb_vm_lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1672{
1673 return lookup_overloaded_cme(cme);
1674}
1675#endif
1676
1677static void
1678delete_overloaded_cme(const rb_callable_method_entry_t *cme)
1679{
1680 st_data_t cme_data = (st_data_t)cme;
1681 ASSERT_vm_locking();
1682 st_delete(overloaded_cme_table(), &cme_data, NULL);
1683}
1684
1685static const rb_callable_method_entry_t *
1686get_overloaded_cme(const rb_callable_method_entry_t *cme)
1687{
1688 const rb_callable_method_entry_t *monly_cme = lookup_overloaded_cme(cme);
1689
1690 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1691 return monly_cme;
1692 }
1693 else {
1694 // create
1695 rb_method_definition_t *def = rb_method_definition_create(VM_METHOD_TYPE_ISEQ, cme->def->original_id);
1696 rb_method_entry_t *me = rb_method_entry_alloc(cme->called_id,
1697 cme->owner,
1698 cme->defined_class,
1699 def,
1700 false);
1701
1702 RB_OBJ_WRITE(me, &def->body.iseq.cref, cme->def->body.iseq.cref);
1703 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, ISEQ_BODY(cme->def->body.iseq.iseqptr)->mandatory_only_iseq);
1704 RB_OBJ_WRITE(me, &def->original_module, cme->def->original_module);
1705
1706 ASSERT_vm_locking();
1707 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1708
1709 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1710 return (rb_callable_method_entry_t *)me;
1711 }
1712}
1713
1715rb_check_overloaded_cme(const rb_callable_method_entry_t *cme, const struct rb_callinfo * const ci)
1716{
1717 if (UNLIKELY(cme->def->iseq_overload) &&
1718 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1719 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1720 (int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1721 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ, "type: %d", cme->def->type); // iseq_overload is marked only on ISEQ methods
1722
1723 cme = get_overloaded_cme(cme);
1724
1725 VM_ASSERT(cme != NULL);
1726 METHOD_ENTRY_CACHED_SET((struct rb_callable_method_entry_struct *)cme);
1727 }
1728
1729 return cme;
1730}
1731
1732static inline void stack_check(rb_execution_context_t *ec);
1733
1734#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1735 const VALUE arg = ID2SYM(mid); \
1736 VALUE recv_class = (klass); \
1737 ID hook_id = (hook); \
1738 if (RCLASS_SINGLETON_P((klass))) { \
1739 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1740 hook_id = singleton_##hook; \
1741 } \
1742 rb_funcallv_uncached(recv_class, hook_id, 1, &arg); \
1743 } while (0)
1744
1745static void
1746method_added(VALUE klass, ID mid, const rb_method_entry_t *me)
1747{
1748 if (ruby_running) {
1749 /* [Bug #22179] Record the just-defined method entry for method coverage
1750 * so the result no longer depends on walking the heap (and thus on GC
1751 * timing). */
1752 if (me) rb_vm_coverage_record_me(me);
1753 CALL_METHOD_HOOK(klass, added, mid);
1754 }
1755}
1756
1757void
1758rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
1759{
1760 const rb_method_entry_t *me;
1761 struct method_entry_warnings warnings = {0};
1762
1763 method_entry_modify_check(klass, type);
1764
1765 RB_VM_LOCKING() {
1766 me = rb_method_entry_make(klass, mid, klass, visi, type, NULL, mid, opts, &warnings);
1767 }
1768
1769 method_entry_warnings_emit(&warnings);
1770
1771 if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) {
1772 method_added(klass, mid, me);
1773 }
1774}
1775
1776void
1777rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
1778{
1779 struct { /* should be same fields with rb_method_iseq_struct */
1780 const rb_iseq_t *iseqptr;
1781 rb_cref_t *cref;
1782 } iseq_body;
1783
1784 iseq_body.iseqptr = iseq;
1785 iseq_body.cref = cref;
1786
1787 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1788}
1789
1790static rb_method_entry_t *
1791method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
1792 rb_method_visibility_t visi, VALUE defined_class)
1793{
1794 rb_method_entry_t *newme;
1795 struct method_entry_warnings warnings = {0};
1796
1797 method_entry_modify_check(klass, me->def->type);
1798
1799 RB_VM_LOCKING() {
1800 newme = rb_method_entry_make(klass, mid, defined_class, visi,
1801 me->def->type, me->def, 0, NULL, &warnings);
1802 if (newme == me) {
1803 me->def->no_redef_warning = TRUE;
1804 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1805 }
1806 }
1807
1808 method_entry_warnings_emit(&warnings);
1809
1810 method_added(klass, mid, newme);
1811 return newme;
1812}
1813
1815rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_visibility_t visi)
1816{
1817 return method_entry_set(klass, mid, me, visi, klass);
1818}
1819
1820#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1821
1822static void
1823propagate_alloc_func(VALUE subclass, VALUE arg)
1824{
1825 if (RB_TYPE_P(subclass, T_CLASS) &&
1826 !RCLASS_SINGLETON_P(subclass) &&
1827 !FL_TEST_RAW(subclass, RCLASS_ALLOCATOR_DEFINED)) {
1828 RCLASS_SET_ALLOCATOR(subclass, (rb_alloc_func_t)arg);
1829 rb_class_foreach_subclass(subclass, propagate_alloc_func, arg);
1830 }
1831}
1832
1833void
1834rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
1835{
1836 Check_Type(klass, T_CLASS);
1837 if (RCLASS_SINGLETON_P(klass)) {
1838 rb_raise(rb_eTypeError, "can't define an allocator for a singleton class");
1839 }
1840 RCLASS_SET_ALLOCATOR(klass, func);
1841 FL_SET_RAW(klass, RCLASS_ALLOCATOR_DEFINED);
1842 rb_class_foreach_subclass(klass, propagate_alloc_func, (VALUE)func);
1843}
1844
1845void
1847{
1848 Check_Type(klass, T_CLASS);
1849 RCLASS_SET_ALLOCATOR(klass, UNDEF_ALLOC_FUNC);
1850 FL_SET_RAW(klass, RCLASS_ALLOCATOR_DEFINED);
1851 rb_class_foreach_subclass(klass, propagate_alloc_func, (VALUE)UNDEF_ALLOC_FUNC);
1852}
1853
1856{
1857 RBIMPL_ASSERT_TYPE(klass, T_CLASS);
1858
1859 rb_alloc_func_t allocator = RCLASS_ALLOCATOR(klass);
1860 if (allocator == UNDEF_ALLOC_FUNC) return 0;
1861 RUBY_ASSERT(allocator);
1862 return allocator;
1863}
1864
1865const rb_method_entry_t *
1866rb_method_entry_at(VALUE klass, ID id)
1867{
1868 return lookup_method_table(klass, id);
1869}
1870
1871static inline rb_method_entry_t*
1872search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
1873{
1874 rb_method_entry_t *me = NULL;
1875
1876 RB_DEBUG_COUNTER_INC(mc_search);
1877
1878 for (; klass; klass = RCLASS_SUPER(klass)) {
1879 RB_DEBUG_COUNTER_INC(mc_search_super);
1880 if ((me = lookup_method_table(klass, id)) != 0) {
1881 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1882 me->def->body.refined.orig_me) {
1883 break;
1884 }
1885 }
1886 }
1887
1888 if (defined_class_ptr) *defined_class_ptr = klass;
1889
1890 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1891
1892 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me),
1893 "invalid me, mid:%s, klass:%"PRIsVALUE"(%s)",
1894 rb_id2name(id),
1895 RTEST(rb_mod_name(klass)) ? rb_mod_name(klass) : rb_str_new_lit("anonymous"),
1896 rb_obj_info(klass));
1897 return me;
1898}
1899
1900static inline rb_method_entry_t*
1901search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
1902{
1903 return search_method0(klass, id, defined_class_ptr, false);
1904}
1905
1906static rb_method_entry_t *
1907search_method_protect(VALUE klass, ID id, VALUE *defined_class_ptr)
1908{
1909 rb_method_entry_t *me = search_method(klass, id, defined_class_ptr);
1910
1911 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1912 return me;
1913 }
1914 else {
1915 return NULL;
1916 }
1917}
1918
1919const rb_method_entry_t *
1920rb_method_entry(VALUE klass, ID id)
1921{
1922 return search_method_protect(klass, id, NULL);
1923}
1924
1925static inline const rb_callable_method_entry_t *
1926prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_t * const me, int create)
1927{
1928 struct rb_id_table *mtbl;
1929 const rb_callable_method_entry_t *cme;
1930 VALUE cme_data;
1931 int cme_found = 0;
1932
1933 if (me) {
1934 if (me->defined_class == 0) {
1935 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1936 VM_ASSERT_TYPE2(defined_class, T_ICLASS, T_MODULE);
1937
1938 mtbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
1939 if (mtbl && rb_id_table_lookup(mtbl, id, &cme_data)) {
1940 cme = (rb_callable_method_entry_t *)cme_data;
1941 cme_found = 1;
1942 }
1943 if (cme_found) {
1944 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1945 VM_ASSERT(callable_method_entry_p(cme));
1946 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1947 }
1948 else if (create) {
1949 if (!mtbl) {
1950 mtbl = rb_id_table_create(0);
1951 RCLASS_WRITE_CALLABLE_M_TBL(defined_class, mtbl);
1952 }
1953 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1954 rb_id_table_insert(mtbl, id, (VALUE)cme);
1955 RB_OBJ_WRITTEN(defined_class, Qundef, (VALUE)cme);
1956 VM_ASSERT(callable_method_entry_p(cme));
1957 }
1958 else {
1959 return NULL;
1960 }
1961 }
1962 else {
1963 cme = (const rb_callable_method_entry_t *)me;
1964 VM_ASSERT(callable_method_entry_p(cme));
1965 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1966 }
1967 return cme;
1968 }
1969 else {
1970 return NULL;
1971 }
1972}
1973
1974/* A hook like this fires from C with no call site to cache into except for the gccct table,
1975 * which is often cleared anyway. It would leave a permanent CC behind (tied to the class) if it
1976 * created one, so we try to avoid it. */
1977VALUE
1978rb_funcallv_uncached(VALUE recv, ID mid, int argc, const VALUE *argv)
1979{
1980 VALUE defined_class;
1981 const rb_method_entry_t *me = search_method(CLASS_OF(recv), mid, &defined_class);
1982
1983 if (UNLIKELY(UNDEFINED_METHOD_ENTRY_P(me))) {
1984 return rb_funcallv(recv, mid, argc, argv);
1985 }
1986
1987 const rb_callable_method_entry_t *cme;
1988
1989 if (UNLIKELY(me->defined_class == 0)) {
1990 // produce a transient CME that will get collected
1991 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1992 }
1993 else {
1994 cme = (const rb_callable_method_entry_t *)me;
1995 }
1996
1997 rb_execution_context_t *ec = GET_EC();
1998 stack_check(ec);
1999 return rb_vm_call_kw(ec, recv, mid, argc, argv, cme, RB_NO_KEYWORDS);
2000}
2001
2002static const rb_callable_method_entry_t *
2003complemented_callable_method_entry(VALUE klass, ID id)
2004{
2005 VALUE defined_class;
2006 rb_method_entry_t *me = search_method(klass, id, &defined_class);
2007 return prepare_callable_method_entry(defined_class, id, me, FALSE);
2008}
2009
2010static const rb_callable_method_entry_t *
2011cached_callable_method_entry(VALUE klass, ID mid)
2012{
2013 ASSERT_vm_locking();
2014
2015 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
2016 VALUE ccs_data;
2017
2018 if (cc_tbl && rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
2019 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
2020 VM_ASSERT(vm_ccs_p(ccs));
2021
2022 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
2023 VM_ASSERT(ccs->cme->called_id == mid);
2024 RB_DEBUG_COUNTER_INC(ccs_found);
2025 return ccs->cme;
2026 }
2027 else {
2028 rb_vm_barrier();
2029
2030 rb_managed_id_table_delete(cc_tbl, mid);
2031 rb_vm_ccs_invalidate_and_free(ccs);
2032 }
2033 }
2034
2035 RB_DEBUG_COUNTER_INC(ccs_not_found);
2036 return NULL;
2037}
2038
2039static void
2040cache_callable_method_entry(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
2041{
2042 ASSERT_vm_locking();
2043 VM_ASSERT(cme != NULL);
2044
2045 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
2046 VALUE ccs_data;
2047
2048 if (!cc_tbl) {
2049 cc_tbl = rb_vm_cc_table_create(2);
2050 RCLASS_WRITE_CC_TBL(klass, cc_tbl);
2051 }
2052
2053 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
2054#if VM_CHECK_MODE > 0
2055 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
2056 VM_ASSERT(ccs->cme == cme);
2057#endif
2058 }
2059 else {
2060 if (rb_multi_ractor_p()) {
2061 VALUE new_cc_tbl = rb_vm_cc_table_dup(cc_tbl);
2062 vm_ccs_create(klass, new_cc_tbl, mid, cme);
2063 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), new_cc_tbl);
2064 }
2065 else {
2066 vm_ccs_create(klass, cc_tbl, mid, cme);
2067 }
2068 }
2069}
2070
2071static const rb_callable_method_entry_t *
2072negative_cme(ID mid)
2073{
2074 rb_vm_t *vm = GET_VM();
2075 const rb_callable_method_entry_t *cme;
2076 VALUE cme_data;
2077
2078 if (rb_id_table_lookup(&vm->negative_cme_table, mid, &cme_data)) {
2079 cme = (rb_callable_method_entry_t *)cme_data;
2080 }
2081 else {
2082 cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid, Qnil, Qnil, NULL, false);
2083 rb_id_table_insert(&vm->negative_cme_table, mid, (VALUE)cme);
2084 }
2085
2086 VM_ASSERT(cme != NULL);
2087 return cme;
2088}
2089
2090static const rb_callable_method_entry_t *
2091callable_method_entry_or_negative(VALUE klass, ID mid, VALUE *defined_class_ptr)
2092{
2093 const rb_callable_method_entry_t *cme;
2094
2095 VM_ASSERT(!SPECIAL_CONST_P(klass));
2096
2097 if (RB_BUILTIN_TYPE(klass) == T_NONE) {
2098 // If we find a T_NONE here, it's most likely we called CLASS_OF(obj) on a
2099 // garbage collected object (the freelist is stored in the class pointer),
2100 // but it's possible that just the class was GC'd.
2101 // This message intentionally tries to imply the former, but make an
2102 // accurate statement for either case.
2103 rb_bug("attempted to search method '%s' on a garbage collected object",
2104 rb_id2name(mid));
2105 }
2106
2107 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2108
2109 /* Fast path: lock-free read from cache */
2110 VALUE cc_tbl = RUBY_ATOMIC_VALUE_LOAD(RCLASS_WRITABLE_CC_TBL(klass));
2111 if (cc_tbl) {
2112 VALUE ccs_data;
2113 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
2114 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
2115 VM_ASSERT(vm_ccs_p(ccs));
2116
2117 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
2118 VM_ASSERT(ccs->cme->called_id == mid);
2119 if (defined_class_ptr != NULL) *defined_class_ptr = ccs->cme->defined_class;
2120 RB_DEBUG_COUNTER_INC(ccs_found);
2121 return ccs->cme;
2122 }
2123 }
2124 }
2125
2126 /* Slow path: need to lock and potentially populate cache */
2127 RB_VM_LOCKING() {
2128 cme = cached_callable_method_entry(klass, mid);
2129
2130 if (cme) {
2131 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
2132 }
2133 else {
2134 VALUE defined_class;
2135 rb_method_entry_t *me = search_method(klass, mid, &defined_class);
2136 if (defined_class_ptr) *defined_class_ptr = defined_class;
2137
2138 if (me != NULL) {
2139 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
2140 }
2141 else {
2142 cme = negative_cme(mid);
2143 }
2144
2145 cache_callable_method_entry(klass, mid, cme);
2146 }
2147 }
2148
2149 return cme;
2150}
2151
2152// This is exposed for YJIT so that we can make assumptions that methods are
2153// not defined.
2155rb_callable_method_entry_or_negative(VALUE klass, ID mid)
2156{
2157 return callable_method_entry_or_negative(klass, mid, NULL);
2158}
2159
2160static const rb_callable_method_entry_t *
2161callable_method_entry(VALUE klass, ID mid, VALUE *defined_class_ptr)
2162{
2163 const rb_callable_method_entry_t *cme;
2164 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
2165 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
2166}
2167
2169rb_callable_method_entry(VALUE klass, ID mid)
2170{
2171 return callable_method_entry(klass, mid, NULL);
2172}
2173
2174static const rb_method_entry_t *resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr);
2175
2176static const rb_method_entry_t *
2177method_entry_resolve_refinement(VALUE klass, ID id, int with_refinement, VALUE *defined_class_ptr)
2178{
2179 const rb_method_entry_t *me = search_method_protect(klass, id, defined_class_ptr);
2180
2181 if (me) {
2182 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2183 if (with_refinement) {
2184 const rb_cref_t *cref = rb_vm_cref();
2185 VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
2186 me = resolve_refined_method(refinements, me, defined_class_ptr);
2187 }
2188 else {
2189 me = resolve_refined_method(Qnil, me, defined_class_ptr);
2190 }
2191
2192 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
2193 }
2194 }
2195
2196 return me;
2197}
2198
2199const rb_method_entry_t *
2200rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2201{
2202 return method_entry_resolve_refinement(klass, id, TRUE, defined_class_ptr);
2203}
2204
2205static const rb_callable_method_entry_t *
2206callable_method_entry_refinements0(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements,
2207 const rb_callable_method_entry_t *cme)
2208{
2209 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
2210 return cme;
2211 }
2212 else {
2213 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2214 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, with_refinements, dcp);
2215 return prepare_callable_method_entry(*dcp, id, me, TRUE);
2216 }
2217}
2218
2219static const rb_callable_method_entry_t *
2220callable_method_entry_refinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
2221{
2222 const rb_callable_method_entry_t *cme = callable_method_entry(klass, id, defined_class_ptr);
2223 return callable_method_entry_refinements0(klass, id, defined_class_ptr, with_refinements, cme);
2224}
2225
2227rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2228{
2229 return callable_method_entry_refinements(klass, id, defined_class_ptr, true);
2230}
2231
2232static const rb_callable_method_entry_t *
2233callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2234{
2235 return callable_method_entry_refinements(klass, id, defined_class_ptr, false);
2236}
2237
2238const rb_method_entry_t *
2239rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2240{
2241 return method_entry_resolve_refinement(klass, id, FALSE, defined_class_ptr);
2242}
2243
2245rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2246{
2247 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2248 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, FALSE, dcp);
2249 return prepare_callable_method_entry(*dcp, id, me, TRUE);
2250}
2251
2252static const rb_method_entry_t *
2253resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
2254{
2255 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2256 VALUE refinement;
2257 const rb_method_entry_t *tmp_me;
2258 VALUE super;
2259
2260 refinement = find_refinement(refinements, me->owner);
2261 if (!NIL_P(refinement)) {
2262 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
2263
2264 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
2265 return tmp_me;
2266 }
2267 }
2268
2269 tmp_me = me->def->body.refined.orig_me;
2270 if (tmp_me) {
2271 if (!tmp_me->defined_class) {
2272 VM_ASSERT_TYPE(tmp_me->owner, T_MODULE);
2273 }
2274 else if (defined_class_ptr) {
2275 *defined_class_ptr = tmp_me->defined_class;
2276 }
2277 return tmp_me;
2278 }
2279
2280 super = RCLASS_SUPER(me->owner);
2281 if (!super) {
2282 return 0;
2283 }
2284
2285 me = search_method_protect(super, me->called_id, defined_class_ptr);
2286 }
2287 return me;
2288}
2289
2290const rb_method_entry_t *
2291rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
2292{
2293 return resolve_refined_method(refinements, me, NULL);
2294}
2295
2297rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
2298{
2299 VALUE defined_class = me->defined_class;
2300 const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (const rb_method_entry_t *)me, &defined_class);
2301
2302 if (resolved_me && resolved_me->defined_class == 0) {
2303 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
2304 }
2305 else {
2306 return (const rb_callable_method_entry_t *)resolved_me;
2307 }
2308}
2309
2310static void
2311remove_method(VALUE klass, ID mid)
2312{
2313 VALUE data;
2314 rb_method_entry_t *me = 0;
2315 VALUE self = klass;
2316
2317 rb_class_modify_check(klass);
2318 klass = RCLASS_ORIGIN(klass);
2319 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
2320 rb_warn("removing '%s' may cause serious problems", rb_id2name(mid));
2321 }
2322
2323 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
2324 !(me = (rb_method_entry_t *)data) ||
2325 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
2326 UNDEFINED_REFINED_METHOD_P(me->def)) {
2327 rb_name_err_raise("method '%1$s' not defined in %2$s",
2328 klass, ID2SYM(mid));
2329 }
2330
2331 if (klass != self) {
2332 rb_clear_method_cache(self, mid);
2333 }
2334 rb_clear_method_cache(klass, mid);
2335 rb_id_table_delete(RCLASS_WRITABLE_M_TBL(klass), mid);
2336
2337 rb_vm_check_redefinition_opt_method(me, klass);
2338
2339 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2340 rb_add_refined_method_entry(klass, mid);
2341 }
2342
2343 CALL_METHOD_HOOK(self, removed, mid);
2344}
2345
2346void
2348{
2349 remove_method(klass, mid);
2350}
2351
2352void
2353rb_remove_method(VALUE klass, const char *name)
2354{
2355 remove_method(klass, rb_intern(name));
2356}
2357
2358/*
2359 * call-seq:
2360 * remove_method(symbol) -> self
2361 * remove_method(string) -> self
2362 *
2363 * Removes the method identified by _symbol_ from the current
2364 * class. For an example, see Module#undef_method.
2365 * String arguments are converted to symbols.
2366 */
2367
2368static VALUE
2369rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
2370{
2371 int i;
2372
2373 for (i = 0; i < argc; i++) {
2374 VALUE v = argv[i];
2375 ID id = rb_check_id(&v);
2376 if (!id) {
2377 rb_name_err_raise("method '%1$s' not defined in %2$s",
2378 mod, v);
2379 }
2380 remove_method(mod, id);
2381 }
2382 return mod;
2383}
2384
2385static void
2386rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
2387{
2389 VALUE defined_class;
2390 VALUE origin_class = RCLASS_ORIGIN(klass);
2391
2392 me = search_method0(origin_class, name, &defined_class, true);
2393
2394 if (!me && RB_TYPE_P(klass, T_MODULE)) {
2395 me = search_method(rb_cObject, name, &defined_class);
2396 }
2397
2398 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2399 UNDEFINED_REFINED_METHOD_P(me->def)) {
2400 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
2401 }
2402
2403 if (METHOD_ENTRY_VISI(me) != visi) {
2404 rb_vm_check_redefinition_opt_method(me, klass);
2405
2406 if (klass == defined_class || origin_class == defined_class) {
2407 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2408 // Refinement method entries should always be public because the refinement
2409 // search is always performed.
2410 if (me->def->body.refined.orig_me) {
2411 METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
2412 }
2413 }
2414 else {
2415 METHOD_ENTRY_VISI_SET(me, visi);
2416 }
2417 rb_clear_method_cache(klass, name);
2418 }
2419 else {
2420 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
2421 }
2422 }
2423}
2424
2425#define BOUND_PRIVATE 0x01
2426#define BOUND_RESPONDS 0x02
2427
2428static int
2429method_boundp(VALUE klass, ID id, int ex)
2430{
2431 const rb_callable_method_entry_t *cme;
2432
2433 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2434
2435 if (ex & BOUND_RESPONDS) {
2436 cme = rb_callable_method_entry_with_refinements(klass, id, NULL);
2437 }
2438 else {
2439 cme = callable_method_entry_without_refinements(klass, id, NULL);
2440 }
2441
2442 if (cme != NULL) {
2443 if (ex & ~BOUND_RESPONDS) {
2444 switch (METHOD_ENTRY_VISI(cme)) {
2445 case METHOD_VISI_PRIVATE:
2446 return 0;
2447 case METHOD_VISI_PROTECTED:
2448 if (ex & BOUND_RESPONDS) return 0;
2449 default:
2450 break;
2451 }
2452 }
2453
2454 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
2455 if (ex & BOUND_RESPONDS) return 2;
2456 return 0;
2457 }
2458 return 1;
2459 }
2460 return 0;
2461}
2462
2463// deprecated
2464int
2465rb_method_boundp(VALUE klass, ID id, int ex)
2466{
2467 return method_boundp(klass, id, ex);
2468}
2469
2470static void
2471vm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func)
2472{
2473 rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t *)&rb_vm_cref()->scope_visi;
2474 scope_visi->method_visi = method_visi;
2475 scope_visi->module_func = module_func;
2476}
2477
2478void
2479rb_scope_visibility_set(rb_method_visibility_t visi)
2480{
2481 vm_cref_set_visibility(visi, FALSE);
2482}
2483
2484static void
2485scope_visibility_check(void)
2486{
2487 /* Check for public/protected/private/module_function called inside a method */
2488 rb_control_frame_t *cfp = GET_EC()->cfp+1;
2489 if (cfp && CFP_ISEQ(cfp) && ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_METHOD) {
2490 rb_warn("calling %s without arguments inside a method may not have the intended effect",
2491 rb_id2name(rb_frame_this_func()));
2492 }
2493}
2494
2495static void
2496rb_scope_module_func_set(void)
2497{
2498 scope_visibility_check();
2499 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
2500}
2501
2502const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
2503void
2504rb_attr(VALUE klass, ID id, int read, int write, int ex)
2505{
2506 ID attriv;
2507 rb_method_visibility_t visi;
2508 const rb_execution_context_t *ec = GET_EC();
2509 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
2510
2511 if (!ex || !cref) {
2512 visi = METHOD_VISI_PUBLIC;
2513 }
2514 else {
2515 switch (vm_scope_visibility_get(ec)) {
2516 case METHOD_VISI_PRIVATE:
2517 if (vm_scope_module_func_check(ec)) {
2518 rb_warning("attribute accessor as module_function");
2519 }
2520 visi = METHOD_VISI_PRIVATE;
2521 break;
2522 case METHOD_VISI_PROTECTED:
2523 visi = METHOD_VISI_PROTECTED;
2524 break;
2525 default:
2526 visi = METHOD_VISI_PUBLIC;
2527 break;
2528 }
2529 }
2530
2531 attriv = rb_intern_str(rb_sprintf("@%"PRIsVALUE, rb_id2str(id)));
2532 if (read) {
2533 rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, visi);
2534 }
2535 if (write) {
2536 rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, visi);
2537 }
2538}
2539
2540void
2542{
2543 const rb_method_entry_t *me;
2544
2545 if (NIL_P(klass)) {
2546 rb_raise(rb_eTypeError, "no class to undef method");
2547 }
2548 rb_class_modify_check(klass);
2549 if (id == object_id || id == id__id__ || id == id__send__ || id == idInitialize) {
2550 rb_warn("undefining '%s' may cause serious problems", rb_id2name(id));
2551 }
2552
2553 me = search_method(klass, id, 0);
2554 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2555 me = rb_resolve_refined_method(Qnil, me);
2556 }
2557
2558 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2559 UNDEFINED_REFINED_METHOD_P(me->def)) {
2560 rb_method_name_error(klass, rb_id2str(id));
2561 }
2562
2563 rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
2564
2565 CALL_METHOD_HOOK(klass, undefined, id);
2566}
2567
2568/*
2569 * call-seq:
2570 * undef_method(symbol) -> self
2571 * undef_method(string) -> self
2572 *
2573 * Prevents the current class from responding to calls to the named
2574 * method. Contrast this with <code>remove_method</code>, which deletes
2575 * the method from the particular class; Ruby will still search
2576 * superclasses and mixed-in modules for a possible receiver.
2577 * String arguments are converted to symbols.
2578 *
2579 * class Parent
2580 * def hello
2581 * puts "In parent"
2582 * end
2583 * end
2584 * class Child < Parent
2585 * def hello
2586 * puts "In child"
2587 * end
2588 * end
2589 *
2590 *
2591 * c = Child.new
2592 * c.hello
2593 *
2594 *
2595 * class Child
2596 * remove_method :hello # remove from child, still in parent
2597 * end
2598 * c.hello
2599 *
2600 *
2601 * class Child
2602 * undef_method :hello # prevent any calls to 'hello'
2603 * end
2604 * c.hello
2605 *
2606 * <em>produces:</em>
2607 *
2608 * In child
2609 * In parent
2610 * prog.rb:23: undefined method 'hello' for #<Child:0x401b3bb4> (NoMethodError)
2611 */
2612
2613static VALUE
2614rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
2615{
2616 int i;
2617 for (i = 0; i < argc; i++) {
2618 VALUE v = argv[i];
2619 ID id = rb_check_id(&v);
2620 if (!id) {
2621 rb_method_name_error(mod, v);
2622 }
2623 rb_undef(mod, id);
2624 }
2625 return mod;
2626}
2627
2628static rb_method_visibility_t
2629check_definition_visibility(VALUE mod, VALUE mid, bool inc_super)
2630{
2631 ID id = rb_check_id(&mid);
2632 if (!id) return METHOD_VISI_UNDEF;
2633
2634 VALUE lookup_mod = inc_super ? mod : RCLASS_ORIGIN(mod);
2635
2636 const rb_method_entry_t *me = rb_method_entry_without_refinements(lookup_mod, id, NULL);
2637 if (me) {
2638 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) return METHOD_VISI_UNDEF;
2639 if (!inc_super && me->owner != mod) return METHOD_VISI_UNDEF;
2640 return METHOD_ENTRY_VISI(me);
2641 }
2642 return METHOD_VISI_UNDEF;
2643}
2644
2645/*
2646 * call-seq:
2647 * mod.method_defined?(symbol, inherit=true, include_all = false) -> true or false
2648 * mod.method_defined?(string, inherit=true, include_all = false) -> true or false
2649 *
2650 * Returns +true+ if the named method is defined by
2651 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2652 * ancestors.
2653 * By default only public and protected methods are matched, but if _include_all_
2654 * is set the lookup will also consider private methods.
2655 * String arguments are converted to symbols.
2656 *
2657 * module A
2658 * def method1() end
2659 * def protected_method1() end
2660 * protected :protected_method1
2661 * end
2662 * class B
2663 * def method2() end
2664 * def private_method2() end
2665 * private :private_method2
2666 * end
2667 * class C < B
2668 * include A
2669 * def method3() end
2670 * end
2671 *
2672 * A.method_defined? :method1 #=> true
2673 * C.method_defined? "method1" #=> true
2674 * C.method_defined? "method2" #=> true
2675 * C.method_defined? "method2", true #=> true
2676 * C.method_defined? "method2", false #=> false
2677 * C.method_defined? "method3" #=> true
2678 * C.method_defined? "protected_method1" #=> true
2679 * C.method_defined? "method4" #=> false
2680 * C.method_defined? "private_method2" #=> false
2681 * C.method_defined? "private_method2", true, true #=> true
2682 * C.method_defined? "private_method2", false, true #=> false
2683 */
2684
2685static VALUE
2686rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
2687{
2688 VALUE mid, include_super, include_private;
2689
2690 rb_scan_args(argc, argv, "12", &mid, &include_super, &include_private);
2691 if (argc < 3) {
2692 include_private = Qfalse;
2693 if (argc < 2) {
2694 include_super = Qtrue;
2695 }
2696 }
2697
2698 rb_method_visibility_t visi = check_definition_visibility(mod, mid, RTEST(include_super));
2699 switch (visi) {
2700 case METHOD_VISI_UNDEF:
2701 return Qfalse;
2702 case METHOD_VISI_PUBLIC:
2703 case METHOD_VISI_PROTECTED:
2704 return Qtrue;
2705 case METHOD_VISI_PRIVATE:
2706 return RBOOL(RTEST(include_private));
2707 default:
2709 }
2710}
2711
2712static VALUE
2713check_definition(VALUE mod, int argc, VALUE *argv, rb_method_visibility_t visi)
2714{
2715 VALUE mid, include_super;
2716 rb_scan_args(argc, argv, "11", &mid, &include_super);
2717 if (argc < 2) {
2718 include_super = Qtrue;
2719 }
2720 return RBOOL(check_definition_visibility(mod, mid, RTEST(include_super)) == visi);
2721}
2722
2723/*
2724 * call-seq:
2725 * mod.public_method_defined?(symbol, inherit=true) -> true or false
2726 * mod.public_method_defined?(string, inherit=true) -> true or false
2727 *
2728 * Returns +true+ if the named public method is defined by
2729 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2730 * ancestors.
2731 * String arguments are converted to symbols.
2732 *
2733 * module A
2734 * def method1() end
2735 * end
2736 * class B
2737 * protected
2738 * def method2() end
2739 * end
2740 * class C < B
2741 * include A
2742 * def method3() end
2743 * end
2744 *
2745 * A.method_defined? :method1 #=> true
2746 * C.public_method_defined? "method1" #=> true
2747 * C.public_method_defined? "method1", true #=> true
2748 * C.public_method_defined? "method1", false #=> true
2749 * C.public_method_defined? "method2" #=> false
2750 * C.method_defined? "method2" #=> true
2751 */
2752
2753static VALUE
2754rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
2755{
2756 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2757}
2758
2759/*
2760 * call-seq:
2761 * mod.private_method_defined?(symbol, inherit=true) -> true or false
2762 * mod.private_method_defined?(string, inherit=true) -> true or false
2763 *
2764 * Returns +true+ if the named private method is defined by
2765 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2766 * ancestors.
2767 * String arguments are converted to symbols.
2768 *
2769 * module A
2770 * def method1() end
2771 * end
2772 * class B
2773 * private
2774 * def method2() end
2775 * end
2776 * class C < B
2777 * include A
2778 * def method3() end
2779 * end
2780 *
2781 * A.method_defined? :method1 #=> true
2782 * C.private_method_defined? "method1" #=> false
2783 * C.private_method_defined? "method2" #=> true
2784 * C.private_method_defined? "method2", true #=> true
2785 * C.private_method_defined? "method2", false #=> false
2786 * C.method_defined? "method2" #=> false
2787 */
2788
2789static VALUE
2790rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
2791{
2792 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2793}
2794
2795/*
2796 * call-seq:
2797 * mod.protected_method_defined?(symbol, inherit=true) -> true or false
2798 * mod.protected_method_defined?(string, inherit=true) -> true or false
2799 *
2800 * Returns +true+ if the named protected method is defined
2801 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2802 * ancestors.
2803 * String arguments are converted to symbols.
2804 *
2805 * module A
2806 * def method1() end
2807 * end
2808 * class B
2809 * protected
2810 * def method2() end
2811 * end
2812 * class C < B
2813 * include A
2814 * def method3() end
2815 * end
2816 *
2817 * A.method_defined? :method1 #=> true
2818 * C.protected_method_defined? "method1" #=> false
2819 * C.protected_method_defined? "method2" #=> true
2820 * C.protected_method_defined? "method2", true #=> true
2821 * C.protected_method_defined? "method2", false #=> false
2822 * C.method_defined? "method2" #=> true
2823 */
2824
2825static VALUE
2826rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
2827{
2828 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2829}
2830
2831int
2832rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
2833{
2834 return rb_method_definition_eq(m1->def, m2->def);
2835}
2836
2837static const rb_method_definition_t *
2838original_method_definition(const rb_method_definition_t *def)
2839{
2840 again:
2841 if (def) {
2842 switch (def->type) {
2843 case VM_METHOD_TYPE_REFINED:
2844 if (def->body.refined.orig_me) {
2845 def = def->body.refined.orig_me->def;
2846 goto again;
2847 }
2848 break;
2849 case VM_METHOD_TYPE_ALIAS:
2850 def = def->body.alias.original_me->def;
2851 goto again;
2852 default:
2853 break;
2854 }
2855 }
2856 return def;
2857}
2858
2859int
2860rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
2861{
2862 d1 = original_method_definition(d1);
2863 d2 = original_method_definition(d2);
2864
2865 if (d1 == d2) return 1;
2866 if (!d1 || !d2) return 0;
2867 if (d1->type != d2->type) return 0;
2868
2869 switch (d1->type) {
2870 case VM_METHOD_TYPE_ISEQ:
2871 return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
2872 case VM_METHOD_TYPE_CFUNC:
2873 return
2874 d1->body.cfunc.func == d2->body.cfunc.func &&
2875 d1->body.cfunc.argc == d2->body.cfunc.argc;
2876 case VM_METHOD_TYPE_ATTRSET:
2877 case VM_METHOD_TYPE_IVAR:
2878 return d1->body.attr.id == d2->body.attr.id;
2879 case VM_METHOD_TYPE_BMETHOD:
2880 return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2881 case VM_METHOD_TYPE_MISSING:
2882 return d1->original_id == d2->original_id;
2883 case VM_METHOD_TYPE_ZSUPER:
2884 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2885 case VM_METHOD_TYPE_UNDEF:
2886 return 1;
2887 case VM_METHOD_TYPE_OPTIMIZED:
2888 return (d1->body.optimized.type == d2->body.optimized.type) &&
2889 (d1->body.optimized.index == d2->body.optimized.index);
2890 case VM_METHOD_TYPE_REFINED:
2891 case VM_METHOD_TYPE_ALIAS:
2892 break;
2893 }
2894 rb_bug("rb_method_definition_eq: unsupported type: %d", d1->type);
2895}
2896
2897static st_index_t
2898rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
2899{
2900 hash = rb_hash_uint(hash, def->type);
2901 def = original_method_definition(def);
2902
2903 if (!def) return hash;
2904
2905 switch (def->type) {
2906 case VM_METHOD_TYPE_ISEQ:
2907 return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr->body);
2908 case VM_METHOD_TYPE_CFUNC:
2909 hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2910 return rb_hash_uint(hash, def->body.cfunc.argc);
2911 case VM_METHOD_TYPE_ATTRSET:
2912 case VM_METHOD_TYPE_IVAR:
2913 return rb_hash_uint(hash, def->body.attr.id);
2914 case VM_METHOD_TYPE_BMETHOD:
2915 return rb_hash_proc(hash, def->body.bmethod.proc);
2916 case VM_METHOD_TYPE_MISSING:
2917 return rb_hash_uint(hash, def->original_id);
2918 case VM_METHOD_TYPE_ZSUPER:
2919 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2920 case VM_METHOD_TYPE_UNDEF:
2921 return hash;
2922 case VM_METHOD_TYPE_OPTIMIZED:
2923 hash = rb_hash_uint(hash, def->body.optimized.index);
2924 return rb_hash_uint(hash, def->body.optimized.type);
2925 case VM_METHOD_TYPE_REFINED:
2926 case VM_METHOD_TYPE_ALIAS:
2927 break; /* unreachable */
2928 }
2929 rb_bug("rb_hash_method_definition: unsupported method type (%d)", def->type);
2930}
2931
2932st_index_t
2933rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
2934{
2935 return rb_hash_method_definition(hash, me->def);
2936}
2937
2938void
2939rb_add_alias(VALUE klass, ID alias_name, ID original_name, rb_method_visibility_t visi_alias)
2940{
2941 const VALUE target_klass = klass;
2942 VALUE defined_class;
2943 const rb_method_entry_t *orig_me;
2944 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2945
2946 if (NIL_P(klass)) {
2947 rb_raise(rb_eTypeError, "no class to make alias");
2948 }
2949
2950 rb_class_modify_check(target_klass);
2951
2952 again:
2953 orig_me = search_method(klass, original_name, &defined_class);
2954
2955 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2956 orig_me = rb_resolve_refined_method(Qnil, orig_me);
2957 }
2958
2959 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2960 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2961 if ((!RB_TYPE_P(klass, T_MODULE)) ||
2962 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2963 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2964 rb_print_undef(target_klass, original_name, METHOD_VISI_UNDEF);
2965 }
2966 rb_warn_scheduled_deprecation(4.2, 4.3,
2967 "the fallback to Object for alias of '%"PRIsVALUE"' in module '%"PRIsVALUE"'",
2968 NULL, QUOTE_ID(original_name), rb_class_path(target_klass));
2969 }
2970
2971 switch (orig_me->def->type) {
2972 case VM_METHOD_TYPE_ZSUPER:
2973 klass = RCLASS_SUPER(klass);
2974 original_name = orig_me->def->original_id;
2975 visi = METHOD_ENTRY_VISI(orig_me);
2976 goto again;
2977 case VM_METHOD_TYPE_ALIAS:
2978 visi = METHOD_ENTRY_VISI(orig_me);
2979 orig_me = orig_me->def->body.alias.original_me;
2980 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2981 break;
2982 default: break;
2983 }
2984
2985 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2986 if (visi_alias != METHOD_VISI_UNDEF) visi = visi_alias;
2987
2988 if (!NIL_P(ruby_verbose) && rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) {
2989 VALUE owner_class = orig_me->defined_class ? orig_me->defined_class : defined_class;
2990 VALUE origin = RCLASS_ORIGIN(target_klass);
2991 bool in_prepended_module = false;
2992
2993 if (origin != target_klass) {
2994 for (VALUE p = RCLASS_SUPER(target_klass); !in_prepended_module && p && p != origin; p = RCLASS_SUPER(p)) {
2995 if (p == owner_class) {
2996 in_prepended_module = true;
2997 }
2998 }
2999 }
3000
3001 if (in_prepended_module) {
3002 rb_warn_scheduled_deprecation(4.2, 4.3,
3003 "aliasing %"PRIsVALUE"#%"PRIsVALUE" defined in a prepended module %"PRIsVALUE,
3004 NULL,
3005 rb_class_path(target_klass), QUOTE_ID(original_name),
3006 rb_class_path(orig_me->owner));
3007 }
3008 }
3009
3010 if (orig_me->defined_class == 0) {
3011 struct method_entry_warnings warnings = {0};
3012 const rb_method_entry_t *alias_me =
3013 // TODO: needs vm lock?
3014 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
3015 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
3016 (void *)rb_method_entry_clone(orig_me), &warnings);
3017
3018 method_entry_warnings_emit(&warnings);
3019 method_added(target_klass, alias_name, alias_me);
3020 }
3021 else {
3022 rb_method_entry_t *alias_me;
3023
3024 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
3025 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
3026
3027 if (RB_TYPE_P(target_klass, T_MODULE)) {
3028 // defined_class should not be set
3029 }
3030 else {
3031 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
3032 }
3033 }
3034}
3035
3036void
3037rb_alias(VALUE klass, ID alias_name, ID original_name)
3038{
3039 rb_add_alias(klass, alias_name, original_name, METHOD_VISI_UNDEF);
3040}
3041
3042/*
3043 * call-seq:
3044 * alias_method(new_name, old_name) -> symbol
3045 *
3046 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
3047 * be used to retain access to methods that are overridden.
3048 *
3049 * class Greeter
3050 * def hello
3051 * "hello"
3052 * end
3053 * alias_method :orig_hello, :hello #=> :orig_hello
3054 * def hello
3055 * "#{orig_hello}, world"
3056 * end
3057 * end
3058 * Greeter.new.hello #=> "hello, world"
3059 *
3060 * In a module, <i>old_name</i> must be a method of the module or its
3061 * ancestors. Aliasing a method found only through the fallback to
3062 * Object is deprecated, and will raise a NameError in Ruby 4.3.
3063 */
3064
3065static VALUE
3066rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
3067{
3068 ID oldid = rb_check_id(&oldname);
3069 if (!oldid) {
3070 rb_print_undef_str(mod, oldname);
3071 }
3072 VALUE id = rb_to_id(newname);
3073 rb_alias(mod, id, oldid);
3074 return ID2SYM(id);
3075}
3076
3077static void
3078check_and_export_method(VALUE self, VALUE name, rb_method_visibility_t visi)
3079{
3080 ID id = rb_check_id(&name);
3081 if (!id) {
3082 rb_print_undef_str(self, name);
3083 }
3084 rb_export_method(self, id, visi);
3085}
3086
3087static void
3088set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibility_t visi)
3089{
3090 int i;
3091
3092 // Not rb_class_modify_check: that also marks a module initialized, which this
3093 // path has never done.
3094 rb_check_frozen(self);
3095 rb_class_owner_check(self);
3096 if (argc == 0) {
3097 rb_warning("%"PRIsVALUE" with no argument is just ignored",
3098 QUOTE_ID(rb_frame_callee()));
3099 return;
3100 }
3101
3102
3103 VALUE v;
3104
3105 if (argc == 1 && (v = rb_check_array_type(argv[0])) != Qnil) {
3106 long j;
3107
3108 for (j = 0; j < RARRAY_LEN(v); j++) {
3109 check_and_export_method(self, RARRAY_AREF(v, j), visi);
3110 }
3111 }
3112 else {
3113 for (i = 0; i < argc; i++) {
3114 check_and_export_method(self, argv[i], visi);
3115 }
3116 }
3117}
3118
3119static VALUE
3120set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi)
3121{
3122 if (argc == 0) {
3123 scope_visibility_check();
3124 rb_scope_visibility_set(visi);
3125 return Qnil;
3126 }
3127
3128 set_method_visibility(module, argc, argv, visi);
3129 if (argc == 1) {
3130 return argv[0];
3131 }
3132 return rb_ary_new_from_values(argc, argv);
3133}
3134
3135/*
3136 * call-seq:
3137 * public -> nil
3138 * public(method_name) -> method_name
3139 * public(method_name, method_name, ...) -> array
3140 * public(array) -> array
3141 *
3142 * With no arguments, sets the default visibility for subsequently
3143 * defined methods to public. With arguments, sets the named methods to
3144 * have public visibility.
3145 * String arguments are converted to symbols.
3146 * An Array of Symbols and/or Strings is also accepted.
3147 * If a single argument is passed, it is returned.
3148 * If no argument is passed, nil is returned.
3149 * If multiple arguments are passed, the arguments are returned as an array.
3150 */
3151
3152static VALUE
3153rb_mod_public(int argc, VALUE *argv, VALUE module)
3154{
3155 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
3156}
3157
3158/*
3159 * call-seq:
3160 * protected -> nil
3161 * protected(method_name) -> method_name
3162 * protected(method_name, method_name, ...) -> array
3163 * protected(array) -> array
3164 *
3165 * Sets the visibility of a section or of a list of method names as protected.
3166 * Accepts no arguments, a splat of method names (symbols or strings) or an
3167 * array of method names. Returns the arguments that it received.
3168 *
3169 * == Important difference between protected in other languages
3170 *
3171 * Protected methods in Ruby are different from other languages such as Java,
3172 * where methods are marked as protected to give access to subclasses. In Ruby,
3173 * subclasses <b>already have access to all methods defined in the parent
3174 * class</b>, even private ones.
3175 *
3176 * Marking a method as protected allows <b>different objects of the same
3177 * class</b> to call it.
3178 *
3179 * One use case is for comparison methods, such as <code>==</code>, if we want
3180 * to expose a method for comparison between objects of the same class without
3181 * making the method public to objects of other classes.
3182 *
3183 * == Performance considerations
3184 *
3185 * Protected methods are slower than others because they can't use inline
3186 * cache.
3187 *
3188 * == Example
3189 *
3190 * class Account
3191 * # Mark balance as protected, so that we can compare between accounts
3192 * # without making it public.
3193 * attr_reader :balance
3194 * protected :balance
3195 *
3196 * def initialize(balance)
3197 * @balance = balance
3198 * end
3199 *
3200 * def >(other)
3201 * # The invocation to `other.balance` is allowed because `other` is a
3202 * # different object of the same class (Account).
3203 * balance > other.balance
3204 * end
3205 * end
3206 *
3207 * account1 = Account.new(100)
3208 * account2 = Account.new(50)
3209 *
3210 * account1 > account2 # => true (works)
3211 * account1.balance # => NoMethodError (fails because balance is not public)
3212 *
3213 * To show a private method on RDoc, use <code>:doc:</code> instead of this.
3214 */
3215
3216static VALUE
3217rb_mod_protected(int argc, VALUE *argv, VALUE module)
3218{
3219 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
3220}
3221
3222/*
3223 * call-seq:
3224 * private -> nil
3225 * private(method_name) -> method_name
3226 * private(method_name, method_name, ...) -> array
3227 * private(array) -> array
3228 *
3229 * With no arguments, sets the default visibility for subsequently
3230 * defined methods to private. With arguments, sets the named methods
3231 * to have private visibility.
3232 * String arguments are converted to symbols.
3233 * An Array of Symbols and/or Strings is also accepted.
3234 * If a single argument is passed, it is returned.
3235 * If no argument is passed, nil is returned.
3236 * If multiple arguments are passed, the arguments are returned as an array.
3237 *
3238 * module Mod
3239 * def a() end
3240 * def b() end
3241 * private
3242 * def c() end
3243 * private :a
3244 * end
3245 * Mod.private_instance_methods #=> [:a, :c]
3246 *
3247 * Note that to show a private method on RDoc, use <code>:doc:</code>.
3248 */
3249
3250static VALUE
3251rb_mod_private(int argc, VALUE *argv, VALUE module)
3252{
3253 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
3254}
3255
3256/*
3257 * call-seq:
3258 * ruby2_keywords(method_name, ...) -> nil
3259 *
3260 * Deprecated: will be removed in Ruby 4.4. Use <tt>...</tt>
3261 * {argument forwarding}[rdoc-ref:syntax/methods.rdoc@Argument+Forwarding]
3262 * or other delegation styles instead; they work correctly on Ruby 3.0
3263 * and later. See https://bugs.ruby-lang.org/issues/22205 for the
3264 * schedule.
3265 *
3266 * For the given method names, marks the method as passing keywords through
3267 * a normal argument splat. This should only be called on methods that
3268 * accept an argument splat (<tt>*args</tt>) but not explicit keywords or
3269 * a keyword splat. It marks the method such that if the method is called
3270 * with keyword arguments, the final hash argument is marked with a special
3271 * flag such that if it is the final element of a normal argument splat to
3272 * another method call, and that method call does not include explicit
3273 * keywords or a keyword splat, the final element is interpreted as keywords.
3274 * In other words, keywords will be passed through the method to other
3275 * methods.
3276 *
3277 * This should only be used for methods that delegate keywords to another
3278 * method, and only for backwards compatibility with Ruby versions before 3.0.
3279 * See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
3280 * for details on why +ruby2_keywords+ exists and when and how to use it.
3281 */
3282
3283static VALUE
3284rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module)
3285{
3286 int i;
3287 VALUE origin_class = RCLASS_ORIGIN(module);
3288
3290 rb_check_frozen(module);
3291 rb_class_owner_check(module);
3292
3293 for (i = 0; i < argc; i++) {
3294 VALUE v = argv[i];
3295 ID name = rb_check_id(&v);
3297 VALUE defined_class;
3298
3299 if (!name) {
3300 rb_print_undef_str(module, v);
3301 }
3302
3303 me = search_method(origin_class, name, &defined_class);
3304 if (!me && RB_TYPE_P(module, T_MODULE)) {
3305 me = search_method(rb_cObject, name, &defined_class);
3306 }
3307
3308 if (UNDEFINED_METHOD_ENTRY_P(me) ||
3309 UNDEFINED_REFINED_METHOD_P(me->def)) {
3310 rb_print_undef(module, name, METHOD_VISI_UNDEF);
3311 }
3312
3313 if (module == defined_class || origin_class == defined_class) {
3314 switch (me->def->type) {
3315 case VM_METHOD_TYPE_ISEQ:
3316 if (ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_rest &&
3317 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_post &&
3318 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kw &&
3319 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kwrest) {
3320 ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.ruby2_keywords = 1;
3321 rb_clear_method_cache(module, name);
3322 }
3323 else {
3324 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));
3325 }
3326 break;
3327 case VM_METHOD_TYPE_BMETHOD: {
3328 VALUE procval = me->def->body.bmethod.proc;
3329 if (vm_block_handler_type(procval) == block_handler_type_proc) {
3330 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
3331 }
3332
3333 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
3334 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval);
3335 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
3336 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
3337 !ISEQ_BODY(iseq)->param.flags.has_post &&
3338 !ISEQ_BODY(iseq)->param.flags.has_kw &&
3339 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
3340 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
3341 rb_clear_method_cache(module, name);
3342 }
3343 else {
3344 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));
3345 }
3346 break;
3347 }
3348 }
3349 /* fallthrough */
3350 default:
3351 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method not defined in Ruby)", QUOTE_ID(name));
3352 break;
3353 }
3354 }
3355 else {
3356 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (can only set in method defining module)", QUOTE_ID(name));
3357 }
3358 }
3359 return Qnil;
3360}
3361
3362/*
3363 * call-seq:
3364 * mod.public_class_method(symbol, ...) -> mod
3365 * mod.public_class_method(string, ...) -> mod
3366 * mod.public_class_method(array) -> mod
3367 *
3368 * Makes a list of existing class methods public.
3369 *
3370 * String arguments are converted to symbols.
3371 * An Array of Symbols and/or Strings is also accepted.
3372 */
3373
3374static VALUE
3375rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
3376{
3377 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
3378 return obj;
3379}
3380
3381/*
3382 * call-seq:
3383 * mod.private_class_method(symbol, ...) -> mod
3384 * mod.private_class_method(string, ...) -> mod
3385 * mod.private_class_method(array) -> mod
3386 *
3387 * Makes existing class methods private. Often used to hide the default
3388 * constructor <code>new</code>.
3389 *
3390 * String arguments are converted to symbols.
3391 * An Array of Symbols and/or Strings is also accepted.
3392 *
3393 * class SimpleSingleton # Not thread safe
3394 * private_class_method :new
3395 * def SimpleSingleton.create(*args, &block)
3396 * @me = new(*args, &block) if ! @me
3397 * @me
3398 * end
3399 * end
3400 */
3401
3402static VALUE
3403rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
3404{
3405 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
3406 return obj;
3407}
3408
3409/*
3410 * call-seq:
3411 * public
3412 * public(symbol, ...)
3413 * public(string, ...)
3414 * public(array)
3415 *
3416 * With no arguments, sets the default visibility for subsequently
3417 * defined methods to public. With arguments, sets the named methods to
3418 * have public visibility.
3419 *
3420 * String arguments are converted to symbols.
3421 * An Array of Symbols and/or Strings is also accepted.
3422 */
3423
3424static VALUE
3425top_public(int argc, VALUE *argv, VALUE _)
3426{
3427 return rb_mod_public(argc, argv, rb_top_main_class("public"));
3428}
3429
3430/*
3431 * call-seq:
3432 * private
3433 * private(symbol, ...)
3434 * private(string, ...)
3435 * private(array)
3436 *
3437 * With no arguments, sets the default visibility for subsequently
3438 * defined methods to private. With arguments, sets the named methods to
3439 * have private visibility.
3440 *
3441 * String arguments are converted to symbols.
3442 * An Array of Symbols and/or Strings is also accepted.
3443 */
3444static VALUE
3445top_private(int argc, VALUE *argv, VALUE _)
3446{
3447 return rb_mod_private(argc, argv, rb_top_main_class("private"));
3448}
3449
3450/*
3451 * call-seq:
3452 * ruby2_keywords(method_name, ...) -> self
3453 *
3454 * Deprecated: will be removed in Ruby 4.4. Use <tt>...</tt>
3455 * {argument forwarding}[rdoc-ref:syntax/methods.rdoc@Argument+Forwarding]
3456 * or other delegation styles instead; they work correctly on Ruby 3.0
3457 * and later. See https://bugs.ruby-lang.org/issues/22205 for the
3458 * schedule.
3459 *
3460 * For the given method names, marks the method as passing keywords through
3461 * a normal argument splat. See Module#ruby2_keywords in detail.
3462 */
3463static VALUE
3464top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
3465{
3466 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
3467}
3468
3469/*
3470 * call-seq:
3471 * module_function -> nil
3472 * module_function(method_name) -> method_name
3473 * module_function(method_name, method_name, ...) -> array
3474 *
3475 * Creates module functions for the named methods. These functions may
3476 * be called with the module as a receiver, and also become available
3477 * as instance methods to classes that mix in the module. Module
3478 * functions are copies of the original, and so may be changed
3479 * independently. The instance-method versions are made private.
3480 * String arguments are converted to symbols.
3481 * If a single argument is passed, it is returned.
3482 * If no argument is passed, nil is returned.
3483 * If multiple arguments are passed, the arguments are returned as an array.
3484 *
3485 * module Mod
3486 * def one
3487 * "This is one"
3488 * end
3489 * module_function :one
3490 * end
3491 * class Cls
3492 * include Mod
3493 * def call_one
3494 * one
3495 * end
3496 * end
3497 * Mod.one #=> "This is one"
3498 * c = Cls.new
3499 * c.call_one #=> "This is one"
3500 * module Mod
3501 * def one
3502 * "This is the new one"
3503 * end
3504 * end
3505 * Mod.one #=> "This is one"
3506 * c.call_one #=> "This is the new one"
3507 *
3508 * If used with no arguments, subsequently defined methods become module
3509 * functions:
3510 *
3511 * module Mod
3512 * module_function
3513 *
3514 * def two
3515 * "This is two"
3516 * end
3517 * end
3518 * Mod.two #=> "This is two"
3519 */
3520
3521static VALUE
3522rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
3523{
3524 int i;
3525 ID id;
3526 const rb_method_entry_t *me;
3527
3528 if (!RB_TYPE_P(module, T_MODULE)) {
3529 rb_raise(rb_eTypeError, "module_function must be called for modules");
3530 }
3531
3532 if (argc == 0) {
3533 rb_scope_module_func_set();
3534 return Qnil;
3535 }
3536
3537 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
3538
3539 for (i = 0; i < argc; i++) {
3540 VALUE m = module;
3541
3542 id = rb_to_id(argv[i]);
3543 for (;;) {
3544 me = search_method(m, id, 0);
3545 if (me == 0) {
3546 me = search_method(rb_cObject, id, 0);
3547 }
3548 if (UNDEFINED_METHOD_ENTRY_P(me)) {
3549 rb_print_undef(module, id, METHOD_VISI_UNDEF);
3550 }
3551 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
3552 break; /* normal case: need not to follow 'super' link */
3553 }
3554 m = RCLASS_SUPER(m);
3555 if (!m)
3556 break;
3557 }
3558 rb_method_entry_set(rb_singleton_class(module), id, me, METHOD_VISI_PUBLIC);
3559 }
3560 if (argc == 1) {
3561 return argv[0];
3562 }
3563 return rb_ary_new_from_values(argc, argv);
3564}
3565
3566#ifdef __GNUC__
3567#pragma push_macro("rb_method_basic_definition_p")
3568#undef rb_method_basic_definition_p
3569#endif
3570int
3572{
3573 const rb_callable_method_entry_t *cme;
3574 if (!klass) return TRUE; /* hidden object cannot be overridden */
3575 cme = rb_callable_method_entry(klass, id);
3576 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
3577}
3578#ifdef __GNUC__
3579#pragma pop_macro("rb_method_basic_definition_p")
3580#endif
3581
3582static VALUE
3583call_method_entry(rb_execution_context_t *ec, VALUE defined_class, VALUE obj, ID id,
3584 const rb_callable_method_entry_t *cme, int argc, const VALUE *argv, int kw_splat)
3585{
3586 VALUE passed_block_handler = vm_passed_block_handler(ec);
3587 VALUE result = rb_vm_call_kw(ec, obj, id, argc, argv, cme, kw_splat);
3588 vm_passed_block_handler_set(ec, passed_block_handler);
3589 return result;
3590}
3591
3592static VALUE
3593basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
3594 VALUE mid, VALUE priv)
3595{
3596 VALUE defined_class, args[2];
3597 const ID rtmid = idRespond_to_missing;
3598 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, rtmid, &defined_class);
3599
3600 if (!cme || METHOD_ENTRY_BASIC(cme)) return Qundef;
3601 args[0] = mid;
3602 args[1] = priv;
3603 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args, RB_NO_KEYWORDS);
3604}
3605
3606static inline int
3607basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
3608{
3609 VALUE klass = CLASS_OF(obj);
3610 VALUE ret;
3611
3612 switch (method_boundp(klass, id, pub|BOUND_RESPONDS)) {
3613 case 2:
3614 return FALSE;
3615 case 0:
3616 ret = basic_obj_respond_to_missing(ec, klass, obj, ID2SYM(id),
3617 RBOOL(!pub));
3618 return RTEST(ret) && !UNDEF_P(ret);
3619 default:
3620 return TRUE;
3621 }
3622}
3623
3624static int
3625vm_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
3626{
3627 VALUE defined_class;
3628 const ID resid = idRespond_to;
3629 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, resid, &defined_class);
3630
3631 if (!cme) return -1;
3632 if (METHOD_ENTRY_BASIC(cme)) {
3633 return -1;
3634 }
3635 else {
3636 int argc = 1;
3637 VALUE args[2];
3638 VALUE result;
3639
3640 args[0] = ID2SYM(id);
3641 args[1] = Qtrue;
3642 if (priv) {
3643 argc = rb_method_entry_arity((const rb_method_entry_t *)cme);
3644 if (argc > 2) {
3645 rb_raise(rb_eArgError,
3646 "respond_to? must accept 1 or 2 arguments (requires %d)",
3647 argc);
3648 }
3649 if (argc != 1) {
3650 argc = 2;
3651 }
3652 else if (!NIL_P(ruby_verbose)) {
3653 VALUE location = rb_method_entry_location((const rb_method_entry_t *)cme);
3655 "%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") uses"
3656 " the deprecated method signature, which takes one parameter",
3657 (RCLASS_SINGLETON_P(klass) ? obj : klass),
3658 (RCLASS_SINGLETON_P(klass) ? '.' : '#'),
3659 QUOTE_ID(id));
3660 if (!NIL_P(location)) {
3661 VALUE path = RARRAY_AREF(location, 0);
3662 VALUE line = RARRAY_AREF(location, 1);
3663 if (!NIL_P(path)) {
3665 RSTRING_PTR(path), NUM2INT(line),
3666 "respond_to? is defined here");
3667 }
3668 }
3669 }
3670 }
3671 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args, RB_NO_KEYWORDS);
3672 return RTEST(result);
3673 }
3674}
3675
3676int
3677rb_obj_respond_to(VALUE obj, ID id, int priv)
3678{
3679 rb_execution_context_t *ec = GET_EC();
3680 return rb_ec_obj_respond_to(ec, obj, id, priv);
3681}
3682
3683int
3684rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int priv)
3685{
3686 VALUE klass = CLASS_OF(obj);
3687 int ret = vm_respond_to(ec, klass, obj, id, priv);
3688 if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
3689 return ret;
3690}
3691
3692int
3694{
3695 return rb_obj_respond_to(obj, id, FALSE);
3696}
3697
3698
3699/*
3700 * call-seq:
3701 * obj.respond_to?(symbol, include_all=false) -> true or false
3702 * obj.respond_to?(string, include_all=false) -> true or false
3703 *
3704 * Returns +true+ if _obj_ responds to the given method. Private and
3705 * protected methods are included in the search only if the optional
3706 * second parameter evaluates to +true+.
3707 *
3708 * If the method is not implemented,
3709 * as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
3710 * false is returned.
3711 *
3712 * If the method is not defined, <code>respond_to_missing?</code>
3713 * method is called and the result is returned.
3714 *
3715 * When the method name parameter is given as a string, the string is
3716 * converted to a symbol.
3717 */
3718
3719static VALUE
3720obj_respond_to(int argc, VALUE *argv, VALUE obj)
3721{
3722 VALUE mid, priv;
3723 ID id;
3724 rb_execution_context_t *ec = GET_EC();
3725
3726 rb_scan_args(argc, argv, "11", &mid, &priv);
3727 if (!(id = rb_check_id(&mid))) {
3728 VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
3729 rb_to_symbol(mid), priv);
3730 if (UNDEF_P(ret)) ret = Qfalse;
3731 return ret;
3732 }
3733 return RBOOL(basic_obj_respond_to(ec, obj, id, !RTEST(priv)));
3734}
3735
3736/*
3737 * call-seq:
3738 * obj.respond_to_missing?(symbol, include_all) -> true or false
3739 * obj.respond_to_missing?(string, include_all) -> true or false
3740 *
3741 * DO NOT USE THIS DIRECTLY.
3742 *
3743 * Hook method to return whether the _obj_ can respond to _id_ method
3744 * or not.
3745 *
3746 * When the method name parameter is given as a string, the string is
3747 * converted to a symbol.
3748 *
3749 * See #respond_to?, and the example of BasicObject.
3750 */
3751static VALUE
3752obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
3753{
3754 return Qfalse;
3755}
3756
3757void
3758Init_eval_method(void)
3759{
3760 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
3761 rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2);
3762
3763 rb_define_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
3764 rb_define_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
3765 rb_define_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
3766 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
3767 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
3768 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
3769 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
3770 rb_define_private_method(rb_cModule, "ruby2_keywords", rb_mod_ruby2_keywords, -1);
3771
3772 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
3773 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
3774 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
3775 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
3776 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
3777 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
3778
3780 "public", top_public, -1);
3782 "private", top_private, -1);
3784 "ruby2_keywords", top_ruby2_keywords, -1);
3785
3786 {
3787#define REPLICATE_METHOD(klass, id) do { \
3788 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3789 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
3790 } while (0)
3791
3792 REPLICATE_METHOD(rb_eException, idMethodMissing);
3793 REPLICATE_METHOD(rb_eException, idRespond_to);
3794 REPLICATE_METHOD(rb_eException, idRespond_to_missing);
3795 }
3796}
#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:3051
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:3384
#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:478
#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:1473
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:441
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:468
VALUE rb_eException
Mother of all exceptions.
Definition error.c:1465
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:499
@ 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
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:140
#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:1081
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:355
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_hidden_new(long 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.
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
Definition vm_method.c:2541
#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:152
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:398
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3693
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:1846
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:3037
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:3571
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:2504
void rb_remove_method(VALUE klass, const char *name)
Removes a method.
Definition vm_method.c:2353
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1855
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:2347
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:2465
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:3677
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:14145
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:14135
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.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
Definition rarray.h:402
#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:1454
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
struct rb_data_type_struct::@64 function
Function pointers.
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
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
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