GCC Code Coverage Report


./
File: mruby/include/mruby/value.h
Date: 2024-05-21 01:02:25
Lines:
9/9
100.0%
Functions:
3/3
100.0%
Branches:
0/0
-%

Line Branch Exec Source
1 /**
2 ** @file mruby/value.h - mruby value definitions
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #ifndef MRUBY_VALUE_H
8 #define MRUBY_VALUE_H
9
10 #include "common.h"
11
12 /*
13 * mruby Value definition functions and macros.
14 */
15 MRB_BEGIN_DECL
16
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 */
23 typedef uint32_t mrb_sym;
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 */
34 #if defined(__cplusplus) || (defined(__bool_true_false_are_defined) && __bool_true_false_are_defined)
35 typedef bool mrb_bool;
36
37 # ifndef FALSE
38 # define FALSE false
39 # endif
40 # ifndef TRUE
41 # define TRUE true
42 # endif
43 #else
44 # if __STDC_VERSION__ >= 199901L
45 typedef _Bool mrb_bool;
46 # else
47 typedef uint8_t mrb_bool;
48 # endif
49
50 # ifndef FALSE
51 # define FALSE 0
52 # endif
53 # ifndef TRUE
54 # define TRUE 1
55 # endif
56 #endif
57
58 struct mrb_state;
59
60 #if defined _MSC_VER && _MSC_VER < 1800
61 # define PRIo64 "llo"
62 # define PRId64 "lld"
63 # define PRIu64 "llu"
64 # define PRIx64 "llx"
65 # define PRIo16 "ho"
66 # define PRId16 "hd"
67 # define PRIu16 "hu"
68 # define PRIx16 "hx"
69 # define PRIo32 "o"
70 # define PRId32 "d"
71 # define PRIu32 "u"
72 # define PRIx32 "x"
73 #else
74 # include <inttypes.h>
75 #endif
76
77 #if defined(MRB_INT64)
78 typedef int64_t mrb_int;
79 typedef uint64_t mrb_uint;
80 # define MRB_INT_BIT 64
81 # define MRB_INT_MIN INT64_MIN
82 # define MRB_INT_MAX INT64_MAX
83 # define MRB_PRIo PRIo64
84 # define MRB_PRId PRId64
85 # define MRB_PRIx PRIx64
86 #else
87 typedef int32_t mrb_int;
88 typedef uint32_t mrb_uint;
89 # define MRB_INT_BIT 32
90 # define MRB_INT_MIN INT32_MIN
91 # define MRB_INT_MAX INT32_MAX
92 # define MRB_PRIo PRIo32
93 # define MRB_PRId PRId32
94 # define MRB_PRIx PRIx32
95 #endif
96
97 #ifdef MRB_ENDIAN_BIG
98 # define MRB_ENDIAN_LOHI(a,b) a b
99 #else
100 # define MRB_ENDIAN_LOHI(a,b) b a
101 #endif
102
103 MRB_API mrb_bool mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np);
104 /* obsolete; do not use mrb_int_read() */
105 MRB_API mrb_int mrb_int_read(const char*, const char*, char**);
106 #ifndef MRB_NO_FLOAT
107 MRB_API mrb_bool mrb_read_float(const char *p, char **endp, double *fp);
108 /* obsolete; do not use mrb_float_read() */
109 MRB_API double mrb_float_read(const char *p, char **endp);
110 #ifdef MRB_USE_FLOAT32
111 typedef float mrb_float;
112 #else
113 typedef double mrb_float;
114 #endif
115 #endif
116
117 #if defined _MSC_VER && _MSC_VER < 1900
118 MRB_API int mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg);
119 MRB_API int mrb_msvc_snprintf(char *s, size_t n, const char *format, ...);
120 # define vsnprintf(s, n, format, arg) mrb_msvc_vsnprintf(s, n, format, arg)
121 # define snprintf(s, n, format, ...) mrb_msvc_snprintf(s, n, format, __VA_ARGS__)
122 # if _MSC_VER < 1800 && !defined MRB_NO_FLOAT
123 # define isfinite(n) _finite(n)
124 # define isnan _isnan
125 # define isinf(n) (!_finite(n) && !_isnan(n))
126 # define signbit(n) (_copysign(1.0, (n)) < 0.0)
127 static const unsigned int IEEE754_INFINITY_BITS_SINGLE = 0x7F800000;
128 # define INFINITY (*(float*)&IEEE754_INFINITY_BITS_SINGLE)
129 # define NAN ((float)(INFINITY - INFINITY))
130 # endif
131 #endif
132
133 #define MRB_VTYPE_FOREACH(f) \
134 /* mrb_vtype */ /* c type */ /* ruby class */ \
135 f(MRB_TT_FALSE, void, "false") \
136 f(MRB_TT_TRUE, void, "true") \
137 f(MRB_TT_SYMBOL, void, "Symbol") \
138 f(MRB_TT_UNDEF, void, "undefined") \
139 f(MRB_TT_FREE, void, "free") \
140 f(MRB_TT_FLOAT, struct RFloat, "Float") \
141 f(MRB_TT_INTEGER, struct RInteger, "Integer") \
142 f(MRB_TT_CPTR, struct RCptr, "cptr") \
143 f(MRB_TT_OBJECT, struct RObject, "Object") \
144 f(MRB_TT_CLASS, struct RClass, "Class") \
145 f(MRB_TT_MODULE, struct RClass, "Module") \
146 f(MRB_TT_ICLASS, struct RClass, "iClass") \
147 f(MRB_TT_SCLASS, struct RClass, "SClass") \
148 f(MRB_TT_PROC, struct RProc, "Proc") \
149 f(MRB_TT_ARRAY, struct RArray, "Array") \
150 f(MRB_TT_HASH, struct RHash, "Hash") \
151 f(MRB_TT_STRING, struct RString, "String") \
152 f(MRB_TT_RANGE, struct RRange, "Range") \
153 f(MRB_TT_EXCEPTION, struct RException, "Exception") \
154 f(MRB_TT_ENV, struct REnv, "env") \
155 f(MRB_TT_CDATA, struct RData, "C data") \
156 f(MRB_TT_FIBER, struct RFiber, "Fiber") \
157 f(MRB_TT_STRUCT, struct RArray, "Struct") \
158 f(MRB_TT_ISTRUCT, struct RIStruct, "istruct") \
159 f(MRB_TT_BREAK, struct RBreak, "break") \
160 f(MRB_TT_COMPLEX, struct RComplex, "Complex") \
161 f(MRB_TT_RATIONAL, struct RRational, "Rational") \
162 f(MRB_TT_BIGINT, struct RBigint, "Integer")
163
164 enum mrb_vtype {
165 #define MRB_VTYPE_DEFINE(tt, type, name) tt,
166 MRB_VTYPE_FOREACH(MRB_VTYPE_DEFINE)
167 #undef MRB_VTYPE_DEFINE
168 MRB_TT_MAXDEFINE
169 };
170
171 /* obsolete name for MRB_TT_CDATA */
172 #define MRB_TT_DATA MRB_TT_CDATA
173
174 #define MRB_VTYPE_TYPEOF(tt) MRB_TYPEOF_##tt
175
176 #define MRB_VTYPE_TYPEDEF(tt, type, name) typedef type MRB_VTYPE_TYPEOF(tt);
177 MRB_VTYPE_FOREACH(MRB_VTYPE_TYPEDEF)
178 #undef MRB_VTYPE_TYPEDEF
179
180 /* for compatibility */
181 #define MRB_TT_FIXNUM MRB_TT_INTEGER
182
183 #include <mruby/object.h>
184
185 #ifdef MRB_DOCUMENTATION_BLOCK
186
187 /**
188 * @abstract
189 * mruby value boxing.
190 *
191 * Actual implementation depends on configured boxing type.
192 *
193 * @see mruby/boxing_word.h Word boxing representation (Default)
194 * @see mruby/boxing_no.h No boxing representation
195 * @see mruby/boxing_nan.h Boxed double representation
196 */
197 typedef void mrb_value;
198
199 #endif
200
201 #if defined(MRB_WORD_BOXING) || (defined(MRB_NAN_BOXING) && defined(MRB_64BIT))
202 struct RCptr {
203 MRB_OBJECT_HEADER;
204 void *p;
205 };
206 #endif
207
208 #if defined(MRB_NAN_BOXING)
209 #include "boxing_nan.h"
210 #elif defined(MRB_WORD_BOXING)
211 #include "boxing_word.h"
212 #else
213 #include "boxing_no.h"
214 #endif
215
216 #if INTPTR_MAX < MRB_INT_MAX
217 typedef intptr_t mrb_ssize;
218 # define MRB_SSIZE_MAX INTPTR_MAX
219 #else
220 typedef mrb_int mrb_ssize;
221 # define MRB_SSIZE_MAX MRB_INT_MAX
222 #endif
223
224 #ifndef mrb_immediate_p
225 #define mrb_immediate_p(o) (mrb_type(o) <= MRB_TT_CPTR)
226 #endif
227 #ifndef mrb_integer_p
228 #define mrb_integer_p(o) (mrb_type(o) == MRB_TT_INTEGER)
229 #endif
230 #ifndef mrb_fixnum_p
231 #define mrb_fixnum_p(o) mrb_integer_p(o)
232 #endif
233 #ifndef mrb_symbol_p
234 #define mrb_symbol_p(o) (mrb_type(o) == MRB_TT_SYMBOL)
235 #endif
236 #ifndef mrb_undef_p
237 #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF)
238 #endif
239 #ifndef mrb_nil_p
240 #define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !mrb_fixnum(o))
241 #endif
242 #ifndef mrb_false_p
243 #define mrb_false_p(o) (mrb_type(o) == MRB_TT_FALSE && !!mrb_fixnum(o))
244 #endif
245 #ifndef mrb_true_p
246 #define mrb_true_p(o) (mrb_type(o) == MRB_TT_TRUE)
247 #endif
248 #ifndef mrb_float_p
249 #ifndef MRB_NO_FLOAT
250 #define mrb_float_p(o) (mrb_type(o) == MRB_TT_FLOAT)
251 #else
252 #define mrb_float_p(o) FALSE
253 #endif
254 #endif
255 #ifndef mrb_array_p
256 #define mrb_array_p(o) (mrb_type(o) == MRB_TT_ARRAY)
257 #endif
258 #ifndef mrb_string_p
259 #define mrb_string_p(o) (mrb_type(o) == MRB_TT_STRING)
260 #endif
261 #ifndef mrb_hash_p
262 #define mrb_hash_p(o) (mrb_type(o) == MRB_TT_HASH)
263 #endif
264 #ifndef mrb_cptr_p
265 #define mrb_cptr_p(o) (mrb_type(o) == MRB_TT_CPTR)
266 #endif
267 #ifndef mrb_exception_p
268 #define mrb_exception_p(o) (mrb_type(o) == MRB_TT_EXCEPTION)
269 #endif
270 #ifndef mrb_free_p
271 #define mrb_free_p(o) (mrb_type(o) == MRB_TT_FREE)
272 #endif
273 #ifndef mrb_object_p
274 #define mrb_object_p(o) (mrb_type(o) == MRB_TT_OBJECT)
275 #endif
276 #ifndef mrb_class_p
277 #define mrb_class_p(o) (mrb_type(o) == MRB_TT_CLASS)
278 #endif
279 #ifndef mrb_module_p
280 #define mrb_module_p(o) (mrb_type(o) == MRB_TT_MODULE)
281 #endif
282 #ifndef mrb_iclass_p
283 #define mrb_iclass_p(o) (mrb_type(o) == MRB_TT_ICLASS)
284 #endif
285 #ifndef mrb_sclass_p
286 #define mrb_sclass_p(o) (mrb_type(o) == MRB_TT_SCLASS)
287 #endif
288 #ifndef mrb_proc_p
289 #define mrb_proc_p(o) (mrb_type(o) == MRB_TT_PROC)
290 #endif
291 #ifndef mrb_range_p
292 #define mrb_range_p(o) (mrb_type(o) == MRB_TT_RANGE)
293 #endif
294 #ifndef mrb_env_p
295 #define mrb_env_p(o) (mrb_type(o) == MRB_TT_ENV)
296 #endif
297 #ifndef mrb_data_p
298 #define mrb_data_p(o) (mrb_type(o) == MRB_TT_CDATA)
299 #endif
300 #ifndef mrb_fiber_p
301 #define mrb_fiber_p(o) (mrb_type(o) == MRB_TT_FIBER)
302 #endif
303 #ifndef mrb_istruct_p
304 #define mrb_istruct_p(o) (mrb_type(o) == MRB_TT_ISTRUCT)
305 #endif
306 #ifndef mrb_break_p
307 #define mrb_break_p(o) (mrb_type(o) == MRB_TT_BREAK)
308 #endif
309 #ifndef mrb_bool
310 #define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE)
311 #endif
312 #define mrb_test(o) mrb_bool(o)
313 #ifndef mrb_bigint_p
314 #define mrb_bigint_p(o) (mrb_type(o) == MRB_TT_BIGINT)
315 #endif
316
317 /**
318 * Returns a float in Ruby.
319 *
320 * Takes a float and boxes it into an mrb_value
321 */
322 #ifndef MRB_NO_FLOAT
323 MRB_INLINE mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
324 {
325 mrb_value v;
326 (void) mrb;
327 SET_FLOAT_VALUE(mrb, v, f);
328 return v;
329 }
330 #endif
331
332 MRB_INLINE mrb_value
333 mrb_cptr_value(struct mrb_state *mrb, void *p)
334 {
335 mrb_value v;
336 (void) mrb;
337 SET_CPTR_VALUE(mrb,v,p);
338 return v;
339 }
340
341 /**
342 * Returns an integer in Ruby.
343 */
344 MRB_INLINE mrb_value mrb_int_value(struct mrb_state *mrb, mrb_int i)
345 {
346 mrb_value v;
347 SET_INT_VALUE(mrb, v, i);
348 return v;
349 }
350
351 32 MRB_INLINE mrb_value mrb_fixnum_value(mrb_int i)
352 {
353 mrb_value v;
354 32 SET_FIXNUM_VALUE(v, i);
355 32 return v;
356 }
357
358 MRB_INLINE mrb_value
359 mrb_symbol_value(mrb_sym i)
360 {
361 mrb_value v;
362 SET_SYM_VALUE(v, i);
363 return v;
364 }
365
366 MRB_INLINE mrb_value
367 10 mrb_obj_value(void *p)
368 {
369 mrb_value v;
370 10 SET_OBJ_VALUE(v, (struct RBasic*)p);
371 10 return v;
372 }
373
374 /**
375 * Get a nil mrb_value object.
376 *
377 * @return
378 * nil mrb_value object reference.
379 */
380 47 MRB_INLINE mrb_value mrb_nil_value(void)
381 {
382 mrb_value v;
383 47 SET_NIL_VALUE(v);
384 47 return v;
385 }
386
387 /**
388 * Returns false in Ruby.
389 */
390 MRB_INLINE mrb_value mrb_false_value(void)
391 {
392 mrb_value v;
393 SET_FALSE_VALUE(v);
394 return v;
395 }
396
397 /**
398 * Returns true in Ruby.
399 */
400 MRB_INLINE mrb_value mrb_true_value(void)
401 {
402 mrb_value v;
403 SET_TRUE_VALUE(v);
404 return v;
405 }
406
407 MRB_INLINE mrb_value
408 mrb_bool_value(mrb_bool boolean)
409 {
410 mrb_value v;
411 SET_BOOL_VALUE(v, boolean);
412 return v;
413 }
414
415 MRB_INLINE mrb_value
416 mrb_undef_value(void)
417 {
418 mrb_value v;
419 SET_UNDEF_VALUE(v);
420 return v;
421 }
422
423 #if defined(MRB_USE_CUSTOM_RO_DATA_P)
424 /* If you define `MRB_USE_CUSTOM_RO_DATA_P`, you must implement `mrb_ro_data_p()`. */
425 mrb_bool mrb_ro_data_p(const char *p);
426 #elif !defined(MRB_NO_DEFAULT_RO_DATA_P)
427 #if defined(MRB_USE_ETEXT_RO_DATA_P)
428 #define MRB_LINK_TIME_RO_DATA_P
429 extern char etext, edata;
430 static inline mrb_bool
431 mrb_ro_data_p(const char *p)
432 {
433 return &etext < p && p < &edata;
434 }
435 #elif defined(__APPLE__)
436 #define MRB_LINK_TIME_RO_DATA_P
437 #include <mach-o/getsect.h>
438 #include <crt_externs.h> // for _NSGetMachExecuteHeader
439 static inline mrb_bool
440 mrb_ro_data_p(const char *p)
441 {
442 #ifdef __LP64__
443 struct mach_header_64 *mhp;
444 #else
445 struct mach_header *mhp;
446 #endif
447 mhp = _NSGetMachExecuteHeader();
448 unsigned long textsize, datasize;
449 char *text = (char*)getsegmentdata(mhp, SEG_TEXT, &textsize);
450 char *data = (char*)getsegmentdata(mhp, SEG_DATA, &datasize);
451 return text + textsize < p && p < data + datasize;
452 }
453 #endif /* Linux or macOS */
454 #endif /* MRB_NO_DEFAULT_RO_DATA_P */
455 #ifndef MRB_LINK_TIME_RO_DATA_P
456 # define mrb_ro_data_p(p) FALSE
457 #endif
458
459 MRB_END_DECL
460
461 #endif /* MRUBY_VALUE_H */
462