google-protobuf 3.13.0 → 3.19.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/convert.c +348 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +466 -1548
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +4 -5
- data/ext/google/protobuf_c/map.c +314 -456
- data/ext/google/protobuf_c/map.h +67 -0
- data/ext/google/protobuf_c/message.c +904 -434
- data/ext/google/protobuf_c/message.h +101 -0
- data/ext/google/protobuf_c/protobuf.c +398 -64
- data/ext/google/protobuf_c/protobuf.h +43 -593
- data/ext/google/protobuf_c/repeated_field.c +294 -321
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +9171 -0
- data/ext/google/protobuf_c/{upb.h → ruby-upb.h} +1922 -3995
- data/lib/google/protobuf/api_pb.rb +1 -0
- data/lib/google/protobuf/descriptor_dsl.rb +458 -0
- data/lib/google/protobuf/descriptor_pb.rb +268 -0
- data/lib/google/protobuf/type_pb.rb +1 -0
- data/lib/google/protobuf/well_known_types.rb +5 -0
- data/lib/google/protobuf.rb +1 -69
- data/tests/basic.rb +111 -6
- data/tests/stress.rb +1 -1
- metadata +18 -32
- data/ext/google/protobuf_c/encode_decode.c +0 -1795
- data/ext/google/protobuf_c/storage.c +0 -1198
- data/ext/google/protobuf_c/upb.c +0 -13817
data/ext/google/protobuf_c/map.c
CHANGED
@@ -28,170 +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
|
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
|
-
if (TYPE(key) == T_SYMBOL) {
|
75
|
-
key = rb_id2str(SYM2ID(key));
|
76
|
-
}
|
77
|
-
Check_Type(key, T_STRING);
|
78
|
-
key = native_slot_encode_and_freeze_string(self->key_type, key);
|
79
|
-
*out_key = RSTRING_PTR(key);
|
80
|
-
*out_length = RSTRING_LEN(key);
|
81
|
-
break;
|
82
|
-
|
83
|
-
case UPB_TYPE_BOOL:
|
84
|
-
case UPB_TYPE_INT32:
|
85
|
-
case UPB_TYPE_INT64:
|
86
|
-
case UPB_TYPE_UINT32:
|
87
|
-
case UPB_TYPE_UINT64:
|
88
|
-
native_slot_set("", self->key_type, Qnil, buf, key);
|
89
|
-
*out_key = buf;
|
90
|
-
*out_length = native_slot_size(self->key_type);
|
91
|
-
break;
|
92
|
-
|
93
|
-
default:
|
94
|
-
// Map constructor should not allow a Map with another key type to be
|
95
|
-
// constructed.
|
96
|
-
assert(false);
|
97
|
-
break;
|
98
|
-
}
|
99
|
-
|
100
|
-
return key;
|
101
|
-
}
|
102
|
-
|
103
|
-
static VALUE table_key_to_ruby(Map* self, upb_strview key) {
|
104
|
-
switch (self->key_type) {
|
105
|
-
case UPB_TYPE_BYTES:
|
106
|
-
case UPB_TYPE_STRING: {
|
107
|
-
VALUE ret = rb_str_new(key.data, key.size);
|
108
|
-
rb_enc_associate(ret,
|
109
|
-
(self->key_type == UPB_TYPE_BYTES) ?
|
110
|
-
kRubyString8bitEncoding : kRubyStringUtf8Encoding);
|
111
|
-
return ret;
|
112
|
-
}
|
113
|
-
|
114
|
-
case UPB_TYPE_BOOL:
|
115
|
-
case UPB_TYPE_INT32:
|
116
|
-
case UPB_TYPE_INT64:
|
117
|
-
case UPB_TYPE_UINT32:
|
118
|
-
case UPB_TYPE_UINT64:
|
119
|
-
return native_slot_get(self->key_type, Qnil, key.data);
|
120
|
-
|
121
|
-
default:
|
122
|
-
assert(false);
|
123
|
-
return Qnil;
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
static void* value_memory(upb_value* v) {
|
128
|
-
return (void*)(&v->val);
|
129
|
-
}
|
130
|
-
|
131
46
|
// -----------------------------------------------------------------------------
|
132
47
|
// Map container type.
|
133
48
|
// -----------------------------------------------------------------------------
|
134
49
|
|
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;
|
57
|
+
|
58
|
+
static void Map_mark(void* _self) {
|
59
|
+
Map* self = _self;
|
60
|
+
rb_gc_mark(self->value_type_class);
|
61
|
+
rb_gc_mark(self->arena);
|
62
|
+
}
|
63
|
+
|
135
64
|
const rb_data_type_t Map_type = {
|
136
65
|
"Google::Protobuf::Map",
|
137
|
-
{ Map_mark,
|
66
|
+
{ Map_mark, RUBY_DEFAULT_FREE, NULL },
|
67
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
138
68
|
};
|
139
69
|
|
140
70
|
VALUE cMap;
|
141
71
|
|
142
|
-
Map* ruby_to_Map(VALUE _self) {
|
72
|
+
static Map* ruby_to_Map(VALUE _self) {
|
143
73
|
Map* self;
|
144
74
|
TypedData_Get_Struct(_self, Map, &Map_type, self);
|
145
75
|
return self;
|
146
76
|
}
|
147
77
|
|
148
|
-
|
149
|
-
Map* self =
|
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
|
+
}
|
150
86
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
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);
|
164
105
|
}
|
165
106
|
}
|
107
|
+
|
108
|
+
return val;
|
166
109
|
}
|
167
110
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
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;
|
172
119
|
}
|
173
120
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
return
|
121
|
+
static TypeInfo Map_keyinfo(Map* self) {
|
122
|
+
TypeInfo ret;
|
123
|
+
ret.type = self->key_type;
|
124
|
+
ret.def.msgdef = NULL;
|
125
|
+
return ret;
|
179
126
|
}
|
180
127
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
return val;
|
128
|
+
static upb_map *Map_GetMutable(VALUE _self) {
|
129
|
+
rb_check_frozen(_self);
|
130
|
+
return (upb_map*)ruby_to_Map(_self)->map;
|
185
131
|
}
|
186
132
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
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);
|
138
|
+
|
139
|
+
if (!map) return hash;
|
140
|
+
|
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;
|
150
|
+
}
|
151
|
+
|
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
|
+
}
|
165
|
+
|
166
|
+
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
167
|
+
new_arena_rb);
|
168
|
+
}
|
169
|
+
|
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");
|
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;
|
195
|
+
}
|
196
|
+
|
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, "}");
|
218
|
+
}
|
219
|
+
|
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;
|
227
|
+
}
|
228
|
+
|
229
|
+
// Used only internally -- shared by #merge and #initialize.
|
230
|
+
static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
231
|
+
if (TYPE(hashmap) == T_HASH) {
|
232
|
+
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
233
|
+
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
234
|
+
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
235
|
+
Map* self = ruby_to_Map(_self);
|
236
|
+
Map* other = ruby_to_Map(hashmap);
|
237
|
+
upb_arena *arena = Arena_get(self->arena);
|
238
|
+
upb_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");
|
194
256
|
}
|
257
|
+
return _self;
|
195
258
|
}
|
196
259
|
|
197
260
|
/*
|
@@ -224,9 +287,9 @@ static bool needs_typeclass(upb_fieldtype_t type) {
|
|
224
287
|
* references to underlying objects will be shared if the value type is a
|
225
288
|
* message type.
|
226
289
|
*/
|
227
|
-
VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
290
|
+
static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
228
291
|
Map* self = ruby_to_Map(_self);
|
229
|
-
|
292
|
+
VALUE init_arg;
|
230
293
|
|
231
294
|
// We take either two args (:key_type, :value_type), three args (:key_type,
|
232
295
|
// :value_type, "ValueMessageType"), or four args (the above plus an initial
|
@@ -236,8 +299,9 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
236
299
|
}
|
237
300
|
|
238
301
|
self->key_type = ruby_to_fieldtype(argv[0]);
|
239
|
-
self->
|
240
|
-
|
302
|
+
self->value_type_info =
|
303
|
+
TypeInfo_FromClass(argc, argv, 1, &self->value_type_class, &init_arg);
|
304
|
+
self->arena = Arena_new();
|
241
305
|
|
242
306
|
// Check that the key type is an allowed type.
|
243
307
|
switch (self->key_type) {
|
@@ -254,21 +318,12 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
254
318
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
255
319
|
}
|
256
320
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
validate_type_class(self->value_type, self->value_type_class);
|
261
|
-
init_value_arg = 3;
|
262
|
-
}
|
263
|
-
|
264
|
-
// Table value type is always UINT64: this ensures enough space to store the
|
265
|
-
// native_slot value.
|
266
|
-
if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
|
267
|
-
rb_raise(rb_eRuntimeError, "Could not allocate table.");
|
268
|
-
}
|
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);
|
269
324
|
|
270
|
-
if (
|
271
|
-
Map_merge_into_self(_self,
|
325
|
+
if (init_arg != Qnil) {
|
326
|
+
Map_merge_into_self(_self, init_arg);
|
272
327
|
}
|
273
328
|
|
274
329
|
return Qnil;
|
@@ -282,22 +337,16 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
282
337
|
* Note that Map also includes Enumerable; map thus acts like a normal Ruby
|
283
338
|
* sequence.
|
284
339
|
*/
|
285
|
-
VALUE Map_each(VALUE _self) {
|
340
|
+
static VALUE Map_each(VALUE _self) {
|
286
341
|
Map* self = ruby_to_Map(_self);
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
VALUE
|
293
|
-
|
294
|
-
|
295
|
-
void* mem = value_memory(&v);
|
296
|
-
VALUE value = native_slot_get(self->value_type,
|
297
|
-
self->value_type_class,
|
298
|
-
mem);
|
299
|
-
|
300
|
-
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);
|
301
350
|
}
|
302
351
|
|
303
352
|
return Qnil;
|
@@ -309,17 +358,15 @@ VALUE Map_each(VALUE _self) {
|
|
309
358
|
*
|
310
359
|
* Returns the list of keys contained in the map, in unspecified order.
|
311
360
|
*/
|
312
|
-
VALUE Map_keys(VALUE _self) {
|
361
|
+
static VALUE Map_keys(VALUE _self) {
|
313
362
|
Map* self = ruby_to_Map(_self);
|
314
|
-
|
363
|
+
size_t iter = UPB_MAP_BEGIN;
|
315
364
|
VALUE ret = rb_ary_new();
|
316
|
-
upb_strtable_iter it;
|
317
|
-
for (upb_strtable_begin(&it, &self->table);
|
318
|
-
!upb_strtable_done(&it);
|
319
|
-
upb_strtable_next(&it)) {
|
320
|
-
VALUE key = table_key_to_ruby(self, upb_strtable_iter_key(&it));
|
321
365
|
|
322
|
-
|
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);
|
323
370
|
}
|
324
371
|
|
325
372
|
return ret;
|
@@ -331,22 +378,15 @@ VALUE Map_keys(VALUE _self) {
|
|
331
378
|
*
|
332
379
|
* Returns the list of values contained in the map, in unspecified order.
|
333
380
|
*/
|
334
|
-
VALUE Map_values(VALUE _self) {
|
381
|
+
static VALUE Map_values(VALUE _self) {
|
335
382
|
Map* self = ruby_to_Map(_self);
|
336
|
-
|
383
|
+
size_t iter = UPB_MAP_BEGIN;
|
337
384
|
VALUE ret = rb_ary_new();
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
upb_value v = upb_strtable_iter_value(&it);
|
344
|
-
void* mem = value_memory(&v);
|
345
|
-
VALUE value = native_slot_get(self->value_type,
|
346
|
-
self->value_type_class,
|
347
|
-
mem);
|
348
|
-
|
349
|
-
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);
|
350
390
|
}
|
351
391
|
|
352
392
|
return ret;
|
@@ -359,18 +399,13 @@ VALUE Map_values(VALUE _self) {
|
|
359
399
|
* Accesses the element at the given key. Throws an exception if the key type is
|
360
400
|
* incorrect. Returns nil when the key is not present in the map.
|
361
401
|
*/
|
362
|
-
VALUE Map_index(VALUE _self, VALUE key) {
|
402
|
+
static VALUE Map_index(VALUE _self, VALUE key) {
|
363
403
|
Map* self = ruby_to_Map(_self);
|
404
|
+
upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
405
|
+
upb_msgval val;
|
364
406
|
|
365
|
-
|
366
|
-
|
367
|
-
size_t length = 0;
|
368
|
-
upb_value v;
|
369
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
370
|
-
|
371
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
|
372
|
-
void* mem = value_memory(&v);
|
373
|
-
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);
|
374
409
|
} else {
|
375
410
|
return Qnil;
|
376
411
|
}
|
@@ -384,33 +419,15 @@ VALUE Map_index(VALUE _self, VALUE key) {
|
|
384
419
|
* Throws an exception if the key type is incorrect. Returns the new value that
|
385
420
|
* was just inserted.
|
386
421
|
*/
|
387
|
-
VALUE Map_index_set(VALUE _self, VALUE key, VALUE
|
422
|
+
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
388
423
|
Map* self = ruby_to_Map(_self);
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
upb_value v;
|
393
|
-
void* mem;
|
394
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
395
|
-
|
396
|
-
rb_check_frozen(_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);
|
397
427
|
|
398
|
-
|
399
|
-
VALUE args[1] = { value };
|
400
|
-
value = rb_class_new_instance(1, args, self->value_type_class);
|
401
|
-
}
|
402
|
-
|
403
|
-
mem = value_memory(&v);
|
404
|
-
native_slot_set("", self->value_type, self->value_type_class, mem, value);
|
405
|
-
|
406
|
-
// Replace any existing value by issuing a 'remove' operation first.
|
407
|
-
upb_strtable_remove2(&self->table, keyval, length, NULL);
|
408
|
-
if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
|
409
|
-
rb_raise(rb_eRuntimeError, "Could not insert into table");
|
410
|
-
}
|
428
|
+
upb_map_set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
411
429
|
|
412
|
-
|
413
|
-
return value;
|
430
|
+
return val;
|
414
431
|
}
|
415
432
|
|
416
433
|
/*
|
@@ -420,15 +437,11 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
|
|
420
437
|
* Returns true if the given key is present in the map. Throws an exception if
|
421
438
|
* the key has the wrong type.
|
422
439
|
*/
|
423
|
-
VALUE Map_has_key(VALUE _self, VALUE key) {
|
440
|
+
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
424
441
|
Map* self = ruby_to_Map(_self);
|
442
|
+
upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
425
443
|
|
426
|
-
|
427
|
-
const char* keyval = NULL;
|
428
|
-
size_t length = 0;
|
429
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
430
|
-
|
431
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
|
444
|
+
if (upb_map_get(self->map, key_upb, NULL)) {
|
432
445
|
return Qtrue;
|
433
446
|
} else {
|
434
447
|
return Qfalse;
|
@@ -442,22 +455,25 @@ VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
442
455
|
* Deletes the value at the given key, if any, returning either the old value or
|
443
456
|
* nil if none was present. Throws an exception if the key is of the wrong type.
|
444
457
|
*/
|
445
|
-
VALUE Map_delete(VALUE _self, VALUE key) {
|
458
|
+
static VALUE Map_delete(VALUE _self, VALUE key) {
|
446
459
|
Map* self = ruby_to_Map(_self);
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
upb_value v;
|
451
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
460
|
+
upb_msgval key_upb = Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
461
|
+
upb_msgval val_upb;
|
462
|
+
VALUE ret;
|
452
463
|
|
453
464
|
rb_check_frozen(_self);
|
454
465
|
|
455
|
-
|
456
|
-
|
457
|
-
|
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);
|
458
470
|
} else {
|
459
|
-
|
471
|
+
ret = Qnil;
|
460
472
|
}
|
473
|
+
|
474
|
+
upb_map_delete(Map_GetMutable(_self), key_upb);
|
475
|
+
|
476
|
+
return ret;
|
461
477
|
}
|
462
478
|
|
463
479
|
/*
|
@@ -466,17 +482,8 @@ VALUE Map_delete(VALUE _self, VALUE key) {
|
|
466
482
|
*
|
467
483
|
* Removes all entries from the map.
|
468
484
|
*/
|
469
|
-
VALUE Map_clear(VALUE _self) {
|
470
|
-
|
471
|
-
|
472
|
-
rb_check_frozen(_self);
|
473
|
-
|
474
|
-
// Uninit and reinit the table -- this is faster than iterating and doing a
|
475
|
-
// delete-lookup on each key.
|
476
|
-
upb_strtable_uninit(&self->table);
|
477
|
-
if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
|
478
|
-
rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
|
479
|
-
}
|
485
|
+
static VALUE Map_clear(VALUE _self) {
|
486
|
+
upb_map_clear(Map_GetMutable(_self));
|
480
487
|
return Qnil;
|
481
488
|
}
|
482
489
|
|
@@ -486,24 +493,9 @@ VALUE Map_clear(VALUE _self) {
|
|
486
493
|
*
|
487
494
|
* Returns the number of entries (key-value pairs) in the map.
|
488
495
|
*/
|
489
|
-
VALUE Map_length(VALUE _self) {
|
496
|
+
static VALUE Map_length(VALUE _self) {
|
490
497
|
Map* self = ruby_to_Map(_self);
|
491
|
-
return ULL2NUM(
|
492
|
-
}
|
493
|
-
|
494
|
-
VALUE Map_new_this_type(VALUE _self) {
|
495
|
-
Map* self = ruby_to_Map(_self);
|
496
|
-
VALUE new_map = Qnil;
|
497
|
-
VALUE key_type = fieldtype_to_ruby(self->key_type);
|
498
|
-
VALUE value_type = fieldtype_to_ruby(self->value_type);
|
499
|
-
if (self->value_type_class != Qnil) {
|
500
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
|
501
|
-
key_type, value_type, self->value_type_class);
|
502
|
-
} else {
|
503
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
|
504
|
-
key_type, value_type);
|
505
|
-
}
|
506
|
-
return new_map;
|
498
|
+
return ULL2NUM(upb_map_size(self->map));
|
507
499
|
}
|
508
500
|
|
509
501
|
/*
|
@@ -513,54 +505,23 @@ VALUE Map_new_this_type(VALUE _self) {
|
|
513
505
|
* Duplicates this map with a shallow copy. References to all non-primitive
|
514
506
|
* element objects (e.g., submessages) are shared.
|
515
507
|
*/
|
516
|
-
VALUE Map_dup(VALUE _self) {
|
517
|
-
Map* self = ruby_to_Map(_self);
|
518
|
-
VALUE new_map = Map_new_this_type(_self);
|
519
|
-
Map* new_self = ruby_to_Map(new_map);
|
520
|
-
|
521
|
-
upb_strtable_iter it;
|
522
|
-
for (upb_strtable_begin(&it, &self->table);
|
523
|
-
!upb_strtable_done(&it);
|
524
|
-
upb_strtable_next(&it)) {
|
525
|
-
upb_strview k = upb_strtable_iter_key(&it);
|
526
|
-
upb_value v = upb_strtable_iter_value(&it);
|
527
|
-
void* mem = value_memory(&v);
|
528
|
-
upb_value dup;
|
529
|
-
void* dup_mem = value_memory(&dup);
|
530
|
-
native_slot_dup(self->value_type, dup_mem, mem);
|
531
|
-
|
532
|
-
if (!upb_strtable_insert2(&new_self->table, k.data, k.size, dup)) {
|
533
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
534
|
-
}
|
535
|
-
}
|
536
|
-
|
537
|
-
return new_map;
|
538
|
-
}
|
539
|
-
|
540
|
-
// Used by Google::Protobuf.deep_copy but not exposed directly.
|
541
|
-
VALUE Map_deep_copy(VALUE _self) {
|
508
|
+
static VALUE Map_dup(VALUE _self) {
|
542
509
|
Map* self = ruby_to_Map(_self);
|
543
|
-
VALUE
|
544
|
-
Map* new_self = ruby_to_Map(
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
native_slot_deep_copy(self->value_type, self->value_type_class, dup_mem,
|
556
|
-
mem);
|
557
|
-
|
558
|
-
if (!upb_strtable_insert2(&new_self->table, k.data, k.size, dup)) {
|
559
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
560
|
-
}
|
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);
|
561
522
|
}
|
562
523
|
|
563
|
-
return
|
524
|
+
return new_map_rb;
|
564
525
|
}
|
565
526
|
|
566
527
|
/*
|
@@ -579,12 +540,11 @@ VALUE Map_deep_copy(VALUE _self) {
|
|
579
540
|
VALUE Map_eq(VALUE _self, VALUE _other) {
|
580
541
|
Map* self = ruby_to_Map(_self);
|
581
542
|
Map* other;
|
582
|
-
upb_strtable_iter it;
|
583
543
|
|
584
544
|
// Allow comparisons to Ruby hashmaps by converting to a temporary Map
|
585
545
|
// instance. Slow, but workable.
|
586
546
|
if (TYPE(_other) == T_HASH) {
|
587
|
-
VALUE other_map = Map_new_this_type(
|
547
|
+
VALUE other_map = Map_new_this_type(self);
|
588
548
|
Map_merge_into_self(other_map, _other);
|
589
549
|
_other = other_map;
|
590
550
|
}
|
@@ -595,33 +555,27 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
595
555
|
return Qtrue;
|
596
556
|
}
|
597
557
|
if (self->key_type != other->key_type ||
|
598
|
-
self->
|
558
|
+
self->value_type_info.type != other->value_type_info.type ||
|
599
559
|
self->value_type_class != other->value_type_class) {
|
600
560
|
return Qfalse;
|
601
561
|
}
|
602
|
-
if (
|
562
|
+
if (upb_map_size(self->map) != upb_map_size(other->map)) {
|
603
563
|
return Qfalse;
|
604
564
|
}
|
605
565
|
|
606
566
|
// For each member of self, check that an equal member exists at the same key
|
607
567
|
// in other.
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
upb_value other_v;
|
615
|
-
void* other_mem = value_memory(&other_v);
|
616
|
-
|
617
|
-
if (!upb_strtable_lookup2(&other->table, k.data, k.size, &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)) {
|
618
574
|
// Not present in other map.
|
619
575
|
return Qfalse;
|
620
576
|
}
|
621
|
-
|
622
|
-
|
623
|
-
other_mem)) {
|
624
|
-
// Present, but value not equal.
|
577
|
+
if (!Msgval_IsEqual(val, other_val, self->value_type_info)) {
|
578
|
+
// Present but different value.
|
625
579
|
return Qfalse;
|
626
580
|
}
|
627
581
|
}
|
@@ -629,6 +583,22 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
629
583
|
return Qtrue;
|
630
584
|
}
|
631
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
|
+
|
632
602
|
/*
|
633
603
|
* call-seq:
|
634
604
|
* Map.hash => hash_value
|
@@ -637,26 +607,18 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
637
607
|
*/
|
638
608
|
VALUE Map_hash(VALUE _self) {
|
639
609
|
Map* self = ruby_to_Map(_self);
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
upb_value v = upb_strtable_iter_value(&it);
|
650
|
-
void* mem = value_memory(&v);
|
651
|
-
VALUE value = native_slot_get(self->value_type,
|
652
|
-
self->value_type_class,
|
653
|
-
mem);
|
654
|
-
|
655
|
-
h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
|
656
|
-
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);
|
657
619
|
}
|
658
620
|
|
659
|
-
return
|
621
|
+
return LL2NUM(hash);
|
660
622
|
}
|
661
623
|
|
662
624
|
/*
|
@@ -667,24 +629,7 @@ VALUE Map_hash(VALUE _self) {
|
|
667
629
|
*/
|
668
630
|
VALUE Map_to_h(VALUE _self) {
|
669
631
|
Map* self = ruby_to_Map(_self);
|
670
|
-
|
671
|
-
upb_strtable_iter it;
|
672
|
-
for (upb_strtable_begin(&it, &self->table);
|
673
|
-
!upb_strtable_done(&it);
|
674
|
-
upb_strtable_next(&it)) {
|
675
|
-
VALUE key = table_key_to_ruby(self, upb_strtable_iter_key(&it));
|
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 (self->value_type == UPB_TYPE_MESSAGE) {
|
683
|
-
value = Message_to_h(value);
|
684
|
-
}
|
685
|
-
rb_hash_aset(hash, key, value);
|
686
|
-
}
|
687
|
-
return hash;
|
632
|
+
return Map_CreateHash(self->map, self->key_type, self->value_type_info);
|
688
633
|
}
|
689
634
|
|
690
635
|
/*
|
@@ -698,34 +643,11 @@ VALUE Map_to_h(VALUE _self) {
|
|
698
643
|
VALUE Map_inspect(VALUE _self) {
|
699
644
|
Map* self = ruby_to_Map(_self);
|
700
645
|
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
upb_strtable_iter it;
|
707
|
-
for (upb_strtable_begin(&it, &self->table); !upb_strtable_done(&it);
|
708
|
-
upb_strtable_next(&it)) {
|
709
|
-
VALUE key = table_key_to_ruby(self, upb_strtable_iter_key(&it));
|
710
|
-
|
711
|
-
upb_value v = upb_strtable_iter_value(&it);
|
712
|
-
void* mem = value_memory(&v);
|
713
|
-
VALUE value = native_slot_get(self->value_type,
|
714
|
-
self->value_type_class,
|
715
|
-
mem);
|
716
|
-
|
717
|
-
if (!first) {
|
718
|
-
str = rb_str_cat2(str, ", ");
|
719
|
-
} else {
|
720
|
-
first = false;
|
721
|
-
}
|
722
|
-
str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
|
723
|
-
str = rb_str_cat2(str, "=>");
|
724
|
-
str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
|
725
|
-
}
|
726
|
-
|
727
|
-
str = rb_str_cat2(str, "}");
|
728
|
-
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;
|
729
651
|
}
|
730
652
|
|
731
653
|
/*
|
@@ -737,79 +659,11 @@ VALUE Map_inspect(VALUE _self) {
|
|
737
659
|
* in the new copy of this map. Returns the new copy of this map with merged
|
738
660
|
* contents.
|
739
661
|
*/
|
740
|
-
VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
662
|
+
static VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
741
663
|
VALUE dupped = Map_dup(_self);
|
742
664
|
return Map_merge_into_self(dupped, hashmap);
|
743
665
|
}
|
744
666
|
|
745
|
-
static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
|
746
|
-
Map_index_set(self, key, value);
|
747
|
-
return ST_CONTINUE;
|
748
|
-
}
|
749
|
-
|
750
|
-
// Used only internally -- shared by #merge and #initialize.
|
751
|
-
VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
752
|
-
if (TYPE(hashmap) == T_HASH) {
|
753
|
-
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
754
|
-
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
755
|
-
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
756
|
-
|
757
|
-
Map* self = ruby_to_Map(_self);
|
758
|
-
Map* other = ruby_to_Map(hashmap);
|
759
|
-
upb_strtable_iter it;
|
760
|
-
|
761
|
-
if (self->key_type != other->key_type ||
|
762
|
-
self->value_type != other->value_type ||
|
763
|
-
self->value_type_class != other->value_type_class) {
|
764
|
-
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
765
|
-
}
|
766
|
-
|
767
|
-
for (upb_strtable_begin(&it, &other->table);
|
768
|
-
!upb_strtable_done(&it);
|
769
|
-
upb_strtable_next(&it)) {
|
770
|
-
upb_strview k = upb_strtable_iter_key(&it);
|
771
|
-
|
772
|
-
// Replace any existing value by issuing a 'remove' operation first.
|
773
|
-
upb_value v;
|
774
|
-
upb_value oldv;
|
775
|
-
upb_strtable_remove2(&self->table, k.data, k.size, &oldv);
|
776
|
-
|
777
|
-
v = upb_strtable_iter_value(&it);
|
778
|
-
upb_strtable_insert2(&self->table, k.data, k.size, v);
|
779
|
-
}
|
780
|
-
} else {
|
781
|
-
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
782
|
-
}
|
783
|
-
return _self;
|
784
|
-
}
|
785
|
-
|
786
|
-
// Internal method: map iterator initialization (used for serialization).
|
787
|
-
void Map_begin(VALUE _self, Map_iter* iter) {
|
788
|
-
Map* self = ruby_to_Map(_self);
|
789
|
-
iter->self = self;
|
790
|
-
upb_strtable_begin(&iter->it, &self->table);
|
791
|
-
}
|
792
|
-
|
793
|
-
void Map_next(Map_iter* iter) {
|
794
|
-
upb_strtable_next(&iter->it);
|
795
|
-
}
|
796
|
-
|
797
|
-
bool Map_done(Map_iter* iter) {
|
798
|
-
return upb_strtable_done(&iter->it);
|
799
|
-
}
|
800
|
-
|
801
|
-
VALUE Map_iter_key(Map_iter* iter) {
|
802
|
-
return table_key_to_ruby(iter->self, upb_strtable_iter_key(&iter->it));
|
803
|
-
}
|
804
|
-
|
805
|
-
VALUE Map_iter_value(Map_iter* iter) {
|
806
|
-
upb_value v = upb_strtable_iter_value(&iter->it);
|
807
|
-
void* mem = value_memory(&v);
|
808
|
-
return native_slot_get(iter->self->value_type,
|
809
|
-
iter->self->value_type_class,
|
810
|
-
mem);
|
811
|
-
}
|
812
|
-
|
813
667
|
void Map_register(VALUE module) {
|
814
668
|
VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
|
815
669
|
rb_define_alloc_func(klass, Map_alloc);
|
@@ -826,8 +680,12 @@ void Map_register(VALUE module) {
|
|
826
680
|
rb_define_method(klass, "delete", Map_delete, 1);
|
827
681
|
rb_define_method(klass, "clear", Map_clear, 0);
|
828
682
|
rb_define_method(klass, "length", Map_length, 0);
|
683
|
+
rb_define_method(klass, "size", Map_length, 0);
|
829
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);
|
830
687
|
rb_define_method(klass, "==", Map_eq, 1);
|
688
|
+
rb_define_method(klass, "freeze", Map_freeze, 0);
|
831
689
|
rb_define_method(klass, "hash", Map_hash, 0);
|
832
690
|
rb_define_method(klass, "to_h", Map_to_h, 0);
|
833
691
|
rb_define_method(klass, "inspect", Map_inspect, 0);
|