protobuf.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 FileDescriptor;
  40. struct FieldDescriptor;
  41. struct EnumDescriptor;
  42. struct MessageLayout;
  43. struct MessageField;
  44. struct MessageHeader;
  45. struct MessageBuilderContext;
  46. struct EnumBuilderContext;
  47. struct FileBuilderContext;
  48. struct Builder;
  49. typedef struct DescriptorPool DescriptorPool;
  50. typedef struct Descriptor Descriptor;
  51. typedef struct FileDescriptor FileDescriptor;
  52. typedef struct FieldDescriptor FieldDescriptor;
  53. typedef struct OneofDescriptor OneofDescriptor;
  54. typedef struct EnumDescriptor EnumDescriptor;
  55. typedef struct MessageLayout MessageLayout;
  56. typedef struct MessageField MessageField;
  57. typedef struct MessageOneof MessageOneof;
  58. typedef struct MessageHeader MessageHeader;
  59. typedef struct MessageBuilderContext MessageBuilderContext;
  60. typedef struct OneofBuilderContext OneofBuilderContext;
  61. typedef struct EnumBuilderContext EnumBuilderContext;
  62. typedef struct FileBuilderContext FileBuilderContext;
  63. typedef struct Builder Builder;
  64. /*
  65. It can be a bit confusing how the C structs defined below and the Ruby
  66. objects interact and hold references to each other. First, a few principles:
  67. - Ruby's "TypedData" abstraction lets a Ruby VALUE hold a pointer to a C
  68. struct (or arbitrary memory chunk), own it, and free it when collected.
  69. Thus, each struct below will have a corresponding Ruby object
  70. wrapping/owning it.
  71. - To get back from an underlying upb {msg,enum}def to the Ruby object, we
  72. keep a global hashmap, accessed by get_def_obj/add_def_obj below.
  73. The in-memory structure is then something like:
  74. Ruby | upb
  75. |
  76. DescriptorPool ------------|-----------> upb_symtab____________________
  77. | | (message types) \
  78. | v \
  79. Descriptor ---------------|-----------> upb_msgdef (enum types)|
  80. |--> msgclass | | ^ |
  81. | (dynamically built) | | | (submsg fields) |
  82. |--> MessageLayout | | | /
  83. |--------------------------|> decoder method| | /
  84. \--------------------------|> serialize | | /
  85. | handlers v | /
  86. FieldDescriptor -----------|-----------> upb_fielddef /
  87. | | /
  88. | v (enum fields) /
  89. EnumDescriptor ------------|-----------> upb_enumdef <----------'
  90. |
  91. |
  92. ^ | \___/
  93. `---------------|-----------------' (get_def_obj map)
  94. */
  95. // -----------------------------------------------------------------------------
  96. // Ruby class structure definitions.
  97. // -----------------------------------------------------------------------------
  98. struct DescriptorPool {
  99. upb_symtab* symtab;
  100. };
  101. struct Descriptor {
  102. const upb_msgdef* msgdef;
  103. MessageLayout* layout;
  104. VALUE klass; // begins as nil
  105. const upb_handlers* fill_handlers;
  106. const upb_pbdecodermethod* fill_method;
  107. const upb_json_parsermethod* json_fill_method;
  108. const upb_handlers* pb_serialize_handlers;
  109. const upb_handlers* json_serialize_handlers;
  110. const upb_handlers* json_serialize_handlers_preserve;
  111. };
  112. struct FileDescriptor {
  113. const upb_filedef* filedef;
  114. };
  115. struct FieldDescriptor {
  116. const upb_fielddef* fielddef;
  117. };
  118. struct OneofDescriptor {
  119. const upb_oneofdef* oneofdef;
  120. };
  121. struct EnumDescriptor {
  122. const upb_enumdef* enumdef;
  123. VALUE module; // begins as nil
  124. };
  125. struct MessageBuilderContext {
  126. VALUE descriptor;
  127. VALUE builder;
  128. };
  129. struct OneofBuilderContext {
  130. VALUE descriptor;
  131. VALUE builder;
  132. };
  133. struct EnumBuilderContext {
  134. VALUE enumdesc;
  135. };
  136. struct FileBuilderContext {
  137. VALUE pending_list;
  138. VALUE file_descriptor;
  139. VALUE builder;
  140. };
  141. struct Builder {
  142. VALUE pending_list;
  143. VALUE default_file_descriptor;
  144. upb_def** defs; // used only while finalizing
  145. };
  146. extern VALUE cDescriptorPool;
  147. extern VALUE cDescriptor;
  148. extern VALUE cFileDescriptor;
  149. extern VALUE cFieldDescriptor;
  150. extern VALUE cEnumDescriptor;
  151. extern VALUE cMessageBuilderContext;
  152. extern VALUE cOneofBuilderContext;
  153. extern VALUE cEnumBuilderContext;
  154. extern VALUE cFileBuilderContext;
  155. extern VALUE cBuilder;
  156. extern VALUE cError;
  157. extern VALUE cParseError;
  158. extern VALUE cTypeError;
  159. // We forward-declare all of the Ruby method implementations here because we
  160. // sometimes call the methods directly across .c files, rather than going
  161. // through Ruby's method dispatching (e.g. during message parse). It's cleaner
  162. // to keep the list of object methods together than to split them between
  163. // static-in-file definitions and header declarations.
  164. void DescriptorPool_mark(void* _self);
  165. void DescriptorPool_free(void* _self);
  166. VALUE DescriptorPool_alloc(VALUE klass);
  167. void DescriptorPool_register(VALUE module);
  168. DescriptorPool* ruby_to_DescriptorPool(VALUE value);
  169. VALUE DescriptorPool_add(VALUE _self, VALUE def);
  170. VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self);
  171. VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
  172. VALUE DescriptorPool_generated_pool(VALUE _self);
  173. extern VALUE generated_pool;
  174. void Descriptor_mark(void* _self);
  175. void Descriptor_free(void* _self);
  176. VALUE Descriptor_alloc(VALUE klass);
  177. void Descriptor_register(VALUE module);
  178. Descriptor* ruby_to_Descriptor(VALUE value);
  179. VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
  180. VALUE Descriptor_name(VALUE _self);
  181. VALUE Descriptor_name_set(VALUE _self, VALUE str);
  182. VALUE Descriptor_each(VALUE _self);
  183. VALUE Descriptor_lookup(VALUE _self, VALUE name);
  184. VALUE Descriptor_add_field(VALUE _self, VALUE obj);
  185. VALUE Descriptor_add_oneof(VALUE _self, VALUE obj);
  186. VALUE Descriptor_each_oneof(VALUE _self);
  187. VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
  188. VALUE Descriptor_msgclass(VALUE _self);
  189. VALUE Descriptor_file_descriptor(VALUE _self);
  190. extern const rb_data_type_t _Descriptor_type;
  191. void FileDescriptor_mark(void* _self);
  192. void FileDescriptor_free(void* _self);
  193. VALUE FileDescriptor_alloc(VALUE klass);
  194. void FileDescriptor_register(VALUE module);
  195. FileDescriptor* ruby_to_FileDescriptor(VALUE value);
  196. VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self);
  197. VALUE FileDescriptor_name(VALUE _self);
  198. VALUE FileDescriptor_syntax(VALUE _self);
  199. VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax);
  200. void FieldDescriptor_mark(void* _self);
  201. void FieldDescriptor_free(void* _self);
  202. VALUE FieldDescriptor_alloc(VALUE klass);
  203. void FieldDescriptor_register(VALUE module);
  204. FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
  205. VALUE FieldDescriptor_name(VALUE _self);
  206. VALUE FieldDescriptor_name_set(VALUE _self, VALUE str);
  207. VALUE FieldDescriptor_type(VALUE _self);
  208. VALUE FieldDescriptor_type_set(VALUE _self, VALUE type);
  209. VALUE FieldDescriptor_default(VALUE _self);
  210. VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value);
  211. VALUE FieldDescriptor_label(VALUE _self);
  212. VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
  213. VALUE FieldDescriptor_number(VALUE _self);
  214. VALUE FieldDescriptor_number_set(VALUE _self, VALUE number);
  215. VALUE FieldDescriptor_submsg_name(VALUE _self);
  216. VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value);
  217. VALUE FieldDescriptor_subtype(VALUE _self);
  218. VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb);
  219. VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb);
  220. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb);
  221. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
  222. upb_fieldtype_t ruby_to_fieldtype(VALUE type);
  223. VALUE fieldtype_to_ruby(upb_fieldtype_t type);
  224. void OneofDescriptor_mark(void* _self);
  225. void OneofDescriptor_free(void* _self);
  226. VALUE OneofDescriptor_alloc(VALUE klass);
  227. void OneofDescriptor_register(VALUE module);
  228. OneofDescriptor* ruby_to_OneofDescriptor(VALUE value);
  229. VALUE OneofDescriptor_name(VALUE _self);
  230. VALUE OneofDescriptor_name_set(VALUE _self, VALUE value);
  231. VALUE OneofDescriptor_add_field(VALUE _self, VALUE field);
  232. VALUE OneofDescriptor_each(VALUE _self, VALUE field);
  233. void EnumDescriptor_mark(void* _self);
  234. void EnumDescriptor_free(void* _self);
  235. VALUE EnumDescriptor_alloc(VALUE klass);
  236. void EnumDescriptor_register(VALUE module);
  237. EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
  238. VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
  239. VALUE EnumDescriptor_file_descriptor(VALUE _self);
  240. VALUE EnumDescriptor_name(VALUE _self);
  241. VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
  242. VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
  243. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
  244. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
  245. VALUE EnumDescriptor_each(VALUE _self);
  246. VALUE EnumDescriptor_enummodule(VALUE _self);
  247. extern const rb_data_type_t _EnumDescriptor_type;
  248. void MessageBuilderContext_mark(void* _self);
  249. void MessageBuilderContext_free(void* _self);
  250. VALUE MessageBuilderContext_alloc(VALUE klass);
  251. void MessageBuilderContext_register(VALUE module);
  252. MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
  253. VALUE MessageBuilderContext_initialize(VALUE _self,
  254. VALUE descriptor,
  255. VALUE builder);
  256. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
  257. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
  258. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
  259. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
  260. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name);
  261. void OneofBuilderContext_mark(void* _self);
  262. void OneofBuilderContext_free(void* _self);
  263. VALUE OneofBuilderContext_alloc(VALUE klass);
  264. void OneofBuilderContext_register(VALUE module);
  265. OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE value);
  266. VALUE OneofBuilderContext_initialize(VALUE _self,
  267. VALUE descriptor,
  268. VALUE builder);
  269. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
  270. void EnumBuilderContext_mark(void* _self);
  271. void EnumBuilderContext_free(void* _self);
  272. VALUE EnumBuilderContext_alloc(VALUE klass);
  273. void EnumBuilderContext_register(VALUE module);
  274. EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
  275. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
  276. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
  277. void FileBuilderContext_mark(void* _self);
  278. void FileBuilderContext_free(void* _self);
  279. VALUE FileBuilderContext_alloc(VALUE klass);
  280. void FileBuilderContext_register(VALUE module);
  281. VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
  282. VALUE builder);
  283. VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
  284. VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
  285. VALUE FileBuilderContext_pending_descriptors(VALUE _self);
  286. void Builder_mark(void* _self);
  287. void Builder_free(void* _self);
  288. VALUE Builder_alloc(VALUE klass);
  289. void Builder_register(VALUE module);
  290. Builder* ruby_to_Builder(VALUE value);
  291. VALUE Builder_initialize(VALUE _self);
  292. VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
  293. VALUE Builder_add_message(VALUE _self, VALUE name);
  294. VALUE Builder_add_enum(VALUE _self, VALUE name);
  295. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
  296. // -----------------------------------------------------------------------------
  297. // Native slot storage abstraction.
  298. // -----------------------------------------------------------------------------
  299. #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
  300. size_t native_slot_size(upb_fieldtype_t type);
  301. void native_slot_set(const char* name,
  302. upb_fieldtype_t type,
  303. VALUE type_class,
  304. void* memory,
  305. VALUE value);
  306. // Atomically (with respect to Ruby VM calls) either update the value and set a
  307. // oneof case, or do neither. If |case_memory| is null, then no case value is
  308. // set.
  309. void native_slot_set_value_and_case(const char* name,
  310. upb_fieldtype_t type,
  311. VALUE type_class,
  312. void* memory,
  313. VALUE value,
  314. uint32_t* case_memory,
  315. uint32_t case_number);
  316. VALUE native_slot_get(upb_fieldtype_t type,
  317. VALUE type_class,
  318. const void* memory);
  319. void native_slot_init(upb_fieldtype_t type, void* memory);
  320. void native_slot_mark(upb_fieldtype_t type, void* memory);
  321. void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
  322. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from);
  323. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2);
  324. VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value);
  325. void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE value);
  326. uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
  327. const upb_oneofdef* oneof);
  328. extern rb_encoding* kRubyStringUtf8Encoding;
  329. extern rb_encoding* kRubyStringASCIIEncoding;
  330. extern rb_encoding* kRubyString8bitEncoding;
  331. VALUE field_type_class(const upb_fielddef* field);
  332. #define MAP_KEY_FIELD 1
  333. #define MAP_VALUE_FIELD 2
  334. // Oneof case slot value to indicate that no oneof case is set. The value `0` is
  335. // safe because field numbers are used as case identifiers, and no field can
  336. // have a number of 0.
  337. #define ONEOF_CASE_NONE 0
  338. // These operate on a map field (i.e., a repeated field of submessages whose
  339. // submessage type is a map-entry msgdef).
  340. bool is_map_field(const upb_fielddef* field);
  341. const upb_fielddef* map_field_key(const upb_fielddef* field);
  342. const upb_fielddef* map_field_value(const upb_fielddef* field);
  343. // These operate on a map-entry msgdef.
  344. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  345. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  346. // -----------------------------------------------------------------------------
  347. // Repeated field container type.
  348. // -----------------------------------------------------------------------------
  349. typedef struct {
  350. upb_fieldtype_t field_type;
  351. VALUE field_type_class;
  352. void* elements;
  353. int size;
  354. int capacity;
  355. } RepeatedField;
  356. void RepeatedField_mark(void* self);
  357. void RepeatedField_free(void* self);
  358. VALUE RepeatedField_alloc(VALUE klass);
  359. VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
  360. void RepeatedField_register(VALUE module);
  361. extern const rb_data_type_t RepeatedField_type;
  362. extern VALUE cRepeatedField;
  363. RepeatedField* ruby_to_RepeatedField(VALUE value);
  364. VALUE RepeatedField_each(VALUE _self);
  365. VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self);
  366. void* RepeatedField_index_native(VALUE _self, int index);
  367. int RepeatedField_size(VALUE _self);
  368. VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
  369. void RepeatedField_reserve(RepeatedField* self, int new_size);
  370. VALUE RepeatedField_push(VALUE _self, VALUE val);
  371. void RepeatedField_push_native(VALUE _self, void* data);
  372. VALUE RepeatedField_pop_one(VALUE _self);
  373. VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
  374. VALUE RepeatedField_replace(VALUE _self, VALUE list);
  375. VALUE RepeatedField_clear(VALUE _self);
  376. VALUE RepeatedField_length(VALUE _self);
  377. VALUE RepeatedField_dup(VALUE _self);
  378. VALUE RepeatedField_deep_copy(VALUE _self);
  379. VALUE RepeatedField_to_ary(VALUE _self);
  380. VALUE RepeatedField_eq(VALUE _self, VALUE _other);
  381. VALUE RepeatedField_hash(VALUE _self);
  382. VALUE RepeatedField_inspect(VALUE _self);
  383. VALUE RepeatedField_plus(VALUE _self, VALUE list);
  384. // Defined in repeated_field.c; also used by Map.
  385. void validate_type_class(upb_fieldtype_t type, VALUE klass);
  386. // -----------------------------------------------------------------------------
  387. // Map container type.
  388. // -----------------------------------------------------------------------------
  389. typedef struct {
  390. upb_fieldtype_t key_type;
  391. upb_fieldtype_t value_type;
  392. VALUE value_type_class;
  393. VALUE parse_frame;
  394. upb_strtable table;
  395. } Map;
  396. void Map_mark(void* self);
  397. void Map_free(void* self);
  398. VALUE Map_alloc(VALUE klass);
  399. VALUE Map_init(int argc, VALUE* argv, VALUE self);
  400. void Map_register(VALUE module);
  401. VALUE Map_set_frame(VALUE self, VALUE val);
  402. extern const rb_data_type_t Map_type;
  403. extern VALUE cMap;
  404. Map* ruby_to_Map(VALUE value);
  405. VALUE Map_each(VALUE _self);
  406. VALUE Map_keys(VALUE _self);
  407. VALUE Map_values(VALUE _self);
  408. VALUE Map_index(VALUE _self, VALUE key);
  409. VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
  410. VALUE Map_has_key(VALUE _self, VALUE key);
  411. VALUE Map_delete(VALUE _self, VALUE key);
  412. VALUE Map_clear(VALUE _self);
  413. VALUE Map_length(VALUE _self);
  414. VALUE Map_dup(VALUE _self);
  415. VALUE Map_deep_copy(VALUE _self);
  416. VALUE Map_eq(VALUE _self, VALUE _other);
  417. VALUE Map_hash(VALUE _self);
  418. VALUE Map_to_h(VALUE _self);
  419. VALUE Map_inspect(VALUE _self);
  420. VALUE Map_merge(VALUE _self, VALUE hashmap);
  421. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
  422. typedef struct {
  423. Map* self;
  424. upb_strtable_iter it;
  425. } Map_iter;
  426. void Map_begin(VALUE _self, Map_iter* iter);
  427. void Map_next(Map_iter* iter);
  428. bool Map_done(Map_iter* iter);
  429. VALUE Map_iter_key(Map_iter* iter);
  430. VALUE Map_iter_value(Map_iter* iter);
  431. // -----------------------------------------------------------------------------
  432. // Message layout / storage.
  433. // -----------------------------------------------------------------------------
  434. #define MESSAGE_FIELD_NO_HASBIT ((uint32_t)-1)
  435. struct MessageField {
  436. uint32_t offset;
  437. uint32_t hasbit;
  438. };
  439. struct MessageOneof {
  440. uint32_t case_offset;
  441. };
  442. struct MessageLayout {
  443. const upb_msgdef* msgdef;
  444. MessageField* fields;
  445. MessageOneof* oneofs;
  446. size_t size;
  447. };
  448. MessageLayout* create_layout(const upb_msgdef* msgdef);
  449. void free_layout(MessageLayout* layout);
  450. bool field_contains_hasbit(MessageLayout* layout,
  451. const upb_fielddef* field);
  452. VALUE layout_get_default(const upb_fielddef* field);
  453. VALUE layout_get(MessageLayout* layout,
  454. const void* storage,
  455. const upb_fielddef* field);
  456. void layout_set(MessageLayout* layout,
  457. void* storage,
  458. const upb_fielddef* field,
  459. VALUE val);
  460. VALUE layout_has(MessageLayout* layout,
  461. const void* storage,
  462. const upb_fielddef* field);
  463. void layout_clear(MessageLayout* layout,
  464. const void* storage,
  465. const upb_fielddef* field);
  466. void layout_init(MessageLayout* layout, void* storage);
  467. void layout_mark(MessageLayout* layout, void* storage);
  468. void layout_dup(MessageLayout* layout, void* to, void* from);
  469. void layout_deep_copy(MessageLayout* layout, void* to, void* from);
  470. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
  471. VALUE layout_hash(MessageLayout* layout, void* storage);
  472. VALUE layout_inspect(MessageLayout* layout, void* storage);
  473. // -----------------------------------------------------------------------------
  474. // Message class creation.
  475. // -----------------------------------------------------------------------------
  476. // This should probably be factored into a common upb component.
  477. typedef struct {
  478. upb_byteshandler handler;
  479. upb_bytessink sink;
  480. char *ptr;
  481. size_t len, size;
  482. } stringsink;
  483. void stringsink_uninit(stringsink *sink);
  484. struct MessageHeader {
  485. Descriptor* descriptor; // kept alive by self.class.descriptor reference.
  486. stringsink* unknown_fields; // store unknown fields in decoding.
  487. // Data comes after this.
  488. };
  489. extern rb_data_type_t Message_type;
  490. VALUE build_class_from_descriptor(Descriptor* descriptor);
  491. void* Message_data(void* msg);
  492. void Message_mark(void* self);
  493. void Message_free(void* self);
  494. VALUE Message_alloc(VALUE klass);
  495. VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
  496. VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
  497. VALUE Message_dup(VALUE _self);
  498. VALUE Message_deep_copy(VALUE _self);
  499. VALUE Message_eq(VALUE _self, VALUE _other);
  500. VALUE Message_hash(VALUE _self);
  501. VALUE Message_inspect(VALUE _self);
  502. VALUE Message_to_h(VALUE _self);
  503. VALUE Message_index(VALUE _self, VALUE field_name);
  504. VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
  505. VALUE Message_descriptor(VALUE klass);
  506. VALUE Message_decode(VALUE klass, VALUE data);
  507. VALUE Message_encode(VALUE klass, VALUE msg_rb);
  508. VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass);
  509. VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
  510. VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
  511. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
  512. VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
  513. VALUE enum_lookup(VALUE self, VALUE number);
  514. VALUE enum_resolve(VALUE self, VALUE sym);
  515. const upb_pbdecodermethod *new_fillmsg_decodermethod(
  516. Descriptor* descriptor, const void *owner);
  517. // Maximum depth allowed during encoding, to avoid stack overflows due to
  518. // cycles.
  519. #define ENCODE_MAX_NESTING 63
  520. // -----------------------------------------------------------------------------
  521. // A cache of frozen string objects to use as field defaults.
  522. // -----------------------------------------------------------------------------
  523. VALUE get_frozen_string(const char* data, size_t size, bool binary);
  524. // -----------------------------------------------------------------------------
  525. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  526. // instances.
  527. // -----------------------------------------------------------------------------
  528. void add_def_obj(const void* def, VALUE value);
  529. VALUE get_def_obj(const void* def);
  530. // -----------------------------------------------------------------------------
  531. // Utilities.
  532. // -----------------------------------------------------------------------------
  533. void check_upb_status(const upb_status* status, const char* msg);
  534. #define CHECK_UPB(code, msg) do { \
  535. upb_status status = UPB_STATUS_INIT; \
  536. code; \
  537. check_upb_status(&status, msg); \
  538. } while (0)
  539. extern ID descriptor_instancevar_interned;
  540. #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__