Ruby  1.9.3p429(2013-05-15revision40747)
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  skip:
703  module = RCLASS_SUPER(module);
704  }
705  if (changed) rb_clear_cache();
706 }
707 
708 /*
709  * call-seq:
710  * mod.included_modules -> array
711  *
712  * Returns the list of modules included in <i>mod</i>.
713  *
714  * module Mixin
715  * end
716  *
717  * module Outer
718  * include Mixin
719  * end
720  *
721  * Mixin.included_modules #=> []
722  * Outer.included_modules #=> [Mixin]
723  */
724 
725 VALUE
727 {
728  VALUE ary = rb_ary_new();
729  VALUE p;
730 
731  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
732  if (BUILTIN_TYPE(p) == T_ICLASS) {
733  rb_ary_push(ary, RBASIC(p)->klass);
734  }
735  }
736  return ary;
737 }
738 
739 /*
740  * call-seq:
741  * mod.include?(module) -> true or false
742  *
743  * Returns <code>true</code> if <i>module</i> is included in
744  * <i>mod</i> or one of <i>mod</i>'s ancestors.
745  *
746  * module A
747  * end
748  * class B
749  * include A
750  * end
751  * class C < B
752  * end
753  * B.include?(A) #=> true
754  * C.include?(A) #=> true
755  * A.include?(A) #=> false
756  */
757 
758 VALUE
760 {
761  VALUE p;
762 
763  Check_Type(mod2, T_MODULE);
764  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
765  if (BUILTIN_TYPE(p) == T_ICLASS) {
766  if (RBASIC(p)->klass == mod2) return Qtrue;
767  }
768  }
769  return Qfalse;
770 }
771 
772 /*
773  * call-seq:
774  * mod.ancestors -> array
775  *
776  * Returns a list of modules included in <i>mod</i> (including
777  * <i>mod</i> itself).
778  *
779  * module Mod
780  * include Math
781  * include Comparable
782  * end
783  *
784  * Mod.ancestors #=> [Mod, Comparable, Math]
785  * Math.ancestors #=> [Math]
786  */
787 
788 VALUE
790 {
791  VALUE p, ary = rb_ary_new();
792 
793  for (p = mod; p; p = RCLASS_SUPER(p)) {
794  if (FL_TEST(p, FL_SINGLETON))
795  continue;
796  if (BUILTIN_TYPE(p) == T_ICLASS) {
797  rb_ary_push(ary, RBASIC(p)->klass);
798  }
799  else {
800  rb_ary_push(ary, p);
801  }
802  }
803  return ary;
804 }
805 
806 #define VISI(x) ((x)&NOEX_MASK)
807 #define VISI_CHECK(x,f) (VISI(x) == (f))
808 
809 static int
810 ins_methods_push(ID name, long type, VALUE ary, long visi)
811 {
812  if (type == -1) return ST_CONTINUE;
813 
814  switch (visi) {
815  case NOEX_PRIVATE:
816  case NOEX_PROTECTED:
817  case NOEX_PUBLIC:
818  visi = (type == visi);
819  break;
820  default:
821  visi = (type != NOEX_PRIVATE);
822  break;
823  }
824  if (visi) {
825  rb_ary_push(ary, ID2SYM(name));
826  }
827  return ST_CONTINUE;
828 }
829 
830 static int
832 {
833  return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
834 }
835 
836 static int
838 {
839  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
840 }
841 
842 static int
844 {
845  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
846 }
847 
848 static int
850 {
851  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
852 }
853 
854 static int
856 {
857  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
858  st_table *list = (st_table *)data;
859  long type;
860 
861  if ((ID)key == ID_ALLOCATOR) {
862  return ST_CONTINUE;
863  }
864 
865  if (!st_lookup(list, key, 0)) {
866  if (UNDEFINED_METHOD_ENTRY_P(me)) {
867  type = -1; /* none */
868  }
869  else {
870  type = VISI(me->flag);
871  }
872  st_add_direct(list, key, type);
873  }
874  return ST_CONTINUE;
875 }
876 
877 static VALUE
879 {
880  VALUE ary;
881  int recur;
882  st_table *list;
883 
884  if (argc == 0) {
885  recur = TRUE;
886  }
887  else {
888  VALUE r;
889  rb_scan_args(argc, argv, "01", &r);
890  recur = RTEST(r);
891  }
892 
893  list = st_init_numtable();
894  for (; mod; mod = RCLASS_SUPER(mod)) {
896  if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
897  if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
898  if (!recur) break;
899  }
900  ary = rb_ary_new();
901  st_foreach(list, func, ary);
902  st_free_table(list);
903 
904  return ary;
905 }
906 
907 /*
908  * call-seq:
909  * mod.instance_methods(include_super=true) -> array
910  *
911  * Returns an array containing the names of the public and protected instance
912  * methods in the receiver. For a module, these are the public and protected methods;
913  * for a class, they are the instance (not singleton) methods. With no
914  * argument, or with an argument that is <code>false</code>, the
915  * instance methods in <i>mod</i> are returned, otherwise the methods
916  * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
917  *
918  * module A
919  * def method1() end
920  * end
921  * class B
922  * def method2() end
923  * end
924  * class C < B
925  * def method3() end
926  * end
927  *
928  * A.instance_methods #=> [:method1]
929  * B.instance_methods(false) #=> [:method2]
930  * C.instance_methods(false) #=> [:method3]
931  * C.instance_methods(true).length #=> 43
932  */
933 
934 VALUE
935 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
936 {
937  return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
938 }
939 
940 /*
941  * call-seq:
942  * mod.protected_instance_methods(include_super=true) -> array
943  *
944  * Returns a list of the protected instance methods defined in
945  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
946  * methods of any ancestors are included.
947  */
948 
949 VALUE
951 {
952  return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
953 }
954 
955 /*
956  * call-seq:
957  * mod.private_instance_methods(include_super=true) -> array
958  *
959  * Returns a list of the private instance methods defined in
960  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
961  * methods of any ancestors are included.
962  *
963  * module Mod
964  * def method1() end
965  * private :method1
966  * def method2() end
967  * end
968  * Mod.instance_methods #=> [:method2]
969  * Mod.private_instance_methods #=> [:method1]
970  */
971 
972 VALUE
974 {
975  return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
976 }
977 
978 /*
979  * call-seq:
980  * mod.public_instance_methods(include_super=true) -> array
981  *
982  * Returns a list of the public instance methods defined in <i>mod</i>.
983  * If the optional parameter is not <code>false</code>, the methods of
984  * any ancestors are included.
985  */
986 
987 VALUE
989 {
990  return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
991 }
992 
993 /*
994  * call-seq:
995  * obj.methods -> array
996  *
997  * Returns a list of the names of public and protected methods of
998  * <i>obj</i>. This will include all the methods accessible in
999  * <i>obj</i>'s ancestors.
1000  *
1001  * class Klass
1002  * def klass_method()
1003  * end
1004  * end
1005  * k = Klass.new
1006  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1007  * # :==~, :!, :eql?
1008  * # :hash, :<=>, :class, :singleton_class]
1009  * k.methods.length #=> 57
1010  */
1011 
1012 VALUE
1013 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1014 {
1015  retry:
1016  if (argc == 0) {
1017  VALUE args[1];
1018 
1019  args[0] = Qtrue;
1020  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1021  }
1022  else {
1023  VALUE recur;
1024 
1025  rb_scan_args(argc, argv, "1", &recur);
1026  if (RTEST(recur)) {
1027  argc = 0;
1028  goto retry;
1029  }
1030  return rb_obj_singleton_methods(argc, argv, obj);
1031  }
1032 }
1033 
1034 /*
1035  * call-seq:
1036  * obj.protected_methods(all=true) -> array
1037  *
1038  * Returns the list of protected methods accessible to <i>obj</i>. If
1039  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1040  * in the receiver will be listed.
1041  */
1042 
1043 VALUE
1044 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1045 {
1046  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1047 }
1048 
1049 /*
1050  * call-seq:
1051  * obj.private_methods(all=true) -> array
1052  *
1053  * Returns the list of private methods accessible to <i>obj</i>. If
1054  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1055  * in the receiver will be listed.
1056  */
1057 
1058 VALUE
1059 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1060 {
1061  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1062 }
1063 
1064 /*
1065  * call-seq:
1066  * obj.public_methods(all=true) -> array
1067  *
1068  * Returns the list of public methods accessible to <i>obj</i>. If
1069  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1070  * in the receiver will be listed.
1071  */
1072 
1073 VALUE
1074 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1075 {
1076  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1077 }
1078 
1079 /*
1080  * call-seq:
1081  * obj.singleton_methods(all=true) -> array
1082  *
1083  * Returns an array of the names of singleton methods for <i>obj</i>.
1084  * If the optional <i>all</i> parameter is true, the list will include
1085  * methods in modules included in <i>obj</i>.
1086  * Only public and protected singleton methods are returned.
1087  *
1088  * module Other
1089  * def three() end
1090  * end
1091  *
1092  * class Single
1093  * def Single.four() end
1094  * end
1095  *
1096  * a = Single.new
1097  *
1098  * def a.one()
1099  * end
1100  *
1101  * class << a
1102  * include Other
1103  * def two()
1104  * end
1105  * end
1106  *
1107  * Single.singleton_methods #=> [:four]
1108  * a.singleton_methods(false) #=> [:two, :one]
1109  * a.singleton_methods #=> [:two, :one, :three]
1110  */
1111 
1112 VALUE
1113 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1114 {
1115  VALUE recur, ary, klass;
1116  st_table *list;
1117 
1118  if (argc == 0) {
1119  recur = Qtrue;
1120  }
1121  else {
1122  rb_scan_args(argc, argv, "01", &recur);
1123  }
1124  klass = CLASS_OF(obj);
1125  list = st_init_numtable();
1126  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1128  klass = RCLASS_SUPER(klass);
1129  }
1130  if (RTEST(recur)) {
1131  while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
1133  klass = RCLASS_SUPER(klass);
1134  }
1135  }
1136  ary = rb_ary_new();
1137  st_foreach(list, ins_methods_i, ary);
1138  st_free_table(list);
1139 
1140  return ary;
1141 }
1142 
1200 void
1202 {
1203  rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1204 }
1205 
1206 void
1207 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1208 {
1209  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1210 }
1211 
1212 void
1213 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1214 {
1215  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1216 }
1217 
1218 void
1219 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1220 {
1221  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1222 }
1223 
1224 void
1225 rb_undef_method(VALUE klass, const char *name)
1226 {
1228 }
1229 
1238 #define SPECIAL_SINGLETON(x,c) do {\
1239  if (obj == (x)) {\
1240  return (c);\
1241  }\
1242 } while (0)
1243 
1244 
1254 static VALUE
1256 {
1257  VALUE klass;
1258 
1259  if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
1260  rb_raise(rb_eTypeError, "can't define singleton");
1261  }
1262  if (rb_special_const_p(obj)) {
1266  rb_bug("unknown immediate %p", (void *)obj);
1267  }
1268 
1269  if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1270  rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1271  klass = RBASIC(obj)->klass;
1272  }
1273  else {
1274  klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1275  }
1276 
1277  if (OBJ_TAINTED(obj)) {
1278  OBJ_TAINT(klass);
1279  }
1280  else {
1281  FL_UNSET(klass, FL_TAINT);
1282  }
1283  if (OBJ_UNTRUSTED(obj)) {
1284  OBJ_UNTRUST(klass);
1285  }
1286  else {
1287  FL_UNSET(klass, FL_UNTRUSTED);
1288  }
1289  if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1290 
1291  return klass;
1292 }
1293 
1294 
1312 VALUE
1314 {
1315  VALUE klass = singleton_class_of(obj);
1316 
1317  /* ensures an exposed class belongs to its own eigenclass */
1318  if (TYPE(obj) == T_CLASS) (void)ENSURE_EIGENCLASS(klass);
1319 
1320  return klass;
1321 }
1322 
1339 void
1340 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1341 {
1342  rb_define_method(singleton_class_of(obj), name, func, argc);
1343 }
1344 
1345 
1346 
1354 void
1355 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1356 {
1357  rb_define_private_method(module, name, func, argc);
1358  rb_define_singleton_method(module, name, func, argc);
1359 }
1360 
1361 
1368 void
1369 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1370 {
1372 }
1373 
1374 
1381 void
1382 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1383 {
1384  rb_alias(klass, rb_intern(name1), rb_intern(name2));
1385 }
1386 
1394 void
1395 rb_define_attr(VALUE klass, const char *name, int read, int write)
1396 {
1397  rb_attr(klass, rb_intern(name), read, write, FALSE);
1398 }
1399 
1400 int
1402 {
1403  const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
1404  if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1405  me->def->body.cfunc.func == rb_any_to_s)
1406  return 1;
1407  return 0;
1408 }
1409 
1410 #include <stdarg.h>
1411 
1412 int
1413 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1414 {
1415  int i;
1416  const char *p = fmt;
1417  VALUE *var;
1418  va_list vargs;
1419  int f_var = 0, f_hash = 0, f_block = 0;
1420  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1421  int argi = 0;
1422  VALUE hash = Qnil;
1423 
1424  if (ISDIGIT(*p)) {
1425  n_lead = *p - '0';
1426  p++;
1427  if (ISDIGIT(*p)) {
1428  n_opt = *p - '0';
1429  p++;
1430  if (ISDIGIT(*p)) {
1431  n_trail = *p - '0';
1432  p++;
1433  goto block_arg;
1434  }
1435  }
1436  }
1437  if (*p == '*') {
1438  f_var = 1;
1439  p++;
1440  if (ISDIGIT(*p)) {
1441  n_trail = *p - '0';
1442  p++;
1443  }
1444  }
1445  block_arg:
1446  if (*p == ':') {
1447  f_hash = 1;
1448  p++;
1449  }
1450  if (*p == '&') {
1451  f_block = 1;
1452  p++;
1453  }
1454  if (*p != '\0') {
1455  rb_fatal("bad scan arg format: %s", fmt);
1456  }
1457  n_mand = n_lead + n_trail;
1458 
1459  if (argc < n_mand)
1460  goto argc_error;
1461 
1462  va_start(vargs, fmt);
1463 
1464  /* capture an option hash - phase 1: pop */
1465  if (f_hash && n_mand < argc) {
1466  VALUE last = argv[argc - 1];
1467 
1468  if (NIL_P(last)) {
1469  /* nil is taken as an empty option hash only if it is not
1470  ambiguous; i.e. '*' is not specified and arguments are
1471  given more than sufficient */
1472  if (!f_var && n_mand + n_opt < argc)
1473  argc--;
1474  }
1475  else {
1476  hash = rb_check_convert_type(last, T_HASH, "Hash", "to_hash");
1477  if (!NIL_P(hash))
1478  argc--;
1479  }
1480  }
1481  /* capture leading mandatory arguments */
1482  for (i = n_lead; i-- > 0; ) {
1483  var = va_arg(vargs, VALUE *);
1484  if (var) *var = argv[argi];
1485  argi++;
1486  }
1487  /* capture optional arguments */
1488  for (i = n_opt; i-- > 0; ) {
1489  var = va_arg(vargs, VALUE *);
1490  if (argi < argc - n_trail) {
1491  if (var) *var = argv[argi];
1492  argi++;
1493  }
1494  else {
1495  if (var) *var = Qnil;
1496  }
1497  }
1498  /* capture variable length arguments */
1499  if (f_var) {
1500  int n_var = argc - argi - n_trail;
1501 
1502  var = va_arg(vargs, VALUE *);
1503  if (0 < n_var) {
1504  if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1505  argi += n_var;
1506  }
1507  else {
1508  if (var) *var = rb_ary_new();
1509  }
1510  }
1511  /* capture trailing mandatory arguments */
1512  for (i = n_trail; i-- > 0; ) {
1513  var = va_arg(vargs, VALUE *);
1514  if (var) *var = argv[argi];
1515  argi++;
1516  }
1517  /* capture an option hash - phase 2: assignment */
1518  if (f_hash) {
1519  var = va_arg(vargs, VALUE *);
1520  if (var) *var = hash;
1521  }
1522  /* capture iterator block */
1523  if (f_block) {
1524  var = va_arg(vargs, VALUE *);
1525  if (rb_block_given_p()) {
1526  *var = rb_block_proc();
1527  }
1528  else {
1529  *var = Qnil;
1530  }
1531  }
1532  va_end(vargs);
1533 
1534  if (argi < argc)
1535  goto argc_error;
1536 
1537  return argc;
1538 
1539  argc_error:
1540  if (0 < n_opt)
1541  rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)",
1542  argc, n_mand, n_mand + n_opt, f_var ? "+" : "");
1543  else
1544  rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)",
1545  argc, n_mand, f_var ? "+" : "");
1546 }
1547