google-protobuf 3.8.0 → 3.15.0

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.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +349 -0
  3. data/ext/google/protobuf_c/convert.h +72 -0
  4. data/ext/google/protobuf_c/defs.c +1525 -1223
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +4 -7
  7. data/ext/google/protobuf_c/map.c +309 -476
  8. data/ext/google/protobuf_c/map.h +66 -0
  9. data/ext/google/protobuf_c/message.c +912 -446
  10. data/ext/google/protobuf_c/message.h +98 -0
  11. data/ext/google/protobuf_c/protobuf.c +326 -51
  12. data/ext/google/protobuf_c/protobuf.h +46 -549
  13. data/ext/google/protobuf_c/repeated_field.c +308 -312
  14. data/ext/google/protobuf_c/repeated_field.h +62 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +8915 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +4452 -0
  17. data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +145 -0
  18. data/lib/google/protobuf/any_pb.rb +1 -1
  19. data/lib/google/protobuf/api_pb.rb +3 -3
  20. data/lib/google/protobuf/duration_pb.rb +1 -1
  21. data/lib/google/protobuf/empty_pb.rb +1 -1
  22. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  23. data/lib/google/protobuf/source_context_pb.rb +1 -1
  24. data/lib/google/protobuf/struct_pb.rb +4 -4
  25. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  26. data/lib/google/protobuf/type_pb.rb +8 -8
  27. data/lib/google/protobuf/well_known_types.rb +8 -2
  28. data/lib/google/protobuf/wrappers_pb.rb +9 -9
  29. data/lib/google/protobuf.rb +70 -0
  30. data/tests/basic.rb +262 -71
  31. data/tests/generated_code_test.rb +0 -0
  32. data/tests/stress.rb +0 -0
  33. metadata +26 -14
  34. data/ext/google/protobuf_c/encode_decode.c +0 -1614
  35. data/ext/google/protobuf_c/storage.c +0 -1062
  36. data/ext/google/protobuf_c/upb.c +0 -17480
  37. data/ext/google/protobuf_c/upb.h +0 -10641
@@ -28,91 +28,154 @@
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
+ #include "message.h"
32
+
33
+ #include "convert.h"
34
+ #include "defs.h"
35
+ #include "map.h"
31
36
  #include "protobuf.h"
37
+ #include "repeated_field.h"
38
+ #include "third_party/wyhash/wyhash.h"
32
39
 
33
- // -----------------------------------------------------------------------------
34
- // Class/module creation from msgdefs and enumdefs, respectively.
35
- // -----------------------------------------------------------------------------
40
+ static VALUE cParseError = Qnil;
41
+ static ID descriptor_instancevar_interned;
36
42
 
