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