From 53884f8da6ee92ee1d0354b22813b2142650e024 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Wed, 19 Oct 2016 15:56:30 +0300 Subject: [PATCH] Interop/StubGenerator: add minor code improvements --- .../native/interop/gen/jvm/StubGenerator.kt | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt index 5fb933c44c1..40606e6cb9b 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt @@ -107,6 +107,15 @@ class StubGenerator( return withOutput({ oldOut(" $it") }, action) } + private fun block(header: String, body: () -> R): R { + out("$header {") + val res = indent { + body() + } + out("}") + return res + } + /** * Returns the expression which could be used for this type in C code. * @@ -418,8 +427,7 @@ class StubGenerator( } val className = decl.kotlinName - out("class $className(ptr: NativePtr) : NativeStruct(ptr) {") - indent { + block("class $className(ptr: NativePtr) : NativeStruct(ptr)") { out("") out("companion object : Type<$className>(${def.size}, ::$className)") out("") @@ -435,7 +443,6 @@ class StubGenerator( } } } - out("}") } /** @@ -443,9 +450,9 @@ class StubGenerator( */ private fun generateForwardStruct(s: StructDecl) { val className = s.kotlinName - out("class $className(ptr: NativePtr) : NativeRef(ptr) {") - out(" companion object : Type<$className>(::$className)") - out("}") + block("class $className(ptr: NativePtr) : NativeRef(ptr)") { + out("companion object : Type<$className>(::$className)") + } } /** @@ -459,26 +466,23 @@ class StubGenerator( val baseRefType = getKotlinTypeForRefTo(e.baseType) - out("enum class ${e.kotlinName}(val value: ${e.baseType.kotlinType}) {") - indent { + block("enum class ${e.kotlinName}(val value: ${e.baseType.kotlinType})") { e.values.forEach { out("${it.name}(${it.value}),") } out(";") out("") - out("companion object {") - out(" fun byValue(value: ${e.baseType.kotlinType}) = ${e.kotlinName}.values().find { it.value == value }!!") - out("}") + block("companion object") { + out("fun byValue(value: ${e.baseType.kotlinType}) = ${e.kotlinName}.values().find { it.value == value }!!") + } out("") - out("class ref(ptr: NativePtr) : NativeRef(ptr) {") - out(" companion object : TypeWithSize(${baseRefType.typeExpr}.size, ::ref)") - out(" var value: ${e.kotlinName}") - out(" get() = byValue(${baseRefType.typeExpr}.byPtr(ptr).value)") - out(" set(value) { ${baseRefType.typeExpr}.byPtr(ptr).value = value.value }") - out("}") - + block("class ref(ptr: NativePtr) : NativeRef(ptr)") { + out("companion object : TypeWithSize(${baseRefType.typeExpr}.size, ::ref)") + out("var value: ${e.kotlinName}") + out(" get() = byValue(${baseRefType.typeExpr}.byPtr(ptr).value)") + out(" set(value) { ${baseRefType.typeExpr}.byPtr(ptr).value = value.value }") + } } - out("}") } /** @@ -646,10 +650,8 @@ class StubGenerator( val constructorArgsStr = constructorArgs.joinToString(", ") - out("object $name : NativeFunctionType<$kotlinFunctionType>($constructorArgsStr) {") - indent { - out("override fun invoke(function: $kotlinFunctionType, args: NativeArray, ret: NativePtr) {") - indent { + block("object $name : NativeFunctionType<$kotlinFunctionType>($constructorArgsStr)") { + block("override fun invoke(function: $kotlinFunctionType, args: NativeArray, ret: NativePtr)") { val args = type.parameterTypes.mapIndexed { i, paramType -> val refType = getKotlinTypeForRefTo(paramType) val ref = "args[$i].value.asRef(${refType.typeExpr})!!" @@ -671,9 +673,7 @@ class StubGenerator( } } - out("}") } - out("}") } /** @@ -734,8 +734,7 @@ class StubGenerator( out("") } - out("object externals {") - indent { + block("object externals") { out("init { System.loadLibrary(\"$libName\") }") functionsToBind.forEach { try { @@ -748,7 +747,6 @@ class StubGenerator( } } } - out("}") } /** @@ -819,16 +817,16 @@ class StubGenerator( } val jniFuncName = "Java_" + funcFullName.replace("_", "_1").replace('.', '_').replace("$", "_00024") - out("JNIEXPORT $cReturnType JNICALL $jniFuncName (JNIEnv *env, jobject obj$args) {") + block("JNIEXPORT $cReturnType JNICALL $jniFuncName (JNIEnv *env, jobject obj$args)") { - if (cReturnType == "void") { - out(" $callExpr;") - } else if (funcReturnType is RecordType) { - out(" *(${funcReturnType.decl.spelling}*)retValPlacement = $callExpr;") - out(" return ($cReturnType) retValPlacement;") - } else { - out(" return ($cReturnType) ($callExpr);") + if (cReturnType == "void") { + out("$callExpr;") + } else if (funcReturnType is RecordType) { + out("*(${funcReturnType.decl.spelling}*)retValPlacement = $callExpr;") + out("return ($cReturnType) retValPlacement;") + } else { + out("return ($cReturnType) ($callExpr);") + } } - out("}") } }