diff --git a/src/Csharpc/CodeGen/ModuleCodeGen.PInvoke.cs b/src/Csharpc/CodeGen/ModuleCodeGen.PInvoke.cs index d3282a28..7af8f543 100644 --- a/src/Csharpc/CodeGen/ModuleCodeGen.PInvoke.cs +++ b/src/Csharpc/CodeGen/ModuleCodeGen.PInvoke.cs @@ -554,6 +554,10 @@ or SpecialType.System_Int32 or SpecialType.System_UInt32 or SpecialType.System_I // blittable as in .NET. Other shadows may lay their fields out differently from .NET: not blittable. INamedTypeSymbol { Name: "Guid", TypeKind: TypeKind.Struct, ContainingType: null, ContainingNamespace: { Name: "System", ContainingNamespace.IsGlobalNamespace: true } } s when CoreLib.TryMap(s) is { TypeKind: TypeKind.Struct } shadow && IsUserType(shadow) => IsBlittable(shadow), + // A CoreLib struct served from the framework's IL (upstream-types/, ADR 0023) has .NET's own fields, so it is as + // blittable as they are: NFloat (one double on 64-bit targets), which Apple's CGPoint/CGSize/CGRect are made of. + INamedTypeSymbol { TypeKind: TypeKind.Struct } s when !IsUserType(s) && CoreLib.TryMap(s) is { TypeKind: TypeKind.Struct } served + && served.ContainingNamespace.ToDisplayString().StartsWith(Frontend.UpstreamSlice.Prefix, StringComparison.Ordinal) && IsUserType(served) => IsBlittable(served), _ => false, }; diff --git a/src/Csharpc/upstream-types/System.Runtime.InteropServices.NFloat.txt b/src/Csharpc/upstream-types/System.Runtime.InteropServices.NFloat.txt new file mode 100644 index 00000000..8cfc926b --- /dev/null +++ b/src/Csharpc/upstream-types/System.Runtime.InteropServices.NFloat.txt @@ -0,0 +1,3 @@ +# NFloat (Apple's CGFloat: a double on 64-bit targets) served from CoreLib's IL (ADR 0023): .NET's own arithmetic, conversions, +# formatting and parsing; P/Invoke sees it as blittable as its fields (ModuleCodeGen.PInvoke.IsBlittable). +System.Runtime.InteropServices.NFloat diff --git a/tests/platform-skips.txt b/tests/platform-skips.txt index aaf979a2..9103e6ca 100644 --- a/tests/platform-skips.txt +++ b/tests/platform-skips.txt @@ -336,6 +336,7 @@ wasi 1540_enum_negative_value_culture_sign .NET on WASI is globalization-invari wasi 1550_threading_runtime_members needs threads: Thread.Start throws PlatformNotSupportedException on WebAssembly wasi 1553_interop_gap_members loads a shared library with NativeLibrary.Load: WASI has no dynamic libraries wasi 1559_driveinfo WASI has no statfs/statvfs: DriveFormat and the drive sizes throw IOException (ENOTSUP) +wasi 1346_nfloat_from_framework NFloat is a float on 32-bit targets (wasm32), a double on 64-bit: its size and range differ from the 64-bit recording windows 1563_timezoneinfo_from_il live: Windows takes the local zone from the system settings, not TZ; recorded there macos 1562_special_folders live: macOS takes the user folders from NSFileManager (the real home, not $HOME), Linux from $HOME and XDG; recorded on each windows 1562_special_folders known gap: Environment.GetFolderPath on Windows (the known folders) is not supported by csharpc yet; an explicit NotSupportedException diff --git a/tests/samples/1346_nfloat_from_framework.cs b/tests/samples/1346_nfloat_from_framework.cs new file mode 100644 index 00000000..b14ce2af --- /dev/null +++ b/tests/samples/1346_nfloat_from_framework.cs @@ -0,0 +1,34 @@ +#:property AllowUnsafeBlocks=true +// System.Runtime.InteropServices.NFloat (Apple's CGFloat; a double on 64-bit targets), served from the framework's IL: arithmetic, +// conversions, classification, comparison, formatting and parsing, and NFloat (and structs of it, like CGPoint) across +// unmanaged function pointers, where it is blittable. +using System; +using System.Globalization; +using System.Runtime.InteropServices; + +unsafe +{ + var inv = CultureInfo.InvariantCulture; + NFloat x = 1.5f, y = (NFloat)2.25; + Console.WriteLine($"{(x + y).ToString(inv)} {(x * y).ToString(inv)} {(y - x).ToString(inv)} {(y / x).ToString(inv)} {(-x).ToString(inv)} {(y % x).ToString(inv)}"); + Console.WriteLine($"{x < y} {x >= y} {x == (NFloat)1.5} {x.Equals(y)} {x.CompareTo(y)} {NFloat.Size} {sizeof(NFloat)}"); + Console.WriteLine($"{(double)NFloat.MaxValue == double.MaxValue} {NFloat.IsNaN(NFloat.NaN)} {NFloat.IsInfinity(NFloat.PositiveInfinity)} {NFloat.IsNegative(NFloat.NegativeZero)}"); + Console.WriteLine($"{(int)(NFloat)7.9} {(long)(NFloat)(-3.5)} {((float)y).ToString(inv)} {((decimal)(NFloat)0.25).ToString(inv)} {((NFloat)(Half)0.5).ToString(inv)}"); + Console.WriteLine($"{((NFloat)0.1).ToString(inv)} {((NFloat)1234.5678).ToString("F2", inv)} {((NFloat)1e300).ToString(inv)}"); + Console.WriteLine($"{NFloat.Parse("3.25", inv).ToString(inv)} {NFloat.TryParse("oops", inv, out var bad)} {bad.ToString(inv)}"); + Console.WriteLine($"{NFloat.Max(x, y).ToString(inv)} {NFloat.Abs((NFloat)(-2)).ToString(inv)} {NFloat.Sqrt((NFloat)16).ToString(inv)} {NFloat.Round((NFloat)2.5).ToString(inv)}"); + + delegate* unmanaged hypot = &Native.Hypot; + Console.WriteLine(hypot(3, 4).ToString(inv)); + delegate* unmanaged scale = &Native.Scale; + var p = scale(new CGPoint { X = 1.5f, Y = -2 }, 2); + Console.WriteLine($"{p.X.ToString(inv)} {p.Y.ToString(inv)} {sizeof(CGPoint)} {Marshal.SizeOf()}"); +} + +struct CGPoint { public NFloat X, Y; } + +static class Native +{ + [UnmanagedCallersOnly] public static NFloat Hypot(NFloat a, NFloat b) => NFloat.Sqrt(a * a + b * b); + [UnmanagedCallersOnly] public static CGPoint Scale(CGPoint p, NFloat by) => new CGPoint { X = p.X * by, Y = p.Y * by }; +} diff --git a/tests/samples/1346_nfloat_from_framework.expected b/tests/samples/1346_nfloat_from_framework.expected new file mode 100644 index 00000000..6204dc62 --- /dev/null +++ b/tests/samples/1346_nfloat_from_framework.expected @@ -0,0 +1,9 @@ +3.75 3.375 0.75 1.5 -1.5 0.75 +True False True False -1 8 8 +True True True True +7 -3 2.25 0.25 0.5 +0.1 1234.57 1E+300 +3.25 False 0 +2.25 2 4 2 +5 +3 -4 16 16