google-protobuf 3.22.3 → 4.27.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +28 -72
- data/ext/google/protobuf_c/convert.h +3 -28
- data/ext/google/protobuf_c/defs.c +534 -72
- data/ext/google/protobuf_c/defs.h +3 -28
- data/ext/google/protobuf_c/extconf.rb +2 -1
- data/ext/google/protobuf_c/glue.c +72 -0
- data/ext/google/protobuf_c/map.c +28 -36
- data/ext/google/protobuf_c/map.h +6 -28
- data/ext/google/protobuf_c/message.c +101 -148
- data/ext/google/protobuf_c/message.h +10 -32
- data/ext/google/protobuf_c/protobuf.c +53 -177
- data/ext/google/protobuf_c/protobuf.h +24 -32
- data/ext/google/protobuf_c/repeated_field.c +23 -33
- data/ext/google/protobuf_c/repeated_field.h +6 -28
- data/ext/google/protobuf_c/ruby-upb.c +12741 -10337
- data/ext/google/protobuf_c/ruby-upb.h +8911 -5235
- data/ext/google/protobuf_c/shared_convert.c +69 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +37 -0
- data/ext/google/protobuf_c/shared_message.h +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +467 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
- data/ext/google/protobuf_c/wrap_memcpy.c +3 -26
- data/lib/google/protobuf/any_pb.rb +6 -8
- data/lib/google/protobuf/api_pb.rb +6 -26
- data/lib/google/protobuf/descriptor_pb.rb +20 -252
- data/lib/google/protobuf/duration_pb.rb +6 -8
- data/lib/google/protobuf/empty_pb.rb +6 -6
- data/lib/google/protobuf/ffi/descriptor.rb +166 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +77 -0
- data/lib/google/protobuf/ffi/enum_descriptor.rb +173 -0
- data/lib/google/protobuf/ffi/ffi.rb +210 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +330 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +49 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +289 -0
- data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
- data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
- data/lib/google/protobuf/ffi/map.rb +409 -0
- data/lib/google/protobuf/ffi/message.rb +659 -0
- data/lib/google/protobuf/ffi/method_descriptor.rb +114 -0
- data/lib/google/protobuf/ffi/object_cache.rb +30 -0
- data/lib/google/protobuf/ffi/oneof_descriptor.rb +97 -0
- data/lib/google/protobuf/ffi/repeated_field.rb +385 -0
- data/lib/google/protobuf/ffi/service_descriptor.rb +107 -0
- data/lib/google/protobuf/field_mask_pb.rb +6 -7
- data/lib/google/protobuf/internal/object_cache.rb +99 -0
- data/lib/google/protobuf/message_exts.rb +3 -26
- data/lib/google/protobuf/plugin_pb.rb +6 -31
- data/lib/google/protobuf/repeated_field.rb +7 -31
- data/lib/google/protobuf/source_context_pb.rb +6 -7
- data/lib/google/protobuf/struct_pb.rb +6 -23
- data/lib/google/protobuf/timestamp_pb.rb +6 -8
- data/lib/google/protobuf/type_pb.rb +6 -71
- data/lib/google/protobuf/well_known_types.rb +5 -34
- data/lib/google/protobuf/wrappers_pb.rb +6 -31
- data/lib/google/protobuf.rb +27 -45
- data/lib/google/protobuf_ffi.rb +51 -0
- data/lib/google/protobuf_native.rb +19 -0
- data/lib/google/tasks/ffi.rake +100 -0
- metadata +89 -8
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
- data/lib/google/protobuf/descriptor_dsl.rb +0 -465
@@ -1,32 +1,9 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2014 Google Inc. All rights reserved.
|
3
|
-
// https://developers.google.com/protocol-buffers/
|
4
3
|
//
|
5
|
-
//
|
6
|
-
//
|
7
|
-
//
|
8
|
-
//
|
9
|
-
// * Redistributions of source code must retain the above copyright
|
10
|
-
// notice, this list of conditions and the following disclaimer.
|
11
|
-
// * Redistributions in binary form must reproduce the above
|
12
|
-
// copyright notice, this list of conditions and the following disclaimer
|
13
|
-
// in the documentation and/or other materials provided with the
|
14
|
-
// distribution.
|
15
|
-
// * Neither the name of Google Inc. nor the names of its
|
16
|
-
// contributors may be used to endorse or promote products derived from
|
17
|
-
// this software without specific prior written permission.
|
18
|
-
//
|
19
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
4
|
+
// Use of this source code is governed by a BSD-style
|
5
|
+
// license that can be found in the LICENSE file or at
|
6
|
+
// https://developers.google.com/open-source/licenses/bsd
|
30
7
|
|
31
8
|
#include "message.h"
|
32
9
|
|
@@ -35,6 +12,7 @@
|
|
35
12
|
#include "map.h"
|
36
13
|
#include "protobuf.h"
|
37
14
|
#include "repeated_field.h"
|
15
|
+
#include "shared_message.h"
|
38
16
|
|
39
17
|
static VALUE cParseError = Qnil;
|
40
18
|
static VALUE cAbstractMessage = Qnil;
|
@@ -53,6 +31,8 @@ VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
|
|
53
31
|
// -----------------------------------------------------------------------------
|
54
32
|
|
55
33
|
typedef struct {
|
34
|
+
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
|
35
|
+
// macro to update VALUE references, as to trigger write barriers.
|
56
36
|
VALUE arena;
|
57
37
|
const upb_Message* msg; // Can get as mutable when non-frozen.
|
58
38
|
const upb_MessageDef*
|
@@ -64,10 +44,12 @@ static void Message_mark(void* _self) {
|
|
64
44
|
rb_gc_mark(self->arena);
|
65
45
|
}
|
66
46
|
|
47
|
+
static size_t Message_memsize(const void* _self) { return sizeof(Message); }
|
48
|
+
|
67
49
|
static rb_data_type_t Message_type = {
|
68
|
-
"Message",
|
69
|
-
{Message_mark, RUBY_DEFAULT_FREE,
|
70
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
50
|
+
"Google::Protobuf::Message",
|
51
|
+
{Message_mark, RUBY_DEFAULT_FREE, Message_memsize},
|
52
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
71
53
|
};
|
72
54
|
|
73
55
|
static Message* ruby_to_Message(VALUE msg_rb) {
|
@@ -105,8 +87,10 @@ upb_Message* Message_GetMutable(VALUE msg_rb, const upb_MessageDef** m) {
|
|
105
87
|
void Message_InitPtr(VALUE self_, upb_Message* msg, VALUE arena) {
|
106
88
|
Message* self = ruby_to_Message(self_);
|
107
89
|
self->msg = msg;
|
108
|
-
self->arena
|
109
|
-
|
90
|
+
RB_OBJ_WRITE(self_, &self->arena, arena);
|
91
|
+
VALUE stored = ObjectCache_TryAdd(msg, self_);
|
92
|
+
(void)stored;
|
93
|
+
PBRUBY_ASSERT(stored == self_);
|
110
94
|
}
|
111
95
|
|
112
96
|
VALUE Message_GetArena(VALUE msg_rb) {
|
@@ -506,7 +490,8 @@ static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
506
490
|
k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
|
507
491
|
|
508
492
|
if (map_init->val_type.type == kUpb_CType_Message && TYPE(val) == T_HASH) {
|
509
|
-
upb_MiniTable* t =
|
493
|
+
const upb_MiniTable* t =
|
494
|
+
upb_MessageDef_MiniTable(map_init->val_type.def.msgdef);
|
510
495
|
upb_Message* msg = upb_Message_New(t, map_init->arena);
|
511
496
|
Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
|
512
497
|
map_init->arena);
|
@@ -537,7 +522,7 @@ static upb_MessageValue MessageValue_FromValue(VALUE val, TypeInfo info,
|
|
537
522
|
upb_Arena* arena) {
|
538
523
|
if (info.type == kUpb_CType_Message) {
|
539
524
|
upb_MessageValue msgval;
|
540
|
-
upb_MiniTable* t = upb_MessageDef_MiniTable(info.def.msgdef);
|
525
|
+
const upb_MiniTable* t = upb_MessageDef_MiniTable(info.def.msgdef);
|
541
526
|
upb_Message* msg = upb_Message_New(t, arena);
|
542
527
|
Message_InitFromValue(msg, info.def.msgdef, val, arena);
|
543
528
|
msgval.msg_val = msg;
|
@@ -653,7 +638,7 @@ static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
653
638
|
Message* self = ruby_to_Message(_self);
|
654
639
|
VALUE arena_rb = Arena_new();
|
655
640
|
upb_Arena* arena = Arena_get(arena_rb);
|
656
|
-
upb_MiniTable* t = upb_MessageDef_MiniTable(self->msgdef);
|
641
|
+
const upb_MiniTable* t = upb_MessageDef_MiniTable(self->msgdef);
|
657
642
|
upb_Message* msg = upb_Message_New(t, arena);
|
658
643
|
|
659
644
|
Message_InitPtr(_self, msg, arena_rb);
|
@@ -678,44 +663,12 @@ static VALUE Message_dup(VALUE _self) {
|
|
678
663
|
Message* self = ruby_to_Message(_self);
|
679
664
|
VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
680
665
|
Message* new_msg_self = ruby_to_Message(new_msg);
|
681
|
-
|
682
|
-
|
683
|
-
// TODO(copy unknown fields?)
|
684
|
-
// TODO(use official upb msg copy function)
|
685
|
-
memcpy((upb_Message*)new_msg_self->msg, self->msg, size);
|
666
|
+
const upb_MiniTable* m = upb_MessageDef_MiniTable(self->msgdef);
|
667
|
+
upb_Message_ShallowCopy((upb_Message*)new_msg_self->msg, self->msg, m);
|
686
668
|
Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
|
687
669
|
return new_msg;
|
688
670
|
}
|
689
671
|
|
690
|
-
// Support function for Message_eq, and also used by other #eq functions.
|
691
|
-
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
692
|
-
const upb_MessageDef* m) {
|
693
|
-
if (m1 == m2) return true;
|
694
|
-
|
695
|
-
size_t size1, size2;
|
696
|
-
int encode_opts =
|
697
|
-
kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic;
|
698
|
-
upb_Arena* arena_tmp = upb_Arena_New();
|
699
|
-
const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
|
700
|
-
|
701
|
-
// Compare deterministically serialized payloads with no unknown fields.
|
702
|
-
char* data1;
|
703
|
-
char* data2;
|
704
|
-
upb_EncodeStatus status1 =
|
705
|
-
upb_Encode(m1, layout, encode_opts, arena_tmp, &data1, &size1);
|
706
|
-
upb_EncodeStatus status2 =
|
707
|
-
upb_Encode(m2, layout, encode_opts, arena_tmp, &data2, &size2);
|
708
|
-
|
709
|
-
if (status1 == kUpb_EncodeStatus_Ok && status2 == kUpb_EncodeStatus_Ok) {
|
710
|
-
bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
|
711
|
-
upb_Arena_Free(arena_tmp);
|
712
|
-
return ret;
|
713
|
-
} else {
|
714
|
-
upb_Arena_Free(arena_tmp);
|
715
|
-
rb_raise(cParseError, "Error comparing messages");
|
716
|
-
}
|
717
|
-
}
|
718
|
-
|
719
672
|
/*
|
720
673
|
* call-seq:
|
721
674
|
* Message.==(other) => boolean
|
@@ -732,28 +685,22 @@ static VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
732
685
|
Message* other = ruby_to_Message(_other);
|
733
686
|
assert(self->msgdef == other->msgdef);
|
734
687
|
|
735
|
-
|
688
|
+
const upb_MiniTable* m = upb_MessageDef_MiniTable(self->msgdef);
|
689
|
+
const int options = 0;
|
690
|
+
return upb_Message_IsEqual(self->msg, other->msg, m, options) ? Qtrue
|
691
|
+
: Qfalse;
|
736
692
|
}
|
737
693
|
|
738
694
|
uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
739
695
|
uint64_t seed) {
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
upb_EncodeStatus status = upb_Encode(
|
746
|
-
msg, upb_MessageDef_MiniTable(m),
|
747
|
-
kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic, arena,
|
748
|
-
&data, &size);
|
749
|
-
|
750
|
-
if (status == kUpb_EncodeStatus_Ok) {
|
751
|
-
uint64_t ret = _upb_Hash(data, size, seed);
|
752
|
-
upb_Arena_Free(arena);
|
753
|
-
return ret;
|
696
|
+
upb_Status status;
|
697
|
+
upb_Status_Clear(&status);
|
698
|
+
uint64_t return_value = shared_Message_Hash(msg, m, seed, &status);
|
699
|
+
if (upb_Status_IsOk(&status)) {
|
700
|
+
return return_value;
|
754
701
|
} else {
|
755
|
-
|
756
|
-
|
702
|
+
rb_raise(cParseError, "Message_Hash(): %s",
|
703
|
+
upb_Status_ErrorMessage(&status));
|
757
704
|
}
|
758
705
|
}
|
759
706
|
|
@@ -810,58 +757,34 @@ static VALUE Message_CreateHash(const upb_Message* msg,
|
|
810
757
|
if (!msg) return Qnil;
|
811
758
|
|
812
759
|
VALUE hash = rb_hash_new();
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
// This is unfortunate, we should key behaviors off field attributes (like
|
818
|
-
// whether a field has presence), not proto2 vs. proto3. We should see if we
|
819
|
-
// can change this without breaking users.
|
820
|
-
is_proto2 = upb_MessageDef_Syntax(m) == kUpb_Syntax_Proto2;
|
821
|
-
|
822
|
-
for (int i = 0; i < n; i++) {
|
823
|
-
const upb_FieldDef* field = upb_MessageDef_Field(m, i);
|
824
|
-
TypeInfo type_info = TypeInfo_get(field);
|
825
|
-
upb_MessageValue msgval;
|
826
|
-
VALUE msg_value;
|
827
|
-
VALUE msg_key;
|
760
|
+
size_t iter = kUpb_Message_Begin;
|
761
|
+
const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(m));
|
762
|
+
const upb_FieldDef* field;
|
763
|
+
upb_MessageValue val;
|
828
764
|
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
//
|
833
|
-
msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
834
|
-
rb_hash_aset(hash, msg_key, Qnil);
|
765
|
+
while (upb_Message_Next(msg, m, pool, &field, &val, &iter)) {
|
766
|
+
if (upb_FieldDef_IsExtension(field)) {
|
767
|
+
// TODO: allow extensions once we have decided what naming scheme the
|
768
|
+
// symbol should use. eg. :"[pkg.ext]"
|
835
769
|
continue;
|
836
770
|
}
|
837
771
|
|
838
|
-
|
839
|
-
|
840
|
-
!upb_Message_HasFieldByDef(msg, field)) {
|
841
|
-
continue;
|
842
|
-
}
|
843
|
-
|
844
|
-
msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
845
|
-
msgval = upb_Message_GetFieldByDef(msg, field);
|
846
|
-
|
847
|
-
// Proto2 omits empty map/repeated filds also.
|
772
|
+
TypeInfo type_info = TypeInfo_get(field);
|
773
|
+
VALUE msg_value;
|
848
774
|
|
849
775
|
if (upb_FieldDef_IsMap(field)) {
|
850
776
|
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
|
851
777
|
const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry_m, 1);
|
852
778
|
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
|
853
779
|
upb_CType key_type = upb_FieldDef_CType(key_f);
|
854
|
-
msg_value = Map_CreateHash(
|
780
|
+
msg_value = Map_CreateHash(val.map_val, key_type, TypeInfo_get(val_f));
|
855
781
|
} else if (upb_FieldDef_IsRepeated(field)) {
|
856
|
-
|
857
|
-
(!msgval.array_val || upb_Array_Size(msgval.array_val) == 0)) {
|
858
|
-
continue;
|
859
|
-
}
|
860
|
-
msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
|
782
|
+
msg_value = RepeatedField_CreateArray(val.array_val, type_info);
|
861
783
|
} else {
|
862
|
-
msg_value = Scalar_CreateHash(
|
784
|
+
msg_value = Scalar_CreateHash(val, type_info);
|
863
785
|
}
|
864
786
|
|
787
|
+
VALUE msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
865
788
|
rb_hash_aset(hash, msg_key, msg_value);
|
866
789
|
}
|
867
790
|
|
@@ -894,11 +817,27 @@ static VALUE Message_to_h(VALUE _self) {
|
|
894
817
|
* Freezes the message object. We have to intercept this so we can pin the
|
895
818
|
* Ruby object into memory so we don't forget it's frozen.
|
896
819
|
*/
|
897
|
-
|
820
|
+
VALUE Message_freeze(VALUE _self) {
|
898
821
|
Message* self = ruby_to_Message(_self);
|
899
|
-
|
900
|
-
|
901
|
-
|
822
|
+
|
823
|
+
if (RB_OBJ_FROZEN(_self)) return _self;
|
824
|
+
Arena_Pin(self->arena, _self);
|
825
|
+
RB_OBJ_FREEZE(_self);
|
826
|
+
|
827
|
+
int n = upb_MessageDef_FieldCount(self->msgdef);
|
828
|
+
for (int i = 0; i < n; i++) {
|
829
|
+
const upb_FieldDef* f = upb_MessageDef_Field(self->msgdef, i);
|
830
|
+
VALUE field = Message_getfield(_self, f);
|
831
|
+
|
832
|
+
if (field != Qnil) {
|
833
|
+
if (upb_FieldDef_IsMap(f)) {
|
834
|
+
Map_freeze(field);
|
835
|
+
} else if (upb_FieldDef_IsRepeated(f)) {
|
836
|
+
RepeatedField_freeze(field);
|
837
|
+
} else if (upb_FieldDef_IsSubMessage(f)) {
|
838
|
+
Message_freeze(field);
|
839
|
+
}
|
840
|
+
}
|
902
841
|
}
|
903
842
|
return _self;
|
904
843
|
}
|
@@ -955,7 +894,7 @@ static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
955
894
|
* MessageClass.decode(data, options) => message
|
956
895
|
*
|
957
896
|
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
958
|
-
* format) under the
|
897
|
+
* format) under the interpretation given by this message class's definition
|
959
898
|
* and returns a message object with the corresponding field values.
|
960
899
|
* @param options [Hash] options for the decoder
|
961
900
|
* recursion_limit: set to maximum decoding depth for message (default is 64)
|
@@ -978,7 +917,7 @@ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
|
|
978
917
|
rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
979
918
|
|
980
919
|
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
981
|
-
options |=
|
920
|
+
options |= upb_DecodeOptions_MaxDepth(FIX2INT(depth));
|
982
921
|
}
|
983
922
|
}
|
984
923
|
|
@@ -986,18 +925,27 @@ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
|
|
986
925
|
rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
|
987
926
|
}
|
988
927
|
|
928
|
+
return Message_decode_bytes(RSTRING_LEN(data), RSTRING_PTR(data), options,
|
929
|
+
klass, /*freeze*/ false);
|
930
|
+
}
|
931
|
+
|
932
|
+
VALUE Message_decode_bytes(int size, const char* bytes, int options,
|
933
|
+
VALUE klass, bool freeze) {
|
989
934
|
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
990
935
|
Message* msg = ruby_to_Message(msg_rb);
|
991
936
|
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
937
|
+
const upb_FileDef* file = upb_MessageDef_File(msg->msgdef);
|
938
|
+
const upb_ExtensionRegistry* extreg =
|
939
|
+
upb_DefPool_ExtensionRegistry(upb_FileDef_Pool(file));
|
940
|
+
upb_DecodeStatus status = upb_Decode(bytes, size, (upb_Message*)msg->msg,
|
941
|
+
upb_MessageDef_MiniTable(msg->msgdef),
|
942
|
+
extreg, options, Arena_get(msg->arena));
|
997
943
|
if (status != kUpb_DecodeStatus_Ok) {
|
998
944
|
rb_raise(cParseError, "Error occurred during parsing");
|
999
945
|
}
|
1000
|
-
|
946
|
+
if (freeze) {
|
947
|
+
Message_freeze(msg_rb);
|
948
|
+
}
|
1001
949
|
return msg_rb;
|
1002
950
|
}
|
1003
951
|
|
@@ -1018,9 +966,6 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1018
966
|
int options = 0;
|
1019
967
|
upb_Status status;
|
1020
968
|
|
1021
|
-
// TODO(haberman): use this message's pool instead.
|
1022
|
-
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
1023
|
-
|
1024
969
|
if (argc < 1 || argc > 2) {
|
1025
970
|
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
1026
971
|
}
|
@@ -1041,7 +986,7 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1041
986
|
rb_raise(rb_eArgError, "Expected string for JSON data.");
|
1042
987
|
}
|
1043
988
|
|
1044
|
-
// TODO
|
989
|
+
// TODO: Check and respect string encoding. If not UTF-8, we need to
|
1045
990
|
// convert, because string handlers pass data directly to message string
|
1046
991
|
// fields.
|
1047
992
|
|
@@ -1054,8 +999,9 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1054
999
|
}
|
1055
1000
|
|
1056
1001
|
upb_Status_Clear(&status);
|
1002
|
+
const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(msg->msgdef));
|
1057
1003
|
if (!upb_JsonDecode(RSTRING_PTR(data), RSTRING_LEN(data),
|
1058
|
-
(upb_Message*)msg->msg, msg->msgdef,
|
1004
|
+
(upb_Message*)msg->msg, msg->msgdef, pool, options,
|
1059
1005
|
Arena_get(msg->arena), &status)) {
|
1060
1006
|
rb_raise(cParseError, "Error occurred during parsing: %s",
|
1061
1007
|
upb_Status_ErrorMessage(&status));
|
@@ -1096,7 +1042,7 @@ static VALUE Message_encode(int argc, VALUE* argv, VALUE klass) {
|
|
1096
1042
|
rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
1097
1043
|
|
1098
1044
|
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
1099
|
-
options |=
|
1045
|
+
options |= upb_DecodeOptions_MaxDepth(FIX2INT(depth));
|
1100
1046
|
}
|
1101
1047
|
}
|
1102
1048
|
|
@@ -1134,9 +1080,6 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1134
1080
|
size_t size;
|
1135
1081
|
upb_Status status;
|
1136
1082
|
|
1137
|
-
// TODO(haberman): use this message's pool instead.
|
1138
|
-
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
1139
|
-
|
1140
1083
|
if (argc < 1 || argc > 2) {
|
1141
1084
|
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
1142
1085
|
}
|
@@ -1162,11 +1105,18 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1162
1105
|
Qfalse))) {
|
1163
1106
|
options |= upb_JsonEncode_EmitDefaults;
|
1164
1107
|
}
|
1108
|
+
|
1109
|
+
if (RTEST(rb_hash_lookup2(hash_args,
|
1110
|
+
ID2SYM(rb_intern("format_enums_as_integers")),
|
1111
|
+
Qfalse))) {
|
1112
|
+
options |= upb_JsonEncode_FormatEnumsAsIntegers;
|
1113
|
+
}
|
1165
1114
|
}
|
1166
1115
|
|
1167
1116
|
upb_Status_Clear(&status);
|
1168
|
-
|
1169
|
-
|
1117
|
+
const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(msg->msgdef));
|
1118
|
+
size = upb_JsonEncode(msg->msg, msg->msgdef, pool, options, buf, sizeof(buf),
|
1119
|
+
&status);
|
1170
1120
|
|
1171
1121
|
if (!upb_Status_IsOk(&status)) {
|
1172
1122
|
rb_raise(cParseError, "Error occurred during encoding: %s",
|
@@ -1176,7 +1126,7 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1176
1126
|
VALUE ret;
|
1177
1127
|
if (size >= sizeof(buf)) {
|
1178
1128
|
char* buf2 = malloc(size + 1);
|
1179
|
-
upb_JsonEncode(msg->msg, msg->msgdef,
|
1129
|
+
upb_JsonEncode(msg->msg, msg->msgdef, pool, options, buf2, size + 1,
|
1180
1130
|
&status);
|
1181
1131
|
ret = rb_str_new(buf2, size);
|
1182
1132
|
free(buf2);
|
@@ -1309,9 +1259,12 @@ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
|
1309
1259
|
upb_Message* new_msg = upb_Message_New(layout, arena);
|
1310
1260
|
char* data;
|
1311
1261
|
|
1262
|
+
const upb_FileDef* file = upb_MessageDef_File(m);
|
1263
|
+
const upb_ExtensionRegistry* extreg =
|
1264
|
+
upb_DefPool_ExtensionRegistry(upb_FileDef_Pool(file));
|
1312
1265
|
if (upb_Encode(msg, layout, 0, tmp_arena, &data, &size) !=
|
1313
1266
|
kUpb_EncodeStatus_Ok ||
|
1314
|
-
upb_Decode(data, size, new_msg, layout,
|
1267
|
+
upb_Decode(data, size, new_msg, layout, extreg, 0, arena) !=
|
1315
1268
|
kUpb_DecodeStatus_Ok) {
|
1316
1269
|
upb_Arena_Free(tmp_arena);
|
1317
1270
|
rb_raise(cParseError, "Error occurred copying proto");
|
@@ -1,38 +1,13 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2008 Google Inc. All rights reserved.
|
3
|
-
// https://developers.google.com/protocol-buffers/
|
4
3
|
//
|
5
|
-
//
|
6
|
-
//
|
7
|
-
//
|
8
|
-
//
|
9
|
-
// * Redistributions of source code must retain the above copyright
|
10
|
-
// notice, this list of conditions and the following disclaimer.
|
11
|
-
// * Redistributions in binary form must reproduce the above
|
12
|
-
// copyright notice, this list of conditions and the following disclaimer
|
13
|
-
// in the documentation and/or other materials provided with the
|
14
|
-
// distribution.
|
15
|
-
// * Neither the name of Google Inc. nor the names of its
|
16
|
-
// contributors may be used to endorse or promote products derived from
|
17
|
-
// this software without specific prior written permission.
|
18
|
-
//
|
19
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
4
|
+
// Use of this source code is governed by a BSD-style
|
5
|
+
// license that can be found in the LICENSE file or at
|
6
|
+
// https://developers.google.com/open-source/licenses/bsd
|
30
7
|
|
31
8
|
#ifndef RUBY_PROTOBUF_MESSAGE_H_
|
32
9
|
#define RUBY_PROTOBUF_MESSAGE_H_
|
33
10
|
|
34
|
-
#include <ruby/ruby.h>
|
35
|
-
|
36
11
|
#include "protobuf.h"
|
37
12
|
#include "ruby-upb.h"
|
38
13
|
|
@@ -79,10 +54,6 @@ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
|
79
54
|
upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
80
55
|
upb_Arena* arena);
|
81
56
|
|
82
|
-
// Returns true if these two messages are equal.
|
83
|
-
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
84
|
-
const upb_MessageDef* m);
|
85
|
-
|
86
57
|
// Checks that this Ruby object is a message, and raises an exception if not.
|
87
58
|
void Message_CheckClass(VALUE klass);
|
88
59
|
|
@@ -98,6 +69,13 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc);
|
|
98
69
|
// module.
|
99
70
|
VALUE MessageOrEnum_GetDescriptor(VALUE klass);
|
100
71
|
|
72
|
+
// Decodes a Message from a byte sequence.
|
73
|
+
VALUE Message_decode_bytes(int size, const char* bytes, int options,
|
74
|
+
VALUE klass, bool freeze);
|
75
|
+
|
76
|
+
// Recursively freeze message
|
77
|
+
VALUE Message_freeze(VALUE _self);
|
78
|
+
|
101
79
|
// Call at startup to register all types in this module.
|
102
80
|
void Message_register(VALUE protobuf);
|
103
81
|
|