google-protobuf 3.14.0 → 3.25.8

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +317 -0
  4. data/ext/google/protobuf_c/convert.h +50 -0
  5. data/ext/google/protobuf_c/defs.c +743 -1706
  6. data/ext/google/protobuf_c/defs.h +82 -0
  7. data/ext/google/protobuf_c/extconf.rb +15 -8
  8. data/ext/google/protobuf_c/glue.c +56 -0
  9. data/ext/google/protobuf_c/map.c +336 -486
  10. data/ext/google/protobuf_c/map.h +44 -0
  11. data/ext/google/protobuf_c/message.c +1088 -518
  12. data/ext/google/protobuf_c/message.h +86 -0
  13. data/ext/google/protobuf_c/protobuf.c +301 -94
  14. data/ext/google/protobuf_c/protobuf.h +66 -621
  15. data/ext/google/protobuf_c/repeated_field.c +323 -353
  16. data/ext/google/protobuf_c/repeated_field.h +41 -0
  17. data/ext/google/protobuf_c/ruby-upb.c +14440 -0
  18. data/ext/google/protobuf_c/ruby-upb.h +13044 -0
  19. data/ext/google/protobuf_c/shared_convert.c +64 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +66 -0
  22. data/ext/google/protobuf_c/shared_message.h +25 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  29. data/lib/google/protobuf/any_pb.rb +24 -5
  30. data/lib/google/protobuf/api_pb.rb +27 -23
  31. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  32. data/lib/google/protobuf/descriptor_pb.rb +86 -0
  33. data/lib/google/protobuf/duration_pb.rb +24 -5
  34. data/lib/google/protobuf/empty_pb.rb +24 -3
  35. data/lib/google/protobuf/ffi/descriptor.rb +165 -0
  36. data/lib/google/protobuf/ffi/descriptor_pool.rb +75 -0
  37. data/lib/google/protobuf/ffi/enum_descriptor.rb +171 -0
  38. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  39. data/lib/google/protobuf/ffi/field_descriptor.rb +319 -0
  40. data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
  41. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  42. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  43. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  44. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  45. data/lib/google/protobuf/ffi/map.rb +407 -0
  46. data/lib/google/protobuf/ffi/message.rb +662 -0
  47. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  48. data/lib/google/protobuf/ffi/oneof_descriptor.rb +95 -0
  49. data/lib/google/protobuf/ffi/repeated_field.rb +383 -0
  50. data/lib/google/protobuf/field_mask_pb.rb +24 -4
  51. data/lib/google/protobuf/message_exts.rb +10 -28
  52. data/lib/google/protobuf/object_cache.rb +97 -0
  53. data/lib/google/protobuf/plugin_pb.rb +47 -0
  54. data/lib/google/protobuf/repeated_field.rb +18 -28
  55. data/lib/google/protobuf/source_context_pb.rb +24 -4
  56. data/lib/google/protobuf/struct_pb.rb +24 -20
  57. data/lib/google/protobuf/timestamp_pb.rb +24 -5
  58. data/lib/google/protobuf/type_pb.rb +27 -68
  59. data/lib/google/protobuf/well_known_types.rb +17 -36
  60. data/lib/google/protobuf/wrappers_pb.rb +24 -28
  61. data/lib/google/protobuf.rb +32 -118
  62. data/lib/google/protobuf_ffi.rb +50 -0
  63. data/lib/google/protobuf_native.rb +20 -0
  64. data/lib/google/tasks/ffi.rake +102 -0
  65. metadata +92 -38
  66. data/ext/google/protobuf_c/encode_decode.c +0 -1795
  67. data/ext/google/protobuf_c/storage.c +0 -1198
  68. data/ext/google/protobuf_c/upb.c +0 -13817
  69. data/ext/google/protobuf_c/upb.h +0 -6777
  70. data/tests/basic.rb +0 -543
  71. data/tests/generated_code_test.rb +0 -23
  72. 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_internal_deep_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,318 @@
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
- // 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.
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
- VALUE cError;
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
- static VALUE cached_empty_string = Qnil;
39
- static VALUE cached_empty_bytes = Qnil;
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
+ // -----------------------------------------------------------------------------
40
33
 
