diff options
Diffstat (limited to 'ruby/ext')
-rw-r--r-- | ruby/ext/google/protobuf_c/defs.c | 10 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/encode_decode.c | 4 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/message.c | 6 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/protobuf.c | 2 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/protobuf.h | 1 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/storage.c | 34 |
6 files changed, 33 insertions, 24 deletions
diff --git a/ruby/ext/google/protobuf_c/defs.c b/ruby/ext/google/protobuf_c/defs.c index d9d2ebac..9fe04503 100644 --- a/ruby/ext/google/protobuf_c/defs.c +++ b/ruby/ext/google/protobuf_c/defs.c @@ -812,7 +812,7 @@ VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) { upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); const char* str = get_str(value); if (!upb_fielddef_hassubdef(self->fielddef)) { - rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef."); + rb_raise(cTypeError, "FieldDescriptor does not have subdef."); } CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status), "Error setting submessage name"); @@ -854,7 +854,7 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) { MessageHeader* msg; TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { - rb_raise(rb_eTypeError, "get method called on wrong message type"); + rb_raise(cTypeError, "get method called on wrong message type"); } return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef); } @@ -872,7 +872,7 @@ VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) { MessageHeader* msg; TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { - rb_raise(rb_eTypeError, "set method called on wrong message type"); + rb_raise(cTypeError, "set method called on wrong message type"); } layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value); return Qnil; @@ -1713,7 +1713,7 @@ static void validate_msgdef(const upb_msgdef* msgdef) { upb_msg_field_next(&it)) { const upb_fielddef* field = upb_msg_iter_field(&it); if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) { - rb_raise(rb_eTypeError, "Required fields are unsupported in proto3."); + rb_raise(cTypeError, "Required fields are unsupported in proto3."); } } } @@ -1723,7 +1723,7 @@ static void validate_enumdef(const upb_enumdef* enumdef) { // value.) const char* lookup = upb_enumdef_iton(enumdef, 0); if (lookup == NULL) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Enum definition does not contain a value for '0'."); } } diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c index 9fa4bfd9..d5842051 100644 --- a/ruby/ext/google/protobuf_c/encode_decode.c +++ b/ruby/ext/google/protobuf_c/encode_decode.c @@ -963,13 +963,15 @@ static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink, if (ary == Qnil) return; + size = NUM2INT(RepeatedField_length(ary)); + if (size == 0 && !emit_defaults) return; + upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); if (upb_fielddef_isprimitive(f)) { sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); } - size = NUM2INT(RepeatedField_length(ary)); for (int i = 0; i < size; i++) { void* memory = RepeatedField_index_native(ary, i); switch (type) { diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index 63a61baf..721c1112 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -256,6 +256,10 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { "Unknown field name '%s' in initialization map entry.", name); } + if (TYPE(val) == T_NIL) { + return 0; + } + if (is_map_field(f)) { VALUE map; @@ -631,7 +635,7 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) { const char* name = upb_enum_iter_name(&it); int32_t value = upb_enum_iter_number(&it); if (name[0] < 'A' || name[0] > 'Z') { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Enum value '%s' does not start with an uppercase letter " "as is required for Ruby constants.", name); diff --git a/ruby/ext/google/protobuf_c/protobuf.c b/ruby/ext/google/protobuf_c/protobuf.c index db696426..fe6bb406 100644 --- a/ruby/ext/google/protobuf_c/protobuf.c +++ b/ruby/ext/google/protobuf_c/protobuf.c @@ -41,6 +41,7 @@ VALUE upb_def_to_ruby_obj_map; VALUE cError; VALUE cParseError; +VALUE cTypeError; void add_def_obj(const void* def, VALUE value) { rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value); @@ -102,6 +103,7 @@ void Init_protobuf_c() { cError = rb_const_get(protobuf, rb_intern("Error")); cParseError = rb_const_get(protobuf, rb_intern("ParseError")); + cTypeError = rb_const_get(protobuf, rb_intern("TypeError")); rb_define_singleton_method(protobuf, "discard_unknown", Google_Protobuf_discard_unknown, 1); diff --git a/ruby/ext/google/protobuf_c/protobuf.h b/ruby/ext/google/protobuf_c/protobuf.h index 5266aa8d..3e5c0520 100644 --- a/ruby/ext/google/protobuf_c/protobuf.h +++ b/ruby/ext/google/protobuf_c/protobuf.h @@ -161,6 +161,7 @@ extern VALUE cBuilder; extern VALUE cError; extern VALUE cParseError; +extern VALUE cTypeError; // We forward-declare all of the Ruby method implementations here because we // sometimes call the methods directly across .c files, rather than going diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 5d842b74..163b2f81 100644 --- a/ruby/ext/google/protobuf_c/storage.c +++ b/ruby/ext/google/protobuf_c/storage.c @@ -96,7 +96,7 @@ static bool is_ruby_num(VALUE value) { void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) { if (!is_ruby_num(val)) { - rb_raise(rb_eTypeError, "Expected number type for integral field."); + rb_raise(cTypeError, "Expected number type for integral field."); } // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper @@ -153,13 +153,13 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, switch (type) { case UPB_TYPE_FLOAT: if (!is_ruby_num(value)) { - rb_raise(rb_eTypeError, "Expected number type for float field."); + rb_raise(cTypeError, "Expected number type for float field."); } DEREF(memory, float) = NUM2DBL(value); break; case UPB_TYPE_DOUBLE: if (!is_ruby_num(value)) { - rb_raise(rb_eTypeError, "Expected number type for double field."); + rb_raise(cTypeError, "Expected number type for double field."); } DEREF(memory, double) = NUM2DBL(value); break; @@ -170,7 +170,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, } else if (value == Qfalse) { val = 0; } else { - rb_raise(rb_eTypeError, "Invalid argument for boolean field."); + rb_raise(cTypeError, "Invalid argument for boolean field."); } DEREF(memory, int8_t) = val; break; @@ -179,7 +179,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, if (CLASS_OF(value) == rb_cSymbol) { value = rb_funcall(value, rb_intern("to_s"), 0); } else if (CLASS_OF(value) != rb_cString) { - rb_raise(rb_eTypeError, "Invalid argument for string field."); + rb_raise(cTypeError, "Invalid argument for string field."); } DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value); @@ -187,7 +187,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, case UPB_TYPE_BYTES: { if (CLASS_OF(value) != rb_cString) { - rb_raise(rb_eTypeError, "Invalid argument for string field."); + rb_raise(cTypeError, "Invalid argument for string field."); } DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value); @@ -197,7 +197,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, if (CLASS_OF(value) == CLASS_OF(Qnil)) { value = Qnil; } else if (CLASS_OF(value) != type_class) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Invalid type %s to assign to submessage field.", rb_class2name(CLASS_OF(value))); } @@ -209,7 +209,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, if (TYPE(value) == T_STRING) { value = rb_funcall(value, rb_intern("to_sym"), 0); } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Expected number or symbol type for enum field."); } if (TYPE(value) == T_SYMBOL) { @@ -598,18 +598,18 @@ static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) || RTYPEDDATA_TYPE(val) != &RepeatedField_type) { - rb_raise(rb_eTypeError, "Expected repeated field array"); + rb_raise(cTypeError, "Expected repeated field array"); } self = ruby_to_RepeatedField(val); if (self->field_type != upb_fielddef_type(field)) { - rb_raise(rb_eTypeError, "Repeated field array has wrong element type"); + rb_raise(cTypeError, "Repeated field array has wrong element type"); } - if (self->field_type == UPB_TYPE_MESSAGE) { + if (self->field_type == UPB_TYPE_MESSAGE) { if (self->field_type_class != Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Repeated field array has wrong message class"); } } @@ -618,7 +618,7 @@ static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { if (self->field_type == UPB_TYPE_ENUM) { if (self->field_type_class != EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Repeated field array has wrong enum class"); } } @@ -631,21 +631,21 @@ static void check_map_field_type(VALUE val, const upb_fielddef* field) { if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) || RTYPEDDATA_TYPE(val) != &Map_type) { - rb_raise(rb_eTypeError, "Expected Map instance"); + rb_raise(cTypeError, "Expected Map instance"); } self = ruby_to_Map(val); if (self->key_type != upb_fielddef_type(key_field)) { - rb_raise(rb_eTypeError, "Map key type does not match field's key type"); + rb_raise(cTypeError, "Map key type does not match field's key type"); } if (self->value_type != upb_fielddef_type(value_field)) { - rb_raise(rb_eTypeError, "Map value type does not match field's value type"); + rb_raise(cTypeError, "Map value type does not match field's value type"); } if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE || upb_fielddef_type(value_field) == UPB_TYPE_ENUM) { if (self->value_type_class != get_def_obj(upb_fielddef_subdef(value_field))) { - rb_raise(rb_eTypeError, + rb_raise(cTypeError, "Map value type has wrong message/enum class"); } } |