From e31636c0029f047854b346393e6f02dfc88e5ffc Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Fri, 4 Oct 2024 11:15:56 -0700 Subject: [PATCH] Fix kotlin trait method and cb struct and enum returns --- tool/src/kotlin/mod.rs | 38 +++++++++++++++++++ ...iplomat_tool__kotlin__test__trait_gen.snap | 38 ++++++++++++++++++- tool/templates/kotlin/Callback.kt.jinja | 8 ++-- tool/templates/kotlin/Trait.kt.jinja | 8 ++-- 4 files changed, 82 insertions(+), 10 deletions(-) diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 7c6af3653..a72b9ccea 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -1075,6 +1075,18 @@ returnVal.option() ?: return null _ => (in_name.clone(), format!("{}: {}", in_name, in_ty)), }) .unzip(); + let (native_output_type, return_modification) = match **output { + Some(ref ty) => ( + self.gen_native_type_name(ty, None).into(), + match ty { + Type::Enum(..) => ".toNative()", + Type::Struct(..) => ".nativeStruct", + _ => "", + } + .into(), + ), + None => ("Unit".into(), "".into()), + }; self.callback_params.push(CallbackParamInfo { name: "DiplomatCallback_".to_owned() + &additional_name.clone().unwrap(), input_types: param_input_types.join(", "), @@ -1084,6 +1096,8 @@ returnVal.option() ?: return null }, native_input_params_and_types: native_input_params_and_types.join(", "), native_input_names: native_input_names.join(", "), + native_output_type, + return_modification, }) } _ => (), @@ -1533,12 +1547,26 @@ returnVal.option() ?: return null + (if !cur.is_empty() { ", " } else { "" }) + &format!("{}: {}", in_name, in_ty) }); + let (native_output_type, return_modification) = match *method.output { + Some(ref ty) => ( + self.gen_native_type_name(ty, None).into(), + match ty { + Type::Enum(..) => ".toNative()", + Type::Struct(..) => ".nativeStruct", + _ => "", + } + .into(), + ), + None => ("Unit".into(), "".into()), + }; TraitMethodInfo { name: method_name, output_type: match *method.output { Some(ref ty) => self.gen_type_name(ty, None).into(), None => "Unit".into(), }, + native_output_type, + return_modification, input_params_and_types: native_input_params_and_types.join(", "), non_native_params_and_types, input_params: native_input_names.join(", "), @@ -1917,6 +1945,8 @@ struct TraitMethodInfo { input_params_and_types: String, input_params: String, output_type: String, + native_output_type: String, + return_modification: String, non_native_params_and_types: String, } @@ -1928,6 +1958,8 @@ struct CallbackParamInfo { native_input_params_and_types: String, native_input_names: String, output_type: String, + native_output_type: String, + return_modification: String, } #[cfg(test)] @@ -2299,11 +2331,17 @@ mod test { x: i32, y: i32, } + pub enum TraitTestingEnum { + One, + Two, + } pub trait TesterTrait { fn test_trait_fn(&self, x: i32, y: i32, z: u8) -> i32; fn test_void_trait_fn(&self); fn test_struct_trait_fn(&self, s: TraitTestingStruct) -> i32; fn test_with_slices(&mut self, a: &[u8], b: &[i16]) -> i32; + fn test_struct_return(&self) -> TraitTestingStruct; + fn test_enum_return(&self) -> TraitTestingEnum; } pub struct Wrapper { cant_be_empty: bool, diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__trait_gen.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__trait_gen.snap index bb8da2558..7a052aacf 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__trait_gen.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__trait_gen.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2335 +assertion_line: 2380 expression: result --- package dev.gigapixel.somelib @@ -16,6 +16,8 @@ interface TesterTrait { fun testVoidTraitFn(): Unit; fun testStructTraitFn(s: TraitTestingStruct): Int; fun testWithSlices(a: UByteArray, b: ShortArray): Int; + fun testStructReturn(): TraitTestingStruct; + fun testEnumReturn(): TraitTestingEnum; } @@ -31,6 +33,12 @@ internal interface Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn: Cal internal interface Runner_DiplomatTraitMethod_TesterTrait_testWithSlices: Callback { fun invoke(ignored: Pointer?, a: Slice, b: Slice ): Int } +internal interface Runner_DiplomatTraitMethod_TesterTrait_testStructReturn: Callback { + fun invoke(ignored: Pointer?): TraitTestingStructNative +} +internal interface Runner_DiplomatTraitMethod_TesterTrait_testEnumReturn: Callback { + fun invoke(ignored: Pointer?): Int +} internal object TesterTrait_VTable_destructor: Callback { fun invoke(obj_pointer: Pointer) { @@ -74,9 +82,23 @@ internal class DiplomatTrait_TesterTrait_VTable_Native: Structure(), Structure.B throw Exception("ERROR NOT IMPLEMENTED") } } + @JvmField + internal var run_testStructReturn_callback: Runner_DiplomatTraitMethod_TesterTrait_testStructReturn + = object : Runner_DiplomatTraitMethod_TesterTrait_testStructReturn { + override fun invoke(ignored: Pointer?): TraitTestingStructNative { + throw Exception("ERROR NOT IMPLEMENTED") + } + } + @JvmField + internal var run_testEnumReturn_callback: Runner_DiplomatTraitMethod_TesterTrait_testEnumReturn + = object : Runner_DiplomatTraitMethod_TesterTrait_testEnumReturn { + override fun invoke(ignored: Pointer?): Int { + throw Exception("ERROR NOT IMPLEMENTED") + } + } // Define the fields of the struct override fun getFieldOrder(): List { - return listOf("destructor", "size", "alignment", "run_testTraitFn_callback", "run_testVoidTraitFn_callback", "run_testStructTraitFn_callback", "run_testWithSlices_callback") + return listOf("destructor", "size", "alignment", "run_testTraitFn_callback", "run_testVoidTraitFn_callback", "run_testStructTraitFn_callback", "run_testWithSlices_callback", "run_testStructReturn_callback", "run_testEnumReturn_callback") } } @@ -129,6 +151,18 @@ internal class DiplomatTrait_TesterTrait_Wrapper internal constructor ( } } vtable.run_testWithSlices_callback = testWithSlices; + val testStructReturn: Runner_DiplomatTraitMethod_TesterTrait_testStructReturn = object : Runner_DiplomatTraitMethod_TesterTrait_testStructReturn { + override fun invoke(ignored: Pointer?): TraitTestingStructNative { + return trt_obj.testStructReturn().nativeStruct; + } + } + vtable.run_testStructReturn_callback = testStructReturn; + val testEnumReturn: Runner_DiplomatTraitMethod_TesterTrait_testEnumReturn = object : Runner_DiplomatTraitMethod_TesterTrait_testEnumReturn { + override fun invoke(ignored: Pointer?): Int { + return trt_obj.testEnumReturn().toNative(); + } + } + vtable.run_testEnumReturn_callback = testEnumReturn; val native_wrapper = DiplomatTrait_TesterTrait_Wrapper_Native(); native_wrapper.vtable = vtable; native_wrapper.data_ = DiplomatJVMRuntime.buildRustCookie(vtable as Object); diff --git a/tool/templates/kotlin/Callback.kt.jinja b/tool/templates/kotlin/Callback.kt.jinja index 120f1aae0..4d1532eef 100644 --- a/tool/templates/kotlin/Callback.kt.jinja +++ b/tool/templates/kotlin/Callback.kt.jinja @@ -1,6 +1,6 @@ internal interface Runner_{{name}}: Callback { - fun invoke(lang_specific_context: Pointer?{% if native_input_params_and_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{output_type}} + fun invoke(lang_specific_context: Pointer?{% if native_input_params_and_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{native_output_type}} } internal class {{name}}_Native: Structure(), Structure.ByValue { @@ -9,7 +9,7 @@ internal class {{name}}_Native: Structure(), Structure.ByValue { @JvmField internal var run_callback: Runner_{{name}} = object : Runner_{{name}} { - override fun invoke(lang_specific_context: Pointer?{% if input_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{output_type}} { + override fun invoke(lang_specific_context: Pointer?{% if input_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{native_output_type}} { throw Exception("Default callback runner -- should be replaced.") } } @@ -37,8 +37,8 @@ internal class {{name}} internal constructor ( fun fromCallback(cb: ({{input_types}})->{{output_type}}): {{name}} { val callback: Runner_{{name}} = object : Runner_{{name}} { - override fun invoke(lang_specific_context: Pointer?{% if input_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{output_type}} { - return cb({{native_input_names}}); + override fun invoke(lang_specific_context: Pointer?{% if input_types != "" %}, {{native_input_params_and_types}} {% endif %}): {{native_output_type}} { + return cb({{native_input_names}}){{return_modification}}; } } val cb_wrap = {{name}}_Native() diff --git a/tool/templates/kotlin/Trait.kt.jinja b/tool/templates/kotlin/Trait.kt.jinja index 781d6d6fc..f7038729e 100644 --- a/tool/templates/kotlin/Trait.kt.jinja +++ b/tool/templates/kotlin/Trait.kt.jinja @@ -21,7 +21,7 @@ interface {{trait_name}} { {% if !trait_methods.is_empty() -%} {%- for trait_method in trait_methods %} internal interface Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name}}: Callback { - fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.output_type}} + fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.native_output_type}} } {%- endfor %} {%- endif %} @@ -44,7 +44,7 @@ internal class DiplomatTrait_{{trait_name}}_VTable_Native: Structure(), Structur @JvmField internal var run_{{trait_method.name}}_callback: Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name}} = object : Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name}} { - override fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.output_type}} { + override fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.native_output_type}} { throw Exception("ERROR NOT IMPLEMENTED") } } @@ -84,8 +84,8 @@ internal class DiplomatTrait_{{trait_name}}_Wrapper internal constructor ( {% if !trait_methods.is_empty() -%} {%- for trait_method in trait_methods %} val {{trait_method.name}}: Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name}} = object : Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name}} { - override fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.output_type}} { - return trt_obj.{{trait_method.name}}({{trait_method.input_params}}); + override fun invoke(ignored: Pointer?{% if trait_method.input_params_and_types != "" %}, {{trait_method.input_params_and_types}} {% endif %}): {{trait_method.native_output_type}} { + return trt_obj.{{trait_method.name}}({{trait_method.input_params}}){{trait_method.return_modification}}; } } vtable.run_{{trait_method.name}}_callback = {{trait_method.name}};