google-protobuf 3.19.1 → 3.25.1
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.
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
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"
|
@@ -34,7 +11,7 @@
|
|
34
11
|
#include "protobuf.h"
|
35
12
|
|
36
13
|
// -----------------------------------------------------------------------------
|
37
|
-
// Basic map operations on top of
|
14
|
+
// Basic map operations on top of upb_Map.
|
38
15
|
//
|
39
16
|
// Note that we roll our own `Map` container here because, as for
|
40
17
|
// `RepeatedField`, we want a strongly-typed container. This is so that any user
|
@@ -48,8 +25,8 @@
|
|
48
25
|
// -----------------------------------------------------------------------------
|
49
26
|
|
50
27
|
typedef struct {
|
51
|
-
const
|
52
|
-
|
28
|
+
const upb_Map* map; // Can convert to mutable when non-frozen.
|
29
|
+
upb_CType key_type;
|
53
30
|
TypeInfo value_type_info;
|
54
31
|
VALUE value_type_class;
|
55
32
|
VALUE arena;
|
@@ -62,9 +39,9 @@ static void Map_mark(void* _self) {
|
|
62
39
|
}
|
63
40
|
|
64
41
|
const rb_data_type_t Map_type = {
|
65
|
-
|
66
|
-
|
67
|
-
|
42
|
+
"Google::Protobuf::Map",
|
43
|
+
{Map_mark, RUBY_DEFAULT_FREE, NULL},
|
44
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
68
45
|
};
|
69
46
|
|
70
47
|
VALUE cMap;
|
@@ -84,8 +61,8 @@ static VALUE Map_alloc(VALUE klass) {
|
|
84
61
|
return TypedData_Wrap_Struct(klass, &Map_type, self);
|
85
62
|
}
|
86
63
|
|
87
|
-
VALUE Map_GetRubyWrapper(
|
88
|
-
|
64
|
+
VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
65
|
+
VALUE arena) {
|
89
66
|
PBRUBY_ASSERT(map);
|
90
67
|
|
91
68
|
VALUE val = ObjectCache_Get(map);
|
@@ -93,24 +70,24 @@ VALUE Map_GetRubyWrapper(upb_map* map, upb_fieldtype_t key_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;
|
100
76
|
self->key_type = key_type;
|
101
77
|
self->value_type_info = value_type;
|
102
|
-
if (self->value_type_info.type ==
|
103
|
-
const
|
78
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
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;
|
109
86
|
}
|
110
87
|
|
111
|
-
static VALUE Map_new_this_type(Map
|
88
|
+
static VALUE Map_new_this_type(Map* from) {
|
112
89
|
VALUE arena_rb = Arena_new();
|
113
|
-
|
90
|
+
upb_Map* map = upb_Map_New(Arena_get(arena_rb), from->key_type,
|
114
91
|
from->value_type_info.type);
|
115
92
|
VALUE ret =
|
116
93
|
Map_GetRubyWrapper(map, from->key_type, from->value_type_info, arena_rb);
|
@@ -125,22 +102,21 @@ static TypeInfo Map_keyinfo(Map* self) {
|
|
125
102
|
return ret;
|
126
103
|
}
|
127
104
|
|
128
|
-
static
|
105
|
+
static upb_Map* Map_GetMutable(VALUE _self) {
|
129
106
|
rb_check_frozen(_self);
|
130
|
-
return (
|
107
|
+
return (upb_Map*)ruby_to_Map(_self)->map;
|
131
108
|
}
|
132
109
|
|
133
|
-
VALUE Map_CreateHash(const
|
110
|
+
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
|
134
111
|
TypeInfo val_info) {
|
135
112
|
VALUE hash = rb_hash_new();
|
136
|
-
size_t iter = UPB_MAP_BEGIN;
|
137
113
|
TypeInfo key_info = TypeInfo_from_type(key_type);
|
138
114
|
|
139
115
|
if (!map) return hash;
|
140
116
|
|
141
|
-
|
142
|
-
|
143
|
-
|
117
|
+
size_t iter = kUpb_Map_Begin;
|
118
|
+
upb_MessageValue key, val;
|
119
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
144
120
|
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
|
145
121
|
VALUE val_val = Scalar_CreateHash(val, val_info);
|
146
122
|
rb_hash_aset(hash, key_val, val_val);
|
@@ -152,25 +128,25 @@ VALUE Map_CreateHash(const upb_map* map, upb_fieldtype_t key_type,
|
|
152
128
|
VALUE Map_deep_copy(VALUE obj) {
|
153
129
|
Map* self = ruby_to_Map(obj);
|
154
130
|
VALUE new_arena_rb = Arena_new();
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
size_t iter =
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
131
|
+
upb_Arena* arena = Arena_get(new_arena_rb);
|
132
|
+
upb_Map* new_map =
|
133
|
+
upb_Map_New(arena, self->key_type, self->value_type_info.type);
|
134
|
+
size_t iter = kUpb_Map_Begin;
|
135
|
+
upb_MessageValue key, val;
|
136
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
137
|
+
upb_MessageValue val_copy =
|
138
|
+
Msgval_DeepCopy(val, self->value_type_info, arena);
|
139
|
+
upb_Map_Set(new_map, key, val_copy, arena);
|
164
140
|
}
|
165
141
|
|
166
142
|
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
167
143
|
new_arena_rb);
|
168
144
|
}
|
169
145
|
|
170
|
-
const
|
171
|
-
|
172
|
-
const
|
173
|
-
const
|
146
|
+
const upb_Map* Map_GetUpbMap(VALUE val, const upb_FieldDef* field,
|
147
|
+
upb_Arena* arena) {
|
148
|
+
const upb_FieldDef* key_field = map_field_key(field);
|
149
|
+
const upb_FieldDef* value_field = map_field_value(field);
|
174
150
|
TypeInfo value_type_info = TypeInfo_get(value_field);
|
175
151
|
Map* self;
|
176
152
|
|
@@ -180,7 +156,7 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
180
156
|
}
|
181
157
|
|
182
158
|
self = ruby_to_Map(val);
|
183
|
-
if (self->key_type !=
|
159
|
+
if (self->key_type != upb_FieldDef_CType(key_field)) {
|
184
160
|
rb_raise(cTypeError, "Map key type does not match field's key type");
|
185
161
|
}
|
186
162
|
if (self->value_type_info.type != value_type_info.type) {
|
@@ -194,16 +170,15 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
194
170
|
return self->map;
|
195
171
|
}
|
196
172
|
|
197
|
-
void Map_Inspect(StringBuilder* b, const
|
173
|
+
void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
|
198
174
|
TypeInfo val_type) {
|
199
175
|
bool first = true;
|
200
176
|
TypeInfo key_type_info = {key_type};
|
201
177
|
StringBuilder_Printf(b, "{");
|
202
178
|
if (map) {
|
203
|
-
size_t iter =
|
204
|
-
|
205
|
-
|
206
|
-
upb_msgval val = upb_mapiter_value(map, iter);
|
179
|
+
size_t iter = kUpb_Map_Begin;
|
180
|
+
upb_MessageValue key, val;
|
181
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
207
182
|
if (first) {
|
208
183
|
first = false;
|
209
184
|
} else {
|
@@ -219,10 +194,12 @@ void Map_Inspect(StringBuilder* b, const upb_map* map, upb_fieldtype_t key_type,
|
|
219
194
|
|
220
195
|
static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
|
221
196
|
Map* self = ruby_to_Map(_self);
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
197
|
+
upb_Arena* arena = Arena_get(self->arena);
|
198
|
+
upb_MessageValue key_val =
|
199
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), arena);
|
200
|
+
upb_MessageValue val_val =
|
201
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
202
|
+
upb_Map_Set(Map_GetMutable(_self), key_val, val_val, arena);
|
226
203
|
return ST_CONTINUE;
|
227
204
|
}
|
228
205
|
|
@@ -234,9 +211,8 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
234
211
|
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
235
212
|
Map* self = ruby_to_Map(_self);
|
236
213
|
Map* other = ruby_to_Map(hashmap);
|
237
|
-
|
238
|
-
|
239
|
-
size_t iter = UPB_MAP_BEGIN;
|
214
|
+
upb_Arena* arena = Arena_get(self->arena);
|
215
|
+
upb_Message* self_msg = Map_GetMutable(_self);
|
240
216
|
|
241
217
|
Arena_fuse(other->arena, arena);
|
242
218
|
|
@@ -246,10 +222,10 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
246
222
|
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
247
223
|
}
|
248
224
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
225
|
+
size_t iter = kUpb_Map_Begin;
|
226
|
+
upb_MessageValue key, val;
|
227
|
+
while (upb_Map_Next(other->map, &key, &val, &iter)) {
|
228
|
+
upb_Map_Set(self_msg, key, val, arena);
|
253
229
|
}
|
254
230
|
} else {
|
255
231
|
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
@@ -305,22 +281,24 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
305
281
|
|
306
282
|
// Check that the key type is an allowed type.
|
307
283
|
switch (self->key_type) {
|
308
|
-
case
|
309
|
-
case
|
310
|
-
case
|
311
|
-
case
|
312
|
-
case
|
313
|
-
case
|
314
|
-
case
|
284
|
+
case kUpb_CType_Int32:
|
285
|
+
case kUpb_CType_Int64:
|
286
|
+
case kUpb_CType_UInt32:
|
287
|
+
case kUpb_CType_UInt64:
|
288
|
+
case kUpb_CType_Bool:
|
289
|
+
case kUpb_CType_String:
|
290
|
+
case kUpb_CType_Bytes:
|
315
291
|
// These are OK.
|
316
292
|
break;
|
317
293
|
default:
|
318
294
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
319
295
|
}
|
320
296
|
|
321
|
-
self->map =
|
297
|
+
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
322
298
|
self->value_type_info.type);
|
323
|
-
|
299
|
+
VALUE stored = ObjectCache_TryAdd(self->map, _self);
|
300
|
+
(void)stored;
|
301
|
+
PBRUBY_ASSERT(stored == _self);
|
324
302
|
|
325
303
|
if (init_arg != Qnil) {
|
326
304
|
Map_merge_into_self(_self, init_arg);
|
@@ -339,11 +317,10 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
339
317
|
*/
|
340
318
|
static VALUE Map_each(VALUE _self) {
|
341
319
|
Map* self = ruby_to_Map(_self);
|
342
|
-
size_t iter =
|
320
|
+
size_t iter = kUpb_Map_Begin;
|
321
|
+
upb_MessageValue key, val;
|
343
322
|
|
344
|
-
while (
|
345
|
-
upb_msgval key = upb_mapiter_key(self->map, iter);
|
346
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
323
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
347
324
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
348
325
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
349
326
|
rb_yield_values(2, key_val, val_val);
|
@@ -360,11 +337,11 @@ static VALUE Map_each(VALUE _self) {
|
|
360
337
|
*/
|
361
338
|
static VALUE Map_keys(VALUE _self) {
|
362
339
|
Map* self = ruby_to_Map(_self);
|
363
|
-
size_t iter =
|
340
|
+
size_t iter = kUpb_Map_Begin;
|
364
341
|
VALUE ret = rb_ary_new();
|
342
|
+
upb_MessageValue key, val;
|
365
343
|
|
366
|
-
while (
|
367
|
-
upb_msgval key = upb_mapiter_key(self->map, iter);
|
344
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
368
345
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
369
346
|
rb_ary_push(ret, key_val);
|
370
347
|
}
|
@@ -380,11 +357,11 @@ static VALUE Map_keys(VALUE _self) {
|
|
380
357
|
*/
|
381
358
|
static VALUE Map_values(VALUE _self) {
|
382
359
|
Map* self = ruby_to_Map(_self);
|
383
|
-
size_t iter =
|
360
|
+
size_t iter = kUpb_Map_Begin;
|
384
361
|
VALUE ret = rb_ary_new();
|
362
|
+
upb_MessageValue key, val;
|
385
363
|
|
386
|
-
while (
|
387
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
364
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
388
365
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
389
366
|
rb_ary_push(ret, val_val);
|
390
367
|
}
|
@@ -401,10 +378,11 @@ static VALUE Map_values(VALUE _self) {
|
|
401
378
|
*/
|
402
379
|
static VALUE Map_index(VALUE _self, VALUE key) {
|
403
380
|
Map* self = ruby_to_Map(_self);
|
404
|
-
|
405
|
-
|
381
|
+
upb_MessageValue key_upb =
|
382
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
383
|
+
upb_MessageValue val;
|
406
384
|
|
407
|
-
if (
|
385
|
+
if (upb_Map_Get(self->map, key_upb, &val)) {
|
408
386
|
return Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
409
387
|
} else {
|
410
388
|
return Qnil;
|
@@ -421,11 +399,13 @@ static VALUE Map_index(VALUE _self, VALUE key) {
|
|
421
399
|
*/
|
422
400
|
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
423
401
|
Map* self = ruby_to_Map(_self);
|
424
|
-
|
425
|
-
|
426
|
-
|
402
|
+
upb_Arena* arena = Arena_get(self->arena);
|
403
|
+
upb_MessageValue key_upb =
|
404
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
405
|
+
upb_MessageValue val_upb =
|
406
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
427
407
|
|
428
|
-
|
408
|
+
upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
429
409
|
|
430
410
|
return val;
|
431
411
|
}
|
@@ -439,9 +419,10 @@ static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
|
439
419
|
*/
|
440
420
|
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
441
421
|
Map* self = ruby_to_Map(_self);
|
442
|
-
|
422
|
+
upb_MessageValue key_upb =
|
423
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
443
424
|
|
444
|
-
if (
|
425
|
+
if (upb_Map_Get(self->map, key_upb, NULL)) {
|
445
426
|
return Qtrue;
|
446
427
|
} else {
|
447
428
|
return Qfalse;
|
@@ -457,23 +438,17 @@ static VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
457
438
|
*/
|
458
439
|
static VALUE Map_delete(VALUE _self, VALUE key) {
|
459
440
|
Map* self = ruby_to_Map(_self);
|
460
|
-
upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
461
|
-
upb_msgval val_upb;
|
462
|
-
VALUE ret;
|
463
|
-
|
464
441
|
rb_check_frozen(_self);
|
465
442
|
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
443
|
+
upb_MessageValue key_upb =
|
444
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
445
|
+
upb_MessageValue val_upb;
|
446
|
+
|
447
|
+
if (upb_Map_Delete(self->map, key_upb, &val_upb)) {
|
448
|
+
return Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
470
449
|
} else {
|
471
|
-
|
450
|
+
return Qnil;
|
472
451
|
}
|
473
|
-
|
474
|
-
upb_map_delete(Map_GetMutable(_self), key_upb);
|
475
|
-
|
476
|
-
return ret;
|
477
452
|
}
|
478
453
|
|
479
454
|
/*
|
@@ -483,7 +458,7 @@ static VALUE Map_delete(VALUE _self, VALUE key) {
|
|
483
458
|
* Removes all entries from the map.
|
484
459
|
*/
|
485
460
|
static VALUE Map_clear(VALUE _self) {
|
486
|
-
|
461
|
+
upb_Map_Clear(Map_GetMutable(_self));
|
487
462
|
return Qnil;
|
488
463
|
}
|
489
464
|
|
@@ -495,7 +470,7 @@ static VALUE Map_clear(VALUE _self) {
|
|
495
470
|
*/
|
496
471
|
static VALUE Map_length(VALUE _self) {
|
497
472
|
Map* self = ruby_to_Map(_self);
|
498
|
-
return ULL2NUM(
|
473
|
+
return ULL2NUM(upb_Map_Size(self->map));
|
499
474
|
}
|
500
475
|
|
501
476
|
/*
|
@@ -509,16 +484,15 @@ static VALUE Map_dup(VALUE _self) {
|
|
509
484
|
Map* self = ruby_to_Map(_self);
|
510
485
|
VALUE new_map_rb = Map_new_this_type(self);
|
511
486
|
Map* new_self = ruby_to_Map(new_map_rb);
|
512
|
-
size_t iter =
|
513
|
-
|
514
|
-
|
487
|
+
size_t iter = kUpb_Map_Begin;
|
488
|
+
upb_Arena* arena = Arena_get(new_self->arena);
|
489
|
+
upb_Map* new_map = Map_GetMutable(new_map_rb);
|
515
490
|
|
516
491
|
Arena_fuse(self->arena, arena);
|
517
492
|
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
upb_map_set(new_map, key, val, arena);
|
493
|
+
upb_MessageValue key, val;
|
494
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
495
|
+
upb_Map_Set(new_map, key, val, arena);
|
522
496
|
}
|
523
497
|
|
524
498
|
return new_map_rb;
|
@@ -559,18 +533,17 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
559
533
|
self->value_type_class != other->value_type_class) {
|
560
534
|
return Qfalse;
|
561
535
|
}
|
562
|
-
if (
|
536
|
+
if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
|
563
537
|
return Qfalse;
|
564
538
|
}
|
565
539
|
|
566
540
|
// For each member of self, check that an equal member exists at the same key
|
567
541
|
// in other.
|
568
|
-
size_t iter =
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
if (!upb_map_get(other->map, key, &other_val)) {
|
542
|
+
size_t iter = kUpb_Map_Begin;
|
543
|
+
upb_MessageValue key, val;
|
544
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
545
|
+
upb_MessageValue other_val;
|
546
|
+
if (!upb_Map_Get(other->map, key, &other_val)) {
|
574
547
|
// Not present in other map.
|
575
548
|
return Qfalse;
|
576
549
|
}
|
@@ -599,6 +572,26 @@ static VALUE Map_freeze(VALUE _self) {
|
|
599
572
|
return _self;
|
600
573
|
}
|
601
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
|
+
|
602
595
|
/*
|
603
596
|
* call-seq:
|
604
597
|
* Map.hash => hash_value
|
@@ -609,11 +602,10 @@ VALUE Map_hash(VALUE _self) {
|
|
609
602
|
Map* self = ruby_to_Map(_self);
|
610
603
|
uint64_t hash = 0;
|
611
604
|
|
612
|
-
size_t iter =
|
605
|
+
size_t iter = kUpb_Map_Begin;
|
613
606
|
TypeInfo key_info = {self->key_type};
|
614
|
-
|
615
|
-
|
616
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
607
|
+
upb_MessageValue key, val;
|
608
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
617
609
|
hash = Msgval_GetHash(key, key_info, hash);
|
618
610
|
hash = Msgval_GetHash(val, self->value_type_info, hash);
|
619
611
|
}
|
data/ext/google/protobuf_c/map.h
CHANGED
@@ -1,59 +1,33 @@
|
|
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
|
|
39
14
|
// Returns a Ruby wrapper object for the given map, which will be created if
|
40
15
|
// one does not exist already.
|
41
|
-
VALUE Map_GetRubyWrapper(
|
42
|
-
|
16
|
+
VALUE Map_GetRubyWrapper(upb_Map *map, upb_CType key_type, TypeInfo value_type,
|
17
|
+
VALUE arena);
|
43
18
|
|
44
|
-
// Gets the underlying
|
19
|
+
// Gets the underlying upb_Map for this Ruby map object, which must have
|
45
20
|
// key/value type that match |field|. If this is not a map or the type doesn't
|
46
21
|
// match, raises an exception.
|
47
|
-
const
|
48
|
-
|
22
|
+
const upb_Map *Map_GetUpbMap(VALUE val, const upb_FieldDef *field,
|
23
|
+
upb_Arena *arena);
|
49
24
|
|
50
25
|
// Implements #inspect for this map by appending its contents to |b|.
|
51
|
-
void Map_Inspect(StringBuilder *b, const
|
26
|
+
void Map_Inspect(StringBuilder *b, const upb_Map *map, upb_CType key_type,
|
52
27
|
TypeInfo val_type);
|
53
28
|
|
54
29
|
// Returns a new Hash object containing the contents of this Map.
|
55
|
-
VALUE Map_CreateHash(const
|
56
|
-
TypeInfo val_info);
|
30
|
+
VALUE Map_CreateHash(const upb_Map *map, upb_CType key_type, TypeInfo val_info);
|
57
31
|
|
58
32
|
// Returns a deep copy of this Map object.
|
59
33
|
VALUE Map_deep_copy(VALUE obj);
|
@@ -64,4 +38,7 @@ extern VALUE cMap;
|
|
64
38
|
// Call at startup to register all types in this module.
|
65
39
|
void Map_register(VALUE module);
|
66
40
|
|
41
|
+
// Recursively freeze map
|
42
|
+
VALUE Map_internal_deep_freeze(VALUE _self);
|
43
|
+
|
67
44
|
#endif // RUBY_PROTOBUF_MAP_H_
|