Ruby  1.9.3p551(2014-11-13revision48407)
class.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  class.c -
4 
5  $Author: usa $
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 {
53  NEWOBJ(obj, struct RClass);
54  OBJSETUP(obj, klass, flags);
55  obj->ptr = ext;
56  RCLASS_IV_TBL(obj) = 0;
57  RCLASS_CONST_TBL(obj) = 0;
58  RCLASS_M_TBL(obj) = 0;
59  RCLASS_SUPER(obj) = 0;
60  RCLASS_IV_INDEX_TBL(obj) = 0;
61  return (VALUE)obj;
62 }
63 
64 
74 VALUE
76 {
78 
79  RCLASS_SUPER(klass) = super;
80  RCLASS_M_TBL(klass) = st_init_numtable();
81 
82  OBJ_INFECT(klass, super);
83  return (VALUE)klass;
84 }
85 
86 
93 void
95 {
96  if (TYPE(super) != T_CLASS) {
97  rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
98  rb_obj_classname(super));
99  }
100  if (RBASIC(super)->flags & FL_SINGLETON) {
101  rb_raise(rb_eTypeError, "can't make subclass of singleton class");
102  }
103  if (super == rb_cClass) {
104  rb_raise(rb_eTypeError, "can't make subclass of Class");
105  }
106 }
107 
108 
115 VALUE
117 {
118  Check_Type(super, T_CLASS);
119  rb_check_inheritable(super);
120  return rb_class_boot(super);
121 }
122 
126 };
127 
128 VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase);
129 
130 static int
131 clone_method(ID mid, const rb_method_entry_t *me, struct clone_method_data *data)
132 {
133  VALUE newiseqval;
134  if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
135  rb_iseq_t *iseq;
136  newiseqval = rb_iseq_clone(me->def->body.iseq->self, data->klass);
137  GetISeqPtr(newiseqval, iseq);
138  rb_add_method(data->klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
139  RB_GC_GUARD(newiseqval);
140  }
141  else {
142  rb_method_entry_set(data->klass, mid, me, me->flag);
143  }
144  return ST_CONTINUE;
145 }
146 
147 static int
149 {
151  *nce = *ce;
152  st_insert(tbl, key, (st_data_t)nce);
153  return ST_CONTINUE;
154 }
155 
156 static int
158 {
159  return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
160 }
161 
162 static void
164 {
165  if (orig == rb_cBasicObject) {
166  rb_raise(rb_eTypeError, "can't copy the root class");
167  }
168  if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
169  rb_raise(rb_eTypeError, "already initialized class");
170  }
171  if (FL_TEST(orig, FL_SINGLETON)) {
172  rb_raise(rb_eTypeError, "can't copy singleton class");
173  }
174 }
175 
176 /* :nodoc: */
177 VALUE
179 {
180  if (RB_TYPE_P(clone, T_CLASS)) {
181  class_init_copy_check(clone, orig);
182  }
183  if (clone == orig) return clone;
184  rb_obj_init_copy(clone, orig);
185  if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
186  RBASIC(clone)->klass = rb_singleton_class_clone(orig);
187  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
188  }
189  RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
190  if (RCLASS_IV_TBL(orig)) {
191  st_data_t id;
192 
193  if (RCLASS_IV_TBL(clone)) {
195  }
196  RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
197  CONST_ID(id, "__classpath__");
198  st_delete(RCLASS_IV_TBL(clone), &id, 0);
199  CONST_ID(id, "__classid__");
200  st_delete(RCLASS_IV_TBL(clone), &id, 0);
201  }
202  if (RCLASS_CONST_TBL(orig)) {
203  if (RCLASS_CONST_TBL(clone)) {
205  }
208  }
209  if (RCLASS_M_TBL(orig)) {
210  struct clone_method_data data;
211 
212  if (RCLASS_M_TBL(clone)) {
214  }
215  data.tbl = RCLASS_M_TBL(clone) = st_init_numtable();
216  data.klass = clone;
218  (st_data_t)&data);
219  }
220 
221  return clone;
222 }
223 
224 VALUE
226 {
227  VALUE klass = RBASIC(obj)->klass;
228 
229  if (!FL_TEST(klass, FL_SINGLETON))
230  return klass;
231  else {
232  struct clone_method_data data;
233  /* copy singleton(unnamed) class */
234  VALUE clone = class_alloc((RBASIC(klass)->flags & ~(FL_MARK)), 0);
235 
236  if (BUILTIN_TYPE(obj) == T_CLASS) {
237  RBASIC(clone)->klass = (VALUE)clone;
238  }
239  else {
240  RBASIC(clone)->klass = rb_singleton_class_clone(klass);
241  }
242 
243  RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
244  if (RCLASS_IV_TBL(klass)) {
245  RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
246  }
247  if (RCLASS_CONST_TBL(klass)) {
250  }
251  RCLASS_M_TBL(clone) = st_init_numtable();
252  data.tbl = RCLASS_M_TBL(clone);
253  data.klass = (VALUE)clone;
255  (st_data_t)&data);
256  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
257  FL_SET(clone, FL_SINGLETON);
258  return (VALUE)clone;
259  }
260 }
261 
266 void
268 {
269  if (FL_TEST(klass, FL_SINGLETON)) {
270  if (!RCLASS_IV_TBL(klass)) {
271  RCLASS_IV_TBL(klass) = st_init_numtable();
272  }
273  st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
274  }
275 }
276 
277 
278 
279 #define METACLASS_OF(k) RBASIC(k)->klass
280 
286 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
287 
288 
296 #define ENSURE_EIGENCLASS(klass) \
297  (rb_ivar_get(METACLASS_OF(klass), id_attached) == (klass) ? METACLASS_OF(klass) : make_metaclass(klass))
298 
299 
309 static inline VALUE
311 {
312  VALUE super;
313  VALUE metaclass = rb_class_boot(Qundef);
314 
315  FL_SET(metaclass, FL_SINGLETON);
316  rb_singleton_class_attached(metaclass, klass);
317 
318  if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
319  METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
320  }
321  else {
322  VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
323  METACLASS_OF(klass) = metaclass;
324  METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
325  }
326 
327  super = RCLASS_SUPER(klass);
328  while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
329  RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
330 
331  OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
332 
333  return metaclass;
334 }
335 
342 static inline VALUE
344 {
345  VALUE orig_class = RBASIC(obj)->klass;
346  VALUE klass = rb_class_boot(orig_class);
347 
348  FL_SET(klass, FL_SINGLETON);
349  RBASIC(obj)->klass = klass;
350  rb_singleton_class_attached(klass, obj);
351 
352  METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
353  return klass;
354 }
355 
356 
357 static VALUE
358 boot_defclass(const char *name, VALUE super)
359 {
360  extern st_table *rb_class_tbl;
361  VALUE obj = rb_class_boot(super);
362  ID id = rb_intern(name);
363 
364  rb_name_class(obj, id);
365  st_add_direct(rb_class_tbl, id, obj);
366  rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
367  return obj;
368 }
369 
370 void
372 {
373  id_attached = rb_intern("__attached__");
374 
375  rb_cBasicObject = boot_defclass("BasicObject", 0);
377  rb_cModule = boot_defclass("Module", rb_cObject);
378  rb_cClass = boot_defclass("Class", rb_cModule);
379 
381  RBASIC(rb_cClass)->klass
382  = RBASIC(rb_cModule)->klass
383  = RBASIC(rb_cObject)->klass
384  = RBASIC(rb_cBasicObject)->klass
385  = rb_cClass;
386 }
387 
388 
399 VALUE
401 {
402  if (BUILTIN_TYPE(obj) == T_CLASS) {
403  return make_metaclass(obj);
404  }
405  else {
406  return make_singleton_class(obj);
407  }
408 }
409 
410 
421 VALUE
423 {
424  VALUE klass;
425 
426  if (!super) super = rb_cObject;
427  klass = rb_class_new(super);
428  rb_make_metaclass(klass, RBASIC(super)->klass);
429 
430  return klass;
431 }
432 
433 
442 VALUE
444 {
445  ID inherited;
446  if (!super) super = rb_cObject;
447  CONST_ID(inherited, "inherited");
448  return rb_funcall(super, inherited, 1, klass);
449 }
450 
451 
452 
468 VALUE
469 rb_define_class(const char *name, VALUE super)
470 {
471  VALUE klass;
472  ID id;
473 
474  id = rb_intern(name);
475  if (rb_const_defined(rb_cObject, id)) {
476  klass = rb_const_get(rb_cObject, id);
477  if (TYPE(klass) != T_CLASS) {
478  rb_raise(rb_eTypeError, "%s is not a class", name);
479  }
480  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
481  rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
482  }
483  return klass;
484  }
485  if (!super) {
486  rb_warn("no super class for `%s', Object assumed", name);
487  }
488  klass = rb_define_class_id(id, super);
489  st_add_direct(rb_class_tbl, id, klass);
490  rb_name_class(klass, id);
491  rb_const_set(rb_cObject, id, klass);
492  rb_class_inherited(super, klass);
493 
494  return klass;
495 }
496 
497 
514 VALUE
515 rb_define_class_under(VALUE outer, const char *name, VALUE super)
516 {
517  return rb_define_class_id_under(outer, rb_intern(name), super);
518 }
519 
520 
537 VALUE
539 {
540  VALUE klass;
541 
542  if (rb_const_defined_at(outer, id)) {
543  klass = rb_const_get_at(outer, id);
544  if (TYPE(klass) != T_CLASS) {
545  rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
546  }
547  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
548  rb_name_error(id, "%s is already defined", rb_id2name(id));
549  }
550  return klass;
551  }
552  if (!super) {
553  rb_warn("no super class for `%s::%s', Object assumed",
554  rb_class2name(outer), rb_id2name(id));
555  }
556  klass = rb_define_class_id(id, super);
557  rb_set_class_path_string(klass, outer, rb_id2str(id));
558  rb_const_set(outer, id, klass);
559  rb_class_inherited(super, klass);
561 
562  return klass;
563 }
564 
565 VALUE
567 {
569 
571 
572  return (VALUE)mdl;
573 }
574 
575 VALUE
577 {
578  VALUE mdl;
579 
580  mdl = rb_module_new();
581  rb_name_class(mdl, id);
582 
583  return mdl;
584 }
585 
586 VALUE
587 rb_define_module(const char *name)
588 {
589  VALUE module;
590  ID id;
591 
592  id = rb_intern(name);
593  if (rb_const_defined(rb_cObject, id)) {
594  module = rb_const_get(rb_cObject, id);
595  if (TYPE(module) == T_MODULE)
596  return module;
597  rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
598  }
599  module = rb_define_module_id(id);
600  st_add_direct(rb_class_tbl, id, module);
601  rb_const_set(rb_cObject, id, module);
602 
603  return module;
604 }
605 
606 VALUE
607 rb_define_module_under(VALUE outer, const char *name)
608 {
609  return rb_define_module_id_under(outer, rb_intern(name));
610 }
611 
612 VALUE
614 {
615  VALUE module;
616 
617  if (rb_const_defined_at(outer, id)) {
618  module = rb_const_get_at(outer, id);
619  if (TYPE(module) == T_MODULE)
620  return module;
621  rb_raise(rb_eTypeError, "%s::%s is not a module",
622  rb_class2name(outer), rb_obj_classname(module));
623  }
624  module = rb_define_module_id(id);
625  rb_const_set(outer, id, module);
626  rb_set_class_path_string(module, outer, rb_id2str(id));
628 
629  return module;
630 }
631 
632 static VALUE
634 {
636 
637  if (BUILTIN_TYPE(module) == T_ICLASS) {
638  module = RBASIC(module)->klass;
639  }
640  if (!RCLASS_IV_TBL(module)) {
641  RCLASS_IV_TBL(module) = st_init_numtable();
642  }
643  if (!RCLASS_CONST_TBL(module)) {
645  }
646  RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
647  RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
648  RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
649  RCLASS_SUPER(klass) = super;
650  if (TYPE(module) == T_ICLASS) {
651  RBASIC(klass)->klass = RBASIC(module)->klass;
652  }
653  else {
654  RBASIC(klass)->klass = module;
655  }
656  OBJ_INFECT(klass, module);
657  OBJ_INFECT(klass, super);
658 
659  return (VALUE)klass;
660 }
661 
662 void
664 {
665  VALUE p, c;
666  int changed = 0;
667 
668  rb_frozen_class_p(klass);
669  if (!OBJ_UNTRUSTED(klass)) {
670  rb_secure(4);
671  }
672 
673  if (TYPE(module) != T_MODULE) {
674  Check_Type(module, T_MODULE);
675  }
676 
677  OBJ_INFECT(klass, module);
678  c = klass;
679  while (module) {
680  int superclass_seen = FALSE;
681 
682  if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
683  rb_raise(rb_eArgError, "cyclic include detected");
684  /* ignore if the module included already in superclasses */
685  for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
686  switch (BUILTIN_TYPE(p)) {
687  case T_ICLASS:
688  if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
689  if (!superclass_seen) {
690  c = p; /* move insertion point */
691  }
692  goto skip;
693  }
694  break;
695  case T_CLASS:
696  superclass_seen = TRUE;
697  break;
698  }
699  }
700  c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
701  if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
702  changed = 1;
703  if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries)
704  changed = 1;
705  skip:
706  module = RCLASS_SUPER(module);
707  }
708  if (changed) rb_clear_cache();
709 }
710 
711 /*
712  * call-seq:
713  * mod.included_modules -> array
714  *
715  * Returns the list of modules included in <i>mod</i>.
716  *
717  * module Mixin
718  * end
719  *
720  * module Outer
721  * include Mixin
722  * end
723  *
724  * Mixin.included_modules #=> []
725  * Outer.included_modules #=> [Mixin]
726  */
727 
728 VALUE
730 {
731  VALUE ary = rb_ary_new();
732  VALUE p;
733 
734  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
735  if (BUILTIN_TYPE(p) == T_ICLASS) {
736  rb_ary_push(ary, RBASIC(p)->klass);
737  }
738  }
739  return ary;
740 }
741 
742 /*
743  * call-seq:
744  * mod.include?(module) -> true or false
745  *
746  * Returns <code>true</code> if <i>module</i> is included in
747  * <i>mod</i> or one of <i>mod</i>'s ancestors.
748  *
749  * module A
750  * end
751  * class B
752  * include A
753  * end
754  * class C < B
755  * end
756  * B.include?(A) #=> true
757  * C.include?(A) #=> true
758  * A.include?(A) #=> false
759  */
760 
761 VALUE
763 {
764  VALUE p;
765 
766  Check_Type(mod2, T_MODULE);
767  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
768  if (BUILTIN_TYPE(p) == T_ICLASS) {
769  if (RBASIC(p)->klass == mod2) return Qtrue;
770  }
771  }
772  return Qfalse;
773 }
774 
775 /*
776  * call-seq:
777  * mod.ancestors -> array
778  *
779  * Returns a list of modules included in <i>mod</i> (including
780  * <i>mod</i> itself).
781  *
782  * module Mod
783  * include Math
784  * include Comparable
785  * end
786  *
787  * Mod.ancestors #=> [Mod, Comparable, Math]
788  * Math.ancestors #=> [Math]
789  */
790 
791 VALUE
793 {
794  VALUE p, ary = rb_ary_new();
795 
796  for (p = mod; p; p = RCLASS_SUPER(p)) {
797  if (FL_TEST(p, FL_SINGLETON))
798  continue;
799  if (BUILTIN_TYPE(p) == T_ICLASS) {
800  rb_ary_push(ary, RBASIC(p)->klass);
801  }
802  else {
803  rb_ary_push(ary, p);
804  }
805  }
806  return ary;
807 }
808 
809 #define VISI(x) ((x)&NOEX_MASK)
810 #define VISI_CHECK(x,f) (VISI(x) == (f))
811 
812 static int
813 ins_methods_push(ID name, long type, VALUE ary, long visi)
814 {
815  if (type == -1) return ST_CONTINUE;
816 
817  switch (visi) {
818  case NOEX_PRIVATE:
819  case NOEX_PROTECTED:
820  case NOEX_PUBLIC:
821  visi = (type == visi);
822  break;
823  default:
824  visi = (type != NOEX_PRIVATE);
825  break;
826  }
827  if (visi) {
828  rb_ary_push(ary, ID2SYM(name));
829  }
830  return ST_CONTINUE;
831 }
832 
833 static int
835 {
836  return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
837 }
838 
839 static int
841 {
842  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
843 }
844 
845 static int
847 {
848  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
849 }
850 
851 static int
853 {
854  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
855 }
856 
857 static int
859 {
860  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
861  st_table *list = (st_table *)data;
862  long type;
863 
864  if ((ID)key == ID_ALLOCATOR) {
865  return ST_CONTINUE;
866  }
867 
868  if (!st_lookup(list, key, 0)) {
869  if (UNDEFINED_METHOD_ENTRY_P(me)) {
870  type = -1; /* none */
871  }
872  else {
873  type = VISI(me->flag);
874  }
875  st_add_direct(list, key, type);
876  }
877  return ST_CONTINUE;
878 }
879 
880 static VALUE
882 {
883  VALUE ary;
884  int recur;
885  st_table *list;
886 
887  if (argc == 0) {
888  recur = TRUE;
889  }
890  else {
891  VALUE r;
892  rb_scan_args(argc, argv, "01", &r);
893  recur = RTEST(r);
894  }
895 
896  list = st_init_numtable();
897  for (; mod; mod = RCLASS_SUPER(mod)) {
899  if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
900  if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
901  if (!recur) break;
902  }
903  ary = rb_ary_new();
904  st_foreach(list, func, ary);
905  st_free_table(list);
906 
907  return ary;
908 }
909 
910 /*
911  * call-seq:
912  * mod.instance_methods(include_super=true) -> array
913  *
914  * Returns an array containing the names of the public and protected instance
915  * methods in the receiver. For a module, these are the public and protected methods;
916  * for a class, they are the instance (not singleton) methods. With no
917  * argument, or with an argument that is <code>false</code>, the
918  * instance methods in <i>mod</i> are returned, otherwise the methods
919  * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
920  *
921  * module A
922  * def method1() end
923  * end
924  * class B
925  * def method2() end
926  * end
927  * class C < B
928  * def method3() end
929  * end
930  *
931  * A.instance_methods #=> [:method1]
932  * B.instance_methods(false) #=> [:method2]
933  * C.instance_methods(false) #=> [:method3]
934  * C.instance_methods(true).length #=> 43
935  */
936 
937 VALUE
938 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
939 {
940  return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
941 }
942 
943 /*
944  * call-seq:
945  * mod.protected_instance_methods(include_super=true) -> array
946  *
947  * Returns a list of the protected instance methods defined in
948  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
949  * methods of any ancestors are included.
950  */
951 
952 VALUE
954 {
955  return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
956 }
957 
958 /*
959  * call-seq:
960  * mod.private_instance_methods(include_super=true) -> array
961  *
962  * Returns a list of the private instance methods defined in
963  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
964  * methods of any ancestors are included.
965  *
966  * module Mod
967  * def method1() end
968  * private :method1
969  * def method2() end
970  * end
971  * Mod.instance_methods #=> [:method2]
972  * Mod.private_instance_methods #=> [:method1]
973  */
974 
975 VALUE
977 {
978  return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
979 }
980 
981 /*
982  * call-seq:
983  * mod.public_instance_methods(include_super=true) -> array
984  *
985  * Returns a list of the public instance methods defined in <i>mod</i>.
986  * If the optional parameter is not <code>false</code>, the methods of
987  * any ancestors are included.
988  */
989 
990 VALUE
992 {
993  return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
994 }
995 
996 /*
997  * call-seq:
998  * obj.methods -> array
999  *
1000  * Returns a list of the names of public and protected methods of
1001  * <i>obj</i>. This will include all the methods accessible in
1002  * <i>obj</i>'s ancestors.
1003  *
1004  * class Klass
1005  * def klass_method()
1006  * end
1007  * end
1008  * k = Klass.new
1009  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1010  * # :==~, :!, :eql?
1011  * # :hash, :<=>, :class, :singleton_class]
1012  * k.methods.length #=> 57
1013  */
1014 
1015 VALUE
1016 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1017 {
1018  retry:
1019  if (argc == 0) {
1020  VALUE args[1];
1021 
1022  args[0] = Qtrue;
1023  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1024  }
1025  else {
1026  VALUE recur;
1027 
1028  rb_scan_args(argc, argv, "1", &recur);
1029  if (RTEST(recur)) {
1030  argc = 0;
1031  goto retry;
1032  }
1033  return rb_obj_singleton_methods(argc, argv, obj);
1034  }
1035 }
1036 
1037 /*
1038  * call-seq:
1039  * obj.protected_methods(all=true) -> array
1040  *
1041  * Returns the list of protected methods accessible to <i>obj</i>. If
1042  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1043  * in the receiver will be listed.
1044  */
1045 
1046 VALUE
1047 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1048 {
1049  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1050 }
1051 
1052 /*
1053  * call-seq:
1054  * obj.private_methods(all=true) -> array
1055  *
1056  * Returns the list of private methods accessible to <i>obj</i>. If
1057  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1058  * in the receiver will be listed.
1059  */
1060 
1061 VALUE
1062 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1063 {
1064  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1065 }
1066 
1067 /*
1068  * call-seq:
1069  * obj.public_methods(all=true) -> array
1070  *
1071  * Returns the list of public methods accessible to <i>obj</i>. If
1072  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1073  * in the receiver will be listed.
1074  */
1075 
1076 VALUE
1077 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1078 {
1079  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1080 }
1081 
1082 /*
1083  * call-seq:
1084  * obj.singleton_methods(all=true) -> array
1085  *
1086  * Returns an array of the names of singleton methods for <i>obj</i>.
1087  * If the optional <i>all</i> parameter is true, the list will include
1088  * methods in modules included in <i>obj</i>.
1089  * Only public and protected singleton methods are returned.
1090  *
1091  * module Other
1092  * def three() end
1093  * end
1094  *
1095  * class Single
1096  * def Single.four() end
1097  * end
1098  *
1099  * a = Single.new
1100  *
1101  * def a.one()
1102  * end
1103  *
1104  * class << a
1105  * include Other
1106  * def two()
1107  * end
1108  * end
1109  *
1110  * Single.singleton_methods #=> [:four]
1111  * a.singleton_methods(false) #=> [:two, :one]
1112  * a.singleton_methods #=> [:two, :one, :three]
1113  */
1114 
1115 VALUE
1116 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1117 {
1118  VALUE recur, ary, klass;
1119  st_table *list;
1120 
1121  if (argc == 0) {
1122  recur = Qtrue;
1123  }
1124  else {
1125  rb_scan_args(argc, argv, "01", &recur);
1126  }
1127  klass = CLASS_OF(obj);
1128  list = st_init_numtable();
1129  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1131  klass = RCLASS_SUPER(klass);
1132  }
1133  if (RTEST(recur)) {
1134  while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
1136  klass = RCLASS_SUPER(klass);
1137  }
1138  }
1139  ary = rb_ary_new();
1140  st_foreach(list, ins_methods_i, ary);
1141  st_free_table(list);
1142 
1143  return ary;
1144 }
1145 
1203 void
1205 {
1206  rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1207 }
1208 
1209 void
1210 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1211 {
1212  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1213 }
1214 
1215 void
1216 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1217 {
1218  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1219 }
1220 
1221 void
1222 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1223 {
1224  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1225 }
1226 
1227 void
1228 rb_undef_method(VALUE klass, const char *name)
1229 {
1231 }
1232 
1241 #define SPECIAL_SINGLETON(x,c) do {\
1242  if (obj == (x)) {\
1243  return (c);\
1244  }\
1245 } while (0)
1246 
1247 
1257 static VALUE
1259 {
1260  VALUE klass;
1261 
1262  if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
1263  rb_raise(rb_eTypeError, "can't define singleton");
1264  }
1265  if (rb_special_const_p(obj)) {
1269  rb_bug("unknown immediate %p", (void *)obj);
1270  }
1271 
1272  if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1273  rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1274  klass = RBASIC(obj)->klass;
1275  }
1276  else {
1277  klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1278  }
1279 
1280  if (OBJ_TAINTED(obj)) {
1281  OBJ_TAINT(klass);
1282  }
1283  else {
1284  FL_UNSET(klass, FL_TAINT);
1285  }
1286  if (OBJ_UNTRUSTED(obj)) {
1287  OBJ_UNTRUST(klass);
1288  }
1289  else {
1290  FL_UNSET(klass, FL_UNTRUSTED);
1291  }
1292  if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1293 
1294  return klass;
1295 }
1296 
1297 
1315 VALUE
1317 {
1318  VALUE klass = singleton_class_of(obj);
1319 
1320  /* ensures an exposed class belongs to its own eigenclass */
1321  if (TYPE(obj) == T_CLASS) (void)ENSURE_EIGENCLASS(klass);
1322 
1323  return klass;
1324 }
1325 
1342 void
1343 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1344 {
1345  rb_define_method(singleton_class_of(obj), name, func, argc);
1346 }
1347 
1348 
1349 
1357 void
1358 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1359 {
1360  rb_define_private_method(module, name, func, argc);
1361  rb_define_singleton_method(module, name, func, argc);
1362 }
1363 
1364 
1371 void
1372 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1373 {
1375 }
1376 
1377 
1384 void
1385 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1386 {
1387  rb_alias(klass, rb_intern(name1), rb_intern(name2));
1388 }
1389 
1397 void
1398 rb_define_attr(VALUE klass, const char *name, int read, int write)
1399 {
1400  rb_attr(klass, rb_intern(name), read, write, FALSE);
1401 }
1402 
1403 int
1405 {
1406  const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
1407  if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1408  me->def->body.cfunc.func == rb_any_to_s)
1409  return 1;
1410  return 0;
1411 }
1412 
1413 #include <stdarg.h>
1414 
1415 int
1416 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1417 {
1418  int i;
1419  const char *p = fmt;
1420  VALUE *var;
1421  va_list vargs;
1422  int f_var = 0, f_hash = 0, f_block = 0;
1423  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1424  int argi = 0;
1425  VALUE hash = Qnil;
1426 
1427  if (ISDIGIT(*p)) {
1428  n_lead = *p - '0';
1429  p++;
1430  if (ISDIGIT(*p)) {
1431  n_opt = *p - '0';
1432  p++;
1433  if (ISDIGIT(*p)) {
1434  n_trail = *p - '0';
1435  p++;
1436  goto block_arg;
1437  }
1438  }
1439  }
1440  if (*p == '*') {
1441  f_var = 1;
1442  p++;
1443  if (ISDIGIT(*p)) {
1444  n_trail = *p - '0';
1445  p++;
1446  }
1447  }
1448  block_arg:
1449  if (*p == ':') {
1450  f_hash = 1;
1451  p++;
1452  }
1453  if (*p == '&') {
1454  f_block = 1;
1455  p++;
1456  }
1457  if (*p != '\0') {
1458  rb_fatal("bad scan arg format: %s", fmt);
1459  }
1460  n_mand = n_lead + n_trail;
1461 
1462  if (argc < n_mand)
1463  goto argc_error;
1464 
1465  va_start(vargs, fmt);
1466 
1467  /* capture an option hash - phase 1: pop */
1468  if (f_hash && n_mand < argc) {
1469  VALUE last = argv[argc - 1];
1470 
1471  if (NIL_P(last)) {
1472  /* nil is taken as an empty option hash only if it is not
1473  ambiguous; i.e. '*' is not specified and arguments are
1474  given more than sufficient */
1475  if (!f_var && n_mand + n_opt < argc)
1476  argc--;
1477  }
1478  else {
1479  hash = rb_check_convert_type(last, T_HASH, "Hash", "to_hash");
1480  if (!NIL_P(hash))
1481  argc--;
1482  }
1483  }
1484  /* capture leading mandatory arguments */
1485  for (i = n_lead; i-- > 0; ) {
1486  var = va_arg(vargs, VALUE *);
1487  if (var) *var = argv[argi];
1488  argi++;
1489  }
1490  /* capture optional arguments */
1491  for (i = n_opt; i-- > 0; ) {
1492  var = va_arg(vargs, VALUE *);
1493  if (argi < argc - n_trail) {
1494  if (var) *var = argv[argi];
1495  argi++;
1496  }
1497  else {
1498  if (var) *var = Qnil;
1499  }
1500  }
1501  /* capture variable length arguments */
1502  if (f_var) {
1503  int n_var = argc - argi - n_trail;
1504 
1505  var = va_arg(vargs, VALUE *);
1506  if (0 < n_var) {
1507  if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1508  argi += n_var;
1509  }
1510  else {
1511  if (var) *var = rb_ary_new();
1512  }
1513  }
1514  /* capture trailing mandatory arguments */
1515  for (i = n_trail; i-- > 0; ) {
1516  var = va_arg(vargs, VALUE *);
1517  if (var) *var = argv[argi];
1518  argi++;
1519  }
1520  /* capture an option hash - phase 2: assignment */
1521  if (f_hash) {
1522  var = va_arg(vargs, VALUE *);
1523  if (var) *var = hash;
1524  }
1525  /* capture iterator block */
1526  if (f_block) {
1527  var = va_arg(vargs, VALUE *);
1528  if (rb_block_given_p()) {
1529  *var = rb_block_proc();
1530  }
1531  else {
1532  *var = Qnil;
1533  }
1534  }
1535  va_end(vargs);
1536 
1537  if (argi < argc)
1538  goto argc_error;
1539 
1540  return argc;
1541 
1542  argc_error:
1543  if (0 < n_opt)
1544  rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)",
1545  argc, n_mand, n_mand + n_opt, f_var ? "+" : "");
1546  else
1547  rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)",
1548  argc, n_mand, f_var ? "+" : "");
1549 }
1550