Codemesh Runtime v3 C++ API Reference  3.9.205
jnet_rt.h
1 //
2 // Copyright (c) 1999-2020 by Codemesh, Inc.. ALL RIGHTS RESERVED.
3 //
4 
5 #ifndef NETRT_INC
6 #define NETRT_INC
7 
8 
9 #include "xmog_localenv.h"
10 
11 
12 #if (XMOG_SUPPORTS_DOTNET==1)
13 
14 
15 typedef jint int32;
16 typedef jlong int64;
17 
18 
19 #undef TRACE
20 
21 
22 typedef void (__stdcall * TRACING_CALLBACK)( int, LPSTR );
23 
24 
25 /*
26  * Trace levels that are controlled from the .NET side
27  */
28 #define TL_NONE 0
29 #define TL_ERROR 1
30 #define TL_WARNING 2
31 #define TL_INFO 3
32 #define TL_VERBOSE 4
33 
34 #define STR_SUCCESS "OK"
35 #define STR_FAILURE "ERROR"
36 
37 
38 #define STATIC 1
39 
40 #define TYPE_EXCEPTION 1 /*overloaded value used in callbacks*/
41 
42 #define TYPE_BOOLEAN 2
43 #define TYPE_BYTE 4
44 #define TYPE_CHAR 8
45 #define TYPE_DOUBLE 16
46 #define TYPE_FLOAT 32
47 #define TYPE_INT 64
48 #define TYPE_LONG 128
49 #define TYPE_SHORT 256
50 #define TYPE_OBJECT 512
51 #define TYPE_VOID 1024
52 
53 #define CTOR 2048
54 #define FINAL 2048
55 
56 #define ARRCTOR 4096
57 
58 #define CACHED 8192
59 #define NETRT_INDIRECT 16384
60 
61 #define JNI_TYPE_MASK (TYPE_BOOLEAN|TYPE_BYTE|TYPE_CHAR|TYPE_DOUBLE|TYPE_FLOAT|TYPE_INT|TYPE_LONG|TYPE_SHORT|TYPE_OBJECT|TYPE_VOID)
62 #define TYPE_MASK (JNI_TYPE_MASK)
63 
64 #define TYPE(x) ((x)&(TYPE_MASK))
65 #define IS_ARRCTOR(x) (((x)&(ARRCTOR))!=0)
66 #define IS_CTOR(x) (((x)&(CTOR))!=0)
67 #define IS_STATIC(x) (((x)&(STATIC))!=0)
68 #define IS_INDIRECT(x) (((x)&(NETRT_INDIRECT))!=0)
69 #define IS_VOID(x) (((x)&(TYPE_VOID))!=0)
70 
71 
72 #define NETRT_SUCCESS (0)
73 #define NETRT_FAILURE (-1)
74 #define NETRT_WARNING (1)
75 #define IS_NETRT_SUCCESS(x) ((x)==NETRT_SUCCESS)
76 
77 
78 //----------------------------------------------------------------------------
79 //
80 // A class encapsulating global JNI references AND NOT PROVIDING JNI CLEANUP.
81 //
82 // This class is intended to be a convenient holder of object references that
83 // are passed over to the .NET side. For that reason, we never delete the
84 // JNI references that are held by instances of this class.
85 //
86 class global_object
87 {
88 private:
89 
90  JNIEnv * env;
91  jobject jobj;
92 
93 public:
94 
95  //
96  // Initialize with a pending exception.
97  //
98  global_object( JNIEnv * lenv ) :
99  env( lenv ),
100  jobj( 0 )
101  {
102  jobject temp = env->ExceptionOccurred();
103 
104  if( temp )
105  {
106 #ifdef TRACE
107  env->ExceptionDescribe();
108 #endif
109  env->ExceptionClear();
110  jobj = env->NewGlobalRef( temp );
111  env->DeleteLocalRef( temp );
112  }
113  }
114 
115  global_object( JNIEnv * lenv, jobject gobj ) :
116  env( lenv ),
117  jobj( gobj )
118  {
119  }
120 
121  global_object( const global_object & gobj ) :
122  env( gobj.env ),
123  jobj( gobj.jobj )
124  {
125  }
126 
127  global_object & operator = ( const global_object & gobj )
128  {
129  env = gobj.env;
130  jobj = gobj.jobj;
131  }
132 
133  operator jclass() const
134  {
135  return (jclass)jobj;
136  }
137 
138  operator jobject() const
139  {
140  return jobj;
141  }
142 
143  //
144  // The method used to extract the reference into an int for use as a result.
145  //
146  /*operator int() const
147  {
148  return (int)jobj;
149  }*/
150 
151  ~global_object()
152  {
153  // !!! DOES NOT DELETE THE GLOBAL REFERENCE !!!
154  }
155 };
156 
157 
158 //----------------------------------------------------------------------------
159 //
160 // A class providing automatic cleanup for local JNI references.
161 //
162 class local_object
163 {
164 private:
165 
166  JNIEnv * env;
167  jobject jobj;
168 
169 public:
170 
171  local_object( JNIEnv * lenv, jobject lobj ) :
172  env( lenv ),
173  jobj( lobj )
174  {
175  }
176 
177  local_object( const local_object & lobj ) :
178  env( lobj.env ),
179  jobj( lobj.jobj )
180  {
181  if( jobj )
182  jobj = env->NewLocalRef( jobj );
183  }
184 
185  local_object & operator = ( const local_object & lobj )
186  {
187  env = lobj.env;
188  if( jobj )
189  env->DeleteLocalRef( jobj );
190  jobj = lobj.jobj;
191  if( jobj )
192  jobj = env->NewLocalRef( jobj );
193 
194  return *this;
195  }
196 
197  local_object & operator = ( jobject jobj )
198  {
199  if( this->jobj )
200  env->DeleteLocalRef( this->jobj );
201  this->jobj = jobj;
202 
203  return *this;
204  }
205 
206  operator global_object() const
207  {
208  jobject temp = jobj ? env->NewGlobalRef( jobj ) : 0;
209 
210  return global_object( env, temp );
211  }
212 
213  operator jobject() const
214  {
215  return jobj;
216  }
217 
218  /*operator int() const
219  {
220  return (int)jobj;
221  }*/
222 
223  ~local_object()
224  {
225  if( jobj )
226  env->DeleteLocalRef( jobj );
227  }
228 };
229 
230 //----------------------------------------------------------------------------
231 //
232 // A utility class for acquiring a JNIEnv pointer.
233 //
234 class env_manager
235 {
236 public:
237 
238  static JavaVM * theJVM;
239 
240  static HMODULE hJVM;
241 
242  static HMODULE hThis;
243 
244  static DWORD dwTLSIndex;
245 
246  static CRITICAL_SECTION initjvm_lock;
247 
248  static CRITICAL_SECTION initccl_lock;
249 
250  static int theJNILevel;
251 
252  static int theJVMLevel;
253 
254  static int tl_jvm;
255 
256  static void * tcb_jvm;
257 
258  static int tl_typesystem;
259 
260  static void * tcb_typesystem;
261 
262  static int tl_callbacks;
263 
264  static void * tcb_callbacks;
265 
266  static int tl_finalization;
267 
268  static void * tcb_finalization;
269 
270  static jclass clsClass;
271 
272  static jclass clsClassLoader;
273 
274  static jclass clsThread;
275 
276  static jclass clsJvalue;
277 
278  static jclass clsRuntimeException;
279 
280  static jclass clsCLRException;
281 
282  static jobject sysClassLoader;
283 
284  static jmethodID midGetName;
285 
286  static jmethodID midLoadClass;
287 
288  static jmethodID midCurrentThread;
289 
290  static jmethodID midGetContextClassLoader;
291 
292  static jmethodID midGetSystemClassLoader;
293 
294  static jmethodID midSetContextClassLoader;
295 
296  static jmethodID midGetImplementedIfcs;
297 
298  static jmethodID midRTEctor;
299 
300  static bool bShuttingDown;
301 
302  static jint (__stdcall *cbVfprintf)( LPSTR msg );
303 
304  static void (__stdcall *cbExit)(jint code);
305 
306  static void (__stdcall *cbAbort)(void);
307 
308 
309 public:
310 
311  static void calculate_levels();
312 
313  static void init( HMODULE hThis );
314 
315  static void initClass();
316 
317  static void initCCLFix();
318 
319  static void init_jvalue( JNIEnv * env );
320 
321  static void deinit();
322 
323  static JNIEnv * get_env();
324 
325  static void set_env( JNIEnv * env );
326 
327  static int on_load( JavaVM * vm, void * reserved );
328 
329  static void on_unload( JavaVM * vm, void * reserved );
330 };
331 
332 
333 typedef int (__stdcall JUGGERNET_CALLBACK)( int & result_type, jvalue & result, jvalue * param );
334 
335 extern "C"
336 {
337  void JNICALL dotnet_abort();
338 
339  void JNICALL dotnet_exit( jint code );
340 
341  jint JNICALL dotnet_vfprintf( FILE * fp, const char *format, va_list args );
342 
343  jint JNICALL dotnet_callback( JNIEnv * env, jclass clazz, jobject that, int cbPointer, jobject result, jobject params );
344 
345  __declspec(dllexport) int __stdcall attach( int group, LPSTR name, BOOL bAsDaemon );
346 
347  __declspec(dllexport) int __stdcall bind_jvm( LPSTR name, int & bWasAlreadyLoaded, int & bWasAlreadyInitialized );
348 
349  __declspec(dllexport) int __stdcall call_method( int methodID,
350  int clazz,
351  int inst,
352  int flags,
353  jvalue * param,
354  jvalue & result,
355  int & exc );
356 
357  __declspec(dllexport) int __stdcall define_class( LPWSTR name, LPBYTE code, UINT size, int & clazz, int & exc );
358 
359  __declspec(dllexport) int __stdcall detach();
360 
361  __declspec(dllexport) int __stdcall equals( int o1, int o2, BOOL & result, int & exc );
362 
363  __declspec(dllexport) int __stdcall find_class( LPWSTR name, int & clazz, int & exc );
364 
365  __declspec(dllexport) int __stdcall for_name( LPWSTR name, int & inst, int & exc );
366 
367  __declspec(dllexport) int __stdcall free_instance( int inst );
368 
369  __declspec(dllexport) int __stdcall get_array_element( int arr, int index, int type, jvalue & val, int & exc );
370 
371  __declspec(dllexport) int __stdcall get_array_length( int arr, int & result, int & exc );
372 
373  __declspec(dllexport) int __stdcall get_class( int inst );
374 
375  __declspec(dllexport) int __stdcall get_classname( int inst, LPWSTR name, int size, int & required );
376 
377  __declspec(dllexport) int __stdcall get_classclassname( int clazz, LPWSTR name, int size, int & required );
378 
379  __declspec(dllexport) int __stdcall get_duplicate( int inst );
380 
381  __declspec(dllexport) int __stdcall get_exception_message( int exc, LPWSTR msg, int size, int & required );
382 
383  __declspec(dllexport) int __stdcall get_ifcnames( int inst, int & strarr );
384 
385  __declspec(dllexport) int __stdcall get_ifc_name( int strarr, int index, LPWSTR msg, int size, int & required );
386 
387  __declspec(dllexport) int __stdcall get_stacktrace( int exc, LPWSTR msg, int size, int & required );
388 
389  __declspec(dllexport) int __stdcall get_string( jobject str, LPWSTR buffer, int32 size, int32 & required );
390 
391  __declspec(dllexport) jclass __stdcall get_superclass( jclass clazz );
392 
393  __declspec(dllexport) int __stdcall hashCode( jobject obj, jobject & exc );
394 
395  __declspec(dllexport) int __stdcall init_field( jclass clazz, LPWSTR wname, LPWSTR wtype, int32 flags, jfieldID & fieldID, jobject & exc );
396 
397  __declspec(dllexport) int __stdcall init_method( jclass clazz, LPWSTR wname, LPWSTR wtype, int32 flags, jmethodID & methodID, jobject & exc );
398 
399  __declspec(dllexport) int __stdcall init_tracing_callbacks( void* pTraceCallbacks, void* pTraceJvm, void* pTraveTypeSystem );
400 
401  __declspec(dllexport) bool __stdcall is_instance_of( jobject inst, jclass clazz );
402 
403  __declspec(dllexport) bool __stdcall is_same( jobject inst1, jobject inst2 );
404 
405  __declspec(dllexport) int __stdcall new_instance( LPWSTR classname, jclass clazz, __int64 size, jobject & result, jobject & exc );
406 
407  jint JNICALL java_call( JNIEnv * env, jclass clazz, jobject java_this, jlong dotnet_this, jlong cbPointer, jlong mid, jobject result, jobjectArray params );
408 
409  __declspec(dllexport) int __stdcall set_array_element( jobject arr, int32 index, int32 type, jvalue val, jobject & exc );
410 
411  __declspec(dllexport) int __stdcall set_array_byte( jobject arr, void * narr, int32 start, int32 length, jobject & exc );
412 
413  __declspec(dllexport) int __stdcall to_string( LPWSTR str, jobject & result, jobject & exc );
414 
415  __declspec(dllexport) int __stdcall ToString( jlong inst, LPWSTR result, int32 size, int32 & required, jlong & exc );
416 };
417 
418 //
419 // These are alternate entrypoints that take a prefetched environment pointer as input.
420 //
421 // They are not exported from the library and are only used when one method in this library
422 // delegates to another.
423 //
424 int check_license();
425 int call_method( xmog_localenv * env, jmethodID methodID, jobject clazz, jobject inst, int32 flags, jvalue * param, jvalue & result, jobject & exc );
426 int equals( xmog_localenv * env, jobject o1, jobject o2, BOOL & result, jobject & exc );
427 int find_class( xmog_localenv * env, LPWSTR name, jobject & clazz, jobject & exc, bool bIgnoreExc );
428 int free_instance( xmog_localenv * env, jobject inst );
429 int get_array_element( xmog_localenv * env, jobject arr, int32 index, int32 type, jvalue & val, jobject & exc );
430 int get_array_length( xmog_localenv * env, jobject arr, int32 & result, jobject & exc );
431 jlong GetClass( xmog_localenv * env, jlong inst );
432 int GetClassName( xmog_localenv * env, jlong inst, LPSTR name, int32 size, int32 & required );
433 int GetClassClassName( xmog_localenv * env, jlong clazz, LPSTR name, int32 size, int32 & required );
434 int get_exception_message( xmog_localenv * env, jobject exc, LPWSTR msg, int32 size, int32 & required );
435 int get_stacktrace( xmog_localenv * env, jobject exc, LPWSTR msg, int32 size, int32 & required );
436 int Get_String( xmog_localenv * env, jlong str, LPWSTR buffer, int32 size, int32 & required );
437 jclass get_superclass( xmog_localenv * env, jclass clazz );
438 int new_instance( xmog_localenv * env, LPWSTR classname, jclass clazz, __int64 size, jobject & result, jobject & exc );
439 int set_array_element( xmog_localenv * env, jobject arr, int32 index, int32 type, jvalue val, jobject & exc );
440 int to_string( xmog_localenv * env, LPWSTR str, jobject & result, jobject & exc );
441 int ToString( xmog_localenv * env, jlong inst, LPWSTR result, int32 size, int32 & required, jlong & exc );
442 LPSTR toUTF( LPWSTR wstr );
443 LPSTR toUTFwithReplace( LPWSTR wstr, WCHAR from, WCHAR to );
444 LPWSTR fromUTF( LPSTR str );
445 
446 
447 
448 #endif /* XMOG_SUPPORTS_DOTNET */
449 
450 
451 #endif /* NETRT_INC */
xmog_localenv
A class representing per-thread information for the integration runtime.
Definition: xmog_localenv.h:32

Copyright (c) 1999-2020 by Codemesh, Inc., ALL RIGHTS RESERVED.