ruby/ext/google/protobuf_c/protobuf.c
Go to the documentation of this file.
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 #include "protobuf.h"
32 
33 // -----------------------------------------------------------------------------
34 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
35 // instances.
36 // -----------------------------------------------------------------------------
37 
38 // This is a hash table from def objects (encoded by converting pointers to
39 // Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
41 
42 VALUE cError;
44 VALUE cTypeError;
45 
46 void add_def_obj(const void* def, VALUE value) {
47  rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
48 }
49 
50 VALUE get_def_obj(const void* def) {
51  return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
52 }
53 
54 // -----------------------------------------------------------------------------
55 // Utilities.
56 // -----------------------------------------------------------------------------
57 
58 // Raises a Ruby error if |status| is not OK, using its error message.
59 void check_upb_status(const upb_status* status, const char* msg) {
60  if (!upb_ok(status)) {
61  rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
62  }
63 }
64 
65 // String encodings: we look these up once, at load time, and then cache them
66 // here.
70 
71 // Ruby-interned string: "descriptor". We use this identifier to store an
72 // instance variable on message classes we create in order to link them back to
73 // their descriptors.
74 //
75 // We intern this once at module load time then use the interned identifier at
76 // runtime in order to avoid the cost of repeatedly interning in hot paths.
77 const char* kDescriptorInstanceVar = "descriptor";
79 
80 // -----------------------------------------------------------------------------
81 // Initialization/entry point.
82 // -----------------------------------------------------------------------------
83 
84 // This must be named "Init_protobuf_c" because the Ruby module is named
85 // "protobuf_c" -- the VM looks for this symbol in our .so.
87  VALUE google = rb_define_module("Google");
88  VALUE protobuf = rb_define_module_under(google, "Protobuf");
89  VALUE internal = rb_define_module_under(protobuf, "Internal");
90 
92  DescriptorPool_register(protobuf);
93  Descriptor_register(protobuf);
94  FileDescriptor_register(protobuf);
95  FieldDescriptor_register(protobuf);
96  OneofDescriptor_register(protobuf);
97  EnumDescriptor_register(protobuf);
100  EnumBuilderContext_register(internal);
101  FileBuilderContext_register(internal);
102  Builder_register(internal);
103  RepeatedField_register(protobuf);
104  Map_register(protobuf);
105 
106  cError = rb_const_get(protobuf, rb_intern("Error"));
107  cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
108  cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
109 
110  rb_define_singleton_method(protobuf, "discard_unknown",
112  rb_define_singleton_method(protobuf, "deep_copy",
114 
115  kRubyStringUtf8Encoding = rb_utf8_encoding();
116  kRubyStringASCIIEncoding = rb_usascii_encoding();
117  kRubyString8bitEncoding = rb_ascii8bit_encoding();
118 
119  rb_gc_register_address(&upb_def_to_ruby_obj_map);
120  upb_def_to_ruby_obj_map = rb_hash_new();
121 }
EnumBuilderContext_register
void EnumBuilderContext_register(VALUE module)
Definition: defs.c:1902
upb_status
Definition: php/ext/google/protobuf/upb.h:170
Google_Protobuf_deep_copy
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj)
Definition: ruby/ext/google/protobuf_c/message.c:839
kRubyStringASCIIEncoding
rb_encoding * kRubyStringASCIIEncoding
Definition: ruby/ext/google/protobuf_c/protobuf.c:68
descriptor_instancevar_interned
ID descriptor_instancevar_interned
Definition: ruby/ext/google/protobuf_c/protobuf.c:78
OneofDescriptor_register
void OneofDescriptor_register(VALUE module)
Definition: defs.c:1185
protobuf.h
Builder_register
void Builder_register(VALUE module)
Definition: defs.c:2086
cError
VALUE cError
Definition: ruby/ext/google/protobuf_c/protobuf.c:42
OneofBuilderContext_register
void OneofBuilderContext_register(VALUE module)
Definition: defs.c:1828
EnumDescriptor_register
void EnumDescriptor_register(VALUE module)
Definition: defs.c:1303
cParseError
VALUE cParseError
Definition: ruby/ext/google/protobuf_c/protobuf.c:43
MessageBuilderContext_register
void MessageBuilderContext_register(VALUE module)
Definition: defs.c:1500
kRubyStringUtf8Encoding
rb_encoding * kRubyStringUtf8Encoding
Definition: ruby/ext/google/protobuf_c/protobuf.c:67
Google_Protobuf_discard_unknown
VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb)
Definition: ruby/ext/google/protobuf_c/encode_decode.c:1607
FileDescriptor_register
void FileDescriptor_register(VALUE module)
Definition: defs.c:535
RepeatedField_register
void RepeatedField_register(VALUE module)
Definition: repeated_field.c:631
kRubyString8bitEncoding
rb_encoding * kRubyString8bitEncoding
Definition: ruby/ext/google/protobuf_c/protobuf.c:69
Map_register
void Map_register(VALUE module)
Definition: ruby/ext/google/protobuf_c/map.c:831
add_def_obj
void add_def_obj(const void *def, VALUE value)
Definition: ruby/ext/google/protobuf_c/protobuf.c:46
Descriptor_register
void Descriptor_register(VALUE module)
Definition: defs.c:288
Init_protobuf_c
void Init_protobuf_c()
Definition: ruby/ext/google/protobuf_c/protobuf.c:86
cTypeError
VALUE cTypeError
Definition: ruby/ext/google/protobuf_c/protobuf.c:44
check_upb_status
void check_upb_status(const upb_status *status, const char *msg)
Definition: ruby/ext/google/protobuf_c/protobuf.c:59
upb_def_to_ruby_obj_map
VALUE upb_def_to_ruby_obj_map
Definition: ruby/ext/google/protobuf_c/protobuf.c:40
kDescriptorInstanceVar
const char * kDescriptorInstanceVar
Definition: ruby/ext/google/protobuf_c/protobuf.c:77
DescriptorPool_register
void DescriptorPool_register(VALUE module)
Definition: defs.c:120
upb_status_errmsg
const char * upb_status_errmsg(const upb_status *status)
Definition: php/ext/google/protobuf/upb.c:5575
get_def_obj
VALUE get_def_obj(const void *def)
Definition: ruby/ext/google/protobuf_c/protobuf.c:50
FieldDescriptor_register
void FieldDescriptor_register(VALUE module)
Definition: defs.c:672
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
FileBuilderContext_register
void FileBuilderContext_register(VALUE module)
Definition: defs.c:1973
upb_ok
bool upb_ok(const upb_status *status)
Definition: php/ext/google/protobuf/upb.c:5573
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57