Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix kotlin trait method and cb struct and enum returns #705

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tool/src/kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(", "),
Expand All @@ -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,
})
}
_ => (),
Expand Down Expand Up @@ -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(", "),
Expand Down Expand Up @@ -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,
}

Expand All @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tool/src/kotlin/mod.rs
assertion_line: 2335
assertion_line: 2380
expression: result
---
package dev.gigapixel.somelib
Expand All @@ -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;
}


Expand All @@ -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) {
Expand Down Expand Up @@ -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<String> {
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")
}
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tool/templates/kotlin/Callback.kt.jinja
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.")
}
}
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tool/templates/kotlin/Trait.kt.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -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}};
Expand Down