Skip to content

Commit

Permalink
[Gen] Allow null passing of funcs, and transform into IntPtr.Zero, no…
Browse files Browse the repository at this point in the history
…t a callback to null (#218)
  • Loading branch information
Therzok committed Feb 8, 2018
1 parent c59b84b commit d2257a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion generator/CallbackGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override string MarshalType {
public override string CallByName (string var_name)
{
if (WithParamGCHandle)
return NS + "Sharp." + Name + "Wrapper.NativeDelegate";
return "(" + var_name + " == null) ? null : " + NS + "Sharp." + Name + "Wrapper.NativeDelegate";
return var_name + ".NativeDelegate";
}

Expand Down
8 changes: 6 additions & 2 deletions generator/MethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public string GetCallString (bool is_set)

if (p.IsUserData && parameters.IsHidden (p) && !parameters.HideData &&
(i == 0 || parameters [i - 1].Scope != "notified")) {
var cb = parameters [i - 1].Generatable as CallbackGen;
var param = parameters [i - 1];
var cb = param.Generatable as CallbackGen;
if (cb != null && cb.WithParamGCHandle)
call_parm = "(IntPtr)gch";
else
Expand Down Expand Up @@ -128,7 +129,10 @@ public void Initialize (GenerationInfo gen_info, bool is_get, bool is_set, strin

case "async":
if (cbgen.GenerateStaticWrapper) {
sw.WriteLine (indent + "\t\t\tGCHandle gch = GCHandle.Alloc ({0});", name);
sw.WriteLine (indent + "\t\t\tIntPtr gch = IntPtr.Zero;");
sw.WriteLine (indent + "\t\t\tif ({0} != null) {{", name);
sw.WriteLine (indent + "\t\t\t\tgch = (IntPtr)GCHandle.Alloc ({0});", name);
sw.WriteLine (indent + "\t\t\t}");
} else {
sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name);
if (cbgen.WithParamGCHandle)
Expand Down
10 changes: 8 additions & 2 deletions generator/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,14 @@ public virtual string CallString {
string call_parm;

IGeneratable gen = Generatable;
if (gen is CallbackGen)
return SymbolTable.Table.CallByName (CType, CallName + "_wrapper");
if (gen is CallbackGen) {
var cb = (CallbackGen)gen;
if (cb.GenerateStaticWrapper) {
return SymbolTable.Table.CallByName (CType, CallName);
} else {
return SymbolTable.Table.CallByName (CType, CallName + "_wrapper");
}
}
else if (PassAs != String.Empty) {
call_parm = PassAs + " ";
if (CSType != MarshalType)
Expand Down

0 comments on commit d2257a2

Please sign in to comment.