diff options
author | Michael Mayr <mayr_michael@gmx.at> | 2017-05-16 08:51:54 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2017-05-16 08:51:54 +0300 |
commit | c99a7ee88c1b746adf8601e87cc83d7073f57e8f (patch) | |
tree | 38afe7daf6f7f2708a20b1cadb4630df2806dfbc /src | |
parent | 3caab9d7bfd24f0c5089226aa68bf2a3e652a1bc (diff) |
Make non-essential types optional (#3591)
* Fixes #3568
* Added optional throwIfNotFound to reflect optional types in the TypeSystemContext interface
* Only System.Object is required - all other types are optional
Diffstat (limited to 'src')
4 files changed, 24 insertions, 9 deletions
diff --git a/src/Common/src/TypeSystem/Common/MetadataTypeSystemContext.cs b/src/Common/src/TypeSystem/Common/MetadataTypeSystemContext.cs index 3c5278b08..7b82dc361 100644 --- a/src/Common/src/TypeSystem/Common/MetadataTypeSystemContext.cs +++ b/src/Common/src/TypeSystem/Common/MetadataTypeSystemContext.cs @@ -11,6 +11,7 @@ namespace Internal.TypeSystem { private static readonly string[] s_wellKnownTypeNames = new string[] { "Void", + "Boolean", "Char", "SByte", @@ -68,16 +69,27 @@ namespace Internal.TypeSystem // Initialize all well known types - it will save us from checking the name for each loaded type for (int typeIndex = 0; typeIndex < _wellKnownTypes.Length; typeIndex++) { - MetadataType type = systemModule.GetType("System", s_wellKnownTypeNames[typeIndex]); - type.SetWellKnownType((WellKnownType)(typeIndex + 1)); - _wellKnownTypes[typeIndex] = type; + // Require System.Object to be present as a minimal sanity check. + // The set of required well-known types is not strictly defined since different .NET profiles implement different subsets. + MetadataType type = systemModule.GetType("System", s_wellKnownTypeNames[typeIndex], typeIndex == (int)WellKnownType.Object); + if (type != null) + { + type.SetWellKnownType((WellKnownType)(typeIndex + 1)); + _wellKnownTypes[typeIndex] = type; + } } } - public override DefType GetWellKnownType(WellKnownType wellKnownType) + public override DefType GetWellKnownType(WellKnownType wellKnownType, bool throwIfNotFound = true) { Debug.Assert(_wellKnownTypes != null, "Forgot to call SetSystemModule?"); - return _wellKnownTypes[(int)wellKnownType - 1]; + + int typeIndex = (int)wellKnownType - 1; + DefType type = _wellKnownTypes[typeIndex]; + if (type == null && throwIfNotFound) + throw new TypeSystemException.TypeLoadException("System", s_wellKnownTypeNames[typeIndex], SystemModule); + + return type; } protected sealed internal override bool ComputeHasStaticConstructor(TypeDesc type) diff --git a/src/Common/src/TypeSystem/Common/TypeSystemContext.cs b/src/Common/src/TypeSystem/Common/TypeSystemContext.cs index ea5e69591..4f9dbb141 100644 --- a/src/Common/src/TypeSystem/Common/TypeSystemContext.cs +++ b/src/Common/src/TypeSystem/Common/TypeSystemContext.cs @@ -57,7 +57,7 @@ namespace Internal.TypeSystem SystemModule = systemModule; } - public abstract DefType GetWellKnownType(WellKnownType wellKnownType); + public abstract DefType GetWellKnownType(WellKnownType wellKnownType, bool throwIfNotFound = true); public virtual ModuleDesc ResolveAssembly(AssemblyName name, bool throwIfNotFound = true) { diff --git a/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs b/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs index cf1d3db16..255eb7cfd 100644 --- a/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs +++ b/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs @@ -12,7 +12,7 @@ namespace Internal.TypeSystem { public static bool IsWellKnownType(this TypeDesc type, WellKnownType wellKnownType) { - return type == type.Context.GetWellKnownType(wellKnownType); + return type == type.Context.GetWellKnownType(wellKnownType, false); } public static InstantiatedType MakeInstantiatedType(this MetadataType typeDef, Instantiation instantiation) diff --git a/src/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderTypeSystemContext.cs b/src/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderTypeSystemContext.cs index 9a90eac5d..537e00bae 100644 --- a/src/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderTypeSystemContext.cs +++ b/src/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderTypeSystemContext.cs @@ -107,7 +107,7 @@ namespace Internal.Runtime.TypeLoader return s_nativeLayoutInterfacesAlgorithm; } - public override DefType GetWellKnownType(WellKnownType wellKnownType) + public override DefType GetWellKnownType(WellKnownType wellKnownType, bool throwIfNotFound = true) { switch (wellKnownType) { @@ -190,7 +190,10 @@ namespace Internal.Runtime.TypeLoader return (DefType)ResolveRuntimeTypeHandle(typeof(Exception).TypeHandle); default: - throw new NotImplementedException(); + if (throwIfNotFound) + throw new NotImplementedException(); + else + return null; } } |