source: EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-object-ext/src/object.c@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 2.1 KB
Line 
1#include "mruby.h"
2#include "mruby/array.h"
3#include "mruby/class.h"
4
5/*
6 * call-seq:
7 * nil.to_a -> []
8 *
9 * Always returns an empty array.
10 */
11
12static mrb_value
13nil_to_a(mrb_state *mrb, mrb_value obj)
14{
15 return mrb_ary_new(mrb);
16}
17
18/*
19 * call-seq:
20 * nil.to_f -> 0.0
21 *
22 * Always returns zero.
23 */
24
25static mrb_value
26nil_to_f(mrb_state *mrb, mrb_value obj)
27{
28 return mrb_float_value(mrb, 0.0);
29}
30
31/*
32 * call-seq:
33 * nil.to_i -> 0
34 *
35 * Always returns zero.
36 */
37
38static mrb_value
39nil_to_i(mrb_state *mrb, mrb_value obj)
40{
41 return mrb_fixnum_value(0);
42}
43
44/*
45 * call-seq:
46 * obj.instance_exec(arg...) {|var...| block } -> obj
47 *
48 * Executes the given block within the context of the receiver
49 * (_obj_). In order to set the context, the variable +self+ is set
50 * to _obj_ while the code is executing, giving the code access to
51 * _obj_'s instance variables. Arguments are passed as block parameters.
52 *
53 * class KlassWithSecret
54 * def initialize
55 * @secret = 99
56 * end
57 * end
58 * k = KlassWithSecret.new
59 * k.instance_exec(5) {|x| @secret+x } #=> 104
60 */
61
62static mrb_value
63mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
64{
65 mrb_value *argv;
66 mrb_int argc;
67 mrb_value blk;
68 struct RClass *c;
69
70 mrb_get_args(mrb, "*&", &argv, &argc, &blk);
71
72 if (mrb_nil_p(blk)) {
73 mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
74 }
75
76 switch (mrb_type(self)) {
77 case MRB_TT_SYMBOL:
78 case MRB_TT_FIXNUM:
79 case MRB_TT_FLOAT:
80 c = NULL;
81 break;
82 default:
83 c = mrb_class_ptr(mrb_singleton_class(mrb, self));
84 break;
85 }
86
87 return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
88}
89
90void
91mrb_mruby_object_ext_gem_init(mrb_state* mrb)
92{
93 struct RClass * n = mrb->nil_class;
94
95 mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE());
96 mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
97 mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
98
99 mrb_define_method(mrb, mrb->object_class, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
100}
101
102void
103mrb_mruby_object_ext_gem_final(mrb_state* mrb)
104{
105}
Note: See TracBrowser for help on using the repository browser.