Ruby 4.1.0dev (2026-09-07 revision b57404b461ba8bf34e802d86b0db78388216e182)
vm_method.c (b57404b461ba8bf34e802d86b0db78388216e182)
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 ccs->cme = (const struct rb_callable_method_entry_struct *)rb_gc_location((VALUE)ccs->cme);
109 VM_ASSERT(vm_ccs_p(ccs));
110
111 for (int i=0; i<ccs->len; i++) {
112 ccs->entries[i].cc = (const struct rb_callcache *)rb_gc_location((VALUE)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 e->entries[i] = rb_gc_location(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 struct cc_refinement_entries *e = RTYPEDDATA_GET_DATA(vm->cc_refinement_set);
830 if (e->len == e->capa) {
831 size_t new_capa = e->capa == 0 ? 16 : e->capa * 2;
832 SIZED_REALLOC_N(e->entries, VALUE, new_capa, e->capa);
833 e->capa = new_capa;
834 }
835 e->entries[e->len++] = (VALUE)cc;
836
837 // We never mark the cc, but we need to issue a writebarrier so that
838 // the refinement set can be added to the remembered set
839 RB_OBJ_WRITTEN(vm->cc_refinement_set, Qundef, (VALUE)cc);
840 }
841 RB_VM_LOCK_LEAVE();
842}
843
844void
845rb_clear_all_refinement_method_cache(void)
846{
847 rb_vm_t *vm = GET_VM();
848
849 RB_VM_LOCK_ENTER();
850 {
851 struct cc_refinement_entries *e = RTYPEDDATA_GET_DATA(vm->cc_refinement_set);
852 for (size_t i = 0; i < e->len; i++) {
853 VALUE v = e->entries[i];
854
855 // All objects should be live as weak references are pruned in
856 // cc_refinement_set_handle_weak_references
857 VM_ASSERT(!rb_objspace_garbage_object_p(v));
858
859 const struct rb_callcache *cc = (const struct rb_callcache *)v;
860 VM_ASSERT(vm_cc_refinement_p(cc));
861
862 if (vm_cc_valid(cc)) {
863 vm_cc_invalidate(cc);
864 }
865 }
866 e->len = 0;
867 }
868 RB_VM_LOCK_LEAVE();
869
870 rb_yjit_invalidate_all_method_lookup_assumptions();
871}
872
873void
874rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me)
875{
876 RB_VM_LOCKING() {
877 rb_method_table_insert0(klass, table, method_id, me, RB_TYPE_P(klass, T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass));
878 }
879}
880
881void
882rb_method_table_insert0(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me, bool iclass_shared_mtbl)
883{
884 VALUE table_owner = klass;
885 if (iclass_shared_mtbl) {
886 table_owner = RBASIC(table_owner)->klass;
887 }
888 VM_ASSERT_TYPE3(table_owner, T_CLASS, T_ICLASS, T_MODULE);
889 rb_id_table_insert(table, method_id, (VALUE)me);
890 RB_OBJ_WRITTEN(table_owner, Qundef, (VALUE)me);
891}
892
893// rb_f_notimplement has an extra trailing argument to distinguish it from other methods
894// at compile-time to override arity to be -1. But the trailing argument introduces a
895// signature mismatch between caller and callee, so rb_define_method family inserts a
896// method entry with rb_f_notimplement_internal, which has canonical arity=-1 signature,
897// instead of rb_f_notimplement.
898NORETURN(static VALUE rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj));
899
900static VALUE
901rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj)
902{
903 rb_notimplement();
904
906}
907
908VALUE
909rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
910{
911 rb_f_notimplement_internal(argc, argv, obj);
912}
913
914static void
915rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_visibility_t visi)
916{
917 rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, (void *)1, visi);
918}
919
920void
921rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi)
922{
923 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc);
924 if (func != (VALUE(*)(ANYARGS))rb_f_notimplement) {
926 opt.func = func;
927 opt.argc = argc;
928 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
929 }
930 else {
931 rb_define_notimplement_method_id(klass, mid, visi);
932 }
933}
934
935void
936rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type opt_type, unsigned int index, rb_method_visibility_t visi)
937{
939 .type = opt_type,
940 .index = index,
941 };
942 rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
943}
944
945static void
946method_definition_release(rb_method_definition_t *def)
947{
948 if (def != NULL) {
949 const unsigned int reference_count_was = RUBY_ATOMIC_FETCH_SUB(def->reference_count, 1);
950
951 RUBY_ASSERT_ALWAYS(reference_count_was != 0);
952
953 if (reference_count_was == 1) {
954 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:1->0 (remove)\n", (void *)def,
955 rb_id2name(def->original_id));
956 SIZED_FREE(def);
957 }
958 else {
959 if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d (dec)\n", (void *)def, rb_id2name(def->original_id),
960 reference_count_was, reference_count_was - 1);
961 }
962 }
963}
964
965void
966rb_method_definition_release(rb_method_definition_t *def)
967{
968 method_definition_release(def);
969}
970
971static void delete_overloaded_cme(const rb_callable_method_entry_t *cme);
972
973void
974rb_free_method_entry_vm_weak_references(const rb_method_entry_t *me)
975{
976 if (me->def && me->def->iseq_overload) {
977 delete_overloaded_cme((const rb_callable_method_entry_t *)me);
978 }
979}
980
981void
982rb_free_method_entry(const rb_method_entry_t *me)
983{
984#if USE_ZJIT
985 if (METHOD_ENTRY_CACHED(me)) {
986 rb_zjit_cme_free((const rb_callable_method_entry_t *)me);
987 }
988#endif
989
990#if USE_YJIT
991 // YJIT rb_yjit_root_mark() roots CMEs in `Invariants`,
992 // to remove from `Invariants` here.
993#endif
994
995 method_definition_release(me->def);
996}
997
998static inline rb_method_entry_t *search_method(VALUE klass, ID id, VALUE *defined_class_ptr);
999extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);
1000
1001static VALUE
1002(*call_cfunc_invoker_func(int argc))(VALUE recv, int argc, const VALUE *, VALUE (*func)(ANYARGS))
1003{
1004 if (!GET_THREAD()->ext_config.ractor_safe) {
1005 switch (argc) {
1006 case -2: return &call_cfunc_m2;
1007 case -1: return &call_cfunc_m1;
1008 case 0: return &call_cfunc_0;
1009 case 1: return &call_cfunc_1;
1010 case 2: return &call_cfunc_2;
1011 case 3: return &call_cfunc_3;
1012 case 4: return &call_cfunc_4;
1013 case 5: return &call_cfunc_5;
1014 case 6: return &call_cfunc_6;
1015 case 7: return &call_cfunc_7;
1016 case 8: return &call_cfunc_8;
1017 case 9: return &call_cfunc_9;
1018 case 10: return &call_cfunc_10;
1019 case 11: return &call_cfunc_11;
1020 case 12: return &call_cfunc_12;
1021 case 13: return &call_cfunc_13;
1022 case 14: return &call_cfunc_14;
1023 case 15: return &call_cfunc_15;
1024 default:
1025 rb_bug("unsupported length: %d", argc);
1026 }
1027 }
1028 else {
1029 switch (argc) {
1030 case -2: return &ractor_safe_call_cfunc_m2;
1031 case -1: return &ractor_safe_call_cfunc_m1;
1032 case 0: return &ractor_safe_call_cfunc_0;
1033 case 1: return &ractor_safe_call_cfunc_1;
1034 case 2: return &ractor_safe_call_cfunc_2;
1035 case 3: return &ractor_safe_call_cfunc_3;
1036 case 4: return &ractor_safe_call_cfunc_4;
1037 case 5: return &ractor_safe_call_cfunc_5;
1038 case 6: return &ractor_safe_call_cfunc_6;
1039 case 7: return &ractor_safe_call_cfunc_7;
1040 case 8: return &ractor_safe_call_cfunc_8;
1041 case 9: return &ractor_safe_call_cfunc_9;
1042 case 10: return &ractor_safe_call_cfunc_10;
1043 case 11: return &ractor_safe_call_cfunc_11;
1044 case 12: return &ractor_safe_call_cfunc_12;
1045 case 13: return &ractor_safe_call_cfunc_13;
1046 case 14: return &ractor_safe_call_cfunc_14;
1047 case 15: return &ractor_safe_call_cfunc_15;
1048 default:
1049 rb_bug("unsupported length: %d", argc);
1050 }
1051 }
1052}
1053
1054static void
1055setup_method_cfunc_struct(rb_method_cfunc_t *cfunc, VALUE (*func)(ANYARGS), int argc)
1056{
1057 cfunc->func = func;
1058 cfunc->argc = argc;
1059 cfunc->invoker = call_cfunc_invoker_func(argc);
1060}
1061
1062
1064method_definition_addref(rb_method_definition_t *def, bool complemented)
1065{
1066 unsigned int reference_count_was = RUBY_ATOMIC_FETCH_ADD(def->reference_count, 1);
1067 if (!complemented && reference_count_was > 0) {
1068 /* TODO: A Ractor can reach this via UnboundMethod#bind */
1069 def->aliased = true;
1070 }
1071 if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d->%d\n", (void *)def, rb_id2name(def->original_id), reference_count_was, reference_count_was+1);
1072
1073 return def;
1074}
1075
1076void
1077rb_method_definition_addref(rb_method_definition_t *def)
1078{
1079 method_definition_addref(def, false);
1080}
1081
1082void
1083rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
1084{
1085 method_definition_release(me->def);
1086 *(rb_method_definition_t **)&me->def = method_definition_addref(def, METHOD_ENTRY_COMPLEMENTED(me));
1087
1088 if (!ruby_running) add_opt_method_entry(me);
1089
1090 if (opts != NULL) {
1091 switch (def->type) {
1092 case VM_METHOD_TYPE_ISEQ:
1093 {
1094 rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
1095 const rb_iseq_t *iseq = iseq_body->iseqptr;
1096 rb_cref_t *method_cref, *cref = iseq_body->cref;
1097
1098 /* setup iseq first (before invoking GC) */
1099 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq);
1100
1101 // Methods defined in `with_jit` should be considered METHOD_ENTRY_BASIC
1102 if (rb_iseq_attr_p(iseq, BUILTIN_ATTR_C_TRACE)) {
1103 METHOD_ENTRY_BASIC_SET((rb_method_entry_t *)me, TRUE);
1104 }
1105
1106 if (ISEQ_BODY(iseq)->mandatory_only_iseq) def->iseq_overload = 1;
1107
1108 if (0) vm_cref_dump("rb_method_definition_create", cref);
1109
1110 if (cref) {
1111 method_cref = cref;
1112 }
1113 else {
1114 method_cref = vm_cref_new_toplevel(GET_EC()); /* TODO: can we reuse? */
1115 }
1116
1117 RB_OBJ_WRITE(me, &def->body.iseq.cref, method_cref);
1118 return;
1119 }
1120 case VM_METHOD_TYPE_CFUNC:
1121 {
1122 rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts;
1123 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
1124 return;
1125 }
1126 case VM_METHOD_TYPE_ATTRSET:
1127 case VM_METHOD_TYPE_IVAR:
1128 {
1129 const rb_execution_context_t *ec = GET_EC();
1130 rb_control_frame_t *cfp;
1131 int line;
1132
1133 def->body.attr.id = (ID)(VALUE)opts;
1134
1135 cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1136
1137 if (cfp && (line = rb_vm_get_sourceline(cfp))) {
1138 VALUE location = rb_ary_new3(2, rb_iseq_path(CFP_ISEQ(cfp)), INT2FIX(line));
1139 rb_ary_freeze(location);
1140 RB_OBJ_SET_SHAREABLE(location);
1141 RB_OBJ_WRITE(me, &def->body.attr.location, location);
1142 }
1143 else {
1144 VM_ASSERT(def->body.attr.location == 0);
1145 }
1146 return;
1147 }
1148 case VM_METHOD_TYPE_BMETHOD:
1149 RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts);
1150 def->body.bmethod.defined_ractor_id = rb_ec_ractor_id(GET_EC());
1151 return;
1152 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1153 setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (VALUE(*)(ANYARGS))rb_f_notimplement_internal, -1);
1154 return;
1155 case VM_METHOD_TYPE_OPTIMIZED:
1156 def->body.optimized = *(rb_method_optimized_t *)opts;
1157 return;
1158 case VM_METHOD_TYPE_REFINED:
1159 {
1160 const rb_method_entry_t *orig_me = (const rb_method_entry_t *)opts;
1161 RB_OBJ_WRITE(me, &def->body.refined.orig_me, orig_me);
1162 RB_OBJ_WRITE(me, &def->original_module, orig_me->def->original_module);
1163 return;
1164 }
1165 case VM_METHOD_TYPE_ALIAS:
1166 {
1167 const rb_method_entry_t *orig_me = (const rb_method_entry_t *)opts;
1168 RB_OBJ_WRITE(me, &def->body.alias.original_me, orig_me);
1169 RB_OBJ_WRITE(me, &def->original_module, orig_me->def->original_module);
1170 return;
1171 }
1172 case VM_METHOD_TYPE_ZSUPER:
1173 case VM_METHOD_TYPE_UNDEF:
1174 case VM_METHOD_TYPE_MISSING:
1175 return;
1176 }
1177 }
1178}
1179
1180static void
1181method_definition_reset(const rb_method_entry_t *me)
1182{
1183 rb_method_definition_t *def = me->def;
1184
1185 RB_OBJ_WRITTEN(me, Qundef, def->original_module);
1186
1187 switch (def->type) {
1188 case VM_METHOD_TYPE_ISEQ:
1189 RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.iseqptr);
1190 RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.cref);
1191 break;
1192 case VM_METHOD_TYPE_ATTRSET:
1193 case VM_METHOD_TYPE_IVAR:
1194 RB_OBJ_WRITTEN(me, Qundef, def->body.attr.location);
1195 break;
1196 case VM_METHOD_TYPE_BMETHOD:
1197 RB_OBJ_WRITTEN(me, Qundef, def->body.bmethod.proc);
1198 break;
1199 case VM_METHOD_TYPE_REFINED:
1200 RB_OBJ_WRITTEN(me, Qundef, def->body.refined.orig_me);
1201 break;
1202 case VM_METHOD_TYPE_ALIAS:
1203 RB_OBJ_WRITTEN(me, Qundef, def->body.alias.original_me);
1204 break;
1205 case VM_METHOD_TYPE_CFUNC:
1206 case VM_METHOD_TYPE_ZSUPER:
1207 case VM_METHOD_TYPE_MISSING:
1208 case VM_METHOD_TYPE_OPTIMIZED:
1209 case VM_METHOD_TYPE_UNDEF:
1210 case VM_METHOD_TYPE_NOTIMPLEMENTED:
1211 break;
1212 }
1213}
1214
1215static rb_atomic_t method_serial = 1;
1216
1218rb_method_definition_create(rb_method_type_t type, ID mid)
1219{
1222 def->type = type;
1223 def->original_id = mid;
1224 def->method_serial = (uintptr_t)RUBY_ATOMIC_FETCH_ADD(method_serial, 1);
1225 def->box = rb_current_box();
1226 return def;
1227}
1228
1229static rb_method_entry_t *
1230rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, rb_method_definition_t *def, bool complement)
1231{
1232 if (def) method_definition_addref(def, complement);
1233 if (RTEST(defined_class)) {
1234 // not negative cache
1235 VM_ASSERT_TYPE2(defined_class, T_CLASS, T_ICLASS);
1236 }
1237 rb_method_entry_t *me = SHAREABLE_IMEMO_NEW(rb_method_entry_t, imemo_ment, defined_class);
1238
1239 // mark_and_move_method_entry pins itself when it is in the overloaded_cme table
1240 rb_gc_register_pinning_obj((VALUE)me);
1241
1242 *((rb_method_definition_t **)&me->def) = def;
1243 me->called_id = called_id;
1244 me->owner = owner;
1245
1246 return me;
1247}
1248
1249static VALUE
1250filter_defined_class(VALUE klass)
1251{
1252 switch (BUILTIN_TYPE(klass)) {
1253 case T_CLASS:
1254 return klass;
1255 case T_MODULE:
1256 return 0;
1257 case T_ICLASS:
1258 break;
1259 default:
1260 break;
1261 }
1262 rb_bug("filter_defined_class: %s", rb_obj_info(klass));
1263}
1264
1266rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def)
1267{
1268 rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def, false);
1269 METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
1270 if (def != NULL) method_definition_reset(me);
1271 return me;
1272}
1273
1274// Return a cloned ME that's not invalidated (MEs are disposable for caching).
1275const rb_method_entry_t *
1276rb_method_entry_clone(const rb_method_entry_t *src_me)
1277{
1278 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));
1279
1280 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1281
1282 // Also clone inner ME in case of refinement ME
1283 if (src_me->def &&
1284 src_me->def->type == VM_METHOD_TYPE_REFINED &&
1285 src_me->def->body.refined.orig_me) {
1286 const rb_method_entry_t *orig_me = src_me->def->body.refined.orig_me;
1287 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_REFINED);
1288
1289 rb_method_entry_t *orig_clone = rb_method_entry_alloc(orig_me->called_id,
1290 orig_me->owner, orig_me->defined_class, orig_me->def, METHOD_ENTRY_COMPLEMENTED(orig_me));
1291 METHOD_ENTRY_FLAGS_COPY(orig_clone, orig_me);
1292
1293 // Clone definition, since writing a VALUE to a shared definition
1294 // can create reference edges we can't run WBs for.
1295 rb_method_definition_t *clone_def =
1296 rb_method_definition_create(VM_METHOD_TYPE_REFINED, src_me->called_id);
1297 rb_method_definition_set(me, clone_def, orig_clone);
1298 }
1299 return me;
1300}
1301
1303rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
1304{
1305 rb_method_definition_t *def = src_me->def;
1307 const rb_method_entry_t *refined_orig_me = NULL;
1308
1309 if (!src_me->defined_class &&
1310 def->type == VM_METHOD_TYPE_REFINED &&
1311 def->body.refined.orig_me) {
1312 const rb_method_entry_t *orig_me =
1313 rb_method_entry_clone(def->body.refined.orig_me);
1314 RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
1315 refined_orig_me = orig_me;
1316 def = NULL;
1317 }
1318
1319 me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def, true);
1320 METHOD_ENTRY_FLAGS_COPY(me, src_me);
1321 METHOD_ENTRY_COMPLEMENTED_SET(me);
1322 if (!def) {
1323 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
1324 rb_method_definition_set(me, def, (void *)refined_orig_me);
1325 }
1326
1327 VM_ASSERT_TYPE(me->owner, T_MODULE);
1328
1329 return (rb_callable_method_entry_t *)me;
1330}
1331
1332void
1333rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
1334{
1335 method_definition_release(dst->def);
1336 *(rb_method_definition_t **)&dst->def = method_definition_addref(src->def, METHOD_ENTRY_COMPLEMENTED(src));
1337 method_definition_reset(dst);
1338 dst->called_id = src->called_id;
1339 RB_OBJ_WRITE((VALUE)dst, &dst->owner, src->owner);
1340 RB_OBJ_WRITE((VALUE)dst, &dst->defined_class, src->defined_class);
1341 METHOD_ENTRY_FLAGS_COPY(dst, src);
1342}
1343
1344static void
1345make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
1346{
1347 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1348 return;
1349 }
1350 else {
1352
1353 rb_vm_check_redefinition_opt_method(me, me->owner);
1354
1355 struct rb_method_entry_struct *orig_me =
1356 rb_method_entry_alloc(me->called_id,
1357 me->owner,
1358 me->defined_class,
1359 me->def,
1360 true);
1361 METHOD_ENTRY_FLAGS_COPY(orig_me, me);
1362
1363 def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
1364 rb_method_definition_set(me, def, orig_me);
1365 METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
1366 }
1367}
1368
1369static inline rb_method_entry_t *
1370lookup_method_table(VALUE klass, ID id)
1371{
1372 st_data_t body;
1373 struct rb_id_table *m_tbl = RCLASS_M_TBL(klass);
1374
1375 if (rb_id_table_lookup(m_tbl, id, &body)) {
1376 return (rb_method_entry_t *) body;
1377 }
1378 else {
1379 return 0;
1380 }
1381}
1382
1383void
1384rb_add_refined_method_entry(VALUE refined_class, ID mid)
1385{
1386 rb_method_entry_t *me = lookup_method_table(refined_class, mid);
1387
1388 if (me) {
1389 make_method_entry_refined(refined_class, me);
1390 rb_clear_method_cache(refined_class, mid);
1391 }
1392 else {
1393 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
1394 }
1395}
1396
1397static void
1398check_override_opt_method_i(VALUE klass, VALUE arg)
1399{
1400 if (RB_TYPE_P(klass, T_ICLASS)) {
1401 // ICLASS from a module's subclass list: check the includer and
1402 // recurse into the includer's T_CLASS subclasses.
1403 VALUE includer = RCLASS_INCLUDER(klass);
1404 if (!UNDEF_P(includer) && includer) {
1405 check_override_opt_method_i(includer, arg);
1406 }
1407 return;
1408 }
1409
1410 ID mid = (ID)arg;
1411 const rb_method_entry_t *me, *newme;
1412
1413 if (vm_redefinition_check_flag(klass)) {
1414 me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1415 if (me) {
1416 newme = rb_method_entry(klass, mid);
1417 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1418 }
1419 }
1420 rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
1421}
1422
1423static void
1424check_override_opt_method(VALUE klass, VALUE mid)
1425{
1426 if (rb_vm_check_optimizable_mid(mid)) {
1427 check_override_opt_method_i(klass, mid);
1428 }
1429}
1430
1431VALUE
1432rb_zsuper_to_super(int argc, VALUE *argv, VALUE self)
1433{
1434 return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
1435}
1436
1437static inline rb_method_entry_t* search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined);
1438/*
1439 * klass->method_table[mid] = method_entry(defined_class, visi, def)
1440 *
1441 * If def is given (!= NULL), then just use it and ignore original_id and otps.
1442 * If not given, then make a new def with original_id and opts.
1443 */
1444static rb_method_entry_t *
1445rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
1446 rb_method_type_t type, rb_method_definition_t *def, ID original_id, void *opts)
1447{
1449 struct rb_id_table *mtbl;
1450 st_data_t data;
1451 int make_refined = 0;
1452 VALUE orig_klass;
1453 bool turn_zsuper_to_super = false;
1454
1455 if (NIL_P(klass)) {
1456 klass = rb_cObject;
1457 }
1458 orig_klass = klass;
1459
1460 if (!RCLASS_SINGLETON_P(klass) &&
1461 type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
1462 type != VM_METHOD_TYPE_ZSUPER) {
1463 switch (mid) {
1464 case idInitialize:
1465 case idInitialize_copy:
1466 case idInitialize_clone:
1467 case idInitialize_dup:
1468 case idRespond_to_missing:
1469 visi = METHOD_VISI_PRIVATE;
1470 }
1471 }
1472
1473 if (type != VM_METHOD_TYPE_REFINED) {
1474 rb_class_modify_check(klass);
1475 }
1476
1477 if (RB_TYPE_P(klass, T_MODULE) && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1478 VALUE refined_class = rb_refinement_module_get_refined_class(klass);
1479 if (type == VM_METHOD_TYPE_ZSUPER) {
1480 turn_zsuper_to_super = true;
1481 }
1482 rb_add_refined_method_entry(refined_class, mid);
1483 }
1484 if (type == VM_METHOD_TYPE_REFINED) {
1485 rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
1486 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
1487 }
1488 else {
1489 klass = RCLASS_ORIGIN(klass);
1490 if (klass != orig_klass) {
1491 rb_clear_method_cache(orig_klass, mid);
1492 }
1493 }
1494 mtbl = RCLASS_WRITABLE_M_TBL(klass);
1495
1496 /* check re-definition */
1497 if (rb_id_table_lookup(mtbl, mid, &data)) {
1498 rb_method_entry_t *old_me = (rb_method_entry_t *)data;
1499 rb_method_definition_t *old_def = old_me->def;
1500
1501 if (rb_method_definition_eq(old_def, def)) return old_me;
1502 rb_vm_check_redefinition_opt_method(old_me, klass);
1503
1504 if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;
1505
1506 if (RTEST(ruby_verbose) &&
1507 type != VM_METHOD_TYPE_UNDEF &&
1508 (old_def->aliased == false) &&
1509 (!old_def->no_redef_warning) &&
1510 !make_refined &&
1511 old_def->type != VM_METHOD_TYPE_UNDEF &&
1512 old_def->type != VM_METHOD_TYPE_ZSUPER &&
1513 old_def->type != VM_METHOD_TYPE_ALIAS) {
1514 const rb_iseq_t *iseq = 0;
1515
1516 switch (old_def->type) {
1517 case VM_METHOD_TYPE_ISEQ:
1518 iseq = def_iseq_ptr(old_def);
1519 break;
1520 case VM_METHOD_TYPE_BMETHOD:
1521 iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
1522 break;
1523 default:
1524 break;
1525 }
1526 if (iseq) {
1527 rb_warning(
1528 "method redefined; discarding old %"PRIsVALUE"\n%"PRIsVALUE":%d: warning: previous definition of %"PRIsVALUE" was here",
1529 rb_id2str(mid),
1530 rb_iseq_path(iseq),
1531 ISEQ_BODY(iseq)->location.first_lineno,
1532 rb_id2str(old_def->original_id)
1533 );
1534 }
1535 else {
1536 rb_warning("method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
1537 }
1538 }
1539 }
1540
1541 /* create method entry */
1542 me = rb_method_entry_create(mid, defined_class, visi, NULL);
1543 if (def == NULL) {
1544 def = rb_method_definition_create(type, original_id);
1545 if (turn_zsuper_to_super) {
1546 def->type = VM_METHOD_TYPE_CFUNC;
1547 def->body.cfunc.func = (rb_cfunc_t)rb_zsuper_to_super;
1548 def->body.cfunc.invoker = ractor_safe_call_cfunc_m1;
1549 def->body.cfunc.argc = -1;
1550 }
1551 RB_OBJ_WRITE(me, &def->original_module, me->owner);
1552 }
1553 rb_method_definition_set(me, def, opts);
1554
1555 rb_clear_method_cache(klass, mid);
1556
1557 /* check mid */
1558 if (klass == rb_cObject) {
1559 switch (mid) {
1560 case idInitialize:
1561 case idRespond_to_missing:
1562 case idMethodMissing:
1563 case idRespond_to:
1564 rb_warn("redefining Object#%s may cause infinite loop", rb_id2name(mid));
1565 }
1566 }
1567 /* check mid */
1568 if (mid == object_id || mid == id__id__ || mid == id__send__) {
1569 if (type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
1570 rb_warn("redefining '%s' may cause serious problems", rb_id2name(mid));
1571 }
1572 }
1573
1574 if (make_refined) {
1575 make_method_entry_refined(klass, me);
1576 }
1577
1578 rb_method_table_insert(klass, mtbl, mid, me);
1579
1580 VM_ASSERT(me->def != NULL);
1581
1582 /* check optimized method override by a prepended module */
1583 if (RB_TYPE_P(orig_klass, T_MODULE)) {
1584 check_override_opt_method(klass, (VALUE)mid);
1585 }
1586
1587 return me;
1588}
1589
1590static st_table *
1591overloaded_cme_table(void)
1592{
1593 return &GET_VM()->overloaded_cme_table;
1594}
1595
1596#if VM_CHECK_MODE > 0
1597static int
1598vm_dump_overloaded_cme_table(st_data_t key, st_data_t val, st_data_t dmy)
1599{
1600 fprintf(stderr, "key: "); rp(key);
1601 fprintf(stderr, "val: "); rp(val);
1602 return ST_CONTINUE;
1603}
1604
1605void
1606rb_vm_dump_overloaded_cme_table(void)
1607{
1608 fprintf(stderr, "== rb_vm_dump_overloaded_cme_table\n");
1609 st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
1610}
1611#endif
1612
1613static int
1614lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
1615{
1616 if (existing) {
1617 const rb_callable_method_entry_t *cme = (const rb_callable_method_entry_t *)*key;
1618 const rb_callable_method_entry_t *monly_cme = (const rb_callable_method_entry_t *)*value;
1619 const rb_callable_method_entry_t **ptr = (const rb_callable_method_entry_t **)data;
1620
1621 if (rb_objspace_garbage_object_p((VALUE)cme) ||
1622 rb_objspace_garbage_object_p((VALUE)monly_cme)) {
1623 *ptr = NULL;
1624 return ST_DELETE;
1625 }
1626 else {
1627 *ptr = monly_cme;
1628 }
1629 }
1630
1631 return ST_STOP;
1632}
1633
1634static const rb_callable_method_entry_t *
1635lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1636{
1637 ASSERT_vm_locking();
1638
1639 const rb_callable_method_entry_t *monly_cme = NULL;
1640 st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
1641 return monly_cme;
1642}
1643
1644#if VM_CHECK_MODE > 0
1646rb_vm_lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
1647{
1648 return lookup_overloaded_cme(cme);
1649}
1650#endif
1651
1652static void
1653delete_overloaded_cme(const rb_callable_method_entry_t *cme)
1654{
1655 st_data_t cme_data = (st_data_t)cme;
1656 ASSERT_vm_locking();
1657 st_delete(overloaded_cme_table(), &cme_data, NULL);
1658}
1659
1660static const rb_callable_method_entry_t *
1661get_overloaded_cme(const rb_callable_method_entry_t *cme)
1662{
1663 const rb_callable_method_entry_t *monly_cme = lookup_overloaded_cme(cme);
1664
1665 if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
1666 return monly_cme;
1667 }
1668 else {
1669 // create
1670 rb_method_definition_t *def = rb_method_definition_create(VM_METHOD_TYPE_ISEQ, cme->def->original_id);
1671 rb_method_entry_t *me = rb_method_entry_alloc(cme->called_id,
1672 cme->owner,
1673 cme->defined_class,
1674 def,
1675 false);
1676
1677 RB_OBJ_WRITE(me, &def->body.iseq.cref, cme->def->body.iseq.cref);
1678 RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, ISEQ_BODY(cme->def->body.iseq.iseqptr)->mandatory_only_iseq);
1679 RB_OBJ_WRITE(me, &def->original_module, cme->def->original_module);
1680
1681 ASSERT_vm_locking();
1682 st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);
1683
1684 METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
1685 return (rb_callable_method_entry_t *)me;
1686 }
1687}
1688
1690rb_check_overloaded_cme(const rb_callable_method_entry_t *cme, const struct rb_callinfo * const ci)
1691{
1692 if (UNLIKELY(cme->def->iseq_overload) &&
1693 (vm_ci_flag(ci) & (VM_CALL_ARGS_SIMPLE)) &&
1694 (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) &&
1695 (int)vm_ci_argc(ci) == ISEQ_BODY(method_entry_iseqptr(cme))->param.lead_num) {
1696 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ, "type: %d", cme->def->type); // iseq_overload is marked only on ISEQ methods
1697
1698 cme = get_overloaded_cme(cme);
1699
1700 VM_ASSERT(cme != NULL);
1701 METHOD_ENTRY_CACHED_SET((struct rb_callable_method_entry_struct *)cme);
1702 }
1703
1704 return cme;
1705}
1706
1707static inline void stack_check(rb_execution_context_t *ec);
1708
1709#define CALL_METHOD_HOOK(klass, hook, mid) do { \
1710 const VALUE arg = ID2SYM(mid); \
1711 VALUE recv_class = (klass); \
1712 ID hook_id = (hook); \
1713 if (RCLASS_SINGLETON_P((klass))) { \
1714 recv_class = RCLASS_ATTACHED_OBJECT((klass)); \
1715 hook_id = singleton_##hook; \
1716 } \
1717 rb_funcallv_uncached(recv_class, hook_id, 1, &arg); \
1718 } while (0)
1719
1720static void
1721method_added(VALUE klass, ID mid, const rb_method_entry_t *me)
1722{
1723 if (ruby_running) {
1724 /* [Bug #22179] Record the just-defined method entry for method coverage
1725 * so the result no longer depends on walking the heap (and thus on GC
1726 * timing). */
1727 if (me) rb_vm_coverage_record_me(me);
1728 CALL_METHOD_HOOK(klass, added, mid);
1729 }
1730}
1731
1732void
1733rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
1734{
1735 const rb_method_entry_t *me;
1736 RB_VM_LOCKING() {
1737 me = rb_method_entry_make(klass, mid, klass, visi, type, NULL, mid, opts);
1738 }
1739
1740 if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) {
1741 method_added(klass, mid, me);
1742 }
1743}
1744
1745void
1746rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
1747{
1748 struct { /* should be same fields with rb_method_iseq_struct */
1749 const rb_iseq_t *iseqptr;
1750 rb_cref_t *cref;
1751 } iseq_body;
1752
1753 iseq_body.iseqptr = iseq;
1754 iseq_body.cref = cref;
1755
1756 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
1757}
1758
1759static rb_method_entry_t *
1760method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
1761 rb_method_visibility_t visi, VALUE defined_class)
1762{
1763 rb_method_entry_t *newme;
1764 RB_VM_LOCKING() {
1765 newme = rb_method_entry_make(klass, mid, defined_class, visi,
1766 me->def->type, me->def, 0, NULL);
1767 if (newme == me) {
1768 me->def->no_redef_warning = TRUE;
1769 METHOD_ENTRY_FLAGS_SET(newme, visi, FALSE);
1770 }
1771 }
1772
1773 method_added(klass, mid, newme);
1774 return newme;
1775}
1776
1778rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_visibility_t visi)
1779{
1780 return method_entry_set(klass, mid, me, visi, klass);
1781}
1782
1783#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
1784
1785static void
1786propagate_alloc_func(VALUE subclass, VALUE arg)
1787{
1788 if (RB_TYPE_P(subclass, T_CLASS) &&
1789 !RCLASS_SINGLETON_P(subclass) &&
1790 !FL_TEST_RAW(subclass, RCLASS_ALLOCATOR_DEFINED)) {
1791 RCLASS_SET_ALLOCATOR(subclass, (rb_alloc_func_t)arg);
1792 rb_class_foreach_subclass(subclass, propagate_alloc_func, arg);
1793 }
1794}
1795
1796void
1797rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
1798{
1799 Check_Type(klass, T_CLASS);
1800 if (RCLASS_SINGLETON_P(klass)) {
1801 rb_raise(rb_eTypeError, "can't define an allocator for a singleton class");
1802 }
1803 RCLASS_SET_ALLOCATOR(klass, func);
1804 FL_SET_RAW(klass, RCLASS_ALLOCATOR_DEFINED);
1805 rb_class_foreach_subclass(klass, propagate_alloc_func, (VALUE)func);
1806}
1807
1808void
1810{
1811 Check_Type(klass, T_CLASS);
1812 RCLASS_SET_ALLOCATOR(klass, UNDEF_ALLOC_FUNC);
1813 FL_SET_RAW(klass, RCLASS_ALLOCATOR_DEFINED);
1814 rb_class_foreach_subclass(klass, propagate_alloc_func, (VALUE)UNDEF_ALLOC_FUNC);
1815}
1816
1819{
1820 RBIMPL_ASSERT_TYPE(klass, T_CLASS);
1821
1822 rb_alloc_func_t allocator = RCLASS_ALLOCATOR(klass);
1823 if (allocator == UNDEF_ALLOC_FUNC) return 0;
1824 RUBY_ASSERT(allocator);
1825 return allocator;
1826}
1827
1828const rb_method_entry_t *
1829rb_method_entry_at(VALUE klass, ID id)
1830{
1831 return lookup_method_table(klass, id);
1832}
1833
1834static inline rb_method_entry_t*
1835search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
1836{
1837 rb_method_entry_t *me = NULL;
1838
1839 RB_DEBUG_COUNTER_INC(mc_search);
1840
1841 for (; klass; klass = RCLASS_SUPER(klass)) {
1842 RB_DEBUG_COUNTER_INC(mc_search_super);
1843 if ((me = lookup_method_table(klass, id)) != 0) {
1844 if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
1845 me->def->body.refined.orig_me) {
1846 break;
1847 }
1848 }
1849 }
1850
1851 if (defined_class_ptr) *defined_class_ptr = klass;
1852
1853 if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);
1854
1855 VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me),
1856 "invalid me, mid:%s, klass:%"PRIsVALUE"(%s)",
1857 rb_id2name(id),
1858 RTEST(rb_mod_name(klass)) ? rb_mod_name(klass) : rb_str_new_lit("anonymous"),
1859 rb_obj_info(klass));
1860 return me;
1861}
1862
1863static inline rb_method_entry_t*
1864search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
1865{
1866 return search_method0(klass, id, defined_class_ptr, false);
1867}
1868
1869static rb_method_entry_t *
1870search_method_protect(VALUE klass, ID id, VALUE *defined_class_ptr)
1871{
1872 rb_method_entry_t *me = search_method(klass, id, defined_class_ptr);
1873
1874 if (!UNDEFINED_METHOD_ENTRY_P(me)) {
1875 return me;
1876 }
1877 else {
1878 return NULL;
1879 }
1880}
1881
1882const rb_method_entry_t *
1883rb_method_entry(VALUE klass, ID id)
1884{
1885 return search_method_protect(klass, id, NULL);
1886}
1887
1888static inline const rb_callable_method_entry_t *
1889prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_t * const me, int create)
1890{
1891 struct rb_id_table *mtbl;
1892 const rb_callable_method_entry_t *cme;
1893 VALUE cme_data;
1894 int cme_found = 0;
1895
1896 if (me) {
1897 if (me->defined_class == 0) {
1898 RB_DEBUG_COUNTER_INC(mc_cme_complement);
1899 VM_ASSERT_TYPE2(defined_class, T_ICLASS, T_MODULE);
1900
1901 mtbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
1902 if (mtbl && rb_id_table_lookup(mtbl, id, &cme_data)) {
1903 cme = (rb_callable_method_entry_t *)cme_data;
1904 cme_found = 1;
1905 }
1906 if (cme_found) {
1907 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
1908 VM_ASSERT(callable_method_entry_p(cme));
1909 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1910 }
1911 else if (create) {
1912 if (!mtbl) {
1913 mtbl = rb_id_table_create(0);
1914 RCLASS_WRITE_CALLABLE_M_TBL(defined_class, mtbl);
1915 }
1916 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1917 rb_id_table_insert(mtbl, id, (VALUE)cme);
1918 RB_OBJ_WRITTEN(defined_class, Qundef, (VALUE)cme);
1919 VM_ASSERT(callable_method_entry_p(cme));
1920 }
1921 else {
1922 return NULL;
1923 }
1924 }
1925 else {
1926 cme = (const rb_callable_method_entry_t *)me;
1927 VM_ASSERT(callable_method_entry_p(cme));
1928 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
1929 }
1930 return cme;
1931 }
1932 else {
1933 return NULL;
1934 }
1935}
1936
1937/* A hook like this fires from C with no call site to cache into except for the gccct table,
1938 * which is often cleared anyway. It would leave a permanent CC behind (tied to the class) if it
1939 * created one, so we try to avoid it. */
1940VALUE
1941rb_funcallv_uncached(VALUE recv, ID mid, int argc, const VALUE *argv)
1942{
1943 VALUE defined_class;
1944 const rb_method_entry_t *me = search_method(CLASS_OF(recv), mid, &defined_class);
1945
1946 if (UNLIKELY(UNDEFINED_METHOD_ENTRY_P(me))) {
1947 return rb_funcallv(recv, mid, argc, argv);
1948 }
1949
1950 const rb_callable_method_entry_t *cme;
1951
1952 if (UNLIKELY(me->defined_class == 0)) {
1953 // produce a transient CME that will get collected
1954 cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
1955 }
1956 else {
1957 cme = (const rb_callable_method_entry_t *)me;
1958 }
1959
1960 rb_execution_context_t *ec = GET_EC();
1961 stack_check(ec);
1962 return rb_vm_call_kw(ec, recv, mid, argc, argv, cme, RB_NO_KEYWORDS);
1963}
1964
1965static const rb_callable_method_entry_t *
1966complemented_callable_method_entry(VALUE klass, ID id)
1967{
1968 VALUE defined_class;
1969 rb_method_entry_t *me = search_method(klass, id, &defined_class);
1970 return prepare_callable_method_entry(defined_class, id, me, FALSE);
1971}
1972
1973static const rb_callable_method_entry_t *
1974cached_callable_method_entry(VALUE klass, ID mid)
1975{
1976 ASSERT_vm_locking();
1977
1978 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
1979 VALUE ccs_data;
1980
1981 if (cc_tbl && rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
1982 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
1983 VM_ASSERT(vm_ccs_p(ccs));
1984
1985 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
1986 VM_ASSERT(ccs->cme->called_id == mid);
1987 RB_DEBUG_COUNTER_INC(ccs_found);
1988 return ccs->cme;
1989 }
1990 else {
1991 rb_vm_barrier();
1992
1993 rb_managed_id_table_delete(cc_tbl, mid);
1994 rb_vm_ccs_invalidate_and_free(ccs);
1995 }
1996 }
1997
1998 RB_DEBUG_COUNTER_INC(ccs_not_found);
1999 return NULL;
2000}
2001
2002static void
2003cache_callable_method_entry(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
2004{
2005 ASSERT_vm_locking();
2006 VM_ASSERT(cme != NULL);
2007
2008 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
2009 VALUE ccs_data;
2010
2011 if (!cc_tbl) {
2012 cc_tbl = rb_vm_cc_table_create(2);
2013 RCLASS_WRITE_CC_TBL(klass, cc_tbl);
2014 }
2015
2016 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
2017#if VM_CHECK_MODE > 0
2018 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
2019 VM_ASSERT(ccs->cme == cme);
2020#endif
2021 }
2022 else {
2023 if (rb_multi_ractor_p()) {
2024 VALUE new_cc_tbl = rb_vm_cc_table_dup(cc_tbl);
2025 vm_ccs_create(klass, new_cc_tbl, mid, cme);
2026 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), new_cc_tbl);
2027 }
2028 else {
2029 vm_ccs_create(klass, cc_tbl, mid, cme);
2030 }
2031 }
2032}
2033
2034static const rb_callable_method_entry_t *
2035negative_cme(ID mid)
2036{
2037 rb_vm_t *vm = GET_VM();
2038 const rb_callable_method_entry_t *cme;
2039 VALUE cme_data;
2040
2041 if (rb_id_table_lookup(&vm->negative_cme_table, mid, &cme_data)) {
2042 cme = (rb_callable_method_entry_t *)cme_data;
2043 }
2044 else {
2045 cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid, Qnil, Qnil, NULL, false);
2046 rb_id_table_insert(&vm->negative_cme_table, mid, (VALUE)cme);
2047 }
2048
2049 VM_ASSERT(cme != NULL);
2050 return cme;
2051}
2052
2053static const rb_callable_method_entry_t *
2054callable_method_entry_or_negative(VALUE klass, ID mid, VALUE *defined_class_ptr)
2055{
2056 const rb_callable_method_entry_t *cme;
2057
2058 VM_ASSERT(!SPECIAL_CONST_P(klass));
2059
2060 if (RB_BUILTIN_TYPE(klass) == T_NONE) {
2061 // If we find a T_NONE here, it's most likely we called CLASS_OF(obj) on a
2062 // garbage collected object (the freelist is stored in the class pointer),
2063 // but it's possible that just the class was GC'd.
2064 // This message intentionally tries to imply the former, but make an
2065 // accurate statement for either case.
2066 rb_bug("attempted to search method '%s' on a garbage collected object",
2067 rb_id2name(mid));
2068 }
2069
2070 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2071
2072 /* Fast path: lock-free read from cache */
2073 VALUE cc_tbl = RUBY_ATOMIC_VALUE_LOAD(RCLASS_WRITABLE_CC_TBL(klass));
2074 if (cc_tbl) {
2075 VALUE ccs_data;
2076 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_data)) {
2077 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
2078 VM_ASSERT(vm_ccs_p(ccs));
2079
2080 if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
2081 VM_ASSERT(ccs->cme->called_id == mid);
2082 if (defined_class_ptr != NULL) *defined_class_ptr = ccs->cme->defined_class;
2083 RB_DEBUG_COUNTER_INC(ccs_found);
2084 return ccs->cme;
2085 }
2086 }
2087 }
2088
2089 /* Slow path: need to lock and potentially populate cache */
2090 RB_VM_LOCKING() {
2091 cme = cached_callable_method_entry(klass, mid);
2092
2093 if (cme) {
2094 if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
2095 }
2096 else {
2097 VALUE defined_class;
2098 rb_method_entry_t *me = search_method(klass, mid, &defined_class);
2099 if (defined_class_ptr) *defined_class_ptr = defined_class;
2100
2101 if (me != NULL) {
2102 cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
2103 }
2104 else {
2105 cme = negative_cme(mid);
2106 }
2107
2108 cache_callable_method_entry(klass, mid, cme);
2109 }
2110 }
2111
2112 return cme;
2113}
2114
2115// This is exposed for YJIT so that we can make assumptions that methods are
2116// not defined.
2118rb_callable_method_entry_or_negative(VALUE klass, ID mid)
2119{
2120 return callable_method_entry_or_negative(klass, mid, NULL);
2121}
2122
2123static const rb_callable_method_entry_t *
2124callable_method_entry(VALUE klass, ID mid, VALUE *defined_class_ptr)
2125{
2126 const rb_callable_method_entry_t *cme;
2127 cme = callable_method_entry_or_negative(klass, mid, defined_class_ptr);
2128 return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
2129}
2130
2132rb_callable_method_entry(VALUE klass, ID mid)
2133{
2134 return callable_method_entry(klass, mid, NULL);
2135}
2136
2137static const rb_method_entry_t *resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr);
2138
2139static const rb_method_entry_t *
2140method_entry_resolve_refinement(VALUE klass, ID id, int with_refinement, VALUE *defined_class_ptr)
2141{
2142 const rb_method_entry_t *me = search_method_protect(klass, id, defined_class_ptr);
2143
2144 if (me) {
2145 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2146 if (with_refinement) {
2147 const rb_cref_t *cref = rb_vm_cref();
2148 VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
2149 me = resolve_refined_method(refinements, me, defined_class_ptr);
2150 }
2151 else {
2152 me = resolve_refined_method(Qnil, me, defined_class_ptr);
2153 }
2154
2155 if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
2156 }
2157 }
2158
2159 return me;
2160}
2161
2162const rb_method_entry_t *
2163rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2164{
2165 return method_entry_resolve_refinement(klass, id, TRUE, defined_class_ptr);
2166}
2167
2168static const rb_callable_method_entry_t *
2169callable_method_entry_refinements0(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements,
2170 const rb_callable_method_entry_t *cme)
2171{
2172 if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
2173 return cme;
2174 }
2175 else {
2176 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2177 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, with_refinements, dcp);
2178 return prepare_callable_method_entry(*dcp, id, me, TRUE);
2179 }
2180}
2181
2182static const rb_callable_method_entry_t *
2183callable_method_entry_refinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
2184{
2185 const rb_callable_method_entry_t *cme = callable_method_entry(klass, id, defined_class_ptr);
2186 return callable_method_entry_refinements0(klass, id, defined_class_ptr, with_refinements, cme);
2187}
2188
2190rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2191{
2192 return callable_method_entry_refinements(klass, id, defined_class_ptr, true);
2193}
2194
2195static const rb_callable_method_entry_t *
2196callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2197{
2198 return callable_method_entry_refinements(klass, id, defined_class_ptr, false);
2199}
2200
2201const rb_method_entry_t *
2202rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2203{
2204 return method_entry_resolve_refinement(klass, id, FALSE, defined_class_ptr);
2205}
2206
2208rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
2209{
2210 VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
2211 const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, FALSE, dcp);
2212 return prepare_callable_method_entry(*dcp, id, me, TRUE);
2213}
2214
2215static const rb_method_entry_t *
2216resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
2217{
2218 while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2219 VALUE refinement;
2220 const rb_method_entry_t *tmp_me;
2221 VALUE super;
2222
2223 refinement = find_refinement(refinements, me->owner);
2224 if (!NIL_P(refinement)) {
2225 tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);
2226
2227 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
2228 return tmp_me;
2229 }
2230 }
2231
2232 tmp_me = me->def->body.refined.orig_me;
2233 if (tmp_me) {
2234 if (!tmp_me->defined_class) {
2235 VM_ASSERT_TYPE(tmp_me->owner, T_MODULE);
2236 }
2237 else if (defined_class_ptr) {
2238 *defined_class_ptr = tmp_me->defined_class;
2239 }
2240 return tmp_me;
2241 }
2242
2243 super = RCLASS_SUPER(me->owner);
2244 if (!super) {
2245 return 0;
2246 }
2247
2248 me = search_method_protect(super, me->called_id, defined_class_ptr);
2249 }
2250 return me;
2251}
2252
2253const rb_method_entry_t *
2254rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
2255{
2256 return resolve_refined_method(refinements, me, NULL);
2257}
2258
2260rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
2261{
2262 VALUE defined_class = me->defined_class;
2263 const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (const rb_method_entry_t *)me, &defined_class);
2264
2265 if (resolved_me && resolved_me->defined_class == 0) {
2266 return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
2267 }
2268 else {
2269 return (const rb_callable_method_entry_t *)resolved_me;
2270 }
2271}
2272
2273static void
2274remove_method(VALUE klass, ID mid)
2275{
2276 VALUE data;
2277 rb_method_entry_t *me = 0;
2278 VALUE self = klass;
2279
2280 rb_class_modify_check(klass);
2281 klass = RCLASS_ORIGIN(klass);
2282 if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
2283 rb_warn("removing '%s' may cause serious problems", rb_id2name(mid));
2284 }
2285
2286 if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
2287 !(me = (rb_method_entry_t *)data) ||
2288 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
2289 UNDEFINED_REFINED_METHOD_P(me->def)) {
2290 rb_name_err_raise("method '%1$s' not defined in %2$s",
2291 klass, ID2SYM(mid));
2292 }
2293
2294 if (klass != self) {
2295 rb_clear_method_cache(self, mid);
2296 }
2297 rb_clear_method_cache(klass, mid);
2298 rb_id_table_delete(RCLASS_WRITABLE_M_TBL(klass), mid);
2299
2300 rb_vm_check_redefinition_opt_method(me, klass);
2301
2302 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2303 rb_add_refined_method_entry(klass, mid);
2304 }
2305
2306 CALL_METHOD_HOOK(self, removed, mid);
2307}
2308
2309void
2311{
2312 remove_method(klass, mid);
2313}
2314
2315void
2316rb_remove_method(VALUE klass, const char *name)
2317{
2318 remove_method(klass, rb_intern(name));
2319}
2320
2321/*
2322 * call-seq:
2323 * remove_method(symbol) -> self
2324 * remove_method(string) -> self
2325 *
2326 * Removes the method identified by _symbol_ from the current
2327 * class. For an example, see Module#undef_method.
2328 * String arguments are converted to symbols.
2329 */
2330
2331static VALUE
2332rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
2333{
2334 int i;
2335
2336 for (i = 0; i < argc; i++) {
2337 VALUE v = argv[i];
2338 ID id = rb_check_id(&v);
2339 if (!id) {
2340 rb_name_err_raise("method '%1$s' not defined in %2$s",
2341 mod, v);
2342 }
2343 remove_method(mod, id);
2344 }
2345 return mod;
2346}
2347
2348static void
2349rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
2350{
2352 VALUE defined_class;
2353 VALUE origin_class = RCLASS_ORIGIN(klass);
2354
2355 me = search_method0(origin_class, name, &defined_class, true);
2356
2357 if (!me && RB_TYPE_P(klass, T_MODULE)) {
2358 me = search_method(rb_cObject, name, &defined_class);
2359 }
2360
2361 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2362 UNDEFINED_REFINED_METHOD_P(me->def)) {
2363 rb_print_undef(klass, name, METHOD_VISI_UNDEF);
2364 }
2365
2366 if (METHOD_ENTRY_VISI(me) != visi) {
2367 rb_vm_check_redefinition_opt_method(me, klass);
2368
2369 if (klass == defined_class || origin_class == defined_class) {
2370 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2371 // Refinement method entries should always be public because the refinement
2372 // search is always performed.
2373 if (me->def->body.refined.orig_me) {
2374 METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
2375 }
2376 }
2377 else {
2378 METHOD_ENTRY_VISI_SET(me, visi);
2379 }
2380 rb_clear_method_cache(klass, name);
2381 }
2382 else {
2383 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
2384 }
2385 }
2386}
2387
2388#define BOUND_PRIVATE 0x01
2389#define BOUND_RESPONDS 0x02
2390
2391static int
2392method_boundp(VALUE klass, ID id, int ex)
2393{
2394 const rb_callable_method_entry_t *cme;
2395
2396 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2397
2398 if (ex & BOUND_RESPONDS) {
2399 cme = rb_callable_method_entry_with_refinements(klass, id, NULL);
2400 }
2401 else {
2402 cme = callable_method_entry_without_refinements(klass, id, NULL);
2403 }
2404
2405 if (cme != NULL) {
2406 if (ex & ~BOUND_RESPONDS) {
2407 switch (METHOD_ENTRY_VISI(cme)) {
2408 case METHOD_VISI_PRIVATE:
2409 return 0;
2410 case METHOD_VISI_PROTECTED:
2411 if (ex & BOUND_RESPONDS) return 0;
2412 default:
2413 break;
2414 }
2415 }
2416
2417 if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
2418 if (ex & BOUND_RESPONDS) return 2;
2419 return 0;
2420 }
2421 return 1;
2422 }
2423 return 0;
2424}
2425
2426// deprecated
2427int
2428rb_method_boundp(VALUE klass, ID id, int ex)
2429{
2430 return method_boundp(klass, id, ex);
2431}
2432
2433static void
2434vm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func)
2435{
2436 rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t *)&rb_vm_cref()->scope_visi;
2437 scope_visi->method_visi = method_visi;
2438 scope_visi->module_func = module_func;
2439}
2440
2441void
2442rb_scope_visibility_set(rb_method_visibility_t visi)
2443{
2444 vm_cref_set_visibility(visi, FALSE);
2445}
2446
2447static void
2448scope_visibility_check(void)
2449{
2450 /* Check for public/protected/private/module_function called inside a method */
2451 rb_control_frame_t *cfp = GET_EC()->cfp+1;
2452 if (cfp && CFP_ISEQ(cfp) && ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_METHOD) {
2453 rb_warn("calling %s without arguments inside a method may not have the intended effect",
2454 rb_id2name(rb_frame_this_func()));
2455 }
2456}
2457
2458static void
2459rb_scope_module_func_set(void)
2460{
2461 scope_visibility_check();
2462 vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
2463}
2464
2465const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
2466void
2467rb_attr(VALUE klass, ID id, int read, int write, int ex)
2468{
2469 ID attriv;
2470 rb_method_visibility_t visi;
2471 const rb_execution_context_t *ec = GET_EC();
2472 const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);
2473
2474 if (!ex || !cref) {
2475 visi = METHOD_VISI_PUBLIC;
2476 }
2477 else {
2478 switch (vm_scope_visibility_get(ec)) {
2479 case METHOD_VISI_PRIVATE:
2480 if (vm_scope_module_func_check(ec)) {
2481 rb_warning("attribute accessor as module_function");
2482 }
2483 visi = METHOD_VISI_PRIVATE;
2484 break;
2485 case METHOD_VISI_PROTECTED:
2486 visi = METHOD_VISI_PROTECTED;
2487 break;
2488 default:
2489 visi = METHOD_VISI_PUBLIC;
2490 break;
2491 }
2492 }
2493
2494 attriv = rb_intern_str(rb_sprintf("@%"PRIsVALUE, rb_id2str(id)));
2495 if (read) {
2496 rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, visi);
2497 }
2498 if (write) {
2499 rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, visi);
2500 }
2501}
2502
2503void
2505{
2506 const rb_method_entry_t *me;
2507
2508 if (NIL_P(klass)) {
2509 rb_raise(rb_eTypeError, "no class to undef method");
2510 }
2511 rb_class_modify_check(klass);
2512 if (id == object_id || id == id__id__ || id == id__send__ || id == idInitialize) {
2513 rb_warn("undefining '%s' may cause serious problems", rb_id2name(id));
2514 }
2515
2516 me = search_method(klass, id, 0);
2517 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
2518 me = rb_resolve_refined_method(Qnil, me);
2519 }
2520
2521 if (UNDEFINED_METHOD_ENTRY_P(me) ||
2522 UNDEFINED_REFINED_METHOD_P(me->def)) {
2523 rb_method_name_error(klass, rb_id2str(id));
2524 }
2525
2526 rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);
2527
2528 CALL_METHOD_HOOK(klass, undefined, id);
2529}
2530
2531/*
2532 * call-seq:
2533 * undef_method(symbol) -> self
2534 * undef_method(string) -> self
2535 *
2536 * Prevents the current class from responding to calls to the named
2537 * method. Contrast this with <code>remove_method</code>, which deletes
2538 * the method from the particular class; Ruby will still search
2539 * superclasses and mixed-in modules for a possible receiver.
2540 * String arguments are converted to symbols.
2541 *
2542 * class Parent
2543 * def hello
2544 * puts "In parent"
2545 * end
2546 * end
2547 * class Child < Parent
2548 * def hello
2549 * puts "In child"
2550 * end
2551 * end
2552 *
2553 *
2554 * c = Child.new
2555 * c.hello
2556 *
2557 *
2558 * class Child
2559 * remove_method :hello # remove from child, still in parent
2560 * end
2561 * c.hello
2562 *
2563 *
2564 * class Child
2565 * undef_method :hello # prevent any calls to 'hello'
2566 * end
2567 * c.hello
2568 *
2569 * <em>produces:</em>
2570 *
2571 * In child
2572 * In parent
2573 * prog.rb:23: undefined method 'hello' for #<Child:0x401b3bb4> (NoMethodError)
2574 */
2575
2576static VALUE
2577rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
2578{
2579 int i;
2580 for (i = 0; i < argc; i++) {
2581 VALUE v = argv[i];
2582 ID id = rb_check_id(&v);
2583 if (!id) {
2584 rb_method_name_error(mod, v);
2585 }
2586 rb_undef(mod, id);
2587 }
2588 return mod;
2589}
2590
2591static rb_method_visibility_t
2592check_definition_visibility(VALUE mod, int argc, VALUE *argv)
2593{
2594 const rb_method_entry_t *me;
2595 VALUE mid, include_super, lookup_mod = mod;
2596 int inc_super;
2597 ID id;
2598
2599 rb_scan_args(argc, argv, "11", &mid, &include_super);
2600 id = rb_check_id(&mid);
2601 if (!id) return METHOD_VISI_UNDEF;
2602
2603 if (argc == 1) {
2604 inc_super = 1;
2605 }
2606 else {
2607 inc_super = RTEST(include_super);
2608 if (!inc_super) {
2609 lookup_mod = RCLASS_ORIGIN(mod);
2610 }
2611 }
2612
2613 me = rb_method_entry_without_refinements(lookup_mod, id, NULL);
2614 if (me) {
2615 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) return METHOD_VISI_UNDEF;
2616 if (!inc_super && me->owner != mod) return METHOD_VISI_UNDEF;
2617 return METHOD_ENTRY_VISI(me);
2618 }
2619 return METHOD_VISI_UNDEF;
2620}
2621
2622/*
2623 * call-seq:
2624 * mod.method_defined?(symbol, inherit=true) -> true or false
2625 * mod.method_defined?(string, inherit=true) -> true or false
2626 *
2627 * Returns +true+ if the named method is defined by
2628 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2629 * ancestors. Public and protected methods are matched.
2630 * String arguments are converted to symbols.
2631 *
2632 * module A
2633 * def method1() end
2634 * def protected_method1() end
2635 * protected :protected_method1
2636 * end
2637 * class B
2638 * def method2() end
2639 * def private_method2() end
2640 * private :private_method2
2641 * end
2642 * class C < B
2643 * include A
2644 * def method3() end
2645 * end
2646 *
2647 * A.method_defined? :method1 #=> true
2648 * C.method_defined? "method1" #=> true
2649 * C.method_defined? "method2" #=> true
2650 * C.method_defined? "method2", true #=> true
2651 * C.method_defined? "method2", false #=> false
2652 * C.method_defined? "method3" #=> true
2653 * C.method_defined? "protected_method1" #=> true
2654 * C.method_defined? "method4" #=> false
2655 * C.method_defined? "private_method2" #=> false
2656 */
2657
2658static VALUE
2659rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
2660{
2661 rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
2662 return RBOOL(visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED);
2663}
2664
2665static VALUE
2666check_definition(VALUE mod, int argc, VALUE *argv, rb_method_visibility_t visi)
2667{
2668 return RBOOL(check_definition_visibility(mod, argc, argv) == visi);
2669}
2670
2671/*
2672 * call-seq:
2673 * mod.public_method_defined?(symbol, inherit=true) -> true or false
2674 * mod.public_method_defined?(string, inherit=true) -> true or false
2675 *
2676 * Returns +true+ if the named public method is defined by
2677 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2678 * ancestors.
2679 * String arguments are converted to symbols.
2680 *
2681 * module A
2682 * def method1() end
2683 * end
2684 * class B
2685 * protected
2686 * def method2() end
2687 * end
2688 * class C < B
2689 * include A
2690 * def method3() end
2691 * end
2692 *
2693 * A.method_defined? :method1 #=> true
2694 * C.public_method_defined? "method1" #=> true
2695 * C.public_method_defined? "method1", true #=> true
2696 * C.public_method_defined? "method1", false #=> true
2697 * C.public_method_defined? "method2" #=> false
2698 * C.method_defined? "method2" #=> true
2699 */
2700
2701static VALUE
2702rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
2703{
2704 return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
2705}
2706
2707/*
2708 * call-seq:
2709 * mod.private_method_defined?(symbol, inherit=true) -> true or false
2710 * mod.private_method_defined?(string, inherit=true) -> true or false
2711 *
2712 * Returns +true+ if the named private method is defined by
2713 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2714 * ancestors.
2715 * String arguments are converted to symbols.
2716 *
2717 * module A
2718 * def method1() end
2719 * end
2720 * class B
2721 * private
2722 * def method2() end
2723 * end
2724 * class C < B
2725 * include A
2726 * def method3() end
2727 * end
2728 *
2729 * A.method_defined? :method1 #=> true
2730 * C.private_method_defined? "method1" #=> false
2731 * C.private_method_defined? "method2" #=> true
2732 * C.private_method_defined? "method2", true #=> true
2733 * C.private_method_defined? "method2", false #=> false
2734 * C.method_defined? "method2" #=> false
2735 */
2736
2737static VALUE
2738rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
2739{
2740 return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
2741}
2742
2743/*
2744 * call-seq:
2745 * mod.protected_method_defined?(symbol, inherit=true) -> true or false
2746 * mod.protected_method_defined?(string, inherit=true) -> true or false
2747 *
2748 * Returns +true+ if the named protected method is defined
2749 * _mod_. If _inherit_ is set, the lookup will also search _mod_'s
2750 * ancestors.
2751 * String arguments are converted to symbols.
2752 *
2753 * module A
2754 * def method1() end
2755 * end
2756 * class B
2757 * protected
2758 * def method2() end
2759 * end
2760 * class C < B
2761 * include A
2762 * def method3() end
2763 * end
2764 *
2765 * A.method_defined? :method1 #=> true
2766 * C.protected_method_defined? "method1" #=> false
2767 * C.protected_method_defined? "method2" #=> true
2768 * C.protected_method_defined? "method2", true #=> true
2769 * C.protected_method_defined? "method2", false #=> false
2770 * C.method_defined? "method2" #=> true
2771 */
2772
2773static VALUE
2774rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
2775{
2776 return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
2777}
2778
2779int
2780rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
2781{
2782 return rb_method_definition_eq(m1->def, m2->def);
2783}
2784
2785static const rb_method_definition_t *
2786original_method_definition(const rb_method_definition_t *def)
2787{
2788 again:
2789 if (def) {
2790 switch (def->type) {
2791 case VM_METHOD_TYPE_REFINED:
2792 if (def->body.refined.orig_me) {
2793 def = def->body.refined.orig_me->def;
2794 goto again;
2795 }
2796 break;
2797 case VM_METHOD_TYPE_ALIAS:
2798 def = def->body.alias.original_me->def;
2799 goto again;
2800 default:
2801 break;
2802 }
2803 }
2804 return def;
2805}
2806
2807int
2808rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
2809{
2810 d1 = original_method_definition(d1);
2811 d2 = original_method_definition(d2);
2812
2813 if (d1 == d2) return 1;
2814 if (!d1 || !d2) return 0;
2815 if (d1->type != d2->type) return 0;
2816
2817 switch (d1->type) {
2818 case VM_METHOD_TYPE_ISEQ:
2819 return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
2820 case VM_METHOD_TYPE_CFUNC:
2821 return
2822 d1->body.cfunc.func == d2->body.cfunc.func &&
2823 d1->body.cfunc.argc == d2->body.cfunc.argc;
2824 case VM_METHOD_TYPE_ATTRSET:
2825 case VM_METHOD_TYPE_IVAR:
2826 return d1->body.attr.id == d2->body.attr.id;
2827 case VM_METHOD_TYPE_BMETHOD:
2828 return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
2829 case VM_METHOD_TYPE_MISSING:
2830 return d1->original_id == d2->original_id;
2831 case VM_METHOD_TYPE_ZSUPER:
2832 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2833 case VM_METHOD_TYPE_UNDEF:
2834 return 1;
2835 case VM_METHOD_TYPE_OPTIMIZED:
2836 return (d1->body.optimized.type == d2->body.optimized.type) &&
2837 (d1->body.optimized.index == d2->body.optimized.index);
2838 case VM_METHOD_TYPE_REFINED:
2839 case VM_METHOD_TYPE_ALIAS:
2840 break;
2841 }
2842 rb_bug("rb_method_definition_eq: unsupported type: %d", d1->type);
2843}
2844
2845static st_index_t
2846rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
2847{
2848 hash = rb_hash_uint(hash, def->type);
2849 def = original_method_definition(def);
2850
2851 if (!def) return hash;
2852
2853 switch (def->type) {
2854 case VM_METHOD_TYPE_ISEQ:
2855 return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr->body);
2856 case VM_METHOD_TYPE_CFUNC:
2857 hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
2858 return rb_hash_uint(hash, def->body.cfunc.argc);
2859 case VM_METHOD_TYPE_ATTRSET:
2860 case VM_METHOD_TYPE_IVAR:
2861 return rb_hash_uint(hash, def->body.attr.id);
2862 case VM_METHOD_TYPE_BMETHOD:
2863 return rb_hash_proc(hash, def->body.bmethod.proc);
2864 case VM_METHOD_TYPE_MISSING:
2865 return rb_hash_uint(hash, def->original_id);
2866 case VM_METHOD_TYPE_ZSUPER:
2867 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2868 case VM_METHOD_TYPE_UNDEF:
2869 return hash;
2870 case VM_METHOD_TYPE_OPTIMIZED:
2871 hash = rb_hash_uint(hash, def->body.optimized.index);
2872 return rb_hash_uint(hash, def->body.optimized.type);
2873 case VM_METHOD_TYPE_REFINED:
2874 case VM_METHOD_TYPE_ALIAS:
2875 break; /* unreachable */
2876 }
2877 rb_bug("rb_hash_method_definition: unsupported method type (%d)", def->type);
2878}
2879
2880st_index_t
2881rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
2882{
2883 return rb_hash_method_definition(hash, me->def);
2884}
2885
2886void
2887rb_alias(VALUE klass, ID alias_name, ID original_name)
2888{
2889 const VALUE target_klass = klass;
2890 VALUE defined_class;
2891 const rb_method_entry_t *orig_me;
2892 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2893
2894 if (NIL_P(klass)) {
2895 rb_raise(rb_eTypeError, "no class to make alias");
2896 }
2897
2898 rb_class_modify_check(klass);
2899
2900 again:
2901 orig_me = search_method(klass, original_name, &defined_class);
2902
2903 if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
2904 orig_me = rb_resolve_refined_method(Qnil, orig_me);
2905 }
2906
2907 if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
2908 UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
2909 if ((!RB_TYPE_P(klass, T_MODULE)) ||
2910 (orig_me = search_method(rb_cObject, original_name, &defined_class),
2911 UNDEFINED_METHOD_ENTRY_P(orig_me))) {
2912 rb_print_undef(target_klass, original_name, METHOD_VISI_UNDEF);
2913 }
2914 }
2915
2916 switch (orig_me->def->type) {
2917 case VM_METHOD_TYPE_ZSUPER:
2918 klass = RCLASS_SUPER(klass);
2919 original_name = orig_me->def->original_id;
2920 visi = METHOD_ENTRY_VISI(orig_me);
2921 goto again;
2922 case VM_METHOD_TYPE_ALIAS:
2923 visi = METHOD_ENTRY_VISI(orig_me);
2924 orig_me = orig_me->def->body.alias.original_me;
2925 VM_ASSERT(orig_me->def->type != VM_METHOD_TYPE_ALIAS);
2926 break;
2927 default: break;
2928 }
2929
2930 if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
2931
2932 if (orig_me->defined_class == 0) {
2933 const rb_method_entry_t *alias_me =
2934 rb_method_entry_make(target_klass, alias_name, target_klass, visi,
2935 VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
2936 (void *)rb_method_entry_clone(orig_me));
2937 method_added(target_klass, alias_name, alias_me);
2938 }
2939 else {
2940 rb_method_entry_t *alias_me;
2941
2942 alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
2943 RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2944
2945 if (RB_TYPE_P(target_klass, T_MODULE)) {
2946 // defined_class should not be set
2947 }
2948 else {
2949 RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2950 }
2951 }
2952}
2953
2954/*
2955 * call-seq:
2956 * alias_method(new_name, old_name) -> symbol
2957 *
2958 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
2959 * be used to retain access to methods that are overridden.
2960 *
2961 * module Mod
2962 * alias_method :orig_exit, :exit #=> :orig_exit
2963 * def exit(code=0)
2964 * puts "Exiting with code #{code}"
2965 * orig_exit(code)
2966 * end
2967 * end
2968 * include Mod
2969 * exit(99)
2970 *
2971 * <em>produces:</em>
2972 *
2973 * Exiting with code 99
2974 */
2975
2976static VALUE
2977rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
2978{
2979 ID oldid = rb_check_id(&oldname);
2980 if (!oldid) {
2981 rb_print_undef_str(mod, oldname);
2982 }
2983 VALUE id = rb_to_id(newname);
2984 rb_alias(mod, id, oldid);
2985 return ID2SYM(id);
2986}
2987
2988static void
2989check_and_export_method(VALUE self, VALUE name, rb_method_visibility_t visi)
2990{
2991 ID id = rb_check_id(&name);
2992 if (!id) {
2993 rb_print_undef_str(self, name);
2994 }
2995 rb_export_method(self, id, visi);
2996}
2997
2998static void
2999set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibility_t visi)
3000{
3001 int i;
3002
3003 rb_check_frozen(self);
3004 if (argc == 0) {
3005 rb_warning("%"PRIsVALUE" with no argument is just ignored",
3006 QUOTE_ID(rb_frame_callee()));
3007 return;
3008 }
3009
3010
3011 VALUE v;
3012
3013 if (argc == 1 && (v = rb_check_array_type(argv[0])) != Qnil) {
3014 long j;
3015
3016 for (j = 0; j < RARRAY_LEN(v); j++) {
3017 check_and_export_method(self, RARRAY_AREF(v, j), visi);
3018 }
3019 }
3020 else {
3021 for (i = 0; i < argc; i++) {
3022 check_and_export_method(self, argv[i], visi);
3023 }
3024 }
3025}
3026
3027static VALUE
3028set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi)
3029{
3030 if (argc == 0) {
3031 scope_visibility_check();
3032 rb_scope_visibility_set(visi);
3033 return Qnil;
3034 }
3035
3036 set_method_visibility(module, argc, argv, visi);
3037 if (argc == 1) {
3038 return argv[0];
3039 }
3040 return rb_ary_new_from_values(argc, argv);
3041}
3042
3043/*
3044 * call-seq:
3045 * public -> nil
3046 * public(method_name) -> method_name
3047 * public(method_name, method_name, ...) -> array
3048 * public(array) -> array
3049 *
3050 * With no arguments, sets the default visibility for subsequently
3051 * defined methods to public. With arguments, sets the named methods to
3052 * have public visibility.
3053 * String arguments are converted to symbols.
3054 * An Array of Symbols and/or Strings is also accepted.
3055 * If a single argument is passed, it is returned.
3056 * If no argument is passed, nil is returned.
3057 * If multiple arguments are passed, the arguments are returned as an array.
3058 */
3059
3060static VALUE
3061rb_mod_public(int argc, VALUE *argv, VALUE module)
3062{
3063 return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
3064}
3065
3066/*
3067 * call-seq:
3068 * protected -> nil
3069 * protected(method_name) -> method_name
3070 * protected(method_name, method_name, ...) -> array
3071 * protected(array) -> array
3072 *
3073 * Sets the visibility of a section or of a list of method names as protected.
3074 * Accepts no arguments, a splat of method names (symbols or strings) or an
3075 * array of method names. Returns the arguments that it received.
3076 *
3077 * == Important difference between protected in other languages
3078 *
3079 * Protected methods in Ruby are different from other languages such as Java,
3080 * where methods are marked as protected to give access to subclasses. In Ruby,
3081 * subclasses <b>already have access to all methods defined in the parent
3082 * class</b>, even private ones.
3083 *
3084 * Marking a method as protected allows <b>different objects of the same
3085 * class</b> to call it.
3086 *
3087 * One use case is for comparison methods, such as <code>==</code>, if we want
3088 * to expose a method for comparison between objects of the same class without
3089 * making the method public to objects of other classes.
3090 *
3091 * == Performance considerations
3092 *
3093 * Protected methods are slower than others because they can't use inline
3094 * cache.
3095 *
3096 * == Example
3097 *
3098 * class Account
3099 * # Mark balance as protected, so that we can compare between accounts
3100 * # without making it public.
3101 * attr_reader :balance
3102 * protected :balance
3103 *
3104 * def initialize(balance)
3105 * @balance = balance
3106 * end
3107 *
3108 * def >(other)
3109 * # The invocation to `other.balance` is allowed because `other` is a
3110 * # different object of the same class (Account).
3111 * balance > other.balance
3112 * end
3113 * end
3114 *
3115 * account1 = Account.new(100)
3116 * account2 = Account.new(50)
3117 *
3118 * account1 > account2 # => true (works)
3119 * account1.balance # => NoMethodError (fails because balance is not public)
3120 *
3121 * To show a private method on RDoc, use <code>:doc:</code> instead of this.
3122 */
3123
3124static VALUE
3125rb_mod_protected(int argc, VALUE *argv, VALUE module)
3126{
3127 return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
3128}
3129
3130/*
3131 * call-seq:
3132 * private -> nil
3133 * private(method_name) -> method_name
3134 * private(method_name, method_name, ...) -> array
3135 * private(array) -> array
3136 *
3137 * With no arguments, sets the default visibility for subsequently
3138 * defined methods to private. With arguments, sets the named methods
3139 * to have private visibility.
3140 * String arguments are converted to symbols.
3141 * An Array of Symbols and/or Strings is also accepted.
3142 * If a single argument is passed, it is returned.
3143 * If no argument is passed, nil is returned.
3144 * If multiple arguments are passed, the arguments are returned as an array.
3145 *
3146 * module Mod
3147 * def a() end
3148 * def b() end
3149 * private
3150 * def c() end
3151 * private :a
3152 * end
3153 * Mod.private_instance_methods #=> [:a, :c]
3154 *
3155 * Note that to show a private method on RDoc, use <code>:doc:</code>.
3156 */
3157
3158static VALUE
3159rb_mod_private(int argc, VALUE *argv, VALUE module)
3160{
3161 return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
3162}
3163
3164/*
3165 * call-seq:
3166 * ruby2_keywords(method_name, ...) -> nil
3167 *
3168 * Deprecated: will be removed in Ruby 4.4. Use <tt>...</tt>
3169 * {argument forwarding}[rdoc-ref:syntax/methods.rdoc@Argument+Forwarding]
3170 * or other delegation styles instead; they work correctly on Ruby 3.0
3171 * and later. See https://bugs.ruby-lang.org/issues/22205 for the
3172 * schedule.
3173 *
3174 * For the given method names, marks the method as passing keywords through
3175 * a normal argument splat. This should only be called on methods that
3176 * accept an argument splat (<tt>*args</tt>) but not explicit keywords or
3177 * a keyword splat. It marks the method such that if the method is called
3178 * with keyword arguments, the final hash argument is marked with a special
3179 * flag such that if it is the final element of a normal argument splat to
3180 * another method call, and that method call does not include explicit
3181 * keywords or a keyword splat, the final element is interpreted as keywords.
3182 * In other words, keywords will be passed through the method to other
3183 * methods.
3184 *
3185 * This should only be used for methods that delegate keywords to another
3186 * method, and only for backwards compatibility with Ruby versions before 3.0.
3187 * See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
3188 * for details on why +ruby2_keywords+ exists and when and how to use it.
3189 */
3190
3191static VALUE
3192rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module)
3193{
3194 int i;
3195 VALUE origin_class = RCLASS_ORIGIN(module);
3196
3198 rb_check_frozen(module);
3199
3200 for (i = 0; i < argc; i++) {
3201 VALUE v = argv[i];
3202 ID name = rb_check_id(&v);
3204 VALUE defined_class;
3205
3206 if (!name) {
3207 rb_print_undef_str(module, v);
3208 }
3209
3210 me = search_method(origin_class, name, &defined_class);
3211 if (!me && RB_TYPE_P(module, T_MODULE)) {
3212 me = search_method(rb_cObject, name, &defined_class);
3213 }
3214
3215 if (UNDEFINED_METHOD_ENTRY_P(me) ||
3216 UNDEFINED_REFINED_METHOD_P(me->def)) {
3217 rb_print_undef(module, name, METHOD_VISI_UNDEF);
3218 }
3219
3220 if (module == defined_class || origin_class == defined_class) {
3221 switch (me->def->type) {
3222 case VM_METHOD_TYPE_ISEQ:
3223 if (ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_rest &&
3224 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_post &&
3225 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kw &&
3226 !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kwrest) {
3227 ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.ruby2_keywords = 1;
3228 rb_clear_method_cache(module, name);
3229 }
3230 else {
3231 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));
3232 }
3233 break;
3234 case VM_METHOD_TYPE_BMETHOD: {
3235 VALUE procval = me->def->body.bmethod.proc;
3236 if (vm_block_handler_type(procval) == block_handler_type_proc) {
3237 procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
3238 }
3239
3240 if (vm_block_handler_type(procval) == block_handler_type_iseq) {
3241 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval);
3242 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
3243 if (ISEQ_BODY(iseq)->param.flags.has_rest &&
3244 !ISEQ_BODY(iseq)->param.flags.has_post &&
3245 !ISEQ_BODY(iseq)->param.flags.has_kw &&
3246 !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
3247 ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1;
3248 rb_clear_method_cache(module, name);
3249 }
3250 else {
3251 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));
3252 }
3253 break;
3254 }
3255 }
3256 /* fallthrough */
3257 default:
3258 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method not defined in Ruby)", QUOTE_ID(name));
3259 break;
3260 }
3261 }
3262 else {
3263 rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (can only set in method defining module)", QUOTE_ID(name));
3264 }
3265 }
3266 return Qnil;
3267}
3268
3269/*
3270 * call-seq:
3271 * mod.public_class_method(symbol, ...) -> mod
3272 * mod.public_class_method(string, ...) -> mod
3273 * mod.public_class_method(array) -> mod
3274 *
3275 * Makes a list of existing class methods public.
3276 *
3277 * String arguments are converted to symbols.
3278 * An Array of Symbols and/or Strings is also accepted.
3279 */
3280
3281static VALUE
3282rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
3283{
3284 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
3285 return obj;
3286}
3287
3288/*
3289 * call-seq:
3290 * mod.private_class_method(symbol, ...) -> mod
3291 * mod.private_class_method(string, ...) -> mod
3292 * mod.private_class_method(array) -> mod
3293 *
3294 * Makes existing class methods private. Often used to hide the default
3295 * constructor <code>new</code>.
3296 *
3297 * String arguments are converted to symbols.
3298 * An Array of Symbols and/or Strings is also accepted.
3299 *
3300 * class SimpleSingleton # Not thread safe
3301 * private_class_method :new
3302 * def SimpleSingleton.create(*args, &block)
3303 * @me = new(*args, &block) if ! @me
3304 * @me
3305 * end
3306 * end
3307 */
3308
3309static VALUE
3310rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
3311{
3312 set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
3313 return obj;
3314}
3315
3316/*
3317 * call-seq:
3318 * public
3319 * public(symbol, ...)
3320 * public(string, ...)
3321 * public(array)
3322 *
3323 * With no arguments, sets the default visibility for subsequently
3324 * defined methods to public. With arguments, sets the named methods to
3325 * have public visibility.
3326 *
3327 * String arguments are converted to symbols.
3328 * An Array of Symbols and/or Strings is also accepted.
3329 */
3330
3331static VALUE
3332top_public(int argc, VALUE *argv, VALUE _)
3333{
3334 return rb_mod_public(argc, argv, rb_top_main_class("public"));
3335}
3336
3337/*
3338 * call-seq:
3339 * private
3340 * private(symbol, ...)
3341 * private(string, ...)
3342 * private(array)
3343 *
3344 * With no arguments, sets the default visibility for subsequently
3345 * defined methods to private. With arguments, sets the named methods to
3346 * have private visibility.
3347 *
3348 * String arguments are converted to symbols.
3349 * An Array of Symbols and/or Strings is also accepted.
3350 */
3351static VALUE
3352top_private(int argc, VALUE *argv, VALUE _)
3353{
3354 return rb_mod_private(argc, argv, rb_top_main_class("private"));
3355}
3356
3357/*
3358 * call-seq:
3359 * ruby2_keywords(method_name, ...) -> self
3360 *
3361 * Deprecated: will be removed in Ruby 4.4. Use <tt>...</tt>
3362 * {argument forwarding}[rdoc-ref:syntax/methods.rdoc@Argument+Forwarding]
3363 * or other delegation styles instead; they work correctly on Ruby 3.0
3364 * and later. See https://bugs.ruby-lang.org/issues/22205 for the
3365 * schedule.
3366 *
3367 * For the given method names, marks the method as passing keywords through
3368 * a normal argument splat. See Module#ruby2_keywords in detail.
3369 */
3370static VALUE
3371top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
3372{
3373 return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
3374}
3375
3376/*
3377 * call-seq:
3378 * module_function -> nil
3379 * module_function(method_name) -> method_name
3380 * module_function(method_name, method_name, ...) -> array
3381 *
3382 * Creates module functions for the named methods. These functions may
3383 * be called with the module as a receiver, and also become available
3384 * as instance methods to classes that mix in the module. Module
3385 * functions are copies of the original, and so may be changed
3386 * independently. The instance-method versions are made private. If
3387 * used with no arguments, subsequently defined methods become module
3388 * functions.
3389 * String arguments are converted to symbols.
3390 * If a single argument is passed, it is returned.
3391 * If no argument is passed, nil is returned.
3392 * If multiple arguments are passed, the arguments are returned as an array.
3393 *
3394 * module Mod
3395 * def one
3396 * "This is one"
3397 * end
3398 * module_function :one
3399 * end
3400 * class Cls
3401 * include Mod
3402 * def call_one
3403 * one
3404 * end
3405 * end
3406 * Mod.one #=> "This is one"
3407 * c = Cls.new
3408 * c.call_one #=> "This is one"
3409 * module Mod
3410 * def one
3411 * "This is the new one"
3412 * end
3413 * end
3414 * Mod.one #=> "This is one"
3415 * c.call_one #=> "This is the new one"
3416 */
3417
3418static VALUE
3419rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
3420{
3421 int i;
3422 ID id;
3423 const rb_method_entry_t *me;
3424
3425 if (!RB_TYPE_P(module, T_MODULE)) {
3426 rb_raise(rb_eTypeError, "module_function must be called for modules");
3427 }
3428
3429 if (argc == 0) {
3430 rb_scope_module_func_set();
3431 return Qnil;
3432 }
3433
3434 set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);
3435
3436 for (i = 0; i < argc; i++) {
3437 VALUE m = module;
3438
3439 id = rb_to_id(argv[i]);
3440 for (;;) {
3441 me = search_method(m, id, 0);
3442 if (me == 0) {
3443 me = search_method(rb_cObject, id, 0);
3444 }
3445 if (UNDEFINED_METHOD_ENTRY_P(me)) {
3446 rb_print_undef(module, id, METHOD_VISI_UNDEF);
3447 }
3448 if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
3449 break; /* normal case: need not to follow 'super' link */
3450 }
3451 m = RCLASS_SUPER(m);
3452 if (!m)
3453 break;
3454 }
3455 rb_method_entry_set(rb_singleton_class(module), id, me, METHOD_VISI_PUBLIC);
3456 }
3457 if (argc == 1) {
3458 return argv[0];
3459 }
3460 return rb_ary_new_from_values(argc, argv);
3461}
3462
3463#ifdef __GNUC__
3464#pragma push_macro("rb_method_basic_definition_p")
3465#undef rb_method_basic_definition_p
3466#endif
3467int
3469{
3470 const rb_callable_method_entry_t *cme;
3471 if (!klass) return TRUE; /* hidden object cannot be overridden */
3472 cme = rb_callable_method_entry(klass, id);
3473 return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
3474}
3475#ifdef __GNUC__
3476#pragma pop_macro("rb_method_basic_definition_p")
3477#endif
3478
3479static VALUE
3480call_method_entry(rb_execution_context_t *ec, VALUE defined_class, VALUE obj, ID id,
3481 const rb_callable_method_entry_t *cme, int argc, const VALUE *argv, int kw_splat)
3482{
3483 VALUE passed_block_handler = vm_passed_block_handler(ec);
3484 VALUE result = rb_vm_call_kw(ec, obj, id, argc, argv, cme, kw_splat);
3485 vm_passed_block_handler_set(ec, passed_block_handler);
3486 return result;
3487}
3488
3489static VALUE
3490basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
3491 VALUE mid, VALUE priv)
3492{
3493 VALUE defined_class, args[2];
3494 const ID rtmid = idRespond_to_missing;
3495 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, rtmid, &defined_class);
3496
3497 if (!cme || METHOD_ENTRY_BASIC(cme)) return Qundef;
3498 args[0] = mid;
3499 args[1] = priv;
3500 return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args, RB_NO_KEYWORDS);
3501}
3502
3503static inline int
3504basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
3505{
3506 VALUE klass = CLASS_OF(obj);
3507 VALUE ret;
3508
3509 switch (method_boundp(klass, id, pub|BOUND_RESPONDS)) {
3510 case 2:
3511 return FALSE;
3512 case 0:
3513 ret = basic_obj_respond_to_missing(ec, klass, obj, ID2SYM(id),
3514 RBOOL(!pub));
3515 return RTEST(ret) && !UNDEF_P(ret);
3516 default:
3517 return TRUE;
3518 }
3519}
3520
3521static int
3522vm_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
3523{
3524 VALUE defined_class;
3525 const ID resid = idRespond_to;
3526 const rb_callable_method_entry_t *const cme = callable_method_entry(klass, resid, &defined_class);
3527
3528 if (!cme) return -1;
3529 if (METHOD_ENTRY_BASIC(cme)) {
3530 return -1;
3531 }
3532 else {
3533 int argc = 1;
3534 VALUE args[2];
3535 VALUE result;
3536
3537 args[0] = ID2SYM(id);
3538 args[1] = Qtrue;
3539 if (priv) {
3540 argc = rb_method_entry_arity((const rb_method_entry_t *)cme);
3541 if (argc > 2) {
3542 rb_raise(rb_eArgError,
3543 "respond_to? must accept 1 or 2 arguments (requires %d)",
3544 argc);
3545 }
3546 if (argc != 1) {
3547 argc = 2;
3548 }
3549 else if (!NIL_P(ruby_verbose)) {
3550 VALUE location = rb_method_entry_location((const rb_method_entry_t *)cme);
3552 "%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") uses"
3553 " the deprecated method signature, which takes one parameter",
3554 (RCLASS_SINGLETON_P(klass) ? obj : klass),
3555 (RCLASS_SINGLETON_P(klass) ? '.' : '#'),
3556 QUOTE_ID(id));
3557 if (!NIL_P(location)) {
3558 VALUE path = RARRAY_AREF(location, 0);
3559 VALUE line = RARRAY_AREF(location, 1);
3560 if (!NIL_P(path)) {
3562 RSTRING_PTR(path), NUM2INT(line),
3563 "respond_to? is defined here");
3564 }
3565 }
3566 }
3567 }
3568 result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args, RB_NO_KEYWORDS);
3569 return RTEST(result);
3570 }
3571}
3572
3573int
3574rb_obj_respond_to(VALUE obj, ID id, int priv)
3575{
3576 rb_execution_context_t *ec = GET_EC();
3577 return rb_ec_obj_respond_to(ec, obj, id, priv);
3578}
3579
3580int
3581rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int priv)
3582{
3583 VALUE klass = CLASS_OF(obj);
3584 int ret = vm_respond_to(ec, klass, obj, id, priv);
3585 if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
3586 return ret;
3587}
3588
3589int
3591{
3592 return rb_obj_respond_to(obj, id, FALSE);
3593}
3594
3595
3596/*
3597 * call-seq:
3598 * obj.respond_to?(symbol, include_all=false) -> true or false
3599 * obj.respond_to?(string, include_all=false) -> true or false
3600 *
3601 * Returns +true+ if _obj_ responds to the given method. Private and
3602 * protected methods are included in the search only if the optional
3603 * second parameter evaluates to +true+.
3604 *
3605 * If the method is not implemented,
3606 * as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
3607 * false is returned.
3608 *
3609 * If the method is not defined, <code>respond_to_missing?</code>
3610 * method is called and the result is returned.
3611 *
3612 * When the method name parameter is given as a string, the string is
3613 * converted to a symbol.
3614 */
3615
3616static VALUE
3617obj_respond_to(int argc, VALUE *argv, VALUE obj)
3618{
3619 VALUE mid, priv;
3620 ID id;
3621 rb_execution_context_t *ec = GET_EC();
3622
3623 rb_scan_args(argc, argv, "11", &mid, &priv);
3624 if (!(id = rb_check_id(&mid))) {
3625 VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
3626 rb_to_symbol(mid), priv);
3627 if (UNDEF_P(ret)) ret = Qfalse;
3628 return ret;
3629 }
3630 return RBOOL(basic_obj_respond_to(ec, obj, id, !RTEST(priv)));
3631}
3632
3633/*
3634 * call-seq:
3635 * obj.respond_to_missing?(symbol, include_all) -> true or false
3636 * obj.respond_to_missing?(string, include_all) -> true or false
3637 *
3638 * DO NOT USE THIS DIRECTLY.
3639 *
3640 * Hook method to return whether the _obj_ can respond to _id_ method
3641 * or not.
3642 *
3643 * When the method name parameter is given as a string, the string is
3644 * converted to a symbol.
3645 *
3646 * See #respond_to?, and the example of BasicObject.
3647 */
3648static VALUE
3649obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
3650{
3651 return Qfalse;
3652}
3653
3654void
3655Init_eval_method(void)
3656{
3657 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
3658 rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2);
3659
3660 rb_define_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
3661 rb_define_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
3662 rb_define_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
3663 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
3664 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
3665 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
3666 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
3667 rb_define_private_method(rb_cModule, "ruby2_keywords", rb_mod_ruby2_keywords, -1);
3668
3669 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
3670 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
3671 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
3672 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
3673 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
3674 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
3675
3677 "public", top_public, -1);
3679 "private", top_private, -1);
3681 "ruby2_keywords", top_ruby2_keywords, -1);
3682
3683 {
3684#define REPLICATE_METHOD(klass, id) do { \
3685 const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
3686 rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
3687 } while (0)
3688
3689 REPLICATE_METHOD(rb_eException, idMethodMissing);
3690 REPLICATE_METHOD(rb_eException, idRespond_to);
3691 REPLICATE_METHOD(rb_eException, idRespond_to_missing);
3692 }
3693}
#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:2870
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:444
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:3203
#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:1431
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:1423
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:468
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
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:2504
#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:943
#define rb_str_new_lit(str)
Identical to rb_str_new_static(), except it cannot take string variables.
Definition string.h:1706
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:151
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3590
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:1809
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2887
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:3468
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:2467
void rb_remove_method(VALUE klass, const char *name)
Removes a method.
Definition vm_method.c:2316
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1818
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:2310
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:909
int rb_method_boundp(VALUE klass, ID id, int ex)
Queries if the klass has this method.
Definition vm_method.c:2428
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:3574
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1288
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:13740
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:13730
int capa
Designed capacity of the buffer.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
#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 method.h:63
CREF (Class REFerence)
Definition method.h:45
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:238
size_t(* dsize)(const void *)
This function is to query the size of the underlying memory regions.
Definition rtypeddata.h:278
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:268
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:245
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:352
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