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