Commit 28849ec
authored
[Java.Interop] Allow JniRuntime init from JavaVM* and JNIEnv* (#1158)
Context: #1153
[JNI][0] supports *two* modes of operation:
1. Native code creates the JVM, e.g. via [`JNI_CreateJavaVM()`][1]
2. The JVM already exists, and when Java code calls
[`System.loadLibrary()`][3], the JVM calls the
[`JNI_OnLoad()`][2] function on the specified library.
Java.Interop samples and unit tests rely on the first approach,
e.g. `TestJVM` subclasses `JreRuntime`, which is responsible for
calling `JNI_CreateJavaVM()` so that Java code can be used.
PR #1153 is exploring the use of [.NET Native AOT][4] to produce a
native library which is used with Java-originated initialization.
In order to make Java-originated initialization *work*, we need
to be able to initialize `JniRuntime` and `JreRuntime` around
existing JVM-provided pointers:
* The `JavaVM*` provided to `JNI_OnLoad()`, which can be used to
set `JniRuntime.CreationOptions.InvocationPointer`:
[UnmanagedCallersOnly(EntryPoint="JNI_OnLoad")]
int JNI_OnLoad(IntPtr vm, IntPtr reserved)
{
var options = new JreRuntimeOptions {
InvocationPointer = vm,
};
var runtime = options.CreateJreVM ();
return runtime.JniVersion;
return JNI_VERSION_1_6;
}
* The [`JNIEnv*` value provided to Java `native` methods][5] when
they are invoked, which can be used to set
`JniRuntime.CreationOptions.EnvironmentPointer`:
[UnmanagedCallersOnly(EntryPoint="Java_example_Whatever_init")]
void Whatever_init(IntPtr jnienv, IntPtr Whatever_class)
{
var options = new JreRuntimeOptions {
EnvironmentPointer = jnienv,
};
var runtime = options.CreateJreVM ();
}
Update `JniRuntime` and `JreRuntime` to support these Java-originated
initialization strategies. In particular, don't require that
`JreRuntimeOptions.JvmLibraryPath` be set, avoiding:
System.InvalidOperationException: Member `JreRuntimeOptions.JvmLibraryPath` must be set.
at Java.Interop.JreRuntime.CreateJreVM(JreRuntimeOptions builder)
at Java.Interop.JreRuntime..ctor(JreRuntimeOptions builder)
at Java.Interop.JreRuntimeOptions.CreateJreVM()
[0]: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html
[1]: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#creating_the_vm
[2]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Runtime.html#loadLibrary(java.lang.String)
[3]: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#JNJI_OnLoad
[4]: https://learn.microsoft.com/dotnet/core/deploying/native-aot/
[5]: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#native_method_arguments1 parent 38c8a82 commit 28849ec
File tree
3 files changed
+62
-10
lines changed- src
- Java.Interop/Java.Interop
- Java.Runtime.Environment/Java.Interop
- tests/Java.Interop-Tests/Java.Interop
3 files changed
+62
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | | - | |
169 | | - | |
| 168 | + | |
| 169 | + | |
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
179 | 184 | | |
180 | 185 | | |
181 | 186 | | |
| |||
230 | 235 | | |
231 | 236 | | |
232 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
233 | 258 | | |
234 | 259 | | |
235 | 260 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 42 | + | |
47 | 43 | | |
48 | 44 | | |
49 | 45 | | |
| |||
80 | 76 | | |
81 | 77 | | |
82 | 78 | | |
83 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
84 | 82 | | |
85 | 83 | | |
86 | 84 | | |
| |||
99 | 97 | | |
100 | 98 | | |
101 | 99 | | |
102 | | - | |
| 100 | + | |
103 | 101 | | |
104 | 102 | | |
105 | 103 | | |
106 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
107 | 115 | | |
108 | 116 | | |
109 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
39 | 58 | | |
40 | 59 | | |
41 | 60 | | |
| |||
0 commit comments