Skip to content

Commit

Permalink
prepare IntArray passing
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Dec 9, 2024
1 parent e3e37e0 commit da9d717
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
lib = "2.0.0-alpha-09"
lib = "2.0.0-alpha-10-SNAPSHOT"
android = "8.4.2"
auto-service = "1.1.1"
kotlin = "2.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class SekretPlugin : Plugin<Project> {
}

companion object {
private const val VERSION = "2.0.0-alpha-09"
private const val VERSION = "2.0.0-alpha-10-SNAPSHOT"

internal fun getVersion(): String {
return runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import kotlinx.cinterop.*

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.newString(chars: CPointer<jCharVar>, length: Int): jString? {
val method = pointed.pointed?.NewString ?: error("Could not find NewString method in JNI")
val method = pointed.pointed?.NewString ?: return null
return method.invoke(this, chars, length)
}

@OptIn(ExperimentalForeignApi::class)
actual fun jString.getStringUTFChars(env: CPointer<JNIEnvVar>): CPointer<ByteVar>? {
val method = env.pointed.pointed?.GetStringUTFChars ?: error("Could not find GetStringUTFChars method in JNI")
val method = env.pointed.pointed?.GetStringUTFChars ?: return null
return method.invoke(env, this, null)
}

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.newIntArray(size: Int): jIntArray? {
val method = pointed.pointed?.NewIntArray ?: return null
return method.invoke(this, size)
}

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.fill(target: jIntArray, value: IntArray): jIntArray? {
val method = pointed.pointed?.SetIntArrayRegion ?: return null
value.usePinned { pinnedArray ->
val pointer = pinnedArray.addressOf(0)
method.invoke(this, target, 0, value.size, pointer)
}
return target
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public fun SekretHelper.getNativeValue(
key: jString,
env: CPointer<JNIEnvVar>
): jString? {
val obfuscator = key.getString(env)
val obfuscator = key.getString(env) ?: return null
return SekretHelper.getNativeValue(secret, obfuscator).toJString(env)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ typealias jCharVar = UShortVarOf<jChar>

typealias jInt = int32_t

@OptIn(ExperimentalForeignApi::class)
typealias jIntArray = CPointer<out CPointed>

typealias jSize = jInt

typealias jBoolean = uint8_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ internal fun String.toJString(env: CPointer<JNIEnvVar>): jString? = memScoped {
expect fun jString.getStringUTFChars(env: CPointer<JNIEnvVar>): CPointer<ByteVar>?

@OptIn(ExperimentalForeignApi::class)
internal fun jString.getString(env: CPointer<JNIEnvVar>): String {
internal fun jString.getString(env: CPointer<JNIEnvVar>): String? {
val chars = getStringUTFChars(env)
return chars?.toKStringFromUtf8() ?: error("Unable to create String from the given jString")
return chars?.toKStringFromUtf8()
}

@OptIn(ExperimentalForeignApi::class)
expect fun CPointer<JNIEnvVar>.newIntArray(size: Int): jIntArray?

@OptIn(ExperimentalForeignApi::class)
expect fun CPointer<JNIEnvVar>.fill(target: jIntArray, value: IntArray): jIntArray?
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import kotlinx.cinterop.*

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.newString(chars: CPointer<jCharVar>, length: Int): jString? {
val method = pointed.pointed?.NewString ?: error("Could not find NewString method in JNI")
val method = pointed.pointed?.NewString ?: return null
return method.invoke(this, chars, length)
}

@OptIn(ExperimentalForeignApi::class)
actual fun jString.getStringUTFChars(env: CPointer<JNIEnvVar>): CPointer<ByteVar>? {
val method = env.pointed.pointed?.GetStringUTFChars ?: error("Could not find GetStringUTFChars method in JNI")
val method = env.pointed.pointed?.GetStringUTFChars ?: return null
return method.invoke(env, (this as? jstring), null)
}

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.newIntArray(size: Int): jIntArray? {
val method = pointed.pointed?.NewIntArray ?: return null
return method.invoke(this, size)
}

@OptIn(ExperimentalForeignApi::class)
actual fun CPointer<JNIEnvVar>.fill(target: jIntArray, value: IntArray): jIntArray? {
val method = pointed.pointed?.SetIntArrayRegion ?: return null
value.usePinned { pinnedArray ->
val pointer = pinnedArray.addressOf(0)
method.invoke(this, (target as? jintArray), 0, value.size, pointer)
}
return target
}

0 comments on commit da9d717

Please sign in to comment.