From ccae88473962261d28d1ef56dd05637ca1445df6 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Fri, 31 Jul 2009 18:47:09 +0000 Subject: [PATCH] THRIFT-498. rb: Compact and Binary native protocols, used at the same time, can cause some issues Disable native method table usage. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@799693 13f79535-47bb-0310-9956-ffa450edef68 --- lib/rb/ext/struct.c | 91 +++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c index fee285e9..31c62d53 100644 --- a/lib/rb/ext/struct.c +++ b/lib/rb/ext/struct.c @@ -45,28 +45,28 @@ strlcpy (char *dst, const char *src, size_t dst_sz) static native_proto_method_table *mt; static native_proto_method_table *default_mt; -static VALUE last_proto_class = Qnil; +// static VALUE last_proto_class = Qnil; #define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET) #define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id) -static void set_native_proto_function_pointers(VALUE protocol) { - VALUE method_table_object = rb_const_get(CLASS_OF(protocol), rb_intern("@native_method_table")); - // TODO: check nil? - Data_Get_Struct(method_table_object, native_proto_method_table, mt); -} - -static void check_native_proto_method_table(VALUE protocol) { - VALUE protoclass = CLASS_OF(protocol); - if (protoclass != last_proto_class) { - last_proto_class = protoclass; - if (rb_funcall(protocol, native_qmark_method_id, 0) == Qtrue) { - set_native_proto_function_pointers(protocol); - } else { - mt = default_mt; - } - } -} +// static void set_native_proto_function_pointers(VALUE protocol) { +// VALUE method_table_object = rb_const_get(CLASS_OF(protocol), rb_intern("@native_method_table")); +// // TODO: check nil? +// Data_Get_Struct(method_table_object, native_proto_method_table, mt); +// } + +// static void check_native_proto_method_table(VALUE protocol) { +// VALUE protoclass = CLASS_OF(protocol); +// if (protoclass != last_proto_class) { +// last_proto_class = protoclass; +// if (rb_funcall(protocol, native_qmark_method_id, 0) == Qtrue) { +// set_native_proto_function_pointers(protocol); +// } else { +// mt = default_mt; +// } +// } +// } //------------------------------------------- // Writing section @@ -416,16 +416,16 @@ static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { // call validate rb_funcall(self, validate_method_id, 0); - check_native_proto_method_table(protocol); - + // check_native_proto_method_table(protocol); + // write struct begin mt->write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); - + // iterate through all the fields here VALUE struct_fields = STRUCT_FIELDS(self); VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0); VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0); - + int i = 0; for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) { VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i); @@ -444,12 +444,12 @@ static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { mt->write_field_end(protocol); } } - + mt->write_field_stop(protocol); - + // write struct end mt->write_struct_end(protocol); - + return Qnil; } @@ -470,7 +470,7 @@ static void set_field_value(VALUE obj, VALUE field_name, VALUE value) { static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { VALUE result = Qnil; - + if (ttype == TTYPE_BOOL) { result = mt->read_bool(protocol); } else if (ttype == TTYPE_BYTE) { @@ -496,21 +496,21 @@ static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); - + VALUE key_info = rb_hash_aref(field_info, key_sym); VALUE value_info = rb_hash_aref(field_info, value_sym); result = rb_hash_new(); - + for (i = 0; i < num_entries; ++i) { VALUE key, val; - + key = read_anything(protocol, key_ttype, key_info); val = read_anything(protocol, value_ttype, value_info); - + rb_hash_aset(result, key, val); } - + mt->read_map_end(protocol); } else if (ttype == TTYPE_LIST) { int i; @@ -519,7 +519,7 @@ static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); result = rb_ary_new2(num_elements); - + for (i = 0; i < num_elements; ++i) { rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } @@ -534,36 +534,36 @@ static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); items = rb_ary_new2(num_elements); - + for (i = 0; i < num_elements; ++i) { rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } - + mt->read_set_end(protocol); - + result = rb_class_new_instance(1, &items, rb_cSet); } else { rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); } - + return result; } static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) { - check_native_proto_method_table(protocol); - + // check_native_proto_method_table(protocol); + // read struct begin mt->read_struct_begin(protocol); VALUE struct_fields = STRUCT_FIELDS(self); - + // read each field while (true) { VALUE field_header = mt->read_field_begin(protocol); VALUE field_type_value = rb_ary_entry(field_header, 1); int field_type = FIX2INT(field_type_value); - + if (field_type == TTYPE_STOP) { break; } @@ -583,23 +583,24 @@ static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) { } else { rb_funcall(protocol, skip_method_id, 1, field_type_value); } - + // read field end mt->read_field_end(protocol); } - + // read struct end mt->read_struct_end(protocol); - + return Qnil; } void Init_struct() { VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct")); - + rb_define_method(struct_module, "write", rb_thrift_struct_write, 1); rb_define_method(struct_module, "read", rb_thrift_struct_read, 1); - + set_default_proto_function_pointers(); + mt = default_mt; } -- 2.17.1