37
- void* Message_data(void* msg) {
38
- return ((uint8_t *)msg) + sizeof(MessageHeader);
43
+ static VALUE initialize_rb_class_with_no_args(VALUE klass) {
44
+ return rb_funcall(klass, rb_intern("new"), 0);
39
45
  }
40
46
 
41
- void Message_mark(void* _self) {
42
- MessageHeader* self = (MessageHeader *)_self;
43
- layout_mark(self->descriptor->layout, Message_data(self));
47
+ VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
48
+ return rb_ivar_get(klass, descriptor_instancevar_interned);
44
49
  }
45
50
 
46
- void Message_free(void* self) {
47
- stringsink* unknown = ((MessageHeader *)self)->unknown_fields;
48
- if (unknown != NULL) {
49
- stringsink_uninit(unknown);
50
- free(unknown);
51
- }
52
- xfree(self);
51
+ // -----------------------------------------------------------------------------
52
+ // Class/module creation from msgdefs and enumdefs, respectively.
53
+ // -----------------------------------------------------------------------------
54
+
55
+ typedef struct {
56
+ VALUE arena;
57
+ const upb_msg* msg; // Can get as mutable when non-frozen.
58
+ const upb_msgdef* msgdef; // kept alive by self.class.descriptor reference.
59
+ } Message;
60
+
61
+ static void Message_mark(void* _self) {
62
+ Message* self = (Message *)_self;
63
+ rb_gc_mark(self->arena);
53
64
  }
54
65
 
55
- rb_data_type_t Message_type = {
66
+ static rb_data_type_t Message_type = {
56
67
  "Message",
57
- { Message_mark, Message_free, NULL },
68
+ { Message_mark, RUBY_DEFAULT_FREE, NULL },
69
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
58
70
  };
59
71
 
60
- VALUE Message_alloc(VALUE klass) {
72
+ static Message* ruby_to_Message(VALUE msg_rb) {
73
+ Message* msg;
74
+ TypedData_Get_Struct(msg_rb, Message, &Message_type, msg);
75
+ return msg;
76
+ }
77
+
78
+ static VALUE Message_alloc(VALUE klass) {
61
79
  VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
62
- Descriptor* desc = ruby_to_Descriptor(descriptor);
63
- MessageHeader* msg = (MessageHeader*)ALLOC_N(
64
- uint8_t, sizeof(MessageHeader) + desc->layout->size);
80
+ Message* msg = ALLOC(Message);
65
81
  VALUE ret;
66
82
 
67
- memset(Message_data(msg), 0, desc->layout->size);
83
+ msg->msgdef = Descriptor_GetMsgDef(descriptor);
84
+ msg->arena = Qnil;
85
+ msg->msg = NULL;
68
86
 
69
- // We wrap first so that everything in the message object is GC-rooted in case
70
- // a collection happens during object creation in layout_init().
71
87
  ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
72
- msg->descriptor = desc;
73
88
  rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
74
89
 
75
- msg->unknown_fields = NULL;
90
+ return ret;
91
+ }
76
92
 
77
- layout_init(desc->layout, Message_data(msg));
93
+ const upb_msg *Message_Get(VALUE msg_rb, const upb_msgdef **m) {
94
+ Message* msg = ruby_to_Message(msg_rb);
95
+ if (m) *m = msg->msgdef;
96
+ return msg->msg;
97
+ }
78
98
 
79
- return ret;
99
+ upb_msg *Message_GetMutable(VALUE msg_rb, const upb_msgdef **m) {
100
+ rb_check_frozen(msg_rb);
101
+ return (upb_msg*)Message_Get(msg_rb, m);
80
102
  }
81
103
 
82
- static const upb_fielddef* which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
83
- upb_oneof_iter it;
84
- size_t case_ofs;
85
- uint32_t oneof_case;
86
- const upb_fielddef* first_field;
87
- const upb_fielddef* f;
104
+ void Message_InitPtr(VALUE self_, upb_msg *msg, VALUE arena) {
105
+ Message* self = ruby_to_Message(self_);
106
+ self->msg = msg;
107
+ self->arena = arena;
108
+ ObjectCache_Add(msg, self_, Arena_get(arena));
109
+ }
88
110
 
89
- // If no fields in the oneof, always nil.
90
- if (upb_oneofdef_numfields(o) == 0) {
91
- return NULL;
111
+ VALUE Message_GetArena(VALUE msg_rb) {
112
+ Message* msg = ruby_to_Message(msg_rb);
113
+ return msg->arena;
114
+ }
115
+
116
+ void Message_CheckClass(VALUE klass) {
117
+ if (rb_get_alloc_func(klass) != &Message_alloc) {
118
+ rb_raise(rb_eArgError,
119
+ "Message class was not returned by the DescriptorPool.");
92
120
  }
93
- // Grab the first field in the oneof so we can get its layout info to find the
94
- // oneof_case field.
95
- upb_oneof_begin(&it, o);
96
- assert(!upb_oneof_done(&it));
97
- first_field = upb_oneof_iter_field(&it);
98
- assert(upb_fielddef_containingoneof(first_field) != NULL);
121
+ }
99
122
 
100
- case_ofs =
101
- self->descriptor->layout->
102
- fields[upb_fielddef_index(first_field)].case_offset;
103
- oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
123
+ VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena) {
124
+ if (msg == NULL) return Qnil;
104
125
 
105
- if (oneof_case == ONEOF_CASE_NONE) {
106
- return NULL;
126
+ VALUE val = ObjectCache_Get(msg);
127
+
128
+ if (val == Qnil) {
129
+ VALUE klass = Descriptor_DefToClass(m);
130
+ val = Message_alloc(klass);
131
+ Message_InitPtr(val, msg, arena);
107
132
  }
108
133
 
109
- // oneof_case is a field index, so find that field.
110
- f = upb_oneofdef_itof(o, oneof_case);
111
- assert(f != NULL);
134
+ return val;
135
+ }
136
+
137
+ void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
138
+ const upb_msgdef* m) {
139
+ bool first = true;
140
+ int n = upb_msgdef_fieldcount(m);
141
+ VALUE klass = Descriptor_DefToClass(m);
142
+ StringBuilder_Printf(b, "<%s: ", rb_class2name(klass));
143
+
144
+ for (int i = 0; i < n; i++) {
145
+ const upb_fielddef* field = upb_msgdef_field(m, i);
146
+
147
+ if (upb_fielddef_haspresence(field) && !upb_msg_has(msg, field)) {
148
+ continue;
149
+ }
150
+
151
+ if (!first) {
152
+ StringBuilder_Printf(b, ", ");
153
+ } else {
154
+ first = false;
155
+ }
156
+
157
+ upb_msgval msgval = upb_msg_get(msg, field);
158
+
159
+ StringBuilder_Printf(b, "%s: ", upb_fielddef_name(field));
160
+
161
+ if (upb_fielddef_ismap(field)) {
162
+ const upb_msgdef* entry_m = upb_fielddef_msgsubdef(field);
163
+ const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
164
+ const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
165
+ TypeInfo val_info = TypeInfo_get(val_f);
166
+ Map_Inspect(b, msgval.map_val, upb_fielddef_type(key_f), val_info);
167
+ } else if (upb_fielddef_isseq(field)) {
168
+ RepeatedField_Inspect(b, msgval.array_val, TypeInfo_get(field));
169
+ } else {
170
+ StringBuilder_PrintMsgval(b, msgval, TypeInfo_get(field));
171
+ }
172
+ }
112
173
 
113
- return f;
174
+ StringBuilder_Printf(b, ">");
114
175
  }
115
176
 
177
+ // Helper functions for #method_missing ////////////////////////////////////////
178
+
116
179
  enum {
117
180
  METHOD_UNKNOWN = 0,
118
181
  METHOD_GETTER = 1,
@@ -125,133 +188,199 @@ enum {
125
188
  };
126
189
 
127
190
  // Check if the field is a well known wrapper type
128
- static bool is_wrapper_type_field(const upb_fielddef* field) {
129
- char* field_type_name = rb_class2name(field_type_class(field));
130
-
131
- return strcmp(field_type_name, "Google::Protobuf::DoubleValue") == 0 ||
132
- strcmp(field_type_name, "Google::Protobuf::FloatValue") == 0 ||
133
- strcmp(field_type_name, "Google::Protobuf::Int32Value") == 0 ||
134
- strcmp(field_type_name, "Google::Protobuf::Int64Value") == 0 ||
135
- strcmp(field_type_name, "Google::Protobuf::UInt32Value") == 0 ||
136
- strcmp(field_type_name, "Google::Protobuf::UInt64Value") == 0 ||
137
- strcmp(field_type_name, "Google::Protobuf::BoolValue") == 0 ||
138
- strcmp(field_type_name, "Google::Protobuf::StringValue") == 0 ||
139
- strcmp(field_type_name, "Google::Protobuf::BytesValue") == 0;
140
- }
141
-
142
- // Get a new Ruby wrapper type and set the initial value
143
- static VALUE ruby_wrapper_type(const upb_fielddef* field, const VALUE* value) {
144
- if (is_wrapper_type_field(field) && value != Qnil) {
145
- VALUE hash = rb_hash_new();
146
- rb_hash_aset(hash, rb_str_new2("value"), value);
147
- VALUE args[1] = { hash };
148
- return rb_class_new_instance(1, args, field_type_class(field));
191
+ static bool IsWrapper(const upb_fielddef* f) {
192
+ return upb_fielddef_issubmsg(f) &&
193
+ upb_msgdef_iswrapper(upb_fielddef_msgsubdef(f));
194
+ }
195
+
196
+ static bool Match(const upb_msgdef* m, const char* name, const upb_fielddef** f,
197
+ const upb_oneofdef** o, const char* prefix,
198
+ const char* suffix) {
199
+ size_t sp = strlen(prefix);
200
+ size_t ss = strlen(suffix);
201
+ size_t sn = strlen(name);
202
+
203
+ if (sn <= sp + ss) return false;
204
+
205
+ if (memcmp(name, prefix, sp) != 0 ||
206
+ memcmp(name + sn - ss, suffix, ss) != 0) {
207
+ return false;
149
208
  }
150
- return Qnil;
209
+
210
+ return upb_msgdef_lookupname(m, name + sp, sn - sp - ss, f, o);
151
211
  }
152
212
 
153
- static int extract_method_call(VALUE method_name, MessageHeader* self,
154
- const upb_fielddef **f, const upb_oneofdef **o) {
213
+ static int extract_method_call(VALUE method_name, Message* self,
214
+ const upb_fielddef** f, const upb_oneofdef** o) {
215
+ const upb_msgdef* m = self->msgdef;
216
+ const char* name;
217
+
155
218
  Check_Type(method_name, T_SYMBOL);
219
+ name = rb_id2name(SYM2ID(method_name));
220
+
221
+ if (Match(m, name, f, o, "", "")) return METHOD_GETTER;
222
+ if (Match(m, name, f, o, "", "=")) return METHOD_SETTER;
223
+ if (Match(m, name, f, o, "clear_", "")) return METHOD_CLEAR;
224
+ if (Match(m, name, f, o, "has_", "?") &&
225
+ (*o || (*f && upb_fielddef_haspresence(*f)))) {
226
+ // Disallow oneof hazzers for proto3.
227
+ // TODO(haberman): remove this test when we are enabling oneof hazzers for
228
+ // proto3.
229
+ if (*f && !upb_fielddef_issubmsg(*f) &&
230
+ upb_fielddef_realcontainingoneof(*f) &&
231
+ upb_msgdef_syntax(upb_fielddef_containingtype(*f)) !=
232
+ UPB_SYNTAX_PROTO2) {
233
+ return METHOD_UNKNOWN;
234
+ }
235
+ return METHOD_PRESENCE;
236
+ }
237
+ if (Match(m, name, f, o, "", "_as_value") && *f && !upb_fielddef_isseq(*f) &&
238
+ IsWrapper(*f)) {
239
+ return METHOD_WRAPPER_GETTER;
240
+ }
241
+ if (Match(m, name, f, o, "", "_as_value=") && *f && !upb_fielddef_isseq(*f) &&
242
+ IsWrapper(*f)) {
243
+ return METHOD_WRAPPER_SETTER;
244
+ }
245
+ if (Match(m, name, f, o, "", "_const") && *f &&
246
+ upb_fielddef_type(*f) == UPB_TYPE_ENUM) {
247
+ return METHOD_ENUM_GETTER;
248
+ }
156
249
 
157
- VALUE method_str = rb_id2str(SYM2ID(method_name));
158
- char* name = RSTRING_PTR(method_str);
159
- size_t name_len = RSTRING_LEN(method_str);
160
- int accessor_type;
161
- const upb_oneofdef* test_o;
162
- const upb_fielddef* test_f;
163
-
164
- if (name[name_len - 1] == '=') {
165
- accessor_type = METHOD_SETTER;
166
- name_len--;
167
- // We want to ensure if the proto has something named clear_foo or has_foo?,
168
- // we don't strip the prefix.
169
- } else if (strncmp("clear_", name, 6) == 0 &&
170
- !upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
171
- &test_f, &test_o)) {
172
- accessor_type = METHOD_CLEAR;
173
- name = name + 6;
174
- name_len = name_len - 6;
175
- } else if (strncmp("has_", name, 4) == 0 && name[name_len - 1] == '?' &&
176
- !upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
177
- &test_f, &test_o)) {
178
- accessor_type = METHOD_PRESENCE;
179
- name = name + 4;
180
- name_len = name_len - 5;
181
- } else {
182
- accessor_type = METHOD_GETTER;
183
- }
184
-
185
- bool has_field = upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
186
- &test_f, &test_o);
187
-
188
- // Look for wrapper type accessor of the form <field_name>_as_value
189
- if (!has_field &&
190
- (accessor_type == METHOD_GETTER || accessor_type == METHOD_SETTER) &&
191
- name_len > 9 && strncmp(name + name_len - 9, "_as_value", 9) == 0) {
192
- // Find the field name
193
- char wrapper_field_name[name_len - 8];
194
- strncpy(wrapper_field_name, name, name_len - 9);
195
- wrapper_field_name[name_len - 7] = '\0';
196
-
197
- // Check if field exists and is a wrapper type
198
- const upb_oneofdef* test_o_wrapper;
199
- const upb_fielddef* test_f_wrapper;
200
- if (upb_msgdef_lookupname(self->descriptor->msgdef, wrapper_field_name, name_len - 9,
201
- &test_f_wrapper, &test_o_wrapper) &&
202
- upb_fielddef_type(test_f_wrapper) == UPB_TYPE_MESSAGE &&
203
- is_wrapper_type_field(test_f_wrapper)) {
204
- // It does exist!
205
- has_field = true;
206
- if (accessor_type == METHOD_SETTER) {
207
- accessor_type = METHOD_WRAPPER_SETTER;
208
- } else {
209
- accessor_type = METHOD_WRAPPER_GETTER;
250
+ return METHOD_UNKNOWN;
251
+ }
252
+
253
+ static VALUE Message_oneof_accessor(VALUE _self, const upb_oneofdef* o,
254
+ int accessor_type) {
255
+ Message* self = ruby_to_Message(_self);
256
+ const upb_fielddef* oneof_field = upb_msg_whichoneof(self->msg, o);
257
+
258
+ switch (accessor_type) {
259
+ case METHOD_PRESENCE:
260
+ return oneof_field == NULL ? Qfalse : Qtrue;
261
+ case METHOD_CLEAR:
262
+ if (oneof_field != NULL) {
263
+ upb_msg_clearfield(Message_GetMutable(_self, NULL), oneof_field);
210
264
  }
211
- test_o = test_o_wrapper;
212
- test_f = test_f_wrapper;
213
- }
265
+ return Qnil;
266
+ case METHOD_GETTER:
267
+ return oneof_field == NULL
268
+ ? Qnil
269
+ : ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
270
+ case METHOD_SETTER:
271
+ rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
214
272
  }
273
+ rb_raise(rb_eRuntimeError, "Invalid access of oneof field.");
274
+ }
215
275
 
216
- // Look for enum accessor of the form <enum_name>_const
217
- if (!has_field && accessor_type == METHOD_GETTER &&
218
- name_len > 6 && strncmp(name + name_len - 6, "_const", 6) == 0) {
219
-
220
- // Find enum field name
221
- char enum_name[name_len - 5];
222
- strncpy(enum_name, name, name_len - 6);
223
- enum_name[name_len - 4] = '\0';
224
-
225
- // Check if enum field exists
226
- const upb_oneofdef* test_o_enum;
227
- const upb_fielddef* test_f_enum;
228
- if (upb_msgdef_lookupname(self->descriptor->msgdef, enum_name, name_len - 6,
229
- &test_f_enum, &test_o_enum) &&
230
- upb_fielddef_type(test_f_enum) == UPB_TYPE_ENUM) {
231
- // It does exist!
232
- has_field = true;
233
- accessor_type = METHOD_ENUM_GETTER;
234
- test_o = test_o_enum;
235
- test_f = test_f_enum;
276
+ static void Message_setfield(upb_msg* msg, const upb_fielddef* f, VALUE val,
277
+ upb_arena* arena) {
278
+ upb_msgval msgval;
279
+ if (upb_fielddef_ismap(f)) {
280
+ msgval.map_val = Map_GetUpbMap(val, f);
281
+ } else if (upb_fielddef_isseq(f)) {
282
+ msgval.array_val = RepeatedField_GetUpbArray(val, f);
283
+ } else {
284
+ if (val == Qnil &&
285
+ (upb_fielddef_issubmsg(f) || upb_fielddef_realcontainingoneof(f))) {
286
+ upb_msg_clearfield(msg, f);
287
+ return;
236
288
  }
289
+ msgval =
290
+ Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
237
291
  }
292
+ upb_msg_set(msg, f, msgval, arena);
293
+ }
238
294
 
239
- // Verify the name corresponds to a oneof or field in this message.
240
- if (!has_field) {
241
- return METHOD_UNKNOWN;
242
- }
295
+ static VALUE Message_field_accessor(VALUE _self, const upb_fielddef* f,
296
+ int accessor_type, int argc, VALUE* argv) {
297
+ upb_arena *arena = Arena_get(Message_GetArena(_self));
243
298
 
244
- // Method calls like 'has_foo?' are not allowed if field "foo" does not have
245
- // a hasbit (e.g. repeated fields or non-message type fields for proto3
246
- // syntax).
247
- if (accessor_type == METHOD_PRESENCE && test_f != NULL &&
248
- !upb_fielddef_haspresence(test_f)) {
249
- return METHOD_UNKNOWN;
299
+ switch (accessor_type) {
300
+ case METHOD_SETTER:
301
+ Message_setfield(Message_GetMutable(_self, NULL), f, argv[1], arena);
302
+ return Qnil;
303
+ case METHOD_CLEAR:
304
+ upb_msg_clearfield(Message_GetMutable(_self, NULL), f);
305
+ return Qnil;
306
+ case METHOD_PRESENCE:
307
+ if (!upb_fielddef_haspresence(f)) {
308
+ rb_raise(rb_eRuntimeError, "Field does not have presence.");
309
+ }
310
+ return upb_msg_has(Message_Get(_self, NULL), f);
311
+ case METHOD_WRAPPER_GETTER: {
312
+ Message* self = ruby_to_Message(_self);
313
+ if (upb_msg_has(self->msg, f)) {
314
+ PBRUBY_ASSERT(upb_fielddef_issubmsg(f) && !upb_fielddef_isseq(f));
315
+ upb_msgval wrapper = upb_msg_get(self->msg, f);
316
+ const upb_msgdef *wrapper_m = upb_fielddef_msgsubdef(f);
317
+ const upb_fielddef *value_f = upb_msgdef_itof(wrapper_m, 1);
318
+ upb_msgval value = upb_msg_get(wrapper.msg_val, value_f);
319
+ return Convert_UpbToRuby(value, TypeInfo_get(value_f), self->arena);
320
+ } else {
321
+ return Qnil;
322
+ }
323
+ }
324
+ case METHOD_WRAPPER_SETTER: {
325
+ upb_msg *msg = Message_GetMutable(_self, NULL);
326
+ if (argv[1] == Qnil) {
327
+ upb_msg_clearfield(msg, f);
328
+ } else {
329
+ const upb_fielddef *val_f = upb_msgdef_itof(upb_fielddef_msgsubdef(f), 1);
330
+ upb_msgval msgval = Convert_RubyToUpb(argv[1], upb_fielddef_name(f),
331
+ TypeInfo_get(val_f), arena);
332
+ upb_msg *wrapper = upb_msg_mutable(msg, f, arena).msg;
333
+ upb_msg_set(wrapper, val_f, msgval, arena);
334
+ }
335
+ return Qnil;
336
+ }
337
+ case METHOD_ENUM_GETTER: {
338
+ upb_msgval msgval = upb_msg_get(Message_Get(_self, NULL), f);
339
+
340
+ if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
341
+ // Map repeated fields to a new type with ints
342
+ VALUE arr = rb_ary_new();
343
+ size_t i, n = upb_array_size(msgval.array_val);
344
+ for (i = 0; i < n; i++) {
345
+ upb_msgval elem = upb_array_get(msgval.array_val, i);
346
+ rb_ary_push(arr, INT2NUM(elem.int32_val));
347
+ }
348
+ return arr;
349
+ } else {
350
+ return INT2NUM(msgval.int32_val);
351
+ }
352
+ }
353
+ case METHOD_GETTER: {
354
+ Message* self = ruby_to_Message(_self);
355
+ // This is a special-case: upb_msg_mutable() for map & array are logically
356
+ // const (they will not change what is serialized) but physically
357
+ // non-const, as they do allocate a repeated field or map. The logical
358
+ // constness means it's ok to do even if the message is frozen.
359
+ upb_msg *msg = (upb_msg*)self->msg;
360
+ if (upb_fielddef_ismap(f)) {
361
+ upb_map *map = upb_msg_mutable(msg, f, arena).map;
362
+ const upb_fielddef *key_f = map_field_key(f);
363
+ const upb_fielddef *val_f = map_field_value(f);
364
+ upb_fieldtype_t key_type = upb_fielddef_type(key_f);
365
+ TypeInfo value_type_info = TypeInfo_get(val_f);
366
+ return Map_GetRubyWrapper(map, key_type, value_type_info, self->arena);
367
+ } else if (upb_fielddef_isseq(f)) {
368
+ upb_array *arr = upb_msg_mutable(msg, f, arena).array;
369
+ return RepeatedField_GetRubyWrapper(arr, TypeInfo_get(f), self->arena);
370
+ } else if (upb_fielddef_issubmsg(f)) {
371
+ if (!upb_msg_has(self->msg, f)) return Qnil;
372
+ upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
373
+ const upb_msgdef *m = upb_fielddef_msgsubdef(f);
374
+ return Message_GetRubyWrapper(submsg, m, self->arena);
375
+ } else {
376
+ upb_msgval msgval = upb_msg_get(self->msg, f);
377
+ return Convert_UpbToRuby(msgval, TypeInfo_get(f), self->arena);
378
+ }
379
+ default:
380
+ rb_raise(rb_eRuntimeError, "Internal error, no such accessor: %d",
381
+ accessor_type);
382
+ }
250
383
  }
251
-
252
- *o = test_o;
253
- *f = test_f;
254
- return accessor_type;
255
384
  }
256
385
 
257
386
  /*
@@ -281,102 +410,56 @@ static int extract_method_call(VALUE method_name, MessageHeader* self,
281
410
  * true if the field 'fieldname' is set in the message object, else false. For
282
411
  * 'proto3' syntax, calling this for a basic type field will result in an error.
283
412
  */
284
- VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
285
- MessageHeader* self;
413
+ static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
414
+ Message* self = ruby_to_Message(_self);
286
415
  const upb_oneofdef* o;
287
416
  const upb_fielddef* f;
417
+ int accessor_type;
288
418
 
289
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
290
419
  if (argc < 1) {
291
420
  rb_raise(rb_eArgError, "Expected method name as first argument.");
292
421
  }
293
422
 
294
- int accessor_type = extract_method_call(argv[0], self, &f, &o);
295
- if (accessor_type == METHOD_UNKNOWN || (o == NULL && f == NULL) ) {
296
- return rb_call_super(argc, argv);
297
- } else if (accessor_type == METHOD_SETTER || accessor_type == METHOD_WRAPPER_SETTER) {
298
- if (argc != 2) {
299
- rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
300
- }
301
- rb_check_frozen(_self);
302
- } else if (argc != 1) {
303
- rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
304
- }
423
+ accessor_type = extract_method_call(argv[0], self, &f, &o);
305
424
 
306
- // Return which of the oneof fields are set
307
- if (o != NULL) {
308
- if (accessor_type == METHOD_SETTER) {
309
- rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
310
- }
425
+ if (accessor_type == METHOD_UNKNOWN) return rb_call_super(argc, argv);
311
426
 
312
- const upb_fielddef* oneof_field = which_oneof_field(self, o);
313
- if (accessor_type == METHOD_PRESENCE) {
314
- return oneof_field == NULL ? Qfalse : Qtrue;
315
- } else if (accessor_type == METHOD_CLEAR) {
316
- if (oneof_field != NULL) {
317
- layout_clear(self->descriptor->layout, Message_data(self), oneof_field);
427
+ // Validate argument count.
428
+ switch (accessor_type) {
429
+ case METHOD_SETTER:
430
+ case METHOD_WRAPPER_SETTER:
431
+ if (argc != 2) {
432
+ rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
318
433
  }
319
- return Qnil;
320
- } else {
321
- // METHOD_ACCESSOR
322
- return oneof_field == NULL ? Qnil :
323
- ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
324
- }
325
- // Otherwise we're operating on a single proto field
326
- } else if (accessor_type == METHOD_SETTER) {
327
- layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
328
- return Qnil;
329
- } else if (accessor_type == METHOD_CLEAR) {
330
- layout_clear(self->descriptor->layout, Message_data(self), f);
331
- return Qnil;
332
- } else if (accessor_type == METHOD_PRESENCE) {
333
- return layout_has(self->descriptor->layout, Message_data(self), f);
334
- } else if (accessor_type == METHOD_WRAPPER_GETTER) {
335
- VALUE value = layout_get(self->descriptor->layout, Message_data(self), f);
336
- if (value != Qnil) {
337
- value = rb_funcall(value, rb_intern("value"), 0);
338
- }
339
- return value;
340
- } else if (accessor_type == METHOD_WRAPPER_SETTER) {
341
- VALUE wrapper = ruby_wrapper_type(f, argv[1]);
342
- layout_set(self->descriptor->layout, Message_data(self), f, wrapper);
343
- return Qnil;
344
- } else if (accessor_type == METHOD_ENUM_GETTER) {
345
- VALUE enum_type = field_type_class(f);
346
- VALUE method = rb_intern("const_get");
347
- VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f);
348
-
349
- // Map repeated fields to a new type with ints
350
- if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
351
- int array_size = FIX2INT(rb_funcall(raw_value, rb_intern("length"), 0));
352
- VALUE array_args[1] = { ID2SYM(rb_intern("int64")) };
353
- VALUE array = rb_class_new_instance(1, array_args, CLASS_OF(raw_value));
354
- for (int i = 0; i < array_size; i++) {
355
- VALUE entry = rb_funcall(enum_type, method, 1, rb_funcall(raw_value,
356
- rb_intern("at"), 1, INT2NUM(i)));
357
- rb_funcall(array, rb_intern("push"), 1, entry);
434
+ rb_check_frozen(_self);
435
+ break;
436
+ default:
437
+ if (argc != 1) {
438
+ rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
358
439
  }
359
- return array;
360
- }
361
- // Convert the value for singular fields
362
- return rb_funcall(enum_type, method, 1, raw_value);
440
+ break;
441
+ }
442
+
443
+ // Dispatch accessor.
444
+ if (o != NULL) {
445
+ return Message_oneof_accessor(_self, o, accessor_type);
363
446
  } else {
364
- return layout_get(self->descriptor->layout, Message_data(self), f);
447
+ return Message_field_accessor(_self, f, accessor_type, argc, argv);
365
448
  }
366
449
  }
367
450
 
368
-
369
- VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
370
- MessageHeader* self;
451
+ static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
452
+ Message* self = ruby_to_Message(_self);
371
453
  const upb_oneofdef* o;
372
454
  const upb_fielddef* f;
455
+ int accessor_type;
373
456
 
374
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
375
457
  if (argc < 1) {
376
458
  rb_raise(rb_eArgError, "Expected method name as first argument.");
377
459
  }
378
460
 
379
- int accessor_type = extract_method_call(argv[0], self, &f, &o);
461
+ accessor_type = extract_method_call(argv[0], self, &f, &o);
462
+
380
463
  if (accessor_type == METHOD_UNKNOWN) {
381
464
  return rb_call_super(argc, argv);
382
465
  } else if (o != NULL) {
@@ -386,22 +469,116 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
386
469
  }
387
470
  }
388
471
 
389
- VALUE create_submsg_from_hash(const upb_fielddef *f, VALUE hash) {
390
- const upb_def *d = upb_fielddef_subdef(f);
391
- assert(d != NULL);
472
+ void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
473
+ upb_arena* arena);
474
+
475
+ typedef struct {
476
+ upb_map *map;
477
+ TypeInfo key_type;
478
+ TypeInfo val_type;
479
+ upb_arena *arena;
480
+ } MapInit;
481
+
482
+ static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
483
+ MapInit *map_init = (MapInit*)_self;
484
+ upb_msgval k, v;
485
+ k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
486
+
487
+ if (map_init->val_type.type == UPB_TYPE_MESSAGE && TYPE(val) == T_HASH) {
488
+ upb_msg *msg = upb_msg_new(map_init->val_type.def.msgdef, map_init->arena);
489
+ Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
490
+ map_init->arena);
491
+ v.msg_val = msg;
492
+ } else {
493
+ v = Convert_RubyToUpb(val, "", map_init->val_type, map_init->arena);
494
+ }
495
+ upb_map_set(map_init->map, k, v, map_init->arena);
496
+ return ST_CONTINUE;
497
+ }
392
498
 
393
- VALUE descriptor = get_def_obj(d);
394
- VALUE msgclass = rb_funcall(descriptor, rb_intern("msgclass"), 0, NULL);
499
+ static void Map_InitFromValue(upb_map* map, const upb_fielddef* f, VALUE val,
500
+ upb_arena* arena) {
501
+ const upb_msgdef* entry_m = upb_fielddef_msgsubdef(f);
502
+ const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
503
+ const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
504
+ if (TYPE(val) != T_HASH) {
505
+ rb_raise(rb_eArgError,
506
+ "Expected Hash object as initializer value for map field '%s' "
507
+ "(given %s).",
508
+ upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
509
+ }
510
+ MapInit map_init = {map, TypeInfo_get(key_f), TypeInfo_get(val_f), arena};
511
+ rb_hash_foreach(val, Map_initialize_kwarg, (VALUE)&map_init);
512
+ }
395
513
 
396
- VALUE args[1] = { hash };
397
- return rb_class_new_instance(1, args, msgclass);
514
+ static upb_msgval MessageValue_FromValue(VALUE val, TypeInfo info,
515
+ upb_arena* arena) {
516
+ if (info.type == UPB_TYPE_MESSAGE) {
517
+ upb_msgval msgval;
518
+ upb_msg* msg = upb_msg_new(info.def.msgdef, arena);
519
+ Message_InitFromValue(msg, info.def.msgdef, val, arena);
520
+ msgval.msg_val = msg;
521
+ return msgval;
522
+ } else {
523
+ return Convert_RubyToUpb(val, "", info, arena);
524
+ }
398
525
  }
399
526
 
400
- int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
401
- MessageHeader* self;
402
- char *name;
403
- const upb_fielddef* f;
404
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
527
+ static void RepeatedField_InitFromValue(upb_array* arr, const upb_fielddef* f,
528
+ VALUE val, upb_arena* arena) {
529
+ TypeInfo type_info = TypeInfo_get(f);
530
+
531
+ if (TYPE(val) != T_ARRAY) {
532
+ rb_raise(rb_eArgError,
533
+ "Expected array as initializer value for repeated field '%s' (given %s).",
534
+ upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
535
+ }
536
+
537
+ for (int i = 0; i < RARRAY_LEN(val); i++) {
538
+ VALUE entry = rb_ary_entry(val, i);
539
+ upb_msgval msgval;
540
+ if (upb_fielddef_issubmsg(f) && TYPE(entry) == T_HASH) {
541
+ msgval = MessageValue_FromValue(entry, type_info, arena);
542
+ } else {
543
+ msgval = Convert_RubyToUpb(entry, upb_fielddef_name(f), type_info, arena);
544
+ }
545
+ upb_array_append(arr, msgval, arena);
546
+ }
547
+ }
548
+
549
+ static void Message_InitFieldFromValue(upb_msg* msg, const upb_fielddef* f,
550
+ VALUE val, upb_arena* arena) {
551
+ if (TYPE(val) == T_NIL) return;
552
+
553
+ if (upb_fielddef_ismap(f)) {
554
+ upb_map *map = upb_msg_mutable(msg, f, arena).map;
555
+ Map_InitFromValue(map, f, val, arena);
556
+ } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
557
+ upb_array *arr = upb_msg_mutable(msg, f, arena).array;
558
+ RepeatedField_InitFromValue(arr, f, val, arena);
559
+ } else if (upb_fielddef_issubmsg(f)) {
560
+ if (TYPE(val) == T_HASH) {
561
+ upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
562
+ Message_InitFromValue(submsg, upb_fielddef_msgsubdef(f), val, arena);
563
+ } else {
564
+ Message_setfield(msg, f, val, arena);
565
+ }
566
+ } else {
567
+ upb_msgval msgval =
568
+ Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
569
+ upb_msg_set(msg, f, msgval, arena);
570
+ }
571
+ }
572
+
573
+ typedef struct {
574
+ upb_msg *msg;
575
+ const upb_msgdef *msgdef;
576
+ upb_arena *arena;
577
+ } MsgInit;
578
+
579
+ static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
580
+ MsgInit *msg_init = (MsgInit*)_self;
581
+ const char *name;
405
582
 
406
583
  if (TYPE(key) == T_STRING) {
407
584
  name = RSTRING_PTR(key);
@@ -412,51 +589,26 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
412
589
  "Expected string or symbols as hash keys when initializing proto from hash.");
413
590
  }
414
591
 
415
- f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
592
+ const upb_fielddef* f = upb_msgdef_ntofz(msg_init->msgdef, name);
593
+
416
594
  if (f == NULL) {
417
595
  rb_raise(rb_eArgError,
418
596
  "Unknown field name '%s' in initialization map entry.", name);
419
597
  }
420
598
 
421
- if (TYPE(val) == T_NIL) {
422
- return 0;
423
- }
424
-
425
- if (is_map_field(f)) {
426
- VALUE map;
427
-
428
- if (TYPE(val) != T_HASH) {
429
- rb_raise(rb_eArgError,
430
- "Expected Hash object as initializer value for map field '%s' (given %s).",
431
- name, rb_class2name(CLASS_OF(val)));
432
- }
433
- map = layout_get(self->descriptor->layout, Message_data(self), f);
434
- Map_merge_into_self(map, val);
435
- } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
436
- VALUE ary;
437
-
438
- if (TYPE(val) != T_ARRAY) {
439
- rb_raise(rb_eArgError,
440
- "Expected array as initializer value for repeated field '%s' (given %s).",
441
- name, rb_class2name(CLASS_OF(val)));
442
- }
443
- ary = layout_get(self->descriptor->layout, Message_data(self), f);
444
- for (int i = 0; i < RARRAY_LEN(val); i++) {
445
- VALUE entry = rb_ary_entry(val, i);
446
- if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
447
- entry = create_submsg_from_hash(f, entry);
448
- }
599
+ Message_InitFieldFromValue(msg_init->msg, f, val, msg_init->arena);
600
+ return ST_CONTINUE;
601
+ }
449
602
 
450
- RepeatedField_push(ary, entry);
451
- }
603
+ void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
604
+ upb_arena* arena) {
605
+ MsgInit msg_init = {msg, m, arena};
606
+ if (TYPE(val) == T_HASH) {
607
+ rb_hash_foreach(val, Message_initialize_kwarg, (VALUE)&msg_init);
452
608
  } else {
453
- if (TYPE(val) == T_HASH && upb_fielddef_issubmsg(f)) {
454
- val = create_submsg_from_hash(f, val);
455
- }
456
-
457
- layout_set(self->descriptor->layout, Message_data(self), f, val);
609
+ rb_raise(rb_eArgError, "Expected hash arguments or message, not %s",
610
+ rb_class2name(CLASS_OF(val)));
458
611
  }
459
- return 0;
460
612
  }
461
613
 
462
614
  /*
@@ -471,8 +623,13 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
471
623
  * have been added to a pool. The method definitions described here on the
472
624
  * Message class are provided on each concrete message class.
473
625
  */
474
- VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
475
- VALUE hash_args;
626
+ static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
627
+ Message* self = ruby_to_Message(_self);
628
+ VALUE arena_rb = Arena_new();
629
+ upb_arena *arena = Arena_get(arena_rb);
630
+ upb_msg *msg = upb_msg_new(self->msgdef, arena);
631
+
632
+ Message_InitPtr(_self, msg, arena_rb);
476
633
 
477
634
  if (argc == 0) {
478
635
  return Qnil;
@@ -480,12 +637,7 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
480
637
  if (argc != 1) {
481
638
  rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
482
639
  }
483
- hash_args = argv[0];
484
- if (TYPE(hash_args) != T_HASH) {
485
- rb_raise(rb_eArgError, "Expected hash arguments.");
486
- }
487
-
488
- rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
640
+ Message_InitFromValue((upb_msg*)self->msg, self->msgdef, argv[0], arena);
489
641
  return Qnil;
490
642
  }
491
643
 
@@ -495,37 +647,40 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
495
647
  *
496
648
  * Performs a shallow copy of this message and returns the new copy.
497
649
  */
498
- VALUE Message_dup(VALUE _self) {
499
- MessageHeader* self;
500
- VALUE new_msg;
501
- MessageHeader* new_msg_self;
502
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
503
-
504
- new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
505
- TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
506
-
507
- layout_dup(self->descriptor->layout,
508
- Message_data(new_msg_self),
509
- Message_data(self));
510
-
650
+ static VALUE Message_dup(VALUE _self) {
651
+ Message* self = ruby_to_Message(_self);
652
+ VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
653
+ Message* new_msg_self = ruby_to_Message(new_msg);
654
+ size_t size = upb_msgdef_layout(self->msgdef)->size;
655
+
656
+ // TODO(copy unknown fields?)
657
+ // TODO(use official upb msg copy function)
658
+ memcpy((upb_msg*)new_msg_self->msg, self->msg, size);
659
+ upb_arena_fuse(Arena_get(new_msg_self->arena), Arena_get(self->arena));
511
660
  return new_msg;
512
661
  }
513
662
 
514
- // Internal only; used by Google::Protobuf.deep_copy.
515
- VALUE Message_deep_copy(VALUE _self) {
516
- MessageHeader* self;
517
- MessageHeader* new_msg_self;
518
- VALUE new_msg;
519
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
663
+ // Support function for Message_eq, and also used by other #eq functions.
664
+ bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m) {
665
+ if (m1 == m2) return true;
520
666
 
521
- new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
522
- TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
667
+ size_t size1, size2;
668
+ int encode_opts = UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC;
669
+ upb_arena *arena_tmp = upb_arena_new();
670
+ const upb_msglayout *layout = upb_msgdef_layout(m);
523
671
 
524
- layout_deep_copy(self->descriptor->layout,
525
- Message_data(new_msg_self),
526
- Message_data(self));
672
+ // Compare deterministically serialized payloads with no unknown fields.
673
+ char *data1 = upb_encode_ex(m1, layout, encode_opts, arena_tmp, &size1);
674
+ char *data2 = upb_encode_ex(m2, layout, encode_opts, arena_tmp, &size2);
527
675
 
528
- return new_msg;
676
+ if (data1 && data2) {
677
+ bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
678
+ upb_arena_free(arena_tmp);
679
+ return ret;
680
+ } else {
681
+ upb_arena_free(arena_tmp);
682
+ rb_raise(cParseError, "Error comparing messages");
683
+ }
529
684
  }
530
685
 
531
686
  /*
@@ -537,22 +692,37 @@ VALUE Message_deep_copy(VALUE _self) {
537
692
  * method's semantics (a more efficient comparison may actually be done if the
538
693
  * field is of a primitive type).
539
694
  */
540
- VALUE Message_eq(VALUE _self, VALUE _other) {
541
- MessageHeader* self;
542
- MessageHeader* other;
695
+ static VALUE Message_eq(VALUE _self, VALUE _other) {
543
696
  if (TYPE(_self) != TYPE(_other)) {
544
697
  return Qfalse;
545
698
  }
546
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
547
- TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
548
699
 
549
- if (self->descriptor != other->descriptor) {
550
- return Qfalse;
551
- }
700
+ Message* self = ruby_to_Message(_self);
701
+ Message* other = ruby_to_Message(_other);
702
+
703
+ return Message_Equal(self->msg, other->msg, self->msgdef)
704
+ ? Qtrue
705
+ : Qfalse;
706
+ }
707
+
708
+ uint64_t Message_Hash(const upb_msg* msg, const upb_msgdef* m, uint64_t seed) {
709
+ upb_arena *arena = upb_arena_new();
710
+ const char *data;
711
+ size_t size;
712
+
713
+ // Hash a deterministically serialized payloads with no unknown fields.
714
+ data = upb_encode_ex(msg, upb_msgdef_layout(m),
715
+ UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC, arena,
716
+ &size);
552
717
 
553
- return layout_eq(self->descriptor->layout,
554
- Message_data(self),
555
- Message_data(other));
718
+ if (data) {
719
+ uint64_t ret = wyhash(data, size, seed, _wyp);
720
+ upb_arena_free(arena);
721
+ return ret;
722
+ } else {
723
+ upb_arena_free(arena);
724
+ rb_raise(cParseError, "Error calculating hash");
725
+ }
556
726
  }
557
727
 
558
728
  /*
@@ -561,11 +731,9 @@ VALUE Message_eq(VALUE _self, VALUE _other) {
561
731
  *
562
732
  * Returns a hash value that represents this message's field values.
563
733
  */
564
- VALUE Message_hash(VALUE _self) {
565
- MessageHeader* self;
566
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
567
-
568
- return layout_hash(self->descriptor->layout, Message_data(self));
734
+ static VALUE Message_hash(VALUE _self) {
735
+ Message* self = ruby_to_Message(_self);
736
+ return INT2FIX(Message_Hash(self->msg, self->msgdef, 0));
569
737
  }
570
738
 
571
739
  /*
@@ -576,75 +744,117 @@ VALUE Message_hash(VALUE _self) {
576
744
  * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
577
745
  * field's value is represented according to its own #inspect method.
578
746
  */
579
- VALUE Message_inspect(VALUE _self) {
580
- MessageHeader* self;
581
- VALUE str;
582
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
747
+ static VALUE Message_inspect(VALUE _self) {
748
+ Message* self = ruby_to_Message(_self);
583
749
 
584
- str = rb_str_new2("<");
585
- str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
586
- str = rb_str_cat2(str, ": ");
587
- str = rb_str_append(str, layout_inspect(
588
- self->descriptor->layout, Message_data(self)));
589
- str = rb_str_cat2(str, ">");
590
- return str;
750
+ StringBuilder* builder = StringBuilder_New();
751
+ Message_PrintMessage(builder, self->msg, self->msgdef);
752
+ VALUE ret = StringBuilder_ToRubyString(builder);
753
+ StringBuilder_Free(builder);
754
+ return ret;
591
755
  }
592
756
 
593
- /*
594
- * call-seq:
595
- * Message.to_h => {}
596
- *
597
- * Returns the message as a Ruby Hash object, with keys as symbols.
598
- */
599
- VALUE Message_to_h(VALUE _self) {
600
- MessageHeader* self;
601
- VALUE hash;
602
- upb_msg_field_iter it;
603
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
604
-
605
- hash = rb_hash_new();
606
-
607
- for (upb_msg_field_begin(&it, self->descriptor->msgdef);
608
- !upb_msg_field_done(&it);
609
- upb_msg_field_next(&it)) {
610
- const upb_fielddef* field = upb_msg_iter_field(&it);
611
-
612
- // For proto2, do not include fields which are not set.
613
- if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
614
- field_contains_hasbit(self->descriptor->layout, field) &&
615
- !layout_has(self->descriptor->layout, Message_data(self), field)) {
757
+ // Support functions for Message_to_h //////////////////////////////////////////
758
+
759
+ static VALUE RepeatedField_CreateArray(const upb_array* arr,
760
+ TypeInfo type_info) {
761
+ int size = arr ? upb_array_size(arr) : 0;
762
+ VALUE ary = rb_ary_new2(size);
763
+
764
+ for (int i = 0; i < size; i++) {
765
+ upb_msgval msgval = upb_array_get(arr, i);
766
+ VALUE val = Scalar_CreateHash(msgval, type_info);
767
+ rb_ary_push(ary, val);
768
+ }
769
+
770
+ return ary;
771
+ }
772
+
773
+ static VALUE Message_CreateHash(const upb_msg *msg, const upb_msgdef *m) {
774
+ if (!msg) return Qnil;
775
+
776
+ VALUE hash = rb_hash_new();
777
+ int n = upb_msgdef_fieldcount(m);
778
+ bool is_proto2;
779
+
780
+ // We currently have a few behaviors that are specific to proto2.
781
+ // This is unfortunate, we should key behaviors off field attributes (like
782
+ // whether a field has presence), not proto2 vs. proto3. We should see if we
783
+ // can change this without breaking users.
784
+ is_proto2 = upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2;
785
+
786
+ for (int i = 0; i < n; i++) {
787
+ const upb_fielddef* field = upb_msgdef_field(m, i);
788
+ TypeInfo type_info = TypeInfo_get(field);
789
+ upb_msgval msgval;
790
+ VALUE msg_value;
791
+ VALUE msg_key;
792
+
793
+ // Do not include fields that are not present (oneof or optional fields).
794
+ if (is_proto2 && upb_fielddef_haspresence(field) &&
795
+ !upb_msg_has(msg, field)) {
616
796
  continue;
617
797
  }
618
798
 
619
- VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self),
620
- field);
621
- VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
622
- if (is_map_field(field)) {
623
- msg_value = Map_to_h(msg_value);
624
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
625
- msg_value = RepeatedField_to_ary(msg_value);
626
- if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
627
- RARRAY_LEN(msg_value) == 0) {
628
- continue;
629
- }
799
+ msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
800
+ msgval = upb_msg_get(msg, field);
630
801
 
631
- if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
632
- for (int i = 0; i < RARRAY_LEN(msg_value); i++) {
633
- VALUE elem = rb_ary_entry(msg_value, i);
634
- rb_ary_store(msg_value, i, Message_to_h(elem));
635
- }
636
- }
802
+ // Proto2 omits empty map/repeated filds also.
637
803
 
638
- } else if (msg_value != Qnil &&
639
- upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
640
- msg_value = Message_to_h(msg_value);
804
+ if (upb_fielddef_ismap(field)) {
805
+ const upb_msgdef *entry_m = upb_fielddef_msgsubdef(field);
806
+ const upb_fielddef *key_f = upb_msgdef_itof(entry_m, 1);
807
+ const upb_fielddef *val_f = upb_msgdef_itof(entry_m, 2);
808
+ upb_fieldtype_t key_type = upb_fielddef_type(key_f);
809
+ msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
810
+ } else if (upb_fielddef_isseq(field)) {
811
+ if (is_proto2 &&
812
+ (!msgval.array_val || upb_array_size(msgval.array_val) == 0)) {
813
+ continue;
814
+ }
815
+ msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
816
+ } else {
817
+ msg_value = Scalar_CreateHash(msgval, type_info);
641
818
  }
819
+
642
820
  rb_hash_aset(hash, msg_key, msg_value);
643
821
  }
822
+
644
823
  return hash;
645
824
  }
646
825
 
826
+ VALUE Scalar_CreateHash(upb_msgval msgval, TypeInfo type_info) {
827
+ if (type_info.type == UPB_TYPE_MESSAGE) {
828
+ return Message_CreateHash(msgval.msg_val, type_info.def.msgdef);
829
+ } else {
830
+ return Convert_UpbToRuby(msgval, type_info, Qnil);
831
+ }
832
+ }
647
833
 
834
+ /*
835
+ * call-seq:
836
+ * Message.to_h => {}
837
+ *
838
+ * Returns the message as a Ruby Hash object, with keys as symbols.
839
+ */
840
+ static VALUE Message_to_h(VALUE _self) {
841
+ Message* self = ruby_to_Message(_self);
842
+ return Message_CreateHash(self->msg, self->msgdef);
843
+ }
844
+
845
+ /*
846
+ * call-seq:
847
+ * Message.freeze => self
848
+ *
849
+ * Freezes the message object. We have to intercept this so we can pin the
850
+ * Ruby object into memory so we don't forget it's frozen.
851
+ */
852
+ static VALUE Message_freeze(VALUE _self) {
853
+ Message* self = ruby_to_Message(_self);
854
+ ObjectCache_Pin(self->msg, _self, Arena_get(self->arena));
855
+ RB_OBJ_FREEZE(_self);
856
+ return _self;
857
+ }
648
858
 
649
859
  /*
650
860
  * call-seq:
@@ -653,16 +863,20 @@ VALUE Message_to_h(VALUE _self) {
653
863
  * Accesses a field's value by field name. The provided field name should be a
654
864
  * string.
655
865
  */
656
- VALUE Message_index(VALUE _self, VALUE field_name) {
657
- MessageHeader* self;
866
+ static VALUE Message_index(VALUE _self, VALUE field_name) {
867
+ Message* self = ruby_to_Message(_self);
658
868
  const upb_fielddef* field;
659
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
869
+ upb_msgval val;
870
+
660
871
  Check_Type(field_name, T_STRING);
661
- field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
872
+ field = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
873
+
662
874
  if (field == NULL) {
663
875
  return Qnil;
664
876
  }
665
- return layout_get(self->descriptor->layout, Message_data(self), field);
877
+
878
+ val = upb_msg_get(self->msg, field);
879
+ return Convert_UpbToRuby(val, TypeInfo_get(field), self->arena);
666
880
  }
667
881
 
668
882
  /*
@@ -672,19 +886,208 @@ VALUE Message_index(VALUE _self, VALUE field_name) {
672
886
  * Sets a field's value by field name. The provided field name should be a
673
887
  * string.
674
888
  */
675
- VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
676
- MessageHeader* self;
677
- const upb_fielddef* field;
678
- TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
889
+ static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
890
+ Message* self = ruby_to_Message(_self);
891
+ const upb_fielddef* f;
892
+ upb_msgval val;
893
+ upb_arena *arena = Arena_get(self->arena);
894
+
679
895
  Check_Type(field_name, T_STRING);
680
- field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
681
- if (field == NULL) {
896
+ f = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
897
+
898
+ if (f == NULL) {
682
899
  rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
683
900
  }
684
- layout_set(self->descriptor->layout, Message_data(self), field, value);
901
+
902
+ val = Convert_RubyToUpb(value, upb_fielddef_name(f), TypeInfo_get(f), arena);
903
+ upb_msg_set(Message_GetMutable(_self, NULL), f, val, arena);
904
+
685
905
  return Qnil;
686
906
  }
687
907
 
908
+ /*
909
+ * call-seq:
910
+ * MessageClass.decode(data) => message
911
+ *
912
+ * Decodes the given data (as a string containing bytes in protocol buffers wire
913
+ * format) under the interpretration given by this message class's definition
914
+ * and returns a message object with the corresponding field values.
915
+ */
916
+ static VALUE Message_decode(VALUE klass, VALUE data) {
917
+ if (TYPE(data) != T_STRING) {
918
+ rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
919
+ }
920
+
921
+ VALUE msg_rb = initialize_rb_class_with_no_args(klass);
922
+ Message* msg = ruby_to_Message(msg_rb);
923
+
924
+ if (!upb_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
925
+ upb_msgdef_layout(msg->msgdef),
926
+ Arena_get(msg->arena))) {
927
+ rb_raise(cParseError, "Error occurred during parsing");
928
+ }
929
+
930
+ return msg_rb;
931
+ }
932
+
933
+ /*
934
+ * call-seq:
935
+ * MessageClass.decode_json(data, options = {}) => message
936
+ *
937
+ * Decodes the given data (as a string containing bytes in protocol buffers wire
938
+ * format) under the interpretration given by this message class's definition
939
+ * and returns a message object with the corresponding field values.
940
+ *
941
+ * @param options [Hash] options for the decoder
942
+ * ignore_unknown_fields: set true to ignore unknown fields (default is to
943
+ * raise an error)
944
+ */
945
+ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
946
+ VALUE data = argv[0];
947
+ int options = 0;
948
+ upb_status status;
949
+
950
+ // TODO(haberman): use this message's pool instead.
951
+ const upb_symtab *symtab = DescriptorPool_GetSymtab(generated_pool);
952
+
953
+ if (argc < 1 || argc > 2) {
954
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
955
+ }
956
+
957
+ if (argc == 2) {
958
+ VALUE hash_args = argv[1];
959
+ if (TYPE(hash_args) != T_HASH) {
960
+ rb_raise(rb_eArgError, "Expected hash arguments.");
961
+ }
962
+
963
+ if (RTEST(rb_hash_lookup2( hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse))) {
964
+ options |= UPB_JSONDEC_IGNOREUNKNOWN;
965
+ }
966
+ }
967
+
968
+ if (TYPE(data) != T_STRING) {
969
+ rb_raise(rb_eArgError, "Expected string for JSON data.");
970
+ }
971
+
972
+ // TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
973
+ // convert, because string handlers pass data directly to message string
974
+ // fields.
975
+
976
+ VALUE msg_rb = initialize_rb_class_with_no_args(klass);
977
+ Message* msg = ruby_to_Message(msg_rb);
978
+
979
+ // We don't allow users to decode a wrapper type directly.
980
+ if (upb_msgdef_iswrapper(msg->msgdef)) {
981
+ rb_raise(rb_eRuntimeError, "Cannot parse a wrapper directly.");
982
+ }
983
+
984
+ upb_status_clear(&status);
985
+ if (!upb_json_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
986
+ msg->msgdef, symtab, options,
987
+ Arena_get(msg->arena), &status)) {
988
+ rb_raise(cParseError, "Error occurred during parsing: %s",
989
+ upb_status_errmsg(&status));
990
+ }
991
+
992
+ return msg_rb;
993
+ }
994
+
995
+ /*
996
+ * call-seq:
997
+ * MessageClass.encode(msg) => bytes
998
+ *
999
+ * Encodes the given message object to its serialized form in protocol buffers
1000
+ * wire format.
1001
+ */
1002
+ static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
1003
+ Message* msg = ruby_to_Message(msg_rb);
1004
+ upb_arena *arena = upb_arena_new();
1005
+ const char *data;
1006
+ size_t size;
1007
+
1008
+ if (CLASS_OF(msg_rb) != klass) {
1009
+ rb_raise(rb_eArgError, "Message of wrong type.");
1010
+ }
1011
+
1012
+ data = upb_encode(msg->msg, upb_msgdef_layout(msg->msgdef), arena,
1013
+ &size);
1014
+
1015
+ if (data) {
1016
+ VALUE ret = rb_str_new(data, size);
1017
+ rb_enc_associate(ret, rb_ascii8bit_encoding());
1018
+ upb_arena_free(arena);
1019
+ return ret;
1020
+ } else {
1021
+ upb_arena_free(arena);
1022
+ rb_raise(rb_eRuntimeError, "Exceeded maximum depth (possibly cycle)");
1023
+ }
1024
+ }
1025
+
1026
+ /*
1027
+ * call-seq:
1028
+ * MessageClass.encode_json(msg, options = {}) => json_string
1029
+ *
1030
+ * Encodes the given message object into its serialized JSON representation.
1031
+ * @param options [Hash] options for the decoder
1032
+ * preserve_proto_fieldnames: set true to use original fieldnames (default is to camelCase)
1033
+ * emit_defaults: set true to emit 0/false values (default is to omit them)
1034
+ */
1035
+ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
1036
+ Message* msg = ruby_to_Message(argv[0]);
1037
+ int options = 0;
1038
+ char buf[1024];
1039
+ size_t size;
1040
+ upb_status status;
1041
+
1042
+ // TODO(haberman): use this message's pool instead.
1043
+ const upb_symtab *symtab = DescriptorPool_GetSymtab(generated_pool);
1044
+
1045
+ if (argc < 1 || argc > 2) {
1046
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1047
+ }
1048
+
1049
+ if (argc == 2) {
1050
+ VALUE hash_args = argv[1];
1051
+ if (TYPE(hash_args) != T_HASH) {
1052
+ rb_raise(rb_eArgError, "Expected hash arguments.");
1053
+ }
1054
+
1055
+ if (RTEST(rb_hash_lookup2(hash_args,
1056
+ ID2SYM(rb_intern("preserve_proto_fieldnames")),
1057
+ Qfalse))) {
1058
+ options |= UPB_JSONENC_PROTONAMES;
1059
+ }
1060
+
1061
+ if (RTEST(rb_hash_lookup2(hash_args, ID2SYM(rb_intern("emit_defaults")),
1062
+ Qfalse))) {
1063
+ options |= UPB_JSONENC_EMITDEFAULTS;
1064
+ }
1065
+ }
1066
+
1067
+ upb_status_clear(&status);
1068
+ size = upb_json_encode(msg->msg, msg->msgdef, symtab, options, buf,
1069
+ sizeof(buf), &status);
1070
+
1071
+ if (!upb_ok(&status)) {
1072
+ rb_raise(cParseError, "Error occurred during encoding: %s",
1073
+ upb_status_errmsg(&status));
1074
+ }
1075
+
1076
+ VALUE ret;
1077
+ if (size >= sizeof(buf)) {
1078
+ char* buf2 = malloc(size + 1);
1079
+ upb_json_encode(msg->msg, msg->msgdef, symtab, options, buf2, size + 1,
1080
+ &status);
1081
+ ret = rb_str_new(buf2, size);
1082
+ free(buf2);
1083
+ } else {
1084
+ ret = rb_str_new(buf, size);
1085
+ }
1086
+
1087
+ rb_enc_associate(ret, rb_utf8_encoding());
1088
+ return ret;
1089
+ }
1090
+
688
1091
  /*
689
1092
  * call-seq:
690
1093
  * Message.descriptor => descriptor
@@ -692,22 +1095,15 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
692
1095
  * Class method that returns the Descriptor instance corresponding to this
693
1096
  * message class's type.
694
1097
  */
695
- VALUE Message_descriptor(VALUE klass) {
1098
+ static VALUE Message_descriptor(VALUE klass) {
696
1099
  return rb_ivar_get(klass, descriptor_instancevar_interned);
697
1100
  }
698
1101
 
699
- VALUE build_class_from_descriptor(Descriptor* desc) {
1102
+ VALUE build_class_from_descriptor(VALUE descriptor) {
700
1103
  const char *name;
701
1104
  VALUE klass;
702
1105
 
703
- if (desc->layout == NULL) {
704
- desc->layout = create_layout(desc->msgdef);
705
- }
706
- if (desc->fill_method == NULL) {
707
- desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
708
- }
709
-
710
- name = upb_msgdef_fullname(desc->msgdef);
1106
+ name = upb_msgdef_fullname(Descriptor_GetMsgDef(descriptor));
711
1107
  if (name == NULL) {
712
1108
  rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
713
1109
  }
@@ -717,8 +1113,7 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
717
1113
  // their own toplevel constant class name.
718
1114
  rb_intern("Message"),
719
1115
  rb_cObject);
720
- rb_ivar_set(klass, descriptor_instancevar_interned,
721
- get_def_obj(desc->msgdef));
1116
+ rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
722
1117
  rb_define_alloc_func(klass, Message_alloc);
723
1118
  rb_require("google/protobuf/message_exts");
724
1119
  rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
@@ -735,9 +1130,9 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
735
1130
  rb_define_method(klass, "clone", Message_dup, 0);
736
1131
  rb_define_method(klass, "==", Message_eq, 1);
737
1132
  rb_define_method(klass, "eql?", Message_eq, 1);
1133
+ rb_define_method(klass, "freeze", Message_freeze, 0);
738
1134
  rb_define_method(klass, "hash", Message_hash, 0);
739
1135
  rb_define_method(klass, "to_h", Message_to_h, 0);
740
- rb_define_method(klass, "to_hash", Message_to_h, 0);
741
1136
  rb_define_method(klass, "inspect", Message_inspect, 0);
742
1137
  rb_define_method(klass, "to_s", Message_inspect, 0);
743
1138
  rb_define_method(klass, "[]", Message_index, 1);
@@ -758,12 +1153,12 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
758
1153
  * This module method, provided on each generated enum module, looks up an enum
759
1154
  * value by number and returns its name as a Ruby symbol, or nil if not found.
760
1155
  */
761
- VALUE enum_lookup(VALUE self, VALUE number) {
1156
+ static VALUE enum_lookup(VALUE self, VALUE number) {
762
1157
  int32_t num = NUM2INT(number);
763
1158
  VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
764
- EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
1159
+ const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
765
1160
 
766
- const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
1161
+ const char* name = upb_enumdef_iton(e, num);
767
1162
  if (name == NULL) {
768
1163
  return Qnil;
769
1164
  } else {
@@ -778,13 +1173,13 @@ VALUE enum_lookup(VALUE self, VALUE number) {
778
1173
  * This module method, provided on each generated enum module, looks up an enum
779
1174
  * value by name (as a Ruby symbol) and returns its name, or nil if not found.
780
1175
  */
781
- VALUE enum_resolve(VALUE self, VALUE sym) {
1176
+ static VALUE enum_resolve(VALUE self, VALUE sym) {
782
1177
  const char* name = rb_id2name(SYM2ID(sym));
783
1178
  VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
784
- EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
1179
+ const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
785
1180
 
786
1181
  int32_t num = 0;
787
- bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
1182
+ bool found = upb_enumdef_ntoiz(e, name, &num);
788
1183
  if (!found) {
789
1184
  return Qnil;
790
1185
  } else {
@@ -799,16 +1194,16 @@ VALUE enum_resolve(VALUE self, VALUE sym) {
799
1194
  * This module method, provided on each generated enum module, returns the
800
1195
  * EnumDescriptor corresponding to this enum type.
801
1196
  */
802
- VALUE enum_descriptor(VALUE self) {
1197
+ static VALUE enum_descriptor(VALUE self) {
803
1198
  return rb_ivar_get(self, descriptor_instancevar_interned);
804
1199
  }
805
1200
 
806
- VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
807
- VALUE mod = rb_define_module_id(
808
- rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
1201
+ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
1202
+ const upb_enumdef *e = EnumDescriptor_GetEnumDef(_enumdesc);
1203
+ VALUE mod = rb_define_module_id(rb_intern(upb_enumdef_fullname(e)));
809
1204
 
810
1205
  upb_enum_iter it;
811
- for (upb_enum_begin(&it, enumdesc->enumdef);
1206
+ for (upb_enum_begin(&it, e);
812
1207
  !upb_enum_done(&it);
813
1208
  upb_enum_next(&it)) {
814
1209
  const char* name = upb_enum_iter_name(&it);
@@ -824,26 +1219,97 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
824
1219
  rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
825
1220
  rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
826
1221
  rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
827
- rb_ivar_set(mod, descriptor_instancevar_interned,
828
- get_def_obj(enumdesc->enumdef));
1222
+ rb_ivar_set(mod, descriptor_instancevar_interned, _enumdesc);
829
1223
 
830
1224
  return mod;
831
1225
  }
832
1226
 
833
- /*
834
- * call-seq:
835
- * Google::Protobuf.deep_copy(obj) => copy_of_obj
836
- *
837
- * Performs a deep copy of a RepeatedField instance, a Map instance, or a
838
- * message object, recursively copying its members.
839
- */
840
- VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
841
- VALUE klass = CLASS_OF(obj);
842
- if (klass == cRepeatedField) {
843
- return RepeatedField_deep_copy(obj);
844
- } else if (klass == cMap) {
845
- return Map_deep_copy(obj);
846
- } else {
847
- return Message_deep_copy(obj);
1227
+ // Internal only; used by Google::Protobuf.deep_copy.
1228
+ upb_msg* Message_deep_copy(const upb_msg* msg, const upb_msgdef* m,
1229
+ upb_arena *arena) {
1230
+ // Serialize and parse.
1231
+ upb_arena *tmp_arena = upb_arena_new();
1232
+ const upb_msglayout *layout = upb_msgdef_layout(m);
1233
+ size_t size;
1234
+
1235
+ char* data = upb_encode_ex(msg, layout, 0, tmp_arena, &size);
1236
+ upb_msg* new_msg = upb_msg_new(m, arena);
1237
+
1238
+ if (!data || !upb_decode(data, size, new_msg, layout, arena)) {
1239
+ upb_arena_free(tmp_arena);
1240
+ rb_raise(cParseError, "Error occurred copying proto");
848
1241
  }
1242
+
1243
+ upb_arena_free(tmp_arena);
1244
+ return new_msg;
1245
+ }
1246
+
1247
+ const upb_msg* Message_GetUpbMessage(VALUE value, const upb_msgdef* m,
1248
+ const char* name, upb_arena* arena) {
1249
+ if (value == Qnil) return NULL;
1250
+
1251
+ VALUE klass = CLASS_OF(value);
1252
+ VALUE desc_rb = rb_ivar_get(klass, descriptor_instancevar_interned);
1253
+ const upb_msgdef* val_m =
1254
+ desc_rb == Qnil ? NULL : Descriptor_GetMsgDef(desc_rb);
1255
+
1256
+ if (val_m != m) {
1257
+ // Check for possible implicit conversions
1258
+ // TODO: hash conversion?
1259
+
1260
+ switch (upb_msgdef_wellknowntype(m)) {
1261
+ case UPB_WELLKNOWN_TIMESTAMP: {
1262
+ // Time -> Google::Protobuf::Timestamp
1263
+ upb_msg *msg = upb_msg_new(m, arena);
1264
+ upb_msgval sec, nsec;
1265
+ struct timespec time;
1266
+ const upb_fielddef *sec_f = upb_msgdef_itof(m, 1);
1267
+ const upb_fielddef *nsec_f = upb_msgdef_itof(m, 2);
1268
+
1269
+ if (!rb_obj_is_kind_of(value, rb_cTime)) goto badtype;
1270
+
1271
+ time = rb_time_timespec(value);
1272
+ sec.int64_val = time.tv_sec;
1273
+ nsec.int32_val = time.tv_nsec;
1274
+ upb_msg_set(msg, sec_f, sec, arena);
1275
+ upb_msg_set(msg, nsec_f, nsec, arena);
1276
+ return msg;
1277
+ }
1278
+ case UPB_WELLKNOWN_DURATION: {
1279
+ // Numeric -> Google::Protobuf::Duration
1280
+ upb_msg *msg = upb_msg_new(m, arena);
1281
+ upb_msgval sec, nsec;
1282
+ const upb_fielddef *sec_f = upb_msgdef_itof(m, 1);
1283
+ const upb_fielddef *nsec_f = upb_msgdef_itof(m, 2);
1284
+
1285
+ if (!rb_obj_is_kind_of(value, rb_cNumeric)) goto badtype;
1286
+
1287
+ sec.int64_val = NUM2LL(value);
1288
+ nsec.int32_val = (NUM2DBL(value) - NUM2LL(value)) * 1000000000;
1289
+ upb_msg_set(msg, sec_f, sec, arena);
1290
+ upb_msg_set(msg, nsec_f, nsec, arena);
1291
+ return msg;
1292
+ }
1293
+ default:
1294
+ badtype:
1295
+ rb_raise(cTypeError,
1296
+ "Invalid type %s to assign to submessage field '%s'.",
1297
+ rb_class2name(CLASS_OF(value)), name);
1298
+ }
1299
+
1300
+ }
1301
+
1302
+ Message* self = ruby_to_Message(value);
1303
+ upb_arena_fuse(arena, Arena_get(self->arena));
1304
+
1305
+ return self->msg;
1306
+ }
1307
+
1308
+ void Message_register(VALUE protobuf) {
1309
+ cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
1310
+
1311
+ // Ruby-interned string: "descriptor". We use this identifier to store an
1312
+ // instance variable on message classes we create in order to link them back
1313
+ // to their descriptors.
1314
+ descriptor_instancevar_interned = rb_intern("descriptor");
849
1315
  }