1 /* src/native/jni.cpp - implementation of the Java Native Interface functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "mm/memory.h"
37 #include "native/jni.hpp"
38 #include "native/llni.h"
39 #include "native/localref.h"
40 #include "native/native.h"
42 #if defined(ENABLE_JVMTI)
43 # include "native/jvmti/cacaodbg.h"
46 #include "threads/lock-common.h"
47 #include "threads/thread.hpp"
49 #include "toolbox/logging.h"
52 #include "vm/builtin.h"
53 #include "vm/exceptions.hpp"
54 #include "vm/global.h"
55 #include "vm/globals.hpp"
56 #include "vm/initialize.h"
57 #include "vm/javaobjects.hpp"
58 #include "vm/loader.h"
59 #include "vm/options.h"
60 #include "vm/primitive.hpp"
61 #include "vm/resolve.h"
62 #include "vm/statistics.h"
63 #include "vm/string.hpp"
66 #include "vm/jit/argument.h"
67 #include "vm/jit/asmpart.h"
68 #include "vm/jit/jit.h"
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 /* We support JNI 1.6. */
798 return JNI_VERSION_1_6;
802 /* Class Operations ***********************************************************/
804 /* DefineClass *****************************************************************
806 Loads a class from a buffer of raw class data. The buffer
807 containing the raw class data is not referenced by the VM after the
808 DefineClass call returns, and it may be discarded if desired.
810 *******************************************************************************/
812 jclass jni_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize bufLen)
814 #if defined(ENABLE_JAVASE)
820 TRACEJNICALLS(("jni_DefineClass(env=%p, name=%s, loader=%p, buf=%p, bufLen=%d)", env, name, loader, buf, bufLen));
822 u = utf_new_char(name);
823 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
825 c = class_define(u, cl, bufLen, (uint8_t *) buf, NULL);
827 h = LLNI_classinfo_wrap(c);
829 return (jclass) jni_NewLocalRef(env, (jobject) h);
831 vm_abort("jni_DefineClass: Not implemented in this configuration");
833 // Keep compiler happy.
840 /* FindClass *******************************************************************
842 This function loads a locally-defined class. It searches the
843 directories and zip files specified by the CLASSPATH environment
844 variable for the class with the specified name.
846 *******************************************************************************/
848 jclass jni_FindClass(JNIEnv *env, const char *name)
850 #if defined(ENABLE_JAVASE)
857 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
859 /* FIXME If name is NULL we have a problem here. */
861 u = utf_new_char_classname((char *) name);
863 if ((u == NULL) /*|| (int)strlen(name) > symbolOopDesc::max_length() */) {
864 exceptions_throw_noclassdeffounderror(u);
868 /* Check stacktrace for classloader, if one found use it,
869 otherwise use the system classloader. */
871 /* Quote from the JNI documentation:
873 In the Java 2 Platform, FindClass locates the class loader
874 associated with the current native method. If the native code
875 belongs to a system class, no class loader will be
876 involved. Otherwise, the proper class loader will be invoked to
877 load and link the named class. When FindClass is called through
878 the Invocation Interface, there is no current native method or
879 its associated class loader. In that case, the result of
880 ClassLoader.getBaseClassLoader is used." */
882 cc = stacktrace_get_current_class();
885 c = load_class_from_sysloader(u);
887 c = load_class_from_classloader(u, cc->classloader);
890 resolve_handle_pending_exception(true);
897 h = LLNI_classinfo_wrap(c);
899 return (jclass) jni_NewLocalRef(env, (jobject) h);
901 #elif defined(ENABLE_JAVAME_CLDC1_1)
906 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
908 u = utf_new_char_classname((char *) name);
909 c = load_class_bootstrap(u);
912 resolve_handle_pending_exception(true);
919 return (jclass) jni_NewLocalRef(env, (jobject) c);
922 vm_abort("jni_FindClass: not implemented in this configuration");
924 /* keep compiler happy */
931 /* GetSuperclass ***************************************************************
933 If clazz represents any class other than the class Object, then
934 this function returns the object that represents the superclass of
935 the class specified by clazz.
937 *******************************************************************************/
939 jclass jni_GetSuperclass(JNIEnv *env, jclass sub)
944 TRACEJNICALLS(("jni_GetSuperclass(env=%p, sub=%p)", env, sub));
946 c = LLNI_classinfo_unwrap(sub);
951 super = class_get_superclass(c);
953 java_handle_t* h = LLNI_classinfo_wrap(super);
955 return (jclass) jni_NewLocalRef(env, (jobject) h);
959 /* IsAssignableFrom ************************************************************
961 Determines whether an object of sub can be safely cast to sup.
963 *******************************************************************************/
965 jboolean _Jv_JNI_IsAssignableFrom(JNIEnv *env, jclass sub, jclass sup)
970 TRACEJNICALLS(("_Jv_JNI_IsAssignableFrom(env=%p, sub=%p, sup=%p)", env, sub, sup));
972 to = (classinfo *) sup;
973 from = (classinfo *) sub;
975 return class_is_assignable_from(to, from);
979 /* Throw ***********************************************************************
981 Causes a java.lang.Throwable object to be thrown.
983 *******************************************************************************/
985 jint _Jv_JNI_Throw(JNIEnv *env, jthrowable obj)
989 STATISTICS(jniinvokation());
991 o = (java_handle_t *) obj;
993 exceptions_set_exception(o);
999 /* ThrowNew ********************************************************************
1001 Constructs an exception object from the specified class with the
1002 message specified by message and causes that exception to be
1005 *******************************************************************************/
1007 jint _Jv_JNI_ThrowNew(JNIEnv* env, jclass clazz, const char *msg)
1013 STATISTICS(jniinvokation());
1015 c = LLNI_classinfo_unwrap(clazz);
1018 s = javastring_new_from_utf_string(msg);
1020 /* instantiate exception object */
1022 o = native_new_and_init_string(c, s);
1027 exceptions_set_exception(o);
1033 /* ExceptionOccurred ***********************************************************
1035 Determines if an exception is being thrown. The exception stays
1036 being thrown until either the native code calls ExceptionClear(),
1037 or the Java code handles the exception.
1039 *******************************************************************************/
1041 jthrowable _Jv_JNI_ExceptionOccurred(JNIEnv *env)
1045 TRACEJNICALLS(("_Jv_JNI_ExceptionOccurred(env=%p)", env));
1047 o = exceptions_get_exception();
1049 return (jthrowable) jni_NewLocalRef(env, (jthrowable) o);
1053 /* ExceptionDescribe ***********************************************************
1055 Prints an exception and a backtrace of the stack to a system
1056 error-reporting channel, such as stderr. This is a convenience
1057 routine provided for debugging.
1059 *******************************************************************************/
1061 void jni_ExceptionDescribe(JNIEnv *env)
1063 TRACEJNICALLS(("jni_ExceptionDescribe(env=%p)", env));
1065 exceptions_print_stacktrace();
1069 /* ExceptionClear **************************************************************
1071 Clears any exception that is currently being thrown. If no
1072 exception is currently being thrown, this routine has no effect.
1074 *******************************************************************************/
1076 void jni_ExceptionClear(JNIEnv *env)
1078 TRACEJNICALLS(("jni_ExceptionClear(env=%p)", env));
1080 exceptions_clear_exception();
1084 /* FatalError ******************************************************************
1086 Raises a fatal error and does not expect the VM to recover. This
1087 function does not return.
1089 *******************************************************************************/
1091 void _Jv_JNI_FatalError(JNIEnv *env, const char *msg)
1093 STATISTICS(jniinvokation());
1095 /* this seems to be the best way */
1097 vm_abort("JNI Fatal error: %s", msg);
1101 /* PushLocalFrame **************************************************************
1103 Creates a new local reference frame, in which at least a given
1104 number of local references can be created.
1106 *******************************************************************************/
1108 jint jni_PushLocalFrame(JNIEnv* env, jint capacity)
1110 TRACEJNICALLS(("jni_PushLocalFrame(env=%p, capacity=%d)", env, capacity));
1115 /* add new local reference frame to current table */
1117 if (!localref_frame_push(capacity))
1124 /* PopLocalFrame ***************************************************************
1126 Pops off the current local reference frame, frees all the local
1127 references, and returns a local reference in the previous local
1128 reference frame for the given result object.
1130 *******************************************************************************/
1132 jobject jni_PopLocalFrame(JNIEnv* env, jobject result)
1134 TRACEJNICALLS(("jni_PopLocalFrame(env=%p, result=%p)", env, result));
1136 /* release all current local frames */
1138 localref_frame_pop_all();
1140 /* add local reference and return the value */
1142 return jni_NewLocalRef(env, result);
1146 /* DeleteLocalRef **************************************************************
1148 Deletes the local reference pointed to by localRef.
1150 *******************************************************************************/
1152 void jni_DeleteLocalRef(JNIEnv *env, jobject localRef)
1156 TRACEJNICALLS(("jni_DeleteLocalRef(env=%p, ref=%p)", env, localRef));
1158 o = (java_handle_t *) localRef;
1163 /* delete the reference */
1169 /* IsSameObject ****************************************************************
1171 Tests whether two references refer to the same Java object.
1173 *******************************************************************************/
1175 jboolean _Jv_JNI_IsSameObject(JNIEnv *env, jobject ref1, jobject ref2)
1181 STATISTICS(jniinvokation());
1183 o1 = (java_handle_t *) ref1;
1184 o2 = (java_handle_t *) ref2;
1186 LLNI_CRITICAL_START;
1188 if (LLNI_UNWRAP(o1) == LLNI_UNWRAP(o2))
1199 /* NewLocalRef *****************************************************************
1201 Creates a new local reference that refers to the same object as ref.
1203 *******************************************************************************/
1205 jobject jni_NewLocalRef(JNIEnv *env, jobject ref)
1208 java_handle_t *localref;
1210 TRACEJNICALLS(("jni_NewLocalRef(env=%p, ref=%p)", env, ref));
1212 o = (java_handle_t *) ref;
1217 /* insert the reference */
1219 localref = localref_add(LLNI_DIRECT(o));
1221 return (jobject) localref;
1225 /* EnsureLocalCapacity *********************************************************
1227 Ensures that at least a given number of local references can be
1228 created in the current thread
1230 *******************************************************************************/
1232 jint jni_EnsureLocalCapacity(JNIEnv* env, jint capacity)
1234 localref_table *lrt;
1236 TRACEJNICALLS(("jni_EnsureLocalCapacity(env=%p, capacity=%d)", env, capacity));
1238 /* get local reference table (thread specific) */
1240 lrt = LOCALREFTABLE;
1242 /* check if capacity elements are available in the local references table */
1244 if ((lrt->used + capacity) > lrt->capacity)
1245 return jni_PushLocalFrame(env, capacity);
1251 /* AllocObject *****************************************************************
1253 Allocates a new Java object without invoking any of the
1254 constructors for the object. Returns a reference to the object.
1256 *******************************************************************************/
1258 jobject _Jv_JNI_AllocObject(JNIEnv *env, jclass clazz)
1263 STATISTICS(jniinvokation());
1265 c = LLNI_classinfo_unwrap(clazz);
1267 if ((c->flags & ACC_INTERFACE) || (c->flags & ACC_ABSTRACT)) {
1268 exceptions_throw_instantiationexception(c);
1274 return jni_NewLocalRef(env, (jobject) o);
1278 /* NewObject *******************************************************************
1280 Programmers place all arguments that are to be passed to the
1281 constructor immediately following the methodID
1282 argument. NewObject() accepts these arguments and passes them to
1283 the Java method that the programmer wishes to invoke.
1285 *******************************************************************************/
1287 jobject jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
1294 TRACEJNICALLSENTER(("jni_NewObject(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
1296 c = LLNI_classinfo_unwrap(clazz);
1297 m = (methodinfo *) methodID;
1306 /* call constructor */
1308 va_start(ap, methodID);
1309 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1312 TRACEJNICALLSEXIT(("->%p", o));
1314 return jni_NewLocalRef(env, (jobject) o);
1318 /* NewObjectV ******************************************************************
1320 Programmers place all arguments that are to be passed to the
1321 constructor in an args argument of type va_list that immediately
1322 follows the methodID argument. NewObjectV() accepts these
1323 arguments, and, in turn, passes them to the Java method that the
1324 programmer wishes to invoke.
1326 *******************************************************************************/
1328 jobject _Jv_JNI_NewObjectV(JNIEnv* env, jclass clazz, jmethodID methodID,
1335 STATISTICS(jniinvokation());
1337 c = LLNI_classinfo_unwrap(clazz);
1338 m = (methodinfo *) methodID;
1347 /* call constructor */
1349 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1351 return jni_NewLocalRef(env, (jobject) o);
1355 /* NewObjectA *****************************************************************
1357 Programmers place all arguments that are to be passed to the
1358 constructor in an args array of jvalues that immediately follows
1359 the methodID argument. NewObjectA() accepts the arguments in this
1360 array, and, in turn, passes them to the Java method that the
1361 programmer wishes to invoke.
1363 *******************************************************************************/
1365 jobject _Jv_JNI_NewObjectA(JNIEnv* env, jclass clazz, jmethodID methodID,
1372 STATISTICS(jniinvokation());
1374 c = LLNI_classinfo_unwrap(clazz);
1375 m = (methodinfo *) methodID;
1384 /* call constructor */
1386 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1388 return jni_NewLocalRef(env, (jobject) o);
1392 /* GetObjectClass **************************************************************
1394 Returns the class of an object.
1396 *******************************************************************************/
1398 jclass jni_GetObjectClass(JNIEnv *env, jobject obj)
1403 TRACEJNICALLS(("jni_GetObjectClass(env=%p, obj=%p)", env, obj));
1405 o = (java_handle_t *) obj;
1407 if ((o == NULL) || (LLNI_vftbl_direct(o) == NULL))
1410 LLNI_class_get(o, c);
1412 java_handle_t* h = LLNI_classinfo_wrap(c);
1414 return (jclass) jni_NewLocalRef(env, (jobject) h);
1418 /* IsInstanceOf ****************************************************************
1420 Tests whether an object is an instance of a class.
1422 *******************************************************************************/
1424 jboolean _Jv_JNI_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)
1429 TRACEJNICALLS(("_Jv_JNI_IsInstanceOf(env=%p, obj=%p, clazz=%p)", env, obj, clazz));
1431 /* XXX Is this correct? */
1432 c = LLNI_classinfo_unwrap(clazz);
1433 h = (java_handle_t *) obj;
1435 return class_is_instance(c, h);
1439 /* Reflection Support *********************************************************/
1441 /* FromReflectedMethod *********************************************************
1443 Converts java.lang.reflect.Method or java.lang.reflect.Constructor
1444 object to a method ID.
1446 *******************************************************************************/
1448 jmethodID jni_FromReflectedMethod(JNIEnv *env, jobject method)
1450 #if defined(ENABLE_JAVASE)
1454 TRACEJNICALLS(("jni_FromReflectedMethod(env=%p, method=%p)", env, method));
1456 o = (java_handle_t *) method;
1461 // FIXME We can't access the object here directly.
1462 if (o->vftbl->clazz == class_java_lang_reflect_Constructor) {
1463 java_lang_reflect_Constructor rc(method);
1464 m = rc.get_method();
1467 // FIXME We can't access the object here directly.
1468 assert(o->vftbl->clazz == class_java_lang_reflect_Method);
1470 java_lang_reflect_Method rm(method);
1471 m = rm.get_method();
1474 return (jmethodID) m;
1476 vm_abort("jni_FromReflectedMethod: Not implemented in this configuration.");
1478 // Keep compiler happy.
1484 /* FromReflectedField **********************************************************
1486 Converts a java.lang.reflect.Field to a field ID.
1488 *******************************************************************************/
1490 jfieldID jni_FromReflectedField(JNIEnv* env, jobject field)
1492 #if defined(ENABLE_JAVASE)
1494 TRACEJNICALLS(("jni_FromReflectedField(env=%p, field=%p)", env, field));
1496 java_lang_reflect_Field rf(field);
1501 fieldinfo* f = rf.get_field();
1503 return (jfieldID) f;
1505 vm_abort("jni_FromReflectedField: Not implemented in this configuration.");
1507 // Keep compiler happy.
1513 /* ToReflectedMethod ***********************************************************
1515 Converts a method ID derived from cls to an instance of the
1516 java.lang.reflect.Method class or to an instance of the
1517 java.lang.reflect.Constructor class.
1519 *******************************************************************************/
1521 jobject jni_ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic)
1523 #if defined(ENABLE_JAVASE)
1524 TRACEJNICALLS(("jni_ToReflectedMethod(env=%p, cls=%p, methodID=%p, isStatic=%d)", env, cls, methodID, isStatic));
1526 methodinfo* m = (methodinfo *) methodID;
1528 /* HotSpot does the same assert. */
1530 assert(((m->flags & ACC_STATIC) != 0) == (isStatic != 0));
1534 if (m->name == utf_init) {
1535 h = java_lang_reflect_Constructor(m).get_handle();
1538 h = java_lang_reflect_Method(m).get_handle();
1543 vm_abort("jni_ToReflectedMethod: Not implemented in this configuration.");
1545 /* keep compiler happy */
1552 /* ToReflectedField ************************************************************
1554 Converts a field ID derived from cls to an instance of the
1555 java.lang.reflect.Field class.
1557 *******************************************************************************/
1559 jobject _Jv_JNI_ToReflectedField(JNIEnv* env, jclass cls, jfieldID fieldID,
1562 STATISTICS(jniinvokation());
1564 log_text("JNI-Call: ToReflectedField: IMPLEMENT ME!");
1570 /* Calling Instance Methods ***************************************************/
1572 /* GetMethodID *****************************************************************
1574 Returns the method ID for an instance (nonstatic) method of a class
1575 or interface. The method may be defined in one of the clazz's
1576 superclasses and inherited by clazz. The method is determined by
1577 its name and signature.
1579 GetMethodID() causes an uninitialized class to be initialized.
1581 *******************************************************************************/
1583 jmethodID _Jv_JNI_GetMethodID(JNIEnv* env, jclass clazz, const char *name,
1591 STATISTICS(jniinvokation());
1593 c = LLNI_classinfo_unwrap(clazz);
1598 if (!(c->state & CLASS_INITIALIZED))
1599 if (!initialize_class(c))
1602 /* try to get the method of the class or one of it's superclasses */
1604 uname = utf_new_char((char *) name);
1605 udesc = utf_new_char((char *) sig);
1607 m = class_resolvemethod(c, uname, udesc);
1609 if ((m == NULL) || (m->flags & ACC_STATIC)) {
1610 exceptions_throw_nosuchmethoderror(c, uname, udesc);
1615 return (jmethodID) m;
1619 /* JNI-functions for calling instance methods *********************************/
1621 #define JNI_CALL_VIRTUAL_METHOD(name, type, intern) \
1622 type _Jv_JNI_Call##name##Method(JNIEnv *env, jobject obj, \
1623 jmethodID methodID, ...) \
1630 o = (java_handle_t *) obj; \
1631 m = (methodinfo *) methodID; \
1633 va_start(ap, methodID); \
1634 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, ap); \
1640 JNI_CALL_VIRTUAL_METHOD(Boolean, jboolean, Int)
1641 JNI_CALL_VIRTUAL_METHOD(Byte, jbyte, Int)
1642 JNI_CALL_VIRTUAL_METHOD(Char, jchar, Int)
1643 JNI_CALL_VIRTUAL_METHOD(Short, jshort, Int)
1644 JNI_CALL_VIRTUAL_METHOD(Int, jint, Int)
1645 JNI_CALL_VIRTUAL_METHOD(Long, jlong, Long)
1646 JNI_CALL_VIRTUAL_METHOD(Float, jfloat, Float)
1647 JNI_CALL_VIRTUAL_METHOD(Double, jdouble, Double)
1650 #define JNI_CALL_VIRTUAL_METHOD_V(name, type, intern) \
1651 type _Jv_JNI_Call##name##MethodV(JNIEnv *env, jobject obj, \
1652 jmethodID methodID, va_list args) \
1658 o = (java_handle_t *) obj; \
1659 m = (methodinfo *) methodID; \
1661 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, args); \
1666 JNI_CALL_VIRTUAL_METHOD_V(Boolean, jboolean, Int)
1667 JNI_CALL_VIRTUAL_METHOD_V(Byte, jbyte, Int)
1668 JNI_CALL_VIRTUAL_METHOD_V(Char, jchar, Int)
1669 JNI_CALL_VIRTUAL_METHOD_V(Short, jshort, Int)
1670 JNI_CALL_VIRTUAL_METHOD_V(Int, jint, Int)
1671 JNI_CALL_VIRTUAL_METHOD_V(Long, jlong, Long)
1672 JNI_CALL_VIRTUAL_METHOD_V(Float, jfloat, Float)
1673 JNI_CALL_VIRTUAL_METHOD_V(Double, jdouble, Double)
1676 #define JNI_CALL_VIRTUAL_METHOD_A(name, type, intern) \
1677 type _Jv_JNI_Call##name##MethodA(JNIEnv *env, jobject obj, \
1678 jmethodID methodID, \
1679 const jvalue *args) \
1685 o = (java_handle_t *) obj; \
1686 m = (methodinfo *) methodID; \
1688 ret = _Jv_jni_Call##intern##MethodA(o, LLNI_vftbl_direct(o), m, args); \
1693 JNI_CALL_VIRTUAL_METHOD_A(Boolean, jboolean, Int)
1694 JNI_CALL_VIRTUAL_METHOD_A(Byte, jbyte, Int)
1695 JNI_CALL_VIRTUAL_METHOD_A(Char, jchar, Int)
1696 JNI_CALL_VIRTUAL_METHOD_A(Short, jshort, Int)
1697 JNI_CALL_VIRTUAL_METHOD_A(Int, jint, Int)
1698 JNI_CALL_VIRTUAL_METHOD_A(Long, jlong, Long)
1699 JNI_CALL_VIRTUAL_METHOD_A(Float, jfloat, Float)
1700 JNI_CALL_VIRTUAL_METHOD_A(Double, jdouble, Double)
1703 jobject _Jv_JNI_CallObjectMethod(JNIEnv *env, jobject obj, jmethodID methodID,
1711 o = (java_handle_t *) obj;
1712 m = (methodinfo *) methodID;
1714 va_start(ap, methodID);
1715 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, ap);
1718 return jni_NewLocalRef(env, (jobject) ret);
1722 jobject _Jv_JNI_CallObjectMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1729 o = (java_handle_t *) obj;
1730 m = (methodinfo *) methodID;
1732 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, args);
1734 return jni_NewLocalRef(env, (jobject) ret);
1738 jobject _Jv_JNI_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1745 o = (java_handle_t *) obj;
1746 m = (methodinfo *) methodID;
1748 ret = _Jv_jni_CallObjectMethodA(o, LLNI_vftbl_direct(o), m, args);
1750 return jni_NewLocalRef(env, (jobject) ret);
1755 void _Jv_JNI_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
1761 o = (java_handle_t *) obj;
1762 m = (methodinfo *) methodID;
1764 va_start(ap, methodID);
1765 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1770 void _Jv_JNI_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1776 o = (java_handle_t *) obj;
1777 m = (methodinfo *) methodID;
1779 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1783 void _Jv_JNI_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1789 o = (java_handle_t *) obj;
1790 m = (methodinfo *) methodID;
1792 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1797 #define JNI_CALL_NONVIRTUAL_METHOD(name, type, intern) \
1798 type _Jv_JNI_CallNonvirtual##name##Method(JNIEnv *env, jobject obj, \
1799 jclass clazz, jmethodID methodID, \
1808 o = (java_handle_t *) obj; \
1809 c = LLNI_classinfo_unwrap(clazz); \
1810 m = (methodinfo *) methodID; \
1812 va_start(ap, methodID); \
1813 ret = _Jv_jni_Call##intern##Method(o, c->vftbl, m, ap); \
1819 JNI_CALL_NONVIRTUAL_METHOD(Boolean, jboolean, Int)
1820 JNI_CALL_NONVIRTUAL_METHOD(Byte, jbyte, Int)
1821 JNI_CALL_NONVIRTUAL_METHOD(Char, jchar, Int)
1822 JNI_CALL_NONVIRTUAL_METHOD(Short, jshort, Int)
1823 JNI_CALL_NONVIRTUAL_METHOD(Int, jint, Int)
1824 JNI_CALL_NONVIRTUAL_METHOD(Long, jlong, Long)
1825 JNI_CALL_NONVIRTUAL_METHOD(Float, jfloat, Float)
1826 JNI_CALL_NONVIRTUAL_METHOD(Double, jdouble, Double)
1829 #define JNI_CALL_NONVIRTUAL_METHOD_V(name, type, intern) \
1830 type _Jv_JNI_CallNonvirtual##name##MethodV(JNIEnv *env, jobject obj, \
1831 jclass clazz, jmethodID methodID, \
1839 o = (java_handle_t *) obj; \
1840 c = LLNI_classinfo_unwrap(clazz); \
1841 m = (methodinfo *) methodID; \
1843 ret = _Jv_jni_CallIntMethod(o, c->vftbl, m, args); \
1848 JNI_CALL_NONVIRTUAL_METHOD_V(Boolean, jboolean, Int)
1849 JNI_CALL_NONVIRTUAL_METHOD_V(Byte, jbyte, Int)
1850 JNI_CALL_NONVIRTUAL_METHOD_V(Char, jchar, Int)
1851 JNI_CALL_NONVIRTUAL_METHOD_V(Short, jshort, Int)
1852 JNI_CALL_NONVIRTUAL_METHOD_V(Int, jint, Int)
1853 JNI_CALL_NONVIRTUAL_METHOD_V(Long, jlong, Long)
1854 JNI_CALL_NONVIRTUAL_METHOD_V(Float, jfloat, Float)
1855 JNI_CALL_NONVIRTUAL_METHOD_V(Double, jdouble, Double)
1858 #define JNI_CALL_NONVIRTUAL_METHOD_A(name, type, intern) \
1859 type _Jv_JNI_CallNonvirtual##name##MethodA(JNIEnv *env, jobject obj, \
1860 jclass clazz, jmethodID methodID, \
1861 const jvalue *args) \
1863 log_text("JNI-Call: CallNonvirtual##name##MethodA: IMPLEMENT ME!"); \
1868 JNI_CALL_NONVIRTUAL_METHOD_A(Boolean, jboolean, Int)
1869 JNI_CALL_NONVIRTUAL_METHOD_A(Byte, jbyte, Int)
1870 JNI_CALL_NONVIRTUAL_METHOD_A(Char, jchar, Int)
1871 JNI_CALL_NONVIRTUAL_METHOD_A(Short, jshort, Int)
1872 JNI_CALL_NONVIRTUAL_METHOD_A(Int, jint, Int)
1873 JNI_CALL_NONVIRTUAL_METHOD_A(Long, jlong, Long)
1874 JNI_CALL_NONVIRTUAL_METHOD_A(Float, jfloat, Float)
1875 JNI_CALL_NONVIRTUAL_METHOD_A(Double, jdouble, Double)
1877 jobject _Jv_JNI_CallNonvirtualObjectMethod(JNIEnv *env, jobject obj,
1878 jclass clazz, jmethodID methodID,
1887 o = (java_handle_t *) obj;
1888 c = LLNI_classinfo_unwrap(clazz);
1889 m = (methodinfo *) methodID;
1891 va_start(ap, methodID);
1892 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, ap);
1895 return jni_NewLocalRef(env, (jobject) r);
1899 jobject _Jv_JNI_CallNonvirtualObjectMethodV(JNIEnv *env, jobject obj,
1900 jclass clazz, jmethodID methodID,
1908 o = (java_handle_t *) obj;
1909 c = LLNI_classinfo_unwrap(clazz);
1910 m = (methodinfo *) methodID;
1912 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, args);
1914 return jni_NewLocalRef(env, (jobject) r);
1918 jobject _Jv_JNI_CallNonvirtualObjectMethodA(JNIEnv *env, jobject obj,
1919 jclass clazz, jmethodID methodID,
1922 log_text("JNI-Call: CallNonvirtualObjectMethodA: IMPLEMENT ME!");
1924 return jni_NewLocalRef(env, NULL);
1928 void _Jv_JNI_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass clazz,
1929 jmethodID methodID, ...)
1936 o = (java_handle_t *) obj;
1937 c = LLNI_classinfo_unwrap(clazz);
1938 m = (methodinfo *) methodID;
1940 va_start(ap, methodID);
1941 _Jv_jni_CallVoidMethod(o, c->vftbl, m, ap);
1946 void _Jv_JNI_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass clazz,
1947 jmethodID methodID, va_list args)
1953 o = (java_handle_t *) obj;
1954 c = LLNI_classinfo_unwrap(clazz);
1955 m = (methodinfo *) methodID;
1957 _Jv_jni_CallVoidMethod(o, c->vftbl, m, args);
1961 void _Jv_JNI_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass clazz,
1962 jmethodID methodID, const jvalue * args)
1968 o = (java_handle_t *) obj;
1969 c = LLNI_classinfo_unwrap(clazz);
1970 m = (methodinfo *) methodID;
1972 _Jv_jni_CallVoidMethodA(o, c->vftbl, m, args);
1976 /* Accessing Fields of Objects ************************************************/
1978 /* GetFieldID ******************************************************************
1980 Returns the field ID for an instance (nonstatic) field of a
1981 class. The field is specified by its name and signature. The
1982 Get<type>Field and Set<type>Field families of accessor functions
1983 use field IDs to retrieve object fields.
1985 *******************************************************************************/
1987 jfieldID _Jv_JNI_GetFieldID(JNIEnv *env, jclass clazz, const char *name,
1995 STATISTICS(jniinvokation());
1997 c = LLNI_classinfo_unwrap(clazz);
1999 /* XXX NPE check? */
2001 uname = utf_new_char((char *) name);
2002 udesc = utf_new_char((char *) sig);
2004 f = class_findfield(c, uname, udesc);
2007 exceptions_throw_nosuchfielderror(c, uname);
2009 return (jfieldID) f;
2013 /* Get<type>Field Routines *****************************************************
2015 This family of accessor routines returns the value of an instance
2016 (nonstatic) field of an object. The field to access is specified by
2017 a field ID obtained by calling GetFieldID().
2019 *******************************************************************************/
2021 #define GET_FIELD(o,type,f) \
2022 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset)))
2024 #define JNI_GET_FIELD(name, type, intern) \
2025 type _Jv_JNI_Get##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID) \
2029 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "Field(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID)); \
2031 LLNI_CRITICAL_START; \
2033 ret = GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID); \
2035 LLNI_CRITICAL_END; \
2037 return (type) ret; \
2040 JNI_GET_FIELD(Boolean, jboolean, s4)
2041 JNI_GET_FIELD(Byte, jbyte, s4)
2042 JNI_GET_FIELD(Char, jchar, s4)
2043 JNI_GET_FIELD(Short, jshort, s4)
2044 JNI_GET_FIELD(Int, jint, s4)
2045 JNI_GET_FIELD(Long, jlong, s8)
2046 JNI_GET_FIELD(Float, jfloat, float)
2047 JNI_GET_FIELD(Double, jdouble, double)
2050 jobject _Jv_JNI_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)
2054 TRACEJNICALLS(("_Jv_JNI_GetObjectField(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID));
2056 LLNI_CRITICAL_START;
2058 o = LLNI_WRAP(GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), java_object_t*, fieldID));
2062 return jni_NewLocalRef(env, (jobject) o);
2066 /* Set<type>Field Routines *****************************************************
2068 This family of accessor routines sets the value of an instance
2069 (nonstatic) field of an object. The field to access is specified by
2070 a field ID obtained by calling GetFieldID().
2072 *******************************************************************************/
2074 #define SET_FIELD(o,type,f,value) \
2075 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset))) = (type) (value)
2077 #define JNI_SET_FIELD(name, type, intern) \
2078 void _Jv_JNI_Set##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID, \
2081 TRACEJNICALLS(("_Jv_JNI_Set" STR(name) "Field(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value)); \
2083 LLNI_CRITICAL_START; \
2085 SET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID, value); \
2087 LLNI_CRITICAL_START; \
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));
2113 /* Calling Static Methods *****************************************************/
2115 /* GetStaticMethodID ***********************************************************
2117 Returns the method ID for a static method of a class. The method is
2118 specified by its name and signature.
2120 GetStaticMethodID() causes an uninitialized class to be
2123 *******************************************************************************/
2125 jmethodID _Jv_JNI_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name,
2133 TRACEJNICALLS(("_Jv_JNI_GetStaticMethodID(env=%p, clazz=%p, name=%s, sig=%s)", env, clazz, name, sig));
2135 c = LLNI_classinfo_unwrap(clazz);
2140 if (!(c->state & CLASS_INITIALIZED))
2141 if (!initialize_class(c))
2144 /* try to get the static method of the class */
2146 uname = utf_new_char((char *) name);
2147 udesc = utf_new_char((char *) sig);
2149 m = class_resolvemethod(c, uname, udesc);
2151 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
2152 exceptions_throw_nosuchmethoderror(c, uname, udesc);
2157 return (jmethodID) m;
2161 #define JNI_CALL_STATIC_METHOD(name, type, intern) \
2162 type _Jv_JNI_CallStatic##name##Method(JNIEnv *env, jclass clazz, \
2163 jmethodID methodID, ...) \
2169 m = (methodinfo *) methodID; \
2171 va_start(ap, methodID); \
2172 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, ap); \
2178 JNI_CALL_STATIC_METHOD(Boolean, jboolean, Int)
2179 JNI_CALL_STATIC_METHOD(Byte, jbyte, Int)
2180 JNI_CALL_STATIC_METHOD(Char, jchar, Int)
2181 JNI_CALL_STATIC_METHOD(Short, jshort, Int)
2182 JNI_CALL_STATIC_METHOD(Int, jint, Int)
2183 JNI_CALL_STATIC_METHOD(Long, jlong, Long)
2184 JNI_CALL_STATIC_METHOD(Float, jfloat, Float)
2185 JNI_CALL_STATIC_METHOD(Double, jdouble, Double)
2188 #define JNI_CALL_STATIC_METHOD_V(name, type, intern) \
2189 type _Jv_JNI_CallStatic##name##MethodV(JNIEnv *env, jclass clazz, \
2190 jmethodID methodID, va_list args) \
2195 m = (methodinfo *) methodID; \
2197 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, args); \
2202 JNI_CALL_STATIC_METHOD_V(Boolean, jboolean, Int)
2203 JNI_CALL_STATIC_METHOD_V(Byte, jbyte, Int)
2204 JNI_CALL_STATIC_METHOD_V(Char, jchar, Int)
2205 JNI_CALL_STATIC_METHOD_V(Short, jshort, Int)
2206 JNI_CALL_STATIC_METHOD_V(Int, jint, Int)
2207 JNI_CALL_STATIC_METHOD_V(Long, jlong, Long)
2208 JNI_CALL_STATIC_METHOD_V(Float, jfloat, Float)
2209 JNI_CALL_STATIC_METHOD_V(Double, jdouble, Double)
2212 #define JNI_CALL_STATIC_METHOD_A(name, type, intern) \
2213 type _Jv_JNI_CallStatic##name##MethodA(JNIEnv *env, jclass clazz, \
2214 jmethodID methodID, const jvalue *args) \
2219 m = (methodinfo *) methodID; \
2221 res = _Jv_jni_Call##intern##MethodA(NULL, NULL, m, args); \
2226 JNI_CALL_STATIC_METHOD_A(Boolean, jboolean, Int)
2227 JNI_CALL_STATIC_METHOD_A(Byte, jbyte, Int)
2228 JNI_CALL_STATIC_METHOD_A(Char, jchar, Int)
2229 JNI_CALL_STATIC_METHOD_A(Short, jshort, Int)
2230 JNI_CALL_STATIC_METHOD_A(Int, jint, Int)
2231 JNI_CALL_STATIC_METHOD_A(Long, jlong, Long)
2232 JNI_CALL_STATIC_METHOD_A(Float, jfloat, Float)
2233 JNI_CALL_STATIC_METHOD_A(Double, jdouble, Double)
2236 jobject _Jv_JNI_CallStaticObjectMethod(JNIEnv *env, jclass clazz,
2237 jmethodID methodID, ...)
2243 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2245 m = (methodinfo *) methodID;
2247 va_start(ap, methodID);
2248 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, ap);
2251 return jni_NewLocalRef(env, (jobject) o);
2255 jobject _Jv_JNI_CallStaticObjectMethodV(JNIEnv *env, jclass clazz,
2256 jmethodID methodID, va_list args)
2261 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2263 m = (methodinfo *) methodID;
2265 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, args);
2267 return jni_NewLocalRef(env, (jobject) o);
2271 jobject _Jv_JNI_CallStaticObjectMethodA(JNIEnv *env, jclass clazz,
2272 jmethodID methodID, const jvalue *args)
2277 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2279 m = (methodinfo *) methodID;
2281 o = _Jv_jni_CallObjectMethodA(NULL, NULL, m, args);
2283 return jni_NewLocalRef(env, (jobject) o);
2287 void _Jv_JNI_CallStaticVoidMethod(JNIEnv *env, jclass clazz,
2288 jmethodID methodID, ...)
2293 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2295 m = (methodinfo *) methodID;
2297 va_start(ap, methodID);
2298 _Jv_jni_CallVoidMethod(NULL, NULL, m, ap);
2303 void _Jv_JNI_CallStaticVoidMethodV(JNIEnv *env, jclass clazz,
2304 jmethodID methodID, va_list args)
2308 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2310 m = (methodinfo *) methodID;
2312 _Jv_jni_CallVoidMethod(NULL, NULL, m, args);
2316 void _Jv_JNI_CallStaticVoidMethodA(JNIEnv *env, jclass clazz,
2317 jmethodID methodID, const jvalue * args)
2321 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2323 m = (methodinfo *) methodID;
2325 _Jv_jni_CallVoidMethodA(NULL, NULL, m, args);
2329 /* Accessing Static Fields ****************************************************/
2331 /* GetStaticFieldID ************************************************************
2333 Returns the field ID for a static field of a class. The field is
2334 specified by its name and signature. The GetStatic<type>Field and
2335 SetStatic<type>Field families of accessor functions use field IDs
2336 to retrieve static fields.
2338 *******************************************************************************/
2340 jfieldID _Jv_JNI_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name,
2348 STATISTICS(jniinvokation());
2350 c = LLNI_classinfo_unwrap(clazz);
2352 uname = utf_new_char((char *) name);
2353 usig = utf_new_char((char *) sig);
2355 f = class_findfield(c, uname, usig);
2358 exceptions_throw_nosuchfielderror(c, uname);
2360 return (jfieldID) f;
2364 /* GetStatic<type>Field ********************************************************
2366 This family of accessor routines returns the value of a static
2369 *******************************************************************************/
2371 #define JNI_GET_STATIC_FIELD(name, type, field) \
2372 type _Jv_JNI_GetStatic##name##Field(JNIEnv *env, jclass clazz, \
2378 STATISTICS(jniinvokation()); \
2380 c = LLNI_classinfo_unwrap(clazz); \
2381 f = (fieldinfo *) fieldID; \
2383 if (!(c->state & CLASS_INITIALIZED)) \
2384 if (!initialize_class(c)) \
2387 return f->value->field; \
2390 JNI_GET_STATIC_FIELD(Boolean, jboolean, i)
2391 JNI_GET_STATIC_FIELD(Byte, jbyte, i)
2392 JNI_GET_STATIC_FIELD(Char, jchar, i)
2393 JNI_GET_STATIC_FIELD(Short, jshort, i)
2394 JNI_GET_STATIC_FIELD(Int, jint, i)
2395 JNI_GET_STATIC_FIELD(Long, jlong, l)
2396 JNI_GET_STATIC_FIELD(Float, jfloat, f)
2397 JNI_GET_STATIC_FIELD(Double, jdouble, d)
2400 jobject _Jv_JNI_GetStaticObjectField(JNIEnv *env, jclass clazz,
2407 STATISTICS(jniinvokation());
2409 c = LLNI_classinfo_unwrap(clazz);
2410 f = (fieldinfo *) fieldID;
2412 if (!(c->state & CLASS_INITIALIZED))
2413 if (!initialize_class(c))
2416 h = (java_handle_t*) LLNI_WRAP(f->value->a);
2418 return jni_NewLocalRef(env, (jobject) h);
2422 /* SetStatic<type>Field *******************************************************
2424 This family of accessor routines sets the value of a static field
2427 *******************************************************************************/
2429 #define JNI_SET_STATIC_FIELD(name, type, field) \
2430 void _Jv_JNI_SetStatic##name##Field(JNIEnv *env, jclass clazz, \
2437 STATISTICS(jniinvokation()); \
2439 c = LLNI_classinfo_unwrap(clazz); \
2440 f = (fieldinfo *) fieldID; \
2442 if (!(c->state & CLASS_INITIALIZED)) \
2443 if (!initialize_class(c)) \
2446 f->value->field = value; \
2449 JNI_SET_STATIC_FIELD(Boolean, jboolean, i)
2450 JNI_SET_STATIC_FIELD(Byte, jbyte, i)
2451 JNI_SET_STATIC_FIELD(Char, jchar, i)
2452 JNI_SET_STATIC_FIELD(Short, jshort, i)
2453 JNI_SET_STATIC_FIELD(Int, jint, i)
2454 JNI_SET_STATIC_FIELD(Long, jlong, l)
2455 JNI_SET_STATIC_FIELD(Float, jfloat, f)
2456 JNI_SET_STATIC_FIELD(Double, jdouble, d)
2459 void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID,
2465 STATISTICS(jniinvokation());
2467 c = LLNI_classinfo_unwrap(clazz);
2468 f = (fieldinfo *) fieldID;
2470 if (!(c->state & CLASS_INITIALIZED))
2471 if (!initialize_class(c))
2474 f->value->a = LLNI_UNWRAP((java_handle_t *) value);
2478 /* String Operations **********************************************************/
2480 /* NewString *******************************************************************
2482 Create new java.lang.String object from an array of Unicode
2485 *******************************************************************************/
2487 jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len)
2489 TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len));
2491 java_handle_chararray_t* a = builtin_newarray_char(len);
2497 for (jsize i = 0; i < len; i++)
2498 LLNI_array_direct(a, i) = buf[i];
2500 java_handle_t* h = builtin_new(class_java_lang_String);
2505 java_lang_String s(h, a, len, 0);
2507 return (jstring) jni_NewLocalRef(env, (jobject) s.get_handle());
2511 static jchar emptyStringJ[]={0,0};
2513 /* GetStringLength *************************************************************
2515 Returns the length (the count of Unicode characters) of a Java
2518 *******************************************************************************/
2520 jsize jni_GetStringLength(JNIEnv *env, jstring str)
2522 TRACEJNICALLSENTER(("jni_GetStringLength(env=%p, str=%p)", env, str));
2524 java_lang_String s(str);
2525 jsize count = s.get_count();
2527 TRACEJNICALLSEXIT(("->%d)", count));
2533 /* GetStringChars **************************************************************
2535 Returns a pointer to the array of Unicode characters of the
2536 string. This pointer is valid until ReleaseStringChars() is called.
2538 *******************************************************************************/
2540 const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy)
2545 TRACEJNICALLS(("jni_GetStringChars(env=%p, str=%p, isCopy=%p)", env, str, isCopy));
2548 // FIXME This is really ugly.
2549 return emptyStringJ;
2551 java_lang_String s(str);
2553 java_handle_chararray_t* ca = s.get_value();
2554 int32_t count = s.get_count();
2555 int32_t offset = s.get_offset();
2560 /* allocate memory */
2562 stringbuffer = MNEW(u2, count + 1);
2566 for (i = 0; i < count; i++)
2567 stringbuffer[i] = LLNI_array_direct(ca, offset + i);
2569 /* terminate string */
2571 stringbuffer[i] = '\0';
2576 return (jchar*) stringbuffer;
2580 /* ReleaseStringChars **********************************************************
2582 Informs the VM that the native code no longer needs access to
2583 chars. The chars argument is a pointer obtained from string using
2586 *******************************************************************************/
2588 void _Jv_JNI_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)
2590 TRACEJNICALLS(("jni_ReleaseStringChars(env=%p, str=%p, chars=%p)", env, str, chars));
2593 if (chars == emptyStringJ)
2596 java_lang_String s(str);
2597 int32_t count = s.get_count();
2599 MFREE(((jchar*) chars), jchar, count + 1);
2603 /* NewStringUTF ****************************************************************
2605 Constructs a new java.lang.String object from an array of UTF-8
2608 *******************************************************************************/
2610 jstring jni_NewStringUTF(JNIEnv *env, const char *bytes)
2612 TRACEJNICALLS(("jni_NewStringUTF(env=%p, bytes=%s)", env, bytes));
2614 java_handle_t *h = javastring_safe_new_from_utf8(bytes);
2616 return (jstring) jni_NewLocalRef(env, (jobject) h);
2620 /****************** returns the utf8 length in bytes of a string *******************/
2622 jsize jni_GetStringUTFLength(JNIEnv *env, jstring string)
2624 TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string));
2626 java_lang_String s(string);
2627 java_handle_chararray_t* ca = s.get_value();
2628 int32_t count = s.get_count();
2630 // FIXME GC critical section!
2631 int32_t length = u2_utflength(ca->data, count);
2637 /* GetStringUTFChars ***********************************************************
2639 Returns a pointer to an array of UTF-8 characters of the
2640 string. This array is valid until it is released by
2641 ReleaseStringUTFChars().
2643 *******************************************************************************/
2645 const char *_Jv_JNI_GetStringUTFChars(JNIEnv *env, jstring string,
2650 STATISTICS(jniinvokation());
2658 u = javastring_toutf((java_handle_t *) string, false);
2667 /* ReleaseStringUTFChars *******************************************************
2669 Informs the VM that the native code no longer needs access to
2670 utf. The utf argument is a pointer derived from string using
2671 GetStringUTFChars().
2673 *******************************************************************************/
2675 void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf)
2677 STATISTICS(jniinvokation());
2679 /* XXX we don't release utf chars right now, perhaps that should be done
2680 later. Since there is always one reference the garbage collector will
2685 /* Array Operations ***********************************************************/
2687 /* GetArrayLength **************************************************************
2689 Returns the number of elements in the array.
2691 *******************************************************************************/
2693 jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array)
2698 TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array));
2700 a = (java_handle_t *) array;
2702 size = LLNI_array_size(a);
2708 /* NewObjectArray **************************************************************
2710 Constructs a new array holding objects in class elementClass. All
2711 elements are initially set to initialElement.
2713 *******************************************************************************/
2715 jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length,
2716 jclass elementClass, jobject initialElement)
2720 java_handle_objectarray_t *oa;
2723 STATISTICS(jniinvokation());
2725 c = LLNI_classinfo_unwrap(elementClass);
2726 o = (java_handle_t *) initialElement;
2729 exceptions_throw_negativearraysizeexception();
2733 oa = builtin_anewarray(length, c);
2738 /* set all elements to initialElement */
2740 for (i = 0; i < length; i++)
2741 array_objectarray_element_set(oa, i, o);
2743 return (jobjectArray) jni_NewLocalRef(env, (jobject) oa);
2747 jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array,
2750 java_handle_objectarray_t *oa;
2753 STATISTICS(jniinvokation());
2755 oa = (java_handle_objectarray_t *) array;
2757 if (index >= LLNI_array_size(oa)) {
2758 exceptions_throw_arrayindexoutofboundsexception();
2762 o = array_objectarray_element_get(oa, index);
2764 return jni_NewLocalRef(env, (jobject) o);
2768 void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array,
2769 jsize index, jobject val)
2771 java_handle_objectarray_t *oa;
2774 STATISTICS(jniinvokation());
2776 oa = (java_handle_objectarray_t *) array;
2777 o = (java_handle_t *) val;
2779 if (index >= LLNI_array_size(oa)) {
2780 exceptions_throw_arrayindexoutofboundsexception();
2784 /* check if the class of value is a subclass of the element class
2787 if (!builtin_canstore(oa, o))
2790 array_objectarray_element_set(oa, index, o);
2794 #define JNI_NEW_ARRAY(name, type, intern) \
2795 type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \
2797 java_handle_##intern##array_t *a; \
2799 STATISTICS(jniinvokation()); \
2802 exceptions_throw_negativearraysizeexception(); \
2806 a = builtin_newarray_##intern(len); \
2808 return (type) jni_NewLocalRef(env, (jobject) a); \
2811 JNI_NEW_ARRAY(Boolean, jbooleanArray, boolean)
2812 JNI_NEW_ARRAY(Byte, jbyteArray, byte)
2813 JNI_NEW_ARRAY(Char, jcharArray, char)
2814 JNI_NEW_ARRAY(Short, jshortArray, short)
2815 JNI_NEW_ARRAY(Int, jintArray, int)
2816 JNI_NEW_ARRAY(Long, jlongArray, long)
2817 JNI_NEW_ARRAY(Float, jfloatArray, float)
2818 JNI_NEW_ARRAY(Double, jdoubleArray, double)
2821 /* Get<PrimitiveType>ArrayElements *********************************************
2823 A family of functions that returns the body of the primitive array.
2825 *******************************************************************************/
2827 #define JNI_GET_ARRAY_ELEMENTS(name, type, intern) \
2828 type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \
2831 java_handle_##intern##array_t *a; \
2833 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \
2835 a = (java_handle_##intern##array_t *) array; \
2838 *isCopy = JNI_FALSE; \
2840 return (type *) LLNI_array_data(a); \
2843 JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean)
2844 JNI_GET_ARRAY_ELEMENTS(Byte, jbyte, byte)
2845 JNI_GET_ARRAY_ELEMENTS(Char, jchar, char)
2846 JNI_GET_ARRAY_ELEMENTS(Short, jshort, short)
2847 JNI_GET_ARRAY_ELEMENTS(Int, jint, int)
2848 JNI_GET_ARRAY_ELEMENTS(Long, jlong, long)
2849 JNI_GET_ARRAY_ELEMENTS(Float, jfloat, float)
2850 JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double)
2853 /* Release<PrimitiveType>ArrayElements *****************************************
2855 A family of functions that informs the VM that the native code no
2856 longer needs access to elems. The elems argument is a pointer
2857 derived from array using the corresponding
2858 Get<PrimitiveType>ArrayElements() function. If necessary, this
2859 function copies back all changes made to elems to the original
2862 *******************************************************************************/
2864 #define JNI_RELEASE_ARRAY_ELEMENTS(name, type, intern, intern2) \
2865 void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \
2866 type *elems, jint mode) \
2868 java_handle_##intern##array_t *a; \
2870 STATISTICS(jniinvokation()); \
2872 a = (java_handle_##intern##array_t *) array; \
2874 if (elems != (type *) LLNI_array_data(a)) { \
2877 MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \
2880 MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \
2881 /* XXX TWISTI how should it be freed? */ \
2884 /* XXX TWISTI how should it be freed? */ \
2890 JNI_RELEASE_ARRAY_ELEMENTS(Boolean, jboolean, boolean, u1)
2891 JNI_RELEASE_ARRAY_ELEMENTS(Byte, jbyte, byte, s1)
2892 JNI_RELEASE_ARRAY_ELEMENTS(Char, jchar, char, u2)
2893 JNI_RELEASE_ARRAY_ELEMENTS(Short, jshort, short, s2)
2894 JNI_RELEASE_ARRAY_ELEMENTS(Int, jint, int, s4)
2895 JNI_RELEASE_ARRAY_ELEMENTS(Long, jlong, long, s8)
2896 JNI_RELEASE_ARRAY_ELEMENTS(Float, jfloat, float, float)
2897 JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double)
2900 /* Get<PrimitiveType>ArrayRegion **********************************************
2902 A family of functions that copies a region of a primitive array
2905 *******************************************************************************/
2907 #define JNI_GET_ARRAY_REGION(name, type, intern, intern2) \
2908 void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \
2909 jsize start, jsize len, type *buf) \
2911 java_handle_##intern##array_t *a; \
2913 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \
2915 a = (java_handle_##intern##array_t *) array; \
2917 if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \
2918 exceptions_throw_arrayindexoutofboundsexception(); \
2920 MCOPY(buf, &LLNI_array_direct(a, start), intern2, len); \
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 java_handle_##intern##array_t *a; \
2946 STATISTICS(jniinvokation()); \
2948 a = (java_handle_##intern##array_t *) array; \
2950 if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \
2951 exceptions_throw_arrayindexoutofboundsexception(); \
2953 MCOPY(&LLNI_array_direct(a, start), buf, intern2, len); \
2956 JNI_SET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2957 JNI_SET_ARRAY_REGION(Byte, jbyte, byte, s1)
2958 JNI_SET_ARRAY_REGION(Char, jchar, char, u2)
2959 JNI_SET_ARRAY_REGION(Short, jshort, short, s2)
2960 JNI_SET_ARRAY_REGION(Int, jint, int, s4)
2961 JNI_SET_ARRAY_REGION(Long, jlong, long, s8)
2962 JNI_SET_ARRAY_REGION(Float, jfloat, float, float)
2963 JNI_SET_ARRAY_REGION(Double, jdouble, double, double)
2966 /* Registering Native Methods *************************************************/
2968 /* RegisterNatives *************************************************************
2970 Registers native methods with the class specified by the clazz
2971 argument. The methods parameter specifies an array of
2972 JNINativeMethod structures that contain the names, signatures, and
2973 function pointers of the native methods. The nMethods parameter
2974 specifies the number of native methods in the array.
2976 *******************************************************************************/
2978 jint _Jv_JNI_RegisterNatives(JNIEnv *env, jclass clazz,
2979 const JNINativeMethod *methods, jint nMethods)
2983 STATISTICS(jniinvokation());
2985 c = LLNI_classinfo_unwrap(clazz);
2987 /* XXX: if implemented this needs a call to jvmti_NativeMethodBind
2988 if (jvmti) jvmti_NativeMethodBind(method, address, new_address_ptr);
2991 native_method_register(c->name, methods, nMethods);
2997 /* UnregisterNatives ***********************************************************
2999 Unregisters native methods of a class. The class goes back to the
3000 state before it was linked or registered with its native method
3003 This function should not be used in normal native code. Instead, it
3004 provides special programs a way to reload and relink native
3007 *******************************************************************************/
3009 jint _Jv_JNI_UnregisterNatives(JNIEnv *env, jclass clazz)
3011 STATISTICS(jniinvokation());
3013 /* XXX TWISTI hmm, maybe we should not support that (like kaffe) */
3015 log_text("JNI-Call: UnregisterNatives: IMPLEMENT ME!!!");
3021 /* Monitor Operations *********************************************************/
3023 /* MonitorEnter ****************************************************************
3025 Enters the monitor associated with the underlying Java object
3028 *******************************************************************************/
3030 jint _Jv_JNI_MonitorEnter(JNIEnv *env, jobject obj)
3032 STATISTICS(jniinvokation());
3035 exceptions_throw_nullpointerexception();
3039 LOCK_MONITOR_ENTER(obj);
3045 /* MonitorExit *****************************************************************
3047 The current thread must be the owner of the monitor associated with
3048 the underlying Java object referred to by obj. The thread
3049 decrements the counter indicating the number of times it has
3050 entered this monitor. If the value of the counter becomes zero, the
3051 current thread releases the monitor.
3053 *******************************************************************************/
3055 jint _Jv_JNI_MonitorExit(JNIEnv *env, jobject obj)
3057 STATISTICS(jniinvokation());
3060 exceptions_throw_nullpointerexception();
3064 LOCK_MONITOR_EXIT(obj);
3070 /* JavaVM Interface ***********************************************************/
3072 /* GetJavaVM *******************************************************************
3074 Returns the Java VM interface (used in the Invocation API)
3075 associated with the current thread. The result is placed at the
3076 location pointed to by the second argument, vm.
3078 *******************************************************************************/
3080 jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm)
3082 STATISTICS(jniinvokation());
3084 *javavm = VM::get_current()->get_javavm();
3090 /* GetStringRegion *************************************************************
3092 Copies len number of Unicode characters beginning at offset start
3093 to the given buffer buf.
3095 Throws StringIndexOutOfBoundsException on index overflow.
3097 *******************************************************************************/
3099 void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf)
3101 java_lang_String s(str);
3102 java_handle_chararray_t* ca = s.get_value();
3103 int32_t count = s.get_count();
3105 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3106 exceptions_throw_stringindexoutofboundsexception();
3110 MCOPY(buf, &LLNI_array_direct(ca, start), u2, len);
3114 /* GetStringUTFRegion **********************************************************
3116 Translates len number of Unicode characters beginning at offset
3117 start into UTF-8 format and place the result in the given buffer
3120 Throws StringIndexOutOfBoundsException on index overflow.
3122 *******************************************************************************/
3124 void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char *buf)
3126 TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf));
3128 java_lang_String s(str);
3129 java_handle_chararray_t* ca = s.get_value();
3130 int32_t count = s.get_count();
3131 int32_t offset = s.get_offset();
3133 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3134 exceptions_throw_stringindexoutofboundsexception();
3140 for (i = 0; i < len; i++)
3141 buf[i] = LLNI_array_direct(ca, offset + start + i);
3147 /* GetPrimitiveArrayCritical ***************************************************
3149 Obtain a direct pointer to array elements.
3151 ATTENTION: Critical section keeps open when this function returns!
3152 See ReleasePrimitiveArrayCritical.
3154 *******************************************************************************/
3156 void* jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)
3160 arraydescriptor* ad;
3163 TRACEJNICALLS(("jni_GetPrimitiveArrayCritical(env=%p, array=%p, isCopy=%d)", env, array, isCopy));
3165 if (isCopy != NULL) {
3166 *isCopy = JNI_FALSE;
3169 LLNI_CRITICAL_START;
3171 h = (java_handle_t*) array;
3172 a = (java_array_t*) LLNI_UNWRAP(h);
3173 ad = a->objheader.vftbl->arraydesc;
3179 data = (void*) (((intptr_t) a) + ad->dataoffset);
3185 /* ReleasePrimitiveArrayCritical ***********************************************
3187 No specific documentation.
3189 ATTENTION: This function closes the critical section opened in
3190 GetPrimitiveArrayCritical!
3192 *******************************************************************************/
3194 void jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)
3196 TRACEJNICALLS(("jni_ReleasePrimitiveArrayCritical(env=%p, array=%p, carray=%p, mode=%d)", env, array, carray, mode));
3202 /* GetStringCritical ***********************************************************
3204 The semantics of these two functions are similar to the existing
3205 Get/ReleaseStringChars functions.
3207 *******************************************************************************/
3209 const jchar *_Jv_JNI_GetStringCritical(JNIEnv *env, jstring string,
3212 STATISTICS(jniinvokation());
3214 return jni_GetStringChars(env, string, isCopy);
3218 void _Jv_JNI_ReleaseStringCritical(JNIEnv *env, jstring string,
3219 const jchar *cstring)
3221 STATISTICS(jniinvokation());
3223 _Jv_JNI_ReleaseStringChars(env, string, cstring);
3227 jweak _Jv_JNI_NewWeakGlobalRef(JNIEnv* env, jobject obj)
3229 TRACEJNICALLS(("_Jv_JNI_NewWeakGlobalRef(env=%p, obj=%p): IMPLEMENT ME!", env, obj));
3235 void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
3237 TRACEJNICALLS(("_Jv_JNI_DeleteWeakGlobalRef(env=%p, ref=%p): IMPLEMENT ME", env, ref));
3241 /* NewGlobalRef ****************************************************************
3243 Creates a new global reference to the object referred to by the obj
3246 *******************************************************************************/
3248 jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
3250 hashtable_global_ref_entry *gre;
3251 u4 key; /* hashkey */
3252 u4 slot; /* slot in hashtable */
3255 TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
3257 o = (java_handle_t *) obj;
3259 LOCK_MONITOR_ENTER(hashtable_global_ref->header);
3261 LLNI_CRITICAL_START;
3263 /* normally addresses are aligned to 4, 8 or 16 bytes */
3265 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3266 slot = key & (hashtable_global_ref->size - 1);
3267 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3269 /* search external hash chain for the entry */
3272 if (gre->o == LLNI_DIRECT(o)) {
3273 /* global object found, increment the reference */
3280 gre = gre->hashlink; /* next element in external chain */
3285 /* global ref not found, create a new one */
3288 gre = (hashtable_global_ref_entry*) heap_alloc_uncollectable(sizeof(hashtable_global_ref_entry));
3290 #if defined(ENABLE_GC_CACAO)
3291 /* register global ref with the GC */
3293 gc_reference_register(&(gre->o), GC_REFTYPE_JNI_GLOBALREF);
3296 LLNI_CRITICAL_START;
3298 gre->o = LLNI_DIRECT(o);
3303 /* insert entry into hashtable */
3305 gre->hashlink = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3307 hashtable_global_ref->ptr[slot] = gre;
3309 /* update number of hashtable-entries */
3311 hashtable_global_ref->entries++;
3314 LOCK_MONITOR_EXIT(hashtable_global_ref->header);
3316 #if defined(ENABLE_HANDLES)
3324 /* DeleteGlobalRef *************************************************************
3326 Deletes the global reference pointed to by globalRef.
3328 *******************************************************************************/
3330 void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
3332 hashtable_global_ref_entry *gre;
3333 hashtable_global_ref_entry *prevgre;
3334 u4 key; /* hashkey */
3335 u4 slot; /* slot in hashtable */
3338 TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
3340 o = (java_handle_t *) globalRef;
3342 LOCK_MONITOR_ENTER(hashtable_global_ref->header);
3344 LLNI_CRITICAL_START;
3346 /* normally addresses are aligned to 4, 8 or 16 bytes */
3348 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3349 slot = key & (hashtable_global_ref->size - 1);
3350 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3352 /* initialize prevgre */
3356 /* search external hash chain for the entry */
3359 if (gre->o == LLNI_DIRECT(o)) {
3360 /* global object found, decrement the reference count */
3364 /* if reference count is 0, remove the entry */
3366 if (gre->refs == 0) {
3367 /* special handling if it's the first in the chain */
3369 if (prevgre == NULL)
3370 hashtable_global_ref->ptr[slot] = gre->hashlink;
3372 prevgre->hashlink = gre->hashlink;
3374 #if defined(ENABLE_GC_CACAO)
3375 /* unregister global ref with the GC */
3377 gc_reference_unregister(&(gre->o));
3385 LOCK_MONITOR_EXIT(hashtable_global_ref->header);
3390 prevgre = gre; /* save current pointer for removal */
3391 gre = gre->hashlink; /* next element in external chain */
3394 log_println("jni_DeleteGlobalRef: Global reference not found.");
3398 LOCK_MONITOR_EXIT(hashtable_global_ref->header);
3402 /* ExceptionCheck **************************************************************
3404 Returns JNI_TRUE when there is a pending exception; otherwise,
3407 *******************************************************************************/
3409 jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
3413 STATISTICS(jniinvokation());
3415 o = exceptions_get_exception();
3417 return (o != NULL) ? JNI_TRUE : JNI_FALSE;
3421 /* New JNI 1.4 functions ******************************************************/
3423 /* NewDirectByteBuffer *********************************************************
3425 Allocates and returns a direct java.nio.ByteBuffer referring to the
3426 block of memory starting at the memory address address and
3427 extending capacity bytes.
3429 *******************************************************************************/
3431 jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
3433 #if defined(ENABLE_JAVASE)
3434 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3435 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3437 // Allocate a gnu.classpath.Pointer{32,64} object.
3439 # if SIZEOF_VOID_P == 8
3440 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer64);
3442 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer32);
3448 gnu_classpath_Pointer p(h, address);
3450 // Create a java.nio.DirectByteBufferImpl$ReadWrite object.
3452 java_handle_t* nbuf =
3453 (java_handle_t*) jni_NewObject(env, (jclass) class_java_nio_DirectByteBufferImpl_ReadWrite,
3454 (jmethodID) dbbirw_init, NULL, p.get_handle(),
3455 (jint) capacity, (jint) capacity, (jint) 0);
3457 // Add a local reference and return the value.
3459 TRACEJNICALLSEXIT(("->%p", nbuf));
3461 return jni_NewLocalRef(env, (jobject) nbuf);
3463 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3469 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3471 /* Be paranoid about address sign-extension. */
3473 addr = (int64_t) ((uintptr_t) address);
3474 cap = (int32_t) capacity;
3476 o = jni_NewObject(env, (jclass) class_java_nio_DirectByteBuffer,
3477 (jmethodID) dbb_init, addr, cap);
3479 /* Add local reference and return the value. */
3481 TRACEJNICALLSEXIT(("->%p", o));
3483 return jni_NewLocalRef(env, o);
3486 # error unknown classpath configuration
3490 vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
3492 /* keep compiler happy */
3499 /* GetDirectBufferAddress ******************************************************
3501 Fetches and returns the starting address of the memory region
3502 referenced by the given direct java.nio.Buffer.
3504 *******************************************************************************/
3506 void* jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3508 #if defined(ENABLE_JAVASE)
3509 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3511 TRACEJNICALLSENTER(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3513 /* Prevent compiler warning. */
3515 java_handle_t* h = (java_handle_t *) buf;
3517 if ((h != NULL) && !builtin_instanceof(h, class_java_nio_Buffer))
3520 java_nio_DirectByteBufferImpl dbb(buf);
3521 java_handle_t* address = dbb.get_address();
3523 if (address == NULL) {
3524 TRACEJNICALLSEXIT(("->%p", NULL));
3528 gnu_classpath_Pointer p(address);
3529 void* data = p.get_data();
3531 TRACEJNICALLSEXIT(("->%p", data));
3535 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3537 TRACEJNICALLS(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3539 java_nio_Buffer jnb(buf);
3541 if (jnb.is_non_null() && !builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3544 void* address = jnb.get_address();
3549 # error unknown classpath configuration
3554 vm_abort("jni_GetDirectBufferAddress: Not implemented in this configuration.");
3556 // Keep compiler happy.
3563 /* GetDirectBufferCapacity *****************************************************
3565 Fetches and returns the capacity in bytes of the memory region
3566 referenced by the given direct java.nio.Buffer.
3568 *******************************************************************************/
3570 jlong jni_GetDirectBufferCapacity(JNIEnv* env, jobject buf)
3572 #if defined(ENABLE_JAVASE) && defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3573 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3575 java_handle_t* h = (java_handle_t *) buf;
3577 if (!builtin_instanceof(h, class_java_nio_DirectByteBufferImpl))
3580 java_nio_Buffer b(h);
3581 jlong capacity = b.get_cap();
3585 vm_abort("jni_GetDirectBufferCapacity: not implemented in this configuration");
3587 // Keep compiler happy.
3594 /* GetObjectRefType ************************************************************
3596 Returns the type of the object referred to by the obj argument. The
3597 argument obj can either be a local, global or weak global
3600 *******************************************************************************/
3602 jobjectRefType jni_GetObjectRefType(JNIEnv *env, jobject obj)
3604 log_println("jni_GetObjectRefType: IMPLEMENT ME!");
3606 return (jobjectRefType) NULL;
3610 /* DestroyJavaVM ***************************************************************
3612 Unloads a Java VM and reclaims its resources. Only the main thread
3613 can unload the VM. The system waits until the main thread is only
3614 remaining user thread before it destroys the VM.
3616 *******************************************************************************/
3618 jint _Jv_JNI_DestroyJavaVM(JavaVM *javavm)
3622 TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(javavm=%p)", javavm));
3624 if (VM::get_current()->is_created() == false)
3627 status = vm_destroy(javavm);
3633 /* AttachCurrentThread *********************************************************
3635 Attaches the current thread to a Java VM. Returns a JNI interface
3636 pointer in the JNIEnv argument.
3638 Trying to attach a thread that is already attached is a no-op.
3640 A native thread cannot be attached simultaneously to two Java VMs.
3642 When a thread is attached to the VM, the context class loader is
3643 the bootstrap loader.
3645 *******************************************************************************/
3647 static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
3649 #if defined(ENABLE_THREADS)
3650 JavaVMAttachArgs *vm_aargs;
3653 /* If the current thread has already been attached, this operation
3656 result = thread_current_is_attached();
3658 if (result == true) {
3659 *p_env = VM::get_current()->get_jnienv();
3663 vm_aargs = (JavaVMAttachArgs *) thr_args;
3665 if (vm_aargs != NULL) {
3666 if ((vm_aargs->version != JNI_VERSION_1_2) &&
3667 (vm_aargs->version != JNI_VERSION_1_4))
3668 return JNI_EVERSION;
3671 if (!thread_attach_current_external_thread(vm_aargs, false))
3674 if (!localref_table_init())
3678 *p_env = VM::get_current()->get_jnienv();
3684 jint jni_AttachCurrentThread(JavaVM *javavm, void **p_env, void *thr_args)
3688 TRACEJNICALLS(("jni_AttachCurrentThread(javavm=%p, p_env=%p, thr_args=%p)", javavm, p_env, thr_args));
3690 if (VM::get_current()->is_created() == false)
3693 result = jni_attach_current_thread(p_env, thr_args, false);
3699 /* DetachCurrentThread *********************************************************
3701 Detaches the current thread from a Java VM. All Java monitors held
3702 by this thread are released. All Java threads waiting for this
3703 thread to die are notified.
3705 In JDK 1.1, the main thread cannot be detached from the VM. It must
3706 call DestroyJavaVM to unload the entire VM.
3708 In the JDK, the main thread can be detached from the VM.
3710 The main thread, which is the thread that created the Java VM,
3711 cannot be detached from the VM. Instead, the main thread must call
3712 JNI_DestroyJavaVM() to unload the entire VM.
3714 *******************************************************************************/
3716 jint jni_DetachCurrentThread(JavaVM *vm)
3718 #if defined(ENABLE_THREADS)
3721 TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
3723 /* If the current thread has already been detached, this operation
3726 result = thread_current_is_attached();
3728 if (result == false)
3731 /* We need to pop all frames before we can destroy the table. */
3733 localref_frame_pop_all();
3735 if (!localref_table_destroy())
3738 if (!thread_detach_current_external_thread())
3746 /* GetEnv **********************************************************************
3748 If the current thread is not attached to the VM, sets *env to NULL,
3749 and returns JNI_EDETACHED. If the specified version is not
3750 supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise,
3751 sets *env to the appropriate interface, and returns JNI_OK.
3753 *******************************************************************************/
3755 jint jni_GetEnv(JavaVM *javavm, void **env, jint version)
3757 TRACEJNICALLS(("jni_GetEnv(javavm=%p, env=%p, version=%d)", javavm, env, version));
3759 if (VM::get_current()->is_created() == false) {
3761 return JNI_EDETACHED;
3764 #if defined(ENABLE_THREADS)
3765 if (thread_get_current() == NULL) {
3768 return JNI_EDETACHED;
3772 /* Check the JNI version. */
3774 if (jni_version_check(version) == true) {
3775 *env = VM::get_current()->get_jnienv();
3779 #if defined(ENABLE_JVMTI)
3780 if ((version & JVMTI_VERSION_MASK_INTERFACE_TYPE)
3781 == JVMTI_VERSION_INTERFACE_JVMTI) {
3783 *env = (void *) jvmti_new_environment();
3792 return JNI_EVERSION;
3796 /* AttachCurrentThreadAsDaemon *************************************************
3798 Same semantics as AttachCurrentThread, but the newly-created
3799 java.lang.Thread instance is a daemon.
3801 If the thread has already been attached via either
3802 AttachCurrentThread or AttachCurrentThreadAsDaemon, this routine
3803 simply sets the value pointed to by penv to the JNIEnv of the
3804 current thread. In this case neither AttachCurrentThread nor this
3805 routine have any effect on the daemon status of the thread.
3807 *******************************************************************************/
3809 jint jni_AttachCurrentThreadAsDaemon(JavaVM *javavm, void **penv, void *args)
3813 TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(javavm=%p, penv=%p, args=%p)", javavm, penv, args));
3815 if (VM::get_current()->is_created() == false)
3818 result = jni_attach_current_thread(penv, args, true);
3824 /* JNI invocation table *******************************************************/
3826 const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
3831 _Jv_JNI_DestroyJavaVM,
3832 jni_AttachCurrentThread,
3833 jni_DetachCurrentThread,
3835 jni_AttachCurrentThreadAsDaemon
3839 /* JNI function table *********************************************************/
3841 struct JNINativeInterface_ _Jv_JNINativeInterface = {
3850 jni_FromReflectedMethod,
3851 jni_FromReflectedField,
3852 jni_ToReflectedMethod,
3854 _Jv_JNI_IsAssignableFrom,
3855 _Jv_JNI_ToReflectedField,
3859 _Jv_JNI_ExceptionOccurred,
3860 jni_ExceptionDescribe,
3867 jni_DeleteGlobalRef,
3869 _Jv_JNI_IsSameObject,
3871 jni_EnsureLocalCapacity,
3873 _Jv_JNI_AllocObject,
3879 _Jv_JNI_IsInstanceOf,
3881 _Jv_JNI_GetMethodID,
3883 _Jv_JNI_CallObjectMethod,
3884 _Jv_JNI_CallObjectMethodV,
3885 _Jv_JNI_CallObjectMethodA,
3886 _Jv_JNI_CallBooleanMethod,
3887 _Jv_JNI_CallBooleanMethodV,
3888 _Jv_JNI_CallBooleanMethodA,
3889 _Jv_JNI_CallByteMethod,
3890 _Jv_JNI_CallByteMethodV,
3891 _Jv_JNI_CallByteMethodA,
3892 _Jv_JNI_CallCharMethod,
3893 _Jv_JNI_CallCharMethodV,
3894 _Jv_JNI_CallCharMethodA,
3895 _Jv_JNI_CallShortMethod,
3896 _Jv_JNI_CallShortMethodV,
3897 _Jv_JNI_CallShortMethodA,
3898 _Jv_JNI_CallIntMethod,
3899 _Jv_JNI_CallIntMethodV,
3900 _Jv_JNI_CallIntMethodA,
3901 _Jv_JNI_CallLongMethod,
3902 _Jv_JNI_CallLongMethodV,
3903 _Jv_JNI_CallLongMethodA,
3904 _Jv_JNI_CallFloatMethod,
3905 _Jv_JNI_CallFloatMethodV,
3906 _Jv_JNI_CallFloatMethodA,
3907 _Jv_JNI_CallDoubleMethod,
3908 _Jv_JNI_CallDoubleMethodV,
3909 _Jv_JNI_CallDoubleMethodA,
3910 _Jv_JNI_CallVoidMethod,
3911 _Jv_JNI_CallVoidMethodV,
3912 _Jv_JNI_CallVoidMethodA,
3914 _Jv_JNI_CallNonvirtualObjectMethod,
3915 _Jv_JNI_CallNonvirtualObjectMethodV,
3916 _Jv_JNI_CallNonvirtualObjectMethodA,
3917 _Jv_JNI_CallNonvirtualBooleanMethod,
3918 _Jv_JNI_CallNonvirtualBooleanMethodV,
3919 _Jv_JNI_CallNonvirtualBooleanMethodA,
3920 _Jv_JNI_CallNonvirtualByteMethod,
3921 _Jv_JNI_CallNonvirtualByteMethodV,
3922 _Jv_JNI_CallNonvirtualByteMethodA,
3923 _Jv_JNI_CallNonvirtualCharMethod,
3924 _Jv_JNI_CallNonvirtualCharMethodV,
3925 _Jv_JNI_CallNonvirtualCharMethodA,
3926 _Jv_JNI_CallNonvirtualShortMethod,
3927 _Jv_JNI_CallNonvirtualShortMethodV,
3928 _Jv_JNI_CallNonvirtualShortMethodA,
3929 _Jv_JNI_CallNonvirtualIntMethod,
3930 _Jv_JNI_CallNonvirtualIntMethodV,
3931 _Jv_JNI_CallNonvirtualIntMethodA,
3932 _Jv_JNI_CallNonvirtualLongMethod,
3933 _Jv_JNI_CallNonvirtualLongMethodV,
3934 _Jv_JNI_CallNonvirtualLongMethodA,
3935 _Jv_JNI_CallNonvirtualFloatMethod,
3936 _Jv_JNI_CallNonvirtualFloatMethodV,
3937 _Jv_JNI_CallNonvirtualFloatMethodA,
3938 _Jv_JNI_CallNonvirtualDoubleMethod,
3939 _Jv_JNI_CallNonvirtualDoubleMethodV,
3940 _Jv_JNI_CallNonvirtualDoubleMethodA,
3941 _Jv_JNI_CallNonvirtualVoidMethod,
3942 _Jv_JNI_CallNonvirtualVoidMethodV,
3943 _Jv_JNI_CallNonvirtualVoidMethodA,
3947 _Jv_JNI_GetObjectField,
3948 _Jv_JNI_GetBooleanField,
3949 _Jv_JNI_GetByteField,
3950 _Jv_JNI_GetCharField,
3951 _Jv_JNI_GetShortField,
3952 _Jv_JNI_GetIntField,
3953 _Jv_JNI_GetLongField,
3954 _Jv_JNI_GetFloatField,
3955 _Jv_JNI_GetDoubleField,
3956 _Jv_JNI_SetObjectField,
3957 _Jv_JNI_SetBooleanField,
3958 _Jv_JNI_SetByteField,
3959 _Jv_JNI_SetCharField,
3960 _Jv_JNI_SetShortField,
3961 _Jv_JNI_SetIntField,
3962 _Jv_JNI_SetLongField,
3963 _Jv_JNI_SetFloatField,
3964 _Jv_JNI_SetDoubleField,
3966 _Jv_JNI_GetStaticMethodID,
3968 _Jv_JNI_CallStaticObjectMethod,
3969 _Jv_JNI_CallStaticObjectMethodV,
3970 _Jv_JNI_CallStaticObjectMethodA,
3971 _Jv_JNI_CallStaticBooleanMethod,
3972 _Jv_JNI_CallStaticBooleanMethodV,
3973 _Jv_JNI_CallStaticBooleanMethodA,
3974 _Jv_JNI_CallStaticByteMethod,
3975 _Jv_JNI_CallStaticByteMethodV,
3976 _Jv_JNI_CallStaticByteMethodA,
3977 _Jv_JNI_CallStaticCharMethod,
3978 _Jv_JNI_CallStaticCharMethodV,
3979 _Jv_JNI_CallStaticCharMethodA,
3980 _Jv_JNI_CallStaticShortMethod,
3981 _Jv_JNI_CallStaticShortMethodV,
3982 _Jv_JNI_CallStaticShortMethodA,
3983 _Jv_JNI_CallStaticIntMethod,
3984 _Jv_JNI_CallStaticIntMethodV,
3985 _Jv_JNI_CallStaticIntMethodA,
3986 _Jv_JNI_CallStaticLongMethod,
3987 _Jv_JNI_CallStaticLongMethodV,
3988 _Jv_JNI_CallStaticLongMethodA,
3989 _Jv_JNI_CallStaticFloatMethod,
3990 _Jv_JNI_CallStaticFloatMethodV,
3991 _Jv_JNI_CallStaticFloatMethodA,
3992 _Jv_JNI_CallStaticDoubleMethod,
3993 _Jv_JNI_CallStaticDoubleMethodV,
3994 _Jv_JNI_CallStaticDoubleMethodA,
3995 _Jv_JNI_CallStaticVoidMethod,
3996 _Jv_JNI_CallStaticVoidMethodV,
3997 _Jv_JNI_CallStaticVoidMethodA,
3999 _Jv_JNI_GetStaticFieldID,
4001 _Jv_JNI_GetStaticObjectField,
4002 _Jv_JNI_GetStaticBooleanField,
4003 _Jv_JNI_GetStaticByteField,
4004 _Jv_JNI_GetStaticCharField,
4005 _Jv_JNI_GetStaticShortField,
4006 _Jv_JNI_GetStaticIntField,
4007 _Jv_JNI_GetStaticLongField,
4008 _Jv_JNI_GetStaticFloatField,
4009 _Jv_JNI_GetStaticDoubleField,
4010 _Jv_JNI_SetStaticObjectField,
4011 _Jv_JNI_SetStaticBooleanField,
4012 _Jv_JNI_SetStaticByteField,
4013 _Jv_JNI_SetStaticCharField,
4014 _Jv_JNI_SetStaticShortField,
4015 _Jv_JNI_SetStaticIntField,
4016 _Jv_JNI_SetStaticLongField,
4017 _Jv_JNI_SetStaticFloatField,
4018 _Jv_JNI_SetStaticDoubleField,
4021 jni_GetStringLength,
4023 _Jv_JNI_ReleaseStringChars,
4026 jni_GetStringUTFLength,
4027 _Jv_JNI_GetStringUTFChars,
4028 _Jv_JNI_ReleaseStringUTFChars,
4030 _Jv_JNI_GetArrayLength,
4032 _Jv_JNI_NewObjectArray,
4033 _Jv_JNI_GetObjectArrayElement,
4034 _Jv_JNI_SetObjectArrayElement,
4036 _Jv_JNI_NewBooleanArray,
4037 _Jv_JNI_NewByteArray,
4038 _Jv_JNI_NewCharArray,
4039 _Jv_JNI_NewShortArray,
4040 _Jv_JNI_NewIntArray,
4041 _Jv_JNI_NewLongArray,
4042 _Jv_JNI_NewFloatArray,
4043 _Jv_JNI_NewDoubleArray,
4045 _Jv_JNI_GetBooleanArrayElements,
4046 _Jv_JNI_GetByteArrayElements,
4047 _Jv_JNI_GetCharArrayElements,
4048 _Jv_JNI_GetShortArrayElements,
4049 _Jv_JNI_GetIntArrayElements,
4050 _Jv_JNI_GetLongArrayElements,
4051 _Jv_JNI_GetFloatArrayElements,
4052 _Jv_JNI_GetDoubleArrayElements,
4054 _Jv_JNI_ReleaseBooleanArrayElements,
4055 _Jv_JNI_ReleaseByteArrayElements,
4056 _Jv_JNI_ReleaseCharArrayElements,
4057 _Jv_JNI_ReleaseShortArrayElements,
4058 _Jv_JNI_ReleaseIntArrayElements,
4059 _Jv_JNI_ReleaseLongArrayElements,
4060 _Jv_JNI_ReleaseFloatArrayElements,
4061 _Jv_JNI_ReleaseDoubleArrayElements,
4063 _Jv_JNI_GetBooleanArrayRegion,
4064 _Jv_JNI_GetByteArrayRegion,
4065 _Jv_JNI_GetCharArrayRegion,
4066 _Jv_JNI_GetShortArrayRegion,
4067 _Jv_JNI_GetIntArrayRegion,
4068 _Jv_JNI_GetLongArrayRegion,
4069 _Jv_JNI_GetFloatArrayRegion,
4070 _Jv_JNI_GetDoubleArrayRegion,
4071 _Jv_JNI_SetBooleanArrayRegion,
4072 _Jv_JNI_SetByteArrayRegion,
4073 _Jv_JNI_SetCharArrayRegion,
4074 _Jv_JNI_SetShortArrayRegion,
4075 _Jv_JNI_SetIntArrayRegion,
4076 _Jv_JNI_SetLongArrayRegion,
4077 _Jv_JNI_SetFloatArrayRegion,
4078 _Jv_JNI_SetDoubleArrayRegion,
4080 _Jv_JNI_RegisterNatives,
4081 _Jv_JNI_UnregisterNatives,
4083 _Jv_JNI_MonitorEnter,
4084 _Jv_JNI_MonitorExit,
4088 /* New JNI 1.2 functions. */
4090 jni_GetStringRegion,
4091 jni_GetStringUTFRegion,
4093 jni_GetPrimitiveArrayCritical,
4094 jni_ReleasePrimitiveArrayCritical,
4096 _Jv_JNI_GetStringCritical,
4097 _Jv_JNI_ReleaseStringCritical,
4099 _Jv_JNI_NewWeakGlobalRef,
4100 _Jv_JNI_DeleteWeakGlobalRef,
4102 _Jv_JNI_ExceptionCheck,
4104 /* New JNI 1.4 functions. */
4106 jni_NewDirectByteBuffer,
4107 jni_GetDirectBufferAddress,
4108 jni_GetDirectBufferCapacity,
4110 /* New JNI 1.6 functions. */
4112 jni_GetObjectRefType
4116 /* Invocation API Functions ***************************************************/
4118 /* JNI_GetDefaultJavaVMInitArgs ************************************************
4120 Returns a default configuration for the Java VM.
4122 *******************************************************************************/
4124 jint JNI_GetDefaultJavaVMInitArgs(void *vm_args)
4126 JavaVMInitArgs *_vm_args;
4128 _vm_args = (JavaVMInitArgs *) vm_args;
4130 /* GNU classpath currently supports JNI 1.2 */
4132 switch (_vm_args->version) {
4133 case JNI_VERSION_1_1:
4134 _vm_args->version = JNI_VERSION_1_1;
4137 case JNI_VERSION_1_2:
4138 case JNI_VERSION_1_4:
4139 _vm_args->ignoreUnrecognized = JNI_FALSE;
4140 _vm_args->options = NULL;
4141 _vm_args->nOptions = 0;
4152 /* JNI_GetCreatedJavaVMs *******************************************************
4154 Returns all Java VMs that have been created. Pointers to VMs are written in
4155 the buffer vmBuf in the order they are created. At most bufLen number of
4156 entries will be written. The total number of created VMs is returned in
4159 *******************************************************************************/
4161 jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
4163 TRACEJNICALLS(("JNI_GetCreatedJavaVMs(vmBuf=%p, jsize=%d, jsize=%p)", vmBuf, bufLen, nVMs));
4168 // We currently only support 1 VM running.
4170 vmBuf[0] = VM::get_current()->get_javavm();
4177 /* JNI_CreateJavaVM ************************************************************
4179 Loads and initializes a Java VM. The current thread becomes the main thread.
4180 Sets the env argument to the JNI interface pointer of the main thread.
4182 *******************************************************************************/
4184 jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
4186 TRACEJNICALLS(("JNI_CreateJavaVM(p_vm=%p, p_env=%p, vm_args=%p)", p_vm, p_env, vm_args));
4188 /* actually create the JVM */
4190 if (!VM_create(p_vm, p_env, vm_args))
4200 * These are local overrides for various environment variables in Emacs.
4201 * Please do not remove this and leave it at the end of the file, where
4202 * Emacs will automagically detect them.
4203 * ---------------------------------------------------------------------
4206 * indent-tabs-mode: t
4210 * vim:noexpandtab:sw=4:ts=4: