From 09b47cc536ebd951c231fd5e09b4382a25b98020 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 07:00:19 +0900 Subject: [PATCH] ruby: fixes compatibility with ruby-1.8.5 --- ruby/{encoding.h => compat.h} | 35 +++++++++++++++++++++++++++++++---- ruby/pack.c | 6 +++--- ruby/rbinit.c | 6 +++--- ruby/test/test_pack_unpack.rb | 12 ++++++++---- ruby/unpack.c | 12 +++--------- 5 files changed, 48 insertions(+), 23 deletions(-) rename ruby/{encoding.h => compat.h} (60%) diff --git a/ruby/encoding.h b/ruby/compat.h similarity index 60% rename from ruby/encoding.h rename to ruby/compat.h index 2ad3fd7f..98c88816 100644 --- a/ruby/encoding.h +++ b/ruby/compat.h @@ -15,19 +15,46 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef ENCODING_H__ -#define ENCODING_H__ +#ifndef COMPAT_H__ +#define COMPAT_H__ #ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" -#define MSGPACK_RUBY_ENCODING +#define COMPAT_HAVE_ENCODING extern int s_enc_utf8; extern int s_enc_ascii8bit; extern int s_enc_usascii; extern VALUE s_enc_utf8_value; #endif +#ifdef RUBY_VM +#define COMPAT_RERAISE rb_exc_raise(rb_errinfo()) +#else +#define COMPAT_RERAISE rb_exc_raise(ruby_errinfo) +#endif -#endif /* encoding.h */ + +/* ruby 1.8.5 */ +#ifndef RSTRING_PTR +#define RSTRING_PTR(s) (RSTRING(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RSTRING_LEN +#define RSTRING_LEN(s) (RSTRING(s)->len) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_PTR +#define RARRAY_PTR(s) (RARRAY(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_LEN +#define RARRAY_LEN(s) (RARRAY(s)->len) +#endif + + +#endif /* compat.h */ diff --git a/ruby/pack.c b/ruby/pack.c index 35878c7d..49b69fc9 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -16,7 +16,7 @@ * limitations under the License. */ #include "ruby.h" -#include "encoding.h" +#include "compat.h" #include "msgpack/pack_define.h" @@ -169,7 +169,7 @@ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING int enc = ENCODING_GET(self); if(enc != s_enc_utf8 && enc != s_enc_ascii8bit && enc != s_enc_usascii) { if(!ENC_CODERANGE_ASCIIONLY(self)) { @@ -193,7 +193,7 @@ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); #else ARG_BUFFER(out, argc, argv); diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 46781594..1d1cbc69 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -17,11 +17,11 @@ */ #include "pack.h" #include "unpack.h" -#include "encoding.h" +#include "compat.h" static VALUE mMessagePack; -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING int s_enc_utf8; int s_enc_ascii8bit; int s_enc_usascii; @@ -54,7 +54,7 @@ void Init_msgpack(void) rb_define_const(mMessagePack, "VERSION", rb_str_new2(MESSAGEPACK_VERSION)); -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING s_enc_ascii8bit = rb_ascii8bit_encindex(); s_enc_utf8 = rb_utf8_encindex(); s_enc_usascii = rb_usascii_encindex(); diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb index 25bde81e..545e5939 100644 --- a/ruby/test/test_pack_unpack.rb +++ b/ruby/test/test_pack_unpack.rb @@ -153,7 +153,8 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "{1=>1}" do - match ({1=>1}), "\x81\x01\x01" + obj = {1=>1} + match obj, "\x81\x01\x01" end it "1.0" do @@ -165,15 +166,18 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "[0, 1, ..., 14]" do - match (0..14).to_a, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" + obj = (0..14).to_a + match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" end it "[0, 1, ..., 15]" do - match (0..15).to_a, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + obj = (0..15).to_a + match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" end it "{}" do - match ({}), "\x80" + obj = {} + match obj, "\x80" end ## FIXME diff --git a/ruby/unpack.c b/ruby/unpack.c index 3c5e3507..2d10e75d 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -16,7 +16,7 @@ * limitations under the License. */ #include "ruby.h" -#include "encoding.h" +#include "compat.h" #include "msgpack/unpack_define.h" @@ -132,7 +132,7 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha } else { *o = rb_str_substr(u->source, p - b, l); } -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING ENCODING_SET(*o, s_enc_utf8); #endif return 0; @@ -155,17 +155,11 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha rb_raise(rb_eTypeError, "instance of String needed"); \ } -#ifdef RUBY_VM -#define RERAISE rb_exc_raise(rb_errinfo()) -#else -#define RERAISE rb_exc_raise(ruby_errinfo) -#endif - static VALUE template_execute_rescue(VALUE nouse) { rb_gc_enable(); - RERAISE; + COMPAT_RERAISE; } static VALUE template_execute_do(VALUE argv)