google-protobuf 3.14.0 → 3.15.0.rc.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/convert.c +349 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +924 -731
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +4 -5
- data/ext/google/protobuf_c/map.c +308 -456
- data/ext/google/protobuf_c/map.h +66 -0
- data/ext/google/protobuf_c/message.c +889 -433
- data/ext/google/protobuf_c/message.h +98 -0
- data/ext/google/protobuf_c/protobuf.c +325 -65
- data/ext/google/protobuf_c/protobuf.h +42 -597
- data/ext/google/protobuf_c/repeated_field.c +291 -321
- data/ext/google/protobuf_c/repeated_field.h +62 -0
- data/ext/google/protobuf_c/ruby-upb.c +8915 -0
- data/ext/google/protobuf_c/{upb.h → ruby-upb.h} +1362 -3687
- data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +145 -0
- data/tests/basic.rb +35 -6
- metadata +17 -12
- 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
@@ -0,0 +1,98 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
// https://developers.google.com/protocol-buffers/
|
4
|
+
//
|
5
|
+
// Redistribution and use in source and binary forms, with or without
|
6
|
+
// modification, are permitted provided that the following conditions are
|
7
|
+
// met:
|
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.
|
30
|
+
|
31
|
+
#ifndef RUBY_PROTOBUF_MESSAGE_H_
|
32
|
+
#define RUBY_PROTOBUF_MESSAGE_H_
|
33
|
+
|
34
|
+
#include <ruby/ruby.h>
|
35
|
+
|
36
|
+
#include "protobuf.h"
|
37
|
+
#include "ruby-upb.h"
|
38
|
+
|
39
|
+
// Gets the underlying upb_msg* and upb_msgdef for the given Ruby message
|
40
|
+
// wrapper. Requires that |value| is indeed a message object.
|
41
|
+
const upb_msg *Message_Get(VALUE value, const upb_msgdef **m);
|
42
|
+
|
43
|
+
// Like Message_Get(), but checks that the object is not frozen and returns a
|
44
|
+
// mutable pointer.
|
45
|
+
upb_msg *Message_GetMutable(VALUE value, const upb_msgdef **m);
|
46
|
+
|
47
|
+
// Returns the Arena object for this message.
|
48
|
+
VALUE Message_GetArena(VALUE value);
|
49
|
+
|
50
|
+
// Converts |value| into a upb_msg value of the expected upb_msgdef type,
|
51
|
+
// raising an error if this is not possible. Used when assigning |value| to a
|
52
|
+
// field of another message, which means the message must be of a particular
|
53
|
+
// type.
|
54
|
+
//
|
55
|
+
// This will perform automatic conversions in some cases (for example, Time ->
|
56
|
+
// Google::Protobuf::Timestamp). If any new message is created, it will be
|
57
|
+
// created on |arena|, and any existing message will have its arena fused with
|
58
|
+
// |arena|.
|
59
|
+
const upb_msg* Message_GetUpbMessage(VALUE value, const upb_msgdef* m,
|
60
|
+
const char* name, upb_arena* arena);
|
61
|
+
|
62
|
+
// Gets or constructs a Ruby wrapper object for the given message. The wrapper
|
63
|
+
// object will reference |arena| and ensure that it outlives this object.
|
64
|
+
VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena);
|
65
|
+
|
66
|
+
// Implements #inspect for this message, printing the text to |b|.
|
67
|
+
void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
|
68
|
+
const upb_msgdef* m);
|
69
|
+
|
70
|
+
// Returns a hash value for the given message.
|
71
|
+
uint64_t Message_Hash(const upb_msg *msg, const upb_msgdef *m, uint64_t seed);
|
72
|
+
|
73
|
+
// Returns a deep copy of the given message.
|
74
|
+
upb_msg* Message_deep_copy(const upb_msg* msg, const upb_msgdef* m,
|
75
|
+
upb_arena *arena);
|
76
|
+
|
77
|
+
// Returns true if these two messages are equal.
|
78
|
+
bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m);
|
79
|
+
|
80
|
+
// Checks that this Ruby object is a message, and raises an exception if not.
|
81
|
+
void Message_CheckClass(VALUE klass);
|
82
|
+
|
83
|
+
// Returns a new Hash object containing the contents of this message.
|
84
|
+
VALUE Scalar_CreateHash(upb_msgval val, TypeInfo type_info);
|
85
|
+
|
86
|
+
// Creates a message class or enum module for this descriptor, respectively.
|
87
|
+
VALUE build_class_from_descriptor(VALUE descriptor);
|
88
|
+
VALUE build_module_from_enumdesc(VALUE _enumdesc);
|
89
|
+
|
90
|
+
// Returns the Descriptor/EnumDescriptor for the given message class or enum
|
91
|
+
// module, respectively. Returns nil if this is not a message class or enum
|
92
|
+
// module.
|
93
|
+
VALUE MessageOrEnum_GetDescriptor(VALUE klass);
|
94
|
+
|
95
|
+
// Call at startup to register all types in this module.
|
96
|
+
void Message_register(VALUE protobuf);
|
97
|
+
|
98
|
+
#endif // RUBY_PROTOBUF_MESSAGE_H_
|
@@ -30,62 +30,342 @@
|
|
30
30
|
|
31
31
|
#include "protobuf.h"
|
32
32
|
|
33
|
+
#include <ruby/version.h>
|
34
|
+
|
35
|
+
#include "defs.h"
|
36
|
+
#include "map.h"
|
37
|
+
#include "message.h"
|
38
|
+
#include "repeated_field.h"
|
39
|
+
|
33
40
|
VALUE cError;
|
34
|
-
VALUE cParseError;
|
35
41
|
VALUE cTypeError;
|
36
|
-
VALUE c_only_cookie = Qnil;
|
37
42
|
|
38
|
-
|
39
|
-
|
43
|
+
const upb_fielddef* map_field_key(const upb_fielddef* field) {
|
44
|
+
const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
|
45
|
+
return upb_msgdef_itof(entry, 1);
|
46
|
+
}
|
47
|
+
|
48
|
+
const upb_fielddef* map_field_value(const upb_fielddef* field) {
|
49
|
+
const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
|
50
|
+
return upb_msgdef_itof(entry, 2);
|
51
|
+
}
|
40
52
|
|
41
|
-
|
42
|
-
|
53
|
+
// -----------------------------------------------------------------------------
|
54
|
+
// StringBuilder, for inspect
|
55
|
+
// -----------------------------------------------------------------------------
|
56
|
+
|
57
|
+
struct StringBuilder {
|
58
|
+
size_t size;
|
59
|
+
size_t cap;
|
60
|
+
char *data;
|
61
|
+
};
|
62
|
+
|
63
|
+
typedef struct StringBuilder StringBuilder;
|
64
|
+
|
65
|
+
static size_t StringBuilder_SizeOf(size_t cap) {
|
66
|
+
return sizeof(StringBuilder) + cap;
|
67
|
+
}
|
68
|
+
|
69
|
+
StringBuilder* StringBuilder_New() {
|
70
|
+
const size_t cap = 128;
|
71
|
+
StringBuilder* builder = malloc(sizeof(*builder));
|
72
|
+
builder->size = 0;
|
73
|
+
builder->cap = cap;
|
74
|
+
builder->data = malloc(builder->cap);
|
75
|
+
return builder;
|
76
|
+
}
|
77
|
+
|
78
|
+
void StringBuilder_Free(StringBuilder* b) {
|
79
|
+
free(b->data);
|
80
|
+
free(b);
|
81
|
+
}
|
82
|
+
|
83
|
+
void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...) {
|
84
|
+
size_t have = b->cap - b->size;
|
85
|
+
size_t n;
|
86
|
+
va_list args;
|
87
|
+
|
88
|
+
va_start(args, fmt);
|
89
|
+
n = vsnprintf(&b->data[b->size], have, fmt, args);
|
90
|
+
va_end(args);
|
91
|
+
|
92
|
+
if (have <= n) {
|
93
|
+
while (have <= n) {
|
94
|
+
b->cap *= 2;
|
95
|
+
have = b->cap - b->size;
|
96
|
+
}
|
97
|
+
b->data = realloc(b->data, StringBuilder_SizeOf(b->cap));
|
98
|
+
va_start(args, fmt);
|
99
|
+
n = vsnprintf(&b->data[b->size], have, fmt, args);
|
100
|
+
va_end(args);
|
101
|
+
PBRUBY_ASSERT(n < have);
|
102
|
+
}
|
103
|
+
|
104
|
+
b->size += n;
|
105
|
+
}
|
43
106
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
return
|
107
|
+
VALUE StringBuilder_ToRubyString(StringBuilder* b) {
|
108
|
+
VALUE ret = rb_str_new(b->data, b->size);
|
109
|
+
rb_enc_associate(ret, rb_utf8_encoding());
|
110
|
+
return ret;
|
48
111
|
}
|
49
112
|
|
50
|
-
|
51
|
-
|
52
|
-
|
113
|
+
static void StringBuilder_PrintEnum(StringBuilder* b, int32_t val,
|
114
|
+
const upb_enumdef* e) {
|
115
|
+
const char *name = upb_enumdef_iton(e, val);
|
116
|
+
if (name) {
|
117
|
+
StringBuilder_Printf(b, ":%s", name);
|
53
118
|
} else {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
119
|
+
StringBuilder_Printf(b, "%" PRId32, val);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val,
|
124
|
+
TypeInfo info) {
|
125
|
+
switch (info.type) {
|
126
|
+
case UPB_TYPE_BOOL:
|
127
|
+
StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
|
128
|
+
break;
|
129
|
+
case UPB_TYPE_FLOAT: {
|
130
|
+
VALUE str = rb_inspect(DBL2NUM(val.float_val));
|
131
|
+
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
132
|
+
break;
|
133
|
+
}
|
134
|
+
case UPB_TYPE_DOUBLE: {
|
135
|
+
VALUE str = rb_inspect(DBL2NUM(val.double_val));
|
136
|
+
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
137
|
+
break;
|
138
|
+
}
|
139
|
+
case UPB_TYPE_INT32:
|
140
|
+
StringBuilder_Printf(b, "%" PRId32, val.int32_val);
|
141
|
+
break;
|
142
|
+
case UPB_TYPE_UINT32:
|
143
|
+
StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
|
144
|
+
break;
|
145
|
+
case UPB_TYPE_INT64:
|
146
|
+
StringBuilder_Printf(b, "%" PRId64, val.int64_val);
|
147
|
+
break;
|
148
|
+
case UPB_TYPE_UINT64:
|
149
|
+
StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
|
150
|
+
break;
|
151
|
+
case UPB_TYPE_STRING:
|
152
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
|
153
|
+
break;
|
154
|
+
case UPB_TYPE_BYTES:
|
155
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
|
156
|
+
break;
|
157
|
+
case UPB_TYPE_ENUM:
|
158
|
+
StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
|
159
|
+
break;
|
160
|
+
case UPB_TYPE_MESSAGE:
|
161
|
+
Message_PrintMessage(b, val.msg_val, info.def.msgdef);
|
162
|
+
break;
|
61
163
|
}
|
62
164
|
}
|
63
165
|
|
64
166
|
// -----------------------------------------------------------------------------
|
65
|
-
//
|
167
|
+
// Arena
|
66
168
|
// -----------------------------------------------------------------------------
|
67
169
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
170
|
+
void Arena_free(void* data) { upb_arena_free(data); }
|
171
|
+
|
172
|
+
static VALUE cArena;
|
173
|
+
|
174
|
+
const rb_data_type_t Arena_type = {
|
175
|
+
"Google::Protobuf::Internal::Arena",
|
176
|
+
{ NULL, Arena_free, NULL },
|
177
|
+
};
|
178
|
+
|
179
|
+
static VALUE Arena_alloc(VALUE klass) {
|
180
|
+
upb_arena *arena = upb_arena_new();
|
181
|
+
return TypedData_Wrap_Struct(klass, &Arena_type, arena);
|
182
|
+
}
|
183
|
+
|
184
|
+
upb_arena *Arena_get(VALUE _arena) {
|
185
|
+
upb_arena *arena;
|
186
|
+
TypedData_Get_Struct(_arena, upb_arena, &Arena_type, arena);
|
187
|
+
return arena;
|
188
|
+
}
|
189
|
+
|
190
|
+
VALUE Arena_new() {
|
191
|
+
return Arena_alloc(cArena);
|
192
|
+
}
|
193
|
+
|
194
|
+
void Arena_register(VALUE module) {
|
195
|
+
VALUE internal = rb_define_module_under(module, "Internal");
|
196
|
+
VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
|
197
|
+
rb_define_alloc_func(klass, Arena_alloc);
|
198
|
+
rb_gc_register_address(&cArena);
|
199
|
+
cArena = klass;
|
73
200
|
}
|
74
201
|
|
75
|
-
//
|
76
|
-
//
|
77
|
-
|
78
|
-
rb_encoding* kRubyStringASCIIEncoding;
|
79
|
-
rb_encoding* kRubyString8bitEncoding;
|
202
|
+
// -----------------------------------------------------------------------------
|
203
|
+
// Object Cache
|
204
|
+
// -----------------------------------------------------------------------------
|
80
205
|
|
81
|
-
// Ruby
|
82
|
-
//
|
83
|
-
//
|
206
|
+
// A pointer -> Ruby Object cache that keeps references to Ruby wrapper
|
207
|
+
// objects. This allows us to look up any Ruby wrapper object by the address
|
208
|
+
// of the object it is wrapping. That way we can avoid ever creating two
|
209
|
+
// different wrapper objects for the same C object, which saves memory and
|
210
|
+
// preserves object identity.
|
84
211
|
//
|
85
|
-
// We
|
86
|
-
//
|
87
|
-
|
88
|
-
|
212
|
+
// We use Hash and/or WeakMap for the cache. WeakMap is faster overall
|
213
|
+
// (probably due to removal being integrated with GC) but doesn't work for Ruby
|
214
|
+
// <2.7 (see note below). We need Hash for Ruby <2.7 and for cases where we
|
215
|
+
// need to GC-root the object (notably when the object has been frozen).
|
216
|
+
|
217
|
+
#if RUBY_API_VERSION_CODE >= 20700
|
218
|
+
#define USE_WEAK_MAP 1
|
219
|
+
#else
|
220
|
+
#define USE_WEAK_MAP 0
|
221
|
+
#endif
|
222
|
+
|
223
|
+
static VALUE ObjectCache_GetKey(const void* key) {
|
224
|
+
char buf[sizeof(key)];
|
225
|
+
memcpy(&buf, &key, sizeof(key));
|
226
|
+
intptr_t key_int = (intptr_t)key;
|
227
|
+
PBRUBY_ASSERT((key_int & 3) == 0);
|
228
|
+
return LL2NUM(key_int >> 2);
|
229
|
+
}
|
230
|
+
|
231
|
+
// Strong object cache, uses regular Hash and GC-roots objects.
|
232
|
+
// - For Ruby <2.7, used for all objects.
|
233
|
+
// - For Ruby >=2.7, used only for frozen objects, so we preserve the "frozen"
|
234
|
+
// bit (since this information is not preserved at the upb level).
|
235
|
+
|
236
|
+
VALUE strong_obj_cache = Qnil;
|
237
|
+
|
238
|
+
static void StrongObjectCache_Init() {
|
239
|
+
rb_gc_register_address(&strong_obj_cache);
|
240
|
+
strong_obj_cache = rb_hash_new();
|
241
|
+
}
|
242
|
+
|
243
|
+
static void StrongObjectCache_Remove(void* key) {
|
244
|
+
VALUE key_rb = ObjectCache_GetKey(key);
|
245
|
+
PBRUBY_ASSERT(rb_hash_lookup(strong_obj_cache, key_rb) != Qnil);
|
246
|
+
rb_hash_delete(strong_obj_cache, key_rb);
|
247
|
+
}
|
248
|
+
|
249
|
+
static VALUE StrongObjectCache_Get(const void* key) {
|
250
|
+
VALUE key_rb = ObjectCache_GetKey(key);
|
251
|
+
return rb_hash_lookup(strong_obj_cache, key_rb);
|
252
|
+
}
|
253
|
+
|
254
|
+
static void StrongObjectCache_Add(const void* key, VALUE val,
|
255
|
+
upb_arena* arena) {
|
256
|
+
PBRUBY_ASSERT(StrongObjectCache_Get(key) == Qnil);
|
257
|
+
VALUE key_rb = ObjectCache_GetKey(key);
|
258
|
+
rb_hash_aset(strong_obj_cache, key_rb, val);
|
259
|
+
upb_arena_addcleanup(arena, (void*)key, StrongObjectCache_Remove);
|
260
|
+
}
|
261
|
+
|
262
|
+
// Weak object cache. This speeds up the test suite significantly, so we
|
263
|
+
// presume it speeds up real code also. However we can only use it in Ruby
|
264
|
+
// >=2.7 due to:
|
265
|
+
// https://bugs.ruby-lang.org/issues/16035
|
266
|
+
|
267
|
+
#if USE_WEAK_MAP
|
268
|
+
|
269
|
+
VALUE weak_obj_cache = Qnil;
|
270
|
+
|
271
|
+
static void WeakObjectCache_Init() {
|
272
|
+
rb_gc_register_address(&weak_obj_cache);
|
273
|
+
VALUE klass = rb_eval_string("ObjectSpace::WeakMap");
|
274
|
+
weak_obj_cache = rb_class_new_instance(0, NULL, klass);
|
275
|
+
}
|
276
|
+
|
277
|
+
static VALUE WeakObjectCache_Get(const void* key) {
|
278
|
+
VALUE key_rb = ObjectCache_GetKey(key);
|
279
|
+
VALUE ret = rb_funcall(weak_obj_cache, rb_intern("[]"), 1, key_rb);
|
280
|
+
return ret;
|
281
|
+
}
|
282
|
+
|
283
|
+
static void WeakObjectCache_Add(const void* key, VALUE val) {
|
284
|
+
PBRUBY_ASSERT(WeakObjectCache_Get(key) == Qnil);
|
285
|
+
VALUE key_rb = ObjectCache_GetKey(key);
|
286
|
+
rb_funcall(weak_obj_cache, rb_intern("[]="), 2, key_rb, val);
|
287
|
+
PBRUBY_ASSERT(WeakObjectCache_Get(key) == val);
|
288
|
+
}
|
289
|
+
|
290
|
+
#endif
|
291
|
+
|
292
|
+
// Public ObjectCache API.
|
293
|
+
|
294
|
+
static void ObjectCache_Init() {
|
295
|
+
StrongObjectCache_Init();
|
296
|
+
#if USE_WEAK_MAP
|
297
|
+
WeakObjectCache_Init();
|
298
|
+
#endif
|
299
|
+
}
|
300
|
+
|
301
|
+
void ObjectCache_Add(const void* key, VALUE val, upb_arena *arena) {
|
302
|
+
#if USE_WEAK_MAP
|
303
|
+
(void)arena;
|
304
|
+
WeakObjectCache_Add(key, val);
|
305
|
+
#else
|
306
|
+
StrongObjectCache_Add(key, val, arena);
|
307
|
+
#endif
|
308
|
+
}
|
309
|
+
|
310
|
+
// Returns the cached object for this key, if any. Otherwise returns Qnil.
|
311
|
+
VALUE ObjectCache_Get(const void* key) {
|
312
|
+
#if USE_WEAK_MAP
|
313
|
+
return WeakObjectCache_Get(key);
|
314
|
+
#else
|
315
|
+
return StrongObjectCache_Get(key);
|
316
|
+
#endif
|
317
|
+
}
|
318
|
+
|
319
|
+
void ObjectCache_Pin(const void* key, VALUE val, upb_arena *arena) {
|
320
|
+
#if USE_WEAK_MAP
|
321
|
+
PBRUBY_ASSERT(WeakObjectCache_Get(key) == val);
|
322
|
+
// This will GC-root the object, but we'll still use the weak map for
|
323
|
+
// actual lookup.
|
324
|
+
StrongObjectCache_Add(key, val, arena);
|
325
|
+
#else
|
326
|
+
// Value is already pinned, nothing to do.
|
327
|
+
#endif
|
328
|
+
}
|
329
|
+
|
330
|
+
/*
|
331
|
+
* call-seq:
|
332
|
+
* Google::Protobuf.discard_unknown(msg)
|
333
|
+
*
|
334
|
+
* Discard unknown fields in the given message object and recursively discard
|
335
|
+
* unknown fields in submessages.
|
336
|
+
*/
|
337
|
+
static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
|
338
|
+
const upb_msgdef *m;
|
339
|
+
upb_msg *msg = Message_GetMutable(msg_rb, &m);
|
340
|
+
if (!upb_msg_discardunknown(msg, m, 128)) {
|
341
|
+
rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
|
342
|
+
}
|
343
|
+
|
344
|
+
return Qnil;
|
345
|
+
}
|
346
|
+
|
347
|
+
/*
|
348
|
+
* call-seq:
|
349
|
+
* Google::Protobuf.deep_copy(obj) => copy_of_obj
|
350
|
+
*
|
351
|
+
* Performs a deep copy of a RepeatedField instance, a Map instance, or a
|
352
|
+
* message object, recursively copying its members.
|
353
|
+
*/
|
354
|
+
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
355
|
+
VALUE klass = CLASS_OF(obj);
|
356
|
+
if (klass == cRepeatedField) {
|
357
|
+
return RepeatedField_deep_copy(obj);
|
358
|
+
} else if (klass == cMap) {
|
359
|
+
return Map_deep_copy(obj);
|
360
|
+
} else {
|
361
|
+
VALUE new_arena_rb = Arena_new();
|
362
|
+
upb_arena *new_arena = Arena_get(new_arena_rb);
|
363
|
+
const upb_msgdef *m;
|
364
|
+
const upb_msg *msg = Message_Get(obj, &m);
|
365
|
+
upb_msg* new_msg = Message_deep_copy(msg, m, new_arena);
|
366
|
+
return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
|
367
|
+
}
|
368
|
+
}
|
89
369
|
|
90
370
|
// -----------------------------------------------------------------------------
|
91
371
|
// Initialization/entry point.
|
@@ -93,44 +373,24 @@ ID descriptor_instancevar_interned;
|
|
93
373
|
|
94
374
|
// This must be named "Init_protobuf_c" because the Ruby module is named
|
95
375
|
// "protobuf_c" -- the VM looks for this symbol in our .so.
|
376
|
+
__attribute__ ((visibility ("default")))
|
96
377
|
void Init_protobuf_c() {
|
378
|
+
ObjectCache_Init();
|
379
|
+
|
97
380
|
VALUE google = rb_define_module("Google");
|
98
381
|
VALUE protobuf = rb_define_module_under(google, "Protobuf");
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
DescriptorPool_register(protobuf);
|
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);
|
382
|
+
|
383
|
+
Arena_register(protobuf);
|
384
|
+
Defs_register(protobuf);
|
113
385
|
RepeatedField_register(protobuf);
|
114
386
|
Map_register(protobuf);
|
387
|
+
Message_register(protobuf);
|
115
388
|
|
116
389
|
cError = rb_const_get(protobuf, rb_intern("Error"));
|
117
|
-
cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
|
118
390
|
cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
|
119
391
|
|
120
392
|
rb_define_singleton_method(protobuf, "discard_unknown",
|
121
393
|
Google_Protobuf_discard_unknown, 1);
|
122
394
|
rb_define_singleton_method(protobuf, "deep_copy",
|
123
395
|
Google_Protobuf_deep_copy, 1);
|
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);
|
136
396
|
}
|