diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-3.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-3.snap new file mode 100644 index 000000000..3a4308c54 --- /dev/null +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-3.snap @@ -0,0 +1,37 @@ +--- +source: tool/src/kotlin/mod.rs +expression: result +--- +package dev.diplomattest.somelib; +import com.sun.jna.Callback +import com.sun.jna.Library +import com.sun.jna.Native +import com.sun.jna.Pointer +import com.sun.jna.Structure + + +internal interface BorrowWrapperLib: Library { + fun BorrowWrapper_destroy(handle: Pointer) +} + +class BorrowWrapper internal constructor ( + internal val handle: Pointer, + // These ensure that anything that is borrowed is kept alive and not cleaned + // up by the garbage collector. + internal val selfEdges: List, + internal val aEdges: List, + internal val bEdges: List, +) { + + internal class BorrowWrapperCleaner(val handle: Pointer, val lib: BorrowWrapperLib) : Runnable { + override fun run() { + lib.BorrowWrapper_destroy(handle) + } + } + + companion object { + internal val libClass: Class = BorrowWrapperLib::class.java + internal val lib: BorrowWrapperLib = Native.load("somelib", libClass) + } + +} diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-4.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-4.snap new file mode 100644 index 000000000..31bfb7228 --- /dev/null +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-4.snap @@ -0,0 +1,35 @@ +--- +source: tool/src/kotlin/mod.rs +expression: result +--- +package dev.diplomattest.somelib; +import com.sun.jna.Callback +import com.sun.jna.Library +import com.sun.jna.Native +import com.sun.jna.Pointer +import com.sun.jna.Structure + + +internal interface InputStructLib: Library { + fun InputStruct_destroy(handle: Pointer) +} + +class InputStruct internal constructor ( + internal val handle: Pointer, + // These ensure that anything that is borrowed is kept alive and not cleaned + // up by the garbage collector. + internal val selfEdges: List +) { + + internal class InputStructCleaner(val handle: Pointer, val lib: InputStructLib) : Runnable { + override fun run() { + lib.InputStruct_destroy(handle) + } + } + + companion object { + internal val libClass: Class = InputStructLib::class.java + internal val lib: InputStructLib = Native.load("somelib", libClass) + } + +} diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-5.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-5.snap new file mode 100644 index 000000000..ade8ff1f1 --- /dev/null +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen-5.snap @@ -0,0 +1,160 @@ +--- +source: tool/src/kotlin/mod.rs +expression: result +--- +package dev.diplomattest.somelib; +import com.sun.jna.Callback +import com.sun.jna.Library +import com.sun.jna.Native +import com.sun.jna.Pointer +import com.sun.jna.Structure + + +internal interface MyOpaqueStructLib: Library { + fun MyOpaqueStruct_destroy(handle: Pointer) + fun MyOpaqueStruct_get_byte(): Byte + fun MyOpaqueStruct_get_string_wrapper(in1: Int): Int + fun MyOpaqueStruct_copy(handle: Pointer, borrow: Pointer): Int + fun MyOpaqueStruct_borrow_other(inp1: Pointer, inp2: Pointer, borrow: Pointer): Pointer + fun MyOpaqueStruct_create(in1: Int): Pointer + fun MyOpaqueStruct_do_stuff(handle: Pointer, in1: Int): Double + fun MyOpaqueStruct_borrow(handle: Pointer): Pointer + fun MyOpaqueStruct_borrow2(handle: Pointer): Pointer + fun MyOpaqueStruct_borrow3(handle: Pointer, write: Pointer): Unit + fun MyOpaqueStruct_borrow(other: Pointer): Pointer + fun MyOpaqueStruct_string_stuff(handle: Pointer, someStr: Slice): Pointer + fun MyOpaqueStruct_string_stuff_2(handle: Pointer, someStr: Slice): Pointer +} + +class MyOpaqueStruct internal constructor ( + internal val handle: Pointer, + // These ensure that anything that is borrowed is kept alive and not cleaned + // up by the garbage collector. + internal val selfEdges: List, + internal val bEdges: List, +) { + + internal class MyOpaqueStructCleaner(val handle: Pointer, val lib: MyOpaqueStructLib) : Runnable { + override fun run() { + lib.MyOpaqueStruct_destroy(handle) + } + } + + companion object { + internal val libClass: Class = MyOpaqueStructLib::class.java + internal val lib: MyOpaqueStructLib = Native.load("somelib", libClass) + + fun getByte(): UByte { + + val returnVal = lib.MyOpaqueStruct_get_byte(); + return returnVal.toUByte() + } + + fun getStringWrapper(in1: Int): Int { + + val returnVal = lib.MyOpaqueStruct_get_string_wrapper(in1); + return returnVal + } + + fun borrowOther(inp1: InputStruct, inp2: InputStruct, borrow: MyOpaqueStruct): MyOpaqueStruct { + + val returnVal = lib.MyOpaqueStruct_borrow_other(inp1.handle, inp2.handle, borrow.handle); + val selfEdges: List = listOf(inp1) + listOf(inp2) + listOf(borrow) + val bEdges: List = listOf(borrow) + val handle = returnVal + val returnOpaque = MyOpaqueStruct(handle, selfEdges, bEdges) + return returnOpaque + } + + fun create(in1: Int): MyOpaqueStruct { + + val returnVal = lib.MyOpaqueStruct_create(in1); + val selfEdges: List = listOf() + val bEdges: List = listOf() + val handle = returnVal + val returnOpaque = MyOpaqueStruct(handle, selfEdges, bEdges) + CLEANER.register(returnOpaque, MyOpaqueStruct.MyOpaqueStructCleaner(handle, MyOpaqueStruct.lib)); + + return returnOpaque + } + + fun borrow(other: MyOpaqueStruct): BorrowWrapper { + + val returnVal = lib.MyOpaqueStruct_borrow(other.handle); + val selfEdges: List = listOf() + val bEdges: List = listOf(other) + val aEdges: List = listOf(other) + val handle = returnVal + val returnOpaque = BorrowWrapper(handle, selfEdges, bEdges, aEdges) + CLEANER.register(returnOpaque, BorrowWrapper.BorrowWrapperCleaner(handle, BorrowWrapper.lib)); + + return returnOpaque + } + } + + fun copy(borrow: MyOpaqueStruct): Int { + + val returnVal = lib.MyOpaqueStruct_copy(handle, borrow.handle); + return returnVal + } + + fun doStuff(in1: Int): Double { + + val returnVal = lib.MyOpaqueStruct_do_stuff(handle, in1); + return returnVal + } + + fun borrow(): BorrowWrapper { + + val returnVal = lib.MyOpaqueStruct_borrow(handle); + val selfEdges: List = listOf() + val bEdges: List = listOf(this) + val aEdges: List = listOf(this) + val handle = returnVal + val returnOpaque = BorrowWrapper(handle, selfEdges, bEdges, aEdges) + CLEANER.register(returnOpaque, BorrowWrapper.BorrowWrapperCleaner(handle, BorrowWrapper.lib)); + + return returnOpaque + } + + fun borrow2(): MyOpaqueStruct { + + val returnVal = lib.MyOpaqueStruct_borrow2(handle); + val selfEdges: List = listOf(this) + val bEdges: List = listOf(this) + val handle = returnVal + val returnOpaque = MyOpaqueStruct(handle, selfEdges, bEdges) + return returnOpaque + } + + fun borrow3(): String { + val write = DW.lib.diplomat_buffer_write_create(0) + val returnVal = lib.MyOpaqueStruct_borrow3(handle, write); + + val returnString = DW.writeToString(write) + return returnString + } + + fun stringStuff(someStr: String): MyOpaqueStruct { + val (someStrMem, someStrSlice) = PrimitiveArrayTools.readUtf8(someStr) + + val returnVal = lib.MyOpaqueStruct_string_stuff(handle, someStrSlice); + val selfEdges: List = listOf(this) + listOf(someStrMem) + val bEdges: List = listOf(this) + val handle = returnVal + val returnOpaque = MyOpaqueStruct(handle, selfEdges, bEdges) + return returnOpaque + } + + fun stringStuff2(someStr: String): MyOpaqueStruct { + val (someStrMem, someStrSlice) = PrimitiveArrayTools.readUtf8(someStr) + + val returnVal = lib.MyOpaqueStruct_string_stuff_2(handle, someStrSlice); + val selfEdges: List = listOf(this) + val bEdges: List = listOf(this) + val handle = returnVal + val returnOpaque = MyOpaqueStruct(handle, selfEdges, bEdges)someStrMem.close() + return returnOpaque + } + +}