protobuf.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "protobuf.h"
  31. // -----------------------------------------------------------------------------
  32. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  33. // instances.
  34. // -----------------------------------------------------------------------------
  35. // This is a hash table from def objects (encoded by converting pointers to
  36. // Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
  37. VALUE upb_def_to_ruby_obj_map;
  38. VALUE cError;
  39. VALUE cParseError;
  40. VALUE cTypeError;
  41. void add_def_obj(const void* def, VALUE value) {
  42. rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
  43. }
  44. VALUE get_def_obj(const void* def) {
  45. return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
  46. }
  47. static VALUE cached_empty_string = Qnil;
  48. static VALUE cached_empty_bytes = Qnil;
  49. static VALUE create_frozen_string(const char* str, size_t size, bool binary) {
  50. VALUE str_rb = rb_str_new(str, size);
  51. rb_enc_associate(str_rb,
  52. binary ? kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  53. rb_obj_freeze(str_rb);
  54. return str_rb;
  55. }
  56. VALUE get_frozen_string(const char* str, size_t size, bool binary) {
  57. if (size == 0) {
  58. return binary ? cached_empty_bytes : cached_empty_string;
  59. } else {
  60. // It is harder to memoize non-empty strings. The obvious approach would be
  61. // to use a Ruby hash keyed by string as memo table, but looking up in such a table
  62. // requires constructing a string (the very thing we're trying to avoid).
  63. //
  64. // Since few fields have defaults, we will just optimize the empty string
  65. // case for now.
  66. return create_frozen_string(str, size, binary);
  67. }
  68. }
  69. // -----------------------------------------------------------------------------
  70. // Utilities.
  71. // -----------------------------------------------------------------------------
  72. // Raises a Ruby error if |status| is not OK, using its error message.
  73. void check_upb_status(const upb_status* status, const char* msg) {
  74. if (!upb_ok(status)) {
  75. rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
  76. }
  77. }
  78. // String encodings: we look these up once, at load time, and then cache them
  79. // here.
  80. rb_encoding* kRubyStringUtf8Encoding;
  81. rb_encoding* kRubyStringASCIIEncoding;
  82. rb_encoding* kRubyString8bitEncoding;
  83. // Ruby-interned string: "descriptor". We use this identifier to store an
  84. // instance variable on message classes we create in order to link them back to
  85. // their descriptors.
  86. //
  87. // We intern this once at module load time then use the interned identifier at
  88. // runtime in order to avoid the cost of repeatedly interning in hot paths.
  89. const char* kDescriptorInstanceVar = "descriptor";
  90. ID descriptor_instancevar_interned;
  91. // -----------------------------------------------------------------------------
  92. // Initialization/entry point.
  93. // -----------------------------------------------------------------------------
  94. // This must be named "Init_protobuf_c" because the Ruby module is named
  95. // "protobuf_c" -- the VM looks for this symbol in our .so.
  96. void Init_protobuf_c() {
  97. VALUE google = rb_define_module("Google");
  98. VALUE protobuf = rb_define_module_under(google, "Protobuf");
  99. VALUE internal = rb_define_module_under(protobuf, "Internal");
  100. descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar);
  101. DescriptorPool_register(protobuf);
  102. Descriptor_register(protobuf);
  103. FileDescriptor_register(protobuf);
  104. FieldDescriptor_register(protobuf);
  105. OneofDescriptor_register(protobuf);
  106. EnumDescriptor_register(protobuf);
  107. MessageBuilderContext_register(internal);
  108. OneofBuilderContext_register(internal);
  109. EnumBuilderContext_register(internal);
  110. FileBuilderContext_register(internal);
  111. Builder_register(internal);
  112. RepeatedField_register(protobuf);
  113. Map_register(protobuf);
  114. cError = rb_const_get(protobuf, rb_intern("Error"));
  115. cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
  116. cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
  117. rb_define_singleton_method(protobuf, "discard_unknown",
  118. Google_Protobuf_discard_unknown, 1);
  119. rb_define_singleton_method(protobuf, "deep_copy",
  120. Google_Protobuf_deep_copy, 1);
  121. kRubyStringUtf8Encoding = rb_utf8_encoding();
  122. kRubyStringASCIIEncoding = rb_usascii_encoding();
  123. kRubyString8bitEncoding = rb_ascii8bit_encoding();
  124. rb_gc_register_address(&upb_def_to_ruby_obj_map);
  125. upb_def_to_ruby_obj_map = rb_hash_new();
  126. rb_gc_register_address(&cached_empty_string);
  127. rb_gc_register_address(&cached_empty_bytes);
  128. cached_empty_string = create_frozen_string("", 0, false);
  129. cached_empty_bytes = create_frozen_string("", 0, true);
  130. }