blob: 8c2f6424c38a9f79fa082d5b53e5d1914f364505 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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 "protobuf.h"
32
33// -----------------------------------------------------------------------------
34// Basic map operations on top of upb's strtable.
35//
36// Note that we roll our own `Map` container here because, as for
37// `RepeatedField`, we want a strongly-typed container. This is so that any user
38// errors due to incorrect map key or value types are raised as close as
39// possible to the error site, rather than at some deferred point (e.g.,
40// 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// -----------------------------------------------------------------------------
47
48// Map values are stored using the native_slot abstraction (as with repeated
49// field values), but keys are a bit special. Since we use a strtable, we need
50// to store keys as sequences of bytes such that equality of those bytes maps
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)
Austin Schuh40c16522018-10-28 20:27:54 -070066static VALUE table_key(Map* self, VALUE key,
67 char* buf,
68 const char** out_key,
69 size_t* out_length) {
Brian Silverman9c614bc2016-02-15 20:20:02 -050070 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);
Austin Schuh40c16522018-10-28 20:27:54 -070075 key = native_slot_encode_and_freeze_string(self->key_type, key);
Brian Silverman9c614bc2016-02-15 20:20:02 -050076 *out_key = RSTRING_PTR(key);
77 *out_length = RSTRING_LEN(key);
78 break;
79
80 case UPB_TYPE_BOOL:
81 case UPB_TYPE_INT32:
82 case UPB_TYPE_INT64:
83 case UPB_TYPE_UINT32:
84 case UPB_TYPE_UINT64:
85 native_slot_set(self->key_type, Qnil, buf, key);
86 *out_key = buf;
87 *out_length = native_slot_size(self->key_type);
88 break;
89
90 default:
91 // Map constructor should not allow a Map with another key type to be
92 // constructed.
93 assert(false);
94 break;
95 }
Austin Schuh40c16522018-10-28 20:27:54 -070096
97 return key;
Brian Silverman9c614bc2016-02-15 20:20:02 -050098}
99
100static VALUE table_key_to_ruby(Map* self, const char* buf, size_t length) {
101 switch (self->key_type) {
102 case UPB_TYPE_BYTES:
103 case UPB_TYPE_STRING: {
104 VALUE ret = rb_str_new(buf, length);
105 rb_enc_associate(ret,
106 (self->key_type == UPB_TYPE_BYTES) ?
107 kRubyString8bitEncoding : kRubyStringUtf8Encoding);
108 return ret;
109 }
110
111 case UPB_TYPE_BOOL:
112 case UPB_TYPE_INT32:
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);
117
118 default:
119 assert(false);
120 return Qnil;
121 }
122}
123
124static void* value_memory(upb_value* v) {
125 return (void*)(&v->val);
126}
127
128// -----------------------------------------------------------------------------
129// Map container type.
130// -----------------------------------------------------------------------------
131
132const rb_data_type_t Map_type = {
133 "Google::Protobuf::Map",
134 { Map_mark, Map_free, NULL },
135};
136
137VALUE cMap;
138
139Map* ruby_to_Map(VALUE _self) {
140 Map* self;
141 TypedData_Get_Struct(_self, Map, &Map_type, self);
142 return self;
143}
144
145void Map_mark(void* _self) {
146 Map* self = _self;
147
148 rb_gc_mark(self->value_type_class);
Austin Schuh40c16522018-10-28 20:27:54 -0700149 rb_gc_mark(self->parse_frame);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500150
151 if (self->value_type == UPB_TYPE_STRING ||
152 self->value_type == UPB_TYPE_BYTES ||
153 self->value_type == UPB_TYPE_MESSAGE) {
154 upb_strtable_iter it;
155 for (upb_strtable_begin(&it, &self->table);
156 !upb_strtable_done(&it);
157 upb_strtable_next(&it)) {
158 upb_value v = upb_strtable_iter_value(&it);
159 void* mem = value_memory(&v);
160 native_slot_mark(self->value_type, mem);
161 }
162 }
163}
164
165void Map_free(void* _self) {
166 Map* self = _self;
167 upb_strtable_uninit(&self->table);
168 xfree(self);
169}
170
171VALUE Map_alloc(VALUE klass) {
172 Map* self = ALLOC(Map);
173 memset(self, 0, sizeof(Map));
174 self->value_type_class = Qnil;
175 return TypedData_Wrap_Struct(klass, &Map_type, self);
176}
177
Austin Schuh40c16522018-10-28 20:27:54 -0700178VALUE Map_set_frame(VALUE map, VALUE val) {
179 Map* self = ruby_to_Map(map);
180 self->parse_frame = val;
181 return val;
182}
183
Brian Silverman9c614bc2016-02-15 20:20:02 -0500184static bool needs_typeclass(upb_fieldtype_t type) {
185 switch (type) {
186 case UPB_TYPE_MESSAGE:
187 case UPB_TYPE_ENUM:
188 return true;
189 default:
190 return false;
191 }
192}
193
194/*
195 * call-seq:
196 * Map.new(key_type, value_type, value_typeclass = nil, init_hashmap = {})
197 * => new map
198 *
199 * Allocates a new Map container. This constructor may be called with 2, 3, or 4
200 * arguments. The first two arguments are always present and are symbols (taking
201 * on the same values as field-type symbols in message descriptors) that
202 * indicate the type of the map key and value fields.
203 *
204 * The supported key types are: :int32, :int64, :uint32, :uint64, :bool,
205 * :string, :bytes.
206 *
207 * The supported value types are: :int32, :int64, :uint32, :uint64, :bool,
208 * :string, :bytes, :enum, :message.
209 *
210 * The third argument, value_typeclass, must be present if value_type is :enum
211 * or :message. As in RepeatedField#new, this argument must be a message class
212 * (for :message) or enum module (for :enum).
213 *
214 * The last argument, if present, provides initial content for map. Note that
215 * this may be an ordinary Ruby hashmap or another Map instance with identical
216 * key and value types. Also note that this argument may be present whether or
217 * not value_typeclass is present (and it is unambiguously separate from
218 * value_typeclass because value_typeclass's presence is strictly determined by
219 * value_type). The contents of this initial hashmap or Map instance are
220 * shallow-copied into the new Map: the original map is unmodified, but
221 * references to underlying objects will be shared if the value type is a
222 * message type.
223 */
224VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
225 Map* self = ruby_to_Map(_self);
226 int init_value_arg;
227
228 // We take either two args (:key_type, :value_type), three args (:key_type,
229 // :value_type, "ValueMessageType"), or four args (the above plus an initial
230 // hashmap).
231 if (argc < 2 || argc > 4) {
232 rb_raise(rb_eArgError, "Map constructor expects 2, 3 or 4 arguments.");
233 }
234
235 self->key_type = ruby_to_fieldtype(argv[0]);
236 self->value_type = ruby_to_fieldtype(argv[1]);
Austin Schuh40c16522018-10-28 20:27:54 -0700237 self->parse_frame = Qnil;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500238
239 // Check that the key type is an allowed type.
240 switch (self->key_type) {
241 case UPB_TYPE_INT32:
242 case UPB_TYPE_INT64:
243 case UPB_TYPE_UINT32:
244 case UPB_TYPE_UINT64:
245 case UPB_TYPE_BOOL:
246 case UPB_TYPE_STRING:
247 case UPB_TYPE_BYTES:
248 // These are OK.
249 break;
250 default:
251 rb_raise(rb_eArgError, "Invalid key type for map.");
252 }
253
254 init_value_arg = 2;
255 if (needs_typeclass(self->value_type) && argc > 2) {
256 self->value_type_class = argv[2];
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 }
266
267 if (argc > init_value_arg) {
268 Map_merge_into_self(_self, argv[init_value_arg]);
269 }
270
271 return Qnil;
272}
273
274/*
275 * call-seq:
276 * Map.each(&block)
277 *
278 * Invokes &block on each |key, value| pair in the map, in unspecified order.
279 * Note that Map also includes Enumerable; map thus acts like a normal Ruby
280 * sequence.
281 */
282VALUE Map_each(VALUE _self) {
283 Map* self = ruby_to_Map(_self);
284
285 upb_strtable_iter it;
286 for (upb_strtable_begin(&it, &self->table);
287 !upb_strtable_done(&it);
288 upb_strtable_next(&it)) {
289
290 VALUE key = table_key_to_ruby(
291 self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
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);
300 }
301
302 return Qnil;
303}
304
305/*
306 * call-seq:
307 * Map.keys => [list_of_keys]
308 *
309 * Returns the list of keys contained in the map, in unspecified order.
310 */
311VALUE Map_keys(VALUE _self) {
312 Map* self = ruby_to_Map(_self);
313
314 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
323 rb_ary_push(ret, key);
324 }
325
326 return ret;
327}
328
329/*
330 * call-seq:
331 * Map.values => [list_of_values]
332 *
333 * Returns the list of values contained in the map, in unspecified order.
334 */
335VALUE Map_values(VALUE _self) {
336 Map* self = ruby_to_Map(_self);
337
338 VALUE ret = rb_ary_new();
339 upb_strtable_iter it;
340 for (upb_strtable_begin(&it, &self->table);
341 !upb_strtable_done(&it);
342 upb_strtable_next(&it)) {
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);
351 }
352
353 return ret;
354}
355
356/*
357 * call-seq:
358 * Map.[](key) => value
359 *
360 * Accesses the element at the given key. Throws an exception if the key type is
361 * incorrect. Returns nil when the key is not present in the map.
362 */
363VALUE Map_index(VALUE _self, VALUE key) {
364 Map* self = ruby_to_Map(_self);
365
366 char keybuf[TABLE_KEY_BUF_LENGTH];
367 const char* keyval = NULL;
368 size_t length = 0;
369 upb_value v;
Austin Schuh40c16522018-10-28 20:27:54 -0700370 key = table_key(self, key, keybuf, &keyval, &length);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500371
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);
375 } else {
376 return Qnil;
377 }
378}
379
380/*
381 * call-seq:
382 * Map.[]=(key, value) => value
383 *
384 * Inserts or overwrites the value at the given key with the given new value.
385 * Throws an exception if the key type is incorrect. Returns the new value that
386 * was just inserted.
387 */
388VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
389 Map* self = ruby_to_Map(_self);
390
391 char keybuf[TABLE_KEY_BUF_LENGTH];
392 const char* keyval = NULL;
393 size_t length = 0;
394 upb_value v;
395 void* mem;
Austin Schuh40c16522018-10-28 20:27:54 -0700396 key = table_key(self, key, keybuf, &keyval, &length);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500397
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 }
406
407 // Ruby hashmap's :[]= method also returns the inserted value.
408 return value;
409}
410
411/*
412 * call-seq:
413 * Map.has_key?(key) => bool
414 *
415 * Returns true if the given key is present in the map. Throws an exception if
416 * the key has the wrong type.
417 */
418VALUE Map_has_key(VALUE _self, VALUE key) {
419 Map* self = ruby_to_Map(_self);
420
421 char keybuf[TABLE_KEY_BUF_LENGTH];
422 const char* keyval = NULL;
423 size_t length = 0;
Austin Schuh40c16522018-10-28 20:27:54 -0700424 key = table_key(self, key, keybuf, &keyval, &length);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500425
426 if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
427 return Qtrue;
428 } else {
429 return Qfalse;
430 }
431}
432
433/*
434 * call-seq:
435 * Map.delete(key) => old_value
436 *
437 * Deletes the value at the given key, if any, returning either the old value or
438 * nil if none was present. Throws an exception if the key is of the wrong type.
439 */
440VALUE Map_delete(VALUE _self, VALUE key) {
441 Map* self = ruby_to_Map(_self);
442
443 char keybuf[TABLE_KEY_BUF_LENGTH];
444 const char* keyval = NULL;
445 size_t length = 0;
446 upb_value v;
Austin Schuh40c16522018-10-28 20:27:54 -0700447 key = table_key(self, key, keybuf, &keyval, &length);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500448
449 if (upb_strtable_remove2(&self->table, keyval, length, &v)) {
450 void* mem = value_memory(&v);
451 return native_slot_get(self->value_type, self->value_type_class, mem);
452 } else {
453 return Qnil;
454 }
455}
456
457/*
458 * call-seq:
459 * Map.clear
460 *
461 * Removes all entries from the map.
462 */
463VALUE Map_clear(VALUE _self) {
464 Map* self = ruby_to_Map(_self);
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 }
472 return Qnil;
473}
474
475/*
476 * call-seq:
477 * Map.length
478 *
479 * Returns the number of entries (key-value pairs) in the map.
480 */
481VALUE Map_length(VALUE _self) {
482 Map* self = ruby_to_Map(_self);
483 return ULL2NUM(upb_strtable_count(&self->table));
484}
485
486static VALUE Map_new_this_type(VALUE _self) {
487 Map* self = ruby_to_Map(_self);
488 VALUE new_map = Qnil;
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;
499}
500
501/*
502 * call-seq:
503 * Map.dup => new_map
504 *
505 * Duplicates this map with a shallow copy. References to all non-primitive
506 * element objects (e.g., submessages) are shared.
507 */
508VALUE 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.
536VALUE Map_deep_copy(VALUE _self) {
537 Map* self = ruby_to_Map(_self);
538 VALUE new_map = Map_new_this_type(_self);
539 Map* new_self = ruby_to_Map(new_map);
540
541 upb_strtable_iter it;
542 for (upb_strtable_begin(&it, &self->table);
543 !upb_strtable_done(&it);
544 upb_strtable_next(&it)) {
545
546 upb_value v = upb_strtable_iter_value(&it);
547 void* mem = value_memory(&v);
548 upb_value dup;
549 void* dup_mem = value_memory(&dup);
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 }
558 }
559
560 return new_map;
561}
562
563/*
564 * call-seq:
565 * Map.==(other) => boolean
566 *
567 * Compares this map to another. Maps are equal if they have identical key sets,
568 * and for each key, the values in both maps compare equal. Elements are
569 * compared as per normal Ruby semantics, by calling their :== methods (or
570 * performing a more efficient comparison for primitive types).
571 *
572 * Maps with dissimilar key types or value types/typeclasses are never equal,
573 * even if value comparison (for example, between integers and floats) would
574 * have otherwise indicated that every element has equal value.
575 */
576VALUE Map_eq(VALUE _self, VALUE _other) {
577 Map* self = ruby_to_Map(_self);
578 Map* other;
579 upb_strtable_iter it;
580
581 // Allow comparisons to Ruby hashmaps by converting to a temporary Map
582 // instance. Slow, but workable.
583 if (TYPE(_other) == T_HASH) {
584 VALUE other_map = Map_new_this_type(_self);
585 Map_merge_into_self(other_map, _other);
586 _other = other_map;
587 }
588
589 other = ruby_to_Map(_other);
590
591 if (self == other) {
592 return Qtrue;
593 }
594 if (self->key_type != other->key_type ||
595 self->value_type != other->value_type ||
596 self->value_type_class != other->value_type_class) {
597 return Qfalse;
598 }
599 if (upb_strtable_count(&self->table) != upb_strtable_count(&other->table)) {
600 return Qfalse;
601 }
602
603 // For each member of self, check that an equal member exists at the same key
604 // in other.
605 for (upb_strtable_begin(&it, &self->table);
606 !upb_strtable_done(&it);
607 upb_strtable_next(&it)) {
608
609 upb_value v = upb_strtable_iter_value(&it);
610 void* mem = value_memory(&v);
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)) {
618 // Not present in other map.
619 return Qfalse;
620 }
621
622 if (!native_slot_eq(self->value_type, mem, other_mem)) {
623 // Present, but value not equal.
624 return Qfalse;
625 }
626 }
627
628 return Qtrue;
629}
630
631/*
632 * call-seq:
633 * Map.hash => hash_value
634 *
635 * Returns a hash value based on this map's contents.
636 */
637VALUE Map_hash(VALUE _self) {
638 Map* self = ruby_to_Map(_self);
639
640 st_index_t h = rb_hash_start(0);
641 VALUE hash_sym = rb_intern("hash");
642
643 upb_strtable_iter it;
644 for (upb_strtable_begin(&it, &self->table);
645 !upb_strtable_done(&it);
646 upb_strtable_next(&it)) {
647 VALUE key = table_key_to_ruby(
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)));
658 }
659
660 return INT2FIX(h);
661}
662
663/*
664 * call-seq:
Austin Schuh40c16522018-10-28 20:27:54 -0700665 * Map.to_h => {}
666 *
667 * Returns a Ruby Hash object containing all the values within the map
668 */
669VALUE Map_to_h(VALUE _self) {
670 Map* self = ruby_to_Map(_self);
671 VALUE hash = rb_hash_new();
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;
690}
691
692/*
693 * call-seq:
Brian Silverman9c614bc2016-02-15 20:20:02 -0500694 * Map.inspect => string
695 *
696 * Returns a string representing this map's elements. It will be formatted as
697 * "{key => value, key => value, ...}", with each key and value string
698 * representation computed by its own #inspect method.
699 */
700VALUE Map_inspect(VALUE _self) {
701 Map* self = ruby_to_Map(_self);
702
703 VALUE str = rb_str_new2("{");
704
705 bool first = true;
706 VALUE inspect_sym = rb_intern("inspect");
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;
733}
734
735/*
736 * call-seq:
737 * Map.merge(other_map) => map
738 *
739 * Copies key/value pairs from other_map into a copy of this map. If a key is
740 * set in other_map and this map, the value from other_map overwrites the value
741 * in the new copy of this map. Returns the new copy of this map with merged
742 * contents.
743 */
744VALUE Map_merge(VALUE _self, VALUE hashmap) {
745 VALUE dupped = Map_dup(_self);
746 return Map_merge_into_self(dupped, hashmap);
747}
748
749static 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.
755VALUE 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).
796void 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
802void Map_next(Map_iter* iter) {
803 upb_strtable_next(&iter->it);
804}
805
806bool Map_done(Map_iter* iter) {
807 return upb_strtable_done(&iter->it);
808}
809
810VALUE 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
817VALUE 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
825void Map_register(VALUE module) {
826 VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
827 rb_define_alloc_func(klass, Map_alloc);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500828 rb_gc_register_address(&cMap);
Austin Schuh40c16522018-10-28 20:27:54 -0700829 cMap = klass;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500830
831 rb_define_method(klass, "initialize", Map_init, -1);
832 rb_define_method(klass, "each", Map_each, 0);
833 rb_define_method(klass, "keys", Map_keys, 0);
834 rb_define_method(klass, "values", Map_values, 0);
835 rb_define_method(klass, "[]", Map_index, 1);
836 rb_define_method(klass, "[]=", Map_index_set, 2);
837 rb_define_method(klass, "has_key?", Map_has_key, 1);
838 rb_define_method(klass, "delete", Map_delete, 1);
839 rb_define_method(klass, "clear", Map_clear, 0);
840 rb_define_method(klass, "length", Map_length, 0);
841 rb_define_method(klass, "dup", Map_dup, 0);
842 rb_define_method(klass, "==", Map_eq, 1);
843 rb_define_method(klass, "hash", Map_hash, 0);
Austin Schuh40c16522018-10-28 20:27:54 -0700844 rb_define_method(klass, "to_hash", Map_to_h, 0);
845 rb_define_method(klass, "to_h", Map_to_h, 0);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500846 rb_define_method(klass, "inspect", Map_inspect, 0);
847 rb_define_method(klass, "merge", Map_merge, 1);
848 rb_include_module(klass, rb_mEnumerable);
849}