Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

class.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 class.c -
4
5 $Author: nagachika $
6 created at: Tue Aug 10 15:05:44 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10 **********************************************************************/
11
26 #include "ruby/ruby.h"
27 #include "ruby/st.h"
28 #include "method.h"
29 #include "constant.h"
30 #include "vm_core.h"
31 #include "internal.h"
32 #include <ctype.h>
33
34 int rb_vm_add_root_module(ID id, VALUE module);
35
36
37 #define id_attached id__attached__
38
39 void
40 rb_class_subclass_add(VALUE super, VALUE klass)
41 {
42 rb_subclass_entry_t *entry, *head;
43
44 if (super && super != Qundef) {
45 entry = xmalloc(sizeof(*entry));
46 entry->klass = klass;
47 entry->next = NULL;
48
49 head = RCLASS_EXT(super)->subclasses;
50 if (head) {
51 entry->next = head;
52 RCLASS_EXT(head->klass)->parent_subclasses = &entry->next;
53 }
54
55 RCLASS_EXT(super)->subclasses = entry;
56 RCLASS_EXT(klass)->parent_subclasses = &RCLASS_EXT(super)->subclasses;
57 }
58 }
59
60 static void
61 rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
62 {
63 rb_subclass_entry_t *entry, *head;
64
65 entry = xmalloc(sizeof(*entry));
66 entry->klass = iclass;
67 entry->next = NULL;
68
69 head = RCLASS_EXT(module)->subclasses;
70 if (head) {
71 entry->next = head;
72 RCLASS_EXT(head->klass)->module_subclasses = &entry->next;
73 }
74
75 RCLASS_EXT(module)->subclasses = entry;
76 RCLASS_EXT(iclass)->module_subclasses = &RCLASS_EXT(module)->subclasses;
77 }
78
79 void
80 rb_class_remove_from_super_subclasses(VALUE klass)
81 {
82 rb_subclass_entry_t *entry;
83
84 if (RCLASS_EXT(klass)->parent_subclasses) {
85 entry = *RCLASS_EXT(klass)->parent_subclasses;
86
87 *RCLASS_EXT(klass)->parent_subclasses = entry->next;
88 if (entry->next) {
89 RCLASS_EXT(entry->next->klass)->parent_subclasses = RCLASS_EXT(klass)->parent_subclasses;
90 }
91 xfree(entry);
92 }
93
94 RCLASS_EXT(klass)->parent_subclasses = NULL;
95 }
96
97 void
98 rb_class_remove_from_module_subclasses(VALUE klass)
99 {
100 rb_subclass_entry_t *entry;
101
102 if (RCLASS_EXT(klass)->module_subclasses) {
103 entry = *RCLASS_EXT(klass)->module_subclasses;
104 *RCLASS_EXT(klass)->module_subclasses = entry->next;
105
106 if (entry->next) {
107 RCLASS_EXT(entry->next->klass)->module_subclasses = RCLASS_EXT(klass)->module_subclasses;
108 }
109
110 xfree(entry);
111 }
112
113 RCLASS_EXT(klass)->module_subclasses = NULL;
114 }
115
116 void
117 rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE))
118 {
119 rb_subclass_entry_t *cur = RCLASS_EXT(klass)->subclasses;
120
121 /* do not be tempted to simplify this loop into a for loop, the order of
122 operations is important here if `f` modifies the linked list */
123 while (cur) {
124 VALUE curklass = cur->klass;
125 cur = cur->next;
126 f(curklass);
127 }
128 }
129
130 void
131 rb_class_detach_subclasses(VALUE klass)
132 {
133 rb_class_foreach_subclass(klass, rb_class_remove_from_super_subclasses);
134 }
135
136 void
137 rb_class_detach_module_subclasses(VALUE klass)
138 {
139 rb_class_foreach_subclass(klass, rb_class_remove_from_module_subclasses);
140 }
141
154 static VALUE
155 class_alloc(VALUE flags, VALUE klass)
156 {
157 NEWOBJ_OF(obj, struct RClass, klass, (flags & T_MASK) | (RGENGC_WB_PROTECTED_CLASS ? FL_WB_PROTECTED : 0));
158 obj->ptr = ALLOC(rb_classext_t);
159 RCLASS_IV_TBL(obj) = 0;
160 RCLASS_CONST_TBL(obj) = 0;
161 RCLASS_M_TBL_WRAPPER(obj) = 0;
162 RCLASS_SET_SUPER((VALUE)obj, 0);
163 RCLASS_ORIGIN(obj) = (VALUE)obj;
164 RCLASS_IV_INDEX_TBL(obj) = 0;
165
166 RCLASS_EXT(obj)->subclasses = NULL;
167 RCLASS_EXT(obj)->parent_subclasses = NULL;
168 RCLASS_EXT(obj)->module_subclasses = NULL;
169 RCLASS_SERIAL(obj) = rb_next_class_serial();
170
171 RCLASS_REFINED_CLASS(obj) = Qnil;
172 RCLASS_EXT(obj)->allocator = 0;
173 return (VALUE)obj;
174 }
175
176
186 VALUE
187 rb_class_boot(VALUE super)
188 {
189 VALUE klass = class_alloc(T_CLASS, rb_cClass);
190
191 RCLASS_SET_SUPER(klass, super);
192 RCLASS_M_TBL_INIT(klass);
193
194 OBJ_INFECT(klass, super);
195 return (VALUE)klass;
196 }
197
198
205 void
206 rb_check_inheritable(VALUE super)
207 {
208 if (!RB_TYPE_P(super, T_CLASS)) {
209 rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
210 rb_obj_classname(super));
211 }
212 if (RBASIC(super)->flags & FL_SINGLETON) {
213 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
214 }
215 if (super == rb_cClass) {
216 rb_raise(rb_eTypeError, "can't make subclass of Class");
217 }
218 }
219
220
227 VALUE
228 rb_class_new(VALUE super)
229 {
230 Check_Type(super, T_CLASS);
231 rb_check_inheritable(super);
232 return rb_class_boot(super);
233 }
234
235 static void
236 rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass, NODE **new_cref_ptr)
237 {
238 NODE *new_node;
239 while (node) {
240 if (node->nd_clss == old_klass) {
241 new_node = NEW_CREF(new_klass);
242 RB_OBJ_WRITE(new_node, &new_node->nd_next, node->nd_next);
243 *new_cref_ptr = new_node;
244 return;
245 }
246 new_node = NEW_CREF(node->nd_clss);
247 node = node->nd_next;
248 *new_cref_ptr = new_node;
249 new_cref_ptr = &new_node->nd_next;
250 }
251 *new_cref_ptr = NULL;
252 }
253
254 static void
255 clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
256 {
257 VALUE newiseqval;
258 if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
259 rb_iseq_t *iseq;
260 NODE *new_cref;
261 newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
262 GetISeqPtr(newiseqval, iseq);
263 rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass, &new_cref);
264 RB_OBJ_WRITE(iseq->self, &iseq->cref_stack, new_cref);
265 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
266 RB_GC_GUARD(newiseqval);
267 }
268 else {
269 rb_method_entry_set(klass, mid, me, me->flag);
270 }
271 }
272
273 static int
274 clone_method_i(st_data_t key, st_data_t value, st_data_t data)
275 {
276 clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
277 return ST_CONTINUE;
278 }
279
280 struct clone_const_arg {
281 VALUE klass;
282 st_table *tbl;
283 };
284
285 static int
286 clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
287 {
288 rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
289 MEMCPY(nce, ce, rb_const_entry_t, 1);
290 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
291 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
292
293 st_insert(arg->tbl, key, (st_data_t)nce);
294 return ST_CONTINUE;
295 }
296
297 static int
298 clone_const_i(st_data_t key, st_data_t value, st_data_t data)
299 {
300 return clone_const((ID)key, (const rb_const_entry_t *)value, (struct clone_const_arg *)data);
301 }
302
303 static void
304 class_init_copy_check(VALUE clone, VALUE orig)
305 {
306 if (orig == rb_cBasicObject) {
307 rb_raise(rb_eTypeError, "can't copy the root class");
308 }
309 if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
310 rb_raise(rb_eTypeError, "already initialized class");
311 }
312 if (FL_TEST(orig, FL_SINGLETON)) {
313 rb_raise(rb_eTypeError, "can't copy singleton class");
314 }
315 }
316
317 /* :nodoc: */
318 VALUE
319 rb_mod_init_copy(VALUE clone, VALUE orig)
320 {
321 if (RB_TYPE_P(clone, T_CLASS)) {
322 class_init_copy_check(clone, orig);
323 }
324 if (!OBJ_INIT_COPY(clone, orig)) return clone;
325 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
326 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
327 rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
328 }
329 RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
330 RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
331 if (RCLASS_IV_TBL(clone)) {
332 st_free_table(RCLASS_IV_TBL(clone));
333 RCLASS_IV_TBL(clone) = 0;
334 }
335 if (RCLASS_CONST_TBL(clone)) {
336 rb_free_const_table(RCLASS_CONST_TBL(clone));
337 RCLASS_CONST_TBL(clone) = 0;
338 }
339 if (RCLASS_M_TBL_WRAPPER(clone)) {
340 rb_free_m_tbl_wrapper(RCLASS_M_TBL_WRAPPER(clone));
341 RCLASS_M_TBL_WRAPPER(clone) = 0;
342 }
343 if (RCLASS_IV_TBL(orig)) {
344 st_data_t id;
345
346 RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(orig));
347 CONST_ID(id, "__tmp_classpath__");
348 st_delete(RCLASS_IV_TBL(clone), &id, 0);
349 CONST_ID(id, "__classpath__");
350 st_delete(RCLASS_IV_TBL(clone), &id, 0);
351 CONST_ID(id, "__classid__");
352 st_delete(RCLASS_IV_TBL(clone), &id, 0);
353 }
354 if (RCLASS_CONST_TBL(orig)) {
355 struct clone_const_arg arg;
356
357 RCLASS_CONST_TBL(clone) = st_init_numtable();
358 arg.klass = clone;
359 arg.tbl = RCLASS_CONST_TBL(clone);
360 st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)&arg);
361 }
362 if (RCLASS_M_TBL(orig)) {
363 RCLASS_M_TBL_INIT(clone);
364 st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
365 }
366
367 return clone;
368 }
369
370 VALUE
371 rb_singleton_class_clone(VALUE obj)
372 {
373 return rb_singleton_class_clone_and_attach(obj, Qundef);
374 }
375
376 VALUE
377 rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
378 {
379 VALUE klass = RBASIC(obj)->klass;
380
381 if (!FL_TEST(klass, FL_SINGLETON))
382 return klass;
383 else {
384 /* copy singleton(unnamed) class */
385 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
386
387 if (BUILTIN_TYPE(obj) == T_CLASS) {
388 RBASIC_SET_CLASS(clone, clone);
389 }
390 else {
391 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(klass));
392 }
393
394 RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
395 RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
396 if (RCLASS_IV_TBL(klass)) {
397 RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(klass));
398 }
399 if (RCLASS_CONST_TBL(klass)) {
400 struct clone_const_arg arg;
401 RCLASS_CONST_TBL(clone) = st_init_numtable();
402 arg.klass = clone;
403 arg.tbl = RCLASS_CONST_TBL(clone);
404 st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)&arg);
405 }
406 if (attach != Qundef) {
407 rb_singleton_class_attached(clone, attach);
408 }
409 RCLASS_M_TBL_INIT(clone);
410 st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
411 rb_singleton_class_attached(RBASIC(clone)->klass, clone);
412 FL_SET(clone, FL_SINGLETON);
413
414 return clone;
415 }
416 }
417
422 void
423 rb_singleton_class_attached(VALUE klass, VALUE obj)
424 {
425 if (FL_TEST(klass, FL_SINGLETON)) {
426 if (!RCLASS_IV_TBL(klass)) {
427 RCLASS_IV_TBL(klass) = st_init_numtable();
428 }
429 rb_st_insert_id_and_value(klass, RCLASS_IV_TBL(klass), id_attached, obj);
430 }
431 }
432
433
434
435 #define METACLASS_OF(k) RBASIC(k)->klass
436 #define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
437
443 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
444
450 #define HAVE_METACLASS_P(k) \
451 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
452 rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
453
461 #define ENSURE_EIGENCLASS(klass) \
462 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
463
464
474 static inline VALUE
475 make_metaclass(VALUE klass)
476 {
477 VALUE super;
478 VALUE metaclass = rb_class_boot(Qundef);
479
480 FL_SET(metaclass, FL_SINGLETON);
481 rb_singleton_class_attached(metaclass, klass);
482
483 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
484 SET_METACLASS_OF(klass, metaclass);
485 SET_METACLASS_OF(metaclass, metaclass);
486 }
487 else {
488 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
489 SET_METACLASS_OF(klass, metaclass);
490 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
491 }
492
493 super = RCLASS_SUPER(klass);
494 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
495 RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
496
497 OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
498
499 return metaclass;
500 }
501
508 static inline VALUE
509 make_singleton_class(VALUE obj)
510 {
511 VALUE orig_class = RBASIC(obj)->klass;
512 VALUE klass = rb_class_boot(orig_class);
513
514 FL_SET(klass, FL_SINGLETON);
515 RBASIC_SET_CLASS(obj, klass);
516 rb_singleton_class_attached(klass, obj);
517
518 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
519 return klass;
520 }
521
522
523 static VALUE
524 boot_defclass(const char *name, VALUE super)
525 {
526 VALUE obj = rb_class_boot(super);
527 ID id = rb_intern(name);
528
529 rb_name_class(obj, id);
530 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
531 return obj;
532 }
533
534 void
535 Init_class_hierarchy(void)
536 {
537 rb_cBasicObject = boot_defclass("BasicObject", 0);
538 rb_cObject = boot_defclass("Object", rb_cBasicObject);
539 rb_cModule = boot_defclass("Module", rb_cObject);
540 rb_cClass = boot_defclass("Class", rb_cModule);
541
542 rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
543 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
544 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
545 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
546 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
547 }
548
549
560 VALUE
561 rb_make_metaclass(VALUE obj, VALUE unused)
562 {
563 if (BUILTIN_TYPE(obj) == T_CLASS) {
564 return make_metaclass(obj);
565 }
566 else {
567 return make_singleton_class(obj);
568 }
569 }
570
571
582 VALUE
583 rb_define_class_id(ID id, VALUE super)
584 {
585 VALUE klass;
586
587 if (!super) super = rb_cObject;
588 klass = rb_class_new(super);
589 rb_make_metaclass(klass, RBASIC(super)->klass);
590
591 return klass;
592 }
593
594
603 VALUE
604 rb_class_inherited(VALUE super, VALUE klass)
605 {
606 ID inherited;
607 if (!super) super = rb_cObject;
608 CONST_ID(inherited, "inherited");
609 return rb_funcall(super, inherited, 1, klass);
610 }
611
612
613
629 VALUE
630 rb_define_class(const char *name, VALUE super)
631 {
632 VALUE klass;
633 ID id;
634
635 id = rb_intern(name);
636 if (rb_const_defined(rb_cObject, id)) {
637 klass = rb_const_get(rb_cObject, id);
638 if (!RB_TYPE_P(klass, T_CLASS)) {
639 rb_raise(rb_eTypeError, "%s is not a class", name);
640 }
641 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
642 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
643 }
644 return klass;
645 }
646 if (!super) {
647 rb_warn("no super class for `%s', Object assumed", name);
648 }
649 klass = rb_define_class_id(id, super);
650 rb_vm_add_root_module(id, klass);
651 rb_name_class(klass, id);
652 rb_const_set(rb_cObject, id, klass);
653 rb_class_inherited(super, klass);
654
655 return klass;
656 }
657
658
675 VALUE
676 rb_define_class_under(VALUE outer, const char *name, VALUE super)
677 {
678 return rb_define_class_id_under(outer, rb_intern(name), super);
679 }
680
681
698 VALUE
699 rb_define_class_id_under(VALUE outer, ID id, VALUE super)
700 {
701 VALUE klass;
702
703 if (rb_const_defined_at(outer, id)) {
704 klass = rb_const_get_at(outer, id);
705 if (!RB_TYPE_P(klass, T_CLASS)) {
706 rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
707 }
708 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
709 rb_name_error(id, "%s is already defined", rb_id2name(id));
710 }
711 return klass;
712 }
713 if (!super) {
714 rb_warn("no super class for `%s::%s', Object assumed",
715 rb_class2name(outer), rb_id2name(id));
716 }
717 klass = rb_define_class_id(id, super);
718 rb_set_class_path_string(klass, outer, rb_id2str(id));
719 rb_const_set(outer, id, klass);
720 rb_class_inherited(super, klass);
721 rb_gc_register_mark_object(klass);
722
723 return klass;
724 }
725
726 VALUE
727 rb_module_new(void)
728 {
729 VALUE mdl = class_alloc(T_MODULE, rb_cModule);
730 RCLASS_M_TBL_INIT(mdl);
731 return (VALUE)mdl;
732 }
733
734 VALUE
735 rb_define_module_id(ID id)
736 {
737 VALUE mdl;
738
739 mdl = rb_module_new();
740 rb_name_class(mdl, id);
741
742 return mdl;
743 }
744
745 VALUE
746 rb_define_module(const char *name)
747 {
748 VALUE module;
749 ID id;
750
751 id = rb_intern(name);
752 if (rb_const_defined(rb_cObject, id)) {
753 module = rb_const_get(rb_cObject, id);
754 if (RB_TYPE_P(module, T_MODULE))
755 return module;
756 rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
757 }
758 module = rb_define_module_id(id);
759 rb_vm_add_root_module(id, module);
760 rb_const_set(rb_cObject, id, module);
761
762 return module;
763 }
764
765 VALUE
766 rb_define_module_under(VALUE outer, const char *name)
767 {
768 return rb_define_module_id_under(outer, rb_intern(name));
769 }
770
771 VALUE
772 rb_define_module_id_under(VALUE outer, ID id)
773 {
774 VALUE module;
775
776 if (rb_const_defined_at(outer, id)) {
777 module = rb_const_get_at(outer, id);
778 if (RB_TYPE_P(module, T_MODULE))
779 return module;
780 rb_raise(rb_eTypeError, "%s::%s is not a module",
781 rb_class2name(outer), rb_obj_classname(module));
782 }
783 module = rb_define_module_id(id);
784 rb_const_set(outer, id, module);
785 rb_set_class_path_string(module, outer, rb_id2str(id));
786 rb_gc_register_mark_object(module);
787
788 return module;
789 }
790
791 VALUE
792 rb_include_class_new(VALUE module, VALUE super)
793 {
794 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
795
796 if (BUILTIN_TYPE(module) == T_ICLASS) {
797 module = RBASIC(module)->klass;
798 }
799 if (!RCLASS_IV_TBL(module)) {
800 RCLASS_IV_TBL(module) = st_init_numtable();
801 }
802 if (!RCLASS_CONST_TBL(module)) {
803 RCLASS_CONST_TBL(module) = st_init_numtable();
804 }
805 RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
806 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
807
808 RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(klass)) =
809 RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(RCLASS_ORIGIN(module)));
810
811 RCLASS_SET_SUPER(klass, super);
812 if (RB_TYPE_P(module, T_ICLASS)) {
813 RBASIC_SET_CLASS(klass, RBASIC(module)->klass);
814 }
815 else {
816 RBASIC_SET_CLASS(klass, module);
817 }
818 OBJ_INFECT(klass, module);
819 OBJ_INFECT(klass, super);
820
821 return (VALUE)klass;
822 }
823
824 static int include_modules_at(const VALUE klass, VALUE c, VALUE module);
825
826 void
827 rb_include_module(VALUE klass, VALUE module)
828 {
829 int changed = 0;
830
831 rb_frozen_class_p(klass);
832
833 if (!RB_TYPE_P(module, T_MODULE)) {
834 Check_Type(module, T_MODULE);
835 }
836
837 OBJ_INFECT(klass, module);
838
839 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module);
840 if (changed < 0)
841 rb_raise(rb_eArgError, "cyclic include detected");
842 }
843
844 static int
845 add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
846 {
847 rb_add_refined_method_entry((VALUE) data, (ID) key);
848 return ST_CONTINUE;
849 }
850
851 static int
852 include_modules_at(const VALUE klass, VALUE c, VALUE module)
853 {
854 VALUE p, iclass;
855 int method_changed = 0, constant_changed = 0;
856 const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
857
858 while (module) {
859 int superclass_seen = FALSE;
860
861 if (RCLASS_ORIGIN(module) != module)
862 goto skip;
863 if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
864 return -1;
865 /* ignore if the module included already in superclasses */
866 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
867 switch (BUILTIN_TYPE(p)) {
868 case T_ICLASS:
869 if (RCLASS_M_TBL_WRAPPER(p) == RCLASS_M_TBL_WRAPPER(module)) {
870 if (!superclass_seen) {
871 c = p; /* move insertion point */
872 }
873 goto skip;
874 }
875 break;
876 case T_CLASS:
877 superclass_seen = TRUE;
878 break;
879 }
880 }
881 iclass = rb_include_class_new(module, RCLASS_SUPER(c));
882 c = RCLASS_SET_SUPER(c, iclass);
883
884 if (BUILTIN_TYPE(module) == T_ICLASS) {
885 rb_module_add_to_subclasses_list(RBASIC(module)->klass, iclass);
886 } else {
887 rb_module_add_to_subclasses_list(module, iclass);
888 }
889
890 if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
891 VALUE refined_class =
892 rb_refinement_module_get_refined_class(klass);
893
894 st_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i,
895 (st_data_t) refined_class);
896 FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT);
897 }
898 if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
899 method_changed = 1;
900 if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries)
901 constant_changed = 1;
902 skip:
903 module = RCLASS_SUPER(module);
904 }
905
906 if (method_changed) rb_clear_method_cache_by_class(klass);
907 if (constant_changed) rb_clear_constant_cache();
908
909 return method_changed;
910 }
911
912 static int
913 move_refined_method(st_data_t key, st_data_t value, st_data_t data)
914 {
915 rb_method_entry_t *me = (rb_method_entry_t *) value;
916 st_table *tbl = (st_table *) data;
917
918 if (me->def->type == VM_METHOD_TYPE_REFINED) {
919 if (me->def->body.orig_me) {
920 rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me;
921 me->def->body.orig_me = NULL;
922 new_me = ALLOC(rb_method_entry_t);
923 *new_me = *me;
924 st_add_direct(tbl, key, (st_data_t) new_me);
925 *me = *orig_me;
926 xfree(orig_me);
927 return ST_CONTINUE;
928 }
929 else {
930 st_add_direct(tbl, key, (st_data_t) me);
931 return ST_DELETE;
932 }
933 }
934 else {
935 return ST_CONTINUE;
936 }
937 }
938
939 void
940 rb_prepend_module(VALUE klass, VALUE module)
941 {
942 void rb_vm_check_redefinition_by_prepend(VALUE klass);
943 VALUE origin;
944 int changed = 0;
945
946 rb_frozen_class_p(klass);
947
948 Check_Type(module, T_MODULE);
949
950 OBJ_INFECT(klass, module);
951
952 origin = RCLASS_ORIGIN(klass);
953 if (origin == klass) {
954 origin = class_alloc(T_ICLASS, klass);
955 OBJ_WB_UNPROTECT(origin); /* TODO: conservertive shading. Need more survery. */
956 RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
957 RCLASS_SET_SUPER(klass, origin);
958 RCLASS_ORIGIN(klass) = origin;
959 RCLASS_M_TBL_WRAPPER(origin) = RCLASS_M_TBL_WRAPPER(klass);
960 RCLASS_M_TBL_INIT(klass);
961 st_foreach(RCLASS_M_TBL(origin), move_refined_method,
962 (st_data_t) RCLASS_M_TBL(klass));
963 }
964 changed = include_modules_at(klass, klass, module);
965 if (changed < 0)
966 rb_raise(rb_eArgError, "cyclic prepend detected");
967 if (changed) {
968 rb_vm_check_redefinition_by_prepend(klass);
969 }
970 }
971
972 /*
973 * call-seq:
974 * mod.included_modules -> array
975 *
976 * Returns the list of modules included in <i>mod</i>.
977 *
978 * module Mixin
979 * end
980 *
981 * module Outer
982 * include Mixin
983 * end
984 *
985 * Mixin.included_modules #=> []
986 * Outer.included_modules #=> [Mixin]
987 */
988
989 VALUE
990 rb_mod_included_modules(VALUE mod)
991 {
992 VALUE ary = rb_ary_new();
993 VALUE p;
994 VALUE origin = RCLASS_ORIGIN(mod);
995
996 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
997 if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
998 VALUE m = RBASIC(p)->klass;
999 if (RB_TYPE_P(m, T_MODULE))
1000 rb_ary_push(ary, m);
1001 }
1002 }
1003 return ary;
1004 }
1005
1006 /*
1007 * call-seq:
1008 * mod.include?(module) -> true or false
1009 *
1010 * Returns <code>true</code> if <i>module</i> is included in
1011 * <i>mod</i> or one of <i>mod</i>'s ancestors.
1012 *
1013 * module A
1014 * end
1015 * class B
1016 * include A
1017 * end
1018 * class C < B
1019 * end
1020 * B.include?(A) #=> true
1021 * C.include?(A) #=> true
1022 * A.include?(A) #=> false
1023 */
1024
1025 VALUE
1026 rb_mod_include_p(VALUE mod, VALUE mod2)
1027 {
1028 VALUE p;
1029
1030 Check_Type(mod2, T_MODULE);
1031 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1032 if (BUILTIN_TYPE(p) == T_ICLASS) {
1033 if (RBASIC(p)->klass == mod2) return Qtrue;
1034 }
1035 }
1036 return Qfalse;
1037 }
1038
1039 /*
1040 * call-seq:
1041 * mod.ancestors -> array
1042 *
1043 * Returns a list of modules included in <i>mod</i> (including
1044 * <i>mod</i> itself).
1045 *
1046 * module Mod
1047 * include Math
1048 * include Comparable
1049 * end
1050 *
1051 * Mod.ancestors #=> [Mod, Comparable, Math]
1052 * Math.ancestors #=> [Math]
1053 */
1054
1055 VALUE
1056 rb_mod_ancestors(VALUE mod)
1057 {
1058 VALUE p, ary = rb_ary_new();
1059
1060 for (p = mod; p; p = RCLASS_SUPER(p)) {
1061 if (BUILTIN_TYPE(p) == T_ICLASS) {
1062 rb_ary_push(ary, RBASIC(p)->klass);
1063 }
1064 else if (p == RCLASS_ORIGIN(p)) {
1065 rb_ary_push(ary, p);
1066 }
1067 }
1068 return ary;
1069 }
1070
1071 #define VISI(x) ((x)&NOEX_MASK)
1072 #define VISI_CHECK(x,f) (VISI(x) == (f))
1073
1074 static int
1075 ins_methods_push(ID name, long type, VALUE ary, long visi)
1076 {
1077 if (type == -1) return ST_CONTINUE;
1078
1079 switch (visi) {
1080 case NOEX_PRIVATE:
1081 case NOEX_PROTECTED:
1082 case NOEX_PUBLIC:
1083 visi = (type == visi);
1084 break;
1085 default:
1086 visi = (type != NOEX_PRIVATE);
1087 break;
1088 }
1089 if (visi) {
1090 rb_ary_push(ary, ID2SYM(name));
1091 }
1092 return ST_CONTINUE;
1093 }
1094
1095 static int
1096 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1097 {
1098 return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
1099 }
1100
1101 static int
1102 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1103 {
1104 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
1105 }
1106
1107 static int
1108 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1109 {
1110 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
1111 }
1112
1113 static int
1114 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1115 {
1116 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
1117 }
1118
1119 static int
1120 method_entry_i(st_data_t key, st_data_t value, st_data_t data)
1121 {
1122 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1123 st_table *list = (st_table *)data;
1124 long type;
1125
1126 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1127 me = rb_resolve_refined_method(Qnil, me, NULL);
1128 if (!me) return ST_CONTINUE;
1129 }
1130 if (!st_lookup(list, key, 0)) {
1131 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1132 type = -1; /* none */
1133 }
1134 else {
1135 type = VISI(me->flag);
1136 }
1137 st_add_direct(list, key, type);
1138 }
1139 return ST_CONTINUE;
1140 }
1141
1142 static VALUE
1143 class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1144 {
1145 VALUE ary;
1146 int recur, prepended = 0;
1147 st_table *list;
1148
1149 if (argc == 0) {
1150 recur = TRUE;
1151 }
1152 else {
1153 VALUE r;
1154 rb_scan_args(argc, argv, "01", &r);
1155 recur = RTEST(r);
1156 }
1157
1158 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1159 mod = RCLASS_ORIGIN(mod);
1160 prepended = 1;
1161 }
1162
1163 list = st_init_numtable();
1164 for (; mod; mod = RCLASS_SUPER(mod)) {
1165 if (RCLASS_M_TBL(mod)) st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list);
1166 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1167 if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1168 if (!recur) break;
1169 }
1170 ary = rb_ary_new();
1171 st_foreach(list, func, ary);
1172 st_free_table(list);
1173
1174 return ary;
1175 }
1176
1177 /*
1178 * call-seq:
1179 * mod.instance_methods(include_super=true) -> array
1180 *
1181 * Returns an array containing the names of the public and protected instance
1182 * methods in the receiver. For a module, these are the public and protected methods;
1183 * for a class, they are the instance (not singleton) methods. With no
1184 * argument, or with an argument that is <code>false</code>, the
1185 * instance methods in <i>mod</i> are returned, otherwise the methods
1186 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
1187 *
1188 * module A
1189 * def method1() end
1190 * end
1191 * class B
1192 * def method2() end
1193 * end
1194 * class C < B
1195 * def method3() end
1196 * end
1197 *
1198 * A.instance_methods #=> [:method1]
1199 * B.instance_methods(false) #=> [:method2]
1200 * C.instance_methods(false) #=> [:method3]
1201 * C.instance_methods(true).length #=> 43
1202 */
1203
1204 VALUE
1205 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
1206 {
1207 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1208 }
1209
1210 /*
1211 * call-seq:
1212 * mod.protected_instance_methods(include_super=true) -> array
1213 *
1214 * Returns a list of the protected instance methods defined in
1215 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1216 * methods of any ancestors are included.
1217 */
1218
1219 VALUE
1220 rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
1221 {
1222 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1223 }
1224
1225 /*
1226 * call-seq:
1227 * mod.private_instance_methods(include_super=true) -> array
1228 *
1229 * Returns a list of the private instance methods defined in
1230 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1231 * methods of any ancestors are included.
1232 *
1233 * module Mod
1234 * def method1() end
1235 * private :method1
1236 * def method2() end
1237 * end
1238 * Mod.instance_methods #=> [:method2]
1239 * Mod.private_instance_methods #=> [:method1]
1240 */
1241
1242 VALUE
1243 rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
1244 {
1245 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1246 }
1247
1248 /*
1249 * call-seq:
1250 * mod.public_instance_methods(include_super=true) -> array
1251 *
1252 * Returns a list of the public instance methods defined in <i>mod</i>.
1253 * If the optional parameter is not <code>false</code>, the methods of
1254 * any ancestors are included.
1255 */
1256
1257 VALUE
1258 rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
1259 {
1260 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1261 }
1262
1263 /*
1264 * call-seq:
1265 * obj.methods(regular=true) -> array
1266 *
1267 * Returns a list of the names of public and protected methods of
1268 * <i>obj</i>. This will include all the methods accessible in
1269 * <i>obj</i>'s ancestors.
1270 * If the <i>regular</i> parameter is set to <code>false</code>,
1271 * Returns an array of obj's public and protected singleton methods,
1272 * the array will not include methods in modules included in <i>obj</i>.
1273 *
1274 * class Klass
1275 * def klass_method()
1276 * end
1277 * end
1278 * k = Klass.new
1279 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1280 * # :==~, :!, :eql?
1281 * # :hash, :<=>, :class, :singleton_class]
1282 * k.methods.length #=> 57
1283 *
1284 * k.methods(false) #=> []
1285 * def k.singleton_method; end
1286 * k.methods(false) #=> [:singleton_method]
1287 *
1288 * module M123; def m123; end end
1289 * k.extend M123
1290 * k.methods(false) #=> [:singleton_method]
1291 */
1292
1293 VALUE
1294 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1295 {
1296 retry:
1297 if (argc == 0) {
1298 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1299 }
1300 else {
1301 VALUE recur;
1302
1303 rb_scan_args(argc, argv, "1", &recur);
1304 if (RTEST(recur)) {
1305 argc = 0;
1306 goto retry;
1307 }
1308 return rb_obj_singleton_methods(argc, argv, obj);
1309 }
1310 }
1311
1312 /*
1313 * call-seq:
1314 * obj.protected_methods(all=true) -> array
1315 *
1316 * Returns the list of protected methods accessible to <i>obj</i>. If
1317 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1318 * in the receiver will be listed.
1319 */
1320
1321 VALUE
1322 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1323 {
1324 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1325 }
1326
1327 /*
1328 * call-seq:
1329 * obj.private_methods(all=true) -> array
1330 *
1331 * Returns the list of private methods accessible to <i>obj</i>. If
1332 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1333 * in the receiver will be listed.
1334 */
1335
1336 VALUE
1337 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1338 {
1339 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1340 }
1341
1342 /*
1343 * call-seq:
1344 * obj.public_methods(all=true) -> array
1345 *
1346 * Returns the list of public methods accessible to <i>obj</i>. If
1347 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1348 * in the receiver will be listed.
1349 */
1350
1351 VALUE
1352 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1353 {
1354 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1355 }
1356
1357 /*
1358 * call-seq:
1359 * obj.singleton_methods(all=true) -> array
1360 *
1361 * Returns an array of the names of singleton methods for <i>obj</i>.
1362 * If the optional <i>all</i> parameter is true, the list will include
1363 * methods in modules included in <i>obj</i>.
1364 * Only public and protected singleton methods are returned.
1365 *
1366 * module Other
1367 * def three() end
1368 * end
1369 *
1370 * class Single
1371 * def Single.four() end
1372 * end
1373 *
1374 * a = Single.new
1375 *
1376 * def a.one()
1377 * end
1378 *
1379 * class << a
1380 * include Other
1381 * def two()
1382 * end
1383 * end
1384 *
1385 * Single.singleton_methods #=> [:four]
1386 * a.singleton_methods(false) #=> [:two, :one]
1387 * a.singleton_methods #=> [:two, :one, :three]
1388 */
1389
1390 VALUE
1391 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1392 {
1393 VALUE recur, ary, klass, origin;
1394 st_table *list, *mtbl;
1395
1396 if (argc == 0) {
1397 recur = Qtrue;
1398 }
1399 else {
1400 rb_scan_args(argc, argv, "01", &recur);
1401 }
1402 klass = CLASS_OF(obj);
1403 origin = RCLASS_ORIGIN(klass);
1404 list = st_init_numtable();
1405 if (klass && FL_TEST(klass, FL_SINGLETON)) {
1406 if ((mtbl = RCLASS_M_TBL(origin)) != 0)
1407 st_foreach(mtbl, method_entry_i, (st_data_t)list);
1408 klass = RCLASS_SUPER(klass);
1409 }
1410 if (RTEST(recur)) {
1411 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1412 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0)
1413 st_foreach(mtbl, method_entry_i, (st_data_t)list);
1414 klass = RCLASS_SUPER(klass);
1415 }
1416 }
1417 ary = rb_ary_new();
1418 st_foreach(list, ins_methods_i, ary);
1419 st_free_table(list);
1420
1421 return ary;
1422 }
1423
1481 void
1482 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1483 {
1484 rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1485 }
1486
1487 void
1488 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1489 {
1490 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1491 }
1492
1493 void
1494 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1495 {
1496 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1497 }
1498
1499 void
1500 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1501 {
1502 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1503 }
1504
1505 void
1506 rb_undef_method(VALUE klass, const char *name)
1507 {
1508 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
1509 }
1510
1519 #define SPECIAL_SINGLETON(x,c) do {\
1520 if (obj == (x)) {\
1521 return (c);\
1522 }\
1523 } while (0)
1524
1525 static inline VALUE
1526 special_singleton_class_of(VALUE obj)
1527 {
1528 SPECIAL_SINGLETON(Qnil, rb_cNilClass);
1529 SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
1530 SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
1531 return Qnil;
1532 }
1533
1534 VALUE
1535 rb_special_singleton_class(VALUE obj)
1536 {
1537 return special_singleton_class_of(obj);
1538 }
1539
1549 static VALUE
1550 singleton_class_of(VALUE obj)
1551 {
1552 VALUE klass;
1553
1554 if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
1555 rb_raise(rb_eTypeError, "can't define singleton");
1556 }
1557 if (SPECIAL_CONST_P(obj)) {
1558 klass = special_singleton_class_of(obj);
1559 if (NIL_P(klass))
1560 rb_bug("unknown immediate %p", (void *)obj);
1561 return klass;
1562 }
1563 else {
1564 enum ruby_value_type type = BUILTIN_TYPE(obj);
1565 if (type == T_FLOAT || type == T_BIGNUM) {
1566 rb_raise(rb_eTypeError, "can't define singleton");
1567 }
1568 }
1569
1570 if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1571 rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1572 klass = RBASIC(obj)->klass;
1573 }
1574 else {
1575 klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1576 }
1577
1578 if (OBJ_TAINTED(obj)) {
1579 OBJ_TAINT(klass);
1580 }
1581 else {
1582 FL_UNSET(klass, FL_TAINT);
1583 }
1584 if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1585
1586 return klass;
1587 }
1588
1596 VALUE
1597 rb_singleton_class_get(VALUE obj)
1598 {
1599 VALUE klass;
1600
1601 if (SPECIAL_CONST_P(obj)) {
1602 return rb_special_singleton_class(obj);
1603 }
1604 klass = RBASIC(obj)->klass;
1605 if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
1606 if (rb_ivar_get(klass, id_attached) != obj) return Qnil;
1607 return klass;
1608 }
1609
1627 VALUE
1628 rb_singleton_class(VALUE obj)
1629 {
1630 VALUE klass = singleton_class_of(obj);
1631
1632 /* ensures an exposed class belongs to its own eigenclass */
1633 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1634
1635 return klass;
1636 }
1637
1654 void
1655 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1656 {
1657 rb_define_method(singleton_class_of(obj), name, func, argc);
1658 }
1659
1660
1661
1669 void
1670 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1671 {
1672 rb_define_private_method(module, name, func, argc);
1673 rb_define_singleton_method(module, name, func, argc);
1674 }
1675
1676
1683 void
1684 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1685 {
1686 rb_define_module_function(rb_mKernel, name, func, argc);
1687 }
1688
1689
1696 void
1697 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1698 {
1699 rb_alias(klass, rb_intern(name1), rb_intern(name2));
1700 }
1701
1709 void
1710 rb_define_attr(VALUE klass, const char *name, int read, int write)
1711 {
1712 rb_attr(klass, rb_intern(name), read, write, FALSE);
1713 }
1714
1715 int
1716 rb_obj_basic_to_s_p(VALUE obj)
1717 {
1718 const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0);
1719 if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1720 me->def->body.cfunc.func == rb_any_to_s)
1721 return 1;
1722 return 0;
1723 }
1724
1725 #include <stdarg.h>
1726
1727 int
1728 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1729 {
1730 int i;
1731 const char *p = fmt;
1732 VALUE *var;
1733 va_list vargs;
1734 int f_var = 0, f_hash = 0, f_block = 0;
1735 int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1736 int argi = 0;
1737 VALUE hash = Qnil;
1738
1739 if (ISDIGIT(*p)) {
1740 n_lead = *p - '0';
1741 p++;
1742 if (ISDIGIT(*p)) {
1743 n_opt = *p - '0';
1744 p++;
1745 if (ISDIGIT(*p)) {
1746 n_trail = *p - '0';
1747 p++;
1748 goto block_arg;
1749 }
1750 }
1751 }
1752 if (*p == '*') {
1753 f_var = 1;
1754 p++;
1755 if (ISDIGIT(*p)) {
1756 n_trail = *p - '0';
1757 p++;
1758 }
1759 }
1760 block_arg:
1761 if (*p == ':') {
1762 f_hash = 1;
1763 p++;
1764 }
1765 if (*p == '&') {
1766 f_block = 1;
1767 p++;
1768 }
1769 if (*p != '\0') {
1770 rb_fatal("bad scan arg format: %s", fmt);
1771 }
1772 n_mand = n_lead + n_trail;
1773
1774 if (argc < n_mand)
1775 goto argc_error;
1776
1777 va_start(vargs, fmt);
1778
1779 /* capture an option hash - phase 1: pop */
1780 if (f_hash && n_mand < argc) {
1781 VALUE last = argv[argc - 1];
1782
1783 if (NIL_P(last)) {
1784 /* nil is taken as an empty option hash only if it is not
1785 ambiguous; i.e. '*' is not specified and arguments are
1786 given more than sufficient */
1787 if (!f_var && n_mand + n_opt < argc)
1788 argc--;
1789 }
1790 else {
1791 hash = rb_check_hash_type(last);
1792 if (!NIL_P(hash)) {
1793 VALUE opts = rb_extract_keywords(&hash);
1794 if (!hash) argc--;
1795 hash = opts ? opts : Qnil;
1796 }
1797 }
1798 }
1799 /* capture leading mandatory arguments */
1800 for (i = n_lead; i-- > 0; ) {
1801 var = va_arg(vargs, VALUE *);
1802 if (var) *var = argv[argi];
1803 argi++;
1804 }
1805 /* capture optional arguments */
1806 for (i = n_opt; i-- > 0; ) {
1807 var = va_arg(vargs, VALUE *);
1808 if (argi < argc - n_trail) {
1809 if (var) *var = argv[argi];
1810 argi++;
1811 }
1812 else {
1813 if (var) *var = Qnil;
1814 }
1815 }
1816 /* capture variable length arguments */
1817 if (f_var) {
1818 int n_var = argc - argi - n_trail;
1819
1820 var = va_arg(vargs, VALUE *);
1821 if (0 < n_var) {
1822 if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1823 argi += n_var;
1824 }
1825 else {
1826 if (var) *var = rb_ary_new();
1827 }
1828 }
1829 /* capture trailing mandatory arguments */
1830 for (i = n_trail; i-- > 0; ) {
1831 var = va_arg(vargs, VALUE *);
1832 if (var) *var = argv[argi];
1833 argi++;
1834 }
1835 /* capture an option hash - phase 2: assignment */
1836 if (f_hash) {
1837 var = va_arg(vargs, VALUE *);
1838 if (var) *var = hash;
1839 }
1840 /* capture iterator block */
1841 if (f_block) {
1842 var = va_arg(vargs, VALUE *);
1843 if (rb_block_given_p()) {
1844 *var = rb_block_proc();
1845 }
1846 else {
1847 *var = Qnil;
1848 }
1849 }
1850 va_end(vargs);
1851
1852 if (argi < argc) {
1853 argc_error:
1854 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
1855 }
1856
1857 return argc;
1858 }
1859
1860 NORETURN(static void keyword_error(const char *error, VALUE keys));
1861 static void
1862 keyword_error(const char *error, VALUE keys)
1863 {
1864 const char *msg = "";
1865 if (RARRAY_LEN(keys) == 1) {
1866 keys = RARRAY_AREF(keys, 0);
1867 }
1868 else {
1869 keys = rb_ary_join(keys, rb_usascii_str_new2(", "));
1870 msg = "s";
1871 }
1872 rb_raise(rb_eArgError, "%s keyword%s: %"PRIsVALUE, error, msg, keys);
1873 }
1874
1875 NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
1876 static void
1877 unknown_keyword_error(VALUE hash, const ID *table, int keywords)
1878 {
1879 st_table *tbl = rb_hash_tbl_raw(hash);
1880 VALUE keys;
1881 int i;
1882 for (i = 0; i < keywords; i++) {
1883 st_data_t key = ID2SYM(table[i]);
1884 st_delete(tbl, &key, NULL);
1885 }
1886 keys = rb_funcall(hash, rb_intern("keys"), 0, 0);
1887 if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword");
1888 keyword_error("unknown", keys);
1889 }
1890
1891 static int
1892 separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
1893 {
1894 VALUE *kwdhash = (VALUE *)arg;
1895
1896 if (!SYMBOL_P(key)) kwdhash++;
1897 if (!*kwdhash) *kwdhash = rb_hash_new();
1898 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
1899 return ST_CONTINUE;
1900 }
1901
1902 VALUE
1903 rb_extract_keywords(VALUE *orighash)
1904 {
1905 VALUE parthash[2] = {0, 0};
1906 VALUE hash = *orighash;
1907
1908 if (RHASH_EMPTY_P(hash)) {
1909 *orighash = 0;
1910 return hash;
1911 }
1912 st_foreach(rb_hash_tbl_raw(hash), separate_symbol, (st_data_t)&parthash);
1913 *orighash = parthash[1];
1914 return parthash[0];
1915 }
1916
1917 int
1918 rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
1919 {
1920 int i = 0, j;
1921 int rest = 0;
1922 VALUE missing = Qnil;
1923 st_data_t key;
1924
1925 #define extract_kwarg(keyword, val) \
1926 (key = (st_data_t)(keyword), values ? \
1927 st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : \
1928 st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
1929
1930 if (optional < 0) {
1931 rest = 1;
1932 optional = -1-optional;
1933 }
1934 if (values) {
1935 for (j = 0; j < required + optional; j++) {
1936 values[j] = Qundef;
1937 }
1938 }
1939 if (required) {
1940 for (; i < required; i++) {
1941 VALUE keyword = ID2SYM(table[i]);
1942 if (keyword_hash) {
1943 st_data_t val;
1944 if (extract_kwarg(keyword, &val)) {
1945 if (values) values[i] = (VALUE)val;
1946 continue;
1947 }
1948 }
1949 if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
1950 rb_ary_push(missing, keyword);
1951 }
1952 if (!NIL_P(missing)) {
1953 keyword_error("missing", missing);
1954 }
1955 }
1956 j = i;
1957 if (optional && keyword_hash) {
1958 for (i = 0; i < optional; i++) {
1959 st_data_t val;
1960 if (extract_kwarg(ID2SYM(table[required+i]), &val)) {
1961 if (values) values[required+i] = (VALUE)val;
1962 j++;
1963 }
1964 }
1965 }
1966 if (!rest && keyword_hash) {
1967 if (RHASH_SIZE(keyword_hash) > (unsigned int)j) {
1968 unknown_keyword_error(keyword_hash, table, required+optional);
1969 }
1970 }
1971 return j;
1972 #undef extract_kwarg
1973 }
1974
make_metaclass
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition: class.c:475
rb_module_add_to_subclasses_list
static void rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
Definition: class.c:61
rewrite_cref_stack
static void rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass, NODE **new_cref_ptr)
Definition: class.c:236
rb_st_insert_id_and_value
int rb_st_insert_id_and_value(VALUE obj, st_table *tbl, ID key, VALUE value)
Definition: variable.c:2618
rb_class_remove_from_super_subclasses
void rb_class_remove_from_super_subclasses(VALUE klass)
Definition: class.c:80
ISDIGIT
#define ISDIGIT(c)
Definition: ruby.h:1775
UNDEFINED_METHOD_ENTRY_P
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:110
ST_DELETE
Definition: st.h:100
rb_class_detach_subclasses
void rb_class_detach_subclasses(VALUE klass)
Definition: class.c:131
rb_define_module_id_under
VALUE rb_define_module_id_under(VALUE outer, ID id)
Definition: class.c:772
rb_vm_check_redefinition_by_prepend
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1211
rb_iseq_struct::cref_stack
NODE *const cref_stack
Definition: vm_core.h:315
rb_cFalseClass
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ruby.h:1561
rb_mod_include_p
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:1026
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:878
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:327
rb_method_definition_struct::type
rb_method_type_t type
Definition: method.h:79
rb_vm_add_root_module
int rb_vm_add_root_module(ID id, VALUE module)
Definition: vm.c:1806
FALSE
#define FALSE
Definition: nkf.h:174
rb_check_inheritable
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:206
ins_methods_push
static int ins_methods_push(ID name, long type, VALUE ary, long visi)
Definition: class.c:1075
RCLASS_CONST_TBL
#define RCLASS_CONST_TBL(c)
Definition: internal.h:293
rb_const_entry_struct
Definition: constant.h:19
st_table
Definition: st.h:69
rb_obj_private_methods
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1337
rb_id2str
VALUE rb_id2str(ID id)
Definition: ripper.c:17160
VISI
#define VISI(x)
Definition: class.c:1071
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1214
rb_subclass_entry::next
rb_subclass_entry_t * next
Definition: internal.h:250
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1655
rb_class_foreach_subclass
void rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE))
Definition: class.c:117
ruby.h
rb_method_entry_struct::flag
rb_method_flag_t flag
Definition: method.h:98
rb_usascii_str_new2
#define rb_usascii_str_new2
Definition: intern.h:846
rb_class_private_instance_methods
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1243
FL_TAINT
#define FL_TAINT
Definition: ruby.h:1137
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:440
method.h
T_MODULE
#define T_MODULE
Definition: ruby.h:480
rb_define_class_id_under
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:699
rb_const_entry_struct::file
const VALUE file
Definition: constant.h:22
RCLASS_EXT
#define RCLASS_EXT(c)
Definition: classext.h:15
rb_class_remove_from_module_subclasses
void rb_class_remove_from_module_subclasses(VALUE klass)
Definition: class.c:98
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1918
Qtrue
#define Qtrue
Definition: ruby.h:426
st_insert
int st_insert(st_table *, st_data_t, st_data_t)
class_alloc
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition: class.c:155
OBJ_INIT_COPY
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:287
rb_method_definition_struct::orig_me
struct rb_method_entry_struct * orig_me
Definition: method.h:92
ST_CONTINUE
Definition: st.h:100
rb_mod_ancestors
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:1056
id
const int id
Definition: nkf.c:209
rb_refinement_module_get_refined_class
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1177
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1500
VM_METHOD_TYPE_ISEQ
Definition: method.h:48
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:548
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
rb_singleton_class_clone
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:371
st_free_table
void st_free_table(st_table *)
Definition: st.c:334
constant.h
rb_hash_tbl_raw
struct st_table * rb_hash_tbl_raw(VALUE hash)
Definition: hash.c:351
rb_mod_init_copy
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:319
rb_ary_tmp_new
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:534
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:775
Init_class_hierarchy
void Init_class_hierarchy(void)
Definition: class.c:535
RBASIC_SET_CLASS
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:609
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:676
Check_Type
#define Check_Type(v, t)
Definition: ruby.h:532
ins_methods_priv_i
static int ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1108
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1115
SET_METACLASS_OF
#define SET_METACLASS_OF(k, cls)
Definition: class.c:436
clone_const_i
static int clone_const_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:298
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: ruby.h:523
rb_clear_constant_cache
void rb_clear_constant_cache(void)
Definition: vm_method.c:60
rb_const_defined
int rb_const_defined(VALUE, ID)
Definition: variable.c:2124
RCLASS_M_TBL_WRAPPER
#define RCLASS_M_TBL_WRAPPER(c)
Definition: internal.h:294
rb_const_entry_struct::value
const VALUE value
Definition: constant.h:21
rb_method_definition_struct::body
union rb_method_definition_struct::@126 body
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:827
T_ARRAY
#define T_ARRAY
Definition: ruby.h:484
rb_define_protected_method
void rb_define_protected_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1494
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1684
last
unsigned int last
Definition: nkf.c:4310
RMODULE_M_TBL
#define RMODULE_M_TBL(m)
Definition: ruby.h:799
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:347
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1506
OBJ_TAINTED
#define OBJ_TAINTED(x)
Definition: ruby.h:1176
VM_METHOD_TYPE_REFINED
Definition: method.h:58
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
head
#define head
Definition: st.c:107
st_data_t
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:20
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:967
RNode
Definition: node.h:239
NEWOBJ_OF
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:694
rb_free_m_tbl_wrapper
void rb_free_m_tbl_wrapper(struct method_table_wrapper *wrapper)
Definition: gc.c:1450
FL_SINGLETON
#define FL_SINGLETON
Definition: ruby.h:1133
rb_prepend_module
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:940
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1628
rb_extract_keywords
VALUE rb_extract_keywords(VALUE *orighash)
Definition: class.c:1903
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
st_lookup
int st_lookup(st_table *, st_data_t, st_data_t *)
rb_method_definition_struct::cfunc
rb_method_cfunc_t cfunc
Definition: method.h:83
FL_TEST
#define FL_TEST(x, f)
Definition: ruby.h:1169
rb_class_inherited
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:604
NORETURN
NORETURN(static void keyword_error(const char *error, VALUE keys))
rb_subclass_entry
Definition: internal.h:248
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:712
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
VM_METHOD_TYPE_UNDEF
Definition: method.h:54
val
#define val
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
rb_special_singleton_class
VALUE rb_special_singleton_class(VALUE obj)
Definition: class.c:1535
RMODULE_IS_REFINEMENT
#define RMODULE_IS_REFINEMENT
Definition: ruby.h:802
RGENGC_WB_PROTECTED_CLASS
#define RGENGC_WB_PROTECTED_CLASS
Definition: ruby.h:729
rb_attr
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:860
boot_defclass
static VALUE boot_defclass(const char *name, VALUE super)
Definition: class.c:524
rb_class_real
VALUE rb_class_real(VALUE)
Definition: object.c:204
rb_cBasicObject
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1552
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:495
clone_method
static void clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
Definition: class.c:255
RMODULE_CONST_TBL
#define RMODULE_CONST_TBL(m)
Definition: ruby.h:798
OBJ_WB_UNPROTECT
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1191
rb_mKernel
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1541
RCLASS_ORIGIN
#define RCLASS_ORIGIN(c)
Definition: internal.h:297
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
RCLASS_SET_SUPER
static VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super)
Definition: internal.h:319
st_add_direct
void st_add_direct(st_table *, st_data_t, st_data_t)
Definition: st.c:629
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:630
msg
static char msg[50]
Definition: strerror.c:8
rb_cTrueClass
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ruby.h:1588
special_singleton_class_of
static VALUE special_singleton_class_of(VALUE obj)
Definition: class.c:1526
st_delete
int st_delete(st_table *, st_data_t *, st_data_t *)
RCLASS_IV_TBL
#define RCLASS_IV_TBL(c)
Definition: internal.h:292
rb_class_subclass_add
void rb_class_subclass_add(VALUE super, VALUE klass)
Definition: class.c:40
rb_define_module_id
VALUE rb_define_module_id(ID id)
Definition: class.c:735
METACLASS_OF
#define METACLASS_OF(k)
Definition: class.c:435
OBJ_FROZEN
#define OBJ_FROZEN(x)
Definition: ruby.h:1185
FLONUM_P
#define FLONUM_P(x)
Definition: ruby.h:367
META_CLASS_OF_CLASS_CLASS_P
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition: class.c:443
T_FLOAT
#define T_FLOAT
Definition: ruby.h:481
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_method_entry_struct
Definition: method.h:97
RCLASS_M_TBL_INIT
static void RCLASS_M_TBL_INIT(VALUE c)
Definition: internal.h:302
rb_cModule
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1572
T_BIGNUM
#define T_BIGNUM
Definition: ruby.h:487
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:4920
MEMCPY
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1352
rb_obj_protected_methods
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1322
rb_ary_new4
#define rb_ary_new4
Definition: intern.h:92
OBJ_FREEZE
#define OBJ_FREEZE(x)
Definition: ruby.h:1186
rb_resolve_refined_method
rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
Definition: vm_method.c:655
rb_method_entry
rb_method_entry_t * rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr)
Definition: vm_method.c:617
rb_method_cfunc_struct::func
VALUE(* func)(ANYARGS)
Definition: method.h:66
rb_method_entry_struct::klass
VALUE klass
Definition: method.h:102
ALLOC
#define ALLOC(type)
Definition: ruby.h:1334
rb_obj_basic_to_s_p
int rb_obj_basic_to_s_p(VALUE obj)
Definition: class.c:1716
RCLASS_REFINED_CLASS
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:298
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1880
id_attached
#define id_attached
Definition: class.c:37
rb_iseq_clone
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase)
Definition: iseq.c:1922
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1697
rb_define_module_function
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1670
RCLASS_M_TBL
#define RCLASS_M_TBL(c)
Definition: internal.h:295
add_refined_method_entry_i
static int add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:845
rb_method_definition_struct::iseq
rb_iseq_t *const iseq
Definition: method.h:82
func
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:113
class_instance_method_list
static VALUE class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int(*func)(st_data_t, st_data_t, st_data_t))
Definition: class.c:1143
TRUE
#define TRUE
Definition: nkf.h:175
rb_include_class_new
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:792
rb_fatal
void rb_fatal(const char *fmt,...)
Definition: error.c:1911
rb_subclass_entry::klass
VALUE klass
Definition: internal.h:249
rb_add_method
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex)
Definition: vm_method.c:428
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:298
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
rb_check_hash_type
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:588
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:137
internal.h
ID
unsigned long ID
Definition: ruby.h:89
separate_symbol
static int separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
Definition: class.c:1892
RHASH_SIZE
#define RHASH_SIZE(h)
Definition: ruby.h:930
ins_methods_i
static int ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1096
Qnil
#define Qnil
Definition: ruby.h:427
rb_clear_method_cache_by_class
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:66
rb_const_set
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2160
extract_kwarg
#define extract_kwarg(keyword, val)
type
int type
Definition: tcltklib.c:112
include_modules_at
static int include_modules_at(const VALUE klass, VALUE c, VALUE module)
Definition: class.c:852
vm_core.h
rb_obj_public_methods
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1352
ins_methods_pub_i
static int ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1114
BUILTIN_TYPE
#define BUILTIN_TYPE(x)
Definition: ruby.h:502
OBJ_TAINT
#define OBJ_TAINT(x)
Definition: ruby.h:1177
VALUE
unsigned long VALUE
Definition: ruby.h:88
rb_name_class
void rb_name_class(VALUE, ID)
Definition: variable.c:385
RBASIC
#define RBASIC(obj)
Definition: ruby.h:1116
rb_class2name
const char * rb_class2name(VALUE)
Definition: variable.c:397
NOEX_PROTECTED
Definition: method.h:28
rb_make_metaclass
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:561
rb_alias
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1244
clone_const
static int clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
Definition: class.c:286
clone_const_arg::tbl
st_table * tbl
Definition: class.c:282
rb_cClass
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1557
st_init_numtable
st_table * st_init_numtable(void)
Definition: st.c:272
rb_class_detach_module_subclasses
void rb_class_detach_module_subclasses(VALUE klass)
Definition: class.c:137
ruby_value_type
ruby_value_type
Definition: ruby.h:442
rb_class_instance_methods
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1205
FL_UNSET
#define FL_UNSET(x, f)
Definition: ruby.h:1173
rb_free_const_table
void rb_free_const_table(st_table *tbl)
Definition: gc.c:1466
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:766
recur
#define recur(fmt)
Definition: date_strptime.c:147
rb_class_protected_instance_methods
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1220
rb_const_defined_at
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2130
rb_define_method_id
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1482
rb_class_public_instance_methods
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1258
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:423
f
#define f
unknown_keyword_error
static void unknown_keyword_error(VALUE hash, const ID *table, int keywords)
Definition: class.c:1877
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
RCLASS_SUPER
#define RCLASS_SUPER(c)
Definition: classext.h:16
rb_module_new
VALUE rb_module_new(void)
Definition: class.c:727
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:901
ENSURE_EIGENCLASS
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:461
rb_block_proc
VALUE rb_block_proc(void)
Definition: proc.c:620
xmalloc
#define xmalloc
Definition: defines.h:108
rb_obj_singleton_methods
VALUE rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1391
rb_method_entry_struct::def
rb_method_definition_t * def
Definition: method.h:100
rb_define_attr
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines (a) public accessor method(s) for an attribute.
Definition: class.c:1710
ANYARGS
#define ANYARGS
Definition: defines.h:98
rb_error_arity
void rb_error_arity(int argc, int min, int max)
Definition: vm_insnhelper.c:149
RCLASS_IV_INDEX_TBL
#define RCLASS_IV_INDEX_TBL(c)
Definition: internal.h:296
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Definition: ruby.h:1134
NEW_CREF
#define NEW_CREF(a)
Definition: node.h:452
key
uint8_t key[16]
Definition: random.c:1250
make_singleton_class
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition: class.c:509
rb_singleton_class_get
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition: class.c:1597
rb_any_to_s
VALUE rb_any_to_s(VALUE)
Definition: object.c:452
RTEST
#define RTEST(v)
Definition: ruby.h:437
method_entry_i
static int method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:1120
rb_st_copy
st_table * rb_st_copy(VALUE obj, struct st_table *orig_tbl)
Definition: variable.c:2633
OBJ_INFECT
#define OBJ_INFECT(x, s)
Definition: ruby.h:1180
list
struct rb_encoding_entry * list
Definition: encoding.c:47
rb_obj_methods
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1294
rb_add_refined_method_entry
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:221
clone_const_arg::klass
VALUE klass
Definition: class.c:281
ins_methods_prot_i
static int ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1102
st.h
hash
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
rb_ary_join
VALUE rb_ary_join(VALUE ary, VALUE sep)
Definition: array.c:1995
T_CLASS
#define T_CLASS
Definition: ruby.h:478
move_refined_method
static int move_refined_method(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:913
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:1886
rb_frozen_class_p
void rb_frozen_class_p(VALUE klass)
Definition: eval.c:406
name
const char * name
Definition: nkf.c:208
class_init_copy_check
static void class_init_copy_check(VALUE clone, VALUE orig)
Definition: class.c:304
FL_SET
#define FL_SET(x, f)
Definition: ruby.h:1172
rb_iseq_struct::self
VALUE self
Definition: vm_core.h:303
rb_iseq_struct
Definition: vm_core.h:206
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:355
GetISeqPtr
#define GetISeqPtr(obj, ptr)
Definition: vm_core.h:193
RMODULE_INCLUDED_INTO_REFINEMENT
#define RMODULE_INCLUDED_INTO_REFINEMENT
Definition: ruby.h:803
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17230
rb_mod_included_modules
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:990
rb_define_class_id
VALUE rb_define_class_id(ID id, VALUE super)
Defines a new class.
Definition: class.c:583
rb_classext_struct
Definition: internal.h:264
rb_next_class_serial
rb_serial_t rb_next_class_serial(void)
Definition: vm.c:92
VM_METHOD_TYPE_CFUNC
Definition: method.h:49
SPECIAL_SINGLETON
#define SPECIAL_SINGLETON(x, c)
Definition: class.c:1519
rb_method_entry_set
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_flag_t noex)
Definition: vm_method.c:504
CONST_ID
#define CONST_ID(var, str)
Definition: ruby.h:1428
NOEX_PUBLIC
Definition: method.h:25
SPECIAL_CONST_P
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1165
xfree
void void xfree(void *)
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Definition: ruby.h:931
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:746
clone_const_arg
Definition: class.c:280
rb_intern
#define rb_intern(str)
SYMBOL_P
#define SYMBOL_P(x)
Definition: ruby.h:354
mod
#define mod(x, y)
Definition: date_strftime.c:28
keyword_error
static void keyword_error(const char *error, VALUE keys)
Definition: class.c:1862
NULL
#define NULL
Definition: _sdbm.c:103
singleton_class_of
static VALUE singleton_class_of(VALUE obj)
Definition: class.c:1550
RClass
Definition: ruby.h:790
clone_method_i
static int clone_method_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:274
Qundef
#define Qundef
Definition: ruby.h:428
T_ICLASS
#define T_ICLASS
Definition: ruby.h:479
RCLASS_SERIAL
#define RCLASS_SERIAL(c)
Definition: internal.h:299
rb_add_method_cfunc
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_flag_t noex)
Definition: vm_method.c:99
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new class.
Definition: class.c:228
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
st_foreach
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
Definition: st.c:1034
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:223
NOEX_UNDEF
Definition: method.h:31
rb_class_boot
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:187
rb_cNilClass
RUBY_EXTERN VALUE rb_cNilClass
Definition: ruby.h:1574
rb_eArgError
VALUE rb_eArgError
Definition: error.c:549
NOEX_PRIVATE
Definition: method.h:27
T_MASK
#define T_MASK
Definition: md5.c:131
rb_set_class_path_string
void rb_set_class_path_string(VALUE, VALUE, VALUE)
Definition: variable.c:293
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1213
argv
char ** argv
Definition: ruby.c:132
rb_singleton_class_clone_and_attach
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:377