Skip to content

Commit a2893df

Browse files
Jozott00Mr3zee
authored andcommitted
grpc-native: Address PR review issues
Signed-off-by: Johannes Zottele <[email protected]>
1 parent f5f8f09 commit a2893df

File tree

12 files changed

+93
-686
lines changed

12 files changed

+93
-686
lines changed

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/KTag.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal fun KTag.toRawKTag(): UInt {
3434
return (fieldNr.toUInt() shl K_TAG_TYPE_BITS) or wireType.ordinal.toUInt()
3535
}
3636

37-
internal fun KTag.Companion.from(rawKTag: UInt): KTag? {
37+
internal fun KTag.Companion.fromOrNull(rawKTag: UInt): KTag? {
3838
val type = (rawKTag and K_TAG_TYPE_MASK).toInt()
3939
val field = (rawKTag shr K_TAG_TYPE_BITS).toInt()
4040
if (!isValidFieldNr(field)) {

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/WireDecoder.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import kotlinx.io.Buffer
99
/**
1010
* A platform-specific decoder for wire format data.
1111
*
12+
* This decoder is used by first calling [readTag], than looking up the field based on the field number in the returned,
13+
* tag and then calling the actual `read*()` method to read the value to the corresponding field.
14+
* This means that the nullable return value does not collide with optional fields, as optional fields would not
15+
* include a tag in the encoded message.
16+
*
1217
* If one `read*()` method returns `null`, decoding the data failed and no further
1318
* decoding can be done.
1419
*
@@ -43,6 +48,7 @@ internal interface WireDecoder : AutoCloseable {
4348
fun readSFixed64(): Long?
4449
fun readFloat(): Float?
4550
fun readDouble(): Double?
51+
4652
fun readEnum(): Int?
4753
fun readString(): String?
4854
fun readBytes(): ByteArray?

grpc/grpc-core/src/jvmMain/java/kotlinx/rpc/grpc/internal/WireDecoder.jvm.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

grpc/grpc-core/src/jvmMain/kotlin/kotlinx/rpc/grpc/internal/WireDecoder.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/WireDecoder.native.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlin.experimental.ExperimentalNativeApi
1212
import kotlin.math.min
1313
import kotlin.native.ref.createCleaner
1414

15+
// TODO: Evaluate if this buffer size is suitable for all targets (KRPC-186)
1516
// maximum buffer size to allocate as contiguous memory in bytes
1617
private const val MAX_PACKED_BULK_SIZE: Int = 1_000_000
1718

@@ -25,7 +26,7 @@ internal class WireDecoderNative(private val source: Buffer) : WireDecoder {
2526
// construct the pw_decoder_t by passing a pw_zero_copy_input_t that provides a bridge between
2627
// the CodedInputStream and the given source buffer. it passes functions that call the respective
2728
// ZeroCopyInputSource methods.
28-
internal val raw = run {
29+
internal val raw: CPointer<pw_decoder_t> = run {
2930
// construct the pw_zero_copy_input_t that functions as a bridge to the ZeroCopyInputSource
3031
val zeroCopyCInput = cValue<pw_zero_copy_input> {
3132
ctx = zeroCopyInput.asCPointer()
@@ -62,7 +63,7 @@ internal class WireDecoderNative(private val source: Buffer) : WireDecoder {
6263

6364
override fun readTag(): KTag? {
6465
val tag = pw_decoder_read_tag(raw)
65-
return KTag.from(tag)
66+
return KTag.fromOrNull(tag)
6667
}
6768

6869
override fun readBool(): Boolean? = memScoped {
@@ -177,7 +178,7 @@ internal class WireDecoderNative(private val source: Buffer) : WireDecoder {
177178
return null
178179
}
179180

180-
// TODO: Is it possible to avoid copying the c_str, by directly allocating a K/N String (as in readBytes)?
181+
// TODO: Is it possible to avoid copying the c_str, by directly allocating a K/N String (as in readBytes)? KRPC-187
181182
override fun readString(): String? = memScoped {
182183
val str = alloc<CPointerVar<pw_string_t>>()
183184
val ok = pw_decoder_read_string(raw, str.ptr)
@@ -263,7 +264,7 @@ internal class WireDecoderNative(private val source: Buffer) : WireDecoder {
263264
val byteLen = readInt32() ?: return null
264265
if (byteLen < 0) return null
265266
if (source.size < byteLen) return null
266-
if (byteLen == 0) return null
267+
if (byteLen == 0) return emptyList()
267268

268269
val limit = pw_decoder_push_limit(raw, byteLen)
269270

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/WireEncoder.native.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class WireEncoderNative(private val sink: Sink) : WireEncoder {
3030
private val context = StableRef.create(this.Ctx())
3131

3232
// construct encoder with a callback that calls write() on this.context
33-
internal val raw = run {
33+
internal val raw: CPointer<pw_encoder_t> = run {
3434
pw_encoder_new(context.asCPointer(), staticCFunction { ctx, buf, size ->
3535
if (buf == null || ctx == null) {
3636
return@staticCFunction false
@@ -175,8 +175,7 @@ private inline fun <T> WireEncoderNative.writePackedInternal(
175175
fieldSize: Int,
176176
crossinline writer: (CValuesRef<pw_encoder_t>?, T) -> Boolean
177177
): Boolean {
178-
val ktag = KTag(fieldNr, WireType.LENGTH_DELIMITED).toRawKTag()
179-
pw_encoder_write_tag(raw, ktag)
178+
pw_encoder_write_tag(raw, fieldNr, WireType.LENGTH_DELIMITED.ordinal)
180179
// write the field size of the packed field
181180
pw_encoder_write_int32_no_tag(raw, fieldSize)
182181
for (v in value) {

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/bufferUnsafeExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import platform.posix.memcpy
1313

1414

1515
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class)
16-
public fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
16+
internal fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
1717
var consumed = 0L
1818
while (consumed < length) {
1919
UnsafeBufferOperations.writeToTail(this.buffer, 1) { array, start, endExclusive ->

grpc/grpcpp-c/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
21
load("@rules_cc//cc:defs.bzl", "cc_library")
32

43
cc_binary(
@@ -24,7 +23,7 @@ cc_library(
2423
includes = ["include"],
2524
visibility = ["//visibility:public"],
2625
deps = [
27-
# TODO: Reduce the dependencies and only use required once
26+
# TODO: Reduce the dependencies and only use required once. KRPC-185
2827
"@com_github_grpc_grpc//:channelz",
2928
"@com_github_grpc_grpc//:generic_stub",
3029
"@com_github_grpc_grpc//:grpc++",

grpc/grpcpp-c/MODULE.bazel

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,3 @@ bazel_dep(
2222
version = "1.73.1",
2323
repo_name = "com_github_grpc_grpc",
2424
)
25-
26-
emsdk_version = "4.0.11"
27-
28-
bazel_dep(name = "emsdk", version = emsdk_version)
29-
git_override(
30-
module_name = "emsdk",
31-
remote = "https://github.com/emscripten-core/emsdk.git",
32-
strip_prefix = "bazel",
33-
tag = emsdk_version,
34-
)

0 commit comments

Comments
 (0)