[nnyeah] Fix for handling nfloat (#15021)

nfloat needed a proper reference to System.Runtime.InteropServices.
This commit is contained in:
Steve Hawley 2022-05-16 11:53:33 -04:00 коммит произвёл GitHub
Родитель 4d3ef30198
Коммит 746cf884c0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 35 добавлений и 8 удалений

Просмотреть файл

@ -4,23 +4,46 @@ using System.Collections.Generic;
namespace Microsoft.MaciOS.Nnyeah {
public static class ModuleExtensions {
static Dictionary<string, PlatformName> moduleToPlatform = new Dictionary<string, PlatformName> {
static Dictionary<string, PlatformName> xamarinModuleToPlatform = new Dictionary<string, PlatformName> {
{ "Xamarin.iOS", PlatformName.iOS },
{ "Xamarin.Mac", PlatformName.macOS },
{ "Xamarin.TVOS", PlatformName.tvOS },
{ "Xamarin.WatchOS", PlatformName.watchOS }
};
static Dictionary<string, PlatformName> microsoftModuleToPlatform = new Dictionary<string, PlatformName> {
{ "Microsoft.iOS", PlatformName.iOS },
{ "Microsoft.macOS", PlatformName.macOS },
{ "Microsoft.tvOS", PlatformName.tvOS },
{ "Microsoft.watchOS", PlatformName.watchOS }
};
public static bool DependsOnXamarin (this ModuleDefinition module)
{
return module.XamarinPlatformName () != PlatformName.None;
}
public static PlatformName XamarinPlatformName (this ModuleDefinition module)
{
return FindPlatformName (module, xamarinModuleToPlatform);
}
public static bool DependsOnMicrosoft (this ModuleDefinition module)
{
return module.MicrosoftPlatformName () != PlatformName.None;
}
public static PlatformName MicrosoftPlatformName (this ModuleDefinition module)
{
return FindPlatformName (module, microsoftModuleToPlatform);
}
static PlatformName FindPlatformName (ModuleDefinition module, Dictionary<string, PlatformName> knownModules)
{
foreach (var possibleModule in module.AssemblyReferences) {
if (moduleToPlatform.TryGetValue (possibleModule.Name, out var platformName))
if (knownModules.TryGetValue (possibleModule.Name, out var platformName)) {
return platformName;
}
}
return PlatformName.None;
}

Просмотреть файл

@ -27,6 +27,7 @@ namespace Microsoft.MaciOS.Nnyeah {
TypeReference NewNfloatTypeReference;
ModuleReference NewNfloatModuleReference;
TypeDefinition NewNativeHandleTypeDefinition;
AssemblyNameReference InteropServicesAssembly;
TypeAndMemberMap ModuleMap;
@ -80,6 +81,11 @@ namespace Microsoft.MaciOS.Nnyeah {
NativeIntegerAttributeTypeRef = new TypeReference (NativeIntegerAttributeTypeDef.Namespace,
NativeIntegerAttributeTypeDef.Name, NativeIntegerAttributeTypeDef.Module, NativeIntegerAttributeTypeDef.Scope);
if (moduleMap.MicrosoftModule.AssemblyReferences.FirstOrDefault (an => an.Name == "System.Runtime.InteropServices") is AssemblyNameReference validReference) {
InteropServicesAssembly = validReference;
} else {
throw new NotSupportedException ($"Assembly {moduleMap.MicrosoftModule.Name} does not have reference to System.Runtime.InteropServices. This is not possible.");
}
// if these type references aren't found in the module
// then we don't ever need to worry about reworking them
@ -92,10 +98,9 @@ namespace Microsoft.MaciOS.Nnyeah {
}
if (!module.TryGetTypeReference ("System.nfloat", out NfloatTypeReference)) {
NfloatTypeReference = EmptyTypeReference;
} else {
NewNfloatTypeReference = module.ImportReference (new TypeReference ("System.Runtime.InteropServices", "NFloat", null, InteropServicesAssembly, true));
}
NewNfloatModuleReference = new ModuleReference ("System.Private.CoreLib");
NewNfloatTypeReference = new TypeReference ("System.Runtime.InteropServices", "NFloat", null, NewNfloatModuleReference, true);
NewNativeHandleTypeDefinition = moduleMap.MicrosoftModule.Types.First (t => t.FullName == "ObjCRuntime.NativeHandle");
// These must be called last as they depend on Module and NativeIntegerAttributeTypeRef to be setup

Просмотреть файл

@ -12,8 +12,7 @@ namespace Microsoft.MaciOS.Nnyeah.Tests {
public class DependencyRemovedTests {
[TestCase ("nint")]
[TestCase ("nuint")]
// nfloat has an issue on write.
// [TestCase ("nfloat")]
[TestCase ("nfloat")]
public async Task BasicDependencyRemoved (string type)
{
var dir = Cache.CreateTemporaryDirectory ($"DependencyRemoved_{type}");
@ -37,7 +36,7 @@ public class Foo {{
var module = ModuleDefinition.ReadModule (targetRewrite);
var platform = module.XamarinPlatformName ();
Assert.AreEqual (Microsoft.MaciOS.Nnyeah.PlatformName.None, platform);
Assert.AreEqual (Microsoft.MaciOS.Nnyeah.PlatformName.None, platform, "still has Xamarin dependency");
}
}
}