blob: 0c054813d8395810e6b4daef1cea8ac8f1755ce7 [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 Duxburyd815c212009-03-19 18:57:43 +000020#include <ruby.h>
21#include <stdbool.h>
22#include <stdint.h>
Jake Farrellb5a18a12012-10-09 01:10:43 +000023#include <constants.h>
24#include <struct.h>
25#include <macros.h>
26#include <bytes.h>
Bryan Duxburyd815c212009-03-19 18:57:43 +000027
28#define LAST_ID(obj) FIX2INT(rb_ary_pop(rb_ivar_get(obj, last_field_id)))
29#define SET_LAST_ID(obj, val) rb_ary_push(rb_ivar_get(obj, last_field_id), val)
30
31VALUE rb_thrift_compact_proto_native_qmark(VALUE self) {
32 return Qtrue;
33}
34
35static ID last_field_id;
36static ID boolean_field_id;
37static ID bool_value_id;
Bryan Duxburyad0ad822011-06-28 18:46:03 +000038static ID rbuf_ivar_id;
Bryan Duxburyd815c212009-03-19 18:57:43 +000039
40static int VERSION;
41static int VERSION_MASK;
42static int TYPE_MASK;
43static int TYPE_SHIFT_AMOUNT;
44static int PROTOCOL_ID;
45
46static VALUE thrift_compact_protocol_class;
47
48static int CTYPE_BOOLEAN_TRUE = 0x01;
49static int CTYPE_BOOLEAN_FALSE = 0x02;
50static int CTYPE_BYTE = 0x03;
51static int CTYPE_I16 = 0x04;
52static int CTYPE_I32 = 0x05;
53static int CTYPE_I64 = 0x06;
54static int CTYPE_DOUBLE = 0x07;
55static int CTYPE_BINARY = 0x08;
56static int CTYPE_LIST = 0x09;
57static int CTYPE_SET = 0x0A;
58static int CTYPE_MAP = 0x0B;
59static int CTYPE_STRUCT = 0x0C;
60
61VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16);
62
63// TODO: implement this
64static int get_compact_type(VALUE type_value) {
65 int type = FIX2INT(type_value);
66 if (type == TTYPE_BOOL) {
67 return CTYPE_BOOLEAN_TRUE;
68 } else if (type == TTYPE_BYTE) {
69 return CTYPE_BYTE;
70 } else if (type == TTYPE_I16) {
71 return CTYPE_I16;
72 } else if (type == TTYPE_I32) {
73 return CTYPE_I32;
74 } else if (type == TTYPE_I64) {
75 return CTYPE_I64;
76 } else if (type == TTYPE_DOUBLE) {
77 return CTYPE_DOUBLE;
78 } else if (type == TTYPE_STRING) {
79 return CTYPE_BINARY;
80 } else if (type == TTYPE_LIST) {
81 return CTYPE_LIST;
82 } else if (type == TTYPE_SET) {
83 return CTYPE_SET;
84 } else if (type == TTYPE_MAP) {
85 return CTYPE_MAP;
86 } else if (type == TTYPE_STRUCT) {
87 return CTYPE_STRUCT;
88 } else {
89 char str[50];
90 sprintf(str, "don't know what type: %d", type);
Bryan Duxburyfd58c552009-09-01 22:32:50 +000091 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +000092 return 0;
93 }
94}
95
96static void write_byte_direct(VALUE transport, int8_t b) {
97 WRITE(transport, (char*)&b, 1);
98}
99
100static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, VALUE type_override) {
101 int id = FIX2INT(id_value);
102 int last_id = LAST_ID(self);
103 VALUE transport = GET_TRANSPORT(self);
104
105 // if there's a type override, use that.
106 int8_t type_to_write = RTEST(type_override) ? FIX2INT(type_override) : get_compact_type(type);
107 // check if we can use delta encoding for the field id
108 int diff = id - last_id;
109 if (diff > 0 && diff <= 15) {
110 // write them together
111 write_byte_direct(transport, diff << 4 | (type_to_write & 0x0f));
112 } else {
113 // write them separate
114 write_byte_direct(transport, type_to_write & 0x0f);
115 rb_thrift_compact_proto_write_i16(self, id_value);
116 }
117
118 SET_LAST_ID(self, id_value);
119}
120
121static int32_t int_to_zig_zag(int32_t n) {
122 return (n << 1) ^ (n >> 31);
123}
124
125static uint64_t ll_to_zig_zag(int64_t n) {
126 return (n << 1) ^ (n >> 63);
127}
128
129static void write_varint32(VALUE transport, uint32_t n) {
130 while (true) {
131 if ((n & ~0x7F) == 0) {
132 write_byte_direct(transport, n & 0x7f);
133 break;
134 } else {
135 write_byte_direct(transport, (n & 0x7F) | 0x80);
136 n = n >> 7;
137 }
138 }
139}
140
141static void write_varint64(VALUE transport, uint64_t n) {
142 while (true) {
143 if ((n & ~0x7F) == 0) {
144 write_byte_direct(transport, n & 0x7f);
145 break;
146 } else {
147 write_byte_direct(transport, (n & 0x7F) | 0x80);
148 n = n >> 7;
149 }
150 }
151}
152
153static void write_collection_begin(VALUE transport, VALUE elem_type, VALUE size_value) {
154 int size = FIX2INT(size_value);
155 if (size <= 14) {
156 write_byte_direct(transport, size << 4 | get_compact_type(elem_type));
157 } else {
158 write_byte_direct(transport, 0xf0 | get_compact_type(elem_type));
159 write_varint32(transport, size);
160 }
161}
162
163
164//--------------------------------
165// interface writing methods
166//--------------------------------
167
168VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
169VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
170
171VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
172 return Qnil;
173}
174
175VALUE rb_thrift_compact_proto_write_struct_begin(VALUE self, VALUE name) {
176 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
177 return Qnil;
178}
179
180VALUE rb_thrift_compact_proto_write_struct_end(VALUE self) {
181 rb_ary_pop(rb_ivar_get(self, last_field_id));
182 return Qnil;
183}
184
185VALUE rb_thrift_compact_proto_write_field_end(VALUE self) {
186 return Qnil;
187}
188
189VALUE rb_thrift_compact_proto_write_map_end(VALUE self) {
190 return Qnil;
191}
192
193VALUE rb_thrift_compact_proto_write_list_end(VALUE self) {
194 return Qnil;
195}
196
197VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
198 return Qnil;
199}
200
201VALUE rb_thrift_compact_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
202 VALUE transport = GET_TRANSPORT(self);
203 write_byte_direct(transport, PROTOCOL_ID);
204 write_byte_direct(transport, (VERSION & VERSION_MASK) | ((FIX2INT(type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
205 write_varint32(transport, FIX2INT(seqid));
206 rb_thrift_compact_proto_write_string(self, name);
207
208 return Qnil;
209}
210
211VALUE rb_thrift_compact_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
212 if (FIX2INT(type) == TTYPE_BOOL) {
213 // we want to possibly include the value, so we'll wait.
214 rb_ivar_set(self, boolean_field_id, rb_ary_new3(2, type, id));
215 } else {
216 write_field_begin_internal(self, type, id, Qnil);
217 }
218
219 return Qnil;
220}
221
222VALUE rb_thrift_compact_proto_write_field_stop(VALUE self) {
223 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
224 return Qnil;
225}
226
227VALUE rb_thrift_compact_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size_value) {
228 int size = FIX2INT(size_value);
229 VALUE transport = GET_TRANSPORT(self);
230 if (size == 0) {
231 write_byte_direct(transport, 0);
232 } else {
233 write_varint32(transport, size);
234 write_byte_direct(transport, get_compact_type(ktype) << 4 | get_compact_type(vtype));
235 }
236 return Qnil;
237}
238
239VALUE rb_thrift_compact_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
240 write_collection_begin(GET_TRANSPORT(self), etype, size);
241 return Qnil;
242}
243
244VALUE rb_thrift_compact_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
245 write_collection_begin(GET_TRANSPORT(self), etype, size);
246 return Qnil;
247}
248
249VALUE rb_thrift_compact_proto_write_bool(VALUE self, VALUE b) {
250 int8_t type = b == Qtrue ? CTYPE_BOOLEAN_TRUE : CTYPE_BOOLEAN_FALSE;
251 VALUE boolean_field = rb_ivar_get(self, boolean_field_id);
252 if (NIL_P(boolean_field)) {
253 // we're not part of a field, so just write the value.
254 write_byte_direct(GET_TRANSPORT(self), type);
255 } else {
256 // we haven't written the field header yet
257 write_field_begin_internal(self, rb_ary_entry(boolean_field, 0), rb_ary_entry(boolean_field, 1), INT2FIX(type));
258 rb_ivar_set(self, boolean_field_id, Qnil);
259 }
260 return Qnil;
261}
262
263VALUE rb_thrift_compact_proto_write_byte(VALUE self, VALUE byte) {
264 CHECK_NIL(byte);
265 write_byte_direct(GET_TRANSPORT(self), FIX2INT(byte));
266 return Qnil;
267}
268
269VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16) {
270 rb_thrift_compact_proto_write_i32(self, i16);
271 return Qnil;
272}
273
274VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32) {
275 CHECK_NIL(i32);
276 write_varint32(GET_TRANSPORT(self), int_to_zig_zag(NUM2INT(i32)));
277 return Qnil;
278}
279
280VALUE rb_thrift_compact_proto_write_i64(VALUE self, VALUE i64) {
281 CHECK_NIL(i64);
282 write_varint64(GET_TRANSPORT(self), ll_to_zig_zag(NUM2LL(i64)));
283 return Qnil;
284}
285
286VALUE rb_thrift_compact_proto_write_double(VALUE self, VALUE dub) {
287 CHECK_NIL(dub);
288 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
289 union {
290 double f;
291 int64_t l;
292 } transfer;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000293 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000294 char buf[8];
295 buf[0] = transfer.l & 0xff;
296 buf[1] = (transfer.l >> 8) & 0xff;
297 buf[2] = (transfer.l >> 16) & 0xff;
298 buf[3] = (transfer.l >> 24) & 0xff;
299 buf[4] = (transfer.l >> 32) & 0xff;
300 buf[5] = (transfer.l >> 40) & 0xff;
301 buf[6] = (transfer.l >> 48) & 0xff;
302 buf[7] = (transfer.l >> 56) & 0xff;
303 WRITE(GET_TRANSPORT(self), buf, 8);
304 return Qnil;
305}
306
307VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
308 VALUE transport = GET_TRANSPORT(self);
Jake Farrellb5a18a12012-10-09 01:10:43 +0000309 str = convert_to_utf8_byte_buffer(str);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000310 write_varint32(transport, RSTRING_LEN(str));
311 WRITE(transport, RSTRING_PTR(str), RSTRING_LEN(str));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000312 return Qnil;
313}
314
315//---------------------------------------
316// interface reading methods
317//---------------------------------------
318
319#define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
320
321VALUE rb_thrift_compact_proto_read_string(VALUE self);
322VALUE rb_thrift_compact_proto_read_byte(VALUE self);
323VALUE rb_thrift_compact_proto_read_i32(VALUE self);
324VALUE rb_thrift_compact_proto_read_i16(VALUE self);
325
326static int8_t get_ttype(int8_t ctype) {
327 if (ctype == TTYPE_STOP) {
328 return TTYPE_STOP;
329 } else if (ctype == CTYPE_BOOLEAN_TRUE || ctype == CTYPE_BOOLEAN_FALSE) {
330 return TTYPE_BOOL;
331 } else if (ctype == CTYPE_BYTE) {
332 return TTYPE_BYTE;
333 } else if (ctype == CTYPE_I16) {
334 return TTYPE_I16;
335 } else if (ctype == CTYPE_I32) {
336 return TTYPE_I32;
337 } else if (ctype == CTYPE_I64) {
338 return TTYPE_I64;
339 } else if (ctype == CTYPE_DOUBLE) {
340 return TTYPE_DOUBLE;
341 } else if (ctype == CTYPE_BINARY) {
342 return TTYPE_STRING;
343 } else if (ctype == CTYPE_LIST) {
344 return TTYPE_LIST;
345 } else if (ctype == CTYPE_SET) {
346 return TTYPE_SET;
347 } else if (ctype == CTYPE_MAP) {
348 return TTYPE_MAP;
349 } else if (ctype == CTYPE_STRUCT) {
350 return TTYPE_STRUCT;
351 } else {
352 char str[50];
353 sprintf(str, "don't know what type: %d", ctype);
Bryan Duxburyfd58c552009-09-01 22:32:50 +0000354 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000355 return 0;
356 }
357}
358
359static char read_byte_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000360 VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
361 return (char)(FIX2INT(byte));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000362}
363
364static int64_t zig_zag_to_ll(int64_t n) {
365 return (((uint64_t)n) >> 1) ^ -(n & 1);
366}
367
368static int32_t zig_zag_to_int(int32_t n) {
369 return (((uint32_t)n) >> 1) ^ -(n & 1);
370}
371
372static int64_t read_varint64(VALUE self) {
373 int shift = 0;
374 int64_t result = 0;
375 while (true) {
376 int8_t b = read_byte_direct(self);
377 result = result | ((uint64_t)(b & 0x7f) << shift);
378 if ((b & 0x80) != 0x80) {
379 break;
380 }
381 shift += 7;
382 }
383 return result;
384}
385
386static int16_t read_i16(VALUE self) {
387 return zig_zag_to_int((int32_t)read_varint64(self));
388}
389
390static VALUE get_protocol_exception(VALUE code, VALUE message) {
391 VALUE args[2];
392 args[0] = code;
393 args[1] = message;
394 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
395}
396
397VALUE rb_thrift_compact_proto_read_message_end(VALUE self) {
398 return Qnil;
399}
400
401VALUE rb_thrift_compact_proto_read_struct_begin(VALUE self) {
402 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
403 return Qnil;
404}
405
406VALUE rb_thrift_compact_proto_read_struct_end(VALUE self) {
407 rb_ary_pop(rb_ivar_get(self, last_field_id));
408 return Qnil;
409}
410
411VALUE rb_thrift_compact_proto_read_field_end(VALUE self) {
412 return Qnil;
413}
414
415VALUE rb_thrift_compact_proto_read_map_end(VALUE self) {
416 return Qnil;
417}
418
419VALUE rb_thrift_compact_proto_read_list_end(VALUE self) {
420 return Qnil;
421}
422
423VALUE rb_thrift_compact_proto_read_set_end(VALUE self) {
424 return Qnil;
425}
426
427VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
428 int8_t protocol_id = read_byte_direct(self);
429 if (protocol_id != PROTOCOL_ID) {
430 char buf[100];
431 int len = sprintf(buf, "Expected protocol id %d but got %d", PROTOCOL_ID, protocol_id);
432 buf[len] = 0;
433 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
434 }
435
436 int8_t version_and_type = read_byte_direct(self);
437 int8_t version = version_and_type & VERSION_MASK;
438 if (version != VERSION) {
439 char buf[100];
440 int len = sprintf(buf, "Expected version id %d but got %d", version, VERSION);
441 buf[len] = 0;
442 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
443 }
444
445 int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03;
446 int32_t seqid = read_varint64(self);
447 VALUE messageName = rb_thrift_compact_proto_read_string(self);
448 return rb_ary_new3(3, messageName, INT2FIX(type), INT2NUM(seqid));
449}
450
451VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
452 int8_t type = read_byte_direct(self);
453 // if it's a stop, then we can return immediately, as the struct is over.
454 if ((type & 0x0f) == TTYPE_STOP) {
455 return rb_ary_new3(3, Qnil, INT2FIX(0), INT2FIX(0));
456 } else {
457 int field_id = 0;
458
459 // mask off the 4 MSB of the type header. it could contain a field id delta.
460 uint8_t modifier = ((type & 0xf0) >> 4);
461
462 if (modifier == 0) {
463 // not a delta. look ahead for the zigzag varint field id.
Bryan Duxbury09d13c22010-08-11 18:37:25 +0000464 (void) LAST_ID(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000465 field_id = read_i16(self);
466 } else {
467 // has a delta. add the delta to the last read field id.
468 field_id = LAST_ID(self) + modifier;
469 }
470
471 // if this happens to be a boolean field, the value is encoded in the type
472 if (is_bool_type(type)) {
473 // save the boolean value in a special instance variable.
474 rb_ivar_set(self, bool_value_id, (type & 0x0f) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse);
475 }
476
477 // push the new field onto the field stack so we can keep the deltas going.
478 SET_LAST_ID(self, INT2FIX(field_id));
479 return rb_ary_new3(3, Qnil, INT2FIX(get_ttype(type & 0x0f)), INT2FIX(field_id));
480 }
481}
482
483VALUE rb_thrift_compact_proto_read_map_begin(VALUE self) {
484 int32_t size = read_varint64(self);
485 uint8_t key_and_value_type = size == 0 ? 0 : read_byte_direct(self);
486 return rb_ary_new3(3, INT2FIX(get_ttype(key_and_value_type >> 4)), INT2FIX(get_ttype(key_and_value_type & 0xf)), INT2FIX(size));
487}
488
489VALUE rb_thrift_compact_proto_read_list_begin(VALUE self) {
490 uint8_t size_and_type = read_byte_direct(self);
491 int32_t size = (size_and_type >> 4) & 0x0f;
492 if (size == 15) {
493 size = read_varint64(self);
494 }
495 uint8_t type = get_ttype(size_and_type & 0x0f);
496 return rb_ary_new3(2, INT2FIX(type), INT2FIX(size));
497}
498
499VALUE rb_thrift_compact_proto_read_set_begin(VALUE self) {
500 return rb_thrift_compact_proto_read_list_begin(self);
501}
502
503VALUE rb_thrift_compact_proto_read_bool(VALUE self) {
504 VALUE bool_value = rb_ivar_get(self, bool_value_id);
505 if (NIL_P(bool_value)) {
506 return read_byte_direct(self) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse;
507 } else {
508 rb_ivar_set(self, bool_value_id, Qnil);
509 return bool_value;
510 }
511}
512
513VALUE rb_thrift_compact_proto_read_byte(VALUE self) {
514 return INT2FIX(read_byte_direct(self));
515}
516
517VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
518 return INT2FIX(read_i16(self));
519}
520
521VALUE rb_thrift_compact_proto_read_i32(VALUE self) {
522 return INT2NUM(zig_zag_to_int(read_varint64(self)));
523}
524
525VALUE rb_thrift_compact_proto_read_i64(VALUE self) {
526 return LL2NUM(zig_zag_to_ll(read_varint64(self)));
527}
528
529VALUE rb_thrift_compact_proto_read_double(VALUE self) {
530 union {
531 double f;
532 int64_t l;
533 } transfer;
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000534 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
535 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
536 uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[0]))
537 | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 8)
538 | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 16)
539 | (((uint8_t)(RSTRING_PTR(rbuf)[3])) << 24);
540 uint64_t hi = (((uint8_t)(RSTRING_PTR(rbuf)[4])))
541 | (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 8)
542 | (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 16)
543 | (((uint8_t)(RSTRING_PTR(rbuf)[7])) << 24);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000544 transfer.l = (hi << 32) | lo;
545
546 return rb_float_new(transfer.f);
547}
548
549VALUE rb_thrift_compact_proto_read_string(VALUE self) {
550 int64_t size = read_varint64(self);
Jake Farrellb5a18a12012-10-09 01:10:43 +0000551 VALUE buffer = READ(self, size);
552 return convert_to_string(buffer);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000553}
554
555static void Init_constants() {
556 thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
557
558 VERSION = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
559 VERSION_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
560 TYPE_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
561 TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
562 PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
563
564 last_field_id = rb_intern("@last_field");
565 boolean_field_id = rb_intern("@boolean_field");
566 bool_value_id = rb_intern("@bool_value");
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000567 rbuf_ivar_id = rb_intern("@rbuf");
Bryan Duxburyd815c212009-03-19 18:57:43 +0000568}
569
570static void Init_rb_methods() {
571 rb_define_method(thrift_compact_protocol_class, "native?", rb_thrift_compact_proto_native_qmark, 0);
572
573 rb_define_method(thrift_compact_protocol_class, "write_message_begin", rb_thrift_compact_proto_write_message_begin, 3);
574 rb_define_method(thrift_compact_protocol_class, "write_field_begin", rb_thrift_compact_proto_write_field_begin, 3);
575 rb_define_method(thrift_compact_protocol_class, "write_field_stop", rb_thrift_compact_proto_write_field_stop, 0);
576 rb_define_method(thrift_compact_protocol_class, "write_map_begin", rb_thrift_compact_proto_write_map_begin, 3);
577 rb_define_method(thrift_compact_protocol_class, "write_list_begin", rb_thrift_compact_proto_write_list_begin, 2);
578 rb_define_method(thrift_compact_protocol_class, "write_set_begin", rb_thrift_compact_proto_write_set_begin, 2);
579 rb_define_method(thrift_compact_protocol_class, "write_byte", rb_thrift_compact_proto_write_byte, 1);
580 rb_define_method(thrift_compact_protocol_class, "write_bool", rb_thrift_compact_proto_write_bool, 1);
581 rb_define_method(thrift_compact_protocol_class, "write_i16", rb_thrift_compact_proto_write_i16, 1);
582 rb_define_method(thrift_compact_protocol_class, "write_i32", rb_thrift_compact_proto_write_i32, 1);
583 rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
584 rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
585 rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
586
587 rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
588 rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
589 rb_define_method(thrift_compact_protocol_class, "write_struct_end", rb_thrift_compact_proto_write_struct_end, 0);
590 rb_define_method(thrift_compact_protocol_class, "write_field_end", rb_thrift_compact_proto_write_field_end, 0);
591 rb_define_method(thrift_compact_protocol_class, "write_map_end", rb_thrift_compact_proto_write_map_end, 0);
592 rb_define_method(thrift_compact_protocol_class, "write_list_end", rb_thrift_compact_proto_write_list_end, 0);
593 rb_define_method(thrift_compact_protocol_class, "write_set_end", rb_thrift_compact_proto_write_set_end, 0);
594
595
596 rb_define_method(thrift_compact_protocol_class, "read_message_begin", rb_thrift_compact_proto_read_message_begin, 0);
597 rb_define_method(thrift_compact_protocol_class, "read_field_begin", rb_thrift_compact_proto_read_field_begin, 0);
598 rb_define_method(thrift_compact_protocol_class, "read_map_begin", rb_thrift_compact_proto_read_map_begin, 0);
599 rb_define_method(thrift_compact_protocol_class, "read_list_begin", rb_thrift_compact_proto_read_list_begin, 0);
600 rb_define_method(thrift_compact_protocol_class, "read_set_begin", rb_thrift_compact_proto_read_set_begin, 0);
601 rb_define_method(thrift_compact_protocol_class, "read_byte", rb_thrift_compact_proto_read_byte, 0);
602 rb_define_method(thrift_compact_protocol_class, "read_bool", rb_thrift_compact_proto_read_bool, 0);
603 rb_define_method(thrift_compact_protocol_class, "read_i16", rb_thrift_compact_proto_read_i16, 0);
604 rb_define_method(thrift_compact_protocol_class, "read_i32", rb_thrift_compact_proto_read_i32, 0);
605 rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
606 rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
607 rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
608
609 rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
610 rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);
611 rb_define_method(thrift_compact_protocol_class, "read_struct_end", rb_thrift_compact_proto_read_struct_end, 0);
612 rb_define_method(thrift_compact_protocol_class, "read_field_end", rb_thrift_compact_proto_read_field_end, 0);
613 rb_define_method(thrift_compact_protocol_class, "read_map_end", rb_thrift_compact_proto_read_map_end, 0);
614 rb_define_method(thrift_compact_protocol_class, "read_list_end", rb_thrift_compact_proto_read_list_end, 0);
615 rb_define_method(thrift_compact_protocol_class, "read_set_end", rb_thrift_compact_proto_read_set_end, 0);
616}
617
Bryan Duxburyd815c212009-03-19 18:57:43 +0000618void Init_compact_protocol() {
619 Init_constants();
620 Init_rb_methods();
Bryan Duxburyd815c212009-03-19 18:57:43 +0000621}