google-protobuf 3.22.2 → 4.30.1
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.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +60 -86
- data/ext/google/protobuf_c/convert.h +3 -28
- data/ext/google/protobuf_c/defs.c +692 -72
- data/ext/google/protobuf_c/defs.h +3 -28
- data/ext/google/protobuf_c/extconf.rb +14 -1
- data/ext/google/protobuf_c/glue.c +135 -0
- data/ext/google/protobuf_c/map.c +89 -45
- data/ext/google/protobuf_c/map.h +12 -30
- data/ext/google/protobuf_c/message.c +169 -169
- data/ext/google/protobuf_c/message.h +11 -33
- data/ext/google/protobuf_c/protobuf.c +65 -188
- data/ext/google/protobuf_c/protobuf.h +27 -39
- data/ext/google/protobuf_c/repeated_field.c +72 -38
- data/ext/google/protobuf_c/repeated_field.h +11 -29
- data/ext/google/protobuf_c/ruby-upb.c +15064 -11220
- data/ext/google/protobuf_c/ruby-upb.h +10643 -5403
- data/ext/google/protobuf_c/shared_convert.c +69 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +37 -0
- data/ext/google/protobuf_c/shared_message.h +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +207 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +3 -26
- data/lib/google/protobuf/any_pb.rb +6 -8
- data/lib/google/protobuf/api_pb.rb +6 -26
- data/lib/google/protobuf/descriptor_pb.rb +21 -252
- data/lib/google/protobuf/duration_pb.rb +6 -8
- data/lib/google/protobuf/empty_pb.rb +6 -6
- data/lib/google/protobuf/ffi/descriptor.rb +175 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +77 -0
- data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
- data/lib/google/protobuf/ffi/ffi.rb +214 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +340 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +292 -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 +433 -0
- data/lib/google/protobuf/ffi/message.rb +783 -0
- data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
- data/lib/google/protobuf/ffi/object_cache.rb +30 -0
- data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
- data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
- data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
- data/lib/google/protobuf/field_mask_pb.rb +6 -7
- data/lib/google/protobuf/internal/object_cache.rb +99 -0
- data/lib/google/protobuf/message_exts.rb +3 -26
- data/lib/google/protobuf/plugin_pb.rb +6 -31
- data/lib/google/protobuf/repeated_field.rb +7 -31
- data/lib/google/protobuf/source_context_pb.rb +6 -7
- data/lib/google/protobuf/struct_pb.rb +6 -23
- data/lib/google/protobuf/timestamp_pb.rb +6 -8
- data/lib/google/protobuf/type_pb.rb +6 -71
- data/lib/google/protobuf/well_known_types.rb +5 -34
- data/lib/google/protobuf/wrappers_pb.rb +6 -31
- data/lib/google/protobuf.rb +27 -45
- data/lib/google/protobuf_ffi.rb +52 -0
- data/lib/google/protobuf_native.rb +19 -0
- data/lib/google/tasks/ffi.rake +100 -0
- metadata +92 -9
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
- data/lib/google/protobuf/descriptor_dsl.rb +0 -465
@@ -1,38 +1,13 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2008 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
|
|
31
8
|
#ifndef RUBY_PROTOBUF_DEFS_H_
|
32
9
|
#define RUBY_PROTOBUF_DEFS_H_
|
33
10
|
|
34
|
-
#include <ruby/ruby.h>
|
35
|
-
|
36
11
|
#include "protobuf.h"
|
37
12
|
#include "ruby-upb.h"
|
38
13
|
|
@@ -6,6 +6,18 @@ ext_name = "google/protobuf_c"
|
|
6
6
|
|
7
7
|
dir_config(ext_name)
|
8
8
|
|
9
|
+
if ENV["CC"]
|
10
|
+
RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"]
|
11
|
+
end
|
12
|
+
|
13
|
+
if ENV["CXX"]
|
14
|
+
RbConfig::CONFIG["CXX"] = RbConfig::MAKEFILE_CONFIG["CXX"] = ENV["CXX"]
|
15
|
+
end
|
16
|
+
|
17
|
+
if ENV["LD"]
|
18
|
+
RbConfig::CONFIG["LD"] = RbConfig::MAKEFILE_CONFIG["LD"] = ENV["LD"]
|
19
|
+
end
|
20
|
+
|
9
21
|
if RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/ || RUBY_PLATFORM =~ /freebsd/
|
10
22
|
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG -fvisibility=hidden -Wall -Wsign-compare -Wno-declaration-after-statement"
|
11
23
|
else
|
@@ -22,6 +34,7 @@ $INCFLAGS += " -I$(srcdir)/third_party/utf8_range"
|
|
22
34
|
|
23
35
|
$srcs = ["protobuf.c", "convert.c", "defs.c", "message.c",
|
24
36
|
"repeated_field.c", "map.c", "ruby-upb.c", "wrap_memcpy.c",
|
25
|
-
"
|
37
|
+
"utf8_range.c", "shared_convert.c",
|
38
|
+
"shared_message.c"]
|
26
39
|
|
27
40
|
create_makefile(ext_name)
|
@@ -0,0 +1,135 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2023 Google Inc. All rights reserved.
|
3
|
+
//
|
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
|
7
|
+
|
8
|
+
// -----------------------------------------------------------------------------
|
9
|
+
// Exposing inlined UPB functions. Strictly free of dependencies on
|
10
|
+
// Ruby interpreter internals.
|
11
|
+
|
12
|
+
#include "ruby-upb.h"
|
13
|
+
|
14
|
+
upb_Arena* Arena_create() { return upb_Arena_Init(NULL, 0, &upb_alloc_global); }
|
15
|
+
|
16
|
+
google_protobuf_FileDescriptorProto* FileDescriptorProto_parse(
|
17
|
+
const char* serialized_file_proto, size_t length, upb_Arena* arena) {
|
18
|
+
return google_protobuf_FileDescriptorProto_parse(serialized_file_proto,
|
19
|
+
length, arena);
|
20
|
+
}
|
21
|
+
|
22
|
+
char* EnumDescriptor_serialized_options(const upb_EnumDef* enumdef,
|
23
|
+
size_t* size, upb_Arena* arena) {
|
24
|
+
const google_protobuf_EnumOptions* opts = upb_EnumDef_Options(enumdef);
|
25
|
+
char* serialized = google_protobuf_EnumOptions_serialize(opts, arena, size);
|
26
|
+
return serialized;
|
27
|
+
}
|
28
|
+
|
29
|
+
char* EnumDescriptor_serialized_to_proto(const upb_EnumDef* enumdef,
|
30
|
+
size_t* size, upb_Arena* arena) {
|
31
|
+
const google_protobuf_EnumDescriptorProto* file_proto =
|
32
|
+
upb_EnumDef_ToProto(enumdef, arena);
|
33
|
+
char* serialized =
|
34
|
+
google_protobuf_EnumDescriptorProto_serialize(file_proto, arena, size);
|
35
|
+
return serialized;
|
36
|
+
}
|
37
|
+
|
38
|
+
char* FileDescriptor_serialized_options(const upb_FileDef* filedef,
|
39
|
+
size_t* size, upb_Arena* arena) {
|
40
|
+
const google_protobuf_FileOptions* opts = upb_FileDef_Options(filedef);
|
41
|
+
char* serialized = google_protobuf_FileOptions_serialize(opts, arena, size);
|
42
|
+
return serialized;
|
43
|
+
}
|
44
|
+
|
45
|
+
char* FileDescriptor_serialized_to_proto(const upb_FileDef* filedef,
|
46
|
+
size_t* size, upb_Arena* arena) {
|
47
|
+
const google_protobuf_FileDescriptorProto* file_proto =
|
48
|
+
upb_FileDef_ToProto(filedef, arena);
|
49
|
+
char* serialized =
|
50
|
+
google_protobuf_FileDescriptorProto_serialize(file_proto, arena, size);
|
51
|
+
return serialized;
|
52
|
+
}
|
53
|
+
|
54
|
+
char* Descriptor_serialized_options(const upb_MessageDef* msgdef, size_t* size,
|
55
|
+
upb_Arena* arena) {
|
56
|
+
const google_protobuf_MessageOptions* opts = upb_MessageDef_Options(msgdef);
|
57
|
+
char* serialized =
|
58
|
+
google_protobuf_MessageOptions_serialize(opts, arena, size);
|
59
|
+
return serialized;
|
60
|
+
}
|
61
|
+
|
62
|
+
char* Descriptor_serialized_to_proto(const upb_MessageDef* msgdef, size_t* size,
|
63
|
+
upb_Arena* arena) {
|
64
|
+
const google_protobuf_DescriptorProto* proto =
|
65
|
+
upb_MessageDef_ToProto(msgdef, arena);
|
66
|
+
char* serialized =
|
67
|
+
google_protobuf_DescriptorProto_serialize(proto, arena, size);
|
68
|
+
return serialized;
|
69
|
+
}
|
70
|
+
|
71
|
+
char* OneOfDescriptor_serialized_options(const upb_OneofDef* oneofdef,
|
72
|
+
size_t* size, upb_Arena* arena) {
|
73
|
+
const google_protobuf_OneofOptions* opts = upb_OneofDef_Options(oneofdef);
|
74
|
+
char* serialized = google_protobuf_OneofOptions_serialize(opts, arena, size);
|
75
|
+
return serialized;
|
76
|
+
}
|
77
|
+
|
78
|
+
char* OneOfDescriptor_serialized_to_proto(const upb_OneofDef* oneofdef,
|
79
|
+
size_t* size, upb_Arena* arena) {
|
80
|
+
const google_protobuf_OneofDescriptorProto* proto =
|
81
|
+
upb_OneofDef_ToProto(oneofdef, arena);
|
82
|
+
char* serialized =
|
83
|
+
google_protobuf_OneofDescriptorProto_serialize(proto, arena, size);
|
84
|
+
return serialized;
|
85
|
+
}
|
86
|
+
|
87
|
+
char* FieldDescriptor_serialized_options(const upb_FieldDef* fielddef,
|
88
|
+
size_t* size, upb_Arena* arena) {
|
89
|
+
const google_protobuf_FieldOptions* opts = upb_FieldDef_Options(fielddef);
|
90
|
+
char* serialized = google_protobuf_FieldOptions_serialize(opts, arena, size);
|
91
|
+
return serialized;
|
92
|
+
}
|
93
|
+
|
94
|
+
char* FieldDescriptor_serialized_to_proto(const upb_FieldDef* fieldef,
|
95
|
+
size_t* size, upb_Arena* arena) {
|
96
|
+
const google_protobuf_FieldDescriptorProto* proto =
|
97
|
+
upb_FieldDef_ToProto(fieldef, arena);
|
98
|
+
char* serialized =
|
99
|
+
google_protobuf_FieldDescriptorProto_serialize(proto, arena, size);
|
100
|
+
return serialized;
|
101
|
+
}
|
102
|
+
|
103
|
+
char* ServiceDescriptor_serialized_options(const upb_ServiceDef* servicedef,
|
104
|
+
size_t* size, upb_Arena* arena) {
|
105
|
+
const google_protobuf_ServiceOptions* opts =
|
106
|
+
upb_ServiceDef_Options(servicedef);
|
107
|
+
char* serialized =
|
108
|
+
google_protobuf_ServiceOptions_serialize(opts, arena, size);
|
109
|
+
return serialized;
|
110
|
+
}
|
111
|
+
|
112
|
+
char* ServiceDescriptor_serialized_to_proto(const upb_ServiceDef* servicedef,
|
113
|
+
size_t* size, upb_Arena* arena) {
|
114
|
+
const google_protobuf_ServiceDescriptorProto* proto =
|
115
|
+
upb_ServiceDef_ToProto(servicedef, arena);
|
116
|
+
char* serialized =
|
117
|
+
google_protobuf_ServiceDescriptorProto_serialize(proto, arena, size);
|
118
|
+
return serialized;
|
119
|
+
}
|
120
|
+
|
121
|
+
char* MethodDescriptor_serialized_options(const upb_MethodDef* methoddef,
|
122
|
+
size_t* size, upb_Arena* arena) {
|
123
|
+
const google_protobuf_MethodOptions* opts = upb_MethodDef_Options(methoddef);
|
124
|
+
char* serialized = google_protobuf_MethodOptions_serialize(opts, arena, size);
|
125
|
+
return serialized;
|
126
|
+
}
|
127
|
+
|
128
|
+
char* MethodDescriptor_serialized_to_proto(const upb_MethodDef* methodef,
|
129
|
+
size_t* size, upb_Arena* arena) {
|
130
|
+
const google_protobuf_MethodDescriptorProto* proto =
|
131
|
+
upb_MethodDef_ToProto(methodef, arena);
|
132
|
+
char* serialized =
|
133
|
+
google_protobuf_MethodDescriptorProto_serialize(proto, arena, size);
|
134
|
+
return serialized;
|
135
|
+
}
|
data/ext/google/protobuf_c/map.c
CHANGED
@@ -1,32 +1,9 @@
|
|
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
|
|
31
8
|
#include "convert.h"
|
32
9
|
#include "defs.h"
|
@@ -61,9 +38,11 @@ static void Map_mark(void* _self) {
|
|
61
38
|
rb_gc_mark(self->arena);
|
62
39
|
}
|
63
40
|
|
41
|
+
static size_t Map_memsize(const void* _self) { return sizeof(Map); }
|
42
|
+
|
64
43
|
const rb_data_type_t Map_type = {
|
65
44
|
"Google::Protobuf::Map",
|
66
|
-
{Map_mark, RUBY_DEFAULT_FREE,
|
45
|
+
{Map_mark, RUBY_DEFAULT_FREE, Map_memsize},
|
67
46
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
68
47
|
};
|
69
48
|
|
@@ -84,16 +63,16 @@ static VALUE Map_alloc(VALUE klass) {
|
|
84
63
|
return TypedData_Wrap_Struct(klass, &Map_type, self);
|
85
64
|
}
|
86
65
|
|
87
|
-
VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type,
|
88
|
-
VALUE arena) {
|
66
|
+
VALUE Map_GetRubyWrapper(const upb_Map* map, upb_CType key_type,
|
67
|
+
TypeInfo value_type, VALUE arena) {
|
89
68
|
PBRUBY_ASSERT(map);
|
69
|
+
PBRUBY_ASSERT(arena != Qnil);
|
90
70
|
|
91
71
|
VALUE val = ObjectCache_Get(map);
|
92
72
|
|
93
73
|
if (val == Qnil) {
|
94
74
|
val = Map_alloc(cMap);
|
95
75
|
Map* self;
|
96
|
-
ObjectCache_Add(map, val);
|
97
76
|
TypedData_Get_Struct(val, Map, &Map_type, self);
|
98
77
|
self->map = map;
|
99
78
|
self->arena = arena;
|
@@ -103,8 +82,8 @@ VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
|
|
103
82
|
const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
|
104
83
|
self->value_type_class = Descriptor_DefToClass(val_m);
|
105
84
|
}
|
85
|
+
return ObjectCache_TryAdd(map, val);
|
106
86
|
}
|
107
|
-
|
108
87
|
return val;
|
109
88
|
}
|
110
89
|
|
@@ -126,8 +105,9 @@ static TypeInfo Map_keyinfo(Map* self) {
|
|
126
105
|
}
|
127
106
|
|
128
107
|
static upb_Map* Map_GetMutable(VALUE _self) {
|
129
|
-
|
130
|
-
|
108
|
+
const upb_Map* map = ruby_to_Map(_self)->map;
|
109
|
+
Protobuf_CheckNotFrozen(_self, upb_Map_IsFrozen(map));
|
110
|
+
return (upb_Map*)map;
|
131
111
|
}
|
132
112
|
|
133
113
|
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type,
|
@@ -235,7 +215,7 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
235
215
|
Map* self = ruby_to_Map(_self);
|
236
216
|
Map* other = ruby_to_Map(hashmap);
|
237
217
|
upb_Arena* arena = Arena_get(self->arena);
|
238
|
-
|
218
|
+
upb_Map* self_map = Map_GetMutable(_self);
|
239
219
|
|
240
220
|
Arena_fuse(other->arena, arena);
|
241
221
|
|
@@ -248,7 +228,7 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
|
|
248
228
|
size_t iter = kUpb_Map_Begin;
|
249
229
|
upb_MessageValue key, val;
|
250
230
|
while (upb_Map_Next(other->map, &key, &val, &iter)) {
|
251
|
-
upb_Map_Set(
|
231
|
+
upb_Map_Set(self_map, key, val, arena);
|
252
232
|
}
|
253
233
|
} else {
|
254
234
|
rb_raise(rb_eArgError, "Unknown type merging into Map");
|
@@ -319,7 +299,9 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
|
|
319
299
|
|
320
300
|
self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
|
321
301
|
self->value_type_info.type);
|
322
|
-
|
302
|
+
VALUE stored = ObjectCache_TryAdd(self->map, _self);
|
303
|
+
(void)stored;
|
304
|
+
PBRUBY_ASSERT(stored == _self);
|
323
305
|
|
324
306
|
if (init_arg != Qnil) {
|
325
307
|
Map_merge_into_self(_self, init_arg);
|
@@ -458,14 +440,14 @@ static VALUE Map_has_key(VALUE _self, VALUE key) {
|
|
458
440
|
* nil if none was present. Throws an exception if the key is of the wrong type.
|
459
441
|
*/
|
460
442
|
static VALUE Map_delete(VALUE _self, VALUE key) {
|
443
|
+
upb_Map* map = Map_GetMutable(_self);
|
461
444
|
Map* self = ruby_to_Map(_self);
|
462
|
-
rb_check_frozen(_self);
|
463
445
|
|
464
446
|
upb_MessageValue key_upb =
|
465
447
|
Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
|
466
448
|
upb_MessageValue val_upb;
|
467
449
|
|
468
|
-
if (upb_Map_Delete(
|
450
|
+
if (upb_Map_Delete(map, key_upb, &val_upb)) {
|
469
451
|
return Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
|
470
452
|
} else {
|
471
453
|
return Qnil;
|
@@ -579,20 +561,81 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
|
|
579
561
|
|
580
562
|
/*
|
581
563
|
* call-seq:
|
582
|
-
*
|
564
|
+
* Map.frozen? => bool
|
583
565
|
*
|
584
|
-
*
|
585
|
-
* Ruby object
|
566
|
+
* Returns true if the map is frozen in either Ruby or the underlying
|
567
|
+
* representation. Freezes the Ruby map object if it is not already frozen in
|
568
|
+
* Ruby but it is frozen in the underlying representation.
|
586
569
|
*/
|
587
|
-
|
570
|
+
VALUE Map_frozen(VALUE _self) {
|
588
571
|
Map* self = ruby_to_Map(_self);
|
589
|
-
if (!
|
590
|
-
|
591
|
-
|
572
|
+
if (!upb_Map_IsFrozen(self->map)) {
|
573
|
+
PBRUBY_ASSERT(!RB_OBJ_FROZEN(_self));
|
574
|
+
return Qfalse;
|
575
|
+
}
|
576
|
+
|
577
|
+
// Lazily freeze the Ruby wrapper.
|
578
|
+
if (!RB_OBJ_FROZEN(_self)) RB_OBJ_FREEZE(_self);
|
579
|
+
return Qtrue;
|
580
|
+
}
|
581
|
+
|
582
|
+
/*
|
583
|
+
* call-seq:
|
584
|
+
* Map.freeze => self
|
585
|
+
*
|
586
|
+
* Freezes the map object. We have to intercept this so we can freeze the
|
587
|
+
* underlying representation, not just the Ruby wrapper.
|
588
|
+
*/
|
589
|
+
VALUE Map_freeze(VALUE _self) {
|
590
|
+
Map* self = ruby_to_Map(_self);
|
591
|
+
if (RB_OBJ_FROZEN(_self)) {
|
592
|
+
PBRUBY_ASSERT(upb_Map_IsFrozen(self->map));
|
593
|
+
return _self;
|
594
|
+
}
|
595
|
+
|
596
|
+
if (!upb_Map_IsFrozen(self->map)) {
|
597
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
598
|
+
upb_Map_Freeze(
|
599
|
+
Map_GetMutable(_self),
|
600
|
+
upb_MessageDef_MiniTable(self->value_type_info.def.msgdef));
|
601
|
+
} else {
|
602
|
+
upb_Map_Freeze(Map_GetMutable(_self), NULL);
|
603
|
+
}
|
592
604
|
}
|
605
|
+
|
606
|
+
RB_OBJ_FREEZE(_self);
|
607
|
+
|
593
608
|
return _self;
|
594
609
|
}
|
595
610
|
|
611
|
+
VALUE Map_EmptyFrozen(const upb_FieldDef* f) {
|
612
|
+
PBRUBY_ASSERT(upb_FieldDef_IsMap(f));
|
613
|
+
VALUE val = ObjectCache_Get(f);
|
614
|
+
|
615
|
+
if (val == Qnil) {
|
616
|
+
const upb_FieldDef* key_f = map_field_key(f);
|
617
|
+
const upb_FieldDef* val_f = map_field_value(f);
|
618
|
+
upb_CType key_type = upb_FieldDef_CType(key_f);
|
619
|
+
TypeInfo value_type_info = TypeInfo_get(val_f);
|
620
|
+
val = Map_alloc(cMap);
|
621
|
+
Map* self;
|
622
|
+
TypedData_Get_Struct(val, Map, &Map_type, self);
|
623
|
+
self->arena = Arena_new();
|
624
|
+
self->map =
|
625
|
+
upb_Map_New(Arena_get(self->arena), key_type, value_type_info.type);
|
626
|
+
self->key_type = key_type;
|
627
|
+
self->value_type_info = value_type_info;
|
628
|
+
if (self->value_type_info.type == kUpb_CType_Message) {
|
629
|
+
const upb_MessageDef* val_m = value_type_info.def.msgdef;
|
630
|
+
self->value_type_class = Descriptor_DefToClass(val_m);
|
631
|
+
}
|
632
|
+
return ObjectCache_TryAdd(f, Map_freeze(val));
|
633
|
+
}
|
634
|
+
PBRUBY_ASSERT(RB_OBJ_FROZEN(val));
|
635
|
+
PBRUBY_ASSERT(upb_Map_IsFrozen(ruby_to_Map(val)->map));
|
636
|
+
return val;
|
637
|
+
}
|
638
|
+
|
596
639
|
/*
|
597
640
|
* call-seq:
|
598
641
|
* Map.hash => hash_value
|
@@ -679,6 +722,7 @@ void Map_register(VALUE module) {
|
|
679
722
|
rb_define_method(klass, "clone", Map_dup, 0);
|
680
723
|
rb_define_method(klass, "==", Map_eq, 1);
|
681
724
|
rb_define_method(klass, "freeze", Map_freeze, 0);
|
725
|
+
rb_define_method(klass, "frozen?", Map_frozen, 0);
|
682
726
|
rb_define_method(klass, "hash", Map_hash, 0);
|
683
727
|
rb_define_method(klass, "to_h", Map_to_h, 0);
|
684
728
|
rb_define_method(klass, "inspect", Map_inspect, 0);
|
data/ext/google/protobuf_c/map.h
CHANGED
@@ -1,45 +1,24 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2008 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
|
|
31
8
|
#ifndef RUBY_PROTOBUF_MAP_H_
|
32
9
|
#define RUBY_PROTOBUF_MAP_H_
|
33
10
|
|
34
|
-
#include <ruby/ruby.h>
|
35
|
-
|
36
11
|
#include "protobuf.h"
|
37
12
|
#include "ruby-upb.h"
|
38
13
|
|
14
|
+
// Returns a frozen sentinel Ruby wrapper object for an empty upb_Map with the
|
15
|
+
// key and value types specified by the field. Creates one if it doesn't exist.
|
16
|
+
VALUE Map_EmptyFrozen(const upb_FieldDef* f);
|
17
|
+
|
39
18
|
// Returns a Ruby wrapper object for the given map, which will be created if
|
40
19
|
// one does not exist already.
|
41
|
-
VALUE Map_GetRubyWrapper(upb_Map *map, upb_CType key_type,
|
42
|
-
VALUE arena);
|
20
|
+
VALUE Map_GetRubyWrapper(const upb_Map *map, upb_CType key_type,
|
21
|
+
TypeInfo value_type, VALUE arena);
|
43
22
|
|
44
23
|
// Gets the underlying upb_Map for this Ruby map object, which must have
|
45
24
|
// key/value type that match |field|. If this is not a map or the type doesn't
|
@@ -63,4 +42,7 @@ extern VALUE cMap;
|
|
63
42
|
// Call at startup to register all types in this module.
|
64
43
|
void Map_register(VALUE module);
|
65
44
|
|
45
|
+
// Recursively freeze map
|
46
|
+
VALUE Map_freeze(VALUE _self);
|
47
|
+
|
66
48
|
#endif // RUBY_PROTOBUF_MAP_H_
|