Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
class.c
Go to the documentation of this file.
1 /*
2 ** class.c - Class class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #include "mruby.h"
8 #include <stdarg.h>
9 #include <ctype.h>
10 #include "mruby/array.h"
11 #include "mruby/class.h"
12 #include "mruby/numeric.h"
13 #include "mruby/proc.h"
14 #include "mruby/string.h"
15 #include "mruby/variable.h"
16 #include "error.h"
17 
19 
20 void
22 {
23  khiter_t k;
24  khash_t(mt) *h = c->mt;
25 
26  if (!h) return;
27  for (k = kh_begin(h); k != kh_end(h); k++) {
28  if (kh_exist(h, k)){
29  struct RProc *m = kh_value(h, k);
30  if (m) {
31  mrb_gc_mark(mrb, (struct RBasic*)m);
32  }
33  }
34  }
35 }
36 
37 size_t
39 {
40  khash_t(mt) *h = c->mt;
41 
42  if (!h) return 0;
43  return kh_size(h);
44 }
45 
46 void
48 {
49  kh_destroy(mt, c->mt);
50 }
51 
52 void
53 mrb_name_class(mrb_state *mrb, struct RClass *c, mrb_sym name)
54 {
55  mrb_obj_iv_set(mrb, (struct RObject*)c,
56  mrb_intern2(mrb, "__classid__", 11), mrb_symbol_value(name));
57 }
58 
59 #define make_metaclass(mrb, c) prepare_singleton_class((mrb), (struct RBasic*)(c))
60 
61 static void
62 prepare_singleton_class(mrb_state *mrb, struct RBasic *o)
63 {
64  struct RClass *sc, *c;
65 
66  if (o->c->tt == MRB_TT_SCLASS) return;
67  sc = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_SCLASS, mrb->class_class);
68  sc->mt = 0;
69  sc->iv = 0;
70  if (o->tt == MRB_TT_CLASS) {
71  c = (struct RClass*)o;
72  if (!c->super) {
73  sc->super = mrb->class_class;
74  }
75  else {
76  sc->super = c->super->c;
77  }
78  }
79  else if (o->tt == MRB_TT_SCLASS) {
80  c = (struct RClass*)o;
81  while (c->super->tt == MRB_TT_ICLASS)
82  c = c->super;
83  make_metaclass(mrb, c->super);
84  sc->super = c->super->c;
85  }
86  else {
87  sc->super = o->c;
88  }
89  o->c = sc;
90  mrb_field_write_barrier(mrb, (struct RBasic*)o, (struct RBasic*)sc);
91  mrb_field_write_barrier(mrb, (struct RBasic*)sc, (struct RBasic*)o);
92  mrb_obj_iv_set(mrb, (struct RObject*)sc, mrb_intern2(mrb, "__attached__", 12), mrb_obj_value(o));
93 }
94 
95 struct RClass*
97 {
98  struct RClass *m = mrb_module_new(mrb);
99 
100  mrb_obj_iv_set(mrb, (struct RObject*)mrb->object_class,
101  name, mrb_obj_value(m));
102  mrb_name_class(mrb, m, name);
103 
104  return m;
105 }
106 
107 struct RClass*
108 mrb_define_module(mrb_state *mrb, const char *name)
109 {
110  return mrb_define_module_id(mrb, mrb_intern_cstr(mrb, name));
111 }
112 
113 static void
114 setup_class(mrb_state *mrb, mrb_value outer, struct RClass *c, mrb_sym id)
115 {
116  mrb_name_class(mrb, c, id);
117  mrb_const_set(mrb, outer, id, mrb_obj_value(c));
118  mrb_obj_iv_set(mrb, (struct RObject*)c,
119  mrb_intern2(mrb, "__outer__", 9), outer);
120 }
121 
122 struct RClass*
124 {
125  mrb_value outer;
126 
127  outer = mrb_obj_iv_get(mrb, (struct RObject*)c, mrb_intern2(mrb, "__outer__", 9));
128  if (mrb_nil_p(outer)) return 0;
129  return mrb_class_ptr(outer);
130 }
131 
132 struct RClass*
134 {
135  struct RClass *c;
136  mrb_value v;
137 
138  if (mrb_const_defined(mrb, outer, id)) {
139  v = mrb_const_get(mrb, outer, id);
140  c = mrb_class_ptr(v);
141  }
142  else {
143  c = mrb_module_new(mrb);
144  setup_class(mrb, outer, c, id);
145  }
146  return c;
147 }
148 
149 struct RClass*
151 {
152  struct RClass *c = mrb_class_new(mrb, super);
153 
154  mrb_obj_iv_set(mrb, (struct RObject*)mrb->object_class,
155  name, mrb_obj_value(c));
156  mrb_name_class(mrb, c, name);
157 
158  return c;
159 }
160 
161 struct RClass*
162 mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super)
163 {
164  return mrb_define_class_id(mrb, mrb_intern_cstr(mrb, name), super);
165 }
166 
167 struct RClass*
169 {
170  struct RClass *c, *s;
171 
172  if (mrb_const_defined(mrb, outer, id)) {
173  mrb_value v = mrb_const_get(mrb, outer, id);
174 
175  mrb_check_type(mrb, v, MRB_TT_CLASS);
176  c = mrb_class_ptr(v);
177  if (!mrb_nil_p(super)) {
178  if (mrb_type(super) != MRB_TT_CLASS) {
179  mrb_raisef(mrb, E_TYPE_ERROR, "superclass must be a Class (%S given)", super);
180  }
181 
182  if (!c->super || mrb_class_ptr(super) != mrb_class_real(c->super)) {
183  mrb_raisef(mrb, E_TYPE_ERROR, "superclass mismatch for class %S", mrb_sym2str(mrb, id));
184  }
185  }
186  return c;
187  }
188 
189  if (!mrb_nil_p(super)) {
190  if (mrb_type(super) != MRB_TT_CLASS) {
191  mrb_raisef(mrb, E_TYPE_ERROR, "superclass must be a Class (%S given)", super);
192  }
193  s = mrb_class_ptr(super);
194  }
195  else {
196  s = mrb->object_class;
197  }
198 
199  c = mrb_class_new(mrb, s);
200  setup_class(mrb, outer, c, id);
201  mrb_funcall(mrb, mrb_obj_value(s), "inherited", 1, mrb_obj_value(c));
202 
203  return c;
204 }
205 
206 mrb_bool
207 mrb_class_defined(mrb_state *mrb, const char *name)
208 {
209  mrb_value sym = mrb_check_intern_cstr(mrb, name);
210  if (mrb_nil_p(sym)) {
211  return FALSE;
212  }
213  return mrb_const_defined(mrb, mrb_obj_value(mrb->object_class), mrb_symbol(sym));
214 }
215 
216 static struct RClass *
217 class_from_sym(mrb_state *mrb, struct RClass *klass, mrb_sym id)
218 {
219  mrb_value c = mrb_const_get(mrb, mrb_obj_value(klass), id);
220 
221  if (mrb_type(c) != MRB_TT_MODULE && mrb_type(c) != MRB_TT_CLASS) {
222  mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a class/module", mrb_sym2str(mrb, id));
223  }
224  return mrb_class_ptr(c);
225 }
226 
227 struct RClass *
228 mrb_class_get(mrb_state *mrb, const char *name)
229 {
230  return mrb_class_get_under(mrb, mrb->object_class, name);
231 }
232 
233 struct RClass *
234 mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name)
235 {
236  return class_from_sym(mrb, outer, mrb_intern_cstr(mrb, name));
237 }
238 
255 struct RClass *
256 mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super)
257 {
258  struct RClass * c;
259  mrb_sym id = mrb_intern_cstr(mrb, name);
260 
261  if (mrb_const_defined_at(mrb, outer, id)) {
262  c = class_from_sym(mrb, outer, id);
263  if (mrb_class_real(c->super) != super) {
264  mrb_name_error(mrb, id, "%S is already defined", name);
265  }
266  return c;
267  }
268  if (!super) {
269  mrb_warn(mrb, "no super class for `%S::%S', Object assumed", outer, name);
270  }
271  c = mrb_class_new(mrb, super);
272  setup_class(mrb, mrb_obj_value(outer), c, id);
273 
274  return c;
275 }
276 
277 struct RClass *
278 mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name)
279 {
280  struct RClass * c;
281  mrb_sym id = mrb_intern_cstr(mrb, name);
282 
283  if (mrb_const_defined_at(mrb, outer, id)) {
284  c = class_from_sym(mrb, outer, id);
285  return c;
286  }
287  c = mrb_module_new(mrb);
288  setup_class(mrb, mrb_obj_value(outer), c, id);
289 
290  return c;
291 }
292 
293 void
294 mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, struct RProc *p)
295 {
296  khash_t(mt) *h = c->mt;
297  khiter_t k;
298 
299  if (!h) h = c->mt = kh_init(mt, mrb);
300  k = kh_put(mt, h, mid);
301  kh_value(h, k) = p;
302  if (p) {
303  mrb_field_write_barrier(mrb, (struct RBasic *)c, (struct RBasic *)p);
304  }
305 }
306 
307 void
309 {
310  struct RProc *p;
311  int ai = mrb_gc_arena_save(mrb);
312 
313  p = mrb_proc_new_cfunc(mrb, func);
314  p->target_class = c;
315  mrb_define_method_raw(mrb, c, mid, p);
316  mrb_gc_arena_restore(mrb, ai);
317 }
318 
319 void
320 mrb_define_method(mrb_state *mrb, struct RClass *c, const char *name, mrb_func_t func, mrb_aspec aspec)
321 {
322  mrb_define_method_id(mrb, c, mrb_intern_cstr(mrb, name), func, aspec);
323 }
324 
325 void
327 {
328  khash_t(mt) *h = c->mt;
329  khiter_t k;
330  struct RProc *p;
331 
332  if (!h) h = c->mt = kh_init(mt, mrb);
333  k = kh_put(mt, h, name);
334  p = mrb_proc_ptr(body);
335  kh_value(h, k) = p;
336  if (p) {
337  mrb_field_write_barrier(mrb, (struct RBasic *)c, (struct RBasic *)p);
338  }
339 }
340 
341 static mrb_value
342 check_type(mrb_state *mrb, mrb_value val, enum mrb_vtype t, const char *c, const char *m)
343 {
344  mrb_value tmp;
345 
346  tmp = mrb_check_convert_type(mrb, val, t, c, m);
347  if (mrb_nil_p(tmp)) {
348  mrb_raisef(mrb, E_TYPE_ERROR, "expected %S", mrb_str_new_cstr(mrb, c));
349  }
350  return tmp;
351 }
352 
353 static mrb_value
354 to_str(mrb_state *mrb, mrb_value val)
355 {
356  return check_type(mrb, val, MRB_TT_STRING, "String", "to_str");
357 }
358 
359 static mrb_value
360 to_ary(mrb_state *mrb, mrb_value val)
361 {
362  return check_type(mrb, val, MRB_TT_ARRAY, "Array", "to_ary");
363 }
364 
365 static mrb_value
366 to_hash(mrb_state *mrb, mrb_value val)
367 {
368  return check_type(mrb, val, MRB_TT_HASH, "Hash", "to_hash");
369 }
370 
371 /*
372  retrieve arguments from mrb_state.
373 
374  mrb_get_args(mrb, format, ...)
375 
376  returns number of arguments parsed.
377 
378  format specifiers:
379 
380  string mruby type C type note
381  ----------------------------------------------------------------------------------------------
382  o: Object [mrb_value]
383  S: String [mrb_value]
384  A: Array [mrb_value]
385  H: Hash [mrb_value]
386  s: String [char*,int] Receive two arguments.
387  z: String [char*] NUL terminated string.
388  a: Array [mrb_value*,mrb_int] Receive two arguments.
389  f: Float [mrb_float]
390  i: Integer [mrb_int]
391  b: Boolean [mrb_bool]
392  n: Symbol [mrb_sym]
393  &: Block [mrb_value]
394  *: rest argument [mrb_value*,int] Receive the rest of the arguments as an array.
395  |: optional Next argument of '|' and later are optional.
396  */
397 int
398 mrb_get_args(mrb_state *mrb, const char *format, ...)
399 {
400  char c;
401  int i = 0;
402  mrb_value *sp = mrb->c->stack + 1;
403  va_list ap;
404  int argc = mrb->c->ci->argc;
405  int opt = 0;
406 
407  va_start(ap, format);
408  if (argc < 0) {
409  struct RArray *a = mrb_ary_ptr(mrb->c->stack[1]);
410 
411  argc = a->len;
412  sp = a->ptr;
413  }
414  while ((c = *format++)) {
415  switch (c) {
416  case '|': case '*': case '&':
417  break;
418  default:
419  if (argc <= i && !opt) {
420  mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
421  }
422  break;
423  }
424 
425  switch (c) {
426  case 'o':
427  {
428  mrb_value *p;
429 
430  p = va_arg(ap, mrb_value*);
431  if (i < argc) {
432  *p = *sp++;
433  i++;
434  }
435  }
436  break;
437  case 'S':
438  {
439  mrb_value *p;
440 
441  p = va_arg(ap, mrb_value*);
442  if (i < argc) {
443  *p = to_str(mrb, *sp++);
444  i++;
445  }
446  }
447  break;
448  case 'A':
449  {
450  mrb_value *p;
451 
452  p = va_arg(ap, mrb_value*);
453  if (i < argc) {
454  *p = to_ary(mrb, *sp++);
455  i++;
456  }
457  }
458  break;
459  case 'H':
460  {
461  mrb_value *p;
462 
463  p = va_arg(ap, mrb_value*);
464  if (i < argc) {
465  *p = to_hash(mrb, *sp++);
466  i++;
467  }
468  }
469  break;
470  case 's':
471  {
472  mrb_value ss;
473  struct RString *s;
474  char **ps = 0;
475  int *pl = 0;
476 
477  ps = va_arg(ap, char**);
478  pl = va_arg(ap, int*);
479  if (i < argc) {
480  ss = to_str(mrb, *sp++);
481  s = mrb_str_ptr(ss);
482  *ps = s->ptr;
483  *pl = s->len;
484  i++;
485  }
486  }
487  break;
488  case 'z':
489  {
490  mrb_value ss;
491  struct RString *s;
492  char **ps;
493  mrb_int len;
494 
495  ps = va_arg(ap, char**);
496  if (i < argc) {
497  ss = to_str(mrb, *sp++);
498  s = mrb_str_ptr(ss);
499  len = (mrb_int)strlen(s->ptr);
500  if (len < s->len) {
501  mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL");
502  }
503  else if (len > s->len) {
504  mrb_str_modify(mrb, s);
505  }
506  *ps = s->ptr;
507  i++;
508  }
509  }
510  break;
511  case 'a':
512  {
513  mrb_value aa;
514  struct RArray *a;
515  mrb_value **pb;
516  mrb_int *pl;
517 
518  pb = va_arg(ap, mrb_value**);
519  pl = va_arg(ap, mrb_int*);
520  if (i < argc) {
521  aa = to_ary(mrb, *sp++);
522  a = mrb_ary_ptr(aa);
523  *pb = a->ptr;
524  *pl = a->len;
525  i++;
526  }
527  }
528  break;
529  case 'f':
530  {
531  mrb_float *p;
532 
533  p = va_arg(ap, mrb_float*);
534  if (i < argc) {
535  switch (mrb_type(*sp)) {
536  case MRB_TT_FLOAT:
537  *p = mrb_float(*sp);
538  break;
539  case MRB_TT_FIXNUM:
540  *p = (mrb_float)mrb_fixnum(*sp);
541  break;
542  case MRB_TT_STRING:
543  mrb_raise(mrb, E_TYPE_ERROR, "String can't be coerced into Float");
544  break;
545  default:
546  {
547  mrb_value tmp;
548 
549  tmp = mrb_convert_type(mrb, *sp, MRB_TT_FLOAT, "Float", "to_f");
550  *p = mrb_float(tmp);
551  }
552  break;
553  }
554  sp++;
555  i++;
556  }
557  }
558  break;
559  case 'i':
560  {
561  mrb_int *p;
562 
563  p = va_arg(ap, mrb_int*);
564  if (i < argc) {
565  switch (mrb_type(*sp)) {
566  case MRB_TT_FIXNUM:
567  *p = mrb_fixnum(*sp);
568  break;
569  case MRB_TT_FLOAT:
570  {
571  mrb_float f = mrb_float(*sp);
572 
573  if (!FIXABLE(f)) {
574  mrb_raise(mrb, E_RANGE_ERROR, "float too big for int");
575  }
576  *p = (mrb_int)f;
577  }
578  break;
579  case MRB_TT_FALSE:
580  *p = 0;
581  break;
582  default:
583  {
584  mrb_value tmp;
585 
586  tmp = mrb_convert_type(mrb, *sp, MRB_TT_FIXNUM, "Integer", "to_int");
587  *p = mrb_fixnum(tmp);
588  }
589  break;
590  }
591  sp++;
592  i++;
593  }
594  }
595  break;
596  case 'b':
597  {
598  mrb_bool *boolp = va_arg(ap, mrb_bool*);
599 
600  if (i < argc) {
601  mrb_value b = *sp++;
602  *boolp = mrb_test(b);
603  i++;
604  }
605  }
606  break;
607  case 'n':
608  {
609  mrb_sym *symp;
610 
611  symp = va_arg(ap, mrb_sym*);
612  if (i < argc) {
613  mrb_value ss;
614 
615  ss = *sp++;
616  if (mrb_type(ss) == MRB_TT_SYMBOL) {
617  *symp = mrb_symbol(ss);
618  }
619  else if (mrb_string_p(ss)) {
620  *symp = mrb_intern_str(mrb, to_str(mrb, ss));
621  }
622  else {
623  mrb_value obj = mrb_funcall(mrb, ss, "inspect", 0);
624  mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", obj);
625  }
626  i++;
627  }
628  }
629  break;
630 
631  case '&':
632  {
633  mrb_value *p, *bp;
634 
635  p = va_arg(ap, mrb_value*);
636  if (mrb->c->ci->argc < 0) {
637  bp = mrb->c->stack + 2;
638  }
639  else {
640  bp = mrb->c->stack + mrb->c->ci->argc + 1;
641  }
642  *p = *bp;
643  }
644  break;
645  case '|':
646  opt = 1;
647  break;
648 
649  case '*':
650  {
651  mrb_value **var;
652  int *pl;
653 
654  var = va_arg(ap, mrb_value**);
655  pl = va_arg(ap, int*);
656  if (argc > i) {
657  *pl = argc-i;
658  if (*pl > 0) {
659  *var = sp;
660  }
661  i = argc;
662  sp += *pl;
663  }
664  else {
665  *pl = 0;
666  *var = NULL;
667  }
668  }
669  break;
670  default:
671  mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid argument specifier %S", mrb_str_new(mrb, &c, 1));
672  break;
673  }
674  }
675  if (!c && argc > i) {
676  mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
677  }
678  va_end(ap);
679  return i;
680 }
681 
682 static struct RClass*
683 boot_defclass(mrb_state *mrb, struct RClass *super)
684 {
685  struct RClass *c;
686 
687  c = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_CLASS, mrb->class_class);
688  c->super = super ? super : mrb->object_class;
689  mrb_field_write_barrier(mrb, (struct RBasic*)c, (struct RBasic*)super);
690  c->mt = kh_init(mt, mrb);
691  return c;
692 }
693 
694 void
695 mrb_include_module(mrb_state *mrb, struct RClass *c, struct RClass *m)
696 {
697  struct RClass *ins_pos;
698 
699  ins_pos = c;
700  while (m) {
701  struct RClass *p = c, *ic;
702  int superclass_seen = 0;
703 
704  if (c->mt == m->mt) {
705  mrb_raise(mrb, E_ARGUMENT_ERROR, "cyclic include detected");
706  }
707  while (p) {
708  if (c != p && p->tt == MRB_TT_CLASS) {
709  superclass_seen = 1;
710  }
711  else if (p->mt == m->mt){
712  if (p->tt == MRB_TT_ICLASS && !superclass_seen) {
713  ins_pos = p;
714  }
715  goto skip;
716  }
717  p = p->super;
718  }
719  ic = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_ICLASS, mrb->class_class);
720  if (m->tt == MRB_TT_ICLASS) {
721  ic->c = m->c;
722  }
723  else {
724  ic->c = m;
725  }
726  ic->mt = m->mt;
727  ic->iv = m->iv;
728  ic->super = ins_pos->super;
729  ins_pos->super = ic;
730  mrb_field_write_barrier(mrb, (struct RBasic*)ins_pos, (struct RBasic*)ic);
731  ins_pos = ic;
732  skip:
733  m = m->super;
734  }
735 }
736 
737 static mrb_value
738 mrb_mod_append_features(mrb_state *mrb, mrb_value mod)
739 {
740  mrb_value klass;
741 
742  mrb_check_type(mrb, mod, MRB_TT_MODULE);
743  mrb_get_args(mrb, "o", &klass);
745  return mod;
746 }
747 
748 static mrb_value
749 mrb_mod_include(mrb_state *mrb, mrb_value klass)
750 {
751  mrb_value *argv;
752  int argc, i;
753 
754  mrb_get_args(mrb, "*", &argv, &argc);
755  for (i=0; i<argc; i++) {
756  mrb_check_type(mrb, argv[i], MRB_TT_MODULE);
757  }
758  while (argc--) {
759  mrb_funcall(mrb, argv[argc], "append_features", 1, klass);
760  mrb_funcall(mrb, argv[argc], "included", 1, klass);
761  }
762 
763  return klass;
764 }
765 
766 /* 15.2.2.4.28 */
767 /*
768  * call-seq:
769  * mod.include?(module) -> true or false
770  *
771  * Returns <code>true</code> if <i>module</i> is included in
772  * <i>mod</i> or one of <i>mod</i>'s ancestors.
773  *
774  * module A
775  * end
776  * class B
777  * include A
778  * end
779  * class C < B
780  * end
781  * B.include?(A) #=> true
782  * C.include?(A) #=> true
783  * A.include?(A) #=> false
784  */
785 static mrb_value
786 mrb_mod_include_p(mrb_state *mrb, mrb_value mod)
787 {
788  mrb_value mod2;
789  struct RClass *c = mrb_class_ptr(mod);
790 
791  mrb_get_args(mrb, "o", &mod2);
792  mrb_check_type(mrb, mod2, MRB_TT_MODULE);
793 
794  while (c) {
795  if (c->tt == MRB_TT_ICLASS) {
796  if (c->c == mrb_class_ptr(mod2)) return mrb_true_value();
797  }
798  c = c->super;
799  }
800  return mrb_false_value();
801 }
802 
803 static mrb_value
804 mrb_mod_ancestors(mrb_state *mrb, mrb_value self)
805 {
806  mrb_value result;
807  struct RClass *c = mrb_class_ptr(self);
808 
809  result = mrb_ary_new(mrb);
810  mrb_ary_push(mrb, result, mrb_obj_value(c));
811  c = c->super;
812  while (c) {
813  if (c->tt == MRB_TT_ICLASS) {
814  mrb_ary_push(mrb, result, mrb_obj_value(c->c));
815  }
816  else if (c->tt != MRB_TT_SCLASS) {
817  mrb_ary_push(mrb, result, mrb_obj_value(c));
818  }
819  c = c->super;
820  }
821 
822  return result;
823 }
824 
825 static mrb_value
826 mrb_mod_extend_object(mrb_state *mrb, mrb_value mod)
827 {
828  mrb_value obj;
829 
830  mrb_check_type(mrb, mod, MRB_TT_MODULE);
831  mrb_get_args(mrb, "o", &obj);
833  return mod;
834 }
835 
836 static mrb_value
837 mrb_mod_included_modules(mrb_state *mrb, mrb_value self)
838 {
839  mrb_value result;
840  struct RClass *c = mrb_class_ptr(self);
841 
842  result = mrb_ary_new(mrb);
843  while (c) {
844  if (c->tt == MRB_TT_ICLASS) {
845  mrb_ary_push(mrb, result, mrb_obj_value(c->c));
846  }
847  c = c->super;
848  }
849 
850  return result;
851 }
852 
854 
855 /* 15.2.2.4.33 */
856 /*
857  * call-seq:
858  * mod.instance_methods(include_super=true) -> array
859  *
860  * Returns an array containing the names of the public and protected instance
861  * methods in the receiver. For a module, these are the public and protected methods;
862  * for a class, they are the instance (not singleton) methods. With no
863  * argument, or with an argument that is <code>false</code>, the
864  * instance methods in <i>mod</i> are returned, otherwise the methods
865  * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
866  *
867  * module A
868  * def method1() end
869  * end
870  * class B
871  * def method2() end
872  * end
873  * class C < B
874  * def method3() end
875  * end
876  *
877  * A.instance_methods #=> [:method1]
878  * B.instance_methods(false) #=> [:method2]
879  * C.instance_methods(false) #=> [:method3]
880  * C.instance_methods(true).length #=> 43
881  */
882 
883 static mrb_value
884 mrb_mod_instance_methods(mrb_state *mrb, mrb_value mod)
885 {
886  struct RClass *c = mrb_class_ptr(mod);
887  mrb_bool recur = TRUE;
888  mrb_get_args(mrb, "|b", &recur);
889  return class_instance_method_list(mrb, recur, c, 0);
890 }
891 
892 mrb_value mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self, struct RClass *c);
893 
894 /* 15.2.2.4.35 */
895 /*
896  * call-seq:
897  * mod.class_eval {| | block } -> obj
898  * mod.module_eval {| | block } -> obj
899  *
900  * Evaluates block in the context of _mod_. This can
901  * be used to add methods to a class. <code>module_eval</code> returns
902  * the result of evaluating its argument.
903  */
904 
905 mrb_value
907 {
908  mrb_value a, b;
909  struct RClass *c;
910 
911  if (mrb_get_args(mrb, "|S&", &a, &b) == 1) {
912  mrb_raise(mrb, E_NOTIMP_ERROR, "module_eval/class_eval with string not implemented");
913  }
914  c = mrb_class_ptr(mod);
915  return mrb_yield_internal(mrb, b, 0, 0, mod, c);
916 }
917 
918 mrb_value
920 {
921  return mod;
922 }
923 
924 mrb_value
926 {
927  struct RBasic *obj;
928 
929  switch (mrb_type(v)) {
930  case MRB_TT_FALSE:
931  if (mrb_nil_p(v))
932  return mrb_obj_value(mrb->nil_class);
933  return mrb_obj_value(mrb->false_class);
934  case MRB_TT_TRUE:
935  return mrb_obj_value(mrb->true_class);
936  case MRB_TT_CPTR:
937  return mrb_obj_value(mrb->object_class);
938  case MRB_TT_SYMBOL:
939  case MRB_TT_FIXNUM:
940  case MRB_TT_FLOAT:
941  mrb_raise(mrb, E_TYPE_ERROR, "can't define singleton");
942  return mrb_nil_value(); /* not reached */
943  default:
944  break;
945  }
946  obj = mrb_basic_ptr(v);
947  prepare_singleton_class(mrb, obj);
948  return mrb_obj_value(obj->c);
949 }
950 
951 void
952 mrb_define_singleton_method(mrb_state *mrb, struct RObject *o, const char *name, mrb_func_t func, mrb_aspec aspec)
953 {
954  prepare_singleton_class(mrb, (struct RBasic*)o);
955  mrb_define_method_id(mrb, o->c, mrb_intern_cstr(mrb, name), func, aspec);
956 }
957 
958 void
959 mrb_define_class_method(mrb_state *mrb, struct RClass *c, const char *name, mrb_func_t func, mrb_aspec aspec)
960 {
961  mrb_define_singleton_method(mrb, (struct RObject*)c, name, func, aspec);
962 }
963 
964 void
965 mrb_define_module_function(mrb_state *mrb, struct RClass *c, const char *name, mrb_func_t func, mrb_aspec aspec)
966 {
967  mrb_define_class_method(mrb, c, name, func, aspec);
968  mrb_define_method(mrb, c, name, func, aspec);
969 }
970 
971 struct RProc*
973 {
974  khiter_t k;
975  struct RProc *m;
976  struct RClass *c = *cp;
977 
978  while (c) {
979  khash_t(mt) *h = c->mt;
980 
981  if (h) {
982  k = kh_get(mt, h, mid);
983  if (k != kh_end(h)) {
984  m = kh_value(h, k);
985  if (!m) break;
986  *cp = c;
987  return m;
988  }
989  }
990  c = c->super;
991  }
992  return 0; /* no method */
993 }
994 
995 struct RProc*
997 {
998  struct RProc *m;
999 
1000  m = mrb_method_search_vm(mrb, &c, mid);
1001  if (!m) {
1002  mrb_value inspect = mrb_funcall(mrb, mrb_obj_value(c), "inspect", 0);
1003  if (RSTRING_LEN(inspect) > 64) {
1004  inspect = mrb_any_to_s(mrb, mrb_obj_value(c));
1005  }
1006  mrb_name_error(mrb, mid, "undefined method '%S' for class %S",
1007  mrb_sym2str(mrb, mid), inspect);
1008  }
1009  return m;
1010 }
1011 
1012 static mrb_value
1013 mrb_instance_alloc(mrb_state *mrb, mrb_value cv)
1014 {
1015  struct RClass *c = mrb_class_ptr(cv);
1016  struct RObject *o;
1017  enum mrb_vtype ttype = MRB_INSTANCE_TT(c);
1018 
1019  if (c->tt == MRB_TT_SCLASS)
1020  mrb_raise(mrb, E_TYPE_ERROR, "can't create instance of singleton class");
1021 
1022  if (ttype == 0) ttype = MRB_TT_OBJECT;
1023  o = (struct RObject*)mrb_obj_alloc(mrb, ttype, c);
1024  return mrb_obj_value(o);
1025 }
1026 
1027 /*
1028  * call-seq:
1029  * class.new(args, ...) -> obj
1030  *
1031  * Calls <code>allocate</code> to create a new object of
1032  * <i>class</i>'s class, then invokes that object's
1033  * <code>initialize</code> method, passing it <i>args</i>.
1034  * This is the method that ends up getting called whenever
1035  * an object is constructed using .new.
1036  *
1037  */
1038 
1039 mrb_value
1041 {
1042  mrb_value obj, blk;
1043  mrb_value *argv;
1044  int argc;
1045 
1046  obj = mrb_instance_alloc(mrb, cv);
1047  mrb_get_args(mrb, "*&", &argv, &argc, &blk);
1048  mrb_funcall_with_block(mrb, obj, mrb_intern(mrb, "initialize"), argc, argv, blk);
1049 
1050  return obj;
1051 }
1052 
1053 mrb_value
1054 mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv)
1055 {
1056  mrb_value obj;
1057 
1058  obj = mrb_instance_alloc(mrb, mrb_obj_value(c));
1059  mrb_funcall_argv(mrb, obj, mrb_intern(mrb, "initialize"), argc, argv);
1060 
1061  return obj;
1062 }
1063 
1064 static mrb_value
1065 mrb_class_new_class(mrb_state *mrb, mrb_value cv)
1066 {
1067  mrb_value super;
1068  struct RClass *new_class;
1069 
1070  if (mrb_get_args(mrb, "|o", &super) == 0) {
1071  super = mrb_obj_value(mrb->object_class);
1072  }
1073  new_class = mrb_class_new(mrb, mrb_class_ptr(super));
1074  mrb_funcall(mrb, super, "inherited", 1, mrb_obj_value(new_class));
1075  return mrb_obj_value(new_class);
1076 }
1077 
1078 mrb_value
1080 {
1081  struct RClass *c;
1082 
1083  c = mrb_class_ptr(klass);
1084  c = c->super;
1085  while (c && c->tt == MRB_TT_ICLASS) {
1086  c = c->super;
1087  }
1088  if (!c) return mrb_nil_value();
1089  return mrb_obj_value(c);
1090 }
1091 
1092 static mrb_value
1093 mrb_bob_init(mrb_state *mrb, mrb_value cv)
1094 {
1095  return mrb_nil_value();
1096 }
1097 
1098 static mrb_value
1099 mrb_bob_not(mrb_state *mrb, mrb_value cv)
1100 {
1101  return mrb_bool_value(!mrb_test(cv));
1102 }
1103 
1104 /* 15.3.1.3.30 */
1105 /*
1106  * call-seq:
1107  * obj.method_missing(symbol [, *args] ) -> result
1108  *
1109  * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
1110  * <i>symbol</i> is the symbol for the method called, and <i>args</i>
1111  * are any arguments that were passed to it. By default, the interpreter
1112  * raises an error when this method is called. However, it is possible
1113  * to override the method to provide more dynamic behavior.
1114  * If it is decided that a particular method should not be handled, then
1115  * <i>super</i> should be called, so that ancestors can pick up the
1116  * missing method.
1117  * The example below creates
1118  * a class <code>Roman</code>, which responds to methods with names
1119  * consisting of roman numerals, returning the corresponding integer
1120  * values.
1121  *
1122  * class Roman
1123  * def romanToInt(str)
1124  * # ...
1125  * end
1126  * def method_missing(methId)
1127  * str = methId.id2name
1128  * romanToInt(str)
1129  * end
1130  * end
1131  *
1132  * r = Roman.new
1133  * r.iv #=> 4
1134  * r.xxiii #=> 23
1135  * r.mm #=> 2000
1136  */
1137 static mrb_value
1138 mrb_bob_missing(mrb_state *mrb, mrb_value mod)
1139 {
1140  mrb_sym name;
1141  mrb_value *a;
1142  int alen;
1143  mrb_value inspect;
1144 
1145  mrb_get_args(mrb, "n*", &name, &a, &alen);
1146 
1147  if (mrb_respond_to(mrb,mod,mrb_intern2(mrb, "inspect",7))){
1148  inspect = mrb_funcall(mrb, mod, "inspect", 0);
1149  if (RSTRING_LEN(inspect) > 64) {
1150  inspect = mrb_any_to_s(mrb, mod);
1151  }
1152  }
1153  else {
1154  inspect = mrb_any_to_s(mrb, mod);
1155  }
1156 
1157  mrb_raisef(mrb, E_NOMETHOD_ERROR, "undefined method '%S' for %S",
1158  mrb_sym2str(mrb, name), inspect);
1159  /* not reached */
1160  return mrb_nil_value();
1161 }
1162 
1163 mrb_bool
1165 {
1166  khiter_t k;
1167 
1168  while (c) {
1169  khash_t(mt) *h = c->mt;
1170 
1171  if (h) {
1172  k = kh_get(mt, h, mid);
1173  if (k != kh_end(h)) {
1174  if (kh_value(h, k)) {
1175  return TRUE; /* method exists */
1176  }
1177  else {
1178  return FALSE; /* undefined method */
1179  }
1180  }
1181  }
1182  c = c->super;
1183  }
1184  return FALSE; /* no method */
1185 }
1186 
1187 mrb_bool
1189 {
1190  return mrb_obj_respond_to(mrb_class(mrb, obj), mid);
1191 }
1192 
1193 mrb_value
1195 {
1196  mrb_value path;
1197  const char *name;
1198  size_t len;
1199  mrb_sym classpath = mrb_intern2(mrb, "__classpath__", 13);
1200 
1201  path = mrb_obj_iv_get(mrb, (struct RObject*)c, classpath);
1202  if (mrb_nil_p(path)) {
1203  struct RClass *outer = mrb_class_outer_module(mrb, c);
1204  mrb_sym sym = mrb_class_sym(mrb, c, outer);
1205  if (sym == 0) {
1206  return mrb_nil_value();
1207  }
1208  else if (outer && outer != mrb->object_class) {
1209  mrb_value base = mrb_class_path(mrb, outer);
1210  path = mrb_str_plus(mrb, base, mrb_str_new(mrb, "::", 2));
1211  name = mrb_sym2name_len(mrb, sym, &len);
1212  mrb_str_concat(mrb, path, mrb_str_new(mrb, name, len));
1213  }
1214  else {
1215  name = mrb_sym2name_len(mrb, sym, &len);
1216  path = mrb_str_new(mrb, name, len);
1217  }
1218  mrb_obj_iv_set(mrb, (struct RObject*)c, classpath, path);
1219  }
1220  return path;
1221 }
1222 
1223 struct RClass *
1225 {
1226  while ((cl->tt == MRB_TT_SCLASS) || (cl->tt == MRB_TT_ICLASS)) {
1227  cl = cl->super;
1228  }
1229  return cl;
1230 }
1231 
1232 const char*
1234 {
1235  mrb_value path = mrb_class_path(mrb, c);
1236  if (mrb_nil_p(path)) {
1237  path = mrb_str_new(mrb, "#<Class:", 8);
1238  mrb_str_concat(mrb, path, mrb_ptr_to_str(mrb, c));
1239  mrb_str_cat(mrb, path, ">", 1);
1240  }
1241  return mrb_str_ptr(path)->ptr;
1242 }
1243 
1244 const char*
1246 {
1247  return mrb_class_name(mrb, mrb_obj_class(mrb, obj));
1248 }
1249 
1256 void
1258 {
1259  if (super->tt != MRB_TT_CLASS) {
1260  mrb_raisef(mrb, E_TYPE_ERROR, "superclass must be a Class (%S given)", mrb_obj_value(super));
1261  }
1262  if (super->tt == MRB_TT_SCLASS) {
1263  mrb_raise(mrb, E_TYPE_ERROR, "can't make subclass of singleton class");
1264  }
1265  if (super == mrb->class_class) {
1266  mrb_raise(mrb, E_TYPE_ERROR, "can't make subclass of Class");
1267  }
1268 }
1269 
1276 struct RClass *
1277 mrb_class_new(mrb_state *mrb, struct RClass *super)
1278 {
1279  struct RClass *c;
1280 
1281  if (super) {
1282  mrb_check_inheritable(mrb, super);
1283  }
1284  c = boot_defclass(mrb, super);
1285  if (super){
1287  }
1288  make_metaclass(mrb, c);
1289 
1290  return c;
1291 }
1292 
1296 struct RClass *
1298 {
1299  struct RClass *m = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_MODULE, mrb->module_class);
1300  m->mt = kh_init(mt, mrb);
1301 
1302  return m;
1303 }
1304 
1305 /*
1306  * call-seq:
1307  * obj.class => class
1308  *
1309  * Returns the class of <i>obj</i>, now preferred over
1310  * <code>Object#type</code>, as an object's type in Ruby is only
1311  * loosely tied to that object's class. This method must always be
1312  * called with an explicit receiver, as <code>class</code> is also a
1313  * reserved word in Ruby.
1314  *
1315  * 1.class #=> Fixnum
1316  * self.class #=> Object
1317  */
1318 
1319 struct RClass*
1321 {
1322  return mrb_class_real(mrb_class(mrb, obj));
1323 }
1324 
1325 void
1327 {
1328  struct RProc *m = mrb_method_search(mrb, c, b);
1329 
1330  mrb_define_method_vm(mrb, c, a, mrb_obj_value(m));
1331 }
1332 
1339 void
1340 mrb_define_alias(mrb_state *mrb, struct RClass *klass, const char *name1, const char *name2)
1341 {
1342  mrb_alias_method(mrb, klass, mrb_intern_cstr(mrb, name1), mrb_intern_cstr(mrb, name2));
1343 }
1344 
1345 /*
1346  * call-seq:
1347  * mod.to_s -> string
1348  *
1349  * Return a string representing this module or class. For basic
1350  * classes and modules, this is the name. For singletons, we
1351  * show information on the thing we're attached to as well.
1352  */
1353 
1354 static mrb_value
1355 mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
1356 {
1357  mrb_value str;
1358 
1359  if (mrb_type(klass) == MRB_TT_SCLASS) {
1360  mrb_value v = mrb_iv_get(mrb, klass, mrb_intern2(mrb, "__attached__", 12));
1361 
1362  str = mrb_str_new(mrb, "#<Class:", 8);
1363 
1364  switch (mrb_type(v)) {
1365  case MRB_TT_CLASS:
1366  case MRB_TT_MODULE:
1367  case MRB_TT_SCLASS:
1368  mrb_str_append(mrb, str, mrb_inspect(mrb, v));
1369  break;
1370  default:
1371  mrb_str_append(mrb, str, mrb_any_to_s(mrb, v));
1372  break;
1373  }
1374  mrb_str_cat(mrb, str, ">", 1);
1375  }
1376  else {
1377  struct RClass *c;
1378  mrb_value path;
1379 
1380  str = mrb_str_buf_new(mrb, 32);
1381  c = mrb_class_ptr(klass);
1382  path = mrb_class_path(mrb, c);
1383 
1384  if (mrb_nil_p(path)) {
1385  switch (mrb_type(klass)) {
1386  case MRB_TT_CLASS:
1387  mrb_str_cat(mrb, str, "#<Class:", 8);
1388  break;
1389 
1390  case MRB_TT_MODULE:
1391  mrb_str_cat(mrb, str, "#<Module:", 9);
1392  break;
1393 
1394  default:
1395  /* Shouldn't be happened? */
1396  mrb_str_cat(mrb, str, "#<??????:", 9);
1397  break;
1398  }
1399  mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, c));
1400  mrb_str_cat(mrb, str, ">", 1);
1401  }
1402  else {
1403  str = path;
1404  }
1405  }
1406 
1407  return str;
1408 }
1409 
1410 mrb_value
1412 {
1413  struct RClass *c = mrb_class_ptr(mod);
1414  mrb_sym new_name, old_name;
1415 
1416  mrb_get_args(mrb, "nn", &new_name, &old_name);
1417  mrb_alias_method(mrb, c, new_name, old_name);
1418  return mrb_nil_value();
1419 }
1420 
1421 static void
1422 undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
1423 {
1424  mrb_value m;
1425 
1426  if (!mrb_obj_respond_to(c, a)) {
1427  mrb_name_error(mrb, a, "undefined method '%S' for class '%S'", mrb_sym2str(mrb, a), mrb_obj_value(c));
1428  }
1429  else {
1430  MRB_SET_VALUE(m, MRB_TT_PROC, value.p, 0);
1431  mrb_define_method_vm(mrb, c, a, m);
1432  }
1433 }
1434 
1435 void
1436 mrb_undef_method(mrb_state *mrb, struct RClass *c, const char *name)
1437 {
1438  undef_method(mrb, c, mrb_intern_cstr(mrb, name));
1439 }
1440 
1441 void
1442 mrb_undef_class_method(mrb_state *mrb, struct RClass *c, const char *name)
1443 {
1444  mrb_undef_method(mrb, mrb_class_ptr(mrb_singleton_class(mrb, mrb_obj_value(c))), name);
1445 }
1446 
1447 mrb_value
1449 {
1450  struct RClass *c = mrb_class_ptr(mod);
1451  int argc;
1452  mrb_value *argv;
1453 
1454  mrb_get_args(mrb, "*", &argv, &argc);
1455  while (argc--) {
1456  undef_method(mrb, c, mrb_symbol(*argv));
1457  argv++;
1458  }
1459  return mrb_nil_value();
1460 }
1461 
1462 static mrb_value
1463 mod_define_method(mrb_state *mrb, mrb_value self)
1464 {
1465  struct RClass *c = mrb_class_ptr(self);
1466  struct RProc *p;
1467  mrb_sym mid;
1468  mrb_value blk;
1469 
1470  mrb_get_args(mrb, "n&", &mid, &blk);
1471  if (mrb_nil_p(blk)) {
1472  mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
1473  }
1474  p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
1475  mrb_proc_copy(p, mrb_proc_ptr(blk));
1476  p->flags |= MRB_PROC_STRICT;
1477  mrb_define_method_raw(mrb, c, mid, p);
1478  return mrb_symbol_value(mid);
1479 }
1480 
1481 static void
1482 check_cv_name_sym(mrb_state *mrb, mrb_sym id)
1483 {
1484  const char *s;
1485  size_t len;
1486 
1487  s = mrb_sym2name_len(mrb, id, &len);
1488  if (len < 3 || !(s[0] == '@' && s[1] == '@')) {
1489  mrb_name_error(mrb, id, "`%S' is not allowed as a class variable name", mrb_sym2str(mrb, id));
1490  }
1491 }
1492 
1493 static void
1494 check_cv_name_str(mrb_state *mrb, mrb_value str)
1495 {
1496  const char *s = RSTRING_PTR(str);
1497  size_t const len = RSTRING_LEN(str);
1498  if (len < 3 || !(s[0] == '@' && s[1] == '@')) {
1499  mrb_name_error(mrb, mrb_intern_str(mrb, str), "`%S' is not allowed as a class variable name", str);
1500  }
1501 }
1502 
1503 static mrb_value
1504 get_sym_or_str_arg(mrb_state *mrb)
1505 {
1506  mrb_value sym_or_str;
1507 
1508  mrb_get_args(mrb, "o", &sym_or_str);
1509 
1510  if (mrb_symbol_p(sym_or_str) || mrb_string_p(sym_or_str)) {
1511  return sym_or_str;
1512  }
1513  else {
1514  mrb_value obj = mrb_funcall(mrb, sym_or_str, "inspect", 0);
1515  mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", obj);
1516  return mrb_nil_value();
1517  }
1518 }
1519 
1520 /* 15.2.2.4.16 */
1521 /*
1522  * call-seq:
1523  * obj.class_variable_defined?(symbol) -> true or false
1524  *
1525  * Returns <code>true</code> if the given class variable is defined
1526  * in <i>obj</i>.
1527  *
1528  * class Fred
1529  * @@foo = 99
1530  * end
1531  * Fred.class_variable_defined?(:@@foo) #=> true
1532  * Fred.class_variable_defined?(:@@bar) #=> false
1533  */
1534 
1535 static mrb_value
1536 mrb_mod_cvar_defined(mrb_state *mrb, mrb_value mod)
1537 {
1538  mrb_value id;
1539  mrb_bool defined_p;
1540 
1541  id = get_sym_or_str_arg(mrb);
1542  if (mrb_symbol_p(id)) {
1543  check_cv_name_sym(mrb, mrb_symbol(id));
1544  defined_p = mrb_cv_defined(mrb, mod, mrb_symbol(id));
1545  }
1546  else {
1547  mrb_value sym;
1548  check_cv_name_str(mrb, id);
1549  sym = mrb_check_intern_str(mrb, id);
1550  if (mrb_nil_p(sym)) {
1551  defined_p = FALSE;
1552  }
1553  else {
1554  defined_p = mrb_cv_defined(mrb, mod, mrb_symbol(sym));
1555  }
1556  }
1557  return mrb_bool_value(defined_p);
1558 }
1559 
1560 /* 15.2.2.4.17 */
1561 /*
1562  * call-seq:
1563  * mod.class_variable_get(symbol) -> obj
1564  *
1565  * Returns the value of the given class variable (or throws a
1566  * <code>NameError</code> exception). The <code>@@</code> part of the
1567  * variable name should be included for regular class variables
1568  *
1569  * class Fred
1570  * @@foo = 99
1571  * end
1572  * Fred.class_variable_get(:@@foo) #=> 99
1573  */
1574 
1575 static mrb_value
1576 mrb_mod_cvar_get(mrb_state *mrb, mrb_value mod)
1577 {
1578  mrb_sym id;
1579 
1580  mrb_get_args(mrb, "n", &id);
1581  check_cv_name_sym(mrb, id);
1582  return mrb_cv_get(mrb, mod, id);
1583 }
1584 
1585 /* 15.2.2.4.18 */
1586 /*
1587  * call-seq:
1588  * obj.class_variable_set(symbol, obj) -> obj
1589  *
1590  * Sets the class variable names by <i>symbol</i> to
1591  * <i>object</i>.
1592  *
1593  * class Fred
1594  * @@foo = 99
1595  * def foo
1596  * @@foo
1597  * end
1598  * end
1599  * Fred.class_variable_set(:@@foo, 101) #=> 101
1600  * Fred.new.foo #=> 101
1601  */
1602 
1603 static mrb_value
1604 mrb_mod_cvar_set(mrb_state *mrb, mrb_value mod)
1605 {
1606  mrb_value value;
1607  mrb_sym id;
1608 
1609  mrb_get_args(mrb, "no", &id, &value);
1610  check_cv_name_sym(mrb, id);
1611  mrb_cv_set(mrb, mod, id, value);
1612  return value;
1613 }
1614 
1615 /* 15.2.2.4.39 */
1616 /*
1617  * call-seq:
1618  * remove_class_variable(sym) -> obj
1619  *
1620  * Removes the definition of the <i>sym</i>, returning that
1621  * constant's value.
1622  *
1623  * class Dummy
1624  * @@var = 99
1625  * puts @@var
1626  * p class_variables
1627  * remove_class_variable(:@@var)
1628  * p class_variables
1629  * end
1630  *
1631  * <em>produces:</em>
1632  *
1633  * 99
1634  * [:@@var]
1635  * []
1636  */
1637 
1638 mrb_value
1640 {
1641  mrb_value val;
1642  mrb_sym id;
1643 
1644  mrb_get_args(mrb, "n", &id);
1645  check_cv_name_sym(mrb, id);
1646 
1647  val = mrb_iv_remove(mrb, mod, id);
1648  if (!mrb_undef_p(val)) return val;
1649 
1650  if (mrb_cv_defined(mrb, mod, id)){
1651  mrb_name_error(mrb, id, "cannot remove %S for %S",
1652  mrb_sym2str(mrb, id), mod);
1653  }
1654 
1655  mrb_name_error(mrb, id, "class variable %S not defined for %S",
1656  mrb_sym2str(mrb, id), mod);
1657 
1658  /* not reached */
1659  return mrb_nil_value();
1660 }
1661 
1662 /* 15.2.2.4.34 */
1663 /*
1664  * call-seq:
1665  * mod.method_defined?(symbol) -> true or false
1666  *
1667  * Returns +true+ if the named method is defined by
1668  * _mod_ (or its included modules and, if _mod_ is a class,
1669  * its ancestors). Public and protected methods are matched.
1670  *
1671  * module A
1672  * def method1() end
1673  * end
1674  * class B
1675  * def method2() end
1676  * end
1677  * class C < B
1678  * include A
1679  * def method3() end
1680  * end
1681  *
1682  * A.method_defined? :method1 #=> true
1683  * C.method_defined? "method1" #=> true
1684  * C.method_defined? "method2" #=> true
1685  * C.method_defined? "method3" #=> true
1686  * C.method_defined? "method4" #=> false
1687  */
1688 
1689 static mrb_value
1690 mrb_mod_method_defined(mrb_state *mrb, mrb_value mod)
1691 {
1692  mrb_value id;
1693  mrb_bool method_defined_p;
1694 
1695  id = get_sym_or_str_arg(mrb);
1696  if (mrb_symbol_p(id)) {
1697  method_defined_p = mrb_obj_respond_to(mrb_class_ptr(mod), mrb_symbol(id));
1698  }
1699  else {
1700  mrb_value sym = mrb_check_intern_str(mrb, id);
1701  if (mrb_nil_p(sym)) {
1702  method_defined_p = FALSE;
1703  }
1704  else {
1705  method_defined_p = mrb_obj_respond_to(mrb_class_ptr(mod), mrb_symbol(sym));
1706  }
1707  }
1708  return mrb_bool_value(method_defined_p);
1709 }
1710 
1711 static void
1712 remove_method(mrb_state *mrb, mrb_value mod, mrb_sym mid)
1713 {
1714  struct RClass *c = mrb_class_ptr(mod);
1715  khash_t(mt) *h = c->mt;
1716  khiter_t k;
1717 
1718  if (h) {
1719  k = kh_get(mt, h, mid);
1720  if (k != kh_end(h)) {
1721  kh_del(mt, h, k);
1722  return;
1723  }
1724  }
1725 
1726  mrb_name_error(mrb, mid, "method `%S' not defined in %S",
1727  mrb_sym2str(mrb, mid), mod);
1728 }
1729 
1730 /* 15.2.2.4.41 */
1731 /*
1732  * call-seq:
1733  * remove_method(symbol) -> self
1734  *
1735  * Removes the method identified by _symbol_ from the current
1736  * class. For an example, see <code>Module.undef_method</code>.
1737  */
1738 
1739 mrb_value
1741 {
1742  int argc;
1743  mrb_value *argv;
1744 
1745  mrb_get_args(mrb, "*", &argv, &argc);
1746  while (argc--) {
1747  remove_method(mrb, mod, mrb_symbol(*argv));
1748  argv++;
1749  }
1750  return mod;
1751 }
1752 
1753 static void
1754 check_const_name_sym(mrb_state *mrb, mrb_sym id)
1755 {
1756  const char *s;
1757  size_t len;
1758 
1759  s = mrb_sym2name_len(mrb, id, &len);
1760  if (len < 1 || !ISUPPER(*s)) {
1761  mrb_name_error(mrb, id, "wrong constant name %S", mrb_sym2str(mrb, id));
1762  }
1763 }
1764 
1765 static void
1766 check_const_name_str(mrb_state *mrb, mrb_value str)
1767 {
1768  if (RSTRING_LEN(str) < 1 || !ISUPPER(*RSTRING_PTR(str))) {
1769  mrb_name_error(mrb, mrb_intern_str(mrb, str), "wrong constant name %S", str);
1770  }
1771 }
1772 
1773 mrb_value
1775 {
1776  mrb_value id;
1777  mrb_bool const_defined_p;
1778 
1779  id = get_sym_or_str_arg(mrb);
1780  if (mrb_type(id) == MRB_TT_SYMBOL) {
1781  check_const_name_sym(mrb, mrb_symbol(id));
1782  const_defined_p = mrb_const_defined(mrb, mod, mrb_symbol(id));
1783  }
1784  else {
1785  mrb_value sym;
1786  check_const_name_str(mrb, id);
1787  sym = mrb_check_intern_str(mrb, id);
1788  if (mrb_nil_p(sym)) {
1789  const_defined_p = FALSE;
1790  }
1791  else {
1792  const_defined_p = mrb_const_defined(mrb, mod, mrb_symbol(sym));
1793  }
1794  }
1795 
1796  return mrb_bool_value(const_defined_p);
1797 }
1798 
1799 mrb_value
1801 {
1802  mrb_sym id;
1803 
1804  mrb_get_args(mrb, "n", &id);
1805  check_const_name_sym(mrb, id);
1806  return mrb_const_get(mrb, mod, id);
1807 }
1808 
1809 mrb_value
1811 {
1812  mrb_sym id;
1813  mrb_value value;
1814 
1815  mrb_get_args(mrb, "no", &id, &value);
1816  check_const_name_sym(mrb, id);
1817  mrb_const_set(mrb, mod, id, value);
1818  return value;
1819 }
1820 
1821 mrb_value
1823 {
1824  mrb_sym id;
1825  mrb_value val;
1826 
1827  mrb_get_args(mrb, "n", &id);
1828  check_const_name_sym(mrb, id);
1829  val = mrb_iv_remove(mrb, mod, id);
1830  if (mrb_undef_p(val)) {
1831  mrb_name_error(mrb, id, "constant %S not defined", mrb_sym2str(mrb, id));
1832  }
1833  return val;
1834 }
1835 
1836 static mrb_value
1837 mrb_mod_s_constants(mrb_state *mrb, mrb_value mod)
1838 {
1839  mrb_raise(mrb, E_NOTIMP_ERROR, "Module.constants not implemented");
1840  return mrb_nil_value(); /* not reached */
1841 }
1842 
1843 static mrb_value
1844 mrb_mod_eqq(mrb_state *mrb, mrb_value mod)
1845 {
1846  mrb_value obj;
1847  mrb_bool eqq;
1848 
1849  mrb_get_args(mrb, "o", &obj);
1850  eqq = mrb_obj_is_kind_of(mrb, obj, mrb_class_ptr(mod));
1851 
1852  return mrb_bool_value(eqq);
1853 }
1854 
1855 void
1857 {
1858  struct RClass *bob; /* BasicObject */
1859  struct RClass *obj; /* Object */
1860  struct RClass *mod; /* Module */
1861  struct RClass *cls; /* Class */
1862  //struct RClass *krn; /* Kernel */
1863 
1864  /* boot class hierarchy */
1865  bob = boot_defclass(mrb, 0);
1866  obj = boot_defclass(mrb, bob); mrb->object_class = obj;
1867  mod = boot_defclass(mrb, obj); mrb->module_class = mod;/* obj -> mod */
1868  cls = boot_defclass(mrb, mod); mrb->class_class = cls; /* obj -> cls */
1869  /* fix-up loose ends */
1870  bob->c = obj->c = mod->c = cls->c = cls;
1871  make_metaclass(mrb, bob);
1872  make_metaclass(mrb, obj);
1873  make_metaclass(mrb, mod);
1874  make_metaclass(mrb, cls);
1875 
1876  /* name basic classes */
1877  mrb_define_const(mrb, bob, "BasicObject", mrb_obj_value(bob));
1878  mrb_define_const(mrb, obj, "BasicObject", mrb_obj_value(bob));
1879  mrb_define_const(mrb, obj, "Object", mrb_obj_value(obj));
1880  mrb_define_const(mrb, obj, "Module", mrb_obj_value(mod));
1881  mrb_define_const(mrb, obj, "Class", mrb_obj_value(cls));
1882 
1883  /* name each classes */
1884  mrb_name_class(mrb, bob, mrb_intern2(mrb, "BasicObject", 11));
1885  mrb_name_class(mrb, obj, mrb_intern2(mrb, "Object", 6));
1886  mrb_name_class(mrb, mod, mrb_intern2(mrb, "Module", 6));
1887  mrb_name_class(mrb, cls, mrb_intern2(mrb, "Class", 5));
1888 
1890  mrb_define_method(mrb, bob, "initialize", mrb_bob_init, MRB_ARGS_NONE());
1891  mrb_define_method(mrb, bob, "!", mrb_bob_not, MRB_ARGS_NONE());
1892  mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, MRB_ARGS_ANY()); /* 15.3.1.3.30 */
1893 
1894  mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, MRB_ARGS_ANY());
1895  mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, MRB_ARGS_NONE()); /* 15.2.3.3.4 */
1896  mrb_define_method(mrb, cls, "new", mrb_instance_new, MRB_ARGS_ANY()); /* 15.2.3.3.3 */
1897  mrb_define_method(mrb, cls, "inherited", mrb_bob_init, MRB_ARGS_REQ(1));
1898 
1900  mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, MRB_ARGS_REQ(1)); /* 15.2.2.4.16 */
1901  mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, MRB_ARGS_REQ(1)); /* 15.2.2.4.17 */
1902  mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, MRB_ARGS_REQ(2)); /* 15.2.2.4.18 */
1903  mrb_define_method(mrb, mod, "extend_object", mrb_mod_extend_object, MRB_ARGS_REQ(1)); /* 15.2.2.4.25 */
1904  mrb_define_method(mrb, mod, "extended", mrb_bob_init, MRB_ARGS_REQ(1)); /* 15.2.2.4.26 */
1905  mrb_define_method(mrb, mod, "include", mrb_mod_include, MRB_ARGS_ANY()); /* 15.2.2.4.27 */
1906  mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, MRB_ARGS_REQ(1)); /* 15.2.2.4.28 */
1907  mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, MRB_ARGS_REQ(1)); /* 15.2.2.4.10 */
1908  mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, MRB_ARGS_ANY()); /* 15.2.2.4.15 */
1909  mrb_define_method(mrb, mod, "included", mrb_bob_init, MRB_ARGS_REQ(1)); /* 15.2.2.4.29 */
1910  mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, MRB_ARGS_NONE()); /* 15.2.2.4.30 */
1911  mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, MRB_ARGS_ANY()); /* 15.2.2.4.33 */
1912  mrb_define_method(mrb, mod, "method_defined?", mrb_mod_method_defined, MRB_ARGS_REQ(1)); /* 15.2.2.4.34 */
1913  mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, MRB_ARGS_ANY()); /* 15.2.2.4.35 */
1914  mrb_define_method(mrb, mod, "private", mrb_mod_dummy_visibility, MRB_ARGS_ANY()); /* 15.2.2.4.36 */
1915  mrb_define_method(mrb, mod, "protected", mrb_mod_dummy_visibility, MRB_ARGS_ANY()); /* 15.2.2.4.37 */
1916  mrb_define_method(mrb, mod, "public", mrb_mod_dummy_visibility, MRB_ARGS_ANY()); /* 15.2.2.4.38 */
1917  mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, MRB_ARGS_REQ(1)); /* 15.2.2.4.39 */
1918  mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, MRB_ARGS_ANY()); /* 15.2.2.4.41 */
1919  mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, MRB_ARGS_NONE());
1920  mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, MRB_ARGS_NONE());
1921  mrb_define_method(mrb, mod, "alias_method", mrb_mod_alias, MRB_ARGS_ANY()); /* 15.2.2.4.8 */
1922  mrb_define_method(mrb, mod, "ancestors", mrb_mod_ancestors, MRB_ARGS_NONE()); /* 15.2.2.4.9 */
1923  mrb_define_method(mrb, mod, "undef_method", mrb_mod_undef, MRB_ARGS_ANY()); /* 15.2.2.4.41 */
1924  mrb_define_method(mrb, mod, "const_defined?", mrb_mod_const_defined, MRB_ARGS_REQ(1)); /* 15.2.2.4.20 */
1925  mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, MRB_ARGS_REQ(1)); /* 15.2.2.4.21 */
1926  mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, MRB_ARGS_REQ(2)); /* 15.2.2.4.23 */
1927  mrb_define_method(mrb, mod, "constants", mrb_mod_constants, MRB_ARGS_NONE()); /* 15.2.2.4.24 */
1928  mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, MRB_ARGS_REQ(1)); /* 15.2.2.4.40 */
1929  mrb_define_method(mrb, mod, "define_method", mod_define_method, MRB_ARGS_REQ(1));
1930  mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, MRB_ARGS_NONE()); /* 15.2.2.4.19 */
1931  mrb_define_method(mrb, mod, "===", mrb_mod_eqq, MRB_ARGS_REQ(1));
1932  mrb_define_class_method(mrb, mod, "constants", mrb_mod_s_constants, MRB_ARGS_ANY()); /* 15.2.2.3.1 */
1933 
1934  mrb_undef_method(mrb, cls, "append_features");
1935  mrb_undef_method(mrb, cls, "extend_object");
1936 }