1 | #include <mruby.h>
|
---|
2 | #include <mruby/error.h>
|
---|
3 | #include <mruby/array.h>
|
---|
4 | #include <mruby/hash.h>
|
---|
5 | #include <mruby/range.h>
|
---|
6 |
|
---|
7 | static mrb_value
|
---|
8 | mrb_f_caller(mrb_state *mrb, mrb_value self)
|
---|
9 | {
|
---|
10 | mrb_value bt, v, length;
|
---|
11 | mrb_int bt_len, argc, lev, n;
|
---|
12 |
|
---|
13 | bt = mrb_get_backtrace(mrb);
|
---|
14 | bt_len = RARRAY_LEN(bt);
|
---|
15 | argc = mrb_get_args(mrb, "|oo", &v, &length);
|
---|
16 |
|
---|
17 | switch (argc) {
|
---|
18 | case 0:
|
---|
19 | lev = 1;
|
---|
20 | n = bt_len - lev;
|
---|
21 | break;
|
---|
22 | case 1:
|
---|
23 | if (mrb_type(v) == MRB_TT_RANGE) {
|
---|
24 | mrb_int beg, len;
|
---|
25 | if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == 1) {
|
---|
26 | lev = beg;
|
---|
27 | n = len;
|
---|
28 | }
|
---|
29 | else {
|
---|
30 | return mrb_nil_value();
|
---|
31 | }
|
---|
32 | }
|
---|
33 | else {
|
---|
34 | v = mrb_to_int(mrb, v);
|
---|
35 | lev = mrb_fixnum(v);
|
---|
36 | if (lev < 0) {
|
---|
37 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
|
---|
38 | }
|
---|
39 | n = bt_len - lev;
|
---|
40 | }
|
---|
41 | break;
|
---|
42 | case 2:
|
---|
43 | lev = mrb_fixnum(mrb_to_int(mrb, v));
|
---|
44 | n = mrb_fixnum(mrb_to_int(mrb, length));
|
---|
45 | if (lev < 0) {
|
---|
46 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
|
---|
47 | }
|
---|
48 | if (n < 0) {
|
---|
49 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative size (%S)", length);
|
---|
50 | }
|
---|
51 | break;
|
---|
52 | default:
|
---|
53 | lev = n = 0;
|
---|
54 | break;
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (n == 0) {
|
---|
58 | return mrb_ary_new(mrb);
|
---|
59 | }
|
---|
60 |
|
---|
61 | return mrb_funcall(mrb, bt, "[]", 2, mrb_fixnum_value(lev), mrb_fixnum_value(n));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * call-seq:
|
---|
66 | * __method__ -> symbol
|
---|
67 | *
|
---|
68 | * Returns the name at the definition of the current method as a
|
---|
69 | * Symbol.
|
---|
70 | * If called outside of a method, it returns <code>nil</code>.
|
---|
71 | *
|
---|
72 | */
|
---|
73 | static mrb_value
|
---|
74 | mrb_f_method(mrb_state *mrb, mrb_value self)
|
---|
75 | {
|
---|
76 | mrb_callinfo *ci = mrb->c->ci;
|
---|
77 | ci--;
|
---|
78 | if (ci->mid)
|
---|
79 | return mrb_symbol_value(ci->mid);
|
---|
80 | else
|
---|
81 | return mrb_nil_value();
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * call-seq:
|
---|
86 | * Integer(arg,base=0) -> integer
|
---|
87 | *
|
---|
88 | * Converts <i>arg</i> to a <code>Fixnum</code>.
|
---|
89 | * Numeric types are converted directly (with floating point numbers
|
---|
90 | * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
|
---|
91 | * integer string representation. If <i>arg</i> is a <code>String</code>,
|
---|
92 | * when <i>base</i> is omitted or equals to zero, radix indicators
|
---|
93 | * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
|
---|
94 | * In any case, strings should be strictly conformed to numeric
|
---|
95 | * representation. This behavior is different from that of
|
---|
96 | * <code>String#to_i</code>. Non string values will be converted using
|
---|
97 | * <code>to_int</code>, and <code>to_i</code>. Passing <code>nil</code>
|
---|
98 | * raises a TypeError.
|
---|
99 | *
|
---|
100 | * Integer(123.999) #=> 123
|
---|
101 | * Integer("0x1a") #=> 26
|
---|
102 | * Integer(Time.new) #=> 1204973019
|
---|
103 | * Integer("0930", 10) #=> 930
|
---|
104 | * Integer("111", 2) #=> 7
|
---|
105 | * Integer(nil) #=> TypeError
|
---|
106 | */
|
---|
107 | static mrb_value
|
---|
108 | mrb_f_integer(mrb_state *mrb, mrb_value self)
|
---|
109 | {
|
---|
110 | mrb_value arg;
|
---|
111 | mrb_int base = 0;
|
---|
112 |
|
---|
113 | mrb_get_args(mrb, "o|i", &arg, &base);
|
---|
114 | return mrb_convert_to_integer(mrb, arg, base);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * call-seq:
|
---|
119 | * Float(arg) -> float
|
---|
120 | *
|
---|
121 | * Returns <i>arg</i> converted to a float. Numeric types are converted
|
---|
122 | * directly, the rest are converted using <i>arg</i>.to_f.
|
---|
123 | *
|
---|
124 | * Float(1) #=> 1.0
|
---|
125 | * Float(123.456) #=> 123.456
|
---|
126 | * Float("123.456") #=> 123.456
|
---|
127 | * Float(nil) #=> TypeError
|
---|
128 | */
|
---|
129 | static mrb_value
|
---|
130 | mrb_f_float(mrb_state *mrb, mrb_value self)
|
---|
131 | {
|
---|
132 | mrb_value arg;
|
---|
133 |
|
---|
134 | mrb_get_args(mrb, "o", &arg);
|
---|
135 | return mrb_Float(mrb, arg);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * call-seq:
|
---|
140 | * String(arg) -> string
|
---|
141 | *
|
---|
142 | * Returns <i>arg</i> as an <code>String</code>.
|
---|
143 | *
|
---|
144 | * First tries to call its <code>to_str</code> method, then its to_s method.
|
---|
145 | *
|
---|
146 | * String(self) #=> "main"
|
---|
147 | * String(self.class) #=> "Object"
|
---|
148 | * String(123456) #=> "123456"
|
---|
149 | */
|
---|
150 | static mrb_value
|
---|
151 | mrb_f_string(mrb_state *mrb, mrb_value self)
|
---|
152 | {
|
---|
153 | mrb_value arg, tmp;
|
---|
154 |
|
---|
155 | mrb_get_args(mrb, "o", &arg);
|
---|
156 | tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_str");
|
---|
157 | if (mrb_nil_p(tmp)) {
|
---|
158 | tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
|
---|
159 | }
|
---|
160 | return tmp;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * call-seq:
|
---|
165 | * Array(arg) -> array
|
---|
166 | *
|
---|
167 | * Returns +arg+ as an Array.
|
---|
168 | *
|
---|
169 | * First tries to call Array#to_ary on +arg+, then Array#to_a.
|
---|
170 | *
|
---|
171 | * Array(1..5) #=> [1, 2, 3, 4, 5]
|
---|
172 | *
|
---|
173 | */
|
---|
174 | static mrb_value
|
---|
175 | mrb_f_array(mrb_state *mrb, mrb_value self)
|
---|
176 | {
|
---|
177 | mrb_value arg, tmp;
|
---|
178 |
|
---|
179 | mrb_get_args(mrb, "o", &arg);
|
---|
180 | tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_ary");
|
---|
181 | if (mrb_nil_p(tmp)) {
|
---|
182 | tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
|
---|
183 | }
|
---|
184 | if (mrb_nil_p(tmp)) {
|
---|
185 | return mrb_ary_new_from_values(mrb, 1, &arg);
|
---|
186 | }
|
---|
187 |
|
---|
188 | return tmp;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * call-seq:
|
---|
193 | * Hash(arg) -> hash
|
---|
194 | *
|
---|
195 | * Converts <i>arg</i> to a <code>Hash</code> by calling
|
---|
196 | * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
|
---|
197 | * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
|
---|
198 | *
|
---|
199 | * Hash([]) #=> {}
|
---|
200 | * Hash(nil) #=> {}
|
---|
201 | * Hash(key: :value) #=> {:key => :value}
|
---|
202 | * Hash([1, 2, 3]) #=> TypeError
|
---|
203 | *
|
---|
204 | */
|
---|
205 | static mrb_value
|
---|
206 | mrb_f_hash(mrb_state *mrb, mrb_value self)
|
---|
207 | {
|
---|
208 | mrb_value arg, tmp;
|
---|
209 |
|
---|
210 | mrb_get_args(mrb, "o", &arg);
|
---|
211 | if (mrb_nil_p(arg)) {
|
---|
212 | return mrb_hash_new(mrb);
|
---|
213 | }
|
---|
214 | tmp = mrb_check_convert_type(mrb, arg, MRB_TT_HASH, "Hash", "to_hash");
|
---|
215 | if (mrb_nil_p(tmp)) {
|
---|
216 | if (mrb_array_p(arg) && RARRAY_LEN(arg) == 0) {
|
---|
217 | return mrb_hash_new(mrb);
|
---|
218 | }
|
---|
219 | mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into Hash",
|
---|
220 | mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, arg)));
|
---|
221 | }
|
---|
222 | return tmp;
|
---|
223 | }
|
---|
224 |
|
---|
225 | void
|
---|
226 | mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
|
---|
227 | {
|
---|
228 | struct RClass *krn = mrb->kernel_module;
|
---|
229 |
|
---|
230 | mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_OPT(2));
|
---|
231 | mrb_define_module_function(mrb, krn, "caller", mrb_f_caller, MRB_ARGS_OPT(2));
|
---|
232 | mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
|
---|
233 | mrb_define_module_function(mrb, krn, "Integer", mrb_f_integer, MRB_ARGS_ANY());
|
---|
234 | mrb_define_module_function(mrb, krn, "Float", mrb_f_float, MRB_ARGS_REQ(1));
|
---|
235 | mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
|
---|
236 | mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
|
---|
237 | mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
|
---|
238 | }
|
---|
239 |
|
---|
240 | void
|
---|
241 | mrb_mruby_kernel_ext_gem_final(mrb_state *mrb)
|
---|
242 | {
|
---|
243 | }
|
---|