2 ** @file mruby/value.h - mruby value definitions
4 ** See Copyright Notice in mruby.h
13 * MRuby Value definition functions and macros.
21 * You can create an mrb_sym by simply using mrb_str_intern() or mrb_intern_cstr()
23 typedef uint32_t mrb_sym;
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()
34 typedef uint8_t mrb_bool;
37 #if defined _MSC_VER && _MSC_VER < 1800
51 # include <inttypes.h>
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)
59 # define MRB_PRIo PRIo64
60 # define MRB_PRId PRId64
61 # define MRB_PRIx PRIx64
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)
67 # define MRB_PRIo PRIo32
68 # define MRB_PRId PRId32
69 # define MRB_PRIx PRIx32
73 # define MRB_ENDIAN_LOHI(a,b) a b
75 # define MRB_ENDIAN_LOHI(a,b) b a
78 #ifndef MRB_WITHOUT_FLOAT
79 MRB_API double mrb_float_read(const char*, char**);
81 typedef float mrb_float;
83 typedef double mrb_float;
87 #if defined _MSC_VER && _MSC_VER < 1900
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__)
93 # if _MSC_VER < 1800 && !defined MRB_WITHOUT_FLOAT
95 # define isfinite(n) _finite(n)
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))
133 #include <mruby/object.h>
135 #ifdef MRB_DOCUMENTATION_BLOCK
139 * MRuby value boxing.
141 * Actual implementation depends on configured boxing type.
143 * @see mruby/boxing_no.h Default boxing representation
144 * @see mruby/boxing_word.h Word representation
145 * @see mruby/boxing_nan.h Boxed double representation
147 typedef void mrb_value;
151 #if defined(MRB_WORD_BOXING) || (defined(MRB_NAN_BOXING) && defined(MRB_64BIT))
158 #if defined(MRB_NAN_BOXING)
159 #include "boxing_nan.h"
160 #elif defined(MRB_WORD_BOXING)
161 #include "boxing_word.h"
163 #include "boxing_no.h"
166 #define MRB_SYMBOL_BIT (sizeof(mrb_sym) * CHAR_BIT - MRB_SYMBOL_SHIFT)
167 #define MRB_SYMBOL_MAX (UINT32_MAX >> MRB_SYMBOL_SHIFT)
169 #if INTPTR_MAX < MRB_INT_MAX
170 typedef intptr_t mrb_ssize;
171 # define MRB_SSIZE_MAX (INTPTR_MAX>>MRB_FIXNUM_SHIFT)
173 typedef mrb_int mrb_ssize;
174 # define MRB_SSIZE_MAX MRB_INT_MAX
177 #ifndef mrb_immediate_p
178 #define mrb_immediate_p(o) (mrb_type(o) < MRB_TT_FREE)
181 #define mrb_fixnum_p(o) (mrb_type(o) == MRB_TT_FIXNUM)
184 #define mrb_symbol_p(o) (mrb_type(o) == MRB_TT_SYMBOL)
187 #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF)
190 #define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !mrb_fixnum(o))
193 #define mrb_false_p(o) (mrb_type(o) == MRB_TT_FALSE && !!mrb_fixnum(o))
196 #define mrb_true_p(o) (mrb_type(o) == MRB_TT_TRUE)
198 #ifndef MRB_WITHOUT_FLOAT
200 #define mrb_float_p(o) (mrb_type(o) == MRB_TT_FLOAT)
204 #define mrb_array_p(o) (mrb_type(o) == MRB_TT_ARRAY)
207 #define mrb_string_p(o) (mrb_type(o) == MRB_TT_STRING)
210 #define mrb_hash_p(o) (mrb_type(o) == MRB_TT_HASH)
213 #define mrb_cptr_p(o) (mrb_type(o) == MRB_TT_CPTR)
215 #ifndef mrb_exception_p
216 #define mrb_exception_p(o) (mrb_type(o) == MRB_TT_EXCEPTION)
219 #define mrb_free_p(o) (mrb_type(o) == MRB_TT_FREE)
222 #define mrb_object_p(o) (mrb_type(o) == MRB_TT_OBJECT)
225 #define mrb_class_p(o) (mrb_type(o) == MRB_TT_CLASS)
228 #define mrb_module_p(o) (mrb_type(o) == MRB_TT_MODULE)
231 #define mrb_iclass_p(o) (mrb_type(o) == MRB_TT_ICLASS)
234 #define mrb_sclass_p(o) (mrb_type(o) == MRB_TT_SCLASS)
237 #define mrb_proc_p(o) (mrb_type(o) == MRB_TT_PROC)
240 #define mrb_range_p(o) (mrb_type(o) == MRB_TT_RANGE)
243 #define mrb_env_p(o) (mrb_type(o) == MRB_TT_ENV)
246 #define mrb_data_p(o) (mrb_type(o) == MRB_TT_DATA)
249 #define mrb_fiber_p(o) (mrb_type(o) == MRB_TT_FIBER)
251 #ifndef mrb_istruct_p
252 #define mrb_istruct_p(o) (mrb_type(o) == MRB_TT_ISTRUCT)
255 #define mrb_break_p(o) (mrb_type(o) == MRB_TT_BREAK)
258 #define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE)
260 #define mrb_test(o) mrb_bool(o)
263 * Returns a float in Ruby.
265 * Takes a float and boxes it into an mrb_value
267 #ifndef MRB_WITHOUT_FLOAT
268 MRB_INLINE mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
272 SET_FLOAT_VALUE(mrb, v, f);
278 mrb_cptr_value(struct mrb_state *mrb, void *p)
282 SET_CPTR_VALUE(mrb,v,p);
287 * Returns a fixnum in Ruby.
289 * Takes an integer and boxes it into an mrb_value
291 MRB_INLINE mrb_value mrb_fixnum_value(mrb_int i)
299 mrb_symbol_value(mrb_sym i)
307 mrb_obj_value(void *p)
310 SET_OBJ_VALUE(v, (struct RBasic*)p);
311 mrb_assert(p == mrb_ptr(v));
312 mrb_assert(((struct RBasic*)p)->tt == mrb_type(v));
317 * Get a nil mrb_value object.
320 * nil mrb_value object reference.
322 MRB_INLINE mrb_value mrb_nil_value(void)
330 * Returns false in Ruby.
332 MRB_INLINE mrb_value mrb_false_value(void)
340 * Returns true in Ruby.
342 MRB_INLINE mrb_value mrb_true_value(void)
350 mrb_bool_value(mrb_bool boolean)
353 SET_BOOL_VALUE(v, boolean);
358 mrb_undef_value(void)
365 #if defined(MRB_USE_ETEXT_EDATA) && !defined(MRB_USE_LINK_TIME_RO_DATA_P)
367 # warning MRB_USE_ETEXT_EDATA is deprecated. Define MRB_USE_LINK_TIME_RO_DATA_P instead.
369 # define MRB_USE_LINK_TIME_RO_DATA_P
372 #if defined(MRB_USE_CUSTOM_RO_DATA_P)
373 /* If you define `MRB_USE_CUSTOM_RO_DATA_P`, you must implement `mrb_ro_data_p()`. */
374 mrb_bool mrb_ro_data_p(const char *p);
375 #elif defined(MRB_USE_LINK_TIME_RO_DATA_P)
376 extern char __ehdr_start[];
377 extern char __init_array_start[];
379 static inline mrb_bool
380 mrb_ro_data_p(const char *p)
382 return __ehdr_start < p && p < __init_array_start;
385 # define mrb_ro_data_p(p) FALSE
390 #endif /* MRUBY_VALUE_H */