|
| 1 | +using System; |
1 | 2 | using System.Collections.Generic;
|
2 | 3 | using System.Linq;
|
3 | 4 | using System.Runtime.InteropServices;
|
|
6 | 7 | using CppSharp.AST.Extensions;
|
7 | 8 | using CppSharp.Generators;
|
8 | 9 | using CppSharp.Generators.CSharp;
|
| 10 | +using Type = CppSharp.AST.Type; |
9 | 11 |
|
10 | 12 | namespace CppSharp.Types.Std
|
11 | 13 | {
|
@@ -145,7 +147,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
|
145 | 147 | // would be really helpful to have ctx hold a Decl property representing the
|
146 | 148 | // "appropriate" Decl when we get here. When MarshalKind == NativeField, Decl would
|
147 | 149 | // be set to the Field we're operating on.
|
148 |
| - var fieldName = ctx.ReturnVarName.Substring(ctx.ReturnVarName.LastIndexOf("->") + 2); |
| 150 | + var fieldName = ctx.ReturnVarName[Math.Max(ctx.ReturnVarName.LastIndexOf('.') + 1, ctx.ReturnVarName.LastIndexOf("->") + 2)..]; |
149 | 151 |
|
150 | 152 | ctx.Before.WriteLine($"if (__{fieldName}_OwnsNativeMemory)");
|
151 | 153 | ctx.Before.WriteLineIndent($"Marshal.FreeHGlobal({ctx.ReturnVarName});");
|
@@ -326,10 +328,24 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
|
326 | 328 | var assign = basicString.Methods.First(m => m.OriginalName == "assign");
|
327 | 329 | if (ctx.MarshalKind == MarshalKind.NativeField)
|
328 | 330 | {
|
| 331 | + string var; |
| 332 | + if (ctx.ReturnVarName.LastIndexOf('.') > ctx.ReturnVarName.LastIndexOf("->")) |
| 333 | + { |
| 334 | + ctx.Before.WriteLine("throw new NotImplementedException(\"This method cannot currently be called because it would " + |
| 335 | + "leave the object in an invalid state. See https://github.com/mono/CppSharp/issues/1777\");"); |
| 336 | + |
| 337 | + var = Generator.GeneratedIdentifier(ctx.ArgName); |
| 338 | + ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})"); |
| 339 | + ctx.Before.WriteOpenBraceAndIndent(); |
| 340 | + ctx.HasCodeBlock = true; |
| 341 | + } |
| 342 | + else |
| 343 | + { |
| 344 | + var = $"&{ctx.ReturnVarName}"; |
| 345 | + } |
329 | 346 | ctx.Return.Write($@"{qualifiedBasicString}Extensions.{
|
330 | 347 | Helpers.InternalStruct}.{assign.Name}(new {
|
331 |
| - typePrinter.IntPtrType}(&{ |
332 |
| - ctx.ReturnVarName}), "); |
| 348 | + typePrinter.IntPtrType}({var}), "); |
333 | 349 | if (ctx.Parameter.Type.IsTemplateParameterType())
|
334 | 350 | ctx.Return.Write("(string) (object) ");
|
335 | 351 | ctx.Return.Write($"{ctx.Parameter.Name})");
|
|
0 commit comments