google-protobuf 3.7.0 → 3.21.0
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 +361 -0
- data/ext/google/protobuf_c/convert.h +75 -0
- data/ext/google/protobuf_c/defs.c +669 -1646
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +13 -8
- data/ext/google/protobuf_c/map.c +330 -477
- data/ext/google/protobuf_c/map.h +66 -0
- data/ext/google/protobuf_c/message.c +1048 -379
- data/ext/google/protobuf_c/message.h +104 -0
- data/ext/google/protobuf_c/protobuf.c +413 -54
- data/ext/google/protobuf_c/protobuf.h +53 -546
- data/ext/google/protobuf_c/repeated_field.c +318 -315
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +11115 -0
- data/ext/google/protobuf_c/ruby-upb.h +5612 -0
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -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 +9 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/protobuf/any_pb.rb +1 -1
- data/lib/google/protobuf/api_pb.rb +4 -3
- data/lib/google/protobuf/descriptor_dsl.rb +465 -0
- data/lib/google/protobuf/descriptor_pb.rb +269 -0
- data/lib/google/protobuf/duration_pb.rb +1 -1
- data/lib/google/protobuf/empty_pb.rb +1 -1
- data/lib/google/protobuf/field_mask_pb.rb +1 -1
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/repeated_field.rb +15 -2
- data/lib/google/protobuf/source_context_pb.rb +1 -1
- data/lib/google/protobuf/struct_pb.rb +4 -4
- data/lib/google/protobuf/timestamp_pb.rb +1 -1
- data/lib/google/protobuf/type_pb.rb +9 -8
- data/lib/google/protobuf/well_known_types.rb +20 -4
- data/lib/google/protobuf/wrappers_pb.rb +9 -9
- data/lib/google/protobuf.rb +6 -4
- data/tests/basic.rb +455 -77
- data/tests/generated_code_test.rb +0 -0
- data/tests/stress.rb +1 -1
- metadata +27 -30
- data/ext/google/protobuf_c/encode_decode.c +0 -1574
- data/ext/google/protobuf_c/storage.c +0 -1019
- data/ext/google/protobuf_c/upb.c +0 -17318
- data/ext/google/protobuf_c/upb.h +0 -9755
data/ext/google/protobuf_c/map.c
CHANGED
@@ -28,167 +28,236 @@
|
|
28
28
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
29
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
30
|
|
31
|
+
#include "convert.h"
|
32
|
+
#include "defs.h"
|
33
|
+
#include "message.h"
|
31
34
|
#include "protobuf.h"
|
32
35
|
|
33
36
|
// -----------------------------------------------------------------------------
|
34
|
-
// Basic map operations on top of
|
37
|
+
// Basic map operations on top of upb_Map.
|
35
38
|
//
|
36
39
|
// Note that we roll our own `Map` container here because, as for
|
37
40
|
// `RepeatedField`, we want a strongly-typed container. This is so that any user
|
38
41
|
// errors due to incorrect map key or value types are raised as close as
|
39
42
|
// possible to the error site, rather than at some deferred point (e.g.,
|
40
43
|
// serialization).
|
41
|
-
//
|
42
|
-
// We build our `Map` on top of upb_strtable so that we're able to take
|
43
|
-
// advantage of the native_slot storage abstraction, as RepeatedField does.
|
44
|
-
// (This is not quite a perfect mapping -- see the key conversions below -- but
|
45
|
-
// gives us full support and error-checking for all value types for free.)
|
46
44
|
// -----------------------------------------------------------------------------
|
47
45
|
|
48
|
-
//
|
49
|
-
//
|
50
|
-
//
|
51
|
-
// one-to-one to equality of keys. We store strings directly (i.e., they map to
|
52
|
-
// their own bytes) and integers as native integers (using the native_slot
|
53
|
-
// abstraction).
|
54
|
-
|
55
|
-
// Note that there is another tradeoff here in keeping string keys as native
|
56
|
-
// strings rather than Ruby strings: traversing the Map requires conversion to
|
57
|
-
// Ruby string values on every traversal, potentially creating more garbage. We
|
58
|
-
// should consider ways to cache a Ruby version of the key if this becomes an
|
59
|
-
// issue later.
|
60
|
-
|
61
|
-
// Forms a key to use with the underlying strtable from a Ruby key value. |buf|
|
62
|
-
// must point to TABLE_KEY_BUF_LENGTH bytes of temporary space, used to
|
63
|
-
// construct a key byte sequence if needed. |out_key| and |out_length| provide
|
64
|
-
// the resulting key data/length.
|
65
|
-
#define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t)
|
66
|
-
static VALUE table_key(Map* self, VALUE key,
|
67
|
-
char* buf,
|
68
|
-
const char** out_key,
|
69
|
-
size_t* out_length) {
|
70
|
-
switch (self->key_type) {
|
71
|
-
case UPB_TYPE_BYTES:
|
72
|
-
case UPB_TYPE_STRING:
|
73
|
-
// Strings: use string content directly.
|
74
|
-
Check_Type(key, T_STRING);
|
75
|
-
key = native_slot_encode_and_freeze_string(self->key_type, key);
|
76
|
-
*out_key = RSTRING_PTR(key);
|
77
|
-
*out_length = RSTRING_LEN(key);
|
78
|
-
break;
|
46
|
+
// -----------------------------------------------------------------------------
|
47
|
+
// Map container type.
|
48
|
+
// -----------------------------------------------------------------------------
|
79
49
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
*out_length = native_slot_size(self->key_type);
|
88
|
-
break;
|
50
|
+
typedef struct {
|
51
|
+
const upb_Map* map; // Can convert to mutable when non-frozen.
|
52
|
+
upb_CType key_type;
|
53
|
+
TypeInfo value_type_info;
|
54
|
+
VALUE value_type_class;
|
55
|
+
VALUE arena;
|
56
|
+
} Map;
|
89
57
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
58
|
+
static void Map_mark(void* _self) {
|
59
|
+
Map* self = _self;
|
60
|
+
rb_gc_mark(self->value_type_class);
|
61
|
+
rb_gc_mark(self->arena);
|
62
|
+
}
|
63
|
+
|
64
|
+
const rb_data_type_t Map_type = {
|
65
|
+
"Google::Protobuf::Map",
|
66
|
+
{Map_mark, RUBY_DEFAULT_FREE, NULL},
|
67
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
68
|
+
};
|
69
|
+
|
70
|
+
VALUE cMap;
|
96
71
|
|
97
|
-
|
72
|
+
static Map* ruby_to_Map(VALUE _self) {
|
73
|
+
Map* self;
|
74
|
+
TypedData_Get_Struct(_self, Map, &Map_type, self);
|
75
|
+
return self;
|
98
76
|
}
|
99
77
|
|
100
|
-
static VALUE
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
78
|
+
static VALUE Map_alloc(VALUE klass) {
|
79
|
+
Map* self = ALLOC(Map);
|
80
|
+
self->map = NULL;
|
81
|
+
self->value_type_class = Qnil;
|
82
|
+
self->value_type_info.def.msgdef = NULL;
|
83
|
+
self->arena = Qnil;
|
84
|
+
return TypedData_Wrap_Struct(klass, &Map_type, self);
|
85
|
+
}
|
86
|
+
|
87
|
+
VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
88
|
+
VALUE arena) {
|
89
|
+
PBRUBY_ASSERT(map);
|
90
|
+
|
91
|
+
VALUE val = ObjectCache_Get(map);
|
92
|
+
|
93
|
+
if (val == Qnil) {
|
94
|
+
val = Map_alloc(cMap);
|
95
|
+
Map* self;
|
96
|
+
ObjectCache_Add(map, val);
|
97
|
+
TypedData_Get_Struct(val, Map, &Map_type, self);
|
98
|
+
self->map = map;
|
99
|
+
self->arena = arena;
|
100
|
+
self->key_type = key_type;
|
101
|
+
self->value_type_info = value_type;
|
102
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
103
|
+
const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
|
104
|
+
self->value_type_class = Descriptor_DefToClass(val_m);
|
109
105
|
}
|
106
|
+
}
|
110
107
|
|
111
|
-
|
112
|
-
|
113
|
-
case UPB_TYPE_INT64:
|
114
|
-
case UPB_TYPE_UINT32:
|
115
|
-
case UPB_TYPE_UINT64:
|
116
|
-
return native_slot_get(self->key_type, Qnil, buf);
|
108
|
+
return val;
|
109
|
+
}
|
117
110
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
111
|
+
static VALUE Map_new_this_type(Map* from) {
|
112
|
+
VALUE arena_rb = Arena_new();
|
113
|
+
upb_Map* map = upb_Map_New(Arena_get(arena_rb), from->key_type,
|
114
|
+
from->value_type_info.type);
|
115
|
+
VALUE ret =
|
116
|
+
Map_GetRubyWrapper(map, from->key_type, from->value_type_info, arena_rb);
|
117
|
+
PBRUBY_ASSERT(ruby_to_Map(ret)->value_type_class == from->value_type_class);
|
118
|
+
return ret;
|
122
119
|
}
|
123
120
|
|
124
|
-
static
|
125
|
-
|
121
|
+
static TypeInfo Map_keyinfo(Map* self) {
|
122
|
+
TypeInfo ret;
|
123
|
+
ret.type = self->key_type;
|
124
|
+
ret.def.msgdef = NULL;
|
125
|
+
return ret;
|
126
126
|
}
|
127
127
|
|
128
|
-
|
129
|
-
|
130
|
-
|
128
|
+
static upb_Map* Map_GetMutable(VALUE _self) {
|
129
|
+
rb_check_frozen(_self);
|
130
|
+
return (upb_Map*)ruby_to_Map(_self)->map;
|
131
|
+
}
|
131
132
|
|
132
|
-
const
|
133
|
-
|
134
|
-
|
135
|
-
|
133
|
+
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
|
134
|
+
TypeInfo val_info) {
|
135
|
+
VALUE hash = rb_hash_new();
|
136
|
+
size_t iter = kUpb_Map_Begin;
|
137
|
+
TypeInfo key_info = TypeInfo_from_type(key_type);
|
136
138
|
|
137
|
-
|
139
|
+
if (!map) return hash;
|
138
140
|
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
+
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
|
145
|
+
VALUE val_val = Scalar_CreateHash(val, val_info);
|
146
|
+
rb_hash_aset(hash, key_val, val_val);
|
147
|
+
}
|
144
148
|
|
145
|
-
|
146
|
-
|
149
|
+
return hash;
|
150
|
+
}
|
147
151
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
self->
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
}
|
152
|
+
VALUE Map_deep_copy(VALUE obj) {
|
153
|
+
Map* self = ruby_to_Map(obj);
|
154
|
+
VALUE new_arena_rb = Arena_new();
|
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);
|
162
165
|
}
|
166
|
+
|
167
|
+
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
168
|
+
new_arena_rb);
|
163
169
|
}
|
164
170
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
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);
|
175
|
+
TypeInfo value_type_info = TypeInfo_get(value_field);
|
176
|
+
Map* self;
|
177
|
+
|
178
|
+
if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
|
179
|
+
RTYPEDDATA_TYPE(val) != &Map_type) {
|
180
|
+
rb_raise(cTypeError, "Expected Map instance");
|
181
|
+
}
|
182
|
+
|
183
|
+
self = ruby_to_Map(val);
|
184
|
+
if (self->key_type != upb_FieldDef_CType(key_field)) {
|
185
|
+
rb_raise(cTypeError, "Map key type does not match field's key type");
|
186
|
+
}
|
187
|
+
if (self->value_type_info.type != value_type_info.type) {
|
188
|
+
rb_raise(cTypeError, "Map value type does not match field's value type");
|
189
|
+
}
|
190
|
+
if (self->value_type_info.def.msgdef != value_type_info.def.msgdef) {
|
191
|
+
rb_raise(cTypeError, "Map value type has wrong message/enum class");
|
192
|
+
}
|
193
|
+
|
194
|
+
Arena_fuse(self->arena, arena);
|
195
|
+
return self->map;
|
169
196
|
}
|
170
197
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
198
|
+
void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
|
199
|
+
TypeInfo val_type) {
|
200
|
+
bool first = true;
|
201
|
+
TypeInfo key_type_info = {key_type};
|
202
|
+
StringBuilder_Printf(b, "{");
|
203
|
+
if (map) {
|
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);
|
208
|
+
if (first) {
|
209
|
+
first = false;
|
210
|
+
} else {
|
211
|
+
StringBuilder_Printf(b, ", ");
|
212
|
+
}
|
213
|
+
StringBuilder_PrintMsgval(b, key, key_type_info);
|
214
|
+
StringBuilder_Printf(b, "=>");
|
215
|
+
StringBuilder_PrintMsgval(b, val, val_type);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
StringBuilder_Printf(b, "}");
|
176
219
|
}
|
177
220
|
|
178
|
-
|
179
|
-
Map* self = ruby_to_Map(
|
180
|
-
|
181
|
-
|
221
|
+
static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
|
222
|
+
Map* self = ruby_to_Map(_self);
|
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);
|
229
|
+
return ST_CONTINUE;
|
182
230
|
}
|
183
231
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
232
|
+
// Used only internally -- shared by #merge and #initialize.
|
233
|
+
static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
234
|
+
if (TYPE(hashmap) == T_HASH) {
|
235
|
+
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
236
|
+
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
237
|
+
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
238
|
+
Map* self = ruby_to_Map(_self);
|
239
|
+
Map* other = ruby_to_Map(hashmap);
|
240
|
+
upb_Arena* arena = Arena_get(self->arena);
|
241
|
+
upb_Message* self_msg = Map_GetMutable(_self);
|
242
|
+
size_t iter = kUpb_Map_Begin;
|
243
|
+
|
244
|
+
Arena_fuse(other->arena, arena);
|
245
|
+
|
246
|
+
if (self->key_type != other->key_type ||
|
247
|
+
self->value_type_info.type != other->value_type_info.type ||
|
248
|
+
self->value_type_class != other->value_type_class) {
|
249
|
+
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
250
|
+
}
|
251
|
+
|
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);
|
256
|
+
}
|
257
|
+
} else {
|
258
|
+
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
191
259
|
}
|
260
|
+
return _self;
|
192
261
|
}
|
193
262
|
|
194
263
|
/*
|
@@ -221,9 +290,9 @@ static bool needs_typeclass(upb_fieldtype_t type) {
|
|
221
290
|
* references to underlying objects will be shared if the value type is a
|
222
291
|
* message type.
|
223
292
|
*/
|
224
|
-
VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
293
|
+
static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
225
294
|
Map* self = ruby_to_Map(_self);
|
226
|
-
|
295
|
+
VALUE init_arg;
|
227
296
|
|
228
297
|
// We take either two args (:key_type, :value_type), three args (:key_type,
|
229
298
|
// :value_type, "ValueMessageType"), or four args (the above plus an initial
|
@@ -233,39 +302,31 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
233
302
|
}
|
234
303
|
|
235
304
|
self->key_type = ruby_to_fieldtype(argv[0]);
|
236
|
-
self->
|
237
|
-
|
305
|
+
self->value_type_info =
|
306
|
+
TypeInfo_FromClass(argc, argv, 1, &self->value_type_class, &init_arg);
|
307
|
+
self->arena = Arena_new();
|
238
308
|
|
239
309
|
// Check that the key type is an allowed type.
|
240
310
|
switch (self->key_type) {
|
241
|
-
case
|
242
|
-
case
|
243
|
-
case
|
244
|
-
case
|
245
|
-
case
|
246
|
-
case
|
247
|
-
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:
|
248
318
|
// These are OK.
|
249
319
|
break;
|
250
320
|
default:
|
251
321
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
252
322
|
}
|
253
323
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
validate_type_class(self->value_type, self->value_type_class);
|
258
|
-
init_value_arg = 3;
|
259
|
-
}
|
260
|
-
|
261
|
-
// Table value type is always UINT64: this ensures enough space to store the
|
262
|
-
// native_slot value.
|
263
|
-
if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
|
264
|
-
rb_raise(rb_eRuntimeError, "Could not allocate table.");
|
265
|
-
}
|
324
|
+
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
325
|
+
self->value_type_info.type);
|
326
|
+
ObjectCache_Add(self->map, _self);
|
266
327
|
|
267
|
-
if (
|
268
|
-
Map_merge_into_self(_self,
|
328
|
+
if (init_arg != Qnil) {
|
329
|
+
Map_merge_into_self(_self, init_arg);
|
269
330
|
}
|
270
331
|
|
271
332
|
return Qnil;
|
@@ -279,24 +340,16 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
279
340
|
* Note that Map also includes Enumerable; map thus acts like a normal Ruby
|
280
341
|
* sequence.
|
281
342
|
*/
|
282
|
-
VALUE Map_each(VALUE _self) {
|
343
|
+
static VALUE Map_each(VALUE _self) {
|
283
344
|
Map* self = ruby_to_Map(_self);
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
VALUE
|
291
|
-
|
292
|
-
|
293
|
-
upb_value v = upb_strtable_iter_value(&it);
|
294
|
-
void* mem = value_memory(&v);
|
295
|
-
VALUE value = native_slot_get(self->value_type,
|
296
|
-
self->value_type_class,
|
297
|
-
mem);
|
298
|
-
|
299
|
-
rb_yield_values(2, key, value);
|
345
|
+
size_t iter = kUpb_Map_Begin;
|
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);
|
350
|
+
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
351
|
+
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
352
|
+
rb_yield_values(2, key_val, val_val);
|
300
353
|
}
|
301
354
|
|
302
355
|
return Qnil;
|
@@ -308,19 +361,15 @@ VALUE Map_each(VALUE _self) {
|
|
308
361
|
*
|
309
362
|
* Returns the list of keys contained in the map, in unspecified order.
|
310
363
|
*/
|
311
|
-
VALUE Map_keys(VALUE _self) {
|
364
|
+
static VALUE Map_keys(VALUE _self) {
|
312
365
|
Map* self = ruby_to_Map(_self);
|
313
|
-
|
366
|
+
size_t iter = kUpb_Map_Begin;
|
314
367
|
VALUE ret = rb_ary_new();
|
315
|
-
upb_strtable_iter it;
|
316
|
-
for (upb_strtable_begin(&it, &self->table);
|
317
|
-
!upb_strtable_done(&it);
|
318
|
-
upb_strtable_next(&it)) {
|
319
|
-
|
320
|
-
VALUE key = table_key_to_ruby(
|
321
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
322
368
|
|
323
|
-
|
369
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
370
|
+
upb_MessageValue key = upb_MapIterator_Key(self->map, iter);
|
371
|
+
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
372
|
+
rb_ary_push(ret, key_val);
|
324
373
|
}
|
325
374
|
|
326
375
|
return ret;
|
@@ -332,22 +381,15 @@ VALUE Map_keys(VALUE _self) {
|
|
332
381
|
*
|
333
382
|
* Returns the list of values contained in the map, in unspecified order.
|
334
383
|
*/
|
335
|
-
VALUE Map_values(VALUE _self) {
|
384
|
+
static VALUE Map_values(VALUE _self) {
|
336
385
|
Map* self = ruby_to_Map(_self);
|
337
|
-
|
386
|
+
size_t iter = kUpb_Map_Begin;
|
338
387
|
VALUE ret = rb_ary_new();
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
upb_value v = upb_strtable_iter_value(&it);
|
345
|
-
void* mem = value_memory(&v);
|
346
|
-
VALUE value = native_slot_get(self->value_type,
|
347
|
-
self->value_type_class,
|
348
|
-
mem);
|
349
|
-
|
350
|
-
rb_ary_push(ret, value);
|
388
|
+
|
389
|
+
while (upb_MapIterator_Next(self->map, &iter)) {
|
390
|
+
upb_MessageValue val = upb_MapIterator_Value(self->map, iter);
|
391
|
+
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
392
|
+
rb_ary_push(ret, val_val);
|
351
393
|
}
|
352
394
|
|
353
395
|
return ret;
|
@@ -360,18 +402,14 @@ VALUE Map_values(VALUE _self) {
|
|
360
402
|
* Accesses the element at the given key. Throws an exception if the key type is
|
361
403
|
* incorrect. Returns nil when the key is not present in the map.
|
362
404
|
*/
|
363
|
-
VALUE Map_index(VALUE _self, VALUE key) {
|
405
|
+
static VALUE Map_index(VALUE _self, VALUE key) {
|
364
406
|
Map* self = ruby_to_Map(_self);
|
407
|
+
upb_MessageValue key_upb =
|
408
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
409
|
+
upb_MessageValue val;
|
365
410
|
|
366
|
-
|
367
|
-
|
368
|
-
size_t length = 0;
|
369
|
-
upb_value v;
|
370
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
371
|
-
|
372
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
|
373
|
-
void* mem = value_memory(&v);
|
374
|
-
return native_slot_get(self->value_type, self->value_type_class, mem);
|
411
|
+
if (upb_Map_Get(self->map, key_upb, &val)) {
|
412
|
+
return Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
375
413
|
} else {
|
376
414
|
return Qnil;
|
377
415
|
}
|
@@ -385,27 +423,17 @@ VALUE Map_index(VALUE _self, VALUE key) {
|
|
385
423
|
* Throws an exception if the key type is incorrect. Returns the new value that
|
386
424
|
* was just inserted.
|
387
425
|
*/
|
388
|
-
VALUE Map_index_set(VALUE _self, VALUE key, VALUE
|
426
|
+
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
389
427
|
Map* self = ruby_to_Map(_self);
|
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);
|
390
433
|
|
391
|
-
|
392
|
-
const char* keyval = NULL;
|
393
|
-
size_t length = 0;
|
394
|
-
upb_value v;
|
395
|
-
void* mem;
|
396
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
397
|
-
|
398
|
-
mem = value_memory(&v);
|
399
|
-
native_slot_set(self->value_type, self->value_type_class, mem, value);
|
400
|
-
|
401
|
-
// Replace any existing value by issuing a 'remove' operation first.
|
402
|
-
upb_strtable_remove2(&self->table, keyval, length, NULL);
|
403
|
-
if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
|
404
|
-
rb_raise(rb_eRuntimeError, "Could not insert into table");
|
405
|
-
}
|
434
|
+
upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
406
435
|
|
407
|
-
|
408
|
-
return value;
|
436
|
+
return val;
|
409
437
|
}
|
410
438
|
|
411
439
|
/*
|
@@ -415,15 +443,12 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
|
|
415
443
|
* Returns true if the given key is present in the map. Throws an exception if
|
416
444
|
* the key has the wrong type.
|
417
445
|
*/
|
418
|
-
VALUE Map_has_key(VALUE _self, VALUE key) {
|
446
|
+
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
419
447
|
Map* self = ruby_to_Map(_self);
|
448
|
+
upb_MessageValue key_upb =
|
449
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
420
450
|
|
421
|
-
|
422
|
-
const char* keyval = NULL;
|
423
|
-
size_t length = 0;
|
424
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
425
|
-
|
426
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
|
451
|
+
if (upb_Map_Get(self->map, key_upb, NULL)) {
|
427
452
|
return Qtrue;
|
428
453
|
} else {
|
429
454
|
return Qfalse;
|
@@ -437,21 +462,26 @@ VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
437
462
|
* Deletes the value at the given key, if any, returning either the old value or
|
438
463
|
* nil if none was present. Throws an exception if the key is of the wrong type.
|
439
464
|
*/
|
440
|
-
VALUE Map_delete(VALUE _self, VALUE key) {
|
465
|
+
static VALUE Map_delete(VALUE _self, VALUE key) {
|
441
466
|
Map* self = ruby_to_Map(_self);
|
467
|
+
upb_MessageValue key_upb =
|
468
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
469
|
+
upb_MessageValue val_upb;
|
470
|
+
VALUE ret;
|
442
471
|
|
443
|
-
|
444
|
-
const char* keyval = NULL;
|
445
|
-
size_t length = 0;
|
446
|
-
upb_value v;
|
447
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
472
|
+
rb_check_frozen(_self);
|
448
473
|
|
449
|
-
|
450
|
-
|
451
|
-
|
474
|
+
// TODO(haberman): make upb_Map_Delete() also capable of returning the deleted
|
475
|
+
// value.
|
476
|
+
if (upb_Map_Get(self->map, key_upb, &val_upb)) {
|
477
|
+
ret = Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
452
478
|
} else {
|
453
|
-
|
479
|
+
ret = Qnil;
|
454
480
|
}
|
481
|
+
|
482
|
+
upb_Map_Delete(Map_GetMutable(_self), key_upb);
|
483
|
+
|
484
|
+
return ret;
|
455
485
|
}
|
456
486
|
|
457
487
|
/*
|
@@ -460,15 +490,8 @@ VALUE Map_delete(VALUE _self, VALUE key) {
|
|
460
490
|
*
|
461
491
|
* Removes all entries from the map.
|
462
492
|
*/
|
463
|
-
VALUE Map_clear(VALUE _self) {
|
464
|
-
|
465
|
-
|
466
|
-
// Uninit and reinit the table -- this is faster than iterating and doing a
|
467
|
-
// delete-lookup on each key.
|
468
|
-
upb_strtable_uninit(&self->table);
|
469
|
-
if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
|
470
|
-
rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
|
471
|
-
}
|
493
|
+
static VALUE Map_clear(VALUE _self) {
|
494
|
+
upb_Map_Clear(Map_GetMutable(_self));
|
472
495
|
return Qnil;
|
473
496
|
}
|
474
497
|
|
@@ -478,24 +501,9 @@ VALUE Map_clear(VALUE _self) {
|
|
478
501
|
*
|
479
502
|
* Returns the number of entries (key-value pairs) in the map.
|
480
503
|
*/
|
481
|
-
VALUE Map_length(VALUE _self) {
|
482
|
-
Map* self = ruby_to_Map(_self);
|
483
|
-
return ULL2NUM(upb_strtable_count(&self->table));
|
484
|
-
}
|
485
|
-
|
486
|
-
static VALUE Map_new_this_type(VALUE _self) {
|
504
|
+
static VALUE Map_length(VALUE _self) {
|
487
505
|
Map* self = ruby_to_Map(_self);
|
488
|
-
|
489
|
-
VALUE key_type = fieldtype_to_ruby(self->key_type);
|
490
|
-
VALUE value_type = fieldtype_to_ruby(self->value_type);
|
491
|
-
if (self->value_type_class != Qnil) {
|
492
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
|
493
|
-
key_type, value_type, self->value_type_class);
|
494
|
-
} else {
|
495
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
|
496
|
-
key_type, value_type);
|
497
|
-
}
|
498
|
-
return new_map;
|
506
|
+
return ULL2NUM(upb_Map_Size(self->map));
|
499
507
|
}
|
500
508
|
|
501
509
|
/*
|
@@ -505,59 +513,23 @@ static VALUE Map_new_this_type(VALUE _self) {
|
|
505
513
|
* Duplicates this map with a shallow copy. References to all non-primitive
|
506
514
|
* element objects (e.g., submessages) are shared.
|
507
515
|
*/
|
508
|
-
VALUE Map_dup(VALUE _self) {
|
509
|
-
Map* self = ruby_to_Map(_self);
|
510
|
-
VALUE new_map = Map_new_this_type(_self);
|
511
|
-
Map* new_self = ruby_to_Map(new_map);
|
512
|
-
|
513
|
-
upb_strtable_iter it;
|
514
|
-
for (upb_strtable_begin(&it, &self->table);
|
515
|
-
!upb_strtable_done(&it);
|
516
|
-
upb_strtable_next(&it)) {
|
517
|
-
|
518
|
-
upb_value v = upb_strtable_iter_value(&it);
|
519
|
-
void* mem = value_memory(&v);
|
520
|
-
upb_value dup;
|
521
|
-
void* dup_mem = value_memory(&dup);
|
522
|
-
native_slot_dup(self->value_type, dup_mem, mem);
|
523
|
-
|
524
|
-
if (!upb_strtable_insert2(&new_self->table,
|
525
|
-
upb_strtable_iter_key(&it),
|
526
|
-
upb_strtable_iter_keylength(&it),
|
527
|
-
dup)) {
|
528
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
529
|
-
}
|
530
|
-
}
|
531
|
-
|
532
|
-
return new_map;
|
533
|
-
}
|
534
|
-
|
535
|
-
// Used by Google::Protobuf.deep_copy but not exposed directly.
|
536
|
-
VALUE Map_deep_copy(VALUE _self) {
|
516
|
+
static VALUE Map_dup(VALUE _self) {
|
537
517
|
Map* self = ruby_to_Map(_self);
|
538
|
-
VALUE
|
539
|
-
Map* new_self = ruby_to_Map(
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
native_slot_deep_copy(self->value_type, dup_mem, mem);
|
551
|
-
|
552
|
-
if (!upb_strtable_insert2(&new_self->table,
|
553
|
-
upb_strtable_iter_key(&it),
|
554
|
-
upb_strtable_iter_keylength(&it),
|
555
|
-
dup)) {
|
556
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
557
|
-
}
|
518
|
+
VALUE new_map_rb = Map_new_this_type(self);
|
519
|
+
Map* new_self = ruby_to_Map(new_map_rb);
|
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);
|
523
|
+
|
524
|
+
Arena_fuse(self->arena, arena);
|
525
|
+
|
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);
|
558
530
|
}
|
559
531
|
|
560
|
-
return
|
532
|
+
return new_map_rb;
|
561
533
|
}
|
562
534
|
|
563
535
|
/*
|
@@ -576,12 +548,11 @@ VALUE Map_deep_copy(VALUE _self) {
|
|
576
548
|
VALUE Map_eq(VALUE _self, VALUE _other) {
|
577
549
|
Map* self = ruby_to_Map(_self);
|
578
550
|
Map* other;
|
579
|
-
upb_strtable_iter it;
|
580
551
|
|
581
552
|
// Allow comparisons to Ruby hashmaps by converting to a temporary Map
|
582
553
|
// instance. Slow, but workable.
|
583
554
|
if (TYPE(_other) == T_HASH) {
|
584
|
-
VALUE other_map = Map_new_this_type(
|
555
|
+
VALUE other_map = Map_new_this_type(self);
|
585
556
|
Map_merge_into_self(other_map, _other);
|
586
557
|
_other = other_map;
|
587
558
|
}
|
@@ -592,35 +563,27 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
592
563
|
return Qtrue;
|
593
564
|
}
|
594
565
|
if (self->key_type != other->key_type ||
|
595
|
-
self->
|
566
|
+
self->value_type_info.type != other->value_type_info.type ||
|
596
567
|
self->value_type_class != other->value_type_class) {
|
597
568
|
return Qfalse;
|
598
569
|
}
|
599
|
-
if (
|
570
|
+
if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
|
600
571
|
return Qfalse;
|
601
572
|
}
|
602
573
|
|
603
574
|
// For each member of self, check that an equal member exists at the same key
|
604
575
|
// in other.
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
upb_value other_v;
|
612
|
-
void* other_mem = value_memory(&other_v);
|
613
|
-
|
614
|
-
if (!upb_strtable_lookup2(&other->table,
|
615
|
-
upb_strtable_iter_key(&it),
|
616
|
-
upb_strtable_iter_keylength(&it),
|
617
|
-
&other_v)) {
|
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)) {
|
618
582
|
// Not present in other map.
|
619
583
|
return Qfalse;
|
620
584
|
}
|
621
|
-
|
622
|
-
|
623
|
-
// Present, but value not equal.
|
585
|
+
if (!Msgval_IsEqual(val, other_val, self->value_type_info)) {
|
586
|
+
// Present but different value.
|
624
587
|
return Qfalse;
|
625
588
|
}
|
626
589
|
}
|
@@ -628,6 +591,22 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
628
591
|
return Qtrue;
|
629
592
|
}
|
630
593
|
|
594
|
+
/*
|
595
|
+
* call-seq:
|
596
|
+
* Message.freeze => self
|
597
|
+
*
|
598
|
+
* Freezes the message object. We have to intercept this so we can pin the
|
599
|
+
* Ruby object into memory so we don't forget it's frozen.
|
600
|
+
*/
|
601
|
+
static VALUE Map_freeze(VALUE _self) {
|
602
|
+
Map* self = ruby_to_Map(_self);
|
603
|
+
if (!RB_OBJ_FROZEN(_self)) {
|
604
|
+
Arena_Pin(self->arena, _self);
|
605
|
+
RB_OBJ_FREEZE(_self);
|
606
|
+
}
|
607
|
+
return _self;
|
608
|
+
}
|
609
|
+
|
631
610
|
/*
|
632
611
|
* call-seq:
|
633
612
|
* Map.hash => hash_value
|
@@ -636,28 +615,18 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
636
615
|
*/
|
637
616
|
VALUE Map_hash(VALUE _self) {
|
638
617
|
Map* self = ruby_to_Map(_self);
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
649
|
-
|
650
|
-
upb_value v = upb_strtable_iter_value(&it);
|
651
|
-
void* mem = value_memory(&v);
|
652
|
-
VALUE value = native_slot_get(self->value_type,
|
653
|
-
self->value_type_class,
|
654
|
-
mem);
|
655
|
-
|
656
|
-
h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
|
657
|
-
h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
|
618
|
+
uint64_t hash = 0;
|
619
|
+
|
620
|
+
size_t iter = kUpb_Map_Begin;
|
621
|
+
TypeInfo key_info = {self->key_type};
|
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);
|
625
|
+
hash = Msgval_GetHash(key, key_info, hash);
|
626
|
+
hash = Msgval_GetHash(val, self->value_type_info, hash);
|
658
627
|
}
|
659
628
|
|
660
|
-
return
|
629
|
+
return LL2NUM(hash);
|
661
630
|
}
|
662
631
|
|
663
632
|
/*
|
@@ -668,25 +637,7 @@ VALUE Map_hash(VALUE _self) {
|
|
668
637
|
*/
|
669
638
|
VALUE Map_to_h(VALUE _self) {
|
670
639
|
Map* self = ruby_to_Map(_self);
|
671
|
-
|
672
|
-
upb_strtable_iter it;
|
673
|
-
for (upb_strtable_begin(&it, &self->table);
|
674
|
-
!upb_strtable_done(&it);
|
675
|
-
upb_strtable_next(&it)) {
|
676
|
-
VALUE key = table_key_to_ruby(
|
677
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
678
|
-
upb_value v = upb_strtable_iter_value(&it);
|
679
|
-
void* mem = value_memory(&v);
|
680
|
-
VALUE value = native_slot_get(self->value_type,
|
681
|
-
self->value_type_class,
|
682
|
-
mem);
|
683
|
-
|
684
|
-
if (self->value_type == UPB_TYPE_MESSAGE) {
|
685
|
-
value = Message_to_h(value);
|
686
|
-
}
|
687
|
-
rb_hash_aset(hash, key, value);
|
688
|
-
}
|
689
|
-
return hash;
|
640
|
+
return Map_CreateHash(self->map, self->key_type, self->value_type_info);
|
690
641
|
}
|
691
642
|
|
692
643
|
/*
|
@@ -700,36 +651,11 @@ VALUE Map_to_h(VALUE _self) {
|
|
700
651
|
VALUE Map_inspect(VALUE _self) {
|
701
652
|
Map* self = ruby_to_Map(_self);
|
702
653
|
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
upb_strtable_iter it;
|
709
|
-
for (upb_strtable_begin(&it, &self->table);
|
710
|
-
!upb_strtable_done(&it);
|
711
|
-
upb_strtable_next(&it)) {
|
712
|
-
VALUE key = table_key_to_ruby(
|
713
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
714
|
-
|
715
|
-
upb_value v = upb_strtable_iter_value(&it);
|
716
|
-
void* mem = value_memory(&v);
|
717
|
-
VALUE value = native_slot_get(self->value_type,
|
718
|
-
self->value_type_class,
|
719
|
-
mem);
|
720
|
-
|
721
|
-
if (!first) {
|
722
|
-
str = rb_str_cat2(str, ", ");
|
723
|
-
} else {
|
724
|
-
first = false;
|
725
|
-
}
|
726
|
-
str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
|
727
|
-
str = rb_str_cat2(str, "=>");
|
728
|
-
str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
|
729
|
-
}
|
730
|
-
|
731
|
-
str = rb_str_cat2(str, "}");
|
732
|
-
return str;
|
654
|
+
StringBuilder* builder = StringBuilder_New();
|
655
|
+
Map_Inspect(builder, self->map, self->key_type, self->value_type_info);
|
656
|
+
VALUE ret = StringBuilder_ToRubyString(builder);
|
657
|
+
StringBuilder_Free(builder);
|
658
|
+
return ret;
|
733
659
|
}
|
734
660
|
|
735
661
|
/*
|
@@ -741,87 +667,11 @@ VALUE Map_inspect(VALUE _self) {
|
|
741
667
|
* in the new copy of this map. Returns the new copy of this map with merged
|
742
668
|
* contents.
|
743
669
|
*/
|
744
|
-
VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
670
|
+
static VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
745
671
|
VALUE dupped = Map_dup(_self);
|
746
672
|
return Map_merge_into_self(dupped, hashmap);
|
747
673
|
}
|
748
674
|
|
749
|
-
static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
|
750
|
-
Map_index_set(self, key, value);
|
751
|
-
return ST_CONTINUE;
|
752
|
-
}
|
753
|
-
|
754
|
-
// Used only internally -- shared by #merge and #initialize.
|
755
|
-
VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
756
|
-
if (TYPE(hashmap) == T_HASH) {
|
757
|
-
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
758
|
-
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
759
|
-
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
760
|
-
|
761
|
-
Map* self = ruby_to_Map(_self);
|
762
|
-
Map* other = ruby_to_Map(hashmap);
|
763
|
-
upb_strtable_iter it;
|
764
|
-
|
765
|
-
if (self->key_type != other->key_type ||
|
766
|
-
self->value_type != other->value_type ||
|
767
|
-
self->value_type_class != other->value_type_class) {
|
768
|
-
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
769
|
-
}
|
770
|
-
|
771
|
-
for (upb_strtable_begin(&it, &other->table);
|
772
|
-
!upb_strtable_done(&it);
|
773
|
-
upb_strtable_next(&it)) {
|
774
|
-
|
775
|
-
// Replace any existing value by issuing a 'remove' operation first.
|
776
|
-
upb_value v;
|
777
|
-
upb_value oldv;
|
778
|
-
upb_strtable_remove2(&self->table,
|
779
|
-
upb_strtable_iter_key(&it),
|
780
|
-
upb_strtable_iter_keylength(&it),
|
781
|
-
&oldv);
|
782
|
-
|
783
|
-
v = upb_strtable_iter_value(&it);
|
784
|
-
upb_strtable_insert2(&self->table,
|
785
|
-
upb_strtable_iter_key(&it),
|
786
|
-
upb_strtable_iter_keylength(&it),
|
787
|
-
v);
|
788
|
-
}
|
789
|
-
} else {
|
790
|
-
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
791
|
-
}
|
792
|
-
return _self;
|
793
|
-
}
|
794
|
-
|
795
|
-
// Internal method: map iterator initialization (used for serialization).
|
796
|
-
void Map_begin(VALUE _self, Map_iter* iter) {
|
797
|
-
Map* self = ruby_to_Map(_self);
|
798
|
-
iter->self = self;
|
799
|
-
upb_strtable_begin(&iter->it, &self->table);
|
800
|
-
}
|
801
|
-
|
802
|
-
void Map_next(Map_iter* iter) {
|
803
|
-
upb_strtable_next(&iter->it);
|
804
|
-
}
|
805
|
-
|
806
|
-
bool Map_done(Map_iter* iter) {
|
807
|
-
return upb_strtable_done(&iter->it);
|
808
|
-
}
|
809
|
-
|
810
|
-
VALUE Map_iter_key(Map_iter* iter) {
|
811
|
-
return table_key_to_ruby(
|
812
|
-
iter->self,
|
813
|
-
upb_strtable_iter_key(&iter->it),
|
814
|
-
upb_strtable_iter_keylength(&iter->it));
|
815
|
-
}
|
816
|
-
|
817
|
-
VALUE Map_iter_value(Map_iter* iter) {
|
818
|
-
upb_value v = upb_strtable_iter_value(&iter->it);
|
819
|
-
void* mem = value_memory(&v);
|
820
|
-
return native_slot_get(iter->self->value_type,
|
821
|
-
iter->self->value_type_class,
|
822
|
-
mem);
|
823
|
-
}
|
824
|
-
|
825
675
|
void Map_register(VALUE module) {
|
826
676
|
VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
|
827
677
|
rb_define_alloc_func(klass, Map_alloc);
|
@@ -838,10 +688,13 @@ void Map_register(VALUE module) {
|
|
838
688
|
rb_define_method(klass, "delete", Map_delete, 1);
|
839
689
|
rb_define_method(klass, "clear", Map_clear, 0);
|
840
690
|
rb_define_method(klass, "length", Map_length, 0);
|
691
|
+
rb_define_method(klass, "size", Map_length, 0);
|
841
692
|
rb_define_method(klass, "dup", Map_dup, 0);
|
693
|
+
// Also define #clone so that we don't inherit Object#clone.
|
694
|
+
rb_define_method(klass, "clone", Map_dup, 0);
|
842
695
|
rb_define_method(klass, "==", Map_eq, 1);
|
696
|
+
rb_define_method(klass, "freeze", Map_freeze, 0);
|
843
697
|
rb_define_method(klass, "hash", Map_hash, 0);
|
844
|
-
rb_define_method(klass, "to_hash", Map_to_h, 0);
|
845
698
|
rb_define_method(klass, "to_h", Map_to_h, 0);
|
846
699
|
rb_define_method(klass, "inspect", Map_inspect, 0);
|
847
700
|
rb_define_method(klass, "merge", Map_merge, 1);
|