protobuf.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
  100. upb_symtab* symtab;
  101. upb_handlercache* fill_handler_cache;
  102. upb_handlercache* pb_serialize_handler_cache;
  103. upb_handlercache* json_serialize_handler_cache;
  104. upb_handlercache* json_serialize_handler_preserve_cache;
  105. upb_pbcodecache* fill_method_cache;
  106. upb_json_codecache* json_fill_method_cache;
  107. };
  108. struct Descriptor {
  109. const upb_msgdef* msgdef;
  110. MessageLayout* layout;
  111. VALUE klass;
  112. VALUE descriptor_pool;
  113. };
  114. struct FileDescriptor {
  115. const upb_filedef* filedef;
  116. VALUE descriptor_pool; // Owns the upb_filedef.
  117. };
  118. struct FieldDescriptor {
  119. const upb_fielddef* fielddef;
  120. VALUE descriptor_pool; // Owns the upb_fielddef.
  121. };
  122. struct OneofDescriptor {
  123. const upb_oneofdef* oneofdef;
  124. VALUE descriptor_pool; // Owns the upb_oneofdef.
  125. };
  126. struct EnumDescriptor {
  127. const upb_enumdef* enumdef;
  128. VALUE module; // begins as nil
  129. VALUE descriptor_pool; // Owns the upb_enumdef.
  130. };
  131. struct MessageBuilderContext {
  132. google_protobuf_DescriptorProto* msg_proto;
  133. VALUE file_builder;
  134. };
  135. struct OneofBuilderContext {
  136. int oneof_index;
  137. VALUE message_builder;
  138. };
  139. struct EnumBuilderContext {
  140. google_protobuf_EnumDescriptorProto* enum_proto;
  141. VALUE file_builder;
  142. };
  143. struct FileBuilderContext {
  144. upb_arena *arena;
  145. google_protobuf_FileDescriptorProto* file_proto;
  146. VALUE descriptor_pool;
  147. };
  148. struct Builder {
  149. VALUE descriptor_pool;
  150. VALUE default_file_builder;
  151. };
  152. extern VALUE cDescriptorPool;
  153. extern VALUE cDescriptor;
  154. extern VALUE cFileDescriptor;
  155. extern VALUE cFieldDescriptor;
  156. extern VALUE cEnumDescriptor;
  157. extern VALUE cMessageBuilderContext;
  158. extern VALUE cOneofBuilderContext;
  159. extern VALUE cEnumBuilderContext;
  160. extern VALUE cFileBuilderContext;
  161. extern VALUE cBuilder;
  162. extern VALUE cError;
  163. extern VALUE cParseError;
  164. extern VALUE cTypeError;
  165. // We forward-declare all of the Ruby method implementations here because we
  166. // sometimes call the methods directly across .c files, rather than going
  167. // through Ruby's method dispatching (e.g. during message parse). It's cleaner
  168. // to keep the list of object methods together than to split them between
  169. // static-in-file definitions and header declarations.
  170. void DescriptorPool_mark(void* _self);
  171. void DescriptorPool_free(void* _self);
  172. VALUE DescriptorPool_alloc(VALUE klass);
  173. void DescriptorPool_register(VALUE module);
  174. DescriptorPool* ruby_to_DescriptorPool(VALUE value);
  175. VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self);
  176. VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
  177. VALUE DescriptorPool_generated_pool(VALUE _self);
  178. extern VALUE generated_pool;
  179. void Descriptor_mark(void* _self);
  180. void Descriptor_free(void* _self);
  181. VALUE Descriptor_alloc(VALUE klass);
  182. void Descriptor_register(VALUE module);
  183. Descriptor* ruby_to_Descriptor(VALUE value);
  184. VALUE Descriptor_initialize(VALUE _self, VALUE cookie, VALUE descriptor_pool,
  185. VALUE ptr);
  186. VALUE Descriptor_name(VALUE _self);
  187. VALUE Descriptor_each(VALUE _self);
  188. VALUE Descriptor_lookup(VALUE _self, VALUE name);
  189. VALUE Descriptor_each_oneof(VALUE _self);
  190. VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
  191. VALUE Descriptor_msgclass(VALUE _self);
  192. VALUE Descriptor_file_descriptor(VALUE _self);
  193. extern const rb_data_type_t _Descriptor_type;
  194. void FileDescriptor_mark(void* _self);
  195. void FileDescriptor_free(void* _self);
  196. VALUE FileDescriptor_alloc(VALUE klass);
  197. void FileDescriptor_register(VALUE module);
  198. FileDescriptor* ruby_to_FileDescriptor(VALUE value);
  199. VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
  200. VALUE descriptor_pool, VALUE ptr);
  201. VALUE FileDescriptor_name(VALUE _self);
  202. VALUE FileDescriptor_syntax(VALUE _self);
  203. void FieldDescriptor_mark(void* _self);
  204. void FieldDescriptor_free(void* _self);
  205. VALUE FieldDescriptor_alloc(VALUE klass);
  206. void FieldDescriptor_register(VALUE module);
  207. FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
  208. VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
  209. VALUE descriptor_pool, VALUE ptr);
  210. VALUE FieldDescriptor_name(VALUE _self);
  211. VALUE FieldDescriptor_type(VALUE _self);
  212. VALUE FieldDescriptor_default(VALUE _self);
  213. VALUE FieldDescriptor_label(VALUE _self);
  214. VALUE FieldDescriptor_number(VALUE _self);
  215. VALUE FieldDescriptor_submsg_name(VALUE _self);
  216. VALUE FieldDescriptor_subtype(VALUE _self);
  217. VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb);
  218. VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb);
  219. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb);
  220. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
  221. upb_fieldtype_t ruby_to_fieldtype(VALUE type);
  222. VALUE fieldtype_to_ruby(upb_fieldtype_t type);
  223. void OneofDescriptor_mark(void* _self);
  224. void OneofDescriptor_free(void* _self);
  225. VALUE OneofDescriptor_alloc(VALUE klass);
  226. void OneofDescriptor_register(VALUE module);
  227. OneofDescriptor* ruby_to_OneofDescriptor(VALUE value);
  228. VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
  229. VALUE descriptor_pool, VALUE ptr);
  230. VALUE OneofDescriptor_name(VALUE _self);
  231. VALUE OneofDescriptor_each(VALUE _self);
  232. void EnumDescriptor_mark(void* _self);
  233. void EnumDescriptor_free(void* _self);
  234. VALUE EnumDescriptor_alloc(VALUE klass);
  235. VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
  236. VALUE descriptor_pool, VALUE ptr);
  237. void EnumDescriptor_register(VALUE module);
  238. EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
  239. VALUE EnumDescriptor_file_descriptor(VALUE _self);
  240. VALUE EnumDescriptor_name(VALUE _self);
  241. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
  242. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
  243. VALUE EnumDescriptor_each(VALUE _self);
  244. VALUE EnumDescriptor_enummodule(VALUE _self);
  245. extern const rb_data_type_t _EnumDescriptor_type;
  246. void MessageBuilderContext_mark(void* _self);
  247. void MessageBuilderContext_free(void* _self);
  248. VALUE MessageBuilderContext_alloc(VALUE klass);
  249. void MessageBuilderContext_register(VALUE module);
  250. MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
  251. VALUE MessageBuilderContext_initialize(VALUE _self,
  252. VALUE _file_builder,
  253. VALUE name);
  254. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
  255. VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv, VALUE _self);
  256. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
  257. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
  258. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
  259. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name);
  260. void OneofBuilderContext_mark(void* _self);
  261. void OneofBuilderContext_free(void* _self);
  262. VALUE OneofBuilderContext_alloc(VALUE klass);
  263. void OneofBuilderContext_register(VALUE module);
  264. OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE value);
  265. VALUE OneofBuilderContext_initialize(VALUE _self,
  266. VALUE descriptor,
  267. VALUE builder);
  268. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
  269. void EnumBuilderContext_mark(void* _self);
  270. void EnumBuilderContext_free(void* _self);
  271. VALUE EnumBuilderContext_alloc(VALUE klass);
  272. void EnumBuilderContext_register(VALUE module);
  273. EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
  274. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
  275. VALUE name);
  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. FileBuilderContext* ruby_to_FileBuilderContext(VALUE _self);
  282. upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str);
  283. upb_strview FileBuilderContext_strdup_name(VALUE _self, VALUE rb_str);
  284. upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym);
  285. VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
  286. VALUE name, VALUE options);
  287. VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
  288. VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
  289. VALUE FileBuilderContext_pending_descriptors(VALUE _self);
  290. void Builder_mark(void* _self);
  291. void Builder_free(void* _self);
  292. VALUE Builder_alloc(VALUE klass);
  293. void Builder_register(VALUE module);
  294. Builder* ruby_to_Builder(VALUE value);
  295. VALUE Builder_build(VALUE _self);
  296. VALUE Builder_initialize(VALUE _self, VALUE descriptor_pool);
  297. VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
  298. VALUE Builder_add_message(VALUE _self, VALUE name);
  299. VALUE Builder_add_enum(VALUE _self, VALUE name);
  300. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
  301. // -----------------------------------------------------------------------------
  302. // Native slot storage abstraction.
  303. // -----------------------------------------------------------------------------
  304. #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
  305. size_t native_slot_size(upb_fieldtype_t type);
  306. void native_slot_set(const char* name,
  307. upb_fieldtype_t type,
  308. VALUE type_class,
  309. void* memory,
  310. VALUE value);
  311. // Atomically (with respect to Ruby VM calls) either update the value and set a
  312. // oneof case, or do neither. If |case_memory| is null, then no case value is
  313. // set.
  314. void native_slot_set_value_and_case(const char* name,
  315. upb_fieldtype_t type,
  316. VALUE type_class,
  317. void* memory,
  318. VALUE value,
  319. uint32_t* case_memory,
  320. uint32_t case_number);
  321. VALUE native_slot_get(upb_fieldtype_t type,
  322. VALUE type_class,
  323. const void* memory);
  324. void native_slot_init(upb_fieldtype_t type, void* memory);
  325. void native_slot_mark(upb_fieldtype_t type, void* memory);
  326. void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
  327. void native_slot_deep_copy(upb_fieldtype_t type, VALUE type_class, void* to,
  328. void* from);
  329. bool native_slot_eq(upb_fieldtype_t type, VALUE type_class, void* mem1,
  330. void* mem2);
  331. VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value);
  332. void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE value);
  333. uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
  334. const upb_oneofdef* oneof);
  335. bool is_value_field(const upb_fielddef* f);
  336. extern rb_encoding* kRubyStringUtf8Encoding;
  337. extern rb_encoding* kRubyStringASCIIEncoding;
  338. extern rb_encoding* kRubyString8bitEncoding;
  339. VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field);
  340. #define MAP_KEY_FIELD 1
  341. #define MAP_VALUE_FIELD 2
  342. // Oneof case slot value to indicate that no oneof case is set. The value `0` is
  343. // safe because field numbers are used as case identifiers, and no field can
  344. // have a number of 0.
  345. #define ONEOF_CASE_NONE 0
  346. // These operate on a map field (i.e., a repeated field of submessages whose
  347. // submessage type is a map-entry msgdef).
  348. bool is_map_field(const upb_fielddef* field);
  349. const upb_fielddef* map_field_key(const upb_fielddef* field);
  350. const upb_fielddef* map_field_value(const upb_fielddef* field);
  351. // These operate on a map-entry msgdef.
  352. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  353. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  354. // -----------------------------------------------------------------------------
  355. // Repeated field container type.
  356. // -----------------------------------------------------------------------------
  357. typedef struct {
  358. upb_fieldtype_t field_type;
  359. VALUE field_type_class;
  360. void* elements;
  361. int size;
  362. int capacity;
  363. } RepeatedField;
  364. void RepeatedField_mark(void* self);
  365. void RepeatedField_free(void* self);
  366. VALUE RepeatedField_alloc(VALUE klass);
  367. VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
  368. void RepeatedField_register(VALUE module);
  369. extern const rb_data_type_t RepeatedField_type;
  370. extern VALUE cRepeatedField;
  371. RepeatedField* ruby_to_RepeatedField(VALUE value);
  372. VALUE RepeatedField_new_this_type(VALUE _self);
  373. VALUE RepeatedField_each(VALUE _self);
  374. VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self);
  375. void* RepeatedField_index_native(VALUE _self, int index);
  376. int RepeatedField_size(VALUE _self);
  377. VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
  378. void RepeatedField_reserve(RepeatedField* self, int new_size);
  379. VALUE RepeatedField_push(VALUE _self, VALUE val);
  380. void RepeatedField_push_native(VALUE _self, void* data);
  381. VALUE RepeatedField_pop_one(VALUE _self);
  382. VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
  383. VALUE RepeatedField_replace(VALUE _self, VALUE list);
  384. VALUE RepeatedField_clear(VALUE _self);
  385. VALUE RepeatedField_length(VALUE _self);
  386. VALUE RepeatedField_dup(VALUE _self);
  387. VALUE RepeatedField_deep_copy(VALUE _self);
  388. VALUE RepeatedField_to_ary(VALUE _self);
  389. VALUE RepeatedField_eq(VALUE _self, VALUE _other);
  390. VALUE RepeatedField_hash(VALUE _self);
  391. VALUE RepeatedField_inspect(VALUE _self);
  392. VALUE RepeatedField_plus(VALUE _self, VALUE list);
  393. // Defined in repeated_field.c; also used by Map.
  394. void validate_type_class(upb_fieldtype_t type, VALUE klass);
  395. // -----------------------------------------------------------------------------
  396. // Map container type.
  397. // -----------------------------------------------------------------------------
  398. typedef struct {
  399. upb_fieldtype_t key_type;
  400. upb_fieldtype_t value_type;
  401. VALUE value_type_class;
  402. VALUE parse_frame;
  403. upb_strtable table;
  404. } Map;
  405. void Map_mark(void* self);
  406. void Map_free(void* self);
  407. VALUE Map_alloc(VALUE klass);
  408. VALUE Map_init(int argc, VALUE* argv, VALUE self);
  409. void Map_register(VALUE module);
  410. VALUE Map_set_frame(VALUE self, VALUE val);
  411. extern const rb_data_type_t Map_type;
  412. extern VALUE cMap;
  413. Map* ruby_to_Map(VALUE value);
  414. VALUE Map_new_this_type(VALUE _self);
  415. VALUE Map_each(VALUE _self);
  416. VALUE Map_keys(VALUE _self);
  417. VALUE Map_values(VALUE _self);
  418. VALUE Map_index(VALUE _self, VALUE key);
  419. VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
  420. VALUE Map_has_key(VALUE _self, VALUE key);
  421. VALUE Map_delete(VALUE _self, VALUE key);
  422. VALUE Map_clear(VALUE _self);
  423. VALUE Map_length(VALUE _self);
  424. VALUE Map_dup(VALUE _self);
  425. VALUE Map_deep_copy(VALUE _self);
  426. VALUE Map_eq(VALUE _self, VALUE _other);
  427. VALUE Map_hash(VALUE _self);
  428. VALUE Map_to_h(VALUE _self);
  429. VALUE Map_inspect(VALUE _self);
  430. VALUE Map_merge(VALUE _self, VALUE hashmap);
  431. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
  432. typedef struct {
  433. Map* self;
  434. upb_strtable_iter it;
  435. } Map_iter;
  436. void Map_begin(VALUE _self, Map_iter* iter);
  437. void Map_next(Map_iter* iter);
  438. bool Map_done(Map_iter* iter);
  439. VALUE Map_iter_key(Map_iter* iter);
  440. VALUE Map_iter_value(Map_iter* iter);
  441. // -----------------------------------------------------------------------------
  442. // Message layout / storage.
  443. // -----------------------------------------------------------------------------
  444. #define MESSAGE_FIELD_NO_HASBIT ((uint32_t)-1)
  445. struct MessageField {
  446. uint32_t offset;
  447. uint32_t hasbit;
  448. };
  449. struct MessageOneof {
  450. uint32_t offset;
  451. uint32_t case_offset;
  452. };
  453. // MessageLayout is owned by the enclosing Descriptor, which must outlive us.
  454. struct MessageLayout {
  455. const Descriptor* desc;
  456. const upb_msgdef* msgdef;
  457. void* empty_template; // Can memcpy() onto a layout to clear it.
  458. MessageField* fields;
  459. MessageOneof* oneofs;
  460. uint32_t size;
  461. uint32_t value_offset;
  462. int value_count;
  463. int repeated_count;
  464. int map_count;
  465. };
  466. #define ONEOF_CASE_MASK 0x80000000
  467. void create_layout(Descriptor* desc);
  468. void free_layout(MessageLayout* layout);
  469. bool field_contains_hasbit(MessageLayout* layout,
  470. const upb_fielddef* field);
  471. VALUE layout_get_default(const upb_fielddef* field);
  472. VALUE layout_get(MessageLayout* layout,
  473. const void* storage,
  474. const upb_fielddef* field);
  475. void layout_set(MessageLayout* layout,
  476. void* storage,
  477. const upb_fielddef* field,
  478. VALUE val);
  479. VALUE layout_has(MessageLayout* layout,
  480. const void* storage,
  481. const upb_fielddef* field);
  482. void layout_clear(MessageLayout* layout,
  483. const void* storage,
  484. const upb_fielddef* field);
  485. void layout_init(MessageLayout* layout, void* storage);
  486. void layout_mark(MessageLayout* layout, void* storage);
  487. void layout_dup(MessageLayout* layout, void* to, void* from);
  488. void layout_deep_copy(MessageLayout* layout, void* to, void* from);
  489. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
  490. VALUE layout_hash(MessageLayout* layout, void* storage);
  491. VALUE layout_inspect(MessageLayout* layout, void* storage);
  492. bool is_wrapper_type_field(const upb_fielddef* field);
  493. VALUE ruby_wrapper_type(VALUE type_class, VALUE value);
  494. // -----------------------------------------------------------------------------
  495. // Message class creation.
  496. // -----------------------------------------------------------------------------
  497. // This should probably be factored into a common upb component.
  498. typedef struct {
  499. upb_byteshandler handler;
  500. upb_bytessink sink;
  501. char *ptr;
  502. size_t len, size;
  503. } stringsink;
  504. void stringsink_uninit(stringsink *sink);
  505. struct MessageHeader {
  506. Descriptor* descriptor; // kept alive by self.class.descriptor reference.
  507. stringsink* unknown_fields; // store unknown fields in decoding.
  508. // Data comes after this.
  509. };
  510. extern rb_data_type_t Message_type;
  511. VALUE build_class_from_descriptor(VALUE descriptor);
  512. void* Message_data(void* msg);
  513. void Message_mark(void* self);
  514. void Message_free(void* self);
  515. VALUE Message_alloc(VALUE klass);
  516. VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
  517. VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
  518. VALUE Message_dup(VALUE _self);
  519. VALUE Message_deep_copy(VALUE _self);
  520. VALUE Message_eq(VALUE _self, VALUE _other);
  521. VALUE Message_hash(VALUE _self);
  522. VALUE Message_inspect(VALUE _self);
  523. VALUE Message_to_h(VALUE _self);
  524. VALUE Message_index(VALUE _self, VALUE field_name);
  525. VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
  526. VALUE Message_descriptor(VALUE klass);
  527. VALUE Message_decode(VALUE klass, VALUE data);
  528. VALUE Message_encode(VALUE klass, VALUE msg_rb);
  529. VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass);
  530. VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
  531. VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
  532. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
  533. VALUE build_module_from_enumdesc(VALUE _enumdesc);
  534. VALUE enum_lookup(VALUE self, VALUE number);
  535. VALUE enum_resolve(VALUE self, VALUE sym);
  536. VALUE enum_descriptor(VALUE self);
  537. const upb_pbdecodermethod *new_fillmsg_decodermethod(
  538. Descriptor* descriptor, const void *owner);
  539. void add_handlers_for_message(const void *closure, upb_handlers *h);
  540. // Maximum depth allowed during encoding, to avoid stack overflows due to
  541. // cycles.
  542. #define ENCODE_MAX_NESTING 63
  543. // -----------------------------------------------------------------------------
  544. // A cache of frozen string objects to use as field defaults.
  545. // -----------------------------------------------------------------------------
  546. VALUE get_frozen_string(const char* data, size_t size, bool binary);
  547. // -----------------------------------------------------------------------------
  548. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  549. // instances.
  550. // -----------------------------------------------------------------------------
  551. VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
  552. VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
  553. VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
  554. VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
  555. VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
  556. // -----------------------------------------------------------------------------
  557. // Utilities.
  558. // -----------------------------------------------------------------------------
  559. void check_upb_status(const upb_status* status, const char* msg);
  560. #define CHECK_UPB(code, msg) do { \
  561. upb_status status = UPB_STATUS_INIT; \
  562. code; \
  563. check_upb_status(&status, msg); \
  564. } while (0)
  565. extern ID descriptor_instancevar_interned;
  566. // A distinct object that is not accessible from Ruby. We use this as a
  567. // constructor argument to enforce that certain objects cannot be created from
  568. // Ruby.
  569. extern VALUE c_only_cookie;
  570. #ifdef NDEBUG
  571. #define UPB_ASSERT(expr) do {} while (false && (expr))
  572. #else
  573. #define UPB_ASSERT(expr) assert(expr)
  574. #endif
  575. #define UPB_UNUSED(var) (void)var
  576. #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__