Home | History | Annotate | Download | only in protobuf_c
      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 "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)
     66 static void 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       native_slot_validate_string_encoding(self->key_type, key);
     76       *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   }
     96 }
     97 
     98 static VALUE table_key_to_ruby(Map* self, const char* buf, size_t length) {
     99   switch (self->key_type) {
    100     case UPB_TYPE_BYTES:
    101     case UPB_TYPE_STRING: {
    102       VALUE ret = rb_str_new(buf, length);
    103       rb_enc_associate(ret,
    104                        (self->key_type == UPB_TYPE_BYTES) ?
    105                        kRubyString8bitEncoding : kRubyStringUtf8Encoding);
    106       return ret;
    107     }
    108 
    109     case UPB_TYPE_BOOL:
    110     case UPB_TYPE_INT32:
    111     case UPB_TYPE_INT64:
    112     case UPB_TYPE_UINT32:
    113     case UPB_TYPE_UINT64:
    114       return native_slot_get(self->key_type, Qnil, buf);
    115 
    116     default:
    117       assert(false);
    118       return Qnil;
    119   }
    120 }
    121 
    122 static void* value_memory(upb_value* v) {
    123   return (void*)(&v->val);
    124 }
    125 
    126 // -----------------------------------------------------------------------------
    127 // Map container type.
    128 // -----------------------------------------------------------------------------
    129 
    130 const rb_data_type_t Map_type = {
    131   "Google::Protobuf::Map",
    132   { Map_mark, Map_free, NULL },
    133 };
    134 
    135 VALUE cMap;
    136 
    137 Map* ruby_to_Map(VALUE _self) {
    138   Map* self;
    139   TypedData_Get_Struct(_self, Map, &Map_type, self);
    140   return self;
    141 }
    142 
    143 void Map_mark(void* _self) {
    144   Map* self = _self;
    145 
    146   rb_gc_mark(self->value_type_class);
    147 
    148   if (self->value_type == UPB_TYPE_STRING ||
    149       self->value_type == UPB_TYPE_BYTES ||
    150       self->value_type == UPB_TYPE_MESSAGE) {
    151     upb_strtable_iter it;
    152     for (upb_strtable_begin(&it, &self->table);
    153          !upb_strtable_done(&it);
    154          upb_strtable_next(&it)) {
    155       upb_value v = upb_strtable_iter_value(&it);
    156       void* mem = value_memory(&v);
    157       native_slot_mark(self->value_type, mem);
    158     }
    159   }
    160 }
    161 
    162 void Map_free(void* _self) {
    163   Map* self = _self;
    164   upb_strtable_uninit(&self->table);
    165   xfree(self);
    166 }
    167 
    168 VALUE Map_alloc(VALUE klass) {
    169   Map* self = ALLOC(Map);
    170   memset(self, 0, sizeof(Map));
    171   self->value_type_class = Qnil;
    172   return TypedData_Wrap_Struct(klass, &Map_type, self);
    173 }
    174 
    175 static bool needs_typeclass(upb_fieldtype_t type) {
    176   switch (type) {
    177     case UPB_TYPE_MESSAGE:
    178     case UPB_TYPE_ENUM:
    179       return true;
    180     default:
    181       return false;
    182   }
    183 }
    184 
    185 /*
    186  * call-seq:
    187  *     Map.new(key_type, value_type, value_typeclass = nil, init_hashmap = {})
    188  *     => new map
    189  *
    190  * Allocates a new Map container. This constructor may be called with 2, 3, or 4
    191  * arguments. The first two arguments are always present and are symbols (taking
    192  * on the same values as field-type symbols in message descriptors) that
    193  * indicate the type of the map key and value fields.
    194  *
    195  * The supported key types are: :int32, :int64, :uint32, :uint64, :bool,
    196  * :string, :bytes.
    197  *
    198  * The supported value types are: :int32, :int64, :uint32, :uint64, :bool,
    199  * :string, :bytes, :enum, :message.
    200  *
    201  * The third argument, value_typeclass, must be present if value_type is :enum
    202  * or :message. As in RepeatedField#new, this argument must be a message class
    203  * (for :message) or enum module (for :enum).
    204  *
    205  * The last argument, if present, provides initial content for map. Note that
    206  * this may be an ordinary Ruby hashmap or another Map instance with identical
    207  * key and value types. Also note that this argument may be present whether or
    208  * not value_typeclass is present (and it is unambiguously separate from
    209  * value_typeclass because value_typeclass's presence is strictly determined by
    210  * value_type). The contents of this initial hashmap or Map instance are
    211  * shallow-copied into the new Map: the original map is unmodified, but
    212  * references to underlying objects will be shared if the value type is a
    213  * message type.
    214  */
    215 VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
    216   Map* self = ruby_to_Map(_self);
    217   int init_value_arg;
    218 
    219   // We take either two args (:key_type, :value_type), three args (:key_type,
    220   // :value_type, "ValueMessageType"), or four args (the above plus an initial
    221   // hashmap).
    222   if (argc < 2 || argc > 4) {
    223     rb_raise(rb_eArgError, "Map constructor expects 2, 3 or 4 arguments.");
    224   }
    225 
    226   self->key_type = ruby_to_fieldtype(argv[0]);
    227   self->value_type = ruby_to_fieldtype(argv[1]);
    228 
    229   // Check that the key type is an allowed type.
    230   switch (self->key_type) {
    231     case UPB_TYPE_INT32:
    232     case UPB_TYPE_INT64:
    233     case UPB_TYPE_UINT32:
    234     case UPB_TYPE_UINT64:
    235     case UPB_TYPE_BOOL:
    236     case UPB_TYPE_STRING:
    237     case UPB_TYPE_BYTES:
    238       // These are OK.
    239       break;
    240     default:
    241       rb_raise(rb_eArgError, "Invalid key type for map.");
    242   }
    243 
    244   init_value_arg = 2;
    245   if (needs_typeclass(self->value_type) && argc > 2) {
    246     self->value_type_class = argv[2];
    247     validate_type_class(self->value_type, self->value_type_class);
    248     init_value_arg = 3;
    249   }
    250 
    251   // Table value type is always UINT64: this ensures enough space to store the
    252   // native_slot value.
    253   if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
    254     rb_raise(rb_eRuntimeError, "Could not allocate table.");
    255   }
    256 
    257   if (argc > init_value_arg) {
    258     Map_merge_into_self(_self, argv[init_value_arg]);
    259   }
    260 
    261   return Qnil;
    262 }
    263 
    264 /*
    265  * call-seq:
    266  *     Map.each(&block)
    267  *
    268  * Invokes &block on each |key, value| pair in the map, in unspecified order.
    269  * Note that Map also includes Enumerable; map thus acts like a normal Ruby
    270  * sequence.
    271  */
    272 VALUE Map_each(VALUE _self) {
    273   Map* self = ruby_to_Map(_self);
    274 
    275   upb_strtable_iter it;
    276   for (upb_strtable_begin(&it, &self->table);
    277        !upb_strtable_done(&it);
    278        upb_strtable_next(&it)) {
    279 
    280     VALUE key = table_key_to_ruby(
    281         self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
    282 
    283     upb_value v = upb_strtable_iter_value(&it);
    284     void* mem = value_memory(&v);
    285     VALUE value = native_slot_get(self->value_type,
    286                                   self->value_type_class,
    287                                   mem);
    288 
    289     rb_yield_values(2, key, value);
    290   }
    291 
    292   return Qnil;
    293 }
    294 
    295 /*
    296  * call-seq:
    297  *     Map.keys => [list_of_keys]
    298  *
    299  * Returns the list of keys contained in the map, in unspecified order.
    300  */
    301 VALUE Map_keys(VALUE _self) {
    302   Map* self = ruby_to_Map(_self);
    303 
    304   VALUE ret = rb_ary_new();
    305   upb_strtable_iter it;
    306   for (upb_strtable_begin(&it, &self->table);
    307        !upb_strtable_done(&it);
    308        upb_strtable_next(&it)) {
    309 
    310     VALUE key = table_key_to_ruby(
    311         self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
    312 
    313     rb_ary_push(ret, key);
    314   }
    315 
    316   return ret;
    317 }
    318 
    319 /*
    320  * call-seq:
    321  *     Map.values => [list_of_values]
    322  *
    323  * Returns the list of values contained in the map, in unspecified order.
    324  */
    325 VALUE Map_values(VALUE _self) {
    326   Map* self = ruby_to_Map(_self);
    327 
    328   VALUE ret = rb_ary_new();
    329   upb_strtable_iter it;
    330   for (upb_strtable_begin(&it, &self->table);
    331        !upb_strtable_done(&it);
    332        upb_strtable_next(&it)) {
    333 
    334     upb_value v = upb_strtable_iter_value(&it);
    335     void* mem = value_memory(&v);
    336     VALUE value = native_slot_get(self->value_type,
    337                                   self->value_type_class,
    338                                   mem);
    339 
    340     rb_ary_push(ret, value);
    341   }
    342 
    343   return ret;
    344 }
    345 
    346 /*
    347  * call-seq:
    348  *     Map.[](key) => value
    349  *
    350  * Accesses the element at the given key. Throws an exception if the key type is
    351  * incorrect. Returns nil when the key is not present in the map.
    352  */
    353 VALUE Map_index(VALUE _self, VALUE key) {
    354   Map* self = ruby_to_Map(_self);
    355 
    356   char keybuf[TABLE_KEY_BUF_LENGTH];
    357   const char* keyval = NULL;
    358   size_t length = 0;
    359   upb_value v;
    360   table_key(self, key, keybuf, &keyval, &length);
    361 
    362   if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
    363     void* mem = value_memory(&v);
    364     return native_slot_get(self->value_type, self->value_type_class, mem);
    365   } else {
    366     return Qnil;
    367   }
    368 }
    369 
    370 /*
    371  * call-seq:
    372  *     Map.[]=(key, value) => value
    373  *
    374  * Inserts or overwrites the value at the given key with the given new value.
    375  * Throws an exception if the key type is incorrect. Returns the new value that
    376  * was just inserted.
    377  */
    378 VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
    379   Map* self = ruby_to_Map(_self);
    380 
    381   char keybuf[TABLE_KEY_BUF_LENGTH];
    382   const char* keyval = NULL;
    383   size_t length = 0;
    384   upb_value v;
    385   void* mem;
    386   table_key(self, key, keybuf, &keyval, &length);
    387 
    388   mem = value_memory(&v);
    389   native_slot_set(self->value_type, self->value_type_class, mem, value);
    390 
    391   // Replace any existing value by issuing a 'remove' operation first.
    392   upb_strtable_remove2(&self->table, keyval, length, NULL);
    393   if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
    394     rb_raise(rb_eRuntimeError, "Could not insert into table");
    395   }
    396 
    397   // Ruby hashmap's :[]= method also returns the inserted value.
    398   return value;
    399 }
    400 
    401 /*
    402  * call-seq:
    403  *     Map.has_key?(key) => bool
    404  *
    405  * Returns true if the given key is present in the map. Throws an exception if
    406  * the key has the wrong type.
    407  */
    408 VALUE Map_has_key(VALUE _self, VALUE key) {
    409   Map* self = ruby_to_Map(_self);
    410 
    411   char keybuf[TABLE_KEY_BUF_LENGTH];
    412   const char* keyval = NULL;
    413   size_t length = 0;
    414   table_key(self, key, keybuf, &keyval, &length);
    415 
    416   if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
    417     return Qtrue;
    418   } else {
    419     return Qfalse;
    420   }
    421 }
    422 
    423 /*
    424  * call-seq:
    425  *     Map.delete(key) => old_value
    426  *
    427  * Deletes the value at the given key, if any, returning either the old value or
    428  * nil if none was present. Throws an exception if the key is of the wrong type.
    429  */
    430 VALUE Map_delete(VALUE _self, VALUE key) {
    431   Map* self = ruby_to_Map(_self);
    432 
    433   char keybuf[TABLE_KEY_BUF_LENGTH];
    434   const char* keyval = NULL;
    435   size_t length = 0;
    436   upb_value v;
    437   table_key(self, key, keybuf, &keyval, &length);
    438 
    439   if (upb_strtable_remove2(&self->table, keyval, length, &v)) {
    440     void* mem = value_memory(&v);
    441     return native_slot_get(self->value_type, self->value_type_class, mem);
    442   } else {
    443     return Qnil;
    444   }
    445 }
    446 
    447 /*
    448  * call-seq:
    449  *     Map.clear
    450  *
    451  * Removes all entries from the map.
    452  */
    453 VALUE Map_clear(VALUE _self) {
    454   Map* self = ruby_to_Map(_self);
    455 
    456   // Uninit and reinit the table -- this is faster than iterating and doing a
    457   // delete-lookup on each key.
    458   upb_strtable_uninit(&self->table);
    459   if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
    460     rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
    461   }
    462   return Qnil;
    463 }
    464 
    465 /*
    466  * call-seq:
    467  *     Map.length
    468  *
    469  * Returns the number of entries (key-value pairs) in the map.
    470  */
    471 VALUE Map_length(VALUE _self) {
    472   Map* self = ruby_to_Map(_self);
    473   return ULL2NUM(upb_strtable_count(&self->table));
    474 }
    475 
    476 static VALUE Map_new_this_type(VALUE _self) {
    477   Map* self = ruby_to_Map(_self);
    478   VALUE new_map = Qnil;
    479   VALUE key_type = fieldtype_to_ruby(self->key_type);
    480   VALUE value_type = fieldtype_to_ruby(self->value_type);
    481   if (self->value_type_class != Qnil) {
    482     new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
    483                          key_type, value_type, self->value_type_class);
    484   } else {
    485     new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
    486                          key_type, value_type);
    487   }
    488   return new_map;
    489 }
    490 
    491 /*
    492  * call-seq:
    493  *     Map.dup => new_map
    494  *
    495  * Duplicates this map with a shallow copy. References to all non-primitive
    496  * element objects (e.g., submessages) are shared.
    497  */
    498 VALUE Map_dup(VALUE _self) {
    499   Map* self = ruby_to_Map(_self);
    500   VALUE new_map = Map_new_this_type(_self);
    501   Map* new_self = ruby_to_Map(new_map);
    502 
    503   upb_strtable_iter it;
    504   for (upb_strtable_begin(&it, &self->table);
    505        !upb_strtable_done(&it);
    506        upb_strtable_next(&it)) {
    507 
    508     upb_value v = upb_strtable_iter_value(&it);
    509     void* mem = value_memory(&v);
    510     upb_value dup;
    511     void* dup_mem = value_memory(&dup);
    512     native_slot_dup(self->value_type, dup_mem, mem);
    513 
    514     if (!upb_strtable_insert2(&new_self->table,
    515                               upb_strtable_iter_key(&it),
    516                               upb_strtable_iter_keylength(&it),
    517                               dup)) {
    518       rb_raise(rb_eRuntimeError, "Error inserting value into new table");
    519     }
    520   }
    521 
    522   return new_map;
    523 }
    524 
    525 // Used by Google::Protobuf.deep_copy but not exposed directly.
    526 VALUE Map_deep_copy(VALUE _self) {
    527   Map* self = ruby_to_Map(_self);
    528   VALUE new_map = Map_new_this_type(_self);
    529   Map* new_self = ruby_to_Map(new_map);
    530 
    531   upb_strtable_iter it;
    532   for (upb_strtable_begin(&it, &self->table);
    533        !upb_strtable_done(&it);
    534        upb_strtable_next(&it)) {
    535 
    536     upb_value v = upb_strtable_iter_value(&it);
    537     void* mem = value_memory(&v);
    538     upb_value dup;
    539     void* dup_mem = value_memory(&dup);
    540     native_slot_deep_copy(self->value_type, dup_mem, mem);
    541 
    542     if (!upb_strtable_insert2(&new_self->table,
    543                               upb_strtable_iter_key(&it),
    544                               upb_strtable_iter_keylength(&it),
    545                               dup)) {
    546       rb_raise(rb_eRuntimeError, "Error inserting value into new table");
    547     }
    548   }
    549 
    550   return new_map;
    551 }
    552 
    553 /*
    554  * call-seq:
    555  *     Map.==(other) => boolean
    556  *
    557  * Compares this map to another. Maps are equal if they have identical key sets,
    558  * and for each key, the values in both maps compare equal. Elements are
    559  * compared as per normal Ruby semantics, by calling their :== methods (or
    560  * performing a more efficient comparison for primitive types).
    561  *
    562  * Maps with dissimilar key types or value types/typeclasses are never equal,
    563  * even if value comparison (for example, between integers and floats) would
    564  * have otherwise indicated that every element has equal value.
    565  */
    566 VALUE Map_eq(VALUE _self, VALUE _other) {
    567   Map* self = ruby_to_Map(_self);
    568   Map* other;
    569   upb_strtable_iter it;
    570 
    571   // Allow comparisons to Ruby hashmaps by converting to a temporary Map
    572   // instance. Slow, but workable.
    573   if (TYPE(_other) == T_HASH) {
    574     VALUE other_map = Map_new_this_type(_self);
    575     Map_merge_into_self(other_map, _other);
    576     _other = other_map;
    577   }
    578 
    579   other = ruby_to_Map(_other);
    580 
    581   if (self == other) {
    582     return Qtrue;
    583   }
    584   if (self->key_type != other->key_type ||
    585       self->value_type != other->value_type ||
    586       self->value_type_class != other->value_type_class) {
    587     return Qfalse;
    588   }
    589   if (upb_strtable_count(&self->table) != upb_strtable_count(&other->table)) {
    590     return Qfalse;
    591   }
    592 
    593   // For each member of self, check that an equal member exists at the same key
    594   // in other.
    595   for (upb_strtable_begin(&it, &self->table);
    596        !upb_strtable_done(&it);
    597        upb_strtable_next(&it)) {
    598 
    599     upb_value v = upb_strtable_iter_value(&it);
    600     void* mem = value_memory(&v);
    601     upb_value other_v;
    602     void* other_mem = value_memory(&other_v);
    603 
    604     if (!upb_strtable_lookup2(&other->table,
    605                               upb_strtable_iter_key(&it),
    606                               upb_strtable_iter_keylength(&it),
    607                               &other_v)) {
    608       // Not present in other map.
    609       return Qfalse;
    610     }
    611 
    612     if (!native_slot_eq(self->value_type, mem, other_mem)) {
    613       // Present, but value not equal.
    614       return Qfalse;
    615     }
    616   }
    617 
    618   return Qtrue;
    619 }
    620 
    621 /*
    622  * call-seq:
    623  *     Map.hash => hash_value
    624  *
    625  * Returns a hash value based on this map's contents.
    626  */
    627 VALUE Map_hash(VALUE _self) {
    628   Map* self = ruby_to_Map(_self);
    629 
    630   st_index_t h = rb_hash_start(0);
    631   VALUE hash_sym = rb_intern("hash");
    632 
    633   upb_strtable_iter it;
    634   for (upb_strtable_begin(&it, &self->table);
    635        !upb_strtable_done(&it);
    636        upb_strtable_next(&it)) {
    637     VALUE key = table_key_to_ruby(
    638         self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
    639 
    640     upb_value v = upb_strtable_iter_value(&it);
    641     void* mem = value_memory(&v);
    642     VALUE value = native_slot_get(self->value_type,
    643                                   self->value_type_class,
    644                                   mem);
    645 
    646     h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
    647     h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
    648   }
    649 
    650   return INT2FIX(h);
    651 }
    652 
    653 /*
    654  * call-seq:
    655  *     Map.inspect => string
    656  *
    657  * Returns a string representing this map's elements. It will be formatted as
    658  * "{key => value, key => value, ...}", with each key and value string
    659  * representation computed by its own #inspect method.
    660  */
    661 VALUE Map_inspect(VALUE _self) {
    662   Map* self = ruby_to_Map(_self);
    663 
    664   VALUE str = rb_str_new2("{");
    665 
    666   bool first = true;
    667   VALUE inspect_sym = rb_intern("inspect");
    668 
    669   upb_strtable_iter it;
    670   for (upb_strtable_begin(&it, &self->table);
    671        !upb_strtable_done(&it);
    672        upb_strtable_next(&it)) {
    673     VALUE key = table_key_to_ruby(
    674         self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
    675 
    676     upb_value v = upb_strtable_iter_value(&it);
    677     void* mem = value_memory(&v);
    678     VALUE value = native_slot_get(self->value_type,
    679                                   self->value_type_class,
    680                                   mem);
    681 
    682     if (!first) {
    683       str = rb_str_cat2(str, ", ");
    684     } else {
    685       first = false;
    686     }
    687     str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
    688     str = rb_str_cat2(str, "=>");
    689     str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
    690   }
    691 
    692   str = rb_str_cat2(str, "}");
    693   return str;
    694 }
    695 
    696 /*
    697  * call-seq:
    698  *     Map.merge(other_map) => map
    699  *
    700  * Copies key/value pairs from other_map into a copy of this map. If a key is
    701  * set in other_map and this map, the value from other_map overwrites the value
    702  * in the new copy of this map. Returns the new copy of this map with merged
    703  * contents.
    704  */
    705 VALUE Map_merge(VALUE _self, VALUE hashmap) {
    706   VALUE dupped = Map_dup(_self);
    707   return Map_merge_into_self(dupped, hashmap);
    708 }
    709 
    710 static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
    711   Map_index_set(self, key, value);
    712   return ST_CONTINUE;
    713 }
    714 
    715 // Used only internally -- shared by #merge and #initialize.
    716 VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
    717   if (TYPE(hashmap) == T_HASH) {
    718     rb_hash_foreach(hashmap, merge_into_self_callback, _self);
    719   } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
    720              RTYPEDDATA_TYPE(hashmap) == &Map_type) {
    721 
    722     Map* self = ruby_to_Map(_self);
    723     Map* other = ruby_to_Map(hashmap);
    724     upb_strtable_iter it;
    725 
    726     if (self->key_type != other->key_type ||
    727         self->value_type != other->value_type ||
    728         self->value_type_class != other->value_type_class) {
    729       rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
    730     }
    731 
    732     for (upb_strtable_begin(&it, &other->table);
    733          !upb_strtable_done(&it);
    734          upb_strtable_next(&it)) {
    735 
    736       // Replace any existing value by issuing a 'remove' operation first.
    737       upb_value v;
    738       upb_value oldv;
    739       upb_strtable_remove2(&self->table,
    740                            upb_strtable_iter_key(&it),
    741                            upb_strtable_iter_keylength(&it),
    742                            &oldv);
    743 
    744       v = upb_strtable_iter_value(&it);
    745       upb_strtable_insert2(&self->table,
    746                            upb_strtable_iter_key(&it),
    747                            upb_strtable_iter_keylength(&it),
    748                            v);
    749     }
    750   } else {
    751     rb_raise(rb_eArgError, "Unknown type merging into Map");
    752   }
    753   return _self;
    754 }
    755 
    756 // Internal method: map iterator initialization (used for serialization).
    757 void Map_begin(VALUE _self, Map_iter* iter) {
    758   Map* self = ruby_to_Map(_self);
    759   iter->self = self;
    760   upb_strtable_begin(&iter->it, &self->table);
    761 }
    762 
    763 void Map_next(Map_iter* iter) {
    764   upb_strtable_next(&iter->it);
    765 }
    766 
    767 bool Map_done(Map_iter* iter) {
    768   return upb_strtable_done(&iter->it);
    769 }
    770 
    771 VALUE Map_iter_key(Map_iter* iter) {
    772   return table_key_to_ruby(
    773       iter->self,
    774       upb_strtable_iter_key(&iter->it),
    775       upb_strtable_iter_keylength(&iter->it));
    776 }
    777 
    778 VALUE Map_iter_value(Map_iter* iter) {
    779   upb_value v = upb_strtable_iter_value(&iter->it);
    780   void* mem = value_memory(&v);
    781   return native_slot_get(iter->self->value_type,
    782                          iter->self->value_type_class,
    783                          mem);
    784 }
    785 
    786 void Map_register(VALUE module) {
    787   VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
    788   rb_define_alloc_func(klass, Map_alloc);
    789   cMap = klass;
    790   rb_gc_register_address(&cMap);
    791 
    792   rb_define_method(klass, "initialize", Map_init, -1);
    793   rb_define_method(klass, "each", Map_each, 0);
    794   rb_define_method(klass, "keys", Map_keys, 0);
    795   rb_define_method(klass, "values", Map_values, 0);
    796   rb_define_method(klass, "[]", Map_index, 1);
    797   rb_define_method(klass, "[]=", Map_index_set, 2);
    798   rb_define_method(klass, "has_key?", Map_has_key, 1);
    799   rb_define_method(klass, "delete", Map_delete, 1);
    800   rb_define_method(klass, "clear", Map_clear, 0);
    801   rb_define_method(klass, "length", Map_length, 0);
    802   rb_define_method(klass, "dup", Map_dup, 0);
    803   rb_define_method(klass, "==", Map_eq, 1);
    804   rb_define_method(klass, "hash", Map_hash, 0);
    805   rb_define_method(klass, "inspect", Map_inspect, 0);
    806   rb_define_method(klass, "merge", Map_merge, 1);
    807   rb_include_module(klass, rb_mEnumerable);
    808 }
    809