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