diff options
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/Gemfile.lock | 30 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/encode_decode.c | 62 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/protobuf.c | 2 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/protobuf.h | 2 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/repeated_field.c | 12 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/upb.c | 2 | ||||
-rw-r--r-- | ruby/google-protobuf.gemspec | 11 | ||||
-rw-r--r-- | ruby/lib/google/protobuf.rb | 2 | ||||
-rw-r--r-- | ruby/pom.xml | 2 | ||||
-rw-r--r-- | ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java | 19 | ||||
-rw-r--r-- | ruby/tests/basic.rb | 5 | ||||
-rwxr-xr-x | ruby/travis-test.sh | 2 |
12 files changed, 94 insertions, 57 deletions
diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock deleted file mode 100644 index d0eb9cc4..00000000 --- a/ruby/Gemfile.lock +++ /dev/null @@ -1,30 +0,0 @@ -PATH - remote: . - specs: - google-protobuf (3.0.0.alpha.5.0.5) - -GEM - remote: https://rubygems.org/ - specs: - power_assert (0.2.2) - rake (10.4.2) - rake-compiler (0.9.5) - rake - rake-compiler-dock (0.5.1) - rubygems-tasks (0.2.4) - test-unit (3.0.9) - power_assert - -PLATFORMS - java - ruby - -DEPENDENCIES - google-protobuf! - rake-compiler - rake-compiler-dock - rubygems-tasks - test-unit - -BUNDLED WITH - 1.11.2 diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c index dd21a046..08c72bcc 100644 --- a/ruby/ext/google/protobuf_c/encode_decode.c +++ b/ruby/ext/google/protobuf_c/encode_decode.c @@ -255,10 +255,54 @@ typedef struct { // value into the map. typedef struct { VALUE map; + const map_handlerdata_t* handlerdata; char key_storage[NATIVE_SLOT_MAX_SIZE]; char value_storage[NATIVE_SLOT_MAX_SIZE]; } map_parse_frame_t; +static void MapParseFrame_mark(void* _self) { + map_parse_frame_t* frame = _self; + + // This shouldn't strictly be necessary since this should be rooted by the + // message itself, but it can't hurt. + rb_gc_mark(frame->map); + + native_slot_mark(frame->handlerdata->key_field_type, &frame->key_storage); + native_slot_mark(frame->handlerdata->value_field_type, &frame->value_storage); +} + +void MapParseFrame_free(void* self) { + xfree(self); +} + +rb_data_type_t MapParseFrame_type = { + "MapParseFrame", + { MapParseFrame_mark, MapParseFrame_free, NULL }, +}; + +// Array of Ruby objects wrapping map_parse_frame_t. +// We don't allow multiple concurrent decodes, so we assume that this global +// variable is specific to the "current" decode. +VALUE map_parse_frames; + +static map_parse_frame_t* map_push_frame(VALUE map, + const map_handlerdata_t* handlerdata) { + map_parse_frame_t* frame = ALLOC(map_parse_frame_t); + frame->handlerdata = handlerdata; + frame->map = map; + native_slot_init(handlerdata->key_field_type, &frame->key_storage); + native_slot_init(handlerdata->value_field_type, &frame->value_storage); + + rb_ary_push(map_parse_frames, + TypedData_Wrap_Struct(rb_cObject, &MapParseFrame_type, frame)); + + return frame; +} + +static void map_pop_frame() { + rb_ary_pop(map_parse_frames); +} + // Handler to begin a map entry: allocates a temporary frame. This is the // 'startsubmsg' handler on the msgdef that contains the map field. static void *startmapentry_handler(void *closure, const void *hd) { @@ -266,13 +310,7 @@ static void *startmapentry_handler(void *closure, const void *hd) { const map_handlerdata_t* mapdata = hd; VALUE map_rb = DEREF(msg, mapdata->ofs, VALUE); - map_parse_frame_t* frame = ALLOC(map_parse_frame_t); - frame->map = map_rb; - - native_slot_init(mapdata->key_field_type, &frame->key_storage); - native_slot_init(mapdata->value_field_type, &frame->value_storage); - - return frame; + return map_push_frame(map_rb, mapdata); } // Handler to end a map entry: inserts the value defined during the message into @@ -298,7 +336,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) { &frame->value_storage); Map_index_set(frame->map, key, value); - xfree(frame); + map_pop_frame(); return true; } @@ -737,6 +775,10 @@ VALUE Message_decode(VALUE klass, VALUE data) { msg_rb = rb_class_new_instance(0, NULL, msgklass); TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + // We generally expect this to be clear already, but clear it in case parsing + // previously got interrupted somehow. + rb_ary_clear(map_parse_frames); + { const upb_pbdecodermethod* method = msgdef_decodermethod(desc); const upb_handlers* h = upb_pbdecodermethod_desthandlers(method); @@ -781,6 +823,10 @@ VALUE Message_decode_json(VALUE klass, VALUE data) { msg_rb = rb_class_new_instance(0, NULL, msgklass); TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + // We generally expect this to be clear already, but clear it in case parsing + // previously got interrupted somehow. + rb_ary_clear(map_parse_frames); + { const upb_json_parsermethod* method = msgdef_jsonparsermethod(desc); stackenv se; diff --git a/ruby/ext/google/protobuf_c/protobuf.c b/ruby/ext/google/protobuf_c/protobuf.c index 7cde4aec..98963667 100644 --- a/ruby/ext/google/protobuf_c/protobuf.c +++ b/ruby/ext/google/protobuf_c/protobuf.c @@ -112,4 +112,6 @@ void Init_protobuf_c() { upb_def_to_ruby_obj_map = rb_hash_new(); rb_gc_register_address(&upb_def_to_ruby_obj_map); + map_parse_frames = rb_ary_new(); + rb_gc_register_address(&map_parse_frames); } diff --git a/ruby/ext/google/protobuf_c/protobuf.h b/ruby/ext/google/protobuf_c/protobuf.h index 08b8d516..d5ced567 100644 --- a/ruby/ext/google/protobuf_c/protobuf.h +++ b/ruby/ext/google/protobuf_c/protobuf.h @@ -166,6 +166,8 @@ extern VALUE cBuilder; extern VALUE cError; extern VALUE cParseError; +extern VALUE map_parse_frames; + // We forward-declare all of the Ruby method implementations here because we // sometimes call the methods directly across .c files, rather than going // through Ruby's method dispatching (e.g. during message parse). It's cleaner diff --git a/ruby/ext/google/protobuf_c/repeated_field.c b/ruby/ext/google/protobuf_c/repeated_field.c index 47c207a5..1c651c19 100644 --- a/ruby/ext/google/protobuf_c/repeated_field.c +++ b/ruby/ext/google/protobuf_c/repeated_field.c @@ -447,9 +447,8 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) { */ VALUE RepeatedField_hash(VALUE _self) { RepeatedField* self = ruby_to_RepeatedField(_self); - - VALUE hash = LL2NUM(0); - + st_index_t h = rb_hash_start(0); + VALUE hash_sym = rb_intern("hash"); upb_fieldtype_t field_type = self->field_type; VALUE field_type_class = self->field_type_class; size_t elem_size = native_slot_size(field_type); @@ -457,12 +456,11 @@ VALUE RepeatedField_hash(VALUE _self) { for (int i = 0; i < self->size; i++, off += elem_size) { void* mem = ((uint8_t *)self->elements) + off; VALUE elem = native_slot_get(field_type, field_type_class, mem); - hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2)); - hash = rb_funcall(hash, rb_intern("^"), 1, - rb_funcall(elem, rb_intern("hash"), 0)); + h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0))); } + h = rb_hash_end(h); - return hash; + return INT2FIX(h); } /* diff --git a/ruby/ext/google/protobuf_c/upb.c b/ruby/ext/google/protobuf_c/upb.c index 976a3934..544ebc04 100644 --- a/ruby/ext/google/protobuf_c/upb.c +++ b/ruby/ext/google/protobuf_c/upb.c @@ -11175,7 +11175,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); upb_sink_putstring(&subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&subsink, sel); + upb_sink_endstr(&p->top->sink, sel); multipart_end(p); break; } diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec index 286d8fe3..19be14e5 100644 --- a/ruby/google-protobuf.gemspec +++ b/ruby/google-protobuf.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "google-protobuf" - s.version = "3.0.0" + s.version = "3.1.0" s.licenses = ["BSD"] s.summary = "Protocol Buffers" s.description = "Protocol Buffers are Google's data interchange format." @@ -10,16 +10,17 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.files = Dir.glob('lib/**/*.rb') if RUBY_PLATFORM == "java" + s.platform = "java" s.files += ["lib/google/protobuf_java.jar"] else s.files += Dir.glob('ext/**/*') s.extensions= ["ext/google/protobuf_c/extconf.rb"] - s.add_development_dependency "rake-compiler-dock" + s.add_development_dependency "rake-compiler-dock", "~> 0.5.1" end s.test_files = ["tests/basic.rb", "tests/stress.rb", "tests/generated_code_test.rb"] - s.add_development_dependency "rake-compiler" - s.add_development_dependency "test-unit" - s.add_development_dependency "rubygems-tasks" + s.add_development_dependency "rake-compiler", "~> 0.9.5" + s.add_development_dependency "test-unit", "~> 3.0.9" + s.add_development_dependency "rubygems-tasks", "~> 0.2.4" end diff --git a/ruby/lib/google/protobuf.rb b/ruby/lib/google/protobuf.rb index 62bdd1bf..9b8d8231 100644 --- a/ruby/lib/google/protobuf.rb +++ b/ruby/lib/google/protobuf.rb @@ -45,7 +45,7 @@ if RUBY_PLATFORM == "java" require 'google/protobuf_java' else begin - require "google/#{RUBY_VERSION.sub(/\.\d$/, '')}/protobuf_c" + require "google/#{RUBY_VERSION.sub(/\.\d+$/, '')}/protobuf_c" rescue LoadError require 'google/protobuf_c' end diff --git a/ruby/pom.xml b/ruby/pom.xml index 99e8449b..7b896137 100644 --- a/ruby/pom.xml +++ b/ruby/pom.xml @@ -86,7 +86,7 @@ <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> - <version>3.0.0-beta-4</version> + <version>3.0.0</version> </dependency> </dependencies> </project> diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index 462f8a69..733ccfbc 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -41,6 +41,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; @@ -164,8 +166,21 @@ public class RubyMessage extends RubyObject { */ @JRubyMethod public IRubyObject hash(ThreadContext context) { - int hashCode = System.identityHashCode(this); - return context.runtime.newFixnum(hashCode); + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + for (RubyMap map : maps.values()) { + digest.update((byte) map.hashCode()); + } + for (RubyRepeatedField repeatedField : repeatedFields.values()) { + digest.update((byte) repeatedFields.hashCode()); + } + for (IRubyObject field : fields.values()) { + digest.update((byte) field.hashCode()); + } + return context.runtime.newString(new ByteList(digest.digest())); + } catch (NoSuchAlgorithmException ignore) { + return context.runtime.newFixnum(System.identityHashCode(this)); + } } /* diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index 8b6d329e..989a047e 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -183,12 +183,15 @@ module BasicTest def test_hash m1 = TestMessage.new(:optional_int32 => 42) - m2 = TestMessage.new(:optional_int32 => 102) + m2 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) + m3 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) assert m1.hash != 0 assert m2.hash != 0 + assert m3.hash != 0 # relying on the randomness here -- if hash function changes and we are # unlucky enough to get a collision, then change the values above. assert m1.hash != m2.hash + assert_equal m2.hash, m3.hash end def test_unknown_field_errors diff --git a/ruby/travis-test.sh b/ruby/travis-test.sh index 59e970a6..d3022657 100755 --- a/ruby/travis-test.sh +++ b/ruby/travis-test.sh @@ -8,7 +8,7 @@ test_version() { if [ "$version" == "jruby-1.7" ] ; then # No conformance tests yet -- JRuby is too broken to run them. bash --login -c \ - "rvm install $version && rvm use $version && \ + "rvm install $version && rvm use $version && rvm get head && \ which ruby && \ gem install bundler && bundle && \ rake test" |