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