blob: 283939c9df1be4170a3f185470bb5be6faae9d69 [file] [log] [blame]
Chris Fallin973f4252014-11-18 14:19:58 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2014 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#include "protobuf.h"
32
33// -----------------------------------------------------------------------------
34// Class/module creation from msgdefs and enumdefs, respectively.
35// -----------------------------------------------------------------------------
36
37void* Message_data(void* msg) {
38 return ((uint8_t *)msg) + sizeof(MessageHeader);
39}
40
41void Message_mark(void* _self) {
42 MessageHeader* self = (MessageHeader *)_self;
43 layout_mark(self->descriptor->layout, Message_data(self));
44}
45
46void Message_free(void* self) {
47 xfree(self);
48}
49
50rb_data_type_t Message_type = {
51 "Message",
52 { Message_mark, Message_free, NULL },
53};
54
55VALUE Message_alloc(VALUE klass) {
Chris Fallin231886f2015-05-19 15:33:48 -070056 VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
Chris Fallin973f4252014-11-18 14:19:58 -080057 Descriptor* desc = ruby_to_Descriptor(descriptor);
58 MessageHeader* msg = (MessageHeader*)ALLOC_N(
59 uint8_t, sizeof(MessageHeader) + desc->layout->size);
Josh Habermana1daeab2015-07-10 11:56:06 -070060 VALUE ret;
61
Chris Fallin973f4252014-11-18 14:19:58 -080062 memset(Message_data(msg), 0, desc->layout->size);
63
64 // We wrap first so that everything in the message object is GC-rooted in case
65 // a collection happens during object creation in layout_init().
Josh Habermana1daeab2015-07-10 11:56:06 -070066 ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
Chris Fallin973f4252014-11-18 14:19:58 -080067 msg->descriptor = desc;
Chris Fallin231886f2015-05-19 15:33:48 -070068 rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
Chris Fallin973f4252014-11-18 14:19:58 -080069
70 layout_init(desc->layout, Message_data(msg));
71
72 return ret;
73}
74
Chris Falline2debef2015-01-14 18:02:27 -080075static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
Josh Habermana1daeab2015-07-10 11:56:06 -070076 upb_oneof_iter it;
77 size_t case_ofs;
78 uint32_t oneof_case;
79 const upb_fielddef* first_field;
80 const upb_fielddef* f;
81
Chris Falline2debef2015-01-14 18:02:27 -080082 // If no fields in the oneof, always nil.
83 if (upb_oneofdef_numfields(o) == 0) {
84 return Qnil;
85 }
86 // Grab the first field in the oneof so we can get its layout info to find the
87 // oneof_case field.
Chris Falline2debef2015-01-14 18:02:27 -080088 upb_oneof_begin(&it, o);
89 assert(!upb_oneof_done(&it));
Josh Habermana1daeab2015-07-10 11:56:06 -070090 first_field = upb_oneof_iter_field(&it);
Chris Falline2debef2015-01-14 18:02:27 -080091 assert(upb_fielddef_containingoneof(first_field) != NULL);
92
Josh Habermana1daeab2015-07-10 11:56:06 -070093 case_ofs =
Chris Falline2debef2015-01-14 18:02:27 -080094 self->descriptor->layout->
95 fields[upb_fielddef_index(first_field)].case_offset;
Josh Habermana1daeab2015-07-10 11:56:06 -070096 oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
Chris Falline2debef2015-01-14 18:02:27 -080097
Chris Fallina3953da2015-02-02 13:16:57 -080098 if (oneof_case == ONEOF_CASE_NONE) {
Chris Falline2debef2015-01-14 18:02:27 -080099 return Qnil;
100 }
101
102 // oneof_case is a field index, so find that field.
Josh Habermana1daeab2015-07-10 11:56:06 -0700103 f = upb_oneofdef_itof(o, oneof_case);
Chris Falline2debef2015-01-14 18:02:27 -0800104 assert(f != NULL);
105
106 return ID2SYM(rb_intern(upb_fielddef_name(f)));
107}
108
Chris Fallin973f4252014-11-18 14:19:58 -0800109/*
110 * call-seq:
111 * Message.method_missing(*args)
112 *
113 * Provides accessors and setters for message fields according to their field
114 * names. For any field whose name does not conflict with a built-in method, an
115 * accessor is provided with the same name as the field, and a setter is
116 * provided with the name of the field plus the '=' suffix. Thus, given a
117 * message instance 'msg' with field 'foo', the following code is valid:
118 *
119 * msg.foo = 42
120 * puts msg.foo
Chris Falline2debef2015-01-14 18:02:27 -0800121 *
122 * This method also provides read-only accessors for oneofs. If a oneof exists
123 * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
124 * the name of the field in that oneof that is currently set, or nil if none.
Chris Fallin973f4252014-11-18 14:19:58 -0800125 */
126VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
127 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700128 VALUE method_name, method_str;
129 char* name;
130 size_t name_len;
131 bool setter;
132 const upb_oneofdef* o;
133 const upb_fielddef* f;
134
Chris Fallin973f4252014-11-18 14:19:58 -0800135 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
136 if (argc < 1) {
137 rb_raise(rb_eArgError, "Expected method name as first argument.");
138 }
Josh Habermana1daeab2015-07-10 11:56:06 -0700139 method_name = argv[0];
Chris Fallin973f4252014-11-18 14:19:58 -0800140 if (!SYMBOL_P(method_name)) {
141 rb_raise(rb_eArgError, "Expected symbol as method name.");
142 }
Josh Habermana1daeab2015-07-10 11:56:06 -0700143 method_str = rb_id2str(SYM2ID(method_name));
144 name = RSTRING_PTR(method_str);
145 name_len = RSTRING_LEN(method_str);
146 setter = false;
Chris Fallin973f4252014-11-18 14:19:58 -0800147
148 // Setters have names that end in '='.
149 if (name[name_len - 1] == '=') {
150 setter = true;
151 name_len--;
152 }
153
Chris Falline2debef2015-01-14 18:02:27 -0800154 // Check for a oneof name first.
Josh Habermana1daeab2015-07-10 11:56:06 -0700155 o = upb_msgdef_ntoo(self->descriptor->msgdef,
Chris Falline2debef2015-01-14 18:02:27 -0800156 name, name_len);
157 if (o != NULL) {
158 if (setter) {
159 rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
160 }
161 return which_oneof_field(self, o);
162 }
163
164 // Otherwise, check for a field with that name.
Josh Habermana1daeab2015-07-10 11:56:06 -0700165 f = upb_msgdef_ntof(self->descriptor->msgdef,
Chris Fallin973f4252014-11-18 14:19:58 -0800166 name, name_len);
167
168 if (f == NULL) {
Anders Carling0df1e392015-11-20 21:55:13 +0100169 return rb_call_super(argc, argv);
Chris Fallin973f4252014-11-18 14:19:58 -0800170 }
171
172 if (setter) {
173 if (argc < 2) {
174 rb_raise(rb_eArgError, "No value provided to setter.");
175 }
176 layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
177 return Qnil;
178 } else {
179 return layout_get(self->descriptor->layout, Message_data(self), f);
180 }
181}
182
183int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
184 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700185 VALUE method_str;
186 char* name;
187 const upb_fielddef* f;
Chris Fallin973f4252014-11-18 14:19:58 -0800188 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
189
190 if (!SYMBOL_P(key)) {
191 rb_raise(rb_eArgError,
192 "Expected symbols as hash keys in initialization map.");
193 }
194
Josh Habermana1daeab2015-07-10 11:56:06 -0700195 method_str = rb_id2str(SYM2ID(key));
196 name = RSTRING_PTR(method_str);
197 f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
Chris Fallin973f4252014-11-18 14:19:58 -0800198 if (f == NULL) {
199 rb_raise(rb_eArgError,
Anders Carling0559f3e2015-11-20 21:57:28 +0100200 "Unknown field name '%s' in initialization map entry.", name);
Chris Fallin973f4252014-11-18 14:19:58 -0800201 }
202
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800203 if (is_map_field(f)) {
Josh Habermana1daeab2015-07-10 11:56:06 -0700204 VALUE map;
205
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800206 if (TYPE(val) != T_HASH) {
207 rb_raise(rb_eArgError,
Anders Carling0559f3e2015-11-20 21:57:28 +0100208 "Expected Hash object as initializer value for map field '%s'.", name);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800209 }
Josh Habermana1daeab2015-07-10 11:56:06 -0700210 map = layout_get(self->descriptor->layout, Message_data(self), f);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800211 Map_merge_into_self(map, val);
212 } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
Josh Habermana1daeab2015-07-10 11:56:06 -0700213 VALUE ary;
214
Chris Fallin973f4252014-11-18 14:19:58 -0800215 if (TYPE(val) != T_ARRAY) {
216 rb_raise(rb_eArgError,
Anders Carling0559f3e2015-11-20 21:57:28 +0100217 "Expected array as initializer value for repeated field '%s'.", name);
Chris Fallin973f4252014-11-18 14:19:58 -0800218 }
Josh Habermana1daeab2015-07-10 11:56:06 -0700219 ary = layout_get(self->descriptor->layout, Message_data(self), f);
Chris Fallin973f4252014-11-18 14:19:58 -0800220 for (int i = 0; i < RARRAY_LEN(val); i++) {
221 RepeatedField_push(ary, rb_ary_entry(val, i));
222 }
223 } else {
224 layout_set(self->descriptor->layout, Message_data(self), f, val);
225 }
226 return 0;
227}
228
229/*
230 * call-seq:
231 * Message.new(kwargs) => new_message
232 *
233 * Creates a new instance of the given message class. Keyword arguments may be
234 * provided with keywords corresponding to field names.
235 *
236 * Note that no literal Message class exists. Only concrete classes per message
237 * type exist, as provided by the #msgclass method on Descriptors after they
238 * have been added to a pool. The method definitions described here on the
239 * Message class are provided on each concrete message class.
240 */
241VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
Josh Habermana1daeab2015-07-10 11:56:06 -0700242 VALUE hash_args;
243
Chris Fallin973f4252014-11-18 14:19:58 -0800244 if (argc == 0) {
245 return Qnil;
246 }
247 if (argc != 1) {
248 rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
249 }
Josh Habermana1daeab2015-07-10 11:56:06 -0700250 hash_args = argv[0];
Chris Fallin973f4252014-11-18 14:19:58 -0800251 if (TYPE(hash_args) != T_HASH) {
252 rb_raise(rb_eArgError, "Expected hash arguments.");
253 }
254
255 rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
256 return Qnil;
257}
258
259/*
260 * call-seq:
261 * Message.dup => new_message
262 *
263 * Performs a shallow copy of this message and returns the new copy.
264 */
265VALUE Message_dup(VALUE _self) {
266 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700267 VALUE new_msg;
268 MessageHeader* new_msg_self;
Chris Fallin973f4252014-11-18 14:19:58 -0800269 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
270
Josh Habermana1daeab2015-07-10 11:56:06 -0700271 new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
Chris Fallin973f4252014-11-18 14:19:58 -0800272 TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
273
274 layout_dup(self->descriptor->layout,
275 Message_data(new_msg_self),
276 Message_data(self));
277
278 return new_msg;
279}
280
281// Internal only; used by Google::Protobuf.deep_copy.
282VALUE Message_deep_copy(VALUE _self) {
283 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700284 MessageHeader* new_msg_self;
285 VALUE new_msg;
Chris Fallin973f4252014-11-18 14:19:58 -0800286 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
287
Josh Habermana1daeab2015-07-10 11:56:06 -0700288 new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
Chris Fallin973f4252014-11-18 14:19:58 -0800289 TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
290
291 layout_deep_copy(self->descriptor->layout,
292 Message_data(new_msg_self),
293 Message_data(self));
294
295 return new_msg;
296}
297
298/*
299 * call-seq:
300 * Message.==(other) => boolean
301 *
302 * Performs a deep comparison of this message with another. Messages are equal
303 * if they have the same type and if each field is equal according to the :==
304 * method's semantics (a more efficient comparison may actually be done if the
305 * field is of a primitive type).
306 */
307VALUE Message_eq(VALUE _self, VALUE _other) {
308 MessageHeader* self;
Chris Fallin973f4252014-11-18 14:19:58 -0800309 MessageHeader* other;
Josh Habermana1daeab2015-07-10 11:56:06 -0700310 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
Chris Fallin973f4252014-11-18 14:19:58 -0800311 TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
312
313 if (self->descriptor != other->descriptor) {
314 return Qfalse;
315 }
316
317 return layout_eq(self->descriptor->layout,
318 Message_data(self),
319 Message_data(other));
320}
321
322/*
323 * call-seq:
324 * Message.hash => hash_value
325 *
326 * Returns a hash value that represents this message's field values.
327 */
328VALUE Message_hash(VALUE _self) {
329 MessageHeader* self;
330 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
331
332 return layout_hash(self->descriptor->layout, Message_data(self));
333}
334
335/*
336 * call-seq:
337 * Message.inspect => string
338 *
339 * Returns a human-readable string representing this message. It will be
340 * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
341 * field's value is represented according to its own #inspect method.
342 */
343VALUE Message_inspect(VALUE _self) {
344 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700345 VALUE str;
Chris Fallin973f4252014-11-18 14:19:58 -0800346 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
347
Josh Habermana1daeab2015-07-10 11:56:06 -0700348 str = rb_str_new2("<");
Chris Fallin973f4252014-11-18 14:19:58 -0800349 str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
350 str = rb_str_cat2(str, ": ");
351 str = rb_str_append(str, layout_inspect(
352 self->descriptor->layout, Message_data(self)));
353 str = rb_str_cat2(str, ">");
354 return str;
355}
356
Adam Greened1b52a02015-05-03 10:55:10 -0700357
358VALUE Message_to_h(VALUE _self) {
359 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700360 VALUE hash;
361 upb_msg_field_iter it;
Adam Greened1b52a02015-05-03 10:55:10 -0700362 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
363
Josh Habermana1daeab2015-07-10 11:56:06 -0700364 hash = rb_hash_new();
Adam Greened1b52a02015-05-03 10:55:10 -0700365
Adam Greened1b52a02015-05-03 10:55:10 -0700366 for (upb_msg_field_begin(&it, self->descriptor->msgdef);
367 !upb_msg_field_done(&it);
368 upb_msg_field_next(&it)) {
369 const upb_fielddef* field = upb_msg_iter_field(&it);
Chris Fallin231886f2015-05-19 15:33:48 -0700370 VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self),
371 field);
Adam Greened1b52a02015-05-03 10:55:10 -0700372 VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
373 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
374 msg_value = RepeatedField_to_ary(msg_value);
375 }
376 rb_hash_aset(hash, msg_key, msg_value);
377 }
378 return hash;
379}
380
381
382
Chris Fallin973f4252014-11-18 14:19:58 -0800383/*
384 * call-seq:
385 * Message.[](index) => value
386 *
387 * Accesses a field's value by field name. The provided field name should be a
388 * string.
389 */
390VALUE Message_index(VALUE _self, VALUE field_name) {
391 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700392 const upb_fielddef* field;
Chris Fallin973f4252014-11-18 14:19:58 -0800393 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
394 Check_Type(field_name, T_STRING);
Josh Habermana1daeab2015-07-10 11:56:06 -0700395 field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
Chris Fallin973f4252014-11-18 14:19:58 -0800396 if (field == NULL) {
397 return Qnil;
398 }
399 return layout_get(self->descriptor->layout, Message_data(self), field);
400}
401
402/*
403 * call-seq:
404 * Message.[]=(index, value)
405 *
406 * Sets a field's value by field name. The provided field name should be a
407 * string.
408 */
409VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
410 MessageHeader* self;
Josh Habermana1daeab2015-07-10 11:56:06 -0700411 const upb_fielddef* field;
Chris Fallin973f4252014-11-18 14:19:58 -0800412 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
413 Check_Type(field_name, T_STRING);
Josh Habermana1daeab2015-07-10 11:56:06 -0700414 field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
Chris Fallin973f4252014-11-18 14:19:58 -0800415 if (field == NULL) {
416 rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
417 }
418 layout_set(self->descriptor->layout, Message_data(self), field, value);
419 return Qnil;
420}
421
422/*
423 * call-seq:
424 * Message.descriptor => descriptor
425 *
426 * Class method that returns the Descriptor instance corresponding to this
427 * message class's type.
428 */
429VALUE Message_descriptor(VALUE klass) {
Chris Fallin231886f2015-05-19 15:33:48 -0700430 return rb_ivar_get(klass, descriptor_instancevar_interned);
Chris Fallin973f4252014-11-18 14:19:58 -0800431}
432
433VALUE build_class_from_descriptor(Descriptor* desc) {
Josh Habermana1daeab2015-07-10 11:56:06 -0700434 const char *name;
435 VALUE klass;
436
Chris Fallin973f4252014-11-18 14:19:58 -0800437 if (desc->layout == NULL) {
438 desc->layout = create_layout(desc->msgdef);
439 }
440 if (desc->fill_method == NULL) {
441 desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
442 }
443
Josh Habermana1daeab2015-07-10 11:56:06 -0700444 name = upb_msgdef_fullname(desc->msgdef);
Chris Fallin973f4252014-11-18 14:19:58 -0800445 if (name == NULL) {
446 rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
447 }
448
Josh Habermana1daeab2015-07-10 11:56:06 -0700449 klass = rb_define_class_id(
Chris Fallin973f4252014-11-18 14:19:58 -0800450 // Docs say this parameter is ignored. User will assign return value to
451 // their own toplevel constant class name.
452 rb_intern("Message"),
453 rb_cObject);
Chris Fallin231886f2015-05-19 15:33:48 -0700454 rb_ivar_set(klass, descriptor_instancevar_interned,
455 get_def_obj(desc->msgdef));
Chris Fallin973f4252014-11-18 14:19:58 -0800456 rb_define_alloc_func(klass, Message_alloc);
Adam Greened1b52a02015-05-03 10:55:10 -0700457 rb_require("google/protobuf/message_exts");
458 rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts"));
Chris Fallin231886f2015-05-19 15:33:48 -0700459 rb_extend_object(
460 klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods"));
Adam Greened1b52a02015-05-03 10:55:10 -0700461
Chris Fallin973f4252014-11-18 14:19:58 -0800462 rb_define_method(klass, "method_missing",
463 Message_method_missing, -1);
464 rb_define_method(klass, "initialize", Message_initialize, -1);
465 rb_define_method(klass, "dup", Message_dup, 0);
466 // Also define #clone so that we don't inherit Object#clone.
467 rb_define_method(klass, "clone", Message_dup, 0);
468 rb_define_method(klass, "==", Message_eq, 1);
469 rb_define_method(klass, "hash", Message_hash, 0);
Adam Greened1b52a02015-05-03 10:55:10 -0700470 rb_define_method(klass, "to_h", Message_to_h, 0);
471 rb_define_method(klass, "to_hash", Message_to_h, 0);
Chris Fallin973f4252014-11-18 14:19:58 -0800472 rb_define_method(klass, "inspect", Message_inspect, 0);
473 rb_define_method(klass, "[]", Message_index, 1);
474 rb_define_method(klass, "[]=", Message_index_set, 2);
475 rb_define_singleton_method(klass, "decode", Message_decode, 1);
476 rb_define_singleton_method(klass, "encode", Message_encode, 1);
477 rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1);
478 rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1);
479 rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
Adam Greened1b52a02015-05-03 10:55:10 -0700480
Chris Fallin973f4252014-11-18 14:19:58 -0800481 return klass;
482}
483
484/*
485 * call-seq:
486 * Enum.lookup(number) => name
487 *
488 * This module method, provided on each generated enum module, looks up an enum
489 * value by number and returns its name as a Ruby symbol, or nil if not found.
490 */
491VALUE enum_lookup(VALUE self, VALUE number) {
492 int32_t num = NUM2INT(number);
Chris Fallin231886f2015-05-19 15:33:48 -0700493 VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
Chris Fallin973f4252014-11-18 14:19:58 -0800494 EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
495
496 const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
497 if (name == NULL) {
498 return Qnil;
499 } else {
500 return ID2SYM(rb_intern(name));
501 }
502}
503
504/*
505 * call-seq:
506 * Enum.resolve(name) => number
507 *
508 * This module method, provided on each generated enum module, looks up an enum
509 * value by name (as a Ruby symbol) and returns its name, or nil if not found.
510 */
511VALUE enum_resolve(VALUE self, VALUE sym) {
512 const char* name = rb_id2name(SYM2ID(sym));
Chris Fallin231886f2015-05-19 15:33:48 -0700513 VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
Chris Fallin973f4252014-11-18 14:19:58 -0800514 EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
515
516 int32_t num = 0;
517 bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
518 if (!found) {
519 return Qnil;
520 } else {
521 return INT2NUM(num);
522 }
523}
524
525/*
526 * call-seq:
527 * Enum.descriptor
528 *
529 * This module method, provided on each generated enum module, returns the
530 * EnumDescriptor corresponding to this enum type.
531 */
532VALUE enum_descriptor(VALUE self) {
Chris Fallin231886f2015-05-19 15:33:48 -0700533 return rb_ivar_get(self, descriptor_instancevar_interned);
Chris Fallin973f4252014-11-18 14:19:58 -0800534}
535
536VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
537 VALUE mod = rb_define_module_id(
538 rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
539
540 upb_enum_iter it;
541 for (upb_enum_begin(&it, enumdesc->enumdef);
542 !upb_enum_done(&it);
543 upb_enum_next(&it)) {
544 const char* name = upb_enum_iter_name(&it);
545 int32_t value = upb_enum_iter_number(&it);
546 if (name[0] < 'A' || name[0] > 'Z') {
547 rb_raise(rb_eTypeError,
548 "Enum value '%s' does not start with an uppercase letter "
549 "as is required for Ruby constants.",
550 name);
551 }
552 rb_define_const(mod, name, INT2NUM(value));
553 }
554
555 rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
556 rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
557 rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
Chris Fallin231886f2015-05-19 15:33:48 -0700558 rb_ivar_set(mod, descriptor_instancevar_interned,
559 get_def_obj(enumdesc->enumdef));
Chris Fallin973f4252014-11-18 14:19:58 -0800560
561 return mod;
562}
563
564/*
565 * call-seq:
566 * Google::Protobuf.deep_copy(obj) => copy_of_obj
567 *
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800568 * Performs a deep copy of a RepeatedField instance, a Map instance, or a
569 * message object, recursively copying its members.
Chris Fallin973f4252014-11-18 14:19:58 -0800570 */
571VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
572 VALUE klass = CLASS_OF(obj);
573 if (klass == cRepeatedField) {
574 return RepeatedField_deep_copy(obj);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800575 } else if (klass == cMap) {
576 return Map_deep_copy(obj);
Chris Fallin973f4252014-11-18 14:19:58 -0800577 } else {
578 return Message_deep_copy(obj);
579 }
580}