41
- static VALUE create_frozen_string(const char* str, size_t size, bool binary) {
42
- VALUE str_rb = rb_str_new(str, size);
34
+ struct StringBuilder {
35
+ size_t size;
36
+ size_t cap;
37
+ char *data;
38
+ };
43
39
 
44
- rb_enc_associate(str_rb,
45
- binary ? kRubyString8bitEncoding : kRubyStringUtf8Encoding);
46
- rb_obj_freeze(str_rb);
47
- return str_rb;
40
+ typedef struct StringBuilder StringBuilder;
41
+
42
+ static size_t StringBuilder_SizeOf(size_t cap) {
43
+ return sizeof(StringBuilder) + cap;
48
44
  }
49
45
 
50
- VALUE get_frozen_string(const char* str, size_t size, bool binary) {
51
- if (size == 0) {
52
- return binary ? cached_empty_bytes : cached_empty_string;
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
- // It is harder to memoize non-empty strings. The obvious approach would be
55
- // to use a Ruby hash keyed by string as memo table, but looking up in such a table
56
- // requires constructing a string (the very thing we're trying to avoid).
57
- //
58
- // Since few fields have defaults, we will just optimize the empty string
59
- // case for now.
60
- return create_frozen_string(str, size, binary);
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
- // Utilities.
146
+ // Arena
66
147
  // -----------------------------------------------------------------------------
67
148
 
68
- // Raises a Ruby error if |status| is not OK, using its error message.
69
- void check_upb_status(const upb_status* status, const char* msg) {
70
- if (!upb_ok(status)) {
71
- rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
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 VALUE cArena;
168
+
169
+ const rb_data_type_t Arena_type = {
170
+ "Google::Protobuf::Internal::Arena",
171
+ {Arena_mark, Arena_free, NULL},
172
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
173
+ };
174
+
175
+ static void *ruby_upb_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
176
+ size_t size) {
177
+ if (size == 0) {
178
+ xfree(ptr);
179
+ return NULL;
180
+ } else {
181
+ return xrealloc(ptr, size);
72
182
  }
73
183
  }
74
184
 
75
- // String encodings: we look these up once, at load time, and then cache them
76
- // here.
77
- rb_encoding* kRubyStringUtf8Encoding;
78
- rb_encoding* kRubyStringASCIIEncoding;
79
- rb_encoding* kRubyString8bitEncoding;
185
+ upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};
80
186
 
81
- // Ruby-interned string: "descriptor". We use this identifier to store an
82
- // instance variable on message classes we create in order to link them back to
83
- // their descriptors.
84
- //
85
- // We intern this once at module load time then use the interned identifier at
86
- // runtime in order to avoid the cost of repeatedly interning in hot paths.
87
- const char* kDescriptorInstanceVar = "descriptor";
88
- ID descriptor_instancevar_interned;
187
+ static VALUE Arena_alloc(VALUE klass) {
188
+ Arena *arena = ALLOC(Arena);
189
+ arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
190
+ arena->pinned_objs = Qnil;
191
+ return TypedData_Wrap_Struct(klass, &Arena_type, arena);
192
+ }
193
+
194
+ upb_Arena *Arena_get(VALUE _arena) {
195
+ Arena *arena;
196
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
197
+ return arena->arena;
198
+ }
199
+
200
+ void Arena_fuse(VALUE _arena, upb_Arena *other) {
201
+ Arena *arena;
202
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
203
+ if (!upb_Arena_Fuse(arena->arena, other)) {
204
+ rb_raise(rb_eRuntimeError,
205
+ "Unable to fuse arenas. This should never happen since Ruby does "
206
+ "not use initial blocks");
207
+ }
208
+ }
209
+
210
+ VALUE Arena_new() { return Arena_alloc(cArena); }
211
+
212
+ void Arena_Pin(VALUE _arena, VALUE obj) {
213
+ Arena *arena;
214
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
215
+ if (arena->pinned_objs == Qnil) {
216
+ RB_OBJ_WRITE(_arena, &arena->pinned_objs, rb_ary_new());
217
+ }
218
+ rb_ary_push(arena->pinned_objs, obj);
219
+ }
220
+
221
+ void Arena_register(VALUE module) {
222
+ VALUE internal = rb_define_module_under(module, "Internal");
223
+ VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
224
+ rb_define_alloc_func(klass, Arena_alloc);
225
+ rb_gc_register_address(&cArena);
226
+ cArena = klass;
227
+ }
228
+
229
+ // -----------------------------------------------------------------------------
230
+ // Object Cache
231
+ // -----------------------------------------------------------------------------
232
+
233
+ // Public ObjectCache API.
234
+
235
+ VALUE weak_obj_cache = Qnil;
236
+ ID item_get;
237
+ ID item_try_add;
238
+
239
+ static void ObjectCache_Init(VALUE protobuf) {
240
+ item_get = rb_intern("get");
241
+ item_try_add = rb_intern("try_add");
242
+
243
+ rb_gc_register_address(&weak_obj_cache);
244
+ #if SIZEOF_LONG >= SIZEOF_VALUE
245
+ VALUE cache_class = rb_const_get(protobuf, rb_intern("ObjectCache"));
246
+ #else
247
+ VALUE cache_class = rb_const_get(protobuf, rb_intern("LegacyObjectCache"));
248
+ #endif
249
+
250
+ weak_obj_cache = rb_class_new_instance(0, NULL, cache_class);
251
+ rb_const_set(protobuf, rb_intern("OBJECT_CACHE"), weak_obj_cache);
252
+ rb_const_set(protobuf, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
253
+ rb_const_set(protobuf, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
254
+ }
255
+
256
+ static VALUE ObjectCache_GetKey(const void *key) {
257
+ VALUE key_val = (VALUE)key;
258
+ PBRUBY_ASSERT((key_val & 3) == 0);
259
+ // Ensure the key can be stored as a Fixnum since 1 bit is needed for
260
+ // FIXNUM_FLAG and 1 bit is needed for the sign bit.
261
+ VALUE new_key = LL2NUM(key_val >> 2);
262
+ PBRUBY_ASSERT(FIXNUM_P(new_key));
263
+ return new_key;
264
+ }
265
+
266
+ VALUE ObjectCache_TryAdd(const void *key, VALUE val) {
267
+ VALUE key_val = ObjectCache_GetKey(key);
268
+ return rb_funcall(weak_obj_cache, item_try_add, 2, key_val, val);
269
+ }
270
+
271
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
272
+ VALUE ObjectCache_Get(const void *key) {
273
+ VALUE key_val = ObjectCache_GetKey(key);
274
+ return rb_funcall(weak_obj_cache, item_get, 1, key_val);
275
+ }
276
+
277
+ /*
278
+ * call-seq:
279
+ * Google::Protobuf.discard_unknown(msg)
280
+ *
281
+ * Discard unknown fields in the given message object and recursively discard
282
+ * unknown fields in submessages.
283
+ */
284
+ static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
285
+ const upb_MessageDef *m;
286
+ upb_Message *msg = Message_GetMutable(msg_rb, &m);
287
+ if (!upb_Message_DiscardUnknown(msg, m, 128)) {
288
+ rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
289
+ }
290
+
291
+ return Qnil;
292
+ }
293
+
294
+ /*
295
+ * call-seq:
296
+ * Google::Protobuf.deep_copy(obj) => copy_of_obj
297
+ *
298
+ * Performs a deep copy of a RepeatedField instance, a Map instance, or a
299
+ * message object, recursively copying its members.
300
+ */
301
+ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
302
+ VALUE klass = CLASS_OF(obj);
303
+ if (klass == cRepeatedField) {
304
+ return RepeatedField_deep_copy(obj);
305
+ } else if (klass == cMap) {
306
+ return Map_deep_copy(obj);
307
+ } else {
308
+ VALUE new_arena_rb = Arena_new();
309
+ upb_Arena *new_arena = Arena_get(new_arena_rb);
310
+ const upb_MessageDef *m;
311
+ const upb_Message *msg = Message_Get(obj, &m);
312
+ upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
313
+ return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
314
+ }
315
+ }
89
316
 
90
317
  // -----------------------------------------------------------------------------
91
318
  // Initialization/entry point.
@@ -93,44 +320,24 @@ ID descriptor_instancevar_interned;
93
320
 
94
321
  // This must be named "Init_protobuf_c" because the Ruby module is named
95
322
  // "protobuf_c" -- the VM looks for this symbol in our .so.
96
- void Init_protobuf_c() {
323
+ __attribute__((visibility("default"))) void Init_protobuf_c() {
97
324
  VALUE google = rb_define_module("Google");
98
325
  VALUE protobuf = rb_define_module_under(google, "Protobuf");
99
- VALUE internal = rb_define_module_under(protobuf, "Internal");
100
-
101
- descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar);
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);
326
+
327
+ ObjectCache_Init(protobuf);
328
+ Arena_register(protobuf);
329
+ Defs_register(protobuf);
113
330
  RepeatedField_register(protobuf);
114
331
  Map_register(protobuf);
332
+ Message_register(protobuf);
115
333
 
116
- cError = rb_const_get(protobuf, rb_intern("Error"));
117
334
  cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
335
+ rb_gc_register_mark_object(cParseError);
118
336
  cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
337
+ rb_gc_register_mark_object(cTypeError);
119
338
 
120
339
  rb_define_singleton_method(protobuf, "discard_unknown",
121
340
  Google_Protobuf_discard_unknown, 1);
122
- rb_define_singleton_method(protobuf, "deep_copy",
123
- 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);
341
+ rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
342
+ 1);
136
343
  }