google-protobuf 3.19.1 → 3.25.1
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 +121 -155
- data/ext/google/protobuf_c/convert.h +15 -37
- data/ext/google/protobuf_c/defs.c +362 -243
- data/ext/google/protobuf_c/defs.h +22 -47
- data/ext/google/protobuf_c/extconf.rb +13 -5
- data/ext/google/protobuf_c/glue.c +56 -0
- data/ext/google/protobuf_c/map.c +129 -137
- data/ext/google/protobuf_c/map.h +13 -36
- data/ext/google/protobuf_c/message.c +486 -389
- data/ext/google/protobuf_c/message.h +32 -47
- data/ext/google/protobuf_c/protobuf.c +101 -228
- data/ext/google/protobuf_c/protobuf.h +37 -42
- data/ext/google/protobuf_c/repeated_field.c +110 -113
- data/ext/google/protobuf_c/repeated_field.h +12 -34
- data/ext/google/protobuf_c/ruby-upb.c +12236 -6993
- data/ext/google/protobuf_c/ruby-upb.h +12127 -3787
- 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/third_party/utf8_range/LICENSE +22 -0
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
- data/lib/google/protobuf/any_pb.rb +24 -5
- data/lib/google/protobuf/api_pb.rb +26 -23
- data/lib/google/protobuf/descriptor_dsl.rb +8 -1
- data/lib/google/protobuf/descriptor_pb.rb +43 -225
- 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 +10 -28
- data/lib/google/protobuf/object_cache.rb +97 -0
- data/lib/google/protobuf/plugin_pb.rb +47 -0
- data/lib/google/protobuf/repeated_field.rb +18 -28
- 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 +16 -40
- data/lib/google/protobuf/wrappers_pb.rb +24 -28
- data/lib/google/protobuf.rb +32 -50
- 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 +82 -20
- data/tests/basic.rb +0 -640
- data/tests/generated_code_test.rb +0 -23
- data/tests/stress.rb +0 -38
@@ -1,63 +1,54 @@
|
|
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
|
#ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
|
32
9
|
#define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
|
33
10
|
|
34
|
-
|
35
|
-
#
|
11
|
+
// Ruby 3+ defines NDEBUG itself, see: https://bugs.ruby-lang.org/issues/18777
|
12
|
+
#ifdef NDEBUG
|
13
|
+
#include <ruby.h>
|
14
|
+
#else
|
15
|
+
#include <ruby.h>
|
16
|
+
#undef NDEBUG
|
17
|
+
#endif
|
18
|
+
|
19
|
+
#include <ruby/version.h>
|
20
|
+
|
21
|
+
#if RUBY_API_VERSION_CODE < 20700
|
22
|
+
#error Protobuf requires Ruby >= 2.7
|
23
|
+
#endif
|
24
|
+
|
25
|
+
#include <assert.h> // Must be included after the NDEBUG logic above.
|
36
26
|
#include <ruby/encoding.h>
|
27
|
+
#include <ruby/vm.h>
|
37
28
|
|
38
|
-
#include "ruby-upb.h"
|
39
29
|
#include "defs.h"
|
30
|
+
#include "ruby-upb.h"
|
40
31
|
|
41
32
|
// These operate on a map field (i.e., a repeated field of submessages whose
|
42
33
|
// submessage type is a map-entry msgdef).
|
43
|
-
const
|
44
|
-
const
|
34
|
+
const upb_FieldDef* map_field_key(const upb_FieldDef* field);
|
35
|
+
const upb_FieldDef* map_field_value(const upb_FieldDef* field);
|
45
36
|
|
46
37
|
// -----------------------------------------------------------------------------
|
47
38
|
// Arena
|
48
39
|
// -----------------------------------------------------------------------------
|
49
40
|
|
50
|
-
// A Ruby object that wraps an underlying
|
41
|
+
// A Ruby object that wraps an underlying upb_Arena. Any objects that are
|
51
42
|
// allocated from this arena should reference the Arena in rb_gc_mark(), to
|
52
43
|
// ensure that the object's underlying memory outlives any Ruby object that can
|
53
44
|
// reach it.
|
54
45
|
|
55
46
|
VALUE Arena_new();
|
56
|
-
|
47
|
+
upb_Arena* Arena_get(VALUE arena);
|
57
48
|
|
58
49
|
// Fuses this arena to another, throwing a Ruby exception if this is not
|
59
50
|
// possible.
|
60
|
-
void Arena_fuse(VALUE arena,
|
51
|
+
void Arena_fuse(VALUE arena, upb_Arena* other);
|
61
52
|
|
62
53
|
// Pins this Ruby object to the lifetime of this arena, so that as long as the
|
63
54
|
// arena is alive this object will not be collected.
|
@@ -76,10 +67,9 @@ void Arena_Pin(VALUE arena, VALUE obj);
|
|
76
67
|
// being collected (though in Ruby <2.7 is it effectively strong, due to
|
77
68
|
// implementation limitations).
|
78
69
|
|
79
|
-
//
|
80
|
-
//
|
81
|
-
|
82
|
-
void ObjectCache_Add(const void* key, VALUE val);
|
70
|
+
// Tries to add a new entry to the cache, returning the newly installed value or
|
71
|
+
// the pre-existing entry.
|
72
|
+
VALUE ObjectCache_TryAdd(const void* key, VALUE val);
|
83
73
|
|
84
74
|
// Returns the cached object for this key, if any. Otherwise returns Qnil.
|
85
75
|
VALUE ObjectCache_Get(const void* key);
|
@@ -93,10 +83,11 @@ typedef struct StringBuilder StringBuilder;
|
|
93
83
|
|
94
84
|
StringBuilder* StringBuilder_New();
|
95
85
|
void StringBuilder_Free(StringBuilder* b);
|
96
|
-
void StringBuilder_Printf(StringBuilder* b, const char
|
86
|
+
void StringBuilder_Printf(StringBuilder* b, const char* fmt, ...);
|
97
87
|
VALUE StringBuilder_ToRubyString(StringBuilder* b);
|
98
88
|
|
99
|
-
void StringBuilder_PrintMsgval(StringBuilder* b,
|
89
|
+
void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
|
90
|
+
TypeInfo info);
|
100
91
|
|
101
92
|
// -----------------------------------------------------------------------------
|
102
93
|
// Utilities.
|
@@ -105,9 +96,13 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
|
|
105
96
|
extern VALUE cTypeError;
|
106
97
|
|
107
98
|
#ifdef NDEBUG
|
108
|
-
#define PBRUBY_ASSERT(expr)
|
99
|
+
#define PBRUBY_ASSERT(expr) \
|
100
|
+
do { \
|
101
|
+
} while (false && (expr))
|
109
102
|
#else
|
110
|
-
#define PBRUBY_ASSERT(expr)
|
103
|
+
#define PBRUBY_ASSERT(expr) \
|
104
|
+
if (!(expr)) \
|
105
|
+
rb_bug("Assertion failed at %s:%d, expr: %s", __FILE__, __LINE__, #expr)
|
111
106
|
#endif
|
112
107
|
|
113
108
|
#define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
|
@@ -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 "repeated_field.h"
|
32
9
|
|
@@ -40,10 +17,10 @@
|
|
40
17
|
// -----------------------------------------------------------------------------
|
41
18
|
|
42
19
|
typedef struct {
|
43
|
-
const
|
20
|
+
const upb_Array* array; // Can get as mutable when non-frozen.
|
44
21
|
TypeInfo type_info;
|
45
22
|
VALUE type_class; // To GC-root the msgdef/enumdef in type_info.
|
46
|
-
VALUE arena; // To GC-root the
|
23
|
+
VALUE arena; // To GC-root the upb_Array.
|
47
24
|
} RepeatedField;
|
48
25
|
|
49
26
|
VALUE cRepeatedField;
|
@@ -55,9 +32,9 @@ static void RepeatedField_mark(void* _self) {
|
|
55
32
|
}
|
56
33
|
|
57
34
|
const rb_data_type_t RepeatedField_type = {
|
58
|
-
|
59
|
-
|
60
|
-
|
35
|
+
"Google::Protobuf::RepeatedField",
|
36
|
+
{RepeatedField_mark, RUBY_DEFAULT_FREE, NULL},
|
37
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
61
38
|
};
|
62
39
|
|
63
40
|
static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
|
@@ -66,9 +43,9 @@ static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
|
|
66
43
|
return self;
|
67
44
|
}
|
68
45
|
|
69
|
-
static
|
46
|
+
static upb_Array* RepeatedField_GetMutable(VALUE _self) {
|
70
47
|
rb_check_frozen(_self);
|
71
|
-
return (
|
48
|
+
return (upb_Array*)ruby_to_RepeatedField(_self)->array;
|
72
49
|
}
|
73
50
|
|
74
51
|
VALUE RepeatedField_alloc(VALUE klass) {
|
@@ -79,7 +56,7 @@ VALUE RepeatedField_alloc(VALUE klass) {
|
|
79
56
|
return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
|
80
57
|
}
|
81
58
|
|
82
|
-
VALUE RepeatedField_GetRubyWrapper(
|
59
|
+
VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
|
83
60
|
VALUE arena) {
|
84
61
|
PBRUBY_ASSERT(array);
|
85
62
|
VALUE val = ObjectCache_Get(array);
|
@@ -87,42 +64,44 @@ VALUE RepeatedField_GetRubyWrapper(upb_array* array, TypeInfo type_info,
|
|
87
64
|
if (val == Qnil) {
|
88
65
|
val = RepeatedField_alloc(cRepeatedField);
|
89
66
|
RepeatedField* self;
|
90
|
-
ObjectCache_Add(array, val);
|
91
67
|
TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
|
92
68
|
self->array = array;
|
93
69
|
self->arena = arena;
|
94
70
|
self->type_info = type_info;
|
95
|
-
if (self->type_info.type ==
|
71
|
+
if (self->type_info.type == kUpb_CType_Message) {
|
96
72
|
self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
|
97
73
|
}
|
74
|
+
val = ObjectCache_TryAdd(array, val);
|
98
75
|
}
|
99
76
|
|
100
77
|
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.type == type_info.type);
|
101
78
|
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
|
102
79
|
type_info.def.msgdef);
|
80
|
+
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->array == array);
|
81
|
+
|
103
82
|
return val;
|
104
83
|
}
|
105
84
|
|
106
85
|
static VALUE RepeatedField_new_this_type(RepeatedField* from) {
|
107
86
|
VALUE arena_rb = Arena_new();
|
108
|
-
|
87
|
+
upb_Array* array = upb_Array_New(Arena_get(arena_rb), from->type_info.type);
|
109
88
|
VALUE ret = RepeatedField_GetRubyWrapper(array, from->type_info, arena_rb);
|
110
89
|
PBRUBY_ASSERT(ruby_to_RepeatedField(ret)->type_class == from->type_class);
|
111
90
|
return ret;
|
112
91
|
}
|
113
92
|
|
114
|
-
void RepeatedField_Inspect(StringBuilder* b, const
|
93
|
+
void RepeatedField_Inspect(StringBuilder* b, const upb_Array* array,
|
115
94
|
TypeInfo info) {
|
116
95
|
bool first = true;
|
117
96
|
StringBuilder_Printf(b, "[");
|
118
|
-
size_t n = array ?
|
97
|
+
size_t n = array ? upb_Array_Size(array) : 0;
|
119
98
|
for (size_t i = 0; i < n; i++) {
|
120
99
|
if (first) {
|
121
100
|
first = false;
|
122
101
|
} else {
|
123
102
|
StringBuilder_Printf(b, ", ");
|
124
103
|
}
|
125
|
-
StringBuilder_PrintMsgval(b,
|
104
|
+
StringBuilder_PrintMsgval(b, upb_Array_Get(array, i), info);
|
126
105
|
}
|
127
106
|
StringBuilder_Printf(b, "]");
|
128
107
|
}
|
@@ -132,24 +111,24 @@ VALUE RepeatedField_deep_copy(VALUE _self) {
|
|
132
111
|
VALUE new_rptfield = RepeatedField_new_this_type(self);
|
133
112
|
RepeatedField* new_self = ruby_to_RepeatedField(new_rptfield);
|
134
113
|
VALUE arena_rb = new_self->arena;
|
135
|
-
|
136
|
-
|
137
|
-
size_t elements =
|
114
|
+
upb_Array* new_array = RepeatedField_GetMutable(new_rptfield);
|
115
|
+
upb_Arena* arena = Arena_get(arena_rb);
|
116
|
+
size_t elements = upb_Array_Size(self->array);
|
138
117
|
|
139
|
-
|
118
|
+
upb_Array_Resize(new_array, elements, arena);
|
140
119
|
|
141
|
-
size_t size =
|
120
|
+
size_t size = upb_Array_Size(self->array);
|
142
121
|
for (size_t i = 0; i < size; i++) {
|
143
|
-
|
144
|
-
|
145
|
-
|
122
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
123
|
+
upb_MessageValue copy = Msgval_DeepCopy(msgval, self->type_info, arena);
|
124
|
+
upb_Array_Set(new_array, i, copy);
|
146
125
|
}
|
147
126
|
|
148
127
|
return new_rptfield;
|
149
128
|
}
|
150
129
|
|
151
|
-
const
|
152
|
-
|
130
|
+
const upb_Array* RepeatedField_GetUpbArray(VALUE val, const upb_FieldDef* field,
|
131
|
+
upb_Arena* arena) {
|
153
132
|
RepeatedField* self;
|
154
133
|
TypeInfo type_info = TypeInfo_get(field);
|
155
134
|
|
@@ -173,17 +152,17 @@ const upb_array* RepeatedField_GetUpbArray(VALUE val, const upb_fielddef* field,
|
|
173
152
|
|
174
153
|
static int index_position(VALUE _index, RepeatedField* repeated_field) {
|
175
154
|
int index = NUM2INT(_index);
|
176
|
-
if (index < 0) index +=
|
155
|
+
if (index < 0) index += upb_Array_Size(repeated_field->array);
|
177
156
|
return index;
|
178
157
|
}
|
179
158
|
|
180
159
|
static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
|
181
|
-
size_t size =
|
160
|
+
size_t size = upb_Array_Size(self->array);
|
182
161
|
VALUE ary = rb_ary_new2(size);
|
183
162
|
long i;
|
184
163
|
|
185
164
|
for (i = beg; i < beg + len; i++) {
|
186
|
-
|
165
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
187
166
|
VALUE elem = Convert_UpbToRuby(msgval, self->type_info, self->arena);
|
188
167
|
rb_ary_push(ary, elem);
|
189
168
|
}
|
@@ -200,18 +179,17 @@ static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
|
|
200
179
|
*/
|
201
180
|
static VALUE RepeatedField_each(VALUE _self) {
|
202
181
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
203
|
-
int size =
|
182
|
+
int size = upb_Array_Size(self->array);
|
204
183
|
int i;
|
205
184
|
|
206
185
|
for (i = 0; i < size; i++) {
|
207
|
-
|
186
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
208
187
|
VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
|
209
188
|
rb_yield(val);
|
210
189
|
}
|
211
190
|
return _self;
|
212
191
|
}
|
213
192
|
|
214
|
-
|
215
193
|
/*
|
216
194
|
* call-seq:
|
217
195
|
* RepeatedField.[](index) => value
|
@@ -220,20 +198,20 @@ static VALUE RepeatedField_each(VALUE _self) {
|
|
220
198
|
*/
|
221
199
|
static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
|
222
200
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
223
|
-
long size =
|
201
|
+
long size = upb_Array_Size(self->array);
|
224
202
|
|
225
203
|
VALUE arg = argv[0];
|
226
204
|
long beg, len;
|
227
205
|
|
228
|
-
if (argc == 1){
|
206
|
+
if (argc == 1) {
|
229
207
|
if (FIXNUM_P(arg)) {
|
230
208
|
/* standard case */
|
231
|
-
|
209
|
+
upb_MessageValue msgval;
|
232
210
|
int index = index_position(argv[0], self);
|
233
|
-
if (index < 0 || (size_t)index >=
|
211
|
+
if (index < 0 || (size_t)index >= upb_Array_Size(self->array)) {
|
234
212
|
return Qnil;
|
235
213
|
}
|
236
|
-
msgval =
|
214
|
+
msgval = upb_Array_Get(self->array, index);
|
237
215
|
return Convert_UpbToRuby(msgval, self->type_info, self->arena);
|
238
216
|
} else {
|
239
217
|
/* check if idx is Range */
|
@@ -269,10 +247,10 @@ static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
|
|
269
247
|
*/
|
270
248
|
static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
|
271
249
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
272
|
-
int size =
|
273
|
-
|
274
|
-
|
275
|
-
|
250
|
+
int size = upb_Array_Size(self->array);
|
251
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
252
|
+
upb_Arena* arena = Arena_get(self->arena);
|
253
|
+
upb_MessageValue msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
|
276
254
|
|
277
255
|
int index = index_position(_index, self);
|
278
256
|
if (index < 0 || index >= (INT_MAX - 1)) {
|
@@ -280,17 +258,17 @@ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
|
|
280
258
|
}
|
281
259
|
|
282
260
|
if (index >= size) {
|
283
|
-
|
284
|
-
|
261
|
+
upb_Array_Resize(array, index + 1, arena);
|
262
|
+
upb_MessageValue fill;
|
285
263
|
memset(&fill, 0, sizeof(fill));
|
286
264
|
for (int i = size; i < index; i++) {
|
287
265
|
// Fill default values.
|
288
|
-
// TODO
|
289
|
-
|
266
|
+
// TODO: should this happen at the upb level?
|
267
|
+
upb_Array_Set(array, i, fill);
|
290
268
|
}
|
291
269
|
}
|
292
270
|
|
293
|
-
|
271
|
+
upb_Array_Set(array, index, msgval);
|
294
272
|
return Qnil;
|
295
273
|
}
|
296
274
|
|
@@ -302,13 +280,14 @@ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
|
|
302
280
|
*/
|
303
281
|
static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
|
304
282
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
305
|
-
|
306
|
-
|
283
|
+
upb_Arena* arena = Arena_get(self->arena);
|
284
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
307
285
|
int i;
|
308
286
|
|
309
287
|
for (i = 0; i < argc; i++) {
|
310
|
-
|
311
|
-
|
288
|
+
upb_MessageValue msgval =
|
289
|
+
Convert_RubyToUpb(argv[i], "", self->type_info, arena);
|
290
|
+
upb_Array_Append(array, msgval, arena);
|
312
291
|
}
|
313
292
|
|
314
293
|
return _self;
|
@@ -322,11 +301,11 @@ static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
|
|
322
301
|
*/
|
323
302
|
static VALUE RepeatedField_push(VALUE _self, VALUE val) {
|
324
303
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
325
|
-
|
326
|
-
|
304
|
+
upb_Arena* arena = Arena_get(self->arena);
|
305
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
327
306
|
|
328
|
-
|
329
|
-
|
307
|
+
upb_MessageValue msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
|
308
|
+
upb_Array_Append(array, msgval, arena);
|
330
309
|
|
331
310
|
return _self;
|
332
311
|
}
|
@@ -336,19 +315,19 @@ static VALUE RepeatedField_push(VALUE _self, VALUE val) {
|
|
336
315
|
*/
|
337
316
|
static VALUE RepeatedField_pop_one(VALUE _self) {
|
338
317
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
339
|
-
size_t size =
|
340
|
-
|
341
|
-
|
318
|
+
size_t size = upb_Array_Size(self->array);
|
319
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
320
|
+
upb_MessageValue last;
|
342
321
|
VALUE ret;
|
343
322
|
|
344
323
|
if (size == 0) {
|
345
324
|
return Qnil;
|
346
325
|
}
|
347
326
|
|
348
|
-
last =
|
327
|
+
last = upb_Array_Get(self->array, size - 1);
|
349
328
|
ret = Convert_UpbToRuby(last, self->type_info, self->arena);
|
350
329
|
|
351
|
-
|
330
|
+
upb_Array_Resize(array, size - 1, Arena_get(self->arena));
|
352
331
|
return ret;
|
353
332
|
}
|
354
333
|
|
@@ -360,11 +339,11 @@ static VALUE RepeatedField_pop_one(VALUE _self) {
|
|
360
339
|
*/
|
361
340
|
static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
|
362
341
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
363
|
-
|
342
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
364
343
|
int i;
|
365
344
|
|
366
345
|
Check_Type(list, T_ARRAY);
|
367
|
-
|
346
|
+
upb_Array_Resize(array, 0, Arena_get(self->arena));
|
368
347
|
|
369
348
|
for (i = 0; i < RARRAY_LEN(list); i++) {
|
370
349
|
RepeatedField_push(_self, rb_ary_entry(list, i));
|
@@ -381,8 +360,8 @@ static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
|
|
381
360
|
*/
|
382
361
|
static VALUE RepeatedField_clear(VALUE _self) {
|
383
362
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
384
|
-
|
385
|
-
|
363
|
+
upb_Array* array = RepeatedField_GetMutable(_self);
|
364
|
+
upb_Array_Resize(array, 0, Arena_get(self->arena));
|
386
365
|
return _self;
|
387
366
|
}
|
388
367
|
|
@@ -394,7 +373,7 @@ static VALUE RepeatedField_clear(VALUE _self) {
|
|
394
373
|
*/
|
395
374
|
static VALUE RepeatedField_length(VALUE _self) {
|
396
375
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
397
|
-
return INT2NUM(
|
376
|
+
return INT2NUM(upb_Array_Size(self->array));
|
398
377
|
}
|
399
378
|
|
400
379
|
/*
|
@@ -408,16 +387,16 @@ static VALUE RepeatedField_dup(VALUE _self) {
|
|
408
387
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
409
388
|
VALUE new_rptfield = RepeatedField_new_this_type(self);
|
410
389
|
RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
|
411
|
-
|
412
|
-
|
413
|
-
int size =
|
390
|
+
upb_Array* new_array = RepeatedField_GetMutable(new_rptfield);
|
391
|
+
upb_Arena* arena = Arena_get(new_rptfield_self->arena);
|
392
|
+
int size = upb_Array_Size(self->array);
|
414
393
|
int i;
|
415
394
|
|
416
395
|
Arena_fuse(self->arena, arena);
|
417
396
|
|
418
397
|
for (i = 0; i < size; i++) {
|
419
|
-
|
420
|
-
|
398
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
399
|
+
upb_Array_Append(new_array, msgval, arena);
|
421
400
|
}
|
422
401
|
|
423
402
|
return new_rptfield;
|
@@ -432,12 +411,12 @@ static VALUE RepeatedField_dup(VALUE _self) {
|
|
432
411
|
*/
|
433
412
|
VALUE RepeatedField_to_ary(VALUE _self) {
|
434
413
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
435
|
-
int size =
|
414
|
+
int size = upb_Array_Size(self->array);
|
436
415
|
VALUE ary = rb_ary_new2(size);
|
437
416
|
int i;
|
438
417
|
|
439
418
|
for (i = 0; i < size; i++) {
|
440
|
-
|
419
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
441
420
|
VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
|
442
421
|
rb_ary_push(ary, val);
|
443
422
|
}
|
@@ -473,17 +452,17 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
|
|
473
452
|
|
474
453
|
self = ruby_to_RepeatedField(_self);
|
475
454
|
other = ruby_to_RepeatedField(_other);
|
476
|
-
size_t n =
|
455
|
+
size_t n = upb_Array_Size(self->array);
|
477
456
|
|
478
457
|
if (self->type_info.type != other->type_info.type ||
|
479
458
|
self->type_class != other->type_class ||
|
480
|
-
|
459
|
+
upb_Array_Size(other->array) != n) {
|
481
460
|
return Qfalse;
|
482
461
|
}
|
483
462
|
|
484
463
|
for (size_t i = 0; i < n; i++) {
|
485
|
-
|
486
|
-
|
464
|
+
upb_MessageValue val1 = upb_Array_Get(self->array, i);
|
465
|
+
upb_MessageValue val2 = upb_Array_Get(other->array, i);
|
487
466
|
if (!Msgval_IsEqual(val1, val2, self->type_info)) {
|
488
467
|
return Qfalse;
|
489
468
|
}
|
@@ -508,6 +487,25 @@ static VALUE RepeatedField_freeze(VALUE _self) {
|
|
508
487
|
return _self;
|
509
488
|
}
|
510
489
|
|
490
|
+
/*
|
491
|
+
* Deep freezes the repeated field and values recursively.
|
492
|
+
* Internal use only.
|
493
|
+
*/
|
494
|
+
VALUE RepeatedField_internal_deep_freeze(VALUE _self) {
|
495
|
+
RepeatedField* self = ruby_to_RepeatedField(_self);
|
496
|
+
RepeatedField_freeze(_self);
|
497
|
+
if (self->type_info.type == kUpb_CType_Message) {
|
498
|
+
int size = upb_Array_Size(self->array);
|
499
|
+
int i;
|
500
|
+
for (i = 0; i < size; i++) {
|
501
|
+
upb_MessageValue msgval = upb_Array_Get(self->array, i);
|
502
|
+
VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
|
503
|
+
Message_internal_deep_freeze(val);
|
504
|
+
}
|
505
|
+
}
|
506
|
+
return _self;
|
507
|
+
}
|
508
|
+
|
511
509
|
/*
|
512
510
|
* call-seq:
|
513
511
|
* RepeatedField.hash => hash_value
|
@@ -517,10 +515,10 @@ static VALUE RepeatedField_freeze(VALUE _self) {
|
|
517
515
|
VALUE RepeatedField_hash(VALUE _self) {
|
518
516
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
519
517
|
uint64_t hash = 0;
|
520
|
-
size_t n =
|
518
|
+
size_t n = upb_Array_Size(self->array);
|
521
519
|
|
522
520
|
for (size_t i = 0; i < n; i++) {
|
523
|
-
|
521
|
+
upb_MessageValue val = upb_Array_Get(self->array, i);
|
524
522
|
hash = Msgval_GetHash(val, self->type_info, hash);
|
525
523
|
}
|
526
524
|
|
@@ -549,10 +547,10 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
|
|
549
547
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
550
548
|
RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
|
551
549
|
RepeatedField* dupped = ruby_to_RepeatedField(dupped_);
|
552
|
-
|
553
|
-
|
550
|
+
upb_Array* dupped_array = RepeatedField_GetMutable(dupped_);
|
551
|
+
upb_Arena* arena = Arena_get(dupped->arena);
|
554
552
|
Arena_fuse(list_rptfield->arena, arena);
|
555
|
-
int size =
|
553
|
+
int size = upb_Array_Size(list_rptfield->array);
|
556
554
|
int i;
|
557
555
|
|
558
556
|
if (self->type_info.type != list_rptfield->type_info.type ||
|
@@ -562,8 +560,8 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
|
|
562
560
|
}
|
563
561
|
|
564
562
|
for (i = 0; i < size; i++) {
|
565
|
-
|
566
|
-
|
563
|
+
upb_MessageValue msgval = upb_Array_Get(list_rptfield->array, i);
|
564
|
+
upb_Array_Append(dupped_array, msgval, arena);
|
567
565
|
}
|
568
566
|
} else {
|
569
567
|
rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
|
@@ -601,7 +599,7 @@ VALUE RepeatedField_concat(VALUE _self, VALUE list) {
|
|
601
599
|
*/
|
602
600
|
VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
|
603
601
|
RepeatedField* self = ruby_to_RepeatedField(_self);
|
604
|
-
|
602
|
+
upb_Arena* arena;
|
605
603
|
VALUE ary = Qnil;
|
606
604
|
|
607
605
|
self->arena = Arena_new();
|
@@ -612,8 +610,9 @@ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
|
|
612
610
|
}
|
613
611
|
|
614
612
|
self->type_info = TypeInfo_FromClass(argc, argv, 0, &self->type_class, &ary);
|
615
|
-
self->array =
|
616
|
-
|
613
|
+
self->array = upb_Array_New(arena, self->type_info.type);
|
614
|
+
VALUE stored_val = ObjectCache_TryAdd(self->array, _self);
|
615
|
+
PBRUBY_ASSERT(stored_val == _self);
|
617
616
|
|
618
617
|
if (ary != Qnil) {
|
619
618
|
if (!RB_TYPE_P(ary, T_ARRAY)) {
|
@@ -627,14 +626,12 @@ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
|
|
627
626
|
}
|
628
627
|
|
629
628
|
void RepeatedField_register(VALUE module) {
|
630
|
-
VALUE klass = rb_define_class_under(
|
631
|
-
module, "RepeatedField", rb_cObject);
|
629
|
+
VALUE klass = rb_define_class_under(module, "RepeatedField", rb_cObject);
|
632
630
|
rb_define_alloc_func(klass, RepeatedField_alloc);
|
633
631
|
rb_gc_register_address(&cRepeatedField);
|
634
632
|
cRepeatedField = klass;
|
635
633
|
|
636
|
-
rb_define_method(klass, "initialize",
|
637
|
-
RepeatedField_init, -1);
|
634
|
+
rb_define_method(klass, "initialize", RepeatedField_init, -1);
|
638
635
|
rb_define_method(klass, "each", RepeatedField_each, 0);
|
639
636
|
rb_define_method(klass, "[]", RepeatedField_index, -1);
|
640
637
|
rb_define_method(klass, "at", RepeatedField_index, -1);
|