From 9d0d213a09339fdf810dec9111b0cf19ebe64acc Mon Sep 17 00:00:00 2001 From: e5l Date: Wed, 10 Aug 2016 12:43:38 +0300 Subject: [PATCH] translator: fix proto runtime --- .../src/main/kotlin/CodedInputStream.kt | 18 ++++-------------- proto/runtime/src/main/kotlin/WireFormat.kt | 11 ++--------- .../kotlinnative/translator/BlockCodegen.kt | 4 ++-- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/proto/runtime/src/main/kotlin/CodedInputStream.kt b/proto/runtime/src/main/kotlin/CodedInputStream.kt index 35e2c5825c7..1b37f3ab14a 100644 --- a/proto/runtime/src/main/kotlin/CodedInputStream.kt +++ b/proto/runtime/src/main/kotlin/CodedInputStream.kt @@ -1,5 +1,3 @@ -import java.nio.ByteBuffer -import java.nio.ByteOrder import WireFormat.VARINT_INFO_BITS_COUNT import WireFormat.VARINT_INFO_BITS_MASK import WireFormat.VARINT_UTIL_BIT_MASK @@ -15,7 +13,7 @@ import WireFormat.VARINT_UTIL_BIT_MASK */ // TODO: refactor correctness checks into readTag -class CodedInputStream(buffer: ByteArray) { +class CodedInputStream(val buffer: ByteArray) { var errorMessage: String = "" val inputStream: KotlinInputStream init { @@ -120,17 +118,10 @@ class CodedInputStream(buffer: ByteArray) { expectedWireType: WireType, actualWireType: WireType) { if (expectedFieldNumber != actualFieldNumber) { - errorMessage = - "Error in protocol format: \n " + - "Expected field number ${expectedFieldNumber}, got ${actualFieldNumber}" - println(errorMessage) return } - if (expectedWireType != actualWireType) { - errorMessage = "Error in protocol format: \n " + - "Expected ${expectedWireType.name} type, got ${actualWireType.name}" - println(errorMessage) + if (expectedWireType.id != actualWireType.id) { return } } @@ -207,13 +198,13 @@ class CodedInputStream(buffer: ByteArray) { // reads zig-zag encoded integer not larger than 32-bit long fun readZigZag32NoTag(): Int { val value = readInt32NoTag() - return (value shr 1) xor (-(value and 1)) // bit magic for decoding zig-zag number + return (value shr 1) xor (0 - (value and 1)) // bit magic for decoding zig-zag number } // reads zig-zag encoded integer not larger than 64-bit long fun readZigZag64NoTag(): Long { val value = readInt64NoTag() - return (value shr 1) xor (-(value and 1L)) // bit magic for decoding zig-zag number + return (value shr 1) xor (0 - (value and 1L)) // bit magic for decoding zig-zag number } // checks if at least one more byte can be read from underlying input stream @@ -221,4 +212,3 @@ class CodedInputStream(buffer: ByteArray) { return inputStream.isAtEnd() } } - diff --git a/proto/runtime/src/main/kotlin/WireFormat.kt b/proto/runtime/src/main/kotlin/WireFormat.kt index 120d8c8f58f..70aa324170e 100644 --- a/proto/runtime/src/main/kotlin/WireFormat.kt +++ b/proto/runtime/src/main/kotlin/WireFormat.kt @@ -23,7 +23,7 @@ object WireFormat { // TODO: refactor casts into function overloading as soon as translator will support it fun getTagSize(fieldNumber: Int, wireType: WireType): Int { - return getVarint32Size((fieldNumber shl 3) or wireType.ordinal) + return getVarint32Size((fieldNumber shl 3) or wireType.id) } fun getVarint32Size(value: Int): Int { @@ -103,16 +103,9 @@ object WireFormat { return getTagSize(fieldNumber, WireType.FIX_64) + FIXED_64_BYTE_SIZE } - fun getStringSize(fieldNumber: Int, value: String): Int { - if (value.length == 0) - return 0 - val encodedStringSize = value.toByteArray(Charsets.UTF_8).size //TODO: not sure if it's the best way to do it - return encodedStringSize + getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + getVarint32Size(encodedStringSize) - } - fun getBytesSize(fieldNumber: Int, value: ByteArray): Int { if (value.size == 0) return 0 return value.size + getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + getVarint32Size(value.size) } -} \ No newline at end of file +} diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index a5a738953a6..7a6d5cbbedb 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -517,14 +517,14 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va left ?: throw UnsupportedOperationException("Wrong binary exception") } - val right = evaluateExpression(expr.lastChild, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception") + val right = evaluateExpression(expr.lastChild, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception: ${expr.text}") return executeBinaryExpression(operator, expr.operationReference, left, right) } private fun evaluatePostfixExpression(expr: KtPostfixExpression, scopeDepth: Int): LLVMSingleValue? { val operator = expr.operationToken - val left = evaluateExpression(expr.baseExpression, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception") + val left = evaluateExpression(expr.baseExpression, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception: ${expr.text}") return executePostfixExpression(operator, expr.operationReference, left as LLVMVariable) }