Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-object-ext/src/object.c

raw
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 #include <mruby/presym.h>
7
8 /*
9 * call-seq:
10 * nil.to_a -> []
11 *
12 * Always returns an empty array.
13 */
14
15 static mrb_value
16 nil_to_a(mrb_state *mrb, mrb_value obj)
17 {
18 return mrb_ary_new(mrb);
19 }
20
21 #ifndef MRB_NO_FLOAT
22 /*
23 * call-seq:
24 * nil.to_f -> 0.0
25 *
26 * Always returns zero.
27 */
28
29 static mrb_value
30 nil_to_f(mrb_state *mrb, mrb_value obj)
31 {
32 return mrb_float_value(mrb, 0.0);
33 }
34 #endif
35
36 /*
37 * call-seq:
38 * nil.to_h -> {}
39 *
40 * Always returns an empty hash.
41 */
42
43 static mrb_value
44 nil_to_h(mrb_state *mrb, mrb_value obj)
45 {
46 return mrb_hash_new(mrb);
47 }
48
49 /*
50 * call-seq:
51 * nil.to_i -> 0
52 *
53 * Always returns zero.
54 */
55
56 static mrb_value
57 nil_to_i(mrb_state *mrb, mrb_value obj)
58 {
59 return mrb_fixnum_value(0);
60 }
61
62 /*
63 * call-seq:
64 * obj.itself -> an_object
65 *
66 * Returns <i>obj</i>.
67 *
68 * string = 'my string' #=> "my string"
69 * string.itself.object_id == string.object_id #=> true
70 *
71 */
72 static mrb_value
73 f_itself(mrb_state *mrb, mrb_value self)
74 {
75 return self;
76 }
77
78 /*
79 * call-seq:
80 * obj.instance_exec(arg...) {|var...| block } -> obj
81 *
82 * Executes the given block within the context of the receiver
83 * (_obj_). In order to set the context, the variable +self+ is set
84 * to _obj_ while the code is executing, giving the code access to
85 * _obj_'s instance variables. Arguments are passed as block parameters.
86 *
87 * class KlassWithSecret
88 * def initialize
89 * @secret = 99
90 * end
91 * end
92 * k = KlassWithSecret.new
93 * k.instance_exec(5) {|x| @secret+x } #=> 104
94 */
95
96 static mrb_value
97 obj_instance_exec(mrb_state *mrb, mrb_value self)
98 {
99 const mrb_value *argv;
100 mrb_int argc;
101 mrb_value blk;
102 struct RClass *c;
103
104 mrb_get_args(mrb, "*&!", &argv, &argc, &blk);
105 c = mrb_singleton_class_ptr(mrb, self);
106 if (mrb->c->ci->cci > 0) {
107 return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
108 }
109 mrb_vm_ci_target_class_set(mrb->c->ci, c);
110 return mrb_yield_cont(mrb, blk, self, argc, argv);
111 }
112
113 void
114 mrb_mruby_object_ext_gem_init(mrb_state* mrb)
115 {
116 struct RClass * n = mrb->nil_class;
117
118 mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE());
119 #ifndef MRB_NO_FLOAT
120 mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
121 #endif
122 mrb_define_method(mrb, n, "to_h", nil_to_h, MRB_ARGS_NONE());
123 mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
124
125 mrb_define_method(mrb, mrb->kernel_module, "itself", f_itself, MRB_ARGS_NONE());
126
127 mrb_define_method(mrb, mrb_class_get_id(mrb, MRB_SYM(BasicObject)), "instance_exec", obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
128 }
129
130 void
131 mrb_mruby_object_ext_gem_final(mrb_state* mrb)
132 {
133 }