[nnyeah] Fix for handling nfloat (#15021)
nfloat needed a proper reference to System.Runtime.InteropServices.
This commit is contained in:
Родитель
4d3ef30198
Коммит
746cf884c0
|
@ -4,23 +4,46 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Microsoft.MaciOS.Nnyeah {
|
namespace Microsoft.MaciOS.Nnyeah {
|
||||||
public static class ModuleExtensions {
|
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.iOS", PlatformName.iOS },
|
||||||
{ "Xamarin.Mac", PlatformName.macOS },
|
{ "Xamarin.Mac", PlatformName.macOS },
|
||||||
{ "Xamarin.TVOS", PlatformName.tvOS },
|
{ "Xamarin.TVOS", PlatformName.tvOS },
|
||||||
{ "Xamarin.WatchOS", PlatformName.watchOS }
|
{ "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)
|
public static bool DependsOnXamarin (this ModuleDefinition module)
|
||||||
{
|
{
|
||||||
return module.XamarinPlatformName () != PlatformName.None;
|
return module.XamarinPlatformName () != PlatformName.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PlatformName XamarinPlatformName (this ModuleDefinition module)
|
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) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return PlatformName.None;
|
return PlatformName.None;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ namespace Microsoft.MaciOS.Nnyeah {
|
||||||
TypeReference NewNfloatTypeReference;
|
TypeReference NewNfloatTypeReference;
|
||||||
ModuleReference NewNfloatModuleReference;
|
ModuleReference NewNfloatModuleReference;
|
||||||
TypeDefinition NewNativeHandleTypeDefinition;
|
TypeDefinition NewNativeHandleTypeDefinition;
|
||||||
|
AssemblyNameReference InteropServicesAssembly;
|
||||||
|
|
||||||
TypeAndMemberMap ModuleMap;
|
TypeAndMemberMap ModuleMap;
|
||||||
|
|
||||||
|
@ -80,6 +81,11 @@ namespace Microsoft.MaciOS.Nnyeah {
|
||||||
NativeIntegerAttributeTypeRef = new TypeReference (NativeIntegerAttributeTypeDef.Namespace,
|
NativeIntegerAttributeTypeRef = new TypeReference (NativeIntegerAttributeTypeDef.Namespace,
|
||||||
NativeIntegerAttributeTypeDef.Name, NativeIntegerAttributeTypeDef.Module, NativeIntegerAttributeTypeDef.Scope);
|
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
|
// if these type references aren't found in the module
|
||||||
// then we don't ever need to worry about reworking them
|
// 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)) {
|
if (!module.TryGetTypeReference ("System.nfloat", out NfloatTypeReference)) {
|
||||||
NfloatTypeReference = EmptyTypeReference;
|
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");
|
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
|
// 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 {
|
public class DependencyRemovedTests {
|
||||||
[TestCase ("nint")]
|
[TestCase ("nint")]
|
||||||
[TestCase ("nuint")]
|
[TestCase ("nuint")]
|
||||||
// nfloat has an issue on write.
|
[TestCase ("nfloat")]
|
||||||
// [TestCase ("nfloat")]
|
|
||||||
public async Task BasicDependencyRemoved (string type)
|
public async Task BasicDependencyRemoved (string type)
|
||||||
{
|
{
|
||||||
var dir = Cache.CreateTemporaryDirectory ($"DependencyRemoved_{type}");
|
var dir = Cache.CreateTemporaryDirectory ($"DependencyRemoved_{type}");
|
||||||
|
@ -37,7 +36,7 @@ public class Foo {{
|
||||||
var module = ModuleDefinition.ReadModule (targetRewrite);
|
var module = ModuleDefinition.ReadModule (targetRewrite);
|
||||||
|
|
||||||
var platform = module.XamarinPlatformName ();
|
var platform = module.XamarinPlatformName ();
|
||||||
Assert.AreEqual (Microsoft.MaciOS.Nnyeah.PlatformName.None, platform);
|
Assert.AreEqual (Microsoft.MaciOS.Nnyeah.PlatformName.None, platform, "still has Xamarin dependency");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче