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