google-protobuf 3.4.0.2 → 3.19.4

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.

Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/ext/google/protobuf_c/convert.c +348 -0
  3. data/ext/google/protobuf_c/convert.h +72 -0
  4. data/ext/google/protobuf_c/defs.c +709 -1188
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +7 -4
  7. data/ext/google/protobuf_c/map.c +316 -463
  8. data/ext/google/protobuf_c/map.h +67 -0
  9. data/ext/google/protobuf_c/message.c +993 -296
  10. data/ext/google/protobuf_c/message.h +101 -0
  11. data/ext/google/protobuf_c/protobuf.c +403 -50
  12. data/ext/google/protobuf_c/protobuf.h +47 -473
  13. data/ext/google/protobuf_c/repeated_field.c +314 -309
  14. data/ext/google/protobuf_c/repeated_field.h +63 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +9171 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +4704 -0
  17. data/ext/google/protobuf_c/wrap_memcpy.c +1 -1
  18. data/lib/google/protobuf/any_pb.rb +6 -4
  19. data/lib/google/protobuf/api_pb.rb +27 -24
  20. data/lib/google/protobuf/descriptor_dsl.rb +458 -0
  21. data/lib/google/protobuf/descriptor_pb.rb +268 -0
  22. data/lib/google/protobuf/duration_pb.rb +6 -4
  23. data/lib/google/protobuf/empty_pb.rb +4 -2
  24. data/lib/google/protobuf/field_mask_pb.rb +5 -3
  25. data/lib/google/protobuf/message_exts.rb +2 -2
  26. data/lib/google/protobuf/repeated_field.rb +3 -3
  27. data/lib/google/protobuf/source_context_pb.rb +5 -3
  28. data/lib/google/protobuf/struct_pb.rb +23 -21
  29. data/lib/google/protobuf/timestamp_pb.rb +6 -4
  30. data/lib/google/protobuf/type_pb.rb +77 -74
  31. data/lib/google/protobuf/well_known_types.rb +25 -2
  32. data/lib/google/protobuf/wrappers_pb.rb +37 -35
  33. data/lib/google/protobuf.rb +7 -4
  34. data/tests/basic.rb +432 -1115
  35. data/tests/generated_code_test.rb +6 -2
  36. data/tests/stress.rb +1 -1
  37. metadata +22 -30
  38. data/ext/google/protobuf_c/encode_decode.c +0 -1311
  39. data/ext/google/protobuf_c/storage.c +0 -893
  40. data/ext/google/protobuf_c/upb.c +0 -13911
  41. data/ext/google/protobuf_c/upb.h +0 -8872
@@ -28,160 +28,233 @@
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 upb's strtable.
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
- // 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 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
- 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;
50
+ typedef struct {
51
+ const upb_map *map; // Can convert to mutable when non-frozen.
52
+ upb_fieldtype_t key_type;
53
+ TypeInfo value_type_info;
54
+ VALUE value_type_class;
55
+ VALUE arena;
56
+ } Map;
89
57
 
90
- default:
91
- // Map constructor should not allow a Map with another key type to be
92
- // constructed.
93
- assert(false);
94
- break;
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
- return key;
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 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;
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_fieldtype_t key_type,
88
+ TypeInfo value_type, 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 == UPB_TYPE_MESSAGE) {
103
+ const upb_msgdef *val_m = self->value_type_info.def.msgdef;
104
+ self->value_type_class = Descriptor_DefToClass(val_m);
109
105
  }
106
+ }
110
107
 
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);
108
+ return val;
109
+ }
117
110
 
