1 #ifndef RBX_CAPI_RUBY_RUBY_H
2 #define RBX_CAPI_RUBY_RUBY_H
8 * - The function prefix capi_* is used for functions that implement
9 * the Ruby C-API but should NEVER be used in a C extension's code.
11 * Just in case, that means NEVER, like NOT EVER. If you do, we'll
28 #include <sys/select.h>
31 // A number of extensions expect these to be already included
36 #include <sys/types.h>
44 #include "capi_constants.h"
48 /* There are C-exts like pg and mysql that are using this define to dectect
49 * whether they are running in 1.9 mode. This is extremely broken, but instead
50 * of trying to chase down all those libraries now, we'll see how far this
51 * gets us. It shouldn't cause issues because any C-exts that use MRI
52 * internals would not compile regardless of this define.
54 #define RUBY_VM 1 /* Fake YARV */
56 #define RUBY_COMPAT_LEVEL 2
59 # ifndef HAVE_PROTOTYPES
60 # define HAVE_PROTOTYPES 1
62 # ifndef HAVE_STDARG_PROTOTYPES
63 # define HAVE_STDARG_PROTOTYPES 1
68 #ifdef HAVE_PROTOTYPES
75 #ifdef HAVE_STDARG_PROTOTYPES
76 # define __(args) args
94 /* assuming 32bit(2's complement) long */
95 # define LONG_MAX 2147483647
99 # define LONG_MIN (-LONG_MAX-1)
106 #if SIZEOF_LONG_LONG > 0
107 # define LONG_LONG long long
110 #if SIZEOF_LONG == SIZEOF_VOIDP
111 # define SIGNED_VALUE long
112 # define SIZEOF_VALUE SIZEOF_LONG
113 # define PRI_VALUE_PREFIX "l"
114 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
115 # define SIGNED_VALUE LONG_LONG
116 # define LONG_LONG_VALUE 1
117 # define SIZEOF_VALUE SIZEOF_LONG_LONG
118 # define PRI_VALUE_PREFIX "ll"
120 # error ---->> ruby requires sizeof(void*) == sizeof(long) to be compiled. <<----
123 #ifndef PRI_INT_PREFIX
124 #define PRI_INT_PREFIX ""
126 #ifndef PRI_LONG_PREFIX
127 #define PRI_LONG_PREFIX "l"
131 #define PRI_64_PREFIX PRI_LONG_PREFIX
132 #elif SIZEOF_LONG_LONG == 8
133 #define PRI_64_PREFIX PRI_LL_PREFIX
136 #define RUBY_PRI_VALUE_MARK "\v"
137 #if defined PRIdPTR && !defined PRI_VALUE_PREFIX
138 #define PRIdVALUE PRIdPTR
139 #define PRIoVALUE PRIoPTR
140 #define PRIuVALUE PRIuPTR
141 #define PRIxVALUE PRIxPTR
142 #define PRIXVALUE PRIXPTR
143 #define PRIsVALUE PRIiPTR"" RUBY_PRI_VALUE_MARK
145 #define PRIdVALUE PRI_VALUE_PREFIX"d"
146 #define PRIoVALUE PRI_VALUE_PREFIX"o"
147 #define PRIuVALUE PRI_VALUE_PREFIX"u"
148 #define PRIxVALUE PRI_VALUE_PREFIX"x"
149 #define PRIXVALUE PRI_VALUE_PREFIX"X"
150 #define PRIsVALUE PRI_VALUE_PREFIX"i" RUBY_PRI_VALUE_MARK
152 #ifndef PRI_VALUE_PREFIX
153 # define PRI_VALUE_PREFIX ""
156 #ifndef PRI_TIMET_PREFIX
157 # if SIZEOF_TIME_T == SIZEOF_INT
158 # define PRI_TIMET_PREFIX
159 # elif SIZEOF_TIME_T == SIZEOF_LONG
160 # define PRI_TIMET_PREFIX "l"
161 # elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
162 # define PRI_TIMET_PREFIX PRI_LL_PREFIX
166 #if defined PRI_PTRDIFF_PREFIX
167 #elif SIZEOF_PTRDIFF_T == SIZEOF_INT
168 # define PRI_PTRDIFF_PREFIX ""
169 #elif SIZEOF_PTRDIFF_T == SIZEOF_LONG
170 # define PRI_PTRDIFF_PREFIX "l"
171 #elif SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
172 # define PRI_PTRDIFF_PREFIX PRI_LL_PREFIX
174 #define PRIdPTRDIFF PRI_PTRDIFF_PREFIX"d"
175 #define PRIiPTRDIFF PRI_PTRDIFF_PREFIX"i"
176 #define PRIoPTRDIFF PRI_PTRDIFF_PREFIX"o"
177 #define PRIuPTRDIFF PRI_PTRDIFF_PREFIX"u"
178 #define PRIxPTRDIFF PRI_PTRDIFF_PREFIX"x"
179 #define PRIXPTRDIFF PRI_PTRDIFF_PREFIX"X"
181 #if defined PRI_SIZE_PREFIX
182 #elif SIZEOF_SIZE_T == SIZEOF_INT
183 # define PRI_SIZE_PREFIX ""
184 #elif SIZEOF_SIZE_T == SIZEOF_LONG
185 # define PRI_SIZE_PREFIX "l"
186 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
187 # define PRI_SIZE_PREFIX PRI_LL_PREFIX
189 #define PRIdSIZE PRI_SIZE_PREFIX"d"
190 #define PRIiSIZE PRI_SIZE_PREFIX"i"
191 #define PRIoSIZE PRI_SIZE_PREFIX"o"
192 #define PRIuSIZE PRI_SIZE_PREFIX"u"
193 #define PRIxSIZE PRI_SIZE_PREFIX"x"
194 #define PRIXSIZE PRI_SIZE_PREFIX"X"
197 #define RB_UNUSED_VAR(x) x __attribute__ ((unused))
199 #define RB_UNUSED_VAR(x) x
203 #define RUBY_EXTERN extern
207 #define EXTERN RUBY_EXTERN /* deprecated */
211 Added to fix https://github.com/rubinius/rubinius/issues/2840. The original
212 definition comes from some Makefile file that, looking at the directory it was
213 in, should only be used for win32. Alas, apparently MRI makes it available and
214 uses it so we have to also define these aliases.
220 void* XMALLOC(size_t bytes
);
221 void XFREE(void* ptr
);
222 void* XREALLOC(void* ptr
, size_t bytes
);
223 void* XCALLOC(size_t items
, size_t bytes
);
225 #define xmalloc XMALLOC
226 #define xcalloc XCALLOC
227 #define xrealloc XREALLOC
230 #define ruby_xmalloc xmalloc
231 #define ruby_xcalloc xcalloc
232 #define ruby_xrealloc xrealloc
233 #define ruby_xfree xfree
235 #define rb_isascii(c) ((unsigned long)(c) < 128)
236 int rb_isalnum(int c
);
237 int rb_isalpha(int c
);
238 int rb_isblank(int c
);
239 int rb_iscntrl(int c
);
240 int rb_isdigit(int c
);
241 int rb_isgraph(int c
);
242 int rb_islower(int c
);
243 int rb_isprint(int c
);
244 int rb_ispunct(int c
);
245 int rb_isspace(int c
);
246 int rb_isupper(int c
);
247 int rb_isxdigit(int c
);
248 int rb_tolower(int c
);
249 int rb_toupper(int c
);
252 #define ISASCII(c) rb_isascii((unsigned char)(c))
254 #define ISPRINT(c) rb_isprint((unsigned char)(c))
255 #define ISSPACE(c) rb_isspace((unsigned char)(c))
256 #define ISUPPER(c) rb_isupper((unsigned char)(c))
257 #define ISLOWER(c) rb_islower((unsigned char)(c))
258 #define ISALNUM(c) rb_isalnum((unsigned char)(c))
259 #define ISALPHA(c) rb_isalpha((unsigned char)(c))
260 #define ISDIGIT(c) rb_isdigit((unsigned char)(c))
261 #define ISXDIGIT(c) rb_isxdigit((unsigned char)(c))
263 #define TOUPPER(c) rb_toupper((unsigned char)(c))
264 #define TOLOWER(c) rb_tolower((unsigned char)(c))
267 * In MRI, RUBY_DATA_FUNC is used for the mark and free functions in
268 * Data_Wrap_Struct and Data_Make_Struct.
270 typedef void (*RUBY_DATA_FUNC
)(void*);
277 #define RUBY_METHOD_FUNC(func) ((VALUE (*)(ANYARGS))func)
279 /** Global class/object etc. types. */
282 extern int __X_rubinius_version
__attribute__((weak
, visibility ("default")));
283 int __X_rubinius_version
= 2;
287 * Integral type map for MRI's types.
289 * Rubinius does not implement all of these,
290 * so T_OBJECT is returned instead in those
294 enum ruby_value_type
{
297 RUBY_T_OBJECT
= 0x01,
299 RUBY_T_MODULE
= 0x03,
301 RUBY_T_STRING
= 0x05,
302 RUBY_T_REGEXP
= 0x06,
305 RUBY_T_STRUCT
= 0x09,
306 RUBY_T_BIGNUM
= 0x0a,
310 RUBY_T_COMPLEX
= 0x0e,
311 RUBY_T_RATIONAL
= 0x0f,
312 RUBY_T_ENCODING
= 0x10,
317 RUBY_T_SYMBOL
= 0x14,
318 RUBY_T_FIXNUM
= 0x15,
321 RUBY_T_IMEMO
= 0x1a, /*!< @see imemo_type */
323 RUBY_T_ICLASS
= 0x1c,
324 RUBY_T_ZOMBIE
= 0x1d,
329 #define T_NONE RUBY_T_NONE
330 #define T_NIL RUBY_T_NIL
331 #define T_OBJECT RUBY_T_OBJECT
332 #define T_CLASS RUBY_T_CLASS
333 #define T_ICLASS RUBY_T_ICLASS
334 #define T_MODULE RUBY_T_MODULE
335 #define T_FLOAT RUBY_T_FLOAT
336 #define T_STRING RUBY_T_STRING
337 #define T_REGEXP RUBY_T_REGEXP
338 #define T_ARRAY RUBY_T_ARRAY
339 #define T_HASH RUBY_T_HASH
340 #define T_STRUCT RUBY_T_STRUCT
341 #define T_BIGNUM RUBY_T_BIGNUM
342 #define T_FILE RUBY_T_FILE
343 #define T_FIXNUM RUBY_T_FIXNUM
344 #define T_TRUE RUBY_T_TRUE
345 #define T_FALSE RUBY_T_FALSE
346 #define T_DATA RUBY_T_DATA
347 #define T_MATCH RUBY_T_MATCH
348 #define T_SYMBOL RUBY_T_SYMBOL
349 #define T_RATIONAL RUBY_T_RATIONAL
350 #define T_COMPLEX RUBY_T_COMPLEX
351 #define T_ENCODING RUBY_T_ENCODING
352 #define T_IMEMO RUBY_T_IMEMO
353 #define T_UNDEF RUBY_T_UNDEF
354 #define T_NODE RUBY_T_NODE
355 #define T_ZOMBIE RUBY_T_ZOMBIE
356 #define T_MASK RUBY_T_MASK
368 #define RSTRING_CACHE_UNSAFE 1
369 #define RSTRING_CACHE_SAFE 2
371 #ifdef RSTRING_MODIFIED
372 /* The default is to update the string when RSTRING(str)->len is
373 * modified. We raise an exception if RSTRING(str)->ptr is changed.
375 #define RSTRING(str) capi_rstring_struct(str, RSTRING_CACHE_UNSAFE)
377 /* Define this macro if the C extension never modifies, but
378 * only reads from, RSTRING(str)->ptr and RSTRING(str)->len.
380 #define RSTRING(str) capi_rstring_struct(str, RSTRING_CACHE_SAFE)
383 #define RSTRING_GETMEM(rb_str, c_str, c_str_len) \
384 ((c_str) = RSTRING_PTR(rb_str), (c_str_len) = RSTRING_LEN(rb_str))
396 #define RARRAY(ary) capi_rarray_struct(ary)
399 void (*dmark
)(void*);
400 void (*dfree
)(void*);
404 #define RDATA(d) capi_rdata_struct(d)
405 #define RTYPEDDATA(d) capi_rtypeddata_struct(d)
411 #define RFLOAT(d) capi_rfloat_struct(d)
412 #define RFLOAT_VALUE(d) capi_rfloat_value(d)
414 // Do not define these messages as strings. We want a syntax error.
415 #define RHASH(obj) ({ C_API_RHASH_is_not_supported_in_Rubinius })
416 #define RHASH_TBL(obj) ({ C_API_RHASH_TBL_is_not_supported_in_Rubinius })
418 #define RHASH_SET_IFNONE(hash, def) rb_hash_set_ifnone(hash, def)
420 typedef struct rb_io_t
{
426 void (*finalize
)(struct rb_io_t
*,int);
430 typedef struct rb_io_t RIO
;
432 #define OpenFile rb_io_t
439 #define RFILE(obj) capi_rfile_struct(obj)
441 #define HAVE_RB_IO_T 1
443 // Fake it out, just make the ptr be the val
444 // MRI checks also that it's not closed...
445 #define GetOpenFile(val, ptr) (ptr) = (capi_rio_struct(val))
446 #define rb_stdin rb_gv_get("$stdin")
447 #define rb_stdout rb_gv_get("$stdout")
448 #define rb_stderr rb_gv_get("$stderr")
449 #define rb_defout rb_gv_get("$stdout")
451 #define GetReadFile(ptr) (ptr->f)
452 #define GetWriteFile(ptr) (ptr->f)
455 #define ruby_verbose (*(mri_global_verbose()))
456 #define ruby_debug (*(mri_global_debug()))
458 #define rb_rs mri_global_rb_rs()
459 #define rb_default_rs mri_global_rb_default_rs()
460 #define rb_output_rs mri_global_rb_output_rs()
461 #define rb_output_fs mri_global_rb_output_fs()
463 /* Global Class objects */
465 #define rb_cArray (capi_get_constant(cCApiArray))
466 #define rb_cBignum (capi_get_constant(cCApiBignum))
467 #define rb_cClass (capi_get_constant(cCApiClass))
468 #define rb_cData (capi_get_constant(cCApiData))
469 #define rb_cFalseClass (capi_get_constant(cCApiFalse))
470 #define rb_cFile (capi_get_constant(cCApiFile))
471 #define rb_cFixnum (capi_get_constant(cCApiFixnum))
472 #define rb_cFloat (capi_get_constant(cCApiFloat))
473 #define rb_cHash (capi_get_constant(cCApiHash))
474 #define rb_cInteger (capi_get_constant(cCApiInteger))
475 #define rb_cIO (capi_get_constant(cCApiIO))
476 #define rb_cMatch (capi_get_constant(cCApiMatch))
477 #define rb_cModule (capi_get_constant(cCApiModule))
478 #define rb_cNilClass (capi_get_constant(cCApiNil))
479 #define rb_cNumeric (capi_get_constant(cCApiNumeric))
480 #define rb_cObject (capi_get_constant(cCApiObject))
481 #define rb_cRange (capi_get_constant(cCApiRange))
482 #define rb_cRegexp (capi_get_constant(cCApiRegexp))
483 #define rb_mRubinius (capi_get_constant(cCApiRubinius))
484 #define rb_cString (capi_get_constant(cCApiString))
485 #define rb_cStruct (capi_get_constant(cCApiStruct))
486 #define rb_cSymbol (capi_get_constant(cCApiSymbol))
487 #define rb_cThread (capi_get_constant(cCApiThread))
488 #define rb_cTime (capi_get_constant(cCApiTime))
489 #define rb_cTrueClass (capi_get_constant(cCApiTrue))
490 #define rb_cProc (capi_get_constant(cCApiProc))
491 #define rb_cMethod (capi_get_constant(cCApiMethod))
492 #define rb_cRational (capi_get_constant(cCApiRational))
493 #define rb_cComplex (capi_get_constant(cCApiComplex))
494 #define rb_cEncoding (capi_get_constant(cCApiEncoding))
495 #define rb_cEnumerator (capi_get_constant(cCApiEnumerator))
496 #define rb_cMutex (capi_get_constant(cCApiMutex))
497 #define rb_cDir (capi_get_constant(cCApiDir))
499 /* Global Module objects. */
501 #define rb_mComparable (capi_get_constant(cCApiComparable))
502 #define rb_mEnumerable (capi_get_constant(cCApiEnumerable))
503 #define rb_mKernel (capi_get_constant(cCApiKernel))
504 #define rb_mGC (capi_get_constant(cCApiGC))
505 #define rb_mWaitReadable (capi_get_constant(cCApiWaitReadable))
506 #define rb_mWaitWritable (capi_get_constant(cCApiWaitWritable))
508 /* Utility modules */
509 #define rb_mCAPI (capi_get_constant(cCApiCAPI))
511 /* Exception classes. */
513 #define rb_eArgError (capi_get_constant(cCApiArgumentError))
514 #define rb_eEOFError (capi_get_constant(cCApiEOFError))
515 #define rb_mErrno (capi_get_constant(cCApiErrno))
516 #define rb_eException (capi_get_constant(cCApiException))
517 #define rb_eFatal (capi_get_constant(cCApiFatal))
518 #define rb_eFloatDomainError (capi_get_constant(cCApiFloatDomainError))
519 #define rb_eIndexError (capi_get_constant(cCApiIndexError))
520 #define rb_eInterrupt (capi_get_constant(cCApiInterrupt))
521 #define rb_eIOError (capi_get_constant(cCApiIOError))
522 #define rb_eLoadError (capi_get_constant(cCApiLoadError))
523 #define rb_eLocalJumpError (capi_get_constant(cCApiLocalJumpError))
524 #define rb_eNameError (capi_get_constant(cCApiNameError))
525 #define rb_eNoMemError (capi_get_constant(cCApiNoMemoryError))
526 #define rb_eNoMethodError (capi_get_constant(cCApiNoMethodError))
527 #define rb_eNotImpError (capi_get_constant(cCApiNotImplementedError))
528 #define rb_eRangeError (capi_get_constant(cCApiRangeError))
529 #define rb_eRegexpError (capi_get_constant(cCApiRegexpError))
530 #define rb_eRuntimeError (capi_get_constant(cCApiRuntimeError))
531 #define rb_eScriptError (capi_get_constant(cCApiScriptError))
532 #define rb_eSecurityError (capi_get_constant(cCApiSecurityError))
533 #define rb_eSignal (capi_get_constant(cCApiSignalException))
534 #define rb_eStandardError (capi_get_constant(cCApiStandardError))
535 #define rb_eSyntaxError (capi_get_constant(cCApiSyntaxError))
536 #define rb_eSystemCallError (capi_get_constant(cCApiSystemCallError))
537 #define rb_eSystemExit (capi_get_constant(cCApiSystemExit))
538 #define rb_eSysStackError (capi_get_constant(cCApiSystemStackError))
539 #define rb_eTypeError (capi_get_constant(cCApiTypeError))
540 #define rb_eThreadError (capi_get_constant(cCApiThreadError))
541 #define rb_eZeroDivError (capi_get_constant(cCApiZeroDivisionError))
542 #define rb_eMathDomainError (capi_get_constant(cCApiMathDomainError))
543 #define rb_eEncCompatError (capi_get_constant(cCApiEncCompatError))
545 /* Interface macros */
547 /** Allocate memory for type. Must NOT be used to allocate Ruby objects. */
548 #define ALLOC(type) (type*)malloc(sizeof(type))
550 /** Allocate memory for N of type. Must NOT be used to allocate Ruby objects. */
551 #define ALLOC_N(type, n) (type*)malloc(sizeof(type) * (n))
553 /** Allocate memory for N of type in the stack frame of the caller. */
554 #define ALLOCA_N(type,n) (type*)alloca(sizeof(type)*(n))
556 /** Reallocate memory allocated with ALLOC or ALLOC_N. */
557 #define REALLOC_N(ptr, type, n) (ptr)=(type*)realloc(ptr, sizeof(type) * (n));
559 #define ZALLOC_N(type,n) ((type*)xcalloc((n),sizeof(type)))
560 #define ZALLOC(type) (ZALLOC_N(type,1))
562 #define ALLOCV(v, n) rb_alloc_tmp_buffer(&(v), (n))
563 #define ALLOCV_N(type, v, n) ((type*)ALLOCV((v), sizeof(type)*(n)))
564 #define ALLOCV_END(v) rb_free_tmp_buffer(&(v))
566 /** Interrupt checking (no-op). */
567 #define CHECK_INTS /* No-op */
568 #define rb_thread_check_ints() /* No-op */
570 #define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg) \
571 VALUE yielded_arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg
573 /** Rubinius doesn't need gc guards */
574 #define RB_GC_GUARD /* No-op */
576 #define POSFIXABLE(f) ((f) <= FIXNUM_MAX)
577 #define NEGFIXABLE(f) ((f) >= FIXNUM_MIN)
578 #define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f))
580 /** Convert a Fixnum to a long int. */
581 #define FIX2LONG(x) (((long)(x)) >> 1)
583 /** Convert a Fixnum to an unsigned long int. */
584 #define FIX2ULONG(x) (((unsigned long)(x))>>1)
586 /** Convert a VALUE into a long int. */
587 #define NUM2LONG(x) (FIXNUM_P(x)?FIX2LONG(x):rb_num2long((VALUE)x))
589 /** Convert a VALUE into an unsigned long int. */
590 #define NUM2ULONG(x) rb_num2ulong((VALUE)x)
592 #if SIZEOF_INT < SIZEOF_LONG
594 /** Convert a Fixnum into an int. */
595 long rb_fix2int(VALUE
);
597 /** Convert a Fixnum into an unsigned int. */
598 unsigned long rb_fix2uint(VALUE
);
600 /** Convert a Fixnum into an int. */
601 #define FIX2INT(x) ((int)rb_fix2int((VALUE)(x)))
603 /** Convert a VALUE into an int. */
604 #define NUM2INT(x) ((int)rb_num2int(x))
606 /** Convert a Fixnum into an unsigned int. */
607 #define FIX2UINT(x) ((unsigned int)rb_fix2uint(x))
609 /** Convert a VALUE into an unsigned int. */
610 #define NUM2UINT(x) ((unsigned int)rb_num2uint(x))
614 /** Convert a VALUE into an int. */
615 #define NUM2INT(x) ((int)NUM2LONG(x))
617 /** Convert a VALUE into an unsigned int. */
618 #define NUM2UINT(x) ((unsigned int)NUM2ULONG(x))
620 /** Convert a Fixnum into an int. */
621 #define FIX2INT(x) ((int)FIX2LONG(x))
623 /** Convert a Fixnum into an unsigned int. */
624 #define FIX2UINT(x) ((unsigned int)FIX2ULONG(x))
628 VALUE
rb_int_positive_pow(long x
, unsigned long y
);
630 /** Get a handle for the Symbol object represented by ID. */
631 #define ID2SYM(id) (id)
633 /** Taint/untrust o1 if o2 is tainted/untrusted. */
634 #define OBJ_INFECT(o1, o2) capi_infect((o1), (o2))
636 /** Taints the object */
637 #define OBJ_TAINT(obj) capi_taint((obj))
639 /** Returns 1 if the object is tainted, 0 otherwise. */
640 #define OBJ_TAINTED(obj) rb_obj_tainted((obj))
642 /** Convert int to a Ruby Integer. */
643 #define INT2FIX(i) CAPI_TAG_FIXNUM(i)
645 /** Convert a char to a Ruby Integer. */
646 #define CHR2FIX(x) INT2FIX((long)((x)&0xff))
648 /** Convert long to a Ruby Integer. */
649 #define LONG2FIX(i) INT2FIX(i)
651 char rb_num2chr(VALUE
);
652 #define NUM2CHR(x) rb_num2chr((VALUE)x)
654 long long rb_num2ll(VALUE
);
655 unsigned long long rb_num2ull(VALUE
);
656 #define NUM2LL(x) (FIXNUM_P(x)?FIX2LONG(x):rb_num2ll((VALUE)x))
657 #define NUM2ULL(x) rb_num2ull((VALUE)x)
658 #define NUM2OFFT(x) ((off_t)NUM2LL(x))
660 #define OFFT2NUM(x) LL2NUM((long long)x)
662 #if SIZEOF_SIZE_T > SIZEOF_LONG
663 # define NUM2SIZET(x) ((size_t)NUM2ULL(x))
664 # define NUM2SSIZET(x) ((size_t)NUM2LL(x))
666 # define NUM2SIZET(x) NUM2ULONG(x)
667 # define NUM2SSIZET(x) NUM2LONG(x)
670 #if SIZEOF_SIZE_T > SIZEOF_LONG
671 # define SIZET2NUM(v) ULL2NUM(v)
672 # define SSIZET2NUM(v) LL2NUM(v)
673 #elif SIZEOF_SIZE_T == SIZEOF_LONG
674 # define SIZET2NUM(v) ULONG2NUM(v)
675 # define SSIZET2NUM(v) LONG2NUM(v)
677 # define SIZET2NUM(v) UINT2NUM(v)
678 # define SSIZET2NUM(v) INT2NUM(v)
681 #if SIZEOF_INT < SIZEOF_LONG
682 int rb_long2int(long n
);
684 #define rb_long2int(n) ((int)(n))
688 /** Convert from a Float to a double */
689 double rb_num2dbl(VALUE
);
690 #define NUM2DBL(x) rb_num2dbl((VALUE)(x))
692 VALUE
rb_int2big(long number
);
693 VALUE
rb_uint2big(unsigned long number
);
695 /** Zero out N elements of type starting at given pointer. */
696 #define MEMZERO(p,type,n) memset((p), 0, (sizeof(type) * (n)))
698 /** Copies n objects of type from p2 to p1. Behavior is undefined if objects
701 #define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n))
703 /** Copies n objects of type from p2 to p1. Objects may overlap. */
704 #define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n))
706 /** Compares n objects of type. */
707 #define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n))
709 #define RARRAY_AREF(a, i) rb_ary_entry(a, i)
711 /** The length of the array. */
712 #define RARRAY_LEN(ary) rb_ary_size(ary)
713 #define RARRAY_LENINT(str) rb_ary_size(str)
715 /** The pointer to the array's data. */
716 #define RARRAY_PTR(ary) (RARRAY(ary)->ptr)
717 #define RARRAY_CONST_PTR(ary) (const VALUE*)RARRAY_PTR(ary)
719 /** The source pattern for a regex. */
720 #define RREGEXP_SRC(reg) rb_reg_source(reg)
722 /** The options of the regex. */
723 #define RREGEXP_OPTIONS(reg) rb_reg_options(reg)
725 /** The length of string str. */
726 #define RSTRING_LEN(str) rb_str_len(str)
727 #define RSTRING_LENINT(str) rb_str_len(str)
729 /** The pointer to the string str's data. */
730 #ifdef RUBY_READONLY_STRING
731 #define RSTRING_PTR(str) rb_str_ptr_readonly(str)
732 #define RSTRING_END(str) rb_str_ptr_readonly_end(str)
734 #define RSTRING_PTR(str) (RSTRING(str)->ptr)
735 #define RSTRING_END(str) (RSTRING(str)->ptr + RSTRING(str)->len)
738 /** The pointer to the data. */
739 #define DATA_PTR(d) (RDATA(d)->data)
741 /** Return true if expression is not Qfalse or Qnil. */
742 #define RTEST(v) (((VALUE)(v) & ~Qnil) != 0)
744 /** Return the super class of the object */
745 #define RCLASS_SUPER(klass) capi_class_superclass((klass))
747 /** Rubinius' SafeStringValue is the same as StringValue. */
748 #define SafeStringValue StringValue
750 /** Return true if expression is an immediate, Qfalse or Qnil. */
751 #define SPECIAL_CONST_P(x) (IMMEDIATE_P(x) || !RTEST(x))
753 /** Modifies the VALUE object in place by calling rb_obj_as_string(). */
754 #define StringValue(v) rb_string_value(&(v))
755 #define StringValuePtr(v) rb_string_value_ptr(&(v))
756 #define StringValueCStr(str) rb_string_value_cstr(&(str))
757 #define STR2CSTR(str) rb_str2cstr((VALUE)(str), 0)
759 #define Check_SafeStr(x)
761 #define FilePathValue(v) rb_file_path_value(&(v))
763 /** Retrieve the ID given a Symbol handle. */
764 #define SYM2ID(sym) (sym)
766 /** Return an integer type id for the object. @see rb_type() */
767 #define TYPE(handle) rb_type(handle)
768 #define rb_type_p(obj, type) (rb_type(obj) == (type))
769 #define RB_TYPE_P(obj, type) rb_type_p(obj, type)
771 /** Alias to rb_type. This is not exactly the same as in MRI, but it makes sure
772 + * that it won't segfault if you give BUILTIN_TYPE an immediate such as a Fixnum
774 #define BUILTIN_TYPE(handle) rb_type(handle)
776 /** Convert unsigned int to a Ruby Integer. */
777 #define UINT2FIX(i) UINT2NUM((i))
779 #define LONG2FIX(i) INT2FIX(i)
781 #define LL2NUM(val) rb_ll2inum(val)
782 #define ULL2NUM(val) rb_ull2inum(val)
784 VALUE
rb_ll2inum(long long val
);
785 VALUE
rb_ull2inum(unsigned long long val
);
787 #if SIZEOF_TIME_T == 8
788 #define TIMET2NUM(v) LL2NUM(v)
789 #define NUM2TIMET(v) NUM2LL(v)
791 #define TIMET2NUM(v) LONG2NUM(v)
792 #define NUM2TIMET(v) NUM2LONG(v)
796 /* Secret extra stuff */
798 typedef VALUE (*CApiAllocFunction
)(VALUE
);
799 typedef VALUE (*CApiGenericFunction
)(ANYARGS
);
805 * Backend for defining methods after normalization.
807 * @see rb_define_*_method.
809 void capi_define_method(const char* file
,
812 CApiGenericFunction fptr
,
814 CApiMethodKind kind
);
816 /** Retrieve a Handle to a globally available object. @internal. */
817 VALUE
capi_get_constant(CApiConstant type
);
819 /** Returns the string associated with a symbol. */
820 const char *rb_id2name(ID sym
);
822 /** Returns the Ruby string associated with the symbol */
823 VALUE
rb_id2str(ID sym
);
825 /** Infect obj2 if obj1 is tainted. @internal.*/
826 void capi_infect(VALUE obj1
, VALUE obj2
);
828 /** False if expression evaluates to nil, true otherwise. @internal. */
829 int capi_nil_p(VALUE expression_result
);
831 /** Taints obj. @internal. */
832 void capi_taint(VALUE obj
);
834 /** Returns 1 if obj is tainted, 0 otherwise. @internal. */
835 int rb_obj_tainted(VALUE obj
);
837 /** Builds a string based stack trace */
838 VALUE
rb_make_backtrace();
840 /** Returns the superclass of klass or NULL. This is not the same as
841 * rb_class_superclass. See MRI's rb_class_s_alloc which returns a
842 * class created with rb_class_boot(0), i.e. having a NULL superclass.
843 * RCLASS_SUPER(klass) is used in a boolean context to exit a loop in
844 * the Digest extension. It's likely other extensions do the same thing.
846 VALUE
capi_class_superclass(VALUE klass
);
848 struct RArray
* capi_rarray_struct(VALUE array
);
849 struct RData
* capi_rdata_struct(VALUE data
);
850 struct RTypedData
* capi_rtypeddata_struct(VALUE data
);
851 struct RString
* capi_rstring_struct(VALUE string
, int cache_level
);
852 struct RFloat
* capi_rfloat_struct(VALUE data
);
853 struct RFile
* capi_rfile_struct(VALUE file
);
854 double capi_rfloat_value(VALUE flt
);
855 RIO
* capi_rio_struct(VALUE handle
);
859 /** Convert a VALUE into an int. */
860 long rb_num2int(VALUE obj
);
861 /** Convert a VALUE into a long int. */
862 long rb_num2long(VALUE obj
);
864 /** Convert a VALUE to an unsigned int. */
865 unsigned long rb_num2uint(VALUE obj
);
866 /** Convert a VALUE to an unsigned long int. */
867 unsigned long rb_num2ulong(VALUE obj
);
869 /** Convert a long int into an Integer. */
870 VALUE
rb_int2inum(int n
);
871 #define rb_int_new(v) rb_int2inum(v)
872 VALUE
INT2NUM(int n
);
873 VALUE
LONG2NUM(long n
);
875 /** Convert a unsigned long int into an Integer. */
876 VALUE
rb_uint2inum(unsigned long n
);
877 VALUE
UINT2NUM(unsigned long n
);
878 VALUE
ULONG2NUM(unsigned long n
);
880 int rb_cmpint(VALUE val
, VALUE a
, VALUE b
);
881 void rb_cmperr(VALUE x
, VALUE y
);
882 VALUE
rb_equal(VALUE a
, VALUE b
);
883 VALUE
rb_class_inherited_p(VALUE mod
, VALUE arg
);
885 typedef struct rb_data_type_struct rb_data_type_t
;
887 struct rb_data_type_struct
{
888 const char *wrap_struct_name
;
890 void (*dmark
)(void*);
891 void (*dfree
)(void*);
892 size_t (*dsize
)(const void *);
893 void *reserved
[2]; /* For future extension.
894 This array *must* be filled with ZERO. */
896 const rb_data_type_t
*parent
;
897 void *data
; /* This area can be used for any purpose
898 by a programmer who define the type. */
899 VALUE flags
; /* FL_WB_PROTECTED */
902 #define HAVE_TYPE_RB_DATA_TYPE_T 1
903 #define HAVE_RB_DATA_TYPE_T_FUNCTION 1
904 #define HAVE_RB_DATA_TYPE_T_PARENT 1
907 const rb_data_type_t
*type
;
908 VALUE typed_flag
; /* 1 or not */
912 #define RTYPEDDATA_P(v) (RTYPEDDATA(v)->typed_flag == 1)
913 #define RTYPEDDATA_TYPE(v) (RTYPEDDATA(v)->type)
914 #define RTYPEDDATA_DATA(v) (RTYPEDDATA(v)->data)
916 VALUE
rb_data_object_alloc(VALUE
,void*,RUBY_DATA_FUNC
,RUBY_DATA_FUNC
);
917 VALUE
rb_data_typed_object_alloc(VALUE klass
, void *datap
, const rb_data_type_t
*);
918 int rb_typeddata_inherited_p(const rb_data_type_t
*child
, const rb_data_type_t
*parent
);
919 int rb_typeddata_is_kind_of(VALUE
, const rb_data_type_t
*);
920 void *rb_check_typeddata(VALUE
, const rb_data_type_t
*);
922 #define Check_TypedStruct(v,t) rb_check_typeddata((VALUE)(v),(t))
923 #define RUBY_DEFAULT_FREE ((RUBY_DATA_FUNC)-1)
924 #define RUBY_NEVER_FREE ((RUBY_DATA_FUNC)0)
925 #define RUBY_TYPED_DEFAULT_FREE RUBY_DEFAULT_FREE
926 #define RUBY_TYPED_NEVER_FREE RUBY_NEVER_FREE
928 /* bits for rb_data_type_struct::flags */
929 #define RUBY_TYPED_FREE_IMMEDIATELY 0xcafebeef
930 #define RUBY_TYPED_WB_PROTECTED 0xbeefcafe
932 #define Data_Wrap_Struct(klass,mark,free,sval)\
933 rb_data_object_alloc((klass),(sval),(RUBY_DATA_FUNC)(mark),(RUBY_DATA_FUNC)(free))
935 #define Data_Make_Struct(klass,type,mark,free,sval) (\
936 (sval) = ALLOC(type),\
937 memset((sval), 0, sizeof(type)),\
938 Data_Wrap_Struct((klass),(mark),(free),(sval))\
941 #define TypedData_Wrap_Struct(klass,data_type,sval)\
942 rb_data_typed_object_alloc((klass),(sval),(data_type))
944 #define TypedData_Make_Struct(klass, type, data_type, sval) (\
945 (sval) = ALLOC(type),\
946 memset((sval), 0, sizeof(type)),\
947 TypedData_Wrap_Struct((klass),(data_type),(sval))\
950 #define Data_Get_Struct(obj,type,sval) do {\
951 Check_Type((obj), T_DATA); \
952 (sval) = (type*)DATA_PTR(obj);\
955 #define TypedData_Get_Struct(obj,type,data_type,sval) do {\
956 (sval) = (type*)rb_check_typeddata((obj), (data_type)); \
959 /** Return Qtrue if obj is an immediate, Qfalse or Qnil. */
960 int rb_special_const_p(VALUE obj
);
962 /** Return Qtrue if sym is suitable as a name of a constant. */
963 int rb_is_const_id(ID sym
);
965 /** Return Qtrue if sym is suitable as a name of an instance variable. */
966 int rb_is_instance_id(ID sym
);
968 /** Return Qtrue if sym is suitable as a name of a class variable. */
969 int rb_is_class_id(ID sym
);
971 /** Return obj if it is an Array, or return wrapped (i.e. [obj]) */
972 VALUE
rb_Array(VALUE object
);
974 /** Remove all elements from the Array. Returns self. */
975 VALUE
rb_ary_clear(VALUE self
);
977 /** Remove and return an element from the Array. */
978 VALUE
rb_ary_delete(VALUE self
, VALUE item
);
980 /** Remove and return the element at index from the Array. */
981 VALUE
rb_ary_delete_at(VALUE self
, long idx
);
983 /** Return shallow copy of the Array. The elements are not dupped. */
984 VALUE
rb_ary_dup(VALUE self
);
986 /** Return object at index. Out-of-bounds access returns Qnil. */
987 VALUE
rb_ary_entry(VALUE self
, long index
);
989 /** Return Qtrue if the array includes the item. */
990 VALUE
rb_ary_includes(VALUE self
, VALUE obj
);
992 /** Array#join. Returns String with all elements to_s, with optional separator String. */
993 VALUE
rb_ary_join(VALUE self
, VALUE separator
);
995 /** Array#to_s. Returns String with all elements to_s, without a separator string */
996 VALUE
rb_ary_to_s(VALUE self
);
998 /** New, empty Array. */
1001 /** New Array of nil elements at given length. */
1002 VALUE
rb_ary_new2(unsigned long length
);
1004 /** New Array of given length, filled with varargs elements. */
1005 VALUE
rb_ary_new3(unsigned long length
, ...);
1007 #define rb_ary_new_from_args rb_ary_new3
1008 #define rb_ary_tmp_new rb_ary_new2
1010 /** New Array of given length, filled with copies of given object. */
1011 VALUE
rb_ary_new4(unsigned long length
, const VALUE
* object
);
1013 /** Remove and return last element of Array or nil. */
1014 VALUE
rb_ary_pop(VALUE self
);
1016 /** Appends value to end of Array and returns self. */
1017 VALUE
rb_ary_push(VALUE self
, VALUE object
);
1019 /** Returns a new Array with elements in reverse order. Elements not dupped. */
1020 VALUE
rb_ary_reverse(VALUE self
);
1022 /** Remove and return first element of Array or nil. Changes other elements' indexes. */
1023 VALUE
rb_ary_shift(VALUE self
);
1025 /** Number of elements in given Array. */
1026 long rb_ary_size(VALUE self
);
1028 /** Store object at given index. Supports negative indexes. Returns object. */
1029 void rb_ary_store(VALUE self
, long int index
, VALUE object
);
1031 /** Concat two arrays */
1032 VALUE
rb_ary_concat(VALUE self
, VALUE second
);
1034 /** Adds two arrays together */
1035 VALUE
rb_ary_plus(VALUE self
, VALUE second
);
1037 /** Add object to the front of Array. Changes old indexes +1. Returns object. */
1038 VALUE
rb_ary_unshift(VALUE self
, VALUE object
);
1040 /** Returns the element at index, or returns a subarray or returns a subarray specified by a range. */
1041 VALUE
rb_ary_aref(int argc
, VALUE
*argv
, VALUE object
);
1043 VALUE
rb_ary_each(VALUE ary
);
1045 /** Same as rb_obj_freeze */
1046 VALUE
rb_ary_freeze(VALUE ary
);
1048 /** Array coercion method */
1049 VALUE
rb_ary_to_ary(VALUE ary
);
1051 /** Array subsequence */
1052 VALUE
rb_ary_subseq(VALUE
, long, long);
1054 void rb_mem_clear(VALUE
* ary
, int len
);
1056 /** Return new Array with elements first and second. */
1057 VALUE
rb_assoc_new(VALUE first
, VALUE second
);
1059 /** @see rb_ivar_get */
1060 VALUE
rb_attr_get(VALUE object
, ID attr_name
);
1062 /** Return Array with names of object's instance variables */
1063 VALUE
rb_obj_instance_variables(VALUE object
);
1065 void rb_attr(VALUE klass
, ID id
, int read
, int write
, int ex
);
1067 /** Return 1 if this send has a block, 0 otherwise. */
1068 int rb_block_given_p();
1070 /* raises a LocalJumpError if no block was given */
1071 void rb_need_block();
1073 /* Converts implicit block into a new Proc. */
1074 VALUE
rb_block_proc();
1076 typedef VALUE
rb_block_call_func(VALUE
, VALUE
, int, VALUE
*);
1077 VALUE
rb_block_call(VALUE
,ID
,int,VALUE
*,VALUE(*)(ANYARGS
),VALUE
);
1078 #define HAVE_RB_BLOCK_CALL 1
1080 VALUE
rb_each(VALUE
);
1082 VALUE
rb_iterate(VALUE (*ifunc
)(VALUE
), VALUE ary
, VALUE(*cb
)(ANYARGS
), VALUE cb_data
);
1084 size_t rb_absint_size(VALUE value
, int* nlz_bits
);
1086 VALUE
rb_big2str(VALUE self
, int base
);
1088 long rb_big2long(VALUE obj
);
1090 unsigned long rb_big2ulong(VALUE obj
);
1092 long long rb_big2ll(VALUE obj
);
1094 unsigned long long rb_big2ull(VALUE
);
1096 double rb_big2dbl(VALUE obj
);
1097 VALUE
rb_dbl2big(double num
);
1099 int rb_big_bytes_used(VALUE obj
);
1101 VALUE
rb_big_cmp(VALUE x
, VALUE y
);
1102 void rb_big_pack(VALUE val
, unsigned long *buf
, long num_longs
);
1104 int rb_big_sign(VALUE obj
);
1105 #define RBIGNUM_SIGN(obj) rb_big_sign(obj)
1106 #define RBIGNUM_POSITIVE_P(b) RBIGNUM_SIGN(b)
1107 #define RBIGNUM_NEGATIVE_P(b) (!RBIGNUM_SIGN(b))
1109 #if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
1110 # define BDIGIT unsigned int
1111 # define SIZEOF_BDIGITS SIZEOF_INT
1112 # define BDIGIT_DBL unsigned LONG_LONG
1113 # define BDIGIT_DBL_SIGNED LONG_LONG
1114 #elif SIZEOF_INT*2 <= SIZEOF_LONG
1115 # define BDIGIT unsigned int
1116 # define SIZEOF_BDIGITS SIZEOF_INT
1117 # define BDIGIT_DBL unsigned long
1118 # define BDIGIT_DBL_SIGNED long
1119 #elif SIZEOF_SHORT*2 <= SIZEOF_LONG
1120 # define BDIGIT unsigned short
1121 # define SIZEOF_BDIGITS SIZEOF_SHORT
1122 # define BDIGIT_DBL unsigned long
1123 # define BDIGIT_DBL_SIGNED long
1125 # define BDIGIT unsigned short
1126 # define SIZEOF_BDIGITS (SIZEOF_LONG/2)
1127 # define BDIGIT_DBL unsigned long
1128 # define BDIGIT_DBL_SIGNED long
1131 #define RBX_CAPI_CEIL(x,y) (((x) + (y) - 1) / (y))
1132 #define RBIGNUM_LEN(obj) RBX_CAPI_CEIL(rb_big_bytes_used(obj), SIZEOF_BDIGITS)
1133 /** Calls this method in a superclass. */
1134 VALUE
rb_call_super(int argc
, const VALUE
*argv
);
1136 /** If object responds to #to_ary, returns the result of that call, otherwise nil. */
1137 VALUE
rb_check_array_type(VALUE object
);
1139 /** If object responds to #to_str, returns the result of that call, otherwise nil. */
1140 VALUE
rb_check_string_type(VALUE object
);
1142 /** If object responds to the given method, returns the result of that call
1143 * when an integer, otherwise nil. */
1144 VALUE
rb_check_to_integer(VALUE object
, const char* method_name
);
1146 /** Raises an exception if object is frozen. */
1147 void rb_check_frozen(VALUE object
);
1149 /** check if obj is frozen. */
1150 VALUE
rb_obj_frozen_p(VALUE obj
);
1151 #define OBJ_FROZEN(obj) (RTEST(rb_obj_frozen_p(obj)))
1153 /** raise error on class */
1154 NORETURN(void rb_error_frozen(const char* what
));
1156 /** Raises an exception if object is not the same type as 'type'. */
1157 void rb_check_type(VALUE object
, int type
);
1159 #define Check_Type(v,t) rb_check_type((VALUE)(v),(t))
1162 * Safe type conversion.
1164 * If the object responds to the given method name, the method is
1165 * called and the result returned. Otherwise returns nil.
1167 * @see rb_check_array_type() and rb_check_string_type().
1169 VALUE
rb_check_convert_type(VALUE object
, int type
,
1170 const char* type_name
, const char* method_name
);
1172 void rb_check_safe_obj(VALUE obj
);
1174 void rb_check_safe_str(VALUE obj
);
1176 void rb_secure_update(VALUE obj
);
1178 /** Returns String representation of the class' name. */
1179 VALUE
rb_class_name(VALUE klass
);
1180 #define rb_class_path(k) rb_class_name(k)
1182 /** Calls the class method 'inherited' on super passing the class.
1183 * If super is NULL, calls Object.inherited. */
1184 VALUE
rb_class_inherited(VALUE super
, VALUE klass
);
1186 /** Returns a new, anonymous class inheriting from super.
1187 * Does NOT call inherited() on the superclass. */
1188 VALUE
rb_class_new(VALUE super
);
1190 /** As Ruby's .new, with the given arguments. Returns the new object. */
1191 VALUE
rb_class_new_instance(int arg_count
, VALUE
* args
, VALUE klass
);
1193 /** Returns the Class object this object is an instance of. */
1194 VALUE
rb_class_of(VALUE object
);
1196 /** Returns the superclass of a class. */
1197 VALUE
rb_class_superclass(VALUE klass
);
1199 /** Returns the first superclass of an object that isn't a singleton or intermediate. */
1200 VALUE
rb_class_real(VALUE object
);
1202 /** Returns the Class object contained in the klass field of object
1203 * (ie, a singleton class if it's there) */
1204 VALUE
CLASS_OF(VALUE object
);
1206 /** C string representation of the class' name. */
1207 const char* rb_class2name(VALUE klass
);
1209 /** Return the module referred to by qualified path (e.g. A::B::C) */
1210 VALUE
rb_path2class(const char*);
1212 /** Return the module referred to by qualified path (e.g. A::B::C) */
1213 VALUE
rb_path_to_class(VALUE str
);
1215 /** Print the value to $stdout */
1218 /** Return an array containing the names of all global variables */
1219 VALUE
rb_f_global_variables();
1221 /** Returns object returned by invoking method on object if right type, or raises error. */
1222 VALUE
rb_convert_type(VALUE object
, int type
,
1223 const char* type_name
, const char* method_name
);
1225 /** Nonzero if constant corresponding to Symbol exists in the Module. */
1226 int rb_const_defined(VALUE module
, ID const_id
);
1228 /** Returns non-zero if the constant is defined in the module. Does
1229 * not search outside of the module itself. */
1230 int rb_const_defined_at(VALUE module
, ID const_id
);
1232 /** Retrieve constant from given module. */
1233 VALUE
rb_const_get(VALUE module
, ID id_name
);
1235 /** Returns a constant defined in module only. Does not search
1236 * outside of the module itself. */
1237 VALUE
rb_const_get_at(VALUE module
, ID id_name
);
1239 /** Retrieve constant from given module. */
1240 VALUE
rb_const_get_from(VALUE module
, ID id_name
);
1242 /** Set constant on the given module */
1243 void rb_const_set(VALUE module
, ID name
, VALUE constant
);
1245 VALUE
rb_mod_remove_const(VALUE mod
, VALUE name
);
1246 VALUE
rb_mod_ancestors(VALUE mod
);
1247 VALUE
rb_mod_name(VALUE mod
);
1248 VALUE
rb_module_new(void);
1250 /** Parses a string into a double value. If badcheck is true, raises an
1251 * exception if the string contains non-digit or '.' characters.
1253 double rb_cstr_to_dbl(const char *p
, int badcheck
);
1255 /** Return Integer obtained from String#to_i using given base. */
1256 VALUE
rb_cstr2inum(const char* string
, int base
);
1257 VALUE
rb_cstr_to_inum(const char* str
, int base
, int badcheck
);
1259 /** Returns module's named class variable. @@ is optional. */
1260 VALUE
rb_cv_get(VALUE module
, const char* name
);
1262 /** Set module's named class variable to given value. Returns the value. @@ is optional. */
1263 VALUE
rb_cv_set(VALUE module
, const char* name
, VALUE value
);
1265 /** Returns a value evaluating true if module has named class var. @@ is optional. */
1266 VALUE
rb_cvar_defined(VALUE module
, ID name
);
1268 /** Returns class variable by (Symbol) name from module. @@ is optional. */
1269 VALUE
rb_cvar_get(VALUE module
, ID name
);
1271 /** Set module's named class variable to given value. Returns the value. @@ is optional. */
1272 VALUE
rb_cvar_set_internal(VALUE module
, ID name
, VALUE value
);
1273 #define rb_cvar_set rb_cvar_set_internal
1275 /** Set module's named class variable to given value. */
1276 void rb_define_class_variable(VALUE klass
, const char* name
, VALUE val
);
1278 /** Alias method by old name as new name. Methods are independent of eachother. */
1279 void rb_define_alias(VALUE module
, const char *new_name
, const char *old_name
);
1280 void rb_alias(VALUE module
, ID id_new
, ID id_old
);
1282 /** Define an .allocate for the given class. Should take no args and return a VALUE. */
1283 void rb_define_alloc_func(VALUE klass
, CApiAllocFunction allocator
);
1285 /** Undefine the .allocate for the given class. */
1286 void rb_undef_alloc_func(VALUE klass
);
1288 /** Ruby's attr_* for given name. Nonzeros to toggle read/write. */
1289 void rb_define_attr(VALUE module
, const char* attr_name
,
1290 int readable
, int writable
);
1292 /** Reopen or create new top-level class with given superclass and name. Returns the Class object. */
1293 VALUE
rb_define_class(const char* name
, VALUE superclass
);
1295 /** Reopen or create new class with superclass and name under parent module. Returns the Class object. */
1296 VALUE
rb_define_class_under(VALUE parent
, const char* name
, VALUE superclass
);
1298 /** Reopen or create new class with superclass and name under parent module. Returns the Class object. */
1299 VALUE
rb_define_class_id_under(VALUE parent
, ID name
, VALUE superclass
);
1301 /** Define a toplevel constant */
1302 void rb_define_global_const(const char* name
, VALUE obj
);
1304 /** Define a constant in given Module's namespace. */
1305 void rb_define_const(VALUE module
, const char* name
, VALUE object
);
1307 /** Generate a NativeMethod to represent a method defined as a C function. Records file. */
1308 #define rb_define_method(mod, name, fptr, arity) \
1309 capi_define_method(__FILE__, (mod), (name), \
1310 (CApiGenericFunction)(fptr), (arity), \
1313 #define UNLIMITED_ARGUMENTS (-1)
1315 NORETURN(void rb_error_arity(int, int, int));
1317 static inline int rb_check_arity(int argc
, int min
, int max
) {
1318 if ((argc
< min
) || (max
!= UNLIMITED_ARGUMENTS
&& argc
> max
)) {
1319 rb_error_arity(argc
, min
, max
);
1324 /** Defines the method on Kernel. */
1325 void rb_define_global_function(const char* name
, CApiGenericFunction func
, int argc
);
1327 /** Reopen or create new top-level Module. */
1328 VALUE
rb_define_module(const char* name
);
1330 /** Defines the method as a private instance method and a singleton method of module. */
1331 void rb_define_module_function(VALUE module
,
1332 const char* name
, CApiGenericFunction func
, int args
);
1334 /** Reopen or create a new Module inside given parent Module. */
1335 VALUE
rb_define_module_under(VALUE parent
, const char* name
);
1337 /** Generate a NativeMethod to represent a private method defined in the C function. */
1338 #define rb_define_private_method(mod, name, fptr, arity) \
1339 capi_define_method(__FILE__, (mod), (name), \
1340 (CApiGenericFunction)(fptr), (arity), \
1343 /** Generate a NativeMethod to represent a protected method defined in the C function. */
1344 #define rb_define_protected_method(mod, name, fptr, arity) \
1345 capi_define_method(__FILE__, (mod), (name), \
1346 (CApiGenericFunction)(fptr), (arity), \
1347 cCApiProtectedMethod)
1349 /** Generate a NativeMethod to represent a singleton method. @see capi_define_method. */
1350 #define rb_define_singleton_method(mod, name, fptr, arity) \
1351 capi_define_method(__FILE__, (mod), (name), \
1352 (CApiGenericFunction)(fptr), (arity), \
1353 cCApiSingletonMethod)
1355 /** Create an Exception from a class, C string and length. */
1356 VALUE
rb_exc_new(VALUE etype
, const char *ptr
, long len
);
1358 /** Create an Exception from a class and C string. */
1359 VALUE
rb_exc_new2(VALUE etype
, const char *s
);
1361 /** Create an Exception from a class and Ruby string. */
1362 VALUE
rb_exc_new3(VALUE etype
, VALUE str
);
1364 /** Raises passed exception handle */
1365 NORETURN(void rb_exc_raise(VALUE exception
));
1367 /** Return the current exception */
1369 #define ruby_errinfo rb_errinfo()
1371 /** Set the current exception */
1372 void rb_set_errinfo(VALUE err
);
1374 // To advertise we have rb_errinfo to extensions
1375 #define HAVE_RB_ERRINFO 1
1377 /** Freeze object and return it. */
1378 VALUE
rb_obj_freeze(VALUE obj
);
1379 #define OBJ_FREEZE(obj) (rb_obj_freeze(obj))
1381 /** Taint an object and return it */
1382 VALUE
rb_obj_taint(VALUE obj
);
1384 /** Returns Method object for given method name */
1385 VALUE
rb_obj_method(VALUE self
, VALUE method
);
1387 int rb_obj_method_arity(VALUE obj
, ID mid
);
1389 /** Returns a string formatted with Kernel#sprintf. */
1390 VALUE
rb_f_sprintf(int argc
, const VALUE
* argv
);
1392 /** Returns a duplicate file discriptor with close-on-exec flag set. */
1393 int rb_cloexec_dup(int fd
);
1395 /** Returns a File opened with the specified mode. */
1396 VALUE
rb_file_open(const char* name
, const char* mode
);
1398 /** Returns a File opened with the specified mode. */
1399 VALUE
rb_file_open_str(VALUE name
, const char* mode
);
1401 /** Calls #to_path or #to_str to convert an object to a String. */
1402 VALUE
rb_file_path_value(volatile VALUE
* obj
);
1405 * Call method on receiver, args as varargs. Calls private methods.
1407 VALUE
rb_funcall(VALUE receiver
, ID method_name
,
1408 int arg_count
, ...);
1411 /** Call the method with args provided in a C array. Calls private methods. */
1412 VALUE
rb_funcall2(VALUE receiver
, ID method_name
,
1413 int arg_count
, const VALUE
* args
);
1415 #define rb_funcallv rb_funcall2
1417 /** Call the method with args provided in a C array and block.
1418 * Calls private methods. */
1419 VALUE
rb_funcall2b(VALUE receiver
, ID method_name
, int arg_count
,
1420 const VALUE
* v_args
, VALUE block
);
1422 /** Call the method with args provided in a C array and block.
1423 * Does not call private methods. */
1424 VALUE
rb_funcall_with_block(VALUE receiver
, ID method_name
, int arg_count
,
1425 const VALUE
* v_args
, VALUE block
);
1427 /** Return name of the function being called */
1428 ID
rb_frame_last_func();
1430 /** Return name of the current Ruby method */
1431 ID
rb_frame_this_func();
1433 VALUE
rb_exec_recursive(VALUE (*func
)(VALUE
, VALUE
, int),
1434 VALUE obj
, VALUE arg
);
1435 #define HAVE_RB_EXEC_RECURSIVE 1
1437 /** Same as rb_funcall2 but will not call private methods. */
1438 VALUE
rb_funcall3(VALUE receiver
, ID method_name
,
1439 int arg_count
, const VALUE
* args
);
1441 /** Return the hash id of the object **/
1442 VALUE
rb_hash(VALUE self
);
1444 /** Create a new Hash object */
1445 VALUE
rb_hash_new();
1447 /** Duplicate the Hash object */
1448 VALUE
rb_hash_dup(VALUE self
);
1449 #define HAVE_RB_HASH_DUP 1
1451 /** Freeze the Hash object */
1452 VALUE
rb_hash_freeze(VALUE self
);
1453 #define HAVE_RB_HASH_FREEZE 1
1455 /** Return the value associated with the key, or raise KeyError. */
1456 VALUE
rb_hash_fetch(VALUE self
, VALUE key
);
1458 /** Return the value associated with the key, including default values. */
1459 VALUE
rb_hash_aref(VALUE self
, VALUE key
);
1461 /** Return the value associated with the key, excluding default values. */
1462 VALUE
rb_hash_lookup(VALUE self
, VALUE key
);
1464 VALUE
rb_hash_lookup2(VALUE hash
, VALUE key
, VALUE def
);
1466 /** Set the value associated with the key. */
1467 VALUE
rb_hash_aset(VALUE self
, VALUE key
, VALUE value
);
1469 /** Clear the Hash object */
1470 VALUE
rb_hash_clear(VALUE self
);
1472 /** Remove the key and return the associated value. */
1473 VALUE
rb_hash_delete(VALUE self
, VALUE key
);
1475 /** Removes the entry if the block returns true. */
1476 VALUE
rb_hash_delete_if(VALUE self
);
1478 /** Returns the number of entries as a Fixnum. */
1479 VALUE
rb_hash_size(VALUE self
);
1481 // macros to access the size "directly"
1482 #define RHASH_SIZE(obj) FIX2INT(rb_hash_size(obj))
1483 #define RHASH_LEN(val) FIX2INT(rb_hash_size(val))
1485 /** Iterate over the hash, calling the function. */
1486 void rb_hash_foreach(VALUE self
,
1487 int (*func
)(ANYARGS
),
1490 VALUE
rb_hash_set_ifnone(VALUE hash
, VALUE def
);
1492 void rb_eof_error();
1494 VALUE
rb_io_addstr(VALUE
, VALUE
);
1495 VALUE
rb_io_printf(int, VALUE
*, VALUE
);
1496 VALUE
rb_io_print(int, VALUE
*, VALUE
);
1497 VALUE
rb_io_puts(int, VALUE
*, VALUE
);
1499 /** Send #write to io passing str. */
1500 VALUE
rb_io_write(VALUE io
, VALUE str
);
1503 VALUE
rb_io_close(VALUE io
);
1505 VALUE
rb_io_binmode(VALUE io
);
1507 int rb_io_fd(VALUE io
);
1508 #define HAVE_RB_IO_FD 1
1510 int rb_io_wait_readable(int fd
);
1511 int rb_io_wait_writable(int fd
);
1513 void rb_io_set_nonblock(rb_io_t
* io
);
1514 void rb_io_check_closed(rb_io_t
* io
);
1515 void rb_io_check_readable(rb_io_t
* io
);
1516 void rb_io_check_writable(rb_io_t
* io
);
1517 VALUE
rb_io_check_io(VALUE io
);
1518 VALUE
rb_io_taint_check(VALUE io
);
1520 FILE * rb_io_stdio_file(rb_io_t
*fptr
);
1522 /* Imported from MRI for rb_integer_pack and rb_integer_unpack: */
1523 /* "MS" in MSWORD and MSBYTE means "most significant" */
1524 /* "LS" in LSWORD and LSBYTE means "least significant" */
1525 #define INTEGER_PACK_MSWORD_FIRST 0x01
1526 #define INTEGER_PACK_LSWORD_FIRST 0x02
1527 #define INTEGER_PACK_MSBYTE_FIRST 0x10
1528 #define INTEGER_PACK_LSBYTE_FIRST 0x20
1529 #define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40
1530 #define INTEGER_PACK_2COMP 0x80
1531 #define INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION 0x400
1532 /* For rb_integer_unpack: */
1533 #define INTEGER_PACK_FORCE_BIGNUM 0x100
1534 #define INTEGER_PACK_NEGATIVE 0x200
1536 #define INTEGER_PACK_LITTLE_ENDIAN \
1537 (INTEGER_PACK_LSWORD_FIRST | INTEGER_PACK_LSBYTE_FIRST)
1538 #define INTEGER_PACK_BIG_ENDIAN \
1539 (INTEGER_PACK_MSWORD_FIRST | INTEGER_PACK_MSBYTE_FIRST)
1541 int rb_integer_pack(VALUE value
, void *words
, size_t numwords
, size_t wordsize
,
1542 size_t nails
, int flags
);
1544 void rb_update_max_fd(int fd
);
1545 void rb_fd_fix_cloexec(int fd
);
1546 #define HAVE_RB_FD_FIX_CLOEXEC 1
1547 int rb_cloexec_open(const char *pathname
, int flags
, int mode
);
1548 #define HAVE_RB_FD_CLOEXEC_OPEN 1
1550 void rb_thread_wait_fd(int fd
);
1551 void rb_thread_fd_writable(int fd
);
1552 void rb_thread_wait_for(struct timeval time
);
1553 #define rb_thread_create(func, arg) capi_thread_create(func, arg, #func, __FILE__)
1554 VALUE
capi_thread_create(VALUE (*)(ANYARGS
), void*, const char* name
, const char* file
);
1556 /** Functions for mutexes, mostly used for thread safety in extensions */
1557 VALUE
rb_mutex_new();
1558 VALUE
rb_mutex_locked_p(VALUE mutex
);
1559 VALUE
rb_mutex_trylock(VALUE mutex
);
1560 VALUE
rb_mutex_lock(VALUE mutex
);
1561 VALUE
rb_mutex_unlock(VALUE mutex
);
1562 VALUE
rb_mutex_sleep(VALUE mutex
, VALUE timeout
);
1563 VALUE
rb_mutex_synchronize(VALUE mutex
, VALUE (*func
)(VALUE arg
), VALUE arg
);
1565 /** Mark ruby object ptr. */
1566 void rb_gc_mark(VALUE ptr
);
1567 /** Mark a list of Ruby object ptr's. */
1568 void rb_gc_mark_locations(VALUE
*, VALUE
*);
1571 * Marks an object if it is in the heap. Equivalent to rb_gc_mark in
1572 * Rubinius since that function checks if a handle is a GC object.
1574 void rb_gc_mark_maybe(VALUE ptr
);
1576 /** Manually runs the garbage collector. */
1577 VALUE
rb_gc_start();
1579 /** Yet another way to request to run the GC */
1582 /** Request to enable GC (is always enabled in Rubinius anyway) */
1583 VALUE
rb_gc_enable();
1585 /** Request to disable GC (doesn't actually happen) */
1586 VALUE
rb_gc_disable();
1588 /** Mark variable global. Will not be GC'd. */
1589 #define rb_global_variable(address) capi_gc_register_address(address, __FILE__, __LINE__)
1590 #define rb_gc_register_address(address) capi_gc_register_address(address, __FILE__, __LINE__)
1591 void capi_gc_register_address(VALUE
* address
, const char* file
, int line
);
1593 /** Unmark variable as global */
1594 void rb_gc_unregister_address(VALUE
* address
);
1596 void rb_gc_force_recycle(VALUE blah
);
1598 /** Called when there is no memory available */
1601 uint64_t rb_memhash(const void *ptr
, long len
);
1603 /** Retrieve global by name. Because of MRI, the leading $ is optional but recommended. */
1604 VALUE
rb_gv_get(const char* name
);
1606 /** Set named global to given value. Returns value. $ optional. */
1607 VALUE
rb_gv_set(const char* name
, VALUE value
);
1609 /** Set a global name to be used to address the VALUE at addr */
1610 void rb_define_readonly_variable(const char* name
, VALUE
* addr
);
1612 /** Include Module in another Module, just as Ruby's Module#include. */
1613 void rb_include_module(VALUE includer
, VALUE includee
);
1615 /** Convert string to an ID */
1616 ID
rb_intern(const char* string
);
1617 ID
rb_intern2(const char* string
, long len
);
1618 ID
rb_intern_const(const char *string
);
1619 ID
rb_intern_str(VALUE string
);
1620 #define HAVE_RB_INTERN_STR 1
1622 VALUE
rb_sym2str(VALUE sym
);
1624 /** Coerce x and y and perform 'x func y' */
1625 VALUE
rb_num_coerce_bin(VALUE x
, VALUE y
, ID func
);
1627 /** Coerce x and y; perform 'x func y' if coerce succeeds, else return Qnil. */
1628 VALUE
rb_num_coerce_cmp(VALUE x
, VALUE y
, ID func
);
1629 #define RB_NUM_COERCE_FUNCS_NEED_OPID 1
1631 /** Coerce x and y; perform 'x relop y' if coerce succeeds, else return Qnil. */
1632 VALUE
rb_num_coerce_relop(VALUE x
, VALUE y
, ID func
);
1634 /** Call #initialize on the object with given arguments. */
1635 void rb_obj_call_init(VALUE object
, int arg_count
, VALUE
* args
);
1637 /** Returns the Class object this object is an instance of. */
1638 #define rb_obj_class(object) rb_class_of((object))
1640 /** String representation of the object's class' name. */
1641 const char* rb_obj_classname(VALUE object
);
1643 /** Returns true-ish if object is an instance of specific class. */
1644 VALUE
rb_obj_is_instance_of(VALUE object
, VALUE klass
);
1646 /** Returns true-ish if module is object's class or other ancestor. */
1647 VALUE
rb_obj_is_kind_of(VALUE object
, VALUE module
);
1649 /** Returns the object_id of the object. */
1650 VALUE
rb_obj_id(VALUE self
);
1652 /** Return object's instance variable by name. @ optional. */
1653 VALUE
rb_iv_get(VALUE self
, const char* name
);
1655 /** Set instance variable by name to given value. Returns the value. @ optional. */
1656 VALUE
rb_iv_set(VALUE self
, const char* name
, VALUE value
);
1658 /** Get object's instance variable. */
1659 VALUE
rb_ivar_get(VALUE object
, ID ivar_name
);
1661 /** Set object's instance variable to given value. */
1662 VALUE
rb_ivar_set(VALUE object
, ID ivar_name
, VALUE value
);
1664 /** Checks if object has an ivar named ivar_name. */
1665 VALUE
rb_ivar_defined(VALUE object
, ID ivar_name
);
1667 /** Allocate uninitialised instance of given class. */
1668 VALUE
rb_obj_alloc(VALUE klass
);
1671 /** Clone an instance of an object. */
1672 VALUE
rb_obj_dup(VALUE obj
);
1674 /** Call #to_s on object. */
1675 VALUE
rb_obj_as_string(VALUE object
);
1677 VALUE
rb_obj_instance_eval(int argc
, VALUE
* argv
, VALUE self
);
1679 VALUE
rb_any_to_s(VALUE obj
);
1681 /** Return a clone of the object by calling the method bound
1682 * to Kernel#clone (i.e. does NOT call specialized #clone method
1683 * on object if one exists).
1685 VALUE
rb_obj_clone(VALUE object
);
1687 /** Adds the module's instance methods to the object. */
1688 void rb_extend_object(VALUE obj
, VALUE mod
);
1690 /** Call #inspect on an object. */
1691 VALUE
rb_inspect(VALUE object
);
1693 /** Returns a Proc wrapping a C function. */
1694 VALUE
rb_proc_new(VALUE (*func
)(ANYARGS
), VALUE val
);
1696 VALUE
rb_protect_inspect(VALUE (*func
)(VALUE a
, VALUE b
), VALUE h_obj
, VALUE h_arg
);
1697 VALUE
rb_inspecting_p(VALUE obj
);
1700 * Raise error of given class using formatted message.
1703 NORETURN(void rb_raise(VALUE error
, const char* format_string
, ...));
1706 * Transfer control to the end of the innermost catch block
1707 * waiting for the given symbol.
1708 * The given value is returned by the catch block.
1710 NORETURN(void rb_throw(const char* symbol
, VALUE result
));
1711 NORETURN(void rb_throw_obj(VALUE obj
, VALUE result
));
1714 * Run the function and catch the possible throw value
1716 VALUE
rb_catch(const char*, VALUE (*func
)(ANYARGS
), VALUE
);
1717 VALUE
rb_catch_obj(VALUE
, VALUE (*func
)(ANYARGS
), VALUE
);
1720 * Calls the function 'func', with arg1 as the argument. If an exception
1721 * occurs during 'func', it calls 'raise_func' with arg2 as the argument. The
1722 * return value of rb_rescue() is the return value from 'func' if no
1723 * exception occurs, from 'raise_func' otherwise.
1725 VALUE
rb_rescue(VALUE (*func
)(ANYARGS
), VALUE arg1
, VALUE (*raise_func
)(ANYARGS
), VALUE arg2
);
1728 * Same as rb_rescue() except that it also receives a list of exceptions classes.
1729 * It will only call 'raise_func' if the exception occurred during 'func' is a
1730 * kind_of? one of the passed exception classes.
1731 * The last argument MUST always be 0!
1733 VALUE
rb_rescue2(VALUE (*func
)(ANYARGS
), VALUE arg1
, VALUE (*raise_func
)(ANYARGS
), VALUE arg2
, ...);
1736 * Calls the function func(), with arg1 as the argument, then call ensure_func()
1737 * with arg2, even if func() raised an exception. The return value from rb_ensure()
1738 * is the return of func().
1740 VALUE
rb_ensure(VALUE (*func
)(ANYARGS
), VALUE arg1
, VALUE (*ensure_func
)(ANYARGS
), VALUE arg2
);
1743 * Call func(), and if there is an exception, returns nil and sets
1744 * *status to 1, otherwise the return of func is returned and *status
1747 VALUE
rb_protect(VALUE (*func
)(VALUE
), VALUE data
, int* status
);
1750 * Break from iteration
1752 NORETURN(void rb_iter_break(void));
1755 * Return the last Ruby source file in the backtrace
1757 const char* rb_sourcefile();
1758 #define HAVE_RB_SOURCEFILE 1
1761 * Return the line of the last Ruby code in the backtrace
1763 int rb_sourceline();
1764 #define HAVE_RB_SOURCELINE 1
1767 * Continue raising a pending exception if status is not 0
1769 void rb_jump_tag(int status
);
1772 * Retrieve the source pattern for the regular expression.
1774 VALUE
rb_reg_source(VALUE r
);
1777 * Retrieve the pattern options for the regular expression.
1779 int rb_reg_options(VALUE r
);
1782 * Compile the String object into a regexp.
1784 VALUE
rb_reg_regcomp(VALUE str
);
1787 * Tests whether the given Regexp matches to the given String
1789 VALUE
rb_reg_match(VALUE re
, VALUE str
);
1792 * Retrieves the last MatchData
1794 VALUE
rb_backref_get(void);
1797 * Require a Ruby file.
1799 * Returns true on first load, false if already loaded or raises.
1801 VALUE
rb_require(const char* name
);
1803 /** 1 if obj.respond_to? method_name evaluates true, 0 otherwise. */
1804 int rb_respond_to(VALUE object
, ID method_name
);
1806 /** 1 if obj.respond_to? method_name evaluates true, 0 otherwise. */
1807 int rb_obj_respond_to(VALUE obj
, ID id
, int priv
);
1809 /** 1 if class has the given method defined */
1810 int rb_method_boundp(VALUE cls
, ID method_name
, int exclude_priv
);
1812 /** Returns the current $SAFE level. */
1813 int rb_safe_level();
1815 /* Marshals an object, optionally writing output to an IO */
1816 VALUE
rb_marshal_dump(VALUE obj
, VALUE io
);
1819 * Process arguments using a template rather than manually.
1821 * The first two arguments are simple: the number of arguments given
1822 * and an array of the args. Usually you get these as parameters to
1825 * The spec works like this: it must have one (or more) of the following
1826 * specifiers, and the specifiers that are given must always appear
1827 * in the order given here. If the first character is a digit (0-9),
1828 * it is the number of required parameters. If there is a second digit
1829 * (0-9), it is the number of optional parameters. The next character
1830 * may be "*", indicating a "splat" i.e. it consumes all remaining
1831 * parameters. Finally, the last character may be "&", signifying
1832 * that the block given (or Qnil) should be stored.
1834 * The remaining arguments are pointers to the variables in which
1835 * the aforementioned format assigns the scanned parameters. For
1836 * example in some imaginary function:
1838 * VALUE required1, required2, optional, splat, block
1839 * rb_scan_args(argc, argv, "21*&", &required1, &required2,
1844 * The required parameters must naturally always be exact. The
1845 * optional parameters are set to nil when parameters run out.
1846 * The splat is always an Array, but may be empty if there were
1847 * no parameters that were not consumed by required or optional.
1848 * Lastly, the block may be nil.
1850 int rb_scan_args_19(int argc
, const VALUE
* argv
, const char* spec
, ...);
1851 #define rb_scan_args rb_scan_args_19
1853 /** Raise error if $SAFE is not higher than the given level. */
1854 void rb_secure(int level
);
1856 /** Set $SAFE to given _higher_ level. Lowering $SAFE is not allowed. */
1857 void rb_set_safe_level(int new_level
);
1859 /** Returns the MetaClass object of the object. */
1860 VALUE
rb_singleton_class(VALUE object
);
1862 /** Tries to return a String using #to_str. Error raised if no or invalid conversion. */
1863 VALUE
rb_String(VALUE object
);
1865 /** Returns a Struct with the specified fields. */
1866 VALUE
rb_struct_define(const char *name
, ...);
1868 /** Creat an instance of a struct */
1869 VALUE
rb_struct_new(VALUE klass
, ...);
1871 /** Returns the value of the key. */
1872 VALUE
rb_struct_aref(VALUE s
, VALUE key
);
1873 VALUE
rb_struct_getmember(VALUE s
, ID key
);
1875 /** Sets the value of the key. */
1876 VALUE
rb_struct_aset(VALUE s
, VALUE key
, VALUE value
);
1878 VALUE
rb_struct_s_members(VALUE obj
);
1880 /** Returns a String in locale encoding. */
1881 VALUE
rb_locale_str_new_cstr(const char* string
);
1883 /** Returns a String in locale encoding. */
1884 VALUE
rb_locale_str_new(const char* string
, long len
);
1886 void* rb_alloc_tmp_buffer(VALUE
* s
, long len
);
1887 void rb_free_tmp_buffer(VALUE
* s
);
1889 VALUE
rb_str_export(VALUE
);
1890 VALUE
rb_str_export_locale(VALUE
);
1892 #define ExportStringValue(v) do {\
1893 SafeStringValue(v);\
1894 (v) = rb_str_export(v);\
1897 /** Returns a pointer to a persistent char [] that contains the same data as
1898 * that contained in the Ruby string. The buffer is flushed to the string
1899 * when control returns to Ruby code. The buffer is updated with the string
1900 * contents when control crosses to C code.
1902 * @note This is NOT an MRI C-API function.
1904 char *rb_str_ptr(VALUE self
);
1906 /** Returns a pointer to a persistent char [] that contains the same data as
1907 * that contained in the Ruby string. The buffer is intended to be
1908 * read-only. No changes to the buffer will be propagated to the Ruby
1909 * string, and no changes to the Ruby object will be propagated to the
1910 * buffer. The buffer will persist as long as the Ruby string persists.
1912 * @note This is NOT an MRI C-API function.
1914 char* rb_str_ptr_readonly(VALUE self
);
1915 #define HAVE_RB_STR_PTR_READONLY 1
1917 char* rb_str_ptr_readonly_end(VALUE self
);
1919 /** Appends other String to self and returns the modified self. */
1920 VALUE
rb_str_append(VALUE self
, VALUE other
);
1922 /** Appends other String to self and returns self. @see rb_str_append */
1923 VALUE
rb_str_buf_append(VALUE self
, VALUE other
);
1925 /** Append given number of bytes from C string to and return the String. */
1926 VALUE
rb_str_buf_cat(VALUE string
, const char* other
, size_t size
);
1928 /** Append C string to and return the String. Uses strlen(). @see rb_str_buf_cat */
1929 VALUE
rb_str_buf_cat2(VALUE string
, const char* other
);
1932 * Return new empty String with preallocated storage.
1934 VALUE
rb_str_buf_new(long capacity
);
1936 /** Return a new string based on the given C string */
1937 VALUE
rb_str_buf_new2(const char* string
);
1939 /** Return new String concatenated of the two. */
1940 VALUE
rb_str_cat(VALUE string
, const char* other
, size_t length
);
1942 /** Return new String concatenated of the two. Uses strlen(). @see rb_str_cat */
1943 VALUE
rb_str_cat2(VALUE string
, const char* other
);
1945 /** Compare Strings as Ruby String#<=>. Returns -1, 0 or 1. */
1946 int rb_str_cmp(VALUE first
, VALUE second
);
1948 /** Append other String or character to self, and return the modified self. */
1949 VALUE
rb_str_concat(VALUE self
, VALUE other
);
1951 /** As Ruby's String#dup, returns copy of self as a new String. */
1952 VALUE
rb_str_dup(VALUE self
);
1954 /** Returns an escaped String. */
1955 VALUE
rb_str_inspect(VALUE self
);
1957 /** Returns a symbol created from this string. */
1958 VALUE
rb_str_intern(VALUE self
);
1960 /** Returns the size of the string. It accesses the size directly and does
1961 * not cause the string to be cached.
1963 * @note This is NOT an MRI C-API function.
1965 long rb_str_len(VALUE self
);
1967 void rb_str_set_len(VALUE self
, size_t len
);
1969 /** Create a String using the designated length of given C string. */
1970 /* length is a long because MRI has it as a long, and it also has
1971 * to check that length is greater than 0 properly */
1972 VALUE
rb_str_new(const char* string
, long length
);
1974 /** Create a String from a C string. Alias of rb_str_new2. */
1975 VALUE
rb_str_new_cstr(const char* string
);
1977 /** Create a String from a C string. */
1978 VALUE
rb_str_new2(const char* string
);
1980 /** Create a String from an existing string. */
1981 VALUE
rb_str_new3(VALUE string
);
1983 /** Create a frozen String from an existing string. */
1984 VALUE
rb_str_new4(VALUE string
);
1985 #define rb_str_new_frozen rb_str_new4
1986 #define rb_str_dup_frozen rb_str_new4
1988 VALUE
rb_str_new5(VALUE object
, const char* ptr
, long size
);
1989 #define rb_str_new_with_class rb_str_new5
1991 /** Creates a new String in the external encoding from a pointer and length. */
1992 VALUE
rb_external_str_new(const char* string
, long size
);
1994 /** Creates a new String in the external encoding from a pointer. */
1995 VALUE
rb_external_str_new_cstr(const char* string
);
1997 void rb_str_modify(VALUE str
);
1999 void rb_str_modify_expand(VALUE str
, long expand
);
2001 /** Deprecated alias for rb_obj_freeze */
2002 VALUE
rb_str_freeze(VALUE str
);
2004 /** Returns a new String created from concatenating self with other. */
2005 VALUE
rb_str_plus(VALUE self
, VALUE other
);
2007 /** Makes str at least len characters. */
2008 VALUE
rb_str_resize(VALUE self
, size_t len
);
2010 /** Splits self using the separator string. Returns Array of substrings. */
2011 VALUE
rb_str_split(VALUE self
, const char* separator
);
2013 VALUE
rb_str_subseq(VALUE self
, size_t starting_index
, size_t length
);
2016 * As Ruby's String#slice.
2018 * Returns new String with copy of length characters
2019 * starting from the given index of self. The index
2020 * may be negative. Normal String#slice border conditions
2023 VALUE
rb_str_substr(VALUE self
, size_t starting_index
, size_t length
);
2025 /** Return a new String containing given number of copies of self. */
2026 VALUE
rb_str_times(VALUE self
, VALUE times
);
2028 /** Return an Integer obtained from String#to_i, using the given base. */
2029 VALUE
rb_str2inum(VALUE self
, int base
);
2031 /** Try to return a String using #to_str. Error raised if no or invalid conversion. */
2032 VALUE
rb_str_to_str(VALUE object
);
2034 /** Call #to_s on object pointed to and _replace_ it with the String. */
2035 VALUE
rb_string_value(volatile VALUE
* object_variable
);
2037 char* rb_string_value_ptr(volatile VALUE
* object_variable
);
2039 * As rb_string_value but also returns a C string of the new String.
2041 char* rb_string_value_cstr(volatile VALUE
* object_variable
);
2044 * Returns an editable pointer to the String, the length is returned
2045 * in len parameter, which can be NULL.
2047 char* rb_str2cstr(VALUE string
, long *len
);
2049 long rb_str_hash(VALUE str
);
2051 void rb_str_update(VALUE str
, long beg
, long end
, VALUE replacement
);
2052 void rb_str_free(VALUE str
);
2054 VALUE
rb_sprintf(const char* format
, ...);
2055 VALUE
rb_vsprintf(const char *format
, va_list varargs
);
2057 VALUE
rb_str_equal(VALUE self
, VALUE other
);
2059 VALUE
rb_str_length(VALUE self
);
2061 /** Raises an exception from the value of errno. */
2062 NORETURN(void rb_sys_fail(const char* mesg
));
2064 /** Raises an exception from the value of errno. */
2065 NORETURN(void rb_sys_fail_str(VALUE mesg
));
2067 /** Raises an exception from the value of the given errno. */
2068 NORETURN(void rb_syserr_fail(int err
, const char* mesg
));
2070 /** Evaluate the given string. */
2071 VALUE
rb_eval_string(const char* string
);
2073 /** Create a String from the C string. */
2074 VALUE
rb_tainted_str_new2(const char* string
);
2076 /** Create a String from the C string. */
2077 VALUE
rb_tainted_str_new(const char* string
, long size
);
2079 /** Issue a thread.pass. */
2080 void rb_thread_schedule();
2082 /** Stubbed to always return 0. */
2083 int rb_thread_alone();
2085 /** Request status of file descriptors */
2086 int rb_thread_select(int max
, fd_set
* read
, fd_set
* write
, fd_set
* except
,
2087 struct timeval
*timeval
);
2089 /** Get current thread */
2090 VALUE
rb_thread_current(void);
2092 /** Returns a thread-local value. */
2093 VALUE
rb_thread_local_aref(VALUE thread
, ID id
);
2095 /** Sets a thread-local value. */
2096 VALUE
rb_thread_local_aset(VALUE thread
, ID id
, VALUE value
);
2098 VALUE
rb_thread_wakeup(VALUE thread
);
2100 /** Marks the call as GC independent.
2102 * Seriously, crazy restriction here. While the return value is the
2103 * value returned by func, it MUST not be a VALUE for a reference
2104 * object, since that means that reference objects were mutated
2105 * in an unmanaged way.
2107 * The ubf function is not supported at all, so it can be anything.
2109 typedef VALUE
rb_blocking_function_t(void *);
2110 typedef void rb_unblock_function_t(void *);
2111 VALUE
rb_thread_blocking_region(rb_blocking_function_t
* func
, void* data
,
2112 rb_unblock_function_t
* ubf
, void* ubf_data
);
2114 /* Experimental API. Call +func+ while being GC dependent. */
2115 typedef void* (*rb_thread_call_func
)(void*);
2117 void* rb_thread_call_with_gvl(void* (*func
)(void*), void* data
);
2118 void *rb_thread_call_without_gvl(void *(*func
)(void *), void *data1
,
2119 rb_unblock_function_t
*ubf
, void *data2
);
2120 void *rb_thread_call_without_gvl2(void *(*func
)(void *), void *data1
,
2121 rb_unblock_function_t
*ubf
, void *data2
);
2123 #define HAVE_RB_THREAD_CALL_WITH_GVL 1
2124 #define HAVE_RB_THREAD_CALL_WITHOUT_GVL 1
2125 #define HAVE_RB_THREAD_CALL_WITHOUT_GVL2 1
2127 // Exists only to make extensions happy. It can be read and written to, but
2128 // it controls nothing.
2129 extern int rb_thread_critical
;
2131 // These are used to allow for writing to ruby_verbose and ruby_debug. These
2132 // variables can be read, but writes ignored.
2133 extern int* mri_global_debug();
2134 extern int* mri_global_verbose();
2136 /** Get the current value of the record separator. @internal. */
2137 VALUE
mri_global_rb_rs();
2139 /** Get the value of the default record separator. @internal. */
2140 VALUE
mri_global_rb_default_rs();
2142 /** Get the value of the output record separator */
2143 VALUE
mri_global_rb_output_rs();
2145 /** Get the value of the output field separator */
2146 VALUE
mri_global_rb_output_fs();
2148 void rb_lastline_set(VALUE obj
);
2150 VALUE
rb_lastline_get(void);
2152 #define HAVE_RB_THREAD_BLOCKING_REGION 1
2154 /* 1.9 provides these, so we will too: */
2155 #define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
2156 #define RUBY_UBF_PROCESS ((rb_unblock_function_t *)-1)
2158 /** Return a new time object based on the given offset from the epoch */
2159 VALUE
rb_time_new(time_t sec
, long usec
);
2160 VALUE
rb_time_nano_new(time_t sec
, long nsec
);
2161 VALUE
rb_time_num_new(VALUE ts
, VALUE offset
);
2162 struct timeval
rb_time_interval(VALUE num
);
2163 struct timeval
rb_time_timeval(VALUE time
);
2164 struct timespec
rb_time_timespec(VALUE time
);
2166 /** Returns a time object with UTC/localtime/fixed offset */
2167 VALUE
rb_time_timespec_new(const struct timespec
*ts
, int offset
);
2169 #define HAVE_RB_TIME_NEW 1
2170 #define HAVE_RB_TIME_NANO_NEW 1
2171 #define HAVE_RB_TIME_NUM_NEW 1
2172 #define HAVE_RB_TIME_INTERVAL 1
2173 #define HAVE_RB_TIME_TIMEVAL 1
2174 #define HAVE_RB_TIME_TIMESPEC 1
2176 /** Returns an integer value representing the object's type. */
2177 int rb_type(VALUE object
);
2179 /** Call #to_sym on object. */
2180 ID
rb_to_id(VALUE object
);
2182 /** Converts an object to an Integer by calling #to_int. */
2183 VALUE
rb_to_int(VALUE object
);
2185 /** Module#undefine_method. Objects of class will not respond to name. @see rb_remove_method */
2186 void rb_undef_method(VALUE module
, const char* name
);
2187 void rb_undef(VALUE handle
, ID name
);
2189 /** Call block with given argument or raise error if no block given. */
2190 VALUE
rb_yield(VALUE argument
);
2191 VALUE
rb_yield_values(int n
, ...);
2192 VALUE
rb_yield_splat(VALUE array
);
2194 VALUE
rb_apply(VALUE recv
, ID mid
, VALUE args
);
2196 VALUE
rb_marshal_load(VALUE string
);
2198 VALUE
rb_float_new(double val
);
2199 #define DBL2NUM(dbl) rb_float_new(dbl)
2201 VALUE
rb_Float(VALUE object
);
2203 VALUE
rb_Integer(VALUE object
);
2205 VALUE
rb_Rational(VALUE num
, VALUE den
);
2206 #define rb_Rational1(x) rb_Rational(x, INT2FIX(1))
2207 #define rb_Rational2(x,y) rb_Rational(x, y)
2209 VALUE
rb_rational_new(VALUE
, VALUE
);
2210 #define rb_rational_new1(x) rb_rational_new((x), INT2FIX(1))
2211 #define rb_rational_new2(x,y) rb_rational_new((x), (y))
2213 VALUE
rb_rational_num(VALUE rat
);
2214 VALUE
rb_rational_den(VALUE rat
);
2216 VALUE
rb_Complex(VALUE real
, VALUE imag
);
2217 #define rb_Complex1(x) rb_Complex(x, INT2FIX(0))
2218 #define rb_Complex2(x,y) rb_Complex(x, y)
2220 VALUE
rb_complex_new(VALUE
, VALUE
);
2221 #define rb_complex_new1(x) rb_complex_new((x), INT2FIX(1))
2222 #define rb_complex_new2(x,y) rb_complex_new((x), (y))
2224 NORETURN(void rb_num_zerodiv(void));
2226 NORETURN(void rb_bug(const char *fmt
, ...));
2228 NORETURN(void rb_fatal(const char *fmt
, ...));
2230 NORETURN(void rb_notimplement());
2232 NORETURN(VALUE
rb_f_notimplement(int argc
, VALUE
*argv
, VALUE obj
));
2234 /** Raises an ArgumentError exception. */
2235 NORETURN(void rb_invalid_str(const char *str
, const char *type
));
2237 /** Print a warning if $VERBOSE is not nil. */
2238 void rb_warn(const char *fmt
, ...);
2240 /** Print a warning if $VERBOSE is true. */
2241 void rb_warning(const char *fmt
, ...);
2243 /** Creates a Range object from begin to end */
2244 VALUE
rb_range_new(VALUE begin
, VALUE end
, int exclude_end
);
2246 VALUE
rb_range_beg_len(VALUE range
, long* begp
, long* lenp
, long len
, int err
);
2248 /** Creates Range values and stores begin and end pointer */
2249 int rb_range_values(VALUE range
, VALUE
*begp
, VALUE
*endp
, int *exclp
);
2251 /** Callback to run when shutting down */
2252 void rb_set_end_proc(void* cb
, VALUE cb_data
);
2254 #define RE_OPTION_IGNORECASE 1
2255 #define RE_OPTION_EXTENDED 2
2256 #define RE_OPTION_MULTILINE 4
2258 /** Creates a Regexp object */
2259 VALUE
rb_reg_new(const char *source
, long len
, int options
);
2261 /** Retrieve the nth match for the given MatchData */
2262 VALUE
rb_reg_nth_match(long nth
, VALUE match_data
);
2264 /** Lock functions used by Onigmo */
2265 void capi_reg_lock();
2266 void capi_reg_unlock();
2268 /** New Enumerator. */
2269 VALUE
rb_enumeratorize(VALUE obj
, VALUE meth
, int argc
, VALUE
*argv
);
2271 #define RETURN_ENUMERATOR(obj, argc, argv) do { \
2272 if(!rb_block_given_p()) \
2273 return rb_enumeratorize((obj), ID2SYM(rb_frame_this_func()),\
2277 #define rb_obj_hide(obj) obj
2278 #define rb_obj_reveal(obj, klass) obj
2279 #define rb_str_resurrect(obj) obj
2281 int rb_during_gc(void);
2283 // include an extconf.h if one is provided
2284 #ifdef RUBY_EXTCONF_H
2285 #include RUBY_EXTCONF_H