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_object_p(obj) && 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 %v", self);
|
---|
329 | }
|
---|
330 | if (mrb_sclass_p(self)) {
|
---|
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 %v", obj);
|
---|
370 | }
|
---|
371 | if (mrb_sclass_p(obj)) {
|
---|
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_argnum_error(mrb, argc, 1, -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 | MRB_API mrb_value
|
---|
435 | mrb_obj_freeze(mrb_state *mrb, mrb_value self)
|
---|
436 | {
|
---|
437 | if (!mrb_immediate_p(self)) {
|
---|
438 | struct RBasic *b = mrb_basic_ptr(self);
|
---|
439 | if (!mrb_frozen_p(b)) {
|
---|
440 | MRB_SET_FROZEN_FLAG(b);
|
---|
441 | if (b->c->tt == MRB_TT_SCLASS) MRB_SET_FROZEN_FLAG(b->c);
|
---|
442 | }
|
---|
443 | }
|
---|
444 | return self;
|
---|
445 | }
|
---|
446 |
|
---|
447 | static mrb_value
|
---|
448 | mrb_obj_frozen(mrb_state *mrb, mrb_value self)
|
---|
449 | {
|
---|
450 | return mrb_bool_value(mrb_immediate_p(self) || mrb_frozen_p(mrb_basic_ptr(self)));
|
---|
451 | }
|
---|
452 |
|
---|
453 | /* 15.3.1.3.15 */
|
---|
454 | /*
|
---|
455 | * call-seq:
|
---|
456 | * obj.hash -> fixnum
|
---|
457 | *
|
---|
458 | * Generates a <code>Fixnum</code> hash value for this object. This
|
---|
459 | * function must have the property that <code>a.eql?(b)</code> implies
|
---|
460 | * <code>a.hash == b.hash</code>. The hash value is used by class
|
---|
461 | * <code>Hash</code>. Any hash value that exceeds the capacity of a
|
---|
462 | * <code>Fixnum</code> will be truncated before being used.
|
---|
463 | */
|
---|
464 | static mrb_value
|
---|
465 | mrb_obj_hash(mrb_state *mrb, mrb_value self)
|
---|
466 | {
|
---|
467 | return mrb_fixnum_value(mrb_obj_id(self));
|
---|
468 | }
|
---|
469 |
|
---|
470 | /* 15.3.1.3.16 */
|
---|
471 | static mrb_value
|
---|
472 | mrb_obj_init_copy(mrb_state *mrb, mrb_value self)
|
---|
473 | {
|
---|
474 | mrb_value orig;
|
---|
475 |
|
---|
476 | mrb_get_args(mrb, "o", &orig);
|
---|
477 | if (mrb_obj_equal(mrb, self, orig)) return self;
|
---|
478 | if ((mrb_type(self) != mrb_type(orig)) || (mrb_obj_class(mrb, self) != mrb_obj_class(mrb, orig))) {
|
---|
479 | mrb_raise(mrb, E_TYPE_ERROR, "initialize_copy should take same class object");
|
---|
480 | }
|
---|
481 | return self;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | MRB_API mrb_bool
|
---|
486 | mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c)
|
---|
487 | {
|
---|
488 | if (mrb_obj_class(mrb, obj) == c) return TRUE;
|
---|
489 | return FALSE;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /* 15.3.1.3.19 */
|
---|
493 | /*
|
---|
494 | * call-seq:
|
---|
495 | * obj.instance_of?(class) -> true or false
|
---|
496 | *
|
---|
497 | * Returns <code>true</code> if <i>obj</i> is an instance of the given
|
---|
498 | * class. See also <code>Object#kind_of?</code>.
|
---|
499 | */
|
---|
500 | static mrb_value
|
---|
501 | obj_is_instance_of(mrb_state *mrb, mrb_value self)
|
---|
502 | {
|
---|
503 | mrb_value arg;
|
---|
504 |
|
---|
505 | mrb_get_args(mrb, "C", &arg);
|
---|
506 |
|
---|
507 | return mrb_bool_value(mrb_obj_is_instance_of(mrb, self, mrb_class_ptr(arg)));
|
---|
508 | }
|
---|
509 |
|
---|
510 | /* 15.3.1.3.24 */
|
---|
511 | /* 15.3.1.3.26 */
|
---|
512 | /*
|
---|
513 | * call-seq:
|
---|
514 | * obj.is_a?(class) -> true or false
|
---|
515 | * obj.kind_of?(class) -> true or false
|
---|
516 | *
|
---|
517 | * Returns <code>true</code> if <i>class</i> is the class of
|
---|
518 | * <i>obj</i>, or if <i>class</i> is one of the superclasses of
|
---|
519 | * <i>obj</i> or modules included in <i>obj</i>.
|
---|
520 | *
|
---|
521 | * module M; end
|
---|
522 | * class A
|
---|
523 | * include M
|
---|
524 | * end
|
---|
525 | * class B < A; end
|
---|
526 | * class C < B; end
|
---|
527 | * b = B.new
|
---|
528 | * b.instance_of? A #=> false
|
---|
529 | * b.instance_of? B #=> true
|
---|
530 | * b.instance_of? C #=> false
|
---|
531 | * b.instance_of? M #=> false
|
---|
532 | * b.kind_of? A #=> true
|
---|
533 | * b.kind_of? B #=> true
|
---|
534 | * b.kind_of? C #=> false
|
---|
535 | * b.kind_of? M #=> true
|
---|
536 | */
|
---|
537 | static mrb_value
|
---|
538 | mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self)
|
---|
539 | {
|
---|
540 | mrb_value arg;
|
---|
541 |
|
---|
542 | mrb_get_args(mrb, "C", &arg);
|
---|
543 |
|
---|
544 | return mrb_bool_value(mrb_obj_is_kind_of(mrb, self, mrb_class_ptr(arg)));
|
---|
545 | }
|
---|
546 |
|
---|
547 | KHASH_DECLARE(st, mrb_sym, char, FALSE)
|
---|
548 | KHASH_DEFINE(st, mrb_sym, char, FALSE, kh_int_hash_func, kh_int_hash_equal)
|
---|
549 |
|
---|
550 | /* 15.3.1.3.32 */
|
---|
551 | /*
|
---|
552 | * call_seq:
|
---|
553 | * nil.nil? -> true
|
---|
554 | * <anything_else>.nil? -> false
|
---|
555 | *
|
---|
556 | * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
|
---|
557 | */
|
---|
558 | static mrb_value
|
---|
559 | mrb_false(mrb_state *mrb, mrb_value self)
|
---|
560 | {
|
---|
561 | return mrb_false_value();
|
---|
562 | }
|
---|
563 |
|
---|
564 | /* 15.3.1.2.12 */
|
---|
565 | /* 15.3.1.3.40 */
|
---|
566 | /*
|
---|
567 | * call-seq:
|
---|
568 | * raise
|
---|
569 | * raise(string)
|
---|
570 | * raise(exception [, string])
|
---|
571 | *
|
---|
572 | * With no arguments, raises a <code>RuntimeError</code>
|
---|
573 | * With a single +String+ argument, raises a
|
---|
574 | * +RuntimeError+ with the string as a message. Otherwise,
|
---|
575 | * the first parameter should be the name of an +Exception+
|
---|
576 | * class (or an object that returns an +Exception+ object when sent
|
---|
577 | * an +exception+ message). The optional second parameter sets the
|
---|
578 | * message associated with the exception, and the third parameter is an
|
---|
579 | * array of callback information. Exceptions are caught by the
|
---|
580 | * +rescue+ clause of <code>begin...end</code> blocks.
|
---|
581 | *
|
---|
582 | * raise "Failed to create socket"
|
---|
583 | * raise ArgumentError, "No parameters", caller
|
---|
584 | */
|
---|
585 | MRB_API mrb_value
|
---|
586 | mrb_f_raise(mrb_state *mrb, mrb_value self)
|
---|
587 | {
|
---|
588 | mrb_value a[2], exc;
|
---|
589 | mrb_int argc;
|
---|
590 |
|
---|
591 |
|
---|
592 | argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]);
|
---|
593 | switch (argc) {
|
---|
594 | case 0:
|
---|
595 | mrb_raise(mrb, E_RUNTIME_ERROR, "");
|
---|
596 | break;
|
---|
597 | case 1:
|
---|
598 | if (mrb_string_p(a[0])) {
|
---|
599 | a[1] = a[0];
|
---|
600 | argc = 2;
|
---|
601 | a[0] = mrb_obj_value(E_RUNTIME_ERROR);
|
---|
602 | }
|
---|
603 | /* fall through */
|
---|
604 | default:
|
---|
605 | exc = mrb_make_exception(mrb, argc, a);
|
---|
606 | mrb_exc_raise(mrb, exc);
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | return mrb_nil_value(); /* not reached */
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* 15.3.1.3.41 */
|
---|
613 | /*
|
---|
614 | * call-seq:
|
---|
615 | * obj.remove_instance_variable(symbol) -> obj
|
---|
616 | *
|
---|
617 | * Removes the named instance variable from <i>obj</i>, returning that
|
---|
618 | * variable's value.
|
---|
619 | *
|
---|
620 | * class Dummy
|
---|
621 | * attr_reader :var
|
---|
622 | * def initialize
|
---|
623 | * @var = 99
|
---|
624 | * end
|
---|
625 | * def remove
|
---|
626 | * remove_instance_variable(:@var)
|
---|
627 | * end
|
---|
628 | * end
|
---|
629 | * d = Dummy.new
|
---|
630 | * d.var #=> 99
|
---|
631 | * d.remove #=> 99
|
---|
632 | * d.var #=> nil
|
---|
633 | */
|
---|
634 | static mrb_value
|
---|
635 | mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self)
|
---|
636 | {
|
---|
637 | mrb_sym sym;
|
---|
638 | mrb_value val;
|
---|
639 |
|
---|
640 | mrb_get_args(mrb, "n", &sym);
|
---|
641 | mrb_iv_name_sym_check(mrb, sym);
|
---|
642 | val = mrb_iv_remove(mrb, self, sym);
|
---|
643 | if (mrb_undef_p(val)) {
|
---|
644 | mrb_name_error(mrb, sym, "instance variable %n not defined", sym);
|
---|
645 | }
|
---|
646 | return val;
|
---|
647 | }
|
---|
648 |
|
---|
649 | void
|
---|
650 | mrb_method_missing(mrb_state *mrb, mrb_sym name, mrb_value self, mrb_value args)
|
---|
651 | {
|
---|
652 | mrb_no_method_error(mrb, name, args, "undefined method '%n'", name);
|
---|
653 | }
|
---|
654 |
|
---|
655 | /* 15.3.1.3.30 */
|
---|
656 | /*
|
---|
657 | * call-seq:
|
---|
658 | * obj.method_missing(symbol [, *args] ) -> result
|
---|
659 | *
|
---|
660 | * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
|
---|
661 | * <i>symbol</i> is the symbol for the method called, and <i>args</i>
|
---|
662 | * are any arguments that were passed to it. By default, the interpreter
|
---|
663 | * raises an error when this method is called. However, it is possible
|
---|
664 | * to override the method to provide more dynamic behavior.
|
---|
665 | * If it is decided that a particular method should not be handled, then
|
---|
666 | * <i>super</i> should be called, so that ancestors can pick up the
|
---|
667 | * missing method.
|
---|
668 | * The example below creates
|
---|
669 | * a class <code>Roman</code>, which responds to methods with names
|
---|
670 | * consisting of roman numerals, returning the corresponding integer
|
---|
671 | * values.
|
---|
672 | *
|
---|
673 | * class Roman
|
---|
674 | * def romanToInt(str)
|
---|
675 | * # ...
|
---|
676 | * end
|
---|
677 | * def method_missing(methId)
|
---|
678 | * str = methId.id2name
|
---|
679 | * romanToInt(str)
|
---|
680 | * end
|
---|
681 | * end
|
---|
682 | *
|
---|
683 | * r = Roman.new
|
---|
684 | * r.iv #=> 4
|
---|
685 | * r.xxiii #=> 23
|
---|
686 | * r.mm #=> 2000
|
---|
687 | */
|
---|
688 | static mrb_value
|
---|
689 | mrb_obj_missing(mrb_state *mrb, mrb_value mod)
|
---|
690 | {
|
---|
691 | mrb_sym name;
|
---|
692 | mrb_value *a;
|
---|
693 | mrb_int alen;
|
---|
694 |
|
---|
695 | mrb_get_args(mrb, "n*!", &name, &a, &alen);
|
---|
696 | mrb_method_missing(mrb, name, mod, mrb_ary_new_from_values(mrb, alen, a));
|
---|
697 | /* not reached */
|
---|
698 | return mrb_nil_value();
|
---|
699 | }
|
---|
700 |
|
---|
701 | static inline mrb_bool
|
---|
702 | basic_obj_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym id, int pub)
|
---|
703 | {
|
---|
704 | return mrb_respond_to(mrb, obj, id);
|
---|
705 | }
|
---|
706 |
|
---|
707 | /* 15.3.1.3.43 */
|
---|
708 | /*
|
---|
709 | * call-seq:
|
---|
710 | * obj.respond_to?(symbol, include_private=false) -> true or false
|
---|
711 | *
|
---|
712 | * Returns +true+ if _obj_ responds to the given
|
---|
713 | * method. Private methods are included in the search only if the
|
---|
714 | * optional second parameter evaluates to +true+.
|
---|
715 | *
|
---|
716 | * If the method is not implemented,
|
---|
717 | * as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
|
---|
718 | * false is returned.
|
---|
719 | *
|
---|
720 | * If the method is not defined, <code>respond_to_missing?</code>
|
---|
721 | * method is called and the result is returned.
|
---|
722 | */
|
---|
723 | static mrb_value
|
---|
724 | obj_respond_to(mrb_state *mrb, mrb_value self)
|
---|
725 | {
|
---|
726 | mrb_sym id, rtm_id;
|
---|
727 | mrb_bool priv = FALSE, respond_to_p;
|
---|
728 |
|
---|
729 | mrb_get_args(mrb, "n|b", &id, &priv);
|
---|
730 | respond_to_p = basic_obj_respond_to(mrb, self, id, !priv);
|
---|
731 | if (!respond_to_p) {
|
---|
732 | rtm_id = mrb_intern_lit(mrb, "respond_to_missing?");
|
---|
733 | if (basic_obj_respond_to(mrb, self, rtm_id, !priv)) {
|
---|
734 | mrb_value args[2], v;
|
---|
735 | args[0] = mrb_symbol_value(id);
|
---|
736 | args[1] = mrb_bool_value(priv);
|
---|
737 | v = mrb_funcall_argv(mrb, self, rtm_id, 2, args);
|
---|
738 | return mrb_bool_value(mrb_bool(v));
|
---|
739 | }
|
---|
740 | }
|
---|
741 | return mrb_bool_value(respond_to_p);
|
---|
742 | }
|
---|
743 |
|
---|
744 | static mrb_value
|
---|
745 | mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
|
---|
746 | {
|
---|
747 | mrb_value v;
|
---|
748 | mrb_int i, len;
|
---|
749 | mrb_sym eqq = mrb_intern_lit(mrb, "===");
|
---|
750 | mrb_value ary = mrb_ary_splat(mrb, self);
|
---|
751 |
|
---|
752 | mrb_get_args(mrb, "o", &v);
|
---|
753 | len = RARRAY_LEN(ary);
|
---|
754 | for (i=0; i<len; i++) {
|
---|
755 | mrb_value c = mrb_funcall_argv(mrb, mrb_ary_entry(ary, i), eqq, 1, &v);
|
---|
756 | if (mrb_test(c)) return mrb_true_value();
|
---|
757 | }
|
---|
758 | return mrb_false_value();
|
---|
759 | }
|
---|
760 |
|
---|
761 | mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
|
---|
762 |
|
---|
763 | void
|
---|
764 | mrb_init_kernel(mrb_state *mrb)
|
---|
765 | {
|
---|
766 | struct RClass *krn;
|
---|
767 |
|
---|
768 | mrb->kernel_module = krn = mrb_define_module(mrb, "Kernel"); /* 15.3.1 */
|
---|
769 | mrb_define_class_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.2 */
|
---|
770 | mrb_define_class_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.5 */
|
---|
771 | ; /* 15.3.1.2.11 */
|
---|
772 | mrb_define_class_method(mrb, krn, "raise", mrb_f_raise, MRB_ARGS_OPT(2)); /* 15.3.1.2.12 */
|
---|
773 |
|
---|
774 |
|
---|
775 | mrb_define_method(mrb, krn, "===", mrb_equal_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.2 */
|
---|
776 | mrb_define_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.3.6 */
|
---|
777 | mrb_define_method(mrb, krn, "class", mrb_obj_class_m, MRB_ARGS_NONE()); /* 15.3.1.3.7 */
|
---|
778 | mrb_define_method(mrb, krn, "clone", mrb_obj_clone, MRB_ARGS_NONE()); /* 15.3.1.3.8 */
|
---|
779 | mrb_define_method(mrb, krn, "dup", mrb_obj_dup, MRB_ARGS_NONE()); /* 15.3.1.3.9 */
|
---|
780 | mrb_define_method(mrb, krn, "eql?", mrb_obj_equal_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.10 */
|
---|
781 | mrb_define_method(mrb, krn, "extend", mrb_obj_extend_m, MRB_ARGS_ANY()); /* 15.3.1.3.13 */
|
---|
782 | mrb_define_method(mrb, krn, "freeze", mrb_obj_freeze, MRB_ARGS_NONE());
|
---|
783 | mrb_define_method(mrb, krn, "frozen?", mrb_obj_frozen, MRB_ARGS_NONE());
|
---|
784 | mrb_define_method(mrb, krn, "hash", mrb_obj_hash, MRB_ARGS_NONE()); /* 15.3.1.3.15 */
|
---|
785 | mrb_define_method(mrb, krn, "initialize_copy", mrb_obj_init_copy, MRB_ARGS_REQ(1)); /* 15.3.1.3.16 */
|
---|
786 | mrb_define_method(mrb, krn, "inspect", mrb_obj_inspect, MRB_ARGS_NONE()); /* 15.3.1.3.17 */
|
---|
787 | mrb_define_method(mrb, krn, "instance_of?", obj_is_instance_of, MRB_ARGS_REQ(1)); /* 15.3.1.3.19 */
|
---|
788 |
|
---|
789 | mrb_define_method(mrb, krn, "is_a?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.24 */
|
---|
790 | mrb_define_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.3.25 */
|
---|
791 | mrb_define_method(mrb, krn, "kind_of?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.26 */
|
---|
792 | mrb_define_method(mrb, krn, "method_missing", mrb_obj_missing, MRB_ARGS_ANY()); /* 15.3.1.3.30 */
|
---|
793 | mrb_define_method(mrb, krn, "nil?", mrb_false, MRB_ARGS_NONE()); /* 15.3.1.3.32 */
|
---|
794 | mrb_define_method(mrb, krn, "object_id", mrb_obj_id_m, MRB_ARGS_NONE()); /* 15.3.1.3.33 */
|
---|
795 | mrb_define_method(mrb, krn, "raise", mrb_f_raise, MRB_ARGS_ANY()); /* 15.3.1.3.40 */
|
---|
796 | mrb_define_method(mrb, krn, "remove_instance_variable", mrb_obj_remove_instance_variable,MRB_ARGS_REQ(1)); /* 15.3.1.3.41 */
|
---|
797 | mrb_define_method(mrb, krn, "respond_to?", obj_respond_to, MRB_ARGS_ARG(1,1)); /* 15.3.1.3.43 */
|
---|
798 | mrb_define_method(mrb, krn, "to_s", mrb_any_to_s, MRB_ARGS_NONE()); /* 15.3.1.3.46 */
|
---|
799 | mrb_define_method(mrb, krn, "__case_eqq", mrb_obj_ceqq, MRB_ARGS_REQ(1)); /* internal */
|
---|
800 | mrb_define_method(mrb, krn, "__to_int", mrb_to_int, MRB_ARGS_NONE()); /* internal */
|
---|
801 | mrb_define_method(mrb, krn, "__to_str", mrb_to_str, MRB_ARGS_NONE()); /* internal */
|
---|
802 |
|
---|
803 | mrb_include_module(mrb, mrb->object_class, mrb->kernel_module);
|
---|
804 | }
|
---|