| Kevin Clark | 916f353 | 2009-03-20 04:21:39 +0000 | [diff] [blame] | 1 | /** |
| 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 20 | #include <ruby.h> |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 21 | #include <bytes.h> |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 22 | #include <struct.h> |
| 23 | #include <binary_protocol_accelerated.h> |
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 24 | #include <compact_protocol.h> |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 25 | #include <memory_buffer.h> |
| 26 | |
| 27 | // cached classes/modules |
| 28 | VALUE rb_cSet; |
| 29 | VALUE thrift_module; |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 30 | VALUE thrift_bytes_module; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 31 | VALUE thrift_types_module; |
| 32 | |
| 33 | // TType constants |
| 34 | int TTYPE_STOP; |
| 35 | int TTYPE_BOOL; |
| 36 | int TTYPE_BYTE; |
| 37 | int TTYPE_I16; |
| 38 | int TTYPE_I32; |
| 39 | int TTYPE_I64; |
| 40 | int TTYPE_DOUBLE; |
| 41 | int TTYPE_STRING; |
| 42 | int TTYPE_MAP; |
| 43 | int TTYPE_SET; |
| 44 | int TTYPE_LIST; |
| 45 | int TTYPE_STRUCT; |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 46 | int TTYPE_UUID; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 47 | |
| 48 | // method ids |
| 49 | ID validate_method_id; |
| 50 | ID write_struct_begin_method_id; |
| 51 | ID write_struct_end_method_id; |
| 52 | ID write_field_begin_method_id; |
| 53 | ID write_field_end_method_id; |
| 54 | ID write_boolean_method_id; |
| 55 | ID write_byte_method_id; |
| 56 | ID write_i16_method_id; |
| 57 | ID write_i32_method_id; |
| 58 | ID write_i64_method_id; |
| 59 | ID write_double_method_id; |
| 60 | ID write_string_method_id; |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 61 | ID write_uuid_method_id; |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 62 | ID write_binary_method_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 63 | ID write_map_begin_method_id; |
| 64 | ID write_map_end_method_id; |
| 65 | ID write_list_begin_method_id; |
| 66 | ID write_list_end_method_id; |
| 67 | ID write_set_begin_method_id; |
| 68 | ID write_set_end_method_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 69 | ID read_bool_method_id; |
| 70 | ID read_byte_method_id; |
| 71 | ID read_i16_method_id; |
| 72 | ID read_i32_method_id; |
| 73 | ID read_i64_method_id; |
| 74 | ID read_string_method_id; |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 75 | ID read_uuid_method_id; |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 76 | ID read_binary_method_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 77 | ID read_double_method_id; |
| 78 | ID read_map_begin_method_id; |
| 79 | ID read_map_end_method_id; |
| 80 | ID read_list_begin_method_id; |
| 81 | ID read_list_end_method_id; |
| 82 | ID read_set_begin_method_id; |
| 83 | ID read_set_end_method_id; |
| 84 | ID read_struct_begin_method_id; |
| 85 | ID read_struct_end_method_id; |
| 86 | ID read_field_begin_method_id; |
| 87 | ID read_field_end_method_id; |
| 88 | ID keys_method_id; |
| Roger Meier | 4f62326 | 2013-05-05 23:59:25 +0200 | [diff] [blame] | 89 | ID entries_method_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 90 | ID write_field_stop_method_id; |
| 91 | ID skip_method_id; |
| 92 | ID write_method_id; |
| Bryan Duxbury | 5b8b484 | 2009-04-01 20:10:15 +0000 | [diff] [blame] | 93 | ID read_all_method_id; |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 94 | ID read_into_buffer_method_id; |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 95 | ID force_binary_encoding_id; |
| 96 | ID convert_to_utf8_byte_buffer_id; |
| 97 | ID convert_to_string_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 98 | |
| 99 | // constant ids |
| 100 | ID fields_const_id; |
| 101 | ID transport_ivar_id; |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 102 | ID strict_read_ivar_id; |
| 103 | ID strict_write_ivar_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 104 | |
| 105 | // cached symbols |
| 106 | VALUE type_sym; |
| 107 | VALUE name_sym; |
| 108 | VALUE key_sym; |
| 109 | VALUE value_sym; |
| 110 | VALUE element_sym; |
| 111 | VALUE class_sym; |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 112 | VALUE binary_sym; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 113 | VALUE protocol_exception_class; |
| 114 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 115 | // protocol errors |
| 116 | int PROTOERR_UNKNOWN; |
| 117 | int PROTOERR_INVALID_DATA; |
| 118 | int PROTOERR_NEGATIVE_SIZE; |
| 119 | int PROTOERR_SIZE_LIMIT; |
| 120 | int PROTOERR_BAD_VERSION; |
| 121 | int PROTOERR_NOT_IMPLEMENTED; |
| 122 | int PROTOERR_DEPTH_LIMIT; |
| 123 | |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 124 | RUBY_FUNC_EXPORTED void Init_thrift_native(void) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 125 | // cached classes |
| 126 | thrift_module = rb_const_get(rb_cObject, rb_intern("Thrift")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 127 | rb_global_variable(&thrift_module); |
| 128 | |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 129 | thrift_bytes_module = rb_const_get(thrift_module, rb_intern("Bytes")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 130 | rb_global_variable(&thrift_bytes_module); |
| 131 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 132 | thrift_types_module = rb_const_get(thrift_module, rb_intern("Types")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 133 | rb_global_variable(&thrift_types_module); |
| 134 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 135 | rb_cSet = rb_const_get(rb_cObject, rb_intern("Set")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 136 | rb_global_variable(&rb_cSet); |
| 137 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 138 | protocol_exception_class = rb_const_get(thrift_module, rb_intern("ProtocolException")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 139 | rb_global_variable(&protocol_exception_class); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 140 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 141 | // 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 Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 153 | TTYPE_UUID = FIX2INT(rb_const_get(thrift_types_module, rb_intern("UUID"))); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 154 | |
| 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 Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 168 | write_uuid_method_id = rb_intern("write_uuid"); |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 169 | write_binary_method_id = rb_intern("write_binary"); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 170 | 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 176 | 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 Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 182 | read_uuid_method_id = rb_intern("read_uuid"); |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 183 | read_binary_method_id = rb_intern("read_binary"); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 184 | read_double_method_id = rb_intern("read_double"); |
| 185 | read_map_begin_method_id = rb_intern("read_map_begin"); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 186 | read_map_end_method_id = rb_intern("read_map_end"); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 187 | 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 197 | 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 Duxbury | 5b8b484 | 2009-04-01 20:10:15 +0000 | [diff] [blame] | 200 | read_all_method_id = rb_intern("read_all"); |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 201 | read_into_buffer_method_id = rb_intern("read_into_buffer"); |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 202 | 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 Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 205 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 206 | // constant ids |
| 207 | fields_const_id = rb_intern("FIELDS"); |
| 208 | transport_ivar_id = rb_intern("@trans"); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 209 | strict_read_ivar_id = rb_intern("@strict_read"); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 210 | strict_write_ivar_id = rb_intern("@strict_write"); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 211 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 212 | // 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")); |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 219 | binary_sym = ID2SYM(rb_intern("binary")); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 220 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 221 | // 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 Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 230 | 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 238 | Init_struct(); |
| 239 | Init_binary_protocol_accelerated(); |
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 240 | Init_compact_protocol(); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 241 | Init_memory_buffer(); |
| Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 242 | } |