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