1 /**********************************************************************
6 created at: Tue Aug 10 15:05:44 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include "ruby/signal.h"
14 #include "ruby/node.h"
18 extern st_table
*rb_class_tbl
;
21 class_alloc(VALUE flags
, VALUE klass
)
23 rb_classext_t
*ext
= ALLOC(rb_classext_t
);
24 NEWOBJ(obj
, struct RClass
);
25 OBJSETUP(obj
, klass
, flags
);
27 RCLASS_IV_TBL(obj
) = 0;
28 RCLASS_M_TBL(obj
) = 0;
29 RCLASS_SUPER(obj
) = 0;
30 RCLASS_IV_INDEX_TBL(obj
) = 0;
35 rb_class_boot(VALUE super
)
37 VALUE klass
= class_alloc(T_CLASS
, rb_cClass
);
39 RCLASS_SUPER(klass
) = super
;
40 RCLASS_M_TBL(klass
) = st_init_numtable();
42 OBJ_INFECT(klass
, super
);
47 rb_check_inheritable(VALUE super
)
49 if (TYPE(super
) != T_CLASS
) {
50 rb_raise(rb_eTypeError
, "superclass must be a Class (%s given)",
51 rb_obj_classname(super
));
53 if (RBASIC(super
)->flags
& FL_SINGLETON
) {
54 rb_raise(rb_eTypeError
, "can't make subclass of singleton class");
59 rb_class_new(VALUE super
)
61 Check_Type(super
, T_CLASS
);
62 rb_check_inheritable(super
);
63 if (super
== rb_cClass
) {
64 rb_raise(rb_eTypeError
, "can't make subclass of Class");
66 return rb_class_boot(super
);
69 struct clone_method_data
{
75 clone_method(ID mid
, NODE
*body
, struct clone_method_data
*data
)
78 st_insert(data
->tbl
, mid
, 0);
81 st_insert(data
->tbl
, mid
,
84 NEW_METHOD(body
->nd_body
->nd_body
,
85 data
->klass
, /* TODO */
86 body
->nd_body
->nd_noex
),
94 rb_mod_init_copy(VALUE clone
, VALUE orig
)
96 rb_obj_init_copy(clone
, orig
);
97 if (!FL_TEST(CLASS_OF(clone
), FL_SINGLETON
)) {
98 RBASIC(clone
)->klass
= rb_singleton_class_clone(orig
);
100 RCLASS_SUPER(clone
) = RCLASS_SUPER(orig
);
101 if (RCLASS_IV_TBL(orig
)) {
104 RCLASS_IV_TBL(clone
) = st_copy(RCLASS_IV_TBL(orig
));
105 id
= rb_intern("__classpath__");
106 st_delete(RCLASS_IV_TBL(clone
), (st_data_t
*)&id
, 0);
107 id
= rb_intern("__classid__");
108 st_delete(RCLASS_IV_TBL(clone
), (st_data_t
*)&id
, 0);
110 if (RCLASS_M_TBL(orig
)) {
111 struct clone_method_data data
;
112 data
.tbl
= RCLASS_M_TBL(clone
) = st_init_numtable();
114 st_foreach(RCLASS_M_TBL(orig
), clone_method
,
123 rb_class_init_copy(VALUE clone
, VALUE orig
)
125 if (RCLASS_SUPER(clone
) != 0) {
126 rb_raise(rb_eTypeError
, "already initialized class");
128 if (FL_TEST(orig
, FL_SINGLETON
)) {
129 rb_raise(rb_eTypeError
, "can't copy singleton class");
131 return rb_mod_init_copy(clone
, orig
);
135 rb_singleton_class_clone(VALUE obj
)
137 VALUE klass
= RBASIC(obj
)->klass
;
139 if (!FL_TEST(klass
, FL_SINGLETON
))
142 struct clone_method_data data
;
143 /* copy singleton(unnamed) class */
144 VALUE clone
= class_alloc(RBASIC(klass
)->flags
, 0);
146 if (BUILTIN_TYPE(obj
) == T_CLASS
) {
147 RBASIC(clone
)->klass
= (VALUE
)clone
;
150 RBASIC(clone
)->klass
= rb_singleton_class_clone(klass
);
153 RCLASS_SUPER(clone
) = RCLASS_SUPER(klass
);
154 if (RCLASS_IV_TBL(klass
)) {
155 RCLASS_IV_TBL(clone
) = st_copy(RCLASS_IV_TBL(klass
));
157 RCLASS_M_TBL(clone
) = st_init_numtable();
158 data
.tbl
= RCLASS_M_TBL(clone
);
159 data
.klass
= (VALUE
)clone
;
160 st_foreach(RCLASS_M_TBL(klass
), clone_method
,
162 rb_singleton_class_attached(RBASIC(clone
)->klass
, (VALUE
)clone
);
163 FL_SET(clone
, FL_SINGLETON
);
169 rb_singleton_class_attached(VALUE klass
, VALUE obj
)
171 if (FL_TEST(klass
, FL_SINGLETON
)) {
172 if (!RCLASS_IV_TBL(klass
)) {
173 RCLASS_IV_TBL(klass
) = st_init_numtable();
175 st_insert(RCLASS_IV_TBL(klass
), rb_intern("__attached__"), obj
);
180 rb_make_metaclass(VALUE obj
, VALUE super
)
182 if (BUILTIN_TYPE(obj
) == T_CLASS
&& FL_TEST(obj
, FL_SINGLETON
)) {
183 return RBASIC(obj
)->klass
= rb_cClass
;
187 VALUE klass
= rb_class_boot(super
);
189 FL_SET(klass
, FL_SINGLETON
);
190 RBASIC(obj
)->klass
= klass
;
191 rb_singleton_class_attached(klass
, obj
);
193 metasuper
= RBASIC(rb_class_real(super
))->klass
;
194 /* metaclass of a superclass may be NULL at boot time */
196 RBASIC(klass
)->klass
= metasuper
;
203 rb_define_class_id(ID id
, VALUE super
)
207 if (!super
) super
= rb_cObject
;
208 klass
= rb_class_new(super
);
209 rb_make_metaclass(klass
, RBASIC(super
)->klass
);
215 rb_class_inherited(VALUE super
, VALUE klass
)
217 if (!super
) super
= rb_cObject
;
218 return rb_funcall(super
, rb_intern("inherited"), 1, klass
);
222 rb_define_class(const char *name
, VALUE super
)
227 id
= rb_intern(name
);
228 if (rb_const_defined(rb_cObject
, id
)) {
229 klass
= rb_const_get(rb_cObject
, id
);
230 if (TYPE(klass
) != T_CLASS
) {
231 rb_raise(rb_eTypeError
, "%s is not a class", name
);
233 if (rb_class_real(RCLASS_SUPER(klass
)) != super
) {
234 rb_name_error(id
, "%s is already defined", name
);
239 rb_warn("no super class for `%s', Object assumed", name
);
241 klass
= rb_define_class_id(id
, super
);
242 st_add_direct(rb_class_tbl
, id
, klass
);
243 rb_name_class(klass
, id
);
244 rb_const_set(rb_cObject
, id
, klass
);
245 rb_class_inherited(super
, klass
);
251 rb_define_class_under(VALUE outer
, const char *name
, VALUE super
)
256 id
= rb_intern(name
);
257 if (rb_const_defined_at(outer
, id
)) {
258 klass
= rb_const_get_at(outer
, id
);
259 if (TYPE(klass
) != T_CLASS
) {
260 rb_raise(rb_eTypeError
, "%s is not a class", name
);
262 if (rb_class_real(RCLASS_SUPER(klass
)) != super
) {
263 rb_name_error(id
, "%s is already defined", name
);
268 rb_warn("no super class for `%s::%s', Object assumed",
269 rb_class2name(outer
), name
);
271 klass
= rb_define_class_id(id
, super
);
272 rb_set_class_path(klass
, outer
, name
);
273 rb_const_set(outer
, id
, klass
);
274 rb_class_inherited(super
, klass
);
282 VALUE mdl
= class_alloc(T_MODULE
, rb_cModule
);
284 RCLASS_M_TBL(mdl
) = st_init_numtable();
290 rb_define_module_id(ID id
)
294 mdl
= rb_module_new();
295 rb_name_class(mdl
, id
);
301 rb_define_module(const char *name
)
306 id
= rb_intern(name
);
307 if (rb_const_defined(rb_cObject
, id
)) {
308 module
= rb_const_get(rb_cObject
, id
);
309 if (TYPE(module
) == T_MODULE
)
311 rb_raise(rb_eTypeError
, "%s is not a module", rb_obj_classname(module
));
313 module
= rb_define_module_id(id
);
314 st_add_direct(rb_class_tbl
, id
, module
);
315 rb_const_set(rb_cObject
, id
, module
);
321 rb_define_module_under(VALUE outer
, const char *name
)
326 id
= rb_intern(name
);
327 if (rb_const_defined_at(outer
, id
)) {
328 module
= rb_const_get_at(outer
, id
);
329 if (TYPE(module
) == T_MODULE
)
331 rb_raise(rb_eTypeError
, "%s::%s is not a module",
332 rb_class2name(outer
), rb_obj_classname(module
));
334 module
= rb_define_module_id(id
);
335 rb_const_set(outer
, id
, module
);
336 rb_set_class_path(module
, outer
, name
);
342 include_class_new(VALUE module
, VALUE super
)
344 VALUE klass
= class_alloc(T_ICLASS
, rb_cClass
);
346 if (BUILTIN_TYPE(module
) == T_ICLASS
) {
347 module
= RBASIC(module
)->klass
;
349 if (!RCLASS_IV_TBL(module
)) {
350 RCLASS_IV_TBL(module
) = st_init_numtable();
352 RCLASS_IV_TBL(klass
) = RCLASS_IV_TBL(module
);
353 RCLASS_M_TBL(klass
) = RCLASS_M_TBL(module
);
354 RCLASS_SUPER(klass
) = super
;
355 if (TYPE(module
) == T_ICLASS
) {
356 RBASIC(klass
)->klass
= RBASIC(module
)->klass
;
359 RBASIC(klass
)->klass
= module
;
361 OBJ_INFECT(klass
, module
);
362 OBJ_INFECT(klass
, super
);
368 rb_include_module(VALUE klass
, VALUE module
)
373 rb_frozen_class_p(klass
);
374 if (!OBJ_TAINTED(klass
)) {
378 if (TYPE(module
) != T_MODULE
) {
379 Check_Type(module
, T_MODULE
);
382 OBJ_INFECT(klass
, module
);
385 int superclass_seen
= Qfalse
;
387 if (RCLASS_M_TBL(klass
) == RCLASS_M_TBL(module
))
388 rb_raise(rb_eArgError
, "cyclic include detected");
389 /* ignore if the module included already in superclasses */
390 for (p
= RCLASS_SUPER(klass
); p
; p
= RCLASS_SUPER(p
)) {
391 switch (BUILTIN_TYPE(p
)) {
393 if (RCLASS_M_TBL(p
) == RCLASS_M_TBL(module
)) {
394 if (!superclass_seen
) {
395 c
= p
; /* move insertion point */
401 superclass_seen
= Qtrue
;
405 c
= RCLASS_SUPER(c
) = include_class_new(module
, RCLASS_SUPER(c
));
408 module
= RCLASS_SUPER(module
);
410 if (changed
) rb_clear_cache();
415 * mod.included_modules -> array
417 * Returns the list of modules included in <i>mod</i>.
426 * Mixin.included_modules #=> []
427 * Outer.included_modules #=> [Mixin]
431 rb_mod_included_modules(VALUE mod
)
433 VALUE ary
= rb_ary_new();
436 for (p
= RCLASS_SUPER(mod
); p
; p
= RCLASS_SUPER(p
)) {
437 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
438 rb_ary_push(ary
, RBASIC(p
)->klass
);
446 * mod.include?(module) => true or false
448 * Returns <code>true</code> if <i>module</i> is included in
449 * <i>mod</i> or one of <i>mod</i>'s ancestors.
458 * B.include?(A) #=> true
459 * C.include?(A) #=> true
460 * A.include?(A) #=> false
464 rb_mod_include_p(VALUE mod
, VALUE mod2
)
468 Check_Type(mod2
, T_MODULE
);
469 for (p
= RCLASS_SUPER(mod
); p
; p
= RCLASS_SUPER(p
)) {
470 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
471 if (RBASIC(p
)->klass
== mod2
) return Qtrue
;
479 * mod.ancestors -> array
481 * Returns a list of modules included in <i>mod</i> (including
482 * <i>mod</i> itself).
489 * Mod.ancestors #=> [Mod, Comparable, Math]
490 * Math.ancestors #=> [Math]
494 rb_mod_ancestors(VALUE mod
)
496 VALUE p
, ary
= rb_ary_new();
498 for (p
= mod
; p
; p
= RCLASS_SUPER(p
)) {
499 if (FL_TEST(p
, FL_SINGLETON
))
501 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
502 rb_ary_push(ary
, RBASIC(p
)->klass
);
511 #define VISI(x) ((x)&NOEX_MASK)
512 #define VISI_CHECK(x,f) (VISI(x) == (f))
515 ins_methods_push(ID name
, long type
, VALUE ary
, long visi
)
517 if (type
== -1) return ST_CONTINUE
;
523 visi
= (type
== visi
);
526 visi
= (type
!= NOEX_PRIVATE
);
530 rb_ary_push(ary
, ID2SYM(name
));
536 ins_methods_i(ID name
, long type
, VALUE ary
)
538 return ins_methods_push(name
, type
, ary
, -1); /* everything but private */
542 ins_methods_prot_i(ID name
, long type
, VALUE ary
)
544 return ins_methods_push(name
, type
, ary
, NOEX_PROTECTED
);
548 ins_methods_priv_i(ID name
, long type
, VALUE ary
)
550 return ins_methods_push(name
, type
, ary
, NOEX_PRIVATE
);
554 ins_methods_pub_i(ID name
, long type
, VALUE ary
)
556 return ins_methods_push(name
, type
, ary
, NOEX_PUBLIC
);
560 method_entry(ID key
, NODE
*body
, st_table
*list
)
564 if (key
== ID_ALLOCATOR
) {
568 if (!st_lookup(list
, key
, 0)) {
569 if (body
==0 || !body
->nd_body
->nd_body
) {
570 type
= -1; /* none */
573 type
= VISI(body
->nd_body
->nd_noex
);
575 st_add_direct(list
, key
, type
);
581 class_instance_method_list(int argc
, VALUE
*argv
, VALUE mod
, int (*func
) (ID
, long, VALUE
))
592 rb_scan_args(argc
, argv
, "01", &r
);
596 list
= st_init_numtable();
597 for (; mod
; mod
= RCLASS_SUPER(mod
)) {
598 st_foreach(RCLASS_M_TBL(mod
), method_entry
, (st_data_t
)list
);
599 if (BUILTIN_TYPE(mod
) == T_ICLASS
) continue;
600 if (FL_TEST(mod
, FL_SINGLETON
)) continue;
604 st_foreach(list
, func
, ary
);
612 * mod.instance_methods(include_super=true) => array
614 * Returns an array containing the names of public instance methods in
615 * the receiver. For a module, these are the public methods; for a
616 * class, they are the instance (not singleton) methods. With no
617 * argument, or with an argument that is <code>false</code>, the
618 * instance methods in <i>mod</i> are returned, otherwise the methods
619 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
631 * A.instance_methods #=> [:method1]
632 * B.instance_methods(false) #=> [:method2]
633 * C.instance_methods(false) #=> [:method3]
634 * C.instance_methods(true).length #=> 43
638 rb_class_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
640 return class_instance_method_list(argc
, argv
, mod
, ins_methods_i
);
645 * mod.protected_instance_methods(include_super=true) => array
647 * Returns a list of the protected instance methods defined in
648 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
649 * methods of any ancestors are included.
653 rb_class_protected_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
655 return class_instance_method_list(argc
, argv
, mod
, ins_methods_prot_i
);
660 * mod.private_instance_methods(include_super=true) => array
662 * Returns a list of the private instance methods defined in
663 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
664 * methods of any ancestors are included.
671 * Mod.instance_methods #=> [:method2]
672 * Mod.private_instance_methods #=> [:method1]
676 rb_class_private_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
678 return class_instance_method_list(argc
, argv
, mod
, ins_methods_priv_i
);
683 * mod.public_instance_methods(include_super=true) => array
685 * Returns a list of the public instance methods defined in <i>mod</i>.
686 * If the optional parameter is not <code>false</code>, the methods of
687 * any ancestors are included.
691 rb_class_public_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
693 return class_instance_method_list(argc
, argv
, mod
, ins_methods_pub_i
);
698 * obj.singleton_methods(all=true) => array
700 * Returns an array of the names of singleton methods for <i>obj</i>.
701 * If the optional <i>all</i> parameter is true, the list will include
702 * methods in modules included in <i>obj</i>.
709 * def Single.four() end
723 * Single.singleton_methods #=> [:four]
724 * a.singleton_methods(false) #=> [:two, :one]
725 * a.singleton_methods #=> [:two, :one, :three]
729 rb_obj_singleton_methods(int argc
, VALUE
*argv
, VALUE obj
)
731 VALUE recur
, ary
, klass
;
738 rb_scan_args(argc
, argv
, "01", &recur
);
740 klass
= CLASS_OF(obj
);
741 list
= st_init_numtable();
742 if (klass
&& FL_TEST(klass
, FL_SINGLETON
)) {
743 st_foreach(RCLASS_M_TBL(klass
), method_entry
, (st_data_t
)list
);
744 klass
= RCLASS_SUPER(klass
);
747 while (klass
&& (FL_TEST(klass
, FL_SINGLETON
) || TYPE(klass
) == T_ICLASS
)) {
748 st_foreach(RCLASS_M_TBL(klass
), method_entry
, (st_data_t
)list
);
749 klass
= RCLASS_SUPER(klass
);
753 st_foreach(list
, ins_methods_i
, ary
);
760 rb_define_method_id(VALUE klass
, ID name
, VALUE (*func
)(ANYARGS
), int argc
)
762 rb_add_method(klass
, name
, NEW_CFUNC(func
,argc
), NOEX_PUBLIC
);
766 rb_define_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
768 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PUBLIC
);
772 rb_define_protected_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
774 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PROTECTED
);
778 rb_define_private_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
780 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PRIVATE
);
784 rb_undef_method(VALUE klass
, const char *name
)
786 rb_add_method(klass
, rb_intern(name
), 0, NOEX_UNDEF
);
789 #define SPECIAL_SINGLETON(x,c) do {\
796 rb_singleton_class(VALUE obj
)
800 if (FIXNUM_P(obj
) || SYMBOL_P(obj
)) {
801 rb_raise(rb_eTypeError
, "can't define singleton");
803 if (rb_special_const_p(obj
)) {
804 SPECIAL_SINGLETON(Qnil
, rb_cNilClass
);
805 SPECIAL_SINGLETON(Qfalse
, rb_cFalseClass
);
806 SPECIAL_SINGLETON(Qtrue
, rb_cTrueClass
);
807 rb_bug("unknown immediate %ld", obj
);
811 if (FL_TEST(RBASIC(obj
)->klass
, FL_SINGLETON
) &&
812 rb_iv_get(RBASIC(obj
)->klass
, "__attached__") == obj
) {
813 klass
= RBASIC(obj
)->klass
;
816 klass
= rb_make_metaclass(obj
, RBASIC(obj
)->klass
);
818 if (OBJ_TAINTED(obj
)) {
822 FL_UNSET(klass
, FL_TAINT
);
824 if (OBJ_FROZEN(obj
)) OBJ_FREEZE(klass
);
831 rb_define_singleton_method(VALUE obj
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
833 rb_define_method(rb_singleton_class(obj
), name
, func
, argc
);
837 rb_define_module_function(VALUE module
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
839 rb_define_private_method(module
, name
, func
, argc
);
840 rb_define_singleton_method(module
, name
, func
, argc
);
844 rb_define_global_function(const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
846 rb_define_module_function(rb_mKernel
, name
, func
, argc
);
850 rb_define_alias(VALUE klass
, const char *name1
, const char *name2
)
852 rb_alias(klass
, rb_intern(name1
), rb_intern(name2
));
856 rb_define_attr(VALUE klass
, const char *name
, int read
, int write
)
858 rb_attr(klass
, rb_intern(name
), read
, write
, Qfalse
);
864 rb_scan_args(int argc
, const VALUE
*argv
, const char *fmt
, ...)
871 va_start(vargs
, fmt
);
873 if (*p
== '*') goto rest_arg
;
878 rb_raise(rb_eArgError
, "wrong number of arguments (%d for %d)", argc
, n
);
879 for (i
=0; i
<n
; i
++) {
880 var
= va_arg(vargs
, VALUE
*);
881 if (var
) *var
= argv
[i
];
892 var
= va_arg(vargs
, VALUE
*);
894 if (var
) *var
= argv
[i
];
897 if (var
) *var
= Qnil
;
905 var
= va_arg(vargs
, VALUE
*);
907 if (var
) *var
= rb_ary_new4(argc
-i
, argv
+i
);
911 if (var
) *var
= rb_ary_new();
917 var
= va_arg(vargs
, VALUE
*);
918 if (rb_block_given_p()) {
919 *var
= rb_block_proc();
933 rb_raise(rb_eArgError
, "wrong number of arguments (%d for %d)", argc
, i
);
939 rb_fatal("bad scan arg format: %s", fmt
);