You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2009/03/25 22:07:04 UTC

svn commit: r758435 - in /incubator/thrift/trunk/lib/rb: ext/ lib/ lib/thrift/ lib/thrift/core_ext/ lib/thrift/protocol/ spec/

Author: bryanduxbury
Date: Wed Mar 25 21:06:53 2009
New Revision: 758435

URL: http://svn.apache.org/viewvc?rev=758435&view=rev
Log:
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.

Added:
    incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/
    incubator/thrift/trunk/lib/rb/lib/thrift/core_ext.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/fixnum.rb
Modified:
    incubator/thrift/trunk/lib/rb/ext/binary_protocol_accelerated.c
    incubator/thrift/trunk/lib/rb/ext/compact_protocol.c
    incubator/thrift/trunk/lib/rb/ext/macros.h
    incubator/thrift/trunk/lib/rb/ext/memory_buffer.c
    incubator/thrift/trunk/lib/rb/ext/struct.c
    incubator/thrift/trunk/lib/rb/lib/thrift.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
    incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec_shared.rb
    incubator/thrift/trunk/lib/rb/spec/client_spec.rb
    incubator/thrift/trunk/lib/rb/spec/server_spec.rb

Modified: incubator/thrift/trunk/lib/rb/ext/binary_protocol_accelerated.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/binary_protocol_accelerated.c?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/binary_protocol_accelerated.c (original)
+++ incubator/thrift/trunk/lib/rb/ext/binary_protocol_accelerated.c Wed Mar 25 21:06:53 2009
@@ -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) {

Modified: incubator/thrift/trunk/lib/rb/ext/compact_protocol.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/compact_protocol.c?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/compact_protocol.c (original)
+++ incubator/thrift/trunk/lib/rb/ext/compact_protocol.c Wed Mar 25 21:06:53 2009
@@ -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);

Modified: incubator/thrift/trunk/lib/rb/ext/macros.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/macros.h?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/macros.h (original)
+++ incubator/thrift/trunk/lib/rb/ext/macros.h Wed Mar 25 21:06:53 2009
@@ -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

Modified: incubator/thrift/trunk/lib/rb/ext/memory_buffer.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/memory_buffer.c?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/memory_buffer.c (original)
+++ incubator/thrift/trunk/lib/rb/ext/memory_buffer.c Wed Mar 25 21:06:53 2009
@@ -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;
   }
 

Modified: incubator/thrift/trunk/lib/rb/ext/struct.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/struct.c?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/struct.c (original)
+++ incubator/thrift/trunk/lib/rb/ext/struct.c Wed Mar 25 21:06:53 2009
@@ -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);
 }

Modified: incubator/thrift/trunk/lib/rb/lib/thrift.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift.rb Wed Mar 25 21:06:53 2009
@@ -24,6 +24,7 @@
   DEPRECATION = false unless const_defined? :DEPRECATION
 end
 
+require 'thrift/core_ext'
 require 'thrift/exceptions'
 require 'thrift/types'
 require 'thrift/processor'

Added: incubator/thrift/trunk/lib/rb/lib/thrift/core_ext.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/core_ext.rb?rev=758435&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/core_ext.rb (added)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/core_ext.rb Wed Mar 25 21:06:53 2009
@@ -0,0 +1,4 @@
+Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each do |file|
+  name = File.basename(file, '.rb')
+  require "thrift/core_ext/#{name}"
+end

Added: incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/fixnum.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/fixnum.rb?rev=758435&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/fixnum.rb (added)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/core_ext/fixnum.rb Wed Mar 25 21:06:53 2009
@@ -0,0 +1,29 @@
+# 
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# Versions of ruby pre 1.8.7 do not have an .ord method available in the Fixnum
+# class.
+#
+if RUBY_VERSION < "1.8.7"
+  class Fixnum
+    def ord
+      self
+    end
+  end
+end
\ No newline at end of file

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb Wed Mar 25 21:06:53 2009
@@ -80,6 +80,7 @@
     end
 
     def write_byte(byte)
+      raise RangeError if byte < -2**31 || byte >= 2**32
       trans.write([byte].pack('c'))
     end
 
@@ -93,6 +94,7 @@
     end
 
     def write_i64(i64)
+      raise RangeError if i64 < -2**63 || i64 >= 2**64
       hi = i64 >> 32
       lo = i64 & 0xffffffff
       trans.write([hi, lo].pack('N2'))
@@ -166,7 +168,7 @@
 
     def read_byte
       dat = trans.read_all(1)
-      val = dat[0]
+      val = dat[0].ord
       if (val > 0x7f)
         val = 0 - ((val - 1) ^ 0xff)
       end

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb Wed Mar 25 21:06:53 2009
@@ -1,3 +1,4 @@
+# encoding: ascii-8bit
 # 
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements. See the NOTICE file

Modified: incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec_shared.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec_shared.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec_shared.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec_shared.rb Wed Mar 25 21:06:53 2009
@@ -88,8 +88,6 @@
       @prot.write_byte(i)
       @trans.read(1).should == [i].pack('c')
     end
-    (-128..127).each do |i|
-    end
     # handing it numbers out of signed range should clip
     @trans.rspec_verify
     (128..255).each do |i|

Modified: incubator/thrift/trunk/lib/rb/spec/client_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/client_spec.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/client_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/client_spec.rb Wed Mar 25 21:06:53 2009
@@ -40,10 +40,10 @@
         @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered
         @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered
         @prot.stub!(:write_message_end)
-        @prot.stub!(:trans).and_return stub_everything("trans")
-        @client.send_message('testMessage', stub_everything("args class"))
-        @client.send_message('testMessage2', stub_everything("args class"))
-        @client.send_message('testMessage3', stub_everything("args class"))
+        @prot.stub!(:trans).and_return mock("trans").as_null_object
+        @client.send_message('testMessage', mock("args class").as_null_object)
+        @client.send_message('testMessage2', mock("args class").as_null_object)
+        @client.send_message('testMessage3', mock("args class").as_null_object)        
       end
     end
 

Modified: incubator/thrift/trunk/lib/rb/spec/server_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/server_spec.rb?rev=758435&r1=758434&r2=758435&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/server_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/server_spec.rb Wed Mar 25 21:06:53 2009
@@ -39,9 +39,9 @@
       x = 0
       @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
         case (x += 1)
-        when 1: raise Thrift::TransportException
-        when 2: raise Thrift::ProtocolException
-        when 3: throw :stop
+        when 1 then raise Thrift::TransportException
+        when 2 then raise Thrift::ProtocolException
+        when 3 then throw :stop
         end
       end
       @trans.should_receive(:close).exactly(3).times
@@ -66,9 +66,9 @@
       x = 0
       @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
         case (x += 1)
-        when 1: raise Thrift::TransportException
-        when 2: raise Thrift::ProtocolException
-        when 3: throw :stop
+        when 1 then raise Thrift::TransportException
+        when 2 then raise Thrift::ProtocolException
+        when 3 then throw :stop
         end
       end
       @trans.should_receive(:close).exactly(3).times
@@ -127,9 +127,9 @@
       error = RuntimeError.new("Stopped")
       @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
         case (x += 1)
-        when 1: raise Thrift::TransportException
-        when 2: raise Thrift::ProtocolException
-        when 3: raise error
+        when 1 then raise Thrift::TransportException
+        when 2 then raise Thrift::ProtocolException
+        when 3 then raise error
         end
       end
       @trans.should_receive(:close).exactly(3).times