Move part of RuntimeHelpers to shared partition (#23130)
authorMarek Safar <marek.safar@gmail.com>
Sat, 9 Mar 2019 05:32:53 +0000 (06:32 +0100)
committerJan Kotas <jkotas@microsoft.com>
Sat, 9 Mar 2019 05:32:53 +0000 (21:32 -0800)
* Move part of RuntimeHelpers to shared partition

* Remove FormatterServices.cs

src/System.Private.CoreLib/System.Private.CoreLib.csproj
src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs [new file with mode: 0644]
src/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs
src/System.Private.CoreLib/src/System/Runtime/Serialization/FormatterServices.cs [deleted file]
src/vm/ecalllist.h
src/vm/reflectioninvocation.cpp

index 816d3ce..836eb00 100644 (file)
     <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\NativeLibrary.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Runtime\Loader\AssemblyDependencyResolver.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Runtime\Loader\AssemblyLoadContext.CoreCLR.cs" />
-    <Compile Include="$(BclSourcesRoot)\System\Runtime\Serialization\FormatterServices.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Runtime\Versioning\CompatibilitySwitch.cs" />
     <Compile Include="$(BclSourcesRoot)\System\RuntimeArgumentHandle.cs" />
     <Compile Include="$(BclSourcesRoot)\System\RuntimeHandles.cs" />
index a168f09..c03b8b1 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\ReferenceAssemblyAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeCompatibilityAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeFeature.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeWrappedException.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\SpecialNameAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\StateMachineAttribute.cs" />
diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs
new file mode 100644 (file)
index 0000000..fcb69ec
--- /dev/null
@@ -0,0 +1,66 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Runtime.Serialization;
+
+namespace System.Runtime.CompilerServices
+{
+    public static partial class RuntimeHelpers
+    {
+        public delegate void TryCode(object userData);
+
+        public delegate void CleanupCode(object userData, bool exceptionThrown);
+
+        /// <summary>
+        /// GetSubArray helper method for the compiler to slice an array using a range.
+        /// </summary>
+        public static T[] GetSubArray<T>(T[] array, Range range)
+        {
+            Type elementType = array.GetType().GetElementType();
+            Span<T> source = array.AsSpan(range);
+
+            if (elementType.IsValueType)
+            {
+                return source.ToArray();
+            }
+            else
+            {
+                T[] newArray = (T[])Array.CreateInstance(elementType, source.Length);
+                source.CopyTo(newArray);
+                return newArray;
+            }
+        }
+
+        public static object GetUninitializedObject(Type type)
+        {
+            if (type is null)
+            {
+                throw new ArgumentNullException(nameof(type), SR.ArgumentNull_Type);
+            }
+            
+            if (!type.IsRuntimeImplemented())
+            {
+                throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type.ToString()));
+            }
+
+            return GetUninitializedObjectInternal(type);
+        }
+
+        public static void PrepareContractedDelegate(Delegate d)
+        {
+        }
+
+        public static void ProbeForSufficientStack()
+        {
+        }
+
+        public static void PrepareConstrainedRegions()
+        {
+        }
+
+        public static void PrepareConstrainedRegionsNoOP()
+        {
+        }
+    }
+}
\ No newline at end of file
index 94a6379..1851d9f 100644 (file)
@@ -2,37 +2,14 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-//
-// RuntimeHelpers
-//    This class defines a set of static methods that provide support for compilers.
-//
-//
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using Internal.Runtime.CompilerServices;
 
 namespace System.Runtime.CompilerServices
 {
-    using System;
-    using System.Diagnostics;
-    using System.Security;
-    using System.Runtime;
-    using System.Runtime.CompilerServices;
-    using System.Runtime.InteropServices;
-    using System.Runtime.ConstrainedExecution;
-    using System.Runtime.Serialization;
-    using System.Threading;
-    using System.Runtime.Versioning;
-    using Internal.Runtime.CompilerServices;
-
-    public static class RuntimeHelpers
+    public static partial class RuntimeHelpers
     {
-        // Exposed here as a more appropriate place than on FormatterServices itself,
-        // which is a high level reflection heavy type.
-        public static object GetUninitializedObject(Type type)
-        {
-            return FormatterServices.GetUninitializedObject(type);
-        }
-
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);
 
@@ -113,8 +90,6 @@ namespace System.Runtime.CompilerServices
             }
         }
 
-        public static void PrepareContractedDelegate(Delegate d) { }
-
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public static extern void PrepareDelegate(Delegate d);
 
@@ -160,27 +135,6 @@ namespace System.Runtime.CompilerServices
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public static extern bool TryEnsureSufficientExecutionStack();
 
-        public static void ProbeForSufficientStack()
-        {
-        }
-
-        // This method is a marker placed immediately before a try clause to mark the corresponding catch and finally blocks as
-        // constrained. There's no code here other than the probe because most of the work is done at JIT time when we spot a call to this routine.
-        public static void PrepareConstrainedRegions()
-        {
-            ProbeForSufficientStack();
-        }
-
-        // When we detect a CER with no calls, we can point the JIT to this non-probing version instead
-        // as we don't need to probe.
-        public static void PrepareConstrainedRegionsNoOP()
-        {
-        }
-
-        public delegate void TryCode(object userData);
-
-        public delegate void CleanupCode(object userData, bool exceptionThrown);
-
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public static extern void ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, object userData);
 
@@ -197,26 +151,6 @@ namespace System.Runtime.CompilerServices
             throw new InvalidOperationException();
         }
 
