google-protobuf 3.17.3 → 3.22.1-arm64-darwin
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/convert.c +128 -116
- data/ext/google/protobuf_c/convert.h +12 -9
- data/ext/google/protobuf_c/defs.c +217 -1521
- data/ext/google/protobuf_c/defs.h +19 -19
- data/ext/google/protobuf_c/extconf.rb +12 -6
- data/ext/google/protobuf_c/map.c +105 -109
- data/ext/google/protobuf_c/map.h +7 -8
- data/ext/google/protobuf_c/message.c +438 -341
- data/ext/google/protobuf_c/message.h +22 -19
- data/ext/google/protobuf_c/protobuf.c +68 -58
- data/ext/google/protobuf_c/protobuf.h +13 -10
- data/ext/google/protobuf_c/repeated_field.c +83 -85
- data/ext/google/protobuf_c/repeated_field.h +6 -6
- data/ext/google/protobuf_c/ruby-upb.c +11782 -6714
- data/ext/google/protobuf_c/ruby-upb.h +9668 -3406
- 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 +4 -3
- data/lib/google/2.6/protobuf_c.bundle +0 -0
- data/lib/google/2.7/protobuf_c.bundle +0 -0
- data/lib/google/3.0/protobuf_c.bundle +0 -0
- data/lib/google/3.1/protobuf_c.bundle +0 -0
- data/lib/google/3.2/protobuf_c.bundle +0 -0
- data/lib/google/protobuf/any_pb.rb +0 -0
- data/lib/google/protobuf/api_pb.rb +1 -0
- data/lib/google/protobuf/descriptor_dsl.rb +465 -0
- data/lib/google/protobuf/descriptor_pb.rb +298 -0
- data/lib/google/protobuf/duration_pb.rb +0 -0
- data/lib/google/protobuf/empty_pb.rb +0 -0
- data/lib/google/protobuf/field_mask_pb.rb +0 -0
- data/lib/google/protobuf/message_exts.rb +7 -2
- data/lib/google/protobuf/plugin_pb.rb +50 -0
- data/lib/google/protobuf/repeated_field.rb +15 -2
- data/lib/google/protobuf/source_context_pb.rb +0 -0
- data/lib/google/protobuf/struct_pb.rb +0 -0
- data/lib/google/protobuf/timestamp_pb.rb +0 -0
- data/lib/google/protobuf/type_pb.rb +1 -0
- data/lib/google/protobuf/well_known_types.rb +12 -2
- data/lib/google/protobuf/wrappers_pb.rb +0 -0
- data/lib/google/protobuf.rb +5 -73
- metadata +28 -40
- data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +0 -145
- data/tests/basic.rb +0 -611
- data/tests/generated_code_test.rb +0 -23
- data/tests/stress.rb +0 -38
@@ -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,20 +2,26 @@
|
|
2
2
|
|
3
3
|
require 'mkmf'
|
4
4
|
|
5
|
-
|
5
|
+
ext_name = "google/protobuf_c"
|
6
|
+
|
7
|
+
dir_config(ext_name)
|
8
|
+
|
9
|
+
if RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/ || RUBY_PLATFORM =~ /freebsd/
|
6
10
|
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG -fvisibility=hidden -Wall -Wsign-compare -Wno-declaration-after-statement"
|
7
11
|
else
|
8
12
|
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG"
|
9
13
|
end
|
10
14
|
|
11
|
-
|
12
15
|
if RUBY_PLATFORM =~ /linux/
|
13
16
|
# Instruct the linker to point memcpy calls at our __wrap_memcpy wrapper.
|
14
17
|
$LDFLAGS += " -Wl,-wrap,memcpy"
|
15
18
|
end
|
16
19
|
|
17
|
-
$
|
18
|
-
|
20
|
+
$VPATH << "$(srcdir)/third_party/utf8_range"
|
21
|
+
$INCFLAGS += " -I$(srcdir)/third_party/utf8_range"
|
22
|
+
|
23
|
+
$srcs = ["protobuf.c", "convert.c", "defs.c", "message.c",
|
24
|
+
"repeated_field.c", "map.c", "ruby-upb.c", "wrap_memcpy.c",
|
25
|
+
"naive.c", "range2-neon.c", "range2-sse.c"]
|
19
26
|
|
20
|
-
|
21
|
-
create_makefile("google/protobuf_c")
|
27
|
+
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,21 @@ 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 = UPB_MAP_BEGIN;
|
137
136
|
TypeInfo key_info = TypeInfo_from_type(key_type);
|
138
137
|
|
139
138
|
if (!map) return hash;
|
140
139
|
|
141
|
-
|
142
|
-
|
143
|
-
|
140
|
+
size_t iter = kUpb_Map_Begin;
|
141
|
+
upb_MessageValue key, val;
|
142
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
144
143
|
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
|
145
144
|
VALUE val_val = Scalar_CreateHash(val, val_info);
|
146
145
|
rb_hash_aset(hash, key_val, val_val);
|
@@ -152,25 +151,25 @@ VALUE Map_CreateHash(const upb_map* map, upb_fieldtype_t key_type,
|
|
152
151
|
VALUE Map_deep_copy(VALUE obj) {
|
153
152
|
Map* self = ruby_to_Map(obj);
|
154
153
|
VALUE new_arena_rb = Arena_new();
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
size_t iter =
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
154
|
+
upb_Arena* arena = Arena_get(new_arena_rb);
|
155
|
+
upb_Map* new_map =
|
156
|
+
upb_Map_New(arena, self->key_type, self->value_type_info.type);
|
157
|
+
size_t iter = kUpb_Map_Begin;
|
158
|
+
upb_MessageValue key, val;
|
159
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
160
|
+
upb_MessageValue val_copy =
|
161
|
+
Msgval_DeepCopy(val, self->value_type_info, arena);
|
162
|
+
upb_Map_Set(new_map, key, val_copy, arena);
|
164
163
|
}
|
165
164
|
|
166
165
|
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
167
166
|
new_arena_rb);
|
168
167
|
}
|
169
168
|
|
170
|
-
const
|
171
|
-
|
172
|
-
const
|
173
|
-
const
|
169
|
+
const upb_Map* Map_GetUpbMap(VALUE val, const upb_FieldDef* field,
|
170
|
+
upb_Arena* arena) {
|
171
|
+
const upb_FieldDef* key_field = map_field_key(field);
|
172
|
+
const upb_FieldDef* value_field = map_field_value(field);
|
174
173
|
TypeInfo value_type_info = TypeInfo_get(value_field);
|
175
174
|
Map* self;
|
176
175
|
|
@@ -180,7 +179,7 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
180
179
|
}
|
181
180
|
|
182
181
|
self = ruby_to_Map(val);
|
183
|
-
if (self->key_type !=
|
182
|
+
if (self->key_type != upb_FieldDef_CType(key_field)) {
|
184
183
|
rb_raise(cTypeError, "Map key type does not match field's key type");
|
185
184
|
}
|
186
185
|
if (self->value_type_info.type != value_type_info.type) {
|
@@ -194,16 +193,15 @@ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
|
|
194
193
|
return self->map;
|
195
194
|
}
|
196
195
|
|
197
|
-
void Map_Inspect(StringBuilder* b, const
|
196
|
+
void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
|
198
197
|
TypeInfo val_type) {
|
199
198
|
bool first = true;
|
200
199
|
TypeInfo key_type_info = {key_type};
|
201
200
|
StringBuilder_Printf(b, "{");
|
202
201
|
if (map) {
|
203
|
-
size_t iter =
|
204
|
-
|
205
|
-
|
206
|
-
upb_msgval val = upb_mapiter_value(map, iter);
|
202
|
+
size_t iter = kUpb_Map_Begin;
|
203
|
+
upb_MessageValue key, val;
|
204
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
207
205
|
if (first) {
|
208
206
|
first = false;
|
209
207
|
} else {
|
@@ -219,10 +217,12 @@ void Map_Inspect(StringBuilder* b, const upb_map* map, upb_fieldtype_t key_type,
|
|
219
217
|
|
220
218
|
static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
|
221
219
|
Map* self = ruby_to_Map(_self);
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
220
|
+
upb_Arena* arena = Arena_get(self->arena);
|
221
|
+
upb_MessageValue key_val =
|
222
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), arena);
|
223
|
+
upb_MessageValue val_val =
|
224
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
225
|
+
upb_Map_Set(Map_GetMutable(_self), key_val, val_val, arena);
|
226
226
|
return ST_CONTINUE;
|
227
227
|
}
|
228
228
|
|
@@ -234,9 +234,8 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
234
234
|
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
235
235
|
Map* self = ruby_to_Map(_self);
|
236
236
|
Map* other = ruby_to_Map(hashmap);
|
237
|
-
|
238
|
-
|
239
|
-
size_t iter = UPB_MAP_BEGIN;
|
237
|
+
upb_Arena* arena = Arena_get(self->arena);
|
238
|
+
upb_Message* self_msg = Map_GetMutable(_self);
|
240
239
|
|
241
240
|
Arena_fuse(other->arena, arena);
|
242
241
|
|
@@ -246,10 +245,10 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
246
245
|
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
247
246
|
}
|
248
247
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
248
|
+
size_t iter = kUpb_Map_Begin;
|
249
|
+
upb_MessageValue key, val;
|
250
|
+
while (upb_Map_Next(other->map, &key, &val, &iter)) {
|
251
|
+
upb_Map_Set(self_msg, key, val, arena);
|
253
252
|
}
|
254
253
|
} else {
|
255
254
|
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
@@ -305,20 +304,20 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
305
304
|
|
306
305
|
// Check that the key type is an allowed type.
|
307
306
|
switch (self->key_type) {
|
308
|
-
case
|
309
|
-
case
|
310
|
-
case
|
311
|
-
case
|
312
|
-
case
|
313
|
-
case
|
314
|
-
case
|
307
|
+
case kUpb_CType_Int32:
|
308
|
+
case kUpb_CType_Int64:
|
309
|
+
case kUpb_CType_UInt32:
|
310
|
+
case kUpb_CType_UInt64:
|
311
|
+
case kUpb_CType_Bool:
|
312
|
+
case kUpb_CType_String:
|
313
|
+
case kUpb_CType_Bytes:
|
315
314
|
// These are OK.
|
316
315
|
break;
|
317
316
|
default:
|
318
317
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
319
318
|
}
|
320
319
|
|
321
|
-
self->map =
|
320
|
+
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
322
321
|
self->value_type_info.type);
|
323
322
|
ObjectCache_Add(self->map, _self);
|
324
323
|
|
@@ -339,11 +338,10 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
339
338
|
*/
|
340
339
|
static VALUE Map_each(VALUE _self) {
|
341
340
|
Map* self = ruby_to_Map(_self);
|
342
|
-
size_t iter =
|
341
|
+
size_t iter = kUpb_Map_Begin;
|
342
|
+
upb_MessageValue key, val;
|
343
343
|
|
344
|
-
while (
|
345
|
-
upb_msgval key = upb_mapiter_key(self->map, iter);
|
346
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
344
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
347
345
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
348
346
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
349
347
|
rb_yield_values(2, key_val, val_val);
|
@@ -360,11 +358,11 @@ static VALUE Map_each(VALUE _self) {
|
|
360
358
|
*/
|
361
359
|
static VALUE Map_keys(VALUE _self) {
|
362
360
|
Map* self = ruby_to_Map(_self);
|
363
|
-
size_t iter =
|
361
|
+
size_t iter = kUpb_Map_Begin;
|
364
362
|
VALUE ret = rb_ary_new();
|
363
|
+
upb_MessageValue key, val;
|
365
364
|
|
366
|
-
while (
|
367
|
-
upb_msgval key = upb_mapiter_key(self->map, iter);
|
365
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
368
366
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
369
367
|
rb_ary_push(ret, key_val);
|
370
368
|
}
|
@@ -380,11 +378,11 @@ static VALUE Map_keys(VALUE _self) {
|
|
380
378
|
*/
|
381
379
|
static VALUE Map_values(VALUE _self) {
|
382
380
|
Map* self = ruby_to_Map(_self);
|
383
|
-
size_t iter =
|
381
|
+
size_t iter = kUpb_Map_Begin;
|
384
382
|
VALUE ret = rb_ary_new();
|
383
|
+
upb_MessageValue key, val;
|
385
384
|
|
386
|
-
while (
|
387
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
385
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
388
386
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
389
387
|
rb_ary_push(ret, val_val);
|
390
388
|
}
|
@@ -401,10 +399,11 @@ static VALUE Map_values(VALUE _self) {
|
|
401
399
|
*/
|
402
400
|
static VALUE Map_index(VALUE _self, VALUE key) {
|
403
401
|
Map* self = ruby_to_Map(_self);
|
404
|
-
|
405
|
-
|
402
|
+
upb_MessageValue key_upb =
|
403
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
404
|
+
upb_MessageValue val;
|
406
405
|
|
407
|
-
if (
|
406
|
+
if (upb_Map_Get(self->map, key_upb, &val)) {
|
408
407
|
return Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
409
408
|
} else {
|
410
409
|
return Qnil;
|
@@ -421,11 +420,13 @@ static VALUE Map_index(VALUE _self, VALUE key) {
|
|
421
420
|
*/
|
422
421
|
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
423
422
|
Map* self = ruby_to_Map(_self);
|
424
|
-
|
425
|
-
|
426
|
-
|
423
|
+
upb_Arena* arena = Arena_get(self->arena);
|
424
|
+
upb_MessageValue key_upb =
|
425
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
426
|
+
upb_MessageValue val_upb =
|
427
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
427
428
|
|
428
|
-
|
429
|
+
upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
429
430
|
|
430
431
|
return val;
|
431
432
|
}
|
@@ -439,9 +440,10 @@ static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
|
439
440
|
*/
|
440
441
|
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
441
442
|
Map* self = ruby_to_Map(_self);
|
442
|
-
|
443
|
+
upb_MessageValue key_upb =
|
444
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
443
445
|
|
444
|
-
if (
|
446
|
+
if (upb_Map_Get(self->map, key_upb, NULL)) {
|
445
447
|
return Qtrue;
|
446
448
|
} else {
|
447
449
|
return Qfalse;
|
@@ -457,23 +459,17 @@ static VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
457
459
|
*/
|
458
460
|
static VALUE Map_delete(VALUE _self, VALUE key) {
|
459
461
|
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
462
|
rb_check_frozen(_self);
|
465
463
|
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
464
|
+
upb_MessageValue key_upb =
|
465
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
466
|
+
upb_MessageValue val_upb;
|
467
|
+
|
468
|
+
if (upb_Map_Delete(self->map, key_upb, &val_upb)) {
|
469
|
+
return Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
470
470
|
} else {
|
471
|
-
|
471
|
+
return Qnil;
|
472
472
|
}
|
473
|
-
|
474
|
-
upb_map_delete(Map_GetMutable(_self), key_upb);
|
475
|
-
|
476
|
-
return ret;
|
477
473
|
}
|
478
474
|
|
479
475
|
/*
|
@@ -483,7 +479,7 @@ static VALUE Map_delete(VALUE _self, VALUE key) {
|
|
483
479
|
* Removes all entries from the map.
|
484
480
|
*/
|
485
481
|
static VALUE Map_clear(VALUE _self) {
|
486
|
-
|
482
|
+
upb_Map_Clear(Map_GetMutable(_self));
|
487
483
|
return Qnil;
|
488
484
|
}
|
489
485
|
|
@@ -495,7 +491,7 @@ static VALUE Map_clear(VALUE _self) {
|
|
495
491
|
*/
|
496
492
|
static VALUE Map_length(VALUE _self) {
|
497
493
|
Map* self = ruby_to_Map(_self);
|
498
|
-
return ULL2NUM(
|
494
|
+
return ULL2NUM(upb_Map_Size(self->map));
|
499
495
|
}
|
500
496
|
|
501
497
|
/*
|
@@ -509,16 +505,15 @@ static VALUE Map_dup(VALUE _self) {
|
|
509
505
|
Map* self = ruby_to_Map(_self);
|
510
506
|
VALUE new_map_rb = Map_new_this_type(self);
|
511
507
|
Map* new_self = ruby_to_Map(new_map_rb);
|
512
|
-
size_t iter =
|
513
|
-
|
514
|
-
|
508
|
+
size_t iter = kUpb_Map_Begin;
|
509
|
+
upb_Arena* arena = Arena_get(new_self->arena);
|
510
|
+
upb_Map* new_map = Map_GetMutable(new_map_rb);
|
515
511
|
|
516
512
|
Arena_fuse(self->arena, arena);
|
517
513
|
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
upb_map_set(new_map, key, val, arena);
|
514
|
+
upb_MessageValue key, val;
|
515
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
516
|
+
upb_Map_Set(new_map, key, val, arena);
|
522
517
|
}
|
523
518
|
|
524
519
|
return new_map_rb;
|
@@ -559,18 +554,17 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
559
554
|
self->value_type_class != other->value_type_class) {
|
560
555
|
return Qfalse;
|
561
556
|
}
|
562
|
-
if (
|
557
|
+
if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
|
563
558
|
return Qfalse;
|
564
559
|
}
|
565
560
|
|
566
561
|
// For each member of self, check that an equal member exists at the same key
|
567
562
|
// in other.
|
568
|
-
size_t iter =
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
if (!upb_map_get(other->map, key, &other_val)) {
|
563
|
+
size_t iter = kUpb_Map_Begin;
|
564
|
+
upb_MessageValue key, val;
|
565
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
566
|
+
upb_MessageValue other_val;
|
567
|
+
if (!upb_Map_Get(other->map, key, &other_val)) {
|
574
568
|
// Not present in other map.
|
575
569
|
return Qfalse;
|
576
570
|
}
|
@@ -609,11 +603,10 @@ VALUE Map_hash(VALUE _self) {
|
|
609
603
|
Map* self = ruby_to_Map(_self);
|
610
604
|
uint64_t hash = 0;
|
611
605
|
|
612
|
-
size_t iter =
|
606
|
+
size_t iter = kUpb_Map_Begin;
|
613
607
|
TypeInfo key_info = {self->key_type};
|
614
|
-
|
615
|
-
|
616
|
-
upb_msgval val = upb_mapiter_value(self->map, iter);
|
608
|
+
upb_MessageValue key, val;
|
609
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
617
610
|
hash = Msgval_GetHash(key, key_info, hash);
|
618
611
|
hash = Msgval_GetHash(val, self->value_type_info, hash);
|
619
612
|
}
|
@@ -680,7 +673,10 @@ void Map_register(VALUE module) {
|
|
680
673
|
rb_define_method(klass, "delete", Map_delete, 1);
|
681
674
|
rb_define_method(klass, "clear", Map_clear, 0);
|
682
675
|
rb_define_method(klass, "length", Map_length, 0);
|
676
|
+
rb_define_method(klass, "size", Map_length, 0);
|
683
677
|
rb_define_method(klass, "dup", Map_dup, 0);
|
678
|
+
// Also define #clone so that we don't inherit Object#clone.
|
679
|
+
rb_define_method(klass, "clone", Map_dup, 0);
|
684
680
|
rb_define_method(klass, "==", Map_eq, 1);
|
685
681
|
rb_define_method(klass, "freeze", Map_freeze, 0);
|
686
682
|
rb_define_method(klass, "hash", Map_hash, 0);
|
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);
|