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