118
- default:
119
- assert(false);
120
- return Qnil;
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 void* value_memory(upb_value* v) {
125
- return (void*)(&v->val);
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
- // Map container type.
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 rb_data_type_t Map_type = {
133
- "Google::Protobuf::Map",
134
- { Map_mark, Map_free, NULL },
135
- };
133
+ VALUE Map_CreateHash(const upb_map* map, upb_fieldtype_t key_type,
134
+ TypeInfo val_info) {
135
+ VALUE hash = rb_hash_new();
136
+ size_t iter = UPB_MAP_BEGIN;
137
+ TypeInfo key_info = TypeInfo_from_type(key_type);
136
138
 
137
- VALUE cMap;
139
+ if (!map) return hash;
138
140
 
139
- Map* ruby_to_Map(VALUE _self) {
140
- Map* self;
141
- TypedData_Get_Struct(_self, Map, &Map_type, self);
142
- return self;
141
+ while (upb_mapiter_next(map, &iter)) {
142
+ upb_msgval key = upb_mapiter_key(map, iter);
143
+ upb_msgval val = upb_mapiter_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
+ }
148
+
149
+ return hash;
143
150
  }
144
151
 
145
- void Map_mark(void* _self) {
146
- Map* self = _self;
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 = UPB_MAP_BEGIN;
159
+ while (upb_mapiter_next(self->map, &iter)) {
160
+ upb_msgval key = upb_mapiter_key(self->map, iter);
161
+ upb_msgval val = upb_mapiter_value(self->map, iter);
162
+ upb_msgval val_copy = Msgval_DeepCopy(val, self->value_type_info, arena);
163
+ upb_map_set(new_map, key, val_copy, arena);
164
+ }
147
165
 
148
- rb_gc_mark(self->value_type_class);
166
+ return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
167
+ new_arena_rb);
168
+ }
149
169
 
150
- if (self->value_type == UPB_TYPE_STRING ||
151
- self->value_type == UPB_TYPE_BYTES ||
152
- self->value_type == UPB_TYPE_MESSAGE) {
153
- upb_strtable_iter it;
154
- for (upb_strtable_begin(&it, &self->table);
155
- !upb_strtable_done(&it);
156
- upb_strtable_next(&it)) {
157
- upb_value v = upb_strtable_iter_value(&it);
158
- void* mem = value_memory(&v);
159
- native_slot_mark(self->value_type, mem);
160
- }
170
+ const upb_map* Map_GetUpbMap(VALUE val, const upb_fielddef* field,
171
+ upb_arena* arena) {
172
+ const upb_fielddef* key_field = map_field_key(field);
173
+ const upb_fielddef* value_field = map_field_value(field);
174
+ TypeInfo value_type_info = TypeInfo_get(value_field);
175
+ Map* self;
176
+
177
+ if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
178
+ RTYPEDDATA_TYPE(val) != &Map_type) {
179
+ rb_raise(cTypeError, "Expected Map instance");
161
180
  }
181
+
182
+ self = ruby_to_Map(val);
183
+ if (self->key_type != upb_fielddef_type(key_field)) {
184
+ rb_raise(cTypeError, "Map key type does not match field's key type");
185
+ }
186
+ if (self->value_type_info.type != value_type_info.type) {
187
+ rb_raise(cTypeError, "Map value type does not match field's value type");
188
+ }
189
+ if (self->value_type_info.def.msgdef != value_type_info.def.msgdef) {
190
+ rb_raise(cTypeError, "Map value type has wrong message/enum class");
191
+ }
192
+
193
+ Arena_fuse(self->arena, arena);
194
+ return self->map;
162
195
  }
163
196
 
164
- void Map_free(void* _self) {
165
- Map* self = _self;
166
- upb_strtable_uninit(&self->table);
167
- xfree(self);
197
+ void Map_Inspect(StringBuilder* b, const upb_map* map, upb_fieldtype_t key_type,
198
+ TypeInfo val_type) {
199
+ bool first = true;
200
+ TypeInfo key_type_info = {key_type};
201
+ StringBuilder_Printf(b, "{");
202
+ if (map) {
203
+ size_t iter = UPB_MAP_BEGIN;
204
+ while (upb_mapiter_next(map, &iter)) {
205
+ upb_msgval key = upb_mapiter_key(map, iter);
206
+ upb_msgval val = upb_mapiter_value(map, iter);
207
+ if (first) {
208
+ first = false;
209
+ } else {
210
+ StringBuilder_Printf(b, ", ");
211
+ }
212
+ StringBuilder_PrintMsgval(b, key, key_type_info);
213
+ StringBuilder_Printf(b, "=>");
214
+ StringBuilder_PrintMsgval(b, val, val_type);
215
+ }
216
+ }
217
+ StringBuilder_Printf(b, "}");
168
218
  }
169
219
 
170
- VALUE Map_alloc(VALUE klass) {
171
- Map* self = ALLOC(Map);
172
- memset(self, 0, sizeof(Map));
173
- self->value_type_class = Qnil;
174
- return TypedData_Wrap_Struct(klass, &Map_type, self);
220
+ static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
221
+ Map* self = ruby_to_Map(_self);
222
+ upb_arena *arena = Arena_get(self->arena);
223
+ upb_msgval key_val = Convert_RubyToUpb(key, "", Map_keyinfo(self), arena);
224
+ upb_msgval val_val = Convert_RubyToUpb(val, "", self->value_type_info, arena);
225
+ upb_map_set(Map_GetMutable(_self), key_val, val_val, arena);
226
+ return ST_CONTINUE;
175
227
  }
176
228
 
177
- static bool needs_typeclass(upb_fieldtype_t type) {
178
- switch (type) {
179
- case UPB_TYPE_MESSAGE:
180
- case UPB_TYPE_ENUM:
181
- return true;
182
- default:
183
- return false;
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_msg *self_msg = Map_GetMutable(_self);
239
+ size_t iter = UPB_MAP_BEGIN;
240
+
241
+ Arena_fuse(other->arena, arena);
242
+
243
+ if (self->key_type != other->key_type ||
244
+ self->value_type_info.type != other->value_type_info.type ||
245
+ self->value_type_class != other->value_type_class) {
246
+ rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
247
+ }
248
+
249
+ while (upb_mapiter_next(other->map, &iter)) {
250
+ upb_msgval key = upb_mapiter_key(other->map, iter);
251
+ upb_msgval val = upb_mapiter_value(other->map, iter);
252
+ upb_map_set(self_msg, key, val, arena);
253
+ }
254
+ } else {
255
+ rb_raise(rb_eArgError, "Unknown type merging into Map");
184
256
  }
257
+ return _self;
185
258
  }
186
259
 
187
260
  /*
@@ -214,9 +287,9 @@ static bool needs_typeclass(upb_fieldtype_t type) {
214
287
  * references to underlying objects will be shared if the value type is a
215
288
  * message type.
216
289
  */
217
- VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
290
+ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
218
291
  Map* self = ruby_to_Map(_self);
219
- int init_value_arg;
292
+ VALUE init_arg;
220
293
 
221
294
  // We take either two args (:key_type, :value_type), three args (:key_type,
222
295
  // :value_type, "ValueMessageType"), or four args (the above plus an initial
@@ -226,7 +299,9 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
226
299
  }
227
300
 
228
301
  self->key_type = ruby_to_fieldtype(argv[0]);
229
- self->value_type = ruby_to_fieldtype(argv[1]);
302
+ self->value_type_info =
303
+ TypeInfo_FromClass(argc, argv, 1, &self->value_type_class, &init_arg);
304
+ self->arena = Arena_new();
230
305
 
231
306
  // Check that the key type is an allowed type.
232
307
  switch (self->key_type) {
@@ -243,21 +318,12 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
243
318
  rb_raise(rb_eArgError, "Invalid key type for map.");
244
319
  }
245
320
 
246
- init_value_arg = 2;
247
- if (needs_typeclass(self->value_type) && argc > 2) {
248
- self->value_type_class = argv[2];
249
- validate_type_class(self->value_type, self->value_type_class);
250
- init_value_arg = 3;
251
- }
321
+ self->map = upb_map_new(Arena_get(self->arena), self->key_type,
322
+ self->value_type_info.type);
323
+ ObjectCache_Add(self->map, _self);
252
324
 
253
- // Table value type is always UINT64: this ensures enough space to store the
254
- // native_slot value.
255
- if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
256
- rb_raise(rb_eRuntimeError, "Could not allocate table.");
257
- }
258
-
259
- if (argc > init_value_arg) {
260
- Map_merge_into_self(_self, argv[init_value_arg]);
325
+ if (init_arg != Qnil) {
326
+ Map_merge_into_self(_self, init_arg);
261
327
  }
262
328
 
263
329
  return Qnil;
@@ -271,24 +337,16 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
271
337
  * Note that Map also includes Enumerable; map thus acts like a normal Ruby
272
338
  * sequence.
273
339
  */
274
- VALUE Map_each(VALUE _self) {
340
+ static VALUE Map_each(VALUE _self) {
275
341
  Map* self = ruby_to_Map(_self);
276
-
277
- upb_strtable_iter it;
278
- for (upb_strtable_begin(&it, &self->table);
279
- !upb_strtable_done(&it);
280
- upb_strtable_next(&it)) {
281
-
282
- VALUE key = table_key_to_ruby(
283
- self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
284
-
285
- upb_value v = upb_strtable_iter_value(&it);
286
- void* mem = value_memory(&v);
287
- VALUE value = native_slot_get(self->value_type,
288
- self->value_type_class,
289
- mem);
290
-
291
- rb_yield_values(2, key, value);
342
+ size_t iter = UPB_MAP_BEGIN;
343
+
344
+ while (upb_mapiter_next(self->map, &iter)) {
345
+ upb_msgval key = upb_mapiter_key(self->map, iter);
346
+ upb_msgval val = upb_mapiter_value(self->map, iter);
347
+ VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
348
+ VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
349
+ rb_yield_values(2, key_val, val_val);
292
350
  }
293
351
 
294
352
  return Qnil;
@@ -300,19 +358,15 @@ VALUE Map_each(VALUE _self) {
300
358
  *
301
359
  * Returns the list of keys contained in the map, in unspecified order.
302
360
  */
303
- VALUE Map_keys(VALUE _self) {
361
+ static VALUE Map_keys(VALUE _self) {
304
362
  Map* self = ruby_to_Map(_self);
305
-
363
+ size_t iter = UPB_MAP_BEGIN;
306
364
  VALUE ret = rb_ary_new();
307
- upb_strtable_iter it;
308
- for (upb_strtable_begin(&it, &self->table);
309
- !upb_strtable_done(&it);
310
- upb_strtable_next(&it)) {
311
-
312
- VALUE key = table_key_to_ruby(
313
- self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
314
365
 
315
- rb_ary_push(ret, key);
366
+ while (upb_mapiter_next(self->map, &iter)) {
367
+ upb_msgval key = upb_mapiter_key(self->map, iter);
368
+ VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
369
+ rb_ary_push(ret, key_val);
316
370
  }
317
371
 
318
372
  return ret;
@@ -324,22 +378,15 @@ VALUE Map_keys(VALUE _self) {
324
378
  *
325
379
  * Returns the list of values contained in the map, in unspecified order.
326
380
  */
327
- VALUE Map_values(VALUE _self) {
381
+ static VALUE Map_values(VALUE _self) {
328
382
  Map* self = ruby_to_Map(_self);
329
-
383
+ size_t iter = UPB_MAP_BEGIN;
330
384
  VALUE ret = rb_ary_new();
331
- upb_strtable_iter it;
332
- for (upb_strtable_begin(&it, &self->table);
333
- !upb_strtable_done(&it);
334
- upb_strtable_next(&it)) {
335
-
336
- upb_value v = upb_strtable_iter_value(&it);
337
- void* mem = value_memory(&v);
338
- VALUE value = native_slot_get(self->value_type,
339
- self->value_type_class,
340
- mem);
341
-
342
- rb_ary_push(ret, value);
385
+
386
+ while (upb_mapiter_next(self->map, &iter)) {
387
+ upb_msgval val = upb_mapiter_value(self->map, iter);
388
+ VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
389
+ rb_ary_push(ret, val_val);
343
390
  }
344
391
 
345
392
  return ret;
@@ -352,18 +399,13 @@ VALUE Map_values(VALUE _self) {
352
399
  * Accesses the element at the given key. Throws an exception if the key type is
353
400
  * incorrect. Returns nil when the key is not present in the map.
354
401
  */
355
- VALUE Map_index(VALUE _self, VALUE key) {
402
+ static VALUE Map_index(VALUE _self, VALUE key) {
356
403
  Map* self = ruby_to_Map(_self);
404
+ upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
405
+ upb_msgval val;
357
406
 
358
- char keybuf[TABLE_KEY_BUF_LENGTH];
359
- const char* keyval = NULL;
360
- size_t length = 0;
361
- upb_value v;
362
- key = table_key(self, key, keybuf, &keyval, &length);
363
-
364
- if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
365
- void* mem = value_memory(&v);
366
- return native_slot_get(self->value_type, self->value_type_class, mem);
407
+ if (upb_map_get(self->map, key_upb, &val)) {
408
+ return Convert_UpbToRuby(val, self->value_type_info, self->arena);
367
409
  } else {
368
410
  return Qnil;
369
411
  }
@@ -377,27 +419,15 @@ VALUE Map_index(VALUE _self, VALUE key) {
377
419
  * Throws an exception if the key type is incorrect. Returns the new value that
378
420
  * was just inserted.
379
421
  */
380
- VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
422
+ static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
381
423
  Map* self = ruby_to_Map(_self);
424
+ upb_arena *arena = Arena_get(self->arena);
425
+ upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
426
+ upb_msgval val_upb = Convert_RubyToUpb(val, "", self->value_type_info, arena);
382
427
 
383
- char keybuf[TABLE_KEY_BUF_LENGTH];
384
- const char* keyval = NULL;
385
- size_t length = 0;
386
- upb_value v;
387
- void* mem;
388
- key = table_key(self, key, keybuf, &keyval, &length);
389
-
390
- mem = value_memory(&v);
391
- native_slot_set(self->value_type, self->value_type_class, mem, value);
428
+ upb_map_set(Map_GetMutable(_self), key_upb, val_upb, arena);
392
429
 
393
- // Replace any existing value by issuing a 'remove' operation first.
394
- upb_strtable_remove2(&self->table, keyval, length, NULL);
395
- if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
396
- rb_raise(rb_eRuntimeError, "Could not insert into table");
397
- }
398
-
399
- // Ruby hashmap's :[]= method also returns the inserted value.
400
- return value;
430
+ return val;
401
431
  }
402
432
 
403
433
  /*
@@ -407,15 +437,11 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
407
437
  * Returns true if the given key is present in the map. Throws an exception if
408
438
  * the key has the wrong type.
409
439
  */
410
- VALUE Map_has_key(VALUE _self, VALUE key) {
440
+ static VALUE Map_has_key(VALUE _self, VALUE key) {
411
441
  Map* self = ruby_to_Map(_self);
442
+ upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
412
443
 
413
- char keybuf[TABLE_KEY_BUF_LENGTH];
414
- const char* keyval = NULL;
415
- size_t length = 0;
416
- key = table_key(self, key, keybuf, &keyval, &length);
417
-
418
- if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
444
+ if (upb_map_get(self->map, key_upb, NULL)) {
419
445
  return Qtrue;
420
446
  } else {
421
447
  return Qfalse;
@@ -429,21 +455,25 @@ VALUE Map_has_key(VALUE _self, VALUE key) {
429
455
  * Deletes the value at the given key, if any, returning either the old value or
430
456
  * nil if none was present. Throws an exception if the key is of the wrong type.
431
457
  */
432
- VALUE Map_delete(VALUE _self, VALUE key) {
458
+ static VALUE Map_delete(VALUE _self, VALUE key) {
433
459
  Map* self = ruby_to_Map(_self);
460
+ upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
461
+ upb_msgval val_upb;
462
+ VALUE ret;
434
463
 
435
- char keybuf[TABLE_KEY_BUF_LENGTH];
436
- const char* keyval = NULL;
437
- size_t length = 0;
438
- upb_value v;
439
- key = table_key(self, key, keybuf, &keyval, &length);
464
+ rb_check_frozen(_self);
440
465
 
441
- if (upb_strtable_remove2(&self->table, keyval, length, &v)) {
442
- void* mem = value_memory(&v);
443
- return native_slot_get(self->value_type, self->value_type_class, mem);
466
+ // TODO(haberman): make upb_map_delete() also capable of returning the deleted
467
+ // value.
468
+ if (upb_map_get(self->map, key_upb, &val_upb)) {
469
+ ret = Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
444
470
  } else {
445
- return Qnil;
471
+ ret = Qnil;
446
472
  }
473
+
474
+ upb_map_delete(Map_GetMutable(_self), key_upb);
475
+
476
+ return ret;
447
477
  }
448
478
 
449
479
  /*
@@ -452,15 +482,8 @@ VALUE Map_delete(VALUE _self, VALUE key) {
452
482
  *
453
483
  * Removes all entries from the map.
454
484
  */
455
- VALUE Map_clear(VALUE _self) {
456
- Map* self = ruby_to_Map(_self);
457
-
458
- // Uninit and reinit the table -- this is faster than iterating and doing a
459
- // delete-lookup on each key.
460
- upb_strtable_uninit(&self->table);
461
- if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
462
- rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
463
- }
485
+ static VALUE Map_clear(VALUE _self) {
486
+ upb_map_clear(Map_GetMutable(_self));
464
487
  return Qnil;
465
488
  }
466
489
 
@@ -470,24 +493,9 @@ VALUE Map_clear(VALUE _self) {
470
493
  *
471
494
  * Returns the number of entries (key-value pairs) in the map.
472
495
  */
473
- VALUE Map_length(VALUE _self) {
496
+ static VALUE Map_length(VALUE _self) {
474
497
  Map* self = ruby_to_Map(_self);
475
- return ULL2NUM(upb_strtable_count(&self->table));
476
- }
477
-
478
- static VALUE Map_new_this_type(VALUE _self) {
479
- Map* self = ruby_to_Map(_self);
480
- VALUE new_map = Qnil;
481
- VALUE key_type = fieldtype_to_ruby(self->key_type);
482
- VALUE value_type = fieldtype_to_ruby(self->value_type);
483
- if (self->value_type_class != Qnil) {
484
- new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
485
- key_type, value_type, self->value_type_class);
486
- } else {
487
- new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
488
- key_type, value_type);
489
- }
490
- return new_map;
498
+ return ULL2NUM(upb_map_size(self->map));
491
499
  }
492
500
 
493
501
  /*
@@ -497,59 +505,23 @@ static VALUE Map_new_this_type(VALUE _self) {
497
505
  * Duplicates this map with a shallow copy. References to all non-primitive
498
506
  * element objects (e.g., submessages) are shared.
499
507
  */
500
- VALUE Map_dup(VALUE _self) {
508
+ static VALUE Map_dup(VALUE _self) {
501
509
  Map* self = ruby_to_Map(_self);
502
- VALUE new_map = Map_new_this_type(_self);
503
- Map* new_self = ruby_to_Map(new_map);
504
-
505
- upb_strtable_iter it;
506
- for (upb_strtable_begin(&it, &self->table);
507
- !upb_strtable_done(&it);
508
- upb_strtable_next(&it)) {
509
-
510
- upb_value v = upb_strtable_iter_value(&it);
511
- void* mem = value_memory(&v);
512
- upb_value dup;
513
- void* dup_mem = value_memory(&dup);
514
- native_slot_dup(self->value_type, dup_mem, mem);
515
-
516
- if (!upb_strtable_insert2(&new_self->table,
517
- upb_strtable_iter_key(&it),
518
- upb_strtable_iter_keylength(&it),
519
- dup)) {
520
- rb_raise(rb_eRuntimeError, "Error inserting value into new table");
521
- }
522
- }
523
-
524
- return new_map;
525
- }
526
-
527
- // Used by Google::Protobuf.deep_copy but not exposed directly.
528
- VALUE Map_deep_copy(VALUE _self) {
529
- Map* self = ruby_to_Map(_self);
530
- VALUE new_map = Map_new_this_type(_self);
531
- Map* new_self = ruby_to_Map(new_map);
532
-
533
- upb_strtable_iter it;
534
- for (upb_strtable_begin(&it, &self->table);
535
- !upb_strtable_done(&it);
536
- upb_strtable_next(&it)) {
537
-
538
- upb_value v = upb_strtable_iter_value(&it);
539
- void* mem = value_memory(&v);
540
- upb_value dup;
541
- void* dup_mem = value_memory(&dup);
542
- native_slot_deep_copy(self->value_type, dup_mem, mem);
543
-
544
- if (!upb_strtable_insert2(&new_self->table,
545
- upb_strtable_iter_key(&it),
546
- upb_strtable_iter_keylength(&it),
547
- dup)) {
548
- rb_raise(rb_eRuntimeError, "Error inserting value into new table");
549
- }
510
+ VALUE new_map_rb = Map_new_this_type(self);
511
+ Map* new_self = ruby_to_Map(new_map_rb);
512
+ size_t iter = UPB_MAP_BEGIN;
513
+ upb_arena *arena = Arena_get(new_self->arena);
514
+ upb_map *new_map = Map_GetMutable(new_map_rb);
515
+
516
+ Arena_fuse(self->arena, arena);
517
+
518
+ while (upb_mapiter_next(self->map, &iter)) {
519
+ upb_msgval key = upb_mapiter_key(self->map, iter);
520
+ upb_msgval val = upb_mapiter_value(self->map, iter);
521
+ upb_map_set(new_map, key, val, arena);
550
522
  }
551
523
 
552
- return new_map;
524
+ return new_map_rb;
553
525
  }
554
526
 
555
527
  /*
@@ -568,12 +540,11 @@ VALUE Map_deep_copy(VALUE _self) {
568
540
  VALUE Map_eq(VALUE _self, VALUE _other) {
569
541
  Map* self = ruby_to_Map(_self);
570
542
  Map* other;
571
- upb_strtable_iter it;
572
543
 
573
544
  // Allow comparisons to Ruby hashmaps by converting to a temporary Map
574
545
  // instance. Slow, but workable.
575
546
  if (TYPE(_other) == T_HASH) {
576
- VALUE other_map = Map_new_this_type(_self);
547
+ VALUE other_map = Map_new_this_type(self);
577
548
  Map_merge_into_self(other_map, _other);
578
549
  _other = other_map;
579
550
  }
@@ -584,35 +555,27 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
584
555
  return Qtrue;
585
556
  }
586
557
  if (self->key_type != other->key_type ||
587
- self->value_type != other->value_type ||
558
+ self->value_type_info.type != other->value_type_info.type ||
588
559
  self->value_type_class != other->value_type_class) {
589
560
  return Qfalse;
590
561
  }
591
- if (upb_strtable_count(&self->table) != upb_strtable_count(&other->table)) {
562
+ if (upb_map_size(self->map) != upb_map_size(other->map)) {
592
563
  return Qfalse;
593
564
  }
594
565
 
595
566
  // For each member of self, check that an equal member exists at the same key
596
567
  // in other.
597
- for (upb_strtable_begin(&it, &self->table);
598
- !upb_strtable_done(&it);
599
- upb_strtable_next(&it)) {
600
-
601
- upb_value v = upb_strtable_iter_value(&it);
602
- void* mem = value_memory(&v);
603
- upb_value other_v;
604
- void* other_mem = value_memory(&other_v);
605
-
606
- if (!upb_strtable_lookup2(&other->table,
607
- upb_strtable_iter_key(&it),
608
- upb_strtable_iter_keylength(&it),
609
- &other_v)) {
568
+ size_t iter = UPB_MAP_BEGIN;
569
+ while (upb_mapiter_next(self->map, &iter)) {
570
+ upb_msgval key = upb_mapiter_key(self->map, iter);
571
+ upb_msgval val = upb_mapiter_value(self->map, iter);
572
+ upb_msgval other_val;
573
+ if (!upb_map_get(other->map, key, &other_val)) {
610
574
  // Not present in other map.
611
575
  return Qfalse;
612
576
  }
613
-
614
- if (!native_slot_eq(self->value_type, mem, other_mem)) {
615
- // Present, but value not equal.
577
+ if (!Msgval_IsEqual(val, other_val, self->value_type_info)) {
578
+ // Present but different value.
616
579
  return Qfalse;
617
580
  }
618
581
  }
@@ -620,6 +583,22 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
620
583
  return Qtrue;
621
584
  }
622
585
 
586
+ /*
587
+ * call-seq:
588
+ * Message.freeze => self
589
+ *
590
+ * Freezes the message object. We have to intercept this so we can pin the
591
+ * Ruby object into memory so we don't forget it's frozen.
592
+ */
593
+ static VALUE Map_freeze(VALUE _self) {
594
+ Map* self = ruby_to_Map(_self);
595
+ if (!RB_OBJ_FROZEN(_self)) {
596
+ Arena_Pin(self->arena, _self);
597
+ RB_OBJ_FREEZE(_self);
598
+ }
599
+ return _self;
600
+ }
601
+
623
602
  /*
624
603
  * call-seq:
625
604
  * Map.hash => hash_value
@@ -628,28 +607,18 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
628
607
  */
629
608
  VALUE Map_hash(VALUE _self) {
630
609
  Map* self = ruby_to_Map(_self);
631
-
632
- st_index_t h = rb_hash_start(0);
633
- VALUE hash_sym = rb_intern("hash");
634
-
635
- upb_strtable_iter it;
636
- for (upb_strtable_begin(&it, &self->table);
637
- !upb_strtable_done(&it);
638
- upb_strtable_next(&it)) {
639
- VALUE key = table_key_to_ruby(
640
- self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
641
-
642
- upb_value v = upb_strtable_iter_value(&it);
643
- void* mem = value_memory(&v);
644
- VALUE value = native_slot_get(self->value_type,
645
- self->value_type_class,
646
- mem);
647
-
648
- h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
649
- h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
610
+ uint64_t hash = 0;
611
+
612
+ size_t iter = UPB_MAP_BEGIN;
613
+ TypeInfo key_info = {self->key_type};
614
+ while (upb_mapiter_next(self->map, &iter)) {
615
+ upb_msgval key = upb_mapiter_key(self->map, iter);
616
+ upb_msgval val = upb_mapiter_value(self->map, iter);
617
+ hash = Msgval_GetHash(key, key_info, hash);
618
+ hash = Msgval_GetHash(val, self->value_type_info, hash);
650
619
  }
651
620
 
652
- return INT2FIX(h);
621
+ return LL2NUM(hash);
653
622
  }
654
623
 
655
624
  /*
@@ -660,25 +629,7 @@ VALUE Map_hash(VALUE _self) {
660
629
  */
661
630
  VALUE Map_to_h(VALUE _self) {
662
631
  Map* self = ruby_to_Map(_self);
663
- VALUE hash = rb_hash_new();
664
- upb_strtable_iter it;
665
- for (upb_strtable_begin(&it, &self->table);
666
- !upb_strtable_done(&it);
667
- upb_strtable_next(&it)) {
668
- VALUE key = table_key_to_ruby(
669
- self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
670
- upb_value v = upb_strtable_iter_value(&it);
671
- void* mem = value_memory(&v);
672
- VALUE value = native_slot_get(self->value_type,
673
- self->value_type_class,
674
- mem);
675
-
676
- if (self->value_type == UPB_TYPE_MESSAGE) {
677
- value = Message_to_h(value);
678
- }
679
- rb_hash_aset(hash, key, value);
680
- }
681
- return hash;
632
+ return Map_CreateHash(self->map, self->key_type, self->value_type_info);
682
633
  }
683
634
 
684
635
  /*
@@ -692,36 +643,11 @@ VALUE Map_to_h(VALUE _self) {
692
643
  VALUE Map_inspect(VALUE _self) {
693
644
  Map* self = ruby_to_Map(_self);
694
645
 
695
- VALUE str = rb_str_new2("{");
696
-
697
- bool first = true;
698
- VALUE inspect_sym = rb_intern("inspect");
699
-
700
- upb_strtable_iter it;
701
- for (upb_strtable_begin(&it, &self->table);
702
- !upb_strtable_done(&it);
703
- upb_strtable_next(&it)) {
704
- VALUE key = table_key_to_ruby(
705
- self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
706
-
707
- upb_value v = upb_strtable_iter_value(&it);
708
- void* mem = value_memory(&v);
709
- VALUE value = native_slot_get(self->value_type,
710
- self->value_type_class,
711
- mem);
712
-
713
- if (!first) {
714
- str = rb_str_cat2(str, ", ");
715
- } else {
716
- first = false;
717
- }
718
- str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
719
- str = rb_str_cat2(str, "=>");
720
- str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
721
- }
722
-
723
- str = rb_str_cat2(str, "}");
724
- return str;
646
+ StringBuilder* builder = StringBuilder_New();
647
+ Map_Inspect(builder, self->map, self->key_type, self->value_type_info);
648
+ VALUE ret = StringBuilder_ToRubyString(builder);
649
+ StringBuilder_Free(builder);
650
+ return ret;
725
651
  }
726
652
 
727
653
  /*
@@ -733,92 +659,16 @@ VALUE Map_inspect(VALUE _self) {
733
659
  * in the new copy of this map. Returns the new copy of this map with merged
734
660
  * contents.
735
661
  */
736
- VALUE Map_merge(VALUE _self, VALUE hashmap) {
662
+ static VALUE Map_merge(VALUE _self, VALUE hashmap) {
737
663
  VALUE dupped = Map_dup(_self);
738
664
  return Map_merge_into_self(dupped, hashmap);
739
665
  }
740
666
 
741
- static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
742
- Map_index_set(self, key, value);
743
- return ST_CONTINUE;
744
- }
745
-
746
- // Used only internally -- shared by #merge and #initialize.
747
- VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
748
- if (TYPE(hashmap) == T_HASH) {
749
- rb_hash_foreach(hashmap, merge_into_self_callback, _self);
750
- } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
751
- RTYPEDDATA_TYPE(hashmap) == &Map_type) {
752
-
753
- Map* self = ruby_to_Map(_self);
754
- Map* other = ruby_to_Map(hashmap);
755
- upb_strtable_iter it;
756
-
757
- if (self->key_type != other->key_type ||
758
- self->value_type != other->value_type ||
759
- self->value_type_class != other->value_type_class) {
760
- rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
761
- }
762
-
763
- for (upb_strtable_begin(&it, &other->table);
764
- !upb_strtable_done(&it);
765
- upb_strtable_next(&it)) {
766
-
767
- // Replace any existing value by issuing a 'remove' operation first.
768
- upb_value v;
769
- upb_value oldv;
770
- upb_strtable_remove2(&self->table,
771
- upb_strtable_iter_key(&it),
772
- upb_strtable_iter_keylength(&it),
773
- &oldv);
774
-
775
- v = upb_strtable_iter_value(&it);
776
- upb_strtable_insert2(&self->table,
777
- upb_strtable_iter_key(&it),
778
- upb_strtable_iter_keylength(&it),
779
- v);
780
- }
781
- } else {
782
- rb_raise(rb_eArgError, "Unknown type merging into Map");
783
- }
784
- return _self;
785
- }
786
-
787
- // Internal method: map iterator initialization (used for serialization).
788
- void Map_begin(VALUE _self, Map_iter* iter) {
789
- Map* self = ruby_to_Map(_self);
790
- iter->self = self;
791
- upb_strtable_begin(&iter->it, &self->table);
792
- }
793
-
794
- void Map_next(Map_iter* iter) {
795
- upb_strtable_next(&iter->it);
796
- }
797
-
798
- bool Map_done(Map_iter* iter) {
799
- return upb_strtable_done(&iter->it);
800
- }
801
-
802
- VALUE Map_iter_key(Map_iter* iter) {
803
- return table_key_to_ruby(
804
- iter->self,
805
- upb_strtable_iter_key(&iter->it),
806
- upb_strtable_iter_keylength(&iter->it));
807
- }
808
-
809
- VALUE Map_iter_value(Map_iter* iter) {
810
- upb_value v = upb_strtable_iter_value(&iter->it);
811
- void* mem = value_memory(&v);
812
- return native_slot_get(iter->self->value_type,
813
- iter->self->value_type_class,
814
- mem);
815
- }
816
-
817
667
  void Map_register(VALUE module) {
818
668
  VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
819
669
  rb_define_alloc_func(klass, Map_alloc);
820
- cMap = klass;
821
670
  rb_gc_register_address(&cMap);
671
+ cMap = klass;
822
672
 
823
673
  rb_define_method(klass, "initialize", Map_init, -1);
824
674
  rb_define_method(klass, "each", Map_each, 0);
@@ -830,10 +680,13 @@ void Map_register(VALUE module) {
830
680
  rb_define_method(klass, "delete", Map_delete, 1);
831
681
  rb_define_method(klass, "clear", Map_clear, 0);
832
682
  rb_define_method(klass, "length", Map_length, 0);
683
+ rb_define_method(klass, "size", Map_length, 0);
833
684
  rb_define_method(klass, "dup", Map_dup, 0);
685
+ // Also define #clone so that we don't inherit Object#clone.
686
+ rb_define_method(klass, "clone", Map_dup, 0);
834
687
  rb_define_method(klass, "==", Map_eq, 1);
688
+ rb_define_method(klass, "freeze", Map_freeze, 0);
835
689
  rb_define_method(klass, "hash", Map_hash, 0);
836
- rb_define_method(klass, "to_hash", Map_to_h, 0);
837
690
  rb_define_method(klass, "to_h", Map_to_h, 0);
838
691
  rb_define_method(klass, "inspect", Map_inspect, 0);
839
692
  rb_define_method(klass, "merge", Map_merge, 1);