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