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