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 CONST_ID(id
, "__classpath__");
106 st_delete(RCLASS_IV_TBL(clone
), (st_data_t
*)&id
, 0);
107 CONST_ID(id
, "__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
)) {
173 if (!RCLASS_IV_TBL(klass
)) {
174 RCLASS_IV_TBL(klass
) = st_init_numtable();
176 CONST_ID(attached
, "__attached__");
177 st_insert(RCLASS_IV_TBL(klass
), attached
, obj
);
182 rb_make_metaclass(VALUE obj
, VALUE super
)
184 if (BUILTIN_TYPE(obj
) == T_CLASS
&& FL_TEST(obj
, FL_SINGLETON
)) {
185 return RBASIC(obj
)->klass
= rb_cClass
;
189 VALUE klass
= rb_class_boot(super
);
191 FL_SET(klass
, FL_SINGLETON
);
192 RBASIC(obj
)->klass
= klass
;
193 rb_singleton_class_attached(klass
, obj
);
195 metasuper
= RBASIC(rb_class_real(super
))->klass
;
196 /* metaclass of a superclass may be NULL at boot time */
198 RBASIC(klass
)->klass
= metasuper
;
205 rb_define_class_id(ID id
, VALUE super
)
209 if (!super
) super
= rb_cObject
;
210 klass
= rb_class_new(super
);
211 rb_make_metaclass(klass
, RBASIC(super
)->klass
);
217 rb_class_inherited(VALUE super
, VALUE klass
)
220 if (!super
) super
= rb_cObject
;
221 CONST_ID(inherited
, "inherited");
222 return rb_funcall(super
, inherited
, 1, klass
);
226 rb_define_class(const char *name
, VALUE super
)
231 id
= rb_intern(name
);
232 if (rb_const_defined(rb_cObject
, id
)) {
233 klass
= rb_const_get(rb_cObject
, id
);
234 if (TYPE(klass
) != T_CLASS
) {
235 rb_raise(rb_eTypeError
, "%s is not a class", name
);
237 if (rb_class_real(RCLASS_SUPER(klass
)) != super
) {
238 rb_name_error(id
, "%s is already defined", name
);
243 rb_warn("no super class for `%s', Object assumed", name
);
245 klass
= rb_define_class_id(id
, super
);
246 st_add_direct(rb_class_tbl
, id
, klass
);
247 rb_name_class(klass
, id
);
248 rb_const_set(rb_cObject
, id
, klass
);
249 rb_class_inherited(super
, klass
);
255 rb_define_class_under(VALUE outer
, const char *name
, VALUE super
)
260 id
= rb_intern(name
);
261 if (rb_const_defined_at(outer
, id
)) {
262 klass
= rb_const_get_at(outer
, id
);
263 if (TYPE(klass
) != T_CLASS
) {
264 rb_raise(rb_eTypeError
, "%s is not a class", name
);
266 if (rb_class_real(RCLASS_SUPER(klass
)) != super
) {
267 rb_name_error(id
, "%s is already defined", name
);
272 rb_warn("no super class for `%s::%s', Object assumed",
273 rb_class2name(outer
), name
);
275 klass
= rb_define_class_id(id
, super
);
276 rb_set_class_path(klass
, outer
, name
);
277 rb_const_set(outer
, id
, klass
);
278 rb_class_inherited(super
, klass
);
286 VALUE mdl
= class_alloc(T_MODULE
, rb_cModule
);
288 RCLASS_M_TBL(mdl
) = st_init_numtable();
294 rb_define_module_id(ID id
)
298 mdl
= rb_module_new();
299 rb_name_class(mdl
, id
);
305 rb_define_module(const char *name
)
310 id
= rb_intern(name
);
311 if (rb_const_defined(rb_cObject
, id
)) {
312 module
= rb_const_get(rb_cObject
, id
);
313 if (TYPE(module
) == T_MODULE
)
315 rb_raise(rb_eTypeError
, "%s is not a module", rb_obj_classname(module
));
317 module
= rb_define_module_id(id
);
318 st_add_direct(rb_class_tbl
, id
, module
);
319 rb_const_set(rb_cObject
, id
, module
);
325 rb_define_module_under(VALUE outer
, const char *name
)
330 id
= rb_intern(name
);
331 if (rb_const_defined_at(outer
, id
)) {
332 module
= rb_const_get_at(outer
, id
);
333 if (TYPE(module
) == T_MODULE
)
335 rb_raise(rb_eTypeError
, "%s::%s is not a module",
336 rb_class2name(outer
), rb_obj_classname(module
));
338 module
= rb_define_module_id(id
);
339 rb_const_set(outer
, id
, module
);
340 rb_set_class_path(module
, outer
, name
);
346 include_class_new(VALUE module
, VALUE super
)
348 VALUE klass
= class_alloc(T_ICLASS
, rb_cClass
);
350 if (BUILTIN_TYPE(module
) == T_ICLASS
) {
351 module
= RBASIC(module
)->klass
;
353 if (!RCLASS_IV_TBL(module
)) {
354 RCLASS_IV_TBL(module
) = st_init_numtable();
356 RCLASS_IV_TBL(klass
) = RCLASS_IV_TBL(module
);
357 RCLASS_M_TBL(klass
) = RCLASS_M_TBL(module
);
358 RCLASS_SUPER(klass
) = super
;
359 if (TYPE(module
) == T_ICLASS
) {
360 RBASIC(klass
)->klass
= RBASIC(module
)->klass
;
363 RBASIC(klass
)->klass
= module
;
365 OBJ_INFECT(klass
, module
);
366 OBJ_INFECT(klass
, super
);
372 rb_include_module(VALUE klass
, VALUE module
)
377 rb_frozen_class_p(klass
);
378 if (!OBJ_TAINTED(klass
)) {
382 if (TYPE(module
) != T_MODULE
) {
383 Check_Type(module
, T_MODULE
);
386 OBJ_INFECT(klass
, module
);
389 int superclass_seen
= Qfalse
;
391 if (RCLASS_M_TBL(klass
) == RCLASS_M_TBL(module
))
392 rb_raise(rb_eArgError
, "cyclic include detected");
393 /* ignore if the module included already in superclasses */
394 for (p
= RCLASS_SUPER(klass
); p
; p
= RCLASS_SUPER(p
)) {
395 switch (BUILTIN_TYPE(p
)) {
397 if (RCLASS_M_TBL(p
) == RCLASS_M_TBL(module
)) {
398 if (!superclass_seen
) {
399 c
= p
; /* move insertion point */
405 superclass_seen
= Qtrue
;
409 c
= RCLASS_SUPER(c
) = include_class_new(module
, RCLASS_SUPER(c
));
412 module
= RCLASS_SUPER(module
);
414 if (changed
) rb_clear_cache();
419 * mod.included_modules -> array
421 * Returns the list of modules included in <i>mod</i>.
430 * Mixin.included_modules #=> []
431 * Outer.included_modules #=> [Mixin]
435 rb_mod_included_modules(VALUE mod
)
437 VALUE ary
= rb_ary_new();
440 for (p
= RCLASS_SUPER(mod
); p
; p
= RCLASS_SUPER(p
)) {
441 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
442 rb_ary_push(ary
, RBASIC(p
)->klass
);
450 * mod.include?(module) => true or false
452 * Returns <code>true</code> if <i>module</i> is included in
453 * <i>mod</i> or one of <i>mod</i>'s ancestors.
462 * B.include?(A) #=> true
463 * C.include?(A) #=> true
464 * A.include?(A) #=> false
468 rb_mod_include_p(VALUE mod
, VALUE mod2
)
472 Check_Type(mod2
, T_MODULE
);
473 for (p
= RCLASS_SUPER(mod
); p
; p
= RCLASS_SUPER(p
)) {
474 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
475 if (RBASIC(p
)->klass
== mod2
) return Qtrue
;
483 * mod.ancestors -> array
485 * Returns a list of modules included in <i>mod</i> (including
486 * <i>mod</i> itself).
493 * Mod.ancestors #=> [Mod, Comparable, Math]
494 * Math.ancestors #=> [Math]
498 rb_mod_ancestors(VALUE mod
)
500 VALUE p
, ary
= rb_ary_new();
502 for (p
= mod
; p
; p
= RCLASS_SUPER(p
)) {
503 if (FL_TEST(p
, FL_SINGLETON
))
505 if (BUILTIN_TYPE(p
) == T_ICLASS
) {
506 rb_ary_push(ary
, RBASIC(p
)->klass
);
515 #define VISI(x) ((x)&NOEX_MASK)
516 #define VISI_CHECK(x,f) (VISI(x) == (f))
519 ins_methods_push(ID name
, long type
, VALUE ary
, long visi
)
521 if (type
== -1) return ST_CONTINUE
;
527 visi
= (type
== visi
);
530 visi
= (type
!= NOEX_PRIVATE
);
534 rb_ary_push(ary
, ID2SYM(name
));
540 ins_methods_i(ID name
, long type
, VALUE ary
)
542 return ins_methods_push(name
, type
, ary
, -1); /* everything but private */
546 ins_methods_prot_i(ID name
, long type
, VALUE ary
)
548 return ins_methods_push(name
, type
, ary
, NOEX_PROTECTED
);
552 ins_methods_priv_i(ID name
, long type
, VALUE ary
)
554 return ins_methods_push(name
, type
, ary
, NOEX_PRIVATE
);
558 ins_methods_pub_i(ID name
, long type
, VALUE ary
)
560 return ins_methods_push(name
, type
, ary
, NOEX_PUBLIC
);
564 method_entry(ID key
, NODE
*body
, st_table
*list
)
568 if (key
== ID_ALLOCATOR
) {
572 if (!st_lookup(list
, key
, 0)) {
573 if (body
==0 || !body
->nd_body
->nd_body
) {
574 type
= -1; /* none */
577 type
= VISI(body
->nd_body
->nd_noex
);
579 st_add_direct(list
, key
, type
);
585 class_instance_method_list(int argc
, VALUE
*argv
, VALUE mod
, int (*func
) (ID
, long, VALUE
))
596 rb_scan_args(argc
, argv
, "01", &r
);
600 list
= st_init_numtable();
601 for (; mod
; mod
= RCLASS_SUPER(mod
)) {
602 st_foreach(RCLASS_M_TBL(mod
), method_entry
, (st_data_t
)list
);
603 if (BUILTIN_TYPE(mod
) == T_ICLASS
) continue;
604 if (FL_TEST(mod
, FL_SINGLETON
)) continue;
608 st_foreach(list
, func
, ary
);
616 * mod.instance_methods(include_super=true) => array
618 * Returns an array containing the names of public instance methods in
619 * the receiver. For a module, these are the public methods; for a
620 * class, they are the instance (not singleton) methods. With no
621 * argument, or with an argument that is <code>false</code>, the
622 * instance methods in <i>mod</i> are returned, otherwise the methods
623 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
635 * A.instance_methods #=> [:method1]
636 * B.instance_methods(false) #=> [:method2]
637 * C.instance_methods(false) #=> [:method3]
638 * C.instance_methods(true).length #=> 43
642 rb_class_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
644 return class_instance_method_list(argc
, argv
, mod
, ins_methods_i
);
649 * mod.protected_instance_methods(include_super=true) => array
651 * Returns a list of the protected instance methods defined in
652 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
653 * methods of any ancestors are included.
657 rb_class_protected_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
659 return class_instance_method_list(argc
, argv
, mod
, ins_methods_prot_i
);
664 * mod.private_instance_methods(include_super=true) => array
666 * Returns a list of the private instance methods defined in
667 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
668 * methods of any ancestors are included.
675 * Mod.instance_methods #=> [:method2]
676 * Mod.private_instance_methods #=> [:method1]
680 rb_class_private_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
682 return class_instance_method_list(argc
, argv
, mod
, ins_methods_priv_i
);
687 * mod.public_instance_methods(include_super=true) => array
689 * Returns a list of the public instance methods defined in <i>mod</i>.
690 * If the optional parameter is not <code>false</code>, the methods of
691 * any ancestors are included.
695 rb_class_public_instance_methods(int argc
, VALUE
*argv
, VALUE mod
)
697 return class_instance_method_list(argc
, argv
, mod
, ins_methods_pub_i
);
702 * obj.singleton_methods(all=true) => array
704 * Returns an array of the names of singleton methods for <i>obj</i>.
705 * If the optional <i>all</i> parameter is true, the list will include
706 * methods in modules included in <i>obj</i>.
713 * def Single.four() end
727 * Single.singleton_methods #=> [:four]
728 * a.singleton_methods(false) #=> [:two, :one]
729 * a.singleton_methods #=> [:two, :one, :three]
733 rb_obj_singleton_methods(int argc
, VALUE
*argv
, VALUE obj
)
735 VALUE recur
, ary
, klass
;
742 rb_scan_args(argc
, argv
, "01", &recur
);
744 klass
= CLASS_OF(obj
);
745 list
= st_init_numtable();
746 if (klass
&& FL_TEST(klass
, FL_SINGLETON
)) {
747 st_foreach(RCLASS_M_TBL(klass
), method_entry
, (st_data_t
)list
);
748 klass
= RCLASS_SUPER(klass
);
751 while (klass
&& (FL_TEST(klass
, FL_SINGLETON
) || TYPE(klass
) == T_ICLASS
)) {
752 st_foreach(RCLASS_M_TBL(klass
), method_entry
, (st_data_t
)list
);
753 klass
= RCLASS_SUPER(klass
);
757 st_foreach(list
, ins_methods_i
, ary
);
764 rb_define_method_id(VALUE klass
, ID name
, VALUE (*func
)(ANYARGS
), int argc
)
766 rb_add_method(klass
, name
, NEW_CFUNC(func
,argc
), NOEX_PUBLIC
);
770 rb_define_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
772 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PUBLIC
);
776 rb_define_protected_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
778 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PROTECTED
);
782 rb_define_private_method(VALUE klass
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
784 rb_add_method(klass
, rb_intern(name
), NEW_CFUNC(func
, argc
), NOEX_PRIVATE
);
788 rb_undef_method(VALUE klass
, const char *name
)
790 rb_add_method(klass
, rb_intern(name
), 0, NOEX_UNDEF
);
793 #define SPECIAL_SINGLETON(x,c) do {\
800 rb_singleton_class(VALUE obj
)
804 if (FIXNUM_P(obj
) || SYMBOL_P(obj
)) {
805 rb_raise(rb_eTypeError
, "can't define singleton");
807 if (rb_special_const_p(obj
)) {
808 SPECIAL_SINGLETON(Qnil
, rb_cNilClass
);
809 SPECIAL_SINGLETON(Qfalse
, rb_cFalseClass
);
810 SPECIAL_SINGLETON(Qtrue
, rb_cTrueClass
);
811 rb_bug("unknown immediate %ld", obj
);
815 if (FL_TEST(RBASIC(obj
)->klass
, FL_SINGLETON
) &&
816 rb_iv_get(RBASIC(obj
)->klass
, "__attached__") == obj
) {
817 klass
= RBASIC(obj
)->klass
;
820 klass
= rb_make_metaclass(obj
, RBASIC(obj
)->klass
);
822 if (OBJ_TAINTED(obj
)) {
826 FL_UNSET(klass
, FL_TAINT
);
828 if (OBJ_FROZEN(obj
)) OBJ_FREEZE(klass
);
835 rb_define_singleton_method(VALUE obj
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
837 rb_define_method(rb_singleton_class(obj
), name
, func
, argc
);
841 rb_define_module_function(VALUE module
, const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
843 rb_define_private_method(module
, name
, func
, argc
);
844 rb_define_singleton_method(module
, name
, func
, argc
);
848 rb_define_global_function(const char *name
, VALUE (*func
)(ANYARGS
), int argc
)
850 rb_define_module_function(rb_mKernel
, name
, func
, argc
);
854 rb_define_alias(VALUE klass
, const char *name1
, const char *name2
)
856 rb_alias(klass
, rb_intern(name1
), rb_intern(name2
));
860 rb_define_attr(VALUE klass
, const char *name
, int read
, int write
)
862 rb_attr(klass
, rb_intern(name
), read
, write
, Qfalse
);
868 rb_scan_args(int argc
, const VALUE
*argv
, const char *fmt
, ...)
875 va_start(vargs
, fmt
);
877 if (*p
== '*') goto rest_arg
;
882 rb_raise(rb_eArgError
, "wrong number of arguments (%d for %d)", argc
, n
);
883 for (i
=0; i
<n
; i
++) {
884 var
= va_arg(vargs
, VALUE
*);
885 if (var
) *var
= argv
[i
];
896 var
= va_arg(vargs
, VALUE
*);
898 if (var
) *var
= argv
[i
];
901 if (var
) *var
= Qnil
;
909 var
= va_arg(vargs
, VALUE
*);
911 if (var
) *var
= rb_ary_new4(argc
-i
, argv
+i
);
915 if (var
) *var
= rb_ary_new();
921 var
= va_arg(vargs
, VALUE
*);
922 if (rb_block_given_p()) {
923 *var
= rb_block_proc();
937 rb_raise(rb_eArgError
, "wrong number of arguments (%d for %d)", argc
, i
);
943 rb_fatal("bad scan arg format: %s", fmt
);