[439] | 1 | /**
|
---|
| 2 | ** @file mruby/value.h - mruby value definitions
|
---|
[270] | 3 | **
|
---|
| 4 | ** See Copyright Notice in mruby.h
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #ifndef MRUBY_VALUE_H
|
---|
| 8 | #define MRUBY_VALUE_H
|
---|
| 9 |
|
---|
[331] | 10 | #include "common.h"
|
---|
[270] | 11 |
|
---|
[439] | 12 | /*
|
---|
[270] | 13 | * MRuby Value definition functions and macros.
|
---|
| 14 | */
|
---|
| 15 | MRB_BEGIN_DECL
|
---|
| 16 |
|
---|
[439] | 17 | /**
|
---|
| 18 | * mruby Symbol.
|
---|
| 19 | * @class mrb_sym
|
---|
| 20 | *
|
---|
| 21 | * You can create an mrb_sym by simply using mrb_str_intern() or mrb_intern_cstr()
|
---|
| 22 | */
|
---|
[270] | 23 | typedef uint32_t mrb_sym;
|
---|
[439] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * mruby Boolean.
|
---|
| 27 | * @class mrb_bool
|
---|
| 28 | *
|
---|
| 29 | *
|
---|
| 30 | * Used internally to represent boolean. Can be TRUE or FALSE.
|
---|
| 31 | * Not to be confused with Ruby's boolean classes, which can be
|
---|
| 32 | * obtained using mrb_false_value() and mrb_true_value()
|
---|
| 33 | */
|
---|
[270] | 34 | typedef uint8_t mrb_bool;
|
---|
| 35 | struct mrb_state;
|
---|
| 36 |
|
---|
[331] | 37 | #if defined _MSC_VER && _MSC_VER < 1800
|
---|
| 38 | # define PRIo64 "llo"
|
---|
| 39 | # define PRId64 "lld"
|
---|
[439] | 40 | # define PRIu64 "llu"
|
---|
[331] | 41 | # define PRIx64 "llx"
|
---|
| 42 | # define PRIo16 "ho"
|
---|
| 43 | # define PRId16 "hd"
|
---|
[439] | 44 | # define PRIu16 "hu"
|
---|
[331] | 45 | # define PRIx16 "hx"
|
---|
| 46 | # define PRIo32 "o"
|
---|
| 47 | # define PRId32 "d"
|
---|
[439] | 48 | # define PRIu32 "u"
|
---|
[331] | 49 | # define PRIx32 "x"
|
---|
| 50 | #else
|
---|
| 51 | # include <inttypes.h>
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
[270] | 54 | #if defined(MRB_INT64)
|
---|
| 55 | typedef int64_t mrb_int;
|
---|
| 56 | # define MRB_INT_BIT 64
|
---|
| 57 | # define MRB_INT_MIN (INT64_MIN>>MRB_FIXNUM_SHIFT)
|
---|
| 58 | # define MRB_INT_MAX (INT64_MAX>>MRB_FIXNUM_SHIFT)
|
---|
[331] | 59 | # define MRB_PRIo PRIo64
|
---|
| 60 | # define MRB_PRId PRId64
|
---|
| 61 | # define MRB_PRIx PRIx64
|
---|
[270] | 62 | #else
|
---|
| 63 | typedef int32_t mrb_int;
|
---|
| 64 | # define MRB_INT_BIT 32
|
---|
| 65 | # define MRB_INT_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT)
|
---|
| 66 | # define MRB_INT_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT)
|
---|
[331] | 67 | # define MRB_PRIo PRIo32
|
---|
| 68 | # define MRB_PRId PRId32
|
---|
| 69 | # define MRB_PRIx PRIx32
|
---|
[270] | 70 | #endif
|
---|
| 71 |
|
---|
[439] | 72 | #ifdef MRB_ENDIAN_BIG
|
---|
| 73 | # define MRB_ENDIAN_LOHI(a,b) a b
|
---|
| 74 | #else
|
---|
| 75 | # define MRB_ENDIAN_LOHI(a,b) b a
|
---|
| 76 | #endif
|
---|
[331] | 77 |
|
---|
[439] | 78 | #ifndef MRB_WITHOUT_FLOAT
|
---|
[331] | 79 | MRB_API double mrb_float_read(const char*, char**);
|
---|
[270] | 80 | #ifdef MRB_USE_FLOAT
|
---|
| 81 | typedef float mrb_float;
|
---|
| 82 | #else
|
---|
| 83 | typedef double mrb_float;
|
---|
| 84 | #endif
|
---|
[439] | 85 | #endif
|
---|
[270] | 86 |
|
---|
| 87 | #if defined _MSC_VER && _MSC_VER < 1900
|
---|
| 88 | # include <stdarg.h>
|
---|
| 89 | MRB_API int mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg);
|
---|
| 90 | MRB_API int mrb_msvc_snprintf(char *s, size_t n, const char *format, ...);
|
---|
| 91 | # define vsnprintf(s, n, format, arg) mrb_msvc_vsnprintf(s, n, format, arg)
|
---|
| 92 | # define snprintf(s, n, format, ...) mrb_msvc_snprintf(s, n, format, __VA_ARGS__)
|
---|
[439] | 93 | # if _MSC_VER < 1800 && !defined MRB_WITHOUT_FLOAT
|
---|
[270] | 94 | # include <float.h>
|
---|
| 95 | # define isfinite(n) _finite(n)
|
---|
| 96 | # define isnan _isnan
|
---|
| 97 | # define isinf(n) (!_finite(n) && !_isnan(n))
|
---|
| 98 | # define signbit(n) (_copysign(1.0, (n)) < 0.0)
|
---|
| 99 | static const unsigned int IEEE754_INFINITY_BITS_SINGLE = 0x7F800000;
|
---|
| 100 | # define INFINITY (*(float *)&IEEE754_INFINITY_BITS_SINGLE)
|
---|
| 101 | # define NAN ((float)(INFINITY - INFINITY))
|
---|
| 102 | # endif
|
---|
| 103 | #endif
|
---|
| 104 |
|
---|
| 105 | enum mrb_vtype {
|
---|
| 106 | MRB_TT_FALSE = 0, /* 0 */
|
---|
[439] | 107 | MRB_TT_TRUE, /* 1 */
|
---|
| 108 | MRB_TT_FLOAT, /* 2 */
|
---|
[270] | 109 | MRB_TT_FIXNUM, /* 3 */
|
---|
| 110 | MRB_TT_SYMBOL, /* 4 */
|
---|
| 111 | MRB_TT_UNDEF, /* 5 */
|
---|
[439] | 112 | MRB_TT_CPTR, /* 6 */
|
---|
| 113 | MRB_TT_FREE, /* 7 */
|
---|
[270] | 114 | MRB_TT_OBJECT, /* 8 */
|
---|
| 115 | MRB_TT_CLASS, /* 9 */
|
---|
| 116 | MRB_TT_MODULE, /* 10 */
|
---|
| 117 | MRB_TT_ICLASS, /* 11 */
|
---|
| 118 | MRB_TT_SCLASS, /* 12 */
|
---|
| 119 | MRB_TT_PROC, /* 13 */
|
---|
| 120 | MRB_TT_ARRAY, /* 14 */
|
---|
| 121 | MRB_TT_HASH, /* 15 */
|
---|
| 122 | MRB_TT_STRING, /* 16 */
|
---|
| 123 | MRB_TT_RANGE, /* 17 */
|
---|
| 124 | MRB_TT_EXCEPTION, /* 18 */
|
---|
| 125 | MRB_TT_FILE, /* 19 */
|
---|
| 126 | MRB_TT_ENV, /* 20 */
|
---|
| 127 | MRB_TT_DATA, /* 21 */
|
---|
| 128 | MRB_TT_FIBER, /* 22 */
|
---|
[331] | 129 | MRB_TT_ISTRUCT, /* 23 */
|
---|
| 130 | MRB_TT_BREAK, /* 24 */
|
---|
| 131 | MRB_TT_MAXDEFINE /* 25 */
|
---|
[270] | 132 | };
|
---|
| 133 |
|
---|
[331] | 134 | #include <mruby/object.h>
|
---|
[270] | 135 |
|
---|
| 136 | #ifdef MRB_DOCUMENTATION_BLOCK
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * @abstract
|
---|
| 140 | * MRuby value boxing.
|
---|
| 141 | *
|
---|
| 142 | * Actual implementation depends on configured boxing type.
|
---|
| 143 | *
|
---|
| 144 | * @see mruby/boxing_no.h Default boxing representation
|
---|
| 145 | * @see mruby/boxing_word.h Word representation
|
---|
| 146 | * @see mruby/boxing_nan.h Boxed double representation
|
---|
| 147 | */
|
---|
| 148 | typedef void mrb_value;
|
---|
| 149 |
|
---|
| 150 | #endif
|
---|
| 151 |
|
---|
| 152 | #if defined(MRB_NAN_BOXING)
|
---|
| 153 | #include "boxing_nan.h"
|
---|
| 154 | #elif defined(MRB_WORD_BOXING)
|
---|
| 155 | #include "boxing_word.h"
|
---|
| 156 | #else
|
---|
| 157 | #include "boxing_no.h"
|
---|
| 158 | #endif
|
---|
| 159 |
|
---|
[439] | 160 | #define MRB_SYMBOL_BIT (sizeof(mrb_sym) * CHAR_BIT - MRB_SYMBOL_SHIFT)
|
---|
| 161 | #define MRB_SYMBOL_MAX (UINT32_MAX >> MRB_SYMBOL_SHIFT)
|
---|
| 162 |
|
---|
| 163 | #if INTPTR_MAX < MRB_INT_MAX
|
---|
| 164 | typedef intptr_t mrb_ssize;
|
---|
| 165 | # define MRB_SSIZE_MAX (INTPTR_MAX>>MRB_FIXNUM_SHIFT)
|
---|
| 166 | #else
|
---|
| 167 | typedef mrb_int mrb_ssize;
|
---|
| 168 | # define MRB_SSIZE_MAX MRB_INT_MAX
|
---|
| 169 | #endif
|
---|
| 170 |
|
---|
| 171 | #ifndef mrb_immediate_p
|
---|
| 172 | #define mrb_immediate_p(o) (mrb_type(o) < MRB_TT_FREE)
|
---|
| 173 | #endif
|
---|
[270] | 174 | #ifndef mrb_fixnum_p
|
---|
| 175 | #define mrb_fixnum_p(o) (mrb_type(o) == MRB_TT_FIXNUM)
|
---|
| 176 | #endif
|
---|
[439] | 177 | #ifndef mrb_symbol_p
|
---|
| 178 | #define mrb_symbol_p(o) (mrb_type(o) == MRB_TT_SYMBOL)
|
---|
| 179 | #endif
|
---|
[270] | 180 | #ifndef mrb_undef_p
|
---|
| 181 | #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF)
|
---|
| 182 | #endif
|
---|
| 183 | #ifndef mrb_nil_p
|
---|
| 184 | #define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !mrb_fixnum(o))
|
---|
| 185 | #endif
|
---|
[439] | 186 | #ifndef mrb_false_p
|
---|
| 187 | #define mrb_false_p(o) (mrb_type(o) == MRB_TT_FALSE && !!mrb_fixnum(o))
|
---|
[270] | 188 | #endif
|
---|
[439] | 189 | #ifndef mrb_true_p
|
---|
| 190 | #define mrb_true_p(o) (mrb_type(o) == MRB_TT_TRUE)
|
---|
| 191 | #endif
|
---|
| 192 | #ifndef MRB_WITHOUT_FLOAT
|
---|
| 193 | #ifndef mrb_float_p
|
---|
[270] | 194 | #define mrb_float_p(o) (mrb_type(o) == MRB_TT_FLOAT)
|
---|
[439] | 195 | #endif
|
---|
| 196 | #endif
|
---|
| 197 | #ifndef mrb_array_p
|
---|
[270] | 198 | #define mrb_array_p(o) (mrb_type(o) == MRB_TT_ARRAY)
|
---|
[439] | 199 | #endif
|
---|
| 200 | #ifndef mrb_string_p
|
---|
[270] | 201 | #define mrb_string_p(o) (mrb_type(o) == MRB_TT_STRING)
|
---|
[439] | 202 | #endif
|
---|
| 203 | #ifndef mrb_hash_p
|
---|
[270] | 204 | #define mrb_hash_p(o) (mrb_type(o) == MRB_TT_HASH)
|
---|
[439] | 205 | #endif
|
---|
| 206 | #ifndef mrb_cptr_p
|
---|
[270] | 207 | #define mrb_cptr_p(o) (mrb_type(o) == MRB_TT_CPTR)
|
---|
[439] | 208 | #endif
|
---|
| 209 | #ifndef mrb_exception_p
|
---|
[270] | 210 | #define mrb_exception_p(o) (mrb_type(o) == MRB_TT_EXCEPTION)
|
---|
[439] | 211 | #endif
|
---|
| 212 | #ifndef mrb_free_p
|
---|
| 213 | #define mrb_free_p(o) (mrb_type(o) == MRB_TT_FREE)
|
---|
| 214 | #endif
|
---|
| 215 | #ifndef mrb_object_p
|
---|
| 216 | #define mrb_object_p(o) (mrb_type(o) == MRB_TT_OBJECT)
|
---|
| 217 | #endif
|
---|
| 218 | #ifndef mrb_class_p
|
---|
| 219 | #define mrb_class_p(o) (mrb_type(o) == MRB_TT_CLASS)
|
---|
| 220 | #endif
|
---|
| 221 | #ifndef mrb_module_p
|
---|
| 222 | #define mrb_module_p(o) (mrb_type(o) == MRB_TT_MODULE)
|
---|
| 223 | #endif
|
---|
| 224 | #ifndef mrb_iclass_p
|
---|
| 225 | #define mrb_iclass_p(o) (mrb_type(o) == MRB_TT_ICLASS)
|
---|
| 226 | #endif
|
---|
| 227 | #ifndef mrb_sclass_p
|
---|
| 228 | #define mrb_sclass_p(o) (mrb_type(o) == MRB_TT_SCLASS)
|
---|
| 229 | #endif
|
---|
| 230 | #ifndef mrb_proc_p
|
---|
| 231 | #define mrb_proc_p(o) (mrb_type(o) == MRB_TT_PROC)
|
---|
| 232 | #endif
|
---|
| 233 | #ifndef mrb_range_p
|
---|
| 234 | #define mrb_range_p(o) (mrb_type(o) == MRB_TT_RANGE)
|
---|
| 235 | #endif
|
---|
| 236 | #ifndef mrb_file_p
|
---|
| 237 | #define mrb_file_p(o) (mrb_type(o) == MRB_TT_FILE)
|
---|
| 238 | #endif
|
---|
| 239 | #ifndef mrb_env_p
|
---|
| 240 | #define mrb_env_p(o) (mrb_type(o) == MRB_TT_ENV)
|
---|
| 241 | #endif
|
---|
| 242 | #ifndef mrb_data_p
|
---|
| 243 | #define mrb_data_p(o) (mrb_type(o) == MRB_TT_DATA)
|
---|
| 244 | #endif
|
---|
| 245 | #ifndef mrb_fiber_p
|
---|
| 246 | #define mrb_fiber_p(o) (mrb_type(o) == MRB_TT_FIBER)
|
---|
| 247 | #endif
|
---|
| 248 | #ifndef mrb_istruct_p
|
---|
| 249 | #define mrb_istruct_p(o) (mrb_type(o) == MRB_TT_ISTRUCT)
|
---|
| 250 | #endif
|
---|
| 251 | #ifndef mrb_break_p
|
---|
| 252 | #define mrb_break_p(o) (mrb_type(o) == MRB_TT_BREAK)
|
---|
| 253 | #endif
|
---|
| 254 | #ifndef mrb_bool
|
---|
| 255 | #define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE)
|
---|
| 256 | #endif
|
---|
[270] | 257 | #define mrb_test(o) mrb_bool(o)
|
---|
| 258 |
|
---|
[439] | 259 | /**
|
---|
[270] | 260 | * Returns a float in Ruby.
|
---|
[439] | 261 | *
|
---|
| 262 | * Takes a float and boxes it into an mrb_value
|
---|
[270] | 263 | */
|
---|
[439] | 264 | #ifndef MRB_WITHOUT_FLOAT
|
---|
[270] | 265 | MRB_INLINE mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
|
---|
| 266 | {
|
---|
| 267 | mrb_value v;
|
---|
| 268 | (void) mrb;
|
---|
| 269 | SET_FLOAT_VALUE(mrb, v, f);
|
---|
| 270 | return v;
|
---|
| 271 | }
|
---|
[439] | 272 | #endif
|
---|
[270] | 273 |
|
---|
[439] | 274 | MRB_INLINE mrb_value
|
---|
[270] | 275 | mrb_cptr_value(struct mrb_state *mrb, void *p)
|
---|
| 276 | {
|
---|
| 277 | mrb_value v;
|
---|
| 278 | (void) mrb;
|
---|
| 279 | SET_CPTR_VALUE(mrb,v,p);
|
---|
| 280 | return v;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[439] | 283 | /**
|
---|
[270] | 284 | * Returns a fixnum in Ruby.
|
---|
[439] | 285 | *
|
---|
| 286 | * Takes an integer and boxes it into an mrb_value
|
---|
[270] | 287 | */
|
---|
| 288 | MRB_INLINE mrb_value mrb_fixnum_value(mrb_int i)
|
---|
| 289 | {
|
---|
| 290 | mrb_value v;
|
---|
| 291 | SET_INT_VALUE(v, i);
|
---|
| 292 | return v;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[439] | 295 | MRB_INLINE mrb_value
|
---|
[270] | 296 | mrb_symbol_value(mrb_sym i)
|
---|
| 297 | {
|
---|
| 298 | mrb_value v;
|
---|
| 299 | SET_SYM_VALUE(v, i);
|
---|
| 300 | return v;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[439] | 303 | MRB_INLINE mrb_value
|
---|
[270] | 304 | mrb_obj_value(void *p)
|
---|
| 305 | {
|
---|
| 306 | mrb_value v;
|
---|
| 307 | SET_OBJ_VALUE(v, (struct RBasic*)p);
|
---|
[331] | 308 | mrb_assert(p == mrb_ptr(v));
|
---|
| 309 | mrb_assert(((struct RBasic*)p)->tt == mrb_type(v));
|
---|
[270] | 310 | return v;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[439] | 313 | /**
|
---|
[270] | 314 | * Get a nil mrb_value object.
|
---|
| 315 | *
|
---|
| 316 | * @return
|
---|
| 317 | * nil mrb_value object reference.
|
---|
| 318 | */
|
---|
| 319 | MRB_INLINE mrb_value mrb_nil_value(void)
|
---|
| 320 | {
|
---|
| 321 | mrb_value v;
|
---|
| 322 | SET_NIL_VALUE(v);
|
---|
| 323 | return v;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[439] | 326 | /**
|
---|
[270] | 327 | * Returns false in Ruby.
|
---|
| 328 | */
|
---|
| 329 | MRB_INLINE mrb_value mrb_false_value(void)
|
---|
| 330 | {
|
---|
| 331 | mrb_value v;
|
---|
| 332 | SET_FALSE_VALUE(v);
|
---|
| 333 | return v;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[439] | 336 | /**
|
---|
[270] | 337 | * Returns true in Ruby.
|
---|
| 338 | */
|
---|
| 339 | MRB_INLINE mrb_value mrb_true_value(void)
|
---|
| 340 | {
|
---|
| 341 | mrb_value v;
|
---|
| 342 | SET_TRUE_VALUE(v);
|
---|
| 343 | return v;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[439] | 346 | MRB_INLINE mrb_value
|
---|
[270] | 347 | mrb_bool_value(mrb_bool boolean)
|
---|
| 348 | {
|
---|
| 349 | mrb_value v;
|
---|
| 350 | SET_BOOL_VALUE(v, boolean);
|
---|
| 351 | return v;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[439] | 354 | MRB_INLINE mrb_value
|
---|
[270] | 355 | mrb_undef_value(void)
|
---|
| 356 | {
|
---|
| 357 | mrb_value v;
|
---|
| 358 | SET_UNDEF_VALUE(v);
|
---|
| 359 | return v;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[439] | 362 | #if defined(MRB_USE_ETEXT_EDATA) && !defined(MRB_USE_LINK_TIME_RO_DATA_P)
|
---|
| 363 | # ifdef __GNUC__
|
---|
| 364 | # warning MRB_USE_ETEXT_EDATA is deprecated. Define MRB_USE_LINK_TIME_RO_DATA_P instead.
|
---|
| 365 | # endif
|
---|
| 366 | # define MRB_USE_LINK_TIME_RO_DATA_P
|
---|
| 367 | #endif
|
---|
[270] | 368 |
|
---|
[439] | 369 | #if defined(MRB_USE_CUSTOM_RO_DATA_P)
|
---|
| 370 | /* If you define `MRB_USE_CUSTOM_RO_DATA_P`, you must implement `mrb_ro_data_p()`. */
|
---|
| 371 | mrb_bool mrb_ro_data_p(const char *p);
|
---|
| 372 | #elif defined(MRB_USE_LINK_TIME_RO_DATA_P)
|
---|
| 373 | extern char __ehdr_start[];
|
---|
[270] | 374 | extern char __init_array_start[];
|
---|
| 375 |
|
---|
| 376 | static inline mrb_bool
|
---|
| 377 | mrb_ro_data_p(const char *p)
|
---|
| 378 | {
|
---|
[439] | 379 | return __ehdr_start < p && p < __init_array_start;
|
---|
[270] | 380 | }
|
---|
| 381 | #else
|
---|
| 382 | # define mrb_ro_data_p(p) FALSE
|
---|
| 383 | #endif
|
---|
| 384 |
|
---|
| 385 | MRB_END_DECL
|
---|
| 386 |
|
---|
| 387 | #endif /* MRUBY_VALUE_H */
|
---|