message.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // 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. #include "protobuf.h"
  31. // -----------------------------------------------------------------------------
  32. // Class/module creation from msgdefs and enumdefs, respectively.
  33. // -----------------------------------------------------------------------------
  34. void* Message_data(void* msg) {
  35. return ((uint8_t *)msg) + sizeof(MessageHeader);
  36. }
  37. void Message_mark(void* _self) {
  38. MessageHeader* self = (MessageHeader *)_self;
  39. layout_mark(self->descriptor->layout, Message_data(self));
  40. }
  41. void Message_free(void* self) {
  42. xfree(self);
  43. }
  44. rb_data_type_t Message_type = {
  45. "Message",
  46. { Message_mark, Message_free, NULL },
  47. };
  48. VALUE Message_alloc(VALUE klass) {
  49. VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
  50. Descriptor* desc = ruby_to_Descriptor(descriptor);
  51. MessageHeader* msg = (MessageHeader*)ALLOC_N(
  52. uint8_t, sizeof(MessageHeader) + desc->layout->size);
  53. memset(Message_data(msg), 0, desc->layout->size);
  54. // We wrap first so that everything in the message object is GC-rooted in case
  55. // a collection happens during object creation in layout_init().
  56. VALUE ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
  57. msg->descriptor = desc;
  58. rb_iv_set(ret, kDescriptorInstanceVar, descriptor);
  59. layout_init(desc->layout, Message_data(msg));
  60. return ret;
  61. }
  62. static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
  63. // If no fields in the oneof, always nil.
  64. if (upb_oneofdef_numfields(o) == 0) {
  65. return Qnil;
  66. }
  67. // Grab the first field in the oneof so we can get its layout info to find the
  68. // oneof_case field.
  69. upb_oneof_iter it;
  70. upb_oneof_begin(&it, o);
  71. assert(!upb_oneof_done(&it));
  72. const upb_fielddef* first_field = upb_oneof_iter_field(&it);
  73. assert(upb_fielddef_containingoneof(first_field) != NULL);
  74. size_t case_ofs =
  75. self->descriptor->layout->
  76. fields[upb_fielddef_index(first_field)].case_offset;
  77. uint32_t oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
  78. if (oneof_case == ONEOF_CASE_NONE) {
  79. return Qnil;
  80. }
  81. // oneof_case is a field index, so find that field.
  82. const upb_fielddef* f = upb_oneofdef_itof(o, oneof_case);
  83. assert(f != NULL);
  84. return ID2SYM(rb_intern(upb_fielddef_name(f)));
  85. }
  86. /*
  87. * call-seq:
  88. * Message.method_missing(*args)
  89. *
  90. * Provides accessors and setters for message fields according to their field
  91. * names. For any field whose name does not conflict with a built-in method, an
  92. * accessor is provided with the same name as the field, and a setter is
  93. * provided with the name of the field plus the '=' suffix. Thus, given a
  94. * message instance 'msg' with field 'foo', the following code is valid:
  95. *
  96. * msg.foo = 42
  97. * puts msg.foo
  98. *
  99. * This method also provides read-only accessors for oneofs. If a oneof exists
  100. * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
  101. * the name of the field in that oneof that is currently set, or nil if none.
  102. */
  103. VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
  104. MessageHeader* self;
  105. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  106. if (argc < 1) {
  107. rb_raise(rb_eArgError, "Expected method name as first argument.");
  108. }
  109. VALUE method_name = argv[0];
  110. if (!SYMBOL_P(method_name)) {
  111. rb_raise(rb_eArgError, "Expected symbol as method name.");
  112. }
  113. VALUE method_str = rb_id2str(SYM2ID(method_name));
  114. char* name = RSTRING_PTR(method_str);
  115. size_t name_len = RSTRING_LEN(method_str);
  116. bool setter = false;
  117. // Setters have names that end in '='.
  118. if (name[name_len - 1] == '=') {
  119. setter = true;
  120. name_len--;
  121. }
  122. // Check for a oneof name first.
  123. const upb_oneofdef* o = upb_msgdef_ntoo(self->descriptor->msgdef,
  124. name, name_len);
  125. if (o != NULL) {
  126. if (setter) {
  127. rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
  128. }
  129. return which_oneof_field(self, o);
  130. }
  131. // Otherwise, check for a field with that name.
  132. const upb_fielddef* f = upb_msgdef_ntof(self->descriptor->msgdef,
  133. name, name_len);
  134. if (f == NULL) {
  135. rb_raise(rb_eArgError, "Unknown field");
  136. }
  137. if (setter) {
  138. if (argc < 2) {
  139. rb_raise(rb_eArgError, "No value provided to setter.");
  140. }
  141. layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
  142. return Qnil;
  143. } else {
  144. return layout_get(self->descriptor->layout, Message_data(self), f);
  145. }
  146. }
  147. int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
  148. MessageHeader* self;
  149. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  150. if (!SYMBOL_P(key)) {
  151. rb_raise(rb_eArgError,
  152. "Expected symbols as hash keys in initialization map.");
  153. }
  154. VALUE method_str = rb_id2str(SYM2ID(key));
  155. char* name = RSTRING_PTR(method_str);
  156. const upb_fielddef* f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
  157. if (f == NULL) {
  158. rb_raise(rb_eArgError,
  159. "Unknown field name in initialization map entry.");
  160. }
  161. if (is_map_field(f)) {
  162. if (TYPE(val) != T_HASH) {
  163. rb_raise(rb_eArgError,
  164. "Expected Hash object as initializer value for map field.");
  165. }
  166. VALUE map = layout_get(self->descriptor->layout, Message_data(self), f);
  167. Map_merge_into_self(map, val);
  168. } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
  169. if (TYPE(val) != T_ARRAY) {
  170. rb_raise(rb_eArgError,
  171. "Expected array as initializer value for repeated field.");
  172. }
  173. VALUE ary = layout_get(self->descriptor->layout, Message_data(self), f);
  174. for (int i = 0; i < RARRAY_LEN(val); i++) {
  175. RepeatedField_push(ary, rb_ary_entry(val, i));
  176. }
  177. } else {
  178. layout_set(self->descriptor->layout, Message_data(self), f, val);
  179. }
  180. return 0;
  181. }
  182. /*
  183. * call-seq:
  184. * Message.new(kwargs) => new_message
  185. *
  186. * Creates a new instance of the given message class. Keyword arguments may be
  187. * provided with keywords corresponding to field names.
  188. *
  189. * Note that no literal Message class exists. Only concrete classes per message
  190. * type exist, as provided by the #msgclass method on Descriptors after they
  191. * have been added to a pool. The method definitions described here on the
  192. * Message class are provided on each concrete message class.
  193. */
  194. VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
  195. if (argc == 0) {
  196. return Qnil;
  197. }
  198. if (argc != 1) {
  199. rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
  200. }
  201. VALUE hash_args = argv[0];
  202. if (TYPE(hash_args) != T_HASH) {
  203. rb_raise(rb_eArgError, "Expected hash arguments.");
  204. }
  205. rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
  206. return Qnil;
  207. }
  208. /*
  209. * call-seq:
  210. * Message.dup => new_message
  211. *
  212. * Performs a shallow copy of this message and returns the new copy.
  213. */
  214. VALUE Message_dup(VALUE _self) {
  215. MessageHeader* self;
  216. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  217. VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
  218. MessageHeader* new_msg_self;
  219. TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
  220. layout_dup(self->descriptor->layout,
  221. Message_data(new_msg_self),
  222. Message_data(self));
  223. return new_msg;
  224. }
  225. // Internal only; used by Google::Protobuf.deep_copy.
  226. VALUE Message_deep_copy(VALUE _self) {
  227. MessageHeader* self;
  228. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  229. VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
  230. MessageHeader* new_msg_self;
  231. TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
  232. layout_deep_copy(self->descriptor->layout,
  233. Message_data(new_msg_self),
  234. Message_data(self));
  235. return new_msg;
  236. }
  237. /*
  238. * call-seq:
  239. * Message.==(other) => boolean
  240. *
  241. * Performs a deep comparison of this message with another. Messages are equal
  242. * if they have the same type and if each field is equal according to the :==
  243. * method's semantics (a more efficient comparison may actually be done if the
  244. * field is of a primitive type).
  245. */
  246. VALUE Message_eq(VALUE _self, VALUE _other) {
  247. MessageHeader* self;
  248. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  249. MessageHeader* other;
  250. TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
  251. if (self->descriptor != other->descriptor) {
  252. return Qfalse;
  253. }
  254. return layout_eq(self->descriptor->layout,
  255. Message_data(self),
  256. Message_data(other));
  257. }
  258. /*
  259. * call-seq:
  260. * Message.hash => hash_value
  261. *
  262. * Returns a hash value that represents this message's field values.
  263. */
  264. VALUE Message_hash(VALUE _self) {
  265. MessageHeader* self;
  266. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  267. return layout_hash(self->descriptor->layout, Message_data(self));
  268. }
  269. /*
  270. * call-seq:
  271. * Message.inspect => string
  272. *
  273. * Returns a human-readable string representing this message. It will be
  274. * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
  275. * field's value is represented according to its own #inspect method.
  276. */
  277. VALUE Message_inspect(VALUE _self) {
  278. MessageHeader* self;
  279. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  280. VALUE str = rb_str_new2("<");
  281. str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
  282. str = rb_str_cat2(str, ": ");
  283. str = rb_str_append(str, layout_inspect(
  284. self->descriptor->layout, Message_data(self)));
  285. str = rb_str_cat2(str, ">");
  286. return str;
  287. }
  288. VALUE Message_to_h(VALUE _self) {
  289. MessageHeader* self;
  290. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  291. VALUE hash = rb_hash_new();
  292. upb_msg_field_iter it;
  293. for (upb_msg_field_begin(&it, self->descriptor->msgdef);
  294. !upb_msg_field_done(&it);
  295. upb_msg_field_next(&it)) {
  296. const upb_fielddef* field = upb_msg_iter_field(&it);
  297. VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self), field);
  298. VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
  299. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  300. msg_value = RepeatedField_to_ary(msg_value);
  301. }
  302. rb_hash_aset(hash, msg_key, msg_value);
  303. }
  304. return hash;
  305. }
  306. /*
  307. * call-seq:
  308. * Message.[](index) => value
  309. *
  310. * Accesses a field's value by field name. The provided field name should be a
  311. * string.
  312. */
  313. VALUE Message_index(VALUE _self, VALUE field_name) {
  314. MessageHeader* self;
  315. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  316. Check_Type(field_name, T_STRING);
  317. const upb_fielddef* field =
  318. upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
  319. if (field == NULL) {
  320. return Qnil;
  321. }
  322. return layout_get(self->descriptor->layout, Message_data(self), field);
  323. }
  324. /*
  325. * call-seq:
  326. * Message.[]=(index, value)
  327. *
  328. * Sets a field's value by field name. The provided field name should be a
  329. * string.
  330. */
  331. VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
  332. MessageHeader* self;
  333. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  334. Check_Type(field_name, T_STRING);
  335. const upb_fielddef* field =
  336. upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
  337. if (field == NULL) {
  338. rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
  339. }
  340. layout_set(self->descriptor->layout, Message_data(self), field, value);
  341. return Qnil;
  342. }
  343. /*
  344. * call-seq:
  345. * Message.descriptor => descriptor
  346. *
  347. * Class method that returns the Descriptor instance corresponding to this
  348. * message class's type.
  349. */
  350. VALUE Message_descriptor(VALUE klass) {
  351. return rb_iv_get(klass, kDescriptorInstanceVar);
  352. }
  353. VALUE build_class_from_descriptor(Descriptor* desc) {
  354. if (desc->layout == NULL) {
  355. desc->layout = create_layout(desc->msgdef);
  356. }
  357. if (desc->fill_method == NULL) {
  358. desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
  359. }
  360. const char* name = upb_msgdef_fullname(desc->msgdef);
  361. if (name == NULL) {
  362. rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
  363. }
  364. VALUE klass = rb_define_class_id(
  365. // Docs say this parameter is ignored. User will assign return value to
  366. // their own toplevel constant class name.
  367. rb_intern("Message"),
  368. rb_cObject);
  369. rb_iv_set(klass, kDescriptorInstanceVar, get_def_obj(desc->msgdef));
  370. rb_define_alloc_func(klass, Message_alloc);
  371. rb_require("google/protobuf/message_exts");
  372. rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts"));
  373. rb_extend_object(klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods"));
  374. rb_define_method(klass, "method_missing",
  375. Message_method_missing, -1);
  376. rb_define_method(klass, "initialize", Message_initialize, -1);
  377. rb_define_method(klass, "dup", Message_dup, 0);
  378. // Also define #clone so that we don't inherit Object#clone.
  379. rb_define_method(klass, "clone", Message_dup, 0);
  380. rb_define_method(klass, "==", Message_eq, 1);
  381. rb_define_method(klass, "hash", Message_hash, 0);
  382. rb_define_method(klass, "to_h", Message_to_h, 0);
  383. rb_define_method(klass, "to_hash", Message_to_h, 0);
  384. rb_define_method(klass, "inspect", Message_inspect, 0);
  385. rb_define_method(klass, "[]", Message_index, 1);
  386. rb_define_method(klass, "[]=", Message_index_set, 2);
  387. rb_define_singleton_method(klass, "decode", Message_decode, 1);
  388. rb_define_singleton_method(klass, "encode", Message_encode, 1);
  389. rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1);
  390. rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1);
  391. rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
  392. return klass;
  393. }
  394. /*
  395. * call-seq:
  396. * Enum.lookup(number) => name
  397. *
  398. * This module method, provided on each generated enum module, looks up an enum
  399. * value by number and returns its name as a Ruby symbol, or nil if not found.
  400. */
  401. VALUE enum_lookup(VALUE self, VALUE number) {
  402. int32_t num = NUM2INT(number);
  403. VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
  404. EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
  405. const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
  406. if (name == NULL) {
  407. return Qnil;
  408. } else {
  409. return ID2SYM(rb_intern(name));
  410. }
  411. }
  412. /*
  413. * call-seq:
  414. * Enum.resolve(name) => number
  415. *
  416. * This module method, provided on each generated enum module, looks up an enum
  417. * value by name (as a Ruby symbol) and returns its name, or nil if not found.
  418. */
  419. VALUE enum_resolve(VALUE self, VALUE sym) {
  420. const char* name = rb_id2name(SYM2ID(sym));
  421. VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
  422. EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
  423. int32_t num = 0;
  424. bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
  425. if (!found) {
  426. return Qnil;
  427. } else {
  428. return INT2NUM(num);
  429. }
  430. }
  431. /*
  432. * call-seq:
  433. * Enum.descriptor
  434. *
  435. * This module method, provided on each generated enum module, returns the
  436. * EnumDescriptor corresponding to this enum type.
  437. */
  438. VALUE enum_descriptor(VALUE self) {
  439. return rb_iv_get(self, kDescriptorInstanceVar);
  440. }
  441. VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
  442. VALUE mod = rb_define_module_id(
  443. rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
  444. upb_enum_iter it;
  445. for (upb_enum_begin(&it, enumdesc->enumdef);
  446. !upb_enum_done(&it);
  447. upb_enum_next(&it)) {
  448. const char* name = upb_enum_iter_name(&it);
  449. int32_t value = upb_enum_iter_number(&it);
  450. if (name[0] < 'A' || name[0] > 'Z') {
  451. rb_raise(rb_eTypeError,
  452. "Enum value '%s' does not start with an uppercase letter "
  453. "as is required for Ruby constants.",
  454. name);
  455. }
  456. rb_define_const(mod, name, INT2NUM(value));
  457. }
  458. rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
  459. rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
  460. rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
  461. rb_iv_set(mod, kDescriptorInstanceVar, get_def_obj(enumdesc->enumdef));
  462. return mod;
  463. }
  464. /*
  465. * call-seq:
  466. * Google::Protobuf.deep_copy(obj) => copy_of_obj
  467. *
  468. * Performs a deep copy of a RepeatedField instance, a Map instance, or a
  469. * message object, recursively copying its members.
  470. */
  471. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
  472. VALUE klass = CLASS_OF(obj);
  473. if (klass == cRepeatedField) {
  474. return RepeatedField_deep_copy(obj);
  475. } else if (klass == cMap) {
  476. return Map_deep_copy(obj);
  477. } else {
  478. return Message_deep_copy(obj);
  479. }
  480. }