| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,13 +10,8 @@ class InternalLoadContext : AssemblyLoadContext | |||
| 10 | 10 | ||
| 11 | 11 | protected override IntPtr LoadUnmanagedDll(string name) | |
| 12 | 12 | { | |
| 13 | - if (name == "__Internal") | ||
| 14 | - { | ||
| 15 | - var loader = LibraryLoader.Get(OperatingSystemType.Linux); | ||
| 16 | - return loader.Load(null); | ||
| 17 | - } | ||
| 18 | - | ||
| 19 | - return IntPtr.Zero; | ||
| 13 | + var filtered = name == "__Internal" ? null : name; | ||
| 14 | + return LibraryLoader.Instance.Load(filtered); | ||
| 20 | 15 | } | |
| 21 | 16 | ||
| 22 | 17 | public static AssemblyLoadContext Instance { get; } = new InternalLoadContext(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,18 +15,27 @@ interface ILibraryLoader | |||
| 15 | 15 | ||
| 16 | 16 | static class LibraryLoader | |
| 17 | 17 | { | |
| 18 | - public static ILibraryLoader Get(OperatingSystemType os) | ||
| 18 | + static ILibraryLoader _instance = null; | ||
| 19 | + | ||
| 20 | + public static ILibraryLoader Instance | ||
| 19 | 21 | { | |
| 20 | - switch (os) | ||
| 22 | + get | ||
| 21 | 23 | { | |
| 22 | - case OperatingSystemType.Windows: | ||
| 23 | - return new WindowsLoader(); | ||
| 24 | - case OperatingSystemType.Darwin: | ||
| 25 | - return new DarwinLoader(); | ||
| 26 | - case OperatingSystemType.Linux: | ||
| 27 | - return new LinuxLoader(); | ||
| 28 | - default: | ||
| 29 | - throw new PlatformNotSupportedException($"This operating system ({os}) is not supported"); | ||
| 24 | + if (_instance == null) | ||
| 25 | + { | ||
| 26 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| 27 | + _instance = new WindowsLoader(); | ||
| 28 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
| 29 | + _instance = new DarwinLoader(); | ||
| 30 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| 31 | + _instance = new LinuxLoader(); | ||
| 32 | + else | ||
| 33 | + throw new PlatformNotSupportedException( | ||
| 34 | + $"This operating system is not supported" | ||
| 35 | + ); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + return _instance; | ||
| 30 | 39 | } | |
| 31 | 40 | } | |
| 32 | 41 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,56 +26,7 @@ public static partial class Runtime | |||
| 26 | 26 | ||
| 27 | 27 | internal static bool Is32Bit = IntPtr.Size == 4; | |
| 28 | 28 | ||
| 29 | - // .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | ||
| 30 | - internal static bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; | ||
| 31 | - | ||
| 32 | - static readonly Dictionary<string, OperatingSystemType> OperatingSystemTypeMapping = new Dictionary<string, OperatingSystemType>() | ||
| 33 | - { | ||
| 34 | - { "Windows", OperatingSystemType.Windows }, | ||
| 35 | - { "Darwin", OperatingSystemType.Darwin }, | ||
| 36 | - { "Linux", OperatingSystemType.Linux }, | ||
| 37 | - }; | ||
| 38 | - | ||
| 39 | - /// <summary> | ||
| 40 | - /// Gets the operating system as reported by python's platform.system(). | ||
| 41 | - /// </summary> | ||
| 42 | - public static OperatingSystemType OperatingSystem { get; private set; } | ||
| 43 | - | ||
| 44 | - /// <summary> | ||
| 45 | - /// Gets the operating system as reported by python's platform.system(). | ||
| 46 | - /// </summary> | ||
| 47 | - public static string OperatingSystemName { get; private set; } | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - /// <summary> | ||
| 51 | - /// Map lower-case version of the python machine name to the processor | ||
| 52 | - /// type. There are aliases, e.g. x86_64 and amd64 are two names for | ||
| 53 | - /// the same thing. Make sure to lower-case the search string, because | ||
| 54 | - /// capitalization can differ. | ||
| 55 | - /// </summary> | ||
| 56 | - static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>() | ||
| 57 | - { | ||
| 58 | - ["i386"] = MachineType.i386, | ||
| 59 | - ["i686"] = MachineType.i386, | ||
| 60 | - ["x86"] = MachineType.i386, | ||
| 61 | - ["x86_64"] = MachineType.x86_64, | ||
| 62 | - ["amd64"] = MachineType.x86_64, | ||
| 63 | - ["x64"] = MachineType.x86_64, | ||
| 64 | - ["em64t"] = MachineType.x86_64, | ||
| 65 | - ["armv7l"] = MachineType.armv7l, | ||
| 66 | - ["armv8"] = MachineType.armv8, | ||
| 67 | - ["aarch64"] = MachineType.aarch64, | ||
| 68 | - }; | ||
| 69 | - | ||
| 70 | - /// <summary> | ||
| 71 | - /// Gets the machine architecture as reported by python's platform.machine(). | ||
| 72 | - /// </summary> | ||
| 73 | - public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */ | ||
| 74 | - | ||
| 75 | - /// <summary> | ||
| 76 | - /// Gets the machine architecture as reported by python's platform.machine(). | ||
| 77 | - /// </summary> | ||
| 78 | - public static string MachineName { get; private set; } | ||
| 29 | + internal static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
| 79 | 30 | ||
| 80 | 31 | #if PYTHON2 | |
| 81 | 32 | internal static bool IsPython2 = true; | |
@@ -221,13 +172,8 @@ internal static void Initialize(bool initSigs = false) | |||
| 221 | 172 | ||
| 222 | 173 | Error = new IntPtr(-1); | |
| 223 | 174 | ||
| 224 | - // Initialize data about the platform we're running on. We need | ||
| 225 | - // this for the type manager and potentially other details. Must | ||
| 226 | - // happen after caching the python types, above. | ||
| 227 | - InitializePlatformData(); | ||
| 228 | - | ||
| 229 | 175 | IntPtr dllLocal = IntPtr.Zero; | |
| 230 | - var loader = LibraryLoader.Get(OperatingSystem); | ||
| 176 | + var loader = LibraryLoader.Instance; | ||
| 231 | 177 | ||
| 232 | 178 | _PyObject_NextNotImplemented = loader.GetFunction(dllLocal, "_PyObject_NextNotImplemented"); | |
| 233 | 179 | PyModuleType = loader.GetFunction(dllLocal, "PyModule_Type"); | |
@@ -248,53 +194,6 @@ internal static void Initialize(bool initSigs = false) | |||
| 248 | 194 | AssemblyManager.UpdatePath(); | |
| 249 | 195 | } | |
| 250 | 196 | ||
| 251 | - /// <summary> | ||
| 252 | - /// Initializes the data about platforms. | ||
| 253 | - /// | ||
| 254 | - /// This must be the last step when initializing the runtime: | ||
| 255 | - /// GetManagedString needs to have the cached values for types. | ||
| 256 | - /// But it must run before initializing anything outside the runtime | ||
| 257 | - /// because those rely on the platform data. | ||
| 258 | - /// </summary> | ||
| 259 | - private static void InitializePlatformData() | ||
| 260 | - { | ||
| 261 | - IntPtr op; | ||
| 262 | - IntPtr fn; | ||
| 263 | - IntPtr platformModule = PyImport_ImportModule("platform"); | ||
| 264 | - IntPtr emptyTuple = PyTuple_New(0); | ||
| 265 | - | ||
| 266 | - fn = PyObject_GetAttrString(platformModule, "system"); | ||
| 267 | - op = PyObject_Call(fn, emptyTuple, IntPtr.Zero); | ||
| 268 | - OperatingSystemName = GetManagedString(op); | ||
| 269 | - XDecref(op); | ||
| 270 | - XDecref(fn); | ||
| 271 | - | ||
| 272 | - fn = PyObject_GetAttrString(platformModule, "machine"); | ||
| 273 | - op = PyObject_Call(fn, emptyTuple, IntPtr.Zero); | ||
| 274 | - MachineName = GetManagedString(op); | ||
| 275 | - XDecref(op); | ||
| 276 | - XDecref(fn); | ||
| 277 | - | ||
| 278 | - XDecref(emptyTuple); | ||
| 279 | - XDecref(platformModule); | ||
| 280 | - | ||
| 281 | - // Now convert the strings into enum values so we can do switch | ||
| 282 | - // statements rather than constant parsing. | ||
| 283 | - OperatingSystemType OSType; | ||
| 284 | - if (!OperatingSystemTypeMapping.TryGetValue(OperatingSystemName, out OSType)) | ||
| 285 | - { | ||
| 286 | - OSType = OperatingSystemType.Other; | ||
| 287 | - } | ||
| 288 | - OperatingSystem = OSType; | ||
| 289 | - | ||
| 290 | - MachineType MType; | ||
| 291 | - if (!MachineTypeMapping.TryGetValue(MachineName.ToLower(), out MType)) | ||
| 292 | - { | ||
| 293 | - MType = MachineType.Other; | ||
| 294 | - } | ||
| 295 | - Machine = MType; | ||
| 296 | - } | ||
| 297 | - | ||
| 298 | 197 | internal static void Shutdown() | |
| 299 | 198 | { | |
| 300 | 199 | AssemblyManager.Shutdown(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -503,11 +503,11 @@ public static NativeCode Active | |||
| 503 | 503 | { | |
| 504 | 504 | get | |
| 505 | 505 | { | |
| 506 | - switch (Runtime.Machine) | ||
| 506 | + switch (RuntimeInformation.ProcessArchitecture) | ||
| 507 | 507 | { | |
| 508 | - case MachineType.i386: | ||
| 508 | + case Architecture.X86: | ||
| 509 | 509 | return I386; | |
| 510 | - case MachineType.x86_64: | ||
| 510 | + case Architecture.X64: | ||
| 511 | 511 | return X86_64; | |
| 512 | 512 | default: | |
| 513 | 513 | return null; | |
@@ -600,15 +600,12 @@ int MAP_ANONYMOUS | |||
| 600 | 600 | { | |
| 601 | 601 | get | |
| 602 | 602 | { | |
| 603 | - switch (Runtime.OperatingSystem) | ||
| 604 | - { | ||
| 605 | - case OperatingSystemType.Darwin: | ||
| 606 | - return 0x1000; | ||
| 607 | - case OperatingSystemType.Linux: | ||
| 608 | - return 0x20; | ||
| 609 | - default: | ||
| 610 | - throw new NotImplementedException($"mmap is not supported on {Runtime.OperatingSystemName}"); | ||
| 611 | - } | ||
| 603 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
| 604 | + return 0x1000; | ||
| 605 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| 606 | + return 0x20; | ||
| 607 | + else | ||
| 608 | + throw new NotImplementedException($"mmap is not supported on this operating system"); | ||
| 612 | 609 | } | |
| 613 | 610 | } | |
| 614 | 611 | ||
@@ -633,16 +630,10 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes) | |||
| 633 | 630 | ||
| 634 | 631 | internal static IMemoryMapper CreateMemoryMapper() | |
| 635 | 632 | { | |
| 636 | - switch (Runtime.OperatingSystem) | ||
| 637 | - { | ||
| 638 | - case OperatingSystemType.Darwin: | ||
| 639 | - case OperatingSystemType.Linux: | ||
| 640 | - return new UnixMemoryMapper(); | ||
| 641 | - case OperatingSystemType.Windows: | ||
| 642 | - return new WindowsMemoryMapper(); | ||
| 643 | - default: | ||
| 644 | - throw new NotImplementedException($"No support for {Runtime.OperatingSystemName}"); | ||
| 645 | - } | ||
| 633 | + if (Runtime.IsWindows) | ||
| 634 | + return new WindowsMemoryMapper(); | ||
| 635 | + else | ||
| 636 | + return new UnixMemoryMapper(); | ||
| 646 | 637 | } | |
| 647 | 638 | ||
| 648 | 639 | /// <summary> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments