google-protobuf 3.22.5 → 3.25.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +23 -70
- data/ext/google/protobuf_c/convert.h +3 -28
- data/ext/google/protobuf_c/defs.c +175 -52
- 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 +56 -0
- data/ext/google/protobuf_c/map.c +27 -28
- data/ext/google/protobuf_c/map.h +6 -28
- data/ext/google/protobuf_c/message.c +83 -83
- data/ext/google/protobuf_c/message.h +10 -28
- data/ext/google/protobuf_c/protobuf.c +39 -176
- data/ext/google/protobuf_c/protobuf.h +24 -32
- data/ext/google/protobuf_c/repeated_field.c +28 -29
- data/ext/google/protobuf_c/repeated_field.h +6 -28
- data/ext/google/protobuf_c/ruby-upb.c +2982 -2494
- data/ext/google/protobuf_c/ruby-upb.h +5838 -3467
- data/ext/google/protobuf_c/shared_convert.c +64 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +65 -0
- data/ext/google/protobuf_c/shared_message.h +25 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +3 -26
- data/lib/google/protobuf/any_pb.rb +24 -5
- data/lib/google/protobuf/api_pb.rb +26 -23
- data/lib/google/protobuf/descriptor_pb.rb +40 -252
- data/lib/google/protobuf/duration_pb.rb +24 -5
- data/lib/google/protobuf/empty_pb.rb +24 -3
- data/lib/google/protobuf/ffi/descriptor.rb +165 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +75 -0
- data/lib/google/protobuf/ffi/enum_descriptor.rb +171 -0
- data/lib/google/protobuf/ffi/ffi.rb +213 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +319 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +305 -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 +407 -0
- data/lib/google/protobuf/ffi/message.rb +662 -0
- data/lib/google/protobuf/ffi/object_cache.rb +30 -0
- data/lib/google/protobuf/ffi/oneof_descriptor.rb +95 -0
- data/lib/google/protobuf/ffi/repeated_field.rb +383 -0
- data/lib/google/protobuf/field_mask_pb.rb +24 -4
- data/lib/google/protobuf/message_exts.rb +3 -26
- data/lib/google/protobuf/object_cache.rb +97 -0
- data/lib/google/protobuf/plugin_pb.rb +25 -28
- data/lib/google/protobuf/repeated_field.rb +3 -26
- data/lib/google/protobuf/source_context_pb.rb +24 -4
- data/lib/google/protobuf/struct_pb.rb +24 -20
- data/lib/google/protobuf/timestamp_pb.rb +24 -5
- data/lib/google/protobuf/type_pb.rb +26 -68
- data/lib/google/protobuf/well_known_types.rb +5 -34
- data/lib/google/protobuf/wrappers_pb.rb +24 -28
- data/lib/google/protobuf.rb +27 -45
- data/lib/google/protobuf_ffi.rb +50 -0
- data/lib/google/protobuf_native.rb +20 -0
- data/lib/google/tasks/ffi.rake +102 -0
- metadata +72 -4
data/ext/google/protobuf_c/map.c
CHANGED
@@ -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 "convert.h"
|
32
9
|
#include "defs.h"
|
@@ -93,7 +70,6 @@ VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
|
93
70
|
if (val == Qnil) {
|
94
71
|
val = Map_alloc(cMap);
|
95
72
|
Map* self;
|
96
|
-
ObjectCache_Add(map, val);
|
97
73
|
TypedData_Get_Struct(val, Map, &Map_type, self);
|
98
74
|
self->map = map;
|
99
75
|
self->arena = arena;
|
@@ -103,6 +79,7 @@ VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
|
103
79
|
const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
|
104
80
|
self->value_type_class = Descriptor_DefToClass(val_m);
|
105
81
|
}
|
82
|
+
return ObjectCache_TryAdd(map, val);
|
106
83
|
}
|
107
84
|
|
108
85
|
return val;
|
@@ -319,7 +296,9 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
319
296
|
|
320
297
|
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
321
298
|
self->value_type_info.type);
|
322
|
-
|
299
|
+
VALUE stored = ObjectCache_TryAdd(self->map, _self);
|
300
|
+
(void)stored;
|
301
|
+
PBRUBY_ASSERT(stored == _self);
|
323
302
|
|
324
303
|
if (init_arg != Qnil) {
|
325
304
|
Map_merge_into_self(_self, init_arg);
|
@@ -593,6 +572,26 @@ static VALUE Map_freeze(VALUE _self) {
|
|
593
572
|
return _self;
|
594
573
|
}
|
595
574
|
|
575
|
+
/*
|
576
|
+
* Deep freezes the map and values recursively.
|
577
|
+
* Internal use only.
|
578
|
+
*/
|
579
|
+
VALUE Map_internal_deep_freeze(VALUE _self) {
|
580
|
+
Map* self = ruby_to_Map(_self);
|
581
|
+
Map_freeze(_self);
|
582
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
583
|
+
size_t iter = kUpb_Map_Begin;
|
584
|
+
upb_MessageValue key, val;
|
585
|
+
|
586
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
587
|
+
VALUE val_val =
|
588
|
+
Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
589
|
+
Message_internal_deep_freeze(val_val);
|
590
|
+
}
|
591
|
+
}
|
592
|
+
return _self;
|
593
|
+
}
|
594
|
+
|
596
595
|
/*
|
597
596
|
* call-seq:
|
598
597
|
* Map.hash => hash_value
|
data/ext/google/protobuf_c/map.h
CHANGED
@@ -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_MAP_H_
|
32
9
|
#define RUBY_PROTOBUF_MAP_H_
|
33
10
|
|
34
|
-
#include <ruby/ruby.h>
|
35
|
-
|
36
11
|
#include "protobuf.h"
|
37
12
|
#include "ruby-upb.h"
|
38
13
|
|
@@ -63,4 +38,7 @@ extern VALUE cMap;
|
|
63
38
|
// Call at startup to register all types in this module.
|
64
39
|
void Map_register(VALUE module);
|
65
40
|
|
41
|
+
// Recursively freeze map
|
42
|
+
VALUE Map_internal_deep_freeze(VALUE _self);
|
43
|
+
|
66
44
|
#endif // RUBY_PROTOBUF_MAP_H_
|
@@ -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*
|
@@ -65,9 +45,9 @@ static void Message_mark(void* _self) {
|
|
65
45
|
}
|
66
46
|
|
67
47
|
static rb_data_type_t Message_type = {
|
68
|
-
"Message",
|
48
|
+
"Google::Protobuf::Message",
|
69
49
|
{Message_mark, RUBY_DEFAULT_FREE, NULL},
|
70
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
50
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
71
51
|
};
|
72
52
|
|
73
53
|
static Message* ruby_to_Message(VALUE msg_rb) {
|
@@ -105,8 +85,10 @@ upb_Message* Message_GetMutable(VALUE msg_rb, const upb_MessageDef** m) {
|
|
105
85
|
void Message_InitPtr(VALUE self_, upb_Message* msg, VALUE arena) {
|
106
86
|
Message* self = ruby_to_Message(self_);
|
107
87
|
self->msg = msg;
|
108
|
-
self->arena
|
109
|
-
|
88
|
+
RB_OBJ_WRITE(self_, &self->arena, arena);
|
89
|
+
VALUE stored = ObjectCache_TryAdd(msg, self_);
|
90
|
+
(void)stored;
|
91
|
+
PBRUBY_ASSERT(stored == self_);
|
110
92
|
}
|
111
93
|
|
112
94
|
VALUE Message_GetArena(VALUE msg_rb) {
|
@@ -680,8 +662,8 @@ static VALUE Message_dup(VALUE _self) {
|
|
680
662
|
Message* new_msg_self = ruby_to_Message(new_msg);
|
681
663
|
size_t size = upb_MessageDef_MiniTable(self->msgdef)->size;
|
682
664
|
|
683
|
-
// TODO
|
684
|
-
// TODO
|
665
|
+
// TODO
|
666
|
+
// TODO
|
685
667
|
memcpy((upb_Message*)new_msg_self->msg, self->msg, size);
|
686
668
|
Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
|
687
669
|
return new_msg;
|
@@ -690,29 +672,13 @@ static VALUE Message_dup(VALUE _self) {
|
|
690
672
|
// Support function for Message_eq, and also used by other #eq functions.
|
691
673
|
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
692
674
|
const upb_MessageDef* m) {
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
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;
|
675
|
+
upb_Status status;
|
676
|
+
upb_Status_Clear(&status);
|
677
|
+
bool return_value = shared_Message_Equal(m1, m2, m, &status);
|
678
|
+
if (upb_Status_IsOk(&status)) {
|
679
|
+
return return_value;
|
713
680
|
} else {
|
714
|
-
|
715
|
-
rb_raise(cParseError, "Error comparing messages");
|
681
|
+
rb_raise(cParseError, upb_Status_ErrorMessage(&status));
|
716
682
|
}
|
717
683
|
}
|
718
684
|
|
@@ -737,23 +703,13 @@ static VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
737
703
|
|
738
704
|
uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
739
705
|
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;
|
706
|
+
upb_Status status;
|
707
|
+
upb_Status_Clear(&status);
|
708
|
+
uint64_t return_value = shared_Message_Hash(msg, m, seed, &status);
|
709
|
+
if (upb_Status_IsOk(&status)) {
|
710
|
+
return return_value;
|
754
711
|
} else {
|
755
|
-
|
756
|
-
rb_raise(cParseError, "Error calculating hash");
|
712
|
+
rb_raise(cParseError, upb_Status_ErrorMessage(&status));
|
757
713
|
}
|
758
714
|
}
|
759
715
|
|
@@ -903,6 +859,32 @@ static VALUE Message_freeze(VALUE _self) {
|
|
903
859
|
return _self;
|
904
860
|
}
|
905
861
|
|
862
|
+
/*
|
863
|
+
* Deep freezes the message object recursively.
|
864
|
+
* Internal use only.
|
865
|
+
*/
|
866
|
+
VALUE Message_internal_deep_freeze(VALUE _self) {
|
867
|
+
Message* self = ruby_to_Message(_self);
|
868
|
+
Message_freeze(_self);
|
869
|
+
|
870
|
+
int n = upb_MessageDef_FieldCount(self->msgdef);
|
871
|
+
for (int i = 0; i < n; i++) {
|
872
|
+
const upb_FieldDef* f = upb_MessageDef_Field(self->msgdef, i);
|
873
|
+
VALUE field = Message_getfield(_self, f);
|
874
|
+
|
875
|
+
if (field != Qnil) {
|
876
|
+
if (upb_FieldDef_IsMap(f)) {
|
877
|
+
Map_internal_deep_freeze(field);
|
878
|
+
} else if (upb_FieldDef_IsRepeated(f)) {
|
879
|
+
RepeatedField_internal_deep_freeze(field);
|
880
|
+
} else if (upb_FieldDef_IsSubMessage(f)) {
|
881
|
+
Message_internal_deep_freeze(field);
|
882
|
+
}
|
883
|
+
}
|
884
|
+
}
|
885
|
+
return _self;
|
886
|
+
}
|
887
|
+
|
906
888
|
/*
|
907
889
|
* call-seq:
|
908
890
|
* Message.[](index) => value
|
@@ -955,7 +937,7 @@ static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
955
937
|
* MessageClass.decode(data, options) => message
|
956
938
|
*
|
957
939
|
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
958
|
-
* format) under the
|
940
|
+
* format) under the interpretation given by this message class's definition
|
959
941
|
* and returns a message object with the corresponding field values.
|
960
942
|
* @param options [Hash] options for the decoder
|
961
943
|
* recursion_limit: set to maximum decoding depth for message (default is 64)
|
@@ -978,7 +960,7 @@ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
|
|
978
960
|
rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
979
961
|
|
980
962
|
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
981
|
-
options |=
|
963
|
+
options |= upb_DecodeOptions_MaxDepth(FIX2INT(depth));
|
982
964
|
}
|
983
965
|
}
|
984
966
|
|
@@ -986,18 +968,27 @@ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
|
|
986
968
|
rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
|
987
969
|
}
|
988
970
|
|
971
|
+
return Message_decode_bytes(RSTRING_LEN(data), RSTRING_PTR(data), options,
|
972
|
+
klass, /*freeze*/ false);
|
973
|
+
}
|
974
|
+
|
975
|
+
VALUE Message_decode_bytes(int size, const char* bytes, int options,
|
976
|
+
VALUE klass, bool freeze) {
|
989
977
|
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
990
978
|
Message* msg = ruby_to_Message(msg_rb);
|
991
979
|
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
980
|
+
const upb_FileDef* file = upb_MessageDef_File(msg->msgdef);
|
981
|
+
const upb_ExtensionRegistry* extreg =
|
982
|
+
upb_DefPool_ExtensionRegistry(upb_FileDef_Pool(file));
|
983
|
+
upb_DecodeStatus status = upb_Decode(bytes, size, (upb_Message*)msg->msg,
|
984
|
+
upb_MessageDef_MiniTable(msg->msgdef),
|
985
|
+
extreg, options, Arena_get(msg->arena));
|
997
986
|
if (status != kUpb_DecodeStatus_Ok) {
|
998
987
|
rb_raise(cParseError, "Error occurred during parsing");
|
999
988
|
}
|
1000
|
-
|
989
|
+
if (freeze) {
|
990
|
+
Message_internal_deep_freeze(msg_rb);
|
991
|
+
}
|
1001
992
|
return msg_rb;
|
1002
993
|
}
|
1003
994
|
|
@@ -1018,7 +1009,7 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1018
1009
|
int options = 0;
|
1019
1010
|
upb_Status status;
|
1020
1011
|
|
1021
|
-
// TODO
|
1012
|
+
// TODO: use this message's pool instead.
|
1022
1013
|
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
1023
1014
|
|
1024
1015
|
if (argc < 1 || argc > 2) {
|
@@ -1041,7 +1032,7 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1041
1032
|
rb_raise(rb_eArgError, "Expected string for JSON data.");
|
1042
1033
|
}
|
1043
1034
|
|
1044
|
-
// TODO
|
1035
|
+
// TODO: Check and respect string encoding. If not UTF-8, we need to
|
1045
1036
|
// convert, because string handlers pass data directly to message string
|
1046
1037
|
// fields.
|
1047
1038
|
|
@@ -1096,7 +1087,7 @@ static VALUE Message_encode(int argc, VALUE* argv, VALUE klass) {
|
|
1096
1087
|
rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
1097
1088
|
|
1098
1089
|
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
1099
|
-
options |=
|
1090
|
+
options |= upb_DecodeOptions_MaxDepth(FIX2INT(depth));
|
1100
1091
|
}
|
1101
1092
|
}
|
1102
1093
|
|
@@ -1134,7 +1125,7 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1134
1125
|
size_t size;
|
1135
1126
|
upb_Status status;
|
1136
1127
|
|
1137
|
-
// TODO
|
1128
|
+
// TODO: use this message's pool instead.
|
1138
1129
|
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
1139
1130
|
|
1140
1131
|
if (argc < 1 || argc > 2) {
|
@@ -1162,6 +1153,12 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1162
1153
|
Qfalse))) {
|
1163
1154
|
options |= upb_JsonEncode_EmitDefaults;
|
1164
1155
|
}
|
1156
|
+
|
1157
|
+
if (RTEST(rb_hash_lookup2(hash_args,
|
1158
|
+
ID2SYM(rb_intern("format_enums_as_integers")),
|
1159
|
+
Qfalse))) {
|
1160
|
+
options |= upb_JsonEncode_FormatEnumsAsIntegers;
|
1161
|
+
}
|
1165
1162
|
}
|
1166
1163
|
|
1167
1164
|
upb_Status_Clear(&status);
|
@@ -1309,9 +1306,12 @@ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
|
1309
1306
|
upb_Message* new_msg = upb_Message_New(layout, arena);
|
1310
1307
|
char* data;
|
1311
1308
|
|
1309
|
+
const upb_FileDef* file = upb_MessageDef_File(m);
|
1310
|
+
const upb_ExtensionRegistry* extreg =
|
1311
|
+
upb_DefPool_ExtensionRegistry(upb_FileDef_Pool(file));
|
1312
1312
|
if (upb_Encode(msg, layout, 0, tmp_arena, &data, &size) !=
|
1313
1313
|
kUpb_EncodeStatus_Ok ||
|
1314
|
-
upb_Decode(data, size, new_msg, layout,
|
1314
|
+
upb_Decode(data, size, new_msg, layout, extreg, 0, arena) !=
|
1315
1315
|
kUpb_DecodeStatus_Ok) {
|
1316
1316
|
upb_Arena_Free(tmp_arena);
|
1317
1317
|
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
|
|
@@ -98,6 +73,13 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc);
|
|
98
73
|
// module.
|
99
74
|
VALUE MessageOrEnum_GetDescriptor(VALUE klass);
|
100
75
|
|
76
|
+
// Decodes a Message from a byte sequence.
|
77
|
+
VALUE Message_decode_bytes(int size, const char* bytes, int options,
|
78
|
+
VALUE klass, bool freeze);
|
79
|
+
|
80
|
+
// Recursively freeze message
|
81
|
+
VALUE Message_internal_deep_freeze(VALUE _self);
|
82
|
+
|
101
83
|
// Call at startup to register all types in this module.
|
102
84
|
void Message_register(VALUE protobuf);
|
103
85
|
|