Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import kotlinx.rpc.buf.tasks.BufGenerateTask
import kotlinx.rpc.util.findOrCreate
import kotlinx.rpc.util.withKotlinJvmExtension
import kotlinx.rpc.util.withKotlinKmpExtension
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectFactory
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.Project
import org.gradle.api.*
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -104,7 +100,7 @@ internal fun Project.createProtoExtensions() {
findOrCreateAndConfigure("jvmTest", null)

sourceSets.configureEach {
if (name == "jvmMain" || name == "jvmTest") {
if (name == "jvmMain" || name == "jvmTest" || name == "nativeTest" || name == "commonTest") {
findOrCreateAndConfigure(name, this)
}
}
Expand Down
21 changes: 18 additions & 3 deletions grpc/grpc-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import kotlinx.rpc.buf.tasks.BufGenerateTask
import kotlinx.rpc.proto.kotlinMultiplatform
import org.gradle.kotlin.dsl.withType
import org.gradle.internal.extensions.stdlib.capitalized
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.CInteropProcess
Expand Down Expand Up @@ -123,8 +122,8 @@ kotlin {
)
}

val libUpbTask = "cinterop${libprotowire.name.capitalized()}${it.targetName.capitalized()}"
tasks.named(libUpbTask, CInteropProcess::class) {
val libProtowireTask = "cinterop${libprotowire.name.capitalized()}${it.targetName.capitalized()}"
tasks.named(libProtowireTask, CInteropProcess::class) {
dependsOn(buildGrpcppCLib)
}

Expand All @@ -139,8 +138,15 @@ protoSourceSets {
exclude("exclude/**")
}
}

configureEach {
proto {
exclude("exclude/**")
}
}
}


