google-protobuf 3.19.6-x86-linux → 3.20.0.rc.1-x86-linux
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/convert.c +124 -111
- data/ext/google/protobuf_c/convert.h +12 -9
- data/ext/google/protobuf_c/defs.c +196 -201
- data/ext/google/protobuf_c/defs.h +19 -19
- data/ext/google/protobuf_c/extconf.rb +11 -3
- data/ext/google/protobuf_c/map.c +109 -101
- data/ext/google/protobuf_c/map.h +7 -8
- data/ext/google/protobuf_c/message.c +382 -303
- data/ext/google/protobuf_c/message.h +22 -19
- data/ext/google/protobuf_c/protobuf.c +57 -58
- data/ext/google/protobuf_c/protobuf.h +13 -10
- data/ext/google/protobuf_c/repeated_field.c +82 -84
- data/ext/google/protobuf_c/repeated_field.h +6 -6
- data/ext/google/protobuf_c/ruby-upb.c +4945 -3084
- data/ext/google/protobuf_c/ruby-upb.h +2285 -1701
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +395 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/2.5/protobuf_c.so +0 -0
- data/lib/google/2.6/protobuf_c.so +0 -0
- data/lib/google/2.7/protobuf_c.so +0 -0
- data/lib/google/3.0/protobuf_c.so +0 -0
- data/lib/google/protobuf/descriptor_dsl.rb +8 -1
- data/lib/google/protobuf/descriptor_pb.rb +3 -2
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/well_known_types.rb +11 -6
- data/lib/google/protobuf.rb +4 -4
- data/tests/basic.rb +14 -0
- metadata +8 -5
@@ -40,9 +40,9 @@
|
|
40
40
|
// TypeInfo
|
41
41
|
// -----------------------------------------------------------------------------
|
42
42
|
|
43
|
-
// This bundles a
|
43
|
+
// This bundles a upb_CType and msgdef/enumdef when appropriate. This is
|
44
44
|
// convenient for functions that need type information but cannot necessarily
|
45
|
-
// assume a
|
45
|
+
// assume a upb_FieldDef will be available.
|
46
46
|
//
|
47
47
|
// For example, Google::Protobuf::Map and Google::Protobuf::RepeatedField can
|
48
48
|
// be constructed with type information alone:
|
@@ -51,21 +51,21 @@
|
|
51
51
|
// Google::Protobuf::RepeatedField.new(:message, FooMessage)
|
52
52
|
|
53
53
|
typedef struct {
|
54
|
-
|
54
|
+
upb_CType type;
|
55
55
|
union {
|
56
|
-
const
|
57
|
-
const
|
56
|
+
const upb_MessageDef* msgdef; // When type == kUpb_CType_Message
|
57
|
+
const upb_EnumDef* enumdef; // When type == kUpb_CType_Enum
|
58
58
|
} def;
|
59
59
|
} TypeInfo;
|
60
60
|
|
61
|
-
static inline TypeInfo TypeInfo_get(const
|
62
|
-
TypeInfo ret = {
|
61
|
+
static inline TypeInfo TypeInfo_get(const upb_FieldDef* f) {
|
62
|
+
TypeInfo ret = {upb_FieldDef_CType(f), {NULL}};
|
63
63
|
switch (ret.type) {
|
64
|
-
case
|
65
|
-
ret.def.msgdef =
|
64
|
+
case kUpb_CType_Message:
|
65
|
+
ret.def.msgdef = upb_FieldDef_MessageSubDef(f);
|
66
66
|
break;
|
67
|
-
case
|
68
|
-
ret.def.enumdef =
|
67
|
+
case kUpb_CType_Enum:
|
68
|
+
ret.def.enumdef = upb_FieldDef_EnumSubDef(f);
|
69
69
|
break;
|
70
70
|
default:
|
71
71
|
break;
|
@@ -76,9 +76,9 @@ static inline TypeInfo TypeInfo_get(const upb_fielddef *f) {
|
|
76
76
|
TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
|
77
77
|
VALUE* type_class, VALUE* init_arg);
|
78
78
|
|
79
|
-
static inline TypeInfo TypeInfo_from_type(
|
79
|
+
static inline TypeInfo TypeInfo_from_type(upb_CType type) {
|
80
80
|
TypeInfo ret = {type};
|
81
|
-
assert(type !=
|
81
|
+
assert(type != kUpb_CType_Message && type != kUpb_CType_Enum);
|
82
82
|
return ret;
|
83
83
|
}
|
84
84
|
|
@@ -86,17 +86,17 @@ static inline TypeInfo TypeInfo_from_type(upb_fieldtype_t type) {
|
|
86
86
|
// Other utilities
|
87
87
|
// -----------------------------------------------------------------------------
|
88
88
|
|
89
|
-
VALUE Descriptor_DefToClass(const
|
89
|
+
VALUE Descriptor_DefToClass(const upb_MessageDef* m);
|
90
90
|
|
91
91
|
// Returns the underlying msgdef, enumdef, or symtab (respectively) for the
|
92
92
|
// given Descriptor, EnumDescriptor, or DescriptorPool Ruby object.
|
93
|
-
const
|
94
|
-
const
|
95
|
-
const
|
93
|
+
const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb);
|
94
|
+
const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb);
|
95
|
+
const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb);
|
96
96
|
|
97
97
|
// Returns a upb field type for the given Ruby symbol
|
98
|
-
// (eg. :float =>
|
99
|
-
|
98
|
+
// (eg. :float => kUpb_CType_Float).
|
99
|
+
upb_CType ruby_to_fieldtype(VALUE type);
|
100
100
|
|
101
101
|
// The singleton generated pool (a DescriptorPool object).
|
102
102
|
extern VALUE generated_pool;
|
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'mkmf'
|
4
4
|
|
5
|
+
ext_name = "google/protobuf_c"
|
6
|
+
|
7
|
+
dir_config(ext_name)
|
8
|
+
|
5
9
|
if RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/
|
6
10
|
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG -fvisibility=hidden -Wall -Wsign-compare -Wno-declaration-after-statement"
|
7
11
|
else
|
@@ -14,7 +18,11 @@ if RUBY_PLATFORM =~ /linux/
|
|
14
18
|
$LDFLAGS += " -Wl,-wrap,memcpy"
|
15
19
|
end
|
16
20
|
|
17
|
-
$
|
18
|
-
|
21
|
+
$VPATH << "$(srcdir)/third_party/utf8_range"
|
22
|
+
$INCFLAGS << "$(srcdir)/third_party/utf8_range"
|
23
|
+
|
24
|
+
$srcs = ["protobuf.c", "convert.c", "defs.c", "message.c",
|
25
|
+
"repeated_field.c", "map.c", "ruby-upb.c", "wrap_memcpy.c",
|
26
|
+
"utf8_range.c"]
|
19
27
|
|
20
|
-
create_makefile(
|
28
|
+
create_makefile(ext_name)
|
data/ext/google/protobuf_c/map.c
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
#include "protobuf.h"
|
35
35
|
|
36
36
|
// -----------------------------------------------------------------------------
|
37
|
-
// Basic map operations on top of
|
37
|
+
// Basic map operations on top of upb_Map.
|
38
38
|
//
|
39
39
|
// Note that we roll our own `Map` container here because, as for
|
40
40
|
// `RepeatedField`, we want a strongly-typed container. This is so that any user
|
@@ -48,8 +48,8 @@
|
|
48
48
|
// -----------------------------------------------------------------------------
|
49
49
|
|
50
50
|
typedef struct {
|
51
|
-
const
|
52
|
-
|
51
|
+
const upb_Map* map; // Can convert to mutable when non-frozen.
|
52
|
+
upb_CType key_type;
|
53
53
|
TypeInfo value_type_info;
|
54
54
|
VALUE value_type_class;
|
55
55
|
VALUE arena;
|
@@ -62,9 +62,9 @@ static void Map_mark(void* _self) {
|
|
62
62
|
}
|
63
63
|
|
64
64
|
const rb_data_type_t Map_type = {
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
"Google::Protobuf::Map",
|
66
|
+
{Map_mark, RUBY_DEFAULT_FREE, NULL},
|
67
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
68
68
|
};
|
69
69
|
|
70
70
|
VALUE cMap;
|
@@ -84,8 +84,8 @@ static VALUE Map_alloc(VALUE klass) {
|
|
84
84
|
return TypedData_Wrap_Struct(klass, &Map_type, self);
|
85
85
|
}
|
86
86
|
|
87
|
-
VALUE Map_GetRubyWrapper(
|
88
|
-
|
87
|
+
VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
88
|
+
VALUE arena) {
|
89
89
|
PBRUBY_ASSERT(map);
|
90
90
|
|
91
91
|
VALUE val = ObjectCache_Get(map);
|
@@ -99,8 +99,8 @@ VALUE Map_GetRubyWrapper(upb_map* map, upb_fieldtype_t key_type,
|
|
99
99
|
self->arena = arena;
|
100
100
|
self->key_type = key_type;
|
101
101
|
self->value_type_info = value_type;
|
102
|
-
if (self->value_type_info.type ==
|
103
|
-
const
|
102
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
103
|
+
const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
|
104
104
|
self->value_type_class = Descriptor_DefToClass(val_m);
|
105
105
|
}
|
106
106
|
}
|
@@ -108,9 +108,9 @@ VALUE Map_GetRubyWrapper(upb_map* map, upb_fieldtype_t key_type,
|
|
108
108
|
return val;
|
109
109
|
}
|
110
110
|
|
111
|
-
static VALUE Map_new_this_type(Map
|
111
|
+
static VALUE Map_new_this_type(Map* from) {
|
112
112
|
VALUE arena_rb = Arena_new();
|
113
|
-
|
113
|
+
upb_Map* map = upb_Map_New(Arena_get(arena_rb), from->key_type,
|
114
114
|
from->value_type_info.type);
|
115
115
|
VALUE ret =
|
116
116
|
Map_GetRubyWrapper(map, from->key_type, from->value_type_info, arena_rb);
|
@@ -125,22 +125,22 @@ static TypeInfo Map_keyinfo(Map* self) {
|
|
125
125
|
return ret;
|
126
126
|
}
|
127
127
|
|
128
|
-
static
|
128
|
+
static upb_Map* Map_GetMutable(VALUE _self) {
|
129
129
|
rb_check_frozen(_self);
|
130
|
-
return (
|
130
|
+
return (upb_Map*)ruby_to_Map(_self)->map;
|
131
131
|
}
|
132
132
|
|
133
|
-
VALUE Map_CreateHash(const
|
133
|
+
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
|
134
134
|
TypeInfo val_info) {
|
135
135
|
VALUE hash = rb_hash_new();
|
136
|
-
size_t iter =
|
136
|
+
size_t iter = kUpb_Map_Begin;
|
137
137
|
TypeInfo key_info = TypeInfo_from_type(key_type);
|
138
138
|
|
139
139
|
if (!map) return hash;
|
140
140
|
|
141
|
-
while (
|
142
|
-
|
143
|
-
|
141
|
+
while (upb_MapIterator_Next(map, &iter)) {
|
142
|
+
upb_MessageValue key = upb_MapIterator_Key(map, iter);
|
143
|
+
upb_MessageValue val = upb_MapIterator_Value(map, iter);
|
144
144
|
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
|
145
145
|
VALUE val_val = Scalar_CreateHash(val, val_info);
|
146
146
|
rb_hash_aset(hash, key_val, val_val);
|
@@ -152,25 +152,26 @@ VALUE Map_CreateHash(const upb_map* map, upb_fieldtype_t key_type,
|
|
152
152
|
VALUE Map_deep_copy(VALUE obj) {
|
153
153
|
Map* self = ruby_to_Map(obj);
|
154
154
|
VALUE new_arena_rb = Arena_new();
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
size_t iter =
|
159
|
-
while (
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
155
|
+
upb_Arena* arena = Arena_get(new_arena_rb);
|
156
|
+
upb_Map* new_map =
|
157
|
+
upb_Map_New(arena, self->key_type, self->value_type_info.type);
|
158
|
+
size_t iter = kUpb_Map_Begin;
|
159
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
160
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
161
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
162
|
+
upb_MessageValue val_copy =
|
163
|
+
Msgval_DeepCopy(val, self->value_type_info, arena);
|
164
|
+
upb_Map_Set(new_map, key, val_copy, arena);
|
164
165
|
}
|
165
166
|
|
166
167
|
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
167
168
|
new_arena_rb);
|
168
169
|
}
|
169
170
|
|
170
|
-
const
|
171
|
-
|
172
|
-
const
|
173
|
-
const
|
171
|
+
const upb_Map* Map_GetUpbMap(VALUE val, const upb_FieldDef* field,
|
172
|
+
upb_Arena* arena) {
|
173
|
+
const upb_FieldDef* key_field = map_field_key(field);
|
174
|
+
const upb_FieldDef* value_field = map_field_value(field);
|
174
175
|
TypeInfo value_type_info = TypeInfo_get(value_field);
|
175
176
|
Map* self;
|
176
177
|
|
@@ -180,7 +181,7 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
180
181
|
}
|
181
182
|
|
182
183
|
self = ruby_to_Map(val);
|
183
|
-
if (self->key_type !=
|
184
|
+
if (self->key_type != upb_FieldDef_CType(key_field)) {
|
184
185
|
rb_raise(cTypeError, "Map key type does not match field's key type");
|
185
186
|
}
|
186
187
|
if (self->value_type_info.type != value_type_info.type) {
|
@@ -194,16 +195,16 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
194
195
|
return self->map;
|
195
196
|
}
|
196
197
|
|
197
|
-
void Map_Inspect(StringBuilder* b, const
|
198
|
+
void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
|
198
199
|
TypeInfo val_type) {
|
199
200
|
bool first = true;
|
200
201
|
TypeInfo key_type_info = {key_type};
|
201
202
|
StringBuilder_Printf(b, "{");
|
202
203
|
if (map) {
|
203
|
-
size_t iter =
|
204
|
-
while (
|
205
|
-
|
206
|
-
|
204
|
+
size_t iter = kUpb_Map_Begin;
|
205
|
+
while (upb_MapIterator_Next(map, &iter)) {
|
206
|
+
upb_MessageValue key = upb_MapIterator_Key(map, iter);
|
207
|
+
upb_MessageValue val = upb_MapIterator_Value(map, iter);
|
207
208
|
if (first) {
|
208
209
|
first = false;
|
209
210
|
} else {
|
@@ -219,10 +220,12 @@ void Map_Inspect(StringBuilder* b, const upb_map* map, upb_fieldtype_t key_type,
|
|
219
220
|
|
220
221
|
static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
|
221
222
|
Map* self = ruby_to_Map(_self);
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
223
|
+
upb_Arena* arena = Arena_get(self->arena);
|
224
|
+
upb_MessageValue key_val =
|
225
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), arena);
|
226
|
+
upb_MessageValue val_val =
|
227
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
228
|
+
upb_Map_Set(Map_GetMutable(_self), key_val, val_val, arena);
|
226
229
|
return ST_CONTINUE;
|
227
230
|
}
|
228
231
|
|
@@ -234,9 +237,9 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
234
237
|
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
235
238
|
Map* self = ruby_to_Map(_self);
|
236
239
|
Map* other = ruby_to_Map(hashmap);
|
237
|
-
|
238
|
-
|
239
|
-
size_t iter =
|
240
|
+
upb_Arena* arena = Arena_get(self->arena);
|
241
|
+
upb_Message* self_msg = Map_GetMutable(_self);
|
242
|
+
size_t iter = kUpb_Map_Begin;
|
240
243
|
|
241
244
|
Arena_fuse(other->arena, arena);
|
242
245
|
|
@@ -246,10 +249,10 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
246
249
|
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
247
250
|
}
|
248
251
|
|
249
|
-
while (
|
250
|
-
|
251
|
-
|
252
|
-
|
252
|
+
while (upb_MapIterator_Next(other->map, &iter)) {
|
253
|
+
upb_MessageValue key = upb_MapIterator_Key(other->map, iter);
|
254
|
+
upb_MessageValue val = upb_MapIterator_Value(other->map, iter);
|
255
|
+
upb_Map_Set(self_msg, key, val, arena);
|
253
256
|
}
|
254
257
|
} else {
|
255
258
|
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
@@ -305,20 +308,20 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
305
308
|
|
306
309
|
// Check that the key type is an allowed type.
|
307
310
|
switch (self->key_type) {
|
308
|
-
case
|
309
|
-
case
|
310
|
-
case
|
311
|
-
case
|
312
|
-
case
|
313
|
-
case
|
314
|
-
case
|
311
|
+
case kUpb_CType_Int32:
|
312
|
+
case kUpb_CType_Int64:
|
313
|
+
case kUpb_CType_UInt32:
|
314
|
+
case kUpb_CType_UInt64:
|
315
|
+
case kUpb_CType_Bool:
|
316
|
+
case kUpb_CType_String:
|
317
|
+
case kUpb_CType_Bytes:
|
315
318
|
// These are OK.
|
316
319
|
break;
|
317
320
|
default:
|
318
321
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
319
322
|
}
|
320
323
|
|
321
|
-
self->map =
|
324
|
+
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
322
325
|
self->value_type_info.type);
|
323
326
|
ObjectCache_Add(self->map, _self);
|
324
327
|
|
@@ -339,11 +342,11 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
339
342
|
*/
|
340
343
|
static VALUE Map_each(VALUE _self) {
|
341
344
|
Map* self = ruby_to_Map(_self);
|
342
|
-
size_t iter =
|
345
|
+
size_t iter = kUpb_Map_Begin;
|
343
346
|
|
344
|
-
while (
|
345
|
-
|
346
|
-
|
347
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
348
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
349
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
347
350
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
348
351
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
349
352
|
rb_yield_values(2, key_val, val_val);
|
@@ -360,11 +363,11 @@ static VALUE Map_each(VALUE _self) {
|
|
360
363
|
*/
|
361
364
|
static VALUE Map_keys(VALUE _self) {
|
362
365
|
Map* self = ruby_to_Map(_self);
|
363
|
-
size_t iter =
|
366
|
+
size_t iter = kUpb_Map_Begin;
|
364
367
|
VALUE ret = rb_ary_new();
|
365
368
|
|
366
|
-
while (
|
367
|
-
|
369
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
370
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
368
371
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
369
372
|
rb_ary_push(ret, key_val);
|
370
373
|
}
|
@@ -380,11 +383,11 @@ static VALUE Map_keys(VALUE _self) {
|
|
380
383
|
*/
|
381
384
|
static VALUE Map_values(VALUE _self) {
|
382
385
|
Map* self = ruby_to_Map(_self);
|
383
|
-
size_t iter =
|
386
|
+
size_t iter = kUpb_Map_Begin;
|
384
387
|
VALUE ret = rb_ary_new();
|
385
388
|
|
386
|
-
while (
|
387
|
-
|
389
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
390
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
388
391
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
389
392
|
rb_ary_push(ret, val_val);
|
390
393
|
}
|
@@ -401,10 +404,11 @@ static VALUE Map_values(VALUE _self) {
|
|
401
404
|
*/
|
402
405
|
static VALUE Map_index(VALUE _self, VALUE key) {
|
403
406
|
Map* self = ruby_to_Map(_self);
|
404
|
-
|
405
|
-
|
407
|
+
upb_MessageValue key_upb =
|
408
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
409
|
+
upb_MessageValue val;
|
406
410
|
|
407
|
-
if (
|
411
|
+
if (upb_Map_Get(self->map, key_upb, &val)) {
|
408
412
|
return Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
409
413
|
} else {
|
410
414
|
return Qnil;
|
@@ -421,11 +425,13 @@ static VALUE Map_index(VALUE _self, VALUE key) {
|
|
421
425
|
*/
|
422
426
|
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
423
427
|
Map* self = ruby_to_Map(_self);
|
424
|
-
|
425
|
-
|
426
|
-
|
428
|
+
upb_Arena* arena = Arena_get(self->arena);
|
429
|
+
upb_MessageValue key_upb =
|
430
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
431
|
+
upb_MessageValue val_upb =
|
432
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
427
433
|
|
428
|
-
|
434
|
+
upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
429
435
|
|
430
436
|
return val;
|
431
437
|
}
|
@@ -439,9 +445,10 @@ static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
|
439
445
|
*/
|
440
446
|
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
441
447
|
Map* self = ruby_to_Map(_self);
|
442
|
-
|
448
|
+
upb_MessageValue key_upb =
|
449
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
443
450
|
|
444
|
-
if (
|
451
|
+
if (upb_Map_Get(self->map, key_upb, NULL)) {
|
445
452
|
return Qtrue;
|
446
453
|
} else {
|
447
454
|
return Qfalse;
|
@@ -457,21 +464,22 @@ static VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
457
464
|
*/
|
458
465
|
static VALUE Map_delete(VALUE _self, VALUE key) {
|
459
466
|
Map* self = ruby_to_Map(_self);
|
460
|
-
|
461
|
-
|
467
|
+
upb_MessageValue key_upb =
|
468
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
469
|
+
upb_MessageValue val_upb;
|
462
470
|
VALUE ret;
|
463
471
|
|
464
472
|
rb_check_frozen(_self);
|
465
473
|
|
466
|
-
// TODO(haberman): make
|
474
|
+
// TODO(haberman): make upb_Map_Delete() also capable of returning the deleted
|
467
475
|
// value.
|
468
|
-
if (
|
476
|
+
if (upb_Map_Get(self->map, key_upb, &val_upb)) {
|
469
477
|
ret = Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
470
478
|
} else {
|
471
479
|
ret = Qnil;
|
472
480
|
}
|
473
481
|
|
474
|
-
|
482
|
+
upb_Map_Delete(Map_GetMutable(_self), key_upb);
|
475
483
|
|
476
484
|
return ret;
|
477
485
|
}
|
@@ -483,7 +491,7 @@ static VALUE Map_delete(VALUE _self, VALUE key) {
|
|
483
491
|
* Removes all entries from the map.
|
484
492
|
*/
|
485
493
|
static VALUE Map_clear(VALUE _self) {
|
486
|
-
|
494
|
+
upb_Map_Clear(Map_GetMutable(_self));
|
487
495
|
return Qnil;
|
488
496
|
}
|
489
497
|
|
@@ -495,7 +503,7 @@ static VALUE Map_clear(VALUE _self) {
|
|
495
503
|
*/
|
496
504
|
static VALUE Map_length(VALUE _self) {
|
497
505
|
Map* self = ruby_to_Map(_self);
|
498
|
-
return ULL2NUM(
|
506
|
+
return ULL2NUM(upb_Map_Size(self->map));
|
499
507
|
}
|
500
508
|
|
501
509
|
/*
|
@@ -509,16 +517,16 @@ static VALUE Map_dup(VALUE _self) {
|
|
509
517
|
Map* self = ruby_to_Map(_self);
|
510
518
|
VALUE new_map_rb = Map_new_this_type(self);
|
511
519
|
Map* new_self = ruby_to_Map(new_map_rb);
|
512
|
-
size_t iter =
|
513
|
-
|
514
|
-
|
520
|
+
size_t iter = kUpb_Map_Begin;
|
521
|
+
upb_Arena* arena = Arena_get(new_self->arena);
|
522
|
+
upb_Map* new_map = Map_GetMutable(new_map_rb);
|
515
523
|
|
516
524
|
Arena_fuse(self->arena, arena);
|
517
525
|
|
518
|
-
while (
|
519
|
-
|
520
|
-
|
521
|
-
|
526
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
527
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
528
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
529
|
+
upb_Map_Set(new_map, key, val, arena);
|
522
530
|
}
|
523
531
|
|
524
532
|
return new_map_rb;
|
@@ -559,18 +567,18 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
559
567
|
self->value_type_class != other->value_type_class) {
|
560
568
|
return Qfalse;
|
561
569
|
}
|
562
|
-
if (
|
570
|
+
if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
|
563
571
|
return Qfalse;
|
564
572
|
}
|
565
573
|
|
566
574
|
// For each member of self, check that an equal member exists at the same key
|
567
575
|
// in other.
|
568
|
-
size_t iter =
|
569
|
-
while (
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
if (!
|
576
|
+
size_t iter = kUpb_Map_Begin;
|
577
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
578
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
579
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
580
|
+
upb_MessageValue other_val;
|
581
|
+
if (!upb_Map_Get(other->map, key, &other_val)) {
|
574
582
|
// Not present in other map.
|
575
583
|
return Qfalse;
|
576
584
|
}
|
@@ -609,11 +617,11 @@ VALUE Map_hash(VALUE _self) {
|
|
609
617
|
Map* self = ruby_to_Map(_self);
|
610
618
|
uint64_t hash = 0;
|
611
619
|
|
612
|
-
size_t iter =
|
620
|
+
size_t iter = kUpb_Map_Begin;
|
613
621
|
TypeInfo key_info = {self->key_type};
|
614
|
-
while (
|
615
|
-
|
616
|
-
|
622
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
623
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
624
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
617
625
|
hash = Msgval_GetHash(key, key_info, hash);
|
618
626
|
hash = Msgval_GetHash(val, self->value_type_info, hash);
|
619
627
|
}
|
data/ext/google/protobuf_c/map.h
CHANGED
@@ -38,22 +38,21 @@
|
|
38
38
|
|
39
39
|
// Returns a Ruby wrapper object for the given map, which will be created if
|
40
40
|
// one does not exist already.
|
41
|
-
VALUE Map_GetRubyWrapper(
|
42
|
-
|
41
|
+
VALUE Map_GetRubyWrapper(upb_Map *map, upb_CType key_type, TypeInfo value_type,
|
42
|
+
VALUE arena);
|
43
43
|
|
44
|
-
// Gets the underlying
|
44
|
+
// Gets the underlying upb_Map for this Ruby map object, which must have
|
45
45
|
// key/value type that match |field|. If this is not a map or the type doesn't
|
46
46
|
// match, raises an exception.
|
47
|
-
const
|
48
|
-
|
47
|
+
const upb_Map *Map_GetUpbMap(VALUE val, const upb_FieldDef *field,
|
48
|
+
upb_Arena *arena);
|
49
49
|
|
50
50
|
// Implements #inspect for this map by appending its contents to |b|.
|
51
|
-
void Map_Inspect(StringBuilder *b, const
|
51
|
+
void Map_Inspect(StringBuilder *b, const upb_Map *map, upb_CType key_type,
|
52
52
|
TypeInfo val_type);
|
53
53
|
|
54
54
|
// Returns a new Hash object containing the contents of this Map.
|
55
|
-
VALUE Map_CreateHash(const
|
56
|
-
TypeInfo val_info);
|
55
|
+
VALUE Map_CreateHash(const upb_Map *map, upb_CType key_type, TypeInfo val_info);
|
57
56
|
|
58
57
|
// Returns a deep copy of this Map object.
|
59
58
|
VALUE Map_deep_copy(VALUE obj);
|