Ruby  2.5.0dev(2017-10-22revision60238)
class.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  class.c -
4 
5  $Author$
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
57 rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
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
127 class_detach_subclasses(VALUE klass, VALUE arg)
128 {
130 }
131 
132 void
134 {
135  rb_class_foreach_subclass(klass, class_detach_subclasses, Qnil);
136 }
137 
138 static void
139 class_detach_module_subclasses(VALUE klass, VALUE arg)
140 {
142 }
143 
144 void
146 {
147  rb_class_foreach_subclass(klass, class_detach_module_subclasses, Qnil);
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
186 RCLASS_M_TBL_INIT(VALUE c)
187 {
189 }
190 
200 VALUE
202 {
203  VALUE klass = class_alloc(T_CLASS, rb_cClass);
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
281 clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
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
299 class_init_copy_check(VALUE clone, VALUE orig)
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;
351  rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
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);
358  rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
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;
397  rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
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;
407  rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
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
444 rb_singleton_class_has_metaclass_p(VALUE sklass)
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) &&
453  !rb_singleton_class_has_metaclass_p(sklass));
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
486 make_metaclass(VALUE klass)
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
520 make_singleton_class(VALUE obj)
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);
549  rb_cObject = boot_defclass("Object", rb_cBasicObject);
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 {
751  VALUE mdl = class_alloc(T_MODULE, rb_cModule);
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 {
820  VALUE klass = class_alloc(T_ICLASS, rb_cClass);
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
853 ensure_includable(VALUE klass, VALUE module)
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
876 add_refined_method_entry_i(ID key, VALUE value, void *data)
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;
919  rb_module_add_to_subclasses_list(m, iclass);
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
1101 ins_methods_push(st_data_t name, st_data_t ary)
1102 {
1103  rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1104 }
1105 
1106 static int
1107 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
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
1121 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1122 {
1124  ins_methods_push(name, ary);
1125  }
1126  return ST_CONTINUE;
1127 }
1128 
1129 static int
1130 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1131 {
1133  ins_methods_push(name, ary);
1134  }
1135  return ST_CONTINUE;
1136 }
1137 
1138 static int
1139 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
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
1178 class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
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 {
1523  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
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
1570 special_singleton_class_of(VALUE obj)
1571 {
1575  return Qnil;
1576 }
1577 
1578 VALUE
1580 {
1581  return special_singleton_class_of(obj);
1582 }
1583 
1593 static VALUE
1594 singleton_class_of(VALUE obj)
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 {
1747  rb_define_module_function(rb_mKernel, name, func, argc);
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 VALUE
1777 rb_keyword_error_new(const char *error, VALUE keys)
1778 {
1779  const VALUE *ptr = RARRAY_CONST_PTR(keys);
1780  long i = 0, len = RARRAY_LEN(keys);
1781  VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
1782 
1783  if (len > 0) {
1784  rb_str_cat_cstr(error_message, ": ");
1785  while (1) {
1786  const VALUE k = ptr[i];
1787  Check_Type(k, T_SYMBOL); /* wrong hash is given to rb_get_kwargs */
1788  rb_str_append(error_message, rb_sym2str(k));
1789  if (++i >= len) break;
1790  rb_str_cat_cstr(error_message, ", ");
1791  }
1792  }
1793 
1794  return rb_exc_new_str(rb_eArgError, error_message);
1795 }
1796 
1797 NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
1798 static void
1799 rb_keyword_error(const char *error, VALUE keys)
1800 {
1801  rb_exc_raise(rb_keyword_error_new(error, keys));
1802 }
1803 
1804 NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
1805 static void
1806 unknown_keyword_error(VALUE hash, const ID *table, int keywords)
1807 {
1808  st_table *tbl = rb_hash_tbl_raw(hash);
1809  int i;
1810  for (i = 0; i < keywords; i++) {
1811  st_data_t key = ID2SYM(table[i]);
1812  st_delete(tbl, &key, NULL);
1813  }
1814  rb_keyword_error("unknown", rb_hash_keys(hash));
1815 }
1816 
1817 static int
1818 separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
1819 {
1820  VALUE *kwdhash = (VALUE *)arg;
1821 
1822  if (!SYMBOL_P(key)) kwdhash++;
1823  if (!*kwdhash) *kwdhash = rb_hash_new();
1824  rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
1825  return ST_CONTINUE;
1826 }
1827 
1828 VALUE
1830 {
1831  VALUE parthash[2] = {0, 0};
1832  VALUE hash = *orighash;
1833 
1834  if (RHASH_EMPTY_P(hash)) {
1835  *orighash = 0;
1836  return hash;
1837  }
1838  st_foreach(rb_hash_tbl_raw(hash), separate_symbol, (st_data_t)&parthash);
1839  *orighash = parthash[1];
1840  if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
1841  RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
1842  }
1843  return parthash[0];
1844 }
1845 
1846 int
1847 rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
1848 {
1849  int i = 0, j;
1850  int rest = 0;
1851  VALUE missing = Qnil;
1852  st_data_t key;
1853 
1854 #define extract_kwarg(keyword, val) \
1855  (key = (st_data_t)(keyword), values ? \
1856  st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : \
1857  st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
1858 
1859  if (NIL_P(keyword_hash)) keyword_hash = 0;
1860 
1861  if (optional < 0) {
1862  rest = 1;
1863  optional = -1-optional;
1864  }
1865  if (values) {
1866  for (j = 0; j < required + optional; j++) {
1867  values[j] = Qundef;
1868  }
1869  }
1870  if (required) {
1871  for (; i < required; i++) {
1872  VALUE keyword = ID2SYM(table[i]);
1873  if (keyword_hash) {
1874  st_data_t val;
1875  if (extract_kwarg(keyword, &val)) {
1876  if (values) values[i] = (VALUE)val;
1877  continue;
1878  }
1879  }
1880  if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
1881  rb_ary_push(missing, keyword);
1882  }
1883  if (!NIL_P(missing)) {
1884  rb_keyword_error("missing", missing);
1885  }
1886  }
1887  j = i;
1888  if (optional && keyword_hash) {
1889  for (i = 0; i < optional; i++) {
1890  st_data_t val;
1891  if (extract_kwarg(ID2SYM(table[required+i]), &val)) {
1892  if (values) values[required+i] = (VALUE)val;
1893  j++;
1894  }
1895  }
1896  }
1897  if (!rest && keyword_hash) {
1898  if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
1899  unknown_keyword_error(keyword_hash, table, required+optional);
1900  }
1901  }
1902  return j;
1903 #undef extract_kwarg
1904 }
1905 
1906 #undef rb_scan_args
1907 int
1908 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1909 {
1910  int i;
1911  const char *p = fmt;
1912  VALUE *var;
1913  va_list vargs;
1914  int f_var = 0, f_hash = 0, f_block = 0;
1915  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1916  int argi = 0, last_idx = -1;
1917  VALUE hash = Qnil, last_hash = 0;
1918 
1919  if (ISDIGIT(*p)) {
1920  n_lead = *p - '0';
1921  p++;
1922  if (ISDIGIT(*p)) {
1923  n_opt = *p - '0';
1924  p++;
1925  }
1926  }
1927  if (*p == '*') {
1928  f_var = 1;
1929  p++;
1930  }
1931  if (ISDIGIT(*p)) {
1932  n_trail = *p - '0';
1933  p++;
1934  }
1935  if (*p == ':') {
1936  f_hash = 1;
1937  p++;
1938  }
1939  if (*p == '&') {
1940  f_block = 1;
1941  p++;
1942  }
1943  if (*p != '\0') {
1944  rb_fatal("bad scan arg format: %s", fmt);
1945  }
1946  n_mand = n_lead + n_trail;
1947 
1948  if (argc < n_mand)
1949  goto argc_error;
1950 
1951  va_start(vargs, fmt);
1952 
1953  /* capture an option hash - phase 1: pop */
1954  if (f_hash && n_mand < argc) {
1955  VALUE last = argv[argc - 1];
1956 
1957  if (NIL_P(last)) {
1958  /* nil is taken as an empty option hash only if it is not
1959  ambiguous; i.e. '*' is not specified and arguments are
1960  given more than sufficient */
1961  if (!f_var && n_mand + n_opt < argc)
1962  argc--;
1963  }
1964  else {
1965  hash = rb_check_hash_type(last);
1966  if (!NIL_P(hash)) {
1967  VALUE opts = rb_extract_keywords(&hash);
1968  if (!(last_hash = hash)) argc--;
1969  else last_idx = argc - 1;
1970  hash = opts ? opts : Qnil;
1971  }
1972  }
1973  }
1974  /* capture leading mandatory arguments */
1975  for (i = n_lead; i-- > 0; ) {
1976  var = va_arg(vargs, VALUE *);
1977  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
1978  argi++;
1979  }
1980  /* capture optional arguments */
1981  for (i = n_opt; i-- > 0; ) {
1982  var = va_arg(vargs, VALUE *);
1983  if (argi < argc - n_trail) {
1984  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
1985  argi++;
1986  }
1987  else {
1988  if (var) *var = Qnil;
1989  }
1990  }
1991  /* capture variable length arguments */
1992  if (f_var) {
1993  int n_var = argc - argi - n_trail;
1994 
1995  var = va_arg(vargs, VALUE *);
1996  if (0 < n_var) {
1997  if (var) {
1998  int f_last = (last_idx + 1 == argc - n_trail);
1999  *var = rb_ary_new4(n_var-f_last, &argv[argi]);
2000  if (f_last) rb_ary_push(*var, last_hash);
2001  }
2002  argi += n_var;
2003  }
2004  else {
2005  if (var) *var = rb_ary_new();
2006  }
2007  }
2008  /* capture trailing mandatory arguments */
2009  for (i = n_trail; i-- > 0; ) {
2010  var = va_arg(vargs, VALUE *);
2011  if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
2012  argi++;
2013  }
2014  /* capture an option hash - phase 2: assignment */
2015  if (f_hash) {
2016  var = va_arg(vargs, VALUE *);
2017  if (var) *var = hash;
2018  }
2019  /* capture iterator block */
2020  if (f_block) {
2021  var = va_arg(vargs, VALUE *);
2022  if (rb_block_given_p()) {
2023  *var = rb_block_proc();
2024  }
2025  else {
2026  *var = Qnil;
2027  }
2028  }
2029  va_end(vargs);
2030 
2031  if (argi < argc) {
2032  argc_error:
2033  rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2034  }
2035 
2036  return argc;
2037 }
2038 
2039 int
2041 {
2042  return rb_id_table_size(RCLASS_M_TBL(c)) == 0 ? FALSE : TRUE;
2043 }
2044 
st_table * list
Definition: class.c:1148
void rb_fatal(const char *fmt,...)
Definition: error.c:2338
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:2150
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:175
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
void rb_bug(const char *fmt,...)
Definition: error.c:521
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ruby.h:1903
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:1019
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:660
#define FALSE
Definition: nkf.h:174
#define RB_FL_SET_RAW(x, f)
Definition: ruby.h:1251
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:220
#define RCLASS_CONST_TBL(c)
Definition: internal.h:790
Definition: constant.h:31
Definition: st.h:79
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *me)
Definition: vm_method.c:396
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:637
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1438
rb_subclass_entry_t * next
Definition: internal.h:741
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
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
#define FL_TAINT
Definition: ruby.h:1213
#define CLASS_OF(v)
Definition: ruby.h:453
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#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
#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:1847
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1256
#define Qtrue
Definition: ruby.h:437
VALUE rb_cHash
Definition: hash.c:82
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:283
#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
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
Ruby method.
Definition: method.h:102
#define rb_check_arity
Definition: intern.h:298
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:365
const VALUE owner
Definition: method.h:56
struct st_table * rb_hash_tbl_raw(VALUE hash)
Definition: hash.c:482
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:314
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:544
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
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:1471
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
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1210
Definition: class.c:1147
#define SET_METACLASS_OF(k, cls)
Definition: class.c:434
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:848
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1552
void rb_clear_constant_cache(void)
Definition: vm_method.c:84
int rb_const_defined(VALUE, ID)
Definition: variable.c:2537
const VALUE value
Definition: constant.h:34
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
#define FL_TEST_RAW(x, f)
Definition: ruby.h:1281
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1388
#define FL_UNSET(x, f)
Definition: ruby.h:1290
#define st_delete
Definition: regint.h:182
#define st_lookup
Definition: regint.h:185
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:916
#define FIXNUM_P(f)
Definition: ruby.h:365
#define FL_TEST(x, f)
Definition: ruby.h:1282
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
refinement
Definition: method.h:113
VALUE rb_eArgError
Definition: error.c:802
VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:2131
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
#define FL_SINGLETON
Definition: ruby.h:1208
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
VALUE rb_extract_keywords(VALUE *orighash)
Definition: class.c:1829
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
Definition: object.c:277
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:620
VALUE rb_class_name(VALUE)
Definition: variable.c:444
Definition: internal.h:739
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
Definition: vm_method.c:418
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
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:1137
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1892
#define FL_SET(x, f)
Definition: ruby.h:1288
VALUE rb_ary_new(void)
Definition: array.c:499
#define RMODULE_CONST_TBL(m)
Definition: ruby.h:915
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1424
int rb_class_has_methods(VALUE c)
Definition: class.c:2040
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1379
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1881
#define RCLASS_ORIGIN(c)
Definition: internal.h:794
#define NIL_P(v)
Definition: ruby.h:451
void rb_frozen_class_p(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:404
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ruby.h:1932
rb_method_visibility_t
Definition: method.h:26
struct rb_method_definition_struct *const def
Definition: method.h:54
struct rb_id_table * rb_id_table_create(size_t capa)
Definition: id_table.c:95
#define RCLASS_IV_TBL(c)
Definition: internal.h:789
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:187
const rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
Definition: vm_method.c:940
#define Qfalse
Definition: ruby.h:436
Definition: method.h:51
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1916
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:6227
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define rb_ary_new4
Definition: intern.h:92
#define OBJ_FREEZE_RAW(x)
Definition: ruby.h:1305
#define ALLOC(type)
Definition: ruby.h:1588
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:795
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2292
#define id_attached
Definition: class.c:33
#define FL_PROMOTED1
Definition: ruby.h:1211
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:130
#define ZALLOC(type)
Definition: ruby.h:1590
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:791
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1021
int rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
Definition: id_table.c:256
#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:1452
unsigned long rb_serial_t
Definition: internal.h:751
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:818
int recur
Definition: class.c:1149
VALUE klass
Definition: internal.h:740
VALUE rb_class_path(VALUE)
Definition: variable.c:295
#define RHASH_SIZE(hsh)
Definition: fbuffer.h:8
VALUE rb_hash_new(void)
Definition: hash.c:424
size_t rb_id_table_size(const struct rb_id_table *tbl)
Definition: id_table.c:117
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:722
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1241
#define Qnil
Definition: ruby.h:438
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:90
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2573
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:615
#define extract_kwarg(keyword, val)
#define METHOD_ENTRY_VISI(me)
Definition: method.h:67
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
unsigned long VALUE
Definition: ruby.h:85
#define OBJ_TAINTED(x)
Definition: ruby.h:1296
void rb_undef_methods_from(VALUE klass, VALUE super)
Definition: class.c:1547
void rb_name_class(VALUE, ID)
Definition: variable.c:438
#define RBASIC(obj)
Definition: ruby.h:1197
VALUE rb_eTypeError
Definition: error.c:801
#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:1525
CREF (Class REFerence)
Definition: method.h:41
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1899
void rb_class_detach_module_subclasses(VALUE klass)
Definition: class.c:145
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define recur(fmt)
#define RB_OBJ_WRITE(a, slot, b)
Definition: eval_intern.h:175
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2543
NORETURN(static void rb_keyword_error(const char *error, VALUE keys))
void rb_free_const_table(struct rb_id_table *tbl)
Definition: gc.c:2140
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1509
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:421
#define f
#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 ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:472
VALUE rb_block_proc(void)
Definition: proc.c:780
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:3104
void rb_error_arity(int argc, int min, int max)
#define RB_OBJ_FROZEN_RAW(x)
Definition: ruby.h:1270
#define FL_WB_PROTECTED
Definition: ruby.h:1209
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_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:625
st_table * rb_st_copy(VALUE obj, struct st_table *orig_tbl)
Definition: variable.c:3120
#define OBJ_INFECT(x, s)
Definition: ruby.h:1302
#define st_add_direct
Definition: regint.h:187
VALUE rb_str_cat_cstr(VALUE, const char *)
Definition: string.c:2756
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:456
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1279
VALUE klass
Definition: class.c:276
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1330
#define T_CLASS
Definition: ruby.h:492
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2298
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:383
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:1017
#define FL_FREEZE
Definition: ruby.h:1216
#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:309
#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:1777
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1242
void void xfree(void *)
#define RHASH_EMPTY_P(h)
Definition: ruby.h:1060
VALUE rb_define_module(const char *name)
Definition: class.c:768
void rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
Definition: id_table.c:270
#define rb_intern(str)
#define SYMBOL_P(x)
Definition: ruby.h:382
#define mod(x, y)
Definition: date_strftime.c:28
int rb_vm_add_root_module(ID id, VALUE module)
Definition: vm.c:2172
#define NULL
Definition: _sdbm.c:102
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1364
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
#define RCLASS_SERIAL(c)
Definition: internal.h:796
VALUE rb_class_real(VALUE cl)
Looks up the nearest ancestor of cl, skipping singleton classes or module inclusions.
Definition: object.c:251
#define OBJ_TAINT(x)
Definition: ruby.h:1298
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_str_append(VALUE, VALUE)
Definition: string.c:2900
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:1918
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:344
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1224
VALUE rb_keyword_error_new(const char *error, VALUE keys)
Definition: class.c:1777
char ** argv
Definition: ruby.c:188
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:371
#define rb_sym2str(sym)
Definition: console.c:107