Debian Codegraph @

native/common/include/jp_context.h

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #ifndef JP_CONTEXT_H
17 #define JP_CONTEXT_H
18 #include <jpype.h>
19 #include <list>
20
21 /** JPClass is a bit heavy when we just need to hold a
22 * class reference. It causes issues during bootstrap. Thus we
23 * need a lightweight reference to a jclass.
24 */
25 template <class jref>
26 class JPRef
27 {
28 private:
29 jref m_Ref;
30
31 public:
32
33 JPRef()
34 {
35 m_Ref = 0;
36 }
37
38 JPRef(jref obj)
39 {
40 m_Ref = 0;
41 JPJavaFrame frame = JPJavaFrame::outer();
42 m_Ref = (jref) frame.NewGlobalRef((jobject) obj);
43 }
44
45 JPRef(JPJavaFrame& frame, jref obj)
46 {
47
48 m_Ref = 0;
49 m_Ref = (jref) frame.NewGlobalRef((jobject) obj);
50 }
51
52 JPRef(const JPRef& other);
53
54 ~JPRef();
55
56 JPRef& operator=(const JPRef& other);
57
58 jref get() const
59 {
60 return m_Ref;
61 }
62 } ;
63
64 using JPClassRef = JPRef<jclass>;
65 using JPObjectRef = JPRef<jobject>;
66 using JPArrayRef = JPRef<jarray>;
67 using JPThrowableRef = JPRef<jthrowable>;
68
69 class JPStackInfo;
70 class JPGarbageCollection;
71
72 void assertJVMRunning(JPContext* context, const JPStackInfo& info);
73
74 int hasInterrupt();
75
76 /**
77 * A Context encapsulates the Java virtual machine, the Java classes required
78 * to function, and the JPype services created for that machine.
79 *
80 * Frames, Environments and Contexts are different concepts.
81 * - Java context is shared with all objects that exist in a virtual machine.
82 * - Java environment exists for each thread for each machine.
83 * - Java frames exist in the stack holding the local variables that
84 * method.
85 * Frames and Environments should never be held longer than the duration of
86 * a method.
87 *
88 * The members in the Context are broken into
89 * - JVM control functions
90 * - JPype services
91 * - Java functions
92 * - Java type resources
93 *
94 * There are two critical phases in the context lifespan where things
95 * are most likely to go wrong. During JVM startup, many vital functions are
96 * not yet fully configured and thus an exception issued during that period
97 * can lead to using a resource that is not yet configured.
98 *
99 * Second shutdown is a vital period. If the user calls shutdown from within
100 * a callback in which there are still JPype context elements on the call
101 * stack, it can lead to a crash.
102 *
103 * After shutdown, the JVM is set to NULL and all actions should assert that
104 * it is running and thus fail. Aside from the context and the services, all
105 * other JPype C++ resources are owned by Java. Java will delete them as needed.
106 * The context itself belongs to Python.
107 */
108 class JPContext
109 {
110 public:
111 friend class JPJavaFrame;
112 friend class JPypeException;
113 friend class JPClass;
114
115 JPContext();
116 virtual ~JPContext();
117 JPContext(const JPContext& orig) = delete;
118
119 // JVM control functions
120 bool isRunning();
121 void startJVM(const string& vmPath, const StringVector& args,
122 bool ignoreUnrecognized, bool convertStrings, bool interrupt);
123 void attachJVM(JNIEnv* env);
124 void initializeResources(JNIEnv* env, bool interrupt);
125 void shutdownJVM(bool destroyJVM, bool freeJVM);
126 void attachCurrentThread();
127 void attachCurrentThreadAsDaemon();
128 bool isThreadAttached();
129 void detachCurrentThread();
130
131 JNIEnv* getEnv();
132
133 JavaVM* getJavaVM()
134 {
135 return m_JavaVM;
136 }
137
138 jobject getJavaContext()
139 {
140 return m_JavaContext.get();
141 }
142
143 /** Release a global reference checking for shutdown.
144 *
145 * This should be used in any calls to release resources from a destructor.
146 * It cannot fail even if the JVM is no longer operating.
147 */
148 void ReleaseGlobalRef(jobject obj);
149
150 // JPype services
151
152 JPTypeManager* getTypeManager()
153 {
154 return m_TypeManager;
155 }
156
157 JPClassLoader* getClassLoader()
158 {
159 return m_ClassLoader;
160 }
161
162 bool getConvertStrings() const
163 {
164 return m_ConvertStrings;
165 }
166
167 // Java type resources
168 JPPrimitiveType* _void{};
169 JPPrimitiveType* _boolean{};
170 JPPrimitiveType* _byte{};
171 JPPrimitiveType* _char{};
172 JPPrimitiveType* _short{};
173 JPPrimitiveType* _int{};
174 JPPrimitiveType* _long{};
175 JPPrimitiveType* _float{};
176 JPPrimitiveType* _double{};
177
178 JPBoxedType* _java_lang_Void{};
179 JPBoxedType* _java_lang_Boolean{};
180 JPBoxedType* _java_lang_Byte{};
181 JPBoxedType* _java_lang_Character{};
182 JPBoxedType* _java_lang_Short{};
183 JPBoxedType* _java_lang_Integer{};
184 JPBoxedType* _java_lang_Long{};
185 JPBoxedType* _java_lang_Float{};
186 JPBoxedType* _java_lang_Double{};
187
188 JPClass* _java_lang_Object{};
189 JPClass* _java_lang_Class{};
190 JPClass* _java_lang_reflect_Field{};
191 JPClass* _java_lang_reflect_Method{};
192 JPClass* _java_lang_Throwable{};
193 JPStringType* _java_lang_String{};
194 JPClass* _java_nio_ByteBuffer{};
195
196 private:
197
198 void loadEntryPoints(const string& path);
199
200 jint(JNICALL * CreateJVM_Method)(JavaVM **pvm, void **penv, void *args){};
201 jint(JNICALL * GetCreatedJVMs_Method)(JavaVM **pvm, jsize size, jsize * nVms){};
202
203 private:
204
205 JavaVM *m_JavaVM{};
206
207 // Java half
208 JPObjectRef m_JavaContext;
209
210 // Services
211 JPTypeManager *m_TypeManager{};
212 JPClassLoader *m_ClassLoader{};
213
214 public:
215 JPClassRef m_ContextClass;
216 JPClassRef m_RuntimeException;
217
218 private:
219 JPClassRef m_Array;
220 JPObjectRef m_Reflector;
221
222 // Java Functions
223 jmethodID m_Object_ToStringID{};
224 jmethodID m_Object_EqualsID{};
225 jmethodID m_Object_HashCodeID{};
226 jmethodID m_CallMethodID{};
227 jmethodID m_Class_GetNameID{};
228 jmethodID m_Context_collectRectangularID{};
229 jmethodID m_Context_assembleID{};
230 jmethodID m_String_ToCharArrayID{};
231 jmethodID m_Context_CreateExceptionID{};
232 jmethodID m_Context_GetExcClassID{};
233 jmethodID m_Context_GetExcValueID{};
234 jmethodID m_Context_ClearInterruptID{};
235 jmethodID m_CompareToID{};
236 jmethodID m_Buffer_IsReadOnlyID{};
237 jmethodID m_Buffer_AsReadOnlyID{};
238 jmethodID m_Context_OrderID{};
239 jmethodID m_Object_GetClassID{};
240 jmethodID m_Array_NewInstanceID{};
241 jmethodID m_Throwable_GetCauseID{};
242 jmethodID m_Throwable_GetMessageID{};
243 jmethodID m_Context_GetFunctionalID{};
244 friend class JPProxy;
245 JPClassRef m_ProxyClass;
246 jmethodID m_Proxy_NewID{};
247 jmethodID m_Proxy_NewInstanceID{};
248
249 jmethodID m_Context_IsPackageID{};
250 jmethodID m_Context_GetPackageID{};
251 jmethodID m_Package_GetObjectID{};
252 jmethodID m_Package_GetContentsID{};
253 jmethodID m_Context_NewWrapperID{};
254 public:
255 jmethodID m_Context_GetStackFrameID{};
256 void onShutdown();
257
258 private:
259 bool m_Running{};
260 bool m_ConvertStrings{};
261 bool m_Embedded;
262 public:
263 JPGarbageCollection *m_GC;
264
265 // This will gather C++ resources to clean up after shutdown.
266 std::list<JPResource*> m_Resources;
267 } ;
268
269 extern "C" JPContext* JPContext_global;
270
271 extern void JPRef_failed();
272
273 // GCOVR_EXCL_START
274 // Not currently used
275
276 template<class jref>
277 JPRef<jref>::JPRef(const JPRef& other)
278 {
279 if (JPContext_global != nullptr)
280 {
281 JPJavaFrame frame = JPJavaFrame::external(JPContext_global->getEnv());
282 m_Ref = (jref) frame.NewGlobalRef((jobject) other.m_Ref);
283 } else
284 {
285 JPRef_failed();
286 }
287 }
288 // GCOVR_EXCL_STOP
289
290 template<class jref>
291 JPRef<jref>::~JPRef()
292 {
293 if (m_Ref != 0)
294 {
295 JPContext_global->ReleaseGlobalRef((jobject) m_Ref);
296 }
297 }
298
299 template<class jref>
300 JPRef<jref>& JPRef<jref>::operator=(const JPRef<jref>& other)
301 {
302 if (other.m_Ref == m_Ref)
303 return *this;
304 // m_Context may or may not be set up here, so we need to use a
305 // different frame for unreferencing and referencing
306 if (JPContext_global != nullptr && m_Ref != 0)
307 { // GCOVR_EXCL_START
308 // This code is not currently used.
309 JPJavaFrame frame = JPJavaFrame::external(JPContext_global->getEnv());
310 if (m_Ref != 0)
311 frame.DeleteGlobalRef((jobject) m_Ref);
312 } // GCOVR_EXCL_STOP
313 m_Ref = other.m_Ref;
314 if (JPContext_global != nullptr && m_Ref != 0)
315 {
316 JPJavaFrame frame = JPJavaFrame::external(JPContext_global->getEnv());
317 m_Ref = (jref) frame.NewGlobalRef((jobject) m_Ref);
318 }
319 return *this;
320 }
321
322 #endif /* JP_CONTEXT_H */