google-protobuf 3.11.1 → 3.25.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +314 -0
- data/ext/google/protobuf_c/convert.h +50 -0
- data/ext/google/protobuf_c/defs.c +745 -1620
- data/ext/google/protobuf_c/defs.h +82 -0
- data/ext/google/protobuf_c/extconf.rb +15 -8
- data/ext/google/protobuf_c/glue.c +56 -0
- data/ext/google/protobuf_c/map.c +336 -512
- data/ext/google/protobuf_c/map.h +44 -0
- data/ext/google/protobuf_c/message.c +1096 -520
- data/ext/google/protobuf_c/message.h +86 -0
- data/ext/google/protobuf_c/protobuf.c +301 -94
- data/ext/google/protobuf_c/protobuf.h +66 -620
- data/ext/google/protobuf_c/repeated_field.c +323 -353
- data/ext/google/protobuf_c/repeated_field.h +41 -0
- data/ext/google/protobuf_c/ruby-upb.c +14414 -0
- data/ext/google/protobuf_c/ruby-upb.h +13044 -0
- data/ext/google/protobuf_c/shared_convert.c +64 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +65 -0
- data/ext/google/protobuf_c/shared_message.h +25 -0
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
- data/lib/google/protobuf/any_pb.rb +24 -5
- data/lib/google/protobuf/api_pb.rb +27 -23
- data/lib/google/protobuf/descriptor_dsl.rb +465 -0
- data/lib/google/protobuf/descriptor_pb.rb +86 -0
- data/lib/google/protobuf/duration_pb.rb +24 -5
- data/lib/google/protobuf/empty_pb.rb +24 -3
- data/lib/google/protobuf/ffi/descriptor.rb +165 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +75 -0
- data/lib/google/protobuf/ffi/enum_descriptor.rb +171 -0
- data/lib/google/protobuf/ffi/ffi.rb +213 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +319 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
- data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
- data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
- data/lib/google/protobuf/ffi/map.rb +407 -0
- data/lib/google/protobuf/ffi/message.rb +662 -0
- data/lib/google/protobuf/ffi/object_cache.rb +30 -0
- data/lib/google/protobuf/ffi/oneof_descriptor.rb +95 -0
- data/lib/google/protobuf/ffi/repeated_field.rb +383 -0
- data/lib/google/protobuf/field_mask_pb.rb +24 -4
- data/lib/google/protobuf/message_exts.rb +10 -28
- data/lib/google/protobuf/object_cache.rb +97 -0
- data/lib/google/protobuf/plugin_pb.rb +47 -0
- data/lib/google/protobuf/repeated_field.rb +18 -28
- data/lib/google/protobuf/source_context_pb.rb +24 -4
- data/lib/google/protobuf/struct_pb.rb +24 -20
- data/lib/google/protobuf/timestamp_pb.rb +24 -5
- data/lib/google/protobuf/type_pb.rb +27 -68
- data/lib/google/protobuf/well_known_types.rb +17 -36
- data/lib/google/protobuf/wrappers_pb.rb +24 -28
- data/lib/google/protobuf.rb +32 -118
- data/lib/google/protobuf_ffi.rb +50 -0
- data/lib/google/protobuf_native.rb +20 -0
- data/lib/google/tasks/ffi.rake +102 -0
- metadata +92 -32
- data/ext/google/protobuf_c/encode_decode.c +0 -1758
- data/ext/google/protobuf_c/storage.c +0 -1149
- data/ext/google/protobuf_c/upb.c +0 -13289
- data/ext/google/protobuf_c/upb.h +0 -7085
- data/tests/basic.rb +0 -445
- data/tests/generated_code_test.rb +0 -23
- data/tests/stress.rb +0 -38
data/ext/google/protobuf_c/map.c
CHANGED
@@ -1,197 +1,236 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2014 Google Inc. All rights reserved.
|
3
|
-
// https://developers.google.com/protocol-buffers/
|
4
3
|
//
|
5
|
-
//
|
6
|
-
//
|
7
|
-
//
|
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.
|
4
|
+
// Use of this source code is governed by a BSD-style
|
5
|
+
// license that can be found in the LICENSE file or at
|
6
|
+
// https://developers.google.com/open-source/licenses/bsd
|
30
7
|
|
8
|
+
#include "convert.h"
|
9
|
+
#include "defs.h"
|
10
|
+
#include "message.h"
|
31
11
|
#include "protobuf.h"
|
32
12
|
|
33
13
|
// -----------------------------------------------------------------------------
|
34
|
-
// Basic map operations on top of
|
14
|
+
// Basic map operations on top of upb_Map.
|
35
15
|
//
|
36
16
|
// Note that we roll our own `Map` container here because, as for
|
37
17
|
// `RepeatedField`, we want a strongly-typed container. This is so that any user
|
38
18
|
// errors due to incorrect map key or value types are raised as close as
|
39
19
|
// possible to the error site, rather than at some deferred point (e.g.,
|
40
20
|
// 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
21
|
// -----------------------------------------------------------------------------
|
47
22
|
|
48
|
-
//
|
49
|
-
//
|
50
|
-
//
|
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;
|
23
|
+
// -----------------------------------------------------------------------------
|
24
|
+
// Map container type.
|
25
|
+
// -----------------------------------------------------------------------------
|
82
26
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
*out_length = native_slot_size(self->key_type);
|
91
|
-
break;
|
27
|
+
typedef struct {
|
28
|
+
const upb_Map* map; // Can convert to mutable when non-frozen.
|
29
|
+
upb_CType key_type;
|
30
|
+
TypeInfo value_type_info;
|
31
|
+
VALUE value_type_class;
|
32
|
+
VALUE arena;
|
33
|
+
} Map;
|
92
34
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
35
|
+
static void Map_mark(void* _self) {
|
36
|
+
Map* self = _self;
|
37
|
+
rb_gc_mark(self->value_type_class);
|
38
|
+
rb_gc_mark(self->arena);
|
39
|
+
}
|
40
|
+
|
41
|
+
const rb_data_type_t Map_type = {
|
42
|
+
"Google::Protobuf::Map",
|
43
|
+
{Map_mark, RUBY_DEFAULT_FREE, NULL},
|
44
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
45
|
+
};
|
99
46
|
|
100
|
-
|
47
|
+
VALUE cMap;
|
48
|
+
|
49
|
+
static Map* ruby_to_Map(VALUE _self) {
|
50
|
+
Map* self;
|
51
|
+
TypedData_Get_Struct(_self, Map, &Map_type, self);
|
52
|
+
return self;
|
101
53
|
}
|
102
54
|
|
103
|
-
static VALUE
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
return ret;
|
112
|
-
}
|
55
|
+
static VALUE Map_alloc(VALUE klass) {
|
56
|
+
Map* self = ALLOC(Map);
|
57
|
+
self->map = NULL;
|
58
|
+
self->value_type_class = Qnil;
|
59
|
+
self->value_type_info.def.msgdef = NULL;
|
60
|
+
self->arena = Qnil;
|
61
|
+
return TypedData_Wrap_Struct(klass, &Map_type, self);
|
62
|
+
}
|
113
63
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
case UPB_TYPE_UINT32:
|
118
|
-
case UPB_TYPE_UINT64:
|
119
|
-
return native_slot_get(self->key_type, Qnil, buf);
|
64
|
+
VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
65
|
+
VALUE arena) {
|
66
|
+
PBRUBY_ASSERT(map);
|
120
67
|
|
121
|
-
|
122
|
-
|
123
|
-
|
68
|
+
VALUE val = ObjectCache_Get(map);
|
69
|
+
|
70
|
+
if (val == Qnil) {
|
71
|
+
val = Map_alloc(cMap);
|
72
|
+
Map* self;
|
73
|
+
TypedData_Get_Struct(val, Map, &Map_type, self);
|
74
|
+
self->map = map;
|
75
|
+
self->arena = arena;
|
76
|
+
self->key_type = key_type;
|
77
|
+
self->value_type_info = value_type;
|
78
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
79
|
+
const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
|
80
|
+
self->value_type_class = Descriptor_DefToClass(val_m);
|
81
|
+
}
|
82
|
+
return ObjectCache_TryAdd(map, val);
|
124
83
|
}
|
84
|
+
|
85
|
+
return val;
|
125
86
|
}
|
126
87
|
|
127
|
-
static
|
128
|
-
|
88
|
+
static VALUE Map_new_this_type(Map* from) {
|
89
|
+
VALUE arena_rb = Arena_new();
|
90
|
+
upb_Map* map = upb_Map_New(Arena_get(arena_rb), from->key_type,
|
91
|
+
from->value_type_info.type);
|
92
|
+
VALUE ret =
|
93
|
+
Map_GetRubyWrapper(map, from->key_type, from->value_type_info, arena_rb);
|
94
|
+
PBRUBY_ASSERT(ruby_to_Map(ret)->value_type_class == from->value_type_class);
|
95
|
+
return ret;
|
129
96
|
}
|
130
97
|
|
131
|
-
|
132
|
-
|
133
|
-
|
98
|
+
static TypeInfo Map_keyinfo(Map* self) {
|
99
|
+
TypeInfo ret;
|
100
|
+
ret.type = self->key_type;
|
101
|
+
ret.def.msgdef = NULL;
|
102
|
+
return ret;
|
103
|
+
}
|
134
104
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
}
|
105
|
+
static upb_Map* Map_GetMutable(VALUE _self) {
|
106
|
+
rb_check_frozen(_self);
|
107
|
+
return (upb_Map*)ruby_to_Map(_self)->map;
|
108
|
+
}
|
139
109
|
|
140
|
-
VALUE
|
110
|
+
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
|
111
|
+
TypeInfo val_info) {
|
112
|
+
VALUE hash = rb_hash_new();
|
113
|
+
TypeInfo key_info = TypeInfo_from_type(key_type);
|
141
114
|
|
142
|
-
|
143
|
-
Map* self;
|
144
|
-
TypedData_Get_Struct(_self, Map, &Map_type, self);
|
145
|
-
return self;
|
146
|
-
}
|
115
|
+
if (!map) return hash;
|
147
116
|
|
148
|
-
|
149
|
-
|
117
|
+
size_t iter = kUpb_Map_Begin;
|
118
|
+
upb_MessageValue key, val;
|
119
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
120
|
+
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil);
|
121
|
+
VALUE val_val = Scalar_CreateHash(val, val_info);
|
122
|
+
rb_hash_aset(hash, key_val, val_val);
|
123
|
+
}
|
150
124
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
125
|
+
return hash;
|
126
|
+
}
|
127
|
+
|
128
|
+
VALUE Map_deep_copy(VALUE obj) {
|
129
|
+
Map* self = ruby_to_Map(obj);
|
130
|
+
VALUE new_arena_rb = Arena_new();
|
131
|
+
upb_Arena* arena = Arena_get(new_arena_rb);
|
132
|
+
upb_Map* new_map =
|
133
|
+
upb_Map_New(arena, self->key_type, self->value_type_info.type);
|
134
|
+
size_t iter = kUpb_Map_Begin;
|
135
|
+
upb_MessageValue key, val;
|
136
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
137
|
+
upb_MessageValue val_copy =
|
138
|
+
Msgval_DeepCopy(val, self->value_type_info, arena);
|
139
|
+
upb_Map_Set(new_map, key, val_copy, arena);
|
165
140
|
}
|
141
|
+
|
142
|
+
return Map_GetRubyWrapper(new_map, self->key_type, self->value_type_info,
|
143
|
+
new_arena_rb);
|
166
144
|
}
|
167
145
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
146
|
+
const upb_Map* Map_GetUpbMap(VALUE val, const upb_FieldDef* field,
|
147
|
+
upb_Arena* arena) {
|
148
|
+
const upb_FieldDef* key_field = map_field_key(field);
|
149
|
+
const upb_FieldDef* value_field = map_field_value(field);
|
150
|
+
TypeInfo value_type_info = TypeInfo_get(value_field);
|
151
|
+
Map* self;
|
152
|
+
|
153
|
+
if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
|
154
|
+
RTYPEDDATA_TYPE(val) != &Map_type) {
|
155
|
+
rb_raise(cTypeError, "Expected Map instance");
|
156
|
+
}
|
157
|
+
|
158
|
+
self = ruby_to_Map(val);
|
159
|
+
if (self->key_type != upb_FieldDef_CType(key_field)) {
|
160
|
+
rb_raise(cTypeError, "Map key type does not match field's key type");
|
161
|
+
}
|
162
|
+
if (self->value_type_info.type != value_type_info.type) {
|
163
|
+
rb_raise(cTypeError, "Map value type does not match field's value type");
|
164
|
+
}
|
165
|
+
if (self->value_type_info.def.msgdef != value_type_info.def.msgdef) {
|
166
|
+
rb_raise(cTypeError, "Map value type has wrong message/enum class");
|
167
|
+
}
|
168
|
+
|
169
|
+
Arena_fuse(self->arena, arena);
|
170
|
+
return self->map;
|
172
171
|
}
|
173
172
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
173
|
+
void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type,
|
174
|
+
TypeInfo val_type) {
|
175
|
+
bool first = true;
|
176
|
+
TypeInfo key_type_info = {key_type};
|
177
|
+
StringBuilder_Printf(b, "{");
|
178
|
+
if (map) {
|
179
|
+
size_t iter = kUpb_Map_Begin;
|
180
|
+
upb_MessageValue key, val;
|
181
|
+
while (upb_Map_Next(map, &key, &val, &iter)) {
|
182
|
+
if (first) {
|
183
|
+
first = false;
|
184
|
+
} else {
|
185
|
+
StringBuilder_Printf(b, ", ");
|
186
|
+
}
|
187
|
+
StringBuilder_PrintMsgval(b, key, key_type_info);
|
188
|
+
StringBuilder_Printf(b, "=>");
|
189
|
+
StringBuilder_PrintMsgval(b, val, val_type);
|
190
|
+
}
|
191
|
+
}
|
192
|
+
StringBuilder_Printf(b, "}");
|
179
193
|
}
|
180
194
|
|
181
|
-
|
182
|
-
Map* self = ruby_to_Map(
|
183
|
-
|
184
|
-
|
195
|
+
static int merge_into_self_callback(VALUE key, VALUE val, VALUE _self) {
|
196
|
+
Map* self = ruby_to_Map(_self);
|
197
|
+
upb_Arena* arena = Arena_get(self->arena);
|
198
|
+
upb_MessageValue key_val =
|
199
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), arena);
|
200
|
+
upb_MessageValue val_val =
|
201
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
202
|
+
upb_Map_Set(Map_GetMutable(_self), key_val, val_val, arena);
|
203
|
+
return ST_CONTINUE;
|
185
204
|
}
|
186
205
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
206
|
+
// Used only internally -- shared by #merge and #initialize.
|
207
|
+
static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
208
|
+
if (TYPE(hashmap) == T_HASH) {
|
209
|
+
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
210
|
+
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
211
|
+
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
212
|
+
Map* self = ruby_to_Map(_self);
|
213
|
+
Map* other = ruby_to_Map(hashmap);
|
214
|
+
upb_Arena* arena = Arena_get(self->arena);
|
215
|
+
upb_Message* self_msg = Map_GetMutable(_self);
|
216
|
+
|
217
|
+
Arena_fuse(other->arena, arena);
|
218
|
+
|
219
|
+
if (self->key_type != other->key_type ||
|
220
|
+
self->value_type_info.type != other->value_type_info.type ||
|
221
|
+
self->value_type_class != other->value_type_class) {
|
222
|
+
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
223
|
+
}
|
224
|
+
|
225
|
+
size_t iter = kUpb_Map_Begin;
|
226
|
+
upb_MessageValue key, val;
|
227
|
+
while (upb_Map_Next(other->map, &key, &val, &iter)) {
|
228
|
+
upb_Map_Set(self_msg, key, val, arena);
|
229
|
+
}
|
230
|
+
} else {
|
231
|
+
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
194
232
|
}
|
233
|
+
return _self;
|
195
234
|
}
|
196
235
|
|
197
236
|
/*
|
@@ -224,9 +263,9 @@ static bool needs_typeclass(upb_fieldtype_t type) {
|
|
224
263
|
* references to underlying objects will be shared if the value type is a
|
225
264
|
* message type.
|
226
265
|
*/
|
227
|
-
VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
266
|
+
static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
228
267
|
Map* self = ruby_to_Map(_self);
|
229
|
-
|
268
|
+
VALUE init_arg;
|
230
269
|
|
231
270
|
// We take either two args (:key_type, :value_type), three args (:key_type,
|
232
271
|
// :value_type, "ValueMessageType"), or four args (the above plus an initial
|
@@ -236,39 +275,33 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
236
275
|
}
|
237
276
|
|
238
277
|
self->key_type = ruby_to_fieldtype(argv[0]);
|
239
|
-
self->
|
240
|
-
|
278
|
+
self->value_type_info =
|
279
|
+
TypeInfo_FromClass(argc, argv, 1, &self->value_type_class, &init_arg);
|
280
|
+
self->arena = Arena_new();
|
241
281
|
|
242
282
|
// Check that the key type is an allowed type.
|
243
283
|
switch (self->key_type) {
|
244
|
-
case
|
245
|
-
case
|
246
|
-
case
|
247
|
-
case
|
248
|
-
case
|
249
|
-
case
|
250
|
-
case
|
284
|
+
case kUpb_CType_Int32:
|
285
|
+
case kUpb_CType_Int64:
|
286
|
+
case kUpb_CType_UInt32:
|
287
|
+
case kUpb_CType_UInt64:
|
288
|
+
case kUpb_CType_Bool:
|
289
|
+
case kUpb_CType_String:
|
290
|
+
case kUpb_CType_Bytes:
|
251
291
|
// These are OK.
|
252
292
|
break;
|
253
293
|
default:
|
254
294
|
rb_raise(rb_eArgError, "Invalid key type for map.");
|
255
295
|
}
|
256
296
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
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
|
-
}
|
297
|
+
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
298
|
+
self->value_type_info.type);
|
299
|
+
VALUE stored = ObjectCache_TryAdd(self->map, _self);
|
300
|
+
(void)stored;
|
301
|
+
PBRUBY_ASSERT(stored == _self);
|
269
302
|
|
270
|
-
if (
|
271
|
-
Map_merge_into_self(_self,
|
303
|
+
if (init_arg != Qnil) {
|
304
|
+
Map_merge_into_self(_self, init_arg);
|
272
305
|
}
|
273
306
|
|
274
307
|
return Qnil;
|
@@ -282,24 +315,15 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
282
315
|
* Note that Map also includes Enumerable; map thus acts like a normal Ruby
|
283
316
|
* sequence.
|
284
317
|
*/
|
285
|
-
VALUE Map_each(VALUE _self) {
|
318
|
+
static VALUE Map_each(VALUE _self) {
|
286
319
|
Map* self = ruby_to_Map(_self);
|
320
|
+
size_t iter = kUpb_Map_Begin;
|
321
|
+
upb_MessageValue key, val;
|
287
322
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
VALUE key = table_key_to_ruby(
|
294
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
295
|
-
|
296
|
-
upb_value v = upb_strtable_iter_value(&it);
|
297
|
-
void* mem = value_memory(&v);
|
298
|
-
VALUE value = native_slot_get(self->value_type,
|
299
|
-
self->value_type_class,
|
300
|
-
mem);
|
301
|
-
|
302
|
-
rb_yield_values(2, key, value);
|
323
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
324
|
+
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
325
|
+
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
326
|
+
rb_yield_values(2, key_val, val_val);
|
303
327
|
}
|
304
328
|
|
305
329
|
return Qnil;
|
@@ -311,19 +335,15 @@ VALUE Map_each(VALUE _self) {
|
|
311
335
|
*
|
312
336
|
* Returns the list of keys contained in the map, in unspecified order.
|
313
337
|
*/
|
314
|
-
VALUE Map_keys(VALUE _self) {
|
338
|
+
static VALUE Map_keys(VALUE _self) {
|
315
339
|
Map* self = ruby_to_Map(_self);
|
316
|
-
|
340
|
+
size_t iter = kUpb_Map_Begin;
|
317
341
|
VALUE ret = rb_ary_new();
|
318
|
-
|
319
|
-
for (upb_strtable_begin(&it, &self->table);
|
320
|
-
!upb_strtable_done(&it);
|
321
|
-
upb_strtable_next(&it)) {
|
342
|
+
upb_MessageValue key, val;
|
322
343
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
rb_ary_push(ret, key);
|
344
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
345
|
+
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena);
|
346
|
+
rb_ary_push(ret, key_val);
|
327
347
|
}
|
328
348
|
|
329
349
|
return ret;
|
@@ -335,22 +355,15 @@ VALUE Map_keys(VALUE _self) {
|
|
335
355
|
*
|
336
356
|
* Returns the list of values contained in the map, in unspecified order.
|
337
357
|
*/
|
338
|
-
VALUE Map_values(VALUE _self) {
|
358
|
+
static VALUE Map_values(VALUE _self) {
|
339
359
|
Map* self = ruby_to_Map(_self);
|
340
|
-
|
360
|
+
size_t iter = kUpb_Map_Begin;
|
341
361
|
VALUE ret = rb_ary_new();
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
upb_value v = upb_strtable_iter_value(&it);
|
348
|
-
void* mem = value_memory(&v);
|
349
|
-
VALUE value = native_slot_get(self->value_type,
|
350
|
-
self->value_type_class,
|
351
|
-
mem);
|
352
|
-
|
353
|
-
rb_ary_push(ret, value);
|
362
|
+
upb_MessageValue key, val;
|
363
|
+
|
364
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
365
|
+
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
366
|
+
rb_ary_push(ret, val_val);
|
354
367
|
}
|
355
368
|
|
356
369
|
return ret;
|
@@ -363,18 +376,14 @@ VALUE Map_values(VALUE _self) {
|
|
363
376
|
* Accesses the element at the given key. Throws an exception if the key type is
|
364
377
|
* incorrect. Returns nil when the key is not present in the map.
|
365
378
|
*/
|
366
|
-
VALUE Map_index(VALUE _self, VALUE key) {
|
379
|
+
static VALUE Map_index(VALUE _self, VALUE key) {
|
367
380
|
Map* self = ruby_to_Map(_self);
|
381
|
+
upb_MessageValue key_upb =
|
382
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
383
|
+
upb_MessageValue val;
|
368
384
|
|
369
|
-
|
370
|
-
|
371
|
-
size_t length = 0;
|
372
|
-
upb_value v;
|
373
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
374
|
-
|
375
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
|
376
|
-
void* mem = value_memory(&v);
|
377
|
-
return native_slot_get(self->value_type, self->value_type_class, mem);
|
385
|
+
if (upb_Map_Get(self->map, key_upb, &val)) {
|
386
|
+
return Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
378
387
|
} else {
|
379
388
|
return Qnil;
|
380
389
|
}
|
@@ -388,33 +397,17 @@ VALUE Map_index(VALUE _self, VALUE key) {
|
|
388
397
|
* Throws an exception if the key type is incorrect. Returns the new value that
|
389
398
|
* was just inserted.
|
390
399
|
*/
|
391
|
-
VALUE Map_index_set(VALUE _self, VALUE key, VALUE
|
400
|
+
static VALUE Map_index_set(VALUE _self, VALUE key, VALUE val) {
|
392
401
|
Map* self = ruby_to_Map(_self);
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
399
|
-
|
400
|
-
rb_check_frozen(_self);
|
402
|
+
upb_Arena* arena = Arena_get(self->arena);
|
403
|
+
upb_MessageValue key_upb =
|
404
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
405
|
+
upb_MessageValue val_upb =
|
406
|
+
Convert_RubyToUpb(val, "", self->value_type_info, arena);
|
401
407
|
|
402
|
-
|
403
|
-
VALUE args[1] = { value };
|
404
|
-
value = rb_class_new_instance(1, args, self->value_type_class);
|
405
|
-
}
|
406
|
-
|
407
|
-
mem = value_memory(&v);
|
408
|
-
native_slot_set("", self->value_type, self->value_type_class, mem, value);
|
408
|
+
upb_Map_Set(Map_GetMutable(_self), key_upb, val_upb, arena);
|
409
409
|
|
410
|
-
|
411
|
-
upb_strtable_remove2(&self->table, keyval, length, NULL);
|
412
|
-
if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
|
413
|
-
rb_raise(rb_eRuntimeError, "Could not insert into table");
|
414
|
-
}
|
415
|
-
|
416
|
-
// Ruby hashmap's :[]= method also returns the inserted value.
|
417
|
-
return value;
|
410
|
+
return val;
|
418
411
|
}
|
419
412
|
|
420
413
|
/*
|
@@ -424,15 +417,12 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
|
|
424
417
|
* Returns true if the given key is present in the map. Throws an exception if
|
425
418
|
* the key has the wrong type.
|
426
419
|
*/
|
427
|
-
VALUE Map_has_key(VALUE _self, VALUE key) {
|
420
|
+
static VALUE Map_has_key(VALUE _self, VALUE key) {
|
428
421
|
Map* self = ruby_to_Map(_self);
|
422
|
+
upb_MessageValue key_upb =
|
423
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
429
424
|
|
430
|
-
|
431
|
-
const char* keyval = NULL;
|
432
|
-
size_t length = 0;
|
433
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
434
|
-
|
435
|
-
if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
|
425
|
+
if (upb_Map_Get(self->map, key_upb, NULL)) {
|
436
426
|
return Qtrue;
|
437
427
|
} else {
|
438
428
|
return Qfalse;
|
@@ -446,19 +436,16 @@ VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
446
436
|
* Deletes the value at the given key, if any, returning either the old value or
|
447
437
|
* nil if none was present. Throws an exception if the key is of the wrong type.
|
448
438
|
*/
|
449
|
-
VALUE Map_delete(VALUE _self, VALUE key) {
|
439
|
+
static VALUE Map_delete(VALUE _self, VALUE key) {
|
450
440
|
Map* self = ruby_to_Map(_self);
|
451
|
-
char keybuf[TABLE_KEY_BUF_LENGTH];
|
452
|
-
const char* keyval = NULL;
|
453
|
-
size_t length = 0;
|
454
|
-
upb_value v;
|
455
|
-
key = table_key(self, key, keybuf, &keyval, &length);
|
456
|
-
|
457
441
|
rb_check_frozen(_self);
|
458
442
|
|
459
|
-
|
460
|
-
|
461
|
-
|
443
|
+
upb_MessageValue key_upb =
|
444
|
+
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
445
|
+
upb_MessageValue val_upb;
|
446
|
+
|
447
|
+
if (upb_Map_Delete(self->map, key_upb, &val_upb)) {
|
448
|
+
return Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
462
449
|
} else {
|
463
450
|
return Qnil;
|
464
451
|
}
|
@@ -470,17 +457,8 @@ VALUE Map_delete(VALUE _self, VALUE key) {
|
|
470
457
|
*
|
471
458
|
* Removes all entries from the map.
|
472
459
|
*/
|
473
|
-
VALUE Map_clear(VALUE _self) {
|
474
|
-
|
475
|
-
|
476
|
-
rb_check_frozen(_self);
|
477
|
-
|
478
|
-
// Uninit and reinit the table -- this is faster than iterating and doing a
|
479
|
-
// delete-lookup on each key.
|
480
|
-
upb_strtable_uninit(&self->table);
|
481
|
-
if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
|
482
|
-
rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
|
483
|
-
}
|
460
|
+
static VALUE Map_clear(VALUE _self) {
|
461
|
+
upb_Map_Clear(Map_GetMutable(_self));
|
484
462
|
return Qnil;
|
485
463
|
}
|
486
464
|
|
@@ -490,24 +468,9 @@ VALUE Map_clear(VALUE _self) {
|
|
490
468
|
*
|
491
469
|
* Returns the number of entries (key-value pairs) in the map.
|
492
470
|
*/
|
493
|
-
VALUE Map_length(VALUE _self) {
|
494
|
-
Map* self = ruby_to_Map(_self);
|
495
|
-
return ULL2NUM(upb_strtable_count(&self->table));
|
496
|
-
}
|
497
|
-
|
498
|
-
VALUE Map_new_this_type(VALUE _self) {
|
471
|
+
static VALUE Map_length(VALUE _self) {
|
499
472
|
Map* self = ruby_to_Map(_self);
|
500
|
-
|
501
|
-
VALUE key_type = fieldtype_to_ruby(self->key_type);
|
502
|
-
VALUE value_type = fieldtype_to_ruby(self->value_type);
|
503
|
-
if (self->value_type_class != Qnil) {
|
504
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
|
505
|
-
key_type, value_type, self->value_type_class);
|
506
|
-
} else {
|
507
|
-
new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
|
508
|
-
key_type, value_type);
|
509
|
-
}
|
510
|
-
return new_map;
|
473
|
+
return ULL2NUM(upb_Map_Size(self->map));
|
511
474
|
}
|
512
475
|
|
513
476
|
/*
|
@@ -517,60 +480,22 @@ VALUE Map_new_this_type(VALUE _self) {
|
|
517
480
|
* Duplicates this map with a shallow copy. References to all non-primitive
|
518
481
|
* element objects (e.g., submessages) are shared.
|
519
482
|
*/
|
520
|
-
VALUE Map_dup(VALUE _self) {
|
483
|
+
static VALUE Map_dup(VALUE _self) {
|
521
484
|
Map* self = ruby_to_Map(_self);
|
522
|
-
VALUE
|
523
|
-
Map* new_self = ruby_to_Map(
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
!upb_strtable_done(&it);
|
528
|
-
upb_strtable_next(&it)) {
|
529
|
-
|
530
|
-
upb_value v = upb_strtable_iter_value(&it);
|
531
|
-
void* mem = value_memory(&v);
|
532
|
-
upb_value dup;
|
533
|
-
void* dup_mem = value_memory(&dup);
|
534
|
-
native_slot_dup(self->value_type, dup_mem, mem);
|
535
|
-
|
536
|
-
if (!upb_strtable_insert2(&new_self->table,
|
537
|
-
upb_strtable_iter_key(&it),
|
538
|
-
upb_strtable_iter_keylength(&it),
|
539
|
-
dup)) {
|
540
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
541
|
-
}
|
542
|
-
}
|
485
|
+
VALUE new_map_rb = Map_new_this_type(self);
|
486
|
+
Map* new_self = ruby_to_Map(new_map_rb);
|
487
|
+
size_t iter = kUpb_Map_Begin;
|
488
|
+
upb_Arena* arena = Arena_get(new_self->arena);
|
489
|
+
upb_Map* new_map = Map_GetMutable(new_map_rb);
|
543
490
|
|
544
|
-
|
545
|
-
}
|
491
|
+
Arena_fuse(self->arena, arena);
|
546
492
|
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
VALUE new_map = Map_new_this_type(_self);
|
551
|
-
Map* new_self = ruby_to_Map(new_map);
|
552
|
-
|
553
|
-
upb_strtable_iter it;
|
554
|
-
for (upb_strtable_begin(&it, &self->table);
|
555
|
-
!upb_strtable_done(&it);
|
556
|
-
upb_strtable_next(&it)) {
|
557
|
-
|
558
|
-
upb_value v = upb_strtable_iter_value(&it);
|
559
|
-
void* mem = value_memory(&v);
|
560
|
-
upb_value dup;
|
561
|
-
void* dup_mem = value_memory(&dup);
|
562
|
-
native_slot_deep_copy(self->value_type, self->value_type_class, dup_mem,
|
563
|
-
mem);
|
564
|
-
|
565
|
-
if (!upb_strtable_insert2(&new_self->table,
|
566
|
-
upb_strtable_iter_key(&it),
|
567
|
-
upb_strtable_iter_keylength(&it),
|
568
|
-
dup)) {
|
569
|
-
rb_raise(rb_eRuntimeError, "Error inserting value into new table");
|
570
|
-
}
|
493
|
+
upb_MessageValue key, val;
|
494
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
495
|
+
upb_Map_Set(new_map, key, val, arena);
|
571
496
|
}
|
572
497
|
|
573
|
-
return
|
498
|
+
return new_map_rb;
|
574
499
|
}
|
575
500
|
|
576
501
|
/*
|
@@ -589,12 +514,11 @@ VALUE Map_deep_copy(VALUE _self) {
|
|
589
514
|
VALUE Map_eq(VALUE _self, VALUE _other) {
|
590
515
|
Map* self = ruby_to_Map(_self);
|
591
516
|
Map* other;
|
592
|
-
upb_strtable_iter it;
|
593
517
|
|
594
518
|
// Allow comparisons to Ruby hashmaps by converting to a temporary Map
|
595
519
|
// instance. Slow, but workable.
|
596
520
|
if (TYPE(_other) == T_HASH) {
|
597
|
-
VALUE other_map = Map_new_this_type(
|
521
|
+
VALUE other_map = Map_new_this_type(self);
|
598
522
|
Map_merge_into_self(other_map, _other);
|
599
523
|
_other = other_map;
|
600
524
|
}
|
@@ -605,36 +529,26 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
605
529
|
return Qtrue;
|
606
530
|
}
|
607
531
|
if (self->key_type != other->key_type ||
|
608
|
-
self->
|
532
|
+
self->value_type_info.type != other->value_type_info.type ||
|
609
533
|
self->value_type_class != other->value_type_class) {
|
610
534
|
return Qfalse;
|
611
535
|
}
|
612
|
-
if (
|
536
|
+
if (upb_Map_Size(self->map) != upb_Map_Size(other->map)) {
|
613
537
|
return Qfalse;
|
614
538
|
}
|
615
539
|
|
616
540
|
// For each member of self, check that an equal member exists at the same key
|
617
541
|
// in other.
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
void* mem = value_memory(&v);
|
624
|
-
upb_value other_v;
|
625
|
-
void* other_mem = value_memory(&other_v);
|
626
|
-
|
627
|
-
if (!upb_strtable_lookup2(&other->table,
|
628
|
-
upb_strtable_iter_key(&it),
|
629
|
-
upb_strtable_iter_keylength(&it),
|
630
|
-
&other_v)) {
|
542
|
+
size_t iter = kUpb_Map_Begin;
|
543
|
+
upb_MessageValue key, val;
|
544
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
545
|
+
upb_MessageValue other_val;
|
546
|
+
if (!upb_Map_Get(other->map, key, &other_val)) {
|
631
547
|
// Not present in other map.
|
632
548
|
return Qfalse;
|
633
549
|
}
|
634
|
-
|
635
|
-
|
636
|
-
other_mem)) {
|
637
|
-
// Present, but value not equal.
|
550
|
+
if (!Msgval_IsEqual(val, other_val, self->value_type_info)) {
|
551
|
+
// Present but different value.
|
638
552
|
return Qfalse;
|
639
553
|
}
|
640
554
|
}
|
@@ -642,6 +556,42 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
642
556
|
return Qtrue;
|
643
557
|
}
|
644
558
|
|
559
|
+
/*
|
560
|
+
* call-seq:
|
561
|
+
* Message.freeze => self
|
562
|
+
*
|
563
|
+
* Freezes the message object. We have to intercept this so we can pin the
|
564
|
+
* Ruby object into memory so we don't forget it's frozen.
|
565
|
+
*/
|
566
|
+
static VALUE Map_freeze(VALUE _self) {
|
567
|
+
Map* self = ruby_to_Map(_self);
|
568
|
+
if (!RB_OBJ_FROZEN(_self)) {
|
569
|
+
Arena_Pin(self->arena, _self);
|
570
|
+
RB_OBJ_FREEZE(_self);
|
571
|
+
}
|
572
|
+
return _self;
|
573
|
+
}
|
574
|
+
|
575
|
+
/*
|
576
|
+
* Deep freezes the map and values recursively.
|
577
|
+
* Internal use only.
|
578
|
+
*/
|
579
|
+
VALUE Map_internal_deep_freeze(VALUE _self) {
|
580
|
+
Map* self = ruby_to_Map(_self);
|
581
|
+
Map_freeze(_self);
|
582
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
583
|
+
size_t iter = kUpb_Map_Begin;
|
584
|
+
upb_MessageValue key, val;
|
585
|
+
|
586
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
587
|
+
VALUE val_val =
|
588
|
+
Convert_UpbToRuby(val, self->value_type_info, self->arena);
|
589
|
+
Message_internal_deep_freeze(val_val);
|
590
|
+
}
|
591
|
+
}
|
592
|
+
return _self;
|
593
|
+
}
|
594
|
+
|
645
595
|
/*
|
646
596
|
* call-seq:
|
647
597
|
* Map.hash => hash_value
|
@@ -650,28 +600,17 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
650
600
|
*/
|
651
601
|
VALUE Map_hash(VALUE _self) {
|
652
602
|
Map* self = ruby_to_Map(_self);
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
VALUE key = table_key_to_ruby(
|
662
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
663
|
-
|
664
|
-
upb_value v = upb_strtable_iter_value(&it);
|
665
|
-
void* mem = value_memory(&v);
|
666
|
-
VALUE value = native_slot_get(self->value_type,
|
667
|
-
self->value_type_class,
|
668
|
-
mem);
|
669
|
-
|
670
|
-
h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
|
671
|
-
h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
|
603
|
+
uint64_t hash = 0;
|
604
|
+
|
605
|
+
size_t iter = kUpb_Map_Begin;
|
606
|
+
TypeInfo key_info = {self->key_type};
|
607
|
+
upb_MessageValue key, val;
|
608
|
+
while (upb_Map_Next(self->map, &key, &val, &iter)) {
|
609
|
+
hash = Msgval_GetHash(key, key_info, hash);
|
610
|
+
hash = Msgval_GetHash(val, self->value_type_info, hash);
|
672
611
|
}
|
673
612
|
|
674
|
-
return
|
613
|
+
return LL2NUM(hash);
|
675
614
|
}
|
676
615
|
|
677
616
|
/*
|
@@ -682,25 +621,7 @@ VALUE Map_hash(VALUE _self) {
|
|
682
621
|
*/
|
683
622
|
VALUE Map_to_h(VALUE _self) {
|
684
623
|
Map* self = ruby_to_Map(_self);
|
685
|
-
|
686
|
-
upb_strtable_iter it;
|
687
|
-
for (upb_strtable_begin(&it, &self->table);
|
688
|
-
!upb_strtable_done(&it);
|
689
|
-
upb_strtable_next(&it)) {
|
690
|
-
VALUE key = table_key_to_ruby(
|
691
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
692
|
-
upb_value v = upb_strtable_iter_value(&it);
|
693
|
-
void* mem = value_memory(&v);
|
694
|
-
VALUE value = native_slot_get(self->value_type,
|
695
|
-
self->value_type_class,
|
696
|
-
mem);
|
697
|
-
|
698
|
-
if (self->value_type == UPB_TYPE_MESSAGE) {
|
699
|
-
value = Message_to_h(value);
|
700
|
-
}
|
701
|
-
rb_hash_aset(hash, key, value);
|
702
|
-
}
|
703
|
-
return hash;
|
624
|
+
return Map_CreateHash(self->map, self->key_type, self->value_type_info);
|
704
625
|
}
|
705
626
|
|
706
627
|
/*
|
@@ -714,36 +635,11 @@ VALUE Map_to_h(VALUE _self) {
|
|
714
635
|
VALUE Map_inspect(VALUE _self) {
|
715
636
|
Map* self = ruby_to_Map(_self);
|
716
637
|
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
upb_strtable_iter it;
|
723
|
-
for (upb_strtable_begin(&it, &self->table);
|
724
|
-
!upb_strtable_done(&it);
|
725
|
-
upb_strtable_next(&it)) {
|
726
|
-
VALUE key = table_key_to_ruby(
|
727
|
-
self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
|
728
|
-
|
729
|
-
upb_value v = upb_strtable_iter_value(&it);
|
730
|
-
void* mem = value_memory(&v);
|
731
|
-
VALUE value = native_slot_get(self->value_type,
|
732
|
-
self->value_type_class,
|
733
|
-
mem);
|
734
|
-
|
735
|
-
if (!first) {
|
736
|
-
str = rb_str_cat2(str, ", ");
|
737
|
-
} else {
|
738
|
-
first = false;
|
739
|
-
}
|
740
|
-
str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
|
741
|
-
str = rb_str_cat2(str, "=>");
|
742
|
-
str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
|
743
|
-
}
|
744
|
-
|
745
|
-
str = rb_str_cat2(str, "}");
|
746
|
-
return str;
|
638
|
+
StringBuilder* builder = StringBuilder_New();
|
639
|
+
Map_Inspect(builder, self->map, self->key_type, self->value_type_info);
|
640
|
+
VALUE ret = StringBuilder_ToRubyString(builder);
|
641
|
+
StringBuilder_Free(builder);
|
642
|
+
return ret;
|
747
643
|
}
|
748
644
|
|
749
645
|
/*
|
@@ -755,87 +651,11 @@ VALUE Map_inspect(VALUE _self) {
|
|
755
651
|
* in the new copy of this map. Returns the new copy of this map with merged
|
756
652
|
* contents.
|
757
653
|
*/
|
758
|
-
VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
654
|
+
static VALUE Map_merge(VALUE _self, VALUE hashmap) {
|
759
655
|
VALUE dupped = Map_dup(_self);
|
760
656
|
return Map_merge_into_self(dupped, hashmap);
|
761
657
|
}
|
762
658
|
|
763
|
-
static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
|
764
|
-
Map_index_set(self, key, value);
|
765
|
-
return ST_CONTINUE;
|
766
|
-
}
|
767
|
-
|
768
|
-
// Used only internally -- shared by #merge and #initialize.
|
769
|
-
VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
770
|
-
if (TYPE(hashmap) == T_HASH) {
|
771
|
-
rb_hash_foreach(hashmap, merge_into_self_callback, _self);
|
772
|
-
} else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
|
773
|
-
RTYPEDDATA_TYPE(hashmap) == &Map_type) {
|
774
|
-
|
775
|
-
Map* self = ruby_to_Map(_self);
|
776
|
-
Map* other = ruby_to_Map(hashmap);
|
777
|
-
upb_strtable_iter it;
|
778
|
-
|
779
|
-
if (self->key_type != other->key_type ||
|
780
|
-
self->value_type != other->value_type ||
|
781
|
-
self->value_type_class != other->value_type_class) {
|
782
|
-
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
|
783
|
-
}
|
784
|
-
|
785
|
-
for (upb_strtable_begin(&it, &other->table);
|
786
|
-
!upb_strtable_done(&it);
|
787
|
-
upb_strtable_next(&it)) {
|
788
|
-
|
789
|
-
// Replace any existing value by issuing a 'remove' operation first.
|
790
|
-
upb_value v;
|
791
|
-
upb_value oldv;
|
792
|
-
upb_strtable_remove2(&self->table,
|
793
|
-
upb_strtable_iter_key(&it),
|
794
|
-
upb_strtable_iter_keylength(&it),
|
795
|
-
&oldv);
|
796
|
-
|
797
|
-
v = upb_strtable_iter_value(&it);
|
798
|
-
upb_strtable_insert2(&self->table,
|
799
|
-
upb_strtable_iter_key(&it),
|
800
|
-
upb_strtable_iter_keylength(&it),
|
801
|
-
v);
|
802
|
-
}
|
803
|
-
} else {
|
804
|
-
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
805
|
-
}
|
806
|
-
return _self;
|
807
|
-
}
|
808
|
-
|
809
|
-
// Internal method: map iterator initialization (used for serialization).
|
810
|
-
void Map_begin(VALUE _self, Map_iter* iter) {
|
811
|
-
Map* self = ruby_to_Map(_self);
|
812
|
-
iter->self = self;
|
813
|
-
upb_strtable_begin(&iter->it, &self->table);
|
814
|
-
}
|
815
|
-
|
816
|
-
void Map_next(Map_iter* iter) {
|
817
|
-
upb_strtable_next(&iter->it);
|
818
|
-
}
|
819
|
-
|
820
|
-
bool Map_done(Map_iter* iter) {
|
821
|
-
return upb_strtable_done(&iter->it);
|
822
|
-
}
|
823
|
-
|
824
|
-
VALUE Map_iter_key(Map_iter* iter) {
|
825
|
-
return table_key_to_ruby(
|
826
|
-
iter->self,
|
827
|
-
upb_strtable_iter_key(&iter->it),
|
828
|
-
upb_strtable_iter_keylength(&iter->it));
|
829
|
-
}
|
830
|
-
|
831
|
-
VALUE Map_iter_value(Map_iter* iter) {
|
832
|
-
upb_value v = upb_strtable_iter_value(&iter->it);
|
833
|
-
void* mem = value_memory(&v);
|
834
|
-
return native_slot_get(iter->self->value_type,
|
835
|
-
iter->self->value_type_class,
|
836
|
-
mem);
|
837
|
-
}
|
838
|
-
|
839
659
|
void Map_register(VALUE module) {
|
840
660
|
VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
|
841
661
|
rb_define_alloc_func(klass, Map_alloc);
|
@@ -852,8 +672,12 @@ void Map_register(VALUE module) {
|
|
852
672
|
rb_define_method(klass, "delete", Map_delete, 1);
|
853
673
|
rb_define_method(klass, "clear", Map_clear, 0);
|
854
674
|
rb_define_method(klass, "length", Map_length, 0);
|
675
|
+
rb_define_method(klass, "size", Map_length, 0);
|
855
676
|
rb_define_method(klass, "dup", Map_dup, 0);
|
677
|
+
// Also define #clone so that we don't inherit Object#clone.
|
678
|
+
rb_define_method(klass, "clone", Map_dup, 0);
|
856
679
|
rb_define_method(klass, "==", Map_eq, 1);
|
680
|
+
rb_define_method(klass, "freeze", Map_freeze, 0);
|
857
681
|
rb_define_method(klass, "hash", Map_hash, 0);
|
858
682
|
rb_define_method(klass, "to_h", Map_to_h, 0);
|
859
683
|
rb_define_method(klass, "inspect", Map_inspect, 0);
|