IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
JNIVM.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2014 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25using System.Collections.Generic;
26using System.Text;
27
28using IKVM.ByteCode.Text;
30
31namespace IKVM.Runtime.JNI
32{
33
34#if FIRST_PASS == false && IMPORTER == false && EXPORTER == false
35
36 using jsize = System.Int32;
37
38 sealed unsafe partial class JNIVM
39 {
40
41 static readonly MUTF8Encoding MUTF8 = MUTF8Encoding.GetMUTF8(52);
42
43 internal static volatile bool jvmCreated;
44 internal static volatile bool jvmDestroyed;
45
46#if NETFRAMEWORK
47 static readonly Encoding platformEncoding = Encoding.Default;
48#else
49 static readonly Encoding platformEncoding = CodePagesEncodingProvider.Instance.GetEncoding(0);
50#endif
51
52 internal static bool IsSupportedJNIVersion(int version)
53 {
54 return version == JNIEnv.JNI_VERSION_1_1
55 || version == JNIEnv.JNI_VERSION_1_2
56 || version == JNIEnv.JNI_VERSION_1_4
57 || version == JNIEnv.JNI_VERSION_1_6
58 || version == JNIEnv.JNI_VERSION_1_8
59 ;
60 }
61
67 static string DecodePlatformString(byte* psz)
68 {
69 var p = psz is not null ? MemoryMarshalExtensions.GetIndexOfNull(psz) : -1;
70 return p < 0 ? null : platformEncoding.GetString(psz, p);
71 }
72
80 public static int CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args)
81 {
82 var pInitArgs = (JavaVMInitArgs*)vm_args;
83
84 // we don't support the JDK 1.1 JavaVMInitArgs
85 if (!IsSupportedJNIVersion(pInitArgs->version) || pInitArgs->version == JNIEnv.JNI_VERSION_1_1)
86 return JNIEnv.JNI_EVERSION;
87
88 // JVM is already created, only one allowed at a time
89 if (jvmCreated)
90 return JNIEnv.JNI_ERR;
91
92 var properties = new Dictionary<string, string>();
93 for (int i = 0; i < pInitArgs->nOptions; i++)
94 {
95 var option = DecodePlatformString(pInitArgs->options[i].optionString);
96 if (option == null)
97 return JNIEnv.JNI_ERR;
98
99 if (option.StartsWith("-D"))
100 {
101 var idx = option.IndexOf('=', 2);
102 properties[option.Substring(2, idx - 2)] = option.Substring(idx + 1);
103 }
104 else if (option.StartsWith("-verbose"))
105 {
106 // ignore
107 }
108 else if (option == "vfprintf" || option == "exit" || option == "abort")
109 {
110 // not supported
111 }
112 else if (pInitArgs->ignoreUnrecognized == JNIEnv.JNI_FALSE)
113 {
114 return JNIEnv.JNI_ERR;
115 }
116 }
117
118 // initialize the JVM properties
119 foreach (var kvp in properties)
120 JVM.Properties.User[kvp.Key] = kvp.Value;
121
122 java.lang.Thread.currentThread();
123 *p_vm = JavaVM.pJavaVM;
124
125 return JavaVM.AttachCurrentThread(JavaVM.pJavaVM, p_env, null);
126 }
127
133 public static int GetDefaultJavaVMInitArgs(void* vm_args)
134 {
135 // This is only used for JDK 1.1 JavaVMInitArgs, and we don't support those.
136 return JNIEnv.JNI_ERR;
137 }
138
146 public static int GetCreatedJavaVMs(JavaVM** ppvmBuf, jsize bufLen, jsize* nVMs)
147 {
148 if (jvmCreated)
149 {
150 if (bufLen >= 1)
151 *ppvmBuf = JavaVM.pJavaVM;
152
153 if (nVMs != null)
154 *nVMs = 1;
155 }
156 else if (nVMs != null)
157 *nVMs = 0;
158
159 return JNIEnv.JNI_OK;
160 }
161
162 }
163
164#endif
165
166}
static unsafe int GetIndexOfNull(byte *ptr, int max=int.MaxValue)
Gets the length of a pointer to a C string.
static int GetCreatedJavaVMs(JavaVM **ppvmBuf, jsize bufLen, jsize *nVMs)
Implements the JNI_GetCreatedJavaVMs native function.
Definition JNIVM.cs:146
static int CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
Implements the JNI_CreateJavaVM native method.
Definition JNIVM.cs:80
static int GetDefaultJavaVMInitArgs(void *vm_args)
Implements the JNI_GetDefaultJavaVMInitArgs native function.
Definition JNIVM.cs:133
Property values loaded into the JVM from various sources.
static IDictionary< string, string > User
Gets the set of properties that are set by the user before initialization. Modification of values in ...
Main state of the running JVM.
System.Int32 jsize
Definition JNIEnv.cs:48