IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
LibIkvm.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Runtime.InteropServices;
4
5namespace IKVM.Runtime
6{
7
8#if FIRST_PASS == false && IMPORTER == FALSE && EXPORTER == false
9
14 internal class LibIkvm
15 {
16
20 static class Externs
21 {
22
28 [DllImport("ikvm", SetLastError = false)]
29 internal static extern nint IKVM_dl_open([MarshalAs(UnmanagedType.LPUTF8Str)] string path);
30
37 [DllImport("ikvm", SetLastError = false)]
38 internal static extern nint IKVM_dl_sym(nint handle, [MarshalAs(UnmanagedType.LPStr)] string symbol);
39
45 [DllImport("ikvm", SetLastError = false)]
46 internal static extern void IKVM_dl_close(nint handle);
47
52 [DllImport("ikvm", SetLastError = false)]
53 internal static unsafe extern int IKVM_sig_get_size_sigaction();
54
60 [DllImport("ikvm", SetLastError = false)]
61 internal static unsafe extern int IKVM_sig_get_chld_action(void* sig);
62
68 [DllImport("ikvm", SetLastError = false)]
69 internal static unsafe extern int IKVM_sig_set_chld_action(void* sig);
70
71 }
72
78 [DllImport("kernel32", SetLastError = false)]
79 static extern nint LoadLibrary(string lpLibFileName);
80
86 [DllImport("kernel32", SetLastError = false)]
87 static extern nint FreeLibrary(nint handle);
88
94 static nint LoadImpl(string nameOrPath)
95 {
96#if NETFRAMEWORK
97 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
98 {
99 return LoadLibrary(nameOrPath);
100 }
101 else
102 {
103 // Mono (Framework on non-Windows), just P/Invoke, we can remap with dllmap
104 return Externs.IKVM_dl_open(nameOrPath);
105 }
106#else
107 return System.Runtime.InteropServices.NativeLibrary.TryLoad(nameOrPath, typeof(LibIkvm).Assembly, null, out var h) ? h : 0;
108#endif
109 }
110
116 static void FreeImpl(nint handle)
117 {
118#if NETFRAMEWORK
119 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
120 {
121 FreeLibrary(handle);
122 }
123 else
124 {
125 // Mono (Framework on non-Windows), just P/Invoke, we can remap with dllmap
126 // this should be a secondary reference, as the DllImports should own the primary
127 Externs.IKVM_dl_close(handle);
128 }
129#else
130 System.Runtime.InteropServices.NativeLibrary.Free(handle);
131#endif
132 }
133
137 public static readonly LibIkvm Instance = new();
138
142 public nint Handle { get; private set; }
143
147 LibIkvm()
148 {
149 if ((Handle = Load()) == 0)
150 throw new InternalException("Could not load ikvm native library.");
151 }
152
156 static nint Load()
157 {
158 // give native loader a chance to load the library
159 if (LoadImpl("ikvm") is nint h1 and not 0)
160 return h1;
161
162 // start looking at assembly path, or path given by environmental variable
163 var asml = typeof(NativeLibrary).Assembly.Location is string s && !string.IsNullOrEmpty(s) ? Path.GetDirectoryName(s) : null;
164 var root = Environment.GetEnvironmentVariable("IKVM_LIBRARY_PATH") ?? asml ?? AppContext.BaseDirectory;
165
166 // assembly possible loaded in memory: we have no available search path
167 if (string.IsNullOrEmpty(root))
168 return 0;
169
170 // check in root directory
171 if (Path.Combine(root, NativeLibrary.MapLibraryName("ikvm")) is string lib1)
172 if (File.Exists(lib1) && LoadImpl(lib1) is nint h2 and not 0)
173 return h2;
174
175 // scan supported RIDs for current platform
176 foreach (var rid in RuntimeUtil.SupportedRuntimeIdentifiers)
177 if (Path.Combine(root, "runtimes", rid, "native", NativeLibrary.MapLibraryName("ikvm")) is string lib2)
178 if (File.Exists(lib2) && LoadImpl(lib2) is nint h3 and not 0)
179 return h3;
180
181 return 0;
182 }
183
189 public nint dl_open(string path) => Externs.IKVM_dl_open(path);
190
197 public nint dl_sym(nint handle, string symbol) => Externs.IKVM_dl_sym(handle, symbol);
198
204 public void dl_close(nint handle) => Externs.IKVM_dl_close(handle);
205
210 public unsafe int sig_get_size_sigaction() => Externs.IKVM_sig_get_size_sigaction();
211
217 public unsafe int sig_get_chld_action(void* sig) => Externs.IKVM_sig_get_chld_action(sig);
218
224 public unsafe int sig_set_chld_action(void* sig) => Externs.IKVM_sig_set_chld_action(sig);
225
229 ~LibIkvm()
230 {
231 if (Handle != 0)
232 {
233 var h = Handle;
234 Handle = 0;
235 FreeImpl(h);
236 }
237 }
238
239 }
240
241#endif
242
243}
IKVM.Reflection.Assembly Assembly