google-protobuf 3.19.6-x86-linux → 3.20.0.rc.1-x86-linux
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 +124 -111
- data/ext/google/protobuf_c/convert.h +12 -9
- data/ext/google/protobuf_c/defs.c +196 -201
- data/ext/google/protobuf_c/defs.h +19 -19
- data/ext/google/protobuf_c/extconf.rb +11 -3
- data/ext/google/protobuf_c/map.c +109 -101
- data/ext/google/protobuf_c/map.h +7 -8
- data/ext/google/protobuf_c/message.c +382 -303
- data/ext/google/protobuf_c/message.h +22 -19
- data/ext/google/protobuf_c/protobuf.c +57 -58
- data/ext/google/protobuf_c/protobuf.h +13 -10
- data/ext/google/protobuf_c/repeated_field.c +82 -84
- data/ext/google/protobuf_c/repeated_field.h +6 -6
- data/ext/google/protobuf_c/ruby-upb.c +4945 -3084
- data/ext/google/protobuf_c/ruby-upb.h +2285 -1701
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +395 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/2.5/protobuf_c.so +0 -0
- data/lib/google/2.6/protobuf_c.so +0 -0
- data/lib/google/2.7/protobuf_c.so +0 -0
- data/lib/google/3.0/protobuf_c.so +0 -0
- data/lib/google/protobuf/descriptor_dsl.rb +8 -1
- data/lib/google/protobuf/descriptor_pb.rb +3 -2
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/well_known_types.rb +11 -6
- data/lib/google/protobuf.rb +4 -4
- data/tests/basic.rb +14 -0
- metadata +8 -5
@@ -36,55 +36,58 @@
|
|
36
36
|
#include "protobuf.h"
|
37
37
|
#include "ruby-upb.h"
|
38
38
|
|
39
|
-
// Gets the underlying
|
40
|
-
// wrapper. Requires that |value| is indeed a message object.
|
41
|
-
const
|
39
|
+
// Gets the underlying upb_Message* and upb_MessageDef for the given Ruby
|
40
|
+
// message wrapper. Requires that |value| is indeed a message object.
|
41
|
+
const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m);
|
42
42
|
|
43
43
|
// Like Message_Get(), but checks that the object is not frozen and returns a
|
44
44
|
// mutable pointer.
|
45
|
-
|
45
|
+
upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m);
|
46
46
|
|
47
47
|
// Returns the Arena object for this message.
|
48
48
|
VALUE Message_GetArena(VALUE value);
|
49
49
|
|
50
|
-
// Converts |value| into a
|
51
|
-
// raising an error if this is not possible. Used when assigning |value|
|
52
|
-
// field of another message, which means the message must be of a
|
53
|
-
// type.
|
50
|
+
// Converts |value| into a upb_Message value of the expected upb_MessageDef
|
51
|
+
// type, raising an error if this is not possible. Used when assigning |value|
|
52
|
+
// to a field of another message, which means the message must be of a
|
53
|
+
// particular type.
|
54
54
|
//
|
55
55
|
// This will perform automatic conversions in some cases (for example, Time ->
|
56
56
|
// Google::Protobuf::Timestamp). If any new message is created, it will be
|
57
57
|
// created on |arena|, and any existing message will have its arena fused with
|
58
58
|
// |arena|.
|
59
|
-
const
|
60
|
-
|
59
|
+
const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
|
60
|
+
const char* name, upb_Arena* arena);
|
61
61
|
|
62
62
|
// Gets or constructs a Ruby wrapper object for the given message. The wrapper
|
63
63
|
// object will reference |arena| and ensure that it outlives this object.
|
64
|
-
VALUE Message_GetRubyWrapper(
|
64
|
+
VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
|
65
|
+
VALUE arena);
|
65
66
|
|
66
67
|
// Gets the given field from this message.
|
67
|
-
VALUE Message_getfield(VALUE _self, const
|
68
|
+
VALUE Message_getfield(VALUE _self, const upb_FieldDef* f);
|
68
69
|
|
69
70
|
// Implements #inspect for this message, printing the text to |b|.
|
70
|
-
void Message_PrintMessage(StringBuilder* b, const
|
71
|
-
const
|
71
|
+
void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
|
72
|
+
const upb_MessageDef* m);
|
72
73
|
|
73
74
|
// Returns a hash value for the given message.
|
74
|
-
uint64_t Message_Hash(const
|
75
|
+
uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
76
|
+
uint64_t seed);
|
75
77
|
|
76
78
|
// Returns a deep copy of the given message.
|
77
|
-
|
78
|
-
|
79
|
+
upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
80
|
+
upb_Arena* arena);
|
79
81
|
|
80
82
|
// Returns true if these two messages are equal.
|
81
|
-
bool Message_Equal(const
|
83
|
+
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
84
|
+
const upb_MessageDef* m);
|
82
85
|
|
83
86
|
// Checks that this Ruby object is a message, and raises an exception if not.
|
84
87
|
void Message_CheckClass(VALUE klass);
|
85
88
|
|
86
89
|
// Returns a new Hash object containing the contents of this message.
|
87
|
-
VALUE Scalar_CreateHash(
|
90
|
+
VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info);
|
88
91
|
|
89
92
|
// Creates a message class or enum module for this descriptor, respectively.
|
90
93
|
VALUE build_class_from_descriptor(VALUE descriptor);
|
@@ -40,14 +40,14 @@
|
|
40
40
|
VALUE cParseError;
|
41
41
|
VALUE cTypeError;
|
42
42
|
|
43
|
-
const
|
44
|
-
const
|
45
|
-
return
|
43
|
+
const upb_FieldDef *map_field_key(const upb_FieldDef *field) {
|
44
|
+
const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
|
45
|
+
return upb_MessageDef_FindFieldByNumberWithSize(entry, 1);
|
46
46
|
}
|
47
47
|
|
48
|
-
const
|
49
|
-
const
|
50
|
-
return
|
48
|
+
const upb_FieldDef *map_field_value(const upb_FieldDef *field) {
|
49
|
+
const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
|
50
|
+
return upb_MessageDef_FindFieldByNumberWithSize(entry, 2);
|
51
51
|
}
|
52
52
|
|
53
53
|
// -----------------------------------------------------------------------------
|
@@ -66,21 +66,21 @@ static size_t StringBuilder_SizeOf(size_t cap) {
|
|
66
66
|
return sizeof(StringBuilder) + cap;
|
67
67
|
}
|
68
68
|
|
69
|
-
StringBuilder*
|
69
|
+
StringBuilder *StringBuilder_New() {
|
70
70
|
const size_t cap = 128;
|
71
|
-
StringBuilder*
|
71
|
+
StringBuilder *builder = malloc(sizeof(*builder));
|
72
72
|
builder->size = 0;
|
73
73
|
builder->cap = cap;
|
74
74
|
builder->data = malloc(builder->cap);
|
75
75
|
return builder;
|
76
76
|
}
|
77
77
|
|
78
|
-
void StringBuilder_Free(StringBuilder*
|
78
|
+
void StringBuilder_Free(StringBuilder *b) {
|
79
79
|
free(b->data);
|
80
80
|
free(b);
|
81
81
|
}
|
82
82
|
|
83
|
-
void StringBuilder_Printf(StringBuilder*
|
83
|
+
void StringBuilder_Printf(StringBuilder *b, const char *fmt, ...) {
|
84
84
|
size_t have = b->cap - b->size;
|
85
85
|
size_t n;
|
86
86
|
va_list args;
|
@@ -104,60 +104,62 @@ void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...) {
|
|
104
104
|
b->size += n;
|
105
105
|
}
|
106
106
|
|
107
|
-
VALUE StringBuilder_ToRubyString(StringBuilder*
|
107
|
+
VALUE StringBuilder_ToRubyString(StringBuilder *b) {
|
108
108
|
VALUE ret = rb_str_new(b->data, b->size);
|
109
109
|
rb_enc_associate(ret, rb_utf8_encoding());
|
110
110
|
return ret;
|
111
111
|
}
|
112
112
|
|
113
|
-
static void StringBuilder_PrintEnum(StringBuilder*
|
114
|
-
const
|
115
|
-
const
|
116
|
-
if (
|
117
|
-
StringBuilder_Printf(b, ":%s",
|
113
|
+
static void StringBuilder_PrintEnum(StringBuilder *b, int32_t val,
|
114
|
+
const upb_EnumDef *e) {
|
115
|
+
const upb_EnumValueDef *ev = upb_EnumDef_FindValueByNumber(e, val);
|
116
|
+
if (ev) {
|
117
|
+
StringBuilder_Printf(b, ":%s", upb_EnumValueDef_Name(ev));
|
118
118
|
} else {
|
119
119
|
StringBuilder_Printf(b, "%" PRId32, val);
|
120
120
|
}
|
121
121
|
}
|
122
122
|
|
123
|
-
void StringBuilder_PrintMsgval(StringBuilder*
|
123
|
+
void StringBuilder_PrintMsgval(StringBuilder *b, upb_MessageValue val,
|
124
124
|
TypeInfo info) {
|
125
125
|
switch (info.type) {
|
126
|
-
case
|
126
|
+
case kUpb_CType_Bool:
|
127
127
|
StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
|
128
128
|
break;
|
129
|
-
case
|
129
|
+
case kUpb_CType_Float: {
|
130
130
|
VALUE str = rb_inspect(DBL2NUM(val.float_val));
|
131
131
|
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
132
132
|
break;
|
133
133
|
}
|
134
|
-
case
|
134
|
+
case kUpb_CType_Double: {
|
135
135
|
VALUE str = rb_inspect(DBL2NUM(val.double_val));
|
136
136
|
StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
|
137
137
|
break;
|
138
138
|
}
|
139
|
-
case
|
139
|
+
case kUpb_CType_Int32:
|
140
140
|
StringBuilder_Printf(b, "%" PRId32, val.int32_val);
|
141
141
|
break;
|
142
|
-
case
|
142
|
+
case kUpb_CType_UInt32:
|
143
143
|
StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
|
144
144
|
break;
|
145
|
-
case
|
145
|
+
case kUpb_CType_Int64:
|
146
146
|
StringBuilder_Printf(b, "%" PRId64, val.int64_val);
|
147
147
|
break;
|
148
|
-
case
|
148
|
+
case kUpb_CType_UInt64:
|
149
149
|
StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
|
150
150
|
break;
|
151
|
-
case
|
152
|
-
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
151
|
+
case kUpb_CType_String:
|
152
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
153
|
+
val.str_val.data);
|
153
154
|
break;
|
154
|
-
case
|
155
|
-
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
155
|
+
case kUpb_CType_Bytes:
|
156
|
+
StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
|
157
|
+
val.str_val.data);
|
156
158
|
break;
|
157
|
-
case
|
159
|
+
case kUpb_CType_Enum:
|
158
160
|
StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
|
159
161
|
break;
|
160
|
-
case
|
162
|
+
case kUpb_CType_Message:
|
161
163
|
Message_PrintMessage(b, val.msg_val, info.def.msgdef);
|
162
164
|
break;
|
163
165
|
}
|
@@ -168,7 +170,7 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val,
|
|
168
170
|
// -----------------------------------------------------------------------------
|
169
171
|
|
170
172
|
typedef struct {
|
171
|
-
|
173
|
+
upb_Arena *arena;
|
172
174
|
VALUE pinned_objs;
|
173
175
|
} Arena;
|
174
176
|
|
@@ -179,44 +181,42 @@ static void Arena_mark(void *data) {
|
|
179
181
|
|
180
182
|
static void Arena_free(void *data) {
|
181
183
|
Arena *arena = data;
|
182
|
-
|
184
|
+
upb_Arena_Free(arena->arena);
|
183
185
|
xfree(arena);
|
184
186
|
}
|
185
187
|
|
186
188
|
static VALUE cArena;
|
187
189
|
|
188
190
|
const rb_data_type_t Arena_type = {
|
189
|
-
|
190
|
-
|
191
|
-
|
191
|
+
"Google::Protobuf::Internal::Arena",
|
192
|
+
{Arena_mark, Arena_free, NULL},
|
193
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
192
194
|
};
|
193
195
|
|
194
196
|
static VALUE Arena_alloc(VALUE klass) {
|
195
197
|
Arena *arena = ALLOC(Arena);
|
196
|
-
arena->arena =
|
198
|
+
arena->arena = upb_Arena_New();
|
197
199
|
arena->pinned_objs = Qnil;
|
198
200
|
return TypedData_Wrap_Struct(klass, &Arena_type, arena);
|
199
201
|
}
|
200
202
|
|
201
|
-
|
203
|
+
upb_Arena *Arena_get(VALUE _arena) {
|
202
204
|
Arena *arena;
|
203
205
|
TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
|
204
206
|
return arena->arena;
|
205
207
|
}
|
206
208
|
|
207
|
-
void Arena_fuse(VALUE _arena,
|
209
|
+
void Arena_fuse(VALUE _arena, upb_Arena *other) {
|
208
210
|
Arena *arena;
|
209
211
|
TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
|
210
|
-
if (!
|
212
|
+
if (!upb_Arena_Fuse(arena->arena, other)) {
|
211
213
|
rb_raise(rb_eRuntimeError,
|
212
214
|
"Unable to fuse arenas. This should never happen since Ruby does "
|
213
215
|
"not use initial blocks");
|
214
216
|
}
|
215
217
|
}
|
216
218
|
|
217
|
-
VALUE Arena_new() {
|
218
|
-
return Arena_alloc(cArena);
|
219
|
-
}
|
219
|
+
VALUE Arena_new() { return Arena_alloc(cArena); }
|
220
220
|
|
221
221
|
void Arena_Pin(VALUE _arena, VALUE obj) {
|
222
222
|
Arena *arena;
|
@@ -333,8 +333,8 @@ static void SecondaryMap_MaybeGC() {
|
|
333
333
|
// avoid O(N^2) CPU costs.
|
334
334
|
size_t threshold = PBRUBY_MAX(secondary_len * 0.2, 2000);
|
335
335
|
if (waste > threshold) {
|
336
|
-
rb_funcall(gc_secondary_map_lambda, rb_intern("call"), 2,
|
337
|
-
|
336
|
+
rb_funcall(gc_secondary_map_lambda, rb_intern("call"), 2, secondary_map,
|
337
|
+
weak_obj_cache);
|
338
338
|
}
|
339
339
|
}
|
340
340
|
|
@@ -353,7 +353,7 @@ static VALUE SecondaryMap_Get(VALUE key, bool create) {
|
|
353
353
|
#endif
|
354
354
|
|
355
355
|
// Requires: secondary_map_mutex is held by this thread iff create == true.
|
356
|
-
static VALUE ObjectCache_GetKey(const void*
|
356
|
+
static VALUE ObjectCache_GetKey(const void *key, bool create) {
|
357
357
|
VALUE key_val = (VALUE)key;
|
358
358
|
PBRUBY_ASSERT((key_val & 3) == 0);
|
359
359
|
VALUE ret = LL2NUM(key_val >> 2);
|
@@ -380,7 +380,7 @@ static void ObjectCache_Init() {
|
|
380
380
|
#endif
|
381
381
|
}
|
382
382
|
|
383
|
-
void ObjectCache_Add(const void*
|
383
|
+
void ObjectCache_Add(const void *key, VALUE val) {
|
384
384
|
PBRUBY_ASSERT(ObjectCache_Get(key) == Qnil);
|
385
385
|
#if USE_SECONDARY_MAP
|
386
386
|
rb_mutex_lock(secondary_map_mutex);
|
@@ -394,7 +394,7 @@ void ObjectCache_Add(const void* key, VALUE val) {
|
|
394
394
|
}
|
395
395
|
|
396
396
|
// Returns the cached object for this key, if any. Otherwise returns Qnil.
|
397
|
-
VALUE ObjectCache_Get(const void*
|
397
|
+
VALUE ObjectCache_Get(const void *key) {
|
398
398
|
VALUE key_rb = ObjectCache_GetKey(key, false);
|
399
399
|
return rb_funcall(weak_obj_cache, item_get, 1, key_rb);
|
400
400
|
}
|
@@ -407,9 +407,9 @@ VALUE ObjectCache_Get(const void* key) {
|
|
407
407
|
* unknown fields in submessages.
|
408
408
|
*/
|
409
409
|
static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
|
410
|
-
const
|
411
|
-
|
412
|
-
if (!
|
410
|
+
const upb_MessageDef *m;
|
411
|
+
upb_Message *msg = Message_GetMutable(msg_rb, &m);
|
412
|
+
if (!upb_Message_DiscardUnknown(msg, m, 128)) {
|
413
413
|
rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
|
414
414
|
}
|
415
415
|
|
@@ -431,10 +431,10 @@ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
|
431
431
|
return Map_deep_copy(obj);
|
432
432
|
} else {
|
433
433
|
VALUE new_arena_rb = Arena_new();
|
434
|
-
|
435
|
-
const
|
436
|
-
const
|
437
|
-
|
434
|
+
upb_Arena *new_arena = Arena_get(new_arena_rb);
|
435
|
+
const upb_MessageDef *m;
|
436
|
+
const upb_Message *msg = Message_Get(obj, &m);
|
437
|
+
upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
|
438
438
|
return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
|
439
439
|
}
|
440
440
|
}
|
@@ -445,8 +445,7 @@ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
|
445
445
|
|
446
446
|
// This must be named "Init_protobuf_c" because the Ruby module is named
|
447
447
|
// "protobuf_c" -- the VM looks for this symbol in our .so.
|
448
|
-
__attribute__
|
449
|
-
void Init_protobuf_c() {
|
448
|
+
__attribute__((visibility("default"))) void Init_protobuf_c() {
|
450
449
|
ObjectCache_Init();
|
451
450
|
|
452
451
|
VALUE google = rb_define_module("Google");
|
@@ -465,6 +464,6 @@ void Init_protobuf_c() {
|
|
465
464
|
|
466
465
|
rb_define_singleton_method(protobuf, "discard_unknown",
|
467
466
|
Google_Protobuf_discard_unknown, 1);
|
468
|
-
rb_define_singleton_method(protobuf, "deep_copy",
|
469
|
-
|
467
|
+
rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
|
468
|
+
1);
|
470
469
|
}
|
@@ -31,33 +31,33 @@
|
|
31
31
|
#ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
|
32
32
|
#define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
|
33
33
|
|
34
|
+
#include <ruby/encoding.h>
|
34
35
|
#include <ruby/ruby.h>
|
35
36
|
#include <ruby/vm.h>
|
36
|
-
#include <ruby/encoding.h>
|
37
37
|
|
38
|
-
#include "ruby-upb.h"
|
39
38
|
#include "defs.h"
|
39
|
+
#include "ruby-upb.h"
|
40
40
|
|
41
41
|
// These operate on a map field (i.e., a repeated field of submessages whose
|
42
42
|
// submessage type is a map-entry msgdef).
|
43
|
-
const
|
44
|
-
const
|
43
|
+
const upb_FieldDef* map_field_key(const upb_FieldDef* field);
|
44
|
+
const upb_FieldDef* map_field_value(const upb_FieldDef* field);
|
45
45
|
|
46
46
|
// -----------------------------------------------------------------------------
|
47
47
|
// Arena
|
48
48
|
// -----------------------------------------------------------------------------
|
49
49
|
|
50
|
-
// A Ruby object that wraps an underlying
|
50
|
+
// A Ruby object that wraps an underlying upb_Arena. Any objects that are
|
51
51
|
// allocated from this arena should reference the Arena in rb_gc_mark(), to
|
52
52
|
// ensure that the object's underlying memory outlives any Ruby object that can
|
53
53
|
// reach it.
|
54
54
|
|
55
55
|
VALUE Arena_new();
|
56
|
-
|
56
|
+
upb_Arena* Arena_get(VALUE arena);
|
57
57
|
|
58
58
|
// Fuses this arena to another, throwing a Ruby exception if this is not
|
59
59
|
// possible.
|
60
|
-
void Arena_fuse(VALUE arena,
|
60
|
+
void Arena_fuse(VALUE arena, upb_Arena* other);
|
61
61
|
|
62
62
|
// Pins this Ruby object to the lifetime of this arena, so that as long as the
|
63
63
|
// arena is alive this object will not be collected.
|
@@ -93,10 +93,11 @@ typedef struct StringBuilder StringBuilder;
|
|
93
93
|
|
94
94
|
StringBuilder* StringBuilder_New();
|
95
95
|
void StringBuilder_Free(StringBuilder* b);
|
96
|
-
void StringBuilder_Printf(StringBuilder* b, const char
|
96
|
+
void StringBuilder_Printf(StringBuilder* b, const char* fmt, ...);
|
97
97
|
VALUE StringBuilder_ToRubyString(StringBuilder* b);
|
98
98
|
|
99
|
-
void StringBuilder_PrintMsgval(StringBuilder* b,
|
99
|
+
void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
|
100
|
+
TypeInfo info);
|
100
101
|
|
101
102
|
// -----------------------------------------------------------------------------
|
102
103
|
// Utilities.
|
@@ -105,7 +106,9 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
|
|
105
106
|
extern VALUE cTypeError;
|
106
107
|
|
107
108
|
#ifdef NDEBUG
|
108
|
-
#define PBRUBY_ASSERT(expr)
|
109
|
+
#define PBRUBY_ASSERT(expr) \
|
110
|
+
do { \
|
111
|
+
} while (false && (expr))
|
109
112
|
#else
|
110
113
|
#define PBRUBY_ASSERT(expr) assert(expr)
|
111
114
|
#endif
|