From 55ab3ab284856ea6498ce595e4528661311747e8 Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Thu, 18 Jul 2013 17:05:58 +0200 Subject: [PATCH] generator: fixed glue code for callbacks --- generator/CallbackGen.cs | 8 ++++++++ generator/FieldBase.cs | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/generator/CallbackGen.cs b/generator/CallbackGen.cs index a63dab813..319fe5be1 100644 --- a/generator/CallbackGen.cs +++ b/generator/CallbackGen.cs @@ -70,6 +70,14 @@ namespace GtkSharp.Generation { } } + public string WrapperName { + get { + if (!valid) + return String.Empty; + return NS + "Sharp." + Name + "Wrapper"; + } + } + public override string MarshalType { get { if (valid) diff --git a/generator/FieldBase.cs b/generator/FieldBase.cs index f42e52eb1..b2985ac97 100644 --- a/generator/FieldBase.cs +++ b/generator/FieldBase.cs @@ -156,6 +156,7 @@ namespace GtkSharp.Generation { GenerateImports (gen_info, indent); SymbolTable table = SymbolTable.Table; + IGeneratable gen = table [CType]; StreamWriter sw = gen_info.Writer; string modifiers = elem.GetAttributeAsBoolean ("new_flag") ? "new " : ""; bool is_struct = table.IsStruct (CType) || table.IsBoxed (CType); @@ -176,13 +177,19 @@ namespace GtkSharp.Generation { } else if (Readable && offsetName != null) { sw.WriteLine (indent + "\tget {"); sw.WriteLine (indent + "\t\tunsafe {"); - sw.WriteLine (indent + "\t\t\t" + table.GetMarshalType (CType) + "* raw_ptr = (" + table.GetMarshalType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); - sw.WriteLine (indent + "\t\t\treturn " + table.FromNative (ctype, "(*raw_ptr)") + ";"); + if (gen is CallbackGen) { + sw.WriteLine (indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); + sw.WriteLine (indent + "\t\t\t {0} del = ({0})Marshal.GetDelegateForFunctionPointer(*raw_ptr, typeof({0}));", table.GetMarshalType (CType)); + sw.WriteLine (indent + "\t\t\treturn " + table.FromNative (ctype, "(del)") + ";"); + } + else { + sw.WriteLine (indent + "\t\t\t" + table.GetMarshalType (CType) + "* raw_ptr = (" + table.GetMarshalType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); + sw.WriteLine (indent + "\t\t\treturn " + table.FromNative (ctype, "(*raw_ptr)") + ";"); + } sw.WriteLine (indent + "\t\t}"); sw.WriteLine (indent + "\t}"); } - IGeneratable gen = table [CType]; string to_native = (gen is IManualMarshaler) ? (gen as IManualMarshaler).AllocNative ("value") : gen.CallByName ("value"); if (Setter != null) { @@ -198,8 +205,15 @@ namespace GtkSharp.Generation { } else if (Writable && offsetName != null) { sw.WriteLine (indent + "\tset {"); sw.WriteLine (indent + "\t\tunsafe {"); - sw.WriteLine (indent + "\t\t\t" + table.GetMarshalType (CType) + "* raw_ptr = (" + table.GetMarshalType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); - sw.WriteLine (indent + "\t\t\t*raw_ptr = " + to_native + ";"); + if (gen is CallbackGen) { + sw.WriteLine (indent + "\t\t\t{0} wrapper = new {0} (value);", ((CallbackGen)gen).WrapperName); + sw.WriteLine (indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); + sw.WriteLine (indent + "\t\t\t*raw_ptr = Marshal.GetFunctionPointerForDelegate (wrapper.NativeDelegate);"); + } + else { + sw.WriteLine (indent + "\t\t\t" + table.GetMarshalType (CType) + "* raw_ptr = (" + table.GetMarshalType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");"); + sw.WriteLine (indent + "\t\t\t*raw_ptr = " + to_native + ";"); + } sw.WriteLine (indent + "\t\t}"); sw.WriteLine (indent + "\t}"); }