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