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);
}
diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb
index df447a0..d3fe7ab 100644
--- a/lib/rb/lib/thrift.rb
+++ b/lib/rb/lib/thrift.rb
@@ -24,6 +24,7 @@
DEPRECATION = false unless const_defined? :DEPRECATION
end
+require 'thrift/core_ext'
require 'thrift/exceptions'
require 'thrift/types'
require 'thrift/processor'
diff --git a/lib/rb/lib/thrift/core_ext.rb b/lib/rb/lib/thrift/core_ext.rb
new file mode 100644
index 0000000..7a90d0a
--- /dev/null
+++ b/lib/rb/lib/thrift/core_ext.rb
@@ -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
diff --git a/lib/rb/lib/thrift/core_ext/fixnum.rb b/lib/rb/lib/thrift/core_ext/fixnum.rb
new file mode 100644
index 0000000..b4fc90d
--- /dev/null
+++ b/lib/rb/lib/thrift/core_ext/fixnum.rb
@@ -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
diff --git a/lib/rb/lib/thrift/protocol/binaryprotocol.rb b/lib/rb/lib/thrift/protocol/binaryprotocol.rb
index dd963f2..ca9ffea 100644
--- a/lib/rb/lib/thrift/protocol/binaryprotocol.rb
+++ b/lib/rb/lib/thrift/protocol/binaryprotocol.rb
@@ -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
diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb
index 6c75ce8..503c6dd 100644
--- a/lib/rb/lib/thrift/transport.rb
+++ b/lib/rb/lib/thrift/transport.rb
@@ -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
diff --git a/lib/rb/spec/binaryprotocol_spec_shared.rb b/lib/rb/spec/binaryprotocol_spec_shared.rb
index 1d685b6..1ff8d84 100644
--- a/lib/rb/spec/binaryprotocol_spec_shared.rb
+++ b/lib/rb/spec/binaryprotocol_spec_shared.rb
@@ -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|
diff --git a/lib/rb/spec/client_spec.rb b/lib/rb/spec/client_spec.rb
index 980552b..5ce0139 100644
--- a/lib/rb/spec/client_spec.rb
+++ b/lib/rb/spec/client_spec.rb
@@ -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
diff --git a/lib/rb/spec/server_spec.rb b/lib/rb/spec/server_spec.rb
index 545ea28..874085c 100644
--- a/lib/rb/spec/server_spec.rb
+++ b/lib/rb/spec/server_spec.rb
@@ -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