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