zsu.kni.ksp.template.NativeEnvStoreImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kni-ksp Show documentation
Show all versions of kni-ksp Show documentation
Auto JNI binding based on Kotlin Multiplatform
The newest version!
package zsu.kni.ksp.template
import org.intellij.lang.annotations.Language
object NativeEnvStoreImpl : Template {
@Language("kotlin")
override fun create(packageName: String, simpleClassName: String, jniPackageName: String): String {
return """
@file:OptIn(ExperimentalForeignApi::class, ExperimentalNativeApi::class)
package $packageName
import kotlinx.cinterop.*
import $jniPackageName.*
import zsu.kni.internal.native.NativeEnvStore
import kotlin.experimental.ExperimentalNativeApi
@CName(externName = "Java_zsu_kni_KniNativeThread_attach")
fun attach(
jenv: CPointer,
classRef: jclass,
id: jint,
) {
NativeEnvStore[id] = jenv
}
@CName(externName = "Java_zsu_kni_KniNativeThread_detach")
fun detach(
jenv: CPointer,
classRef: jclass,
id: jint,
sameJEnv: jint,
) {
val isSameJEnv = sameJEnv == 0
if (isSameJEnv) {
val originJenvPtr: CPointer = NativeEnvStore[id]
val originJenvValue = originJenvPtr.pointed
val currentJenvValue = jenv.pointed
NativeEnvStore.compareEnv(originJenvValue, currentJenvValue)
}
NativeEnvStore.remove(id)
}
@CName(externName = "Java_zsu_kni_KniNativeThread_detachAll")
fun detachAll(
jenv: CPointer,
classRef: jclass,
) {
NativeEnvStore.clear()
}
"""
}
}
private const val dollar = "\$"