blob: cd4faa93e5ffbb974af6b20dca8fcd3731b47ad3 [file] [log] [blame]
Kevin Clark916f3532009-03-20 04:21:39 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Bryan Duxburyc0166282009-02-02 00:48:17 +000020#include <ruby.h>
Jake Farrellb5a18a12012-10-09 01:10:43 +000021#include <bytes.h>
Bryan Duxburyc0166282009-02-02 00:48:17 +000022#include <struct.h>
23#include <binary_protocol_accelerated.h>
Bryan Duxburyd815c212009-03-19 18:57:43 +000024#include <compact_protocol.h>
Bryan Duxburyc0166282009-02-02 00:48:17 +000025#include <memory_buffer.h>
26
27// cached classes/modules
28VALUE rb_cSet;
29VALUE thrift_module;
Jake Farrellb5a18a12012-10-09 01:10:43 +000030VALUE thrift_bytes_module;
Bryan Duxburyc0166282009-02-02 00:48:17 +000031VALUE thrift_types_module;
32
33// TType constants
34int TTYPE_STOP;
35int TTYPE_BOOL;
36int TTYPE_BYTE;
37int TTYPE_I16;
38int TTYPE_I32;
39int TTYPE_I64;
40int TTYPE_DOUBLE;
41int TTYPE_STRING;
42int TTYPE_MAP;
43int TTYPE_SET;
44int TTYPE_LIST;
45int TTYPE_STRUCT;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050046int TTYPE_UUID;
Bryan Duxburyc0166282009-02-02 00:48:17 +000047
48// method ids
49ID validate_method_id;
50ID write_struct_begin_method_id;
51ID write_struct_end_method_id;
52ID write_field_begin_method_id;
53ID write_field_end_method_id;
54ID write_boolean_method_id;
55ID write_byte_method_id;
56ID write_i16_method_id;
57ID write_i32_method_id;
58ID write_i64_method_id;
59ID write_double_method_id;
60ID write_string_method_id;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050061ID write_uuid_method_id;
henrique8a2bab32014-07-16 20:10:57 +020062ID write_binary_method_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000063ID write_map_begin_method_id;
64ID write_map_end_method_id;
65ID write_list_begin_method_id;
66ID write_list_end_method_id;
67ID write_set_begin_method_id;
68ID write_set_end_method_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000069ID read_bool_method_id;
70ID read_byte_method_id;
71ID read_i16_method_id;
72ID read_i32_method_id;
73ID read_i64_method_id;
74ID read_string_method_id;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050075ID read_uuid_method_id;
henrique8a2bab32014-07-16 20:10:57 +020076ID read_binary_method_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000077ID read_double_method_id;
78ID read_map_begin_method_id;
79ID read_map_end_method_id;
80ID read_list_begin_method_id;
81ID read_list_end_method_id;
82ID read_set_begin_method_id;
83ID read_set_end_method_id;
84ID read_struct_begin_method_id;
85ID read_struct_end_method_id;
86ID read_field_begin_method_id;
87ID read_field_end_method_id;
88ID keys_method_id;
Roger Meier4f623262013-05-05 23:59:25 +020089ID entries_method_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000090ID write_field_stop_method_id;
91ID skip_method_id;
92ID write_method_id;
Bryan Duxbury5b8b4842009-04-01 20:10:15 +000093ID read_all_method_id;
Bryan Duxburyad0ad822011-06-28 18:46:03 +000094ID read_into_buffer_method_id;
Jake Farrellb5a18a12012-10-09 01:10:43 +000095ID force_binary_encoding_id;
96ID convert_to_utf8_byte_buffer_id;
97ID convert_to_string_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000098
99// constant ids
100ID fields_const_id;
101ID transport_ivar_id;
Kevin Clarkead33822009-02-04 22:43:59 +0000102ID strict_read_ivar_id;
103ID strict_write_ivar_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000104
105// cached symbols
106VALUE type_sym;
107VALUE name_sym;
108VALUE key_sym;
109VALUE value_sym;
110VALUE element_sym;
111VALUE class_sym;
henrique8a2bab32014-07-16 20:10:57 +0200112VALUE binary_sym;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000113VALUE protocol_exception_class;
114
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500115// protocol errors
116int PROTOERR_UNKNOWN;
117int PROTOERR_INVALID_DATA;
118int PROTOERR_NEGATIVE_SIZE;
119int PROTOERR_SIZE_LIMIT;
120int PROTOERR_BAD_VERSION;
121int PROTOERR_NOT_IMPLEMENTED;
122int PROTOERR_DEPTH_LIMIT;
123
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500124RUBY_FUNC_EXPORTED void Init_thrift_native(void) {
Bryan Duxburyc0166282009-02-02 00:48:17 +0000125 // cached classes
126 thrift_module = rb_const_get(rb_cObject, rb_intern("Thrift"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530127 rb_global_variable(&thrift_module);
128
Jake Farrellb5a18a12012-10-09 01:10:43 +0000129 thrift_bytes_module = rb_const_get(thrift_module, rb_intern("Bytes"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530130 rb_global_variable(&thrift_bytes_module);
131
Bryan Duxburyc0166282009-02-02 00:48:17 +0000132 thrift_types_module = rb_const_get(thrift_module, rb_intern("Types"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530133 rb_global_variable(&thrift_types_module);
134
Bryan Duxburyc0166282009-02-02 00:48:17 +0000135 rb_cSet = rb_const_get(rb_cObject, rb_intern("Set"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530136 rb_global_variable(&rb_cSet);
137
Bryan Duxburyc0166282009-02-02 00:48:17 +0000138 protocol_exception_class = rb_const_get(thrift_module, rb_intern("ProtocolException"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530139 rb_global_variable(&protocol_exception_class);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000140
Bryan Duxburyc0166282009-02-02 00:48:17 +0000141 // Init ttype constants
142 TTYPE_BOOL = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BOOL")));
143 TTYPE_BYTE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BYTE")));
144 TTYPE_I16 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I16")));
145 TTYPE_I32 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I32")));
146 TTYPE_I64 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I64")));
147 TTYPE_DOUBLE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("DOUBLE")));
148 TTYPE_STRING = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRING")));
149 TTYPE_MAP = FIX2INT(rb_const_get(thrift_types_module, rb_intern("MAP")));
150 TTYPE_SET = FIX2INT(rb_const_get(thrift_types_module, rb_intern("SET")));
151 TTYPE_LIST = FIX2INT(rb_const_get(thrift_types_module, rb_intern("LIST")));
152 TTYPE_STRUCT = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRUCT")));
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500153 TTYPE_UUID = FIX2INT(rb_const_get(thrift_types_module, rb_intern("UUID")));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000154
155 // method ids
156 validate_method_id = rb_intern("validate");
157 write_struct_begin_method_id = rb_intern("write_struct_begin");
158 write_struct_end_method_id = rb_intern("write_struct_end");
159 write_field_begin_method_id = rb_intern("write_field_begin");
160 write_field_end_method_id = rb_intern("write_field_end");
161 write_boolean_method_id = rb_intern("write_bool");
162 write_byte_method_id = rb_intern("write_byte");
163 write_i16_method_id = rb_intern("write_i16");
164 write_i32_method_id = rb_intern("write_i32");
165 write_i64_method_id = rb_intern("write_i64");
166 write_double_method_id = rb_intern("write_double");
167 write_string_method_id = rb_intern("write_string");
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500168 write_uuid_method_id = rb_intern("write_uuid");
henrique8a2bab32014-07-16 20:10:57 +0200169 write_binary_method_id = rb_intern("write_binary");
Bryan Duxburyc0166282009-02-02 00:48:17 +0000170 write_map_begin_method_id = rb_intern("write_map_begin");
171 write_map_end_method_id = rb_intern("write_map_end");
172 write_list_begin_method_id = rb_intern("write_list_begin");
173 write_list_end_method_id = rb_intern("write_list_end");
174 write_set_begin_method_id = rb_intern("write_set_begin");
175 write_set_end_method_id = rb_intern("write_set_end");
Bryan Duxburyc0166282009-02-02 00:48:17 +0000176 read_bool_method_id = rb_intern("read_bool");
177 read_byte_method_id = rb_intern("read_byte");
178 read_i16_method_id = rb_intern("read_i16");
179 read_i32_method_id = rb_intern("read_i32");
180 read_i64_method_id = rb_intern("read_i64");
181 read_string_method_id = rb_intern("read_string");
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500182 read_uuid_method_id = rb_intern("read_uuid");
henrique8a2bab32014-07-16 20:10:57 +0200183 read_binary_method_id = rb_intern("read_binary");
Bryan Duxburyc0166282009-02-02 00:48:17 +0000184 read_double_method_id = rb_intern("read_double");
185 read_map_begin_method_id = rb_intern("read_map_begin");
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500186 read_map_end_method_id = rb_intern("read_map_end");
Bryan Duxburyc0166282009-02-02 00:48:17 +0000187 read_list_begin_method_id = rb_intern("read_list_begin");
188 read_list_end_method_id = rb_intern("read_list_end");
189 read_set_begin_method_id = rb_intern("read_set_begin");
190 read_set_end_method_id = rb_intern("read_set_end");
191 read_struct_begin_method_id = rb_intern("read_struct_begin");
192 read_struct_end_method_id = rb_intern("read_struct_end");
193 read_field_begin_method_id = rb_intern("read_field_begin");
194 read_field_end_method_id = rb_intern("read_field_end");
195 keys_method_id = rb_intern("keys");
196 entries_method_id = rb_intern("entries");
Bryan Duxburyc0166282009-02-02 00:48:17 +0000197 write_field_stop_method_id = rb_intern("write_field_stop");
198 skip_method_id = rb_intern("skip");
199 write_method_id = rb_intern("write");
Bryan Duxbury5b8b4842009-04-01 20:10:15 +0000200 read_all_method_id = rb_intern("read_all");
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000201 read_into_buffer_method_id = rb_intern("read_into_buffer");
Jake Farrellb5a18a12012-10-09 01:10:43 +0000202 force_binary_encoding_id = rb_intern("force_binary_encoding");
203 convert_to_utf8_byte_buffer_id = rb_intern("convert_to_utf8_byte_buffer");
204 convert_to_string_id = rb_intern("convert_to_string");
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000205
Bryan Duxburyc0166282009-02-02 00:48:17 +0000206 // constant ids
207 fields_const_id = rb_intern("FIELDS");
208 transport_ivar_id = rb_intern("@trans");
Kevin Clarkead33822009-02-04 22:43:59 +0000209 strict_read_ivar_id = rb_intern("@strict_read");
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500210 strict_write_ivar_id = rb_intern("@strict_write");
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000211
Bryan Duxburyc0166282009-02-02 00:48:17 +0000212 // cached symbols
213 type_sym = ID2SYM(rb_intern("type"));
214 name_sym = ID2SYM(rb_intern("name"));
215 key_sym = ID2SYM(rb_intern("key"));
216 value_sym = ID2SYM(rb_intern("value"));
217 element_sym = ID2SYM(rb_intern("element"));
218 class_sym = ID2SYM(rb_intern("class"));
henrique8a2bab32014-07-16 20:10:57 +0200219 binary_sym = ID2SYM(rb_intern("binary"));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000220
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500221 // protocol errors
222 PROTOERR_UNKNOWN = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("UNKNOWN")));
223 PROTOERR_INVALID_DATA = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("INVALID_DATA")));
224 PROTOERR_NEGATIVE_SIZE = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("NEGATIVE_SIZE")));
225 PROTOERR_SIZE_LIMIT = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("SIZE_LIMIT")));
226 PROTOERR_BAD_VERSION = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("BAD_VERSION")));
227 PROTOERR_NOT_IMPLEMENTED = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("NOT_IMPLEMENTED")));
228 PROTOERR_DEPTH_LIMIT = FIX2INT(rb_const_get(protocol_exception_class, rb_intern("DEPTH_LIMIT")));
229
Stan Hucc70b4e2021-03-11 03:49:57 +0530230 rb_global_variable(&type_sym);
231 rb_global_variable(&name_sym);
232 rb_global_variable(&key_sym);
233 rb_global_variable(&value_sym);
234 rb_global_variable(&element_sym);
235 rb_global_variable(&class_sym);
236 rb_global_variable(&binary_sym);
237
Bryan Duxburyc0166282009-02-02 00:48:17 +0000238 Init_struct();
239 Init_binary_protocol_accelerated();
Bryan Duxburyd815c212009-03-19 18:57:43 +0000240 Init_compact_protocol();
Bryan Duxburyc0166282009-02-02 00:48:17 +0000241 Init_memory_buffer();
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000242}