Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions src/System.Management.Automation/utils/ClrFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ static ClrFacade()
/// <returns>SafeHandle</returns>
internal static SafeHandle GetSafeProcessHandle(Process process)
{
#if CORECLR
return process.SafeHandle;
#else
return new SafeProcessHandle(process.Handle);
#endif
}

#endregion Process
Expand All @@ -79,38 +75,26 @@ internal static SafeHandle GetSafeProcessHandle(Process process)
/// </summary>
internal static int SizeOf<T>()
{
#if CORECLR
// Marshal.SizeOf(Type) is obsolete in CoreCLR
return Marshal.SizeOf<T>();
#else
return Marshal.SizeOf(typeof(T));
#endif
}

/// <summary>
/// Facade for Marshal.DestroyStructure
/// </summary>
internal static void DestroyStructure<T>(IntPtr ptr)
{
#if CORECLR
// Marshal.DestroyStructure(IntPtr, Type) is obsolete in CoreCLR
Marshal.DestroyStructure<T>(ptr);
#else
Marshal.DestroyStructure(ptr, typeof(T));
#endif
}

/// <summary>
/// Facade for Marshal.PtrToStructure
/// </summary>
internal static T PtrToStructure<T>(IntPtr ptr)
{
#if CORECLR
// Marshal.PtrToStructure(IntPtr, Type) is obsolete in CoreCLR
return Marshal.PtrToStructure<T>(ptr);
#else
return (T)Marshal.PtrToStructure(ptr, typeof(T));
#endif
}

/// <summary>
Expand All @@ -121,11 +105,7 @@ internal static void StructureToPtr<T>(
IntPtr ptr,
bool deleteOld)
{
#if CORECLR
Marshal.StructureToPtr<T>(structure, ptr, deleteOld);
#else
Marshal.StructureToPtr(structure, ptr, deleteOld);
#endif
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instances like the above ones, we should make changes in the call sites of those methods to directly use the API available in .NET Core, so that we can completely remove those methods from ClrFacade.cs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave them out, and I will clean them up later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 I don't understand this in depth so leave them to you.

}

#endregion Marshal
Expand Down Expand Up @@ -187,13 +167,11 @@ internal static Encoding GetDefaultEncoding()
{
#if UNIX // PowerShell Core on Unix
s_defaultEncoding = new UTF8Encoding(false);
#elif CORECLR // PowerShell Core on Windows
#else // PowerShell Core on Windows
EncodingRegisterProvider();

uint currentAnsiCp = NativeMethods.GetACP();
s_defaultEncoding = Encoding.GetEncoding((int)currentAnsiCp);
#else // Windows PowerShell
s_defaultEncoding = Encoding.Default;
#endif
}
return s_defaultEncoding;
Expand All @@ -210,31 +188,25 @@ internal static Encoding GetOEMEncoding()
{
#if UNIX // PowerShell Core on Unix
s_oemEncoding = GetDefaultEncoding();
#elif CORECLR // PowerShell Core on Windows
#else // PowerShell Core on Windows
EncodingRegisterProvider();

uint oemCp = NativeMethods.GetOEMCP();
s_oemEncoding = Encoding.GetEncoding((int)oemCp);

#else // Windows PowerShell
uint oemCp = NativeMethods.GetOEMCP();
s_oemEncoding = Encoding.GetEncoding((int)oemCp);
#endif
}
return s_oemEncoding;
}

private static volatile Encoding s_oemEncoding;

#if CORECLR
private static void EncodingRegisterProvider()
{
if (s_defaultEncoding == null && s_oemEncoding == null)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
}
#endif

#endregion Encoding

Expand Down