From 57e8916ccc8995167ee443272e3fd8de3d64e5d3 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Mon, 16 Sep 2024 18:36:03 -0700 Subject: [PATCH] Fixing bug in opaque/struct cleanup codegen in kotlin (#690) * Fixing bug in opaque/struct cleanup codegen in kotlin * filename typo * regen tests --------- Co-authored-by: Ellen Arteca --- .../dev/diplomattest/somelib/DataProvider.kt | 1 - .../dev/diplomattest/somelib/FixedDecimal.kt | 1 - .../somelib/FixedDecimalFormatter.kt | 1 - .../dev/diplomattest/somelib/AttrOpaque1.kt | 1 - .../dev/diplomattest/somelib/Float64Vec.kt | 1 - .../kotlin/dev/diplomattest/somelib/Foo.kt | 4 -- .../dev/diplomattest/somelib/MyIterable.kt | 1 - .../dev/diplomattest/somelib/MyString.kt | 1 - .../kotlin/dev/diplomattest/somelib/One.kt | 11 ---- .../kotlin/dev/diplomattest/somelib/Opaque.kt | 1 - .../diplomattest/somelib/OpaqueIterable.kt | 1 - .../diplomattest/somelib/OpaqueIterator.kt | 1 - .../somelib/OpaqueMutexedString.kt | 2 - .../dev/diplomattest/somelib/OptionOpaque.kt | 2 - .../dev/diplomattest/somelib/RefList.kt | 1 - .../dev/diplomattest/somelib/ResultOpaque.kt | 7 --- .../dev/diplomattest/somelib/Unnamespaced.kt | 1 - tool/src/kotlin/mod.rs | 49 ++++++++++++++++++ ...n__test__opaque_gen_multiple_ref_args.snap | 51 +++++++++++++++++++ tool/templates/kotlin/OpaqueReturn.kt.jinja | 10 ++-- 20 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt index 3ac2e1f18..41862a75e 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt @@ -36,7 +36,6 @@ class DataProvider internal constructor ( val handle = returnVal val returnOpaque = DataProvider(handle, selfEdges) CLEANER.register(returnOpaque, DataProvider.DataProviderCleaner(handle, DataProvider.lib)); - return returnOpaque } diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt index 172d78ebb..9d807c2c5 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt @@ -37,7 +37,6 @@ class FixedDecimal internal constructor ( val handle = returnVal val returnOpaque = FixedDecimal(handle, selfEdges) CLEANER.register(returnOpaque, FixedDecimal.FixedDecimalCleaner(handle, FixedDecimal.lib)); - return returnOpaque } } diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt index 9b12cbe82..49f3416ac 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt @@ -37,7 +37,6 @@ class FixedDecimalFormatter internal constructor ( val handle = returnVal.union.ok val returnOpaque = FixedDecimalFormatter(handle, selfEdges) CLEANER.register(returnOpaque, FixedDecimalFormatter.FixedDecimalFormatterCleaner(handle, FixedDecimalFormatter.lib)); - return returnOpaque.ok() } else { return Err(Unit) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt index be8abb371..e3e917b78 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt @@ -39,7 +39,6 @@ class AttrOpaque1 internal constructor ( val handle = returnVal val returnOpaque = AttrOpaque1(handle, selfEdges) CLEANER.register(returnOpaque, AttrOpaque1.AttrOpaque1Cleaner(handle, AttrOpaque1.lib)); - return returnOpaque } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt index f0950d696..be3e7379c 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt @@ -120,7 +120,6 @@ class Float64Vec internal constructor ( val handle = returnVal val returnOpaque = Float64Vec(handle, selfEdges) CLEANER.register(returnOpaque, Float64Vec.Float64VecCleaner(handle, Float64Vec.lib)); - return returnOpaque } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt index 88e967975..6218fd049 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt @@ -43,7 +43,6 @@ class Foo internal constructor ( val handle = returnVal val returnOpaque = Foo(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, Foo.FooCleaner(handle, Foo.lib)); - return returnOpaque } @@ -68,7 +67,6 @@ class Foo internal constructor ( val handle = returnVal val returnOpaque = Foo(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, Foo.FooCleaner(handle, Foo.lib)); - return returnOpaque } @@ -81,7 +79,6 @@ class Foo internal constructor ( val handle = returnVal val returnOpaque = Foo(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, Foo.FooCleaner(handle, Foo.lib)); - return returnOpaque } } @@ -95,7 +92,6 @@ class Foo internal constructor ( val handle = returnVal val returnOpaque = Bar(handle, selfEdges, bEdges, aEdges) CLEANER.register(returnOpaque, Bar.BarCleaner(handle, Bar.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt index 1a7bf677b..99c39f5e1 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt @@ -50,7 +50,6 @@ class MyIterable internal constructor ( val handle = returnVal val returnOpaque = MyIterator(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, MyIterator.MyIteratorCleaner(handle, MyIterator.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt index 129bdacba..4db862ea3 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt @@ -66,7 +66,6 @@ class MyString internal constructor ( val handle = returnVal val returnOpaque = MyString(handle, selfEdges) CLEANER.register(returnOpaque, MyString.MyStringCleaner(handle, MyString.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt index 2c49a0f27..54a7a5e88 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt @@ -47,7 +47,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -59,7 +58,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -71,7 +69,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -83,7 +80,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, longEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -95,7 +91,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, topEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -107,7 +102,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, leftEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -119,7 +113,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, rightEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -131,7 +124,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, bottomEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -143,7 +135,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -155,7 +146,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } @@ -167,7 +157,6 @@ class One internal constructor ( val handle = returnVal val returnOpaque = One(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, One.OneCleaner(handle, One.lib)); - return returnOpaque } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt index ed1b65b47..525f6c535 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt @@ -42,7 +42,6 @@ class Opaque internal constructor ( val handle = returnVal val returnOpaque = Opaque(handle, selfEdges) CLEANER.register(returnOpaque, Opaque.OpaqueCleaner(handle, Opaque.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt index a38e0f7d8..08c254eed 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt @@ -37,7 +37,6 @@ class OpaqueIterable internal constructor ( val handle = returnVal val returnOpaque = OpaqueIterator(handle, selfEdges, aEdges) CLEANER.register(returnOpaque, OpaqueIterator.OpaqueIteratorCleaner(handle, OpaqueIterator.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt index 20aed3464..f869f06ac 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt @@ -38,7 +38,6 @@ class OpaqueIterator internal constructor ( val handle = returnVal ?: return null val returnOpaque = AttrOpaque1(handle, selfEdges) CLEANER.register(returnOpaque, AttrOpaque1.AttrOpaque1Cleaner(handle, AttrOpaque1.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt index 1723907d3..c81c6b075 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt @@ -42,7 +42,6 @@ class OpaqueMutexedString internal constructor ( val handle = returnVal val returnOpaque = OpaqueMutexedString(handle, selfEdges) CLEANER.register(returnOpaque, OpaqueMutexedString.OpaqueMutexedStringCleaner(handle, OpaqueMutexedString.lib)); - return returnOpaque } @@ -99,7 +98,6 @@ class OpaqueMutexedString internal constructor ( val handle = returnVal val returnOpaque = Utf16Wrap(handle, selfEdges) CLEANER.register(returnOpaque, Utf16Wrap.Utf16WrapCleaner(handle, Utf16Wrap.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt index 0e31a91f3..04e94f5d9 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt @@ -45,7 +45,6 @@ class OptionOpaque internal constructor ( val handle = returnVal ?: return null val returnOpaque = OptionOpaque(handle, selfEdges) CLEANER.register(returnOpaque, OptionOpaque.OptionOpaqueCleaner(handle, OptionOpaque.lib)); - return returnOpaque } @@ -56,7 +55,6 @@ class OptionOpaque internal constructor ( val handle = returnVal ?: return null val returnOpaque = OptionOpaque(handle, selfEdges) CLEANER.register(returnOpaque, OptionOpaque.OptionOpaqueCleaner(handle, OptionOpaque.lib)); - return returnOpaque } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt index 5b711dda2..4cac78c46 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt @@ -37,7 +37,6 @@ class RefList internal constructor ( val handle = returnVal val returnOpaque = RefList(handle, selfEdges, bEdges) CLEANER.register(returnOpaque, RefList.RefListCleaner(handle, RefList.lib)); - return returnOpaque } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt index fb709fffd..cca9c9921 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt @@ -44,7 +44,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.ok val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.ok() } else { return ErrorEnum.fromNative(returnVal.union.err).err() @@ -59,7 +58,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.ok val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.ok() } else { return ErrorEnum.fromNative(returnVal.union.err).err() @@ -74,7 +72,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.ok val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.ok() } else { return ErrorEnum.fromNative(returnVal.union.err).err() @@ -89,7 +86,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.ok val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.ok() } else { return Err(Unit) @@ -104,7 +100,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.ok val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.ok() } else { @@ -123,7 +118,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.err val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.err() } } @@ -148,7 +142,6 @@ class ResultOpaque internal constructor ( val handle = returnVal.union.err val returnOpaque = ResultOpaque(handle, selfEdges) CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); - return returnOpaque.err() } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt index 3730bc657..594babbbf 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt @@ -36,7 +36,6 @@ class Unnamespaced internal constructor ( val handle = returnVal val returnOpaque = Unnamespaced(handle, selfEdges) CLEANER.register(returnOpaque, Unnamespaced.UnnamespacedCleaner(handle, Unnamespaced.lib)); - return returnOpaque } } diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 36e8f8d01..13dd12a1c 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -2051,6 +2051,55 @@ mod test { } } + #[test] + fn test_opaque_gen_multiple_ref_args() { + let tk_stream = quote! { + #[diplomat::bridge] + mod ffi { + #[diplomat::opaque] + struct RustOwnedBytes { + my_bytes: Vec, + } + + #[diplomat::opaque] + struct AnotherOpaque { + my_bytes: Vec, + } + + impl AnotherOpaque { + // we need the 2 referenced inputs to make sure the cleaner + // code is not all on the same line + pub fn get_rust_owned_bytes(&self, a: &[u8], b: &[u8]) -> Box { + return Box::new(RustOwnedBytes{my_bytes: a.to_vec()}) + } + } + } + }; + let tcx = new_tcx(tk_stream); + let mut all_types = tcx.all_types(); + if let (_id, TypeDef::Opaque(opaque_def)) = all_types + .next() + .expect("Failed to generate first opaque def") + { + let eror_store = ErrorStore::default(); + let formatter = KotlinFormatter::new(&tcx, None); + let mut callback_params = Vec::new(); + let mut ty_gen_cx = TyGenContext { + tcx: &tcx, + formatter: &formatter, + result_types: RefCell::new(BTreeSet::new()), + option_types: RefCell::new(BTreeSet::new()), + errors: &eror_store, + callback_params: &mut callback_params, + }; + let type_name = opaque_def.name.to_string(); + // test that we can render and that it doesn't panic + let (_, result) = + ty_gen_cx.gen_opaque_def(opaque_def, &type_name, "dev.gigapixel", "somelib", false); + insta::assert_snapshot!(result) + } + } + #[test] fn test_opaque_gen() { let tk_stream = quote! { diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap new file mode 100644 index 000000000..3cda1b9c6 --- /dev/null +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap @@ -0,0 +1,51 @@ +--- +source: tool/src/kotlin/mod.rs +assertion_line: 2099 +expression: result +--- +package dev.gigapixel.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 AnotherOpaqueLib: Library { + fun AnotherOpaque_destroy(handle: Pointer) + fun AnotherOpaque_get_rust_owned_bytes(handle: Pointer, a: Slice, b: Slice): Pointer +} + +class AnotherOpaque 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 AnotherOpaqueCleaner(val handle: Pointer, val lib: AnotherOpaqueLib) : Runnable { + override fun run() { + lib.AnotherOpaque_destroy(handle) + } + } + + companion object { + internal val libClass: Class = AnotherOpaqueLib::class.java + internal val lib: AnotherOpaqueLib = Native.load("somelib", libClass) + } + + fun getRustOwnedBytes(a: UByteArray, b: UByteArray): RustOwnedBytes { + val (aMem, aSlice) = PrimitiveArrayTools.native(a) + val (bMem, bSlice) = PrimitiveArrayTools.native(b) + + val returnVal = lib.AnotherOpaque_get_rust_owned_bytes(handle, aSlice, bSlice); + val selfEdges: List = listOf() + val handle = returnVal + val returnOpaque = RustOwnedBytes(handle, selfEdges) + CLEANER.register(returnOpaque, RustOwnedBytes.RustOwnedBytesCleaner(handle, RustOwnedBytes.lib)); + aMem.close() + bMem.close() + return returnOpaque + } + +} diff --git a/tool/templates/kotlin/OpaqueReturn.kt.jinja b/tool/templates/kotlin/OpaqueReturn.kt.jinja index cd1a798e8..08025b2d0 100644 --- a/tool/templates/kotlin/OpaqueReturn.kt.jinja +++ b/tool/templates/kotlin/OpaqueReturn.kt.jinja @@ -15,8 +15,10 @@ val returnOpaque = {{return_type_name}}(handle, selfEdges{%- if !borrows.is_empt {%- if is_owned %} {%- if !use_finalizers_not_cleaners %} CLEANER.register(returnOpaque, {{return_type_name}}.{{return_type_name}}Cleaner(handle, {{return_type_name}}.lib)); -{% endif -%} -{% else -%} -{% endif -%} -{% for cleanup in cleanups %}{{cleanup|indent(4)}}{% endfor %} +{%- endif %} +{%- else -%} +{%- endif %} +{%- for cleanup in cleanups %} +{{cleanup|indent(4)}} +{%- endfor %} return returnOpaque{{return_type_modifier}}