Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / src / kernel.c
1 /*
2 ** kernel.c - Kernel module
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/array.h>
9 #include <mruby/hash.h>
10 #include <mruby/class.h>
11 #include <mruby/proc.h>
12 #include <mruby/string.h>
13 #include <mruby/variable.h>
14 #include <mruby/error.h>
15 #include <mruby/istruct.h>
16
17 MRB_API mrb_bool
18 mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func)
19 {
20   struct RClass *c = mrb_class(mrb, obj);
21   mrb_method_t m = mrb_method_search_vm(mrb, &c, mid);
22   struct RProc *p;
23
24   if (MRB_METHOD_UNDEF_P(m)) return FALSE;
25   if (MRB_METHOD_FUNC_P(m))
26     return MRB_METHOD_FUNC(m) == func;
27   p = MRB_METHOD_PROC(m);
28   if (MRB_PROC_CFUNC_P(p) && (MRB_PROC_CFUNC(p) == func))
29     return TRUE;
30   return FALSE;
31 }
32
33 static mrb_bool
34 mrb_obj_basic_to_s_p(mrb_state *mrb, mrb_value obj)
35 {
36   return mrb_func_basic_p(mrb, obj, mrb_intern_lit(mrb, "to_s"), mrb_any_to_s);
37 }
38
39 /* 15.3.1.3.17 */
40 /*
41  *  call-seq:
42  *     obj.inspect   -> string
43  *
44  *  Returns a string containing a human-readable representation of
45  *  <i>obj</i>. If not overridden and no instance variables, uses the
46  *  <code>to_s</code> method to generate the string.
47  *  <i>obj</i>.  If not overridden, uses the <code>to_s</code> method to
48  *  generate the string.
49  *
50  *     [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
51  *     Time.new.inspect                 #=> "2008-03-08 19:43:39 +0900"
52  */
53 MRB_API mrb_value
54 mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
55 {
56   if ((mrb_type(obj) == MRB_TT_OBJECT) && mrb_obj_basic_to_s_p(mrb, obj)) {
57     return mrb_obj_iv_inspect(mrb, mrb_obj_ptr(obj));
58   }
59   return mrb_any_to_s(mrb, obj);
60 }
61
62 /* 15.3.1.3.2  */
63 /*
64  *  call-seq:
65  *     obj === other   -> true or false
66  *
67  *  Case Equality---For class <code>Object</code>, effectively the same
68  *  as calling  <code>#==</code>, but typically overridden by descendants
69  *  to provide meaningful semantics in <code>case</code> statements.
70  */
71 static mrb_value
72 mrb_equal_m(mrb_state *mrb, mrb_value self)
73 {
74   mrb_value arg;
75
76   mrb_get_args(mrb, "o", &arg);
77   return mrb_bool_value(mrb_equal(mrb, self, arg));
78 }
79
80 /* 15.3.1.3.3  */
81 /* 15.3.1.3.33 */
82 /*
83  *  Document-method: __id__
84  *  Document-method: object_id
85  *
86  *  call-seq:
87  *     obj.__id__       -> fixnum
88  *     obj.object_id    -> fixnum
89  *
90  *  Returns an integer identifier for <i>obj</i>. The same number will
91  *  be returned on all calls to <code>id</code> for a given object, and
92  *  no two active objects will share an id.
93  *  <code>Object#object_id</code> is a different concept from the
94  *  <code>:name</code> notation, which returns the symbol id of
95  *  <code>name</code>. Replaces the deprecated <code>Object#id</code>.
96  */
97 mrb_value
98 mrb_obj_id_m(mrb_state *mrb, mrb_value self)
99 {
100   return mrb_fixnum_value(mrb_obj_id(self));
101 }
102
103 /* 15.3.1.2.2  */
104 /* 15.3.1.2.5  */
105 /* 15.3.1.3.6  */
106 /* 15.3.1.3.25 */
107 /*
108  *  call-seq:
109  *     block_given?   -> true or false
110  *     iterator?      -> true or false
111  *
112  *  Returns <code>true</code> if <code>yield</code> would execute a
113  *  block in the current context. The <code>iterator?</code> form
114  *  is mildly deprecated.
115  *
116  *     def try
117  *       if block_given?
118  *         yield
119  *       else
120  *         "no block"
121  *       end
122  *     end
123  *     try                  #=> "no block"
124  *     try { "hello" }      #=> "hello"
125  *     try do "hello" end   #=> "hello"
126  */
127 static mrb_value
128 mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
129 {
130   mrb_callinfo *ci = &mrb->c->ci[-1];
131   mrb_callinfo *cibase = mrb->c->cibase;
132   mrb_value *bp;
133   struct RProc *p;
134
135   if (ci <= cibase) {
136     /* toplevel does not have block */
137     return mrb_false_value();
138   }
139   p = ci->proc;
140   /* search method/class/module proc */
141   while (p) {
142     if (MRB_PROC_SCOPE_P(p)) break;
143     p = p->upper;
144   }
145   if (p == NULL) return mrb_false_value();
146   /* search ci corresponding to proc */
147   while (cibase < ci) {
148     if (ci->proc == p) break;
149     ci--;
150   }
151   if (ci == cibase) {
152     return mrb_false_value();
153   }
154   else if (ci->env) {
155     struct REnv *e = ci->env;
156     int bidx;
157
158     /* top-level does not have block slot (always false) */
159     if (e->stack == mrb->c->stbase)
160       return mrb_false_value();
161     /* use saved block arg position */
162     bidx = MRB_ENV_BIDX(e);
163     /* bidx may be useless (e.g. define_method) */
164     if (bidx >= MRB_ENV_STACK_LEN(e))
165       return mrb_false_value();
166     bp = &e->stack[bidx];
167   }
168   else {
169     bp = ci[1].stackent+1;
170     if (ci->argc >= 0) {
171       bp += ci->argc;
172     }
173     else {
174       bp++;
175     }
176   }
177   if (mrb_nil_p(*bp))
178     return mrb_false_value();
179   return mrb_true_value();
180 }
181
182 /* 15.3.1.3.7  */
183 /*
184  *  call-seq:
185  *     obj.class    -> class
186  *
187  *  Returns the class of <i>obj</i>. This method must always be
188  *  called with an explicit receiver, as <code>class</code> is also a
189  *  reserved word in Ruby.
190  *
191  *     1.class      #=> Fixnum
192  *     self.class   #=> Object
193  */
194 static mrb_value
195 mrb_obj_class_m(mrb_state *mrb, mrb_value self)
196 {
197   return mrb_obj_value(mrb_obj_class(mrb, self));
198 }
199
200 static struct RClass*
201 mrb_singleton_class_clone(mrb_state *mrb, mrb_value obj)
202 {
203   struct RClass *klass = mrb_basic_ptr(obj)->c;
204
205   if (klass->tt != MRB_TT_SCLASS)
206     return klass;
207   else {
208     /* copy singleton(unnamed) class */
209     struct RClass *clone = (struct RClass*)mrb_obj_alloc(mrb, klass->tt, mrb->class_class);
210
211     switch (mrb_type(obj)) {
212     case MRB_TT_CLASS:
213     case MRB_TT_SCLASS:
214       break;
215     default:
216       clone->c = mrb_singleton_class_clone(mrb, mrb_obj_value(klass));
217       break;
218     }
219     clone->super = klass->super;
220     if (klass->iv) {
221       mrb_iv_copy(mrb, mrb_obj_value(clone), mrb_obj_value(klass));
222       mrb_obj_iv_set(mrb, (struct RObject*)clone, mrb_intern_lit(mrb, "__attached__"), obj);
223     }
224     if (klass->mt) {
225       clone->mt = kh_copy(mt, mrb, klass->mt);
226     }
227     else {
228       clone->mt = kh_init(mt, mrb);
229     }
230     clone->tt = MRB_TT_SCLASS;
231     return clone;
232   }
233 }
234
235 static void
236 copy_class(mrb_state *mrb, mrb_value dst, mrb_value src)
237 {
238   struct RClass *dc = mrb_class_ptr(dst);
239   struct RClass *sc = mrb_class_ptr(src);
240   /* if the origin is not the same as the class, then the origin and
241      the current class need to be copied */
242   if (sc->flags & MRB_FL_CLASS_IS_PREPENDED) {
243     struct RClass *c0 = sc->super;
244     struct RClass *c1 = dc;
245
246     /* copy prepended iclasses */
247     while (!(c0->flags & MRB_FL_CLASS_IS_ORIGIN)) {
248       c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0)));
249       c1 = c1->super;
250       c0 = c0->super;
251     }
252     c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0)));
253     c1->super->flags |= MRB_FL_CLASS_IS_ORIGIN;
254   }
255   if (sc->mt) {
256     dc->mt = kh_copy(mt, mrb, sc->mt);
257   }
258   else {
259     dc->mt = kh_init(mt, mrb);
260   }
261   dc->super = sc->super;
262   MRB_SET_INSTANCE_TT(dc, MRB_INSTANCE_TT(sc));
263 }
264
265 static void
266 init_copy(mrb_state *mrb, mrb_value dest, mrb_value obj)
267 {
268   switch (mrb_type(obj)) {
269     case MRB_TT_ICLASS:
270       copy_class(mrb, dest, obj);
271       return;
272     case MRB_TT_CLASS:
273     case MRB_TT_MODULE:
274       copy_class(mrb, dest, obj);
275       mrb_iv_copy(mrb, dest, obj);
276       mrb_iv_remove(mrb, dest, mrb_intern_lit(mrb, "__classname__"));
277       break;
278     case MRB_TT_OBJECT:
279     case MRB_TT_SCLASS:
280     case MRB_TT_HASH:
281     case MRB_TT_DATA:
282     case MRB_TT_EXCEPTION:
283       mrb_iv_copy(mrb, dest, obj);
284       break;
285     case MRB_TT_ISTRUCT:
286       mrb_istruct_copy(dest, obj);
287       break;
288
289     default:
290       break;
291   }
292   mrb_funcall(mrb, dest, "initialize_copy", 1, obj);
293 }
294
295 /* 15.3.1.3.8  */
296 /*
297  *  call-seq:
298  *     obj.clone -> an_object
299  *
300  *  Produces a shallow copy of <i>obj</i>---the instance variables of
301  *  <i>obj</i> are copied, but not the objects they reference. Copies
302  *  the frozen state of <i>obj</i>. See also the discussion
303  *  under <code>Object#dup</code>.
304  *
305  *     class Klass
306  *        attr_accessor :str
307  *     end
308  *     s1 = Klass.new      #=> #<Klass:0x401b3a38>
309  *     s1.str = "Hello"    #=> "Hello"
310  *     s2 = s1.clone       #=> #<Klass:0x401b3998 @str="Hello">
311  *     s2.str[1,4] = "i"   #=> "i"
312  *     s1.inspect          #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
313  *     s2.inspect          #=> "#<Klass:0x401b3998 @str=\"Hi\">"
314  *
315  *  This method may have class-specific behavior.  If so, that
316  *  behavior will be documented under the #+initialize_copy+ method of
317  *  the class.
318  *
319  *  Some Class(True False Nil Symbol Fixnum Float) Object  cannot clone.
320  */
321 MRB_API mrb_value
322 mrb_obj_clone(mrb_state *mrb, mrb_value self)
323 {
324   struct RObject *p;
325   mrb_value clone;
326
327   if (mrb_immediate_p(self)) {
328     mrb_raisef(mrb, E_TYPE_ERROR, "can't clone %S", self);
329   }
330   if (mrb_type(self) == MRB_TT_SCLASS) {
331     mrb_raise(mrb, E_TYPE_ERROR, "can't clone singleton class");
332   }
333   p = (struct RObject*)mrb_obj_alloc(mrb, mrb_type(self), mrb_obj_class(mrb, self));
334   p->c = mrb_singleton_class_clone(mrb, self);
335   mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)p->c);
336   clone = mrb_obj_value(p);
337   init_copy(mrb, clone, self);
338   p->flags |= mrb_obj_ptr(self)->flags & MRB_FL_OBJ_IS_FROZEN;
339
340   return clone;
341 }
342
343 /* 15.3.1.3.9  */
344 /*
345  *  call-seq:
346  *     obj.dup -> an_object
347  *
348  *  Produces a shallow copy of <i>obj</i>---the instance variables of
349  *  <i>obj</i> are copied, but not the objects they reference.
350  *  <code>dup</code> copies the frozen state of <i>obj</i>. See also
351  *  the discussion under <code>Object#clone</code>. In general,
352  *  <code>clone</code> and <code>dup</code> may have different semantics
353  *  in descendant classes. While <code>clone</code> is used to duplicate
354  *  an object, including its internal state, <code>dup</code> typically
355  *  uses the class of the descendant object to create the new instance.
356  *
357  *  This method may have class-specific behavior.  If so, that
358  *  behavior will be documented under the #+initialize_copy+ method of
359  *  the class.
360  */
361
362 MRB_API mrb_value
363 mrb_obj_dup(mrb_state *mrb, mrb_value obj)
364 {
365   struct RBasic *p;
366   mrb_value dup;
367
368   if (mrb_immediate_p(obj)) {
369     mrb_raisef(mrb, E_TYPE_ERROR, "can't dup %S", obj);
370   }
371   if (mrb_type(obj) == MRB_TT_SCLASS) {
372     mrb_raise(mrb, E_TYPE_ERROR, "can't dup singleton class");
373   }
374   p = mrb_obj_alloc(mrb, mrb_type(obj), mrb_obj_class(mrb, obj));
375   dup = mrb_obj_value(p);
376   init_copy(mrb, dup, obj);
377
378   return dup;
379 }
380
381 static mrb_value
382 mrb_obj_extend(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb_value obj)
383 {
384   mrb_int i;
385
386   if (argc == 0) {
387     mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (at least 1)");
388   }
389   for (i = 0; i < argc; i++) {
390     mrb_check_type(mrb, argv[i], MRB_TT_MODULE);
391   }
392   while (argc--) {
393     mrb_funcall(mrb, argv[argc], "extend_object", 1, obj);
394     mrb_funcall(mrb, argv[argc], "extended", 1, obj);
395   }
396   return obj;
397 }
398
399 /* 15.3.1.3.13 */
400 /*
401  *  call-seq:
402  *     obj.extend(module, ...)    -> obj
403  *
404  *  Adds to _obj_ the instance methods from each module given as a
405  *  parameter.
406  *
407  *     module Mod
408  *       def hello
409  *         "Hello from Mod.\n"
410  *       end
411  *     end
412  *
413  *     class Klass
414  *       def hello
415  *         "Hello from Klass.\n"
416  *       end
417  *     end
418  *
419  *     k = Klass.new
420  *     k.hello         #=> "Hello from Klass.\n"
421  *     k.extend(Mod)   #=> #<Klass:0x401b3bc8>
422  *     k.hello         #=> "Hello from Mod.\n"
423  */
424 static mrb_value
425 mrb_obj_extend_m(mrb_state *mrb, mrb_value self)
426 {
427   mrb_value *argv;
428   mrb_int argc;
429
430   mrb_get_args(mrb, "*", &argv, &argc);
431   return mrb_obj_extend(mrb, argc, argv, self);
432 }
433
434 static mrb_value
435 mrb_obj_freeze(mrb_state *mrb, mrb_value self)
436 {
437   struct RBasic *b;
438
439   switch (mrb_type(self)) {
440     case MRB_TT_FALSE:
441     case MRB_TT_TRUE:
442     case MRB_TT_FIXNUM:
443     case MRB_TT_SYMBOL:
444 #ifndef MRB_WITHOUT_FLOAT
445     case MRB_TT_FLOAT:
446 #endif
447       return self;
448     default:
449       break;
450   }
451
452   b = mrb_basic_ptr(self);
453   if (!MRB_FROZEN_P(b)) {
454     MRB_SET_FROZEN_FLAG(b);
455   }
456   return self;
457 }
458
459 static mrb_value
460 mrb_obj_frozen(mrb_state *mrb, mrb_value self)
461 {
462   struct RBasic *b;
463
464   switch (mrb_type(self)) {
465     case MRB_TT_FALSE:
466     case MRB_TT_TRUE:
467     case MRB_TT_FIXNUM:
468     case MRB_TT_SYMBOL:
469 #ifndef MRB_WITHOUT_FLOAT
470     case MRB_TT_FLOAT:
471 #endif
472       return mrb_true_value();
473     default:
474       break;
475   }
476
477   b = mrb_basic_ptr(self);
478   if (!MRB_FROZEN_P(b)) {
479     return mrb_false_value();
480   }
481   return mrb_true_value();
482 }
483
484 /* 15.3.1.3.15 */
485 /*
486  *  call-seq:
487  *     obj.hash    -> fixnum
488  *
489  *  Generates a <code>Fixnum</code> hash value for this object. This
490  *  function must have the property that <code>a.eql?(b)</code> implies
491  *  <code>a.hash == b.hash</code>. The hash value is used by class
492  *  <code>Hash</code>. Any hash value that exceeds the capacity of a
493  *  <code>Fixnum</code> will be truncated before being used.
494  */
495 static mrb_value
496 mrb_obj_hash(mrb_state *mrb, mrb_value self)
497 {
498   return mrb_fixnum_value(mrb_obj_id(self));
499 }
500
501 /* 15.3.1.3.16 */
502 static mrb_value
503 mrb_obj_init_copy(mrb_state *mrb, mrb_value self)
504 {
505   mrb_value orig;
506
507   mrb_get_args(mrb, "o", &orig);
508   if (mrb_obj_equal(mrb, self, orig)) return self;
509   if ((mrb_type(self) != mrb_type(orig)) || (mrb_obj_class(mrb, self) != mrb_obj_class(mrb, orig))) {
510       mrb_raise(mrb, E_TYPE_ERROR, "initialize_copy should take same class object");
511   }
512   return self;
513 }
514
515
516 MRB_API mrb_bool
517 mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c)
518 {
519   if (mrb_obj_class(mrb, obj) == c) return TRUE;
520   return FALSE;
521 }
522
523 /* 15.3.1.3.19 */
524 /*
525  *  call-seq:
526  *     obj.instance_of?(class)    -> true or false
527  *
528  *  Returns <code>true</code> if <i>obj</i> is an instance of the given
529  *  class. See also <code>Object#kind_of?</code>.
530  */
531 static mrb_value
532 obj_is_instance_of(mrb_state *mrb, mrb_value self)
533 {
534   mrb_value arg;
535
536   mrb_get_args(mrb, "C", &arg);
537
538   return mrb_bool_value(mrb_obj_is_instance_of(mrb, self, mrb_class_ptr(arg)));
539 }
540
541 /* 15.3.1.3.24 */
542 /* 15.3.1.3.26 */
543 /*
544  *  call-seq:
545  *     obj.is_a?(class)       -> true or false
546  *     obj.kind_of?(class)    -> true or false
547  *
548  *  Returns <code>true</code> if <i>class</i> is the class of
549  *  <i>obj</i>, or if <i>class</i> is one of the superclasses of
550  *  <i>obj</i> or modules included in <i>obj</i>.
551  *
552  *     module M;    end
553  *     class A
554  *       include M
555  *     end
556  *     class B < A; end
557  *     class C < B; end
558  *     b = B.new
559  *     b.instance_of? A   #=> false
560  *     b.instance_of? B   #=> true
561  *     b.instance_of? C   #=> false
562  *     b.instance_of? M   #=> false
563  *     b.kind_of? A       #=> true
564  *     b.kind_of? B       #=> true
565  *     b.kind_of? C       #=> false
566  *     b.kind_of? M       #=> true
567  */
568 static mrb_value
569 mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self)
570 {
571   mrb_value arg;
572
573   mrb_get_args(mrb, "C", &arg);
574
575   return mrb_bool_value(mrb_obj_is_kind_of(mrb, self, mrb_class_ptr(arg)));
576 }
577
578 KHASH_DECLARE(st, mrb_sym, char, FALSE)
579 KHASH_DEFINE(st, mrb_sym, char, FALSE, kh_int_hash_func, kh_int_hash_equal)
580
581 /* 15.3.1.3.32 */
582 /*
583  * call_seq:
584  *   nil.nil?               -> true
585  *   <anything_else>.nil?   -> false
586  *
587  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
588  */
589 static mrb_value
590 mrb_false(mrb_state *mrb, mrb_value self)
591 {
592   return mrb_false_value();
593 }
594
595 /* 15.3.1.2.12  */
596 /* 15.3.1.3.40 */
597 /*
598  *  call-seq:
599  *     raise
600  *     raise(string)
601  *     raise(exception [, string])
602  *
603  *  With no arguments, raises a <code>RuntimeError</code>
604  *  With a single +String+ argument, raises a
605  *  +RuntimeError+ with the string as a message. Otherwise,
606  *  the first parameter should be the name of an +Exception+
607  *  class (or an object that returns an +Exception+ object when sent
608  *  an +exception+ message). The optional second parameter sets the
609  *  message associated with the exception, and the third parameter is an
610  *  array of callback information. Exceptions are caught by the
611  *  +rescue+ clause of <code>begin...end</code> blocks.
612  *
613  *     raise "Failed to create socket"
614  *     raise ArgumentError, "No parameters", caller
615  */
616 MRB_API mrb_value
617 mrb_f_raise(mrb_state *mrb, mrb_value self)
618 {
619   mrb_value a[2], exc;
620   mrb_int argc;
621
622
623   argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]);
624   switch (argc) {
625   case 0:
626     mrb_raise(mrb, E_RUNTIME_ERROR, "");
627     break;
628   case 1:
629     if (mrb_string_p(a[0])) {
630       a[1] = a[0];
631       argc = 2;
632       a[0] = mrb_obj_value(E_RUNTIME_ERROR);
633     }
634     /* fall through */
635   default:
636     exc = mrb_make_exception(mrb, argc, a);
637     mrb_exc_raise(mrb, exc);
638     break;
639   }
640   return mrb_nil_value();            /* not reached */
641 }
642
643 /* 15.3.1.3.41 */
644 /*
645  *  call-seq:
646  *     obj.remove_instance_variable(symbol)    -> obj
647  *
648  *  Removes the named instance variable from <i>obj</i>, returning that
649  *  variable's value.
650  *
651  *     class Dummy
652  *       attr_reader :var
653  *       def initialize
654  *         @var = 99
655  *       end
656  *       def remove
657  *         remove_instance_variable(:@var)
658  *       end
659  *     end
660  *     d = Dummy.new
661  *     d.var      #=> 99
662  *     d.remove   #=> 99
663  *     d.var      #=> nil
664  */
665 static mrb_value
666 mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self)
667 {
668   mrb_sym sym;
669   mrb_value val;
670
671   mrb_get_args(mrb, "n", &sym);
672   mrb_iv_name_sym_check(mrb, sym);
673   val = mrb_iv_remove(mrb, self, sym);
674   if (mrb_undef_p(val)) {
675     mrb_name_error(mrb, sym, "instance variable %S not defined", mrb_sym2str(mrb, sym));
676   }
677   return val;
678 }
679
680 void
681 mrb_method_missing(mrb_state *mrb, mrb_sym name, mrb_value self, mrb_value args)
682 {
683   mrb_no_method_error(mrb, name, args, "undefined method '%S'", mrb_sym2str(mrb, name));
684 }
685
686 /* 15.3.1.3.30 */
687 /*
688  *  call-seq:
689  *     obj.method_missing(symbol [, *args] )   -> result
690  *
691  *  Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
692  *  <i>symbol</i> is the symbol for the method called, and <i>args</i>
693  *  are any arguments that were passed to it. By default, the interpreter
694  *  raises an error when this method is called. However, it is possible
695  *  to override the method to provide more dynamic behavior.
696  *  If it is decided that a particular method should not be handled, then
697  *  <i>super</i> should be called, so that ancestors can pick up the
698  *  missing method.
699  *  The example below creates
700  *  a class <code>Roman</code>, which responds to methods with names
701  *  consisting of roman numerals, returning the corresponding integer
702  *  values.
703  *
704  *     class Roman
705  *       def romanToInt(str)
706  *         # ...
707  *       end
708  *       def method_missing(methId)
709  *         str = methId.id2name
710  *         romanToInt(str)
711  *       end
712  *     end
713  *
714  *     r = Roman.new
715  *     r.iv      #=> 4
716  *     r.xxiii   #=> 23
717  *     r.mm      #=> 2000
718  */
719 #ifdef MRB_DEFAULT_METHOD_MISSING
720 static mrb_value
721 mrb_obj_missing(mrb_state *mrb, mrb_value mod)
722 {
723   mrb_sym name;
724   mrb_value *a;
725   mrb_int alen;
726
727   mrb_get_args(mrb, "n*!", &name, &a, &alen);
728   mrb_method_missing(mrb, name, mod, mrb_ary_new_from_values(mrb, alen, a));
729   /* not reached */
730   return mrb_nil_value();
731 }
732 #endif
733
734 static inline mrb_bool
735 basic_obj_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym id, int pub)
736 {
737   return mrb_respond_to(mrb, obj, id);
738 }
739
740 /* 15.3.1.3.43 */
741 /*
742  *  call-seq:
743  *     obj.respond_to?(symbol, include_private=false) -> true or false
744  *
745  *  Returns +true+ if _obj_ responds to the given
746  *  method. Private methods are included in the search only if the
747  *  optional second parameter evaluates to +true+.
748  *
749  *  If the method is not implemented,
750  *  as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
751  *  false is returned.
752  *
753  *  If the method is not defined, <code>respond_to_missing?</code>
754  *  method is called and the result is returned.
755  */
756 static mrb_value
757 obj_respond_to(mrb_state *mrb, mrb_value self)
758 {
759   mrb_sym id, rtm_id;
760   mrb_bool priv = FALSE, respond_to_p;
761
762   mrb_get_args(mrb, "n|b", &id, &priv);
763   respond_to_p = basic_obj_respond_to(mrb, self, id, !priv);
764   if (!respond_to_p) {
765     rtm_id = mrb_intern_lit(mrb, "respond_to_missing?");
766     if (basic_obj_respond_to(mrb, self, rtm_id, !priv)) {
767       mrb_value args[2], v;
768       args[0] = mrb_symbol_value(id);
769       args[1] = mrb_bool_value(priv);
770       v = mrb_funcall_argv(mrb, self, rtm_id, 2, args);
771       return mrb_bool_value(mrb_bool(v));
772     }
773   }
774   return mrb_bool_value(respond_to_p);
775 }
776
777 static mrb_value
778 mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
779 {
780   mrb_value v;
781   mrb_int i, len;
782   mrb_sym eqq = mrb_intern_lit(mrb, "===");
783   mrb_value ary = mrb_ary_splat(mrb, self);
784
785   mrb_get_args(mrb, "o", &v);
786   len = RARRAY_LEN(ary);
787   for (i=0; i<len; i++) {
788     mrb_value c = mrb_funcall_argv(mrb, mrb_ary_entry(ary, i), eqq, 1, &v);
789     if (mrb_test(c)) return mrb_true_value();
790   }
791   return mrb_false_value();
792 }
793
794 mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
795
796 void
797 mrb_init_kernel(mrb_state *mrb)
798 {
799   struct RClass *krn;
800
801   mrb->kernel_module = krn = mrb_define_module(mrb, "Kernel");                                                    /* 15.3.1 */
802   mrb_define_class_method(mrb, krn, "block_given?",         mrb_f_block_given_p_m,           MRB_ARGS_NONE());    /* 15.3.1.2.2  */
803   mrb_define_class_method(mrb, krn, "iterator?",            mrb_f_block_given_p_m,           MRB_ARGS_NONE());    /* 15.3.1.2.5  */
804 ;     /* 15.3.1.2.11 */
805   mrb_define_class_method(mrb, krn, "raise",                mrb_f_raise,                     MRB_ARGS_OPT(2));    /* 15.3.1.2.12 */
806
807
808   mrb_define_method(mrb, krn, "===",                        mrb_equal_m,                     MRB_ARGS_REQ(1));    /* 15.3.1.3.2  */
809   mrb_define_method(mrb, krn, "block_given?",               mrb_f_block_given_p_m,           MRB_ARGS_NONE());    /* 15.3.1.3.6  */
810   mrb_define_method(mrb, krn, "class",                      mrb_obj_class_m,                 MRB_ARGS_NONE());    /* 15.3.1.3.7  */
811   mrb_define_method(mrb, krn, "clone",                      mrb_obj_clone,                   MRB_ARGS_NONE());    /* 15.3.1.3.8  */
812   mrb_define_method(mrb, krn, "dup",                        mrb_obj_dup,                     MRB_ARGS_NONE());    /* 15.3.1.3.9  */
813   mrb_define_method(mrb, krn, "eql?",                       mrb_obj_equal_m,                 MRB_ARGS_REQ(1));    /* 15.3.1.3.10 */
814   mrb_define_method(mrb, krn, "equal?",                     mrb_obj_equal_m,                 MRB_ARGS_REQ(1));    /* 15.3.1.3.11 */
815   mrb_define_method(mrb, krn, "extend",                     mrb_obj_extend_m,                MRB_ARGS_ANY());     /* 15.3.1.3.13 */
816   mrb_define_method(mrb, krn, "freeze",                     mrb_obj_freeze,                  MRB_ARGS_NONE());
817   mrb_define_method(mrb, krn, "frozen?",                    mrb_obj_frozen,                  MRB_ARGS_NONE());
818   mrb_define_method(mrb, krn, "global_variables",           mrb_f_global_variables,          MRB_ARGS_NONE());    /* 15.3.1.3.14 */
819   mrb_define_method(mrb, krn, "hash",                       mrb_obj_hash,                    MRB_ARGS_NONE());    /* 15.3.1.3.15 */
820   mrb_define_method(mrb, krn, "initialize_copy",            mrb_obj_init_copy,               MRB_ARGS_REQ(1));    /* 15.3.1.3.16 */
821   mrb_define_method(mrb, krn, "inspect",                    mrb_obj_inspect,                 MRB_ARGS_NONE());    /* 15.3.1.3.17 */
822   mrb_define_method(mrb, krn, "instance_of?",               obj_is_instance_of,              MRB_ARGS_REQ(1));    /* 15.3.1.3.19 */
823
824   mrb_define_method(mrb, krn, "is_a?",                      mrb_obj_is_kind_of_m,            MRB_ARGS_REQ(1));    /* 15.3.1.3.24 */
825   mrb_define_method(mrb, krn, "iterator?",                  mrb_f_block_given_p_m,           MRB_ARGS_NONE());    /* 15.3.1.3.25 */
826   mrb_define_method(mrb, krn, "kind_of?",                   mrb_obj_is_kind_of_m,            MRB_ARGS_REQ(1));    /* 15.3.1.3.26 */
827 #ifdef MRB_DEFAULT_METHOD_MISSING
828   mrb_define_method(mrb, krn, "method_missing",             mrb_obj_missing,                 MRB_ARGS_ANY());     /* 15.3.1.3.30 */
829 #endif
830   mrb_define_method(mrb, krn, "nil?",                       mrb_false,                       MRB_ARGS_NONE());    /* 15.3.1.3.32 */
831   mrb_define_method(mrb, krn, "object_id",                  mrb_obj_id_m,                    MRB_ARGS_NONE());    /* 15.3.1.3.33 */
832   mrb_define_method(mrb, krn, "raise",                      mrb_f_raise,                     MRB_ARGS_ANY());     /* 15.3.1.3.40 */
833   mrb_define_method(mrb, krn, "remove_instance_variable",   mrb_obj_remove_instance_variable,MRB_ARGS_REQ(1));    /* 15.3.1.3.41 */
834   mrb_define_method(mrb, krn, "respond_to?",                obj_respond_to,                  MRB_ARGS_ANY());     /* 15.3.1.3.43 */
835   mrb_define_method(mrb, krn, "to_s",                       mrb_any_to_s,                    MRB_ARGS_NONE());    /* 15.3.1.3.46 */
836   mrb_define_method(mrb, krn, "__case_eqq",                 mrb_obj_ceqq,                    MRB_ARGS_REQ(1));    /* internal */
837   mrb_define_method(mrb, krn, "__to_int",                   mrb_to_int,                      MRB_ARGS_NONE()); /* internal */
838   mrb_define_method(mrb, krn, "__to_str",                   mrb_to_str,                      MRB_ARGS_NONE()); /* internal */
839
840   mrb_include_module(mrb, mrb->object_class, mrb->kernel_module);
841   mrb_define_alias(mrb, mrb->module_class, "dup", "clone"); /* XXX */
842 }