mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/value.h@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 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/value.h - mruby value definitions |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_VALUE_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_VALUE_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | typedef uint32_t mrb_sym; |
mzta | 0:158c61bb030f | 11 | typedef uint8_t mrb_bool; |
mzta | 0:158c61bb030f | 12 | struct mrb_state; |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | #if defined(MRB_INT16) && defined(MRB_INT64) |
mzta | 0:158c61bb030f | 15 | # error "You can't define MRB_INT16 and MRB_INT64 at the same time." |
mzta | 0:158c61bb030f | 16 | #endif |
mzta | 0:158c61bb030f | 17 | |
mzta | 0:158c61bb030f | 18 | #if defined(MRB_INT64) |
mzta | 0:158c61bb030f | 19 | typedef int64_t mrb_int; |
mzta | 0:158c61bb030f | 20 | # define MRB_INT_BIT 64 |
mzta | 0:158c61bb030f | 21 | # define MRB_INT_MIN (INT64_MIN>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 22 | # define MRB_INT_MAX (INT64_MAX>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 23 | #elif defined(MRB_INT16) |
mzta | 0:158c61bb030f | 24 | typedef int16_t mrb_int; |
mzta | 0:158c61bb030f | 25 | # define MRB_INT_BIT 16 |
mzta | 0:158c61bb030f | 26 | # define MRB_INT_MIN (INT16_MIN>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 27 | # define MRB_INT_MAX (INT16_MAX>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 28 | #else |
mzta | 0:158c61bb030f | 29 | typedef int32_t mrb_int; |
mzta | 0:158c61bb030f | 30 | # define MRB_INT_BIT 32 |
mzta | 0:158c61bb030f | 31 | # define MRB_INT_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 32 | # define MRB_INT_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT) |
mzta | 0:158c61bb030f | 33 | #endif |
mzta | 0:158c61bb030f | 34 | |
mzta | 0:158c61bb030f | 35 | #ifdef MRB_USE_FLOAT |
mzta | 0:158c61bb030f | 36 | typedef float mrb_float; |
mzta | 0:158c61bb030f | 37 | # define mrb_float_to_str(buf, i) sprintf(buf, "%.7e", i) |
mzta | 0:158c61bb030f | 38 | # define str_to_mrb_float(buf) strtof(buf, NULL) |
mzta | 0:158c61bb030f | 39 | #else |
mzta | 0:158c61bb030f | 40 | typedef double mrb_float; |
mzta | 0:158c61bb030f | 41 | # define mrb_float_to_str(buf, i) sprintf(buf, "%.16e", i) |
mzta | 0:158c61bb030f | 42 | # define str_to_mrb_float(buf) strtod(buf, NULL) |
mzta | 0:158c61bb030f | 43 | #endif |
mzta | 0:158c61bb030f | 44 | |
mzta | 0:158c61bb030f | 45 | #ifdef _MSC_VER |
mzta | 0:158c61bb030f | 46 | # ifndef __cplusplus |
mzta | 0:158c61bb030f | 47 | # define inline __inline |
mzta | 0:158c61bb030f | 48 | # endif |
mzta | 0:158c61bb030f | 49 | # if _MSC_VER < 1900 |
mzta | 0:158c61bb030f | 50 | # include <stdarg.h> |
mzta | 0:158c61bb030f | 51 | MRB_API int mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg); |
mzta | 0:158c61bb030f | 52 | MRB_API int mrb_msvc_snprintf(char *s, size_t n, const char *format, ...); |
mzta | 0:158c61bb030f | 53 | # define vsnprintf(s, n, format, arg) mrb_msvc_vsnprintf(s, n, format, arg) |
mzta | 0:158c61bb030f | 54 | # define snprintf(s, n, format, ...) mrb_msvc_snprintf(s, n, format, __VA_ARGS__) |
mzta | 0:158c61bb030f | 55 | # endif |
mzta | 0:158c61bb030f | 56 | # if _MSC_VER < 1800 |
mzta | 0:158c61bb030f | 57 | # include <float.h> |
mzta | 0:158c61bb030f | 58 | # define isfinite(n) _finite(n) |
mzta | 0:158c61bb030f | 59 | # define isnan _isnan |
mzta | 0:158c61bb030f | 60 | # define isinf(n) (!_finite(n) && !_isnan(n)) |
mzta | 0:158c61bb030f | 61 | # define signbit(n) (_copysign(1.0, (n)) < 0.0) |
mzta | 0:158c61bb030f | 62 | # define strtof (float)strtod |
mzta | 0:158c61bb030f | 63 | static const unsigned int IEEE754_INFINITY_BITS_SINGLE = 0x7F800000; |
mzta | 0:158c61bb030f | 64 | # define INFINITY (*(float *)&IEEE754_INFINITY_BITS_SINGLE) |
mzta | 0:158c61bb030f | 65 | # define NAN ((float)(INFINITY - INFINITY)) |
mzta | 0:158c61bb030f | 66 | # else |
mzta | 0:158c61bb030f | 67 | # include <inttypes.h> |
mzta | 0:158c61bb030f | 68 | # endif |
mzta | 0:158c61bb030f | 69 | #else |
mzta | 0:158c61bb030f | 70 | # include <inttypes.h> |
mzta | 0:158c61bb030f | 71 | #endif |
mzta | 0:158c61bb030f | 72 | |
mzta | 0:158c61bb030f | 73 | enum mrb_vtype { |
mzta | 0:158c61bb030f | 74 | MRB_TT_FALSE = 0, /* 0 */ |
mzta | 0:158c61bb030f | 75 | MRB_TT_FREE, /* 1 */ |
mzta | 0:158c61bb030f | 76 | MRB_TT_TRUE, /* 2 */ |
mzta | 0:158c61bb030f | 77 | MRB_TT_FIXNUM, /* 3 */ |
mzta | 0:158c61bb030f | 78 | MRB_TT_SYMBOL, /* 4 */ |
mzta | 0:158c61bb030f | 79 | MRB_TT_UNDEF, /* 5 */ |
mzta | 0:158c61bb030f | 80 | MRB_TT_FLOAT, /* 6 */ |
mzta | 0:158c61bb030f | 81 | MRB_TT_CPTR, /* 7 */ |
mzta | 0:158c61bb030f | 82 | MRB_TT_OBJECT, /* 8 */ |
mzta | 0:158c61bb030f | 83 | MRB_TT_CLASS, /* 9 */ |
mzta | 0:158c61bb030f | 84 | MRB_TT_MODULE, /* 10 */ |
mzta | 0:158c61bb030f | 85 | MRB_TT_ICLASS, /* 11 */ |
mzta | 0:158c61bb030f | 86 | MRB_TT_SCLASS, /* 12 */ |
mzta | 0:158c61bb030f | 87 | MRB_TT_PROC, /* 13 */ |
mzta | 0:158c61bb030f | 88 | MRB_TT_ARRAY, /* 14 */ |
mzta | 0:158c61bb030f | 89 | MRB_TT_HASH, /* 15 */ |
mzta | 0:158c61bb030f | 90 | MRB_TT_STRING, /* 16 */ |
mzta | 0:158c61bb030f | 91 | MRB_TT_RANGE, /* 17 */ |
mzta | 0:158c61bb030f | 92 | MRB_TT_EXCEPTION, /* 18 */ |
mzta | 0:158c61bb030f | 93 | MRB_TT_FILE, /* 19 */ |
mzta | 0:158c61bb030f | 94 | MRB_TT_ENV, /* 20 */ |
mzta | 0:158c61bb030f | 95 | MRB_TT_DATA, /* 21 */ |
mzta | 0:158c61bb030f | 96 | MRB_TT_FIBER, /* 22 */ |
mzta | 0:158c61bb030f | 97 | MRB_TT_MAXDEFINE /* 23 */ |
mzta | 0:158c61bb030f | 98 | }; |
mzta | 0:158c61bb030f | 99 | |
mzta | 0:158c61bb030f | 100 | #include "mruby/object.h" |
mzta | 0:158c61bb030f | 101 | |
mzta | 0:158c61bb030f | 102 | #if defined(MRB_NAN_BOXING) |
mzta | 0:158c61bb030f | 103 | #include "boxing_nan.h" |
mzta | 0:158c61bb030f | 104 | #elif defined(MRB_WORD_BOXING) |
mzta | 0:158c61bb030f | 105 | #include "boxing_word.h" |
mzta | 0:158c61bb030f | 106 | #else |
mzta | 0:158c61bb030f | 107 | #include "boxing_no.h" |
mzta | 0:158c61bb030f | 108 | #endif |
mzta | 0:158c61bb030f | 109 | |
mzta | 0:158c61bb030f | 110 | #ifndef mrb_fixnum_p |
mzta | 0:158c61bb030f | 111 | #define mrb_fixnum_p(o) (mrb_type(o) == MRB_TT_FIXNUM) |
mzta | 0:158c61bb030f | 112 | #endif |
mzta | 0:158c61bb030f | 113 | #ifndef mrb_undef_p |
mzta | 0:158c61bb030f | 114 | #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF) |
mzta | 0:158c61bb030f | 115 | #endif |
mzta | 0:158c61bb030f | 116 | #ifndef mrb_nil_p |
mzta | 0:158c61bb030f | 117 | #define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !mrb_fixnum(o)) |
mzta | 0:158c61bb030f | 118 | #endif |
mzta | 0:158c61bb030f | 119 | #ifndef mrb_bool |
mzta | 0:158c61bb030f | 120 | #define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE) |
mzta | 0:158c61bb030f | 121 | #endif |
mzta | 0:158c61bb030f | 122 | #define mrb_float_p(o) (mrb_type(o) == MRB_TT_FLOAT) |
mzta | 0:158c61bb030f | 123 | #define mrb_symbol_p(o) (mrb_type(o) == MRB_TT_SYMBOL) |
mzta | 0:158c61bb030f | 124 | #define mrb_array_p(o) (mrb_type(o) == MRB_TT_ARRAY) |
mzta | 0:158c61bb030f | 125 | #define mrb_string_p(o) (mrb_type(o) == MRB_TT_STRING) |
mzta | 0:158c61bb030f | 126 | #define mrb_hash_p(o) (mrb_type(o) == MRB_TT_HASH) |
mzta | 0:158c61bb030f | 127 | #define mrb_cptr_p(o) (mrb_type(o) == MRB_TT_CPTR) |
mzta | 0:158c61bb030f | 128 | #define mrb_exception_p(o) (mrb_type(o) == MRB_TT_EXCEPTION) |
mzta | 0:158c61bb030f | 129 | #define mrb_test(o) mrb_bool(o) |
mzta | 0:158c61bb030f | 130 | MRB_API mrb_bool mrb_regexp_p(struct mrb_state*, mrb_value); |
mzta | 0:158c61bb030f | 131 | |
mzta | 0:158c61bb030f | 132 | static inline mrb_value |
mzta | 0:158c61bb030f | 133 | mrb_float_value(struct mrb_state *mrb, mrb_float f) |
mzta | 0:158c61bb030f | 134 | { |
mzta | 0:158c61bb030f | 135 | mrb_value v; |
mzta | 0:158c61bb030f | 136 | (void) mrb; |
mzta | 0:158c61bb030f | 137 | SET_FLOAT_VALUE(mrb, v, f); |
mzta | 0:158c61bb030f | 138 | return v; |
mzta | 0:158c61bb030f | 139 | } |
mzta | 0:158c61bb030f | 140 | |
mzta | 0:158c61bb030f | 141 | static inline mrb_value |
mzta | 0:158c61bb030f | 142 | mrb_cptr_value(struct mrb_state *mrb, void *p) |
mzta | 0:158c61bb030f | 143 | { |
mzta | 0:158c61bb030f | 144 | mrb_value v; |
mzta | 0:158c61bb030f | 145 | (void) mrb; |
mzta | 0:158c61bb030f | 146 | SET_CPTR_VALUE(mrb,v,p); |
mzta | 0:158c61bb030f | 147 | return v; |
mzta | 0:158c61bb030f | 148 | } |
mzta | 0:158c61bb030f | 149 | |
mzta | 0:158c61bb030f | 150 | static inline mrb_value |
mzta | 0:158c61bb030f | 151 | mrb_fixnum_value(mrb_int i) |
mzta | 0:158c61bb030f | 152 | { |
mzta | 0:158c61bb030f | 153 | mrb_value v; |
mzta | 0:158c61bb030f | 154 | SET_INT_VALUE(v, i); |
mzta | 0:158c61bb030f | 155 | return v; |
mzta | 0:158c61bb030f | 156 | } |
mzta | 0:158c61bb030f | 157 | |
mzta | 0:158c61bb030f | 158 | static inline mrb_value |
mzta | 0:158c61bb030f | 159 | mrb_symbol_value(mrb_sym i) |
mzta | 0:158c61bb030f | 160 | { |
mzta | 0:158c61bb030f | 161 | mrb_value v; |
mzta | 0:158c61bb030f | 162 | SET_SYM_VALUE(v, i); |
mzta | 0:158c61bb030f | 163 | return v; |
mzta | 0:158c61bb030f | 164 | } |
mzta | 0:158c61bb030f | 165 | |
mzta | 0:158c61bb030f | 166 | static inline mrb_value |
mzta | 0:158c61bb030f | 167 | mrb_obj_value(void *p) |
mzta | 0:158c61bb030f | 168 | { |
mzta | 0:158c61bb030f | 169 | mrb_value v; |
mzta | 0:158c61bb030f | 170 | SET_OBJ_VALUE(v, (struct RBasic*)p); |
mzta | 0:158c61bb030f | 171 | return v; |
mzta | 0:158c61bb030f | 172 | } |
mzta | 0:158c61bb030f | 173 | |
mzta | 0:158c61bb030f | 174 | static inline mrb_value |
mzta | 0:158c61bb030f | 175 | mrb_nil_value(void) |
mzta | 0:158c61bb030f | 176 | { |
mzta | 0:158c61bb030f | 177 | mrb_value v; |
mzta | 0:158c61bb030f | 178 | SET_NIL_VALUE(v); |
mzta | 0:158c61bb030f | 179 | return v; |
mzta | 0:158c61bb030f | 180 | } |
mzta | 0:158c61bb030f | 181 | |
mzta | 0:158c61bb030f | 182 | static inline mrb_value |
mzta | 0:158c61bb030f | 183 | mrb_false_value(void) |
mzta | 0:158c61bb030f | 184 | { |
mzta | 0:158c61bb030f | 185 | mrb_value v; |
mzta | 0:158c61bb030f | 186 | SET_FALSE_VALUE(v); |
mzta | 0:158c61bb030f | 187 | return v; |
mzta | 0:158c61bb030f | 188 | } |
mzta | 0:158c61bb030f | 189 | |
mzta | 0:158c61bb030f | 190 | static inline mrb_value |
mzta | 0:158c61bb030f | 191 | mrb_true_value(void) |
mzta | 0:158c61bb030f | 192 | { |
mzta | 0:158c61bb030f | 193 | mrb_value v; |
mzta | 0:158c61bb030f | 194 | SET_TRUE_VALUE(v); |
mzta | 0:158c61bb030f | 195 | return v; |
mzta | 0:158c61bb030f | 196 | } |
mzta | 0:158c61bb030f | 197 | |
mzta | 0:158c61bb030f | 198 | static inline mrb_value |
mzta | 0:158c61bb030f | 199 | mrb_bool_value(mrb_bool boolean) |
mzta | 0:158c61bb030f | 200 | { |
mzta | 0:158c61bb030f | 201 | mrb_value v; |
mzta | 0:158c61bb030f | 202 | SET_BOOL_VALUE(v, boolean); |
mzta | 0:158c61bb030f | 203 | return v; |
mzta | 0:158c61bb030f | 204 | } |
mzta | 0:158c61bb030f | 205 | |
mzta | 0:158c61bb030f | 206 | static inline mrb_value |
mzta | 0:158c61bb030f | 207 | mrb_undef_value(void) |
mzta | 0:158c61bb030f | 208 | { |
mzta | 0:158c61bb030f | 209 | mrb_value v; |
mzta | 0:158c61bb030f | 210 | SET_UNDEF_VALUE(v); |
mzta | 0:158c61bb030f | 211 | return v; |
mzta | 0:158c61bb030f | 212 | } |
mzta | 0:158c61bb030f | 213 | |
mzta | 0:158c61bb030f | 214 | #ifdef MRB_USE_ETEXT_EDATA |
mzta | 0:158c61bb030f | 215 | extern char _etext[]; |
mzta | 0:158c61bb030f | 216 | #ifdef MRB_NO_INIT_ARRAY_START |
mzta | 0:158c61bb030f | 217 | extern char _edata[]; |
mzta | 0:158c61bb030f | 218 | |
mzta | 0:158c61bb030f | 219 | static inline mrb_bool |
mzta | 0:158c61bb030f | 220 | mrb_ro_data_p(const char *p) |
mzta | 0:158c61bb030f | 221 | { |
mzta | 0:158c61bb030f | 222 | return _etext < p && p < _edata; |
mzta | 0:158c61bb030f | 223 | } |
mzta | 0:158c61bb030f | 224 | #else |
mzta | 0:158c61bb030f | 225 | extern char __init_array_start[]; |
mzta | 0:158c61bb030f | 226 | |
mzta | 0:158c61bb030f | 227 | static inline mrb_bool |
mzta | 0:158c61bb030f | 228 | mrb_ro_data_p(const char *p) |
mzta | 0:158c61bb030f | 229 | { |
mzta | 0:158c61bb030f | 230 | return _etext < p && p < (char*)&__init_array_start; |
mzta | 0:158c61bb030f | 231 | } |
mzta | 0:158c61bb030f | 232 | #endif |
mzta | 0:158c61bb030f | 233 | #else |
mzta | 0:158c61bb030f | 234 | # define mrb_ro_data_p(p) FALSE |
mzta | 0:158c61bb030f | 235 | #endif |
mzta | 0:158c61bb030f | 236 | |
mzta | 0:158c61bb030f | 237 | #endif /* MRUBY_VALUE_H */ |
mzta | 0:158c61bb030f | 238 |