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