THRIFT-374. rb: ruby 1.9 compatibility

This patch updates the thrift_native package to use 1.9 compatible macros and fixes the pure ruby stuff to behave equally well in ruby1.8.6-ruby1.9.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@758435 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/ext/binary_protocol_accelerated.c b/lib/rb/ext/binary_protocol_accelerated.c
index 5d66074..e7ea39e 100644
--- a/lib/rb/ext/binary_protocol_accelerated.c
+++ b/lib/rb/ext/binary_protocol_accelerated.c
@@ -76,7 +76,7 @@
 }
 
 static void write_string_direct(VALUE trans, VALUE str) {
-  write_i32_direct(trans, RSTRING(str)->len);
+  write_i32_direct(trans, RSTRING_LEN(str));
   rb_funcall(trans, write_method_id, 1, str);
 }
 
@@ -200,7 +200,7 @@
     double f;
     int64_t t;
   } transfer;
-  transfer.f = RFLOAT(rb_Float(dub))->value;
+  transfer.f = RFLOAT_VALUE(rb_Float(dub));
   write_i64_direct(GET_TRANSPORT(self), transfer.t);
 
   return Qnil;
@@ -209,8 +209,6 @@
 VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
   CHECK_NIL(str);
   VALUE trans = GET_TRANSPORT(self);
-  // write_i32_direct(trans, RSTRING(str)->len);
-  // rb_funcall(trans, write_method_id, 1, str);
   write_string_direct(trans, str);
   return Qnil;
 }
@@ -225,20 +223,21 @@
 VALUE rb_thrift_binary_proto_read_i16(VALUE self);
 
 static char read_byte_direct(VALUE self) {
-  return (RSTRING(READ(self, 1))->ptr)[0];
+  VALUE buf = READ(self, 1);
+  return RSTRING_PTR(buf)[0];
 }
 
 static int16_t read_i16_direct(VALUE self) {
   VALUE buf = READ(self, 2);
-  return (int16_t)(((uint8_t)(RSTRING(buf)->ptr[1])) | ((uint16_t)((RSTRING(buf)->ptr[0]) << 8)));
+  return (int16_t)(((uint8_t)(RSTRING_PTR(buf)[1])) | ((uint16_t)((RSTRING_PTR(buf)[0]) << 8)));
 }
 
 static int32_t read_i32_direct(VALUE self) {
   VALUE buf = READ(self, 4);
-  return ((uint8_t)(RSTRING(buf)->ptr[3])) | 
-    (((uint8_t)(RSTRING(buf)->ptr[2])) << 8) | 
-    (((uint8_t)(RSTRING(buf)->ptr[1])) << 16) | 
-    (((uint8_t)(RSTRING(buf)->ptr[0])) << 24);
+  return ((uint8_t)(RSTRING_PTR(buf)[3])) | 
+    (((uint8_t)(RSTRING_PTR(buf)[2])) << 8) | 
+    (((uint8_t)(RSTRING_PTR(buf)[1])) << 16) | 
+    (((uint8_t)(RSTRING_PTR(buf)[0])) << 24);
 }
 
 static int64_t read_i64_direct(VALUE self) {
diff --git a/lib/rb/ext/compact_protocol.c b/lib/rb/ext/compact_protocol.c
index d717ff5..7966d3e 100644
--- a/lib/rb/ext/compact_protocol.c
+++ b/lib/rb/ext/compact_protocol.c
@@ -288,7 +288,7 @@
     double f;
     int64_t l;
   } transfer;
-  transfer.f = RFLOAT(rb_Float(dub))->value;
+  transfer.f = RFLOAT_VALUE(rb_Float(dub));
   char buf[8];
   buf[0] = transfer.l & 0xff;
   buf[1] = (transfer.l >> 8) & 0xff;
@@ -304,8 +304,8 @@
 
 VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
   VALUE transport = GET_TRANSPORT(self);
-  write_varint32(transport, RSTRING(str)->len);
-  WRITE(transport, RSTRING(str)->ptr, RSTRING(str)->len);
+  write_varint32(transport, RSTRING_LEN(str));
+  WRITE(transport, RSTRING_PTR(str), RSTRING_LEN(str));
   return Qnil;
 }
 
@@ -354,7 +354,8 @@
 }
 
 static char read_byte_direct(VALUE self) {
-  return (RSTRING(READ(self, 1))->ptr)[0];
+  VALUE buf = READ(self, 1);
+  return RSTRING_PTR(buf)[0];
 }
 
 static int64_t zig_zag_to_ll(int64_t n) {
@@ -527,14 +528,14 @@
     int64_t l;
   } transfer;
   VALUE bytes = READ(self, 8);