-        /// <summary>
-        /// GetSubArray helper method for the compiler to slice an array using a range.
-        /// </summary>
-        public static T[] GetSubArray<T>(T[] array, Range range)
-        {
-            Type elementType = array.GetType().GetElementType();
-            Span<T> source = array.AsSpan(range);
-
-            if (elementType.IsValueType)
-            {
-                return source.ToArray();
-            }
-            else
-            {
-                T[] newArray = (T[])Array.CreateInstance(elementType, source.Length);
-                source.CopyTo(newArray);
-                return newArray;
-            }
-        }
-
         // Returns true iff the object has a component size;
         // i.e., is variable length like System.String or Array.
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -256,5 +190,9 @@ namespace System.Runtime.CompilerServices
             // Ideally this would just be a single dereference:
             // mov tmp, qword ptr [rax] ; rax = obj ref, tmp = MethodTable* pointer
         }
+
+        [MethodImplAttribute(MethodImplOptions.InternalCall)]
+        private static extern object GetUninitializedObjectInternal(Type type);
+
     }
 }
diff --git a/src/System.Private.CoreLib/src/System/Runtime/Serialization/FormatterServices.cs b/src/System.Private.CoreLib/src/System/Runtime/Serialization/FormatterServices.cs
deleted file mode 100644 (file)
index 9da385b..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-**
-**
-** Purpose: Provides some static methods to aid with the implementation
-**          of a Formatter for Serialization.
-**
-**
-============================================================*/
-
-using System;
-using System.Reflection;
-using System.Collections;
-using System.Collections.Generic;
-using System.Security;
-using System.Runtime.CompilerServices;
-using System.Runtime.Versioning;
-using System.Threading;
-using System.IO;
-using System.Text;
-using System.Globalization;
-using System.Diagnostics;
-
-namespace System.Runtime.Serialization
-{
-    // This class duplicates a class on CoreFX. We are keeping it here -- just this one method --
-    // as it was widely invoked by reflection to workaround it being missing in .NET Core 1.0
-    internal static class FormatterServices
-    {
-        // Gets a new instance of the object.  The entire object is initalized to 0 and no 
-        // constructors have been run. **THIS MEANS THAT THE OBJECT MAY NOT BE IN A STATE
-        // CONSISTENT WITH ITS INTERNAL REQUIREMENTS** This method should only be used for
-        // deserialization when the user intends to immediately populate all fields.  This method
-        // will not create an unitialized string because it is non-sensical to create an empty
-        // instance of an immutable type.
-        //
-        public static object GetUninitializedObject(Type type)
-        {
-            if ((object)type == null)
-            {
-                throw new ArgumentNullException(nameof(type));
-            }
-
-            if (!(type is RuntimeType))
-            {
-                throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type));
-            }
-
-            return nativeGetUninitializedObject((RuntimeType)type);
-        }
-
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        private static extern object nativeGetUninitializedObject(RuntimeType type);
-    }
-}
-
-
-
-
-
index 0298939..b4f9895 100644 (file)
@@ -163,10 +163,6 @@ FCFuncStart(gEnvironmentFuncs)
     FCFuncElementSig("FailFast", &gsig_SM_Str_Exception_Str_RetVoid, SystemNative::FailFastWithExceptionAndSource)
 FCFuncEnd()
 
-FCFuncStart(gSerializationFuncs)
-    FCFuncElement("nativeGetUninitializedObject", ReflectionSerialization::GetUninitializedObject)
-FCFuncEnd()
-
 FCFuncStart(gExceptionFuncs)
     FCFuncElement("IsImmutableAgileException", ExceptionNative::IsImmutableAgileException)
     FCFuncElement("nIsTransient", ExceptionNative::IsTransient)
@@ -919,6 +915,7 @@ FCFuncStart(gRuntimeHelpers)
     FCFuncElement("Equals", ObjectNative::Equals)
     FCFuncElement("EnsureSufficientExecutionStack", ReflectionInvocation::EnsureSufficientExecutionStack)
     FCFuncElement("TryEnsureSufficientExecutionStack", ReflectionInvocation::TryEnsureSufficientExecutionStack)
+    FCFuncElement("GetUninitializedObjectInternal", ReflectionSerialization::GetUninitializedObject)
 FCFuncEnd()
 
 FCFuncStart(gContextSynchronizationFuncs)
@@ -1196,7 +1193,6 @@ FCClassElement("EventPipeInternal", "System.Diagnostics.Tracing", gEventPipeInte
 #endif // FEATURE_PERFTRACING
 FCClassElement("Exception", "System", gExceptionFuncs)
 FCClassElement("FileLoadException", "System.IO", gFileLoadExceptionFuncs)
-FCClassElement("FormatterServices", "System.Runtime.Serialization", gSerializationFuncs)
 FCClassElement("GC", "System", gGCInterfaceFuncs)
 FCClassElement("GCHandle", "System.Runtime.InteropServices", gGCHandleFuncs)
 FCClassElement("GCSettings", "System.Runtime", gGCSettingsFuncs)
index a69f543..1f8aa04 100644 (file)
@@ -2581,10 +2581,6 @@ FCIMPL1(Object*, ReflectionSerialization::GetUninitializedObject, ReflectClassBa
 
     HELPER_METHOD_FRAME_BEGIN_RET_NOPOLL();
 
-    if (objType == NULL) {
-        COMPlusThrowArgumentNull(W("type"), W("ArgumentNull_Type"));
-    }
-
     TypeHandle type = objType->GetType();
 
     // Don't allow arrays, pointers, byrefs or function pointers.