Skip to content

Commit

Permalink
Implementing destructors for Kotlin callbacks (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
emarteca committed Sep 11, 2024
1 parent b375b3e commit 63f8cf5
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 17 deletions.
132 changes: 131 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.diplomattest.somelib;

import com.sun.jna.JNIEnv
import com.sun.jna.Library
import com.sun.jna.Memory
import com.sun.jna.Native
import com.sun.jna.Pointer
import com.sun.jna.Structure
import com.sun.jna.Union
import java.util.Collections


// We spawn a cleaner for the library which is responsible for cleaning opaque types.
Expand Down Expand Up @@ -39,6 +41,26 @@ object DW {
}
}

internal interface DiplomatJVMRuntimeLib: Library {
fun create_rust_jvm_cookie(env: JNIEnv, obj: Object): Pointer
fun destroy_rust_jvm_cookie(obj_pointer: Pointer): Unit
}

internal class DiplomatJVMRuntime {
companion object {
val libClass: Class<DiplomatJVMRuntimeLib> = DiplomatJVMRuntimeLib::class.java
val lib: DiplomatJVMRuntimeLib = Native.load("somelib", libClass, Collections.singletonMap(Library.OPTION_ALLOW_OBJECTS, true))

fun buildRustCookie(obj: Object): Pointer {
return lib.create_rust_jvm_cookie(JNIEnv.CURRENT, obj);
}

fun dropRustCookie(obj_pointer: Pointer): Unit {
lib.destroy_rust_jvm_cookie(obj_pointer);
}
}
}


internal object PrimitiveArrayTools {

Expand Down
2 changes: 1 addition & 1 deletion feature_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ crate-type = ["staticlib", "rlib", "cdylib"]

[dependencies]
diplomat = { path = "../macro" }
diplomat-runtime = { path = "../runtime", feature = ["log"] }
diplomat-runtime = { path = "../runtime", feature = ["log", "jvm-callback-support"] }
log = { version = "0.4" }
Loading

0 comments on commit 63f8cf5

Please sign in to comment.