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