jitera-google-protobuf 3.21.12.pre.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/ext/google/protobuf_c/BUILD.bazel +72 -0
  3. data/ext/google/protobuf_c/convert.c +361 -0
  4. data/ext/google/protobuf_c/convert.h +75 -0
  5. data/ext/google/protobuf_c/defs.c +1280 -0
  6. data/ext/google/protobuf_c/defs.h +107 -0
  7. data/ext/google/protobuf_c/extconf.rb +28 -0
  8. data/ext/google/protobuf_c/map.c +687 -0
  9. data/ext/google/protobuf_c/map.h +66 -0
  10. data/ext/google/protobuf_c/message.c +1435 -0
  11. data/ext/google/protobuf_c/message.h +104 -0
  12. data/ext/google/protobuf_c/naive.c +92 -0
  13. data/ext/google/protobuf_c/protobuf.c +480 -0
  14. data/ext/google/protobuf_c/protobuf.h +120 -0
  15. data/ext/google/protobuf_c/range2-neon.c +157 -0
  16. data/ext/google/protobuf_c/range2-sse.c +170 -0
  17. data/ext/google/protobuf_c/repeated_field.c +657 -0
  18. data/ext/google/protobuf_c/repeated_field.h +63 -0
  19. data/ext/google/protobuf_c/ruby-upb.c +13707 -0
  20. data/ext/google/protobuf_c/ruby-upb.h +10582 -0
  21. data/ext/google/protobuf_c/utf8_range.h +21 -0
  22. data/ext/google/protobuf_c/wrap_memcpy.c +52 -0
  23. data/lib/google/protobuf/any_pb.rb +19 -0
  24. data/lib/google/protobuf/api_pb.rb +42 -0
  25. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  26. data/lib/google/protobuf/descriptor_pb.rb +279 -0
  27. data/lib/google/protobuf/duration_pb.rb +19 -0
  28. data/lib/google/protobuf/empty_pb.rb +17 -0
  29. data/lib/google/protobuf/field_mask_pb.rb +18 -0
  30. data/lib/google/protobuf/message_exts.rb +58 -0
  31. data/lib/google/protobuf/repeated_field.rb +201 -0
  32. data/lib/google/protobuf/source_context_pb.rb +18 -0
  33. data/lib/google/protobuf/struct_pb.rb +37 -0
  34. data/lib/google/protobuf/timestamp_pb.rb +19 -0
  35. data/lib/google/protobuf/type_pb.rb +92 -0
  36. data/lib/google/protobuf/well_known_types.rb +240 -0
  37. data/lib/google/protobuf/wrappers_pb.rb +50 -0
  38. data/lib/google/protobuf.rb +79 -0
  39. metadata +129 -0
