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