-
Notifications
You must be signed in to change notification settings - Fork 37
Added jvm codecs implementations #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc.internal | ||
|
||
import kotlinx.io.Buffer | ||
|
||
internal expect fun WireDecoder.pushLimit(byteLen: Int): Int | ||
internal expect fun WireDecoder.popLimit(limit: Int) | ||
internal expect fun WireDecoder.bytesUntilLimit(): Int | ||
|
||
internal inline fun <T : Any> WireDecoder.readPackedVarInternal( | ||
crossinline size: () -> Long, | ||
crossinline readFn: () -> T, | ||
crossinline withError: () -> Unit, | ||
crossinline hadError: () -> Boolean, | ||
): List<T> { | ||
val byteLen = readInt32() | ||
if (hadError()) { | ||
return emptyList() | ||
} | ||
if (byteLen < 0) { | ||
return emptyList<T>().apply { withError() } | ||
} | ||
val size = size() | ||
// no size check on jvm | ||
if (size != -1L && size < byteLen) { | ||
return emptyList<T>().apply { withError() } | ||
} | ||
if (byteLen == 0) { | ||
return emptyList() // actually an empty list (no error) | ||
} | ||
|
||
val limit = pushLimit(byteLen) | ||
|
||
val result = mutableListOf<T>() | ||
|
||
while (bytesUntilLimit() > 0) { | ||
val elem = readFn() | ||
if (hadError()) { | ||
break | ||
} | ||
result.add(elem) | ||
} | ||
|
||
popLimit(limit) | ||
return result | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc.internal | ||
|
||
internal actual fun WireDecoder.pushLimit(byteLen: Int): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
internal actual fun WireDecoder.popLimit(limit: Int) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
internal actual fun WireDecoder.bytesUntilLimit(): Int { | ||
TODO("Not yet implemented") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,121 @@ | |
|
||
package kotlinx.rpc.grpc.internal | ||
|
||
import com.google.protobuf.CodedInputStream | ||
import kotlinx.io.Buffer | ||
import kotlinx.io.Source | ||
import kotlinx.io.asInputStream | ||
|
||
internal class WireDecoderJvm(source: Buffer) : WireDecoder { | ||
// there is no way to omit coping here | ||
internal val codedInputStream: CodedInputStream = CodedInputStream.newInstance(source.asInputStream()) | ||
|
||
// errors in jvm are exceptions | ||
override fun hadError(): Boolean { | ||
return false | ||
} | ||
Comment on lines
+15
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably unify the behavior between all implementations. Probably, the K/N implementation should also throw exceptions instead of providing the error indicator? (in a separate PR) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, let's do that in the next PR |
||
|
||
override fun readTag(): KTag? { | ||
val tag = codedInputStream.readTag().toUInt() | ||
if (tag == 0u) { | ||
return null | ||
} | ||
|
||
return KTag.fromOrNull(tag) | ||
} | ||
|
||
override fun readBool(): Boolean { | ||
return codedInputStream.readBool() | ||
} | ||
|
||
override fun readInt32(): Int { | ||
return codedInputStream.readInt32() | ||
} | ||
|
||
override fun readInt64(): Long { | ||
return codedInputStream.readInt64() | ||
} | ||
|
||
override fun readUInt32(): UInt { | ||
// todo check java unsigned types | ||
return codedInputStream.readUInt32().toUInt() | ||
} | ||
|
||
override fun readUInt64(): ULong { | ||
// todo check java unsigned types | ||
return codedInputStream.readUInt64().toULong() | ||
} | ||
|
||
override fun readSInt32(): Int { | ||
return codedInputStream.readSInt32() | ||
} | ||
|
||
override fun readSInt64(): Long { | ||
return codedInputStream.readSInt64() | ||
} | ||
|
||
override fun readFixed32(): UInt { | ||
// todo check java unsigned types | ||
return codedInputStream.readFixed32().toUInt() | ||
} | ||
|
||
override fun readFixed64(): ULong { | ||
// todo check java unsigned types | ||
return codedInputStream.readFixed64().toULong() | ||
} | ||
|
||
override fun readSFixed32(): Int { | ||
return codedInputStream.readSFixed32() | ||
} | ||
|
||
override fun readSFixed64(): Long { | ||
return codedInputStream.readSFixed64() | ||
} | ||
|
||
override fun readFloat(): Float { | ||
return codedInputStream.readFloat() | ||
} | ||
|
||
override fun readDouble(): Double { | ||
return codedInputStream.readDouble() | ||
} | ||
|
||
override fun readEnum(): Int { | ||
return codedInputStream.readEnum() | ||
} | ||
|
||
override fun readString(): String { | ||
return codedInputStream.readStringRequireUtf8() | ||
} | ||
|
||
override fun readBytes(): ByteArray { | ||
return codedInputStream.readByteArray() | ||
} | ||
|
||
override fun readPackedBool() = readPackedInternal(this::readBool) | ||
override fun readPackedInt32() = readPackedInternal(this::readInt32) | ||
override fun readPackedInt64() = readPackedInternal(this::readInt64) | ||
override fun readPackedUInt32() = readPackedInternal(this::readUInt32) | ||
override fun readPackedUInt64() = readPackedInternal(this::readUInt64) | ||
override fun readPackedSInt32() = readPackedInternal(this::readSInt32) | ||
override fun readPackedSInt64() = readPackedInternal(this::readSInt64) | ||
override fun readPackedEnum() = readPackedInternal(this::readEnum) | ||
override fun readPackedFixed32(): List<UInt> = readPackedInternal(::readFixed32) | ||
override fun readPackedFixed64(): List<ULong> = readPackedInternal(::readFixed64) | ||
override fun readPackedSFixed32(): List<Int> = readPackedInternal(::readSFixed32) | ||
override fun readPackedSFixed64(): List<Long> = readPackedInternal(::readSFixed64) | ||
override fun readPackedFloat(): List<Float> = readPackedInternal(::readFloat) | ||
override fun readPackedDouble(): List<Double> = readPackedInternal(::readDouble) | ||
|
||
override fun close() {} | ||
|
||
private fun <T : Any> readPackedInternal(read: () -> T) = readPackedVarInternal( | ||
size = { -1 }, | ||
readFn = read, | ||
withError = { }, | ||
hadError = { false }, | ||
) | ||
} | ||
|
||
internal actual fun WireDecoder(source: Buffer): WireDecoder { | ||
TODO("Not yet implemented") | ||
} | ||
return WireDecoderJvm(source) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.