-  uint32_t lo = ((uint8_t)(RSTRING(bytes)->ptr[0]))
-    | (((uint8_t)(RSTRING(bytes)->ptr[1])) << 8)
-    | (((uint8_t)(RSTRING(bytes)->ptr[2])) << 16)
-    | (((uint8_t)(RSTRING(bytes)->ptr[3])) << 24);
-  uint64_t hi = (((uint8_t)(RSTRING(bytes)->ptr[4])))
-    | (((uint8_t)(RSTRING(bytes)->ptr[5])) << 8)
-    | (((uint8_t)(RSTRING(bytes)->ptr[6])) << 16)
-    | (((uint8_t)(RSTRING(bytes)->ptr[7])) << 24);
+  uint32_t lo = ((uint8_t)(RSTRING_PTR(bytes)[0]))
+    | (((uint8_t)(RSTRING_PTR(bytes)[1])) << 8)
+    | (((uint8_t)(RSTRING_PTR(bytes)[2])) << 16)
+    | (((uint8_t)(RSTRING_PTR(bytes)[3])) << 24);
+  uint64_t hi = (((uint8_t)(RSTRING_PTR(bytes)[4])))
+    | (((uint8_t)(RSTRING_PTR(bytes)[5])) << 8)
+    | (((uint8_t)(RSTRING_PTR(bytes)[6])) << 16)
+    | (((uint8_t)(RSTRING_PTR(bytes)[7])) << 24);
   transfer.l = (hi << 32) | lo;
 
   return rb_float_new(transfer.f);
diff --git a/lib/rb/ext/macros.h b/lib/rb/ext/macros.h
index 4dd3a57..6dbb7ad 100644
--- a/lib/rb/ext/macros.h
+++ b/lib/rb/ext/macros.h
@@ -22,4 +22,8 @@
 #define GET_STRICT_WRITE(obj) rb_ivar_get(obj, strict_write_ivar_id)
 #define WRITE(obj, data, length) rb_funcall(obj, write_method_id, 1, rb_str_new(data, length))
 #define CHECK_NIL(obj) if (NIL_P(obj)) { rb_raise(rb_eStandardError, "nil argument not allowed!");}
-#define READ(obj, length) rb_funcall(GET_TRANSPORT(obj), read_method_id, 1, INT2FIX(length)) 
\ No newline at end of file
+#define READ(obj, length) rb_funcall(GET_TRANSPORT(obj), read_method_id, 1, INT2FIX(length))
+
+#ifndef RFLOAT_VALUE
+#  define RFLOAT_VALUE(v) RFLOAT(rb_Float(v))->value
+#endif
\ No newline at end of file
diff --git a/lib/rb/ext/memory_buffer.c b/lib/rb/ext/memory_buffer.c
index e37c787..e90de75 100644
--- a/lib/rb/ext/memory_buffer.c
+++ b/lib/rb/ext/memory_buffer.c
@@ -31,7 +31,7 @@
 
 VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str) {
   VALUE buf = GET_BUF(self);
-  rb_str_buf_cat(buf, RSTRING(str)->ptr, RSTRING(str)->len);
+  rb_str_buf_cat(buf, RSTRING_PTR(str), RSTRING_LEN(str));
   return Qnil;
 }
 
@@ -45,11 +45,11 @@
   VALUE data = rb_funcall(buf, slice_method_id, 2, index_value, length_value);
   
   index += length;
-  if (index > RSTRING(buf)->len) {
-    index = RSTRING(buf)->len;
+  if (index > RSTRING_LEN(buf)) {
+    index = RSTRING_LEN(buf);
   }
   if (index >= GARBAGE_BUFFER_SIZE) {
-    rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING(buf)->len - 1)));
+    rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
     index = 0;
   }
 
diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c
index dd02066..4947e26 100644
--- a/lib/rb/ext/struct.c
+++ b/lib/rb/ext/struct.c
@@ -279,10 +279,10 @@
 static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info);
 
 VALUE get_field_value(VALUE obj, VALUE field_name) {
-  char name_buf[RSTRING(field_name)->len + 1];
+  char name_buf[RSTRING_LEN(field_name) + 1];
   
   name_buf[0] = '@';
-  strlcpy(&name_buf[1], RSTRING(field_name)->ptr, sizeof(name_buf));
+  strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
 
   VALUE value = rb_ivar_get(obj, rb_intern(name_buf));
   
@@ -309,7 +309,7 @@
     
     keys = rb_funcall(value, keys_method_id, 0);
     
-    sz = RARRAY(keys)->len;
+    sz = RARRAY_LEN(keys);
     
     mt->write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz));
     
@@ -334,7 +334,7 @@
   } else if (ttype == TTYPE_LIST) {
     Check_Type(value, T_ARRAY);
 
-    sz = RARRAY(value)->len;
+    sz = RARRAY_LEN(value);
 
     VALUE element_type_info = rb_hash_aref(field_info, element_sym);
     VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
@@ -364,7 +364,7 @@
       }
     }
 
-    sz = RARRAY(items)->len;
+    sz = RARRAY_LEN(items);
 
     VALUE element_type_info = rb_hash_aref(field_info, element_sym);
     VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
@@ -431,7 +431,7 @@
   VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0);
   
   int i = 0;
-  for (i=0; i < RARRAY(struct_field_ids_ordered)->len; i++) {
+  for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) {
     VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i);
     VALUE field_info = rb_hash_aref(struct_fields, field_id);
 
@@ -464,10 +464,10 @@
 static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol);
 
 static void set_field_value(VALUE obj, VALUE field_name, VALUE value) {
-  char name_buf[RSTRING(field_name)->len + 1];
+  char name_buf[RSTRING_LEN(field_name) + 1];
 
   name_buf[0] = '@';
-  strlcpy(&name_buf[1], RSTRING(field_name)->ptr, sizeof(name_buf));
+  strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
 
   rb_ivar_set(obj, rb_intern(name_buf), value);
 }