@@ -0,0 +1,687 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are
7
+ // met:
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.
30
+
31
+ #include "convert.h"
32
+ #include "defs.h"
33
+ #include "message.h"
34
+ #include "protobuf.h"
35
+
36
+ // -----------------------------------------------------------------------------
37
+ // Basic map operations on top of upb_Map.
38
+ //
39
+ // Note that we roll our own `Map` container here because, as for
40
+ // `RepeatedField`, we want a strongly-typed container. This is so that any user
41
+ // errors due to incorrect map key or value types are raised as close as
42
+ // possible to the error site, rather than at some deferred point (e.g.,
43
+ // serialization).
44
+ // -----------------------------------------------------------------------------
45
+
46
+ // -----------------------------------------------------------------------------
47
+ // Map container type.
48
+ // -----------------------------------------------------------------------------
49
+
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;
57
+
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;
71
+
72
+ static Map* ruby_to_Map(VALUE _self) {
73
+ Map* self;
74
+ TypedData_Get_Struct(_self, Map, &Map_type, self);
75
+ return self;
76
+ }
77
+
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);
105
+ }
106
+ }
107
+
108
+ return val;
109
+ }
110
+
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;
119
+ }
120
+
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
+ }
127
+
128
+ static upb_Map* Map_GetMutable(VALUE _self) {
129
+ rb_check_frozen(_self);
130
+ return (upb_Map*)ruby_to_Map(_self)->map;
131
+ }
132
+
133
+ VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
134
+ TypeInfo val_info) {
135
+ VALUE hash = rb_hash_new();
136
+ TypeInfo key_info = TypeInfo_from_type(key_type);
137
+
138
+ if (!map) return hash;
139
+
140
+ size_t iter = kUpb_Map_Begin;
141
+ upb_MessageValue key, val;
142
+ while (upb_Map_Next(map, &key, &val, &iter)) {
143
+ VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
144
+ VALUE val_val = Scalar_CreateHash(val, val_info);
145
+ rb_hash_aset(hash, key_val, val_val);
146
+ }
147
+
148
+ return hash;
149
+ }
150
+
151
+ VALUE Map_deep_copy(VALUE obj) {
152
+ Map* self = ruby_to_Map(obj);
153
+ VALUE new_arena_rb = Arena_new();
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);
163
+ }
164
+
165
+ return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
166
+ new_arena_rb);
167
+ }
168
+
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);
173
+ TypeInfo value_type_info = TypeInfo_get(value_field);
174
+ Map* self;
175
+
176
+ if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
177
+ RTYPEDDATA_TYPE(val) != &Map_type) {
178
+ rb_raise(cTypeError, "Expected Map instance");
179
+ }
180
+
181
+ self = ruby_to_Map(val);
182
+ if (self->key_type != upb_FieldDef_CType(key_field)) {
183
+ rb_raise(cTypeError, "Map key type does not match field's key type");
184
+ }
185
+ if (self->value_type_info.type != value_type_info.type) {
186
+ rb_raise(cTypeError, "Map value type does not match field's value type");
187
+ }
188
+ if (self->value_type_info.def.msgdef != value_type_info.def.msgdef) {
189
+ rb_raise(cTypeError, "Map value type has wrong message/enum class");
190
+ }
191
+
192
+ Arena_fuse(self->arena, arena);
193
+ return self->map;
194
+ }
195
+
196
+ void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
197
+ TypeInfo val_type) {
198
+ bool first = true;
199
+ TypeInfo key_type_info = {key_type};
200
+ StringBuilder_Printf(b, "{");
201
+ if (map) {
202
+ size_t iter = kUpb_Map_Begin;
203
+ upb_MessageValue key, val;
204
+ while (upb_Map_Next(map, &key, &val, &iter)) {
205
+ if (first) {
206
+ first = false;
207
+ } else {
208
+ StringBuilder_Printf(b, ", ");
209
+ }
210
+ StringBuilder_PrintMsgval(b, key, key_type_info);
211
+ StringBuilder_Printf(b, "=>");
212
+ StringBuilder_PrintMsgval(b, val, val_type);
213
+ }
214
+ }
215
+ StringBuilder_Printf(b, "}");
216
+ }
217
+
218
+ static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
219
+ Map* self = ruby_to_Map(_self);
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
+ return ST_CONTINUE;
227
+ }
228
+
229
+ // Used only internally -- shared by #merge and #initialize.
230
+ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
231
+ if (TYPE(hashmap) == T_HASH) {
232
+ rb_hash_foreach(hashmap, merge_into_self_callback, _self);
233
+ } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
234
+ RTYPEDDATA_TYPE(hashmap) == &Map_type) {
235
+ Map* self = ruby_to_Map(_self);
236
+ Map* other = ruby_to_Map(hashmap);
237
+ upb_Arena* arena = Arena_get(self->arena);
238
+ upb_Message* self_msg = Map_GetMutable(_self);
239
+
240
+ Arena_fuse(other->arena, arena);
241
+
242
+ if (self->key_type != other->key_type ||
243
+ self->value_type_info.type != other->value_type_info.type ||
244
+ self->value_type_class != other->value_type_class) {
245
+ rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
246
+ }
247
+
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);
252
+ }
253
+ } else {
254
+ rb_raise(rb_eArgError, "Unknown type merging into Map");
255
+ }
256
+ return _self;
257
+ }
258
+
259
+ /*
260
+ * call-seq:
261
+ * Map.new(key_type, value_type, value_typeclass = nil, init_hashmap = {})
262
+ * => new map
263
+ *
264
+ * Allocates a new Map container. This constructor may be called with 2, 3, or 4
265
+ * arguments. The first two arguments are always present and are symbols (taking
266
+ * on the same values as field-type symbols in message descriptors) that
267
+ * indicate the type of the map key and value fields.
268
+ *
269
+ * The supported key types are: :int32, :int64, :uint32, :uint64, :bool,
270
+ * :string, :bytes.
271
+ *
272
+ * The supported value types are: :int32, :int64, :uint32, :uint64, :bool,
273
+ * :string, :bytes, :enum, :message.
274
+ *
275
+ * The third argument, value_typeclass, must be present if value_type is :enum
276
+ * or :message. As in RepeatedField#new, this argument must be a message class
277
+ * (for :message) or enum module (for :enum).
278
+ *
279
+ * The last argument, if present, provides initial content for map. Note that
280
+ * this may be an ordinary Ruby hashmap or another Map instance with identical
281
+ * key and value types. Also note that this argument may be present whether or
282
+ * not value_typeclass is present (and it is unambiguously separate from
283
+ * value_typeclass because value_typeclass's presence is strictly determined by
284
+ * value_type). The contents of this initial hashmap or Map instance are
285
+ * shallow-copied into the new Map: the original map is unmodified, but
286
+ * references to underlying objects will be shared if the value type is a
287
+ * message type.
288
+ */
289
+ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
290
+ Map* self = ruby_to_Map(_self);
291
+ VALUE init_arg;
292
+
293
+ // We take either two args (:key_type, :value_type), three args (:key_type,
294
+ // :value_type, "ValueMessageType"), or four args (the above plus an initial
295
+ // hashmap).
296
+ if (argc < 2 || argc > 4) {
297
+ rb_raise(rb_eArgError, "Map constructor expects 2, 3 or 4 arguments.");
298
+ }
299
+
300
+ self->key_type = ruby_to_fieldtype(argv[0]);
301
+ self->value_type_info =
302
+ TypeInfo_FromClass(argc, argv, 1, &self->value_type_class, &init_arg);
303
+ self->arena = Arena_new();
304
+
305
+ // Check that the key type is an allowed type.
306
+ switch (self->key_type) {
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:
314
+ // These are OK.
315
+ break;
316
+ default:
317
+ rb_raise(rb_eArgError, "Invalid key type for map.");
318
+ }
319
+
320
+ self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
321
+ self->value_type_info.type);
322
+ ObjectCache_Add(self->map, _self);
323
+
324
+ if (init_arg != Qnil) {
325
+ Map_merge_into_self(_self, init_arg);
326
+ }
327
+
328
+ return Qnil;
329
+ }
330
+
331
+ /*
332
+ * call-seq:
333
+ * Map.each(&block)
334
+ *
335
+ * Invokes &block on each |key, value| pair in the map, in unspecified order.
336
+ * Note that Map also includes Enumerable; map thus acts like a normal Ruby
337
+ * sequence.
338
+ */
339
+ static VALUE Map_each(VALUE _self) {
340
+ Map* self = ruby_to_Map(_self);
341
+ size_t iter = kUpb_Map_Begin;
342
+ upb_MessageValue key, val;
343
+
344
+ while (upb_Map_Next(self->map, &key, &val, &iter)) {
345
+ VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
346
+ VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
347
+ rb_yield_values(2, key_val, val_val);
348
+ }
349
+
350
+ return Qnil;
351
+ }
352
+
353
+ /*
354
+ * call-seq:
355
+ * Map.keys => [list_of_keys]
356
+ *
357
+ * Returns the list of keys contained in the map, in unspecified order.
358
+ */
359
+ static VALUE Map_keys(VALUE _self) {
360
+ Map* self = ruby_to_Map(_self);
361
+ size_t iter = kUpb_Map_Begin;
362
+ VALUE ret = rb_ary_new();
363
+ upb_MessageValue key, val;
364
+
365
+ while (upb_Map_Next(self->map, &key, &val, &iter)) {
366
+ VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
367
+ rb_ary_push(ret, key_val);
368
+ }
369
+
370
+ return ret;
371
+ }
372
+
373
+ /*
374
+ * call-seq:
375
+ * Map.values => [list_of_values]
376
+ *
377
+ * Returns the list of values contained in the map, in unspecified order.
378
+ */
379
+ static VALUE Map_values(VALUE _self) {
380
+ Map* self = ruby_to_Map(_self);
381
+ size_t iter = kUpb_Map_Begin;
382
+ VALUE ret = rb_ary_new();
383
+ upb_MessageValue key, val;
384
+
385
+ while (upb_Map_Next(self->map, &key, &val, &iter)) {
386
+ VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
387
+ rb_ary_push(ret, val_val);
388
+ }
389
+
390
+ return ret;
391
+ }
392
+
393
+ /*
394
+ * call-seq:
395
+ * Map.[](key) => value
396
+ *
397
+ * Accesses the element at the given key. Throws an exception if the key type is
398
+ * incorrect. Returns nil when the key is not present in the map.
399
+ */
400
+ static VALUE Map_index(VALUE _self, VALUE key) {
401
+ Map* self = ruby_to_Map(_self);
402
+ upb_MessageValue key_upb =
403
+ Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
404
+ upb_MessageValue val;
405
+
406
+ if (upb_Map_Get(self->map, key_upb, &val)) {
407
+ return Convert_UpbToRuby(val, self->value_type_info, self->arena);
408
+ } else {
409
+ return Qnil;
410
+ }
411
+ }
412
+
413
+ /*
414
+ * call-seq:
415
+ * Map.[]=(key, value) => value
416
+ *
417
+ * Inserts or overwrites the value at the given key with the given new value.
418
+ * Throws an exception if the key type is incorrect. Returns the new value that
419
+ * was just inserted.
420
+ */
421
+ static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
422
+ Map* self = ruby_to_Map(_self);
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);
428
+
429
+ upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
430
+
431
+ return val;
432
+ }
433
+
434
+ /*
435
+ * call-seq:
436
+ * Map.has_key?(key) => bool
437
+ *
438
+ * Returns true if the given key is present in the map. Throws an exception if
439
+ * the key has the wrong type.
440
+ */
441
+ static VALUE Map_has_key(VALUE _self, VALUE key) {
442
+ Map* self = ruby_to_Map(_self);
443
+ upb_MessageValue key_upb =
444
+ Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
445
+
446
+ if (upb_Map_Get(self->map, key_upb, NULL)) {
447
+ return Qtrue;
448
+ } else {
449
+ return Qfalse;
450
+ }
451
+ }
452
+
453
+ /*
454
+ * call-seq:
455
+ * Map.delete(key) => old_value
456
+ *
457
+ * Deletes the value at the given key, if any, returning either the old value or
458
+ * nil if none was present. Throws an exception if the key is of the wrong type.
459
+ */
460
+ static VALUE Map_delete(VALUE _self, VALUE key) {
461
+ Map* self = ruby_to_Map(_self);
462
+ rb_check_frozen(_self);
463
+
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
+ } else {
471
+ return Qnil;
472
+ }
473
+ }
474
+
475
+ /*
476
+ * call-seq:
477
+ * Map.clear
478
+ *
479
+ * Removes all entries from the map.
480
+ */
481
+ static VALUE Map_clear(VALUE _self) {
482
+ upb_Map_Clear(Map_GetMutable(_self));
483
+ return Qnil;
484
+ }
485
+
486
+ /*
487
+ * call-seq:
488
+ * Map.length
489
+ *
490
+ * Returns the number of entries (key-value pairs) in the map.
491
+ */
492
+ static VALUE Map_length(VALUE _self) {
493
+ Map* self = ruby_to_Map(_self);
494
+ return ULL2NUM(upb_Map_Size(self->map));
495
+ }
496
+
497
+ /*
498
+ * call-seq:
499
+ * Map.dup => new_map
500
+ *
501
+ * Duplicates this map with a shallow copy. References to all non-primitive
502
+ * element objects (e.g., submessages) are shared.
503
+ */
504
+ static VALUE Map_dup(VALUE _self) {
505
+ Map* self = ruby_to_Map(_self);
506
+ VALUE new_map_rb = Map_new_this_type(self);
507
+ Map* new_self = ruby_to_Map(new_map_rb);
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);
511
+
512
+ Arena_fuse(self->arena, arena);
513
+
514
+ upb_MessageValue key, val;
515
+ while (upb_Map_Next(self->map, &key, &val, &iter)) {
516
+ upb_Map_Set(new_map, key, val, arena);
517
+ }
518
+
519
+ return new_map_rb;
520
+ }
521
+
522
+ /*
523
+ * call-seq:
524
+ * Map.==(other) => boolean
525
+ *
526
+ * Compares this map to another. Maps are equal if they have identical key sets,
527
+ * and for each key, the values in both maps compare equal. Elements are
528
+ * compared as per normal Ruby semantics, by calling their :== methods (or
529
+ * performing a more efficient comparison for primitive types).
530
+ *
531
+ * Maps with dissimilar key types or value types/typeclasses are never equal,
532
+ * even if value comparison (for example, between integers and floats) would
533
+ * have otherwise indicated that every element has equal value.
534
+ */
535
+ VALUE Map_eq(VALUE _self, VALUE _other) {
536
+ Map* self = ruby_to_Map(_self);
537
+ Map* other;
538
+
539
+ // Allow comparisons to Ruby hashmaps by converting to a temporary Map
540
+ // instance. Slow, but workable.
541
+ if (TYPE(_other) == T_HASH) {
542
+ VALUE other_map = Map_new_this_type(self);
543
+ Map_merge_into_self(other_map, _other);
544
+ _other = other_map;
545
+ }
546
+
547
+ other = ruby_to_Map(_other);
548
+
549
+ if (self == other) {
550
+ return Qtrue;
551
+ }
552
+ if (self->key_type != other->key_type ||
553
+ self->value_type_info.type != other->value_type_info.type ||
554
+ self->value_type_class != other->value_type_class) {
555
+ return Qfalse;
556
+ }
557
+ if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
558
+ return Qfalse;
559
+ }
560
+
561
+ // For each member of self, check that an equal member exists at the same key
562
+ // in other.
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)) {
568
+ // Not present in other map.
569
+ return Qfalse;
570
+ }
571
+ if (!Msgval_IsEqual(val, other_val, self->value_type_info)) {
572
+ // Present but different value.
573
+ return Qfalse;
574
+ }
575
+ }
576
+
577
+ return Qtrue;
578
+ }
579
+
580
+ /*
581
+ * call-seq:
582
+ * Message.freeze => self
583
+ *
584
+ * Freezes the message object. We have to intercept this so we can pin the
585
+ * Ruby object into memory so we don't forget it's frozen.
586
+ */
587
+ static VALUE Map_freeze(VALUE _self) {
588
+ Map* self = ruby_to_Map(_self);
589
+ if (!RB_OBJ_FROZEN(_self)) {
590
+ Arena_Pin(self->arena, _self);
591
+ RB_OBJ_FREEZE(_self);
592
+ }
593
+ return _self;
594
+ }
595
+
596
+ /*
597
+ * call-seq:
598
+ * Map.hash => hash_value
599
+ *
600
+ * Returns a hash value based on this map's contents.
601
+ */
602
+ VALUE Map_hash(VALUE _self) {
603
+ Map* self = ruby_to_Map(_self);
604
+ uint64_t hash = 0;
605
+
606
+ size_t iter = kUpb_Map_Begin;
607
+ TypeInfo key_info = {self->key_type};
608
+ upb_MessageValue key, val;
609
+ while (upb_Map_Next(self->map, &key, &val, &iter)) {
610
+ hash = Msgval_GetHash(key, key_info, hash);
611
+ hash = Msgval_GetHash(val, self->value_type_info, hash);
612
+ }
613
+
614
+ return LL2NUM(hash);
615
+ }
616
+
617
+ /*
618
+ * call-seq:
619
+ * Map.to_h => {}
620
+ *
621
+ * Returns a Ruby Hash object containing all the values within the map
622
+ */
623
+ VALUE Map_to_h(VALUE _self) {
624
+ Map* self = ruby_to_Map(_self);
625
+ return Map_CreateHash(self->map, self->key_type, self->value_type_info);
626
+ }
627
+
628
+ /*
629
+ * call-seq:
630
+ * Map.inspect => string
631
+ *
632
+ * Returns a string representing this map's elements. It will be formatted as
633
+ * "{key => value, key => value, ...}", with each key and value string
634
+ * representation computed by its own #inspect method.
635
+ */
636
+ VALUE Map_inspect(VALUE _self) {
637
+ Map* self = ruby_to_Map(_self);
638
+
639
+ StringBuilder* builder = StringBuilder_New();
640
+ Map_Inspect(builder, self->map, self->key_type, self->value_type_info);
641
+ VALUE ret = StringBuilder_ToRubyString(builder);
642
+ StringBuilder_Free(builder);
643
+ return ret;
644
+ }
645
+
646
+ /*
647
+ * call-seq:
648
+ * Map.merge(other_map) => map
649
+ *
650
+ * Copies key/value pairs from other_map into a copy of this map. If a key is
651
+ * set in other_map and this map, the value from other_map overwrites the value
652
+ * in the new copy of this map. Returns the new copy of this map with merged
653
+ * contents.
654
+ */
655
+ static VALUE Map_merge(VALUE _self, VALUE hashmap) {
656
+ VALUE dupped = Map_dup(_self);
657
+ return Map_merge_into_self(dupped, hashmap);
658
+ }
659
+
660
+ void Map_register(VALUE module) {
661
+ VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
662
+ rb_define_alloc_func(klass, Map_alloc);
663
+ rb_gc_register_address(&cMap);
664
+ cMap = klass;
665
+
666
+ rb_define_method(klass, "initialize", Map_init, -1);
667
+ rb_define_method(klass, "each", Map_each, 0);
668
+ rb_define_method(klass, "keys", Map_keys, 0);
669
+ rb_define_method(klass, "values", Map_values, 0);
670
+ rb_define_method(klass, "[]", Map_index, 1);
671
+ rb_define_method(klass, "[]=", Map_index_set, 2);
672
+ rb_define_method(klass, "has_key?", Map_has_key, 1);
673
+ rb_define_method(klass, "delete", Map_delete, 1);
674
+ rb_define_method(klass, "clear", Map_clear, 0);
675
+ rb_define_method(klass, "length", Map_length, 0);
676
+ rb_define_method(klass, "size", Map_length, 0);
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);
680
+ rb_define_method(klass, "==", Map_eq, 1);
681
+ rb_define_method(klass, "freeze", Map_freeze, 0);
682
+ rb_define_method(klass, "hash", Map_hash, 0);
683
+ rb_define_method(klass, "to_h", Map_to_h, 0);
684
+ rb_define_method(klass, "inspect", Map_inspect, 0);
685
+ rb_define_method(klass, "merge", Map_merge, 1);
686
+ rb_include_module(klass, rb_mEnumerable);
687
+ }