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