rpc {
grpc {
val globalRootDir: String by extra
Expand All @@ -152,6 +158,15 @@ rpc {
}

project.tasks.withType<BufGenerateTask>().configureEach {

// TODO: Remove this once we remove JVM generation
// Set compile for common(native) option
if (name.endsWith("CommonTest")) {
protocPlugins.kotlinMultiplatform {
options.set(options.getOrElse(emptyMap()) + mapOf("targetMode" to "common"))
}
}

if (name.endsWith("Test")) {
dependsOn(gradle.includedBuild("protoc-gen").task(":jar"))
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package kotlinx.rpc.grpc.internal

import kotlinx.io.Buffer
import kotlinx.rpc.grpc.pb.WireDecoder

internal expect fun WireDecoder.pushLimit(byteLen: Int): Int
internal expect fun WireDecoder.popLimit(limit: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* 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
package kotlinx.rpc.grpc.pb

import kotlinx.rpc.grpc.internal.KTag.Companion.K_TAG_TYPE_BITS

internal enum class WireType {
public enum class WireType {
VARINT, // 0
FIXED64, // 1
LENGTH_DELIMITED, // 2
Expand All @@ -15,13 +13,13 @@ internal enum class WireType {
FIXED32, // 5
}

internal data class KTag(val fieldNr: Int, val wireType: WireType) {
public data class KTag(val fieldNr: Int, val wireType: WireType) {

init {
check(isValidFieldNr(fieldNr)) { "Invalid field number: $fieldNr" }
}

companion object {
internal companion object {
// Number of bits in a tag which identify the wire type.
const val K_TAG_TYPE_BITS: Int = 3;

Expand All @@ -31,7 +29,7 @@ internal data class KTag(val fieldNr: Int, val wireType: WireType) {
}

internal fun KTag.toRawKTag(): UInt {
return (fieldNr.toUInt() shl K_TAG_TYPE_BITS) or wireType.ordinal.toUInt()
return (fieldNr.toUInt() shl KTag.Companion.K_TAG_TYPE_BITS) or wireType.ordinal.toUInt()
}

internal fun KTag.Companion.fromOrNull(rawKTag: UInt): KTag? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* 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
package kotlinx.rpc.grpc.pb

import kotlinx.io.Buffer
import kotlinx.rpc.internal.utils.InternalRpcApi

// TODO: Evaluate if this buffer size is suitable for all targets (KRPC-186)
// maximum buffer size to allocate as contiguous memory in bytes
Expand Down Expand Up @@ -36,40 +37,45 @@ internal const val MAX_PACKED_BULK_SIZE: Int = 1_000_000
* }
* ```
*/
internal interface WireDecoder : AutoCloseable {
fun hadError(): Boolean
fun readTag(): KTag?
fun readBool(): Boolean
fun readInt32(): Int
fun readInt64(): Long
fun readUInt32(): UInt
fun readUInt64(): ULong
fun readSInt32(): Int
fun readSInt64(): Long
fun readFixed32(): UInt
fun readFixed64(): ULong
fun readSFixed32(): Int
fun readSFixed64(): Long
fun readFloat(): Float
fun readDouble(): Double
@InternalRpcApi
public interface WireDecoder : AutoCloseable {
public fun hadError(): Boolean

fun readEnum(): Int
fun readString(): String
fun readBytes(): ByteArray
fun readPackedBool(): List<Boolean>
fun readPackedInt32(): List<Int>
fun readPackedInt64(): List<Long>
fun readPackedSInt32(): List<Int>
fun readPackedSInt64(): List<Long>
fun readPackedUInt32(): List<UInt>
fun readPackedUInt64(): List<ULong>
fun readPackedFixed32(): List<UInt>
fun readPackedFixed64(): List<ULong>
fun readPackedSFixed32(): List<Int>
fun readPackedSFixed64(): List<Long>
fun readPackedFloat(): List<Float>
fun readPackedDouble(): List<Double>
fun readPackedEnum(): List<Int>
/**
* When the read tag is null, it indicates EOF and the parse may stop at this point.
*/
public fun readTag(): KTag?
public fun readBool(): Boolean
public fun readInt32(): Int
public fun readInt64(): Long
public fun readUInt32(): UInt
public fun readUInt64(): ULong
public fun readSInt32(): Int
public fun readSInt64(): Long
public fun readFixed32(): UInt
public fun readFixed64(): ULong
public fun readSFixed32(): Int
public fun readSFixed64(): Long
public fun readFloat(): Float
public fun readDouble(): Double

public fun readEnum(): Int
public fun readString(): String
public fun readBytes(): ByteArray
public fun readPackedBool(): List<Boolean>
public fun readPackedInt32(): List<Int>
public fun readPackedInt64(): List<Long>
public fun readPackedSInt32(): List<Int>
public fun readPackedSInt64(): List<Long>
public fun readPackedUInt32(): List<UInt>
public fun readPackedUInt64(): List<ULong>
public fun readPackedFixed32(): List<UInt>
public fun readPackedFixed64(): List<ULong>
public fun readPackedSFixed32(): List<Int>
public fun readPackedSFixed64(): List<Long>
public fun readPackedFloat(): List<Float>
public fun readPackedDouble(): List<Double>
public fun readPackedEnum(): List<Int>
}

/**
Expand All @@ -82,4 +88,4 @@ internal interface WireDecoder : AutoCloseable {
*
* @param source The buffer containing the encoded wire-format data.
*/
internal expect fun WireDecoder(source: Buffer): WireDecoder
internal expect fun WireDecoder(source: Buffer): WireDecoder
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.pb

import kotlinx.io.Sink
import kotlinx.rpc.internal.utils.InternalRpcApi

/**
* A platform-specific class that encodes values into protobuf's wire format.
*
* If one `write*()` method returns false, the encoding of the value failed
* and no further encodings can be performed on this [WireEncoder].
*
* [flush] must be called to ensure that all data is written to the [Sink].
*/
@InternalRpcApi
@OptIn(ExperimentalUnsignedTypes::class)
public interface WireEncoder {
public fun flush()
public fun writeBool(fieldNr: Int, value: Boolean): Boolean
public fun writeInt32(fieldNr: Int, value: Int): Boolean
public fun writeInt64(fieldNr: Int, value: Long): Boolean
public fun writeUInt32(fieldNr: Int, value: UInt): Boolean
public fun writeUInt64(fieldNr: Int, value: ULong): Boolean
public fun writeSInt32(fieldNr: Int, value: Int): Boolean
public fun writeSInt64(fieldNr: Int, value: Long): Boolean
public fun writeFixed32(fieldNr: Int, value: UInt): Boolean
public fun writeFixed64(fieldNr: Int, value: ULong): Boolean
public fun writeSFixed32(fieldNr: Int, value: Int): Boolean
public fun writeSFixed64(fieldNr: Int, value: Long): Boolean
public fun writeFloat(fieldNr: Int, value: Float): Boolean
public fun writeDouble(fieldNr: Int, value: Double): Boolean
public fun writeEnum(fieldNr: Int, value: Int): Boolean
public fun writeBytes(fieldNr: Int, value: ByteArray): Boolean
public fun writeString(fieldNr: Int, value: String): Boolean
public fun writePackedBool(fieldNr: Int, value: List<Boolean>, fieldSize: Int): Boolean
public fun writePackedInt32(fieldNr: Int, value: List<Int>, fieldSize: Int): Boolean
public fun writePackedInt64(fieldNr: Int, value: List<Long>, fieldSize: Int): Boolean
public fun writePackedUInt32(fieldNr: Int, value: List<UInt>, fieldSize: Int): Boolean
public fun writePackedUInt64(fieldNr: Int, value: List<ULong>, fieldSize: Int): Boolean
public fun writePackedSInt32(fieldNr: Int, value: List<Int>, fieldSize: Int): Boolean
public fun writePackedSInt64(fieldNr: Int, value: List<Long>, fieldSize: Int): Boolean
public fun writePackedFixed32(fieldNr: Int, value: List<UInt>): Boolean
public fun writePackedFixed64(fieldNr: Int, value: List<ULong>): Boolean
public fun writePackedSFixed32(fieldNr: Int, value: List<Int>): Boolean
public fun writePackedSFixed64(fieldNr: Int, value: List<Long>): Boolean
public fun writePackedFloat(fieldNr: Int, value: List<Float>): Boolean
public fun writePackedDouble(fieldNr: Int, value: List<Double>): Boolean
public fun writePackedEnum(fieldNr: Int, value: List<Int>, fieldSize: Int): Boolean =
writePackedInt32(fieldNr, value, fieldSize)
}


internal expect fun WireEncoder(sink: Sink): WireEncoder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.pb

public object WireSize

public expect fun WireSize.int32(value: Int): Int
public expect fun WireSize.int64(value: Long): Int
public expect fun WireSize.uInt32(value: UInt): Int
public expect fun WireSize.uInt64(value: ULong): Int
public expect fun WireSize.sInt32(value: Int): Int
public expect fun WireSize.sInt64(value: Long): Int

public fun WireSize.bool(value: Boolean): Int = int32(if (value) 1 else 0)
public fun WireSize.enum(value: Int): Int = int32(value)
public fun WireSize.packedInt32(value: List<Int>): Int = value.sumOf { int32(it) }
public fun WireSize.packedInt64(value: List<Long>): Int = value.sumOf { int64(it) }
public fun WireSize.packedUInt32(value: List<UInt>): Int = value.sumOf { uInt32(it) }
public fun WireSize.packedUInt64(value: List<ULong>): Int = value.sumOf { uInt64(it) }
public fun WireSize.packedSInt32(value: List<Int>): Int = value.sumOf { sInt32(it) }
public fun WireSize.packedSInt64(value: List<Long>): Int = value.sumOf { sInt64(it) }
public fun WireSize.packedEnum(value: List<Int>): Int = value.sumOf { enum(it) }
Loading
Loading