Skip to content

Commit efdec41

Browse files
committed
Add missing GenerateNativeToManagedFor checks
1 parent e160b3a commit efdec41

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/AST/TypeExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,9 @@ public static long GetSizeInBits(this ArrayType array)
443443
return array.Size * array.ElementSize;
444444
}
445445

446-
internal static bool IsReferenceToPtrToClass(this Type type)
446+
internal static bool TryGetReferenceToPtrToClass(this Type type, out Type classType)
447447
{
448+
classType = null;
448449
var @ref = type.Desugar().AsLvReference();
449450
if (@ref == null)
450451
return false;
@@ -454,7 +455,11 @@ internal static bool IsReferenceToPtrToClass(this Type type)
454455
return false;
455456

456457
var @class = @ptr.Pointee;
457-
return @class != null && @class.IsClass();
458+
if (!@class.IsClass())
459+
return false;
460+
461+
classType = @class;
462+
return true;
458463
}
459464

460465
internal static PointerType AsLvReference(this Type type)

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ private string HandleReturnedPointer(Class @class, string qualifiedClass)
393393
var dtorVirtual = (dtor != null && dtor.IsVirtual);
394394
var cache = dtorVirtual && Context.Parameter == null;
395395
var skipVTables = dtorVirtual && Context.Parameter != null;
396-
Context.Before.WriteLine("var {0} = {1}.__GetOrCreateInstance({2}, {3}{4});",
397-
ret, qualifiedClass, Context.ReturnVarName, cache ? "true" : "false", skipVTables ? ", skipVTables: true" : string.Empty);
396+
var get = Context.Context.Options.GenerateNativeToManagedFor(@class) ? "GetOr" : "";
397+
Context.Before.WriteLine("var {0} = {1}.__{5}CreateInstance({2}, {3}{4});",
398+
ret, qualifiedClass, Context.ReturnVarName, cache ? "true" : "false", skipVTables ? ", skipVTables: true" : string.Empty, get);
398399
}
399400
else
400401
{

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,12 +3141,12 @@ public void GenerateFunctionCall(string functionName, Function function,
31413141

31423142
foreach (var param in @params)
31433143
{
3144-
if (param.Param.IsInOut && param.Param.Type.IsReferenceToPtrToClass())
3144+
if (param.Param.IsInOut && param.Param.Type.TryGetReferenceToPtrToClass(out var classType) && classType.TryGetClass(out var @class))
31453145
{
31463146
var qualifiedClass = param.Param.Type.Visit(TypePrinter);
3147-
3147+
var get = Options.GenerateNativeToManagedFor(@class) ? "GetOr" : "";
31483148
WriteLine($"if ({param.Name} != {param.Param.Name}.__Instance)");
3149-
WriteLine($"{param.Param.Name} = {qualifiedClass}.__GetOrCreateInstance(__{param.Name}, false);");
3149+
WriteLine($"{param.Param.Name} = {qualifiedClass}.__{get}CreateInstance(__{param.Name}, false);");
31503150
}
31513151
}
31523152

0 commit comments

Comments
 (0)