Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mruby_mbed_web mirb_mbed
kernel.c
00001 #include "mruby.h" 00002 #include "mruby/error.h" 00003 #include "mruby/array.h" 00004 #include "mruby/hash.h" 00005 00006 /* 00007 * call-seq: 00008 * __method__ -> symbol 00009 * 00010 * Returns the name at the definition of the current method as a 00011 * Symbol. 00012 * If called outside of a method, it returns <code>nil</code>. 00013 * 00014 */ 00015 static mrb_value 00016 mrb_f_method(mrb_state *mrb, mrb_value self) 00017 { 00018 mrb_callinfo *ci = mrb->c->ci; 00019 ci--; 00020 if (ci->mid) 00021 return mrb_symbol_value(ci->mid); 00022 else 00023 return mrb_nil_value(); 00024 } 00025 00026 /* 00027 * call-seq: 00028 * Integer(arg,base=0) -> integer 00029 * 00030 * Converts <i>arg</i> to a <code>Fixnum</code>. 00031 * Numeric types are converted directly (with floating point numbers 00032 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for 00033 * integer string representation. If <i>arg</i> is a <code>String</code>, 00034 * when <i>base</i> is omitted or equals to zero, radix indicators 00035 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored. 00036 * In any case, strings should be strictly conformed to numeric 00037 * representation. This behavior is different from that of 00038 * <code>String#to_i</code>. Non string values will be converted using 00039 * <code>to_int</code>, and <code>to_i</code>. Passing <code>nil</code> 00040 * raises a TypeError. 00041 * 00042 * Integer(123.999) #=> 123 00043 * Integer("0x1a") #=> 26 00044 * Integer(Time.new) #=> 1204973019 00045 * Integer("0930", 10) #=> 930 00046 * Integer("111", 2) #=> 7 00047 * Integer(nil) #=> TypeError 00048 */ 00049 static mrb_value 00050 mrb_f_integer(mrb_state *mrb, mrb_value self) 00051 { 00052 mrb_value arg; 00053 mrb_int base = 0; 00054 00055 mrb_get_args(mrb, "o|i", &arg, &base); 00056 return mrb_convert_to_integer(mrb, arg, base); 00057 } 00058 00059 /* 00060 * call-seq: 00061 * Float(arg) -> float 00062 * 00063 * Returns <i>arg</i> converted to a float. Numeric types are converted 00064 * directly, the rest are converted using <i>arg</i>.to_f. 00065 * 00066 * Float(1) #=> 1.0 00067 * Float(123.456) #=> 123.456 00068 * Float("123.456") #=> 123.456 00069 * Float(nil) #=> TypeError 00070 */ 00071 static mrb_value 00072 mrb_f_float(mrb_state *mrb, mrb_value self) 00073 { 00074 mrb_value arg; 00075 00076 mrb_get_args(mrb, "o", &arg); 00077 return mrb_Float(mrb, arg); 00078 } 00079 00080 /* 00081 * call-seq: 00082 * String(arg) -> string 00083 * 00084 * Returns <i>arg</i> as an <code>String</code>. 00085 * 00086 * First tries to call its <code>to_str</code> method, then its to_s method. 00087 * 00088 * String(self) #=> "main" 00089 * String(self.class) #=> "Object" 00090 * String(123456) #=> "123456" 00091 */ 00092 static mrb_value 00093 mrb_f_string(mrb_state *mrb, mrb_value self) 00094 { 00095 mrb_value arg, tmp; 00096 00097 mrb_get_args(mrb, "o", &arg); 00098 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_str"); 00099 if (mrb_nil_p(tmp)) { 00100 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s"); 00101 } 00102 return tmp; 00103 } 00104 00105 /* 00106 * call-seq: 00107 * Array(arg) -> array 00108 * 00109 * Returns +arg+ as an Array. 00110 * 00111 * First tries to call Array#to_ary on +arg+, then Array#to_a. 00112 * 00113 * Array(1..5) #=> [1, 2, 3, 4, 5] 00114 * 00115 */ 00116 static mrb_value 00117 mrb_f_array(mrb_state *mrb, mrb_value self) 00118 { 00119 mrb_value arg, tmp; 00120 00121 mrb_get_args(mrb, "o", &arg); 00122 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_ary"); 00123 if (mrb_nil_p(tmp)) { 00124 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a"); 00125 } 00126 if (mrb_nil_p(tmp)) { 00127 return mrb_ary_new_from_values(mrb, 1, &arg); 00128 } 00129 00130 return tmp; 00131 } 00132 00133 /* 00134 * call-seq: 00135 * Hash(arg) -> hash 00136 * 00137 * Converts <i>arg</i> to a <code>Hash</code> by calling 00138 * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when 00139 * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>. 00140 * 00141 * Hash([]) #=> {} 00142 * Hash(nil) #=> {} 00143 * Hash(key: :value) #=> {:key => :value} 00144 * Hash([1, 2, 3]) #=> TypeError 00145 */ 00146 static mrb_value 00147 mrb_f_hash(mrb_state *mrb, mrb_value self) 00148 { 00149 mrb_value arg, tmp; 00150 00151 mrb_get_args(mrb, "o", &arg); 00152 if (mrb_nil_p(arg)) { 00153 return mrb_hash_new(mrb); 00154 } 00155 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_HASH, "Hash", "to_hash"); 00156 if (mrb_nil_p(tmp)) { 00157 if (mrb_array_p(arg) && RARRAY_LEN(arg) == 0) { 00158 return mrb_hash_new(mrb); 00159 } 00160 mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into Hash", 00161 mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, arg))); 00162 } 00163 return tmp; 00164 } 00165 00166 void 00167 mrb_mruby_kernel_ext_gem_init(mrb_state *mrb) 00168 { 00169 struct RClass *krn = mrb->kernel_module; 00170 00171 mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_OPT(2)); 00172 mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE()); 00173 mrb_define_module_function(mrb, krn, "Integer", mrb_f_integer, MRB_ARGS_ANY()); 00174 mrb_define_module_function(mrb, krn, "Float", mrb_f_float, MRB_ARGS_REQ(1)); 00175 mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1)); 00176 mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1)); 00177 mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1)); 00178 } 00179 00180 void 00181 mrb_mruby_kernel_ext_gem_final(mrb_state *mrb) 00182 { 00183 } 00184
Generated on Tue Jul 12 2022 18:00:34 by
1.7.2