1 /* src/native/jni.cpp - implementation of the Java Native Interface functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "mm/memory.h"
37 #include "native/jni.hpp"
38 #include "native/llni.h"
39 #include "native/localref.h"
40 #include "native/native.hpp"
42 #if defined(ENABLE_JVMTI)
43 # include "native/jvmti/cacaodbg.h"
46 #include "threads/lock.hpp"
47 #include "threads/mutex.hpp"
48 #include "threads/thread.hpp"
50 #include "toolbox/logging.h"
53 #include "vm/jit/builtin.hpp"
54 #include "vm/exceptions.hpp"
55 #include "vm/global.h"
56 #include "vm/globals.hpp"
57 #include "vm/initialize.h"
58 #include "vm/javaobjects.hpp"
59 #include "vm/loader.hpp"
60 #include "vm/options.h"
61 #include "vm/primitive.hpp"
62 #include "vm/resolve.h"
63 #include "vm/statistics.h"
64 #include "vm/string.hpp"
67 #include "vm/jit/argument.h"
68 #include "vm/jit/asmpart.h"
69 #include "vm/jit/jit.hpp"
70 #include "vm/jit/stacktrace.hpp"
73 /* debug **********************************************************************/
77 # define TRACEJNICALLS(x) \
79 if (opt_TraceJNICalls) { \
84 # define TRACEJNICALLSENTER(x) \
86 if (opt_TraceJNICalls) { \
92 # define TRACEJNICALLSEXIT(x) \
94 if (opt_TraceJNICalls) { \
102 # define TRACEJNICALLS(x)
103 # define TRACEJNICALLSENTER(x)
104 # define TRACEJNICALLSEXIT(x)
109 /* global variables ***********************************************************/
111 /* global reference table *****************************************************/
113 /* hashsize must be power of 2 */
115 #define HASHTABLE_GLOBAL_REF_SIZE 64 /* initial size of globalref-hash */
117 static hashtable *hashtable_global_ref; /* hashtable for globalrefs */
120 /* direct buffer stuff ********************************************************/
122 #if defined(ENABLE_JAVASE)
123 static classinfo *class_java_nio_Buffer;
125 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
127 static classinfo *class_java_nio_DirectByteBufferImpl;
128 static classinfo *class_java_nio_DirectByteBufferImpl_ReadWrite;
130 # if SIZEOF_VOID_P == 8
131 static classinfo *class_gnu_classpath_Pointer64;
133 static classinfo *class_gnu_classpath_Pointer32;
136 static methodinfo *dbbirw_init;
138 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
140 static classinfo *class_sun_nio_ch_DirectBuffer;
141 static classinfo *class_java_nio_DirectByteBuffer;
143 static methodinfo *dbb_init;
149 /* some forward declarations **************************************************/
152 jobject jni_NewLocalRef(JNIEnv *env, jobject ref);
156 /* jni_init ********************************************************************
158 Initialize the JNI subsystem.
160 *******************************************************************************/
164 TRACESUBSYSTEMINITIALIZATION("jni_init");
166 /* create global ref hashtable */
168 hashtable_global_ref = NEW(hashtable);
170 hashtable_create(hashtable_global_ref, HASHTABLE_GLOBAL_REF_SIZE);
173 #if defined(ENABLE_JAVASE)
174 /* Direct buffer stuff. */
176 if (!(class_java_nio_Buffer =
177 load_class_bootstrap(utf_new_char("java/nio/Buffer"))) ||
178 !link_class(class_java_nio_Buffer))
181 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
183 if (!(class_java_nio_DirectByteBufferImpl =
184 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl"))) ||
185 !link_class(class_java_nio_DirectByteBufferImpl))
188 if (!(class_java_nio_DirectByteBufferImpl_ReadWrite =
189 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl$ReadWrite"))) ||
190 !link_class(class_java_nio_DirectByteBufferImpl_ReadWrite))
194 class_resolvemethod(class_java_nio_DirectByteBufferImpl_ReadWrite,
196 utf_new_char("(Ljava/lang/Object;Lgnu/classpath/Pointer;III)V"))))
199 # if SIZEOF_VOID_P == 8
200 if (!(class_gnu_classpath_Pointer64 =
201 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer64"))) ||
202 !link_class(class_gnu_classpath_Pointer64))
205 if (!(class_gnu_classpath_Pointer32 =
206 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer32"))) ||
207 !link_class(class_gnu_classpath_Pointer32))
211 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
213 if (!(class_sun_nio_ch_DirectBuffer =
214 load_class_bootstrap(utf_new_char("sun/nio/ch/DirectBuffer"))))
215 vm_abort("jni_init: loading sun/nio/ch/DirectBuffer failed");
217 if (!link_class(class_sun_nio_ch_DirectBuffer))
218 vm_abort("jni_init: linking sun/nio/ch/DirectBuffer failed");
220 if (!(class_java_nio_DirectByteBuffer =
221 load_class_bootstrap(utf_new_char("java/nio/DirectByteBuffer"))))
222 vm_abort("jni_init: loading java/nio/DirectByteBuffer failed");
224 if (!link_class(class_java_nio_DirectByteBuffer))
225 vm_abort("jni_init: linking java/nio/DirectByteBuffer failed");
228 class_resolvemethod(class_java_nio_DirectByteBuffer,
230 utf_new_char("(JI)V"))))
231 vm_abort("jni_init: resolving java/nio/DirectByteBuffer.init(JI)V failed");
235 #endif /* defined(ENABLE_JAVASE) */
241 /* jni_version_check ***********************************************************
243 Check if the given JNI version is supported.
246 version....JNI version to check
250 false......not supported
252 *******************************************************************************/
254 bool jni_version_check(int version)
257 case JNI_VERSION_1_1:
258 case JNI_VERSION_1_2:
259 case JNI_VERSION_1_4:
260 case JNI_VERSION_1_6:
268 /* _Jv_jni_CallObjectMethod ****************************************************
270 Internal function to call Java Object methods.
272 *******************************************************************************/
274 static java_handle_t *_Jv_jni_CallObjectMethod(java_handle_t *o,
276 methodinfo *m, va_list ap)
281 STATISTICS(jniinvokation());
284 exceptions_throw_nullpointerexception();
288 /* Class initialization is done by the JIT compiler. This is ok
289 since a static method always belongs to the declaring class. */
291 if (m->flags & ACC_STATIC) {
292 /* For static methods we reset the object. */
297 /* for convenience */
302 /* For instance methods we make a virtual function table lookup. */
304 resm = method_vftbl_lookup(vftbl, m);
307 STATISTICS(jnicallXmethodnvokation());
309 ro = vm_call_method_valist(resm, o, ap);
315 /* _Jv_jni_CallObjectMethodA ***************************************************
317 Internal function to call Java Object methods.
319 *******************************************************************************/
321 static java_handle_t *_Jv_jni_CallObjectMethodA(java_handle_t *o,
329 STATISTICS(jniinvokation());
332 exceptions_throw_nullpointerexception();
336 /* Class initialization is done by the JIT compiler. This is ok
337 since a static method always belongs to the declaring class. */
339 if (m->flags & ACC_STATIC) {
340 /* For static methods we reset the object. */
345 /* for convenience */
350 /* For instance methods we make a virtual function table lookup. */
352 resm = method_vftbl_lookup(vftbl, m);
355 STATISTICS(jnicallXmethodnvokation());
357 ro = vm_call_method_jvalue(resm, o, args);
363 /* _Jv_jni_CallIntMethod *******************************************************
365 Internal function to call Java integer class methods (boolean,
366 byte, char, short, int).
368 *******************************************************************************/
370 static jint _Jv_jni_CallIntMethod(java_handle_t *o, vftbl_t *vftbl,
371 methodinfo *m, va_list ap)
376 STATISTICS(jniinvokation());
379 exceptions_throw_nullpointerexception();
383 /* Class initialization is done by the JIT compiler. This is ok
384 since a static method always belongs to the declaring class. */
386 if (m->flags & ACC_STATIC) {
387 /* For static methods we reset the object. */
392 /* for convenience */
397 /* For instance methods we make a virtual function table lookup. */
399 resm = method_vftbl_lookup(vftbl, m);
402 STATISTICS(jnicallXmethodnvokation());
404 i = vm_call_method_int_valist(resm, o, ap);
410 /* _Jv_jni_CallIntMethodA ******************************************************
412 Internal function to call Java integer class methods (boolean,
413 byte, char, short, int).
415 *******************************************************************************/
417 static jint _Jv_jni_CallIntMethodA(java_handle_t *o, vftbl_t *vftbl,
418 methodinfo *m, const jvalue *args)
423 STATISTICS(jniinvokation());
426 exceptions_throw_nullpointerexception();
430 /* Class initialization is done by the JIT compiler. This is ok
431 since a static method always belongs to the declaring class. */
433 if (m->flags & ACC_STATIC) {
434 /* For static methods we reset the object. */
439 /* for convenience */
444 /* For instance methods we make a virtual function table lookup. */
446 resm = method_vftbl_lookup(vftbl, m);
449 STATISTICS(jnicallXmethodnvokation());
451 i = vm_call_method_int_jvalue(resm, o, args);
457 /* _Jv_jni_CallLongMethod ******************************************************
459 Internal function to call Java long methods.
461 *******************************************************************************/
463 static jlong _Jv_jni_CallLongMethod(java_handle_t *o, vftbl_t *vftbl,
464 methodinfo *m, va_list ap)
469 STATISTICS(jniinvokation());
472 exceptions_throw_nullpointerexception();
476 /* Class initialization is done by the JIT compiler. This is ok
477 since a static method always belongs to the declaring class. */
479 if (m->flags & ACC_STATIC) {
480 /* For static methods we reset the object. */
485 /* for convenience */
490 /* For instance methods we make a virtual function table lookup. */
492 resm = method_vftbl_lookup(vftbl, m);
495 STATISTICS(jnicallXmethodnvokation());
497 l = vm_call_method_long_valist(resm, o, ap);
503 /* _Jv_jni_CallLongMethodA *****************************************************
505 Internal function to call Java long methods.
507 *******************************************************************************/
509 static jlong _Jv_jni_CallLongMethodA(java_handle_t *o, vftbl_t *vftbl,
510 methodinfo *m, const jvalue *args)
515 STATISTICS(jniinvokation());
518 exceptions_throw_nullpointerexception();
522 /* Class initialization is done by the JIT compiler. This is ok
523 since a static method always belongs to the declaring class. */
525 if (m->flags & ACC_STATIC) {
526 /* For static methods we reset the object. */
531 /* for convenience */
536 /* For instance methods we make a virtual function table lookup. */
538 resm = method_vftbl_lookup(vftbl, m);
541 STATISTICS(jnicallXmethodnvokation());
543 l = vm_call_method_long_jvalue(resm, o, args);
549 /* _Jv_jni_CallFloatMethod *****************************************************
551 Internal function to call Java float methods.
553 *******************************************************************************/
555 static jfloat _Jv_jni_CallFloatMethod(java_handle_t *o, vftbl_t *vftbl,
556 methodinfo *m, va_list ap)
561 /* Class initialization is done by the JIT compiler. This is ok
562 since a static method always belongs to the declaring class. */
564 if (m->flags & ACC_STATIC) {
565 /* For static methods we reset the object. */
570 /* for convenience */
575 /* For instance methods we make a virtual function table lookup. */
577 resm = method_vftbl_lookup(vftbl, m);
580 STATISTICS(jnicallXmethodnvokation());
582 f = vm_call_method_float_valist(resm, o, ap);
588 /* _Jv_jni_CallFloatMethodA ****************************************************
590 Internal function to call Java float methods.
592 *******************************************************************************/
594 static jfloat _Jv_jni_CallFloatMethodA(java_handle_t *o, vftbl_t *vftbl,
595 methodinfo *m, const jvalue *args)
600 /* Class initialization is done by the JIT compiler. This is ok
601 since a static method always belongs to the declaring class. */
603 if (m->flags & ACC_STATIC) {
604 /* For static methods we reset the object. */
609 /* for convenience */
614 /* For instance methods we make a virtual function table lookup. */
616 resm = method_vftbl_lookup(vftbl, m);
619 STATISTICS(jnicallXmethodnvokation());
621 f = vm_call_method_float_jvalue(resm, o, args);
627 /* _Jv_jni_CallDoubleMethod ****************************************************
629 Internal function to call Java double methods.
631 *******************************************************************************/
633 static jdouble _Jv_jni_CallDoubleMethod(java_handle_t *o, vftbl_t *vftbl,
634 methodinfo *m, va_list ap)
639 /* Class initialization is done by the JIT compiler. This is ok
640 since a static method always belongs to the declaring class. */
642 if (m->flags & ACC_STATIC) {
643 /* For static methods we reset the object. */
648 /* for convenience */
653 /* For instance methods we make a virtual function table lookup. */
655 resm = method_vftbl_lookup(vftbl, m);
658 d = vm_call_method_double_valist(resm, o, ap);
664 /* _Jv_jni_CallDoubleMethodA ***************************************************
666 Internal function to call Java double methods.
668 *******************************************************************************/
670 static jdouble _Jv_jni_CallDoubleMethodA(java_handle_t *o, vftbl_t *vftbl,
671 methodinfo *m, const jvalue *args)
676 /* Class initialization is done by the JIT compiler. This is ok
677 since a static method always belongs to the declaring class. */
679 if (m->flags & ACC_STATIC) {
680 /* For static methods we reset the object. */
685 /* for convenience */
690 /* For instance methods we make a virtual function table lookup. */
692 resm = method_vftbl_lookup(vftbl, m);
695 d = vm_call_method_double_jvalue(resm, o, args);
701 /* _Jv_jni_CallVoidMethod ******************************************************
703 Internal function to call Java void methods.
705 *******************************************************************************/
707 static void _Jv_jni_CallVoidMethod(java_handle_t *o, vftbl_t *vftbl,
708 methodinfo *m, va_list ap)
713 exceptions_throw_nullpointerexception();
717 /* Class initialization is done by the JIT compiler. This is ok
718 since a static method always belongs to the declaring class. */
720 if (m->flags & ACC_STATIC) {
721 /* For static methods we reset the object. */
726 /* for convenience */
731 /* For instance methods we make a virtual function table lookup. */
733 resm = method_vftbl_lookup(vftbl, m);
736 STATISTICS(jnicallXmethodnvokation());
738 (void) vm_call_method_valist(resm, o, ap);
742 /* _Jv_jni_CallVoidMethodA *****************************************************
744 Internal function to call Java void methods.
746 *******************************************************************************/
748 static void _Jv_jni_CallVoidMethodA(java_handle_t *o, vftbl_t *vftbl,
749 methodinfo *m, const jvalue *args)
754 exceptions_throw_nullpointerexception();
758 /* Class initialization is done by the JIT compiler. This is ok
759 since a static method always belongs to the declaring class. */
761 if (m->flags & ACC_STATIC) {
762 /* For static methods we reset the object. */
767 /* for convenience */
772 /* For instance methods we make a virtual function table lookup. */
774 resm = method_vftbl_lookup(vftbl, m);
777 STATISTICS(jnicallXmethodnvokation());
779 (void) vm_call_method_jvalue(resm, o, args);
783 // JNI functions are exported as C functions.
786 /* GetVersion ******************************************************************
788 Returns the major version number in the higher 16 bits and the
789 minor version number in the lower 16 bits.
791 *******************************************************************************/
793 jint _Jv_JNI_GetVersion(JNIEnv *env)
795 TRACEJNICALLS(("_Jv_JNI_GetVersion(env=%p)", env));
797 /* We support JNI 1.6. */
799 return JNI_VERSION_1_6;
803 /* Class Operations ***********************************************************/
805 /* DefineClass *****************************************************************
807 Loads a class from a buffer of raw class data. The buffer
808 containing the raw class data is not referenced by the VM after the
809 DefineClass call returns, and it may be discarded if desired.
811 *******************************************************************************/
813 jclass jni_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize bufLen)
815 #if defined(ENABLE_JAVASE)
821 TRACEJNICALLS(("jni_DefineClass(env=%p, name=%s, loader=%p, buf=%p, bufLen=%d)", env, name, loader, buf, bufLen));
823 u = utf_new_char(name);
824 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
826 c = class_define(u, cl, bufLen, (uint8_t *) buf, NULL);
828 h = LLNI_classinfo_wrap(c);
830 return (jclass) jni_NewLocalRef(env, (jobject) h);
832 vm_abort("jni_DefineClass: Not implemented in this configuration");
834 // Keep compiler happy.
841 /* FindClass *******************************************************************
843 This function loads a locally-defined class. It searches the
844 directories and zip files specified by the CLASSPATH environment
845 variable for the class with the specified name.
847 *******************************************************************************/
849 jclass jni_FindClass(JNIEnv *env, const char *name)
851 #if defined(ENABLE_JAVASE)
858 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
860 /* FIXME If name is NULL we have a problem here. */
862 u = utf_new_char_classname((char *) name);
864 if ((u == NULL) /*|| (int)strlen(name) > symbolOopDesc::max_length() */) {
865 exceptions_throw_noclassdeffounderror(u);
869 /* Check stacktrace for classloader, if one found use it,
870 otherwise use the system classloader. */
872 /* Quote from the JNI documentation:
874 In the Java 2 Platform, FindClass locates the class loader
875 associated with the current native method. If the native code
876 belongs to a system class, no class loader will be
877 involved. Otherwise, the proper class loader will be invoked to
878 load and link the named class. When FindClass is called through
879 the Invocation Interface, there is no current native method or
880 its associated class loader. In that case, the result of
881 ClassLoader.getBaseClassLoader is used." */
883 cc = stacktrace_get_current_class();
886 c = load_class_from_sysloader(u);
888 c = load_class_from_classloader(u, cc->classloader);
891 resolve_handle_pending_exception(true);
898 h = LLNI_classinfo_wrap(c);
900 return (jclass) jni_NewLocalRef(env, (jobject) h);
902 #elif defined(ENABLE_JAVAME_CLDC1_1)
907 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
909 u = utf_new_char_classname((char *) name);
910 c = load_class_bootstrap(u);
913 resolve_handle_pending_exception(true);
920 return (jclass) jni_NewLocalRef(env, (jobject) c);
923 vm_abort("jni_FindClass: not implemented in this configuration");
925 /* keep compiler happy */
932 /* GetSuperclass ***************************************************************
934 If clazz represents any class other than the class Object, then
935 this function returns the object that represents the superclass of
936 the class specified by clazz.
938 *******************************************************************************/
940 jclass jni_GetSuperclass(JNIEnv *env, jclass sub)
945 TRACEJNICALLS(("jni_GetSuperclass(env=%p, sub=%p)", env, sub));
947 c = LLNI_classinfo_unwrap(sub);
952 super = class_get_superclass(c);
954 java_handle_t* h = LLNI_classinfo_wrap(super);
956 return (jclass) jni_NewLocalRef(env, (jobject) h);
960 /* IsAssignableFrom ************************************************************
962 Determines whether an object of sub can be safely cast to sup.
964 *******************************************************************************/
966 jboolean _Jv_JNI_IsAssignableFrom(JNIEnv *env, jclass sub, jclass sup)
971 TRACEJNICALLS(("_Jv_JNI_IsAssignableFrom(env=%p, sub=%p, sup=%p)", env, sub, sup));
973 to = (classinfo *) sup;
974 from = (classinfo *) sub;
976 return class_is_assignable_from(to, from);
980 /* Throw ***********************************************************************
982 Causes a java.lang.Throwable object to be thrown.
984 *******************************************************************************/
986 jint _Jv_JNI_Throw(JNIEnv *env, jthrowable obj)
990 STATISTICS(jniinvokation());
992 o = (java_handle_t *) obj;
994 exceptions_set_exception(o);
1000 /* ThrowNew ********************************************************************
1002 Constructs an exception object from the specified class with the
1003 message specified by message and causes that exception to be
1006 *******************************************************************************/
1008 jint _Jv_JNI_ThrowNew(JNIEnv* env, jclass clazz, const char *msg)
1014 STATISTICS(jniinvokation());
1016 c = LLNI_classinfo_unwrap(clazz);
1019 s = javastring_new_from_utf_string(msg);
1021 /* instantiate exception object */
1023 o = native_new_and_init_string(c, s);
1028 exceptions_set_exception(o);
1034 /* ExceptionOccurred ***********************************************************
1036 Determines if an exception is being thrown. The exception stays
1037 being thrown until either the native code calls ExceptionClear(),
1038 or the Java code handles the exception.
1040 *******************************************************************************/
1042 jthrowable _Jv_JNI_ExceptionOccurred(JNIEnv *env)
1046 TRACEJNICALLS(("_Jv_JNI_ExceptionOccurred(env=%p)", env));
1048 o = exceptions_get_exception();
1050 return (jthrowable) jni_NewLocalRef(env, (jthrowable) o);
1054 /* ExceptionDescribe ***********************************************************
1056 Prints an exception and a backtrace of the stack to a system
1057 error-reporting channel, such as stderr. This is a convenience
1058 routine provided for debugging.
1060 *******************************************************************************/
1062 void jni_ExceptionDescribe(JNIEnv *env)
1064 TRACEJNICALLS(("jni_ExceptionDescribe(env=%p)", env));
1066 exceptions_print_stacktrace();
1070 /* ExceptionClear **************************************************************
1072 Clears any exception that is currently being thrown. If no
1073 exception is currently being thrown, this routine has no effect.
1075 *******************************************************************************/
1077 void jni_ExceptionClear(JNIEnv *env)
1079 TRACEJNICALLS(("jni_ExceptionClear(env=%p)", env));
1081 exceptions_clear_exception();
1085 /* FatalError ******************************************************************
1087 Raises a fatal error and does not expect the VM to recover. This
1088 function does not return.
1090 *******************************************************************************/
1092 void _Jv_JNI_FatalError(JNIEnv *env, const char *msg)
1094 STATISTICS(jniinvokation());
1096 /* this seems to be the best way */
1098 vm_abort("JNI Fatal error: %s", msg);
1102 /* PushLocalFrame **************************************************************
1104 Creates a new local reference frame, in which at least a given
1105 number of local references can be created.
1107 *******************************************************************************/
1109 jint jni_PushLocalFrame(JNIEnv* env, jint capacity)
1111 TRACEJNICALLS(("jni_PushLocalFrame(env=%p, capacity=%d)", env, capacity));
1116 /* add new local reference frame to current table */
1118 if (!localref_frame_push(capacity))
1125 /* PopLocalFrame ***************************************************************
1127 Pops off the current local reference frame, frees all the local
1128 references, and returns a local reference in the previous local
1129 reference frame for the given result object.
1131 *******************************************************************************/
1133 jobject jni_PopLocalFrame(JNIEnv* env, jobject result)
1135 TRACEJNICALLS(("jni_PopLocalFrame(env=%p, result=%p)", env, result));
1137 /* release all current local frames */
1139 localref_frame_pop_all();
1141 /* add local reference and return the value */
1143 return jni_NewLocalRef(env, result);
1147 /* DeleteLocalRef **************************************************************
1149 Deletes the local reference pointed to by localRef.
1151 *******************************************************************************/
1153 void jni_DeleteLocalRef(JNIEnv *env, jobject localRef)
1157 TRACEJNICALLS(("jni_DeleteLocalRef(env=%p, ref=%p)", env, localRef));
1159 o = (java_handle_t *) localRef;
1164 /* delete the reference */
1170 /* IsSameObject ****************************************************************
1172 Tests whether two references refer to the same Java object.
1174 *******************************************************************************/
1176 jboolean _Jv_JNI_IsSameObject(JNIEnv *env, jobject ref1, jobject ref2)
1182 STATISTICS(jniinvokation());
1184 o1 = (java_handle_t *) ref1;
1185 o2 = (java_handle_t *) ref2;
1187 LLNI_CRITICAL_START;
1189 if (LLNI_UNWRAP(o1) == LLNI_UNWRAP(o2))
1200 /* NewLocalRef *****************************************************************
1202 Creates a new local reference that refers to the same object as ref.
1204 *******************************************************************************/
1206 jobject jni_NewLocalRef(JNIEnv *env, jobject ref)
1209 java_handle_t *localref;
1211 TRACEJNICALLS(("jni_NewLocalRef(env=%p, ref=%p)", env, ref));
1213 o = (java_handle_t *) ref;
1218 /* insert the reference */
1220 localref = localref_add(LLNI_DIRECT(o));
1222 return (jobject) localref;
1226 /* EnsureLocalCapacity *********************************************************
1228 Ensures that at least a given number of local references can be
1229 created in the current thread
1231 *******************************************************************************/
1233 jint jni_EnsureLocalCapacity(JNIEnv* env, jint capacity)
1235 localref_table *lrt;
1237 TRACEJNICALLS(("jni_EnsureLocalCapacity(env=%p, capacity=%d)", env, capacity));
1239 /* get local reference table (thread specific) */
1241 lrt = LOCALREFTABLE;
1243 /* check if capacity elements are available in the local references table */
1245 if ((lrt->used + capacity) > lrt->capacity)
1246 return jni_PushLocalFrame(env, capacity);
1252 /* AllocObject *****************************************************************
1254 Allocates a new Java object without invoking any of the
1255 constructors for the object. Returns a reference to the object.
1257 *******************************************************************************/
1259 jobject _Jv_JNI_AllocObject(JNIEnv *env, jclass clazz)
1264 STATISTICS(jniinvokation());
1266 c = LLNI_classinfo_unwrap(clazz);
1268 if ((c->flags & ACC_INTERFACE) || (c->flags & ACC_ABSTRACT)) {
1269 exceptions_throw_instantiationexception(c);
1275 return jni_NewLocalRef(env, (jobject) o);
1279 /* NewObject *******************************************************************
1281 Programmers place all arguments that are to be passed to the
1282 constructor immediately following the methodID
1283 argument. NewObject() accepts these arguments and passes them to
1284 the Java method that the programmer wishes to invoke.
1286 *******************************************************************************/
1288 jobject jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
1295 TRACEJNICALLSENTER(("jni_NewObject(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
1297 c = LLNI_classinfo_unwrap(clazz);
1298 m = (methodinfo *) methodID;
1307 /* call constructor */
1309 va_start(ap, methodID);
1310 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1313 TRACEJNICALLSEXIT(("->%p", o));
1315 return jni_NewLocalRef(env, (jobject) o);
1319 /* NewObjectV ******************************************************************
1321 Programmers place all arguments that are to be passed to the
1322 constructor in an args argument of type va_list that immediately
1323 follows the methodID argument. NewObjectV() accepts these
1324 arguments, and, in turn, passes them to the Java method that the
1325 programmer wishes to invoke.
1327 *******************************************************************************/
1329 jobject _Jv_JNI_NewObjectV(JNIEnv* env, jclass clazz, jmethodID methodID,
1336 STATISTICS(jniinvokation());
1338 c = LLNI_classinfo_unwrap(clazz);
1339 m = (methodinfo *) methodID;
1348 /* call constructor */
1350 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1352 return jni_NewLocalRef(env, (jobject) o);
1356 /* NewObjectA *****************************************************************
1358 Programmers place all arguments that are to be passed to the
1359 constructor in an args array of jvalues that immediately follows
1360 the methodID argument. NewObjectA() accepts the arguments in this
1361 array, and, in turn, passes them to the Java method that the
1362 programmer wishes to invoke.
1364 *******************************************************************************/
1366 jobject _Jv_JNI_NewObjectA(JNIEnv* env, jclass clazz, jmethodID methodID,
1373 STATISTICS(jniinvokation());
1375 c = LLNI_classinfo_unwrap(clazz);
1376 m = (methodinfo *) methodID;
1385 /* call constructor */
1387 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1389 return jni_NewLocalRef(env, (jobject) o);
1393 /* GetObjectClass **************************************************************
1395 Returns the class of an object.
1397 *******************************************************************************/
1399 jclass jni_GetObjectClass(JNIEnv *env, jobject obj)
1404 TRACEJNICALLS(("jni_GetObjectClass(env=%p, obj=%p)", env, obj));
1406 o = (java_handle_t *) obj;
1408 if ((o == NULL) || (LLNI_vftbl_direct(o) == NULL))
1411 LLNI_class_get(o, c);
1413 java_handle_t* h = LLNI_classinfo_wrap(c);
1415 return (jclass) jni_NewLocalRef(env, (jobject) h);
1419 /* IsInstanceOf ****************************************************************
1421 Tests whether an object is an instance of a class.
1423 *******************************************************************************/
1425 jboolean _Jv_JNI_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)
1430 TRACEJNICALLS(("_Jv_JNI_IsInstanceOf(env=%p, obj=%p, clazz=%p)", env, obj, clazz));
1432 /* XXX Is this correct? */
1433 c = LLNI_classinfo_unwrap(clazz);
1434 h = (java_handle_t *) obj;
1436 return class_is_instance(c, h);
1440 /* Reflection Support *********************************************************/
1442 /* FromReflectedMethod *********************************************************
1444 Converts java.lang.reflect.Method or java.lang.reflect.Constructor
1445 object to a method ID.
1447 *******************************************************************************/
1449 jmethodID jni_FromReflectedMethod(JNIEnv *env, jobject method)
1451 #if defined(ENABLE_JAVASE)
1454 TRACEJNICALLS(("jni_FromReflectedMethod(env=%p, method=%p)", env, method));
1456 java_lang_Object o(method);
1461 if (o.get_Class() == class_java_lang_reflect_Constructor) {
1462 java_lang_reflect_Constructor rc(method);
1463 m = rc.get_method();
1466 assert(o.get_Class() == class_java_lang_reflect_Method);
1468 java_lang_reflect_Method rm(method);
1469 m = rm.get_method();
1472 return (jmethodID) m;
1474 vm_abort("jni_FromReflectedMethod: Not implemented in this configuration.");
1476 // Keep compiler happy.
1482 /* FromReflectedField **********************************************************
1484 Converts a java.lang.reflect.Field to a field ID.
1486 *******************************************************************************/
1488 jfieldID jni_FromReflectedField(JNIEnv* env, jobject field)
1490 #if defined(ENABLE_JAVASE)
1492 TRACEJNICALLS(("jni_FromReflectedField(env=%p, field=%p)", env, field));
1494 java_lang_reflect_Field rf(field);
1499 fieldinfo* f = rf.get_field();
1501 return (jfieldID) f;
1503 vm_abort("jni_FromReflectedField: Not implemented in this configuration.");
1505 // Keep compiler happy.
1511 /* ToReflectedMethod ***********************************************************
1513 Converts a method ID derived from cls to an instance of the
1514 java.lang.reflect.Method class or to an instance of the
1515 java.lang.reflect.Constructor class.
1517 *******************************************************************************/
1519 jobject jni_ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic)
1521 #if defined(ENABLE_JAVASE)
1522 TRACEJNICALLS(("jni_ToReflectedMethod(env=%p, cls=%p, methodID=%p, isStatic=%d)", env, cls, methodID, isStatic));
1524 methodinfo* m = (methodinfo *) methodID;
1526 /* HotSpot does the same assert. */
1528 assert(((m->flags & ACC_STATIC) != 0) == (isStatic != 0));
1532 if (m->name == utf_init) {
1533 h = java_lang_reflect_Constructor(m).get_handle();
1536 h = java_lang_reflect_Method(m).get_handle();
1541 vm_abort("jni_ToReflectedMethod: Not implemented in this configuration.");
1543 /* keep compiler happy */
1550 /* ToReflectedField ************************************************************
1552 Converts a field ID derived from cls to an instance of the
1553 java.lang.reflect.Field class.
1555 *******************************************************************************/
1557 jobject _Jv_JNI_ToReflectedField(JNIEnv* env, jclass cls, jfieldID fieldID,
1560 STATISTICS(jniinvokation());
1562 log_text("JNI-Call: ToReflectedField: IMPLEMENT ME!");
1568 /* Calling Instance Methods ***************************************************/
1570 /* GetMethodID *****************************************************************
1572 Returns the method ID for an instance (nonstatic) method of a class
1573 or interface. The method may be defined in one of the clazz's
1574 superclasses and inherited by clazz. The method is determined by
1575 its name and signature.
1577 GetMethodID() causes an uninitialized class to be initialized.
1579 *******************************************************************************/
1581 jmethodID _Jv_JNI_GetMethodID(JNIEnv* env, jclass clazz, const char *name,
1589 STATISTICS(jniinvokation());
1591 c = LLNI_classinfo_unwrap(clazz);
1596 if (!(c->state & CLASS_INITIALIZED))
1597 if (!initialize_class(c))
1600 /* try to get the method of the class or one of it's superclasses */
1602 uname = utf_new_char((char *) name);
1603 udesc = utf_new_char((char *) sig);
1605 m = class_resolvemethod(c, uname, udesc);
1607 if ((m == NULL) || (m->flags & ACC_STATIC)) {
1608 exceptions_throw_nosuchmethoderror(c, uname, udesc);
1613 return (jmethodID) m;
1617 /* JNI-functions for calling instance methods *********************************/
1619 #define JNI_CALL_VIRTUAL_METHOD(name, type, intern) \
1620 type _Jv_JNI_Call##name##Method(JNIEnv *env, jobject obj, \
1621 jmethodID methodID, ...) \
1628 o = (java_handle_t *) obj; \
1629 m = (methodinfo *) methodID; \
1631 va_start(ap, methodID); \
1632 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, ap); \
1638 JNI_CALL_VIRTUAL_METHOD(Boolean, jboolean, Int)
1639 JNI_CALL_VIRTUAL_METHOD(Byte, jbyte, Int)
1640 JNI_CALL_VIRTUAL_METHOD(Char, jchar, Int)
1641 JNI_CALL_VIRTUAL_METHOD(Short, jshort, Int)
1642 JNI_CALL_VIRTUAL_METHOD(Int, jint, Int)
1643 JNI_CALL_VIRTUAL_METHOD(Long, jlong, Long)
1644 JNI_CALL_VIRTUAL_METHOD(Float, jfloat, Float)
1645 JNI_CALL_VIRTUAL_METHOD(Double, jdouble, Double)
1648 #define JNI_CALL_VIRTUAL_METHOD_V(name, type, intern) \
1649 type _Jv_JNI_Call##name##MethodV(JNIEnv *env, jobject obj, \
1650 jmethodID methodID, va_list args) \
1656 o = (java_handle_t *) obj; \
1657 m = (methodinfo *) methodID; \
1659 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, args); \
1664 JNI_CALL_VIRTUAL_METHOD_V(Boolean, jboolean, Int)
1665 JNI_CALL_VIRTUAL_METHOD_V(Byte, jbyte, Int)
1666 JNI_CALL_VIRTUAL_METHOD_V(Char, jchar, Int)
1667 JNI_CALL_VIRTUAL_METHOD_V(Short, jshort, Int)
1668 JNI_CALL_VIRTUAL_METHOD_V(Int, jint, Int)
1669 JNI_CALL_VIRTUAL_METHOD_V(Long, jlong, Long)
1670 JNI_CALL_VIRTUAL_METHOD_V(Float, jfloat, Float)
1671 JNI_CALL_VIRTUAL_METHOD_V(Double, jdouble, Double)
1674 #define JNI_CALL_VIRTUAL_METHOD_A(name, type, intern) \
1675 type _Jv_JNI_Call##name##MethodA(JNIEnv *env, jobject obj, \
1676 jmethodID methodID, \
1677 const jvalue *args) \
1683 o = (java_handle_t *) obj; \
1684 m = (methodinfo *) methodID; \
1686 ret = _Jv_jni_Call##intern##MethodA(o, LLNI_vftbl_direct(o), m, args); \
1691 JNI_CALL_VIRTUAL_METHOD_A(Boolean, jboolean, Int)
1692 JNI_CALL_VIRTUAL_METHOD_A(Byte, jbyte, Int)
1693 JNI_CALL_VIRTUAL_METHOD_A(Char, jchar, Int)
1694 JNI_CALL_VIRTUAL_METHOD_A(Short, jshort, Int)
1695 JNI_CALL_VIRTUAL_METHOD_A(Int, jint, Int)
1696 JNI_CALL_VIRTUAL_METHOD_A(Long, jlong, Long)
1697 JNI_CALL_VIRTUAL_METHOD_A(Float, jfloat, Float)
1698 JNI_CALL_VIRTUAL_METHOD_A(Double, jdouble, Double)
1701 jobject _Jv_JNI_CallObjectMethod(JNIEnv *env, jobject obj, jmethodID methodID,
1709 o = (java_handle_t *) obj;
1710 m = (methodinfo *) methodID;
1712 va_start(ap, methodID);
1713 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, ap);
1716 return jni_NewLocalRef(env, (jobject) ret);
1720 jobject _Jv_JNI_CallObjectMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1727 o = (java_handle_t *) obj;
1728 m = (methodinfo *) methodID;
1730 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, args);
1732 return jni_NewLocalRef(env, (jobject) ret);
1736 jobject _Jv_JNI_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1743 o = (java_handle_t *) obj;
1744 m = (methodinfo *) methodID;
1746 ret = _Jv_jni_CallObjectMethodA(o, LLNI_vftbl_direct(o), m, args);
1748 return jni_NewLocalRef(env, (jobject) ret);
1753 void _Jv_JNI_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
1759 o = (java_handle_t *) obj;
1760 m = (methodinfo *) methodID;
1762 va_start(ap, methodID);
1763 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1768 void _Jv_JNI_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1774 o = (java_handle_t *) obj;
1775 m = (methodinfo *) methodID;
1777 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1781 void _Jv_JNI_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1787 o = (java_handle_t *) obj;
1788 m = (methodinfo *) methodID;
1790 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1795 #define JNI_CALL_NONVIRTUAL_METHOD(name, type, intern) \
1796 type _Jv_JNI_CallNonvirtual##name##Method(JNIEnv *env, jobject obj, \
1797 jclass clazz, jmethodID methodID, \
1806 o = (java_handle_t *) obj; \
1807 c = LLNI_classinfo_unwrap(clazz); \
1808 m = (methodinfo *) methodID; \
1810 va_start(ap, methodID); \
1811 ret = _Jv_jni_Call##intern##Method(o, c->vftbl, m, ap); \
1817 JNI_CALL_NONVIRTUAL_METHOD(Boolean, jboolean, Int)
1818 JNI_CALL_NONVIRTUAL_METHOD(Byte, jbyte, Int)
1819 JNI_CALL_NONVIRTUAL_METHOD(Char, jchar, Int)
1820 JNI_CALL_NONVIRTUAL_METHOD(Short, jshort, Int)
1821 JNI_CALL_NONVIRTUAL_METHOD(Int, jint, Int)
1822 JNI_CALL_NONVIRTUAL_METHOD(Long, jlong, Long)
1823 JNI_CALL_NONVIRTUAL_METHOD(Float, jfloat, Float)
1824 JNI_CALL_NONVIRTUAL_METHOD(Double, jdouble, Double)
1827 #define JNI_CALL_NONVIRTUAL_METHOD_V(name, type, intern) \
1828 type _Jv_JNI_CallNonvirtual##name##MethodV(JNIEnv *env, jobject obj, \
1829 jclass clazz, jmethodID methodID, \
1837 o = (java_handle_t *) obj; \
1838 c = LLNI_classinfo_unwrap(clazz); \
1839 m = (methodinfo *) methodID; \
1841 ret = _Jv_jni_CallIntMethod(o, c->vftbl, m, args); \
1846 JNI_CALL_NONVIRTUAL_METHOD_V(Boolean, jboolean, Int)
1847 JNI_CALL_NONVIRTUAL_METHOD_V(Byte, jbyte, Int)
1848 JNI_CALL_NONVIRTUAL_METHOD_V(Char, jchar, Int)
1849 JNI_CALL_NONVIRTUAL_METHOD_V(Short, jshort, Int)
1850 JNI_CALL_NONVIRTUAL_METHOD_V(Int, jint, Int)
1851 JNI_CALL_NONVIRTUAL_METHOD_V(Long, jlong, Long)
1852 JNI_CALL_NONVIRTUAL_METHOD_V(Float, jfloat, Float)
1853 JNI_CALL_NONVIRTUAL_METHOD_V(Double, jdouble, Double)
1856 #define JNI_CALL_NONVIRTUAL_METHOD_A(name, type, intern) \
1857 type _Jv_JNI_CallNonvirtual##name##MethodA(JNIEnv *env, jobject obj, \
1858 jclass clazz, jmethodID methodID, \
1859 const jvalue *args) \
1861 log_text("JNI-Call: CallNonvirtual##name##MethodA: IMPLEMENT ME!"); \
1866 JNI_CALL_NONVIRTUAL_METHOD_A(Boolean, jboolean, Int)
1867 JNI_CALL_NONVIRTUAL_METHOD_A(Byte, jbyte, Int)
1868 JNI_CALL_NONVIRTUAL_METHOD_A(Char, jchar, Int)
1869 JNI_CALL_NONVIRTUAL_METHOD_A(Short, jshort, Int)
1870 JNI_CALL_NONVIRTUAL_METHOD_A(Int, jint, Int)
1871 JNI_CALL_NONVIRTUAL_METHOD_A(Long, jlong, Long)
1872 JNI_CALL_NONVIRTUAL_METHOD_A(Float, jfloat, Float)
1873 JNI_CALL_NONVIRTUAL_METHOD_A(Double, jdouble, Double)
1875 jobject _Jv_JNI_CallNonvirtualObjectMethod(JNIEnv *env, jobject obj,
1876 jclass clazz, jmethodID methodID,
1885 o = (java_handle_t *) obj;
1886 c = LLNI_classinfo_unwrap(clazz);
1887 m = (methodinfo *) methodID;
1889 va_start(ap, methodID);
1890 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, ap);
1893 return jni_NewLocalRef(env, (jobject) r);
1897 jobject _Jv_JNI_CallNonvirtualObjectMethodV(JNIEnv *env, jobject obj,
1898 jclass clazz, jmethodID methodID,
1906 o = (java_handle_t *) obj;
1907 c = LLNI_classinfo_unwrap(clazz);
1908 m = (methodinfo *) methodID;
1910 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, args);
1912 return jni_NewLocalRef(env, (jobject) r);
1916 jobject _Jv_JNI_CallNonvirtualObjectMethodA(JNIEnv *env, jobject obj,
1917 jclass clazz, jmethodID methodID,
1920 log_text("JNI-Call: CallNonvirtualObjectMethodA: IMPLEMENT ME!");
1922 return jni_NewLocalRef(env, NULL);
1926 void _Jv_JNI_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass clazz,
1927 jmethodID methodID, ...)
1934 o = (java_handle_t *) obj;
1935 c = LLNI_classinfo_unwrap(clazz);
1936 m = (methodinfo *) methodID;
1938 va_start(ap, methodID);
1939 _Jv_jni_CallVoidMethod(o, c->vftbl, m, ap);
1944 void _Jv_JNI_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass clazz,
1945 jmethodID methodID, va_list args)
1951 o = (java_handle_t *) obj;
1952 c = LLNI_classinfo_unwrap(clazz);
1953 m = (methodinfo *) methodID;
1955 _Jv_jni_CallVoidMethod(o, c->vftbl, m, args);
1959 void _Jv_JNI_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass clazz,
1960 jmethodID methodID, const jvalue * args)
1966 o = (java_handle_t *) obj;
1967 c = LLNI_classinfo_unwrap(clazz);
1968 m = (methodinfo *) methodID;
1970 _Jv_jni_CallVoidMethodA(o, c->vftbl, m, args);
1974 /* Accessing Fields of Objects ************************************************/
1976 /* GetFieldID ******************************************************************
1978 Returns the field ID for an instance (nonstatic) field of a
1979 class. The field is specified by its name and signature. The
1980 Get<type>Field and Set<type>Field families of accessor functions
1981 use field IDs to retrieve object fields.
1983 *******************************************************************************/
1985 jfieldID _Jv_JNI_GetFieldID(JNIEnv *env, jclass clazz, const char *name,
1993 STATISTICS(jniinvokation());
1995 c = LLNI_classinfo_unwrap(clazz);
1997 /* XXX NPE check? */
1999 uname = utf_new_char((char *) name);
2000 udesc = utf_new_char((char *) sig);
2002 f = class_findfield(c, uname, udesc);
2005 exceptions_throw_nosuchfielderror(c, uname);
2007 return (jfieldID) f;
2011 /* Get<type>Field Routines *****************************************************
2013 This family of accessor routines returns the value of an instance
2014 (nonstatic) field of an object. The field to access is specified by
2015 a field ID obtained by calling GetFieldID().
2017 *******************************************************************************/
2019 #define GET_FIELD(o,type,f) \
2020 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset)))
2022 #define JNI_GET_FIELD(name, type, intern) \
2023 type _Jv_JNI_Get##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID) \
2027 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "Field(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID)); \
2029 LLNI_CRITICAL_START; \
2031 ret = GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID); \
2033 LLNI_CRITICAL_END; \
2035 return (type) ret; \
2038 JNI_GET_FIELD(Boolean, jboolean, s4)
2039 JNI_GET_FIELD(Byte, jbyte, s4)
2040 JNI_GET_FIELD(Char, jchar, s4)
2041 JNI_GET_FIELD(Short, jshort, s4)
2042 JNI_GET_FIELD(Int, jint, s4)
2043 JNI_GET_FIELD(Long, jlong, s8)
2044 JNI_GET_FIELD(Float, jfloat, float)
2045 JNI_GET_FIELD(Double, jdouble, double)
2048 jobject _Jv_JNI_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)
2052 TRACEJNICALLS(("_Jv_JNI_GetObjectField(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID));
2054 LLNI_CRITICAL_START;
2056 o = LLNI_WRAP(GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), java_object_t*, fieldID));
2060 return jni_NewLocalRef(env, (jobject) o);
2064 /* Set<type>Field Routines *****************************************************
2066 This family of accessor routines sets the value of an instance
2067 (nonstatic) field of an object. The field to access is specified by
2068 a field ID obtained by calling GetFieldID().
2070 *******************************************************************************/
2072 #define SET_FIELD(o,type,f,value) \
2073 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset))) = (type) (value)
2075 #define JNI_SET_FIELD(name, type, intern) \
2076 void _Jv_JNI_Set##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID, \
2079 TRACEJNICALLS(("_Jv_JNI_Set" STR(name) "Field(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value)); \
2081 LLNI_CRITICAL_START; \
2083 SET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID, value); \
2085 LLNI_CRITICAL_START; \
2088 JNI_SET_FIELD(Boolean, jboolean, s4)
2089 JNI_SET_FIELD(Byte, jbyte, s4)
2090 JNI_SET_FIELD(Char, jchar, s4)
2091 JNI_SET_FIELD(Short, jshort, s4)
2092 JNI_SET_FIELD(Int, jint, s4)
2093 JNI_SET_FIELD(Long, jlong, s8)
2094 JNI_SET_FIELD(Float, jfloat, float)
2095 JNI_SET_FIELD(Double, jdouble, double)
2098 void _Jv_JNI_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID,
2101 TRACEJNICALLS(("_Jv_JNI_SetObjectField(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value));
2103 LLNI_CRITICAL_START;
2105 SET_FIELD(obj, java_handle_t*, fieldID, LLNI_UNWRAP((java_handle_t*) value));
2111 /* Calling Static Methods *****************************************************/
2113 /* GetStaticMethodID ***********************************************************
2115 Returns the method ID for a static method of a class. The method is
2116 specified by its name and signature.
2118 GetStaticMethodID() causes an uninitialized class to be
2121 *******************************************************************************/
2123 jmethodID _Jv_JNI_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name,
2131 TRACEJNICALLS(("_Jv_JNI_GetStaticMethodID(env=%p, clazz=%p, name=%s, sig=%s)", env, clazz, name, sig));
2133 c = LLNI_classinfo_unwrap(clazz);
2138 if (!(c->state & CLASS_INITIALIZED))
2139 if (!initialize_class(c))
2142 /* try to get the static method of the class */
2144 uname = utf_new_char((char *) name);
2145 udesc = utf_new_char((char *) sig);
2147 m = class_resolvemethod(c, uname, udesc);
2149 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
2150 exceptions_throw_nosuchmethoderror(c, uname, udesc);
2155 return (jmethodID) m;
2159 #define JNI_CALL_STATIC_METHOD(name, type, intern) \
2160 type _Jv_JNI_CallStatic##name##Method(JNIEnv *env, jclass clazz, \
2161 jmethodID methodID, ...) \
2167 m = (methodinfo *) methodID; \
2169 va_start(ap, methodID); \
2170 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, ap); \
2176 JNI_CALL_STATIC_METHOD(Boolean, jboolean, Int)
2177 JNI_CALL_STATIC_METHOD(Byte, jbyte, Int)
2178 JNI_CALL_STATIC_METHOD(Char, jchar, Int)
2179 JNI_CALL_STATIC_METHOD(Short, jshort, Int)
2180 JNI_CALL_STATIC_METHOD(Int, jint, Int)
2181 JNI_CALL_STATIC_METHOD(Long, jlong, Long)
2182 JNI_CALL_STATIC_METHOD(Float, jfloat, Float)
2183 JNI_CALL_STATIC_METHOD(Double, jdouble, Double)
2186 #define JNI_CALL_STATIC_METHOD_V(name, type, intern) \
2187 type _Jv_JNI_CallStatic##name##MethodV(JNIEnv *env, jclass clazz, \
2188 jmethodID methodID, va_list args) \
2193 m = (methodinfo *) methodID; \
2195 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, args); \
2200 JNI_CALL_STATIC_METHOD_V(Boolean, jboolean, Int)
2201 JNI_CALL_STATIC_METHOD_V(Byte, jbyte, Int)
2202 JNI_CALL_STATIC_METHOD_V(Char, jchar, Int)
2203 JNI_CALL_STATIC_METHOD_V(Short, jshort, Int)
2204 JNI_CALL_STATIC_METHOD_V(Int, jint, Int)
2205 JNI_CALL_STATIC_METHOD_V(Long, jlong, Long)
2206 JNI_CALL_STATIC_METHOD_V(Float, jfloat, Float)
2207 JNI_CALL_STATIC_METHOD_V(Double, jdouble, Double)
2210 #define JNI_CALL_STATIC_METHOD_A(name, type, intern) \
2211 type _Jv_JNI_CallStatic##name##MethodA(JNIEnv *env, jclass clazz, \
2212 jmethodID methodID, const jvalue *args) \
2217 m = (methodinfo *) methodID; \
2219 res = _Jv_jni_Call##intern##MethodA(NULL, NULL, m, args); \
2224 JNI_CALL_STATIC_METHOD_A(Boolean, jboolean, Int)
2225 JNI_CALL_STATIC_METHOD_A(Byte, jbyte, Int)
2226 JNI_CALL_STATIC_METHOD_A(Char, jchar, Int)
2227 JNI_CALL_STATIC_METHOD_A(Short, jshort, Int)
2228 JNI_CALL_STATIC_METHOD_A(Int, jint, Int)
2229 JNI_CALL_STATIC_METHOD_A(Long, jlong, Long)
2230 JNI_CALL_STATIC_METHOD_A(Float, jfloat, Float)
2231 JNI_CALL_STATIC_METHOD_A(Double, jdouble, Double)
2234 jobject _Jv_JNI_CallStaticObjectMethod(JNIEnv *env, jclass clazz,
2235 jmethodID methodID, ...)
2241 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2243 m = (methodinfo *) methodID;
2245 va_start(ap, methodID);
2246 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, ap);
2249 return jni_NewLocalRef(env, (jobject) o);
2253 jobject _Jv_JNI_CallStaticObjectMethodV(JNIEnv *env, jclass clazz,
2254 jmethodID methodID, va_list args)
2259 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2261 m = (methodinfo *) methodID;
2263 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, args);
2265 return jni_NewLocalRef(env, (jobject) o);
2269 jobject _Jv_JNI_CallStaticObjectMethodA(JNIEnv *env, jclass clazz,
2270 jmethodID methodID, const jvalue *args)
2275 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2277 m = (methodinfo *) methodID;
2279 o = _Jv_jni_CallObjectMethodA(NULL, NULL, m, args);
2281 return jni_NewLocalRef(env, (jobject) o);
2285 void _Jv_JNI_CallStaticVoidMethod(JNIEnv *env, jclass clazz,
2286 jmethodID methodID, ...)
2291 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2293 m = (methodinfo *) methodID;
2295 va_start(ap, methodID);
2296 _Jv_jni_CallVoidMethod(NULL, NULL, m, ap);
2301 void _Jv_JNI_CallStaticVoidMethodV(JNIEnv *env, jclass clazz,
2302 jmethodID methodID, va_list args)
2306 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2308 m = (methodinfo *) methodID;
2310 _Jv_jni_CallVoidMethod(NULL, NULL, m, args);
2314 void _Jv_JNI_CallStaticVoidMethodA(JNIEnv *env, jclass clazz,
2315 jmethodID methodID, const jvalue * args)
2319 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2321 m = (methodinfo *) methodID;
2323 _Jv_jni_CallVoidMethodA(NULL, NULL, m, args);
2327 /* Accessing Static Fields ****************************************************/
2329 /* GetStaticFieldID ************************************************************
2331 Returns the field ID for a static field of a class. The field is
2332 specified by its name and signature. The GetStatic<type>Field and
2333 SetStatic<type>Field families of accessor functions use field IDs
2334 to retrieve static fields.
2336 *******************************************************************************/
2338 jfieldID _Jv_JNI_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name,
2346 STATISTICS(jniinvokation());
2348 c = LLNI_classinfo_unwrap(clazz);
2350 uname = utf_new_char((char *) name);
2351 usig = utf_new_char((char *) sig);
2353 f = class_findfield(c, uname, usig);
2356 exceptions_throw_nosuchfielderror(c, uname);
2358 return (jfieldID) f;
2362 /* GetStatic<type>Field ********************************************************
2364 This family of accessor routines returns the value of a static
2367 *******************************************************************************/
2369 #define JNI_GET_STATIC_FIELD(name, type, field) \
2370 type _Jv_JNI_GetStatic##name##Field(JNIEnv *env, jclass clazz, \
2376 STATISTICS(jniinvokation()); \
2378 c = LLNI_classinfo_unwrap(clazz); \
2379 f = (fieldinfo *) fieldID; \
2381 if (!(c->state & CLASS_INITIALIZED)) \
2382 if (!initialize_class(c)) \
2385 return f->value->field; \
2388 JNI_GET_STATIC_FIELD(Boolean, jboolean, i)
2389 JNI_GET_STATIC_FIELD(Byte, jbyte, i)
2390 JNI_GET_STATIC_FIELD(Char, jchar, i)
2391 JNI_GET_STATIC_FIELD(Short, jshort, i)
2392 JNI_GET_STATIC_FIELD(Int, jint, i)
2393 JNI_GET_STATIC_FIELD(Long, jlong, l)
2394 JNI_GET_STATIC_FIELD(Float, jfloat, f)
2395 JNI_GET_STATIC_FIELD(Double, jdouble, d)
2398 jobject _Jv_JNI_GetStaticObjectField(JNIEnv *env, jclass clazz,
2405 STATISTICS(jniinvokation());
2407 c = LLNI_classinfo_unwrap(clazz);
2408 f = (fieldinfo *) fieldID;
2410 if (!(c->state & CLASS_INITIALIZED))
2411 if (!initialize_class(c))
2414 h = (java_handle_t*) LLNI_WRAP(f->value->a);
2416 return jni_NewLocalRef(env, (jobject) h);
2420 /* SetStatic<type>Field *******************************************************
2422 This family of accessor routines sets the value of a static field
2425 *******************************************************************************/
2427 #define JNI_SET_STATIC_FIELD(name, type, field) \
2428 void _Jv_JNI_SetStatic##name##Field(JNIEnv *env, jclass clazz, \
2435 STATISTICS(jniinvokation()); \
2437 c = LLNI_classinfo_unwrap(clazz); \
2438 f = (fieldinfo *) fieldID; \
2440 if (!(c->state & CLASS_INITIALIZED)) \
2441 if (!initialize_class(c)) \
2444 f->value->field = value; \
2447 JNI_SET_STATIC_FIELD(Boolean, jboolean, i)
2448 JNI_SET_STATIC_FIELD(Byte, jbyte, i)
2449 JNI_SET_STATIC_FIELD(Char, jchar, i)
2450 JNI_SET_STATIC_FIELD(Short, jshort, i)
2451 JNI_SET_STATIC_FIELD(Int, jint, i)
2452 JNI_SET_STATIC_FIELD(Long, jlong, l)
2453 JNI_SET_STATIC_FIELD(Float, jfloat, f)
2454 JNI_SET_STATIC_FIELD(Double, jdouble, d)
2457 void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID,
2463 STATISTICS(jniinvokation());
2465 c = LLNI_classinfo_unwrap(clazz);
2466 f = (fieldinfo *) fieldID;
2468 if (!(c->state & CLASS_INITIALIZED))
2469 if (!initialize_class(c))
2472 f->value->a = LLNI_UNWRAP((java_handle_t *) value);
2476 /* String Operations **********************************************************/
2478 /* NewString *******************************************************************
2480 Create new java.lang.String object from an array of Unicode
2483 *******************************************************************************/
2485 jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len)
2487 TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len));
2489 java_handle_chararray_t* a = builtin_newarray_char(len);
2495 for (jsize i = 0; i < len; i++)
2496 LLNI_array_direct(a, i) = buf[i];
2498 java_handle_t* h = builtin_new(class_java_lang_String);
2503 java_lang_String s(h, a, len, 0);
2505 return (jstring) jni_NewLocalRef(env, (jobject) s.get_handle());
2509 static jchar emptyStringJ[]={0,0};
2511 /* GetStringLength *************************************************************
2513 Returns the length (the count of Unicode characters) of a Java
2516 *******************************************************************************/
2518 jsize jni_GetStringLength(JNIEnv *env, jstring str)
2520 TRACEJNICALLSENTER(("jni_GetStringLength(env=%p, str=%p)", env, str));
2522 java_lang_String s(str);
2523 jsize count = s.get_count();
2525 TRACEJNICALLSEXIT(("->%d)", count));
2531 /* GetStringChars **************************************************************
2533 Returns a pointer to the array of Unicode characters of the
2534 string. This pointer is valid until ReleaseStringChars() is called.
2536 *******************************************************************************/
2538 const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy)
2543 TRACEJNICALLS(("jni_GetStringChars(env=%p, str=%p, isCopy=%p)", env, str, isCopy));
2546 // FIXME This is really ugly.
2547 return emptyStringJ;
2549 java_lang_String s(str);
2551 java_handle_chararray_t* ca = s.get_value();
2552 int32_t count = s.get_count();
2553 int32_t offset = s.get_offset();
2558 /* allocate memory */
2560 stringbuffer = MNEW(u2, count + 1);
2564 for (i = 0; i < count; i++)
2565 stringbuffer[i] = LLNI_array_direct(ca, offset + i);
2567 /* terminate string */
2569 stringbuffer[i] = '\0';
2574 return (jchar*) stringbuffer;
2578 /* ReleaseStringChars **********************************************************
2580 Informs the VM that the native code no longer needs access to
2581 chars. The chars argument is a pointer obtained from string using
2584 *******************************************************************************/
2586 void _Jv_JNI_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)
2588 TRACEJNICALLS(("jni_ReleaseStringChars(env=%p, str=%p, chars=%p)", env, str, chars));
2591 if (chars == emptyStringJ)
2594 java_lang_String s(str);
2595 int32_t count = s.get_count();
2597 MFREE(((jchar*) chars), jchar, count + 1);
2601 /* NewStringUTF ****************************************************************
2603 Constructs a new java.lang.String object from an array of UTF-8
2606 *******************************************************************************/
2608 jstring jni_NewStringUTF(JNIEnv *env, const char *bytes)
2610 TRACEJNICALLS(("jni_NewStringUTF(env=%p, bytes=%s)", env, bytes));
2612 java_handle_t *h = javastring_safe_new_from_utf8(bytes);
2614 return (jstring) jni_NewLocalRef(env, (jobject) h);
2618 /****************** returns the utf8 length in bytes of a string *******************/
2620 jsize jni_GetStringUTFLength(JNIEnv *env, jstring string)
2622 TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string));
2624 java_lang_String s(string);
2625 java_handle_chararray_t* ca = s.get_value();
2626 int32_t count = s.get_count();
2628 // FIXME GC critical section!
2629 int32_t length = u2_utflength(ca->data, count);
2635 /* GetStringUTFChars ***********************************************************
2637 Returns a pointer to an array of UTF-8 characters of the
2638 string. This array is valid until it is released by
2639 ReleaseStringUTFChars().
2641 *******************************************************************************/
2643 const char *_Jv_JNI_GetStringUTFChars(JNIEnv *env, jstring string,
2648 STATISTICS(jniinvokation());
2656 u = javastring_toutf((java_handle_t *) string, false);
2665 /* ReleaseStringUTFChars *******************************************************
2667 Informs the VM that the native code no longer needs access to
2668 utf. The utf argument is a pointer derived from string using
2669 GetStringUTFChars().
2671 *******************************************************************************/
2673 void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf)
2675 STATISTICS(jniinvokation());
2677 /* XXX we don't release utf chars right now, perhaps that should be done
2678 later. Since there is always one reference the garbage collector will
2683 /* Array Operations ***********************************************************/
2685 /* GetArrayLength **************************************************************
2687 Returns the number of elements in the array.
2689 *******************************************************************************/
2691 jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array)
2696 TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array));
2698 a = (java_handle_t *) array;
2700 size = LLNI_array_size(a);
2706 /* NewObjectArray **************************************************************
2708 Constructs a new array holding objects in class elementClass. All
2709 elements are initially set to initialElement.
2711 *******************************************************************************/
2713 jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length,
2714 jclass elementClass, jobject initialElement)
2718 java_handle_objectarray_t *oa;
2721 STATISTICS(jniinvokation());
2723 c = LLNI_classinfo_unwrap(elementClass);
2724 o = (java_handle_t *) initialElement;
2727 exceptions_throw_negativearraysizeexception();
2731 oa = builtin_anewarray(length, c);
2736 /* set all elements to initialElement */
2738 for (i = 0; i < length; i++)
2739 array_objectarray_element_set(oa, i, o);
2741 return (jobjectArray) jni_NewLocalRef(env, (jobject) oa);
2745 jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array,
2748 java_handle_objectarray_t *oa;
2751 STATISTICS(jniinvokation());
2753 oa = (java_handle_objectarray_t *) array;
2755 if (index >= LLNI_array_size(oa)) {
2756 exceptions_throw_arrayindexoutofboundsexception();
2760 o = array_objectarray_element_get(oa, index);
2762 return jni_NewLocalRef(env, (jobject) o);
2766 void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array,
2767 jsize index, jobject val)
2769 java_handle_objectarray_t *oa;
2772 STATISTICS(jniinvokation());
2774 oa = (java_handle_objectarray_t *) array;
2775 o = (java_handle_t *) val;
2777 if (index >= LLNI_array_size(oa)) {
2778 exceptions_throw_arrayindexoutofboundsexception();
2782 /* check if the class of value is a subclass of the element class
2785 if (!builtin_canstore(oa, o))
2788 array_objectarray_element_set(oa, index, o);
2792 #define JNI_NEW_ARRAY(name, type, intern) \
2793 type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \
2795 java_handle_##intern##array_t *a; \
2797 STATISTICS(jniinvokation()); \
2800 exceptions_throw_negativearraysizeexception(); \
2804 a = builtin_newarray_##intern(len); \
2806 return (type) jni_NewLocalRef(env, (jobject) a); \
2809 JNI_NEW_ARRAY(Boolean, jbooleanArray, boolean)
2810 JNI_NEW_ARRAY(Byte, jbyteArray, byte)
2811 JNI_NEW_ARRAY(Char, jcharArray, char)
2812 JNI_NEW_ARRAY(Short, jshortArray, short)
2813 JNI_NEW_ARRAY(Int, jintArray, int)
2814 JNI_NEW_ARRAY(Long, jlongArray, long)
2815 JNI_NEW_ARRAY(Float, jfloatArray, float)
2816 JNI_NEW_ARRAY(Double, jdoubleArray, double)
2819 /* Get<PrimitiveType>ArrayElements *********************************************
2821 A family of functions that returns the body of the primitive array.
2823 *******************************************************************************/
2825 #define JNI_GET_ARRAY_ELEMENTS(name, type, intern) \
2826 type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \
2829 java_handle_##intern##array_t *a; \
2831 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \
2833 a = (java_handle_##intern##array_t *) array; \
2836 *isCopy = JNI_FALSE; \
2838 return (type *) LLNI_array_data(a); \
2841 JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean)
2842 JNI_GET_ARRAY_ELEMENTS(Byte, jbyte, byte)
2843 JNI_GET_ARRAY_ELEMENTS(Char, jchar, char)
2844 JNI_GET_ARRAY_ELEMENTS(Short, jshort, short)
2845 JNI_GET_ARRAY_ELEMENTS(Int, jint, int)
2846 JNI_GET_ARRAY_ELEMENTS(Long, jlong, long)
2847 JNI_GET_ARRAY_ELEMENTS(Float, jfloat, float)
2848 JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double)
2851 /* Release<PrimitiveType>ArrayElements *****************************************
2853 A family of functions that informs the VM that the native code no
2854 longer needs access to elems. The elems argument is a pointer
2855 derived from array using the corresponding
2856 Get<PrimitiveType>ArrayElements() function. If necessary, this
2857 function copies back all changes made to elems to the original
2860 *******************************************************************************/
2862 #define JNI_RELEASE_ARRAY_ELEMENTS(name, type, intern, intern2) \
2863 void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \
2864 type *elems, jint mode) \
2866 java_handle_##intern##array_t *a; \
2868 STATISTICS(jniinvokation()); \
2870 a = (java_handle_##intern##array_t *) array; \
2872 if (elems != (type *) LLNI_array_data(a)) { \
2875 MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \
2878 MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \
2879 /* XXX TWISTI how should it be freed? */ \
2882 /* XXX TWISTI how should it be freed? */ \
2888 JNI_RELEASE_ARRAY_ELEMENTS(Boolean, jboolean, boolean, u1)
2889 JNI_RELEASE_ARRAY_ELEMENTS(Byte, jbyte, byte, s1)
2890 JNI_RELEASE_ARRAY_ELEMENTS(Char, jchar, char, u2)
2891 JNI_RELEASE_ARRAY_ELEMENTS(Short, jshort, short, s2)
2892 JNI_RELEASE_ARRAY_ELEMENTS(Int, jint, int, s4)
2893 JNI_RELEASE_ARRAY_ELEMENTS(Long, jlong, long, s8)
2894 JNI_RELEASE_ARRAY_ELEMENTS(Float, jfloat, float, float)
2895 JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double)
2898 /* Get<PrimitiveType>ArrayRegion **********************************************
2900 A family of functions that copies a region of a primitive array
2903 *******************************************************************************/
2905 #define JNI_GET_ARRAY_REGION(name, type, intern, intern2) \
2906 void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \
2907 jsize start, jsize len, type *buf) \
2909 java_handle_##intern##array_t *a; \
2911 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \
2913 a = (java_handle_##intern##array_t *) array; \
2915 if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \
2916 exceptions_throw_arrayindexoutofboundsexception(); \
2918 MCOPY(buf, &LLNI_array_direct(a, start), intern2, len); \
2921 JNI_GET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2922 JNI_GET_ARRAY_REGION(Byte, jbyte, byte, s1)
2923 JNI_GET_ARRAY_REGION(Char, jchar, char, u2)
2924 JNI_GET_ARRAY_REGION(Short, jshort, short, s2)
2925 JNI_GET_ARRAY_REGION(Int, jint, int, s4)
2926 JNI_GET_ARRAY_REGION(Long, jlong, long, s8)
2927 JNI_GET_ARRAY_REGION(Float, jfloat, float, float)
2928 JNI_GET_ARRAY_REGION(Double, jdouble, double, double)
2931 /* Set<PrimitiveType>ArrayRegion **********************************************
2933 A family of functions that copies back a region of a primitive
2934 array from a buffer.
2936 *******************************************************************************/
2938 #define JNI_SET_ARRAY_REGION(name, type, intern, intern2) \
2939 void _Jv_JNI_Set##name##ArrayRegion(JNIEnv *env, type##Array array, \
2940 jsize start, jsize len, const type *buf) \
2942 java_handle_##intern##array_t *a; \
2944 STATISTICS(jniinvokation()); \
2946 a = (java_handle_##intern##array_t *) array; \
2948 if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \
2949 exceptions_throw_arrayindexoutofboundsexception(); \
2951 MCOPY(&LLNI_array_direct(a, start), buf, intern2, len); \
2954 JNI_SET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2955 JNI_SET_ARRAY_REGION(Byte, jbyte, byte, s1)
2956 JNI_SET_ARRAY_REGION(Char, jchar, char, u2)
2957 JNI_SET_ARRAY_REGION(Short, jshort, short, s2)
2958 JNI_SET_ARRAY_REGION(Int, jint, int, s4)
2959 JNI_SET_ARRAY_REGION(Long, jlong, long, s8)
2960 JNI_SET_ARRAY_REGION(Float, jfloat, float, float)
2961 JNI_SET_ARRAY_REGION(Double, jdouble, double, double)
2964 /* Registering Native Methods *************************************************/
2966 /* RegisterNatives *************************************************************
2968 Registers native methods with the class specified by the clazz
2969 argument. The methods parameter specifies an array of
2970 JNINativeMethod structures that contain the names, signatures, and
2971 function pointers of the native methods. The nMethods parameter
2972 specifies the number of native methods in the array.
2974 *******************************************************************************/
2976 jint _Jv_JNI_RegisterNatives(JNIEnv *env, jclass clazz,
2977 const JNINativeMethod *methods, jint nMethods)
2981 STATISTICS(jniinvokation());
2983 c = LLNI_classinfo_unwrap(clazz);
2985 /* XXX: if implemented this needs a call to jvmti_NativeMethodBind
2986 if (jvmti) jvmti_NativeMethodBind(method, address, new_address_ptr);
2989 native_method_register(c->name, methods, nMethods);
2995 /* UnregisterNatives ***********************************************************
2997 Unregisters native methods of a class. The class goes back to the
2998 state before it was linked or registered with its native method
3001 This function should not be used in normal native code. Instead, it
3002 provides special programs a way to reload and relink native
3005 *******************************************************************************/
3007 jint _Jv_JNI_UnregisterNatives(JNIEnv *env, jclass clazz)
3009 STATISTICS(jniinvokation());
3011 /* XXX TWISTI hmm, maybe we should not support that (like kaffe) */
3013 log_text("JNI-Call: UnregisterNatives: IMPLEMENT ME!!!");
3019 /* Monitor Operations *********************************************************/
3021 /* MonitorEnter ****************************************************************
3023 Enters the monitor associated with the underlying Java object
3026 *******************************************************************************/
3028 jint _Jv_JNI_MonitorEnter(JNIEnv *env, jobject obj)
3030 STATISTICS(jniinvokation());
3033 exceptions_throw_nullpointerexception();
3037 LOCK_MONITOR_ENTER(obj);
3043 /* MonitorExit *****************************************************************
3045 The current thread must be the owner of the monitor associated with
3046 the underlying Java object referred to by obj. The thread
3047 decrements the counter indicating the number of times it has
3048 entered this monitor. If the value of the counter becomes zero, the
3049 current thread releases the monitor.
3051 *******************************************************************************/
3053 jint _Jv_JNI_MonitorExit(JNIEnv *env, jobject obj)
3055 STATISTICS(jniinvokation());
3058 exceptions_throw_nullpointerexception();
3062 LOCK_MONITOR_EXIT(obj);
3068 /* JavaVM Interface ***********************************************************/
3070 /* GetJavaVM *******************************************************************
3072 Returns the Java VM interface (used in the Invocation API)
3073 associated with the current thread. The result is placed at the
3074 location pointed to by the second argument, vm.
3076 *******************************************************************************/
3078 jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm)
3080 STATISTICS(jniinvokation());
3082 *javavm = VM::get_current()->get_javavm();
3088 /* GetStringRegion *************************************************************
3090 Copies len number of Unicode characters beginning at offset start
3091 to the given buffer buf.
3093 Throws StringIndexOutOfBoundsException on index overflow.
3095 *******************************************************************************/
3097 void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf)
3099 java_lang_String s(str);
3100 java_handle_chararray_t* ca = s.get_value();
3101 int32_t count = s.get_count();
3103 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3104 exceptions_throw_stringindexoutofboundsexception();
3108 MCOPY(buf, &LLNI_array_direct(ca, start), u2, len);
3112 /* GetStringUTFRegion **********************************************************
3114 Translates len number of Unicode characters beginning at offset
3115 start into UTF-8 format and place the result in the given buffer
3118 Throws StringIndexOutOfBoundsException on index overflow.
3120 *******************************************************************************/
3122 void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char *buf)
3124 TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf));
3126 java_lang_String s(str);
3127 java_handle_chararray_t* ca = s.get_value();
3128 int32_t count = s.get_count();
3129 int32_t offset = s.get_offset();
3131 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3132 exceptions_throw_stringindexoutofboundsexception();
3138 for (i = 0; i < len; i++)
3139 buf[i] = LLNI_array_direct(ca, offset + start + i);
3145 /* GetPrimitiveArrayCritical ***************************************************
3147 Obtain a direct pointer to array elements.
3149 ATTENTION: Critical section keeps open when this function returns!
3150 See ReleasePrimitiveArrayCritical.
3152 *******************************************************************************/
3154 void* jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)
3158 arraydescriptor* ad;
3161 TRACEJNICALLS(("jni_GetPrimitiveArrayCritical(env=%p, array=%p, isCopy=%d)", env, array, isCopy));
3163 if (isCopy != NULL) {
3164 *isCopy = JNI_FALSE;
3167 LLNI_CRITICAL_START;
3169 h = (java_handle_t*) array;
3170 a = (java_array_t*) LLNI_UNWRAP(h);
3171 ad = a->objheader.vftbl->arraydesc;
3177 data = (void*) (((intptr_t) a) + ad->dataoffset);
3183 /* ReleasePrimitiveArrayCritical ***********************************************
3185 No specific documentation.
3187 ATTENTION: This function closes the critical section opened in
3188 GetPrimitiveArrayCritical!
3190 *******************************************************************************/
3192 void jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)
3194 TRACEJNICALLS(("jni_ReleasePrimitiveArrayCritical(env=%p, array=%p, carray=%p, mode=%d)", env, array, carray, mode));
3200 /* GetStringCritical ***********************************************************
3202 The semantics of these two functions are similar to the existing
3203 Get/ReleaseStringChars functions.
3205 *******************************************************************************/
3207 const jchar *_Jv_JNI_GetStringCritical(JNIEnv *env, jstring string,
3210 STATISTICS(jniinvokation());
3212 return jni_GetStringChars(env, string, isCopy);
3216 void _Jv_JNI_ReleaseStringCritical(JNIEnv *env, jstring string,
3217 const jchar *cstring)
3219 STATISTICS(jniinvokation());
3221 _Jv_JNI_ReleaseStringChars(env, string, cstring);
3225 jweak _Jv_JNI_NewWeakGlobalRef(JNIEnv* env, jobject obj)
3227 TRACEJNICALLS(("_Jv_JNI_NewWeakGlobalRef(env=%p, obj=%p): IMPLEMENT ME!", env, obj));
3233 void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
3235 TRACEJNICALLS(("_Jv_JNI_DeleteWeakGlobalRef(env=%p, ref=%p): IMPLEMENT ME", env, ref));
3239 /* NewGlobalRef ****************************************************************
3241 Creates a new global reference to the object referred to by the obj
3244 *******************************************************************************/
3246 jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
3248 hashtable_global_ref_entry *gre;
3249 u4 key; /* hashkey */
3250 u4 slot; /* slot in hashtable */
3253 TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
3255 o = (java_handle_t *) obj;
3257 hashtable_global_ref->mutex->lock();
3259 LLNI_CRITICAL_START;
3261 /* normally addresses are aligned to 4, 8 or 16 bytes */
3263 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3264 slot = key & (hashtable_global_ref->size - 1);
3265 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3267 /* search external hash chain for the entry */
3270 if (gre->o == LLNI_DIRECT(o)) {
3271 /* global object found, increment the reference */
3278 gre = gre->hashlink; /* next element in external chain */
3283 /* global ref not found, create a new one */
3286 gre = (hashtable_global_ref_entry*) heap_alloc_uncollectable(sizeof(hashtable_global_ref_entry));
3288 #if defined(ENABLE_GC_CACAO)
3289 /* register global ref with the GC */
3291 gc_reference_register(&(gre->o), GC_REFTYPE_JNI_GLOBALREF);
3294 LLNI_CRITICAL_START;
3296 gre->o = LLNI_DIRECT(o);
3301 /* insert entry into hashtable */
3303 gre->hashlink = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3305 hashtable_global_ref->ptr[slot] = gre;
3307 /* update number of hashtable-entries */
3309 hashtable_global_ref->entries++;
3312 hashtable_global_ref->mutex->unlock();
3314 #if defined(ENABLE_HANDLES)
3322 /* DeleteGlobalRef *************************************************************
3324 Deletes the global reference pointed to by globalRef.
3326 *******************************************************************************/
3328 void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
3330 hashtable_global_ref_entry *gre;
3331 hashtable_global_ref_entry *prevgre;
3332 u4 key; /* hashkey */
3333 u4 slot; /* slot in hashtable */
3336 TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
3338 o = (java_handle_t *) globalRef;
3340 hashtable_global_ref->mutex->lock();
3342 LLNI_CRITICAL_START;
3344 /* normally addresses are aligned to 4, 8 or 16 bytes */
3346 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3347 slot = key & (hashtable_global_ref->size - 1);
3348 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3350 /* initialize prevgre */
3354 /* search external hash chain for the entry */
3357 if (gre->o == LLNI_DIRECT(o)) {
3358 /* global object found, decrement the reference count */
3362 /* if reference count is 0, remove the entry */
3364 if (gre->refs == 0) {
3365 /* special handling if it's the first in the chain */
3367 if (prevgre == NULL)
3368 hashtable_global_ref->ptr[slot] = gre->hashlink;
3370 prevgre->hashlink = gre->hashlink;
3372 #if defined(ENABLE_GC_CACAO)
3373 /* unregister global ref with the GC */
3375 gc_reference_unregister(&(gre->o));
3383 hashtable_global_ref->mutex->unlock();
3388 prevgre = gre; /* save current pointer for removal */
3389 gre = gre->hashlink; /* next element in external chain */
3392 log_println("jni_DeleteGlobalRef: Global reference not found.");
3396 hashtable_global_ref->mutex->unlock();
3400 /* ExceptionCheck **************************************************************
3402 Returns JNI_TRUE when there is a pending exception; otherwise,
3405 *******************************************************************************/
3407 jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
3411 STATISTICS(jniinvokation());
3413 o = exceptions_get_exception();
3415 return (o != NULL) ? JNI_TRUE : JNI_FALSE;
3419 /* New JNI 1.4 functions ******************************************************/
3421 /* NewDirectByteBuffer *********************************************************
3423 Allocates and returns a direct java.nio.ByteBuffer referring to the
3424 block of memory starting at the memory address address and
3425 extending capacity bytes.
3427 *******************************************************************************/
3429 jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
3431 #if defined(ENABLE_JAVASE)
3432 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3433 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3435 // Allocate a gnu.classpath.Pointer{32,64} object.
3437 # if SIZEOF_VOID_P == 8
3438 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer64);
3440 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer32);
3446 gnu_classpath_Pointer p(h, address);
3448 // Create a java.nio.DirectByteBufferImpl$ReadWrite object.
3450 java_handle_t* nbuf =
3451 (java_handle_t*) jni_NewObject(env, (jclass) class_java_nio_DirectByteBufferImpl_ReadWrite,
3452 (jmethodID) dbbirw_init, NULL, p.get_handle(),
3453 (jint) capacity, (jint) capacity, (jint) 0);
3455 // Add a local reference and return the value.
3457 TRACEJNICALLSEXIT(("->%p", nbuf));
3459 return jni_NewLocalRef(env, (jobject) nbuf);
3461 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3467 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3469 /* Be paranoid about address sign-extension. */
3471 addr = (int64_t) ((uintptr_t) address);
3472 cap = (int32_t) capacity;
3474 o = jni_NewObject(env, (jclass) class_java_nio_DirectByteBuffer,
3475 (jmethodID) dbb_init, addr, cap);
3477 /* Add local reference and return the value. */
3479 TRACEJNICALLSEXIT(("->%p", o));
3481 return jni_NewLocalRef(env, o);
3484 # error unknown classpath configuration
3488 vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
3490 /* keep compiler happy */
3497 /* GetDirectBufferAddress ******************************************************
3499 Fetches and returns the starting address of the memory region
3500 referenced by the given direct java.nio.Buffer.
3502 *******************************************************************************/
3504 void* jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3506 #if defined(ENABLE_JAVASE)
3507 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3509 TRACEJNICALLSENTER(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3511 /* Prevent compiler warning. */
3513 java_handle_t* h = (java_handle_t *) buf;
3515 if ((h != NULL) && !builtin_instanceof(h, class_java_nio_Buffer))
3518 java_nio_DirectByteBufferImpl dbb(buf);
3519 java_handle_t* address = dbb.get_address();
3521 if (address == NULL) {
3522 TRACEJNICALLSEXIT(("->%p", NULL));
3526 gnu_classpath_Pointer p(address);
3527 void* data = p.get_data();
3529 TRACEJNICALLSEXIT(("->%p", data));
3533 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3535 TRACEJNICALLS(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3537 java_nio_Buffer jnb(buf);
3539 if (jnb.is_non_null() && !builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3542 void* address = jnb.get_address();
3547 # error unknown classpath configuration
3552 vm_abort("jni_GetDirectBufferAddress: Not implemented in this configuration.");
3554 // Keep compiler happy.
3561 /* GetDirectBufferCapacity *****************************************************
3563 Fetches and returns the capacity in bytes of the memory region
3564 referenced by the given direct java.nio.Buffer.
3566 *******************************************************************************/
3568 jlong jni_GetDirectBufferCapacity(JNIEnv* env, jobject buf)
3570 #if defined(ENABLE_JAVASE) && defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3571 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3573 java_handle_t* h = (java_handle_t *) buf;
3575 if (!builtin_instanceof(h, class_java_nio_DirectByteBufferImpl))
3578 java_nio_Buffer b(h);
3579 jlong capacity = b.get_cap();
3583 vm_abort("jni_GetDirectBufferCapacity: not implemented in this configuration");
3585 // Keep compiler happy.
3592 /* GetObjectRefType ************************************************************
3594 Returns the type of the object referred to by the obj argument. The
3595 argument obj can either be a local, global or weak global
3598 *******************************************************************************/
3600 jobjectRefType jni_GetObjectRefType(JNIEnv *env, jobject obj)
3602 log_println("jni_GetObjectRefType: IMPLEMENT ME!");
3604 return (jobjectRefType) NULL;
3608 /* DestroyJavaVM ***************************************************************
3610 Unloads a Java VM and reclaims its resources. Only the main thread
3611 can unload the VM. The system waits until the main thread is only
3612 remaining user thread before it destroys the VM.
3614 *******************************************************************************/
3616 jint _Jv_JNI_DestroyJavaVM(JavaVM *javavm)
3620 TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(javavm=%p)", javavm));
3622 if (VM::get_current()->is_created() == false)
3625 status = vm_destroy(javavm);
3631 /* AttachCurrentThread *********************************************************
3633 Attaches the current thread to a Java VM. Returns a JNI interface
3634 pointer in the JNIEnv argument.
3636 Trying to attach a thread that is already attached is a no-op.
3638 A native thread cannot be attached simultaneously to two Java VMs.
3640 When a thread is attached to the VM, the context class loader is
3641 the bootstrap loader.
3643 *******************************************************************************/
3645 static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
3647 #if defined(ENABLE_THREADS)
3648 JavaVMAttachArgs *vm_aargs;
3651 /* If the current thread has already been attached, this operation
3654 result = thread_current_is_attached();
3656 if (result == true) {
3657 *p_env = VM::get_current()->get_jnienv();
3661 vm_aargs = (JavaVMAttachArgs *) thr_args;
3663 if (vm_aargs != NULL) {
3664 if ((vm_aargs->version != JNI_VERSION_1_2) &&
3665 (vm_aargs->version != JNI_VERSION_1_4))
3666 return JNI_EVERSION;
3669 if (!thread_attach_current_external_thread(vm_aargs, false))
3672 if (!localref_table_init())
3676 *p_env = VM::get_current()->get_jnienv();
3682 jint jni_AttachCurrentThread(JavaVM *javavm, void **p_env, void *thr_args)
3686 TRACEJNICALLS(("jni_AttachCurrentThread(javavm=%p, p_env=%p, thr_args=%p)", javavm, p_env, thr_args));
3688 if (VM::get_current()->is_created() == false)
3691 result = jni_attach_current_thread(p_env, thr_args, false);
3697 /* DetachCurrentThread *********************************************************
3699 Detaches the current thread from a Java VM. All Java monitors held
3700 by this thread are released. All Java threads waiting for this
3701 thread to die are notified.
3703 In JDK 1.1, the main thread cannot be detached from the VM. It must
3704 call DestroyJavaVM to unload the entire VM.
3706 In the JDK, the main thread can be detached from the VM.
3708 The main thread, which is the thread that created the Java VM,
3709 cannot be detached from the VM. Instead, the main thread must call
3710 JNI_DestroyJavaVM() to unload the entire VM.
3712 *******************************************************************************/
3714 jint jni_DetachCurrentThread(JavaVM *vm)
3716 #if defined(ENABLE_THREADS)
3719 TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
3721 /* If the current thread has already been detached, this operation
3724 result = thread_current_is_attached();
3726 if (result == false)
3729 /* We need to pop all frames before we can destroy the table. */
3731 localref_frame_pop_all();
3733 if (!localref_table_destroy())
3736 if (!thread_detach_current_external_thread())
3744 /* GetEnv **********************************************************************
3746 If the current thread is not attached to the VM, sets *env to NULL,
3747 and returns JNI_EDETACHED. If the specified version is not
3748 supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise,
3749 sets *env to the appropriate interface, and returns JNI_OK.
3751 *******************************************************************************/
3753 jint jni_GetEnv(JavaVM *javavm, void **env, jint version)
3755 TRACEJNICALLS(("jni_GetEnv(javavm=%p, env=%p, version=%d)", javavm, env, version));
3757 if (VM::get_current()->is_created() == false) {
3759 return JNI_EDETACHED;
3762 #if defined(ENABLE_THREADS)
3763 if (thread_get_current() == NULL) {
3766 return JNI_EDETACHED;
3770 /* Check the JNI version. */
3772 if (jni_version_check(version) == true) {
3773 *env = VM::get_current()->get_jnienv();
3777 #if defined(ENABLE_JVMTI)
3778 if ((version & JVMTI_VERSION_MASK_INTERFACE_TYPE)
3779 == JVMTI_VERSION_INTERFACE_JVMTI) {
3781 *env = (void *) jvmti_new_environment();
3790 return JNI_EVERSION;
3794 /* AttachCurrentThreadAsDaemon *************************************************
3796 Same semantics as AttachCurrentThread, but the newly-created
3797 java.lang.Thread instance is a daemon.
3799 If the thread has already been attached via either
3800 AttachCurrentThread or AttachCurrentThreadAsDaemon, this routine
3801 simply sets the value pointed to by penv to the JNIEnv of the
3802 current thread. In this case neither AttachCurrentThread nor this
3803 routine have any effect on the daemon status of the thread.
3805 *******************************************************************************/
3807 jint jni_AttachCurrentThreadAsDaemon(JavaVM *javavm, void **penv, void *args)
3811 TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(javavm=%p, penv=%p, args=%p)", javavm, penv, args));
3813 if (VM::get_current()->is_created() == false)
3816 result = jni_attach_current_thread(penv, args, true);
3822 /* JNI invocation table *******************************************************/
3824 const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
3829 _Jv_JNI_DestroyJavaVM,
3830 jni_AttachCurrentThread,
3831 jni_DetachCurrentThread,
3833 jni_AttachCurrentThreadAsDaemon
3837 /* JNI function table *********************************************************/
3839 struct JNINativeInterface_ _Jv_JNINativeInterface = {
3848 jni_FromReflectedMethod,
3849 jni_FromReflectedField,
3850 jni_ToReflectedMethod,
3852 _Jv_JNI_IsAssignableFrom,
3853 _Jv_JNI_ToReflectedField,
3857 _Jv_JNI_ExceptionOccurred,
3858 jni_ExceptionDescribe,
3865 jni_DeleteGlobalRef,
3867 _Jv_JNI_IsSameObject,
3869 jni_EnsureLocalCapacity,
3871 _Jv_JNI_AllocObject,
3877 _Jv_JNI_IsInstanceOf,
3879 _Jv_JNI_GetMethodID,
3881 _Jv_JNI_CallObjectMethod,
3882 _Jv_JNI_CallObjectMethodV,
3883 _Jv_JNI_CallObjectMethodA,
3884 _Jv_JNI_CallBooleanMethod,
3885 _Jv_JNI_CallBooleanMethodV,
3886 _Jv_JNI_CallBooleanMethodA,
3887 _Jv_JNI_CallByteMethod,
3888 _Jv_JNI_CallByteMethodV,
3889 _Jv_JNI_CallByteMethodA,
3890 _Jv_JNI_CallCharMethod,
3891 _Jv_JNI_CallCharMethodV,
3892 _Jv_JNI_CallCharMethodA,
3893 _Jv_JNI_CallShortMethod,
3894 _Jv_JNI_CallShortMethodV,
3895 _Jv_JNI_CallShortMethodA,
3896 _Jv_JNI_CallIntMethod,
3897 _Jv_JNI_CallIntMethodV,
3898 _Jv_JNI_CallIntMethodA,
3899 _Jv_JNI_CallLongMethod,
3900 _Jv_JNI_CallLongMethodV,
3901 _Jv_JNI_CallLongMethodA,
3902 _Jv_JNI_CallFloatMethod,
3903 _Jv_JNI_CallFloatMethodV,
3904 _Jv_JNI_CallFloatMethodA,
3905 _Jv_JNI_CallDoubleMethod,
3906 _Jv_JNI_CallDoubleMethodV,
3907 _Jv_JNI_CallDoubleMethodA,
3908 _Jv_JNI_CallVoidMethod,
3909 _Jv_JNI_CallVoidMethodV,
3910 _Jv_JNI_CallVoidMethodA,
3912 _Jv_JNI_CallNonvirtualObjectMethod,
3913 _Jv_JNI_CallNonvirtualObjectMethodV,
3914 _Jv_JNI_CallNonvirtualObjectMethodA,
3915 _Jv_JNI_CallNonvirtualBooleanMethod,
3916 _Jv_JNI_CallNonvirtualBooleanMethodV,
3917 _Jv_JNI_CallNonvirtualBooleanMethodA,
3918 _Jv_JNI_CallNonvirtualByteMethod,
3919 _Jv_JNI_CallNonvirtualByteMethodV,
3920 _Jv_JNI_CallNonvirtualByteMethodA,
3921 _Jv_JNI_CallNonvirtualCharMethod,
3922 _Jv_JNI_CallNonvirtualCharMethodV,
3923 _Jv_JNI_CallNonvirtualCharMethodA,
3924 _Jv_JNI_CallNonvirtualShortMethod,
3925 _Jv_JNI_CallNonvirtualShortMethodV,
3926 _Jv_JNI_CallNonvirtualShortMethodA,
3927 _Jv_JNI_CallNonvirtualIntMethod,
3928 _Jv_JNI_CallNonvirtualIntMethodV,
3929 _Jv_JNI_CallNonvirtualIntMethodA,
3930 _Jv_JNI_CallNonvirtualLongMethod,
3931 _Jv_JNI_CallNonvirtualLongMethodV,
3932 _Jv_JNI_CallNonvirtualLongMethodA,
3933 _Jv_JNI_CallNonvirtualFloatMethod,
3934 _Jv_JNI_CallNonvirtualFloatMethodV,
3935 _Jv_JNI_CallNonvirtualFloatMethodA,
3936 _Jv_JNI_CallNonvirtualDoubleMethod,
3937 _Jv_JNI_CallNonvirtualDoubleMethodV,
3938 _Jv_JNI_CallNonvirtualDoubleMethodA,
3939 _Jv_JNI_CallNonvirtualVoidMethod,
3940 _Jv_JNI_CallNonvirtualVoidMethodV,
3941 _Jv_JNI_CallNonvirtualVoidMethodA,
3945 _Jv_JNI_GetObjectField,
3946 _Jv_JNI_GetBooleanField,
3947 _Jv_JNI_GetByteField,
3948 _Jv_JNI_GetCharField,
3949 _Jv_JNI_GetShortField,
3950 _Jv_JNI_GetIntField,
3951 _Jv_JNI_GetLongField,
3952 _Jv_JNI_GetFloatField,
3953 _Jv_JNI_GetDoubleField,
3954 _Jv_JNI_SetObjectField,
3955 _Jv_JNI_SetBooleanField,
3956 _Jv_JNI_SetByteField,
3957 _Jv_JNI_SetCharField,
3958 _Jv_JNI_SetShortField,
3959 _Jv_JNI_SetIntField,
3960 _Jv_JNI_SetLongField,
3961 _Jv_JNI_SetFloatField,
3962 _Jv_JNI_SetDoubleField,
3964 _Jv_JNI_GetStaticMethodID,
3966 _Jv_JNI_CallStaticObjectMethod,
3967 _Jv_JNI_CallStaticObjectMethodV,
3968 _Jv_JNI_CallStaticObjectMethodA,
3969 _Jv_JNI_CallStaticBooleanMethod,
3970 _Jv_JNI_CallStaticBooleanMethodV,
3971 _Jv_JNI_CallStaticBooleanMethodA,
3972 _Jv_JNI_CallStaticByteMethod,
3973 _Jv_JNI_CallStaticByteMethodV,
3974 _Jv_JNI_CallStaticByteMethodA,
3975 _Jv_JNI_CallStaticCharMethod,
3976 _Jv_JNI_CallStaticCharMethodV,
3977 _Jv_JNI_CallStaticCharMethodA,
3978 _Jv_JNI_CallStaticShortMethod,
3979 _Jv_JNI_CallStaticShortMethodV,
3980 _Jv_JNI_CallStaticShortMethodA,
3981 _Jv_JNI_CallStaticIntMethod,
3982 _Jv_JNI_CallStaticIntMethodV,
3983 _Jv_JNI_CallStaticIntMethodA,
3984 _Jv_JNI_CallStaticLongMethod,
3985 _Jv_JNI_CallStaticLongMethodV,
3986 _Jv_JNI_CallStaticLongMethodA,
3987 _Jv_JNI_CallStaticFloatMethod,
3988 _Jv_JNI_CallStaticFloatMethodV,
3989 _Jv_JNI_CallStaticFloatMethodA,
3990 _Jv_JNI_CallStaticDoubleMethod,
3991 _Jv_JNI_CallStaticDoubleMethodV,
3992 _Jv_JNI_CallStaticDoubleMethodA,
3993 _Jv_JNI_CallStaticVoidMethod,
3994 _Jv_JNI_CallStaticVoidMethodV,
3995 _Jv_JNI_CallStaticVoidMethodA,
3997 _Jv_JNI_GetStaticFieldID,
3999 _Jv_JNI_GetStaticObjectField,
4000 _Jv_JNI_GetStaticBooleanField,
4001 _Jv_JNI_GetStaticByteField,
4002 _Jv_JNI_GetStaticCharField,
4003 _Jv_JNI_GetStaticShortField,
4004 _Jv_JNI_GetStaticIntField,
4005 _Jv_JNI_GetStaticLongField,
4006 _Jv_JNI_GetStaticFloatField,
4007 _Jv_JNI_GetStaticDoubleField,
4008 _Jv_JNI_SetStaticObjectField,
4009 _Jv_JNI_SetStaticBooleanField,
4010 _Jv_JNI_SetStaticByteField,
4011 _Jv_JNI_SetStaticCharField,
4012 _Jv_JNI_SetStaticShortField,
4013 _Jv_JNI_SetStaticIntField,
4014 _Jv_JNI_SetStaticLongField,
4015 _Jv_JNI_SetStaticFloatField,
4016 _Jv_JNI_SetStaticDoubleField,
4019 jni_GetStringLength,
4021 _Jv_JNI_ReleaseStringChars,
4024 jni_GetStringUTFLength,
4025 _Jv_JNI_GetStringUTFChars,
4026 _Jv_JNI_ReleaseStringUTFChars,
4028 _Jv_JNI_GetArrayLength,
4030 _Jv_JNI_NewObjectArray,
4031 _Jv_JNI_GetObjectArrayElement,
4032 _Jv_JNI_SetObjectArrayElement,
4034 _Jv_JNI_NewBooleanArray,
4035 _Jv_JNI_NewByteArray,
4036 _Jv_JNI_NewCharArray,
4037 _Jv_JNI_NewShortArray,
4038 _Jv_JNI_NewIntArray,
4039 _Jv_JNI_NewLongArray,
4040 _Jv_JNI_NewFloatArray,
4041 _Jv_JNI_NewDoubleArray,
4043 _Jv_JNI_GetBooleanArrayElements,
4044 _Jv_JNI_GetByteArrayElements,
4045 _Jv_JNI_GetCharArrayElements,
4046 _Jv_JNI_GetShortArrayElements,
4047 _Jv_JNI_GetIntArrayElements,
4048 _Jv_JNI_GetLongArrayElements,
4049 _Jv_JNI_GetFloatArrayElements,
4050 _Jv_JNI_GetDoubleArrayElements,
4052 _Jv_JNI_ReleaseBooleanArrayElements,
4053 _Jv_JNI_ReleaseByteArrayElements,
4054 _Jv_JNI_ReleaseCharArrayElements,
4055 _Jv_JNI_ReleaseShortArrayElements,
4056 _Jv_JNI_ReleaseIntArrayElements,
4057 _Jv_JNI_ReleaseLongArrayElements,
4058 _Jv_JNI_ReleaseFloatArrayElements,
4059 _Jv_JNI_ReleaseDoubleArrayElements,
4061 _Jv_JNI_GetBooleanArrayRegion,
4062 _Jv_JNI_GetByteArrayRegion,
4063 _Jv_JNI_GetCharArrayRegion,
4064 _Jv_JNI_GetShortArrayRegion,
4065 _Jv_JNI_GetIntArrayRegion,
4066 _Jv_JNI_GetLongArrayRegion,
4067 _Jv_JNI_GetFloatArrayRegion,
4068 _Jv_JNI_GetDoubleArrayRegion,
4069 _Jv_JNI_SetBooleanArrayRegion,
4070 _Jv_JNI_SetByteArrayRegion,
4071 _Jv_JNI_SetCharArrayRegion,
4072 _Jv_JNI_SetShortArrayRegion,
4073 _Jv_JNI_SetIntArrayRegion,
4074 _Jv_JNI_SetLongArrayRegion,
4075 _Jv_JNI_SetFloatArrayRegion,
4076 _Jv_JNI_SetDoubleArrayRegion,
4078 _Jv_JNI_RegisterNatives,
4079 _Jv_JNI_UnregisterNatives,
4081 _Jv_JNI_MonitorEnter,
4082 _Jv_JNI_MonitorExit,
4086 /* New JNI 1.2 functions. */
4088 jni_GetStringRegion,
4089 jni_GetStringUTFRegion,
4091 jni_GetPrimitiveArrayCritical,
4092 jni_ReleasePrimitiveArrayCritical,
4094 _Jv_JNI_GetStringCritical,
4095 _Jv_JNI_ReleaseStringCritical,
4097 _Jv_JNI_NewWeakGlobalRef,
4098 _Jv_JNI_DeleteWeakGlobalRef,
4100 _Jv_JNI_ExceptionCheck,
4102 /* New JNI 1.4 functions. */
4104 jni_NewDirectByteBuffer,
4105 jni_GetDirectBufferAddress,
4106 jni_GetDirectBufferCapacity,
4108 /* New JNI 1.6 functions. */
4110 jni_GetObjectRefType
4114 /* Invocation API Functions ***************************************************/
4116 /* JNI_GetDefaultJavaVMInitArgs ************************************************
4118 Returns a default configuration for the Java VM.
4120 *******************************************************************************/
4122 jint JNI_GetDefaultJavaVMInitArgs(void *vm_args)
4124 JavaVMInitArgs *_vm_args;
4126 _vm_args = (JavaVMInitArgs *) vm_args;
4128 /* GNU classpath currently supports JNI 1.2 */
4130 switch (_vm_args->version) {
4131 case JNI_VERSION_1_1:
4132 _vm_args->version = JNI_VERSION_1_1;
4135 case JNI_VERSION_1_2:
4136 case JNI_VERSION_1_4:
4137 _vm_args->ignoreUnrecognized = JNI_FALSE;
4138 _vm_args->options = NULL;
4139 _vm_args->nOptions = 0;
4150 /* JNI_GetCreatedJavaVMs *******************************************************
4152 Returns all Java VMs that have been created. Pointers to VMs are written in
4153 the buffer vmBuf in the order they are created. At most bufLen number of
4154 entries will be written. The total number of created VMs is returned in
4157 *******************************************************************************/
4159 jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
4161 TRACEJNICALLS(("JNI_GetCreatedJavaVMs(vmBuf=%p, jsize=%d, jsize=%p)", vmBuf, bufLen, nVMs));
4166 // We currently only support 1 VM running.
4168 vmBuf[0] = VM::get_current()->get_javavm();
4175 /* JNI_CreateJavaVM ************************************************************
4177 Loads and initializes a Java VM. The current thread becomes the main thread.
4178 Sets the env argument to the JNI interface pointer of the main thread.
4180 *******************************************************************************/
4182 jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
4184 TRACEJNICALLS(("JNI_CreateJavaVM(p_vm=%p, p_env=%p, vm_args=%p)", p_vm, p_env, vm_args));
4186 /* actually create the JVM */
4188 if (!VM_create(p_vm, p_env, vm_args))
4198 * These are local overrides for various environment variables in Emacs.
4199 * Please do not remove this and leave it at the end of the file, where
4200 * Emacs will automagically detect them.
4201 * ---------------------------------------------------------------------
4204 * indent-tabs-mode: t
4208 * vim:noexpandtab:sw=4:ts=4: