1 /* src/native/jni.cpp - implementation of the Java Native Interface functions
3 Copyright (C) 1996-2011
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.hpp"
37 #include "native/jni.hpp"
38 #include "native/llni.h"
39 #include "native/localref.hpp"
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.hpp"
52 #include "vm/array.hpp"
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.hpp"
58 #include "vm/javaobjects.hpp"
59 #include "vm/loader.hpp"
60 #include "vm/options.h"
61 #include "vm/primitive.hpp"
62 #include "vm/resolve.hpp"
63 #include "vm/statistics.h"
64 #include "vm/string.hpp"
67 #include "vm/jit/asmpart.h"
68 #include "vm/jit/jit.hpp"
69 #include "vm/jit/stacktrace.hpp"
72 /* debug **********************************************************************/
76 # define TRACEJNICALLS(x) \
78 if (opt_TraceJNICalls) { \
83 # define TRACEJNICALLSENTER(x) \
85 if (opt_TraceJNICalls) { \
91 # define TRACEJNICALLSEXIT(x) \
93 if (opt_TraceJNICalls) { \
101 # define TRACEJNICALLS(x)
102 # define TRACEJNICALLSENTER(x)
103 # define TRACEJNICALLSEXIT(x)
108 /* global variables ***********************************************************/
110 /* global reference table *****************************************************/
112 /* hashsize must be power of 2 */
114 #define HASHTABLE_GLOBAL_REF_SIZE 64 /* initial size of globalref-hash */
116 static hashtable *hashtable_global_ref; /* hashtable for globalrefs */
119 /* direct buffer stuff ********************************************************/
121 #if defined(ENABLE_JAVASE)
122 static classinfo *class_java_nio_Buffer;
124 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
126 static classinfo *class_java_nio_DirectByteBufferImpl;
127 static classinfo *class_java_nio_DirectByteBufferImpl_ReadWrite;
129 # if SIZEOF_VOID_P == 8
130 static classinfo *class_gnu_classpath_Pointer64;
132 static classinfo *class_gnu_classpath_Pointer32;
135 static methodinfo *dbbirw_init;
137 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
139 static classinfo *class_sun_nio_ch_DirectBuffer;
140 static classinfo *class_java_nio_DirectByteBuffer;
142 static methodinfo *dbb_init;
148 /* some forward declarations **************************************************/
151 jobject jni_NewLocalRef(JNIEnv *env, jobject ref);
155 /* jni_init ********************************************************************
157 Initialize the JNI subsystem.
159 *******************************************************************************/
163 TRACESUBSYSTEMINITIALIZATION("jni_init");
165 /* create global ref hashtable */
167 hashtable_global_ref = NEW(hashtable);
169 hashtable_create(hashtable_global_ref, HASHTABLE_GLOBAL_REF_SIZE);
172 #if defined(ENABLE_JAVASE)
173 /* Direct buffer stuff. */
175 if (!(class_java_nio_Buffer =
176 load_class_bootstrap(utf_new_char("java/nio/Buffer"))) ||
177 !link_class(class_java_nio_Buffer))
180 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
182 if (!(class_java_nio_DirectByteBufferImpl =
183 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl"))) ||
184 !link_class(class_java_nio_DirectByteBufferImpl))
187 if (!(class_java_nio_DirectByteBufferImpl_ReadWrite =
188 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl$ReadWrite"))) ||
189 !link_class(class_java_nio_DirectByteBufferImpl_ReadWrite))
193 class_resolvemethod(class_java_nio_DirectByteBufferImpl_ReadWrite,
195 utf_new_char("(Ljava/lang/Object;Lgnu/classpath/Pointer;III)V"))))
198 # if SIZEOF_VOID_P == 8
199 if (!(class_gnu_classpath_Pointer64 =
200 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer64"))) ||
201 !link_class(class_gnu_classpath_Pointer64))
204 if (!(class_gnu_classpath_Pointer32 =
205 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer32"))) ||
206 !link_class(class_gnu_classpath_Pointer32))
210 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
212 if (!(class_sun_nio_ch_DirectBuffer =
213 load_class_bootstrap(utf_new_char("sun/nio/ch/DirectBuffer"))))
214 vm_abort("jni_init: loading sun/nio/ch/DirectBuffer failed");
216 if (!link_class(class_sun_nio_ch_DirectBuffer))
217 vm_abort("jni_init: linking sun/nio/ch/DirectBuffer failed");
219 if (!(class_java_nio_DirectByteBuffer =
220 load_class_bootstrap(utf_new_char("java/nio/DirectByteBuffer"))))
221 vm_abort("jni_init: loading java/nio/DirectByteBuffer failed");
223 if (!link_class(class_java_nio_DirectByteBuffer))
224 vm_abort("jni_init: linking java/nio/DirectByteBuffer failed");
227 class_resolvemethod(class_java_nio_DirectByteBuffer,
229 utf_new_char("(JI)V"))))
230 vm_abort("jni_init: resolving java/nio/DirectByteBuffer.init(JI)V failed");
234 #endif /* defined(ENABLE_JAVASE) */
240 /* jni_version_check ***********************************************************
242 Check if the given JNI version is supported.
245 version....JNI version to check
249 false......not supported
251 *******************************************************************************/
253 bool jni_version_check(int version)
256 case JNI_VERSION_1_1:
257 case JNI_VERSION_1_2:
258 case JNI_VERSION_1_4:
259 case JNI_VERSION_1_6:
267 /* _Jv_jni_CallObjectMethod ****************************************************
269 Internal function to call Java Object methods.
271 *******************************************************************************/
273 static java_handle_t *_Jv_jni_CallObjectMethod(java_handle_t *o,
275 methodinfo *m, va_list ap)
280 STATISTICS(jniinvokation());
283 exceptions_throw_nullpointerexception();
287 /* Class initialization is done by the JIT compiler. This is ok
288 since a static method always belongs to the declaring class. */
290 if (m->flags & ACC_STATIC) {
291 /* For static methods we reset the object. */
296 /* for convenience */
301 /* For instance methods we make a virtual function table lookup. */
303 resm = method_vftbl_lookup(vftbl, m);
306 STATISTICS(jnicallXmethodnvokation());
308 ro = vm_call_method_valist(resm, o, ap);
314 /* _Jv_jni_CallObjectMethodA ***************************************************
316 Internal function to call Java Object methods.
318 *******************************************************************************/
320 static java_handle_t *_Jv_jni_CallObjectMethodA(java_handle_t *o,
328 STATISTICS(jniinvokation());
331 exceptions_throw_nullpointerexception();
335 /* Class initialization is done by the JIT compiler. This is ok
336 since a static method always belongs to the declaring class. */
338 if (m->flags & ACC_STATIC) {
339 /* For static methods we reset the object. */
344 /* for convenience */
349 /* For instance methods we make a virtual function table lookup. */
351 resm = method_vftbl_lookup(vftbl, m);
354 STATISTICS(jnicallXmethodnvokation());
356 ro = vm_call_method_jvalue(resm, o, args);
362 /* _Jv_jni_CallIntMethod *******************************************************
364 Internal function to call Java integer class methods (boolean,
365 byte, char, short, int).
367 *******************************************************************************/
369 static jint _Jv_jni_CallIntMethod(java_handle_t *o, vftbl_t *vftbl,
370 methodinfo *m, va_list ap)
375 STATISTICS(jniinvokation());
378 exceptions_throw_nullpointerexception();
382 /* Class initialization is done by the JIT compiler. This is ok
383 since a static method always belongs to the declaring class. */
385 if (m->flags & ACC_STATIC) {
386 /* For static methods we reset the object. */
391 /* for convenience */
396 /* For instance methods we make a virtual function table lookup. */
398 resm = method_vftbl_lookup(vftbl, m);
401 STATISTICS(jnicallXmethodnvokation());
403 i = vm_call_method_int_valist(resm, o, ap);
409 /* _Jv_jni_CallIntMethodA ******************************************************
411 Internal function to call Java integer class methods (boolean,
412 byte, char, short, int).
414 *******************************************************************************/
416 static jint _Jv_jni_CallIntMethodA(java_handle_t *o, vftbl_t *vftbl,
417 methodinfo *m, const jvalue *args)
422 STATISTICS(jniinvokation());
425 exceptions_throw_nullpointerexception();
429 /* Class initialization is done by the JIT compiler. This is ok
430 since a static method always belongs to the declaring class. */
432 if (m->flags & ACC_STATIC) {
433 /* For static methods we reset the object. */
438 /* for convenience */
443 /* For instance methods we make a virtual function table lookup. */
445 resm = method_vftbl_lookup(vftbl, m);
448 STATISTICS(jnicallXmethodnvokation());
450 i = vm_call_method_int_jvalue(resm, o, args);
456 /* _Jv_jni_CallLongMethod ******************************************************
458 Internal function to call Java long methods.
460 *******************************************************************************/
462 static jlong _Jv_jni_CallLongMethod(java_handle_t *o, vftbl_t *vftbl,
463 methodinfo *m, va_list ap)
468 STATISTICS(jniinvokation());
471 exceptions_throw_nullpointerexception();
475 /* Class initialization is done by the JIT compiler. This is ok
476 since a static method always belongs to the declaring class. */
478 if (m->flags & ACC_STATIC) {
479 /* For static methods we reset the object. */
484 /* for convenience */
489 /* For instance methods we make a virtual function table lookup. */
491 resm = method_vftbl_lookup(vftbl, m);
494 STATISTICS(jnicallXmethodnvokation());
496 l = vm_call_method_long_valist(resm, o, ap);
502 /* _Jv_jni_CallLongMethodA *****************************************************
504 Internal function to call Java long methods.
506 *******************************************************************************/
508 static jlong _Jv_jni_CallLongMethodA(java_handle_t *o, vftbl_t *vftbl,
509 methodinfo *m, const jvalue *args)
514 STATISTICS(jniinvokation());
517 exceptions_throw_nullpointerexception();
521 /* Class initialization is done by the JIT compiler. This is ok
522 since a static method always belongs to the declaring class. */
524 if (m->flags & ACC_STATIC) {
525 /* For static methods we reset the object. */
530 /* for convenience */
535 /* For instance methods we make a virtual function table lookup. */
537 resm = method_vftbl_lookup(vftbl, m);
540 STATISTICS(jnicallXmethodnvokation());
542 l = vm_call_method_long_jvalue(resm, o, args);
548 /* _Jv_jni_CallFloatMethod *****************************************************
550 Internal function to call Java float methods.
552 *******************************************************************************/
554 static jfloat _Jv_jni_CallFloatMethod(java_handle_t *o, vftbl_t *vftbl,
555 methodinfo *m, va_list ap)
560 /* Class initialization is done by the JIT compiler. This is ok
561 since a static method always belongs to the declaring class. */
563 if (m->flags & ACC_STATIC) {
564 /* For static methods we reset the object. */
569 /* for convenience */
574 /* For instance methods we make a virtual function table lookup. */
576 resm = method_vftbl_lookup(vftbl, m);
579 STATISTICS(jnicallXmethodnvokation());
581 f = vm_call_method_float_valist(resm, o, ap);
587 /* _Jv_jni_CallFloatMethodA ****************************************************
589 Internal function to call Java float methods.
591 *******************************************************************************/
593 static jfloat _Jv_jni_CallFloatMethodA(java_handle_t *o, vftbl_t *vftbl,
594 methodinfo *m, const jvalue *args)
599 /* Class initialization is done by the JIT compiler. This is ok
600 since a static method always belongs to the declaring class. */
602 if (m->flags & ACC_STATIC) {
603 /* For static methods we reset the object. */
608 /* for convenience */
613 /* For instance methods we make a virtual function table lookup. */
615 resm = method_vftbl_lookup(vftbl, m);
618 STATISTICS(jnicallXmethodnvokation());
620 f = vm_call_method_float_jvalue(resm, o, args);
626 /* _Jv_jni_CallDoubleMethod ****************************************************
628 Internal function to call Java double methods.
630 *******************************************************************************/
632 static jdouble _Jv_jni_CallDoubleMethod(java_handle_t *o, vftbl_t *vftbl,
633 methodinfo *m, va_list ap)
638 /* Class initialization is done by the JIT compiler. This is ok
639 since a static method always belongs to the declaring class. */
641 if (m->flags & ACC_STATIC) {
642 /* For static methods we reset the object. */
647 /* for convenience */
652 /* For instance methods we make a virtual function table lookup. */
654 resm = method_vftbl_lookup(vftbl, m);
657 d = vm_call_method_double_valist(resm, o, ap);
663 /* _Jv_jni_CallDoubleMethodA ***************************************************
665 Internal function to call Java double methods.
667 *******************************************************************************/
669 static jdouble _Jv_jni_CallDoubleMethodA(java_handle_t *o, vftbl_t *vftbl,
670 methodinfo *m, const jvalue *args)
675 /* Class initialization is done by the JIT compiler. This is ok
676 since a static method always belongs to the declaring class. */
678 if (m->flags & ACC_STATIC) {
679 /* For static methods we reset the object. */
684 /* for convenience */
689 /* For instance methods we make a virtual function table lookup. */
691 resm = method_vftbl_lookup(vftbl, m);
694 d = vm_call_method_double_jvalue(resm, o, args);
700 /* _Jv_jni_CallVoidMethod ******************************************************
702 Internal function to call Java void methods.
704 *******************************************************************************/
706 static void _Jv_jni_CallVoidMethod(java_handle_t *o, vftbl_t *vftbl,
707 methodinfo *m, va_list ap)
712 exceptions_throw_nullpointerexception();
716 /* Class initialization is done by the JIT compiler. This is ok
717 since a static method always belongs to the declaring class. */
719 if (m->flags & ACC_STATIC) {
720 /* For static methods we reset the object. */
725 /* for convenience */
730 /* For instance methods we make a virtual function table lookup. */
732 resm = method_vftbl_lookup(vftbl, m);
735 STATISTICS(jnicallXmethodnvokation());
737 (void) vm_call_method_valist(resm, o, ap);
741 /* _Jv_jni_CallVoidMethodA *****************************************************
743 Internal function to call Java void methods.
745 *******************************************************************************/
747 static void _Jv_jni_CallVoidMethodA(java_handle_t *o, vftbl_t *vftbl,
748 methodinfo *m, const jvalue *args)
753 exceptions_throw_nullpointerexception();
757 /* Class initialization is done by the JIT compiler. This is ok
758 since a static method always belongs to the declaring class. */
760 if (m->flags & ACC_STATIC) {
761 /* For static methods we reset the object. */
766 /* for convenience */
771 /* For instance methods we make a virtual function table lookup. */
773 resm = method_vftbl_lookup(vftbl, m);
776 STATISTICS(jnicallXmethodnvokation());
778 (void) vm_call_method_jvalue(resm, o, args);
782 // JNI functions are exported as C functions.
785 /* GetVersion ******************************************************************
787 Returns the major version number in the higher 16 bits and the
788 minor version number in the lower 16 bits.
790 *******************************************************************************/
792 jint _Jv_JNI_GetVersion(JNIEnv *env)
794 TRACEJNICALLS(("_Jv_JNI_GetVersion(env=%p)", env));
796 return JNI_VERSION_SUPPORTED;
800 /* Class Operations ***********************************************************/
802 /* DefineClass *****************************************************************
804 Loads a class from a buffer of raw class data. The buffer
805 containing the raw class data is not referenced by the VM after the
806 DefineClass call returns, and it may be discarded if desired.
808 *******************************************************************************/
810 jclass jni_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize bufLen)
812 #if defined(ENABLE_JAVASE)
818 TRACEJNICALLS(("jni_DefineClass(env=%p, name=%s, loader=%p, buf=%p, bufLen=%d)", env, name, loader, buf, bufLen));
820 u = utf_new_char(name);
821 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
823 c = class_define(u, cl, bufLen, (uint8_t *) buf, NULL);
825 h = LLNI_classinfo_wrap(c);
827 return (jclass) jni_NewLocalRef(env, (jobject) h);
829 vm_abort("jni_DefineClass: Not implemented in this configuration");
831 // Keep compiler happy.
838 /* FindClass *******************************************************************
840 This function loads a locally-defined class. It searches the
841 directories and zip files specified by the CLASSPATH environment
842 variable for the class with the specified name.
844 *******************************************************************************/
846 jclass jni_FindClass(JNIEnv *env, const char *name)
848 #if defined(ENABLE_JAVASE)
855 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
857 /* FIXME If name is NULL we have a problem here. */
859 u = utf_new_char_classname((char *) name);
861 if ((u == NULL) /*|| (int)strlen(name) > symbolOopDesc::max_length() */) {
862 exceptions_throw_noclassdeffounderror(u);
866 /* Check stacktrace for classloader, if one found use it,
867 otherwise use the system classloader. */
869 /* Quote from the JNI documentation:
871 In the Java 2 Platform, FindClass locates the class loader
872 associated with the current native method. If the native code
873 belongs to a system class, no class loader will be
874 involved. Otherwise, the proper class loader will be invoked to
875 load and link the named class. When FindClass is called through
876 the Invocation Interface, there is no current native method or
877 its associated class loader. In that case, the result of
878 ClassLoader.getBaseClassLoader is used." */
880 cc = stacktrace_get_current_class();
883 c = load_class_from_sysloader(u);
885 c = load_class_from_classloader(u, cc->classloader);
888 resolve_handle_pending_exception(true);
895 h = LLNI_classinfo_wrap(c);
897 return (jclass) jni_NewLocalRef(env, (jobject) h);
899 #elif defined(ENABLE_JAVAME_CLDC1_1)
904 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
906 u = utf_new_char_classname((char *) name);
907 c = load_class_bootstrap(u);
910 resolve_handle_pending_exception(true);
917 return (jclass) jni_NewLocalRef(env, (jobject) c);
920 vm_abort("jni_FindClass: not implemented in this configuration");
922 /* keep compiler happy */
929 /* GetSuperclass ***************************************************************
931 If clazz represents any class other than the class Object, then
932 this function returns the object that represents the superclass of
933 the class specified by clazz.
935 *******************************************************************************/
937 jclass jni_GetSuperclass(JNIEnv *env, jclass sub)
942 TRACEJNICALLS(("jni_GetSuperclass(env=%p, sub=%p)", env, sub));
944 c = LLNI_classinfo_unwrap(sub);
949 super = class_get_superclass(c);
951 java_handle_t* h = LLNI_classinfo_wrap(super);
953 return (jclass) jni_NewLocalRef(env, (jobject) h);
957 /* IsAssignableFrom ************************************************************
959 Determines whether an object of sub can be safely cast to sup.
961 *******************************************************************************/
963 jboolean _Jv_JNI_IsAssignableFrom(JNIEnv *env, jclass sub, jclass sup)
968 TRACEJNICALLS(("_Jv_JNI_IsAssignableFrom(env=%p, sub=%p, sup=%p)", env, sub, sup));
970 to = (classinfo *) sup;
971 from = (classinfo *) sub;
973 return class_is_assignable_from(to, from);
977 /* Throw ***********************************************************************
979 Causes a java.lang.Throwable object to be thrown.
981 *******************************************************************************/
983 jint _Jv_JNI_Throw(JNIEnv *env, jthrowable obj)
987 STATISTICS(jniinvokation());
989 o = (java_handle_t *) obj;
991 exceptions_set_exception(o);
997 /* ThrowNew ********************************************************************
999 Constructs an exception object from the specified class with the
1000 message specified by message and causes that exception to be
1003 *******************************************************************************/
1005 jint _Jv_JNI_ThrowNew(JNIEnv* env, jclass clazz, const char *msg)
1011 STATISTICS(jniinvokation());
1013 c = LLNI_classinfo_unwrap(clazz);
1016 s = javastring_new_from_utf_string(msg);
1018 /* instantiate exception object */
1020 o = native_new_and_init_string(c, s);
1025 exceptions_set_exception(o);
1031 /* ExceptionOccurred ***********************************************************
1033 Determines if an exception is being thrown. The exception stays
1034 being thrown until either the native code calls ExceptionClear(),
1035 or the Java code handles the exception.
1037 *******************************************************************************/
1039 jthrowable _Jv_JNI_ExceptionOccurred(JNIEnv *env)
1043 TRACEJNICALLS(("_Jv_JNI_ExceptionOccurred(env=%p)", env));
1045 o = exceptions_get_exception();
1047 return (jthrowable) jni_NewLocalRef(env, (jthrowable) o);
1051 /* ExceptionDescribe ***********************************************************
1053 Prints an exception and a backtrace of the stack to a system
1054 error-reporting channel, such as stderr. This is a convenience
1055 routine provided for debugging.
1057 *******************************************************************************/
1059 void jni_ExceptionDescribe(JNIEnv *env)
1061 TRACEJNICALLS(("jni_ExceptionDescribe(env=%p)", env));
1063 exceptions_print_stacktrace();
1067 /* ExceptionClear **************************************************************
1069 Clears any exception that is currently being thrown. If no
1070 exception is currently being thrown, this routine has no effect.
1072 *******************************************************************************/
1074 void jni_ExceptionClear(JNIEnv *env)
1076 TRACEJNICALLS(("jni_ExceptionClear(env=%p)", env));
1078 exceptions_clear_exception();
1082 /* FatalError ******************************************************************
1084 Raises a fatal error and does not expect the VM to recover. This
1085 function does not return.
1087 *******************************************************************************/
1089 void _Jv_JNI_FatalError(JNIEnv *env, const char *msg)
1091 STATISTICS(jniinvokation());
1093 /* this seems to be the best way */
1095 vm_abort("JNI Fatal error: %s", msg);
1099 /* PushLocalFrame **************************************************************
1101 Creates a new local reference frame, in which at least a given
1102 number of local references can be created.
1104 *******************************************************************************/
1106 jint jni_PushLocalFrame(JNIEnv* env, jint capacity)
1108 TRACEJNICALLS(("jni_PushLocalFrame(env=%p, capacity=%d)", env, capacity));
1113 /* add new local reference frame to current table */
1115 if (!localref_frame_push(capacity))
1122 /* PopLocalFrame ***************************************************************
1124 Pops off the current local reference frame, frees all the local
1125 references, and returns a local reference in the previous local
1126 reference frame for the given result object.
1128 *******************************************************************************/
1130 jobject jni_PopLocalFrame(JNIEnv* env, jobject result)
1132 TRACEJNICALLS(("jni_PopLocalFrame(env=%p, result=%p)", env, result));
1134 /* release all current local frames */
1136 localref_frame_pop_all();
1138 /* add local reference and return the value */
1140 return jni_NewLocalRef(env, result);
1144 /* DeleteLocalRef **************************************************************
1146 Deletes the local reference pointed to by localRef.
1148 *******************************************************************************/
1150 void jni_DeleteLocalRef(JNIEnv *env, jobject localRef)
1154 TRACEJNICALLS(("jni_DeleteLocalRef(env=%p, ref=%p)", env, localRef));
1156 o = (java_handle_t *) localRef;
1161 /* delete the reference */
1167 /* IsSameObject ****************************************************************
1169 Tests whether two references refer to the same Java object.
1171 *******************************************************************************/
1173 jboolean _Jv_JNI_IsSameObject(JNIEnv *env, jobject ref1, jobject ref2)
1179 STATISTICS(jniinvokation());
1181 o1 = (java_handle_t *) ref1;
1182 o2 = (java_handle_t *) ref2;
1184 LLNI_CRITICAL_START;
1186 if (LLNI_UNWRAP(o1) == LLNI_UNWRAP(o2))
1197 /* NewLocalRef *****************************************************************
1199 Creates a new local reference that refers to the same object as ref.
1201 *******************************************************************************/
1203 jobject jni_NewLocalRef(JNIEnv *env, jobject ref)
1206 java_handle_t *localref;
1208 TRACEJNICALLS(("jni_NewLocalRef(env=%p, ref=%p)", env, ref));
1210 o = (java_handle_t *) ref;
1215 /* insert the reference */
1217 localref = localref_add(LLNI_DIRECT(o));
1219 return (jobject) localref;
1223 /* EnsureLocalCapacity *********************************************************
1225 Ensures that at least a given number of local references can be
1226 created in the current thread
1228 *******************************************************************************/
1230 jint jni_EnsureLocalCapacity(JNIEnv* env, jint capacity)
1232 localref_table *lrt;
1234 TRACEJNICALLS(("jni_EnsureLocalCapacity(env=%p, capacity=%d)", env, capacity));
1236 /* get local reference table (thread specific) */
1238 lrt = LOCALREFTABLE;
1240 /* check if capacity elements are available in the local references table */
1242 if ((lrt->used + capacity) > lrt->capacity)
1243 return jni_PushLocalFrame(env, capacity);
1249 /* AllocObject *****************************************************************
1251 Allocates a new Java object without invoking any of the
1252 constructors for the object. Returns a reference to the object.
1254 *******************************************************************************/
1256 jobject _Jv_JNI_AllocObject(JNIEnv *env, jclass clazz)
1261 STATISTICS(jniinvokation());
1263 c = LLNI_classinfo_unwrap(clazz);
1265 if ((c->flags & ACC_INTERFACE) || (c->flags & ACC_ABSTRACT)) {
1266 exceptions_throw_instantiationexception(c);
1272 return jni_NewLocalRef(env, (jobject) o);
1276 /* NewObject *******************************************************************
1278 Programmers place all arguments that are to be passed to the
1279 constructor immediately following the methodID
1280 argument. NewObject() accepts these arguments and passes them to
1281 the Java method that the programmer wishes to invoke.
1283 *******************************************************************************/
1285 jobject jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
1292 TRACEJNICALLSENTER(("jni_NewObject(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
1294 c = LLNI_classinfo_unwrap(clazz);
1295 m = (methodinfo *) methodID;
1304 /* call constructor */
1306 va_start(ap, methodID);
1307 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1310 TRACEJNICALLSEXIT(("->%p", o));
1312 return jni_NewLocalRef(env, (jobject) o);
1316 /* NewObjectV ******************************************************************
1318 Programmers place all arguments that are to be passed to the
1319 constructor in an args argument of type va_list that immediately
1320 follows the methodID argument. NewObjectV() accepts these
1321 arguments, and, in turn, passes them to the Java method that the
1322 programmer wishes to invoke.
1324 *******************************************************************************/
1326 jobject _Jv_JNI_NewObjectV(JNIEnv* env, jclass clazz, jmethodID methodID,
1333 STATISTICS(jniinvokation());
1335 c = LLNI_classinfo_unwrap(clazz);
1336 m = (methodinfo *) methodID;
1345 /* call constructor */
1347 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1349 return jni_NewLocalRef(env, (jobject) o);
1353 /* NewObjectA *****************************************************************
1355 Programmers place all arguments that are to be passed to the
1356 constructor in an args array of jvalues that immediately follows
1357 the methodID argument. NewObjectA() accepts the arguments in this
1358 array, and, in turn, passes them to the Java method that the
1359 programmer wishes to invoke.
1361 *******************************************************************************/
1363 jobject _Jv_JNI_NewObjectA(JNIEnv* env, jclass clazz, jmethodID methodID,
1370 STATISTICS(jniinvokation());
1372 c = LLNI_classinfo_unwrap(clazz);
1373 m = (methodinfo *) methodID;
1382 /* call constructor */
1384 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1386 return jni_NewLocalRef(env, (jobject) o);
1390 /* GetObjectClass **************************************************************
1392 Returns the class of an object.
1394 *******************************************************************************/
1396 jclass jni_GetObjectClass(JNIEnv *env, jobject obj)
1401 TRACEJNICALLS(("jni_GetObjectClass(env=%p, obj=%p)", env, obj));
1403 o = (java_handle_t *) obj;
1405 if ((o == NULL) || (LLNI_vftbl_direct(o) == NULL))
1408 LLNI_class_get(o, c);
1410 java_handle_t* h = LLNI_classinfo_wrap(c);
1412 return (jclass) jni_NewLocalRef(env, (jobject) h);
1416 /* IsInstanceOf ****************************************************************
1418 Tests whether an object is an instance of a class.
1420 *******************************************************************************/
1422 jboolean _Jv_JNI_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)
1427 TRACEJNICALLS(("_Jv_JNI_IsInstanceOf(env=%p, obj=%p, clazz=%p)", env, obj, clazz));
1429 /* XXX Is this correct? */
1430 c = LLNI_classinfo_unwrap(clazz);
1431 h = (java_handle_t *) obj;
1433 return class_is_instance(c, h);
1437 /* Reflection Support *********************************************************/
1439 /* FromReflectedMethod *********************************************************
1441 Converts java.lang.reflect.Method or java.lang.reflect.Constructor
1442 object to a method ID.
1444 *******************************************************************************/
1446 jmethodID jni_FromReflectedMethod(JNIEnv *env, jobject method)
1448 #if defined(ENABLE_JAVASE)
1451 TRACEJNICALLS(("jni_FromReflectedMethod(env=%p, method=%p)", env, method));
1453 java_lang_Object o(method);
1458 if (o.get_Class() == class_java_lang_reflect_Constructor) {
1459 java_lang_reflect_Constructor rc(method);
1460 m = rc.get_method();
1463 assert(o.get_Class() == class_java_lang_reflect_Method);
1465 java_lang_reflect_Method rm(method);
1466 m = rm.get_method();
1469 return (jmethodID) m;
1471 vm_abort("jni_FromReflectedMethod: Not implemented in this configuration.");
1473 // Keep compiler happy.
1479 /* FromReflectedField **********************************************************
1481 Converts a java.lang.reflect.Field to a field ID.
1483 *******************************************************************************/
1485 jfieldID jni_FromReflectedField(JNIEnv* env, jobject field)
1487 #if defined(ENABLE_JAVASE)
1489 TRACEJNICALLS(("jni_FromReflectedField(env=%p, field=%p)", env, field));
1491 java_lang_reflect_Field rf(field);
1496 fieldinfo* f = rf.get_field();
1498 return (jfieldID) f;
1500 vm_abort("jni_FromReflectedField: Not implemented in this configuration.");
1502 // Keep compiler happy.
1508 /* ToReflectedMethod ***********************************************************
1510 Converts a method ID derived from cls to an instance of the
1511 java.lang.reflect.Method class or to an instance of the
1512 java.lang.reflect.Constructor class.
1514 *******************************************************************************/
1516 jobject jni_ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic)
1518 #if defined(ENABLE_JAVASE)
1519 TRACEJNICALLS(("jni_ToReflectedMethod(env=%p, cls=%p, methodID=%p, isStatic=%d)", env, cls, methodID, isStatic));
1521 methodinfo* m = (methodinfo *) methodID;
1523 /* HotSpot does the same assert. */
1525 assert(((m->flags & ACC_STATIC) != 0) == (isStatic != 0));
1529 if (m->name == utf_init) {
1530 h = java_lang_reflect_Constructor(m).get_handle();
1533 h = java_lang_reflect_Method(m).get_handle();
1538 vm_abort("jni_ToReflectedMethod: Not implemented in this configuration.");
1540 /* keep compiler happy */
1547 /* ToReflectedField ************************************************************
1549 Converts a field ID derived from cls to an instance of the
1550 java.lang.reflect.Field class.
1552 *******************************************************************************/
1554 jobject _Jv_JNI_ToReflectedField(JNIEnv* env, jclass cls, jfieldID fieldID,
1557 STATISTICS(jniinvokation());
1559 log_text("JNI-Call: ToReflectedField: IMPLEMENT ME!");
1565 /* Calling Instance Methods ***************************************************/
1567 /* GetMethodID *****************************************************************
1569 Returns the method ID for an instance (nonstatic) method of a class
1570 or interface. The method may be defined in one of the clazz's
1571 superclasses and inherited by clazz. The method is determined by
1572 its name and signature.
1574 GetMethodID() causes an uninitialized class to be initialized.
1576 *******************************************************************************/
1578 jmethodID _Jv_JNI_GetMethodID(JNIEnv* env, jclass clazz, const char *name,
1586 STATISTICS(jniinvokation());
1588 c = LLNI_classinfo_unwrap(clazz);
1593 if (!(c->state & CLASS_INITIALIZED))
1594 if (!initialize_class(c))
1597 /* try to get the method of the class or one of it's superclasses */
1599 uname = utf_new_char((char *) name);
1600 udesc = utf_new_char((char *) sig);
1602 m = class_resolvemethod(c, uname, udesc);
1604 if ((m == NULL) || (m->flags & ACC_STATIC)) {
1605 exceptions_throw_nosuchmethoderror(c, uname, udesc);
1610 return (jmethodID) m;
1614 /* JNI-functions for calling instance methods *********************************/
1616 #define JNI_CALL_VIRTUAL_METHOD(name, type, intern) \
1617 type _Jv_JNI_Call##name##Method(JNIEnv *env, jobject obj, \
1618 jmethodID methodID, ...) \
1625 o = (java_handle_t *) obj; \
1626 m = (methodinfo *) methodID; \
1628 va_start(ap, methodID); \
1629 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, ap); \
1635 JNI_CALL_VIRTUAL_METHOD(Boolean, jboolean, Int)
1636 JNI_CALL_VIRTUAL_METHOD(Byte, jbyte, Int)
1637 JNI_CALL_VIRTUAL_METHOD(Char, jchar, Int)
1638 JNI_CALL_VIRTUAL_METHOD(Short, jshort, Int)
1639 JNI_CALL_VIRTUAL_METHOD(Int, jint, Int)
1640 JNI_CALL_VIRTUAL_METHOD(Long, jlong, Long)
1641 JNI_CALL_VIRTUAL_METHOD(Float, jfloat, Float)
1642 JNI_CALL_VIRTUAL_METHOD(Double, jdouble, Double)
1645 #define JNI_CALL_VIRTUAL_METHOD_V(name, type, intern) \
1646 type _Jv_JNI_Call##name##MethodV(JNIEnv *env, jobject obj, \
1647 jmethodID methodID, va_list args) \
1653 o = (java_handle_t *) obj; \
1654 m = (methodinfo *) methodID; \
1656 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, args); \
1661 JNI_CALL_VIRTUAL_METHOD_V(Boolean, jboolean, Int)
1662 JNI_CALL_VIRTUAL_METHOD_V(Byte, jbyte, Int)
1663 JNI_CALL_VIRTUAL_METHOD_V(Char, jchar, Int)
1664 JNI_CALL_VIRTUAL_METHOD_V(Short, jshort, Int)
1665 JNI_CALL_VIRTUAL_METHOD_V(Int, jint, Int)
1666 JNI_CALL_VIRTUAL_METHOD_V(Long, jlong, Long)
1667 JNI_CALL_VIRTUAL_METHOD_V(Float, jfloat, Float)
1668 JNI_CALL_VIRTUAL_METHOD_V(Double, jdouble, Double)
1671 #define JNI_CALL_VIRTUAL_METHOD_A(name, type, intern) \
1672 type _Jv_JNI_Call##name##MethodA(JNIEnv *env, jobject obj, \
1673 jmethodID methodID, \
1674 const jvalue *args) \
1680 o = (java_handle_t *) obj; \
1681 m = (methodinfo *) methodID; \
1683 ret = _Jv_jni_Call##intern##MethodA(o, LLNI_vftbl_direct(o), m, args); \
1688 JNI_CALL_VIRTUAL_METHOD_A(Boolean, jboolean, Int)
1689 JNI_CALL_VIRTUAL_METHOD_A(Byte, jbyte, Int)
1690 JNI_CALL_VIRTUAL_METHOD_A(Char, jchar, Int)
1691 JNI_CALL_VIRTUAL_METHOD_A(Short, jshort, Int)
1692 JNI_CALL_VIRTUAL_METHOD_A(Int, jint, Int)
1693 JNI_CALL_VIRTUAL_METHOD_A(Long, jlong, Long)
1694 JNI_CALL_VIRTUAL_METHOD_A(Float, jfloat, Float)
1695 JNI_CALL_VIRTUAL_METHOD_A(Double, jdouble, Double)
1698 jobject _Jv_JNI_CallObjectMethod(JNIEnv *env, jobject obj, jmethodID methodID,
1706 o = (java_handle_t *) obj;
1707 m = (methodinfo *) methodID;
1709 va_start(ap, methodID);
1710 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, ap);
1713 return jni_NewLocalRef(env, (jobject) ret);
1717 jobject _Jv_JNI_CallObjectMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1724 o = (java_handle_t *) obj;
1725 m = (methodinfo *) methodID;
1727 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, args);
1729 return jni_NewLocalRef(env, (jobject) ret);
1733 jobject _Jv_JNI_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1740 o = (java_handle_t *) obj;
1741 m = (methodinfo *) methodID;
1743 ret = _Jv_jni_CallObjectMethodA(o, LLNI_vftbl_direct(o), m, args);
1745 return jni_NewLocalRef(env, (jobject) ret);
1750 void _Jv_JNI_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
1756 o = (java_handle_t *) obj;
1757 m = (methodinfo *) methodID;
1759 va_start(ap, methodID);
1760 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1765 void _Jv_JNI_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1771 o = (java_handle_t *) obj;
1772 m = (methodinfo *) methodID;
1774 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1778 void _Jv_JNI_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1784 o = (java_handle_t *) obj;
1785 m = (methodinfo *) methodID;
1787 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1792 #define JNI_CALL_NONVIRTUAL_METHOD(name, type, intern) \
1793 type _Jv_JNI_CallNonvirtual##name##Method(JNIEnv *env, jobject obj, \
1794 jclass clazz, jmethodID methodID, \
1803 o = (java_handle_t *) obj; \
1804 c = LLNI_classinfo_unwrap(clazz); \
1805 m = (methodinfo *) methodID; \
1807 va_start(ap, methodID); \
1808 ret = _Jv_jni_Call##intern##Method(o, c->vftbl, m, ap); \
1814 JNI_CALL_NONVIRTUAL_METHOD(Boolean, jboolean, Int)
1815 JNI_CALL_NONVIRTUAL_METHOD(Byte, jbyte, Int)
1816 JNI_CALL_NONVIRTUAL_METHOD(Char, jchar, Int)
1817 JNI_CALL_NONVIRTUAL_METHOD(Short, jshort, Int)
1818 JNI_CALL_NONVIRTUAL_METHOD(Int, jint, Int)
1819 JNI_CALL_NONVIRTUAL_METHOD(Long, jlong, Long)
1820 JNI_CALL_NONVIRTUAL_METHOD(Float, jfloat, Float)
1821 JNI_CALL_NONVIRTUAL_METHOD(Double, jdouble, Double)
1824 #define JNI_CALL_NONVIRTUAL_METHOD_V(name, type, intern) \
1825 type _Jv_JNI_CallNonvirtual##name##MethodV(JNIEnv *env, jobject obj, \
1826 jclass clazz, jmethodID methodID, \
1834 o = (java_handle_t *) obj; \
1835 c = LLNI_classinfo_unwrap(clazz); \
1836 m = (methodinfo *) methodID; \
1838 ret = _Jv_jni_CallIntMethod(o, c->vftbl, m, args); \
1843 JNI_CALL_NONVIRTUAL_METHOD_V(Boolean, jboolean, Int)
1844 JNI_CALL_NONVIRTUAL_METHOD_V(Byte, jbyte, Int)
1845 JNI_CALL_NONVIRTUAL_METHOD_V(Char, jchar, Int)
1846 JNI_CALL_NONVIRTUAL_METHOD_V(Short, jshort, Int)
1847 JNI_CALL_NONVIRTUAL_METHOD_V(Int, jint, Int)
1848 JNI_CALL_NONVIRTUAL_METHOD_V(Long, jlong, Long)
1849 JNI_CALL_NONVIRTUAL_METHOD_V(Float, jfloat, Float)
1850 JNI_CALL_NONVIRTUAL_METHOD_V(Double, jdouble, Double)
1853 #define JNI_CALL_NONVIRTUAL_METHOD_A(name, type, intern) \
1854 type _Jv_JNI_CallNonvirtual##name##MethodA(JNIEnv *env, jobject obj, \
1855 jclass clazz, jmethodID methodID, \
1856 const jvalue *args) \
1858 log_text("JNI-Call: CallNonvirtual##name##MethodA: IMPLEMENT ME!"); \
1863 JNI_CALL_NONVIRTUAL_METHOD_A(Boolean, jboolean, Int)
1864 JNI_CALL_NONVIRTUAL_METHOD_A(Byte, jbyte, Int)
1865 JNI_CALL_NONVIRTUAL_METHOD_A(Char, jchar, Int)
1866 JNI_CALL_NONVIRTUAL_METHOD_A(Short, jshort, Int)
1867 JNI_CALL_NONVIRTUAL_METHOD_A(Int, jint, Int)
1868 JNI_CALL_NONVIRTUAL_METHOD_A(Long, jlong, Long)
1869 JNI_CALL_NONVIRTUAL_METHOD_A(Float, jfloat, Float)
1870 JNI_CALL_NONVIRTUAL_METHOD_A(Double, jdouble, Double)
1872 jobject _Jv_JNI_CallNonvirtualObjectMethod(JNIEnv *env, jobject obj,
1873 jclass clazz, jmethodID methodID,
1882 o = (java_handle_t *) obj;
1883 c = LLNI_classinfo_unwrap(clazz);
1884 m = (methodinfo *) methodID;
1886 va_start(ap, methodID);
1887 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, ap);
1890 return jni_NewLocalRef(env, (jobject) r);
1894 jobject _Jv_JNI_CallNonvirtualObjectMethodV(JNIEnv *env, jobject obj,
1895 jclass clazz, jmethodID methodID,
1903 o = (java_handle_t *) obj;
1904 c = LLNI_classinfo_unwrap(clazz);
1905 m = (methodinfo *) methodID;
1907 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, args);
1909 return jni_NewLocalRef(env, (jobject) r);
1913 jobject _Jv_JNI_CallNonvirtualObjectMethodA(JNIEnv *env, jobject obj,
1914 jclass clazz, jmethodID methodID,
1917 log_text("JNI-Call: CallNonvirtualObjectMethodA: IMPLEMENT ME!");
1919 return jni_NewLocalRef(env, NULL);
1923 void _Jv_JNI_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass clazz,
1924 jmethodID methodID, ...)
1931 o = (java_handle_t *) obj;
1932 c = LLNI_classinfo_unwrap(clazz);
1933 m = (methodinfo *) methodID;
1935 va_start(ap, methodID);
1936 _Jv_jni_CallVoidMethod(o, c->vftbl, m, ap);
1941 void _Jv_JNI_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass clazz,
1942 jmethodID methodID, va_list args)
1948 o = (java_handle_t *) obj;
1949 c = LLNI_classinfo_unwrap(clazz);
1950 m = (methodinfo *) methodID;
1952 _Jv_jni_CallVoidMethod(o, c->vftbl, m, args);
1956 void _Jv_JNI_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass clazz,
1957 jmethodID methodID, const jvalue * args)
1963 o = (java_handle_t *) obj;
1964 c = LLNI_classinfo_unwrap(clazz);
1965 m = (methodinfo *) methodID;
1967 _Jv_jni_CallVoidMethodA(o, c->vftbl, m, args);
1971 /* Accessing Fields of Objects ************************************************/
1973 /* GetFieldID ******************************************************************
1975 Returns the field ID for an instance (nonstatic) field of a
1976 class. The field is specified by its name and signature. The
1977 Get<type>Field and Set<type>Field families of accessor functions
1978 use field IDs to retrieve object fields.
1980 *******************************************************************************/
1982 jfieldID _Jv_JNI_GetFieldID(JNIEnv *env, jclass clazz, const char *name,
1990 STATISTICS(jniinvokation());
1992 c = LLNI_classinfo_unwrap(clazz);
1994 /* XXX NPE check? */
1996 uname = utf_new_char((char *) name);
1997 udesc = utf_new_char((char *) sig);
1999 f = class_findfield(c, uname, udesc);
2002 exceptions_throw_nosuchfielderror(c, uname);
2004 return (jfieldID) f;
2008 /* Get<type>Field Routines *****************************************************
2010 This family of accessor routines returns the value of an instance
2011 (nonstatic) field of an object. The field to access is specified by
2012 a field ID obtained by calling GetFieldID().
2014 *******************************************************************************/
2016 #define GET_FIELD(o,type,f) \
2017 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset)))
2019 #define JNI_GET_FIELD(name, type, intern) \
2020 type _Jv_JNI_Get##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID) \
2024 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "Field(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID)); \
2026 LLNI_CRITICAL_START; \
2028 ret = GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID); \
2030 LLNI_CRITICAL_END; \
2032 return (type) ret; \
2035 JNI_GET_FIELD(Boolean, jboolean, s4)
2036 JNI_GET_FIELD(Byte, jbyte, s4)
2037 JNI_GET_FIELD(Char, jchar, s4)
2038 JNI_GET_FIELD(Short, jshort, s4)
2039 JNI_GET_FIELD(Int, jint, s4)
2040 JNI_GET_FIELD(Long, jlong, s8)
2041 JNI_GET_FIELD(Float, jfloat, float)
2042 JNI_GET_FIELD(Double, jdouble, double)
2045 jobject _Jv_JNI_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)
2049 TRACEJNICALLS(("_Jv_JNI_GetObjectField(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID));
2051 LLNI_CRITICAL_START;
2053 o = LLNI_WRAP(GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), java_object_t*, fieldID));
2057 return jni_NewLocalRef(env, (jobject) o);
2061 /* Set<type>Field Routines *****************************************************
2063 This family of accessor routines sets the value of an instance
2064 (nonstatic) field of an object. The field to access is specified by
2065 a field ID obtained by calling GetFieldID().
2067 *******************************************************************************/
2069 #define SET_FIELD(o,type,f,value) \
2070 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset))) = (type) (value)
2072 #define GET_FIELDINFO(f) ((fieldinfo*) (f))
2074 #define JNI_SET_FIELD(name, type, intern) \
2075 void _Jv_JNI_Set##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID, \
2078 TRACEJNICALLS(("_Jv_JNI_Set" STR(name) "Field(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value)); \
2080 LLNI_CRITICAL_START; \
2082 SET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID, value); \
2084 LLNI_CRITICAL_END; \
2086 if (GET_FIELDINFO(fieldID)->flags & ACC_VOLATILE) \
2087 Atomic::memory_barrier(); \
2090 JNI_SET_FIELD(Boolean, jboolean, s4)
2091 JNI_SET_FIELD(Byte, jbyte, s4)
2092 JNI_SET_FIELD(Char, jchar, s4)
2093 JNI_SET_FIELD(Short, jshort, s4)
2094 JNI_SET_FIELD(Int, jint, s4)
2095 JNI_SET_FIELD(Long, jlong, s8)
2096 JNI_SET_FIELD(Float, jfloat, float)
2097 JNI_SET_FIELD(Double, jdouble, double)
2100 void _Jv_JNI_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID,
2103 TRACEJNICALLS(("_Jv_JNI_SetObjectField(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value));
2105 LLNI_CRITICAL_START;
2107 SET_FIELD(obj, java_handle_t*, fieldID, LLNI_UNWRAP((java_handle_t*) value));
2111 if (GET_FIELDINFO(fieldID)->flags & ACC_VOLATILE)
2112 Atomic::memory_barrier();
2116 /* Calling Static Methods *****************************************************/
2118 /* GetStaticMethodID ***********************************************************
2120 Returns the method ID for a static method of a class. The method is
2121 specified by its name and signature.
2123 GetStaticMethodID() causes an uninitialized class to be
2126 *******************************************************************************/
2128 jmethodID _Jv_JNI_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name,
2136 TRACEJNICALLS(("_Jv_JNI_GetStaticMethodID(env=%p, clazz=%p, name=%s, sig=%s)", env, clazz, name, sig));
2138 c = LLNI_classinfo_unwrap(clazz);
2143 if (!(c->state & CLASS_INITIALIZED))
2144 if (!initialize_class(c))
2147 /* try to get the static method of the class */
2149 uname = utf_new_char((char *) name);
2150 udesc = utf_new_char((char *) sig);
2152 m = class_resolvemethod(c, uname, udesc);
2154 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
2155 exceptions_throw_nosuchmethoderror(c, uname, udesc);
2160 return (jmethodID) m;
2164 #define JNI_CALL_STATIC_METHOD(name, type, intern) \
2165 type _Jv_JNI_CallStatic##name##Method(JNIEnv *env, jclass clazz, \
2166 jmethodID methodID, ...) \
2172 m = (methodinfo *) methodID; \
2174 va_start(ap, methodID); \
2175 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, ap); \
2181 JNI_CALL_STATIC_METHOD(Boolean, jboolean, Int)
2182 JNI_CALL_STATIC_METHOD(Byte, jbyte, Int)
2183 JNI_CALL_STATIC_METHOD(Char, jchar, Int)
2184 JNI_CALL_STATIC_METHOD(Short, jshort, Int)
2185 JNI_CALL_STATIC_METHOD(Int, jint, Int)
2186 JNI_CALL_STATIC_METHOD(Long, jlong, Long)
2187 JNI_CALL_STATIC_METHOD(Float, jfloat, Float)
2188 JNI_CALL_STATIC_METHOD(Double, jdouble, Double)
2191 #define JNI_CALL_STATIC_METHOD_V(name, type, intern) \
2192 type _Jv_JNI_CallStatic##name##MethodV(JNIEnv *env, jclass clazz, \
2193 jmethodID methodID, va_list args) \
2198 m = (methodinfo *) methodID; \
2200 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, args); \
2205 JNI_CALL_STATIC_METHOD_V(Boolean, jboolean, Int)
2206 JNI_CALL_STATIC_METHOD_V(Byte, jbyte, Int)
2207 JNI_CALL_STATIC_METHOD_V(Char, jchar, Int)
2208 JNI_CALL_STATIC_METHOD_V(Short, jshort, Int)
2209 JNI_CALL_STATIC_METHOD_V(Int, jint, Int)
2210 JNI_CALL_STATIC_METHOD_V(Long, jlong, Long)
2211 JNI_CALL_STATIC_METHOD_V(Float, jfloat, Float)
2212 JNI_CALL_STATIC_METHOD_V(Double, jdouble, Double)
2215 #define JNI_CALL_STATIC_METHOD_A(name, type, intern) \
2216 type _Jv_JNI_CallStatic##name##MethodA(JNIEnv *env, jclass clazz, \
2217 jmethodID methodID, const jvalue *args) \
2222 m = (methodinfo *) methodID; \
2224 res = _Jv_jni_Call##intern##MethodA(NULL, NULL, m, args); \
2229 JNI_CALL_STATIC_METHOD_A(Boolean, jboolean, Int)
2230 JNI_CALL_STATIC_METHOD_A(Byte, jbyte, Int)
2231 JNI_CALL_STATIC_METHOD_A(Char, jchar, Int)
2232 JNI_CALL_STATIC_METHOD_A(Short, jshort, Int)
2233 JNI_CALL_STATIC_METHOD_A(Int, jint, Int)
2234 JNI_CALL_STATIC_METHOD_A(Long, jlong, Long)
2235 JNI_CALL_STATIC_METHOD_A(Float, jfloat, Float)
2236 JNI_CALL_STATIC_METHOD_A(Double, jdouble, Double)
2239 jobject _Jv_JNI_CallStaticObjectMethod(JNIEnv *env, jclass clazz,
2240 jmethodID methodID, ...)
2246 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2248 m = (methodinfo *) methodID;
2250 va_start(ap, methodID);
2251 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, ap);
2254 return jni_NewLocalRef(env, (jobject) o);
2258 jobject _Jv_JNI_CallStaticObjectMethodV(JNIEnv *env, jclass clazz,
2259 jmethodID methodID, va_list args)
2264 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodV(env=%p, clazz=%p, methodID=%p)", env, clazz, methodID));
2266 m = (methodinfo *) methodID;
2268 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, args);
2270 return jni_NewLocalRef(env, (jobject) o);
2274 jobject _Jv_JNI_CallStaticObjectMethodA(JNIEnv *env, jclass clazz,
2275 jmethodID methodID, const jvalue *args)
2280 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2282 m = (methodinfo *) methodID;
2284 o = _Jv_jni_CallObjectMethodA(NULL, NULL, m, args);
2286 return jni_NewLocalRef(env, (jobject) o);
2290 void _Jv_JNI_CallStaticVoidMethod(JNIEnv *env, jclass clazz,
2291 jmethodID methodID, ...)
2296 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2298 m = (methodinfo *) methodID;
2300 va_start(ap, methodID);
2301 _Jv_jni_CallVoidMethod(NULL, NULL, m, ap);
2306 void _Jv_JNI_CallStaticVoidMethodV(JNIEnv *env, jclass clazz,
2307 jmethodID methodID, va_list args)
2311 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodV(env=%p, clazz=%p, methodID=%p)", env, clazz, methodID));
2313 m = (methodinfo *) methodID;
2315 _Jv_jni_CallVoidMethod(NULL, NULL, m, args);
2319 void _Jv_JNI_CallStaticVoidMethodA(JNIEnv *env, jclass clazz,
2320 jmethodID methodID, const jvalue * args)
2324 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2326 m = (methodinfo *) methodID;
2328 _Jv_jni_CallVoidMethodA(NULL, NULL, m, args);
2332 /* Accessing Static Fields ****************************************************/
2334 /* GetStaticFieldID ************************************************************
2336 Returns the field ID for a static field of a class. The field is
2337 specified by its name and signature. The GetStatic<type>Field and
2338 SetStatic<type>Field families of accessor functions use field IDs
2339 to retrieve static fields.
2341 *******************************************************************************/
2343 jfieldID _Jv_JNI_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name,
2351 STATISTICS(jniinvokation());
2353 c = LLNI_classinfo_unwrap(clazz);
2355 uname = utf_new_char((char *) name);
2356 usig = utf_new_char((char *) sig);
2358 f = class_findfield(c, uname, usig);
2361 exceptions_throw_nosuchfielderror(c, uname);
2363 return (jfieldID) f;
2367 /* GetStatic<type>Field ********************************************************
2369 This family of accessor routines returns the value of a static
2372 *******************************************************************************/
2374 #define JNI_GET_STATIC_FIELD(name, type, field) \
2375 type _Jv_JNI_GetStatic##name##Field(JNIEnv *env, jclass clazz, \
2381 STATISTICS(jniinvokation()); \
2383 c = LLNI_classinfo_unwrap(clazz); \
2384 f = (fieldinfo *) fieldID; \
2386 if (!(c->state & CLASS_INITIALIZED)) \
2387 if (!initialize_class(c)) \
2390 return f->value->field; \
2393 JNI_GET_STATIC_FIELD(Boolean, jboolean, i)
2394 JNI_GET_STATIC_FIELD(Byte, jbyte, i)
2395 JNI_GET_STATIC_FIELD(Char, jchar, i)
2396 JNI_GET_STATIC_FIELD(Short, jshort, i)
2397 JNI_GET_STATIC_FIELD(Int, jint, i)
2398 JNI_GET_STATIC_FIELD(Long, jlong, l)
2399 JNI_GET_STATIC_FIELD(Float, jfloat, f)
2400 JNI_GET_STATIC_FIELD(Double, jdouble, d)
2403 jobject _Jv_JNI_GetStaticObjectField(JNIEnv *env, jclass clazz,
2410 STATISTICS(jniinvokation());
2412 c = LLNI_classinfo_unwrap(clazz);
2413 f = (fieldinfo *) fieldID;
2415 if (!(c->state & CLASS_INITIALIZED))
2416 if (!initialize_class(c))
2419 h = (java_handle_t*) LLNI_WRAP(f->value->a);
2421 return jni_NewLocalRef(env, (jobject) h);
2425 /* SetStatic<type>Field *******************************************************
2427 This family of accessor routines sets the value of a static field
2430 *******************************************************************************/
2432 #define JNI_SET_STATIC_FIELD(name, type, field) \
2433 void _Jv_JNI_SetStatic##name##Field(JNIEnv *env, jclass clazz, \
2440 STATISTICS(jniinvokation()); \
2442 c = LLNI_classinfo_unwrap(clazz); \
2443 f = (fieldinfo *) fieldID; \
2445 if (!(c->state & CLASS_INITIALIZED)) \
2446 if (!initialize_class(c)) \
2449 f->value->field = value; \
2451 if (f->flags & ACC_VOLATILE) \
2452 Atomic::memory_barrier(); \
2455 JNI_SET_STATIC_FIELD(Boolean, jboolean, i)
2456 JNI_SET_STATIC_FIELD(Byte, jbyte, i)
2457 JNI_SET_STATIC_FIELD(Char, jchar, i)
2458 JNI_SET_STATIC_FIELD(Short, jshort, i)
2459 JNI_SET_STATIC_FIELD(Int, jint, i)
2460 JNI_SET_STATIC_FIELD(Long, jlong, l)
2461 JNI_SET_STATIC_FIELD(Float, jfloat, f)
2462 JNI_SET_STATIC_FIELD(Double, jdouble, d)
2465 void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID,
2471 STATISTICS(jniinvokation());
2473 c = LLNI_classinfo_unwrap(clazz);
2474 f = (fieldinfo *) fieldID;
2476 if (!(c->state & CLASS_INITIALIZED))
2477 if (!initialize_class(c))
2480 f->value->a = LLNI_UNWRAP((java_handle_t *) value);
2482 if (f->flags & ACC_VOLATILE)
2483 Atomic::memory_barrier();
2487 /* String Operations **********************************************************/
2489 /* NewString *******************************************************************
2491 Create new java.lang.String object from an array of Unicode
2494 *******************************************************************************/
2496 jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len)
2498 TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len));
2508 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2509 for (jsize i = 0; i < len; i++)
2512 java_handle_t* h = builtin_new(class_java_lang_String);
2517 java_lang_String s(h, ca.get_handle(), len, 0);
2519 return (jstring) jni_NewLocalRef(env, (jobject) s.get_handle());
2523 static jchar emptyStringJ[]={0,0};
2525 /* GetStringLength *************************************************************
2527 Returns the length (the count of Unicode characters) of a Java
2530 *******************************************************************************/
2532 jsize jni_GetStringLength(JNIEnv *env, jstring str)
2534 TRACEJNICALLSENTER(("jni_GetStringLength(env=%p, str=%p)", env, str));
2536 java_lang_String s(str);
2537 jsize count = s.get_count();
2539 TRACEJNICALLSEXIT(("->%d)", count));
2545 /* GetStringChars **************************************************************
2547 Returns a pointer to the array of Unicode characters of the
2548 string. This pointer is valid until ReleaseStringChars() is called.
2550 *******************************************************************************/
2552 const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy)
2557 TRACEJNICALLS(("jni_GetStringChars(env=%p, str=%p, isCopy=%p)", env, str, isCopy));
2560 // FIXME This is really ugly.
2561 return emptyStringJ;
2563 java_lang_String s(str);
2565 CharArray ca(s.get_value());
2567 int32_t count = s.get_count();
2568 int32_t offset = s.get_offset();
2573 /* allocate memory */
2575 stringbuffer = MNEW(u2, count + 1);
2580 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2581 for (i = 0; i < count; i++)
2582 stringbuffer[i] = ptr[offset + i];
2584 /* terminate string */
2586 stringbuffer[i] = '\0';
2591 return (jchar*) stringbuffer;
2595 /* ReleaseStringChars **********************************************************
2597 Informs the VM that the native code no longer needs access to
2598 chars. The chars argument is a pointer obtained from string using
2601 *******************************************************************************/
2603 void _Jv_JNI_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)
2605 TRACEJNICALLS(("jni_ReleaseStringChars(env=%p, str=%p, chars=%p)", env, str, chars));
2608 if (chars == emptyStringJ)
2611 java_lang_String s(str);
2612 int32_t count = s.get_count();
2614 MFREE(((jchar*) chars), jchar, count + 1);
2618 /* NewStringUTF ****************************************************************
2620 Constructs a new java.lang.String object from an array of UTF-8
2623 *******************************************************************************/
2625 jstring jni_NewStringUTF(JNIEnv *env, const char *bytes)
2627 TRACEJNICALLS(("jni_NewStringUTF(env=%p, bytes=%s)", env, bytes));
2629 java_handle_t *h = javastring_safe_new_from_utf8(bytes);
2631 return (jstring) jni_NewLocalRef(env, (jobject) h);
2635 /****************** returns the utf8 length in bytes of a string *******************/
2637 jsize jni_GetStringUTFLength(JNIEnv *env, jstring string)
2639 TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string));
2641 java_lang_String s(string);
2642 CharArray ca(s.get_value());
2643 int32_t count = s.get_count();
2645 // FIXME GC critical section!
2646 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2647 int32_t length = u2_utflength(ptr, count);
2653 /* GetStringUTFChars ***********************************************************
2655 Returns a pointer to an array of UTF-8 characters of the
2656 string. This array is valid until it is released by
2657 ReleaseStringUTFChars().
2659 *******************************************************************************/
2661 const char *_Jv_JNI_GetStringUTFChars(JNIEnv *env, jstring string,
2666 STATISTICS(jniinvokation());
2674 u = javastring_toutf((java_handle_t *) string, false);
2683 /* ReleaseStringUTFChars *******************************************************
2685 Informs the VM that the native code no longer needs access to
2686 utf. The utf argument is a pointer derived from string using
2687 GetStringUTFChars().
2689 *******************************************************************************/
2691 void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf)
2693 STATISTICS(jniinvokation());
2695 /* XXX we don't release utf chars right now, perhaps that should be done
2696 later. Since there is always one reference the garbage collector will
2701 /* Array Operations ***********************************************************/
2703 /* GetArrayLength **************************************************************
2705 Returns the number of elements in the array.
2707 *******************************************************************************/
2709 jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array)
2711 TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array));
2715 jsize size = a.get_length();
2721 /* NewObjectArray **************************************************************
2723 Constructs a new array holding objects in class elementClass. All
2724 elements are initially set to initialElement.
2726 *******************************************************************************/
2728 jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length,
2729 jclass elementClass, jobject initialElement)
2735 STATISTICS(jniinvokation());
2737 c = LLNI_classinfo_unwrap(elementClass);
2738 o = (java_handle_t *) initialElement;
2741 exceptions_throw_negativearraysizeexception();
2745 ObjectArray oa(length, c);
2750 /* set all elements to initialElement */
2752 for (i = 0; i < length; i++)
2753 oa.set_element(i, o);
2755 return (jobjectArray) jni_NewLocalRef(env, (jobject) oa.get_handle());
2759 jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array,
2762 STATISTICS(jniinvokation());
2764 ObjectArray oa(array);
2766 if (index >= oa.get_length()) {
2767 exceptions_throw_arrayindexoutofboundsexception();
2771 java_handle_t* o = oa.get_element(index);
2773 return jni_NewLocalRef(env, (jobject) o);
2777 void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array,
2778 jsize index, jobject val)
2780 STATISTICS(jniinvokation());
2782 ObjectArray oa(array);
2784 if (index >= oa.get_length()) {
2785 exceptions_throw_arrayindexoutofboundsexception();
2789 /* check if the class of value is a subclass of the element class
2792 java_handle_t* o = (java_handle_t *) val;
2794 if (!builtin_canstore(oa.get_handle(), o))
2797 oa.set_element(index, o);
2801 #define JNI_NEW_ARRAY(name, type) \
2802 type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \
2804 STATISTICS(jniinvokation()); \
2807 exceptions_throw_negativearraysizeexception(); \
2811 name##Array a(len); \
2813 return (type) jni_NewLocalRef(env, \
2814 (jobject) a.get_handle()); \
2817 JNI_NEW_ARRAY(Boolean, jbooleanArray)
2818 JNI_NEW_ARRAY(Byte, jbyteArray)
2819 JNI_NEW_ARRAY(Char, jcharArray)
2820 JNI_NEW_ARRAY(Short, jshortArray)
2821 JNI_NEW_ARRAY(Int, jintArray)
2822 JNI_NEW_ARRAY(Long, jlongArray)
2823 JNI_NEW_ARRAY(Float, jfloatArray)
2824 JNI_NEW_ARRAY(Double, jdoubleArray)
2827 /* Get<PrimitiveType>ArrayElements *********************************************
2829 A family of functions that returns the body of the primitive array.
2831 *******************************************************************************/
2833 #define JNI_GET_ARRAY_ELEMENTS(name, type, intern) \
2834 type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \
2837 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \
2839 name##Array a(array); \
2842 *isCopy = JNI_FALSE; \
2844 return (type *) a.get_raw_data_ptr(); \
2847 JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean)
2848 JNI_GET_ARRAY_ELEMENTS(Byte, jbyte, byte)
2849 JNI_GET_ARRAY_ELEMENTS(Char, jchar, char)
2850 JNI_GET_ARRAY_ELEMENTS(Short, jshort, short)
2851 JNI_GET_ARRAY_ELEMENTS(Int, jint, int)
2852 JNI_GET_ARRAY_ELEMENTS(Long, jlong, long)
2853 JNI_GET_ARRAY_ELEMENTS(Float, jfloat, float)
2854 JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double)
2857 /* Release<PrimitiveType>ArrayElements *****************************************
2859 A family of functions that informs the VM that the native code no
2860 longer needs access to elems. The elems argument is a pointer
2861 derived from array using the corresponding
2862 Get<PrimitiveType>ArrayElements() function. If necessary, this
2863 function copies back all changes made to elems to the original
2866 *******************************************************************************/
2868 #define JNI_RELEASE_ARRAY_ELEMENTS(name, type, intern, intern2) \
2869 void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \
2870 type *elems, jint mode) \
2872 STATISTICS(jniinvokation()); \
2874 name##Array a(array); \
2876 if (elems != (type *) a.get_raw_data_ptr()) { \
2879 a.set_region(0, a.get_length(), (intern2 *) elems); \
2882 a.set_region(0, a.get_length(), (intern2 *) elems); \
2883 /* XXX TWISTI how should it be freed? */ \
2886 /* XXX TWISTI how should it be freed? */ \
2892 JNI_RELEASE_ARRAY_ELEMENTS(Boolean, jboolean, boolean, u1)
2893 JNI_RELEASE_ARRAY_ELEMENTS(Byte, jbyte, byte, s1)
2894 JNI_RELEASE_ARRAY_ELEMENTS(Char, jchar, char, u2)
2895 JNI_RELEASE_ARRAY_ELEMENTS(Short, jshort, short, s2)
2896 JNI_RELEASE_ARRAY_ELEMENTS(Int, jint, int, s4)
2897 JNI_RELEASE_ARRAY_ELEMENTS(Long, jlong, long, s8)
2898 JNI_RELEASE_ARRAY_ELEMENTS(Float, jfloat, float, float)
2899 JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double)
2902 /* Get<PrimitiveType>ArrayRegion **********************************************
2904 A family of functions that copies a region of a primitive array
2907 *******************************************************************************/
2909 #define JNI_GET_ARRAY_REGION(name, type, intern, intern2) \
2910 void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \
2911 jsize start, jsize len, type *buf) \
2913 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \
2915 name##Array a(array); \
2917 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2918 exceptions_throw_arrayindexoutofboundsexception(); \
2920 a.get_region(start, len, (intern2 *) buf); \
2923 JNI_GET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2924 JNI_GET_ARRAY_REGION(Byte, jbyte, byte, s1)
2925 JNI_GET_ARRAY_REGION(Char, jchar, char, u2)
2926 JNI_GET_ARRAY_REGION(Short, jshort, short, s2)
2927 JNI_GET_ARRAY_REGION(Int, jint, int, s4)
2928 JNI_GET_ARRAY_REGION(Long, jlong, long, s8)
2929 JNI_GET_ARRAY_REGION(Float, jfloat, float, float)
2930 JNI_GET_ARRAY_REGION(Double, jdouble, double, double)
2933 /* Set<PrimitiveType>ArrayRegion **********************************************
2935 A family of functions that copies back a region of a primitive
2936 array from a buffer.
2938 *******************************************************************************/
2940 #define JNI_SET_ARRAY_REGION(name, type, intern, intern2) \
2941 void _Jv_JNI_Set##name##ArrayRegion(JNIEnv *env, type##Array array, \
2942 jsize start, jsize len, const type *buf) \
2944 STATISTICS(jniinvokation()); \
2946 name##Array a(array); \
2948 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2949 exceptions_throw_arrayindexoutofboundsexception(); \
2951 a.set_region(start, len, (intern2 *) buf); \
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 jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods)
2978 TRACEJNICALLS(("jni_RegisterNatives(env=%p, clazz=%p, methods=%p, nMethods=%d)", env, clazz, methods, nMethods));
2980 classinfo* c = LLNI_classinfo_unwrap(clazz);
2982 NativeMethods& nm = VM::get_current()->get_nativemethods();
2983 nm.register_methods(c->name, methods, nMethods);
2989 /* UnregisterNatives ***********************************************************
2991 Unregisters native methods of a class. The class goes back to the
2992 state before it was linked or registered with its native method
2995 This function should not be used in normal native code. Instead, it
2996 provides special programs a way to reload and relink native
2999 *******************************************************************************/
3001 jint _Jv_JNI_UnregisterNatives(JNIEnv *env, jclass clazz)
3003 STATISTICS(jniinvokation());
3005 /* XXX TWISTI hmm, maybe we should not support that (like kaffe) */
3007 log_text("JNI-Call: UnregisterNatives: IMPLEMENT ME!!!");
3013 /* Monitor Operations *********************************************************/
3015 /* MonitorEnter ****************************************************************
3017 Enters the monitor associated with the underlying Java object
3020 *******************************************************************************/
3022 jint _Jv_JNI_MonitorEnter(JNIEnv *env, jobject obj)
3024 STATISTICS(jniinvokation());
3027 exceptions_throw_nullpointerexception();
3031 LOCK_MONITOR_ENTER(obj);
3037 /* MonitorExit *****************************************************************
3039 The current thread must be the owner of the monitor associated with
3040 the underlying Java object referred to by obj. The thread
3041 decrements the counter indicating the number of times it has
3042 entered this monitor. If the value of the counter becomes zero, the
3043 current thread releases the monitor.
3045 *******************************************************************************/
3047 jint _Jv_JNI_MonitorExit(JNIEnv *env, jobject obj)
3049 STATISTICS(jniinvokation());
3052 exceptions_throw_nullpointerexception();
3056 LOCK_MONITOR_EXIT(obj);
3062 /* JavaVM Interface ***********************************************************/
3064 /* GetJavaVM *******************************************************************
3066 Returns the Java VM interface (used in the Invocation API)
3067 associated with the current thread. The result is placed at the
3068 location pointed to by the second argument, vm.
3070 *******************************************************************************/
3072 jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm)
3074 STATISTICS(jniinvokation());
3076 *javavm = VM::get_current()->get_javavm();
3082 /* GetStringRegion *************************************************************
3084 Copies len number of Unicode characters beginning at offset start
3085 to the given buffer buf.
3087 Throws StringIndexOutOfBoundsException on index overflow.
3089 *******************************************************************************/
3091 void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf)
3093 java_lang_String s(str);
3094 CharArray ca(s.get_value());
3095 int32_t count = s.get_count();
3097 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3098 exceptions_throw_stringindexoutofboundsexception();
3102 ca.get_region(start, len, buf);
3106 /* GetStringUTFRegion **********************************************************
3108 Translates len number of Unicode characters beginning at offset
3109 start into UTF-8 format and place the result in the given buffer
3112 Throws StringIndexOutOfBoundsException on index overflow.
3114 *******************************************************************************/
3116 void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char *buf)
3118 TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf));
3120 java_lang_String s(str);
3122 CharArray ca(s.get_value());
3124 int32_t count = s.get_count();
3125 int32_t offset = s.get_offset();
3127 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3128 exceptions_throw_stringindexoutofboundsexception();
3135 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
3136 for (i = 0; i < len; i++)
3137 buf[i] = ptr[offset + start + i];
3143 /* GetPrimitiveArrayCritical ***************************************************
3145 Obtain a direct pointer to array elements.
3147 ATTENTION: Critical section keeps open when this function returns!
3148 See ReleasePrimitiveArrayCritical.
3150 *******************************************************************************/
3152 void* jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)
3156 arraydescriptor* ad;
3159 TRACEJNICALLS(("jni_GetPrimitiveArrayCritical(env=%p, array=%p, isCopy=%d)", env, array, isCopy));
3161 if (isCopy != NULL) {
3162 *isCopy = JNI_FALSE;
3165 LLNI_CRITICAL_START;
3167 h = (java_handle_t*) array;
3168 a = (java_array_t*) LLNI_UNWRAP(h);
3169 ad = a->objheader.vftbl->arraydesc;
3175 data = (void*) (((intptr_t) a) + ad->dataoffset);
3181 /* ReleasePrimitiveArrayCritical ***********************************************
3183 No specific documentation.
3185 ATTENTION: This function closes the critical section opened in
3186 GetPrimitiveArrayCritical!
3188 *******************************************************************************/
3190 void jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)
3192 TRACEJNICALLS(("jni_ReleasePrimitiveArrayCritical(env=%p, array=%p, carray=%p, mode=%d)", env, array, carray, mode));
3198 /* GetStringCritical ***********************************************************
3200 The semantics of these two functions are similar to the existing
3201 Get/ReleaseStringChars functions.
3203 *******************************************************************************/
3205 const jchar *_Jv_JNI_GetStringCritical(JNIEnv *env, jstring string,
3208 STATISTICS(jniinvokation());
3210 return jni_GetStringChars(env, string, isCopy);
3214 void _Jv_JNI_ReleaseStringCritical(JNIEnv *env, jstring string,
3215 const jchar *cstring)
3217 STATISTICS(jniinvokation());
3219 _Jv_JNI_ReleaseStringChars(env, string, cstring);
3223 jweak _Jv_JNI_NewWeakGlobalRef(JNIEnv* env, jobject obj)
3225 TRACEJNICALLS(("_Jv_JNI_NewWeakGlobalRef(env=%p, obj=%p): IMPLEMENT ME!", env, obj));
3231 void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
3233 TRACEJNICALLS(("_Jv_JNI_DeleteWeakGlobalRef(env=%p, ref=%p): IMPLEMENT ME", env, ref));
3237 /* NewGlobalRef ****************************************************************
3239 Creates a new global reference to the object referred to by the obj
3242 *******************************************************************************/
3244 jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
3246 hashtable_global_ref_entry *gre;
3247 u4 key; /* hashkey */
3248 u4 slot; /* slot in hashtable */
3251 TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
3253 o = (java_handle_t *) obj;
3255 hashtable_global_ref->mutex->lock();
3257 LLNI_CRITICAL_START;
3259 /* normally addresses are aligned to 4, 8 or 16 bytes */
3261 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3262 slot = key & (hashtable_global_ref->size - 1);
3263 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3265 /* search external hash chain for the entry */
3268 if (gre->o == LLNI_DIRECT(o)) {
3269 /* global object found, increment the reference */
3276 gre = gre->hashlink; /* next element in external chain */
3281 /* global ref not found, create a new one */
3284 gre = (hashtable_global_ref_entry*) heap_alloc_uncollectable(sizeof(hashtable_global_ref_entry));
3286 #if defined(ENABLE_GC_CACAO)
3287 /* register global ref with the GC */
3289 gc_reference_register(&(gre->o), GC_REFTYPE_JNI_GLOBALREF);
3292 LLNI_CRITICAL_START;
3294 gre->o = LLNI_DIRECT(o);
3299 /* insert entry into hashtable */
3301 gre->hashlink = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3303 hashtable_global_ref->ptr[slot] = gre;
3305 /* update number of hashtable-entries */
3307 hashtable_global_ref->entries++;
3310 hashtable_global_ref->mutex->unlock();
3312 #if defined(ENABLE_HANDLES)
3320 /* DeleteGlobalRef *************************************************************
3322 Deletes the global reference pointed to by globalRef.
3324 *******************************************************************************/
3326 void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
3328 hashtable_global_ref_entry *gre;
3329 hashtable_global_ref_entry *prevgre;
3330 u4 key; /* hashkey */
3331 u4 slot; /* slot in hashtable */
3334 TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
3336 o = (java_handle_t *) globalRef;
3338 hashtable_global_ref->mutex->lock();
3340 LLNI_CRITICAL_START;
3342 /* normally addresses are aligned to 4, 8 or 16 bytes */
3344 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3345 slot = key & (hashtable_global_ref->size - 1);
3346 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3348 /* initialize prevgre */
3352 /* search external hash chain for the entry */
3355 if (gre->o == LLNI_DIRECT(o)) {
3356 /* global object found, decrement the reference count */
3360 /* if reference count is 0, remove the entry */
3362 if (gre->refs == 0) {
3363 /* special handling if it's the first in the chain */
3365 if (prevgre == NULL)
3366 hashtable_global_ref->ptr[slot] = gre->hashlink;
3368 prevgre->hashlink = gre->hashlink;
3370 #if defined(ENABLE_GC_CACAO)
3371 /* unregister global ref with the GC */
3373 gc_reference_unregister(&(gre->o));
3381 hashtable_global_ref->mutex->unlock();
3386 prevgre = gre; /* save current pointer for removal */
3387 gre = gre->hashlink; /* next element in external chain */
3390 log_println("jni_DeleteGlobalRef: Global reference not found.");
3394 hashtable_global_ref->mutex->unlock();
3398 /* ExceptionCheck **************************************************************
3400 Returns JNI_TRUE when there is a pending exception; otherwise,
3403 *******************************************************************************/
3405 jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
3409 STATISTICS(jniinvokation());
3411 o = exceptions_get_exception();
3413 return (o != NULL) ? JNI_TRUE : JNI_FALSE;
3417 /* New JNI 1.4 functions ******************************************************/
3419 /* NewDirectByteBuffer *********************************************************
3421 Allocates and returns a direct java.nio.ByteBuffer referring to the
3422 block of memory starting at the memory address address and
3423 extending capacity bytes.
3425 *******************************************************************************/
3427 jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
3429 #if defined(ENABLE_JAVASE)
3430 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3431 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3433 // Allocate a gnu.classpath.Pointer{32,64} object.
3435 # if SIZEOF_VOID_P == 8
3436 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer64);
3438 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer32);
3444 gnu_classpath_Pointer p(h, address);
3446 // Create a java.nio.DirectByteBufferImpl$ReadWrite object.
3448 java_handle_t* nbuf =
3449 (java_handle_t*) jni_NewObject(env, (jclass) class_java_nio_DirectByteBufferImpl_ReadWrite,
3450 (jmethodID) dbbirw_init, NULL, p.get_handle(),
3451 (jint) capacity, (jint) capacity, (jint) 0);
3453 // Add a local reference and return the value.
3455 TRACEJNICALLSEXIT(("->%p", nbuf));
3457 return jni_NewLocalRef(env, (jobject) nbuf);
3459 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3465 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3467 /* Be paranoid about address sign-extension. */
3469 addr = (int64_t) ((uintptr_t) address);
3470 cap = (int32_t) capacity;
3472 o = jni_NewObject(env, (jclass) class_java_nio_DirectByteBuffer,
3473 (jmethodID) dbb_init, addr, cap);
3475 /* Add local reference and return the value. */
3477 TRACEJNICALLSEXIT(("->%p", o));
3479 return jni_NewLocalRef(env, o);
3482 # error unknown classpath configuration
3486 vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
3488 /* keep compiler happy */
3495 /* GetDirectBufferAddress ******************************************************
3497 Fetches and returns the starting address of the memory region
3498 referenced by the given direct java.nio.Buffer.
3500 *******************************************************************************/
3502 void* jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3504 #if defined(ENABLE_JAVASE)
3505 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3507 TRACEJNICALLSENTER(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3509 /* Prevent compiler warning. */
3511 java_handle_t* h = (java_handle_t *) buf;
3513 if ((h != NULL) && !builtin_instanceof(h, class_java_nio_Buffer))
3516 java_nio_DirectByteBufferImpl dbb(buf);
3517 java_handle_t* address = dbb.get_address();
3519 if (address == NULL) {
3520 TRACEJNICALLSEXIT(("->%p", NULL));
3524 gnu_classpath_Pointer p(address);
3525 void* data = p.get_data();
3527 TRACEJNICALLSEXIT(("->%p", data));
3531 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3533 TRACEJNICALLS(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3535 java_nio_Buffer jnb(buf);
3537 if (jnb.is_non_null() && !builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3540 void* address = jnb.get_address();
3545 # error unknown classpath configuration
3550 vm_abort("jni_GetDirectBufferAddress: Not implemented in this configuration.");
3552 // Keep compiler happy.
3559 /* GetDirectBufferCapacity *****************************************************
3561 Fetches and returns the capacity in bytes of the memory region
3562 referenced by the given direct java.nio.Buffer.
3564 *******************************************************************************/
3566 jlong jni_GetDirectBufferCapacity(JNIEnv* env, jobject buf)
3568 #if defined(ENABLE_JAVASE)
3569 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3570 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3572 java_handle_t* h = (java_handle_t *) buf;
3574 if (!builtin_instanceof(h, class_java_nio_DirectByteBufferImpl))
3577 java_nio_Buffer b(h);
3578 jlong capacity = b.get_cap();
3581 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3583 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3585 java_nio_Buffer jnb(buf);
3587 if (!builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3590 jlong capacity = jnb.get_capacity();
3595 # error unknown classpath configuration
3599 vm_abort("jni_GetDirectBufferCapacity: not implemented in this configuration");
3601 // Keep compiler happy.
3608 /* GetObjectRefType ************************************************************
3610 Returns the type of the object referred to by the obj argument. The
3611 argument obj can either be a local, global or weak global
3614 *******************************************************************************/
3616 jobjectRefType jni_GetObjectRefType(JNIEnv *env, jobject obj)
3618 log_println("jni_GetObjectRefType: IMPLEMENT ME!");
3620 return (jobjectRefType) NULL;
3624 /* DestroyJavaVM ***************************************************************
3626 Unloads a Java VM and reclaims its resources. Only the main thread
3627 can unload the VM. The system waits until the main thread is only
3628 remaining user thread before it destroys the VM.
3630 *******************************************************************************/
3632 jint _Jv_JNI_DestroyJavaVM(JavaVM *javavm)
3636 TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(javavm=%p)", javavm));
3638 if (VM::get_current()->is_created() == false)
3641 status = vm_destroy(javavm);
3647 /* AttachCurrentThread *********************************************************
3649 Attaches the current thread to a Java VM. Returns a JNI interface
3650 pointer in the JNIEnv argument.
3652 Trying to attach a thread that is already attached is a no-op.
3654 A native thread cannot be attached simultaneously to two Java VMs.
3656 When a thread is attached to the VM, the context class loader is
3657 the bootstrap loader.
3659 *******************************************************************************/
3661 static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
3663 #if defined(ENABLE_THREADS)
3664 JavaVMAttachArgs *vm_aargs;
3667 /* If the current thread has already been attached, this operation
3670 result = thread_current_is_attached();
3672 if (result == true) {
3673 *p_env = VM::get_current()->get_jnienv();
3677 vm_aargs = (JavaVMAttachArgs *) thr_args;
3679 if (vm_aargs != NULL) {
3680 if ((vm_aargs->version != JNI_VERSION_1_2) &&
3681 (vm_aargs->version != JNI_VERSION_1_4))
3682 return JNI_EVERSION;
3685 if (!thread_attach_current_external_thread(vm_aargs, false))
3688 if (!localref_table_init())
3692 *p_env = VM::get_current()->get_jnienv();
3698 jint jni_AttachCurrentThread(JavaVM *javavm, void **p_env, void *thr_args)
3702 TRACEJNICALLS(("jni_AttachCurrentThread(javavm=%p, p_env=%p, thr_args=%p)", javavm, p_env, thr_args));
3704 if (VM::get_current()->is_created() == false)
3707 result = jni_attach_current_thread(p_env, thr_args, false);
3713 /* DetachCurrentThread *********************************************************
3715 Detaches the current thread from a Java VM. All Java monitors held
3716 by this thread are released. All Java threads waiting for this
3717 thread to die are notified.
3719 In JDK 1.1, the main thread cannot be detached from the VM. It must
3720 call DestroyJavaVM to unload the entire VM.
3722 In the JDK, the main thread can be detached from the VM.
3724 The main thread, which is the thread that created the Java VM,
3725 cannot be detached from the VM. Instead, the main thread must call
3726 JNI_DestroyJavaVM() to unload the entire VM.
3728 *******************************************************************************/
3730 jint jni_DetachCurrentThread(JavaVM *vm)
3732 #if defined(ENABLE_THREADS)
3735 TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
3737 /* If the current thread has already been detached, this operation
3740 result = thread_current_is_attached();
3742 if (result == false)
3745 /* We need to pop all frames before we can destroy the table. */
3747 localref_frame_pop_all();
3749 if (!localref_table_destroy())
3752 if (!thread_detach_current_external_thread())
3760 /* GetEnv **********************************************************************
3762 If the current thread is not attached to the VM, sets *env to NULL,
3763 and returns JNI_EDETACHED. If the specified version is not
3764 supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise,
3765 sets *env to the appropriate interface, and returns JNI_OK.
3767 *******************************************************************************/
3769 jint jni_GetEnv(JavaVM *javavm, void **env, jint version)
3771 TRACEJNICALLS(("jni_GetEnv(javavm=%p, env=%p, version=%d)", javavm, env, version));
3773 if (VM::get_current()->is_created() == false) {
3775 return JNI_EDETACHED;
3778 #if defined(ENABLE_THREADS)
3779 if (thread_get_current() == NULL) {
3782 return JNI_EDETACHED;
3786 /* Check the JNI version. */
3788 if (jni_version_check(version) == true) {
3789 *env = VM::get_current()->get_jnienv();
3793 #if defined(ENABLE_JVMTI)
3794 if ((version & JVMTI_VERSION_MASK_INTERFACE_TYPE)
3795 == JVMTI_VERSION_INTERFACE_JVMTI) {
3797 *env = (void *) jvmti_new_environment();
3806 return JNI_EVERSION;
3810 /* AttachCurrentThreadAsDaemon *************************************************
3812 Same semantics as AttachCurrentThread, but the newly-created
3813 java.lang.Thread instance is a daemon.
3815 If the thread has already been attached via either
3816 AttachCurrentThread or AttachCurrentThreadAsDaemon, this routine
3817 simply sets the value pointed to by penv to the JNIEnv of the
3818 current thread. In this case neither AttachCurrentThread nor this
3819 routine have any effect on the daemon status of the thread.
3821 *******************************************************************************/
3823 jint jni_AttachCurrentThreadAsDaemon(JavaVM *javavm, void **penv, void *args)
3827 TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(javavm=%p, penv=%p, args=%p)", javavm, penv, args));
3829 if (VM::get_current()->is_created() == false)
3832 result = jni_attach_current_thread(penv, args, true);
3838 /* JNI invocation table *******************************************************/
3840 const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
3845 _Jv_JNI_DestroyJavaVM,
3846 jni_AttachCurrentThread,
3847 jni_DetachCurrentThread,
3849 jni_AttachCurrentThreadAsDaemon
3853 /* JNI function table *********************************************************/
3855 struct JNINativeInterface_ _Jv_JNINativeInterface = {
3864 jni_FromReflectedMethod,
3865 jni_FromReflectedField,
3866 jni_ToReflectedMethod,
3868 _Jv_JNI_IsAssignableFrom,
3869 _Jv_JNI_ToReflectedField,
3873 _Jv_JNI_ExceptionOccurred,
3874 jni_ExceptionDescribe,
3881 jni_DeleteGlobalRef,
3883 _Jv_JNI_IsSameObject,
3885 jni_EnsureLocalCapacity,
3887 _Jv_JNI_AllocObject,
3893 _Jv_JNI_IsInstanceOf,
3895 _Jv_JNI_GetMethodID,
3897 _Jv_JNI_CallObjectMethod,
3898 _Jv_JNI_CallObjectMethodV,
3899 _Jv_JNI_CallObjectMethodA,
3900 _Jv_JNI_CallBooleanMethod,
3901 _Jv_JNI_CallBooleanMethodV,
3902 _Jv_JNI_CallBooleanMethodA,
3903 _Jv_JNI_CallByteMethod,
3904 _Jv_JNI_CallByteMethodV,
3905 _Jv_JNI_CallByteMethodA,
3906 _Jv_JNI_CallCharMethod,
3907 _Jv_JNI_CallCharMethodV,
3908 _Jv_JNI_CallCharMethodA,
3909 _Jv_JNI_CallShortMethod,
3910 _Jv_JNI_CallShortMethodV,
3911 _Jv_JNI_CallShortMethodA,
3912 _Jv_JNI_CallIntMethod,
3913 _Jv_JNI_CallIntMethodV,
3914 _Jv_JNI_CallIntMethodA,
3915 _Jv_JNI_CallLongMethod,
3916 _Jv_JNI_CallLongMethodV,
3917 _Jv_JNI_CallLongMethodA,
3918 _Jv_JNI_CallFloatMethod,
3919 _Jv_JNI_CallFloatMethodV,
3920 _Jv_JNI_CallFloatMethodA,
3921 _Jv_JNI_CallDoubleMethod,
3922 _Jv_JNI_CallDoubleMethodV,
3923 _Jv_JNI_CallDoubleMethodA,
3924 _Jv_JNI_CallVoidMethod,
3925 _Jv_JNI_CallVoidMethodV,
3926 _Jv_JNI_CallVoidMethodA,
3928 _Jv_JNI_CallNonvirtualObjectMethod,
3929 _Jv_JNI_CallNonvirtualObjectMethodV,
3930 _Jv_JNI_CallNonvirtualObjectMethodA,
3931 _Jv_JNI_CallNonvirtualBooleanMethod,
3932 _Jv_JNI_CallNonvirtualBooleanMethodV,
3933 _Jv_JNI_CallNonvirtualBooleanMethodA,
3934 _Jv_JNI_CallNonvirtualByteMethod,
3935 _Jv_JNI_CallNonvirtualByteMethodV,
3936 _Jv_JNI_CallNonvirtualByteMethodA,
3937 _Jv_JNI_CallNonvirtualCharMethod,
3938 _Jv_JNI_CallNonvirtualCharMethodV,
3939 _Jv_JNI_CallNonvirtualCharMethodA,
3940 _Jv_JNI_CallNonvirtualShortMethod,
3941 _Jv_JNI_CallNonvirtualShortMethodV,
3942 _Jv_JNI_CallNonvirtualShortMethodA,
3943 _Jv_JNI_CallNonvirtualIntMethod,
3944 _Jv_JNI_CallNonvirtualIntMethodV,
3945 _Jv_JNI_CallNonvirtualIntMethodA,
3946 _Jv_JNI_CallNonvirtualLongMethod,
3947 _Jv_JNI_CallNonvirtualLongMethodV,
3948 _Jv_JNI_CallNonvirtualLongMethodA,
3949 _Jv_JNI_CallNonvirtualFloatMethod,
3950 _Jv_JNI_CallNonvirtualFloatMethodV,
3951 _Jv_JNI_CallNonvirtualFloatMethodA,
3952 _Jv_JNI_CallNonvirtualDoubleMethod,
3953 _Jv_JNI_CallNonvirtualDoubleMethodV,
3954 _Jv_JNI_CallNonvirtualDoubleMethodA,
3955 _Jv_JNI_CallNonvirtualVoidMethod,
3956 _Jv_JNI_CallNonvirtualVoidMethodV,
3957 _Jv_JNI_CallNonvirtualVoidMethodA,
3961 _Jv_JNI_GetObjectField,
3962 _Jv_JNI_GetBooleanField,
3963 _Jv_JNI_GetByteField,
3964 _Jv_JNI_GetCharField,
3965 _Jv_JNI_GetShortField,
3966 _Jv_JNI_GetIntField,
3967 _Jv_JNI_GetLongField,
3968 _Jv_JNI_GetFloatField,
3969 _Jv_JNI_GetDoubleField,
3970 _Jv_JNI_SetObjectField,
3971 _Jv_JNI_SetBooleanField,
3972 _Jv_JNI_SetByteField,
3973 _Jv_JNI_SetCharField,
3974 _Jv_JNI_SetShortField,
3975 _Jv_JNI_SetIntField,
3976 _Jv_JNI_SetLongField,
3977 _Jv_JNI_SetFloatField,
3978 _Jv_JNI_SetDoubleField,
3980 _Jv_JNI_GetStaticMethodID,
3982 _Jv_JNI_CallStaticObjectMethod,
3983 _Jv_JNI_CallStaticObjectMethodV,
3984 _Jv_JNI_CallStaticObjectMethodA,
3985 _Jv_JNI_CallStaticBooleanMethod,
3986 _Jv_JNI_CallStaticBooleanMethodV,
3987 _Jv_JNI_CallStaticBooleanMethodA,
3988 _Jv_JNI_CallStaticByteMethod,
3989 _Jv_JNI_CallStaticByteMethodV,
3990 _Jv_JNI_CallStaticByteMethodA,
3991 _Jv_JNI_CallStaticCharMethod,
3992 _Jv_JNI_CallStaticCharMethodV,
3993 _Jv_JNI_CallStaticCharMethodA,
3994 _Jv_JNI_CallStaticShortMethod,
3995 _Jv_JNI_CallStaticShortMethodV,
3996 _Jv_JNI_CallStaticShortMethodA,
3997 _Jv_JNI_CallStaticIntMethod,
3998 _Jv_JNI_CallStaticIntMethodV,
3999 _Jv_JNI_CallStaticIntMethodA,
4000 _Jv_JNI_CallStaticLongMethod,
4001 _Jv_JNI_CallStaticLongMethodV,
4002 _Jv_JNI_CallStaticLongMethodA,
4003 _Jv_JNI_CallStaticFloatMethod,
4004 _Jv_JNI_CallStaticFloatMethodV,
4005 _Jv_JNI_CallStaticFloatMethodA,
4006 _Jv_JNI_CallStaticDoubleMethod,
4007 _Jv_JNI_CallStaticDoubleMethodV,
4008 _Jv_JNI_CallStaticDoubleMethodA,
4009 _Jv_JNI_CallStaticVoidMethod,
4010 _Jv_JNI_CallStaticVoidMethodV,
4011 _Jv_JNI_CallStaticVoidMethodA,
4013 _Jv_JNI_GetStaticFieldID,
4015 _Jv_JNI_GetStaticObjectField,
4016 _Jv_JNI_GetStaticBooleanField,
4017 _Jv_JNI_GetStaticByteField,
4018 _Jv_JNI_GetStaticCharField,
4019 _Jv_JNI_GetStaticShortField,
4020 _Jv_JNI_GetStaticIntField,
4021 _Jv_JNI_GetStaticLongField,
4022 _Jv_JNI_GetStaticFloatField,
4023 _Jv_JNI_GetStaticDoubleField,
4024 _Jv_JNI_SetStaticObjectField,
4025 _Jv_JNI_SetStaticBooleanField,
4026 _Jv_JNI_SetStaticByteField,
4027 _Jv_JNI_SetStaticCharField,
4028 _Jv_JNI_SetStaticShortField,
4029 _Jv_JNI_SetStaticIntField,
4030 _Jv_JNI_SetStaticLongField,
4031 _Jv_JNI_SetStaticFloatField,
4032 _Jv_JNI_SetStaticDoubleField,
4035 jni_GetStringLength,
4037 _Jv_JNI_ReleaseStringChars,
4040 jni_GetStringUTFLength,
4041 _Jv_JNI_GetStringUTFChars,
4042 _Jv_JNI_ReleaseStringUTFChars,
4044 _Jv_JNI_GetArrayLength,
4046 _Jv_JNI_NewObjectArray,
4047 _Jv_JNI_GetObjectArrayElement,
4048 _Jv_JNI_SetObjectArrayElement,
4050 _Jv_JNI_NewBooleanArray,
4051 _Jv_JNI_NewByteArray,
4052 _Jv_JNI_NewCharArray,
4053 _Jv_JNI_NewShortArray,
4054 _Jv_JNI_NewIntArray,
4055 _Jv_JNI_NewLongArray,
4056 _Jv_JNI_NewFloatArray,
4057 _Jv_JNI_NewDoubleArray,
4059 _Jv_JNI_GetBooleanArrayElements,
4060 _Jv_JNI_GetByteArrayElements,
4061 _Jv_JNI_GetCharArrayElements,
4062 _Jv_JNI_GetShortArrayElements,
4063 _Jv_JNI_GetIntArrayElements,
4064 _Jv_JNI_GetLongArrayElements,
4065 _Jv_JNI_GetFloatArrayElements,
4066 _Jv_JNI_GetDoubleArrayElements,
4068 _Jv_JNI_ReleaseBooleanArrayElements,
4069 _Jv_JNI_ReleaseByteArrayElements,
4070 _Jv_JNI_ReleaseCharArrayElements,
4071 _Jv_JNI_ReleaseShortArrayElements,
4072 _Jv_JNI_ReleaseIntArrayElements,
4073 _Jv_JNI_ReleaseLongArrayElements,
4074 _Jv_JNI_ReleaseFloatArrayElements,
4075 _Jv_JNI_ReleaseDoubleArrayElements,
4077 _Jv_JNI_GetBooleanArrayRegion,
4078 _Jv_JNI_GetByteArrayRegion,
4079 _Jv_JNI_GetCharArrayRegion,
4080 _Jv_JNI_GetShortArrayRegion,
4081 _Jv_JNI_GetIntArrayRegion,
4082 _Jv_JNI_GetLongArrayRegion,
4083 _Jv_JNI_GetFloatArrayRegion,
4084 _Jv_JNI_GetDoubleArrayRegion,
4085 _Jv_JNI_SetBooleanArrayRegion,
4086 _Jv_JNI_SetByteArrayRegion,
4087 _Jv_JNI_SetCharArrayRegion,
4088 _Jv_JNI_SetShortArrayRegion,
4089 _Jv_JNI_SetIntArrayRegion,
4090 _Jv_JNI_SetLongArrayRegion,
4091 _Jv_JNI_SetFloatArrayRegion,
4092 _Jv_JNI_SetDoubleArrayRegion,
4094 jni_RegisterNatives,
4095 _Jv_JNI_UnregisterNatives,
4097 _Jv_JNI_MonitorEnter,
4098 _Jv_JNI_MonitorExit,
4102 /* New JNI 1.2 functions. */
4104 jni_GetStringRegion,
4105 jni_GetStringUTFRegion,
4107 jni_GetPrimitiveArrayCritical,
4108 jni_ReleasePrimitiveArrayCritical,
4110 _Jv_JNI_GetStringCritical,
4111 _Jv_JNI_ReleaseStringCritical,
4113 _Jv_JNI_NewWeakGlobalRef,
4114 _Jv_JNI_DeleteWeakGlobalRef,
4116 _Jv_JNI_ExceptionCheck,
4118 /* New JNI 1.4 functions. */
4120 jni_NewDirectByteBuffer,
4121 jni_GetDirectBufferAddress,
4122 jni_GetDirectBufferCapacity,
4124 /* New JNI 1.6 functions. */
4126 jni_GetObjectRefType
4130 /* Invocation API Functions ***************************************************/
4132 /* JNI_GetDefaultJavaVMInitArgs ************************************************
4134 Returns a default configuration for the Java VM.
4136 *******************************************************************************/
4138 jint JNI_GetDefaultJavaVMInitArgs(void *vm_args)
4140 JavaVMInitArgs *_vm_args;
4142 _vm_args = (JavaVMInitArgs *) vm_args;
4144 /* GNU classpath currently supports JNI 1.2 */
4146 switch (_vm_args->version) {
4147 case JNI_VERSION_1_1:
4148 _vm_args->version = JNI_VERSION_1_1;
4151 case JNI_VERSION_1_2:
4152 case JNI_VERSION_1_4:
4153 _vm_args->ignoreUnrecognized = JNI_FALSE;
4154 _vm_args->options = NULL;
4155 _vm_args->nOptions = 0;
4158 case JNI_VERSION_CACAO:
4159 // We reveal ourselves by accepting this version number,
4160 // this actually means we are using the supported JNI version.
4161 _vm_args->version = JNI_VERSION_SUPPORTED;
4172 /* JNI_GetCreatedJavaVMs *******************************************************
4174 Returns all Java VMs that have been created. Pointers to VMs are written in
4175 the buffer vmBuf in the order they are created. At most bufLen number of
4176 entries will be written. The total number of created VMs is returned in
4179 *******************************************************************************/
4181 jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
4183 TRACEJNICALLS(("JNI_GetCreatedJavaVMs(vmBuf=%p, jsize=%d, jsize=%p)", vmBuf, bufLen, nVMs));
4188 // We currently only support 1 VM running.
4190 vmBuf[0] = VM::get_current()->get_javavm();
4197 /* JNI_CreateJavaVM ************************************************************
4199 Loads and initializes a Java VM. The current thread becomes the main thread.
4200 Sets the env argument to the JNI interface pointer of the main thread.
4202 *******************************************************************************/
4204 jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
4206 TRACEJNICALLS(("JNI_CreateJavaVM(p_vm=%p, p_env=%p, vm_args=%p)", p_vm, p_env, vm_args));
4208 /* actually create the JVM */
4210 if (!VM_create(p_vm, p_env, vm_args))
4220 * These are local overrides for various environment variables in Emacs.
4221 * Please do not remove this and leave it at the end of the file, where
4222 * Emacs will automagically detect them.
4223 * ---------------------------------------------------------------------
4226 * indent-tabs-mode: t
4230 * vim:noexpandtab:sw=4:ts=4: