Home | History | Annotate | Download | only in ruby
      1 #include <ruby.h>
      2 
      3 /* Ruby 1.9.1 has a "memoisation optimisation" when compiling with GCC which
      4  * breaks using rb_intern as an lvalue, as SWIG does.  We work around this
      5  * issue for now by disabling this.
      6  * https://sourceforge.net/tracker/?func=detail&aid=2859614&group_id=1645&atid=101645
      7  */
      8 #ifdef rb_intern
      9 # undef rb_intern
     10 #endif
     11 
     12 /* Remove global macros defined in Ruby's win32.h */
     13 #ifdef write
     14 # undef write
     15 #endif
     16 #ifdef read
     17 # undef read
     18 #endif
     19 #ifdef bind
     20 # undef bind
     21 #endif
     22 #ifdef close
     23 # undef close
     24 #endif
     25 #ifdef connect
     26 # undef connect
     27 #endif
     28 
     29 
     30 /* Ruby 1.7 defines NUM2LL(), LL2NUM() and ULL2NUM() macros */
     31 #ifndef NUM2LL
     32 #define NUM2LL(x) NUM2LONG((x))
     33 #endif
     34 #ifndef LL2NUM
     35 #define LL2NUM(x) INT2NUM((long) (x))
     36 #endif
     37 #ifndef ULL2NUM
     38 #define ULL2NUM(x) UINT2NUM((unsigned long) (x))
     39 #endif
     40 
     41 /* Ruby 1.7 doesn't (yet) define NUM2ULL() */
     42 #ifndef NUM2ULL
     43 #ifdef HAVE_LONG_LONG
     44 #define NUM2ULL(x) rb_num2ull((x))
     45 #else
     46 #define NUM2ULL(x) NUM2ULONG(x)
     47 #endif
     48 #endif
     49 
     50 /* RSTRING_LEN, etc are new in Ruby 1.9, but ->ptr and ->len no longer work */
     51 /* Define these for older versions so we can just write code the new way */
     52 #ifndef RSTRING_LEN
     53 # define RSTRING_LEN(x) RSTRING(x)->len
     54 #endif
     55 #ifndef RSTRING_PTR
     56 # define RSTRING_PTR(x) RSTRING(x)->ptr
     57 #endif
     58 #ifndef RSTRING_END
     59 # define RSTRING_END(x) (RSTRING_PTR(x) + RSTRING_LEN(x))
     60 #endif
     61 #ifndef RARRAY_LEN
     62 # define RARRAY_LEN(x) RARRAY(x)->len
     63 #endif
     64 #ifndef RARRAY_PTR
     65 # define RARRAY_PTR(x) RARRAY(x)->ptr
     66 #endif
     67 #ifndef RFLOAT_VALUE
     68 # define RFLOAT_VALUE(x) RFLOAT(x)->value
     69 #endif
     70 #ifndef DOUBLE2NUM
     71 # define DOUBLE2NUM(x) rb_float_new(x)
     72 #endif
     73 #ifndef RHASH_TBL
     74 # define RHASH_TBL(x) (RHASH(x)->tbl)
     75 #endif
     76 #ifndef RHASH_ITER_LEV
     77 # define RHASH_ITER_LEV(x) (RHASH(x)->iter_lev)
     78 #endif
     79 #ifndef RHASH_IFNONE
     80 # define RHASH_IFNONE(x) (RHASH(x)->ifnone)
     81 #endif
     82 #ifndef RHASH_SIZE
     83 # define RHASH_SIZE(x) (RHASH(x)->tbl->num_entries)
     84 #endif
     85 #ifndef RHASH_EMPTY_P
     86 # define RHASH_EMPTY_P(x) (RHASH_SIZE(x) == 0)
     87 #endif
     88 #ifndef RSTRUCT_LEN
     89 # define RSTRUCT_LEN(x) RSTRUCT(x)->len
     90 #endif
     91 #ifndef RSTRUCT_PTR
     92 # define RSTRUCT_PTR(x) RSTRUCT(x)->ptr
     93 #endif
     94 
     95 
     96 
     97 /*
     98  * Need to be very careful about how these macros are defined, especially
     99  * when compiling C++ code or C code with an ANSI C compiler.
    100  *
    101  * VALUEFUNC(f) is a macro used to typecast a C function that implements
    102  * a Ruby method so that it can be passed as an argument to API functions
    103  * like rb_define_method() and rb_define_singleton_method().
    104  *
    105  * VOIDFUNC(f) is a macro used to typecast a C function that implements
    106  * either the "mark" or "free" stuff for a Ruby Data object, so that it
    107  * can be passed as an argument to API functions like Data_Wrap_Struct()
    108  * and Data_Make_Struct().
    109  */
    110  
    111 #ifdef __cplusplus
    112 #  ifndef RUBY_METHOD_FUNC /* These definitions should work for Ruby 1.4.6 */
    113 #    define PROTECTFUNC(f) ((VALUE (*)()) f)
    114 #    define VALUEFUNC(f) ((VALUE (*)()) f)
    115 #    define VOIDFUNC(f)  ((void (*)()) f)
    116 #  else
    117 #    ifndef ANYARGS /* These definitions should work for Ruby 1.6 */
    118 #      define PROTECTFUNC(f) ((VALUE (*)()) f)
    119 #      define VALUEFUNC(f) ((VALUE (*)()) f)
    120 #      define VOIDFUNC(f)  ((RUBY_DATA_FUNC) f)
    121 #    else /* These definitions should work for Ruby 1.7+ */
    122 #      define PROTECTFUNC(f) ((VALUE (*)(VALUE)) f)
    123 #      define VALUEFUNC(f) ((VALUE (*)(ANYARGS)) f)
    124 #      define VOIDFUNC(f)  ((RUBY_DATA_FUNC) f)
    125 #    endif
    126 #  endif
    127 #else
    128 #  define VALUEFUNC(f) (f)
    129 #  define VOIDFUNC(f) (f)
    130 #endif
    131 
    132 /* Don't use for expressions have side effect */
    133 #ifndef RB_STRING_VALUE
    134 #define RB_STRING_VALUE(s) (TYPE(s) == T_STRING ? (s) : (*(volatile VALUE *)&(s) = rb_str_to_str(s)))
    135 #endif
    136 #ifndef StringValue
    137 #define StringValue(s) RB_STRING_VALUE(s)
    138 #endif
    139 #ifndef StringValuePtr
    140 #define StringValuePtr(s) RSTRING_PTR(RB_STRING_VALUE(s))
    141 #endif
    142 #ifndef StringValueLen
    143 #define StringValueLen(s) RSTRING_LEN(RB_STRING_VALUE(s))
    144 #endif
    145 #ifndef SafeStringValue
    146 #define SafeStringValue(v) do {\
    147     StringValue(v);\
    148     rb_check_safe_str(v);\
    149 } while (0)
    150 #endif
    151 
    152 #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
    153 #define rb_define_alloc_func(klass, func) rb_define_singleton_method((klass), "new", VALUEFUNC((func)), -1)
    154 #define rb_undef_alloc_func(klass) rb_undef_method(CLASS_OF((klass)), "new")
    155 #endif
    156 
    157 static VALUE _mSWIG = Qnil;
    158