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