Ruby: vm_method.c Source File

Ruby 3.4.4p34 (2025-05-14 revision a38531fd3f617bf734ef7d6c595325f69985ea1d)

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