The Ruby Cross Reference

Implementation: mri jruby rubinius
Version: 1.8.7-p374 1.9.1-p431 1.9.2-p381 1.9.3-p547 2.0.0-p481 2.1.0-p0 2.1.1 2.1.2 HEAD
001 /**********************************************************************
002 
003   ruby/ruby.h -
004 
005   $Author$
006   created at: Thu Jun 10 14:26:32 JST 1993
007 
008   Copyright (C) 1993-2008 Yukihiro Matsumoto
009   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
010   Copyright (C) 2000  Information-technology Promotion Agency, Japan
011 
012 **********************************************************************/
013 
014 #ifndef RUBY_RUBY_H
015 #define RUBY_RUBY_H 1
016 
017 #if defined(__cplusplus)
018 extern "C" {
019 #if 0
020 } /* satisfy cc-mode */
021 #endif
022 #endif
023 
024 #include "ruby/config.h"
025 #ifdef RUBY_EXTCONF_H
026 #include RUBY_EXTCONF_H
027 #endif
028 
029 #include "defines.h"
030 
031 #define NORETURN_STYLE_NEW 1
032 #ifndef NORETURN
033 # define NORETURN(x) x
034 #endif
035 #ifndef DEPRECATED
036 # define DEPRECATED(x) x
037 #endif
038 #ifndef NOINLINE
039 # define NOINLINE(x) x
040 #endif
041 #ifndef UNREACHABLE
042 # define UNREACHABLE            /* unreachable */
043 #endif
044 
045 #ifdef __GNUC__
046 #define PRINTF_ARGS(decl, string_index, first_to_check) \
047   decl __attribute__((format(printf, string_index, first_to_check)))
048 #else
049 #define PRINTF_ARGS(decl, string_index, first_to_check) decl
050 #endif
051 
052 #ifdef HAVE_INTRINSICS_H
053 # include <intrinsics.h>
054 #endif
055 
056 #include <stdarg.h>
057 
058 RUBY_SYMBOL_EXPORT_BEGIN
059 
060 /* Make alloca work the best possible way.  */
061 #ifdef __GNUC__
062 # ifndef atarist
063 #  ifndef alloca
064 #   define alloca __builtin_alloca
065 #  endif
066 # endif /* atarist */
067 #else
068 # ifdef HAVE_ALLOCA_H
069 #  include <alloca.h>
070 # else
071 #  ifdef _AIX
072 #pragma alloca
073 #  else
074 #   ifndef alloca               /* predefined by HP cc +Olibcalls */
075 void *alloca();
076 #   endif
077 #  endif /* AIX */
078 # endif /* HAVE_ALLOCA_H */
079 #endif /* __GNUC__ */
080 
081 #if defined HAVE_UINTPTR_T && 0
082 typedef uintptr_t VALUE;
083 typedef uintptr_t ID;
084 # define SIGNED_VALUE intptr_t
085 # define SIZEOF_VALUE SIZEOF_UINTPTR_T
086 # undef PRI_VALUE_PREFIX
087 #elif SIZEOF_LONG == SIZEOF_VOIDP
088 typedef unsigned long VALUE;
089 typedef unsigned long ID;
090 # define SIGNED_VALUE long
091 # define SIZEOF_VALUE SIZEOF_LONG
092 # define PRI_VALUE_PREFIX "l"
093 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
094 typedef unsigned LONG_LONG VALUE;
095 typedef unsigned LONG_LONG ID;
096 # define SIGNED_VALUE LONG_LONG
097 # define LONG_LONG_VALUE 1
098 # define SIZEOF_VALUE SIZEOF_LONG_LONG
099 # define PRI_VALUE_PREFIX PRI_LL_PREFIX
100 #else
101 # error ---->> ruby requires sizeof(void*) == sizeof(long) or sizeof(LONG_LONG) to be compiled. <<----
102 #endif
103 
104 typedef char ruby_check_sizeof_int[SIZEOF_INT == sizeof(int) ? 1 : -1];
105 typedef char ruby_check_sizeof_long[SIZEOF_LONG == sizeof(long) ? 1 : -1];
106 #ifdef HAVE_LONG_LONG
107 typedef char ruby_check_sizeof_long_long[SIZEOF_LONG_LONG == sizeof(LONG_LONG) ? 1 : -1];
108 #endif
109 typedef char ruby_check_sizeof_voidp[SIZEOF_VOIDP == sizeof(void*) ? 1 : -1];
110 
111 #ifndef PRI_INT_PREFIX
112 #define PRI_INT_PREFIX ""
113 #endif
114 #ifndef PRI_LONG_PREFIX
115 #define PRI_LONG_PREFIX "l"
116 #endif
117 
118 #if SIZEOF_LONG == 8
119 #define PRI_64_PREFIX PRI_LONG_PREFIX
120 #elif SIZEOF_LONG_LONG == 8
121 #define PRI_64_PREFIX PRI_LL_PREFIX
122 #endif
123 
124 #if defined PRIdPTR && !defined PRI_VALUE_PREFIX
125 #define PRIdVALUE PRIdPTR
126 #define PRIoVALUE PRIoPTR
127 #define PRIuVALUE PRIuPTR
128 #define PRIxVALUE PRIxPTR
129 #define PRIXVALUE PRIXPTR
130 #define PRIsVALUE PRIiPTR
131 #else
132 #define PRIdVALUE PRI_VALUE_PREFIX"d"
133 #define PRIoVALUE PRI_VALUE_PREFIX"o"
134 #define PRIuVALUE PRI_VALUE_PREFIX"u"
135 #define PRIxVALUE PRI_VALUE_PREFIX"x"
136 #define PRIXVALUE PRI_VALUE_PREFIX"X"
137 #define PRIsVALUE PRI_VALUE_PREFIX"i"
138 #endif
139 #ifndef PRI_VALUE_PREFIX
140 # define PRI_VALUE_PREFIX ""
141 #endif
142 
143 #ifndef PRI_TIMET_PREFIX
144 # if SIZEOF_TIME_T == SIZEOF_INT
145 #  define PRI_TIMET_PREFIX
146 # elif SIZEOF_TIME_T == SIZEOF_LONG
147 #  define PRI_TIMET_PREFIX "l"
148 # elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
149 #  define PRI_TIMET_PREFIX PRI_LL_PREFIX
150 # endif
151 #endif
152 
153 #if defined PRI_PTRDIFF_PREFIX
154 #elif SIZEOF_PTRDIFF_T == SIZEOF_INT
155 # define PRI_PTRDIFF_PREFIX ""
156 #elif SIZEOF_PTRDIFF_T == SIZEOF_LONG
157 # define PRI_PTRDIFF_PREFIX "l"
158 #elif SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
159 # define PRI_PTRDIFF_PREFIX PRI_LL_PREFIX
160 #endif
161 #define PRIdPTRDIFF PRI_PTRDIFF_PREFIX"d"
162 #define PRIiPTRDIFF PRI_PTRDIFF_PREFIX"i"
163 #define PRIoPTRDIFF PRI_PTRDIFF_PREFIX"o"
164 #define PRIuPTRDIFF PRI_PTRDIFF_PREFIX"u"
165 #define PRIxPTRDIFF PRI_PTRDIFF_PREFIX"x"
166 #define PRIXPTRDIFF PRI_PTRDIFF_PREFIX"X"
167 
168 #if defined PRI_SIZE_PREFIX
169 #elif SIZEOF_SIZE_T == SIZEOF_INT
170 # define PRI_SIZE_PREFIX ""
171 #elif SIZEOF_SIZE_T == SIZEOF_LONG
172 # define PRI_SIZE_PREFIX "l"
173 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
174 # define PRI_SIZE_PREFIX PRI_LL_PREFIX
175 #endif
176 #define PRIdSIZE PRI_SIZE_PREFIX"d"
177 #define PRIiSIZE PRI_SIZE_PREFIX"i"
178 #define PRIoSIZE PRI_SIZE_PREFIX"o"
179 #define PRIuSIZE PRI_SIZE_PREFIX"u"
180 #define PRIxSIZE PRI_SIZE_PREFIX"x"
181 #define PRIXSIZE PRI_SIZE_PREFIX"X"
182 
183 #ifdef __STDC__
184 # include <limits.h>
185 #else
186 # ifndef LONG_MAX
187 #  ifdef HAVE_LIMITS_H
188 #   include <limits.h>
189 #  else
190     /* assuming 32bit(2's compliment) long */
191 #   define LONG_MAX 2147483647
192 #  endif
193 # endif
194 # ifndef LONG_MIN
195 #  define LONG_MIN (-LONG_MAX-1)
196 # endif
197 # ifndef CHAR_BIT
198 #  define CHAR_BIT 8
199 # endif
200 #endif
201 
202 #ifdef HAVE_LONG_LONG
203 # ifndef LLONG_MAX
204 #  ifdef LONG_LONG_MAX
205 #   define LLONG_MAX  LONG_LONG_MAX
206 #  else
207 #   ifdef _I64_MAX
208 #    define LLONG_MAX _I64_MAX
209 #   else
210     /* assuming 64bit(2's complement) long long */
211 #    define LLONG_MAX 9223372036854775807LL
212 #   endif
213 #  endif
214 # endif
215 # ifndef LLONG_MIN
216 #  ifdef LONG_LONG_MIN
217 #   define LLONG_MIN  LONG_LONG_MIN
218 #  else
219 #   ifdef _I64_MIN
220 #    define LLONG_MIN _I64_MIN
221 #   else
222 #    define LLONG_MIN (-LLONG_MAX-1)
223 #   endif
224 #  endif
225 # endif
226 #endif
227 
228 #define FIXNUM_MAX (LONG_MAX>>1)
229 #define FIXNUM_MIN RSHIFT((long)LONG_MIN,1)
230 
231 #define INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG))
232 #define LONG2FIX(i) INT2FIX(i)
233 #define rb_fix_new(v) INT2FIX(v)
234 VALUE rb_int2inum(SIGNED_VALUE);
235 
236 #define rb_int_new(v) rb_int2inum(v)
237 VALUE rb_uint2inum(VALUE);
238 
239 #define rb_uint_new(v) rb_uint2inum(v)
240 
241 #ifdef HAVE_LONG_LONG
242 VALUE rb_ll2inum(LONG_LONG);
243 #define LL2NUM(v) rb_ll2inum(v)
244 VALUE rb_ull2inum(unsigned LONG_LONG);
245 #define ULL2NUM(v) rb_ull2inum(v)
246 #endif
247 
248 #ifndef OFFT2NUM
249 #if SIZEOF_OFF_T > SIZEOF_LONG && defined(HAVE_LONG_LONG)
250 # define OFFT2NUM(v) LL2NUM(v)
251 #elif SIZEOF_OFF_T == SIZEOF_LONG
252 # define OFFT2NUM(v) LONG2NUM(v)
253 #else
254 # define OFFT2NUM(v) INT2NUM(v)
255 #endif
256 #endif
257 
258 #if SIZEOF_SIZE_T > SIZEOF_LONG && defined(HAVE_LONG_LONG)
259 # define SIZET2NUM(v) ULL2NUM(v)
260 # define SSIZET2NUM(v) LL2NUM(v)
261 #elif SIZEOF_SIZE_T == SIZEOF_LONG
262 # define SIZET2NUM(v) ULONG2NUM(v)
263 # define SSIZET2NUM(v) LONG2NUM(v)
264 #else
265 # define SIZET2NUM(v) UINT2NUM(v)
266 # define SSIZET2NUM(v) INT2NUM(v)
267 #endif
268 
269 #ifndef SIZE_MAX
270 # if SIZEOF_SIZE_T > SIZEOF_LONG && defined(HAVE_LONG_LONG)
271 #   define SIZE_MAX ULLONG_MAX
272 #   define SIZE_MIN ULLONG_MIN
273 # elif SIZEOF_SIZE_T == SIZEOF_LONG
274 #   define SIZE_MAX ULONG_MAX
275 #   define SIZE_MIN ULONG_MIN
276 # elif SIZEOF_SIZE_T == SIZEOF_INT
277 #   define SIZE_MAX UINT_MAX
278 #   define SIZE_MIN UINT_MIN
279 # else
280 #   define SIZE_MAX USHRT_MAX
281 #   define SIZE_MIN USHRT_MIN
282 # endif
283 #endif
284 
285 #ifndef SSIZE_MAX
286 # if SIZEOF_SIZE_T > SIZEOF_LONG && defined(HAVE_LONG_LONG)
287 #   define SSIZE_MAX LLONG_MAX
288 #   define SSIZE_MIN LLONG_MIN
289 # elif SIZEOF_SIZE_T == SIZEOF_LONG
290 #   define SSIZE_MAX LONG_MAX
291 #   define SSIZE_MIN LONG_MIN
292 # elif SIZEOF_SIZE_T == SIZEOF_INT
293 #   define SSIZE_MAX INT_MAX
294 #   define SSIZE_MIN INT_MIN
295 # else
296 #   define SSIZE_MAX SHRT_MAX
297 #   define SSIZE_MIN SHRT_MIN
298 # endif
299 #endif
300 
301 #if SIZEOF_INT < SIZEOF_VALUE
302 NORETURN(void rb_out_of_int(SIGNED_VALUE num));
303 #endif
304 
305 #if SIZEOF_INT < SIZEOF_LONG
306 static inline int
307 rb_long2int_inline(long n)
308 {
309     int i = (int)n;
310     if ((long)i != n)
311         rb_out_of_int(n);
312 
313     return i;
314 }
315 #define rb_long2int(n) rb_long2int_inline(n)
316 #else
317 #define rb_long2int(n) ((int)(n))
318 #endif
319 
320 #ifndef PIDT2NUM
321 #define PIDT2NUM(v) LONG2NUM(v)
322 #endif
323 #ifndef NUM2PIDT
324 #define NUM2PIDT(v) NUM2LONG(v)
325 #endif
326 #ifndef UIDT2NUM
327 #define UIDT2NUM(v) LONG2NUM(v)
328 #endif
329 #ifndef NUM2UIDT
330 #define NUM2UIDT(v) NUM2LONG(v)
331 #endif
332 #ifndef GIDT2NUM
333 #define GIDT2NUM(v) LONG2NUM(v)
334 #endif
335 #ifndef NUM2GIDT
336 #define NUM2GIDT(v) NUM2LONG(v)
337 #endif
338 #ifndef NUM2MODET
339 #define NUM2MODET(v) NUM2INT(v)
340 #endif
341 #ifndef MODET2NUM
342 #define MODET2NUM(v) INT2NUM(v)
343 #endif
344 
345 #define FIX2LONG(x) ((long)RSHIFT((SIGNED_VALUE)(x),1))
346 #define FIX2ULONG(x) ((unsigned long)FIX2LONG(x))
347 #define FIXNUM_P(f) (((int)(SIGNED_VALUE)(f))&FIXNUM_FLAG)
348 #define POSFIXABLE(f) ((f) < FIXNUM_MAX+1)
349 #define NEGFIXABLE(f) ((f) >= FIXNUM_MIN)
350 #define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f))
351 
352 #define IMMEDIATE_P(x) ((VALUE)(x) & IMMEDIATE_MASK)
353 
354 #define SYMBOL_P(x) (((VALUE)(x)&~((~(VALUE)0)<<RUBY_SPECIAL_SHIFT))==SYMBOL_FLAG)
355 #define ID2SYM(x) (((VALUE)(x)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG)
356 #define SYM2ID(x) RSHIFT((unsigned long)(x),RUBY_SPECIAL_SHIFT)
357 
358 #ifndef USE_FLONUM
359 #if SIZEOF_VALUE >= SIZEOF_DOUBLE
360 #define USE_FLONUM 1
361 #else
362 #define USE_FLONUM 0
363 #endif
364 #endif
365 
366 #if USE_FLONUM
367 #define FLONUM_P(x) ((((int)(SIGNED_VALUE)(x))&FLONUM_MASK) == FLONUM_FLAG)
368 #else
369 #define FLONUM_P(x) 0
370 #endif
371 
372 /* Module#methods, #singleton_methods and so on return Symbols */
373 #define USE_SYMBOL_AS_METHOD_NAME 1
374 
375 /*
376 !USE_FLONUM
377 -------------------------
378 ...xxxx xxx1 Fixnum
379 ...0000 1110 Symbol
380 ...0000 0000 Qfalse
381 ...0000 0010 Qtrue
382 ...0000 0100 Qnil
383 ...0000 0110 Qundef
384 
385 USE_FLONUM
386 -------------------------
387 ...xxxx xxx1 Fixnum
388 ...xxxx xx10 Flonum
389 ...0000 1100 Symbol
390 ...0000 0000 Qfalse  0x00 =  0
391 ...0000 1000 Qnil    0x08 =  8
392 ...0001 0100 Qtrue   0x14 = 20
393 ...0011 0100 Qundef  0x34 = 52
394  */
395 
396 /* special constants - i.e. non-zero and non-fixnum constants */
397 enum ruby_special_consts {
398 #if USE_FLONUM
399     RUBY_Qfalse = 0x00,
400     RUBY_Qtrue  = 0x14,
401     RUBY_Qnil   = 0x08,
402     RUBY_Qundef = 0x34,
403 
404     RUBY_IMMEDIATE_MASK = 0x07,
405     RUBY_FIXNUM_FLAG    = 0x01,
406     RUBY_FLONUM_MASK    = 0x03,
407     RUBY_FLONUM_FLAG    = 0x02,
408     RUBY_SYMBOL_FLAG    = 0x0c,
409     RUBY_SPECIAL_SHIFT  = 8
410 #else
411     RUBY_Qfalse = 0,
412     RUBY_Qtrue  = 2,
413     RUBY_Qnil   = 4,
414     RUBY_Qundef = 6,
415 
416     RUBY_IMMEDIATE_MASK = 0x03,
417     RUBY_FIXNUM_FLAG    = 0x01,
418     RUBY_FLONUM_MASK    = 0x00, /* any values ANDed with FLONUM_MASK cannot be FLONUM_FLAG */
419     RUBY_FLONUM_FLAG    = 0x02,
420     RUBY_SYMBOL_FLAG    = 0x0e,
421     RUBY_SPECIAL_SHIFT  = 8
422 #endif
423 };
424 
425 #define Qfalse ((VALUE)RUBY_Qfalse)
426 #define Qtrue  ((VALUE)RUBY_Qtrue)
427 #define Qnil   ((VALUE)RUBY_Qnil)
428 #define Qundef ((VALUE)RUBY_Qundef)     /* undefined value for placeholder */
429 #define IMMEDIATE_MASK RUBY_IMMEDIATE_MASK
430 #define FIXNUM_FLAG RUBY_FIXNUM_FLAG
431 #if USE_FLONUM
432 #define FLONUM_MASK RUBY_FLONUM_MASK
433 #define FLONUM_FLAG RUBY_FLONUM_FLAG
434 #endif
435 #define SYMBOL_FLAG RUBY_SYMBOL_FLAG
436 
437 #define RTEST(v) !(((VALUE)(v) & ~Qnil) == 0)
438 #define NIL_P(v) !((VALUE)(v) != Qnil)
439 
440 #define CLASS_OF(v) rb_class_of((VALUE)(v))
441 
442 enum ruby_value_type {
443     RUBY_T_NONE   = 0x00,
444 
445     RUBY_T_OBJECT = 0x01,
446     RUBY_T_CLASS  = 0x02,
447     RUBY_T_MODULE = 0x03,
448     RUBY_T_FLOAT  = 0x04,
449     RUBY_T_STRING = 0x05,
450     RUBY_T_REGEXP = 0x06,
451     RUBY_T_ARRAY  = 0x07,
452     RUBY_T_HASH   = 0x08,
453     RUBY_T_STRUCT = 0x09,
454     RUBY_T_BIGNUM = 0x0a,
455     RUBY_T_FILE   = 0x0b,
456     RUBY_T_DATA   = 0x0c,
457     RUBY_T_MATCH  = 0x0d,
458     RUBY_T_COMPLEX  = 0x0e,
459     RUBY_T_RATIONAL = 0x0f,
460 
461     RUBY_T_NIL    = 0x11,
462     RUBY_T_TRUE   = 0x12,
463     RUBY_T_FALSE  = 0x13,
464     RUBY_T_SYMBOL = 0x14,
465     RUBY_T_FIXNUM = 0x15,
466 
467     RUBY_T_UNDEF  = 0x1b,
468     RUBY_T_NODE   = 0x1c,
469     RUBY_T_ICLASS = 0x1d,
470     RUBY_T_ZOMBIE = 0x1e,
471 
472     RUBY_T_MASK   = 0x1f
473 };
474 
475 #define T_NONE   RUBY_T_NONE
476 #define T_NIL    RUBY_T_NIL
477 #define T_OBJECT RUBY_T_OBJECT
478 #define T_CLASS  RUBY_T_CLASS
479 #define T_ICLASS RUBY_T_ICLASS
480 #define T_MODULE RUBY_T_MODULE
481 #define T_FLOAT  RUBY_T_FLOAT
482 #define T_STRING RUBY_T_STRING
483 #define T_REGEXP RUBY_T_REGEXP
484 #define T_ARRAY  RUBY_T_ARRAY
485 #define T_HASH   RUBY_T_HASH
486 #define T_STRUCT RUBY_T_STRUCT
487 #define T_BIGNUM RUBY_T_BIGNUM
488 #define T_FILE   RUBY_T_FILE
489 #define T_FIXNUM RUBY_T_FIXNUM
490 #define T_TRUE   RUBY_T_TRUE
491 #define T_FALSE  RUBY_T_FALSE
492 #define T_DATA   RUBY_T_DATA
493 #define T_MATCH  RUBY_T_MATCH
494 #define T_SYMBOL RUBY_T_SYMBOL
495 #define T_RATIONAL RUBY_T_RATIONAL
496 #define T_COMPLEX RUBY_T_COMPLEX
497 #define T_UNDEF  RUBY_T_UNDEF
498 #define T_NODE   RUBY_T_NODE
499 #define T_ZOMBIE RUBY_T_ZOMBIE
500 #define T_MASK   RUBY_T_MASK
501 
502 #define BUILTIN_TYPE(x) (int)(((struct RBasic*)(x))->flags & T_MASK)
503 
504 static inline int rb_type(VALUE obj);
505 #define TYPE(x) rb_type((VALUE)(x))
506 
507 /* RB_GC_GUARD_PTR() is an intermediate macro, and has no effect by
508  * itself.  don't use it directly */
509 #ifdef __GNUC__
510 #define RB_GC_GUARD_PTR(ptr) \
511     __extension__ ({volatile VALUE *rb_gc_guarded_ptr = (ptr); rb_gc_guarded_ptr;})
512 #else
513 #ifdef _MSC_VER
514 #pragma optimize("", off)
515 static inline volatile VALUE *rb_gc_guarded_ptr(volatile VALUE *ptr) {return ptr;}
516 #pragma optimize("", on)
517 #else
518 volatile VALUE *rb_gc_guarded_ptr(volatile VALUE *ptr);
519 #define HAVE_RB_GC_GUARDED_PTR 1
520 #endif
521 #define RB_GC_GUARD_PTR(ptr) rb_gc_guarded_ptr(ptr)
522 #endif
523 #define RB_GC_GUARD(v) (*RB_GC_GUARD_PTR(&(v)))
524 
525 #ifdef __GNUC__
526 #define RB_UNUSED_VAR(x) x __attribute__ ((unused))
527 #else
528 #define RB_UNUSED_VAR(x) x
529 #endif
530 
531 void rb_check_type(VALUE,int);
532 #define Check_Type(v,t) rb_check_type((VALUE)(v),(t))
533 
534 VALUE rb_str_to_str(VALUE);
535 VALUE rb_string_value(volatile VALUE*);
536 char *rb_string_value_ptr(volatile VALUE*);
537 char *rb_string_value_cstr(volatile VALUE*);
538 
539 #define StringValue(v) rb_string_value(&(v))
540 #define StringValuePtr(v) rb_string_value_ptr(&(v))
541 #define StringValueCStr(v) rb_string_value_cstr(&(v))
542 
543 void rb_check_safe_obj(VALUE);
544 DEPRECATED(void rb_check_safe_str(VALUE));
545 #define SafeStringValue(v) do {\
546     StringValue(v);\
547     rb_check_safe_obj(v);\
548 } while (0)
549 /* obsolete macro - use SafeStringValue(v) */
550 #define Check_SafeStr(v) rb_check_safe_str((VALUE)(v))
551 
552 VALUE rb_str_export(VALUE);
553 #define ExportStringValue(v) do {\
554     SafeStringValue(v);\
555    (v) = rb_str_export(v);\
556 } while (0)
557 VALUE rb_str_export_locale(VALUE);
558 
559 VALUE rb_get_path(VALUE);
560 #define FilePathValue(v) (RB_GC_GUARD(v) = rb_get_path(v))
561 
562 VALUE rb_get_path_no_checksafe(VALUE);
563 #define FilePathStringValue(v) ((v) = rb_get_path_no_checksafe(v))
564 
565 #define RUBY_SAFE_LEVEL_MAX 3
566 void rb_secure(int);
567 int rb_safe_level(void);
568 void rb_set_safe_level(int);
569 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
570 int ruby$safe_level$4(void) __attribute__((error("$SAFE=4 is obsolete")));
571 #define RUBY_SAFE_LEVEL_INVALID_P(level) \
572     __extension__(__builtin_constant_p(level) && \
573                   ((level) < 0 || RUBY_SAFE_LEVEL_MAX < (level)))
574 #define RUBY_SAFE_LEVEL_CHECK(level) \
575     (RUBY_SAFE_LEVEL_INVALID_P(level) ? ruby$safe_level$4() : (level))
576 #define rb_secure(level) rb_secure(RUBY_SAFE_LEVEL_CHECK(level))
577 #define rb_set_safe_level(level) rb_set_safe_level(RUBY_SAFE_LEVEL_CHECK(level))
578 #endif
579 void rb_set_safe_level_force(int);
580 void rb_secure_update(VALUE);
581 NORETURN(void rb_insecure_operation(void));
582 
583 VALUE rb_errinfo(void);
584 void rb_set_errinfo(VALUE);
585 
586 SIGNED_VALUE rb_num2long(VALUE);
587 VALUE rb_num2ulong(VALUE);
588 static inline long
589 rb_num2long_inline(VALUE x)
590 {
591     if (FIXNUM_P(x))
592         return FIX2LONG(x);
593     else
594         return (long)rb_num2long(x);
595 }
596 #define NUM2LONG(x) rb_num2long_inline(x)
597 static inline unsigned long
598 rb_num2ulong_inline(VALUE x)
599 {
600     if (FIXNUM_P(x))
601         return (unsigned long)FIX2LONG(x);
602     else
603         return (unsigned long)rb_num2ulong(x);
604 }
605 #define NUM2ULONG(x) rb_num2ulong_inline(x)
606 #if SIZEOF_INT < SIZEOF_LONG
607 long rb_num2int(VALUE);
608 long rb_fix2int(VALUE);
609 #define FIX2INT(x) ((int)rb_fix2int((VALUE)(x)))
610 
611 static inline int
612 rb_num2int_inline(VALUE x)
613 {
614     if (FIXNUM_P(x))
615         return FIX2INT(x);
616     else
617         return (int)rb_num2int(x);
618 }
619 #define NUM2INT(x) rb_num2int_inline(x)
620 
621 unsigned long rb_num2uint(VALUE);
622 #define NUM2UINT(x) ((unsigned int)rb_num2uint(x))
623 unsigned long rb_fix2uint(VALUE);
624 #define FIX2UINT(x) ((unsigned int)rb_fix2uint(x))
625 #else /* SIZEOF_INT < SIZEOF_LONG */
626 #define NUM2INT(x) ((int)NUM2LONG(x))
627 #define NUM2UINT(x) ((unsigned int)NUM2ULONG(x))
628 #define FIX2INT(x) ((int)FIX2LONG(x))
629 #define FIX2UINT(x) ((unsigned int)FIX2ULONG(x))
630 #endif /* SIZEOF_INT < SIZEOF_LONG */
631 
632 short rb_num2short(VALUE);
633 unsigned short rb_num2ushort(VALUE);
634 short rb_fix2short(VALUE);
635 unsigned short rb_fix2ushort(VALUE);
636 #define FIX2SHORT(x) (rb_fix2short((VALUE)(x)))
637 static inline short
638 rb_num2short_inline(VALUE x)
639 {
640     if (FIXNUM_P(x))
641         return FIX2SHORT(x);
642     else
643         return rb_num2short(x);
644 }
645 
646 #define NUM2SHORT(x) rb_num2short_inline(x)
647 #define NUM2USHORT(x) rb_num2ushort(x)
648 
649 #ifdef HAVE_LONG_LONG
650 LONG_LONG rb_num2ll(VALUE);
651 unsigned LONG_LONG rb_num2ull(VALUE);
652 static inline LONG_LONG
653 rb_num2ll_inline(VALUE x)
654 {
655     if (FIXNUM_P(x))
656         return FIX2LONG(x);
657     else
658         return rb_num2ll(x);
659 }
660 # define NUM2LL(x) rb_num2ll_inline(x)
661 # define NUM2ULL(x) rb_num2ull(x)
662 #endif
663 
664 #if !defined(NUM2OFFT)
665 # if defined(HAVE_LONG_LONG) && SIZEOF_OFF_T > SIZEOF_LONG
666 #  define NUM2OFFT(x) ((off_t)NUM2LL(x))
667 # else
668 #  define NUM2OFFT(x) NUM2LONG(x)
669 # endif
670 #endif
671 
672 #if defined(HAVE_LONG_LONG) && SIZEOF_SIZE_T > SIZEOF_LONG
673 # define NUM2SIZET(x) ((size_t)NUM2ULL(x))
674 # define NUM2SSIZET(x) ((ssize_t)NUM2LL(x))
675 #else
676 # define NUM2SIZET(x) NUM2ULONG(x)
677 # define NUM2SSIZET(x) NUM2LONG(x)
678 #endif
679 
680 double rb_num2dbl(VALUE);
681 #define NUM2DBL(x) rb_num2dbl((VALUE)(x))
682 
683 VALUE rb_uint2big(VALUE);
684 VALUE rb_int2big(SIGNED_VALUE);
685 
686 VALUE rb_newobj(void);
687 VALUE rb_newobj_of(VALUE, VALUE);
688 VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type);
689 #define NEWOBJ(obj,type) type *(obj) = (type*)rb_newobj()
690 #define NEWOBJ_OF(obj,type,klass,flags) type *(obj) = (type*)rb_newobj_of(klass, flags)
691 #define OBJSETUP(obj,c,t) rb_obj_setup(obj, c, t) /* use NEWOBJ_OF instead of NEWOBJ()+OBJSETUP() */
692 #define CLONESETUP(clone,obj) do {\
693     OBJSETUP((clone),rb_singleton_class_clone((VALUE)(obj)),RBASIC(obj)->flags);\
694     rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)(clone));\
695     if (FL_TEST((obj), FL_EXIVAR)) rb_copy_generic_ivar((VALUE)(clone),(VALUE)(obj));\
696 } while (0)
697 #define DUPSETUP(dup,obj) do {\
698     OBJSETUP((dup),rb_obj_class(obj), (RBASIC(obj)->flags)&(T_MASK|FL_EXIVAR|FL_TAINT)); \
699     if (FL_TEST((obj), FL_EXIVAR)) rb_copy_generic_ivar((VALUE)(dup),(VALUE)(obj));\
700 } while (0)
701 
702 #ifndef USE_RGENGC
703 #define USE_RGENGC 1
704 #endif
705 
706 #ifndef RGENGC_WB_PROTECTED_ARRAY
707 #define RGENGC_WB_PROTECTED_ARRAY 1
708 #endif
709 #ifndef RGENGC_WB_PROTECTED_HASH
710 #define RGENGC_WB_PROTECTED_HASH 1
711 #endif
712 #ifndef RGENGC_WB_PROTECTED_STRUCT
713 #define RGENGC_WB_PROTECTED_STRUCT 1
714 #endif
715 #ifndef RGENGC_WB_PROTECTED_STRING
716 #define RGENGC_WB_PROTECTED_STRING 1
717 #endif
718 #ifndef RGENGC_WB_PROTECTED_OBJECT
719 #define RGENGC_WB_PROTECTED_OBJECT 1
720 #endif
721 #ifndef RGENGC_WB_PROTECTED_REGEXP
722 #define RGENGC_WB_PROTECTED_REGEXP 1
723 #endif
724 #ifndef RGENGC_WB_PROTECTED_CLASS
725 #define RGENGC_WB_PROTECTED_CLASS 1
726 #endif
727 #ifndef RGENGC_WB_PROTECTED_FLOAT
728 #define RGENGC_WB_PROTECTED_FLOAT 1
729 #endif
730 #ifndef RGENGC_WB_PROTECTED_COMPLEX
731 #define RGENGC_WB_PROTECTED_COMPLEX 1
732 #endif
733 #ifndef RGENGC_WB_PROTECTED_RATIONAL
734 #define RGENGC_WB_PROTECTED_RATIONAL 1
735 #endif
736 #ifndef RGENGC_WB_PROTECTED_BIGNUM
737 #define RGENGC_WB_PROTECTED_BIGNUM 1
738 #endif
739 
740 struct RBasic {
741     VALUE flags;
742     const VALUE klass;
743 }
744 #ifdef __GNUC__
745     __attribute__((aligned(sizeof(VALUE))))
746 #endif
747 ;
748 
749 VALUE rb_obj_hide(VALUE obj);
750 VALUE rb_obj_reveal(VALUE obj, VALUE klass); /* do not use this API to change klass information */
751 
752 #define RBASIC_CLASS(obj) (RBASIC(obj)->klass)
753 
754 #define ROBJECT_EMBED_LEN_MAX 3
755 struct RObject {
756     struct RBasic basic;
757     union {
758         struct {
759             long numiv;
760             VALUE *ivptr;
761             struct st_table *iv_index_tbl; /* shortcut for RCLASS_IV_INDEX_TBL(rb_obj_class(obj)) */
762         } heap;
763         VALUE ary[ROBJECT_EMBED_LEN_MAX];
764     } as;
765 };
766 #define ROBJECT_EMBED FL_USER1
767 #define ROBJECT_NUMIV(o) \
768     ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
769      ROBJECT_EMBED_LEN_MAX : \
770      ROBJECT(o)->as.heap.numiv)
771 #define ROBJECT_IVPTR(o) \
772     ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
773      ROBJECT(o)->as.ary : \
774      ROBJECT(o)->as.heap.ivptr)
775 #define ROBJECT_IV_INDEX_TBL(o) \
776     ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
777      RCLASS_IV_INDEX_TBL(rb_obj_class(o)) : \
778      ROBJECT(o)->as.heap.iv_index_tbl)
779 
780 /** @internal */
781 typedef struct rb_classext_struct rb_classext_t;
782 
783 struct RClass {
784     struct RBasic basic;
785     rb_classext_t *ptr;
786     struct st_table *m_tbl;
787     struct st_table *iv_index_tbl;
788 };
789 #define RCLASS_SUPER(c) rb_class_get_superclass(c)
790 #define RMODULE_IV_TBL(m) RCLASS_IV_TBL(m)
791 #define RMODULE_CONST_TBL(m) RCLASS_CONST_TBL(m)
792 #define RMODULE_M_TBL(m) RCLASS_M_TBL(m)
793 #define RMODULE_SUPER(m) RCLASS_SUPER(m)
794 #define RMODULE_IS_OVERLAID FL_USER2
795 #define RMODULE_IS_REFINEMENT FL_USER3
796 #define RMODULE_INCLUDED_INTO_REFINEMENT FL_USER4
797 
798 struct RFloat {
799     struct RBasic basic;
800     double float_value;
801 };
802 
803 VALUE rb_float_new_in_heap(double);
804 
805 #if USE_FLONUM
806 #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
807 #define RUBY_BIT_ROTR(v, n) (((v) >> (n)) | ((v) << ((sizeof(v) * 8) - n)))
808 
809 static inline double
810 rb_float_value(VALUE v)
811 {
812     if (FLONUM_P(v)) {
813         if (v != (VALUE)0x8000000000000002) { /* LIKELY */
814             union {
815                 double d;
816                 VALUE v;
817             } t;
818 
819             VALUE b63 = (v >> 63);
820             /* e: xx1... -> 011... */
821             /*    xx0... -> 100... */
822             /*      ^b63           */
823             t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~0x03), 3);
824             return t.d;
825         }
826         else {
827             return 0.0;
828         }
829     }
830     else {
831         return ((struct RFloat *)v)->float_value;
832     }
833 }
834 
835 static inline VALUE
836 rb_float_new(double d)
837 {
838     union {
839         double d;
840         VALUE v;
841     } t;
842     int bits;
843 
844     t.d = d;
845     bits = (int)((VALUE)(t.v >> 60) & 0x7);
846     /* bits contains 3 bits of b62..b60. */
847     /* bits - 3 = */
848     /*   b011 -> b000 */
849     /*   b100 -> b001 */
850 
851     if (t.v != 0x3000000000000000 /* 1.72723e-77 */ &&
852         !((bits-3) & ~0x01)) {
853         return (RUBY_BIT_ROTL(t.v, 3) & ~(VALUE)0x01) | 0x02;
854     }
855     else {
856         if (t.v == (VALUE)0) {
857             /* +0.0 */
858             return 0x8000000000000002;
859         }
860         else {
861             /* out of range */
862             return rb_float_new_in_heap(d);
863         }
864     }
865 }
866 
867 #else /* USE_FLONUM */
868 
869 static inline double
870 rb_float_value(VALUE v)
871 {
872     return ((struct RFloat *)v)->float_value;
873 }
874 
875 static inline VALUE
876 rb_float_new(double d)
877 {
878     return rb_float_new_in_heap(d);
879 }
880 #endif
881 
882 #define RFLOAT_VALUE(v) rb_float_value(v)
883 #define DBL2NUM(dbl)  rb_float_new(dbl)
884 
885 #define ELTS_SHARED FL_USER2
886 
887 #define RSTRING_EMBED_LEN_MAX ((int)((sizeof(VALUE)*3)/sizeof(char)-1))
888 struct RString {
889     struct RBasic basic;
890     union {
891         struct {
892             long len;
893             char *ptr;
894             union {
895                 long capa;
896                 VALUE shared;
897             } aux;
898         } heap;
899         char ary[RSTRING_EMBED_LEN_MAX + 1];
900     } as;
901 };
902 #define RSTRING_NOEMBED FL_USER1
903 #define RSTRING_EMBED_LEN_MASK (FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6)
904 #define RSTRING_EMBED_LEN_SHIFT (FL_USHIFT+2)
905 #define RSTRING_EMBED_LEN(str) \
906      (long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
907             (RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT))
908 #define RSTRING_LEN(str) \
909     (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
910      RSTRING_EMBED_LEN(str) : \
911      RSTRING(str)->as.heap.len)
912 #define RSTRING_PTR(str) \
913     (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
914      RSTRING(str)->as.ary : \
915      RSTRING(str)->as.heap.ptr)
916 #define RSTRING_END(str) \
917     (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
918      (RSTRING(str)->as.ary + RSTRING_EMBED_LEN(str)) : \
919      (RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len))
920 #define RSTRING_LENINT(str) rb_long2int(RSTRING_LEN(str))
921 #define RSTRING_GETMEM(str, ptrvar, lenvar) \
922     (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
923      ((ptrvar) = RSTRING(str)->as.ary, (lenvar) = RSTRING_EMBED_LEN(str)) : \
924      ((ptrvar) = RSTRING(str)->as.heap.ptr, (lenvar) = RSTRING(str)->as.heap.len))
925 
926 #define RARRAY_EMBED_LEN_MAX 3
927 struct RArray {
928     struct RBasic basic;
929     union {
930         struct {
931             long len;
932             union {
933                 long capa;
934                 VALUE shared;
935             } aux;
936             const VALUE *ptr;
937         } heap;
938         const VALUE ary[RARRAY_EMBED_LEN_MAX];
939     } as;
940 };
941 #define RARRAY_EMBED_FLAG FL_USER1
942 /* FL_USER2 is for ELTS_SHARED */
943 #define RARRAY_EMBED_LEN_MASK (FL_USER4|FL_USER3)
944 #define RARRAY_EMBED_LEN_SHIFT (FL_USHIFT+3)
945 #define RARRAY_LEN(a) \
946     ((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ? \
947      (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
948          (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)) : \
949      RARRAY(a)->as.heap.len)
950 
951 #define RARRAY_LENINT(ary) rb_long2int(RARRAY_LEN(ary))
952 
953 /* DO NOT USE THIS MACRO DIRECTLY */
954 #define RARRAY_RAWPTR(a) \
955   ((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ? \
956    RARRAY(a)->as.ary : \
957    RARRAY(a)->as.heap.ptr)
958 
959 #define RARRAY_PTR_USE_START(a) RARRAY_RAWPTR(a)
960 #define RARRAY_PTR_USE_END(a) /* */
961 
962 #define RARRAY_PTR_USE(ary, ptr_name, expr) do { \
963     const VALUE _ary = (ary); \
964     VALUE *ptr_name = (VALUE *)RARRAY_PTR_USE_START(_ary); \
965     expr; \
966     RARRAY_PTR_USE_END(_ary); \
967 } while (0)
968 
969 #define RARRAY_AREF(a, i)    (RARRAY_RAWPTR(a)[i])
970 #define RARRAY_ASET(a, i, v) do { \
971     const VALUE _ary_ = (a); \
972     OBJ_WRITE(_ary_, &RARRAY_RAWPTR(_ary_)[i], (v)); \
973 } while (0)
974 
975 #define RARRAY_PTR(a) ((VALUE *)RARRAY_RAWPTR(RGENGC_WB_PROTECTED_ARRAY ? OBJ_WB_UNPROTECT((VALUE)a) : ((VALUE)a)))
976 
977 struct RRegexp {
978     struct RBasic basic;
979     struct re_pattern_buffer *ptr;
980     const VALUE src;
981     unsigned long usecnt;
982 };
983 #define RREGEXP_SRC(r) RREGEXP(r)->src
984 #define RREGEXP_SRC_PTR(r) RSTRING_PTR(RREGEXP(r)->src)
985 #define RREGEXP_SRC_LEN(r) RSTRING_LEN(RREGEXP(r)->src)
986 #define RREGEXP_SRC_END(r) RSTRING_END(RREGEXP(r)->src)
987 
988 struct RHash {
989     struct RBasic basic;
990     struct st_table *ntbl;      /* possibly 0 */
991     int iter_lev;
992     const VALUE ifnone;
993 };
994 /* RHASH_TBL allocates st_table if not available. */
995 #define RHASH_TBL(h) rb_hash_tbl(h)
996 #define RHASH_ITER_LEV(h) (RHASH(h)->iter_lev)
997 #define RHASH_IFNONE(h) (RHASH(h)->ifnone)
998 #define RHASH_SIZE(h) (RHASH(h)->ntbl ? (st_index_t)RHASH(h)->ntbl->num_entries : 0)
999 #define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0)
1000 #define RHASH_SET_IFNONE(h, ifnone) rb_hash_set_ifnone((VALUE)h, ifnone)
1001 
1002 struct RFile {
1003     struct RBasic basic;
1004     struct rb_io_t *fptr;
1005 };
1006 
1007 struct RRational {
1008     struct RBasic basic;
1009     const VALUE num;
1010     const VALUE den;
1011 };
1012 
1013 #define RRATIONAL_SET_NUM(rat, n) OBJ_WRITE((rat), &((struct RRational *)(rat))->num,(n))
1014 #define RRATIONAL_SET_DEN(rat, d) OBJ_WRITE((rat), &((struct RRational *)(rat))->den,(d))
1015 
1016 struct RComplex {
1017     struct RBasic basic;
1018     const VALUE real;
1019     const VALUE imag;
1020 };
1021 
1022 #define RCOMPLEX_SET_REAL(cmp, r) OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->real,(r))
1023 #define RCOMPLEX_SET_IMAG(cmp, i) OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->imag,(i))
1024 
1025 struct RData {
1026     struct RBasic basic;
1027     void (*dmark)(void*);
1028     void (*dfree)(void*);
1029     void *data;
1030 };
1031 
1032 typedef struct rb_data_type_struct rb_data_type_t;
1033 
1034 struct rb_data_type_struct {
1035     const char *wrap_struct_name;
1036     struct {
1037         void (*dmark)(void*);
1038         void (*dfree)(void*);
1039         size_t (*dsize)(const void *);
1040         void *reserved[2]; /* For future extension.
1041                               This array *must* be filled with ZERO. */
1042     } function;
1043     const rb_data_type_t *parent;
1044     void *data;        /* This area can be used for any purpose
1045                           by a programmer who define the type. */
1046     VALUE flags;       /* FL_WB_PROTECTED */
1047 };
1048 
1049 #define HAVE_TYPE_RB_DATA_TYPE_T 1
1050 #define HAVE_RB_DATA_TYPE_T_FUNCTION 1
1051 #define HAVE_RB_DATA_TYPE_T_PARENT 1
1052 
1053 struct RTypedData {
1054     struct RBasic basic;
1055     const rb_data_type_t *type;
1056     VALUE typed_flag; /* 1 or not */
1057     void *data;
1058 };
1059 
1060 #define DATA_PTR(dta) (RDATA(dta)->data)
1061 
1062 #define RTYPEDDATA_P(v)    (RTYPEDDATA(v)->typed_flag == 1)
1063 #define RTYPEDDATA_TYPE(v) (RTYPEDDATA(v)->type)
1064 #define RTYPEDDATA_DATA(v) (RTYPEDDATA(v)->data)
1065 
1066 /*
1067 #define RUBY_DATA_FUNC(func) ((void (*)(void*))(func))
1068 */
1069 typedef void (*RUBY_DATA_FUNC)(void*);
1070 
1071 VALUE rb_data_object_alloc(VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC);
1072 VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *);
1073 int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
1074 int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *);
1075 void *rb_check_typeddata(VALUE, const rb_data_type_t *);
1076 #define Check_TypedStruct(v,t) rb_check_typeddata((VALUE)(v),(t))
1077 #define RUBY_DEFAULT_FREE ((RUBY_DATA_FUNC)-1)
1078 #define RUBY_NEVER_FREE   ((RUBY_DATA_FUNC)0)
1079 #define RUBY_TYPED_DEFAULT_FREE RUBY_DEFAULT_FREE
1080 #define RUBY_TYPED_NEVER_FREE   RUBY_NEVER_FREE
1081 
1082 #define Data_Wrap_Struct(klass,mark,free,sval)\
1083     rb_data_object_alloc((klass),(sval),(RUBY_DATA_FUNC)(mark),(RUBY_DATA_FUNC)(free))
1084 
1085 #define Data_Make_Struct(klass,type,mark,free,sval) (\
1086     (sval) = ALLOC(type),\
1087     memset((sval), 0, sizeof(type)),\
1088     Data_Wrap_Struct((klass),(mark),(free),(sval))\
1089 )
1090 
1091 #define TypedData_Wrap_Struct(klass,data_type,sval)\
1092   rb_data_typed_object_alloc((klass),(sval),(data_type))
1093 
1094 #define TypedData_Make_Struct(klass, type, data_type, sval) (\
1095     (sval) = ALLOC(type),\
1096     memset((sval), 0, sizeof(type)),\
1097     TypedData_Wrap_Struct((klass),(data_type),(sval))\
1098 )
1099 
1100 #define Data_Get_Struct(obj,type,sval) do {\
1101     Check_Type((obj), T_DATA); \
1102     (sval) = (type*)DATA_PTR(obj);\
1103 } while (0)
1104 
1105 #define TypedData_Get_Struct(obj,type,data_type,sval) do {\
1106     (sval) = (type*)rb_check_typeddata((obj), (data_type)); \
1107 } while (0)
1108 
1109 #define RSTRUCT_EMBED_LEN_MAX 3
1110 struct RStruct {
1111     struct RBasic basic;
1112     union {
1113         struct {
1114             long len;
1115             const VALUE *ptr;
1116         } heap;
1117         const VALUE ary[RSTRUCT_EMBED_LEN_MAX];
1118     } as;
1119 };
1120 #define RSTRUCT_EMBED_LEN_MASK (FL_USER2|FL_USER1)
1121 #define RSTRUCT_EMBED_LEN_SHIFT (FL_USHIFT+1)
1122 #define RSTRUCT_LEN(st) \
1123     ((RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? \
1124      (long)((RBASIC(st)->flags >> RSTRUCT_EMBED_LEN_SHIFT) & \
1125             (RSTRUCT_EMBED_LEN_MASK >> RSTRUCT_EMBED_LEN_SHIFT)) : \
1126      RSTRUCT(st)->as.heap.len)
1127 #define RSTRUCT_LENINT(st) rb_long2int(RSTRUCT_LEN(st))
1128 #define RSTRUCT_RAWPTR(st) \
1129   ((RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? \
1130    RSTRUCT(st)->as.ary : \
1131    RSTRUCT(st)->as.heap.ptr)
1132 #define RSTRUCT_PTR(st) ((VALUE *)RSTRUCT_RAWPTR(RGENGC_WB_PROTECTED_STRUCT ? OBJ_WB_UNPROTECT((VALUE)st) : (VALUE)st))
1133 
1134 #define RSTRUCT_SET(st, idx, v) OBJ_WRITE(st, &RSTRUCT_RAWPTR(st)[idx], (v))
1135 #define RSTRUCT_GET(st, idx)    (RSTRUCT_RAWPTR(st)[idx])
1136 
1137 #ifndef RBIGNUM_EMBED_LEN_MAX
1138 # define RBIGNUM_EMBED_LEN_MAX ((int)((sizeof(VALUE)*3)/sizeof(BDIGIT)))
1139 #endif
1140 struct RBignum {
1141     struct RBasic basic;
1142     union {
1143         struct {
1144             long len;
1145             BDIGIT *digits;
1146         } heap;
1147         BDIGIT ary[RBIGNUM_EMBED_LEN_MAX];
1148     } as;
1149 };
1150 #define RBIGNUM_SIGN_BIT FL_USER1
1151 /* sign: positive:1, negative:0 */
1152 #define RBIGNUM_SIGN(b) ((RBASIC(b)->flags & RBIGNUM_SIGN_BIT) != 0)
1153 #define RBIGNUM_SET_SIGN(b,sign) \
1154   ((sign) ? (RBASIC(b)->flags |= RBIGNUM_SIGN_BIT) \
1155           : (RBASIC(b)->flags &= ~RBIGNUM_SIGN_BIT))
1156 #define RBIGNUM_POSITIVE_P(b) RBIGNUM_SIGN(b)
1157 #define RBIGNUM_NEGATIVE_P(b) (!RBIGNUM_SIGN(b))
1158 
1159 #define RBIGNUM_EMBED_FLAG FL_USER2
1160 #define RBIGNUM_EMBED_LEN_MASK (FL_USER5|FL_USER4|FL_USER3)
1161 #define RBIGNUM_EMBED_LEN_SHIFT (FL_USHIFT+3)
1162 #define RBIGNUM_LEN(b) \
1163     ((RBASIC(b)->flags & RBIGNUM_EMBED_FLAG) ? \
1164      (long)((RBASIC(b)->flags >> RBIGNUM_EMBED_LEN_SHIFT) & \
1165             (RBIGNUM_EMBED_LEN_MASK >> RBIGNUM_EMBED_LEN_SHIFT)) : \
1166      RBIGNUM(b)->as.heap.len)
1167 /* LSB:RBIGNUM_DIGITS(b)[0], MSB:RBIGNUM_DIGITS(b)[RBIGNUM_LEN(b)-1] */
1168 #define RBIGNUM_DIGITS(b) \
1169     ((RBASIC(b)->flags & RBIGNUM_EMBED_FLAG) ? \
1170      RBIGNUM(b)->as.ary : \
1171      RBIGNUM(b)->as.heap.digits)
1172 #define RBIGNUM_LENINT(b) rb_long2int(RBIGNUM_LEN(b))
1173 
1174 #define R_CAST(st)   (struct st*)
1175 #define RBASIC(obj)  (R_CAST(RBasic)(obj))
1176 #define ROBJECT(obj) (R_CAST(RObject)(obj))
1177 #define RCLASS(obj)  (R_CAST(RClass)(obj))
1178 #define RMODULE(obj) RCLASS(obj)
1179 #define RFLOAT(obj)  (R_CAST(RFloat)(obj))
1180 #define RSTRING(obj) (R_CAST(RString)(obj))
1181 #define RREGEXP(obj) (R_CAST(RRegexp)(obj))
1182 #define RARRAY(obj)  (R_CAST(RArray)(obj))
1183 #define RHASH(obj)   (R_CAST(RHash)(obj))
1184 #define RDATA(obj)   (R_CAST(RData)(obj))
1185 #define RTYPEDDATA(obj)   (R_CAST(RTypedData)(obj))
1186 #define RSTRUCT(obj) (R_CAST(RStruct)(obj))
1187 #define RBIGNUM(obj) (R_CAST(RBignum)(obj))
1188 #define RFILE(obj)   (R_CAST(RFile)(obj))
1189 #define RRATIONAL(obj) (R_CAST(RRational)(obj))
1190 #define RCOMPLEX(obj) (R_CAST(RComplex)(obj))
1191 
1192 #define FL_SINGLETON FL_USER0
1193 #define FL_WB_PROTECTED (((VALUE)1)<<5)
1194 #define FL_OLDGEN    (((VALUE)1)<<6)
1195 #define FL_FINALIZE  (((VALUE)1)<<7)
1196 #define FL_TAINT     (((VALUE)1)<<8)
1197 #define FL_UNTRUSTED FL_TAINT
1198 #define FL_EXIVAR    (((VALUE)1)<<10)
1199 #define FL_FREEZE    (((VALUE)1)<<11)
1200 
1201 #define FL_USHIFT    12
1202 
1203 #define FL_USER0     (((VALUE)1)<<(FL_USHIFT+0))
1204 #define FL_USER1     (((VALUE)1)<<(FL_USHIFT+1))
1205 #define FL_USER2     (((VALUE)1)<<(FL_USHIFT+2))
1206 #define FL_USER3     (((VALUE)1)<<(FL_USHIFT+3))
1207 #define FL_USER4     (((VALUE)1)<<(FL_USHIFT+4))
1208 #define FL_USER5     (((VALUE)1)<<(FL_USHIFT+5))
1209 #define FL_USER6     (((VALUE)1)<<(FL_USHIFT+6))
1210 #define FL_USER7     (((VALUE)1)<<(FL_USHIFT+7))
1211 #define FL_USER8     (((VALUE)1)<<(FL_USHIFT+8))
1212 #define FL_USER9     (((VALUE)1)<<(FL_USHIFT+9))
1213 #define FL_USER10    (((VALUE)1)<<(FL_USHIFT+10))
1214 #define FL_USER11    (((VALUE)1)<<(FL_USHIFT+11))
1215 #define FL_USER12    (((VALUE)1)<<(FL_USHIFT+12))
1216 #define FL_USER13    (((VALUE)1)<<(FL_USHIFT+13))
1217 #define FL_USER14    (((VALUE)1)<<(FL_USHIFT+14))
1218 #define FL_USER15    (((VALUE)1)<<(FL_USHIFT+15))
1219 #define FL_USER16    (((VALUE)1)<<(FL_USHIFT+16))
1220 #define FL_USER17    (((VALUE)1)<<(FL_USHIFT+17))
1221 #define FL_USER18    (((VALUE)1)<<(FL_USHIFT+18))
1222 #define FL_USER19    (((VALUE)1)<<(FL_USHIFT+19))
1223 
1224 #define SPECIAL_CONST_P(x) (IMMEDIATE_P(x) || !RTEST(x))
1225 
1226 #define FL_ABLE(x) (!SPECIAL_CONST_P(x) && BUILTIN_TYPE(x) != T_NODE)
1227 #define FL_TEST_RAW(x,f) (RBASIC(x)->flags&(f))
1228 #define FL_TEST(x,f) (FL_ABLE(x)?FL_TEST_RAW((x),(f)):0)
1229 #define FL_ANY(x,f) FL_TEST((x),(f))
1230 #define FL_ALL(x,f) (FL_TEST((x),(f)) == (f))
1231 #define FL_SET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags |= (f);} while (0)
1232 #define FL_UNSET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags &= ~(f);} while (0)
1233 #define FL_REVERSE(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags ^= (f);} while (0)
1234 
1235 #define OBJ_TAINTED(x) (!!FL_TEST((x), FL_TAINT))
1236 #define OBJ_TAINT(x) FL_SET((x), FL_TAINT)
1237 #define OBJ_UNTRUSTED(x) OBJ_TAINTED(x)
1238 #define OBJ_UNTRUST(x) OBJ_TAINT(x)
1239 #define OBJ_INFECT(x,s) do { \
1240   if (FL_ABLE(x) && FL_ABLE(s)) \
1241     RBASIC(x)->flags |= RBASIC(s)->flags & FL_TAINT; \
1242 } while (0)
1243 
1244 #define OBJ_FROZEN(x) (!!(FL_ABLE(x)?(RBASIC(x)->flags&(FL_FREEZE)):(FIXNUM_P(x)||FLONUM_P(x))))
1245 #define OBJ_FREEZE(x) FL_SET((x), FL_FREEZE)
1246 
1247 #if USE_RGENGC
1248 #define OBJ_PROMOTED(x)             (SPECIAL_CONST_P(x) ? 0 : FL_TEST_RAW((x), FL_OLDGEN))
1249 #define OBJ_WB_PROTECTED(x)         (SPECIAL_CONST_P(x) ? 1 : FL_TEST_RAW((x), FL_WB_PROTECTED))
1250 #define OBJ_WB_UNPROTECT(x)         rb_obj_wb_unprotect(x, __FILE__, __LINE__)
1251 
1252 void rb_gc_writebarrier(VALUE a, VALUE b);
1253 void rb_gc_writebarrier_unprotect_promoted(VALUE obj);
1254 
1255 #else /* USE_RGENGC */
1256 #define OBJ_PROMOTED(x)             0
1257 #define OBJ_WB_PROTECTED(x)         0
1258 #define OBJ_WB_UNPROTECT(x)         rb_obj_wb_unprotect(x, __FILE__, __LINE__)
1259 #endif
1260 
1261 #define OBJ_WRITE(a, slot, b)       rb_obj_write((VALUE)(a), (VALUE *)(slot), (VALUE)(b), __FILE__, __LINE__)
1262 #define OBJ_WRITTEN(a, oldv, b)     rb_obj_written((VALUE)(a), (VALUE)(oldv), (VALUE)(b), __FILE__, __LINE__)
1263 
1264 static inline VALUE
1265 rb_obj_wb_unprotect(VALUE x, const char *filename, int line)
1266 {
1267 #ifdef RGENGC_LOGGING_WB_UNPROTECT
1268     RGENGC_LOGGING_WB_UNPROTECT(x, filename, line);
1269 #endif
1270 
1271 #if USE_RGENGC
1272     /* `x' should be an RVALUE object */
1273     if (FL_TEST_RAW((x), FL_WB_PROTECTED)) {
1274         RBASIC(x)->flags &= ~FL_WB_PROTECTED;
1275 
1276         if (FL_TEST_RAW((x), FL_OLDGEN)) {
1277             rb_gc_writebarrier_unprotect_promoted(x);
1278         }
1279     }
1280 #endif
1281     return x;
1282 }
1283 
1284 static inline VALUE
1285 rb_obj_written(VALUE a, VALUE oldv, VALUE b, const char *filename, int line)
1286 {
1287 #ifdef RGENGC_LOGGING_OBJ_WRITTEN
1288     RGENGC_LOGGING_OBJ_WRITTEN(a, oldv, b, filename, line);
1289 #endif
1290 
1291 #if USE_RGENGC
1292     /* `a' should be an RVALUE object */
1293     if (FL_TEST_RAW((a), FL_OLDGEN) &&
1294         !SPECIAL_CONST_P(b) && !FL_TEST_RAW((b), FL_OLDGEN)) {
1295         rb_gc_writebarrier(a, b);
1296     }
1297 #endif
1298 
1299     return a;
1300 }
1301 
1302 static inline VALUE
1303 rb_obj_write(VALUE a, VALUE *slot, VALUE b, const char *filename, int line)
1304 {
1305 #ifdef RGENGC_LOGGING_WRIET
1306     RGENGC_LOGGING_WRIET(a, slot, b, filename, line);
1307 #endif
1308 
1309     *slot = b;
1310 
1311 #if USE_RGENGC
1312     rb_obj_written(a, Qundef /* ignore `oldv' now */, b, filename, line);
1313 #endif
1314     return a;
1315 }
1316 
1317 #if SIZEOF_INT < SIZEOF_LONG
1318 # define INT2NUM(v) INT2FIX((int)(v))
1319 # define UINT2NUM(v) LONG2FIX((unsigned int)(v))
1320 #else
1321 static inline VALUE
1322 rb_int2num_inline(int v)
1323 {
1324     if (FIXABLE(v))
1325         return INT2FIX(v);
1326     else
1327         return rb_int2big(v);
1328 }
1329 #define INT2NUM(x) rb_int2num_inline(x)
1330 
1331 static inline VALUE
1332 rb_uint2num_inline(unsigned int v)
1333 {
1334     if (POSFIXABLE(v))
1335         return LONG2FIX(v);
1336     else
1337         return rb_uint2big(v);
1338 }
1339 #define UINT2NUM(x) rb_uint2num_inline(x)
1340 #endif
1341 
1342 static inline VALUE
1343 rb_long2num_inline(long v)
1344 {
1345     if (FIXABLE(v))
1346         return LONG2FIX(v);
1347     else
1348         return rb_int2big(v);
1349 }
1350 #define LONG2NUM(x) rb_long2num_inline(x)
1351 
1352 static inline VALUE
1353 rb_ulong2num_inline(unsigned long v)
1354 {
1355     if (POSFIXABLE(v))
1356         return LONG2FIX(v);
1357     else
1358         return rb_uint2big(v);
1359 }
1360 #define ULONG2NUM(x) rb_ulong2num_inline(x)
1361 
1362 static inline char
1363 rb_num2char_inline(VALUE x)
1364 {
1365     if ((TYPE(x) == T_STRING) && (RSTRING_LEN(x)>=1))
1366         return RSTRING_PTR(x)[0];
1367     else
1368         return (char)(NUM2INT(x) & 0xff);
1369 }
1370 #define NUM2CHR(x) rb_num2char_inline(x)
1371 
1372 #define CHR2FIX(x) INT2FIX((long)((x)&0xff))
1373 
1374 #define ALLOC_N(type,n) ((type*)xmalloc2((n),sizeof(type)))
1375 #define ALLOC(type) ((type*)xmalloc(sizeof(type)))
1376 #define REALLOC_N(var,type,n) ((var)=(type*)xrealloc2((char*)(var),(n),sizeof(type)))
1377 
1378 #define ALLOCA_N(type,n) ((type*)alloca(sizeof(type)*(n)))
1379 
1380 void *rb_alloc_tmp_buffer(volatile VALUE *store, long len) RUBY_ATTR_ALLOC_SIZE((2));
1381 void rb_free_tmp_buffer(volatile VALUE *store);
1382 /* allocates _n_ bytes temporary buffer and stores VALUE including it
1383  * in _v_.  _n_ may be evaluated twice. */
1384 #ifdef C_ALLOCA
1385 # define ALLOCV(v, n) rb_alloc_tmp_buffer(&(v), (n))
1386 #else
1387 # define ALLOCV(v, n) ((n) < 1024 ? (RB_GC_GUARD(v) = 0, alloca(n)) : rb_alloc_tmp_buffer(&(v), (n)))
1388 #endif
1389 #define ALLOCV_N(type, v, n) ((type*)ALLOCV((v), sizeof(type)*(n)))
1390 #define ALLOCV_END(v) rb_free_tmp_buffer(&(v))
1391 
1392 #define MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n))
1393 #define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n))
1394 #define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n))
1395 #define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n))
1396 
1397 void rb_obj_infect(VALUE,VALUE);
1398 
1399 typedef int ruby_glob_func(const char*,VALUE, void*);
1400 void rb_glob(const char*,void(*)(const char*,VALUE,void*),VALUE);
1401 int ruby_glob(const char*,int,ruby_glob_func*,VALUE);
1402 int ruby_brace_glob(const char*,int,ruby_glob_func*,VALUE);
1403 
1404 VALUE rb_define_class(const char*,VALUE);
1405 VALUE rb_define_module(const char*);
1406 VALUE rb_define_class_under(VALUE, const char*, VALUE);
1407 VALUE rb_define_module_under(VALUE, const char*);
1408 
1409 void rb_include_module(VALUE,VALUE);
1410 void rb_extend_object(VALUE,VALUE);
1411 void rb_prepend_module(VALUE,VALUE);
1412 
1413 struct rb_global_variable;
1414 
1415 typedef VALUE rb_gvar_getter_t(ID id, void *data, struct rb_global_variable *gvar);
1416 typedef void  rb_gvar_setter_t(VALUE val, ID id, void *data, struct rb_global_variable *gvar);
1417 typedef void  rb_gvar_marker_t(VALUE *var);
1418 
1419 VALUE rb_gvar_undef_getter(ID id, void *data, struct rb_global_variable *gvar);
1420 void  rb_gvar_undef_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar);
1421 void  rb_gvar_undef_marker(VALUE *var);
1422 
1423 VALUE rb_gvar_val_getter(ID id, void *data, struct rb_global_variable *gvar);
1424 void  rb_gvar_val_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar);
1425 void  rb_gvar_val_marker(VALUE *var);
1426 
1427 VALUE rb_gvar_var_getter(ID id, void *data, struct rb_global_variable *gvar);
1428 void  rb_gvar_var_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar);
1429 void  rb_gvar_var_marker(VALUE *var);
1430 
1431 void  rb_gvar_readonly_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar);
1432 
1433 void rb_define_variable(const char*,VALUE*);
1434 void rb_define_virtual_variable(const char*,VALUE(*)(ANYARGS),void(*)(ANYARGS));
1435 void rb_define_hooked_variable(const char*,VALUE*,VALUE(*)(ANYARGS),void(*)(ANYARGS));
1436 void rb_define_readonly_variable(const char*,VALUE*);
1437 void rb_define_const(VALUE,const char*,VALUE);
1438 void rb_define_global_const(const char*,VALUE);
1439 
1440 #define RUBY_METHOD_FUNC(func) ((VALUE (*)(ANYARGS))(func))
1441 void rb_define_method(VALUE,const char*,VALUE(*)(ANYARGS),int);
1442 void rb_define_module_function(VALUE,const char*,VALUE(*)(ANYARGS),int);
1443 void rb_define_global_function(const char*,VALUE(*)(ANYARGS),int);
1444 
1445 void rb_undef_method(VALUE,const char*);
1446 void rb_define_alias(VALUE,const char*,const char*);
1447 void rb_define_attr(VALUE,const char*,int,int);
1448 
1449 void rb_global_variable(VALUE*);
1450 void rb_gc_register_mark_object(VALUE);
1451 void rb_gc_register_address(VALUE*);
1452 void rb_gc_unregister_address(VALUE*);
1453 
1454 ID rb_intern(const char*);
1455 ID rb_intern2(const char*, long);
1456 ID rb_intern_str(VALUE str);
1457 const char *rb_id2name(ID);
1458 ID rb_check_id(volatile VALUE *);
1459 ID rb_to_id(VALUE);
1460 VALUE rb_id2str(ID);
1461 
1462 #define CONST_ID_CACHE(result, str)                     \
1463     {                                                   \
1464         static ID rb_intern_id_cache;                   \
1465         if (!rb_intern_id_cache)                        \
1466             rb_intern_id_cache = rb_intern2((str), (long)strlen(str)); \
1467         result rb_intern_id_cache;                      \
1468     }
1469 #define CONST_ID(var, str) \
1470     do CONST_ID_CACHE((var) =, (str)) while (0)
1471 #ifdef __GNUC__
1472 /* __builtin_constant_p and statement expression is available
1473  * since gcc-2.7.2.3 at least. */
1474 #define rb_intern(str) \
1475     (__builtin_constant_p(str) ? \
1476         __extension__ (CONST_ID_CACHE((ID), (str))) : \
1477         rb_intern(str))
1478 #define rb_intern_const(str) \
1479     (__builtin_constant_p(str) ? \
1480      __extension__ (rb_intern2((str), (long)strlen(str))) : \
1481      (rb_intern)(str))
1482 #else
1483 #define rb_intern_const(str) rb_intern2((str), (long)strlen(str))
1484 #endif
1485 
1486 const char *rb_class2name(VALUE);
1487 const char *rb_obj_classname(VALUE);
1488 
1489 void rb_p(VALUE);
1490 
1491 VALUE rb_eval_string(const char*);
1492 VALUE rb_eval_string_protect(const char*, int*);
1493 VALUE rb_eval_string_wrap(const char*, int*);
1494 VALUE rb_funcall(VALUE, ID, int, ...);
1495 VALUE rb_funcallv(VALUE, ID, int, const VALUE*);
1496 VALUE rb_funcallv_public(VALUE, ID, int, const VALUE*);
1497 #define rb_funcall2 rb_funcallv
1498 #define rb_funcall3 rb_funcallv_public
1499 VALUE rb_funcall_passing_block(VALUE, ID, int, const VALUE*);
1500 VALUE rb_funcall_with_block(VALUE, ID, int, const VALUE*, VALUE);
1501 int rb_scan_args(int, const VALUE*, const char*, ...);
1502 VALUE rb_call_super(int, const VALUE*);
1503 
1504 /* rb_scan_args() format allows ':' for optional hash */
1505 #define HAVE_RB_SCAN_ARGS_OPTIONAL_HASH 1
1506 
1507 VALUE rb_gv_set(const char*, VALUE);
1508 VALUE rb_gv_get(const char*);
1509 VALUE rb_iv_get(VALUE, const char*);
1510 VALUE rb_iv_set(VALUE, const char*, VALUE);
1511 
1512 VALUE rb_equal(VALUE,VALUE);
1513 
1514 VALUE *rb_ruby_verbose_ptr(void);
1515 VALUE *rb_ruby_debug_ptr(void);
1516 #define ruby_verbose (*rb_ruby_verbose_ptr())
1517 #define ruby_debug   (*rb_ruby_debug_ptr())
1518 
1519 PRINTF_ARGS(NORETURN(void rb_raise(VALUE, const char*, ...)), 2, 3);
1520 PRINTF_ARGS(NORETURN(void rb_fatal(const char*, ...)), 1, 2);
1521 PRINTF_ARGS(NORETURN(void rb_bug(const char*, ...)), 1, 2);
1522 NORETURN(void rb_bug_errno(const char*, int));
1523 NORETURN(void rb_sys_fail(const char*));
1524 NORETURN(void rb_sys_fail_str(VALUE));
1525 NORETURN(void rb_mod_sys_fail(VALUE, const char*));
1526 NORETURN(void rb_mod_sys_fail_str(VALUE, VALUE));
1527 NORETURN(void rb_readwrite_sys_fail(int, const char*));
1528 NORETURN(void rb_iter_break(void));
1529 NORETURN(void rb_iter_break_value(VALUE));
1530 NORETURN(void rb_exit(int));
1531 NORETURN(void rb_notimplement(void));
1532 VALUE rb_syserr_new(int, const char *);
1533 VALUE rb_syserr_new_str(int n, VALUE arg);
1534 NORETURN(void rb_syserr_fail(int, const char*));
1535 NORETURN(void rb_syserr_fail_str(int, VALUE));
1536 NORETURN(void rb_mod_syserr_fail(VALUE, int, const char*));
1537 NORETURN(void rb_mod_syserr_fail_str(VALUE, int, VALUE));
1538 
1539 /* reports if `-W' specified */
1540 PRINTF_ARGS(void rb_warning(const char*, ...), 1, 2);
1541 PRINTF_ARGS(void rb_compile_warning(const char *, int, const char*, ...), 3, 4);
1542 PRINTF_ARGS(void rb_sys_warning(const char*, ...), 1, 2);
1543 /* reports always */
1544 PRINTF_ARGS(void rb_warn(const char*, ...), 1, 2);
1545 PRINTF_ARGS(void rb_compile_warn(const char *, int, const char*, ...), 3, 4);
1546 
1547 /* for rb_readwrite_sys_fail first argument */
1548 #define RB_IO_WAIT_READABLE 0
1549 #define RB_IO_WAIT_WRITABLE 1
1550 
1551 typedef VALUE rb_block_call_func(VALUE, VALUE, int, VALUE*);
1552 
1553 VALUE rb_each(VALUE);
1554 VALUE rb_yield(VALUE);
1555 VALUE rb_yield_values(int n, ...);
1556 VALUE rb_yield_values2(int n, const VALUE *argv);
1557 VALUE rb_yield_splat(VALUE);
1558 int rb_block_given_p(void);
1559 void rb_need_block(void);
1560 VALUE rb_iterate(VALUE(*)(VALUE),VALUE,VALUE(*)(ANYARGS),VALUE);
1561 VALUE rb_block_call(VALUE,ID,int,VALUE*,VALUE(*)(ANYARGS),VALUE);
1562 VALUE rb_rescue(VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE);
1563 VALUE rb_rescue2(VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE,...);
1564 VALUE rb_ensure(VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE);
1565 VALUE rb_catch(const char*,VALUE(*)(ANYARGS),VALUE);
1566 VALUE rb_catch_obj(VALUE,VALUE(*)(ANYARGS),VALUE);
1567 NORETURN(void rb_throw(const char*,VALUE));
1568 NORETURN(void rb_throw_obj(VALUE,VALUE));
1569 
1570 VALUE rb_require(const char*);
1571 
1572 RUBY_EXTERN VALUE rb_mKernel;
1573 RUBY_EXTERN VALUE rb_mComparable;
1574 RUBY_EXTERN VALUE rb_mEnumerable;
1575 RUBY_EXTERN VALUE rb_mErrno;
1576 RUBY_EXTERN VALUE rb_mFileTest;
1577 RUBY_EXTERN VALUE rb_mGC;
1578 RUBY_EXTERN VALUE rb_mMath;
1579 RUBY_EXTERN VALUE rb_mProcess;
1580 RUBY_EXTERN VALUE rb_mWaitReadable;
1581 RUBY_EXTERN VALUE rb_mWaitWritable;
1582 
1583 RUBY_EXTERN VALUE rb_cBasicObject;
1584 RUBY_EXTERN VALUE rb_cObject;
1585 RUBY_EXTERN VALUE rb_cArray;
1586 RUBY_EXTERN VALUE rb_cBignum;
1587 RUBY_EXTERN VALUE rb_cBinding;
1588 RUBY_EXTERN VALUE rb_cClass;
1589 RUBY_EXTERN VALUE rb_cCont;
1590 RUBY_EXTERN VALUE rb_cDir;
1591 RUBY_EXTERN VALUE rb_cData;
1592 RUBY_EXTERN VALUE rb_cFalseClass;
1593 RUBY_EXTERN VALUE rb_cEncoding;
1594 RUBY_EXTERN VALUE rb_cEnumerator;
1595 RUBY_EXTERN VALUE rb_cFile;
1596 RUBY_EXTERN VALUE rb_cFixnum;
1597 RUBY_EXTERN VALUE rb_cFloat;
1598 RUBY_EXTERN VALUE rb_cHash;
1599 RUBY_EXTERN VALUE rb_cInteger;
1600 RUBY_EXTERN VALUE rb_cIO;
1601 RUBY_EXTERN VALUE rb_cMatch;
1602 RUBY_EXTERN VALUE rb_cMethod;
1603 RUBY_EXTERN VALUE rb_cModule;
1604 RUBY_EXTERN VALUE rb_cNameErrorMesg;
1605 RUBY_EXTERN VALUE rb_cNilClass;
1606 RUBY_EXTERN VALUE rb_cNumeric;
1607 RUBY_EXTERN VALUE rb_cProc;
1608 RUBY_EXTERN VALUE rb_cRandom;
1609 RUBY_EXTERN VALUE rb_cRange;
1610 RUBY_EXTERN VALUE rb_cRational;
1611 RUBY_EXTERN VALUE rb_cComplex;
1612 RUBY_EXTERN VALUE rb_cRegexp;
1613 RUBY_EXTERN VALUE rb_cStat;
1614 RUBY_EXTERN VALUE rb_cString;
1615 RUBY_EXTERN VALUE rb_cStruct;
1616 RUBY_EXTERN VALUE rb_cSymbol;
1617 RUBY_EXTERN VALUE rb_cThread;
1618 RUBY_EXTERN VALUE rb_cTime;
1619 RUBY_EXTERN VALUE rb_cTrueClass;
1620 RUBY_EXTERN VALUE rb_cUnboundMethod;
1621 
1622 RUBY_EXTERN VALUE rb_eException;
1623 RUBY_EXTERN VALUE rb_eStandardError;
1624 RUBY_EXTERN VALUE rb_eSystemExit;
1625 RUBY_EXTERN VALUE rb_eInterrupt;
1626 RUBY_EXTERN VALUE rb_eSignal;
1627 RUBY_EXTERN VALUE rb_eFatal;
1628 RUBY_EXTERN VALUE rb_eArgError;
1629 RUBY_EXTERN VALUE rb_eEOFError;
1630 RUBY_EXTERN VALUE rb_eIndexError;
1631 RUBY_EXTERN VALUE rb_eStopIteration;
1632 RUBY_EXTERN VALUE rb_eKeyError;
1633 RUBY_EXTERN VALUE rb_eRangeError;
1634 RUBY_EXTERN VALUE rb_eIOError;
1635 RUBY_EXTERN VALUE rb_eRuntimeError;
1636 RUBY_EXTERN VALUE rb_eSecurityError;
1637 RUBY_EXTERN VALUE rb_eSystemCallError;
1638 RUBY_EXTERN VALUE rb_eThreadError;
1639 RUBY_EXTERN VALUE rb_eTypeError;
1640 RUBY_EXTERN VALUE rb_eZeroDivError;
1641 RUBY_EXTERN VALUE rb_eNotImpError;
1642 RUBY_EXTERN VALUE rb_eNoMemError;
1643 RUBY_EXTERN VALUE rb_eNoMethodError;
1644 RUBY_EXTERN VALUE rb_eFloatDomainError;
1645 RUBY_EXTERN VALUE rb_eLocalJumpError;
1646 RUBY_EXTERN VALUE rb_eSysStackError;
1647 RUBY_EXTERN VALUE rb_eRegexpError;
1648 RUBY_EXTERN VALUE rb_eEncodingError;
1649 RUBY_EXTERN VALUE rb_eEncCompatError;
1650 
1651 RUBY_EXTERN VALUE rb_eScriptError;
1652 RUBY_EXTERN VALUE rb_eNameError;
1653 RUBY_EXTERN VALUE rb_eSyntaxError;
1654 RUBY_EXTERN VALUE rb_eLoadError;
1655 
1656 RUBY_EXTERN VALUE rb_eMathDomainError;
1657 
1658 RUBY_EXTERN VALUE rb_stdin, rb_stdout, rb_stderr;
1659 
1660 static inline VALUE
1661 rb_class_of(VALUE obj)
1662 {
1663     if (IMMEDIATE_P(obj)) {
1664         if (FIXNUM_P(obj)) return rb_cFixnum;
1665         if (FLONUM_P(obj)) return rb_cFloat;
1666         if (obj == Qtrue)  return rb_cTrueClass;
1667         if (SYMBOL_P(obj)) return rb_cSymbol;
1668     }
1669     else if (!RTEST(obj)) {
1670         if (obj == Qnil)   return rb_cNilClass;
1671         if (obj == Qfalse) return rb_cFalseClass;
1672     }
1673     return RBASIC(obj)->klass;
1674 }
1675 
1676 static inline int
1677 rb_type(VALUE obj)
1678 {
1679     if (IMMEDIATE_P(obj)) {
1680         if (FIXNUM_P(obj)) return T_FIXNUM;
1681         if (FLONUM_P(obj)) return T_FLOAT;
1682         if (obj == Qtrue)  return T_TRUE;
1683         if (SYMBOL_P(obj)) return T_SYMBOL;
1684         if (obj == Qundef) return T_UNDEF;
1685     }
1686     else if (!RTEST(obj)) {
1687         if (obj == Qnil)   return T_NIL;
1688         if (obj == Qfalse) return T_FALSE;
1689     }
1690     return BUILTIN_TYPE(obj);
1691 }
1692 
1693 #define RB_FLOAT_TYPE_P(obj) (FLONUM_P(obj) || (!SPECIAL_CONST_P(obj) && BUILTIN_TYPE(obj) == T_FLOAT))
1694 
1695 #define RB_TYPE_P(obj, type) ( \
1696         ((type) == T_FIXNUM) ? FIXNUM_P(obj) : \
1697         ((type) == T_TRUE) ? ((obj) == Qtrue) : \
1698         ((type) == T_FALSE) ? ((obj) == Qfalse) : \
1699         ((type) == T_NIL) ? ((obj) == Qnil) : \
1700         ((type) == T_UNDEF) ? ((obj) == Qundef) : \
1701         ((type) == T_SYMBOL) ? SYMBOL_P(obj) : \
1702         ((type) == T_FLOAT) ? RB_FLOAT_TYPE_P(obj) : \
1703         (!SPECIAL_CONST_P(obj) && BUILTIN_TYPE(obj) == (type)))
1704 
1705 #ifdef __GNUC__
1706 #define rb_type_p(obj, type) \
1707     __extension__ (__builtin_constant_p(type) ? RB_TYPE_P((obj), (type)) : \
1708                    rb_type(obj) == (type))
1709 #else
1710 #define rb_type_p(obj, type) (rb_type(obj) == (type))
1711 #endif
1712 
1713 #ifdef __GNUC__
1714 #define rb_special_const_p(obj) \
1715     __extension__ ({VALUE special_const_obj = (obj); (int)(SPECIAL_CONST_P(special_const_obj) ? Qtrue : Qfalse);})
1716 #else
1717 static inline int
1718 rb_special_const_p(VALUE obj)
1719 {
1720     if (SPECIAL_CONST_P(obj)) return (int)Qtrue;
1721     return (int)Qfalse;
1722 }
1723 #endif
1724 
1725 #include "ruby/missing.h"
1726 #include "ruby/intern.h"
1727 
1728 #if defined(EXTLIB) && defined(USE_DLN_A_OUT)
1729 /* hook for external modules */
1730 static char *dln_libs_to_be_linked[] = { EXTLIB, 0 };
1731 #endif
1732 
1733 #define RUBY_VM 1 /* YARV */
1734 #define HAVE_NATIVETHREAD
1735 int ruby_native_thread_p(void);
1736 
1737 /* traditional set_trace_func events */
1738 #define RUBY_EVENT_NONE      0x0000
1739 #define RUBY_EVENT_LINE      0x0001
1740 #define RUBY_EVENT_CLASS     0x0002
1741 #define RUBY_EVENT_END       0x0004
1742 #define RUBY_EVENT_CALL      0x0008
1743 #define RUBY_EVENT_RETURN    0x0010
1744 #define RUBY_EVENT_C_CALL    0x0020
1745 #define RUBY_EVENT_C_RETURN  0x0040
1746 #define RUBY_EVENT_RAISE     0x0080
1747 #define RUBY_EVENT_ALL       0x00ff
1748 
1749 /* for TracePoint extended events */
1750 #define RUBY_EVENT_B_CALL            0x0100
1751 #define RUBY_EVENT_B_RETURN          0x0200
1752 #define RUBY_EVENT_THREAD_BEGIN      0x0400
1753 #define RUBY_EVENT_THREAD_END        0x0800
1754 #define RUBY_EVENT_TRACEPOINT_ALL    0xffff
1755 
1756 /* special events */
1757 #define RUBY_EVENT_SPECIFIED_LINE         0x010000
1758 #define RUBY_EVENT_COVERAGE               0x020000
1759 
1760 /* internal events */
1761 #define RUBY_INTERNAL_EVENT_SWITCH        0x040000
1762                                        /* 0x080000 */
1763 #define RUBY_INTERNAL_EVENT_NEWOBJ        0x100000
1764 #define RUBY_INTERNAL_EVENT_FREEOBJ       0x200000
1765 #define RUBY_INTERNAL_EVENT_GC_START      0x400000
1766 #define RUBY_INTERNAL_EVENT_GC_END        0x800000
1767 #define RUBY_INTERNAL_EVENT_OBJSPACE_MASK 0xf00000
1768 #define RUBY_INTERNAL_EVENT_MASK        0xfffe0000
1769 
1770 typedef unsigned long rb_event_flag_t;
1771 typedef void (*rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass);
1772 
1773 #define RB_EVENT_HOOKS_HAVE_CALLBACK_DATA 1
1774 void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data);
1775 int rb_remove_event_hook(rb_event_hook_func_t func);
1776 
1777 /* locale insensitive functions */
1778 
1779 #define rb_isascii(c) ((unsigned long)(c) < 128)
1780 int rb_isalnum(int c);
1781 int rb_isalpha(int c);
1782 int rb_isblank(int c);
1783 int rb_iscntrl(int c);
1784 int rb_isdigit(int c);
1785 int rb_isgraph(int c);
1786 int rb_islower(int c);
1787 int rb_isprint(int c);
1788 int rb_ispunct(int c);
1789 int rb_isspace(int c);
1790 int rb_isupper(int c);
1791 int rb_isxdigit(int c);
1792 int rb_tolower(int c);
1793 int rb_toupper(int c);
1794 
1795 #ifndef ISPRINT
1796 #define ISASCII(c) rb_isascii((unsigned char)(c))
1797 #undef ISPRINT
1798 #define ISPRINT(c) rb_isprint((unsigned char)(c))
1799 #define ISGRAPH(c) rb_isgraph((unsigned char)(c))
1800 #define ISSPACE(c) rb_isspace((unsigned char)(c))
1801 #define ISUPPER(c) rb_isupper((unsigned char)(c))
1802 #define ISLOWER(c) rb_islower((unsigned char)(c))
1803 #define ISALNUM(c) rb_isalnum((unsigned char)(c))
1804 #define ISALPHA(c) rb_isalpha((unsigned char)(c))
1805 #define ISDIGIT(c) rb_isdigit((unsigned char)(c))
1806 #define ISXDIGIT(c) rb_isxdigit((unsigned char)(c))
1807 #endif
1808 #define TOUPPER(c) rb_toupper((unsigned char)(c))
1809 #define TOLOWER(c) rb_tolower((unsigned char)(c))
1810 
1811 int st_strcasecmp(const char *s1, const char *s2);
1812 int st_strncasecmp(const char *s1, const char *s2, size_t n);
1813 #define STRCASECMP(s1, s2) (st_strcasecmp((s1), (s2)))
1814 #define STRNCASECMP(s1, s2, n) (st_strncasecmp((s1), (s2), (n)))
1815 
1816 unsigned long ruby_strtoul(const char *str, char **endptr, int base);
1817 #define STRTOUL(str, endptr, base) (ruby_strtoul((str), (endptr), (base)))
1818 
1819 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();}
1820 
1821 PRINTF_ARGS(int ruby_snprintf(char *str, size_t n, char const *fmt, ...), 3, 4);
1822 int ruby_vsnprintf(char *str, size_t n, char const *fmt, va_list ap);
1823 
1824 #ifndef RUBY_DONT_SUBST
1825 #include "ruby/subst.h"
1826 #endif
1827 
1828 /**
1829  * @defgroup embed CRuby Embedding APIs
1830  * CRuby interpreter APIs. These are APIs to embed MRI interpreter into your
1831  * program.
1832  * These functions are not a part of Ruby extension library API.
1833  * Extension libraries of Ruby should not depend on these functions.
1834  * @{
1835  */
1836 
1837 /** @defgroup ruby1 ruby(1) implementation
1838  * A part of the implementation of ruby(1) command.
1839  * Other programs that embed Ruby interpreter do not always need to use these
1840  * functions.
1841  * @{
1842  */
1843 
1844 void ruby_sysinit(int *argc, char ***argv);
1845 void ruby_init(void);
1846 void* ruby_options(int argc, char** argv);
1847 int ruby_executable_node(void *n, int *status);
1848 int ruby_run_node(void *n);
1849 
1850 /* version.c */
1851 void ruby_show_version(void);
1852 void ruby_show_copyright(void);
1853 
1854 
1855 /*! A convenience macro to call ruby_init_stack(). Must be placed just after
1856  *  variable declarations */
1857 #define RUBY_INIT_STACK \
1858     VALUE variable_in_this_stack_frame; \
1859     ruby_init_stack(&variable_in_this_stack_frame);
1860 /*! @} */
1861 
1862 #ifdef __ia64
1863 void ruby_init_stack(volatile VALUE*, void*);
1864 #define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
1865 #else
1866 void ruby_init_stack(volatile VALUE*);
1867 #endif
1868 #define Init_stack(addr) ruby_init_stack(addr)
1869 
1870 int ruby_setup(void);
1871 int ruby_cleanup(volatile int);
1872 
1873 void ruby_finalize(void);
1874 NORETURN(void ruby_stop(int));
1875 
1876 void ruby_set_stack_size(size_t);
1877 int ruby_stack_check(void);
1878 size_t ruby_stack_length(VALUE**);
1879 
1880 int ruby_exec_node(void *n);
1881 
1882 void ruby_script(const char* name);
1883 void ruby_set_script_name(VALUE name);
1884 
1885 void ruby_prog_init(void);
1886 void ruby_set_argv(int, char**);
1887 void *ruby_process_options(int, char**);
1888 void ruby_init_loadpath(void);
1889 void ruby_incpush(const char*);
1890 void ruby_sig_finalize(void);
1891 
1892 /*! @} */
1893 
1894 RUBY_SYMBOL_EXPORT_END
1895 
1896 #if defined(__cplusplus)
1897 #if 0
1898 { /* satisfy cc-mode */
1899 #endif
1900 }  /* extern "C" { */
1901 #endif
1902 #endif /* RUBY_RUBY_H */