Skip to content

Commit

Permalink
re: code review, and making usability better
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen Arteca committed Sep 13, 2024
1 parent b0d065e commit 1703505
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 64 deletions.

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

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

17 changes: 17 additions & 0 deletions tool/src/kotlin/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ impl<'tcx> KotlinFormatter<'tcx> {
}
}

pub fn fmt_trait_method_name<'a>(&self, method: &'a hir::Callback) -> Cow<'a, str> {
if method.name.is_none() {
panic!("Trait methods need a name");
}
let name = method.name.clone().unwrap().as_str().to_lower_camel_case();
let name = if method.attrs.is_some() {
method.attrs.as_ref().unwrap().rename.apply(name.into())
} else {
name.into()
};
if INVALID_METHOD_NAMES.contains(&&*name) {
format!("{name}_").into()
} else {
name
}
}

pub fn fmt_param_name<'a>(&self, ident: &'a str) -> Cow<'tcx, str> {
ident.to_lower_camel_case().into()
}
Expand Down
18 changes: 15 additions & 3 deletions tool/src/kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,13 @@ impl<'a, 'cx> TyGenContext<'a, 'cx> {
}
}
Type::Struct(_) => format!("{name}.nativeStruct").into(),
Type::ImplTrait(_) => format!("{name}.nativeStruct").into(),
Type::ImplTrait(ref trt) => {
let trait_id = trt.id();
let resolved = self.tcx.resolve_trait(trait_id);
let trait_name = resolved.name.to_string();
format!("DiplomatTrait_{trait_name}_Wrapper.fromTraitObj({name}).nativeStruct")
.into()
}
Type::Enum(_) => format!("{name}.toNative()").into(),
Type::Slice(Slice::Str(None, _)) | Type::Slice(Slice::Primitive(None, _)) => {
format!("{name}Slice").into()
Expand Down Expand Up @@ -1457,7 +1463,7 @@ retutnVal.option() ?: return null
if method.name.is_none() {
panic!("Trait methods need a name");
}
let method_name = method.name.clone().unwrap().to_string();
let method_name = self.formatter.fmt_trait_method_name(method).into();
let param_input_types: Vec<String> = method
.params
.iter()
Expand Down Expand Up @@ -1777,8 +1783,9 @@ retutnVal.option() ?: return null
}

/// Generate the non-diplomat name for a type -- this only applies to
/// callback types. So: for a callback, instead of returning `DiplomatCallback_...`
/// callback and trait types. So: for a callback, instead of returning `DiplomatCallback_...`
/// it returns `(input types)->output type`.
/// And for traits instead of returning `DiplomatTrait_<TraitName>...` it returns TraitName
/// For all non-callback types it returns the same result as `gen_type_name`.
fn gen_non_wrapped_type_name(
&self,
Expand All @@ -1803,6 +1810,11 @@ retutnVal.option() ?: return null
};
format!("({})->{}", in_type_string, out_type_string).into()
}
Type::ImplTrait(trt) => {
let trait_id = trt.id();
let resolved = self.tcx.resolve_trait(trait_id);
resolved.name.to_string().into()
}
_ => self.gen_type_name(ty, additional_name),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tool/src/kotlin/mod.rs
assertion_line: 2228
assertion_line: 2256
expression: result
---
package dev.gigapixel.somelib
Expand All @@ -12,29 +12,29 @@ import com.sun.jna.Pointer
import com.sun.jna.Structure

interface TesterTrait {
fun test_trait_fn(x: Int): Int;
fun test_void_trait_fn(): Unit;
fun test_struct_trait_fn(s: TraitTestingStruct): Int;
fun testTraitFn(x: Int): Int;
fun testVoidTraitFn(): Unit;
fun testStructTraitFn(s: TraitTestingStruct): Int;
}


