diff --git a/Lib/ruby/ruby.swg b/Lib/ruby/ruby.swg index d5ca420ee..4b51e3895 100644 --- a/Lib/ruby/ruby.swg +++ b/Lib/ruby/ruby.swg @@ -390,7 +390,7 @@ typedef unsigned long VALUE; %typemap(in) (char *STRING, int LENGTH) { $1 = ($1_ltype) StringValuePtr($input); - $2 = ($2_ltype) RSTRING($input)->len; + $2 = ($2_ltype) StringValueLen($input); } /* Some ANSI C typemaps */ diff --git a/Lib/ruby/rubyhead.swg b/Lib/ruby/rubyhead.swg index e2af52e1d..ba3880990 100644 --- a/Lib/ruby/rubyhead.swg +++ b/Lib/ruby/rubyhead.swg @@ -38,11 +38,6 @@ #endif #endif -/* Ruby 1.8 deprecates STR2CSTR() in favor of StringValuePtr() */ -#ifndef StringValuePtr -#define StringValuePtr(x) STR2CSTR((x)) -#endif - /* * Need to be very careful about how these macros are defined, especially * when compiling C++ code or C code with an ANSI C compiler. @@ -80,3 +75,28 @@ typedef struct { void (*mark)(void *); void (*destroy)(void *); } swig_class; + +/* Don't use for expressions have side effect */ +#ifndef RB_STRING_VALUE +#define RB_STRING_VALUE(s) (TYPE(s) == T_STRING ? (s) : (*(volatile VALUE *)&(s) = rb_str_to_str(s))) +#endif +#ifndef StringValue +#define StringValue(s) RB_STRING_VALUE(s) +#endif +#ifndef StringValuePtr +#define StringValuePtr(s) RSTRING(RB_STRING_VALUE(s))->ptr +#endif +#ifndef StringValueLen +#define StringValueLen(s) RSTRING(RB_STRING_VALUE(s))->len +#endif +#ifndef SafeStringValue +#define SafeStringValue(v) do {\ + StringValue(v);\ + rb_check_safe_str(v);\ +} while (0) +#endif + +#ifndef HAVE_RB_DEFINE_ALLOC_FUNC +#define rb_undef_alloc_func(klass) rb_undef_method(CLASS_OF(klass), "new") +#endif + diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index e6993e800..52ae0b021 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -458,8 +458,8 @@ public: void create_command(Node *n, const String_or_char *iname) { + String *alloc_func_name = Swig_name_wrapper(iname); String *wname = Swig_name_wrapper(iname); - String *wname2 = Swig_name_wrapper(iname); if (CPlusPlus) { Insert(wname,0,"VALUEFUNC("); Append(wname,")"); @@ -479,13 +479,11 @@ public: iname, "\", ", wname, ", -1);\n", NIL); break; case CONSTRUCTOR_ALLOCATE: - Printv(s, "#ifdef HAVE_RB_DEFINE_ALLOC_FUNC\n", NIL); - Printv(s, tab4, "rb_define_alloc_func(", klass->vname, - ", ", wname2, ");\n", NIL); - Printv(s, "#else\n", NIL); - Printv(s, tab4, "rb_define_singleton_method(", klass->vname, - ", \"new\", ", wname, ", -1);\n", NIL); - Printv(s, "#endif\n", NIL); + Printf(s, "#ifdef HAVE_RB_DEFINE_ALLOC_FUNC\n"); + Printv(s, tab4, "rb_define_alloc_func(", klass->vname, ", ", alloc_func_name, ");\n", NIL); + Printf(s, "#else\n"); + Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"new\", ", wname, ", -1);\n", NIL); + Printf(s, "#endif\n"); Replaceall(klass->init,"$allocator", s); break; case CONSTRUCTOR_INITIALIZE: @@ -516,7 +514,7 @@ public: Delete(temp); Delete(s); Delete(wname); - Delete(wname2); + Delete(alloc_func_name); } /* --------------------------------------------------------------------- @@ -1342,10 +1340,7 @@ public: Printv(f_header, klass->header,NIL); String *s = NewString(""); - Printv(s, "#ifndef HAVE_RB_DEFINE_ALLOC_FUNC\n", NIL); - Printv(s, tab4, "rb_undef_method(CLASS_OF(", klass->vname, - "), \"new\");\n", NIL); - Printv(s, "#endif\n", NIL); + Printv(s, tab4, "rb_undef_alloc_func(", klass->vname, ");\n", NIL); Replaceall(klass->init,"$allocator", s); Replaceall(klass->init,"$initializer", ""); Replaceall(klass->init,"$super", "rb_cObject");