protobuf.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
  31. #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
  32. #include <ruby/ruby.h>
  33. #include <ruby/vm.h>
  34. #include <ruby/encoding.h>
  35. #include "upb.h"
  36. // Forward decls.
  37. struct DescriptorPool;
  38. struct Descriptor;
  39. struct FieldDescriptor;
  40. struct EnumDescriptor;
  41. struct MessageLayout;
  42. struct MessageHeader;
  43. struct MessageBuilderContext;
  44. struct EnumBuilderContext;
  45. struct Builder;
  46. typedef struct DescriptorPool DescriptorPool;
  47. typedef struct Descriptor Descriptor;
  48. typedef struct FieldDescriptor FieldDescriptor;
  49. typedef struct EnumDescriptor EnumDescriptor;
  50. typedef struct MessageLayout MessageLayout;
  51. typedef struct MessageHeader MessageHeader;
  52. typedef struct MessageBuilderContext MessageBuilderContext;
  53. typedef struct EnumBuilderContext EnumBuilderContext;
  54. typedef struct Builder Builder;
  55. /*
  56. It can be a bit confusing how the C structs defined below and the Ruby
  57. objects interact and hold references to each other. First, a few principles:
  58. - Ruby's "TypedData" abstraction lets a Ruby VALUE hold a pointer to a C
  59. struct (or arbitrary memory chunk), own it, and free it when collected.
  60. Thus, each struct below will have a corresponding Ruby object
  61. wrapping/owning it.
  62. - To get back from an underlying upb {msg,enum}def to the Ruby object, we
  63. keep a global hashmap, accessed by get_def_obj/add_def_obj below.
  64. The in-memory structure is then something like:
  65. Ruby | upb
  66. |
  67. DescriptorPool ------------|-----------> upb_symtab____________________
  68. | | (message types) \
  69. | v \
  70. Descriptor ---------------|-----------> upb_msgdef (enum types)|
  71. |--> msgclass | | ^ |
  72. | (dynamically built) | | | (submsg fields) |
  73. |--> MessageLayout | | | /
  74. |--------------------------|> decoder method| | /
  75. \--------------------------|> serialize | | /
  76. | handlers v | /
  77. FieldDescriptor -----------|-----------> upb_fielddef /
  78. | | /
  79. | v (enum fields) /
  80. EnumDescriptor ------------|-----------> upb_enumdef <----------'
  81. |
  82. |
  83. ^ | \___/
  84. `---------------|-----------------' (get_def_obj map)
  85. */
  86. // -----------------------------------------------------------------------------
  87. // Ruby class structure definitions.
  88. // -----------------------------------------------------------------------------
  89. struct DescriptorPool {
  90. upb_symtab* symtab;
  91. };
  92. struct Descriptor {
  93. const upb_msgdef* msgdef;
  94. MessageLayout* layout;
  95. VALUE klass; // begins as nil
  96. const upb_handlers* fill_handlers;
  97. const upb_pbdecodermethod* fill_method;
  98. const upb_handlers* pb_serialize_handlers;
  99. const upb_handlers* json_serialize_handlers;
  100. // Handlers hold type class references for sub-message fields directly in some
  101. // cases. We need to keep these rooted because they might otherwise be
  102. // collected.
  103. VALUE typeclass_references;
  104. };
  105. struct FieldDescriptor {
  106. const upb_fielddef* fielddef;
  107. };
  108. struct EnumDescriptor {
  109. const upb_enumdef* enumdef;
  110. VALUE module; // begins as nil
  111. };
  112. struct MessageBuilderContext {
  113. VALUE descriptor;
  114. VALUE builder;
  115. };
  116. struct EnumBuilderContext {
  117. VALUE enumdesc;
  118. };
  119. struct Builder {
  120. VALUE pending_list;
  121. upb_def** defs; // used only while finalizing
  122. };
  123. extern VALUE cDescriptorPool;
  124. extern VALUE cDescriptor;
  125. extern VALUE cFieldDescriptor;
  126. extern VALUE cEnumDescriptor;
  127. extern VALUE cMessageBuilderContext;
  128. extern VALUE cEnumBuilderContext;
  129. extern VALUE cBuilder;
  130. extern const char* kDescriptorInstanceVar;
  131. // We forward-declare all of the Ruby method implementations here because we
  132. // sometimes call the methods directly across .c files, rather than going
  133. // through Ruby's method dispatching (e.g. during message parse). It's cleaner
  134. // to keep the list of object methods together than to split them between
  135. // static-in-file definitions and header declarations.
  136. void DescriptorPool_mark(void* _self);
  137. void DescriptorPool_free(void* _self);
  138. VALUE DescriptorPool_alloc(VALUE klass);
  139. void DescriptorPool_register(VALUE module);
  140. DescriptorPool* ruby_to_DescriptorPool(VALUE value);
  141. VALUE DescriptorPool_add(VALUE _self, VALUE def);
  142. VALUE DescriptorPool_build(VALUE _self);
  143. VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
  144. VALUE DescriptorPool_generated_pool(VALUE _self);
  145. void Descriptor_mark(void* _self);
  146. void Descriptor_free(void* _self);
  147. VALUE Descriptor_alloc(VALUE klass);
  148. void Descriptor_register(VALUE module);
  149. Descriptor* ruby_to_Descriptor(VALUE value);
  150. VALUE Descriptor_name(VALUE _self);
  151. VALUE Descriptor_name_set(VALUE _self, VALUE str);
  152. VALUE Descriptor_each(VALUE _self);
  153. VALUE Descriptor_lookup(VALUE _self, VALUE name);
  154. VALUE Descriptor_add_field(VALUE _self, VALUE obj);
  155. VALUE Descriptor_msgclass(VALUE _self);
  156. extern const rb_data_type_t _Descriptor_type;
  157. void FieldDescriptor_mark(void* _self);
  158. void FieldDescriptor_free(void* _self);
  159. VALUE FieldDescriptor_alloc(VALUE klass);
  160. void FieldDescriptor_register(VALUE module);
  161. FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
  162. VALUE FieldDescriptor_name(VALUE _self);
  163. VALUE FieldDescriptor_name_set(VALUE _self, VALUE str);
  164. VALUE FieldDescriptor_type(VALUE _self);
  165. VALUE FieldDescriptor_type_set(VALUE _self, VALUE type);
  166. VALUE FieldDescriptor_label(VALUE _self);
  167. VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
  168. VALUE FieldDescriptor_number(VALUE _self);
  169. VALUE FieldDescriptor_number_set(VALUE _self, VALUE number);
  170. VALUE FieldDescriptor_submsg_name(VALUE _self);
  171. VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value);
  172. VALUE FieldDescriptor_subtype(VALUE _self);
  173. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb);
  174. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
  175. upb_fieldtype_t ruby_to_fieldtype(VALUE type);
  176. VALUE fieldtype_to_ruby(upb_fieldtype_t type);
  177. void EnumDescriptor_mark(void* _self);
  178. void EnumDescriptor_free(void* _self);
  179. VALUE EnumDescriptor_alloc(VALUE klass);
  180. void EnumDescriptor_register(VALUE module);
  181. EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
  182. VALUE EnumDescriptor_name(VALUE _self);
  183. VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
  184. VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
  185. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
  186. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
  187. VALUE EnumDescriptor_each(VALUE _self);
  188. VALUE EnumDescriptor_enummodule(VALUE _self);
  189. extern const rb_data_type_t _EnumDescriptor_type;
  190. void MessageBuilderContext_mark(void* _self);
  191. void MessageBuilderContext_free(void* _self);
  192. VALUE MessageBuilderContext_alloc(VALUE klass);
  193. void MessageBuilderContext_register(VALUE module);
  194. MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
  195. VALUE MessageBuilderContext_initialize(VALUE _self,
  196. VALUE descriptor,
  197. VALUE builder);
  198. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
  199. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
  200. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
  201. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
  202. void EnumBuilderContext_mark(void* _self);
  203. void EnumBuilderContext_free(void* _self);
  204. VALUE EnumBuilderContext_alloc(VALUE klass);
  205. void EnumBuilderContext_register(VALUE module);
  206. EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
  207. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
  208. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
  209. void Builder_mark(void* _self);
  210. void Builder_free(void* _self);
  211. VALUE Builder_alloc(VALUE klass);
  212. void Builder_register(VALUE module);
  213. Builder* ruby_to_Builder(VALUE value);
  214. VALUE Builder_add_message(VALUE _self, VALUE name);
  215. VALUE Builder_add_enum(VALUE _self, VALUE name);
  216. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
  217. // -----------------------------------------------------------------------------
  218. // Native slot storage abstraction.
  219. // -----------------------------------------------------------------------------
  220. #define NATIVE_SLOT_MAX_SIZE sizeof(void*)
  221. size_t native_slot_size(upb_fieldtype_t type);
  222. void native_slot_set(upb_fieldtype_t type,
  223. VALUE type_class,
  224. void* memory,
  225. VALUE value);
  226. VALUE native_slot_get(upb_fieldtype_t type,
  227. VALUE type_class,
  228. const void* memory);
  229. void native_slot_init(upb_fieldtype_t type, void* memory);
  230. void native_slot_mark(upb_fieldtype_t type, void* memory);
  231. void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
  232. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from);
  233. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2);
  234. void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value);
  235. void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE value);
  236. extern rb_encoding* kRubyStringUtf8Encoding;
  237. extern rb_encoding* kRubyStringASCIIEncoding;
  238. extern rb_encoding* kRubyString8bitEncoding;
  239. VALUE field_type_class(const upb_fielddef* field);
  240. #define MAP_KEY_FIELD 1
  241. #define MAP_VALUE_FIELD 2
  242. // These operate on a map field (i.e., a repeated field of submessages whose
  243. // submessage type is a map-entry msgdef).
  244. bool is_map_field(const upb_fielddef* field);
  245. const upb_fielddef* map_field_key(const upb_fielddef* field);
  246. const upb_fielddef* map_field_value(const upb_fielddef* field);
  247. // These operate on a map-entry msgdef.
  248. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  249. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  250. // -----------------------------------------------------------------------------
  251. // Repeated field container type.
  252. // -----------------------------------------------------------------------------
  253. typedef struct {
  254. upb_fieldtype_t field_type;
  255. VALUE field_type_class;
  256. void* elements;
  257. int size;
  258. int capacity;
  259. } RepeatedField;
  260. void RepeatedField_mark(void* self);
  261. void RepeatedField_free(void* self);
  262. VALUE RepeatedField_alloc(VALUE klass);
  263. VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
  264. void RepeatedField_register(VALUE module);
  265. extern const rb_data_type_t RepeatedField_type;
  266. extern VALUE cRepeatedField;
  267. RepeatedField* ruby_to_RepeatedField(VALUE value);
  268. VALUE RepeatedField_each(VALUE _self);
  269. VALUE RepeatedField_index(VALUE _self, VALUE _index);
  270. void* RepeatedField_index_native(VALUE _self, int index);
  271. VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
  272. void RepeatedField_reserve(RepeatedField* self, int new_size);
  273. VALUE RepeatedField_push(VALUE _self, VALUE val);
  274. void RepeatedField_push_native(VALUE _self, void* data);
  275. VALUE RepeatedField_pop(VALUE _self);
  276. VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
  277. VALUE RepeatedField_replace(VALUE _self, VALUE list);
  278. VALUE RepeatedField_clear(VALUE _self);
  279. VALUE RepeatedField_length(VALUE _self);
  280. VALUE RepeatedField_dup(VALUE _self);
  281. VALUE RepeatedField_deep_copy(VALUE _self);
  282. VALUE RepeatedField_eq(VALUE _self, VALUE _other);
  283. VALUE RepeatedField_hash(VALUE _self);
  284. VALUE RepeatedField_inspect(VALUE _self);
  285. VALUE RepeatedField_plus(VALUE _self, VALUE list);
  286. // Defined in repeated_field.c; also used by Map.
  287. void validate_type_class(upb_fieldtype_t type, VALUE klass);
  288. // -----------------------------------------------------------------------------
  289. // Map container type.
  290. // -----------------------------------------------------------------------------
  291. typedef struct {
  292. upb_fieldtype_t key_type;
  293. upb_fieldtype_t value_type;
  294. VALUE value_type_class;
  295. upb_strtable table;
  296. } Map;
  297. void Map_mark(void* self);
  298. void Map_free(void* self);
  299. VALUE Map_alloc(VALUE klass);
  300. VALUE Map_init(int argc, VALUE* argv, VALUE self);
  301. void Map_register(VALUE module);
  302. extern const rb_data_type_t Map_type;
  303. extern VALUE cMap;
  304. Map* ruby_to_Map(VALUE value);
  305. VALUE Map_each(VALUE _self);
  306. VALUE Map_keys(VALUE _self);
  307. VALUE Map_values(VALUE _self);
  308. VALUE Map_index(VALUE _self, VALUE key);
  309. VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
  310. VALUE Map_has_key(VALUE _self, VALUE key);
  311. VALUE Map_delete(VALUE _self, VALUE key);
  312. VALUE Map_clear(VALUE _self);
  313. VALUE Map_length(VALUE _self);
  314. VALUE Map_dup(VALUE _self);
  315. VALUE Map_deep_copy(VALUE _self);
  316. VALUE Map_eq(VALUE _self, VALUE _other);
  317. VALUE Map_hash(VALUE _self);
  318. VALUE Map_inspect(VALUE _self);
  319. VALUE Map_merge(VALUE _self, VALUE hashmap);
  320. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
  321. typedef struct {
  322. Map* self;
  323. upb_strtable_iter it;
  324. } Map_iter;
  325. void Map_begin(VALUE _self, Map_iter* iter);
  326. void Map_next(Map_iter* iter);
  327. bool Map_done(Map_iter* iter);
  328. VALUE Map_iter_key(Map_iter* iter);
  329. VALUE Map_iter_value(Map_iter* iter);
  330. // -----------------------------------------------------------------------------
  331. // Message layout / storage.
  332. // -----------------------------------------------------------------------------
  333. struct MessageLayout {
  334. const upb_msgdef* msgdef;
  335. size_t* offsets;
  336. size_t size;
  337. };
  338. MessageLayout* create_layout(const upb_msgdef* msgdef);
  339. void free_layout(MessageLayout* layout);
  340. VALUE layout_get(MessageLayout* layout,
  341. const void* storage,
  342. const upb_fielddef* field);
  343. void layout_set(MessageLayout* layout,
  344. void* storage,
  345. const upb_fielddef* field,
  346. VALUE val);
  347. void layout_init(MessageLayout* layout, void* storage);
  348. void layout_mark(MessageLayout* layout, void* storage);
  349. void layout_dup(MessageLayout* layout, void* to, void* from);
  350. void layout_deep_copy(MessageLayout* layout, void* to, void* from);
  351. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
  352. VALUE layout_hash(MessageLayout* layout, void* storage);
  353. VALUE layout_inspect(MessageLayout* layout, void* storage);
  354. // -----------------------------------------------------------------------------
  355. // Message class creation.
  356. // -----------------------------------------------------------------------------
  357. struct MessageHeader {
  358. Descriptor* descriptor; // kept alive by self.class.descriptor reference.
  359. // Data comes after this.
  360. };
  361. extern rb_data_type_t Message_type;
  362. VALUE build_class_from_descriptor(Descriptor* descriptor);
  363. void* Message_data(void* msg);
  364. void Message_mark(void* self);
  365. void Message_free(void* self);
  366. VALUE Message_alloc(VALUE klass);
  367. VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
  368. VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
  369. VALUE Message_dup(VALUE _self);
  370. VALUE Message_deep_copy(VALUE _self);
  371. VALUE Message_eq(VALUE _self, VALUE _other);
  372. VALUE Message_hash(VALUE _self);
  373. VALUE Message_inspect(VALUE _self);
  374. VALUE Message_index(VALUE _self, VALUE field_name);
  375. VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
  376. VALUE Message_descriptor(VALUE klass);
  377. VALUE Message_decode(VALUE klass, VALUE data);
  378. VALUE Message_encode(VALUE klass, VALUE msg_rb);
  379. VALUE Message_decode_json(VALUE klass, VALUE data);
  380. VALUE Message_encode_json(VALUE klass, VALUE msg_rb);
  381. VALUE Google_Protobuf_encode(VALUE self, VALUE msg_rb);
  382. VALUE Google_Protobuf_decode(VALUE self, VALUE klass, VALUE msg_rb);
  383. VALUE Google_Protobuf_encode_json(VALUE self, VALUE msg_rb);
  384. VALUE Google_Protobuf_decode_json(VALUE self, VALUE klass, VALUE msg_rb);
  385. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
  386. VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
  387. VALUE enum_lookup(VALUE self, VALUE number);
  388. VALUE enum_resolve(VALUE self, VALUE sym);
  389. const upb_pbdecodermethod *new_fillmsg_decodermethod(
  390. Descriptor* descriptor, const void *owner);
  391. // -----------------------------------------------------------------------------
  392. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  393. // instances.
  394. // -----------------------------------------------------------------------------
  395. void add_def_obj(const void* def, VALUE value);
  396. VALUE get_def_obj(const void* def);
  397. // -----------------------------------------------------------------------------
  398. // Utilities.
  399. // -----------------------------------------------------------------------------
  400. void check_upb_status(const upb_status* status, const char* msg);
  401. #define CHECK_UPB(code, msg) do { \
  402. upb_status status = UPB_STATUS_INIT; \
  403. code; \
  404. check_upb_status(&status, msg); \
  405. } while (0)
  406. #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__