Ruby  1.9.3p385(2013-02-06revision39114)
class.c
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   class.c -
00004 
00005   $Author: usa $
00006   created at: Tue Aug 10 15:05:44 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009 
00010 **********************************************************************/
00011 
00026 #include "ruby/ruby.h"
00027 #include "ruby/st.h"
00028 #include "method.h"
00029 #include "constant.h"
00030 #include "vm_core.h"
00031 #include "internal.h"
00032 #include <ctype.h>
00033 
00034 extern st_table *rb_class_tbl;
00035 static ID id_attached;
00036 
00049 static VALUE
00050 class_alloc(VALUE flags, VALUE klass)
00051 {
00052     rb_classext_t *ext = ALLOC(rb_classext_t);
00053     NEWOBJ(obj, struct RClass);
00054     OBJSETUP(obj, klass, flags);
00055     obj->ptr = ext;
00056     RCLASS_IV_TBL(obj) = 0;
00057     RCLASS_CONST_TBL(obj) = 0;
00058     RCLASS_M_TBL(obj) = 0;
00059     RCLASS_SUPER(obj) = 0;
00060     RCLASS_IV_INDEX_TBL(obj) = 0;
00061     return (VALUE)obj;
00062 }
00063 
00064 
00074 VALUE
00075 rb_class_boot(VALUE super)
00076 {
00077     VALUE klass = class_alloc(T_CLASS, rb_cClass);
00078 
00079     RCLASS_SUPER(klass) = super;
00080     RCLASS_M_TBL(klass) = st_init_numtable();
00081 
00082     OBJ_INFECT(klass, super);
00083     return (VALUE)klass;
00084 }
00085 
00086 
00093 void
00094 rb_check_inheritable(VALUE super)
00095 {
00096     if (TYPE(super) != T_CLASS) {
00097         rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
00098                  rb_obj_classname(super));
00099     }
00100     if (RBASIC(super)->flags & FL_SINGLETON) {
00101         rb_raise(rb_eTypeError, "can't make subclass of singleton class");
00102     }
00103     if (super == rb_cClass) {
00104         rb_raise(rb_eTypeError, "can't make subclass of Class");
00105     }
00106 }
00107 
00108 
00115 VALUE
00116 rb_class_new(VALUE super)
00117 {
00118     Check_Type(super, T_CLASS);
00119     rb_check_inheritable(super);
00120     return rb_class_boot(super);
00121 }
00122 
00123 struct clone_method_data {
00124     st_table *tbl;
00125     VALUE klass;
00126 };
00127 
00128 VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase);
00129 
00130 static int
00131 clone_method(ID mid, const rb_method_entry_t *me, struct clone_method_data *data)
00132 {
00133     VALUE newiseqval;
00134     if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
00135         rb_iseq_t *iseq;
00136         newiseqval = rb_iseq_clone(me->def->body.iseq->self, data->klass);
00137         GetISeqPtr(newiseqval, iseq);
00138         rb_add_method(data->klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
00139         RB_GC_GUARD(newiseqval);
00140     }
00141     else {
00142         rb_method_entry_set(data->klass, mid, me, me->flag);
00143     }
00144     return ST_CONTINUE;
00145 }
00146 
00147 static int
00148 clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
00149 {
00150     rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
00151     *nce = *ce;
00152     st_insert(tbl, key, (st_data_t)nce);
00153     return ST_CONTINUE;
00154 }
00155 
00156 static int
00157 clone_const_i(st_data_t key, st_data_t value, st_data_t data)
00158 {
00159     return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
00160 }
00161 
00162 static void
00163 class_init_copy_check(VALUE clone, VALUE orig)
00164 {
00165     if (orig == rb_cBasicObject) {
00166         rb_raise(rb_eTypeError, "can't copy the root class");
00167     }
00168     if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
00169         rb_raise(rb_eTypeError, "already initialized class");
00170     }
00171     if (FL_TEST(orig, FL_SINGLETON)) {
00172         rb_raise(rb_eTypeError, "can't copy singleton class");
00173     }
00174 }
00175 
00176 /* :nodoc: */
00177 VALUE
00178 rb_mod_init_copy(VALUE clone, VALUE orig)
00179 {
00180     if (RB_TYPE_P(clone, T_CLASS)) {
00181         class_init_copy_check(clone, orig);
00182     }
00183     rb_obj_init_copy(clone, orig);
00184     if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
00185         RBASIC(clone)->klass = rb_singleton_class_clone(orig);
00186         rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
00187     }
00188     RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
00189     if (RCLASS_IV_TBL(orig)) {
00190         st_data_t id;
00191 
00192         if (RCLASS_IV_TBL(clone)) {
00193             st_free_table(RCLASS_IV_TBL(clone));
00194         }
00195         RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
00196         CONST_ID(id, "__classpath__");
00197         st_delete(RCLASS_IV_TBL(clone), &id, 0);
00198         CONST_ID(id, "__classid__");
00199         st_delete(RCLASS_IV_TBL(clone), &id, 0);
00200     }
00201     if (RCLASS_CONST_TBL(orig)) {
00202         if (RCLASS_CONST_TBL(clone)) {
00203             rb_free_const_table(RCLASS_CONST_TBL(clone));
00204         }
00205         RCLASS_CONST_TBL(clone) = st_init_numtable();
00206         st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
00207     }
00208     if (RCLASS_M_TBL(orig)) {
00209         struct clone_method_data data;
00210 
00211         if (RCLASS_M_TBL(clone)) {
00212             rb_free_m_table(RCLASS_M_TBL(clone));
00213         }
00214         data.tbl = RCLASS_M_TBL(clone) = st_init_numtable();
00215         data.klass = clone;
00216         st_foreach(RCLASS_M_TBL(orig), clone_method,
00217                    (st_data_t)&data);
00218     }
00219 
00220     return clone;
00221 }
00222 
00223 VALUE
00224 rb_singleton_class_clone(VALUE obj)
00225 {
00226     VALUE klass = RBASIC(obj)->klass;
00227 
00228     if (!FL_TEST(klass, FL_SINGLETON))
00229         return klass;
00230     else {
00231         struct clone_method_data data;
00232         /* copy singleton(unnamed) class */
00233         VALUE clone = class_alloc((RBASIC(klass)->flags & ~(FL_MARK)), 0);
00234 
00235         if (BUILTIN_TYPE(obj) == T_CLASS) {
00236             RBASIC(clone)->klass = (VALUE)clone;
00237         }
00238         else {
00239             RBASIC(clone)->klass = rb_singleton_class_clone(klass);
00240         }
00241 
00242         RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
00243         if (RCLASS_IV_TBL(klass)) {
00244             RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
00245         }
00246         if (RCLASS_CONST_TBL(klass)) {
00247             RCLASS_CONST_TBL(clone) = st_init_numtable();
00248             st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
00249         }
00250         RCLASS_M_TBL(clone) = st_init_numtable();
00251         data.tbl = RCLASS_M_TBL(clone);
00252         data.klass = (VALUE)clone;
00253         st_foreach(RCLASS_M_TBL(klass), clone_method,
00254                    (st_data_t)&data);
00255         rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
00256         FL_SET(clone, FL_SINGLETON);
00257         return (VALUE)clone;
00258     }
00259 }
00260 
00265 void
00266 rb_singleton_class_attached(VALUE klass, VALUE obj)
00267 {
00268     if (FL_TEST(klass, FL_SINGLETON)) {
00269         if (!RCLASS_IV_TBL(klass)) {
00270             RCLASS_IV_TBL(klass) = st_init_numtable();
00271         }
00272         st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
00273     }
00274 }
00275 
00276 
00277 
00278 #define METACLASS_OF(k) RBASIC(k)->klass
00279 
00285 #define META_CLASS_OF_CLASS_CLASS_P(k)  (METACLASS_OF(k) == (k))
00286 
00287 
00295 #define ENSURE_EIGENCLASS(klass) \
00296  (rb_ivar_get(METACLASS_OF(klass), id_attached) == (klass) ? METACLASS_OF(klass) : make_metaclass(klass))
00297 
00298 
00308 static inline VALUE
00309 make_metaclass(VALUE klass)
00310 {
00311     VALUE super;
00312     VALUE metaclass = rb_class_boot(Qundef);
00313 
00314     FL_SET(metaclass, FL_SINGLETON);
00315     rb_singleton_class_attached(metaclass, klass);
00316 
00317     if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
00318         METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
00319     }
00320     else {
00321         VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
00322         METACLASS_OF(klass) = metaclass;
00323         METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
00324     }
00325 
00326     super = RCLASS_SUPER(klass);
00327     while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
00328     RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
00329 
00330     OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
00331 
00332     return metaclass;
00333 }
00334 
00341 static inline VALUE
00342 make_singleton_class(VALUE obj)
00343 {
00344     VALUE orig_class = RBASIC(obj)->klass;
00345     VALUE klass = rb_class_boot(orig_class);
00346 
00347     FL_SET(klass, FL_SINGLETON);
00348     RBASIC(obj)->klass = klass;
00349     rb_singleton_class_attached(klass, obj);
00350 
00351     METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
00352     return klass;
00353 }
00354 
00355 
00356 static VALUE
00357 boot_defclass(const char *name, VALUE super)
00358 {
00359     extern st_table *rb_class_tbl;
00360     VALUE obj = rb_class_boot(super);
00361     ID id = rb_intern(name);
00362 
00363     rb_name_class(obj, id);
00364     st_add_direct(rb_class_tbl, id, obj);
00365     rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
00366     return obj;
00367 }
00368 
00369 void
00370 Init_class_hierarchy(void)
00371 {
00372     id_attached = rb_intern("__attached__");
00373 
00374     rb_cBasicObject = boot_defclass("BasicObject", 0);
00375     rb_cObject = boot_defclass("Object", rb_cBasicObject);
00376     rb_cModule = boot_defclass("Module", rb_cObject);
00377     rb_cClass =  boot_defclass("Class",  rb_cModule);
00378 
00379     rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
00380     RBASIC(rb_cClass)->klass
00381         = RBASIC(rb_cModule)->klass
00382         = RBASIC(rb_cObject)->klass
00383         = RBASIC(rb_cBasicObject)->klass
00384         = rb_cClass;
00385 }
00386 
00387 
00398 VALUE
00399 rb_make_metaclass(VALUE obj, VALUE unused)
00400 {
00401     if (BUILTIN_TYPE(obj) == T_CLASS) {
00402         return make_metaclass(obj);
00403     }
00404     else {
00405         return make_singleton_class(obj);
00406     }
00407 }
00408 
00409 
00420 VALUE
00421 rb_define_class_id(ID id, VALUE super)
00422 {
00423     VALUE klass;
00424 
00425     if (!super) super = rb_cObject;
00426     klass = rb_class_new(super);
00427     rb_make_metaclass(klass, RBASIC(super)->klass);
00428 
00429     return klass;
00430 }
00431 
00432 
00441 VALUE
00442 rb_class_inherited(VALUE super, VALUE klass)
00443 {
00444     ID inherited;
00445     if (!super) super = rb_cObject;
00446     CONST_ID(inherited, "inherited");
00447     return rb_funcall(super, inherited, 1, klass);
00448 }
00449 
00450 
00451 
00467 VALUE
00468 rb_define_class(const char *name, VALUE super)
00469 {
00470     VALUE klass;
00471     ID id;
00472 
00473     id = rb_intern(name);
00474     if (rb_const_defined(rb_cObject, id)) {
00475         klass = rb_const_get(rb_cObject, id);
00476         if (TYPE(klass) != T_CLASS) {
00477             rb_raise(rb_eTypeError, "%s is not a class", name);
00478         }
00479         if (rb_class_real(RCLASS_SUPER(klass)) != super) {
00480             rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
00481         }
00482         return klass;
00483     }
00484     if (!super) {
00485         rb_warn("no super class for `%s', Object assumed", name);
00486     }
00487     klass = rb_define_class_id(id, super);
00488     st_add_direct(rb_class_tbl, id, klass);
00489     rb_name_class(klass, id);
00490     rb_const_set(rb_cObject, id, klass);
00491     rb_class_inherited(super, klass);
00492 
00493     return klass;
00494 }
00495 
00496 
00513 VALUE
00514 rb_define_class_under(VALUE outer, const char *name, VALUE super)
00515 {
00516     return rb_define_class_id_under(outer, rb_intern(name), super);
00517 }
00518 
00519 
00536 VALUE
00537 rb_define_class_id_under(VALUE outer, ID id, VALUE super)
00538 {
00539     VALUE klass;
00540 
00541     if (rb_const_defined_at(outer, id)) {
00542         klass = rb_const_get_at(outer, id);
00543         if (TYPE(klass) != T_CLASS) {
00544             rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
00545         }
00546         if (rb_class_real(RCLASS_SUPER(klass)) != super) {
00547             rb_name_error(id, "%s is already defined", rb_id2name(id));
00548         }
00549         return klass;
00550     }
00551     if (!super) {
00552         rb_warn("no super class for `%s::%s', Object assumed",
00553                 rb_class2name(outer), rb_id2name(id));
00554     }
00555     klass = rb_define_class_id(id, super);
00556     rb_set_class_path_string(klass, outer, rb_id2str(id));
00557     rb_const_set(outer, id, klass);
00558     rb_class_inherited(super, klass);
00559     rb_gc_register_mark_object(klass);
00560 
00561     return klass;
00562 }
00563 
00564 VALUE
00565 rb_module_new(void)
00566 {
00567     VALUE mdl = class_alloc(T_MODULE, rb_cModule);
00568 
00569     RCLASS_M_TBL(mdl) = st_init_numtable();
00570 
00571     return (VALUE)mdl;
00572 }
00573 
00574 VALUE
00575 rb_define_module_id(ID id)
00576 {
00577     VALUE mdl;
00578 
00579     mdl = rb_module_new();
00580     rb_name_class(mdl, id);
00581 
00582     return mdl;
00583 }
00584 
00585 VALUE
00586 rb_define_module(const char *name)
00587 {
00588     VALUE module;
00589     ID id;
00590 
00591     id = rb_intern(name);
00592     if (rb_const_defined(rb_cObject, id)) {
00593         module = rb_const_get(rb_cObject, id);
00594         if (TYPE(module) == T_MODULE)
00595             return module;
00596         rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
00597     }
00598     module = rb_define_module_id(id);
00599     st_add_direct(rb_class_tbl, id, module);
00600     rb_const_set(rb_cObject, id, module);
00601 
00602     return module;
00603 }
00604 
00605 VALUE
00606 rb_define_module_under(VALUE outer, const char *name)
00607 {
00608     return rb_define_module_id_under(outer, rb_intern(name));
00609 }
00610 
00611 VALUE
00612 rb_define_module_id_under(VALUE outer, ID id)
00613 {
00614     VALUE module;
00615 
00616     if (rb_const_defined_at(outer, id)) {
00617         module = rb_const_get_at(outer, id);
00618         if (TYPE(module) == T_MODULE)
00619             return module;
00620         rb_raise(rb_eTypeError, "%s::%s is not a module",
00621                  rb_class2name(outer), rb_obj_classname(module));
00622     }
00623     module = rb_define_module_id(id);
00624     rb_const_set(outer, id, module);
00625     rb_set_class_path_string(module, outer, rb_id2str(id));
00626     rb_gc_register_mark_object(module);
00627 
00628     return module;
00629 }
00630 
00631 static VALUE
00632 include_class_new(VALUE module, VALUE super)
00633 {
00634     VALUE klass = class_alloc(T_ICLASS, rb_cClass);
00635 
00636     if (BUILTIN_TYPE(module) == T_ICLASS) {
00637         module = RBASIC(module)->klass;
00638     }
00639     if (!RCLASS_IV_TBL(module)) {
00640         RCLASS_IV_TBL(module) = st_init_numtable();
00641     }
00642     if (!RCLASS_CONST_TBL(module)) {
00643         RCLASS_CONST_TBL(module) = st_init_numtable();
00644     }
00645     RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
00646     RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
00647     RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
00648     RCLASS_SUPER(klass) = super;
00649     if (TYPE(module) == T_ICLASS) {
00650         RBASIC(klass)->klass = RBASIC(module)->klass;
00651     }
00652     else {
00653         RBASIC(klass)->klass = module;
00654     }
00655     OBJ_INFECT(klass, module);
00656     OBJ_INFECT(klass, super);
00657 
00658     return (VALUE)klass;
00659 }
00660 
00661 void
00662 rb_include_module(VALUE klass, VALUE module)
00663 {
00664     VALUE p, c;
00665     int changed = 0;
00666 
00667     rb_frozen_class_p(klass);
00668     if (!OBJ_UNTRUSTED(klass)) {
00669         rb_secure(4);
00670     }
00671 
00672     if (TYPE(module) != T_MODULE) {
00673         Check_Type(module, T_MODULE);
00674     }
00675 
00676     OBJ_INFECT(klass, module);
00677     c = klass;
00678     while (module) {
00679         int superclass_seen = FALSE;
00680 
00681         if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
00682             rb_raise(rb_eArgError, "cyclic include detected");
00683         /* ignore if the module included already in superclasses */
00684         for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
00685             switch (BUILTIN_TYPE(p)) {
00686               case T_ICLASS:
00687                 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
00688                     if (!superclass_seen) {
00689                         c = p;  /* move insertion point */
00690                     }
00691                     goto skip;
00692                 }
00693                 break;
00694               case T_CLASS:
00695                 superclass_seen = TRUE;
00696                 break;
00697             }
00698         }
00699         c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
00700         if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
00701             changed = 1;
00702       skip:
00703         module = RCLASS_SUPER(module);
00704     }
00705     if (changed) rb_clear_cache();
00706 }
00707 
00708 /*
00709  *  call-seq:
00710  *     mod.included_modules -> array
00711  *
00712  *  Returns the list of modules included in <i>mod</i>.
00713  *
00714  *     module Mixin
00715  *     end
00716  *
00717  *     module Outer
00718  *       include Mixin
00719  *     end
00720  *
00721  *     Mixin.included_modules   #=> []
00722  *     Outer.included_modules   #=> [Mixin]
00723  */
00724 
00725 VALUE
00726 rb_mod_included_modules(VALUE mod)
00727 {
00728     VALUE ary = rb_ary_new();
00729     VALUE p;
00730 
00731     for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
00732         if (BUILTIN_TYPE(p) == T_ICLASS) {
00733             rb_ary_push(ary, RBASIC(p)->klass);
00734         }
00735     }
00736     return ary;
00737 }
00738 
00739 /*
00740  *  call-seq:
00741  *     mod.include?(module)    -> true or false
00742  *
00743  *  Returns <code>true</code> if <i>module</i> is included in
00744  *  <i>mod</i> or one of <i>mod</i>'s ancestors.
00745  *
00746  *     module A
00747  *     end
00748  *     class B
00749  *       include A
00750  *     end
00751  *     class C < B
00752  *     end
00753  *     B.include?(A)   #=> true
00754  *     C.include?(A)   #=> true
00755  *     A.include?(A)   #=> false
00756  */
00757 
00758 VALUE
00759 rb_mod_include_p(VALUE mod, VALUE mod2)
00760 {
00761     VALUE p;
00762 
00763     Check_Type(mod2, T_MODULE);
00764     for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
00765         if (BUILTIN_TYPE(p) == T_ICLASS) {
00766             if (RBASIC(p)->klass == mod2) return Qtrue;
00767         }
00768     }
00769     return Qfalse;
00770 }
00771 
00772 /*
00773  *  call-seq:
00774  *     mod.ancestors -> array
00775  *
00776  *  Returns a list of modules included in <i>mod</i> (including
00777  *  <i>mod</i> itself).
00778  *
00779  *     module Mod
00780  *       include Math
00781  *       include Comparable
00782  *     end
00783  *
00784  *     Mod.ancestors    #=> [Mod, Comparable, Math]
00785  *     Math.ancestors   #=> [Math]
00786  */
00787 
00788 VALUE
00789 rb_mod_ancestors(VALUE mod)
00790 {
00791     VALUE p, ary = rb_ary_new();
00792 
00793     for (p = mod; p; p = RCLASS_SUPER(p)) {
00794         if (FL_TEST(p, FL_SINGLETON))
00795             continue;
00796         if (BUILTIN_TYPE(p) == T_ICLASS) {
00797             rb_ary_push(ary, RBASIC(p)->klass);
00798         }
00799         else {
00800             rb_ary_push(ary, p);
00801         }
00802     }
00803     return ary;
00804 }
00805 
00806 #define VISI(x) ((x)&NOEX_MASK)
00807 #define VISI_CHECK(x,f) (VISI(x) == (f))
00808 
00809 static int
00810 ins_methods_push(ID name, long type, VALUE ary, long visi)
00811 {
00812     if (type == -1) return ST_CONTINUE;
00813 
00814     switch (visi) {
00815       case NOEX_PRIVATE:
00816       case NOEX_PROTECTED:
00817       case NOEX_PUBLIC:
00818         visi = (type == visi);
00819         break;
00820       default:
00821         visi = (type != NOEX_PRIVATE);
00822         break;
00823     }
00824     if (visi) {
00825         rb_ary_push(ary, ID2SYM(name));
00826     }
00827     return ST_CONTINUE;
00828 }
00829 
00830 static int
00831 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
00832 {
00833     return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
00834 }
00835 
00836 static int
00837 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
00838 {
00839     return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
00840 }
00841 
00842 static int
00843 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
00844 {
00845     return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
00846 }
00847 
00848 static int
00849 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
00850 {
00851     return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
00852 }
00853 
00854 static int
00855 method_entry_i(st_data_t key, st_data_t value, st_data_t data)
00856 {
00857     const rb_method_entry_t *me = (const rb_method_entry_t *)value;
00858     st_table *list = (st_table *)data;
00859     long type;
00860 
00861     if ((ID)key == ID_ALLOCATOR) {
00862         return ST_CONTINUE;
00863     }
00864 
00865     if (!st_lookup(list, key, 0)) {
00866         if (UNDEFINED_METHOD_ENTRY_P(me)) {
00867             type = -1; /* none */
00868         }
00869         else {
00870             type = VISI(me->flag);
00871         }
00872         st_add_direct(list, key, type);
00873     }
00874     return ST_CONTINUE;
00875 }
00876 
00877 static VALUE
00878 class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
00879 {
00880     VALUE ary;
00881     int recur;
00882     st_table *list;
00883 
00884     if (argc == 0) {
00885         recur = TRUE;
00886     }
00887     else {
00888         VALUE r;
00889         rb_scan_args(argc, argv, "01", &r);
00890         recur = RTEST(r);
00891     }
00892 
00893     list = st_init_numtable();
00894     for (; mod; mod = RCLASS_SUPER(mod)) {
00895         st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list);
00896         if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
00897         if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
00898         if (!recur) break;
00899     }
00900     ary = rb_ary_new();
00901     st_foreach(list, func, ary);
00902     st_free_table(list);
00903 
00904     return ary;
00905 }
00906 
00907 /*
00908  *  call-seq:
00909  *     mod.instance_methods(include_super=true)   -> array
00910  *
00911  *  Returns an array containing the names of the public and protected instance
00912  *  methods in the receiver. For a module, these are the public and protected methods;
00913  *  for a class, they are the instance (not singleton) methods. With no
00914  *  argument, or with an argument that is <code>false</code>, the
00915  *  instance methods in <i>mod</i> are returned, otherwise the methods
00916  *  in <i>mod</i> and <i>mod</i>'s superclasses are returned.
00917  *
00918  *     module A
00919  *       def method1()  end
00920  *     end
00921  *     class B
00922  *       def method2()  end
00923  *     end
00924  *     class C < B
00925  *       def method3()  end
00926  *     end
00927  *
00928  *     A.instance_methods                #=> [:method1]
00929  *     B.instance_methods(false)         #=> [:method2]
00930  *     C.instance_methods(false)         #=> [:method3]
00931  *     C.instance_methods(true).length   #=> 43
00932  */
00933 
00934 VALUE
00935 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
00936 {
00937     return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
00938 }
00939 
00940 /*
00941  *  call-seq:
00942  *     mod.protected_instance_methods(include_super=true)   -> array
00943  *
00944  *  Returns a list of the protected instance methods defined in
00945  *  <i>mod</i>. If the optional parameter is not <code>false</code>, the
00946  *  methods of any ancestors are included.
00947  */
00948 
00949 VALUE
00950 rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
00951 {
00952     return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
00953 }
00954 
00955 /*
00956  *  call-seq:
00957  *     mod.private_instance_methods(include_super=true)    -> array
00958  *
00959  *  Returns a list of the private instance methods defined in
00960  *  <i>mod</i>. If the optional parameter is not <code>false</code>, the
00961  *  methods of any ancestors are included.
00962  *
00963  *     module Mod
00964  *       def method1()  end
00965  *       private :method1
00966  *       def method2()  end
00967  *     end
00968  *     Mod.instance_methods           #=> [:method2]
00969  *     Mod.private_instance_methods   #=> [:method1]
00970  */
00971 
00972 VALUE
00973 rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
00974 {
00975     return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
00976 }
00977 
00978 /*
00979  *  call-seq:
00980  *     mod.public_instance_methods(include_super=true)   -> array
00981  *
00982  *  Returns a list of the public instance methods defined in <i>mod</i>.
00983  *  If the optional parameter is not <code>false</code>, the methods of
00984  *  any ancestors are included.
00985  */
00986 
00987 VALUE
00988 rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
00989 {
00990     return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
00991 }
00992 
00993 /*
00994  *  call-seq:
00995  *     obj.methods    -> array
00996  *
00997  *  Returns a list of the names of public and protected methods of
00998  *  <i>obj</i>. This will include all the methods accessible in
00999  *  <i>obj</i>'s ancestors.
01000  *
01001  *     class Klass
01002  *       def klass_method()
01003  *       end
01004  *     end
01005  *     k = Klass.new
01006  *     k.methods[0..9]    #=> [:klass_method, :nil?, :===,
01007  *                        #    :==~, :!, :eql?
01008  *                        #    :hash, :<=>, :class, :singleton_class]
01009  *     k.methods.length   #=> 57
01010  */
01011 
01012 VALUE
01013 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
01014 {
01015   retry:
01016     if (argc == 0) {
01017         VALUE args[1];
01018 
01019         args[0] = Qtrue;
01020         return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
01021     }
01022     else {
01023         VALUE recur;
01024 
01025         rb_scan_args(argc, argv, "1", &recur);
01026         if (RTEST(recur)) {
01027             argc = 0;
01028             goto retry;
01029         }
01030         return rb_obj_singleton_methods(argc, argv, obj);
01031     }
01032 }
01033 
01034 /*
01035  *  call-seq:
01036  *     obj.protected_methods(all=true)   -> array
01037  *
01038  *  Returns the list of protected methods accessible to <i>obj</i>. If
01039  *  the <i>all</i> parameter is set to <code>false</code>, only those methods
01040  *  in the receiver will be listed.
01041  */
01042 
01043 VALUE
01044 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
01045 {
01046     return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
01047 }
01048 
01049 /*
01050  *  call-seq:
01051  *     obj.private_methods(all=true)   -> array
01052  *
01053  *  Returns the list of private methods accessible to <i>obj</i>. If
01054  *  the <i>all</i> parameter is set to <code>false</code>, only those methods
01055  *  in the receiver will be listed.
01056  */
01057 
01058 VALUE
01059 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
01060 {
01061     return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
01062 }
01063 
01064 /*
01065  *  call-seq:
01066  *     obj.public_methods(all=true)   -> array
01067  *
01068  *  Returns the list of public methods accessible to <i>obj</i>. If
01069  *  the <i>all</i> parameter is set to <code>false</code>, only those methods
01070  *  in the receiver will be listed.
01071  */
01072 
01073 VALUE
01074 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
01075 {
01076     return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
01077 }
01078 
01079 /*
01080  *  call-seq:
01081  *     obj.singleton_methods(all=true)    -> array
01082  *
01083  *  Returns an array of the names of singleton methods for <i>obj</i>.
01084  *  If the optional <i>all</i> parameter is true, the list will include
01085  *  methods in modules included in <i>obj</i>.
01086  *  Only public and protected singleton methods are returned.
01087  *
01088  *     module Other
01089  *       def three() end
01090  *     end
01091  *
01092  *     class Single
01093  *       def Single.four() end
01094  *     end
01095  *
01096  *     a = Single.new
01097  *
01098  *     def a.one()
01099  *     end
01100  *
01101  *     class << a
01102  *       include Other
01103  *       def two()
01104  *       end
01105  *     end
01106  *
01107  *     Single.singleton_methods    #=> [:four]
01108  *     a.singleton_methods(false)  #=> [:two, :one]
01109  *     a.singleton_methods         #=> [:two, :one, :three]
01110  */
01111 
01112 VALUE
01113 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
01114 {
01115     VALUE recur, ary, klass;
01116     st_table *list;
01117 
01118     if (argc == 0) {
01119         recur = Qtrue;
01120     }
01121     else {
01122         rb_scan_args(argc, argv, "01", &recur);
01123     }
01124     klass = CLASS_OF(obj);
01125     list = st_init_numtable();
01126     if (klass && FL_TEST(klass, FL_SINGLETON)) {
01127         st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
01128         klass = RCLASS_SUPER(klass);
01129     }
01130     if (RTEST(recur)) {
01131         while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
01132             st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
01133             klass = RCLASS_SUPER(klass);
01134         }
01135     }
01136     ary = rb_ary_new();
01137     st_foreach(list, ins_methods_i, ary);
01138     st_free_table(list);
01139 
01140     return ary;
01141 }
01142 
01200 void
01201 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
01202 {
01203     rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
01204 }
01205 
01206 void
01207 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
01208 {
01209     rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
01210 }
01211 
01212 void
01213 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
01214 {
01215     rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
01216 }
01217 
01218 void
01219 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
01220 {
01221     rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
01222 }
01223 
01224 void
01225 rb_undef_method(VALUE klass, const char *name)
01226 {
01227     rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
01228 }
01229 
01238 #define SPECIAL_SINGLETON(x,c) do {\
01239     if (obj == (x)) {\
01240         return (c);\
01241     }\
01242 } while (0)
01243 
01244 
01254 static VALUE
01255 singleton_class_of(VALUE obj)
01256 {
01257     VALUE klass;
01258 
01259     if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
01260         rb_raise(rb_eTypeError, "can't define singleton");
01261     }
01262     if (rb_special_const_p(obj)) {
01263         SPECIAL_SINGLETON(Qnil, rb_cNilClass);
01264         SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
01265         SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
01266         rb_bug("unknown immediate %p", (void *)obj);
01267     }
01268 
01269     if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
01270         rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
01271         klass = RBASIC(obj)->klass;
01272     }
01273     else {
01274         klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
01275     }
01276 
01277     if (OBJ_TAINTED(obj)) {
01278         OBJ_TAINT(klass);
01279     }
01280     else {
01281         FL_UNSET(klass, FL_TAINT);
01282     }
01283     if (OBJ_UNTRUSTED(obj)) {
01284         OBJ_UNTRUST(klass);
01285     }
01286     else {
01287         FL_UNSET(klass, FL_UNTRUSTED);
01288     }
01289     if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
01290 
01291     return klass;
01292 }
01293 
01294 
01312 VALUE
01313 rb_singleton_class(VALUE obj)
01314 {
01315     VALUE klass = singleton_class_of(obj);
01316 
01317     /* ensures an exposed class belongs to its own eigenclass */
01318     if (TYPE(obj) == T_CLASS) (void)ENSURE_EIGENCLASS(klass);
01319 
01320     return klass;
01321 }
01322 
01339 void
01340 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
01341 {
01342     rb_define_method(singleton_class_of(obj), name, func, argc);
01343 }
01344 
01345 
01346 
01354 void
01355 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
01356 {
01357     rb_define_private_method(module, name, func, argc);
01358     rb_define_singleton_method(module, name, func, argc);
01359 }
01360 
01361 
01368 void
01369 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
01370 {
01371     rb_define_module_function(rb_mKernel, name, func, argc);
01372 }
01373 
01374 
01381 void
01382 rb_define_alias(VALUE klass, const char *name1, const char *name2)
01383 {
01384     rb_alias(klass, rb_intern(name1), rb_intern(name2));
01385 }
01386 
01394 void
01395 rb_define_attr(VALUE klass, const char *name, int read, int write)
01396 {
01397     rb_attr(klass, rb_intern(name), read, write, FALSE);
01398 }
01399 
01400 int
01401 rb_obj_basic_to_s_p(VALUE obj)
01402 {
01403     const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
01404     if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
01405         me->def->body.cfunc.func == rb_any_to_s)
01406         return 1;
01407     return 0;
01408 }
01409 
01410 #include <stdarg.h>
01411 
01412 int
01413 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
01414 {
01415     int i;
01416     const char *p = fmt;
01417     VALUE *var;
01418     va_list vargs;
01419     int f_var = 0, f_hash = 0, f_block = 0;
01420     int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
01421     int argi = 0;
01422     VALUE hash = Qnil;
01423 
01424     if (ISDIGIT(*p)) {
01425         n_lead = *p - '0';
01426         p++;
01427         if (ISDIGIT(*p)) {
01428             n_opt = *p - '0';
01429             p++;
01430             if (ISDIGIT(*p)) {
01431                 n_trail = *p - '0';
01432                 p++;
01433                 goto block_arg;
01434             }
01435         }
01436     }
01437     if (*p == '*') {
01438         f_var = 1;
01439         p++;
01440         if (ISDIGIT(*p)) {
01441             n_trail = *p - '0';
01442             p++;
01443         }
01444     }
01445   block_arg:
01446     if (*p == ':') {
01447         f_hash = 1;
01448         p++;
01449     }
01450     if (*p == '&') {
01451         f_block = 1;
01452         p++;
01453     }
01454     if (*p != '\0') {
01455         rb_fatal("bad scan arg format: %s", fmt);
01456     }
01457     n_mand = n_lead + n_trail;
01458 
01459     if (argc < n_mand)
01460         goto argc_error;
01461 
01462     va_start(vargs, fmt);
01463 
01464     /* capture an option hash - phase 1: pop */
01465     if (f_hash && n_mand < argc) {
01466         VALUE last = argv[argc - 1];
01467 
01468         if (NIL_P(last)) {
01469             /* nil is taken as an empty option hash only if it is not
01470                ambiguous; i.e. '*' is not specified and arguments are
01471                given more than sufficient */
01472             if (!f_var && n_mand + n_opt < argc)
01473                 argc--;
01474         }
01475         else {
01476             hash = rb_check_convert_type(last, T_HASH, "Hash", "to_hash");
01477             if (!NIL_P(hash))
01478                 argc--;
01479         }
01480     }
01481     /* capture leading mandatory arguments */
01482     for (i = n_lead; i-- > 0; ) {
01483         var = va_arg(vargs, VALUE *);
01484         if (var) *var = argv[argi];
01485         argi++;
01486     }
01487     /* capture optional arguments */
01488     for (i = n_opt; i-- > 0; ) {
01489         var = va_arg(vargs, VALUE *);
01490         if (argi < argc - n_trail) {
01491             if (var) *var = argv[argi];
01492             argi++;
01493         }
01494         else {
01495             if (var) *var = Qnil;
01496         }
01497     }
01498     /* capture variable length arguments */
01499     if (f_var) {
01500         int n_var = argc - argi - n_trail;
01501 
01502         var = va_arg(vargs, VALUE *);
01503         if (0 < n_var) {
01504             if (var) *var = rb_ary_new4(n_var, &argv[argi]);
01505             argi += n_var;
01506         }
01507         else {
01508             if (var) *var = rb_ary_new();
01509         }
01510     }
01511     /* capture trailing mandatory arguments */
01512     for (i = n_trail; i-- > 0; ) {
01513         var = va_arg(vargs, VALUE *);
01514         if (var) *var = argv[argi];
01515         argi++;
01516     }
01517     /* capture an option hash - phase 2: assignment */
01518     if (f_hash) {
01519         var = va_arg(vargs, VALUE *);
01520         if (var) *var = hash;
01521     }
01522     /* capture iterator block */
01523     if (f_block) {
01524         var = va_arg(vargs, VALUE *);
01525         if (rb_block_given_p()) {
01526             *var = rb_block_proc();
01527         }
01528         else {
01529             *var = Qnil;
01530         }
01531     }
01532     va_end(vargs);
01533 
01534     if (argi < argc)
01535         goto argc_error;
01536 
01537     return argc;
01538 
01539   argc_error:
01540     if (0 < n_opt)
01541         rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)",
01542                  argc, n_mand, n_mand + n_opt, f_var ? "+" : "");
01543     else
01544         rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)",
01545                  argc, n_mand, f_var ? "+" : "");
01546 }
01547