fix invalid code generation when type alias is fixed-array

This commit is contained in:
neuecc 2023-03-23 00:58:57 +09:00
parent 6b81a0779b
commit 1bd4f42ba2
7 changed files with 89 additions and 12 deletions

View File

@ -26,7 +26,7 @@ Install on `Cargo.toml` as `build-dependencies` and set up `bindgen::Builder` on
```toml ```toml
[build-dependencies] [build-dependencies]
csbindgen = "1.5.0" csbindgen = "1.6.0"
``` ```
### Rust to C#. ### Rust to C#.

View File

@ -73,6 +73,37 @@ mod lz4_ffi;
// println!("{}", str); // println!("{}", str);
// } // }
#[allow(non_camel_case_types)]
pub type png_byte = ::std::os::raw::c_uchar;
#[allow(non_camel_case_types)]
pub type JPH_ContactPoints = [u128; 65usize];
#[allow(non_camel_case_types)]
pub type JPH_ContactPoints2 = [u128; 65];
#[repr(C)]
#[allow(unused)]
#[allow(non_snake_case)]
pub struct JPH_ContactManifold {
pub mPenetrationDepth: f32,
pub mWorldSpaceContactPointsOn1: JPH_ContactPoints,
pub mWorldSpaceContactPointsOn2: JPH_ContactPoints,
pub mWorldSpaceContactPointsOn3: JPH_ContactPoints2,
pub mWorldSpaceContactPointsOn4: [u128; 65],
pub mWorldSpaceContactPointsOn5: [u32; 65],
pub png_name: [png_byte; 5usize],
}
#[no_mangle]
#[allow(unused)]
#[allow(non_snake_case)]
pub extern "C" fn JPH_PruneContactPoints(
ioContactPointsOn1: *mut JPH_ContactPoints,
ioContactPointsOn2: *mut JPH_ContactManifold,
)
{
todo!();
}
/// my comment! /// my comment!

View File

@ -1,6 +1,6 @@
[package] [package]
name = "csbindgen" name = "csbindgen"
version = "1.5.0" version = "1.6.0"
edition = "2021" edition = "2021"
authors = [ authors = [
"Yoshifumi Kawai <ils@neue.cc>", "Yoshifumi Kawai <ils@neue.cc>",

View File

@ -54,10 +54,6 @@ impl AliasMap {
None => name.clone(), None => name.clone(),
} }
} }
pub fn iter(&self) -> std::collections::hash_map::Iter<String, RustType> {
self.type_aliases.iter()
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -224,8 +224,16 @@ pub fn emit_csharp(
"".to_string() "".to_string()
}; };
structs_string structs_string.push_str(
.push_str(format!(" {}public {} {}", attr, type_name, escape_name(field.name.as_str())).as_str()); format!(
" {}public {} {}",
attr,
type_name,
escape_name(field.name.as_str())
)
.as_str(),
);
if let TypeKind::FixedArray(digits, _) = &field.rust_type.type_kind { if let TypeKind::FixedArray(digits, _) = &field.rust_type.type_kind {
let mut digits = digits.clone(); let mut digits = digits.clone();
if digits == "0" { if digits == "0" {
@ -233,7 +241,23 @@ pub fn emit_csharp(
}; };
structs_string.push_str(format!("[{}]", digits).as_str()); structs_string.push_str(format!("[{}]", digits).as_str());
} else {
let alias_resolved_field =
match aliases.get_mapped_value(&field.rust_type.type_name) {
Some(x) => x,
None => field.rust_type.clone(),
};
if let TypeKind::FixedArray(digits, _) = &alias_resolved_field.type_kind {
let mut digits = digits.clone();
if digits == "0" {
digits = "1".to_string(); // 0 fixed array is not allowed in C#
};
structs_string.push_str(format!("[{}]", digits).as_str());
}
} }
structs_string.push_str_ln(";"); structs_string.push_str_ln(";");
} }
structs_string.push_str_ln(" }"); structs_string.push_str_ln(" }");

View File

@ -257,17 +257,28 @@ impl RustType {
match &self.type_kind { match &self.type_kind {
TypeKind::FixedArray(_, _) => { TypeKind::FixedArray(_, _) => {
sb.push_str("fixed "); if emit_from_struct {
sb.push_str("fixed ");
let type_name = type_csharp_string.as_str(); let type_name = type_csharp_string.as_str();
let type_name = match type_name { let type_name = match type_name {
// C# fixed allow types // C# fixed allow types
"bool" | "byte" | "short" | "int" | "long" | "char" | "sbyte" | "ushort" "bool" | "byte" | "short" | "int" | "long" | "char" | "sbyte" | "ushort"
| "uint" | "ulong" | "float" | "double" => type_name.to_owned(), | "uint" | "ulong" | "float" | "double" => type_name.to_owned(),
_ => format!("byte/* {}, this length is invalid so must keep pointer and can't edit from C# */", type_name) _ => format!("byte/* {}, this length is invalid so must keep pointer and can't edit from C# */", type_name)
}; };
sb.push_str(type_name.as_str()); sb.push_str(type_name.as_str());
} else {
let type_name = type_csharp_string.as_str();
sb.push_str(
format!(
"void/* {}[] */",
type_name
)
.as_str(),
);
}
} }
TypeKind::Function(parameters, return_type) => { TypeKind::Function(parameters, return_type) => {
if emit_from_struct && !options.csharp_use_function_pointer { if emit_from_struct && !options.csharp_use_function_pointer {

View File

@ -12,6 +12,9 @@ namespace CsBindgen
{ {
const string __DllName = "csbindgen_tests"; const string __DllName = "csbindgen_tests";
[DllImport(__DllName, EntryPoint = "JPH_PruneContactPoints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void JPH_PruneContactPoints(void/* UInt128[] */* ioContactPointsOn1, JPH_ContactManifold* ioContactPointsOn2);
/// <summary>my comment!</summary> /// <summary>my comment!</summary>
[DllImport(__DllName, EntryPoint = "comment_one", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "comment_one", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void comment_one(); public static extern void comment_one();
@ -141,6 +144,18 @@ namespace CsBindgen
} }
[StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct JPH_ContactManifold
{
public float mPenetrationDepth;
public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn1[65];
public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn2[65];
public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn3[65];
public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn4[65];
public fixed uint mWorldSpaceContactPointsOn5[65];
public fixed byte png_name[5];
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct NfcCard internal unsafe partial struct NfcCard
{ {