From dabad3323ac03bca86d673852f6786a39cbbe725 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sat, 4 Mar 2023 14:54:48 +0900 Subject: [PATCH] struct bool emit MarshalAs --- csbindgen-tests/src/lib.rs | 31 +++++++++++++++++++++++-------- csbindgen/src/emitter.rs | 20 +++++++++++--------- dotnet-sandbox/method_call.cs | 12 ++++++++++++ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/csbindgen-tests/src/lib.rs b/csbindgen-tests/src/lib.rs index 9451f3a..409d4f8 100644 --- a/csbindgen-tests/src/lib.rs +++ b/csbindgen-tests/src/lib.rs @@ -1,5 +1,3 @@ -use std::io::Bytes; - #[allow(dead_code)] #[allow(non_snake_case)] #[allow(non_camel_case_types)] @@ -34,15 +32,15 @@ pub extern "C" fn my_bool( true } -#[no_mangle] -pub unsafe extern "C" fn new(x: *mut *mut Vec) { - let v = Box::new(Vec::new()); - *x = Box::into_raw(v); -} +// #[no_mangle] +// pub unsafe extern "C" fn new(x: *mut *mut Vec) { +// let v = Box::new(Vec::new()); +// *x = Box::into_raw(v); +// } #[no_mangle] pub unsafe extern "C" fn unsafe_return_string() -> *const u8 { -todo!(); + todo!(); } #[no_mangle] @@ -55,6 +53,23 @@ pub extern "C" fn unsafe_destroy_string(s: *mut String) { unsafe { Box::from_raw(s) }; } +#[no_mangle] +pub extern "C" fn create_context() -> *mut Context { + let ctx = Box::new(Context { foo: true }); + Box::into_raw(ctx) +} + +#[no_mangle] +pub extern "C" fn delete_context(context: *mut Context) { + unsafe { Box::from_raw(context) }; +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Context { + pub foo: bool, +} + #[test] fn build_test() { // let path = std::env::current_dir().unwrap(); diff --git a/csbindgen/src/emitter.rs b/csbindgen/src/emitter.rs index bf79d86..636e3ab 100644 --- a/csbindgen/src/emitter.rs +++ b/csbindgen/src/emitter.rs @@ -154,20 +154,22 @@ pub fn emit_csharp( structs_string .push_str_ln(format!(" [StructLayout(LayoutKind.{layout_kind})]").as_str()); - structs_string.push_str_ln(format!(" public unsafe struct {name}").as_str()); + structs_string.push_str_ln(format!(" {accessibility} unsafe struct {name}").as_str()); structs_string.push_str_ln(" {"); for field in &item.fields { if item.is_union { structs_string.push_str_ln(" [FieldOffset(0)]"); } - structs_string.push_str( - format!( - " public {} {}", - field.rust_type.to_csharp_string(options, aliases), - field.name - ) - .as_str(), - ); + + let type_name = field.rust_type.to_csharp_string(options, aliases); + let attr = if type_name == "bool" { + "[MarshalAs(UnmanagedType.U1)] ".to_string() + } else { + "".to_string() + }; + + structs_string + .push_str(format!(" {}public {} {}", attr, type_name, field.name).as_str()); if field.rust_type.is_fixed_array { let mut digits = field.rust_type.fixed_array_digits.clone(); if digits == "0" { diff --git a/dotnet-sandbox/method_call.cs b/dotnet-sandbox/method_call.cs index d165610..2c1003e 100644 --- a/dotnet-sandbox/method_call.cs +++ b/dotnet-sandbox/method_call.cs @@ -27,7 +27,19 @@ namespace CsBindgen [DllImport(__DllName, EntryPoint = "unsafe_destroy_string", CallingConvention = CallingConvention.Cdecl)] public static extern void unsafe_destroy_string(String* s); + [DllImport(__DllName, EntryPoint = "create_context", CallingConvention = CallingConvention.Cdecl)] + public static extern Context* create_context(); + [DllImport(__DllName, EntryPoint = "delete_context", CallingConvention = CallingConvention.Cdecl)] + public static extern void delete_context(Context* context); + + + } + + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct Context + { + [MarshalAs(UnmanagedType.U1)] public bool foo; }