diff --git a/objectivec/GPBDescriptor_PackagePrivate.h b/objectivec/GPBDescriptor_PackagePrivate.h index c9c690e7fe..1a9ddce46f 100644 --- a/objectivec/GPBDescriptor_PackagePrivate.h +++ b/objectivec/GPBDescriptor_PackagePrivate.h @@ -300,10 +300,6 @@ GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) { return (description->options & GPBExtensionPacked) != 0; } -GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) { - return (description->options & GPBExtensionSetWireFormat) != 0; -} - // Helper for compile time assets. #ifndef GPBInternalCompileAssert #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg) diff --git a/objectivec/GPBExtensionInternals.m b/objectivec/GPBExtensionInternals.m index 1c697f21e0..f9b05c68bf 100644 --- a/objectivec/GPBExtensionInternals.m +++ b/objectivec/GPBExtensionInternals.m @@ -97,12 +97,7 @@ static size_t ComputeSerializedSizeIncludingTagOfObject(GPBExtensionDescription FIELD_CASE2(Bytes) FIELD_CASE2(String) FIELD_CASE2(Group) - case GPBDataTypeMessage: - if (GPBExtensionIsWireFormat(description)) { - return GPBComputeMessageSetExtensionSize(description->fieldNumber, object); - } else { - return GPBComputeMessageSize(description->fieldNumber, object); - } + FIELD_CASE2(Message) } #undef FIELD_CASE #undef FIELD_CASE2 @@ -160,13 +155,7 @@ static void WriteObjectIncludingTagToCodedOutputStream(id object, FIELD_CASE2(Bytes) FIELD_CASE2(String) FIELD_CASE2(Group) - case GPBDataTypeMessage: - if (GPBExtensionIsWireFormat(description)) { - [output writeMessageSetExtension:description->fieldNumber value:object]; - } else { - [output writeMessage:description->fieldNumber value:object]; - } - return; + FIELD_CASE2(Message) } #undef FIELD_CASE #undef FIELD_CASE2 diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index fd6a029956..9d1e2b01fe 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -894,38 +894,30 @@ static void DecodeSingleValueFromInputStream(GPBExtensionDescriptor *extension, nsValue = [[NSNumber alloc] initWithInt:val]; } else { AddUnknownFieldVarint32(messageToGetExtension, extension->description_->fieldNumber, val); - nsValue = nil; + return; } break; } - case GPBDataTypeGroup: + case GPBDataTypeGroup: { + [input readGroup:description->fieldNumber + message:targetMessage + extensionRegistry:extensionRegistry]; + // Nothing to add below since the caller provided the message (and added it). + return; + } case GPBDataTypeMessage: { - if (description->dataType == GPBDataTypeGroup) { - [input readGroup:description->fieldNumber - message:targetMessage - extensionRegistry:extensionRegistry]; - } else { -// description->dataType == GPBDataTypeMessage -#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) - NSCAssert(!GPBExtensionIsWireFormat(description), - @"Internal error: got a MessageSet extension when not expected."); -#endif - [input readMessage:targetMessage extensionRegistry:extensionRegistry]; - } + [input readMessage:targetMessage extensionRegistry:extensionRegistry]; // Nothing to add below since the caller provided the message (and added it). - nsValue = nil; - break; + return; } } // switch - if (nsValue) { - if (isRepeated) { - [messageToGetExtension addExtension:extension value:nsValue]; - } else { - [messageToGetExtension setExtension:extension value:nsValue]; - } - [nsValue release]; + if (isRepeated) { + [messageToGetExtension addExtension:extension value:nsValue]; + } else { + [messageToGetExtension setExtension:extension value:nsValue]; } + [nsValue release]; } static void ExtensionMergeFromInputStream(GPBExtensionDescriptor *extension, BOOL isPackedOnStream, @@ -1576,24 +1568,41 @@ void GPBClearMessageAutocreator(GPBMessage *self) { - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output { GPBDescriptor *descriptor = [self descriptor]; + BOOL isMessageSetWireFormat = descriptor.isWireFormat; NSArray *fieldsArray = descriptor->fields_; NSUInteger fieldCount = fieldsArray.count; - const GPBExtensionRange *extensionRanges = descriptor.extensionRanges; - NSUInteger extensionRangesCount = descriptor.extensionRangesCount; NSArray *sortedExtensions = [[extensionMap_ allKeys] sortedArrayUsingSelector:@selector(compareByFieldNumber:)]; - for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) { - if (i == fieldCount) { - [self writeExtensionsToCodedOutputStream:output - range:extensionRanges[j++] - sortedExtensions:sortedExtensions]; - } else if (j == extensionRangesCount || - GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) { - [self writeField:fieldsArray[i++] toCodedOutputStream:output]; - } else { - [self writeExtensionsToCodedOutputStream:output - range:extensionRanges[j++] - sortedExtensions:sortedExtensions]; + if (isMessageSetWireFormat) { +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) + NSAssert(fieldCount == 0, @"MessageSet wire format messages must have no fields."); +#endif + for (GPBExtensionDescriptor *extension in sortedExtensions) { +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) + NSAssert(extension.dataType == GPBDataTypeMessage, + @"Internal Error: MessageSet extension must be a message field."); + NSAssert(!GPBExtensionIsRepeated(extension->description_), + @"Internal Error: MessageSet extension can't be repeated."); +#endif + id value = [extensionMap_ objectForKey:extension]; + [output writeMessageSetExtension:(int32_t)extension.fieldNumber value:value]; + } + } else { + const GPBExtensionRange *extensionRanges = descriptor.extensionRanges; + NSUInteger extensionRangesCount = descriptor.extensionRangesCount; + for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) { + if (i == fieldCount) { + [self writeExtensionsToCodedOutputStream:output + range:extensionRanges[j++] + sortedExtensions:sortedExtensions]; + } else if (j == extensionRangesCount || + GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) { + [self writeField:fieldsArray[i++] toCodedOutputStream:output]; + } else { + [self writeExtensionsToCodedOutputStream:output + range:extensionRanges[j++] + sortedExtensions:sortedExtensions]; + } } } if (unknownFieldData_) { @@ -2359,8 +2368,6 @@ void GPBClearMessageAutocreator(GPBMessage *self) { #if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) NSAssert(extension.dataType == GPBDataTypeMessage, @"Internal Error: MessageSet extension must be a message field."); - NSAssert(GPBExtensionIsWireFormat(extension->description_), - @"Internal Error: MessageSet extension must have message_set_wire_format set."); NSAssert(!GPBExtensionIsRepeated(extension->description_), @"Internal Error: MessageSet extension can't be repeated."); #endif @@ -3280,9 +3287,22 @@ static void MergeRepeatedNotPackedFieldFromCodedInputStream( result += [unknownFieldData_ length]; // Add any extensions. - for (GPBExtensionDescriptor *extension in extensionMap_) { - id value = [extensionMap_ objectForKey:extension]; - result += GPBComputeExtensionSerializedSizeIncludingTag(extension, value); + if (descriptor.isWireFormat) { + for (GPBExtensionDescriptor *extension in extensionMap_) { +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) + NSAssert(extension.dataType == GPBDataTypeMessage, + @"Internal Error: MessageSet extension must be a message field."); + NSAssert(!GPBExtensionIsRepeated(extension->description_), + @"Internal Error: MessageSet extension can't be repeated."); +#endif + id value = [extensionMap_ objectForKey:extension]; + result += GPBComputeMessageSetExtensionSize((int32_t)extension.fieldNumber, value); + } + } else { + for (GPBExtensionDescriptor *extension in extensionMap_) { + id value = [extensionMap_ objectForKey:extension]; + result += GPBComputeExtensionSerializedSizeIncludingTag(extension, value); + } } return result;