message.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /*
  63. * call-seq:
  64. * Message.method_missing(*args)
  65. *
  66. * Provides accessors and setters for message fields according to their field
  67. * names. For any field whose name does not conflict with a built-in method, an
  68. * accessor is provided with the same name as the field, and a setter is
  69. * provided with the name of the field plus the '=' suffix. Thus, given a
  70. * message instance 'msg' with field 'foo', the following code is valid:
  71. *
  72. * msg.foo = 42
  73. * puts msg.foo
  74. */
  75. VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
  76. MessageHeader* self;
  77. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  78. if (argc < 1) {
  79. rb_raise(rb_eArgError, "Expected method name as first argument.");
  80. }
  81. VALUE method_name = argv[0];
  82. if (!SYMBOL_P(method_name)) {
  83. rb_raise(rb_eArgError, "Expected symbol as method name.");
  84. }
  85. VALUE method_str = rb_id2str(SYM2ID(method_name));
  86. char* name = RSTRING_PTR(method_str);
  87. size_t name_len = RSTRING_LEN(method_str);
  88. bool setter = false;
  89. // Setters have names that end in '='.
  90. if (name[name_len - 1] == '=') {
  91. setter = true;
  92. name_len--;
  93. }
  94. const upb_fielddef* f = upb_msgdef_ntof(self->descriptor->msgdef,
  95. name, name_len);
  96. if (f == NULL) {
  97. rb_raise(rb_eArgError, "Unknown field");
  98. }
  99. if (setter) {
  100. if (argc < 2) {
  101. rb_raise(rb_eArgError, "No value provided to setter.");
  102. }
  103. layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
  104. return Qnil;
  105. } else {
  106. return layout_get(self->descriptor->layout, Message_data(self), f);
  107. }
  108. }
  109. int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
  110. MessageHeader* self;
  111. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  112. if (!SYMBOL_P(key)) {
  113. rb_raise(rb_eArgError,
  114. "Expected symbols as hash keys in initialization map.");
  115. }
  116. VALUE method_str = rb_id2str(SYM2ID(key));
  117. char* name = RSTRING_PTR(method_str);
  118. const upb_fielddef* f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
  119. if (f == NULL) {
  120. rb_raise(rb_eArgError,
  121. "Unknown field name in initialization map entry.");
  122. }
  123. if (is_map_field(f)) {
  124. if (TYPE(val) != T_HASH) {
  125. rb_raise(rb_eArgError,
  126. "Expected Hash object as initializer value for map field.");
  127. }
  128. VALUE map = layout_get(self->descriptor->layout, Message_data(self), f);
  129. Map_merge_into_self(map, val);
  130. } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
  131. if (TYPE(val) != T_ARRAY) {
  132. rb_raise(rb_eArgError,
  133. "Expected array as initializer value for repeated field.");
  134. }
  135. VALUE ary = layout_get(self->descriptor->layout, Message_data(self), f);
  136. for (int i = 0; i < RARRAY_LEN(val); i++) {
  137. RepeatedField_push(ary, rb_ary_entry(val, i));
  138. }
  139. } else {
  140. layout_set(self->descriptor->layout, Message_data(self), f, val);
  141. }
  142. return 0;
  143. }
  144. /*
  145. * call-seq:
  146. * Message.new(kwargs) => new_message
  147. *
  148. * Creates a new instance of the given message class. Keyword arguments may be
  149. * provided with keywords corresponding to field names.
  150. *
  151. * Note that no literal Message class exists. Only concrete classes per message
  152. * type exist, as provided by the #msgclass method on Descriptors after they
  153. * have been added to a pool. The method definitions described here on the
  154. * Message class are provided on each concrete message class.
  155. */
  156. VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
  157. if (argc == 0) {
  158. return Qnil;
  159. }
  160. if (argc != 1) {
  161. rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
  162. }
  163. VALUE hash_args = argv[0];
  164. if (TYPE(hash_args) != T_HASH) {
  165. rb_raise(rb_eArgError, "Expected hash arguments.");
  166. }
  167. rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
  168. return Qnil;
  169. }
  170. /*
  171. * call-seq:
  172. * Message.dup => new_message
  173. *
  174. * Performs a shallow copy of this message and returns the new copy.
  175. */
  176. VALUE Message_dup(VALUE _self) {
  177. MessageHeader* self;
  178. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  179. VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
  180. MessageHeader* new_msg_self;
  181. TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
  182. layout_dup(self->descriptor->layout,
  183. Message_data(new_msg_self),
  184. Message_data(self));
  185. return new_msg;
  186. }
  187. // Internal only; used by Google::Protobuf.deep_copy.
  188. VALUE Message_deep_copy(VALUE _self) {
  189. MessageHeader* self;
  190. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  191. VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
  192. MessageHeader* new_msg_self;
  193. TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
  194. layout_deep_copy(self->descriptor->layout,
  195. Message_data(new_msg_self),
  196. Message_data(self));
  197. return new_msg;
  198. }
  199. /*
  200. * call-seq:
  201. * Message.==(other) => boolean
  202. *
  203. * Performs a deep comparison of this message with another. Messages are equal
  204. * if they have the same type and if each field is equal according to the :==
  205. * method's semantics (a more efficient comparison may actually be done if the
  206. * field is of a primitive type).
  207. */
  208. VALUE Message_eq(VALUE _self, VALUE _other) {
  209. MessageHeader* self;
  210. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  211. MessageHeader* other;
  212. TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
  213. if (self->descriptor != other->descriptor) {
  214. return Qfalse;
  215. }
  216. return layout_eq(self->descriptor->layout,
  217. Message_data(self),
  218. Message_data(other));
  219. }
  220. /*
  221. * call-seq:
  222. * Message.hash => hash_value
  223. *
  224. * Returns a hash value that represents this message's field values.
  225. */
  226. VALUE Message_hash(VALUE _self) {
  227. MessageHeader* self;
  228. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  229. return layout_hash(self->descriptor->layout, Message_data(self));
  230. }
  231. /*
  232. * call-seq:
  233. * Message.inspect => string
  234. *
  235. * Returns a human-readable string representing this message. It will be
  236. * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
  237. * field's value is represented according to its own #inspect method.
  238. */
  239. VALUE Message_inspect(VALUE _self) {
  240. MessageHeader* self;
  241. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  242. VALUE str = rb_str_new2("<");
  243. str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
  244. str = rb_str_cat2(str, ": ");
  245. str = rb_str_append(str, layout_inspect(
  246. self->descriptor->layout, Message_data(self)));
  247. str = rb_str_cat2(str, ">");
  248. return str;
  249. }
  250. /*
  251. * call-seq:
  252. * Message.[](index) => value
  253. *
  254. * Accesses a field's value by field name. The provided field name should be a
  255. * string.
  256. */
  257. VALUE Message_index(VALUE _self, VALUE field_name) {
  258. MessageHeader* self;
  259. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  260. Check_Type(field_name, T_STRING);
  261. const upb_fielddef* field =
  262. upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
  263. if (field == NULL) {
  264. return Qnil;
  265. }
  266. return layout_get(self->descriptor->layout, Message_data(self), field);
  267. }
  268. /*
  269. * call-seq:
  270. * Message.[]=(index, value)
  271. *
  272. * Sets a field's value by field name. The provided field name should be a
  273. * string.
  274. */
  275. VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
  276. MessageHeader* self;
  277. TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
  278. Check_Type(field_name, T_STRING);
  279. const upb_fielddef* field =
  280. upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
  281. if (field == NULL) {
  282. rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
  283. }
  284. layout_set(self->descriptor->layout, Message_data(self), field, value);
  285. return Qnil;
  286. }
  287. /*
  288. * call-seq:
  289. * Message.descriptor => descriptor
  290. *
  291. * Class method that returns the Descriptor instance corresponding to this
  292. * message class's type.
  293. */
  294. VALUE Message_descriptor(VALUE klass) {
  295. return rb_iv_get(klass, kDescriptorInstanceVar);
  296. }
  297. VALUE build_class_from_descriptor(Descriptor* desc) {
  298. if (desc->layout == NULL) {
  299. desc->layout = create_layout(desc->msgdef);
  300. }
  301. if (desc->fill_method == NULL) {
  302. desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
  303. }
  304. const char* name = upb_msgdef_fullname(desc->msgdef);
  305. if (name == NULL) {
  306. rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
  307. }
  308. VALUE klass = rb_define_class_id(
  309. // Docs say this parameter is ignored. User will assign return value to
  310. // their own toplevel constant class name.
  311. rb_intern("Message"),
  312. rb_cObject);
  313. rb_iv_set(klass, kDescriptorInstanceVar, get_def_obj(desc->msgdef));
  314. rb_define_alloc_func(klass, Message_alloc);
  315. rb_define_method(klass, "method_missing",
  316. Message_method_missing, -1);
  317. rb_define_method(klass, "initialize", Message_initialize, -1);
  318. rb_define_method(klass, "dup", Message_dup, 0);
  319. // Also define #clone so that we don't inherit Object#clone.
  320. rb_define_method(klass, "clone", Message_dup, 0);
  321. rb_define_method(klass, "==", Message_eq, 1);
  322. rb_define_method(klass, "hash", Message_hash, 0);
  323. rb_define_method(klass, "inspect", Message_inspect, 0);
  324. rb_define_method(klass, "[]", Message_index, 1);
  325. rb_define_method(klass, "[]=", Message_index_set, 2);
  326. rb_define_singleton_method(klass, "decode", Message_decode, 1);
  327. rb_define_singleton_method(klass, "encode", Message_encode, 1);
  328. rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1);
  329. rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1);
  330. rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
  331. return klass;
  332. }
  333. /*
  334. * call-seq:
  335. * Enum.lookup(number) => name
  336. *
  337. * This module method, provided on each generated enum module, looks up an enum
  338. * value by number and returns its name as a Ruby symbol, or nil if not found.
  339. */
  340. VALUE enum_lookup(VALUE self, VALUE number) {
  341. int32_t num = NUM2INT(number);
  342. VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
  343. EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
  344. const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
  345. if (name == NULL) {
  346. return Qnil;
  347. } else {
  348. return ID2SYM(rb_intern(name));
  349. }
  350. }
  351. /*
  352. * call-seq:
  353. * Enum.resolve(name) => number
  354. *
  355. * This module method, provided on each generated enum module, looks up an enum
  356. * value by name (as a Ruby symbol) and returns its name, or nil if not found.
  357. */
  358. VALUE enum_resolve(VALUE self, VALUE sym) {
  359. const char* name = rb_id2name(SYM2ID(sym));
  360. VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
  361. EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
  362. int32_t num = 0;
  363. bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
  364. if (!found) {
  365. return Qnil;
  366. } else {
  367. return INT2NUM(num);
  368. }
  369. }
  370. /*
  371. * call-seq:
  372. * Enum.descriptor
  373. *
  374. * This module method, provided on each generated enum module, returns the
  375. * EnumDescriptor corresponding to this enum type.
  376. */
  377. VALUE enum_descriptor(VALUE self) {
  378. return rb_iv_get(self, kDescriptorInstanceVar);
  379. }
  380. VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
  381. VALUE mod = rb_define_module_id(
  382. rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
  383. upb_enum_iter it;
  384. for (upb_enum_begin(&it, enumdesc->enumdef);
  385. !upb_enum_done(&it);
  386. upb_enum_next(&it)) {
  387. const char* name = upb_enum_iter_name(&it);
  388. int32_t value = upb_enum_iter_number(&it);
  389. if (name[0] < 'A' || name[0] > 'Z') {
  390. rb_raise(rb_eTypeError,
  391. "Enum value '%s' does not start with an uppercase letter "
  392. "as is required for Ruby constants.",
  393. name);
  394. }
  395. rb_define_const(mod, name, INT2NUM(value));
  396. }
  397. rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
  398. rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
  399. rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
  400. rb_iv_set(mod, kDescriptorInstanceVar, get_def_obj(enumdesc->enumdef));
  401. return mod;
  402. }
  403. /*
  404. * call-seq:
  405. * Google::Protobuf.deep_copy(obj) => copy_of_obj
  406. *
  407. * Performs a deep copy of a RepeatedField instance, a Map instance, or a
  408. * message object, recursively copying its members.
  409. */
  410. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
  411. VALUE klass = CLASS_OF(obj);
  412. if (klass == cRepeatedField) {
  413. return RepeatedField_deep_copy(obj);
  414. } else if (klass == cMap) {
  415. return Map_deep_copy(obj);
  416. } else {
  417. return Message_deep_copy(obj);
  418. }
  419. }