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