Ruby  2.4.2p198(2017-09-14revision59899)
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 "internal.h"
27 #include "ruby/st.h"
28 #include "constant.h"
29 #include "vm_core.h"
30 #include "id_table.h"
31 #include <ctype.h>
32 
33 #define id_attached id__attached__
34 
35 void
37 {
38  rb_subclass_entry_t *entry, *head;
39 
40  if (super && super != Qundef) {
41  entry = ALLOC(rb_subclass_entry_t);
42  entry->klass = klass;
43  entry->next = NULL;
44 
45  head = RCLASS_EXT(super)->subclasses;
46  if (head) {
47  entry->next = head;
48  RCLASS_EXT(head->klass)->parent_subclasses = &entry->next;
49  }
50 
51  RCLASS_EXT(super)->subclasses = entry;
52  RCLASS_EXT(klass)->parent_subclasses = &RCLASS_EXT(super)->subclasses;
53  }
54 }
55 
56 static void
58 {
59  rb_subclass_entry_t *entry, *head;
60 
61  entry = ALLOC(rb_subclass_entry_t);
62  entry->klass = iclass;
63  entry->next = NULL;
64 
65  head = RCLASS_EXT(module)->subclasses;
66  if (head) {
67  entry->next = head;
68  RCLASS_EXT(head->klass)->module_subclasses = &entry->next;
69  }
70 
71  RCLASS_EXT(module)->subclasses = entry;
72  RCLASS_EXT(iclass)->module_subclasses = &RCLASS_EXT(module)->subclasses;
73 }
74 
75 void
77 {
78  rb_subclass_entry_t *entry;
79 
80  if (RCLASS_EXT(klass)->parent_subclasses) {
81  entry = *RCLASS_EXT(klass)->parent_subclasses;
82 
83  *RCLASS_EXT(klass)->parent_subclasses = entry->next;
84  if (entry->next) {
85  RCLASS_EXT(entry->next->klass)->parent_subclasses = RCLASS_EXT(klass)->parent_subclasses;
86  }
87  xfree(entry);
88  }
89 
90  RCLASS_EXT(klass)->parent_subclasses = NULL;
91 }
92 
93 void
95 {
96  rb_subclass_entry_t *entry;
97 
98  if (RCLASS_EXT(klass)->module_subclasses) {
99  entry = *RCLASS_EXT(klass)->module_subclasses;
100  *RCLASS_EXT(klass)->module_subclasses = entry->next;
101 
102  if (entry->next) {
103  RCLASS_EXT(entry->next->klass)->module_subclasses = RCLASS_EXT(klass)->module_subclasses;
104  }
105 
106  xfree(entry);
107  }
108 
109  RCLASS_EXT(klass)->module_subclasses = NULL;
110 }
111 
112 void
114 {
115  rb_subclass_entry_t *cur = RCLASS_EXT(klass)->subclasses;
116 
117  /* do not be tempted to simplify this loop into a for loop, the order of
118  operations is important here if `f` modifies the linked list */
119  while (cur) {
120  VALUE curklass = cur->klass;
121  cur = cur->next;
122  f(curklass, arg);
123  }
124 }
125 
126 static void
128 {
130 }
131 
132 void
134 {
136 }
137 
138 static void
140 {
142 }
143 
144 void
146 {
148 }
149 
162 static VALUE
163 class_alloc(VALUE flags, VALUE klass)
164 {
165  NEWOBJ_OF(obj, struct RClass, klass, (flags & T_MASK) | FL_PROMOTED1 /* start from age == 2 */ | (RGENGC_WB_PROTECTED_CLASS ? FL_WB_PROTECTED : 0));
166  obj->ptr = ZALLOC(rb_classext_t);
167  /* ZALLOC
168  RCLASS_IV_TBL(obj) = 0;
169  RCLASS_CONST_TBL(obj) = 0;
170  RCLASS_M_TBL(obj) = 0;
171  RCLASS_IV_INDEX_TBL(obj) = 0;
172  RCLASS_SET_SUPER((VALUE)obj, 0);
173  RCLASS_EXT(obj)->subclasses = NULL;
174  RCLASS_EXT(obj)->parent_subclasses = NULL;
175  RCLASS_EXT(obj)->module_subclasses = NULL;
176  */
177  RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
179  RCLASS_REFINED_CLASS(obj) = Qnil;
180  RCLASS_EXT(obj)->allocator = 0;
181 
182  return (VALUE)obj;
183 }
184 
185 static void
187 {
189 }
190 
200 VALUE
202 {
204 
205  RCLASS_SET_SUPER(klass, super);
206  RCLASS_M_TBL_INIT(klass);
207 
208  OBJ_INFECT(klass, super);
209  return (VALUE)klass;
210 }
211 
212 
219 void
221 {
222  if (!RB_TYPE_P(super, T_CLASS)) {
223  rb_raise(rb_eTypeError, "superclass must be a Class (%"PRIsVALUE" given)",
224  rb_obj_class(super));
225  }
226  if (RBASIC(super)->flags & FL_SINGLETON) {
227  rb_raise(rb_eTypeError, "can't make subclass of singleton class");
228  }
229  if (super == rb_cClass) {
230  rb_raise(rb_eTypeError, "can't make subclass of Class");
231  }
232 }
233 
234 
241 VALUE
243 {
244  Check_Type(super, T_CLASS);
245  rb_check_inheritable(super);
246  return rb_class_boot(super);
247 }
248 
249 static void
250 clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
251 {
252  if (me->def->type == VM_METHOD_TYPE_ISEQ) {
253  rb_cref_t *new_cref;
254  rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass, &new_cref);
255  rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
256  }
257  else {
258  rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
259  }
260 }
261 
265 };
266 
267 static enum rb_id_table_iterator_result
268 clone_method_i(ID key, VALUE value, void *data)
269 {
270  const struct clone_method_arg *arg = (struct clone_method_arg *)data;
271  clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
272  return ID_TABLE_CONTINUE;
273 }
274 
277  struct rb_id_table *tbl;
278 };
279 
280 static int
282 {
284  MEMCPY(nce, ce, rb_const_entry_t, 1);
285  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
286  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
287 
288  rb_id_table_insert(arg->tbl, key, (VALUE)nce);
289  return ID_TABLE_CONTINUE;
290 }
291 
292 static enum rb_id_table_iterator_result
293 clone_const_i(ID key, VALUE value, void *data)
294 {
295  return clone_const(key, (const rb_const_entry_t *)value, data);
296 }
297 
298 static void
300 {
301  if (orig == rb_cBasicObject) {
302  rb_raise(rb_eTypeError, "can't copy the root class");
303  }
304  if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
305  rb_raise(rb_eTypeError, "already initialized class");
306  }
307  if (FL_TEST(orig, FL_SINGLETON)) {
308  rb_raise(rb_eTypeError, "can't copy singleton class");
309  }
310 }
311 
312 /* :nodoc: */
313 VALUE
315 {
316  if (RB_TYPE_P(clone, T_CLASS)) {
317  class_init_copy_check(clone, orig);
318  }
319  if (!OBJ_INIT_COPY(clone, orig)) return clone;
320  if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
322  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
323  }
324  RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
325  RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
326  if (RCLASS_IV_TBL(clone)) {
328  RCLASS_IV_TBL(clone) = 0;
329  }
330  if (RCLASS_CONST_TBL(clone)) {
332  RCLASS_CONST_TBL(clone) = 0;
333  }
334  RCLASS_M_TBL(clone) = 0;
335  if (RCLASS_IV_TBL(orig)) {
336  st_data_t id;
337 
338  RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(orig));
339  CONST_ID(id, "__tmp_classpath__");
340  st_delete(RCLASS_IV_TBL(clone), &id, 0);
341  CONST_ID(id, "__classpath__");
342  st_delete(RCLASS_IV_TBL(clone), &id, 0);
343  CONST_ID(id, "__classid__");
344  st_delete(RCLASS_IV_TBL(clone), &id, 0);
345  }
346  if (RCLASS_CONST_TBL(orig)) {
347  struct clone_const_arg arg;
348 
349  arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
350  arg.klass = clone;
352  }
353  if (RCLASS_M_TBL(orig)) {
354  struct clone_method_arg arg;
355  arg.old_klass = orig;
356  arg.new_klass = clone;
357  RCLASS_M_TBL_INIT(clone);
359  }
360 
361  return clone;
362 }
363 
364 VALUE
366 {
368 }
369 
370 VALUE
372 {
373  const VALUE klass = RBASIC(obj)->klass;
374 
375  if (!FL_TEST(klass, FL_SINGLETON))
376  return klass;
377  else {
378  /* copy singleton(unnamed) class */
379  VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
380 
381  if (BUILTIN_TYPE(obj) == T_CLASS) {
382  RBASIC_SET_CLASS(clone, clone);
383  }
384  else {
386  }
387 
388  RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
389  RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
390  if (RCLASS_IV_TBL(klass)) {
391  RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(klass));
392  }
393  if (RCLASS_CONST_TBL(klass)) {
394  struct clone_const_arg arg;
395  arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
396  arg.klass = clone;
398  }
399  if (attach != Qundef) {
400  rb_singleton_class_attached(clone, attach);
401  }
402  RCLASS_M_TBL_INIT(clone);
403  {
404  struct clone_method_arg arg;
405  arg.old_klass = klass;
406  arg.new_klass = clone;
408  }
409  rb_singleton_class_attached(RBASIC(clone)->klass, clone);
410  FL_SET(clone, FL_SINGLETON);
411 
412  return clone;
413  }
414 }
415 
420 void
422 {
423  if (FL_TEST(klass, FL_SINGLETON)) {
424  if (!RCLASS_IV_TBL(klass)) {
425  RCLASS_IV_TBL(klass) = st_init_numtable();
426  }
427  rb_class_ivar_set(klass, id_attached, obj);
428  }
429 }
430 
431 
432 
433 #define METACLASS_OF(k) RBASIC(k)->klass
434 #define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
435 
441 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
442 
443 static int
445 {
446  return rb_attr_get(METACLASS_OF(sklass), id_attached) == sklass;
447 }
448 
449 int
451 {
452  return (RB_TYPE_P(rb_attr_get(sklass, id_attached), T_CLASS) &&
454 }
455 
461 #define HAVE_METACLASS_P(k) \
462  (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
463  rb_singleton_class_has_metaclass_p(k))
464 
472 #define ENSURE_EIGENCLASS(klass) \
473  (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
474 
475 
485 static inline VALUE
487 {
488  VALUE super;
489  VALUE metaclass = rb_class_boot(Qundef);
490 
491  FL_SET(metaclass, FL_SINGLETON);
492  rb_singleton_class_attached(metaclass, klass);
493 
494  if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
495  SET_METACLASS_OF(klass, metaclass);
496  SET_METACLASS_OF(metaclass, metaclass);
497  }
498  else {
499  VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
500  SET_METACLASS_OF(klass, metaclass);
501  SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
502  }
503 
504  super = RCLASS_SUPER(klass);
505  while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
506  RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
507 
508  OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
509 
510  return metaclass;
511 }
512 
519 static inline VALUE
521 {
522  VALUE orig_class = RBASIC(obj)->klass;
523  VALUE klass = rb_class_boot(orig_class);
524 
525  FL_SET(klass, FL_SINGLETON);
526  RBASIC_SET_CLASS(obj, klass);
527  rb_singleton_class_attached(klass, obj);
528 
529  SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
530  return klass;
531 }
532 
533 
534 static VALUE
535 boot_defclass(const char *name, VALUE super)
536 {
537  VALUE obj = rb_class_boot(super);
538  ID id = rb_intern(name);
539 
540  rb_name_class(obj, id);
541  rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
542  return obj;
543 }
544 
545 void
547 {
548  rb_cBasicObject = boot_defclass("BasicObject", 0);
551 
552  /* resolve class name ASAP for order-independence */
554 
555  rb_cModule = boot_defclass("Module", rb_cObject);
556  rb_cClass = boot_defclass("Class", rb_cModule);
557 
563 }
564 
565 
576 VALUE
578 {
579  if (BUILTIN_TYPE(obj) == T_CLASS) {
580  return make_metaclass(obj);
581  }
582  else {
583  return make_singleton_class(obj);
584  }
585 }
586 
587 
598 VALUE
600 {
601  VALUE klass;
602 
603  if (!super) super = rb_cObject;
604  klass = rb_class_new(super);
605  rb_make_metaclass(klass, RBASIC(super)->klass);
606 
607  return klass;
608 }
609 
610 
619 VALUE
621 {
622  ID inherited;
623  if (!super) super = rb_cObject;
624  CONST_ID(inherited, "inherited");
625  return rb_funcall(super, inherited, 1, klass);
626 }
627 
628 
629 
645 VALUE
646 rb_define_class(const char *name, VALUE super)
647 {
648  VALUE klass;
649  ID id;
650 
651  id = rb_intern(name);
652  if (rb_const_defined(rb_cObject, id)) {
653  klass = rb_const_get(rb_cObject, id);
654  if (!RB_TYPE_P(klass, T_CLASS)) {
655  rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
656  name, rb_obj_class(klass));
657  }
658  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
659  rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
660  }
661  return klass;
662  }
663  if (!super) {
664  rb_raise(rb_eArgError, "no super class for `%s'", name);
665  }
666  klass = rb_define_class_id(id, super);
667  rb_vm_add_root_module(id, klass);
668  rb_name_class(klass, id);
669  rb_const_set(rb_cObject, id, klass);
670  rb_class_inherited(super, klass);
671 
672  return klass;
673 }
674 
675 
692 VALUE
693 rb_define_class_under(VALUE outer, const char *name, VALUE super)
694 {
695  return rb_define_class_id_under(outer, rb_intern(name), super);
696 }
697 
698 
715 VALUE
717 {
718  VALUE klass;
719 
720  if (rb_const_defined_at(outer, id)) {
721  klass = rb_const_get_at(outer, id);
722  if (!RB_TYPE_P(klass, T_CLASS)) {
723  rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
724  " (%"PRIsVALUE")",
725  outer, rb_id2str(id), rb_obj_class(klass));
726  }
727  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
728  rb_raise(rb_eTypeError, "superclass mismatch for class "
729  "%"PRIsVALUE"::%"PRIsVALUE""
730  " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
731  outer, rb_id2str(id), RCLASS_SUPER(klass), super);
732  }
733  return klass;
734  }
735  if (!super) {
736  rb_raise(rb_eArgError, "no super class for `%"PRIsVALUE"::%"PRIsVALUE"'",
737  rb_class_path(outer), rb_id2str(id));
738  }
739  klass = rb_define_class_id(id, super);
740  rb_set_class_path_string(klass, outer, rb_id2str(id));
741  rb_const_set(outer, id, klass);
742  rb_class_inherited(super, klass);
744 
745  return klass;
746 }
747 
748 VALUE
750 {
752  RCLASS_M_TBL_INIT(mdl);
753  return (VALUE)mdl;
754 }
755 
756 VALUE
758 {
759  VALUE mdl;
760 
761  mdl = rb_module_new();
762  rb_name_class(mdl, id);
763 
764  return mdl;
765 }
766 
767 VALUE
768 rb_define_module(const char *name)
769 {
770  VALUE module;
771  ID id;
772 
773  id = rb_intern(name);
774  if (rb_const_defined(rb_cObject, id)) {
775  module = rb_const_get(rb_cObject, id);
776  if (!RB_TYPE_P(module, T_MODULE)) {
777  rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
778  name, rb_obj_class(module));
779  }
780  return module;
781  }
782  module = rb_define_module_id(id);
783  rb_vm_add_root_module(id, module);
784  rb_const_set(rb_cObject, id, module);
785 
786  return module;
787 }
788 
789 VALUE
790 rb_define_module_under(VALUE outer, const char *name)
791 {
792  return rb_define_module_id_under(outer, rb_intern(name));
793 }
794 
795 VALUE
797 {
798  VALUE module;
799 
800  if (rb_const_defined_at(outer, id)) {
801  module = rb_const_get_at(outer, id);
802  if (!RB_TYPE_P(module, T_MODULE)) {
803  rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
804  " (%"PRIsVALUE")",
805  outer, rb_id2str(id), rb_obj_class(module));
806  }
807  return module;
808  }
809  module = rb_define_module_id(id);
810  rb_const_set(outer, id, module);
811  rb_set_class_path_string(module, outer, rb_id2str(id));
813 
814  return module;
815 }
816 
817 VALUE
819 {
821 
822  if (BUILTIN_TYPE(module) == T_ICLASS) {
823  module = RBASIC(module)->klass;
824  }
825  if (!RCLASS_IV_TBL(module)) {
826  RCLASS_IV_TBL(module) = st_init_numtable();
827  }
828  if (!RCLASS_CONST_TBL(module)) {
830  }
831  RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
832  RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
833 
835  RCLASS_M_TBL(OBJ_WB_UNPROTECT(RCLASS_ORIGIN(module))); /* TODO: unprotected? */
836 
837  RCLASS_SET_SUPER(klass, super);
838  if (RB_TYPE_P(module, T_ICLASS)) {
839  RBASIC_SET_CLASS(klass, RBASIC(module)->klass);
840  }
841  else {
842  RBASIC_SET_CLASS(klass, module);
843  }
844  OBJ_INFECT(klass, module);
845  OBJ_INFECT(klass, super);
846 
847  return (VALUE)klass;
848 }
849 
850 static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
851 
852 static void
854 {
855  rb_frozen_class_p(klass);
856  Check_Type(module, T_MODULE);
858  rb_raise(rb_eArgError, "refinement module is not allowed");
859  }
860  OBJ_INFECT(klass, module);
861 }
862 
863 void
865 {
866  int changed = 0;
867 
868  ensure_includable(klass, module);
869 
870  changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
871  if (changed < 0)
872  rb_raise(rb_eArgError, "cyclic include detected");
873 }
874 
875 static enum rb_id_table_iterator_result
877 {
879  return ID_TABLE_CONTINUE;
880 }
881 
882 static int
883 include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
884 {
885  VALUE p, iclass;
886  int method_changed = 0, constant_changed = 0;
887  struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
888 
889  while (module) {
890  int superclass_seen = FALSE;
891  struct rb_id_table *tbl;
892 
893  if (RCLASS_ORIGIN(module) != module)
894  goto skip;
895  if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
896  return -1;
897  /* ignore if the module included already in superclasses */
898  for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
899  int type = BUILTIN_TYPE(p);
900  if (type == T_ICLASS) {
901  if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
902  if (!superclass_seen) {
903  c = p; /* move insertion point */
904  }
905  goto skip;
906  }
907  }
908  else if (type == T_CLASS) {
909  if (!search_super) break;
910  superclass_seen = TRUE;
911  }
912  }
913  iclass = rb_include_class_new(module, RCLASS_SUPER(c));
914  c = RCLASS_SET_SUPER(c, iclass);
915 
916  {
917  VALUE m = module;
918  if (BUILTIN_TYPE(m) == T_ICLASS) m = RBASIC(m)->klass;
920  }
921 
922  if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
923  VALUE refined_class =
925 
926  rb_id_table_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
928  }
929 
930  tbl = RMODULE_M_TBL(module);
931  if (tbl && rb_id_table_size(tbl)) method_changed = 1;
932 
933  tbl = RMODULE_CONST_TBL(module);
934  if (tbl && rb_id_table_size(tbl)) constant_changed = 1;
935  skip:
936  module = RCLASS_SUPER(module);
937  }
938 
939  if (method_changed) rb_clear_method_cache_by_class(klass);
940  if (constant_changed) rb_clear_constant_cache();
941 
942  return method_changed;
943 }
944 
945 static enum rb_id_table_iterator_result
946 move_refined_method(ID key, VALUE value, void *data)
947 {
948  rb_method_entry_t *me = (rb_method_entry_t *) value;
949  VALUE klass = (VALUE)data;
950  struct rb_id_table *tbl = RCLASS_M_TBL(klass);
951 
952  if (me->def->type == VM_METHOD_TYPE_REFINED) {
953  if (me->def->body.refined.orig_me) {
954  const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
955  RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
956  new_me = rb_method_entry_clone(me);
957  rb_id_table_insert(tbl, key, (VALUE)new_me);
958  RB_OBJ_WRITTEN(klass, Qundef, new_me);
959  rb_method_entry_copy(me, orig_me);
960  return ID_TABLE_CONTINUE;
961  }
962  else {
963  rb_id_table_insert(tbl, key, (VALUE)me);
964  return ID_TABLE_DELETE;
965  }
966  }
967  else {
968  return ID_TABLE_CONTINUE;
969  }
970 }
971 
972 void
974 {
975  VALUE origin;
976  int changed = 0;
977 
978  ensure_includable(klass, module);
979 
980  origin = RCLASS_ORIGIN(klass);
981  if (origin == klass) {
982  origin = class_alloc(T_ICLASS, klass);
983  OBJ_WB_UNPROTECT(origin); /* TODO: conservative shading. Need more survey. */
984  RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
985  RCLASS_SET_SUPER(klass, origin);
986  RCLASS_SET_ORIGIN(klass, origin);
987  RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
988  RCLASS_M_TBL_INIT(klass);
989  rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
990  }
991  changed = include_modules_at(klass, klass, module, FALSE);
992  if (changed < 0)
993  rb_raise(rb_eArgError, "cyclic prepend detected");
994  if (changed) {
996  }
997 }
998 
999 /*
1000  * call-seq:
1001  * mod.included_modules -> array
1002  *
1003  * Returns the list of modules included in <i>mod</i>.
1004  *
1005  * module Mixin
1006  * end
1007  *
1008  * module Outer
1009  * include Mixin
1010  * end
1011  *
1012  * Mixin.included_modules #=> []
1013  * Outer.included_modules #=> [Mixin]
1014  */
1015 
1016 VALUE
1018 {
1019  VALUE ary = rb_ary_new();
1020  VALUE p;
1021  VALUE origin = RCLASS_ORIGIN(mod);
1022 
1023  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1024  if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
1025  VALUE m = RBASIC(p)->klass;
1026  if (RB_TYPE_P(m, T_MODULE))
1027  rb_ary_push(ary, m);
1028  }
1029  }
1030  return ary;
1031 }
1032 
1033 /*
1034  * call-seq:
1035  * mod.include?(module) -> true or false
1036  *
1037  * Returns <code>true</code> if <i>module</i> is included in
1038  * <i>mod</i> or one of <i>mod</i>'s ancestors.
1039  *
1040  * module A
1041  * end
1042  * class B
1043  * include A
1044  * end
1045  * class C < B
1046  * end
1047  * B.include?(A) #=> true
1048  * C.include?(A) #=> true
1049  * A.include?(A) #=> false
1050  */
1051 
1052 VALUE
1054 {
1055  VALUE p;
1056 
1057  Check_Type(mod2, T_MODULE);
1058  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1059  if (BUILTIN_TYPE(p) == T_ICLASS) {
1060  if (RBASIC(p)->klass == mod2) return Qtrue;
1061  }
1062  }
1063  return Qfalse;
1064 }
1065 
1066 /*
1067  * call-seq:
1068  * mod.ancestors -> array
1069  *
1070  * Returns a list of modules included/prepended in <i>mod</i>
1071  * (including <i>mod</i> itself).
1072  *
1073  * module Mod
1074  * include Math
1075  * include Comparable
1076  * prepend Enumerable
1077  * end
1078  *
1079  * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1080  * Math.ancestors #=> [Math]
1081  * Enumerable.ancestors #=> [Enumerable]
1082  */
1083 
1084 VALUE
1086 {
1087  VALUE p, ary = rb_ary_new();
1088 
1089  for (p = mod; p; p = RCLASS_SUPER(p)) {
1090  if (BUILTIN_TYPE(p) == T_ICLASS) {
1091  rb_ary_push(ary, RBASIC(p)->klass);
1092  }
1093  else if (p == RCLASS_ORIGIN(p)) {
1094  rb_ary_push(ary, p);
1095  }
1096  }
1097  return ary;
1098 }
1099 
1100 static void
1102 {
1103  rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1104 }
1105 
1106 static int
1108 {
1109  switch ((rb_method_visibility_t)type) {
1110  case METHOD_VISI_UNDEF:
1111  case METHOD_VISI_PRIVATE:
1112  break;
1113  default: /* everything but private */
1114  ins_methods_push(name, ary);
1115  break;
1116  }
1117  return ST_CONTINUE;
1118 }
1119 
1120 static int
1122 {
1124  ins_methods_push(name, ary);
1125  }
1126  return ST_CONTINUE;
1127 }
1128 
1129 static int
1131 {
1133  ins_methods_push(name, ary);
1134  }
1135  return ST_CONTINUE;
1136 }
1137 
1138 static int
1140 {
1142  ins_methods_push(name, ary);
1143  }
1144  return ST_CONTINUE;
1145 }
1146 
1149  int recur;
1150 };
1151 
1152 static enum rb_id_table_iterator_result
1153 method_entry_i(ID key, VALUE value, void *data)
1154 {
1155  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1156  struct method_entry_arg *arg = (struct method_entry_arg *)data;
1158 
1159  if (me->def->type == VM_METHOD_TYPE_REFINED) {
1160  VALUE owner = me->owner;
1161  me = rb_resolve_refined_method(Qnil, me);
1162  if (!me) return ID_TABLE_CONTINUE;
1163  if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1164  }
1165  if (!st_lookup(arg->list, key, 0)) {
1166  if (UNDEFINED_METHOD_ENTRY_P(me)) {
1167  type = METHOD_VISI_UNDEF; /* none */
1168  }
1169  else {
1170  type = METHOD_ENTRY_VISI(me);
1171  }
1172  st_add_direct(arg->list, key, (st_data_t)type);
1173  }
1174  return ID_TABLE_CONTINUE;
1175 }
1176 
1177 static VALUE
1179 {
1180  VALUE ary;
1181  int recur, prepended = 0;
1182  struct method_entry_arg me_arg;
1183 
1184  if (argc == 0) {
1185  recur = TRUE;
1186  }
1187  else {
1188  VALUE r;
1189  rb_scan_args(argc, argv, "01", &r);
1190  recur = RTEST(r);
1191  }
1192 
1193  if (!recur && RCLASS_ORIGIN(mod) != mod) {
1194  mod = RCLASS_ORIGIN(mod);
1195  prepended = 1;
1196  }
1197 
1198  me_arg.list = st_init_numtable();
1199  me_arg.recur = recur;
1200  for (; mod; mod = RCLASS_SUPER(mod)) {
1201  if (RCLASS_M_TBL(mod)) rb_id_table_foreach(RCLASS_M_TBL(mod), method_entry_i, &me_arg);
1202  if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1203  if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1204  if (!recur) break;
1205  }
1206  ary = rb_ary_new();
1207  st_foreach(me_arg.list, func, ary);
1208  st_free_table(me_arg.list);
1209 
1210  return ary;
1211 }
1212 
1213 /*
1214  * call-seq:
1215  * mod.instance_methods(include_super=true) -> array
1216  *
1217  * Returns an array containing the names of the public and protected instance
1218  * methods in the receiver. For a module, these are the public and protected methods;
1219  * for a class, they are the instance (not singleton) methods. If the optional
1220  * parameter is <code>false</code>, the methods of any ancestors are not included.
1221  *
1222  * module A
1223  * def method1() end
1224  * end
1225  * class B
1226  * include A
1227  * def method2() end
1228  * end
1229  * class C < B
1230  * def method3() end
1231  * end
1232  *
1233  * A.instance_methods(false) #=> [:method1]
1234  * B.instance_methods(false) #=> [:method2]
1235  * B.instance_methods(true).include?(:method1) #=> true
1236  * C.instance_methods(false) #=> [:method3]
1237  * C.instance_methods.include?(:method2) #=> true
1238  */
1239 
1240 VALUE
1241 rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1242 {
1243  return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1244 }
1245 
1246 /*
1247  * call-seq:
1248  * mod.protected_instance_methods(include_super=true) -> array
1249  *
1250  * Returns a list of the protected instance methods defined in
1251  * <i>mod</i>. If the optional parameter is <code>false</code>, the
1252  * methods of any ancestors are not included.
1253  */
1254 
1255 VALUE
1257 {
1258  return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1259 }
1260 
1261 /*
1262  * call-seq:
1263  * mod.private_instance_methods(include_super=true) -> array
1264  *
1265  * Returns a list of the private instance methods defined in
1266  * <i>mod</i>. If the optional parameter is <code>false</code>, the
1267  * methods of any ancestors are not included.
1268  *
1269  * module Mod
1270  * def method1() end
1271  * private :method1
1272  * def method2() end
1273  * end
1274  * Mod.instance_methods #=> [:method2]
1275  * Mod.private_instance_methods #=> [:method1]
1276  */
1277 
1278 VALUE
1279 rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
1280 {
1281  return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1282 }
1283 
1284 /*
1285  * call-seq:
1286  * mod.public_instance_methods(include_super=true) -> array
1287  *
1288  * Returns a list of the public instance methods defined in <i>mod</i>.
1289  * If the optional parameter is <code>false</code>, the methods of
1290  * any ancestors are not included.
1291  */
1292 
1293 VALUE
1294 rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
1295 {
1296  return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1297 }
1298 
1299 /*
1300  * call-seq:
1301  * obj.methods(regular=true) -> array
1302  *
1303  * Returns a list of the names of public and protected methods of
1304  * <i>obj</i>. This will include all the methods accessible in
1305  * <i>obj</i>'s ancestors.
1306  * If the optional parameter is <code>false</code>, it
1307  * returns an array of <i>obj<i>'s public and protected singleton methods,
1308  * the array will not include methods in modules included in <i>obj</i>.
1309  *
1310  * class Klass
1311  * def klass_method()
1312  * end
1313  * end
1314  * k = Klass.new
1315  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1316  * # :==~, :!, :eql?
1317  * # :hash, :<=>, :class, :singleton_class]
1318  * k.methods.length #=> 56
1319  *
1320  * k.methods(false) #=> []
1321  * def k.singleton_method; end
1322  * k.methods(false) #=> [:singleton_method]
1323  *
1324  * module M123; def m123; end end
1325  * k.extend M123
1326  * k.methods(false) #=> [:singleton_method]
1327  */
1328 
1329 VALUE
1330 rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
1331 {
1332  rb_check_arity(argc, 0, 1);
1333  if (argc > 0 && !RTEST(argv[0])) {
1334  return rb_obj_singleton_methods(argc, argv, obj);
1335  }
1336  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1337 }
1338 
1339 /*
1340  * call-seq:
1341  * obj.protected_methods(all=true) -> array
1342  *
1343  * Returns the list of protected methods accessible to <i>obj</i>. If
1344  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1345  * in the receiver will be listed.
1346  */
1347 
1348 VALUE
1349 rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
1350 {
1351  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1352 }
1353 
1354 /*
1355  * call-seq:
1356  * obj.private_methods(all=true) -> array
1357  *
1358  * Returns the list of private methods accessible to <i>obj</i>. If
1359  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1360  * in the receiver will be listed.
1361  */
1362 
1363 VALUE
1364 rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
1365 {
1366  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1367 }
1368 
1369 /*
1370  * call-seq:
1371  * obj.public_methods(all=true) -> array
1372  *
1373  * Returns the list of public methods accessible to <i>obj</i>. If
1374  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1375  * in the receiver will be listed.
1376  */
1377 
1378 VALUE
1379 rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
1380 {
1381  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1382 }
1383 
1384 /*
1385  * call-seq:
1386  * obj.singleton_methods(all=true) -> array
1387  *
1388  * Returns an array of the names of singleton methods for <i>obj</i>.
1389  * If the optional <i>all</i> parameter is true, the list will include
1390  * methods in modules included in <i>obj</i>.
1391  * Only public and protected singleton methods are returned.
1392  *
1393  * module Other
1394  * def three() end
1395  * end
1396  *
1397  * class Single
1398  * def Single.four() end
1399  * end
1400  *
1401  * a = Single.new
1402  *
1403  * def a.one()
1404  * end
1405  *
1406  * class << a
1407  * include Other
1408  * def two()
1409  * end
1410  * end
1411  *
1412  * Single.singleton_methods #=> [:four]
1413  * a.singleton_methods(false) #=> [:two, :one]
1414  * a.singleton_methods #=> [:two, :one, :three]
1415  */
1416 
1417 VALUE
1418 rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
1419 {
1420  VALUE recur, ary, klass, origin;
1421  struct method_entry_arg me_arg;
1422  struct rb_id_table *mtbl;
1423 
1424  if (argc == 0) {
1425  recur = Qtrue;
1426  }
1427  else {
1428  rb_scan_args(argc, argv, "01", &recur);
1429  }
1430  klass = CLASS_OF(obj);
1431  origin = RCLASS_ORIGIN(klass);
1432  me_arg.list = st_init_numtable();
1433  me_arg.recur = RTEST(recur);
1434  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1435  if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
1436  klass = RCLASS_SUPER(klass);
1437  }
1438  if (RTEST(recur)) {
1439  while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1440  if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
1441  klass = RCLASS_SUPER(klass);
1442  }
1443  }
1444  ary = rb_ary_new();
1445  st_foreach(me_arg.list, ins_methods_i, ary);
1446  st_free_table(me_arg.list);
1447 
1448  return ary;
1449 }
1450 
1508 void
1509 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1510 {
1511  rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
1512 }
1513 
1514 void
1515 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1516 {
1517  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
1518 }
1519 
1520 void
1521 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1522 {
1524 }
1525 
1526 void
1527 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1528 {
1529  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
1530 }
1531 
1532 void
1533 rb_undef_method(VALUE klass, const char *name)
1534 {
1536 }
1537 
1538 static enum rb_id_table_iterator_result
1539 undef_method_i(ID name, VALUE value, void *data)
1540 {
1541  VALUE klass = (VALUE)data;
1543  return ID_TABLE_CONTINUE;
1544 }
1545 
1546 void
1548 {
1549  struct rb_id_table *mtbl = RCLASS_M_TBL(super);
1550  if (mtbl) {
1551  rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
1552  }
1553 }
1554 
1563 #define SPECIAL_SINGLETON(x,c) do {\
1564  if (obj == (x)) {\
1565  return (c);\
1566  }\
1567 } while (0)
1568 
1569 static inline VALUE
1571 {
1575  return Qnil;
1576 }
1577 
1578 VALUE
1580 {
1581  return special_singleton_class_of(obj);
1582 }
1583 
1593 static VALUE
1595 {
1596  VALUE klass;
1597 
1598  if (FIXNUM_P(obj) || FLONUM_P(obj) || STATIC_SYM_P(obj)) {
1599  no_singleton:
1600  rb_raise(rb_eTypeError, "can't define singleton");
1601  }
1602  if (SPECIAL_CONST_P(obj)) {
1603  klass = special_singleton_class_of(obj);
1604  if (NIL_P(klass))
1605  rb_bug("unknown immediate %p", (void *)obj);
1606  return klass;
1607  }
1608  else {
1609  switch (BUILTIN_TYPE(obj)) {
1610  case T_FLOAT: case T_BIGNUM: case T_SYMBOL:
1611  goto no_singleton;
1612  case T_STRING:
1613  if (FL_TEST_RAW(obj, RSTRING_FSTR)) goto no_singleton;
1614  break;
1615  }
1616  }
1617 
1618  klass = RBASIC(obj)->klass;
1619  if (!(FL_TEST(klass, FL_SINGLETON) &&
1620  rb_ivar_get(klass, id_attached) == obj)) {
1621  rb_serial_t serial = RCLASS_SERIAL(klass);
1622  klass = rb_make_metaclass(obj, klass);
1623  RCLASS_SERIAL(klass) = serial;
1624  }
1625 
1626  if (OBJ_TAINTED(obj)) {
1627  OBJ_TAINT(klass);
1628  }
1629  else {
1630  FL_UNSET(klass, FL_TAINT);
1631  }
1632  RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
1633 
1634  return klass;
1635 }
1636 
1637 void
1639 {
1640  /* should not propagate to meta-meta-class, and so on */
1641  if (!(RBASIC(x)->flags & FL_SINGLETON)) {
1642  VALUE klass = RBASIC_CLASS(x);
1643  if (klass && (klass = RCLASS_ORIGIN(klass)) != 0 &&
1644  FL_TEST(klass, (FL_SINGLETON|FL_FREEZE)) == FL_SINGLETON) {
1645  OBJ_FREEZE_RAW(klass);
1646  }
1647  }
1648 }
1649 
1657 VALUE
1659 {
1660  VALUE klass;
1661 
1662  if (SPECIAL_CONST_P(obj)) {
1663  return rb_special_singleton_class(obj);
1664  }
1665  klass = RBASIC(obj)->klass;
1666  if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
1667  if (rb_ivar_get(klass, id_attached) != obj) return Qnil;
1668  return klass;
1669 }
1670 
1688 VALUE
1690 {
1691  VALUE klass = singleton_class_of(obj);
1692 
1693  /* ensures an exposed class belongs to its own eigenclass */
1694  if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1695 
1696  return klass;
1697 }
1698 
1715 void
1716 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1717 {
1718  rb_define_method(singleton_class_of(obj), name, func, argc);
1719 }
1720 
1721 
1722 
1730 void
1731 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1732 {
1733  rb_define_private_method(module, name, func, argc);
1734  rb_define_singleton_method(module, name, func, argc);
1735 }
1736 
1737 
1744 void
1745 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1746 {
1748 }
1749 
1750 
1757 void
1758 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1759 {
1760  rb_alias(klass, rb_intern(name1), rb_intern(name2));
1761 }
1762 
1770 void
1771 rb_define_attr(VALUE klass, const char *name, int read, int write)
1772 {
1773  rb_attr(klass, rb_intern(name), read, write, FALSE);
1774 }
1775 
1776 int
1778 {
1779  const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
1780  if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1781  me->def->body.cfunc.func == rb_any_to_s)
1782  return 1;
1783  return 0;
1784 }
1785 
1786 VALUE
1787 rb_keyword_error_new(const char *error, VALUE keys)
1788 {
1789  const char *msg = "";
1790  VALUE error_message;
1791 
1792  if (RARRAY_LEN(keys) == 1) {
1793  keys = RARRAY_AREF(keys, 0);
1794  }
1795  else {
1796  keys = rb_ary_join(keys, rb_usascii_str_new2(", "));
1797  msg = "s";
1798  }
1799 
1800  error_message = rb_sprintf("%s keyword%s: %"PRIsVALUE, error, msg, keys);
1801 
1802  return rb_exc_new_str(rb_eArgError, error_message);
1803 }
1804 
1805 NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
1806 static void
1807 rb_keyword_error(const char *error, VALUE keys)
1808 {
1809  rb_exc_raise(rb_keyword_error_new(error, keys));
1810 }
1811 
1812 NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
1813 static void
1814 unknown_keyword_error(VALUE hash, const ID *table, int keywords)
1815 {
1816  st_table *tbl = rb_hash_tbl_raw(hash);
1817  VALUE keys;
1818  int i;
1819  for (i = 0; i < keywords; i++) {
1820  st_data_t key = ID2SYM(table[i]);
1821  st_delete(tbl, &key, NULL);
1822  }
1823  keys = rb_funcallv(hash, rb_intern("keys"), 0, 0);
1824  if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword");
1825  rb_keyword_error("unknown", keys);
1826 }
1827 
1828 static int
1830 {
1831  VALUE *kwdhash = (VALUE *)arg;
1832 
1833  if (!SYMBOL_P(key)) kwdhash++;
1834  if (!*kwdhash) *kwdhash = rb_hash_new();
1835  rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
1836  return ST_CONTINUE;
1837 }
1838 
1839 VALUE
1841 {
1842  VALUE parthash[2] = {0, 0};
1843  VALUE hash = *orighash;
1844 
1845  if (RHASH_EMPTY_P(hash)) {
1846  *orighash = 0;
1847  return hash;
1848  }
1850  *orighash = parthash[1];
1851  if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
1852  RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
1853  }
1854  return parthash[0];
1855 }
1856 
1857 int
1858 rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
1859 {
1860  int i = 0, j;
1861  int rest = 0;
1862  VALUE missing = Qnil;
1863  st_data_t key;
1864 
1865 #define extract_kwarg(keyword, val) \
1866  (key = (st_data_t)(keyword), values ? \
1867  st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : \
1868  st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
1869 
1870  if (NIL_P(keyword_hash)) keyword_hash = 0;
1871 
1872  if (optional < 0) {
1873  rest = 1;
1874  optional = -1-optional;
1875  }
1876  if (values) {
1877  for (j = 0; j < required + optional; j++) {
1878  values[j] = Qundef;
1879  }
1880  }
1881  if (required) {
1882  for (; i < required; i++) {
1883  VALUE keyword = ID2SYM(table[i]);
1884  if (keyword_hash) {
1885  st_data_t val;
1886  if (extract_kwarg(keyword, &val)) {
1887  if (values) values[i] = (VALUE)val;
1888  continue;
1889  }
1890  }
1891  if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
1892  rb_ary_push(missing, keyword);
1893  }
1894  if (!NIL_P(missing)) {
1895  rb_keyword_error("missing", missing);
1896  }
1897  }
1898  j = i;
1899  if (optional && keyword_hash) {
1900  for (i = 0; i < optional; i++) {
1901  st_data_t val;
1902  if (extract_kwarg(ID2SYM(table[required+i]), &val)) {
1903  if (values) values[required+i] = (VALUE)val;
1904  j++;
1905  }
1906  }
1907  }
1908  if (!rest && keyword_hash) {
1909  if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
1910  unknown_keyword_error(keyword_hash, table, required+optional);
1911  }
1912  }
1913  return j;
1914 #undef extract_kwarg
1915 }
1916 
1917 #undef rb_scan_args
1918 int
1919 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1920 {
1921  int i;
1922  const char *p = fmt;
1923  VALUE *var;
1924  va_list vargs;
1925  int f_var = 0, f_hash = 0, f_block = 0;
1926  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1927  int argi = 0, last_idx = -1;
1928  VALUE hash = Qnil, last_hash = 0;
1929 
1930  if (ISDIGIT(*p)) {
1931  n_lead = *p - '0';
1932  p++;
1933  if (ISDIGIT(*p)) {
1934  n_opt = *p - '0';
1935  p++;
1936  }
1937  }
1938  if (*p == '*') {
1939  f_var = 1;
1940  p++;
1941  }
1942  if (ISDIGIT(*p)) {
1943  n_trail = *p - '0';
1944  p++;
1945  }
1946  if (*p == ':') {
1947  f_hash = 1;
1948  p++;
1949  }
1950  if (*p == '&') {
1951  f_block = 1;
1952  p++;
1953  }
1954  if (*p != '\0') {
1955  rb_fatal("bad scan arg format: %s", fmt);
1956  }
1957  n_mand = n_lead + n_trail;
1958 
1959  if (argc < n_mand)
1960  goto argc_error;
1961 
1962  va_start(vargs, fmt);
1963 
1964  /* capture an option hash - phase 1: pop */
1965  if (f_hash && n_mand < argc) {
1966  VALUE last = argv[argc - 1];
1967 
1968  if (NIL_P(last)) {
1969  /* nil is taken as an empty option hash only if it is not
1970  ambiguous; i.e. '*' is not specified and arguments are
1971  given more than sufficient */
1972  if (!f_var && n_mand + n_opt < argc)
1973  argc--;
1974  }
1975  else {
1976  hash = rb_check_hash_type(last);
1977  if (!NIL_P(hash)) {
1978  VALUE opts = rb_extract_keywords(&hash);
1979  if (!(last_hash = hash)) argc--;
1980  else last_idx = argc - 1;
1981  hash = opts ? opts : Qnil;
1982  }
1983  }
1984  }
1985  /* capture leading mandatory arguments */
1986  for (i = n_lead; i-- > 0; ) {
1987  var = va_arg(vargs, VALUE *);
1988  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
1989  argi++;
1990  }
1991  /* capture optional arguments */
1992  for (i = n_opt; i-- > 0; ) {
1993  var = va_arg(vargs, VALUE *);
1994  if (argi < argc - n_trail) {
1995  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
1996  argi++;
1997  }
1998  else {
1999  if (var) *var = Qnil;
2000  }
2001  }
2002  /* capture variable length arguments */
2003  if (f_var) {
2004  int n_var = argc - argi - n_trail;
2005 
2006  var = va_arg(vargs, VALUE *);
2007  if (0 < n_var) {
2008  if (var) {
2009  int f_last = (last_idx + 1 == argc - n_trail);
2010  *var = rb_ary_new4(n_var-f_last, &argv[argi]);
2011  if (f_last) rb_ary_push(*var, last_hash);
2012  }
2013  argi += n_var;
2014  }
2015  else {
2016  if (var) *var = rb_ary_new();
2017  }
2018  }
2019  /* capture trailing mandatory arguments */
2020  for (i = n_trail; i-- > 0; ) {
2021  var = va_arg(vargs, VALUE *);
2022  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
2023  argi++;
2024  }
2025  /* capture an option hash - phase 2: assignment */
2026  if (f_hash) {
2027  var = va_arg(vargs, VALUE *);
2028  if (var) *var = hash;
2029  }
2030  /* capture iterator block */
2031  if (f_block) {
2032  var = va_arg(vargs, VALUE *);
2033  if (rb_block_given_p()) {
2034  *var = rb_block_proc();
2035  }
2036  else {
2037  *var = Qnil;
2038  }
2039  }
2040  va_end(vargs);
2041 
2042  if (argi < argc) {
2043  argc_error:
2044  rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2045  }
2046 
2047  return argc;
2048 }
2049 
2050 int
2052 {
2053  return rb_id_table_size(RCLASS_M_TBL(c)) == 0 ? FALSE : TRUE;
2054 }
2055 
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition: class.c:486
st_table * list
Definition: class.c:1148
static void rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
Definition: class.c:57
void rb_class_remove_from_super_subclasses(VALUE klass)
Definition: class.c:76
#define T_SYMBOL
Definition: ruby.h:508
#define ISDIGIT(c)
Definition: ruby.h:2129
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:171
void rb_class_detach_subclasses(VALUE klass)
Definition: class.c:133
VALUE rb_define_module_id_under(VALUE outer, ID id)
Definition: class.c:796
VALUE old_klass
Definition: class.c:264
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ruby.h:1882
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1294
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:1053
#define RARRAY_LEN(a)
Definition: ruby.h:1026
void rb_bug(const char *fmt,...)
Definition: error.c:482
rb_method_type_t type
Definition: method.h:148
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex)
Definition: vm_method.c:666
#define FALSE
Definition: nkf.h:174
#define RB_FL_SET_RAW(x, f)
Definition: ruby.h:1258
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:220
static void class_detach_subclasses(VALUE klass, VALUE arg)
Definition: class.c:127
#define RCLASS_CONST_TBL(c)
Definition: internal.h:689
Definition: constant.h:31
Definition: st.h:79
static enum rb_id_table_iterator_result clone_method_i(ID key, VALUE value, void *data)
Definition: class.c:268
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *me)
Definition: vm_method.c:402
void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
Definition: vm_method.c:643
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1438
static unsigned int hash(str, len) register const char *str
rb_subclass_entry_t * next
Definition: internal.h:640
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
static void rb_keyword_error(const char *error, VALUE keys)
Definition: class.c:1807
#define rb_usascii_str_new2
Definition: intern.h:863
#define FL_TAINT
Definition: ruby.h:1220
#define CLASS_OF(v)
Definition: ruby.h:453
#define T_MODULE
Definition: ruby.h:494
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:716
const VALUE file
Definition: constant.h:35
#define RCLASS_EXT(c)
Definition: classext.h:15
void rb_class_remove_from_module_subclasses(VALUE klass)
Definition: class.c:94
rb_cref_t *const cref
Definition: method.h:124
#define st_foreach
Definition: regint.h:186
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1858
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1256
#define Qtrue
Definition: ruby.h:437
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition: class.c:163
VALUE rb_cHash
Definition: hash.c:81
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:288
#define rb_id2str(id)
Definition: vm_backtrace.c:29
Definition: st.h:99
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:1085
const int id
Definition: nkf.c:209
#define OBJ_FREEZE_RAW(x)
Definition: ruby.h:1307
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1238
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:809
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
VALUE rb_eTypeError
Definition: error.c:762
#define rb_check_arity
Definition: intern.h:303
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:365
const VALUE owner
Definition: method.h:55
struct st_table * rb_hash_tbl_raw(VALUE hash)
Definition: hash.c:490
static enum rb_id_table_iterator_result move_refined_method(ID key, VALUE value, void *data)
Definition: class.c:946
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:314
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:532
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
void rb_freeze_singleton_class(VALUE x)
Definition: class.c:1638
void Init_class_hierarchy(void)
Definition: class.c:546
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:1314
static enum rb_id_table_iterator_result method_entry_i(ID key, VALUE value, void *data)
Definition: class.c:1153
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define Check_Type(v, t)
Definition: ruby.h:562
static int ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1130
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1260
Definition: class.c:1147
#define SET_METACLASS_OF(k, cls)
Definition: class.c:434
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1529
void rb_clear_constant_cache(void)
Definition: vm_method.c:90
int rb_const_defined(VALUE, ID)
Definition: variable.c:2580
const VALUE value
Definition: constant.h:34
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
#define T_ARRAY
Definition: ruby.h:498
#define st_delete
Definition: regint.h:182
#define st_lookup
Definition: regint.h:185
static enum rb_id_table_iterator_result add_refined_method_entry_i(ID key, VALUE value, void *data)
Definition: class.c:876
void rb_define_protected_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1521
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1745
unsigned int last
Definition: nkf.c:4311
#define RMODULE_M_TBL(m)
Definition: ruby.h:923
#define FIXNUM_P(f)
Definition: ruby.h:365
void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr)
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
#define OBJ_TAINTED(x)
Definition: ruby.h:1298
rb_method_iseq_t iseq
Definition: method.h:153
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:754
void rb_exc_raise(VALUE mesg)
Definition: eval.c:620
#define FL_SINGLETON
Definition: ruby.h:1215
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:973
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1689
static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
Definition: class.c:883
VALUE rb_extract_keywords(VALUE *orighash)
Definition: class.c:1840
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
rb_method_cfunc_t cfunc
Definition: method.h:154
#define FL_TEST(x, f)
Definition: ruby.h:1284
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:620
VALUE rb_class_name(VALUE)
Definition: variable.c:443
Definition: internal.h:638
int rb_block_given_p(void)
Definition: eval.c:797
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1576
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
Definition: vm_method.c:424
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
static void ensure_includable(VALUE klass, VALUE module)
Definition: class.c:853
static void clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
Definition: class.c:250
VALUE rb_special_singleton_class(VALUE obj)
Definition: class.c:1579
#define RGENGC_WB_PROTECTED_CLASS
Definition: ruby.h:789
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1138
static VALUE boot_defclass(const char *name, VALUE super)
Definition: class.c:535
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1871
VALUE rb_ary_new(void)
Definition: array.c:493
#define RMODULE_CONST_TBL(m)
Definition: ruby.h:922
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1424
static void ins_methods_push(st_data_t name, st_data_t ary)
Definition: class.c:1101
int rb_class_has_methods(VALUE c)
Definition: class.c:2051
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1379
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1860
#define RCLASS_ORIGIN(c)
Definition: internal.h:693
#define NIL_P(v)
Definition: ruby.h:451
static VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super)
Definition: internal.h:714
struct rb_id_table * rb_id_table_create(size_t size)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
static char msg[50]
Definition: strerror.c:8
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ruby.h:1911
rb_method_visibility_t
Definition: method.h:26
static VALUE special_singleton_class_of(VALUE obj)
Definition: class.c:1570
struct rb_method_definition_struct *const def
Definition: method.h:53
#define RCLASS_IV_TBL(c)
Definition: internal.h:688
void rb_class_subclass_add(VALUE super, VALUE klass)
Definition: class.c:36
VALUE rb_define_module_id(ID id)
Definition: class.c:757
#define METACLASS_OF(k)
Definition: class.c:433
#define FLONUM_P(x)
Definition: ruby.h:399
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition: class.c:441
#define T_FLOAT
Definition: ruby.h:495
int argc
Definition: ruby.c:183
const rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
Definition: vm_method.c:941
#define Qfalse
Definition: ruby.h:436
Definition: method.h:50
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1895
VALUE new_klass
Definition: class.c:263
#define T_BIGNUM
Definition: ruby.h:501
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:6154
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define rb_ary_new4
Definition: intern.h:92
#define ALLOC(type)
Definition: ruby.h:1588
int rb_obj_basic_to_s_p(VALUE obj)
Definition: class.c:1777
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:694
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2335
#define id_attached
Definition: class.c:33
#define FL_PROMOTED1
Definition: ruby.h:1218
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_visibility_t visi)
Definition: vm_method.c:136
#define ZALLOC(type)
Definition: ruby.h:1590
size_t rb_id_table_size(const struct rb_id_table *tbl)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1731
#define RCLASS_M_TBL(c)
Definition: internal.h:690
union rb_method_definition_struct::@144 body
#define TRUE
Definition: nkf.h:175
void rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE, VALUE), VALUE arg)
Definition: class.c:113
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
unsigned long rb_serial_t
Definition: internal.h:650
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:818
const rb_method_entry_t * rb_method_entry(VALUE klass, ID id)
Definition: vm_method.c:796
void rb_fatal(const char *fmt,...)
Definition: error.c:2261
int recur
Definition: class.c:1149
VALUE klass
Definition: internal.h:639
VALUE rb_class_path(VALUE)
Definition: variable.c:294
VALUE(* func)(ANYARGS)
Definition: method.h:128
static void class_detach_module_subclasses(VALUE klass, VALUE arg)
Definition: class.c:139
#define RHASH_SIZE(hsh)
Definition: fbuffer.h:8
VALUE rb_hash_new(void)
Definition: hash.c:441
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:736
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
static int separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
Definition: class.c:1829
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1241
static int ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1107
#define Qnil
Definition: ruby.h:438
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:96
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2616
#define extract_kwarg(keyword, val)
#define FL_TEST_RAW(x, f)
Definition: ruby.h:1283
static int ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1139
#define METHOD_ENTRY_VISI(me)
Definition: method.h:66
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
#define OBJ_TAINT(x)
Definition: ruby.h:1300
unsigned long VALUE
Definition: ruby.h:85
void rb_undef_methods_from(VALUE klass, VALUE super)
Definition: class.c:1547
void rb_name_class(VALUE, ID)
Definition: variable.c:437
#define RBASIC(obj)
Definition: ruby.h:1204
#define STATIC_SYM_P(x)
Definition: ruby.h:380
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:577
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1349
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1526
static int clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
Definition: class.c:281
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1878
void rb_class_detach_module_subclasses(VALUE klass)
Definition: class.c:145
VALUE rb_class_real(VALUE cl)
Definition: object.c:207
#define FL_UNSET(x, f)
Definition: ruby.h:1292
#define rb_funcallv
Definition: console.c:21
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define recur(fmt)
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2586
NORETURN(static void rb_keyword_error(const char *error, VALUE keys))
void rb_free_const_table(struct rb_id_table *tbl)
Definition: gc.c:2091
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1509
static int rb_singleton_class_has_metaclass_p(VALUE sklass)
Definition: class.c:444
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:421
#define f
static void unknown_keyword_error(VALUE hash, const ID *table, int keywords)
Definition: class.c:1814
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
#define RCLASS_SUPER(c)
Definition: classext.h:16
VALUE rb_module_new(void)
Definition: class.c:749
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:472
static enum rb_id_table_iterator_result clone_const_i(ID key, VALUE value, void *data)
Definition: class.c:293
VALUE rb_block_proc(void)
Definition: proc.c:787
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines (a) public accessor method(s) for an attribute.
Definition: class.c:1771
#define st_init_numtable
Definition: regint.h:178
#define RBASIC_CLASS(obj)
Definition: ruby.h:878
#define ANYARGS
Definition: defines.h:173
int rb_class_ivar_set(VALUE klass, ID vid, VALUE value)
Definition: variable.c:3147
void rb_error_arity(int argc, int min, int max)
#define RB_OBJ_FROZEN_RAW(x)
Definition: ruby.h:1277
#define FL_WB_PROTECTED
Definition: ruby.h:1216
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition: class.c:520
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition: class.c:1658
VALUE rb_any_to_s(VALUE)
Definition: object.c:500
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1418
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi)
Definition: vm_method.c:631
static void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
Definition: internal.h:700
st_table * rb_st_copy(VALUE obj, struct st_table *orig_tbl)
Definition: variable.c:3163
#define OBJ_INFECT(x, s)
Definition: ruby.h:1304
#define st_add_direct
Definition: regint.h:187
int rb_singleton_class_internal_p(VALUE sklass)
Definition: class.c:450
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:462
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1279
VALUE klass
Definition: class.c:276
static int ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1121
static enum rb_id_table_iterator_result undef_method_i(ID name, VALUE value, void *data)
Definition: class.c:1539
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1330
VALUE rb_ary_join(VALUE ary, VALUE sep)
Definition: array.c:2034
#define T_CLASS
Definition: ruby.h:492
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2341
void rb_frozen_class_p(VALUE klass)
Definition: eval.c:396
const char * name
Definition: nkf.c:208
static void class_init_copy_check(VALUE clone, VALUE orig)
Definition: class.c:299
#define FL_SET(x, f)
Definition: ruby.h:1290
static void RCLASS_M_TBL_INIT(VALUE c)
Definition: class.c:186
#define ID2SYM(x)
Definition: ruby.h:383
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:1017
#define FL_FREEZE
Definition: ruby.h:1223
#define st_free_table
Definition: regint.h:188
VALUE rb_define_class_id(ID id, VALUE super)
Defines a new class.
Definition: class.c:599
rb_serial_t rb_next_class_serial(void)
Definition: vm.c:305
#define SPECIAL_SINGLETON(x, c)
Definition: class.c:1563
#define CONST_ID(var, str)
Definition: ruby.h:1743
rb_id_table_iterator_result
Definition: id_table.h:8
#define rb_intern_const(str)
Definition: ruby.h:1756
const rb_iseq_t *const iseqptr
Definition: method.h:123
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1249
void void xfree(void *)
#define RHASH_EMPTY_P(h)
Definition: ruby.h:1067
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
#define SYMBOL_P(x)
Definition: ruby.h:382
#define mod(x, y)
Definition: date_strftime.c:28
const struct rb_method_entry_struct *const orig_me
Definition: method.h:143
int rb_vm_add_root_module(ID id, VALUE module)
Definition: vm.c:2153
#define NULL
Definition: _sdbm.c:102
static VALUE class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int(*func)(st_data_t, st_data_t, st_data_t))
Definition: class.c:1178
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1364
static VALUE singleton_class_of(VALUE obj)
Definition: class.c:1594
rb_method_refined_t refined
Definition: method.h:157
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
#define RCLASS_SERIAL(c)
Definition: internal.h:695
VALUE rb_class_new(VALUE super)
Creates a new class.
Definition: class.c:242
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:201
RUBY_EXTERN VALUE rb_cNilClass
Definition: ruby.h:1897
void rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
VALUE rb_eArgError
Definition: error.c:763
int rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
struct rb_id_table * tbl
Definition: class.c:277
#define T_MASK
Definition: md5.c:131
void rb_set_class_path_string(VALUE, VALUE, VALUE)
Definition: variable.c:343
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1437
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1273
VALUE rb_keyword_error_new(const char *error, VALUE keys)
Definition: class.c:1787
char ** argv
Definition: ruby.c:184
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:371
VALUE rb_obj_class(VALUE)
Definition: object.c:229