From 32c26414d469ff99c81c32cd7fac864e069f6544 Mon Sep 17 00:00:00 2001 From: dsavvinov Date: Thu, 11 Aug 2016 13:41:34 +0300 Subject: [PATCH] Protobuf: fixed a bug in estimating size of packed arrays element (now tags are properly dropped). Added corresponding functions in runtime --- .../kotlin/src/kotlin_field_generator.cc | 4 +- .../kotlin/src/kotlin_file_generator.cc | 4 ++ .../src/main/kotlin/CodedInputStream.kt | 1 + .../src/main/kotlin/CodedOutputStream.kt | 1 + .../src/main/kotlin/KotlinInputStream.kt | 1 + .../src/main/kotlin/KotlinOutputStream.kt | 1 + proto/runtime/src/main/kotlin/WireFormat.kt | 55 ++++++++++++++++++- proto/runtime/src/main/kotlin/WireType.kt | 1 + 8 files changed, 64 insertions(+), 4 deletions(-) diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc index 16a5bd1e72b..37ce6198b38 100644 --- a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc @@ -447,7 +447,9 @@ void FieldGenerator::generateSizeEstimationCode(io::Printer *printer, string var // Finally, get size of all primitive types trivially via call to WireFormat in runtime else { vars["kotlinSuffix"] = getKotlinFunctionSuffix(); - printer->Print(vars, "$varName$ += WireFormat.get$kotlinSuffix$Size($fieldNumber$, $fieldName$)\n"); + vars["noTag"] = noTag ? "NoTag" : ""; + vars["fn"] = noTag ? "" : std::to_string(getFieldNumber()) + ", "; + printer->Print(vars, "$varName$ += WireFormat.get$kotlinSuffix$Size$noTag$($fn$$fieldName$)\n"); } if (isField) { diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_file_generator.cc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_file_generator.cc index 91ad3f5d58e..42ab1c7c758 100644 --- a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_file_generator.cc +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_file_generator.cc @@ -44,6 +44,10 @@ bool FileGenerator::Generate(const FileDescriptor *file, const string ¶meter classes.push_back(cgen); } + // generate package directive + // XXX: stub here, resolve options properly! + printer.Print("package main.kotlin\n"); + // Generate code and clean up generateCode(&printer, classes); diff --git a/proto/runtime/src/main/kotlin/CodedInputStream.kt b/proto/runtime/src/main/kotlin/CodedInputStream.kt index 5474b90fcc4..371e5d99890 100644 --- a/proto/runtime/src/main/kotlin/CodedInputStream.kt +++ b/proto/runtime/src/main/kotlin/CodedInputStream.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by Dmitry Savvinov on 7/6/16. * diff --git a/proto/runtime/src/main/kotlin/CodedOutputStream.kt b/proto/runtime/src/main/kotlin/CodedOutputStream.kt index a55d5a9f2c7..2c0818d352a 100644 --- a/proto/runtime/src/main/kotlin/CodedOutputStream.kt +++ b/proto/runtime/src/main/kotlin/CodedOutputStream.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by user on 7/6/16. */ diff --git a/proto/runtime/src/main/kotlin/KotlinInputStream.kt b/proto/runtime/src/main/kotlin/KotlinInputStream.kt index 89a00d8a1bb..c2d592a729b 100644 --- a/proto/runtime/src/main/kotlin/KotlinInputStream.kt +++ b/proto/runtime/src/main/kotlin/KotlinInputStream.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by user on 8/8/16. */ diff --git a/proto/runtime/src/main/kotlin/KotlinOutputStream.kt b/proto/runtime/src/main/kotlin/KotlinOutputStream.kt index 51f80aaf766..9b2714e17b2 100644 --- a/proto/runtime/src/main/kotlin/KotlinOutputStream.kt +++ b/proto/runtime/src/main/kotlin/KotlinOutputStream.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by user on 8/8/16. */ diff --git a/proto/runtime/src/main/kotlin/WireFormat.kt b/proto/runtime/src/main/kotlin/WireFormat.kt index 70aa324170e..de70a45d65f 100644 --- a/proto/runtime/src/main/kotlin/WireFormat.kt +++ b/proto/runtime/src/main/kotlin/WireFormat.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by user on 7/6/16. */ @@ -58,54 +59,102 @@ object WireFormat { return getTagSize(fieldNumber, WireType.VARINT) + getVarint32Size(value) } + fun getInt32SizeNoTag(value: Int): Int { + return getVarint32Size(value) + } + fun getUInt32Size(fieldNumber: Int, value: Int): Int { return getInt32Size(fieldNumber, value) } + fun getUIn32SizeNoTag(value: Int): Int { + return getVarint32Size(value) + } + fun getInt64Size(fieldNumber: Int, value: Long): Int { return getTagSize(fieldNumber, WireType.VARINT) + getVarint64Size(value) } + fun getInt64SizeNoTag(value: Long): Int { + return getVarint64Size(value) + } + fun getUInt64Size(fieldNumber: Int, value: Long): Int { return getInt64Size(fieldNumber, value) } + fun getUInt64SizeNoTag(value: Long): Int { + return getVarint64Size(value) + } + fun getBoolSize(fieldNumber: Int, value: Boolean): Int { val intValue = if (value) 1 else 0 return getInt32Size(fieldNumber, intValue) } + fun getBoolSizeNoTag(value: Boolean): Int { + val intValue = if (value) 1 else 0 + return getInt32SizeNoTag(intValue) + } + fun getEnumSize(fieldNumber: Int, value: Int): Int { return getInt32Size(fieldNumber, value) } + fun getEnumSizeNoTag(value: Int): Int { + return getInt32SizeNoTag(value) + } + fun getSInt32Size(fieldNumber: Int, value: Int): Int { return getTagSize(fieldNumber, WireType.VARINT) + getZigZag32Size(value) } + fun getSInt32SizeNoTag(value: Int): Int { + return getZigZag32Size(value) + } + fun getSInt64Size(fieldNumber: Int, value: Long): Int { return getTagSize(fieldNumber, WireType.VARINT) + getZigZag64Size(value) } - fun getFixed32Size(fieldNumber: Int, value: Long): Int { + fun getSInt64SizeNoTag(value: Long): Int { + return getZigZag64Size(value) + } + + fun getFixed32Size(fieldNumber: Int, value: Int): Int { return getTagSize(fieldNumber, WireType.FIX_32) + FIXED_32_BYTE_SIZE } + fun getFixed32SizeNoTag(value: Int): Int = FIXED_32_BYTE_SIZE + fun getFixed64Size(fieldNumber: Int, value: Long): Int { return getTagSize(fieldNumber, WireType.FIX_64) + FIXED_64_BYTE_SIZE } + fun getFixed64SizeNoTag(value: Long): Int = FIXED_64_BYTE_SIZE + fun getDoubleSize(fieldNumber: Int, value: Double): Int { + return getTagSize(fieldNumber, WireType.FIX_64) + FIXED_64_BYTE_SIZE + } + + fun getDoubleSizeNoTag(value: Double): Int = FIXED_64_BYTE_SIZE + + fun getFloatSize(fieldNumber: Int, value: Float): Int { return getTagSize(fieldNumber, WireType.FIX_32) + FIXED_32_BYTE_SIZE } - fun getFloatSize(fieldNumber: Int, value: Float): Int { - return getTagSize(fieldNumber, WireType.FIX_64) + FIXED_64_BYTE_SIZE + fun getFloatSizeNoTag(value: Float): Int { + return FIXED_32_BYTE_SIZE } fun getBytesSize(fieldNumber: Int, value: ByteArray): Int { if (value.size == 0) return 0 + var size = 0 return value.size + getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + getVarint32Size(value.size) } + + fun getBytesSizeNoTag(value: ByteArray): Int { + return value.size + getVarint32Size(value.size) + } } diff --git a/proto/runtime/src/main/kotlin/WireType.kt b/proto/runtime/src/main/kotlin/WireType.kt index edc02c60414..4b4478b0a8c 100644 --- a/proto/runtime/src/main/kotlin/WireType.kt +++ b/proto/runtime/src/main/kotlin/WireType.kt @@ -1,3 +1,4 @@ +package main.kotlin /** * Created by Dmitry Savvinov on 7/6/16. * Enum for possible WireTypes.