google-protobuf 3.14.0 → 4.26.1
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 +317 -0
- data/ext/google/protobuf_c/convert.h +50 -0
- data/ext/google/protobuf_c/defs.c +759 -1709
- 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 +328 -485
- data/ext/google/protobuf_c/map.h +44 -0
- data/ext/google/protobuf_c/message.c +1061 -530
- data/ext/google/protobuf_c/message.h +86 -0
- data/ext/google/protobuf_c/protobuf.c +314 -94
- data/ext/google/protobuf_c/protobuf.h +66 -621
- data/ext/google/protobuf_c/repeated_field.c +314 -353
- data/ext/google/protobuf_c/repeated_field.h +41 -0
- data/ext/google/protobuf_c/ruby-upb.c +15407 -0
- data/ext/google/protobuf_c/ruby-upb.h +13966 -0
- data/ext/google/protobuf_c/shared_convert.c +66 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +67 -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/utf8_range.c +467 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +22 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
- data/lib/google/protobuf/any_pb.rb +6 -8
- data/lib/google/protobuf/api_pb.rb +7 -26
- data/lib/google/protobuf/descriptor_pb.rb +65 -0
- 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 +164 -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 +215 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +328 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +47 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +289 -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 +409 -0
- data/lib/google/protobuf/ffi/message.rb +659 -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 +385 -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 +10 -28
- data/lib/google/protobuf/plugin_pb.rb +25 -0
- data/lib/google/protobuf/repeated_field.rb +19 -30
- 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 +7 -71
- data/lib/google/protobuf/well_known_types.rb +17 -36
- data/lib/google/protobuf/wrappers_pb.rb +6 -31
- data/lib/google/protobuf.rb +32 -118
- data/lib/google/protobuf_ffi.rb +49 -0
- data/lib/google/protobuf_native.rb +19 -0
- data/lib/google/tasks/ffi.rake +100 -0
- metadata +88 -37
- 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/upb.h +0 -6777
- data/tests/basic.rb +0 -543
- data/tests/generated_code_test.rb +0 -23
- data/tests/stress.rb +0 -38
@@ -0,0 +1,86 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2008 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
|
+
#ifndef RUBY_PROTOBUF_MESSAGE_H_
|
9
|
+
#define RUBY_PROTOBUF_MESSAGE_H_
|
10
|
+
|
11
|
+
#include "protobuf.h"
|
12
|
+
#include "ruby-upb.h"
|
13
|
+
|
14
|
+
// Gets the underlying upb_Message* and upb_MessageDef for the given Ruby
|
15
|
+
// message wrapper. Requires that |value| is indeed a message object.
|
16
|
+
const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m);
|
17
|
+
|
18
|
+
// Like Message_Get(), but checks that the object is not frozen and returns a
|
19
|
+
// mutable pointer.
|
20
|
+
upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m);
|
21
|
+
|
22
|
+
// Returns the Arena object for this message.
|
23
|
+
VALUE Message_GetArena(VALUE value);
|
24
|
+
|
25
|
+
// Converts |value| into a upb_Message value of the expected upb_MessageDef
|
26
|
+
// type, raising an error if this is not possible. Used when assigning |value|
|
27
|
+
// to a field of another message, which means the message must be of a
|
28
|
+
// particular type.
|
29
|
+
//
|
30
|
+
// This will perform automatic conversions in some cases (for example, Time ->
|
31
|
+
// Google::Protobuf::Timestamp). If any new message is created, it will be
|
32
|
+
// created on |arena|, and any existing message will have its arena fused with
|
33
|
+
// |arena|.
|
34
|
+
const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
|
35
|
+
const char* name, upb_Arena* arena);
|
36
|
+
|
37
|
+
// Gets or constructs a Ruby wrapper object for the given message. The wrapper
|
38
|
+
// object will reference |arena| and ensure that it outlives this object.
|
39
|
+
VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
|
40
|
+
VALUE arena);
|
41
|
+
|
42
|
+
// Gets the given field from this message.
|
43
|
+
VALUE Message_getfield(VALUE _self, const upb_FieldDef* f);
|
44
|
+
|
45
|
+
// Implements #inspect for this message, printing the text to |b|.
|
46
|
+
void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
|
47
|
+
const upb_MessageDef* m);
|
48
|
+
|
49
|
+
// Returns a hash value for the given message.
|
50
|
+
uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
51
|
+
uint64_t seed);
|
52
|
+
|
53
|
+
// Returns a deep copy of the given message.
|
54
|
+
upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
55
|
+
upb_Arena* arena);
|
56
|
+
|
57
|
+
// Returns true if these two messages are equal.
|
58
|
+
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
59
|
+
const upb_MessageDef* m);
|
60
|
+
|
61
|
+
// Checks that this Ruby object is a message, and raises an exception if not.
|
62
|
+
void Message_CheckClass(VALUE klass);
|
63
|
+
|
64
|
+
// Returns a new Hash object containing the contents of this message.
|
65
|
+
VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info);
|
66
|
+
|
67
|
+
// Creates a message class or enum module for this descriptor, respectively.
|
68
|
+
VALUE build_class_from_descriptor(VALUE descriptor);
|
69
|
+
VALUE build_module_from_enumdesc(VALUE _enumdesc);
|
70
|
+
|
71
|
+
// Returns the Descriptor/EnumDescriptor for the given message class or enum
|
72
|
+
// module, respectively. Returns nil if this is not a message class or enum
|
73
|
+
// module.
|
74
|
+
VALUE MessageOrEnum_GetDescriptor(VALUE klass);
|
75
|
+
|
76
|
+
// Decodes a Message from a byte sequence.
|
77
|
+
VALUE Message_decode_bytes(int size, const char* bytes, int options,
|
78
|
+
VALUE klass, bool freeze);
|
79
|
+
|
80
|
+
// Recursively freeze message
|
81
|
+
VALUE Message_freeze(VALUE _self);
|
82
|
+
|
83
|
+
// Call at startup to register all types in this module.
|
84
|
+
void Message_register(VALUE protobuf);
|
85
|
+
|
86
|
+
#endif // RUBY_PROTOBUF_MESSAGE_H_
|
@@ -1,91 +1,331 @@
|
|
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 "protobuf.h"
|
32
9
|
|
33
|
-
|
10
|
+
#include <ruby/version.h>
|
11
|
+
|
12
|
+
#include "defs.h"
|
13
|
+
#include "map.h"
|
14
|
+
#include "message.h"
|
15
|
+
#include "repeated_field.h"
|
16
|
+
|
34
17
|
VALUE cParseError;
|
35
18
|
VALUE cTypeError;
|
36
|
-
VALUE c_only_cookie = Qnil;
|
37
19
|
|
38
|
-
|
39
|
-
|
20
|
+
const upb_FieldDef *map_field_key(const upb_FieldDef *field) {
|
21
|
+
const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
|
22
|
+
return upb_MessageDef_FindFieldByNumber(entry, 1);
|
23
|
+
}
|
24
|
+
|
25
|
+
const upb_FieldDef *map_field_value(const upb_FieldDef *field) {
|
26
|
+
const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
|
27
|
+
return upb_MessageDef_FindFieldByNumber(entry, 2);
|
28
|
+
}
|
29
|
+
|
30
|
+
// -----------------------------------------------------------------------------
|
31
|
+
// StringBuilder, for inspect
|
32
|
+
// -----------------------------------------------------------------------------
|
33
|
+
|
34
|
+
struct StringBuilder {
|
35
|
+
size_t size;
|
36
|
+
size_t cap;
|
37
|
+
char *data;
|
38
|
+
};
|
40
39
|
|
41
|
-
|
42
|
-
VALUE str_rb = rb_str_new(str, size);
|
40
|
+
typedef struct StringBuilder StringBuilder;
|
43
41
|
|
44
|
-
|
45
|
-
|
46
|
-
rb_obj_freeze(str_rb);
|
47
|
-
return str_rb;
|
42
|
+
static size_t StringBuilder_SizeOf(size_t cap) {
|
43
|
+
return sizeof(StringBuilder) + cap;
|
48
44
|
}
|
49
45
|
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
StringBuilder *StringBuilder_New() {
|
47
|
+
const size_t cap = 128;
|
48
|
+
StringBuilder *builder = malloc(sizeof(*builder));
|
49
|
+
builder->size = 0;
|
50
|
+
builder->cap = cap;
|
51
|
+
builder->data = malloc(builder->cap);
|
52
|
+
return builder;
|
53
|
+
}
|
54
|
+
|
55
|
+
void StringBuilder_Free(StringBuilder *b) {
|
56
|
+
free(b->data);
|
57
|
+
free(b);
|
58
|
+
}
|
59
|
+
|
60
|
+
void StringBuilder_Printf(StringBuilder *b, const char *fmt, ...) {
|
61
|
+
size_t have = b->cap - b->size;
|
62
|
+
size_t n;
|
63
|
+
va_list args;
|
64
|
+
|
65
|
+
va_start(args, fmt);
|
66
|
+
n = vsnprintf(&b->data[b->size], have, fmt, args);
|
67
|
+
va_end(args);
|
68
|
+
|
69
|
+
if (have <= n) {
|
70
|
+
while (have <= n) {
|
71
|
+
b->cap *= 2;
|
72
|
+
have = b->cap - b->size;
|
73
|
+
}
|
74
|
+
b->data = realloc(b->data, StringBuilder_SizeOf(b->cap));
|
75
|
+
va_start(args, fmt);
|
76
|
+
n = vsnprintf(&b->data[b->size], have, fmt, args);
|
77
|
+
va_end(args);
|
78
|
+
PBRUBY_ASSERT(n < have);
|
79
|
+
}
|
80
|
+
|
81
|
+
b->size += n;
|
82
|
+
}
|
83
|
+
|
84
|
+
VALUE StringBuilder_ToRubyString(StringBuilder *b) {
|
85
|
+
VALUE ret = rb_str_new(b->data, b->size);
|
86
|
+
rb_enc_associate(ret, rb_utf8_encoding());
|
87
|
+
return ret;
|
88
|
+
}
|
89
|
+
|
90
|
+
static void StringBuilder_PrintEnum(StringBuilder *b, int32_t val,
|
91
|
+
const upb_EnumDef *e) {
|
92
|
+
const upb_EnumValueDef *ev = upb_EnumDef_FindValueByNumber(e, val);
|
93
|
+
if (ev) {
|
94
|
+
StringBuilder_Printf(b, ":%s", upb_EnumValueDef_Name(ev));
|
53
95
|
} else {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
96
|
+
StringBuilder_Printf(b, "%" PRId32, val);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
void StringBuilder_PrintMsgval(StringBuilder *b, upb_MessageValue val,
|
101
|
+
TypeInfo info) {
|
102
|
+
switch (info.type) {
|
103
|
+
case kUpb_CType_Bool:
|
104
|
+
StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
|
105
|
+
break;
|
106
|
+
case kUpb_CType_Float: {
|
107
|
+
VALUE str = rb_inspect(DBL2NUM(val.float_val));
|
108
|
+
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
109
|
+
break;
|
110
|
+
}
|
111
|
+
case kUpb_CType_Double: {
|
112
|
+
VALUE str = rb_inspect(DBL2NUM(val.double_val));
|
113
|
+
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
114
|
+
break;
|
115
|
+
}
|
116
|
+
case kUpb_CType_Int32:
|
117
|
+
StringBuilder_Printf(b, "%" PRId32, val.int32_val);
|
118
|
+
break;
|
119
|
+
case kUpb_CType_UInt32:
|
120
|
+
StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
|
121
|
+
break;
|
122
|
+
case kUpb_CType_Int64:
|
123
|
+
StringBuilder_Printf(b, "%" PRId64, val.int64_val);
|
124
|
+
break;
|
125
|
+
case kUpb_CType_UInt64:
|
126
|
+
StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
|
127
|
+
break;
|
128
|
+
case kUpb_CType_String:
|
129
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
130
|
+
val.str_val.data);
|
131
|
+
break;
|
132
|
+
case kUpb_CType_Bytes:
|
133
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
134
|
+
val.str_val.data);
|
135
|
+
break;
|
136
|
+
case kUpb_CType_Enum:
|
137
|
+
StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
|
138
|
+
break;
|
139
|
+
case kUpb_CType_Message:
|
140
|
+
Message_PrintMessage(b, val.msg_val, info.def.msgdef);
|
141
|
+
break;
|
61
142
|
}
|
62
143
|
}
|
63
144
|
|
64
145
|
// -----------------------------------------------------------------------------
|
65
|
-
//
|
146
|
+
// Arena
|
66
147
|
// -----------------------------------------------------------------------------
|
67
148
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
149
|
+
typedef struct {
|
150
|
+
upb_Arena *arena;
|
151
|
+
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
|
152
|
+
// macro to update VALUE references, as to trigger write barriers.
|
153
|
+
VALUE pinned_objs;
|
154
|
+
} Arena;
|
155
|
+
|
156
|
+
static void Arena_mark(void *data) {
|
157
|
+
Arena *arena = data;
|
158
|
+
rb_gc_mark(arena->pinned_objs);
|
159
|
+
}
|
160
|
+
|
161
|
+
static void Arena_free(void *data) {
|
162
|
+
Arena *arena = data;
|
163
|
+
upb_Arena_Free(arena->arena);
|
164
|
+
xfree(arena);
|
165
|
+
}
|
166
|
+
|
167
|
+
static size_t Arena_memsize(const void *data) {
|
168
|
+
const Arena *arena = data;
|
169
|
+
size_t fused_count;
|
170
|
+
size_t memsize = upb_Arena_SpaceAllocated(arena->arena, &fused_count);
|
171
|
+
if (fused_count > 1) {
|
172
|
+
// If other arena were fused we attribute an equal
|
173
|
+
// share of memory usage to each one.
|
174
|
+
memsize /= fused_count;
|
72
175
|
}
|
176
|
+
return memsize + sizeof(Arena);
|
73
177
|
}
|
74
178
|
|
75
|
-
|
76
|
-
// here.
|
77
|
-
rb_encoding* kRubyStringUtf8Encoding;
|
78
|
-
rb_encoding* kRubyStringASCIIEncoding;
|
79
|
-
rb_encoding* kRubyString8bitEncoding;
|
179
|
+
static VALUE cArena;
|
80
180
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
181
|
+
const rb_data_type_t Arena_type = {
|
182
|
+
"Google::Protobuf::Internal::Arena",
|
183
|
+
{Arena_mark, Arena_free, Arena_memsize},
|
184
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
185
|
+
};
|
186
|
+
|
187
|
+
static void *ruby_upb_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
|
188
|
+
size_t size) {
|
189
|
+
if (size == 0) {
|
190
|
+
xfree(ptr);
|
191
|
+
return NULL;
|
192
|
+
} else {
|
193
|
+
return xrealloc(ptr, size);
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};
|
198
|
+
|
199
|
+
static VALUE Arena_alloc(VALUE klass) {
|
200
|
+
Arena *arena = ALLOC(Arena);
|
201
|
+
arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
|
202
|
+
arena->pinned_objs = Qnil;
|
203
|
+
return TypedData_Wrap_Struct(klass, &Arena_type, arena);
|
204
|
+
}
|
205
|
+
|
206
|
+
upb_Arena *Arena_get(VALUE _arena) {
|
207
|
+
Arena *arena;
|
208
|
+
TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
|
209
|
+
return arena->arena;
|
210
|
+
}
|
211
|
+
|
212
|
+
void Arena_fuse(VALUE _arena, upb_Arena *other) {
|
213
|
+
Arena *arena;
|
214
|
+
TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
|
215
|
+
if (!upb_Arena_Fuse(arena->arena, other)) {
|
216
|
+
rb_raise(rb_eRuntimeError,
|
217
|
+
"Unable to fuse arenas. This should never happen since Ruby does "
|
218
|
+
"not use initial blocks");
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
VALUE Arena_new() { return Arena_alloc(cArena); }
|
223
|
+
|
224
|
+
void Arena_Pin(VALUE _arena, VALUE obj) {
|
225
|
+
Arena *arena;
|
226
|
+
TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
|
227
|
+
if (arena->pinned_objs == Qnil) {
|
228
|
+
RB_OBJ_WRITE(_arena, &arena->pinned_objs, rb_ary_new());
|
229
|
+
}
|
230
|
+
rb_ary_push(arena->pinned_objs, obj);
|
231
|
+
}
|
232
|
+
|
233
|
+
void Arena_register(VALUE module) {
|
234
|
+
VALUE internal = rb_define_module_under(module, "Internal");
|
235
|
+
VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
|
236
|
+
rb_define_alloc_func(klass, Arena_alloc);
|
237
|
+
rb_gc_register_address(&cArena);
|
238
|
+
cArena = klass;
|
239
|
+
}
|
240
|
+
|
241
|
+
// -----------------------------------------------------------------------------
|
242
|
+
// Object Cache
|
243
|
+
// -----------------------------------------------------------------------------
|
244
|
+
|
245
|
+
// Public ObjectCache API.
|
246
|
+
|
247
|
+
VALUE weak_obj_cache = Qnil;
|
248
|
+
ID item_get;
|
249
|
+
ID item_try_add;
|
250
|
+
|
251
|
+
static void ObjectCache_Init(VALUE protobuf) {
|
252
|
+
item_get = rb_intern("get");
|
253
|
+
item_try_add = rb_intern("try_add");
|
254
|
+
|
255
|
+
rb_gc_register_address(&weak_obj_cache);
|
256
|
+
VALUE internal = rb_const_get(protobuf, rb_intern("Internal"));
|
257
|
+
#if SIZEOF_LONG >= SIZEOF_VALUE
|
258
|
+
VALUE cache_class = rb_const_get(internal, rb_intern("ObjectCache"));
|
259
|
+
#else
|
260
|
+
VALUE cache_class = rb_const_get(internal, rb_intern("LegacyObjectCache"));
|
261
|
+
#endif
|
262
|
+
|
263
|
+
weak_obj_cache = rb_class_new_instance(0, NULL, cache_class);
|
264
|
+
rb_const_set(internal, rb_intern("OBJECT_CACHE"), weak_obj_cache);
|
265
|
+
rb_const_set(internal, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
|
266
|
+
rb_const_set(internal, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
|
267
|
+
}
|
268
|
+
|
269
|
+
static VALUE ObjectCache_GetKey(const void *key) {
|
270
|
+
VALUE key_val = (VALUE)key;
|
271
|
+
PBRUBY_ASSERT((key_val & 3) == 0);
|
272
|
+
// Ensure the key can be stored as a Fixnum since 1 bit is needed for
|
273
|
+
// FIXNUM_FLAG and 1 bit is needed for the sign bit.
|
274
|
+
VALUE new_key = LL2NUM(key_val >> 2);
|
275
|
+
PBRUBY_ASSERT(FIXNUM_P(new_key));
|
276
|
+
return new_key;
|
277
|
+
}
|
278
|
+
|
279
|
+
VALUE ObjectCache_TryAdd(const void *key, VALUE val) {
|
280
|
+
VALUE key_val = ObjectCache_GetKey(key);
|
281
|
+
return rb_funcall(weak_obj_cache, item_try_add, 2, key_val, val);
|
282
|
+
}
|
283
|
+
|
284
|
+
// Returns the cached object for this key, if any. Otherwise returns Qnil.
|
285
|
+
VALUE ObjectCache_Get(const void *key) {
|
286
|
+
VALUE key_val = ObjectCache_GetKey(key);
|
287
|
+
return rb_funcall(weak_obj_cache, item_get, 1, key_val);
|
288
|
+
}
|
289
|
+
|
290
|
+
/*
|
291
|
+
* call-seq:
|
292
|
+
* Google::Protobuf.discard_unknown(msg)
|
293
|
+
*
|
294
|
+
* Discard unknown fields in the given message object and recursively discard
|
295
|
+
* unknown fields in submessages.
|
296
|
+
*/
|
297
|
+
static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
|
298
|
+
const upb_MessageDef *m;
|
299
|
+
upb_Message *msg = Message_GetMutable(msg_rb, &m);
|
300
|
+
if (!upb_Message_DiscardUnknown(msg, m, 128)) {
|
301
|
+
rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
|
302
|
+
}
|
303
|
+
|
304
|
+
return Qnil;
|
305
|
+
}
|
306
|
+
|
307
|
+
/*
|
308
|
+
* call-seq:
|
309
|
+
* Google::Protobuf.deep_copy(obj) => copy_of_obj
|
310
|
+
*
|
311
|
+
* Performs a deep copy of a RepeatedField instance, a Map instance, or a
|
312
|
+
* message object, recursively copying its members.
|
313
|
+
*/
|
314
|
+
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
315
|
+
VALUE klass = CLASS_OF(obj);
|
316
|
+
if (klass == cRepeatedField) {
|
317
|
+
return RepeatedField_deep_copy(obj);
|
318
|
+
} else if (klass == cMap) {
|
319
|
+
return Map_deep_copy(obj);
|
320
|
+
} else {
|
321
|
+
VALUE new_arena_rb = Arena_new();
|
322
|
+
upb_Arena *new_arena = Arena_get(new_arena_rb);
|
323
|
+
const upb_MessageDef *m;
|
324
|
+
const upb_Message *msg = Message_Get(obj, &m);
|
325
|
+
upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
|
326
|
+
return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
|
327
|
+
}
|
328
|
+
}
|
89
329
|
|
90
330
|
// -----------------------------------------------------------------------------
|
91
331
|
// Initialization/entry point.
|
@@ -93,44 +333,24 @@ ID descriptor_instancevar_interned;
|
|
93
333
|
|
94
334
|
// This must be named "Init_protobuf_c" because the Ruby module is named
|
95
335
|
// "protobuf_c" -- the VM looks for this symbol in our .so.
|
96
|
-
void Init_protobuf_c() {
|
336
|
+
__attribute__((visibility("default"))) void Init_protobuf_c() {
|
97
337
|
VALUE google = rb_define_module("Google");
|
98
338
|
VALUE protobuf = rb_define_module_under(google, "Protobuf");
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
Descriptor_register(protobuf);
|
104
|
-
FileDescriptor_register(protobuf);
|
105
|
-
FieldDescriptor_register(protobuf);
|
106
|
-
OneofDescriptor_register(protobuf);
|
107
|
-
EnumDescriptor_register(protobuf);
|
108
|
-
MessageBuilderContext_register(internal);
|
109
|
-
OneofBuilderContext_register(internal);
|
110
|
-
EnumBuilderContext_register(internal);
|
111
|
-
FileBuilderContext_register(internal);
|
112
|
-
Builder_register(internal);
|
339
|
+
|
340
|
+
ObjectCache_Init(protobuf);
|
341
|
+
Arena_register(protobuf);
|
342
|
+
Defs_register(protobuf);
|
113
343
|
RepeatedField_register(protobuf);
|
114
344
|
Map_register(protobuf);
|
345
|
+
Message_register(protobuf);
|
115
346
|
|
116
|
-
cError = rb_const_get(protobuf, rb_intern("Error"));
|
117
347
|
cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
|
348
|
+
rb_gc_register_mark_object(cParseError);
|
118
349
|
cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
|
350
|
+
rb_gc_register_mark_object(cTypeError);
|
119
351
|
|
120
352
|
rb_define_singleton_method(protobuf, "discard_unknown",
|
121
353
|
Google_Protobuf_discard_unknown, 1);
|
122
|
-
rb_define_singleton_method(protobuf, "deep_copy",
|
123
|
-
|
124
|
-
|
125
|
-
kRubyStringUtf8Encoding = rb_utf8_encoding();
|
126
|
-
kRubyStringASCIIEncoding = rb_usascii_encoding();
|
127
|
-
kRubyString8bitEncoding = rb_ascii8bit_encoding();
|
128
|
-
|
129
|
-
rb_gc_register_address(&c_only_cookie);
|
130
|
-
c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
|
131
|
-
|
132
|
-
rb_gc_register_address(&cached_empty_string);
|
133
|
-
rb_gc_register_address(&cached_empty_bytes);
|
134
|
-
cached_empty_string = create_frozen_string("", 0, false);
|
135
|
-
cached_empty_bytes = create_frozen_string("", 0, true);
|
354
|
+
rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
|
355
|
+
1);
|
136
356
|
}
|