source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-object-ext/src/object.c@ 439

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

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 2.8 KB
Line 
1#include <mruby.h>
2#include <mruby/array.h>
3#include <mruby/class.h>
4#include <mruby/hash.h>
5#include <mruby/proc.h>
6
7/*
8 * call-seq:
9 * nil.to_a -> []
10 *
11 * Always returns an empty array.
12 */
13
14static mrb_value
15nil_to_a(mrb_state *mrb, mrb_value obj)
16{
17 return mrb_ary_new(mrb);
18}
19
20#ifndef MRB_WITHOUT_FLOAT
21/*
22 * call-seq:
23 * nil.to_f -> 0.0
24 *
25 * Always returns zero.
26 */
27
28static mrb_value
29nil_to_f(mrb_state *mrb, mrb_value obj)
30{
31 return mrb_float_value(mrb, 0.0);
32}
33#endif
34
35/*
36 * call-seq:
37 * nil.to_h -> {}
38 *
39 * Always returns an empty hash.
40 */
41
42static mrb_value
43nil_to_h(mrb_state *mrb, mrb_value obj)
44{
45 return mrb_hash_new(mrb);
46}
47
48/*
49 * call-seq:
50 * nil.to_i -> 0
51 *
52 * Always returns zero.
53 */
54
55static mrb_value
56nil_to_i(mrb_state *mrb, mrb_value obj)
57{
58 return mrb_fixnum_value(0);
59}
60
61/*
62 * call-seq:
63 * obj.itself -> an_object
64 *
65 * Returns <i>obj</i>.
66 *
67 * string = 'my string' #=> "my string"
68 * string.itself.object_id == string.object_id #=> true
69 *
70 */
71static mrb_value
72mrb_f_itself(mrb_state *mrb, mrb_value self)
73{
74 return self;
75}
76
77/*
78 * call-seq:
79 * obj.instance_exec(arg...) {|var...| block } -> obj
80 *
81 * Executes the given block within the context of the receiver
82 * (_obj_). In order to set the context, the variable +self+ is set
83 * to _obj_ while the code is executing, giving the code access to
84 * _obj_'s instance variables. Arguments are passed as block parameters.
85 *
86 * class KlassWithSecret
87 * def initialize
88 * @secret = 99
89 * end
90 * end
91 * k = KlassWithSecret.new
92 * k.instance_exec(5) {|x| @secret+x } #=> 104
93 */
94
95static mrb_value
96mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
97{
98 const mrb_value *argv;
99 mrb_int argc;
100 mrb_value blk;
101 struct RClass *c;
102
103 mrb_get_args(mrb, "*&!", &argv, &argc, &blk);
104
105 switch (mrb_type(self)) {
106 case MRB_TT_SYMBOL:
107 case MRB_TT_FIXNUM:
108#ifndef MRB_WITHOUT_FLOAT
109 case MRB_TT_FLOAT:
110#endif
111 c = NULL;
112 break;
113 default:
114 c = mrb_class_ptr(mrb_singleton_class(mrb, self));
115 break;
116 }
117 mrb->c->ci->target_class = c;
118 return mrb_yield_cont(mrb, blk, self, argc, argv);
119}
120
121void
122mrb_mruby_object_ext_gem_init(mrb_state* mrb)
123{
124 struct RClass * n = mrb->nil_class;
125
126 mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE());
127#ifndef MRB_WITHOUT_FLOAT
128 mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
129#endif
130 mrb_define_method(mrb, n, "to_h", nil_to_h, MRB_ARGS_NONE());
131 mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
132
133 mrb_define_method(mrb, mrb->kernel_module, "itself", mrb_f_itself, MRB_ARGS_NONE());
134
135 mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
136}
137
138void
139mrb_mruby_object_ext_gem_final(mrb_state* mrb)
140{
141}
Note: See TracBrowser for help on using the repository browser.