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