Ruby 2.0.0p247(2013-06-27revision41674)

- 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 extern st_table *rb_class_tbl;
35 static ID id_attached;
36
49 static VALUE
50 class_alloc(VALUE flags, VALUE klass)
51 {
52 NEWOBJ_OF(obj, struct RClass, klass, flags);
53 obj->ptr = ALLOC(rb_classext_t);
54 RCLASS_IV_TBL(obj) = 0;
55 RCLASS_CONST_TBL(obj) = 0;
56 RCLASS_M_TBL(obj) = 0;
57 RCLASS_SUPER(obj) = 0;
58 RCLASS_ORIGIN(obj) = (VALUE)obj;
59 RCLASS_IV_INDEX_TBL(obj) = 0;
60 RCLASS_REFINED_CLASS(obj) = Qnil;
61 RCLASS_EXT(obj)->allocator = 0;
62 return (VALUE)obj;
63 }
64
65
75 VALUE
76 rb_class_boot(VALUE super)
77 {
78 VALUE klass = class_alloc(T_CLASS, rb_cClass);
79
80 RCLASS_SUPER(klass) = super;
81 RCLASS_M_TBL(klass) = st_init_numtable();
82
83 OBJ_INFECT(klass, super);
84 return (VALUE)klass;
85 }
86
87
94 void
95 rb_check_inheritable(VALUE super)
96 {
97 if (!RB_TYPE_P(super, T_CLASS)) {
98 rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
99 rb_obj_classname(super));
100 }
101 if (RBASIC(super)->flags & FL_SINGLETON) {
102 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
103 }
104 if (super == rb_cClass) {
105 rb_raise(rb_eTypeError, "can't make subclass of Class");
106 }
107 }
108
109
116 VALUE
117 rb_class_new(VALUE super)
118 {
119 Check_Type(super, T_CLASS);
120 rb_check_inheritable(super);
121 return rb_class_boot(super);
122 }
123
124 static NODE*
125 rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass)
126 {
127 NODE *new_node;
128 if (!node) {
129 return NULL;
130 }
131 if (node->nd_clss == old_klass) {
132 new_node = NEW_CREF(new_klass);
133 new_node->nd_next = node->nd_next;
134 } else {
135 new_node = NEW_CREF(node->nd_clss);
136 new_node->nd_next = rewrite_cref_stack(node->nd_next, old_klass, new_klass);
137 }
138 return new_node;
139 }
140
141 static void
142 clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
143 {
144 VALUE newiseqval;
145 if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
146 rb_iseq_t *iseq;
147 newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
148 GetISeqPtr(newiseqval, iseq);
149 iseq->cref_stack = rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass);
150 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
151 RB_GC_GUARD(newiseqval);
152 }
153 else {
154 rb_method_entry_set(klass, mid, me, me->flag);
155 }
156 }
157
158 static int
159 clone_method_i(st_data_t key, st_data_t value, st_data_t data)
160 {
161 clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
162 return ST_CONTINUE;
163 }
164
165 static int
166 clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
167 {
168 rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
169 *nce = *ce;
170 st_insert(tbl, key, (st_data_t)nce);
171 return ST_CONTINUE;
172 }
173
174 static int
175 clone_const_i(st_data_t key, st_data_t value, st_data_t data)
176 {
177 return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
178 }
179
180 static void
181 class_init_copy_check(VALUE clone, VALUE orig)
182 {
183 if (orig == rb_cBasicObject) {
184 rb_raise(rb_eTypeError, "can't copy the root class");
185 }
186 if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
187 rb_raise(rb_eTypeError, "already initialized class");
188 }
189 if (FL_TEST(orig, FL_SINGLETON)) {
190 rb_raise(rb_eTypeError, "can't copy singleton class");
191 }
192 }
193
194 /* :nodoc: */
195 VALUE
196 rb_mod_init_copy(VALUE clone, VALUE orig)
197 {
198 if (RB_TYPE_P(clone, T_CLASS)) {
199 class_init_copy_check(clone, orig);
200 }
201 rb_obj_init_copy(clone, orig);
202 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
203 RBASIC(clone)->klass = rb_singleton_class_clone(orig);
204 rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
205 }
206 RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
207 RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
208 if (RCLASS_IV_TBL(orig)) {
209 st_data_t id;
210
211 if (RCLASS_IV_TBL(clone)) {
212 st_free_table(RCLASS_IV_TBL(clone));
213 }
214 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
215 CONST_ID(id, "__tmp_classpath__");
216 st_delete(RCLASS_IV_TBL(clone), &id, 0);
217 CONST_ID(id, "__classpath__");
218 st_delete(RCLASS_IV_TBL(clone), &id, 0);
219 CONST_ID(id, "__classid__");
220 st_delete(RCLASS_IV_TBL(clone), &id, 0);
221 }
222 if (RCLASS_CONST_TBL(orig)) {
223 if (RCLASS_CONST_TBL(clone)) {
224 rb_free_const_table(RCLASS_CONST_TBL(clone));
225 }
226 RCLASS_CONST_TBL(clone) = st_init_numtable();
227 st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
228 }
229 if (RCLASS_M_TBL(orig)) {
230 if (RCLASS_M_TBL(clone)) {
231 rb_free_m_table(RCLASS_M_TBL(clone));
232 }
233 RCLASS_M_TBL(clone) = st_init_numtable();
234 st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
235 }
236
237 return clone;
238 }
239
240 VALUE
241 rb_singleton_class_clone(VALUE obj)
242 {
243 return rb_singleton_class_clone_and_attach(obj, Qundef);
244 }
245
246 VALUE
247 rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
248 {
249 VALUE klass = RBASIC(obj)->klass;
250
251 if (!FL_TEST(klass, FL_SINGLETON))
252 return klass;
253 else {
254 /* copy singleton(unnamed) class */
255 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
256
257 if (BUILTIN_TYPE(obj) == T_CLASS) {
258 RBASIC(clone)->klass = clone;
259 }
260 else {
261 RBASIC(clone)->klass = rb_singleton_class_clone(klass);
262 }
263
264 RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
265 RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
266 if (RCLASS_IV_TBL(klass)) {
267 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
268 }
269 if (RCLASS_CONST_TBL(klass)) {
270 RCLASS_CONST_TBL(clone) = st_init_numtable();
271 st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
272 }
273 if (attach != Qundef) {
274 rb_singleton_class_attached(clone, attach);
275 }
276 RCLASS_M_TBL(clone) = st_init_numtable();
277 st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
278 rb_singleton_class_attached(RBASIC(clone)->klass, clone);
279 FL_SET(clone, FL_SINGLETON);
280 return clone;
281 }
282 }
283
288 void
289 rb_singleton_class_attached(VALUE klass, VALUE obj)
290 {
291 if (FL_TEST(klass, FL_SINGLETON)) {
292 if (!RCLASS_IV_TBL(klass)) {
293 RCLASS_IV_TBL(klass) = st_init_numtable();
294 }
295 st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
296 }
297 }
298
299
300
301 #define METACLASS_OF(k) RBASIC(k)->klass
302
308 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
309
315 #define HAVE_METACLASS_P(k) \
316 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
317 rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
318
326 #define ENSURE_EIGENCLASS(klass) \
327 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
328
329
339 static inline VALUE
340 make_metaclass(VALUE klass)
341 {
342 VALUE super;
343 VALUE metaclass = rb_class_boot(Qundef);
344
345 FL_SET(metaclass, FL_SINGLETON);
346 rb_singleton_class_attached(metaclass, klass);
347
348 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
349 METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
350 }
351 else {
352 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
353 METACLASS_OF(klass) = metaclass;
354 METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
355 }
356
357 super = RCLASS_SUPER(klass);
358 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
359 RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
360
361 OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
362
363 return metaclass;
364 }
365
372 static inline VALUE
373 make_singleton_class(VALUE obj)
374 {
375 VALUE orig_class = RBASIC(obj)->klass;
376 VALUE klass = rb_class_boot(orig_class);
377
378 FL_SET(klass, FL_SINGLETON);
379 RBASIC(obj)->klass = klass;
380 rb_singleton_class_attached(klass, obj);
381
382 METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
383 return klass;
384 }
385
386
387 static VALUE
388 boot_defclass(const char *name, VALUE super)
389 {
390 extern st_table *rb_class_tbl;
391 VALUE obj = rb_class_boot(super);
392 ID id = rb_intern(name);
393
394 rb_name_class(obj, id);
395 st_add_direct(rb_class_tbl, id, obj);
396 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
397 return obj;
398 }
399
400 void
401 Init_class_hierarchy(void)
402 {
403 id_attached = rb_intern("__attached__");
404
405 rb_cBasicObject = boot_defclass("BasicObject", 0);
406 rb_cObject = boot_defclass("Object", rb_cBasicObject);
407 rb_cModule = boot_defclass("Module", rb_cObject);
408 rb_cClass = boot_defclass("Class", rb_cModule);
409
410 rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
411 RBASIC(rb_cClass)->klass
412 = RBASIC(rb_cModule)->klass
413 = RBASIC(rb_cObject)->klass
414 = RBASIC(rb_cBasicObject)->klass
415 = rb_cClass;
416 }
417
418
429 VALUE
430 rb_make_metaclass(VALUE obj, VALUE unused)
431 {
432 if (BUILTIN_TYPE(obj) == T_CLASS) {
433 return make_metaclass(obj);
434 }
435 else {
436 return make_singleton_class(obj);
437 }
438 }
439
440
451 VALUE
452 rb_define_class_id(ID id, VALUE super)
453 {
454 VALUE klass;
455
456 if (!super) super = rb_cObject;
457 klass = rb_class_new(super);
458 rb_make_metaclass(klass, RBASIC(super)->klass);
459
460 return klass;
461 }
462
463
472 VALUE
473 rb_class_inherited(VALUE super, VALUE klass)
474 {
475 ID inherited;
476 if (!super) super = rb_cObject;
477 CONST_ID(inherited, "inherited");
478 return rb_funcall(super, inherited, 1, klass);
479 }
480
481
482
498 VALUE
499 rb_define_class(const char *name, VALUE super)
500 {
501 VALUE klass;
502 ID id;
503
504 id = rb_intern(name);
505 if (rb_const_defined(rb_cObject, id)) {
506 klass = rb_const_get(rb_cObject, id);
507 if (!RB_TYPE_P(klass, T_CLASS)) {
508 rb_raise(rb_eTypeError, "%s is not a class", name);
509 }
510 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
511 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
512 }
513 return klass;
514 }
515 if (!super) {
516 rb_warn("no super class for `%s', Object assumed", name);
517 }
518 klass = rb_define_class_id(id, super);
519 st_add_direct(rb_class_tbl, id, klass);
520 rb_name_class(klass, id);
521 rb_const_set(rb_cObject, id, klass);
522 rb_class_inherited(super, klass);
523
524 return klass;
525 }
526
527
544 VALUE
545 rb_define_class_under(VALUE outer, const char *name, VALUE super)
546 {
547 return rb_define_class_id_under(outer, rb_intern(name), super);
548 }
549
550
567 VALUE
568 rb_define_class_id_under(VALUE outer, ID id, VALUE super)
569 {
570 VALUE klass;
571
572 if (rb_const_defined_at(outer, id)) {
573 klass = rb_const_get_at(outer, id);
574 if (!RB_TYPE_P(klass, T_CLASS)) {
575 rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
576 }
577 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
578 rb_name_error(id, "%s is already defined", rb_id2name(id));
579 }
580 return klass;
581 }
582 if (!super) {
583 rb_warn("no super class for `%s::%s', Object assumed",
584 rb_class2name(outer), rb_id2name(id));
585 }
586 klass = rb_define_class_id(id, super);
587 rb_set_class_path_string(klass, outer, rb_id2str(id));
588 rb_const_set(outer, id, klass);
589 rb_class_inherited(super, klass);
590 rb_gc_register_mark_object(klass);
591
592 return klass;
593 }
594
595 VALUE
596 rb_module_new(void)
597 {
598 VALUE mdl = class_alloc(T_MODULE, rb_cModule);
599
600 RCLASS_M_TBL(mdl) = st_init_numtable();
601
602 return (VALUE)mdl;
603 }
604
605 VALUE
606 rb_define_module_id(ID id)
607 {
608 VALUE mdl;
609
610 mdl = rb_module_new();
611 rb_name_class(mdl, id);
612
613 return mdl;
614 }
615
616 VALUE
617 rb_define_module(const char *name)
618 {
619 VALUE module;
620 ID id;
621
622 id = rb_intern(name);
623 if (rb_const_defined(rb_cObject, id)) {
624 module = rb_const_get(rb_cObject, id);
625 if (RB_TYPE_P(module, T_MODULE))
626 return module;
627 rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
628 }
629 module = rb_define_module_id(id);
630 st_add_direct(rb_class_tbl, id, module);
631 rb_const_set(rb_cObject, id, module);
632
633 return module;
634 }
635
636 VALUE
637 rb_define_module_under(VALUE outer, const char *name)
638 {
639 return rb_define_module_id_under(outer, rb_intern(name));
640 }
641
642 VALUE
643 rb_define_module_id_under(VALUE outer, ID id)
644 {
645 VALUE module;
646
647 if (rb_const_defined_at(outer, id)) {
648 module = rb_const_get_at(outer, id);
649 if (RB_TYPE_P(module, T_MODULE))
650 return module;
651 rb_raise(rb_eTypeError, "%s::%s is not a module",
652 rb_class2name(outer), rb_obj_classname(module));
653 }
654 module = rb_define_module_id(id);
655 rb_const_set(outer, id, module);
656 rb_set_class_path_string(module, outer, rb_id2str(id));
657 rb_gc_register_mark_object(module);
658
659 return module;
660 }
661
662 VALUE
663 rb_include_class_new(VALUE module, VALUE super)
664 {
665 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
666
667 if (BUILTIN_TYPE(module) == T_ICLASS) {
668 module = RBASIC(module)->klass;
669 }
670 if (!RCLASS_IV_TBL(module)) {
671 RCLASS_IV_TBL(module) = st_init_numtable();
672 }
673 if (!RCLASS_CONST_TBL(module)) {
674 RCLASS_CONST_TBL(module) = st_init_numtable();
675 }
676 RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
677 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
678 RCLASS_M_TBL(klass) = RCLASS_M_TBL(RCLASS_ORIGIN(module));
679 RCLASS_SUPER(klass) = super;
680 if (RB_TYPE_P(module, T_ICLASS)) {
681 RBASIC(klass)->klass = RBASIC(module)->klass;
682 }
683 else {
684 RBASIC(klass)->klass = module;
685 }
686 OBJ_INFECT(klass, module);
687 OBJ_INFECT(klass, super);
688
689 return (VALUE)klass;
690 }
691
692 static int include_modules_at(const VALUE klass, VALUE c, VALUE module);
693
694 void
695 rb_include_module(VALUE klass, VALUE module)
696 {
697 int changed = 0;
698
699 rb_frozen_class_p(klass);
700 if (!OBJ_UNTRUSTED(klass)) {
701 rb_secure(4);
702 }
703
704 if (!RB_TYPE_P(module, T_MODULE)) {
705 Check_Type(module, T_MODULE);
706 }
707
708 OBJ_INFECT(klass, module);
709
710 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module);
711 if (changed < 0)
712 rb_raise(rb_eArgError, "cyclic include detected");
713 if (changed) rb_clear_cache();
714 }
715
716 static int
717 add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
718 {
719 rb_add_refined_method_entry((VALUE) data, (ID) key);
720 return ST_CONTINUE;
721 }
722
723 static int
724 include_modules_at(const VALUE klass, VALUE c, VALUE module)
725 {
726 VALUE p;
727 int changed = 0;
728 const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
729
730 while (module) {
731 int superclass_seen = FALSE;
732
733 if (RCLASS_ORIGIN(module) != module)
734 goto skip;
735 if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
736 return -1;
737 /* ignore if the module included already in superclasses */
738 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
739 switch (BUILTIN_TYPE(p)) {
740 case T_ICLASS:
741 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
742 if (!superclass_seen) {
743 c = p; /* move insertion point */
744 }
745 goto skip;
746 }
747 break;
748 case T_CLASS:
749 superclass_seen = TRUE;
750 break;
751 }
752 }
753 c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c));
754 if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
755 VALUE refined_class =
756 rb_refinement_module_get_refined_class(klass);
757
758 st_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i,
759 (st_data_t) refined_class);
760 FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT);
761 }
762 if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
763 changed = 1;
764 if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries)
765 changed = 1;
766 skip:
767 module = RCLASS_SUPER(module);
768 }
769
770 return changed;
771 }
772
773 static int
774 move_refined_method(st_data_t key, st_data_t value, st_data_t data)
775 {
776 rb_method_entry_t *me = (rb_method_entry_t *) value;
777 st_table *tbl = (st_table *) data;
778
779 if (me->def->type == VM_METHOD_TYPE_REFINED) {
780 if (me->def->body.orig_me) {
781 rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me;
782 me->def->body.orig_me = NULL;
783 new_me = ALLOC(rb_method_entry_t);
784 *new_me = *me;
785 st_add_direct(tbl, key, (st_data_t) new_me);
786 *me = *orig_me;
787 xfree(orig_me);
788 return ST_CONTINUE;
789 }
790 else {
791 st_add_direct(tbl, key, (st_data_t) me);
792 return ST_DELETE;
793 }
794 }
795 else {
796 return ST_CONTINUE;
797 }
798 }
799
800 void
801 rb_prepend_module(VALUE klass, VALUE module)
802 {
803 void rb_vm_check_redefinition_by_prepend(VALUE klass);
804 VALUE origin;
805 int changed = 0;
806
807 rb_frozen_class_p(klass);
808 if (!OBJ_UNTRUSTED(klass)) {
809 rb_secure(4);
810 }
811
812 Check_Type(module, T_MODULE);
813
814 OBJ_INFECT(klass, module);
815
816 origin = RCLASS_ORIGIN(klass);
817 if (origin == klass) {
818 origin = class_alloc(T_ICLASS, klass);
819 RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
820 RCLASS_SUPER(klass) = origin;
821 RCLASS_ORIGIN(klass) = origin;
822 RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
823 RCLASS_M_TBL(klass) = st_init_numtable();
824 st_foreach(RCLASS_M_TBL(origin), move_refined_method,
825 (st_data_t) RCLASS_M_TBL(klass));
826 }
827 changed = include_modules_at(klass, klass, module);
828 if (changed < 0)
829 rb_raise(rb_eArgError, "cyclic prepend detected");
830 if (changed) {
831 rb_clear_cache();
832 rb_vm_check_redefinition_by_prepend(klass);
833 }
834 }
835
836 /*
837 * call-seq:
838 * mod.included_modules -> array
839 *
840 * Returns the list of modules included in <i>mod</i>.
841 *
842 * module Mixin
843 * end
844 *
845 * module Outer
846 * include Mixin
847 * end
848 *
849 * Mixin.included_modules #=> []
850 * Outer.included_modules #=> [Mixin]
851 */
852
853 VALUE
854 rb_mod_included_modules(VALUE mod)
855 {
856 VALUE ary = rb_ary_new();
857 VALUE p;
858 VALUE origin = RCLASS_ORIGIN(mod);
859
860 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
861 if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
862 VALUE m = RBASIC(p)->klass;
863 if (RB_TYPE_P(m, T_MODULE))
864 rb_ary_push(ary, m);
865 }
866 }
867 return ary;
868 }
869
870 /*
871 * call-seq:
872 * mod.include?(module) -> true or false
873 *
874 * Returns <code>true</code> if <i>module</i> is included in
875 * <i>mod</i> or one of <i>mod</i>'s ancestors.
876 *
877 * module A
878 * end
879 * class B
880 * include A
881 * end
882 * class C < B
883 * end
884 * B.include?(A) #=> true
885 * C.include?(A) #=> true
886 * A.include?(A) #=> false
887 */
888
889 VALUE
890 rb_mod_include_p(VALUE mod, VALUE mod2)
891 {
892 VALUE p;
893
894 Check_Type(mod2, T_MODULE);
895 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
896 if (BUILTIN_TYPE(p) == T_ICLASS) {
897 if (RBASIC(p)->klass == mod2) return Qtrue;
898 }
899 }
900 return Qfalse;
901 }
902
903 /*
904 * call-seq:
905 * mod.ancestors -> array
906 *
907 * Returns a list of modules included in <i>mod</i> (including
908 * <i>mod</i> itself).
909 *
910 * module Mod
911 * include Math
912 * include Comparable
913 * end
914 *
915 * Mod.ancestors #=> [Mod, Comparable, Math]
916 * Math.ancestors #=> [Math]
917 */
918
919 VALUE
920 rb_mod_ancestors(VALUE mod)
921 {
922 VALUE p, ary = rb_ary_new();
923
924 for (p = mod; p; p = RCLASS_SUPER(p)) {
925 if (FL_TEST(p, FL_SINGLETON))
926 continue;
927 if (BUILTIN_TYPE(p) == T_ICLASS) {
928 rb_ary_push(ary, RBASIC(p)->klass);
929 }
930 else if (p == RCLASS_ORIGIN(p)) {
931 rb_ary_push(ary, p);
932 }
933 }
934 return ary;
935 }
936
937 #define VISI(x) ((x)&NOEX_MASK)
938 #define VISI_CHECK(x,f) (VISI(x) == (f))
939
940 static int
941 ins_methods_push(ID name, long type, VALUE ary, long visi)
942 {
943 if (type == -1) return ST_CONTINUE;
944
945 switch (visi) {
946 case NOEX_PRIVATE:
947 case NOEX_PROTECTED:
948 case NOEX_PUBLIC:
949 visi = (type == visi);
950 break;
951 default:
952 visi = (type != NOEX_PRIVATE);
953 break;
954 }
955 if (visi) {
956 rb_ary_push(ary, ID2SYM(name));
957 }
958 return ST_CONTINUE;
959 }
960
961 static int
962 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
963 {
964 return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
965 }
966
967 static int
968 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
969 {
970 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
971 }
972
973 static int
974 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
975 {
976 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
977 }
978
979 static int
980 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
981 {
982 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
983 }
984
985 static int
986 method_entry_i(st_data_t key, st_data_t value, st_data_t data)
987 {
988 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
989 st_table *list = (st_table *)data;
990 long type;
991
992 if (!st_lookup(list, key, 0)) {
993 if (UNDEFINED_METHOD_ENTRY_P(me)) {
994 type = -1; /* none */
995 }
996 else {
997 type = VISI(me->flag);
998 }
999 st_add_direct(list, key, type);
1000 }
1001 return ST_CONTINUE;
1002 }
1003
1004 static VALUE
1005 class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1006 {
1007 VALUE ary;
1008 int recur, prepended = 0;
1009 st_table *list;
1010
1011 if (argc == 0) {
1012 recur = TRUE;
1013 }
1014 else {
1015 VALUE r;
1016 rb_scan_args(argc, argv, "01", &r);
1017 recur = RTEST(r);
1018 }
1019
1020 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1021 mod = RCLASS_ORIGIN(mod);
1022 prepended = 1;
1023 }
1024
1025 list = st_init_numtable();
1026 for (; mod; mod = RCLASS_SUPER(mod)) {
1027 if (RCLASS_M_TBL(mod)) st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list);
1028 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1029 if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1030 if (!recur) break;
1031 }
1032 ary = rb_ary_new();
1033 st_foreach(list, func, ary);
1034 st_free_table(list);
1035
1036 return ary;
1037 }
1038
1039 /*
1040 * call-seq:
1041 * mod.instance_methods(include_super=true) -> array
1042 *
1043 * Returns an array containing the names of the public and protected instance
1044 * methods in the receiver. For a module, these are the public and protected methods;
1045 * for a class, they are the instance (not singleton) methods. With no
1046 * argument, or with an argument that is <code>false</code>, the
1047 * instance methods in <i>mod</i> are returned, otherwise the methods
1048 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
1049 *
1050 * module A
1051 * def method1() end
1052 * end
1053 * class B
1054 * def method2() end
1055 * end
1056 * class C < B
1057 * def method3() end
1058 * end
1059 *
1060 * A.instance_methods #=> [:method1]
1061 * B.instance_methods(false) #=> [:method2]
1062 * C.instance_methods(false) #=> [:method3]
1063 * C.instance_methods(true).length #=> 43
1064 */
1065
1066 VALUE
1067 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
1068 {
1069 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1070 }
1071
1072 /*
1073 * call-seq:
1074 * mod.protected_instance_methods(include_super=true) -> array
1075 *
1076 * Returns a list of the protected instance methods defined in
1077 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1078 * methods of any ancestors are included.
1079 */
1080
1081 VALUE
1082 rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
1083 {
1084 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1085 }
1086
1087 /*
1088 * call-seq:
1089 * mod.private_instance_methods(include_super=true) -> array
1090 *
1091 * Returns a list of the private instance methods defined in
1092 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1093 * methods of any ancestors are included.
1094 *
1095 * module Mod
1096 * def method1() end
1097 * private :method1
1098 * def method2() end
1099 * end
1100 * Mod.instance_methods #=> [:method2]
1101 * Mod.private_instance_methods #=> [:method1]
1102 */
1103
1104 VALUE
1105 rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
1106 {
1107 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1108 }
1109
1110 /*
1111 * call-seq:
1112 * mod.public_instance_methods(include_super=true) -> array
1113 *
1114 * Returns a list of the public instance methods defined in <i>mod</i>.
1115 * If the optional parameter is not <code>false</code>, the methods of
1116 * any ancestors are included.
1117 */
1118
1119 VALUE
1120 rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
1121 {
1122 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1123 }
1124
1125 /*
1126 * call-seq:
1127 * obj.methods(all=true) -> array
1128 *
1129 * Returns a list of the names of public and protected methods of
1130 * <i>obj</i>. This will include all the methods accessible in
1131 * <i>obj</i>'s ancestors.
1132 * If the <i>all</i> parameter is set to <code>false</code>, only those methods
1133 * in the receiver will be listed.
1134 *
1135 * class Klass
1136 * def klass_method()
1137 * end
1138 * end
1139 * k = Klass.new
1140 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1141 * # :==~, :!, :eql?
1142 * # :hash, :<=>, :class, :singleton_class]
1143 * k.methods.length #=> 57
1144 */
1145
1146 VALUE
1147 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1148 {
1149 retry:
1150 if (argc == 0) {
1151 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1152 }
1153 else {
1154 VALUE recur;
1155
1156 rb_scan_args(argc, argv, "1", &recur);
1157 if (RTEST(recur)) {
1158 argc = 0;
1159 goto retry;
1160 }
1161 return rb_obj_singleton_methods(argc, argv, obj);
1162 }
1163 }
1164
1165 /*
1166 * call-seq:
1167 * obj.protected_methods(all=true) -> array
1168 *
1169 * Returns the list of protected methods accessible to <i>obj</i>. If
1170 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1171 * in the receiver will be listed.
1172 */
1173
1174 VALUE
1175 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1176 {
1177 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1178 }
1179
1180 /*
1181 * call-seq:
1182 * obj.private_methods(all=true) -> array
1183 *
1184 * Returns the list of private methods accessible to <i>obj</i>. If
1185 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1186 * in the receiver will be listed.
1187 */
1188
1189 VALUE
1190 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1191 {
1192 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1193 }
1194
1195 /*
1196 * call-seq:
1197 * obj.public_methods(all=true) -> array
1198 *
1199 * Returns the list of public methods accessible to <i>obj</i>. If
1200 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1201 * in the receiver will be listed.
1202 */
1203
1204 VALUE
1205 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1206 {
1207 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1208 }
1209
1210 /*
1211 * call-seq:
1212 * obj.singleton_methods(all=true) -> array
1213 *
1214 * Returns an array of the names of singleton methods for <i>obj</i>.
1215 * If the optional <i>all</i> parameter is true, the list will include
1216 * methods in modules included in <i>obj</i>.
1217 * Only public and protected singleton methods are returned.
1218 *
1219 * module Other
1220 * def three() end
1221 * end
1222 *
1223 * class Single
1224 * def Single.four() end
1225 * end
1226 *
1227 * a = Single.new
1228 *
1229 * def a.one()
1230 * end
1231 *
1232 * class << a
1233 * include Other
1234 * def two()
1235 * end
1236 * end
1237 *
1238 * Single.singleton_methods #=> [:four]
1239 * a.singleton_methods(false) #=> [:two, :one]
1240 * a.singleton_methods #=> [:two, :one, :three]
1241 */
1242
1243 VALUE
1244 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1245 {
1246 VALUE recur, ary, klass;
1247 st_table *list;
1248
1249 if (argc == 0) {
1250 recur = Qtrue;
1251 }
1252 else {
1253 rb_scan_args(argc, argv, "01", &recur);
1254 }
1255 klass = CLASS_OF(obj);
1256 list = st_init_numtable();
1257 if (klass && FL_TEST(klass, FL_SINGLETON)) {
1258 if (RCLASS_M_TBL(klass))
1259 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
1260 klass = RCLASS_SUPER(klass);
1261 }
1262 if (RTEST(recur)) {
1263 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1264 if (RCLASS_M_TBL(klass))
1265 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
1266 klass = RCLASS_SUPER(klass);
1267 }
1268 }
1269 ary = rb_ary_new();
1270 st_foreach(list, ins_methods_i, ary);
1271 st_free_table(list);
1272
1273 return ary;
1274 }
1275
1333 void
1334 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1335 {
1336 rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1337 }
1338
1339 void
1340 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1341 {
1342 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1343 }
1344
1345 void
1346 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1347 {
1348 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1349 }
1350
1351 void
1352 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1353 {
1354 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1355 }
1356
1357 void
1358 rb_undef_method(VALUE klass, const char *name)
1359 {
1360 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
1361 }
1362
1371 #define SPECIAL_SINGLETON(x,c) do {\
1372 if (obj == (x)) {\
1373 return (c);\
1374 }\
1375 } while (0)
1376
1377 static inline VALUE
1378 special_singleton_class_of(VALUE obj)
1379 {
1380 SPECIAL_SINGLETON(Qnil, rb_cNilClass);
1381 SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
1382 SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
1383 return Qnil;
1384 }
1385
1386 VALUE
1387 rb_special_singleton_class(VALUE obj)
1388 {
1389 return special_singleton_class_of(obj);
1390 }
1391
1401 static VALUE
1402 singleton_class_of(VALUE obj)
1403 {
1404 VALUE klass;
1405
1406 if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
1407 rb_raise(rb_eTypeError, "can't define singleton");
1408 }
1409 if (SPECIAL_CONST_P(obj)) {
1410 klass = special_singleton_class_of(obj);
1411 if (NIL_P(klass))
1412 rb_bug("unknown immediate %p", (void *)obj);
1413 return klass;
1414 }
1415 else {
1416 enum ruby_value_type type = BUILTIN_TYPE(obj);
1417 if (type == T_FLOAT || type == T_BIGNUM) {
1418 rb_raise(rb_eTypeError, "can't define singleton");
1419 }
1420 }
1421
1422 if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1423 rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1424 klass = RBASIC(obj)->klass;
1425 }
1426 else {
1427 klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1428 }
1429
1430 if (OBJ_TAINTED(obj)) {
1431 OBJ_TAINT(klass);
1432 }
1433 else {
1434 FL_UNSET(klass, FL_TAINT);
1435 }
1436 if (OBJ_UNTRUSTED(obj)) {
1437 OBJ_UNTRUST(klass);
1438 }
1439 else {
1440 FL_UNSET(klass, FL_UNTRUSTED);
1441 }
1442 if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1443
1444 return klass;
1445 }
1446
1447
1465 VALUE
1466 rb_singleton_class(VALUE obj)
1467 {
1468 VALUE klass = singleton_class_of(obj);
1469
1470 /* ensures an exposed class belongs to its own eigenclass */
1471 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1472
1473 return klass;
1474 }
1475
1492 void
1493 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1494 {
1495 rb_define_method(singleton_class_of(obj), name, func, argc);
1496 }
1497
1498
1499
1507 void
1508 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1509 {
1510 rb_define_private_method(module, name, func, argc);
1511 rb_define_singleton_method(module, name, func, argc);
1512 }
1513
1514
1521 void
1522 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1523 {
1524 rb_define_module_function(rb_mKernel, name, func, argc);
1525 }
1526
1527
1534 void
1535 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1536 {
1537 rb_alias(klass, rb_intern(name1), rb_intern(name2));
1538 }
1539
1547 void
1548 rb_define_attr(VALUE klass, const char *name, int read, int write)
1549 {
1550 rb_attr(klass, rb_intern(name), read, write, FALSE);
1551 }
1552
1553 int
1554 rb_obj_basic_to_s_p(VALUE obj)
1555 {
1556 const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0);
1557 if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1558 me->def->body.cfunc.func == rb_any_to_s)
1559 return 1;
1560 return 0;
1561 }
1562
1563 #include <stdarg.h>
1564
1565 int
1566 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1567 {
1568 int i;
1569 const char *p = fmt;
1570 VALUE *var;
1571 va_list vargs;
1572 int f_var = 0, f_hash = 0, f_block = 0;
1573 int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1574 int argi = 0;
1575 VALUE hash = Qnil;
1576
1577 if (ISDIGIT(*p)) {
1578 n_lead = *p - '0';
1579 p++;
1580 if (ISDIGIT(*p)) {
1581 n_opt = *p - '0';
1582 p++;
1583 if (ISDIGIT(*p)) {
1584 n_trail = *p - '0';
1585 p++;
1586 goto block_arg;
1587 }
1588 }
1589 }
1590 if (*p == '*') {
1591 f_var = 1;
1592 p++;
1593 if (ISDIGIT(*p)) {
1594 n_trail = *p - '0';
1595 p++;
1596 }
1597 }
1598 block_arg:
1599 if (*p == ':') {
1600 f_hash = 1;
1601 p++;
1602 }
1603 if (*p == '&') {
1604 f_block = 1;
1605 p++;
1606 }
1607 if (*p != '\0') {
1608 rb_fatal("bad scan arg format: %s", fmt);
1609 }
1610 n_mand = n_lead + n_trail;
1611
1612 if (argc < n_mand)
1613 goto argc_error;
1614
1615 va_start(vargs, fmt);
1616
1617 /* capture an option hash - phase 1: pop */
1618 if (f_hash && n_mand < argc) {
1619 VALUE last = argv[argc - 1];
1620
1621 if (NIL_P(last)) {
1622 /* nil is taken as an empty option hash only if it is not
1623 ambiguous; i.e. '*' is not specified and arguments are
1624 given more than sufficient */
1625 if (!f_var && n_mand + n_opt < argc)
1626 argc--;
1627 }
1628 else {
1629 hash = rb_check_hash_type(last);
1630 if (!NIL_P(hash))
1631 argc--;
1632 }
1633 }
1634 /* capture leading mandatory arguments */
1635 for (i = n_lead; i-- > 0; ) {
1636 var = va_arg(vargs, VALUE *);
1637 if (var) *var = argv[argi];
1638 argi++;
1639 }
1640 /* capture optional arguments */
1641 for (i = n_opt; i-- > 0; ) {
1642 var = va_arg(vargs, VALUE *);
1643 if (argi < argc - n_trail) {
1644 if (var) *var = argv[argi];
1645 argi++;
1646 }
1647 else {
1648 if (var) *var = Qnil;
1649 }
1650 }
1651 /* capture variable length arguments */
1652 if (f_var) {
1653 int n_var = argc - argi - n_trail;
1654
1655 var = va_arg(vargs, VALUE *);
1656 if (0 < n_var) {
1657 if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1658 argi += n_var;
1659 }
1660 else {
1661 if (var) *var = rb_ary_new();
1662 }
1663 }
1664 /* capture trailing mandatory arguments */
1665 for (i = n_trail; i-- > 0; ) {
1666 var = va_arg(vargs, VALUE *);
1667 if (var) *var = argv[argi];
1668 argi++;
1669 }
1670 /* capture an option hash - phase 2: assignment */
1671 if (f_hash) {
1672 var = va_arg(vargs, VALUE *);
1673 if (var) *var = hash;
1674 }
1675 /* capture iterator block */
1676 if (f_block) {
1677 var = va_arg(vargs, VALUE *);
1678 if (rb_block_given_p()) {
1679 *var = rb_block_proc();
1680 }
1681 else {
1682 *var = Qnil;
1683 }
1684 }
1685 va_end(vargs);
1686
1687 if (argi < argc) {
1688 argc_error:
1689 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
1690 }
1691
1692 return argc;
1693 }
1694