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