internal interface Runner_DiplomatTraitMethod_TesterTrait_test_trait_fn: Callback {
internal interface Runner_DiplomatTraitMethod_TesterTrait_testTraitFn: Callback {
fun invoke(ignored: Pointer?, x: Int ): Int
}
internal interface Runner_DiplomatTraitMethod_TesterTrait_test_void_trait_fn: Callback {
internal interface Runner_DiplomatTraitMethod_TesterTrait_testVoidTraitFn: Callback {
fun invoke(ignored: Pointer?): Unit
}
internal interface Runner_DiplomatTraitMethod_TesterTrait_test_struct_trait_fn: Callback {
internal interface Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn: Callback {
fun invoke(ignored: Pointer?, s: TraitTestingStructNative ): Int
}

object TesterTrait_VTable_destructor: Callback {
internal object TesterTrait_VTable_destructor: Callback {
fun invoke(obj_pointer: Pointer) {
DiplomatJVMRuntime.dropRustCookie(obj_pointer);
}
};

class DiplomatTrait_TesterTrait_VTable_Native: Structure(), Structure.ByValue {
internal class DiplomatTrait_TesterTrait_VTable_Native: Structure(), Structure.ByValue {
@JvmField
internal var destructor: Callback = TesterTrait_VTable_destructor;
@JvmField
Expand All @@ -43,33 +43,33 @@ class DiplomatTrait_TesterTrait_VTable_Native: Structure(), Structure.ByValue {
internal var alignment: Pointer = Pointer(0L);

@JvmField
internal var run_test_trait_fn_callback: Runner_DiplomatTraitMethod_TesterTrait_test_trait_fn
= object : Runner_DiplomatTraitMethod_TesterTrait_test_trait_fn {
internal var run_testTraitFn_callback: Runner_DiplomatTraitMethod_TesterTrait_testTraitFn
= object : Runner_DiplomatTraitMethod_TesterTrait_testTraitFn {
override fun invoke(ignored: Pointer?, x: Int ): Int {
throw Exception("ERROR NOT IMPLEMENTED")
}
}
@JvmField
internal var run_test_void_trait_fn_callback: Runner_DiplomatTraitMethod_TesterTrait_test_void_trait_fn
= object : Runner_DiplomatTraitMethod_TesterTrait_test_void_trait_fn {
internal var run_testVoidTraitFn_callback: Runner_DiplomatTraitMethod_TesterTrait_testVoidTraitFn
= object : Runner_DiplomatTraitMethod_TesterTrait_testVoidTraitFn {
override fun invoke(ignored: Pointer?): Unit {
throw Exception("ERROR NOT IMPLEMENTED")
}
}
@JvmField
internal var run_test_struct_trait_fn_callback: Runner_DiplomatTraitMethod_TesterTrait_test_struct_trait_fn
= object : Runner_DiplomatTraitMethod_TesterTrait_test_struct_trait_fn {
internal var run_testStructTraitFn_callback: Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn
= object : Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn {
override fun invoke(ignored: Pointer?, s: TraitTestingStructNative ): Int {
throw Exception("ERROR NOT IMPLEMENTED")
}
}
// Define the fields of the struct
override fun getFieldOrder(): List<String> {
return listOf("destructor", "size", "alignment", "run_test_trait_fn_callback", "run_test_void_trait_fn_callback", "run_test_struct_trait_fn_callback")
return listOf("destructor", "size", "alignment", "run_testTraitFn_callback", "run_testVoidTraitFn_callback", "run_testStructTraitFn_callback")
}
}

class DiplomatTrait_TesterTrait_Wrapper_Native: Structure(), Structure.ByValue {
internal class DiplomatTrait_TesterTrait_Wrapper_Native: Structure(), Structure.ByValue {
@JvmField
internal var data_: Pointer = Pointer(0L);
@JvmField
Expand All @@ -82,7 +82,7 @@ class DiplomatTrait_TesterTrait_Wrapper_Native: Structure(), Structure.ByValue {
}
}

class DiplomatTrait_TesterTrait_Wrapper internal constructor (
internal class DiplomatTrait_TesterTrait_Wrapper internal constructor (
internal val nativeStruct: DiplomatTrait_TesterTrait_Wrapper_Native) {
val data_: Pointer = nativeStruct.data_
val vtable: DiplomatTrait_TesterTrait_VTable_Native = nativeStruct.vtable
Expand All @@ -94,24 +94,24 @@ class DiplomatTrait_TesterTrait_Wrapper internal constructor (
val vtable = DiplomatTrait_TesterTrait_VTable_Native()


val test_trait_fn: Runner_DiplomatTraitMethod_TesterTrait_test_trait_fn = object : Runner_DiplomatTraitMethod_TesterTrait_test_trait_fn {
val testTraitFn: Runner_DiplomatTraitMethod_TesterTrait_testTraitFn = object : Runner_DiplomatTraitMethod_TesterTrait_testTraitFn {
override fun invoke(ignored: Pointer?, x: Int ): Int {
return trt_obj.test_trait_fn(x);
return trt_obj.testTraitFn(x);
}
}
vtable.run_test_trait_fn_callback = test_trait_fn;
val test_void_trait_fn: Runner_DiplomatTraitMethod_TesterTrait_test_void_trait_fn = object : Runner_DiplomatTraitMethod_TesterTrait_test_void_trait_fn {
vtable.run_testTraitFn_callback = testTraitFn;
val testVoidTraitFn: Runner_DiplomatTraitMethod_TesterTrait_testVoidTraitFn = object : Runner_DiplomatTraitMethod_TesterTrait_testVoidTraitFn {
override fun invoke(ignored: Pointer?): Unit {
return trt_obj.test_void_trait_fn();
return trt_obj.testVoidTraitFn();
}
}
vtable.run_test_void_trait_fn_callback = test_void_trait_fn;
val test_struct_trait_fn: Runner_DiplomatTraitMethod_TesterTrait_test_struct_trait_fn = object : Runner_DiplomatTraitMethod_TesterTrait_test_struct_trait_fn {
vtable.run_testVoidTraitFn_callback = testVoidTraitFn;
val testStructTraitFn: Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn = object : Runner_DiplomatTraitMethod_TesterTrait_testStructTraitFn {
override fun invoke(ignored: Pointer?, s: TraitTestingStructNative ): Int {
return trt_obj.test_struct_trait_fn(TraitTestingStruct(s));
return trt_obj.testStructTraitFn(TraitTestingStruct(s));
}
}
vtable.run_test_struct_trait_fn_callback = test_struct_trait_fn;
vtable.run_testStructTraitFn_callback = testStructTraitFn;
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/Trait.kt.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ internal interface Runner_DiplomatTraitMethod_{{trait_name}}_{{trait_method.name
{%- endfor %}
{%- endif %}

object {{trait_name}}_VTable_destructor: Callback {
internal object {{trait_name}}_VTable_destructor: Callback {
fun invoke(obj_pointer: Pointer) {
DiplomatJVMRuntime.dropRustCookie(obj_pointer);
}
};

class DiplomatTrait_{{trait_name}}_VTable_Native: Structure(), Structure.ByValue {
internal class DiplomatTrait_{{trait_name}}_VTable_Native: Structure(), Structure.ByValue {
@JvmField
internal var destructor: Callback = {{trait_name}}_VTable_destructor;
@JvmField
Expand All @@ -57,7 +57,7 @@ class DiplomatTrait_{{trait_name}}_VTable_Native: Structure(), Structure.ByValue
}
}

class DiplomatTrait_{{trait_name}}_Wrapper_Native: Structure(), Structure.ByValue {
internal class DiplomatTrait_{{trait_name}}_Wrapper_Native: Structure(), Structure.ByValue {
@JvmField
internal var data_: Pointer = Pointer(0L);
@JvmField
Expand All @@ -70,7 +70,7 @@ class DiplomatTrait_{{trait_name}}_Wrapper_Native: Structure(), Structure.ByValu
}
}

class DiplomatTrait_{{trait_name}}_Wrapper internal constructor (
internal class DiplomatTrait_{{trait_name}}_Wrapper internal constructor (
internal val nativeStruct: DiplomatTrait_{{trait_name}}_Wrapper_Native) {
val data_: Pointer = nativeStruct.data_
val vtable: DiplomatTrait_{{trait_name}}_VTable_Native = nativeStruct.vtable
Expand Down

0 comments on commit 1703505

Please sign in to comment.