cinterop question - trying to emulate: ```JavaVM* ...
# kotlin-native
n
cinterop question - trying to emulate:
Copy code
JavaVM* vm;
GetJavaVM(&vm);
With kotlin.cinterop types and my solution seems so verbose. Are there better APIs that I'm missing?
Copy code
val vm: CPointerVar<JavaVMVar> = allocPointerTo()
val vm2: CPointerVar<CPointerVar<JavaVMVar>> = allocPointerTo()
vm2.pointed = vm
GetJavaVM(vm2.value!!)
j
Depends on what you are trying to do?
Copy code
@CName("JNI_OnLoad")
fun JNI_OnLoad(
    vm: kotlinx.cinterop.CPointer<JavaVMVar>,
    reserved: kotlinx.cinterop.CValuesRef<*>
): platform.android.jint {
    mJniBridge = JniBridge(vm)
    return JNI_VERSION_1_6
}

@CName("JNI_OnUnload")
fun JNI_OnUnload(vm: kotlinx.cinterop.CPointer<JavaVMVar>?, reserved: kotlinx.cinterop.CValuesRef<*>?): kotlin.Unit {
    mJniBridge = null
}
Is available if you are using code that loads your/a library from java
n
Yes, thanks! I'm aware of that, but we can't use it.
GetJavaVM
itself is working fine, but I was wondering if kotlin.cinterop had some better APIs especially for doing
&vm
, which is three characters in C and three lines in Kotlin.
j
Ah if that is your question, thats basically how it is
👍 1
p
hey, I have a question, I got this code
Copy code
@CName("JNI_OnLoad")
    fun JNI_OnLoad(
        vm: CPointer<JavaVMVar>,
        preserved: COpaquePointer
    ): jint {
        logInfo("JNI On Load")
        return memScoped {
            val envStorage = alloc<CPointerVar<JNIEnvVar>>()
            val vmValue = vm.pointed.pointed!!
...
I don’t know why but the
vmValue
throws a NullPointerException in an Android Emulator (X86)
Copy code
I/KonanActivity: JNI On Load
I/Konan_main: Uncaught Kotlin exception: 
    kotlin.NullPointerException
I/Konan_main:     at  (0xeb2ee03f)
        at  (0xeb2f460d)
        at JNI_OnLoad (0xeb325703)
        at _ZN3art9JavaVMExt17LoadNativeLibraryEP7_JNIEnvRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEP8_jobjectP7_jclassPS9_ (0xeeda4609)
        at JVM_NativeLoad (0x9c26e2a3)
        at Runtime_nativeLoad (0x9c2309d7)
        at  (0x6f9988db)
A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 14634 (ication.android), pid 14634 (ication.android)
I don’t know what I’m doing wrong, but at this point I’m crying for some pointers since I’ve been fighting this issue for days
my apologies… I finally got what the error was, apparently it’s NO USE to make those functions within a kotlin class. Apparently they NEED TO BE top level functions
👍 1