Ruby  2.0.0p195(2013-05-14revision40734)
class.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  class.c -
4 
5  $Author: nagachika $
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 {
52  NEWOBJ_OF(obj, struct RClass, klass, flags);
53  obj->ptr = ALLOC(rb_classext_t);
54  RCLASS_IV_TBL(obj) = 0;
55  RCLASS_CONST_TBL(obj) = 0;
56  RCLASS_M_TBL(obj) = 0;
57  RCLASS_SUPER(obj) = 0;
58  RCLASS_ORIGIN(obj) = (VALUE)obj;
59  RCLASS_IV_INDEX_TBL(obj) = 0;
61  RCLASS_EXT(obj)->allocator = 0;
62  return (VALUE)obj;
63 }
64 
65 
75 VALUE
77 {
79 
80  RCLASS_SUPER(klass) = super;
81  RCLASS_M_TBL(klass) = st_init_numtable();
82 
83  OBJ_INFECT(klass, super);
84  return (VALUE)klass;
85 }
86 
87 
94 void
96 {
97  if (!RB_TYPE_P(super, T_CLASS)) {
98  rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
99  rb_obj_classname(super));
100  }
101  if (RBASIC(super)->flags & FL_SINGLETON) {
102  rb_raise(rb_eTypeError, "can't make subclass of singleton class");
103  }
104  if (super == rb_cClass) {
105  rb_raise(rb_eTypeError, "can't make subclass of Class");
106  }
107 }
108 
109 
116 VALUE
118 {
119  Check_Type(super, T_CLASS);
120  rb_check_inheritable(super);
121  return rb_class_boot(super);
122 }
123 
124 static NODE*
125 rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass)
126 {
127  NODE *new_node;
128  if (!node) {
129  return NULL;
130  }
131  if (node->nd_clss == old_klass) {
132  new_node = NEW_CREF(new_klass);
133  new_node->nd_next = node->nd_next;
134  } else {
135  new_node = NEW_CREF(node->nd_clss);
136  new_node->nd_next = rewrite_cref_stack(node->nd_next, old_klass, new_klass);
137  }
138  return new_node;
139 }
140 
141 static void
142 clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
143 {
144  VALUE newiseqval;
145  if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
146  rb_iseq_t *iseq;
147  newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
148  GetISeqPtr(newiseqval, iseq);
149  iseq->cref_stack = rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass);
150  rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
151  RB_GC_GUARD(newiseqval);
152  }
153  else {
154  rb_method_entry_set(klass, mid, me, me->flag);
155  }
156 }
157 
158 static int
160 {
161  clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
162  return ST_CONTINUE;
163 }
164 
165 static int
167 {
169  *nce = *ce;
170  st_insert(tbl, key, (st_data_t)nce);
171  return ST_CONTINUE;
172 }
173 
174 static int
176 {
177  return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
178 }
179 
180 static void
182 {
183  if (orig == rb_cBasicObject) {
184  rb_raise(rb_eTypeError, "can't copy the root class");
185  }
186  if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
187  rb_raise(rb_eTypeError, "already initialized class");
188  }
189  if (FL_TEST(orig, FL_SINGLETON)) {
190  rb_raise(rb_eTypeError, "can't copy singleton class");
191  }
192 }
193 
194 /* :nodoc: */
195 VALUE
197 {
198  if (RB_TYPE_P(clone, T_CLASS)) {
199  class_init_copy_check(clone, orig);
200  }
201  rb_obj_init_copy(clone, orig);
202  if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
203  RBASIC(clone)->klass = rb_singleton_class_clone(orig);
204  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
205  }
206  RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
207  RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
208  if (RCLASS_IV_TBL(orig)) {
209  st_data_t id;
210 
211  if (RCLASS_IV_TBL(clone)) {
213  }
214  RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
215  CONST_ID(id, "__tmp_classpath__");
216  st_delete(RCLASS_IV_TBL(clone), &id, 0);
217  CONST_ID(id, "__classpath__");
218  st_delete(RCLASS_IV_TBL(clone), &id, 0);
219  CONST_ID(id, "__classid__");
220  st_delete(RCLASS_IV_TBL(clone), &id, 0);
221  }
222  if (RCLASS_CONST_TBL(orig)) {
223  if (RCLASS_CONST_TBL(clone)) {
225  }
228  }
229  if (RCLASS_M_TBL(orig)) {
230  if (RCLASS_M_TBL(clone)) {
232  }
233  RCLASS_M_TBL(clone) = st_init_numtable();
235  }
236 
237  return clone;
238 }
239 
240 VALUE
242 {
244 }
245 
246 VALUE
248 {
249  VALUE klass = RBASIC(obj)->klass;
250 
251  if (!FL_TEST(klass, FL_SINGLETON))
252  return klass;
253  else {
254  /* copy singleton(unnamed) class */
255  VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
256 
257  if (BUILTIN_TYPE(obj) == T_CLASS) {
258  RBASIC(clone)->klass = clone;
259  }
260  else {
261  RBASIC(clone)->klass = rb_singleton_class_clone(klass);
262  }
263 
264  RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
265  RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
266  if (RCLASS_IV_TBL(klass)) {
267  RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
268  }
269  if (RCLASS_CONST_TBL(klass)) {
272  }
273  if (attach != Qundef) {
274  rb_singleton_class_attached(clone, attach);
275  }
276  RCLASS_M_TBL(clone) = st_init_numtable();
278  rb_singleton_class_attached(RBASIC(clone)->klass, clone);
279  FL_SET(clone, FL_SINGLETON);
280  return clone;
281  }
282 }
283 
288 void
290 {
291  if (FL_TEST(klass, FL_SINGLETON)) {
292  if (!RCLASS_IV_TBL(klass)) {
293  RCLASS_IV_TBL(klass) = st_init_numtable();
294  }
295  st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
296  }
297 }
298 
299 
300 
301 #define METACLASS_OF(k) RBASIC(k)->klass
302 
308 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
309 
315 #define HAVE_METACLASS_P(k) \
316  (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
317  rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
318 
326 #define ENSURE_EIGENCLASS(klass) \
327  (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
328 
329 
339 static inline VALUE
341 {
342  VALUE super;
343  VALUE metaclass = rb_class_boot(Qundef);
344 
345  FL_SET(metaclass, FL_SINGLETON);
346  rb_singleton_class_attached(metaclass, klass);
347 
348  if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
349  METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
350  }
351  else {
352  VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
353  METACLASS_OF(klass) = metaclass;
354  METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
355  }
356 
357  super = RCLASS_SUPER(klass);
358  while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
359  RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
360 
361  OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
362 
363  return metaclass;
364 }
365 
372 static inline VALUE
374 {
375  VALUE orig_class = RBASIC(obj)->klass;
376  VALUE klass = rb_class_boot(orig_class);
377 
378  FL_SET(klass, FL_SINGLETON);
379  RBASIC(obj)->klass = klass;
380  rb_singleton_class_attached(klass, obj);
381 
382  METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
383  return klass;
384 }
385 
386 
387 static VALUE
388 boot_defclass(const char *name, VALUE super)
389 {
390  extern st_table *rb_class_tbl;
391  VALUE obj = rb_class_boot(super);
392  ID id = rb_intern(name);
393 
394  rb_name_class(obj, id);
395  st_add_direct(rb_class_tbl, id, obj);
396  rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
397  return obj;
398 }
399 
400 void
402 {
403  id_attached = rb_intern("__attached__");
404 
405  rb_cBasicObject = boot_defclass("BasicObject", 0);
407  rb_cModule = boot_defclass("Module", rb_cObject);
408  rb_cClass = boot_defclass("Class", rb_cModule);
409 
411  RBASIC(rb_cClass)->klass
412  = RBASIC(rb_cModule)->klass
413  = RBASIC(rb_cObject)->klass
414  = RBASIC(rb_cBasicObject)->klass
415  = rb_cClass;
416 }
417 
418 
429 VALUE
431 {
432  if (BUILTIN_TYPE(obj) == T_CLASS) {
433  return make_metaclass(obj);
434  }
435  else {
436  return make_singleton_class(obj);
437  }
438 }
439 
440 
451 VALUE
453 {
454  VALUE klass;
455 
456  if (!super) super = rb_cObject;
457  klass = rb_class_new(super);
458  rb_make_metaclass(klass, RBASIC(super)->klass);
459 
460  return klass;
461 }
462 
463 
472 VALUE
474 {
475  ID inherited;
476  if (!super) super = rb_cObject;
477  CONST_ID(inherited, "inherited");
478  return rb_funcall(super, inherited, 1, klass);
479 }
480 
481 
482 
498 VALUE
499 rb_define_class(const char *name, VALUE super)
500 {
501  VALUE klass;
502  ID id;
503 
504  id = rb_intern(name);
505  if (rb_const_defined(rb_cObject, id)) {
506  klass = rb_const_get(rb_cObject, id);
507  if (!RB_TYPE_P(klass, T_CLASS)) {
508  rb_raise(rb_eTypeError, "%s is not a class", name);
509  }
510  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
511  rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
512  }
513  return klass;
514  }
515  if (!super) {
516  rb_warn("no super class for `%s', Object assumed", name);
517  }
518  klass = rb_define_class_id(id, super);
519  st_add_direct(rb_class_tbl, id, klass);
520  rb_name_class(klass, id);
521  rb_const_set(rb_cObject, id, klass);
522  rb_class_inherited(super, klass);
523 
524  return klass;
525 }
526 
527 
544 VALUE
545 rb_define_class_under(VALUE outer, const char *name, VALUE super)
546 {
547  return rb_define_class_id_under(outer, rb_intern(name), super);
548 }
549 
550 
567 VALUE
569 {
570  VALUE klass;
571 
572  if (rb_const_defined_at(outer, id)) {
573  klass = rb_const_get_at(outer, id);
574  if (!RB_TYPE_P(klass, T_CLASS)) {
575  rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
576  }
577  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
578  rb_name_error(id, "%s is already defined", rb_id2name(id));
579  }
580  return klass;
581  }
582  if (!super) {
583  rb_warn("no super class for `%s::%s', Object assumed",
584  rb_class2name(outer), rb_id2name(id));
585  }
586  klass = rb_define_class_id(id, super);
587  rb_set_class_path_string(klass, outer, rb_id2str(id));
588  rb_const_set(outer, id, klass);
589  rb_class_inherited(super, klass);
591 
592  return klass;
593 }
594 
595 VALUE
597 {
599 
601 
602  return (VALUE)mdl;
603 }
604 
605 VALUE
607 {
608  VALUE mdl;
609 
610  mdl = rb_module_new();
611  rb_name_class(mdl, id);
612 
613  return mdl;
614 }
615 
616 VALUE
617 rb_define_module(const char *name)
618 {
619  VALUE module;
620  ID id;
621 
622  id = rb_intern(name);
623  if (rb_const_defined(rb_cObject, id)) {
624  module = rb_const_get(rb_cObject, id);
625  if (RB_TYPE_P(module, T_MODULE))
626  return module;
627  rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
628  }
629  module = rb_define_module_id(id);
630  st_add_direct(rb_class_tbl, id, module);
631  rb_const_set(rb_cObject, id, module);
632 
633  return module;
634 }
635 
636 VALUE
637 rb_define_module_under(VALUE outer, const char *name)
638 {
639  return rb_define_module_id_under(outer, rb_intern(name));
640 }
641 
642 VALUE
644 {
645  VALUE module;
646 
647  if (rb_const_defined_at(outer, id)) {
648  module = rb_const_get_at(outer, id);
649  if (RB_TYPE_P(module, T_MODULE))
650  return module;
651  rb_raise(rb_eTypeError, "%s::%s is not a module",
652  rb_class2name(outer), rb_obj_classname(module));
653  }
654  module = rb_define_module_id(id);
655  rb_const_set(outer, id, module);
656  rb_set_class_path_string(module, outer, rb_id2str(id));
658 
659  return module;
660 }
661 
662 VALUE
664 {
666 
667  if (BUILTIN_TYPE(module) == T_ICLASS) {
668  module = RBASIC(module)->klass;
669  }
670  if (!RCLASS_IV_TBL(module)) {
671  RCLASS_IV_TBL(module) = st_init_numtable();
672  }
673  if (!RCLASS_CONST_TBL(module)) {
675  }
676  RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
677  RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
678  RCLASS_M_TBL(klass) = RCLASS_M_TBL(RCLASS_ORIGIN(module));
679  RCLASS_SUPER(klass) = super;
680  if (RB_TYPE_P(module, T_ICLASS)) {
681  RBASIC(klass)->klass = RBASIC(module)->klass;
682  }
683  else {
684  RBASIC(klass)->klass = module;
685  }
686  OBJ_INFECT(klass, module);
687  OBJ_INFECT(klass, super);
688 
689  return (VALUE)klass;
690 }
691 
692 static int include_modules_at(const VALUE klass, VALUE c, VALUE module);
693 
694 void
696 {
697  int changed = 0;
698 
699  rb_frozen_class_p(klass);
700  if (!OBJ_UNTRUSTED(klass)) {
701  rb_secure(4);
702  }
703 
704  if (!RB_TYPE_P(module, T_MODULE)) {
705  Check_Type(module, T_MODULE);
706  }
707 
708  OBJ_INFECT(klass, module);
709 
710  changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module);
711  if (changed < 0)
712  rb_raise(rb_eArgError, "cyclic include detected");
713  if (changed) rb_clear_cache();
714 }
715 
716 static int
718 {
719  rb_add_refined_method_entry((VALUE) data, (ID) key);
720  return ST_CONTINUE;
721 }
722 
723 static int
724 include_modules_at(const VALUE klass, VALUE c, VALUE module)
725 {
726  VALUE p;
727  int changed = 0;
728  const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
729 
730  while (module) {
731  int superclass_seen = FALSE;
732 
733  if (RCLASS_ORIGIN(module) != module)
734  goto skip;
735  if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
736  return -1;
737  /* ignore if the module included already in superclasses */
738  for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
739  switch (BUILTIN_TYPE(p)) {
740  case T_ICLASS:
741  if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
742  if (!superclass_seen) {
743  c = p; /* move insertion point */
744  }
745  goto skip;
746  }
747  break;
748  case T_CLASS:
749  superclass_seen = TRUE;
750  break;
751  }
752  }
753  c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c));
754  if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
755  VALUE refined_class =
757 
759  (st_data_t) refined_class);
761  }
762  if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
763  changed = 1;
764  skip:
765  module = RCLASS_SUPER(module);
766  }
767 
768  return changed;
769 }
770 
771 static int
773 {
774  rb_method_entry_t *me = (rb_method_entry_t *) value;
775  st_table *tbl = (st_table *) data;
776 
777  if (me->def->type == VM_METHOD_TYPE_REFINED) {
778  if (me->def->body.orig_me) {
779  rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me;
780  me->def->body.orig_me = NULL;
781  new_me = ALLOC(rb_method_entry_t);
782  *new_me = *me;
783  st_add_direct(tbl, key, (st_data_t) new_me);
784  *me = *orig_me;
785  xfree(orig_me);
786  return ST_CONTINUE;
787  }
788  else {
789  st_add_direct(tbl, key, (st_data_t) me);
790  return ST_DELETE;
791  }
792  }
793  else {
794  return ST_CONTINUE;
795  }
796 }
797 
798 void
800 {
802  VALUE origin;
803  int changed = 0;
804 
805  rb_frozen_class_p(klass);
806  if (!OBJ_UNTRUSTED(klass)) {
807  rb_secure(4);
808  }
809 
810  Check_Type(module, T_MODULE);
811 
812  OBJ_INFECT(klass, module);
813 
814  origin = RCLASS_ORIGIN(klass);
815  if (origin == klass) {
816  origin = class_alloc(T_ICLASS, klass);
817  RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
818  RCLASS_SUPER(klass) = origin;
819  RCLASS_ORIGIN(klass) = origin;
820  RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
821  RCLASS_M_TBL(klass) = st_init_numtable();
823  (st_data_t) RCLASS_M_TBL(klass));
824  }
825  changed = include_modules_at(klass, klass, module);
826  if (changed < 0)
827  rb_raise(rb_eArgError, "cyclic prepend detected");
828  if (changed) {
829  rb_clear_cache();
831  }
832 }
833 
834 /*
835  * call-seq:
836  * mod.included_modules -> array
837  *
838  * Returns the list of modules included in <i>mod</i>.
839  *
840  * module Mixin
841  * end
842  *
843  * module Outer
844  * include Mixin
845  * end
846  *
847  * Mixin.included_modules #=> []
848  * Outer.included_modules #=> [Mixin]
849  */
850 
851 VALUE
853 {
854  VALUE ary = rb_ary_new();
855  VALUE p;
856  VALUE origin = RCLASS_ORIGIN(mod);
857 
858  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
859  if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
860  VALUE m = RBASIC(p)->klass;
861  if (RB_TYPE_P(m, T_MODULE))
862  rb_ary_push(ary, m);
863  }
864  }
865  return ary;
866 }
867 
868 /*
869  * call-seq:
870  * mod.include?(module) -> true or false
871  *
872  * Returns <code>true</code> if <i>module</i> is included in
873  * <i>mod</i> or one of <i>mod</i>'s ancestors.
874  *
875  * module A
876  * end
877  * class B
878  * include A
879  * end
880  * class C < B
881  * end
882  * B.include?(A) #=> true
883  * C.include?(A) #=> true
884  * A.include?(A) #=> false
885  */
886 
887 VALUE
889 {
890  VALUE p;
891 
892  Check_Type(mod2, T_MODULE);
893  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
894  if (BUILTIN_TYPE(p) == T_ICLASS) {
895  if (RBASIC(p)->klass == mod2) return Qtrue;
896  }
897  }
898  return Qfalse;
899 }
900 
901 /*
902  * call-seq:
903  * mod.ancestors -> array
904  *
905  * Returns a list of modules included in <i>mod</i> (including
906  * <i>mod</i> itself).
907  *
908  * module Mod
909  * include Math
910  * include Comparable
911  * end
912  *
913  * Mod.ancestors #=> [Mod, Comparable, Math]
914  * Math.ancestors #=> [Math]
915  */
916 
917 VALUE
919 {
920  VALUE p, ary = rb_ary_new();
921 
922  for (p = mod; p; p = RCLASS_SUPER(p)) {
923  if (FL_TEST(p, FL_SINGLETON))
924  continue;
925  if (BUILTIN_TYPE(p) == T_ICLASS) {
926  rb_ary_push(ary, RBASIC(p)->klass);
927  }
928  else if (p == RCLASS_ORIGIN(p)) {
929  rb_ary_push(ary, p);
930  }
931  }
932  return ary;
933 }
934 
935 #define VISI(x) ((x)&NOEX_MASK)
936 #define VISI_CHECK(x,f) (VISI(x) == (f))
937 
938 static int
939 ins_methods_push(ID name, long type, VALUE ary, long visi)
940 {
941  if (type == -1) return ST_CONTINUE;
942 
943  switch (visi) {
944  case NOEX_PRIVATE:
945  case NOEX_PROTECTED:
946  case NOEX_PUBLIC:
947  visi = (type == visi);
948  break;
949  default:
950  visi = (type != NOEX_PRIVATE);
951  break;
952  }
953  if (visi) {
954  rb_ary_push(ary, ID2SYM(name));
955  }
956  return ST_CONTINUE;
957 }
958 
959 static int
961 {
962  return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
963 }
964 
965 static int
967 {
968  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
969 }
970 
971 static int
973 {
974  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
975 }
976 
977 static int
979 {
980  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
981 }
982 
983 static int
985 {
986  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
987  st_table *list = (st_table *)data;
988  long type;
989 
990  if (!st_lookup(list, key, 0)) {
991  if (UNDEFINED_METHOD_ENTRY_P(me)) {
992  type = -1; /* none */
993  }
994  else {
995  type = VISI(me->flag);
996  }
997  st_add_direct(list, key, type);
998  }
999  return ST_CONTINUE;
1000 }
1001 
1002 static VALUE
1004 {
1005  VALUE ary;
1006  int recur, prepended = 0;
1007  st_table *list;
1008 
1009  if (argc == 0) {
1010  recur = TRUE;
1011  }
1012  else {
1013  VALUE r;
1014  rb_scan_args(argc, argv, "01", &r);
1015  recur = RTEST(r);
1016  }
1017 
1018  if (!recur && RCLASS_ORIGIN(mod) != mod) {
1019  mod = RCLASS_ORIGIN(mod);
1020  prepended = 1;
1021  }
1022 
1023  list = st_init_numtable();
1024  for (; mod; mod = RCLASS_SUPER(mod)) {
1026  if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1027  if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1028  if (!recur) break;
1029  }
1030  ary = rb_ary_new();
1031  st_foreach(list, func, ary);
1032  st_free_table(list);
1033 
1034  return ary;
1035 }
1036 
1037 /*
1038  * call-seq:
1039  * mod.instance_methods(include_super=true) -> array
1040  *
1041  * Returns an array containing the names of the public and protected instance
1042  * methods in the receiver. For a module, these are the public and protected methods;
1043  * for a class, they are the instance (not singleton) methods. With no
1044  * argument, or with an argument that is <code>false</code>, the
1045  * instance methods in <i>mod</i> are returned, otherwise the methods
1046  * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
1047  *
1048  * module A
1049  * def method1() end
1050  * end
1051  * class B
1052  * def method2() end
1053  * end
1054  * class C < B
1055  * def method3() end
1056  * end
1057  *
1058  * A.instance_methods #=> [:method1]
1059  * B.instance_methods(false) #=> [:method2]
1060  * C.instance_methods(false) #=> [:method3]
1061  * C.instance_methods(true).length #=> 43
1062  */
1063 
1064 VALUE
1066 {
1067  return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1068 }
1069 
1070 /*
1071  * call-seq:
1072  * mod.protected_instance_methods(include_super=true) -> array
1073  *
1074  * Returns a list of the protected instance methods defined in
1075  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1076  * methods of any ancestors are included.
1077  */
1078 
1079 VALUE
1081 {
1082  return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1083 }
1084 
1085 /*
1086  * call-seq:
1087  * mod.private_instance_methods(include_super=true) -> array
1088  *
1089  * Returns a list of the private instance methods defined in
1090  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1091  * methods of any ancestors are included.
1092  *
1093  * module Mod
1094  * def method1() end
1095  * private :method1
1096  * def method2() end
1097  * end
1098  * Mod.instance_methods #=> [:method2]
1099  * Mod.private_instance_methods #=> [:method1]
1100  */
1101 
1102 VALUE
1104 {
1105  return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1106 }
1107 
1108 /*
1109  * call-seq:
1110  * mod.public_instance_methods(include_super=true) -> array
1111  *
1112  * Returns a list of the public instance methods defined in <i>mod</i>.
1113  * If the optional parameter is not <code>false</code>, the methods of
1114  * any ancestors are included.
1115  */
1116 
1117 VALUE
1119 {
1120  return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1121 }
1122 
1123 /*
1124  * call-seq:
1125  * obj.methods(all=true) -> array
1126  *
1127  * Returns a list of the names of public and protected methods of
1128  * <i>obj</i>. This will include all the methods accessible in
1129  * <i>obj</i>'s ancestors.
1130  * If the <i>all</i> parameter is set to <code>false</code>, only those methods
1131  * in the receiver will be listed.
1132  *
1133  * class Klass
1134  * def klass_method()
1135  * end
1136  * end
1137  * k = Klass.new
1138  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1139  * # :==~, :!, :eql?
1140  * # :hash, :<=>, :class, :singleton_class]
1141  * k.methods.length #=> 57
1142  */
1143 
1144 VALUE
1145 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1146 {
1147  retry:
1148  if (argc == 0) {
1149  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1150  }
1151  else {
1152  VALUE recur;
1153 
1154  rb_scan_args(argc, argv, "1", &recur);
1155  if (RTEST(recur)) {
1156  argc = 0;
1157  goto retry;
1158  }
1159  return rb_obj_singleton_methods(argc, argv, obj);
1160  }
1161 }
1162 
1163 /*
1164  * call-seq:
1165  * obj.protected_methods(all=true) -> array
1166  *
1167  * Returns the list of protected methods accessible to <i>obj</i>. If
1168  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1169  * in the receiver will be listed.
1170  */
1171 
1172 VALUE
1173 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1174 {
1175  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1176 }
1177 
1178 /*
1179  * call-seq:
1180  * obj.private_methods(all=true) -> array
1181  *
1182  * Returns the list of private methods accessible to <i>obj</i>. If
1183  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1184  * in the receiver will be listed.
1185  */
1186 
1187 VALUE
1188 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1189 {
1190  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1191 }
1192 
1193 /*
1194  * call-seq:
1195  * obj.public_methods(all=true) -> array
1196  *
1197  * Returns the list of public methods accessible to <i>obj</i>. If
1198  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1199  * in the receiver will be listed.
1200  */
1201 
1202 VALUE
1203 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1204 {
1205  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1206 }
1207 
1208 /*
1209  * call-seq:
1210  * obj.singleton_methods(all=true) -> array
1211  *
1212  * Returns an array of the names of singleton methods for <i>obj</i>.
1213  * If the optional <i>all</i> parameter is true, the list will include
1214  * methods in modules included in <i>obj</i>.
1215  * Only public and protected singleton methods are returned.
1216  *
1217  * module Other
1218  * def three() end
1219  * end
1220  *
1221  * class Single
1222  * def Single.four() end
1223  * end
1224  *
1225  * a = Single.new
1226  *
1227  * def a.one()
1228  * end
1229  *
1230  * class << a
1231  * include Other
1232  * def two()
1233  * end
1234  * end
1235  *
1236  * Single.singleton_methods #=> [:four]
1237  * a.singleton_methods(false) #=> [:two, :one]
1238  * a.singleton_methods #=> [:two, :one, :three]
1239  */
1240 
1241 VALUE
1242 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1243 {
1244  VALUE recur, ary, klass;
1245  st_table *list;
1246 
1247  if (argc == 0) {
1248  recur = Qtrue;
1249  }
1250  else {
1251  rb_scan_args(argc, argv, "01", &recur);
1252  }
1253  klass = CLASS_OF(obj);
1254  list = st_init_numtable();
1255  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1256  if (RCLASS_M_TBL(klass))
1258  klass = RCLASS_SUPER(klass);
1259  }
1260  if (RTEST(recur)) {
1261  while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1262  if (RCLASS_M_TBL(klass))
1264  klass = RCLASS_SUPER(klass);
1265  }
1266  }
1267  ary = rb_ary_new();
1268  st_foreach(list, ins_methods_i, ary);
1269  st_free_table(list);
1270 
1271  return ary;
1272 }
1273 
1331 void
1332 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1333 {
1334  rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1335 }
1336 
1337 void
1338 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1339 {
1340  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1341 }
1342 
1343 void
1344 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1345 {
1346  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1347 }
1348 
1349 void
1350 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1351 {
1352  rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1353 }
1354 
1355 void
1356 rb_undef_method(VALUE klass, const char *name)
1357 {
1359 }
1360 
1369 #define SPECIAL_SINGLETON(x,c) do {\
1370  if (obj == (x)) {\
1371  return (c);\
1372  }\
1373 } while (0)
1374 
1375 static inline VALUE
1377 {
1381  return Qnil;
1382 }
1383 
1384 VALUE
1386 {
1387  return special_singleton_class_of(obj);
1388 }
1389 
1399 static VALUE
1401 {
1402  VALUE klass;
1403 
1404  if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
1405  rb_raise(rb_eTypeError, "can't define singleton");
1406  }
1407  if (SPECIAL_CONST_P(obj)) {
1408  klass = special_singleton_class_of(obj);
1409  if (NIL_P(klass))
1410  rb_bug("unknown immediate %p", (void *)obj);
1411  return klass;
1412  }
1413  else {
1414  enum ruby_value_type type = BUILTIN_TYPE(obj);
1415  if (type == T_FLOAT || type == T_BIGNUM) {
1416  rb_raise(rb_eTypeError, "can't define singleton");
1417  }
1418  }
1419 
1420  if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1421  rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1422  klass = RBASIC(obj)->klass;
1423  }
1424  else {
1425  klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1426  }
1427 
1428  if (OBJ_TAINTED(obj)) {
1429  OBJ_TAINT(klass);
1430  }
1431  else {
1432  FL_UNSET(klass, FL_TAINT);
1433  }
1434  if (OBJ_UNTRUSTED(obj)) {
1435  OBJ_UNTRUST(klass);
1436  }
1437  else {
1438  FL_UNSET(klass, FL_UNTRUSTED);
1439  }
1440  if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1441 
1442  return klass;
1443 }
1444 
1445 
1463 VALUE
1465 {
1466  VALUE klass = singleton_class_of(obj);
1467 
1468  /* ensures an exposed class belongs to its own eigenclass */
1469  if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1470 
1471  return klass;
1472 }
1473 
1490 void
1491 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1492 {
1493  rb_define_method(singleton_class_of(obj), name, func, argc);
1494 }
1495 
1496 
1497 
1505 void
1506 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1507 {
1508  rb_define_private_method(module, name, func, argc);
1509  rb_define_singleton_method(module, name, func, argc);
1510 }
1511 
1512 
1519 void
1520 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1521 {
1523 }
1524 
1525 
1532 void
1533 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1534 {
1535  rb_alias(klass, rb_intern(name1), rb_intern(name2));
1536 }
1537 
1545 void
1546 rb_define_attr(VALUE klass, const char *name, int read, int write)
1547 {
1548  rb_attr(klass, rb_intern(name), read, write, FALSE);
1549 }
1550 
1551 int
1553 {
1554  const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0);
1555  if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1556  me->def->body.cfunc.func == rb_any_to_s)
1557  return 1;
1558  return 0;
1559 }
1560 
1561 #include <stdarg.h>
1562 
1563 int
1564 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1565 {
1566  int i;
1567  const char *p = fmt;
1568  VALUE *var;
1569  va_list vargs;
1570  int f_var = 0, f_hash = 0, f_block = 0;
1571  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1572  int argi = 0;
1573  VALUE hash = Qnil;
1574 
1575  if (ISDIGIT(*p)) {
1576  n_lead = *p - '0';
1577  p++;
1578  if (ISDIGIT(*p)) {
1579  n_opt = *p - '0';
1580  p++;
1581  if (ISDIGIT(*p)) {
1582  n_trail = *p - '0';
1583  p++;
1584  goto block_arg;
1585  }
1586  }
1587  }
1588  if (*p == '*') {
1589  f_var = 1;
1590  p++;
1591  if (ISDIGIT(*p)) {
1592  n_trail = *p - '0';
1593  p++;
1594  }
1595  }
1596  block_arg:
1597  if (*p == ':') {
1598  f_hash = 1;
1599  p++;
1600  }
1601  if (*p == '&') {
1602  f_block = 1;
1603  p++;
1604  }
1605  if (*p != '\0') {
1606  rb_fatal("bad scan arg format: %s", fmt);
1607  }
1608  n_mand = n_lead + n_trail;
1609 
1610  if (argc < n_mand)
1611  goto argc_error;
1612 
1613  va_start(vargs, fmt);
1614 
1615  /* capture an option hash - phase 1: pop */
1616  if (f_hash && n_mand < argc) {
1617  VALUE last = argv[argc - 1];
1618 
1619  if (NIL_P(last)) {
1620  /* nil is taken as an empty option hash only if it is not
1621  ambiguous; i.e. '*' is not specified and arguments are
1622  given more than sufficient */
1623  if (!f_var && n_mand + n_opt < argc)
1624  argc--;
1625  }
1626  else {
1627  hash = rb_check_hash_type(last);
1628  if (!NIL_P(hash))
1629  argc--;
1630  }
1631  }
1632  /* capture leading mandatory arguments */
1633  for (i = n_lead; i-- > 0; ) {
1634  var = va_arg(vargs, VALUE *);
1635  if (var) *var = argv[argi];
1636  argi++;
1637  }
1638  /* capture optional arguments */
1639  for (i = n_opt; i-- > 0; ) {
1640  var = va_arg(vargs, VALUE *);
1641  if (argi < argc - n_trail) {
1642  if (var) *var = argv[argi];
1643  argi++;
1644  }
1645  else {
1646  if (var) *var = Qnil;
1647  }
1648  }
1649  /* capture variable length arguments */
1650  if (f_var) {
1651  int n_var = argc - argi - n_trail;
1652 
1653  var = va_arg(vargs, VALUE *);
1654  if (0 < n_var) {
1655  if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1656  argi += n_var;
1657  }
1658  else {
1659  if (var) *var = rb_ary_new();
1660  }
1661  }
1662  /* capture trailing mandatory arguments */
1663  for (i = n_trail; i-- > 0; ) {
1664  var = va_arg(vargs, VALUE *);
1665  if (var) *var = argv[argi];
1666  argi++;
1667  }
1668  /* capture an option hash - phase 2: assignment */
1669  if (f_hash) {
1670  var = va_arg(vargs, VALUE *);
1671  if (var) *var = hash;
1672  }
1673  /* capture iterator block */
1674  if (f_block) {
1675  var = va_arg(vargs, VALUE *);
1676  if (rb_block_given_p()) {
1677  *var = rb_block_proc();
1678  }
1679  else {
1680  *var = Qnil;
1681  }
1682  }
1683  va_end(vargs);
1684 
1685  if (argi < argc) {
1686  argc_error:
1687  rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
1688  }
1689 
1690  return argc;
1691 }
1692