protobuf.h 21 KB

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