Ruby: vm_method.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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