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