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.hpp"
37 #include "native/jni.hpp"
38 #include "native/llni.h"
39 #include "native/localref.hpp"
40 #include "native/native.hpp"
42 #if defined(ENABLE_JVMTI)
43 # include "native/jvmti/cacaodbg.h"
46 #include "threads/lock.hpp"
47 #include "threads/mutex.hpp"
48 #include "threads/thread.hpp"
50 #include "toolbox/logging.hpp"
52 #include "vm/array.hpp"
53 #include "vm/jit/builtin.hpp"
54 #include "vm/exceptions.hpp"
55 #include "vm/global.h"
56 #include "vm/globals.hpp"
57 #include "vm/initialize.hpp"
58 #include "vm/javaobjects.hpp"
59 #include "vm/loader.hpp"
60 #include "vm/options.h"
61 #include "vm/primitive.hpp"
62 #include "vm/resolve.hpp"
63 #include "vm/statistics.h"
64 #include "vm/string.hpp"
67 #include "vm/jit/asmpart.h"
68 #include "vm/jit/jit.hpp"
69 #include "vm/jit/stacktrace.hpp"
72 /* debug **********************************************************************/
76 # define TRACEJNICALLS(x) \
78 if (opt_TraceJNICalls) { \
83 # define TRACEJNICALLSENTER(x) \
85 if (opt_TraceJNICalls) { \
91 # define TRACEJNICALLSEXIT(x) \
93 if (opt_TraceJNICalls) { \
101 # define TRACEJNICALLS(x)
102 # define TRACEJNICALLSENTER(x)
103 # define TRACEJNICALLSEXIT(x)
108 /* global variables ***********************************************************/
110 /* global reference table *****************************************************/
112 /* hashsize must be power of 2 */
114 #define HASHTABLE_GLOBAL_REF_SIZE 64 /* initial size of globalref-hash */
116 static hashtable *hashtable_global_ref; /* hashtable for globalrefs */
119 /* direct buffer stuff ********************************************************/
121 #if defined(ENABLE_JAVASE)
122 static classinfo *class_java_nio_Buffer;
124 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
126 static classinfo *class_java_nio_DirectByteBufferImpl;
127 static classinfo *class_java_nio_DirectByteBufferImpl_ReadWrite;
129 # if SIZEOF_VOID_P == 8
130 static classinfo *class_gnu_classpath_Pointer64;
132 static classinfo *class_gnu_classpath_Pointer32;
135 static methodinfo *dbbirw_init;
137 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
139 static classinfo *class_sun_nio_ch_DirectBuffer;
140 static classinfo *class_java_nio_DirectByteBuffer;
142 static methodinfo *dbb_init;
148 /* some forward declarations **************************************************/
151 jobject jni_NewLocalRef(JNIEnv *env, jobject ref);
155 /* jni_init ********************************************************************
157 Initialize the JNI subsystem.
159 *******************************************************************************/
163 TRACESUBSYSTEMINITIALIZATION("jni_init");
165 /* create global ref hashtable */
167 hashtable_global_ref = NEW(hashtable);
169 hashtable_create(hashtable_global_ref, HASHTABLE_GLOBAL_REF_SIZE);
172 #if defined(ENABLE_JAVASE)
173 /* Direct buffer stuff. */
175 if (!(class_java_nio_Buffer =
176 load_class_bootstrap(utf_new_char("java/nio/Buffer"))) ||
177 !link_class(class_java_nio_Buffer))
180 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
182 if (!(class_java_nio_DirectByteBufferImpl =
183 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl"))) ||
184 !link_class(class_java_nio_DirectByteBufferImpl))
187 if (!(class_java_nio_DirectByteBufferImpl_ReadWrite =
188 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl$ReadWrite"))) ||
189 !link_class(class_java_nio_DirectByteBufferImpl_ReadWrite))
193 class_resolvemethod(class_java_nio_DirectByteBufferImpl_ReadWrite,
195 utf_new_char("(Ljava/lang/Object;Lgnu/classpath/Pointer;III)V"))))
198 # if SIZEOF_VOID_P == 8
199 if (!(class_gnu_classpath_Pointer64 =
200 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer64"))) ||
201 !link_class(class_gnu_classpath_Pointer64))
204 if (!(class_gnu_classpath_Pointer32 =
205 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer32"))) ||
206 !link_class(class_gnu_classpath_Pointer32))
210 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
212 if (!(class_sun_nio_ch_DirectBuffer =
213 load_class_bootstrap(utf_new_char("sun/nio/ch/DirectBuffer"))))
214 vm_abort("jni_init: loading sun/nio/ch/DirectBuffer failed");
216 if (!link_class(class_sun_nio_ch_DirectBuffer))
217 vm_abort("jni_init: linking sun/nio/ch/DirectBuffer failed");
219 if (!(class_java_nio_DirectByteBuffer =
220 load_class_bootstrap(utf_new_char("java/nio/DirectByteBuffer"))))
221 vm_abort("jni_init: loading java/nio/DirectByteBuffer failed");
223 if (!link_class(class_java_nio_DirectByteBuffer))
224 vm_abort("jni_init: linking java/nio/DirectByteBuffer failed");
227 class_resolvemethod(class_java_nio_DirectByteBuffer,
229 utf_new_char("(JI)V"))))
230 vm_abort("jni_init: resolving java/nio/DirectByteBuffer.init(JI)V failed");
234 #endif /* defined(ENABLE_JAVASE) */
240 /* jni_version_check ***********************************************************
242 Check if the given JNI version is supported.
245 version....JNI version to check
249 false......not supported
251 *******************************************************************************/
253 bool jni_version_check(int version)
256 case JNI_VERSION_1_1:
257 case JNI_VERSION_1_2:
258 case JNI_VERSION_1_4:
259 case JNI_VERSION_1_6:
267 /* _Jv_jni_CallObjectMethod ****************************************************
269 Internal function to call Java Object methods.
271 *******************************************************************************/
273 static java_handle_t *_Jv_jni_CallObjectMethod(java_handle_t *o,
275 methodinfo *m, va_list ap)
280 STATISTICS(jniinvokation());
283 exceptions_throw_nullpointerexception();
287 /* Class initialization is done by the JIT compiler. This is ok
288 since a static method always belongs to the declaring class. */
290 if (m->flags & ACC_STATIC) {
291 /* For static methods we reset the object. */
296 /* for convenience */
301 /* For instance methods we make a virtual function table lookup. */
303 resm = method_vftbl_lookup(vftbl, m);
306 STATISTICS(jnicallXmethodnvokation());
308 ro = vm_call_method_valist(resm, o, ap);
314 /* _Jv_jni_CallObjectMethodA ***************************************************
316 Internal function to call Java Object methods.
318 *******************************************************************************/
320 static java_handle_t *_Jv_jni_CallObjectMethodA(java_handle_t *o,
328 STATISTICS(jniinvokation());
331 exceptions_throw_nullpointerexception();
335 /* Class initialization is done by the JIT compiler. This is ok
336 since a static method always belongs to the declaring class. */
338 if (m->flags & ACC_STATIC) {
339 /* For static methods we reset the object. */
344 /* for convenience */
349 /* For instance methods we make a virtual function table lookup. */
351 resm = method_vftbl_lookup(vftbl, m);
354 STATISTICS(jnicallXmethodnvokation());
356 ro = vm_call_method_jvalue(resm, o, args);
362 /* _Jv_jni_CallIntMethod *******************************************************
364 Internal function to call Java integer class methods (boolean,
365 byte, char, short, int).
367 *******************************************************************************/
369 static jint _Jv_jni_CallIntMethod(java_handle_t *o, vftbl_t *vftbl,
370 methodinfo *m, va_list ap)
375 STATISTICS(jniinvokation());
378 exceptions_throw_nullpointerexception();
382 /* Class initialization is done by the JIT compiler. This is ok
383 since a static method always belongs to the declaring class. */
385 if (m->flags & ACC_STATIC) {
386 /* For static methods we reset the object. */
391 /* for convenience */
396 /* For instance methods we make a virtual function table lookup. */
398 resm = method_vftbl_lookup(vftbl, m);
401 STATISTICS(jnicallXmethodnvokation());
403 i = vm_call_method_int_valist(resm, o, ap);
409 /* _Jv_jni_CallIntMethodA ******************************************************
411 Internal function to call Java integer class methods (boolean,
412 byte, char, short, int).
414 *******************************************************************************/
416 static jint _Jv_jni_CallIntMethodA(java_handle_t *o, vftbl_t *vftbl,
417 methodinfo *m, const jvalue *args)
422 STATISTICS(jniinvokation());
425 exceptions_throw_nullpointerexception();
429 /* Class initialization is done by the JIT compiler. This is ok
430 since a static method always belongs to the declaring class. */
432 if (m->flags & ACC_STATIC) {
433 /* For static methods we reset the object. */
438 /* for convenience */
443 /* For instance methods we make a virtual function table lookup. */
445 resm = method_vftbl_lookup(vftbl, m);
448 STATISTICS(jnicallXmethodnvokation());
450 i = vm_call_method_int_jvalue(resm, o, args);
456 /* _Jv_jni_CallLongMethod ******************************************************
458 Internal function to call Java long methods.
460 *******************************************************************************/
462 static jlong _Jv_jni_CallLongMethod(java_handle_t *o, vftbl_t *vftbl,
463 methodinfo *m, va_list ap)
468 STATISTICS(jniinvokation());
471 exceptions_throw_nullpointerexception();
475 /* Class initialization is done by the JIT compiler. This is ok
476 since a static method always belongs to the declaring class. */
478 if (m->flags & ACC_STATIC) {
479 /* For static methods we reset the object. */
484 /* for convenience */
489 /* For instance methods we make a virtual function table lookup. */
491 resm = method_vftbl_lookup(vftbl, m);
494 STATISTICS(jnicallXmethodnvokation());
496 l = vm_call_method_long_valist(resm, o, ap);
502 /* _Jv_jni_CallLongMethodA *****************************************************
504 Internal function to call Java long methods.
506 *******************************************************************************/
508 static jlong _Jv_jni_CallLongMethodA(java_handle_t *o, vftbl_t *vftbl,
509 methodinfo *m, const jvalue *args)
514 STATISTICS(jniinvokation());
517 exceptions_throw_nullpointerexception();
521 /* Class initialization is done by the JIT compiler. This is ok
522 since a static method always belongs to the declaring class. */
524 if (m->flags & ACC_STATIC) {
525 /* For static methods we reset the object. */
530 /* for convenience */
535 /* For instance methods we make a virtual function table lookup. */
537 resm = method_vftbl_lookup(vftbl, m);
540 STATISTICS(jnicallXmethodnvokation());
542 l = vm_call_method_long_jvalue(resm, o, args);
548 /* _Jv_jni_CallFloatMethod *****************************************************
550 Internal function to call Java float methods.
552 *******************************************************************************/
554 static jfloat _Jv_jni_CallFloatMethod(java_handle_t *o, vftbl_t *vftbl,
555 methodinfo *m, va_list ap)
560 /* Class initialization is done by the JIT compiler. This is ok
561 since a static method always belongs to the declaring class. */
563 if (m->flags & ACC_STATIC) {
564 /* For static methods we reset the object. */
569 /* for convenience */
574 /* For instance methods we make a virtual function table lookup. */
576 resm = method_vftbl_lookup(vftbl, m);
579 STATISTICS(jnicallXmethodnvokation());
581 f = vm_call_method_float_valist(resm, o, ap);
587 /* _Jv_jni_CallFloatMethodA ****************************************************
589 Internal function to call Java float methods.
591 *******************************************************************************/
593 static jfloat _Jv_jni_CallFloatMethodA(java_handle_t *o, vftbl_t *vftbl,
594 methodinfo *m, const jvalue *args)
599 /* Class initialization is done by the JIT compiler. This is ok
600 since a static method always belongs to the declaring class. */
602 if (m->flags & ACC_STATIC) {
603 /* For static methods we reset the object. */
608 /* for convenience */
613 /* For instance methods we make a virtual function table lookup. */
615 resm = method_vftbl_lookup(vftbl, m);
618 STATISTICS(jnicallXmethodnvokation());
620 f = vm_call_method_float_jvalue(resm, o, args);
626 /* _Jv_jni_CallDoubleMethod ****************************************************
628 Internal function to call Java double methods.
630 *******************************************************************************/
632 static jdouble _Jv_jni_CallDoubleMethod(java_handle_t *o, vftbl_t *vftbl,
633 methodinfo *m, va_list ap)
638 /* Class initialization is done by the JIT compiler. This is ok
639 since a static method always belongs to the declaring class. */
641 if (m->flags & ACC_STATIC) {
642 /* For static methods we reset the object. */
647 /* for convenience */
652 /* For instance methods we make a virtual function table lookup. */
654 resm = method_vftbl_lookup(vftbl, m);
657 d = vm_call_method_double_valist(resm, o, ap);
663 /* _Jv_jni_CallDoubleMethodA ***************************************************
665 Internal function to call Java double methods.
667 *******************************************************************************/
669 static jdouble _Jv_jni_CallDoubleMethodA(java_handle_t *o, vftbl_t *vftbl,
670 methodinfo *m, const jvalue *args)
675 /* Class initialization is done by the JIT compiler. This is ok
676 since a static method always belongs to the declaring class. */
678 if (m->flags & ACC_STATIC) {
679 /* For static methods we reset the object. */
684 /* for convenience */
689 /* For instance methods we make a virtual function table lookup. */
691 resm = method_vftbl_lookup(vftbl, m);
694 d = vm_call_method_double_jvalue(resm, o, args);
700 /* _Jv_jni_CallVoidMethod ******************************************************
702 Internal function to call Java void methods.
704 *******************************************************************************/
706 static void _Jv_jni_CallVoidMethod(java_handle_t *o, vftbl_t *vftbl,
707 methodinfo *m, va_list ap)
712 exceptions_throw_nullpointerexception();
716 /* Class initialization is done by the JIT compiler. This is ok
717 since a static method always belongs to the declaring class. */
719 if (m->flags & ACC_STATIC) {
720 /* For static methods we reset the object. */
725 /* for convenience */
730 /* For instance methods we make a virtual function table lookup. */
732 resm = method_vftbl_lookup(vftbl, m);
735 STATISTICS(jnicallXmethodnvokation());
737 (void) vm_call_method_valist(resm, o, ap);
741 /* _Jv_jni_CallVoidMethodA *****************************************************
743 Internal function to call Java void methods.
745 *******************************************************************************/
747 static void _Jv_jni_CallVoidMethodA(java_handle_t *o, vftbl_t *vftbl,
748 methodinfo *m, const jvalue *args)
753 exceptions_throw_nullpointerexception();
757 /* Class initialization is done by the JIT compiler. This is ok
758 since a static method always belongs to the declaring class. */
760 if (m->flags & ACC_STATIC) {
761 /* For static methods we reset the object. */
766 /* for convenience */
771 /* For instance methods we make a virtual function table lookup. */
773 resm = method_vftbl_lookup(vftbl, m);
776 STATISTICS(jnicallXmethodnvokation());
778 (void) vm_call_method_jvalue(resm, o, args);
782 // JNI functions are exported as C functions.
785 /* GetVersion ******************************************************************
787 Returns the major version number in the higher 16 bits and the
788 minor version number in the lower 16 bits.
790 *******************************************************************************/
792 jint _Jv_JNI_GetVersion(JNIEnv *env)
794 TRACEJNICALLS(("_Jv_JNI_GetVersion(env=%p)", env));
796 /* 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)
1453 TRACEJNICALLS(("jni_FromReflectedMethod(env=%p, method=%p)", env, method));
1455 java_lang_Object o(method);
1460 if (o.get_Class() == class_java_lang_reflect_Constructor) {
1461 java_lang_reflect_Constructor rc(method);
1462 m = rc.get_method();
1465 assert(o.get_Class() == class_java_lang_reflect_Method);
1467 java_lang_reflect_Method rm(method);
1468 m = rm.get_method();
1471 return (jmethodID) m;
1473 vm_abort("jni_FromReflectedMethod: Not implemented in this configuration.");
1475 // Keep compiler happy.
1481 /* FromReflectedField **********************************************************
1483 Converts a java.lang.reflect.Field to a field ID.
1485 *******************************************************************************/
1487 jfieldID jni_FromReflectedField(JNIEnv* env, jobject field)
1489 #if defined(ENABLE_JAVASE)
1491 TRACEJNICALLS(("jni_FromReflectedField(env=%p, field=%p)", env, field));
1493 java_lang_reflect_Field rf(field);
1498 fieldinfo* f = rf.get_field();
1500 return (jfieldID) f;
1502 vm_abort("jni_FromReflectedField: Not implemented in this configuration.");
1504 // Keep compiler happy.
1510 /* ToReflectedMethod ***********************************************************
1512 Converts a method ID derived from cls to an instance of the
1513 java.lang.reflect.Method class or to an instance of the
1514 java.lang.reflect.Constructor class.
1516 *******************************************************************************/
1518 jobject jni_ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic)
1520 #if defined(ENABLE_JAVASE)
1521 TRACEJNICALLS(("jni_ToReflectedMethod(env=%p, cls=%p, methodID=%p, isStatic=%d)", env, cls, methodID, isStatic));
1523 methodinfo* m = (methodinfo *) methodID;
1525 /* HotSpot does the same assert. */
1527 assert(((m->flags & ACC_STATIC) != 0) == (isStatic != 0));
1531 if (m->name == utf_init) {
1532 h = java_lang_reflect_Constructor(m).get_handle();
1535 h = java_lang_reflect_Method(m).get_handle();
1540 vm_abort("jni_ToReflectedMethod: Not implemented in this configuration.");
1542 /* keep compiler happy */
1549 /* ToReflectedField ************************************************************
1551 Converts a field ID derived from cls to an instance of the
1552 java.lang.reflect.Field class.
1554 *******************************************************************************/
1556 jobject _Jv_JNI_ToReflectedField(JNIEnv* env, jclass cls, jfieldID fieldID,
1559 STATISTICS(jniinvokation());
1561 log_text("JNI-Call: ToReflectedField: IMPLEMENT ME!");
1567 /* Calling Instance Methods ***************************************************/
1569 /* GetMethodID *****************************************************************
1571 Returns the method ID for an instance (nonstatic) method of a class
1572 or interface. The method may be defined in one of the clazz's
1573 superclasses and inherited by clazz. The method is determined by
1574 its name and signature.
1576 GetMethodID() causes an uninitialized class to be initialized.
1578 *******************************************************************************/
1580 jmethodID _Jv_JNI_GetMethodID(JNIEnv* env, jclass clazz, const char *name,
1588 STATISTICS(jniinvokation());
1590 c = LLNI_classinfo_unwrap(clazz);
1595 if (!(c->state & CLASS_INITIALIZED))
1596 if (!initialize_class(c))
1599 /* try to get the method of the class or one of it's superclasses */
1601 uname = utf_new_char((char *) name);
1602 udesc = utf_new_char((char *) sig);
1604 m = class_resolvemethod(c, uname, udesc);
1606 if ((m == NULL) || (m->flags & ACC_STATIC)) {
1607 exceptions_throw_nosuchmethoderror(c, uname, udesc);
1612 return (jmethodID) m;
1616 /* JNI-functions for calling instance methods *********************************/
1618 #define JNI_CALL_VIRTUAL_METHOD(name, type, intern) \
1619 type _Jv_JNI_Call##name##Method(JNIEnv *env, jobject obj, \
1620 jmethodID methodID, ...) \
1627 o = (java_handle_t *) obj; \
1628 m = (methodinfo *) methodID; \
1630 va_start(ap, methodID); \
1631 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, ap); \
1637 JNI_CALL_VIRTUAL_METHOD(Boolean, jboolean, Int)
1638 JNI_CALL_VIRTUAL_METHOD(Byte, jbyte, Int)
1639 JNI_CALL_VIRTUAL_METHOD(Char, jchar, Int)
1640 JNI_CALL_VIRTUAL_METHOD(Short, jshort, Int)
1641 JNI_CALL_VIRTUAL_METHOD(Int, jint, Int)
1642 JNI_CALL_VIRTUAL_METHOD(Long, jlong, Long)
1643 JNI_CALL_VIRTUAL_METHOD(Float, jfloat, Float)
1644 JNI_CALL_VIRTUAL_METHOD(Double, jdouble, Double)
1647 #define JNI_CALL_VIRTUAL_METHOD_V(name, type, intern) \
1648 type _Jv_JNI_Call##name##MethodV(JNIEnv *env, jobject obj, \
1649 jmethodID methodID, va_list args) \
1655 o = (java_handle_t *) obj; \
1656 m = (methodinfo *) methodID; \
1658 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, args); \
1663 JNI_CALL_VIRTUAL_METHOD_V(Boolean, jboolean, Int)
1664 JNI_CALL_VIRTUAL_METHOD_V(Byte, jbyte, Int)
1665 JNI_CALL_VIRTUAL_METHOD_V(Char, jchar, Int)
1666 JNI_CALL_VIRTUAL_METHOD_V(Short, jshort, Int)
1667 JNI_CALL_VIRTUAL_METHOD_V(Int, jint, Int)
1668 JNI_CALL_VIRTUAL_METHOD_V(Long, jlong, Long)
1669 JNI_CALL_VIRTUAL_METHOD_V(Float, jfloat, Float)
1670 JNI_CALL_VIRTUAL_METHOD_V(Double, jdouble, Double)
1673 #define JNI_CALL_VIRTUAL_METHOD_A(name, type, intern) \
1674 type _Jv_JNI_Call##name##MethodA(JNIEnv *env, jobject obj, \
1675 jmethodID methodID, \
1676 const jvalue *args) \
1682 o = (java_handle_t *) obj; \
1683 m = (methodinfo *) methodID; \
1685 ret = _Jv_jni_Call##intern##MethodA(o, LLNI_vftbl_direct(o), m, args); \
1690 JNI_CALL_VIRTUAL_METHOD_A(Boolean, jboolean, Int)
1691 JNI_CALL_VIRTUAL_METHOD_A(Byte, jbyte, Int)
1692 JNI_CALL_VIRTUAL_METHOD_A(Char, jchar, Int)
1693 JNI_CALL_VIRTUAL_METHOD_A(Short, jshort, Int)
1694 JNI_CALL_VIRTUAL_METHOD_A(Int, jint, Int)
1695 JNI_CALL_VIRTUAL_METHOD_A(Long, jlong, Long)
1696 JNI_CALL_VIRTUAL_METHOD_A(Float, jfloat, Float)
1697 JNI_CALL_VIRTUAL_METHOD_A(Double, jdouble, Double)
1700 jobject _Jv_JNI_CallObjectMethod(JNIEnv *env, jobject obj, jmethodID methodID,
1708 o = (java_handle_t *) obj;
1709 m = (methodinfo *) methodID;
1711 va_start(ap, methodID);
1712 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, ap);
1715 return jni_NewLocalRef(env, (jobject) ret);
1719 jobject _Jv_JNI_CallObjectMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1726 o = (java_handle_t *) obj;
1727 m = (methodinfo *) methodID;
1729 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, args);
1731 return jni_NewLocalRef(env, (jobject) ret);
1735 jobject _Jv_JNI_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1742 o = (java_handle_t *) obj;
1743 m = (methodinfo *) methodID;
1745 ret = _Jv_jni_CallObjectMethodA(o, LLNI_vftbl_direct(o), m, args);
1747 return jni_NewLocalRef(env, (jobject) ret);
1752 void _Jv_JNI_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
1758 o = (java_handle_t *) obj;
1759 m = (methodinfo *) methodID;
1761 va_start(ap, methodID);
1762 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1767 void _Jv_JNI_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1773 o = (java_handle_t *) obj;
1774 m = (methodinfo *) methodID;
1776 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1780 void _Jv_JNI_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1786 o = (java_handle_t *) obj;
1787 m = (methodinfo *) methodID;
1789 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1794 #define JNI_CALL_NONVIRTUAL_METHOD(name, type, intern) \
1795 type _Jv_JNI_CallNonvirtual##name##Method(JNIEnv *env, jobject obj, \
1796 jclass clazz, jmethodID methodID, \
1805 o = (java_handle_t *) obj; \
1806 c = LLNI_classinfo_unwrap(clazz); \
1807 m = (methodinfo *) methodID; \
1809 va_start(ap, methodID); \
1810 ret = _Jv_jni_Call##intern##Method(o, c->vftbl, m, ap); \
1816 JNI_CALL_NONVIRTUAL_METHOD(Boolean, jboolean, Int)
1817 JNI_CALL_NONVIRTUAL_METHOD(Byte, jbyte, Int)
1818 JNI_CALL_NONVIRTUAL_METHOD(Char, jchar, Int)
1819 JNI_CALL_NONVIRTUAL_METHOD(Short, jshort, Int)
1820 JNI_CALL_NONVIRTUAL_METHOD(Int, jint, Int)
1821 JNI_CALL_NONVIRTUAL_METHOD(Long, jlong, Long)
1822 JNI_CALL_NONVIRTUAL_METHOD(Float, jfloat, Float)
1823 JNI_CALL_NONVIRTUAL_METHOD(Double, jdouble, Double)
1826 #define JNI_CALL_NONVIRTUAL_METHOD_V(name, type, intern) \
1827 type _Jv_JNI_CallNonvirtual##name##MethodV(JNIEnv *env, jobject obj, \
1828 jclass clazz, jmethodID methodID, \
1836 o = (java_handle_t *) obj; \
1837 c = LLNI_classinfo_unwrap(clazz); \
1838 m = (methodinfo *) methodID; \
1840 ret = _Jv_jni_CallIntMethod(o, c->vftbl, m, args); \
1845 JNI_CALL_NONVIRTUAL_METHOD_V(Boolean, jboolean, Int)
1846 JNI_CALL_NONVIRTUAL_METHOD_V(Byte, jbyte, Int)
1847 JNI_CALL_NONVIRTUAL_METHOD_V(Char, jchar, Int)
1848 JNI_CALL_NONVIRTUAL_METHOD_V(Short, jshort, Int)
1849 JNI_CALL_NONVIRTUAL_METHOD_V(Int, jint, Int)
1850 JNI_CALL_NONVIRTUAL_METHOD_V(Long, jlong, Long)
1851 JNI_CALL_NONVIRTUAL_METHOD_V(Float, jfloat, Float)
1852 JNI_CALL_NONVIRTUAL_METHOD_V(Double, jdouble, Double)
1855 #define JNI_CALL_NONVIRTUAL_METHOD_A(name, type, intern) \
1856 type _Jv_JNI_CallNonvirtual##name##MethodA(JNIEnv *env, jobject obj, \
1857 jclass clazz, jmethodID methodID, \
1858 const jvalue *args) \
1860 log_text("JNI-Call: CallNonvirtual##name##MethodA: IMPLEMENT ME!"); \
1865 JNI_CALL_NONVIRTUAL_METHOD_A(Boolean, jboolean, Int)
1866 JNI_CALL_NONVIRTUAL_METHOD_A(Byte, jbyte, Int)
1867 JNI_CALL_NONVIRTUAL_METHOD_A(Char, jchar, Int)
1868 JNI_CALL_NONVIRTUAL_METHOD_A(Short, jshort, Int)
1869 JNI_CALL_NONVIRTUAL_METHOD_A(Int, jint, Int)
1870 JNI_CALL_NONVIRTUAL_METHOD_A(Long, jlong, Long)
1871 JNI_CALL_NONVIRTUAL_METHOD_A(Float, jfloat, Float)
1872 JNI_CALL_NONVIRTUAL_METHOD_A(Double, jdouble, Double)
1874 jobject _Jv_JNI_CallNonvirtualObjectMethod(JNIEnv *env, jobject obj,
1875 jclass clazz, jmethodID methodID,
1884 o = (java_handle_t *) obj;
1885 c = LLNI_classinfo_unwrap(clazz);
1886 m = (methodinfo *) methodID;
1888 va_start(ap, methodID);
1889 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, ap);
1892 return jni_NewLocalRef(env, (jobject) r);
1896 jobject _Jv_JNI_CallNonvirtualObjectMethodV(JNIEnv *env, jobject obj,
1897 jclass clazz, jmethodID methodID,
1905 o = (java_handle_t *) obj;
1906 c = LLNI_classinfo_unwrap(clazz);
1907 m = (methodinfo *) methodID;
1909 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, args);
1911 return jni_NewLocalRef(env, (jobject) r);
1915 jobject _Jv_JNI_CallNonvirtualObjectMethodA(JNIEnv *env, jobject obj,
1916 jclass clazz, jmethodID methodID,
1919 log_text("JNI-Call: CallNonvirtualObjectMethodA: IMPLEMENT ME!");
1921 return jni_NewLocalRef(env, NULL);
1925 void _Jv_JNI_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass clazz,
1926 jmethodID methodID, ...)
1933 o = (java_handle_t *) obj;
1934 c = LLNI_classinfo_unwrap(clazz);
1935 m = (methodinfo *) methodID;
1937 va_start(ap, methodID);
1938 _Jv_jni_CallVoidMethod(o, c->vftbl, m, ap);
1943 void _Jv_JNI_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass clazz,
1944 jmethodID methodID, va_list args)
1950 o = (java_handle_t *) obj;
1951 c = LLNI_classinfo_unwrap(clazz);
1952 m = (methodinfo *) methodID;
1954 _Jv_jni_CallVoidMethod(o, c->vftbl, m, args);
1958 void _Jv_JNI_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass clazz,
1959 jmethodID methodID, const jvalue * args)
1965 o = (java_handle_t *) obj;
1966 c = LLNI_classinfo_unwrap(clazz);
1967 m = (methodinfo *) methodID;
1969 _Jv_jni_CallVoidMethodA(o, c->vftbl, m, args);
1973 /* Accessing Fields of Objects ************************************************/
1975 /* GetFieldID ******************************************************************
1977 Returns the field ID for an instance (nonstatic) field of a
1978 class. The field is specified by its name and signature. The
1979 Get<type>Field and Set<type>Field families of accessor functions
1980 use field IDs to retrieve object fields.
1982 *******************************************************************************/
1984 jfieldID _Jv_JNI_GetFieldID(JNIEnv *env, jclass clazz, const char *name,
1992 STATISTICS(jniinvokation());
1994 c = LLNI_classinfo_unwrap(clazz);
1996 /* XXX NPE check? */
1998 uname = utf_new_char((char *) name);
1999 udesc = utf_new_char((char *) sig);
2001 f = class_findfield(c, uname, udesc);
2004 exceptions_throw_nosuchfielderror(c, uname);
2006 return (jfieldID) f;
2010 /* Get<type>Field Routines *****************************************************
2012 This family of accessor routines returns the value of an instance
2013 (nonstatic) field of an object. The field to access is specified by
2014 a field ID obtained by calling GetFieldID().
2016 *******************************************************************************/
2018 #define GET_FIELD(o,type,f) \
2019 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset)))
2021 #define JNI_GET_FIELD(name, type, intern) \
2022 type _Jv_JNI_Get##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID) \
2026 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "Field(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID)); \
2028 LLNI_CRITICAL_START; \
2030 ret = GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID); \
2032 LLNI_CRITICAL_END; \
2034 return (type) ret; \
2037 JNI_GET_FIELD(Boolean, jboolean, s4)
2038 JNI_GET_FIELD(Byte, jbyte, s4)
2039 JNI_GET_FIELD(Char, jchar, s4)
2040 JNI_GET_FIELD(Short, jshort, s4)
2041 JNI_GET_FIELD(Int, jint, s4)
2042 JNI_GET_FIELD(Long, jlong, s8)
2043 JNI_GET_FIELD(Float, jfloat, float)
2044 JNI_GET_FIELD(Double, jdouble, double)
2047 jobject _Jv_JNI_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)
2051 TRACEJNICALLS(("_Jv_JNI_GetObjectField(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID));
2053 LLNI_CRITICAL_START;
2055 o = LLNI_WRAP(GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), java_object_t*, fieldID));
2059 return jni_NewLocalRef(env, (jobject) o);
2063 /* Set<type>Field Routines *****************************************************
2065 This family of accessor routines sets the value of an instance
2066 (nonstatic) field of an object. The field to access is specified by
2067 a field ID obtained by calling GetFieldID().
2069 *******************************************************************************/
2071 #define SET_FIELD(o,type,f,value) \
2072 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset))) = (type) (value)
2074 #define JNI_SET_FIELD(name, type, intern) \
2075 void _Jv_JNI_Set##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID, \
2078 TRACEJNICALLS(("_Jv_JNI_Set" STR(name) "Field(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value)); \
2080 LLNI_CRITICAL_START; \
2082 SET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID, value); \
2084 LLNI_CRITICAL_START; \
2087 JNI_SET_FIELD(Boolean, jboolean, s4)
2088 JNI_SET_FIELD(Byte, jbyte, s4)
2089 JNI_SET_FIELD(Char, jchar, s4)
2090 JNI_SET_FIELD(Short, jshort, s4)
2091 JNI_SET_FIELD(Int, jint, s4)
2092 JNI_SET_FIELD(Long, jlong, s8)
2093 JNI_SET_FIELD(Float, jfloat, float)
2094 JNI_SET_FIELD(Double, jdouble, double)
2097 void _Jv_JNI_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID,
2100 TRACEJNICALLS(("_Jv_JNI_SetObjectField(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value));
2102 LLNI_CRITICAL_START;
2104 SET_FIELD(obj, java_handle_t*, fieldID, LLNI_UNWRAP((java_handle_t*) value));
2110 /* Calling Static Methods *****************************************************/
2112 /* GetStaticMethodID ***********************************************************
2114 Returns the method ID for a static method of a class. The method is
2115 specified by its name and signature.
2117 GetStaticMethodID() causes an uninitialized class to be
2120 *******************************************************************************/
2122 jmethodID _Jv_JNI_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name,
2130 TRACEJNICALLS(("_Jv_JNI_GetStaticMethodID(env=%p, clazz=%p, name=%s, sig=%s)", env, clazz, name, sig));
2132 c = LLNI_classinfo_unwrap(clazz);
2137 if (!(c->state & CLASS_INITIALIZED))
2138 if (!initialize_class(c))
2141 /* try to get the static method of the class */
2143 uname = utf_new_char((char *) name);
2144 udesc = utf_new_char((char *) sig);
2146 m = class_resolvemethod(c, uname, udesc);
2148 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
2149 exceptions_throw_nosuchmethoderror(c, uname, udesc);
2154 return (jmethodID) m;
2158 #define JNI_CALL_STATIC_METHOD(name, type, intern) \
2159 type _Jv_JNI_CallStatic##name##Method(JNIEnv *env, jclass clazz, \
2160 jmethodID methodID, ...) \
2166 m = (methodinfo *) methodID; \
2168 va_start(ap, methodID); \
2169 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, ap); \
2175 JNI_CALL_STATIC_METHOD(Boolean, jboolean, Int)
2176 JNI_CALL_STATIC_METHOD(Byte, jbyte, Int)
2177 JNI_CALL_STATIC_METHOD(Char, jchar, Int)
2178 JNI_CALL_STATIC_METHOD(Short, jshort, Int)
2179 JNI_CALL_STATIC_METHOD(Int, jint, Int)
2180 JNI_CALL_STATIC_METHOD(Long, jlong, Long)
2181 JNI_CALL_STATIC_METHOD(Float, jfloat, Float)
2182 JNI_CALL_STATIC_METHOD(Double, jdouble, Double)
2185 #define JNI_CALL_STATIC_METHOD_V(name, type, intern) \
2186 type _Jv_JNI_CallStatic##name##MethodV(JNIEnv *env, jclass clazz, \
2187 jmethodID methodID, va_list args) \
2192 m = (methodinfo *) methodID; \
2194 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, args); \
2199 JNI_CALL_STATIC_METHOD_V(Boolean, jboolean, Int)
2200 JNI_CALL_STATIC_METHOD_V(Byte, jbyte, Int)
2201 JNI_CALL_STATIC_METHOD_V(Char, jchar, Int)
2202 JNI_CALL_STATIC_METHOD_V(Short, jshort, Int)
2203 JNI_CALL_STATIC_METHOD_V(Int, jint, Int)
2204 JNI_CALL_STATIC_METHOD_V(Long, jlong, Long)
2205 JNI_CALL_STATIC_METHOD_V(Float, jfloat, Float)
2206 JNI_CALL_STATIC_METHOD_V(Double, jdouble, Double)
2209 #define JNI_CALL_STATIC_METHOD_A(name, type, intern) \
2210 type _Jv_JNI_CallStatic##name##MethodA(JNIEnv *env, jclass clazz, \
2211 jmethodID methodID, const jvalue *args) \
2216 m = (methodinfo *) methodID; \
2218 res = _Jv_jni_Call##intern##MethodA(NULL, NULL, m, args); \
2223 JNI_CALL_STATIC_METHOD_A(Boolean, jboolean, Int)
2224 JNI_CALL_STATIC_METHOD_A(Byte, jbyte, Int)
2225 JNI_CALL_STATIC_METHOD_A(Char, jchar, Int)
2226 JNI_CALL_STATIC_METHOD_A(Short, jshort, Int)
2227 JNI_CALL_STATIC_METHOD_A(Int, jint, Int)
2228 JNI_CALL_STATIC_METHOD_A(Long, jlong, Long)
2229 JNI_CALL_STATIC_METHOD_A(Float, jfloat, Float)
2230 JNI_CALL_STATIC_METHOD_A(Double, jdouble, Double)
2233 jobject _Jv_JNI_CallStaticObjectMethod(JNIEnv *env, jclass clazz,
2234 jmethodID methodID, ...)
2240 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2242 m = (methodinfo *) methodID;
2244 va_start(ap, methodID);
2245 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, ap);
2248 return jni_NewLocalRef(env, (jobject) o);
2252 jobject _Jv_JNI_CallStaticObjectMethodV(JNIEnv *env, jclass clazz,
2253 jmethodID methodID, va_list args)
2258 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2260 m = (methodinfo *) methodID;
2262 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, args);
2264 return jni_NewLocalRef(env, (jobject) o);
2268 jobject _Jv_JNI_CallStaticObjectMethodA(JNIEnv *env, jclass clazz,
2269 jmethodID methodID, const jvalue *args)
2274 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2276 m = (methodinfo *) methodID;
2278 o = _Jv_jni_CallObjectMethodA(NULL, NULL, m, args);
2280 return jni_NewLocalRef(env, (jobject) o);
2284 void _Jv_JNI_CallStaticVoidMethod(JNIEnv *env, jclass clazz,
2285 jmethodID methodID, ...)
2290 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2292 m = (methodinfo *) methodID;
2294 va_start(ap, methodID);
2295 _Jv_jni_CallVoidMethod(NULL, NULL, m, ap);
2300 void _Jv_JNI_CallStaticVoidMethodV(JNIEnv *env, jclass clazz,
2301 jmethodID methodID, va_list args)
2305 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodV(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2307 m = (methodinfo *) methodID;
2309 _Jv_jni_CallVoidMethod(NULL, NULL, m, args);
2313 void _Jv_JNI_CallStaticVoidMethodA(JNIEnv *env, jclass clazz,
2314 jmethodID methodID, const jvalue * args)
2318 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2320 m = (methodinfo *) methodID;
2322 _Jv_jni_CallVoidMethodA(NULL, NULL, m, args);
2326 /* Accessing Static Fields ****************************************************/
2328 /* GetStaticFieldID ************************************************************
2330 Returns the field ID for a static field of a class. The field is
2331 specified by its name and signature. The GetStatic<type>Field and
2332 SetStatic<type>Field families of accessor functions use field IDs
2333 to retrieve static fields.
2335 *******************************************************************************/
2337 jfieldID _Jv_JNI_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name,
2345 STATISTICS(jniinvokation());
2347 c = LLNI_classinfo_unwrap(clazz);
2349 uname = utf_new_char((char *) name);
2350 usig = utf_new_char((char *) sig);
2352 f = class_findfield(c, uname, usig);
2355 exceptions_throw_nosuchfielderror(c, uname);
2357 return (jfieldID) f;
2361 /* GetStatic<type>Field ********************************************************
2363 This family of accessor routines returns the value of a static
2366 *******************************************************************************/
2368 #define JNI_GET_STATIC_FIELD(name, type, field) \
2369 type _Jv_JNI_GetStatic##name##Field(JNIEnv *env, jclass clazz, \
2375 STATISTICS(jniinvokation()); \
2377 c = LLNI_classinfo_unwrap(clazz); \
2378 f = (fieldinfo *) fieldID; \
2380 if (!(c->state & CLASS_INITIALIZED)) \
2381 if (!initialize_class(c)) \
2384 return f->value->field; \
2387 JNI_GET_STATIC_FIELD(Boolean, jboolean, i)
2388 JNI_GET_STATIC_FIELD(Byte, jbyte, i)
2389 JNI_GET_STATIC_FIELD(Char, jchar, i)
2390 JNI_GET_STATIC_FIELD(Short, jshort, i)
2391 JNI_GET_STATIC_FIELD(Int, jint, i)
2392 JNI_GET_STATIC_FIELD(Long, jlong, l)
2393 JNI_GET_STATIC_FIELD(Float, jfloat, f)
2394 JNI_GET_STATIC_FIELD(Double, jdouble, d)
2397 jobject _Jv_JNI_GetStaticObjectField(JNIEnv *env, jclass clazz,
2404 STATISTICS(jniinvokation());
2406 c = LLNI_classinfo_unwrap(clazz);
2407 f = (fieldinfo *) fieldID;
2409 if (!(c->state & CLASS_INITIALIZED))
2410 if (!initialize_class(c))
2413 h = (java_handle_t*) LLNI_WRAP(f->value->a);
2415 return jni_NewLocalRef(env, (jobject) h);
2419 /* SetStatic<type>Field *******************************************************
2421 This family of accessor routines sets the value of a static field
2424 *******************************************************************************/
2426 #define JNI_SET_STATIC_FIELD(name, type, field) \
2427 void _Jv_JNI_SetStatic##name##Field(JNIEnv *env, jclass clazz, \
2434 STATISTICS(jniinvokation()); \
2436 c = LLNI_classinfo_unwrap(clazz); \
2437 f = (fieldinfo *) fieldID; \
2439 if (!(c->state & CLASS_INITIALIZED)) \
2440 if (!initialize_class(c)) \
2443 f->value->field = value; \
2446 JNI_SET_STATIC_FIELD(Boolean, jboolean, i)
2447 JNI_SET_STATIC_FIELD(Byte, jbyte, i)
2448 JNI_SET_STATIC_FIELD(Char, jchar, i)
2449 JNI_SET_STATIC_FIELD(Short, jshort, i)
2450 JNI_SET_STATIC_FIELD(Int, jint, i)
2451 JNI_SET_STATIC_FIELD(Long, jlong, l)
2452 JNI_SET_STATIC_FIELD(Float, jfloat, f)
2453 JNI_SET_STATIC_FIELD(Double, jdouble, d)
2456 void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID,
2462 STATISTICS(jniinvokation());
2464 c = LLNI_classinfo_unwrap(clazz);
2465 f = (fieldinfo *) fieldID;
2467 if (!(c->state & CLASS_INITIALIZED))
2468 if (!initialize_class(c))
2471 f->value->a = LLNI_UNWRAP((java_handle_t *) value);
2475 /* String Operations **********************************************************/
2477 /* NewString *******************************************************************
2479 Create new java.lang.String object from an array of Unicode
2482 *******************************************************************************/
2484 jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len)
2486 TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len));
2496 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2497 for (jsize i = 0; i < len; i++)
2500 java_handle_t* h = builtin_new(class_java_lang_String);
2505 java_lang_String s(h, ca.get_handle(), 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 CharArray ca(s.get_value());
2555 int32_t count = s.get_count();
2556 int32_t offset = s.get_offset();
2561 /* allocate memory */
2563 stringbuffer = MNEW(u2, count + 1);
2568 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2569 for (i = 0; i < count; i++)
2570 stringbuffer[i] = ptr[offset + i];
2572 /* terminate string */
2574 stringbuffer[i] = '\0';
2579 return (jchar*) stringbuffer;
2583 /* ReleaseStringChars **********************************************************
2585 Informs the VM that the native code no longer needs access to
2586 chars. The chars argument is a pointer obtained from string using
2589 *******************************************************************************/
2591 void _Jv_JNI_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)
2593 TRACEJNICALLS(("jni_ReleaseStringChars(env=%p, str=%p, chars=%p)", env, str, chars));
2596 if (chars == emptyStringJ)
2599 java_lang_String s(str);
2600 int32_t count = s.get_count();
2602 MFREE(((jchar*) chars), jchar, count + 1);
2606 /* NewStringUTF ****************************************************************
2608 Constructs a new java.lang.String object from an array of UTF-8
2611 *******************************************************************************/
2613 jstring jni_NewStringUTF(JNIEnv *env, const char *bytes)
2615 TRACEJNICALLS(("jni_NewStringUTF(env=%p, bytes=%s)", env, bytes));
2617 java_handle_t *h = javastring_safe_new_from_utf8(bytes);
2619 return (jstring) jni_NewLocalRef(env, (jobject) h);
2623 /****************** returns the utf8 length in bytes of a string *******************/
2625 jsize jni_GetStringUTFLength(JNIEnv *env, jstring string)
2627 TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string));
2629 java_lang_String s(string);
2630 CharArray ca(s.get_value());
2631 int32_t count = s.get_count();
2633 // FIXME GC critical section!
2634 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2635 int32_t length = u2_utflength(ptr, count);
2641 /* GetStringUTFChars ***********************************************************
2643 Returns a pointer to an array of UTF-8 characters of the
2644 string. This array is valid until it is released by
2645 ReleaseStringUTFChars().
2647 *******************************************************************************/
2649 const char *_Jv_JNI_GetStringUTFChars(JNIEnv *env, jstring string,
2654 STATISTICS(jniinvokation());
2662 u = javastring_toutf((java_handle_t *) string, false);
2671 /* ReleaseStringUTFChars *******************************************************
2673 Informs the VM that the native code no longer needs access to
2674 utf. The utf argument is a pointer derived from string using
2675 GetStringUTFChars().
2677 *******************************************************************************/
2679 void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf)
2681 STATISTICS(jniinvokation());
2683 /* XXX we don't release utf chars right now, perhaps that should be done
2684 later. Since there is always one reference the garbage collector will
2689 /* Array Operations ***********************************************************/
2691 /* GetArrayLength **************************************************************
2693 Returns the number of elements in the array.
2695 *******************************************************************************/
2697 jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array)
2699 TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array));
2703 jsize size = a.get_length();
2709 /* NewObjectArray **************************************************************
2711 Constructs a new array holding objects in class elementClass. All
2712 elements are initially set to initialElement.
2714 *******************************************************************************/
2716 jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length,
2717 jclass elementClass, jobject initialElement)
2723 STATISTICS(jniinvokation());
2725 c = LLNI_classinfo_unwrap(elementClass);
2726 o = (java_handle_t *) initialElement;
2729 exceptions_throw_negativearraysizeexception();
2733 ObjectArray oa(length, c);
2738 /* set all elements to initialElement */
2740 for (i = 0; i < length; i++)
2741 oa.set_element(i, o);
2743 return (jobjectArray) jni_NewLocalRef(env, (jobject) oa.get_handle());
2747 jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array,
2750 STATISTICS(jniinvokation());
2752 ObjectArray oa(array);
2754 if (index >= oa.get_length()) {
2755 exceptions_throw_arrayindexoutofboundsexception();
2759 java_handle_t* o = oa.get_element(index);
2761 return jni_NewLocalRef(env, (jobject) o);
2765 void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array,
2766 jsize index, jobject val)
2768 STATISTICS(jniinvokation());
2770 ObjectArray oa(array);
2772 if (index >= oa.get_length()) {
2773 exceptions_throw_arrayindexoutofboundsexception();
2777 /* check if the class of value is a subclass of the element class
2780 java_handle_t* o = (java_handle_t *) val;
2782 if (!builtin_canstore(oa.get_handle(), o))
2785 oa.set_element(index, o);
2789 #define JNI_NEW_ARRAY(name, type) \
2790 type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \
2792 STATISTICS(jniinvokation()); \
2795 exceptions_throw_negativearraysizeexception(); \
2799 name##Array a(len); \
2801 return (type) jni_NewLocalRef(env, \
2802 (jobject) a.get_handle()); \
2805 JNI_NEW_ARRAY(Boolean, jbooleanArray)
2806 JNI_NEW_ARRAY(Byte, jbyteArray)
2807 JNI_NEW_ARRAY(Char, jcharArray)
2808 JNI_NEW_ARRAY(Short, jshortArray)
2809 JNI_NEW_ARRAY(Int, jintArray)
2810 JNI_NEW_ARRAY(Long, jlongArray)
2811 JNI_NEW_ARRAY(Float, jfloatArray)
2812 JNI_NEW_ARRAY(Double, jdoubleArray)
2815 /* Get<PrimitiveType>ArrayElements *********************************************
2817 A family of functions that returns the body of the primitive array.
2819 *******************************************************************************/
2821 #define JNI_GET_ARRAY_ELEMENTS(name, type, intern) \
2822 type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \
2825 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \
2827 name##Array a(array); \
2830 *isCopy = JNI_FALSE; \
2832 return (type *) a.get_raw_data_ptr(); \
2835 JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean)
2836 JNI_GET_ARRAY_ELEMENTS(Byte, jbyte, byte)
2837 JNI_GET_ARRAY_ELEMENTS(Char, jchar, char)
2838 JNI_GET_ARRAY_ELEMENTS(Short, jshort, short)
2839 JNI_GET_ARRAY_ELEMENTS(Int, jint, int)
2840 JNI_GET_ARRAY_ELEMENTS(Long, jlong, long)
2841 JNI_GET_ARRAY_ELEMENTS(Float, jfloat, float)
2842 JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double)
2845 /* Release<PrimitiveType>ArrayElements *****************************************
2847 A family of functions that informs the VM that the native code no
2848 longer needs access to elems. The elems argument is a pointer
2849 derived from array using the corresponding
2850 Get<PrimitiveType>ArrayElements() function. If necessary, this
2851 function copies back all changes made to elems to the original
2854 *******************************************************************************/
2856 #define JNI_RELEASE_ARRAY_ELEMENTS(name, type, intern, intern2) \
2857 void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \
2858 type *elems, jint mode) \
2860 STATISTICS(jniinvokation()); \
2862 name##Array a(array); \
2864 if (elems != (type *) a.get_raw_data_ptr()) { \
2867 a.set_region(0, a.get_length(), (intern2 *) elems); \
2870 a.set_region(0, a.get_length(), (intern2 *) elems); \
2871 /* XXX TWISTI how should it be freed? */ \
2874 /* XXX TWISTI how should it be freed? */ \
2880 JNI_RELEASE_ARRAY_ELEMENTS(Boolean, jboolean, boolean, u1)
2881 JNI_RELEASE_ARRAY_ELEMENTS(Byte, jbyte, byte, s1)
2882 JNI_RELEASE_ARRAY_ELEMENTS(Char, jchar, char, u2)
2883 JNI_RELEASE_ARRAY_ELEMENTS(Short, jshort, short, s2)
2884 JNI_RELEASE_ARRAY_ELEMENTS(Int, jint, int, s4)
2885 JNI_RELEASE_ARRAY_ELEMENTS(Long, jlong, long, s8)
2886 JNI_RELEASE_ARRAY_ELEMENTS(Float, jfloat, float, float)
2887 JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double)
2890 /* Get<PrimitiveType>ArrayRegion **********************************************
2892 A family of functions that copies a region of a primitive array
2895 *******************************************************************************/
2897 #define JNI_GET_ARRAY_REGION(name, type, intern, intern2) \
2898 void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \
2899 jsize start, jsize len, type *buf) \
2901 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \
2903 name##Array a(array); \
2905 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2906 exceptions_throw_arrayindexoutofboundsexception(); \
2908 a.get_region(start, len, (intern2 *) buf); \
2911 JNI_GET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2912 JNI_GET_ARRAY_REGION(Byte, jbyte, byte, s1)
2913 JNI_GET_ARRAY_REGION(Char, jchar, char, u2)
2914 JNI_GET_ARRAY_REGION(Short, jshort, short, s2)
2915 JNI_GET_ARRAY_REGION(Int, jint, int, s4)
2916 JNI_GET_ARRAY_REGION(Long, jlong, long, s8)
2917 JNI_GET_ARRAY_REGION(Float, jfloat, float, float)
2918 JNI_GET_ARRAY_REGION(Double, jdouble, double, double)
2921 /* Set<PrimitiveType>ArrayRegion **********************************************
2923 A family of functions that copies back a region of a primitive
2924 array from a buffer.
2926 *******************************************************************************/
2928 #define JNI_SET_ARRAY_REGION(name, type, intern, intern2) \
2929 void _Jv_JNI_Set##name##ArrayRegion(JNIEnv *env, type##Array array, \
2930 jsize start, jsize len, const type *buf) \
2932 STATISTICS(jniinvokation()); \
2934 name##Array a(array); \
2936 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2937 exceptions_throw_arrayindexoutofboundsexception(); \
2939 a.set_region(start, len, (intern2 *) buf); \
2942 JNI_SET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2943 JNI_SET_ARRAY_REGION(Byte, jbyte, byte, s1)
2944 JNI_SET_ARRAY_REGION(Char, jchar, char, u2)
2945 JNI_SET_ARRAY_REGION(Short, jshort, short, s2)
2946 JNI_SET_ARRAY_REGION(Int, jint, int, s4)
2947 JNI_SET_ARRAY_REGION(Long, jlong, long, s8)
2948 JNI_SET_ARRAY_REGION(Float, jfloat, float, float)
2949 JNI_SET_ARRAY_REGION(Double, jdouble, double, double)
2952 /* Registering Native Methods *************************************************/
2954 /* RegisterNatives *************************************************************
2956 Registers native methods with the class specified by the clazz
2957 argument. The methods parameter specifies an array of
2958 JNINativeMethod structures that contain the names, signatures, and
2959 function pointers of the native methods. The nMethods parameter
2960 specifies the number of native methods in the array.
2962 *******************************************************************************/
2964 jint jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods)
2966 TRACEJNICALLS(("jni_RegisterNatives(env=%p, clazz=%p, methods=%p, nMethods=%d)", env, clazz, methods, nMethods));
2968 classinfo* c = LLNI_classinfo_unwrap(clazz);
2970 /* XXX: if implemented this needs a call to jvmti_NativeMethodBind
2971 if (jvmti) jvmti_NativeMethodBind(method, address, new_address_ptr);
2974 NativeMethods& nm = VM::get_current()->get_nativemethods();
2975 nm.register_methods(c->name, methods, nMethods);
2981 /* UnregisterNatives ***********************************************************
2983 Unregisters native methods of a class. The class goes back to the
2984 state before it was linked or registered with its native method
2987 This function should not be used in normal native code. Instead, it
2988 provides special programs a way to reload and relink native
2991 *******************************************************************************/
2993 jint _Jv_JNI_UnregisterNatives(JNIEnv *env, jclass clazz)
2995 STATISTICS(jniinvokation());
2997 /* XXX TWISTI hmm, maybe we should not support that (like kaffe) */
2999 log_text("JNI-Call: UnregisterNatives: IMPLEMENT ME!!!");
3005 /* Monitor Operations *********************************************************/
3007 /* MonitorEnter ****************************************************************
3009 Enters the monitor associated with the underlying Java object
3012 *******************************************************************************/
3014 jint _Jv_JNI_MonitorEnter(JNIEnv *env, jobject obj)
3016 STATISTICS(jniinvokation());
3019 exceptions_throw_nullpointerexception();
3023 LOCK_MONITOR_ENTER(obj);
3029 /* MonitorExit *****************************************************************
3031 The current thread must be the owner of the monitor associated with
3032 the underlying Java object referred to by obj. The thread
3033 decrements the counter indicating the number of times it has
3034 entered this monitor. If the value of the counter becomes zero, the
3035 current thread releases the monitor.
3037 *******************************************************************************/
3039 jint _Jv_JNI_MonitorExit(JNIEnv *env, jobject obj)
3041 STATISTICS(jniinvokation());
3044 exceptions_throw_nullpointerexception();
3048 LOCK_MONITOR_EXIT(obj);
3054 /* JavaVM Interface ***********************************************************/
3056 /* GetJavaVM *******************************************************************
3058 Returns the Java VM interface (used in the Invocation API)
3059 associated with the current thread. The result is placed at the
3060 location pointed to by the second argument, vm.
3062 *******************************************************************************/
3064 jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm)
3066 STATISTICS(jniinvokation());
3068 *javavm = VM::get_current()->get_javavm();
3074 /* GetStringRegion *************************************************************
3076 Copies len number of Unicode characters beginning at offset start
3077 to the given buffer buf.
3079 Throws StringIndexOutOfBoundsException on index overflow.
3081 *******************************************************************************/
3083 void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf)
3085 java_lang_String s(str);
3086 CharArray ca(s.get_value());
3087 int32_t count = s.get_count();
3089 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3090 exceptions_throw_stringindexoutofboundsexception();
3094 ca.get_region(start, len, buf);
3098 /* GetStringUTFRegion **********************************************************
3100 Translates len number of Unicode characters beginning at offset
3101 start into UTF-8 format and place the result in the given buffer
3104 Throws StringIndexOutOfBoundsException on index overflow.
3106 *******************************************************************************/
3108 void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char *buf)
3110 TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf));
3112 java_lang_String s(str);
3114 CharArray ca(s.get_value());
3116 int32_t count = s.get_count();
3117 int32_t offset = s.get_offset();
3119 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3120 exceptions_throw_stringindexoutofboundsexception();
3127 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
3128 for (i = 0; i < len; i++)
3129 buf[i] = ptr[offset + start + i];
3135 /* GetPrimitiveArrayCritical ***************************************************
3137 Obtain a direct pointer to array elements.
3139 ATTENTION: Critical section keeps open when this function returns!
3140 See ReleasePrimitiveArrayCritical.
3142 *******************************************************************************/
3144 void* jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)
3148 arraydescriptor* ad;
3151 TRACEJNICALLS(("jni_GetPrimitiveArrayCritical(env=%p, array=%p, isCopy=%d)", env, array, isCopy));
3153 if (isCopy != NULL) {
3154 *isCopy = JNI_FALSE;
3157 LLNI_CRITICAL_START;
3159 h = (java_handle_t*) array;
3160 a = (java_array_t*) LLNI_UNWRAP(h);
3161 ad = a->objheader.vftbl->arraydesc;
3167 data = (void*) (((intptr_t) a) + ad->dataoffset);
3173 /* ReleasePrimitiveArrayCritical ***********************************************
3175 No specific documentation.
3177 ATTENTION: This function closes the critical section opened in
3178 GetPrimitiveArrayCritical!
3180 *******************************************************************************/
3182 void jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)
3184 TRACEJNICALLS(("jni_ReleasePrimitiveArrayCritical(env=%p, array=%p, carray=%p, mode=%d)", env, array, carray, mode));
3190 /* GetStringCritical ***********************************************************
3192 The semantics of these two functions are similar to the existing
3193 Get/ReleaseStringChars functions.
3195 *******************************************************************************/
3197 const jchar *_Jv_JNI_GetStringCritical(JNIEnv *env, jstring string,
3200 STATISTICS(jniinvokation());
3202 return jni_GetStringChars(env, string, isCopy);
3206 void _Jv_JNI_ReleaseStringCritical(JNIEnv *env, jstring string,
3207 const jchar *cstring)
3209 STATISTICS(jniinvokation());
3211 _Jv_JNI_ReleaseStringChars(env, string, cstring);
3215 jweak _Jv_JNI_NewWeakGlobalRef(JNIEnv* env, jobject obj)
3217 TRACEJNICALLS(("_Jv_JNI_NewWeakGlobalRef(env=%p, obj=%p): IMPLEMENT ME!", env, obj));
3223 void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
3225 TRACEJNICALLS(("_Jv_JNI_DeleteWeakGlobalRef(env=%p, ref=%p): IMPLEMENT ME", env, ref));
3229 /* NewGlobalRef ****************************************************************
3231 Creates a new global reference to the object referred to by the obj
3234 *******************************************************************************/
3236 jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
3238 hashtable_global_ref_entry *gre;
3239 u4 key; /* hashkey */
3240 u4 slot; /* slot in hashtable */
3243 TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
3245 o = (java_handle_t *) obj;
3247 hashtable_global_ref->mutex->lock();
3249 LLNI_CRITICAL_START;
3251 /* normally addresses are aligned to 4, 8 or 16 bytes */
3253 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3254 slot = key & (hashtable_global_ref->size - 1);
3255 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3257 /* search external hash chain for the entry */
3260 if (gre->o == LLNI_DIRECT(o)) {
3261 /* global object found, increment the reference */
3268 gre = gre->hashlink; /* next element in external chain */
3273 /* global ref not found, create a new one */
3276 gre = (hashtable_global_ref_entry*) heap_alloc_uncollectable(sizeof(hashtable_global_ref_entry));
3278 #if defined(ENABLE_GC_CACAO)
3279 /* register global ref with the GC */
3281 gc_reference_register(&(gre->o), GC_REFTYPE_JNI_GLOBALREF);
3284 LLNI_CRITICAL_START;
3286 gre->o = LLNI_DIRECT(o);
3291 /* insert entry into hashtable */
3293 gre->hashlink = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3295 hashtable_global_ref->ptr[slot] = gre;
3297 /* update number of hashtable-entries */
3299 hashtable_global_ref->entries++;
3302 hashtable_global_ref->mutex->unlock();
3304 #if defined(ENABLE_HANDLES)
3312 /* DeleteGlobalRef *************************************************************
3314 Deletes the global reference pointed to by globalRef.
3316 *******************************************************************************/
3318 void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
3320 hashtable_global_ref_entry *gre;
3321 hashtable_global_ref_entry *prevgre;
3322 u4 key; /* hashkey */
3323 u4 slot; /* slot in hashtable */
3326 TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
3328 o = (java_handle_t *) globalRef;
3330 hashtable_global_ref->mutex->lock();
3332 LLNI_CRITICAL_START;
3334 /* normally addresses are aligned to 4, 8 or 16 bytes */
3336 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3337 slot = key & (hashtable_global_ref->size - 1);
3338 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3340 /* initialize prevgre */
3344 /* search external hash chain for the entry */
3347 if (gre->o == LLNI_DIRECT(o)) {
3348 /* global object found, decrement the reference count */
3352 /* if reference count is 0, remove the entry */
3354 if (gre->refs == 0) {
3355 /* special handling if it's the first in the chain */
3357 if (prevgre == NULL)
3358 hashtable_global_ref->ptr[slot] = gre->hashlink;
3360 prevgre->hashlink = gre->hashlink;
3362 #if defined(ENABLE_GC_CACAO)
3363 /* unregister global ref with the GC */
3365 gc_reference_unregister(&(gre->o));
3373 hashtable_global_ref->mutex->unlock();
3378 prevgre = gre; /* save current pointer for removal */
3379 gre = gre->hashlink; /* next element in external chain */
3382 log_println("jni_DeleteGlobalRef: Global reference not found.");
3386 hashtable_global_ref->mutex->unlock();
3390 /* ExceptionCheck **************************************************************
3392 Returns JNI_TRUE when there is a pending exception; otherwise,
3395 *******************************************************************************/
3397 jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
3401 STATISTICS(jniinvokation());
3403 o = exceptions_get_exception();
3405 return (o != NULL) ? JNI_TRUE : JNI_FALSE;
3409 /* New JNI 1.4 functions ******************************************************/
3411 /* NewDirectByteBuffer *********************************************************
3413 Allocates and returns a direct java.nio.ByteBuffer referring to the
3414 block of memory starting at the memory address address and
3415 extending capacity bytes.
3417 *******************************************************************************/
3419 jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
3421 #if defined(ENABLE_JAVASE)
3422 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3423 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3425 // Allocate a gnu.classpath.Pointer{32,64} object.
3427 # if SIZEOF_VOID_P == 8
3428 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer64);
3430 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer32);
3436 gnu_classpath_Pointer p(h, address);
3438 // Create a java.nio.DirectByteBufferImpl$ReadWrite object.
3440 java_handle_t* nbuf =
3441 (java_handle_t*) jni_NewObject(env, (jclass) class_java_nio_DirectByteBufferImpl_ReadWrite,
3442 (jmethodID) dbbirw_init, NULL, p.get_handle(),
3443 (jint) capacity, (jint) capacity, (jint) 0);
3445 // Add a local reference and return the value.
3447 TRACEJNICALLSEXIT(("->%p", nbuf));
3449 return jni_NewLocalRef(env, (jobject) nbuf);
3451 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3457 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3459 /* Be paranoid about address sign-extension. */
3461 addr = (int64_t) ((uintptr_t) address);
3462 cap = (int32_t) capacity;
3464 o = jni_NewObject(env, (jclass) class_java_nio_DirectByteBuffer,
3465 (jmethodID) dbb_init, addr, cap);
3467 /* Add local reference and return the value. */
3469 TRACEJNICALLSEXIT(("->%p", o));
3471 return jni_NewLocalRef(env, o);
3474 # error unknown classpath configuration
3478 vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
3480 /* keep compiler happy */
3487 /* GetDirectBufferAddress ******************************************************
3489 Fetches and returns the starting address of the memory region
3490 referenced by the given direct java.nio.Buffer.
3492 *******************************************************************************/
3494 void* jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3496 #if defined(ENABLE_JAVASE)
3497 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3499 TRACEJNICALLSENTER(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3501 /* Prevent compiler warning. */
3503 java_handle_t* h = (java_handle_t *) buf;
3505 if ((h != NULL) && !builtin_instanceof(h, class_java_nio_Buffer))
3508 java_nio_DirectByteBufferImpl dbb(buf);
3509 java_handle_t* address = dbb.get_address();
3511 if (address == NULL) {
3512 TRACEJNICALLSEXIT(("->%p", NULL));
3516 gnu_classpath_Pointer p(address);
3517 void* data = p.get_data();
3519 TRACEJNICALLSEXIT(("->%p", data));
3523 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3525 TRACEJNICALLS(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3527 java_nio_Buffer jnb(buf);
3529 if (jnb.is_non_null() && !builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3532 void* address = jnb.get_address();
3537 # error unknown classpath configuration
3542 vm_abort("jni_GetDirectBufferAddress: Not implemented in this configuration.");
3544 // Keep compiler happy.
3551 /* GetDirectBufferCapacity *****************************************************
3553 Fetches and returns the capacity in bytes of the memory region
3554 referenced by the given direct java.nio.Buffer.
3556 *******************************************************************************/
3558 jlong jni_GetDirectBufferCapacity(JNIEnv* env, jobject buf)
3560 #if defined(ENABLE_JAVASE) && defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3561 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3563 java_handle_t* h = (java_handle_t *) buf;
3565 if (!builtin_instanceof(h, class_java_nio_DirectByteBufferImpl))
3568 java_nio_Buffer b(h);
3569 jlong capacity = b.get_cap();
3573 vm_abort("jni_GetDirectBufferCapacity: not implemented in this configuration");
3575 // Keep compiler happy.
3582 /* GetObjectRefType ************************************************************
3584 Returns the type of the object referred to by the obj argument. The
3585 argument obj can either be a local, global or weak global
3588 *******************************************************************************/
3590 jobjectRefType jni_GetObjectRefType(JNIEnv *env, jobject obj)
3592 log_println("jni_GetObjectRefType: IMPLEMENT ME!");
3594 return (jobjectRefType) NULL;
3598 /* DestroyJavaVM ***************************************************************
3600 Unloads a Java VM and reclaims its resources. Only the main thread
3601 can unload the VM. The system waits until the main thread is only
3602 remaining user thread before it destroys the VM.
3604 *******************************************************************************/
3606 jint _Jv_JNI_DestroyJavaVM(JavaVM *javavm)
3610 TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(javavm=%p)", javavm));
3612 if (VM::get_current()->is_created() == false)
3615 status = vm_destroy(javavm);
3621 /* AttachCurrentThread *********************************************************
3623 Attaches the current thread to a Java VM. Returns a JNI interface
3624 pointer in the JNIEnv argument.
3626 Trying to attach a thread that is already attached is a no-op.
3628 A native thread cannot be attached simultaneously to two Java VMs.
3630 When a thread is attached to the VM, the context class loader is
3631 the bootstrap loader.
3633 *******************************************************************************/
3635 static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
3637 #if defined(ENABLE_THREADS)
3638 JavaVMAttachArgs *vm_aargs;
3641 /* If the current thread has already been attached, this operation
3644 result = thread_current_is_attached();
3646 if (result == true) {
3647 *p_env = VM::get_current()->get_jnienv();
3651 vm_aargs = (JavaVMAttachArgs *) thr_args;
3653 if (vm_aargs != NULL) {
3654 if ((vm_aargs->version != JNI_VERSION_1_2) &&
3655 (vm_aargs->version != JNI_VERSION_1_4))
3656 return JNI_EVERSION;
3659 if (!thread_attach_current_external_thread(vm_aargs, false))
3662 if (!localref_table_init())
3666 *p_env = VM::get_current()->get_jnienv();
3672 jint jni_AttachCurrentThread(JavaVM *javavm, void **p_env, void *thr_args)
3676 TRACEJNICALLS(("jni_AttachCurrentThread(javavm=%p, p_env=%p, thr_args=%p)", javavm, p_env, thr_args));
3678 if (VM::get_current()->is_created() == false)
3681 result = jni_attach_current_thread(p_env, thr_args, false);
3687 /* DetachCurrentThread *********************************************************
3689 Detaches the current thread from a Java VM. All Java monitors held
3690 by this thread are released. All Java threads waiting for this
3691 thread to die are notified.
3693 In JDK 1.1, the main thread cannot be detached from the VM. It must
3694 call DestroyJavaVM to unload the entire VM.
3696 In the JDK, the main thread can be detached from the VM.
3698 The main thread, which is the thread that created the Java VM,
3699 cannot be detached from the VM. Instead, the main thread must call
3700 JNI_DestroyJavaVM() to unload the entire VM.
3702 *******************************************************************************/
3704 jint jni_DetachCurrentThread(JavaVM *vm)
3706 #if defined(ENABLE_THREADS)
3709 TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
3711 /* If the current thread has already been detached, this operation
3714 result = thread_current_is_attached();
3716 if (result == false)
3719 /* We need to pop all frames before we can destroy the table. */
3721 localref_frame_pop_all();
3723 if (!localref_table_destroy())
3726 if (!thread_detach_current_external_thread())
3734 /* GetEnv **********************************************************************
3736 If the current thread is not attached to the VM, sets *env to NULL,
3737 and returns JNI_EDETACHED. If the specified version is not
3738 supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise,
3739 sets *env to the appropriate interface, and returns JNI_OK.
3741 *******************************************************************************/
3743 jint jni_GetEnv(JavaVM *javavm, void **env, jint version)
3745 TRACEJNICALLS(("jni_GetEnv(javavm=%p, env=%p, version=%d)", javavm, env, version));
3747 if (VM::get_current()->is_created() == false) {
3749 return JNI_EDETACHED;
3752 #if defined(ENABLE_THREADS)
3753 if (thread_get_current() == NULL) {
3756 return JNI_EDETACHED;
3760 /* Check the JNI version. */
3762 if (jni_version_check(version) == true) {
3763 *env = VM::get_current()->get_jnienv();
3767 #if defined(ENABLE_JVMTI)
3768 if ((version & JVMTI_VERSION_MASK_INTERFACE_TYPE)
3769 == JVMTI_VERSION_INTERFACE_JVMTI) {
3771 *env = (void *) jvmti_new_environment();
3780 return JNI_EVERSION;
3784 /* AttachCurrentThreadAsDaemon *************************************************
3786 Same semantics as AttachCurrentThread, but the newly-created
3787 java.lang.Thread instance is a daemon.
3789 If the thread has already been attached via either
3790 AttachCurrentThread or AttachCurrentThreadAsDaemon, this routine
3791 simply sets the value pointed to by penv to the JNIEnv of the
3792 current thread. In this case neither AttachCurrentThread nor this
3793 routine have any effect on the daemon status of the thread.
3795 *******************************************************************************/
3797 jint jni_AttachCurrentThreadAsDaemon(JavaVM *javavm, void **penv, void *args)
3801 TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(javavm=%p, penv=%p, args=%p)", javavm, penv, args));
3803 if (VM::get_current()->is_created() == false)
3806 result = jni_attach_current_thread(penv, args, true);
3812 /* JNI invocation table *******************************************************/
3814 const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
3819 _Jv_JNI_DestroyJavaVM,
3820 jni_AttachCurrentThread,
3821 jni_DetachCurrentThread,
3823 jni_AttachCurrentThreadAsDaemon
3827 /* JNI function table *********************************************************/
3829 struct JNINativeInterface_ _Jv_JNINativeInterface = {
3838 jni_FromReflectedMethod,
3839 jni_FromReflectedField,
3840 jni_ToReflectedMethod,
3842 _Jv_JNI_IsAssignableFrom,
3843 _Jv_JNI_ToReflectedField,
3847 _Jv_JNI_ExceptionOccurred,
3848 jni_ExceptionDescribe,
3855 jni_DeleteGlobalRef,
3857 _Jv_JNI_IsSameObject,
3859 jni_EnsureLocalCapacity,
3861 _Jv_JNI_AllocObject,
3867 _Jv_JNI_IsInstanceOf,
3869 _Jv_JNI_GetMethodID,
3871 _Jv_JNI_CallObjectMethod,
3872 _Jv_JNI_CallObjectMethodV,
3873 _Jv_JNI_CallObjectMethodA,
3874 _Jv_JNI_CallBooleanMethod,
3875 _Jv_JNI_CallBooleanMethodV,
3876 _Jv_JNI_CallBooleanMethodA,
3877 _Jv_JNI_CallByteMethod,
3878 _Jv_JNI_CallByteMethodV,
3879 _Jv_JNI_CallByteMethodA,
3880 _Jv_JNI_CallCharMethod,
3881 _Jv_JNI_CallCharMethodV,
3882 _Jv_JNI_CallCharMethodA,
3883 _Jv_JNI_CallShortMethod,
3884 _Jv_JNI_CallShortMethodV,
3885 _Jv_JNI_CallShortMethodA,
3886 _Jv_JNI_CallIntMethod,
3887 _Jv_JNI_CallIntMethodV,
3888 _Jv_JNI_CallIntMethodA,
3889 _Jv_JNI_CallLongMethod,
3890 _Jv_JNI_CallLongMethodV,
3891 _Jv_JNI_CallLongMethodA,
3892 _Jv_JNI_CallFloatMethod,
3893 _Jv_JNI_CallFloatMethodV,
3894 _Jv_JNI_CallFloatMethodA,
3895 _Jv_JNI_CallDoubleMethod,
3896 _Jv_JNI_CallDoubleMethodV,
3897 _Jv_JNI_CallDoubleMethodA,
3898 _Jv_JNI_CallVoidMethod,
3899 _Jv_JNI_CallVoidMethodV,
3900 _Jv_JNI_CallVoidMethodA,
3902 _Jv_JNI_CallNonvirtualObjectMethod,
3903 _Jv_JNI_CallNonvirtualObjectMethodV,
3904 _Jv_JNI_CallNonvirtualObjectMethodA,
3905 _Jv_JNI_CallNonvirtualBooleanMethod,
3906 _Jv_JNI_CallNonvirtualBooleanMethodV,
3907 _Jv_JNI_CallNonvirtualBooleanMethodA,
3908 _Jv_JNI_CallNonvirtualByteMethod,
3909 _Jv_JNI_CallNonvirtualByteMethodV,
3910 _Jv_JNI_CallNonvirtualByteMethodA,
3911 _Jv_JNI_CallNonvirtualCharMethod,
3912 _Jv_JNI_CallNonvirtualCharMethodV,
3913 _Jv_JNI_CallNonvirtualCharMethodA,
3914 _Jv_JNI_CallNonvirtualShortMethod,
3915 _Jv_JNI_CallNonvirtualShortMethodV,
3916 _Jv_JNI_CallNonvirtualShortMethodA,
3917 _Jv_JNI_CallNonvirtualIntMethod,
3918 _Jv_JNI_CallNonvirtualIntMethodV,
3919 _Jv_JNI_CallNonvirtualIntMethodA,
3920 _Jv_JNI_CallNonvirtualLongMethod,
3921 _Jv_JNI_CallNonvirtualLongMethodV,
3922 _Jv_JNI_CallNonvirtualLongMethodA,
3923 _Jv_JNI_CallNonvirtualFloatMethod,
3924 _Jv_JNI_CallNonvirtualFloatMethodV,
3925 _Jv_JNI_CallNonvirtualFloatMethodA,
3926 _Jv_JNI_CallNonvirtualDoubleMethod,
3927 _Jv_JNI_CallNonvirtualDoubleMethodV,
3928 _Jv_JNI_CallNonvirtualDoubleMethodA,
3929 _Jv_JNI_CallNonvirtualVoidMethod,
3930 _Jv_JNI_CallNonvirtualVoidMethodV,
3931 _Jv_JNI_CallNonvirtualVoidMethodA,
3935 _Jv_JNI_GetObjectField,
3936 _Jv_JNI_GetBooleanField,
3937 _Jv_JNI_GetByteField,
3938 _Jv_JNI_GetCharField,
3939 _Jv_JNI_GetShortField,
3940 _Jv_JNI_GetIntField,
3941 _Jv_JNI_GetLongField,
3942 _Jv_JNI_GetFloatField,
3943 _Jv_JNI_GetDoubleField,
3944 _Jv_JNI_SetObjectField,
3945 _Jv_JNI_SetBooleanField,
3946 _Jv_JNI_SetByteField,
3947 _Jv_JNI_SetCharField,
3948 _Jv_JNI_SetShortField,
3949 _Jv_JNI_SetIntField,
3950 _Jv_JNI_SetLongField,
3951 _Jv_JNI_SetFloatField,
3952 _Jv_JNI_SetDoubleField,
3954 _Jv_JNI_GetStaticMethodID,
3956 _Jv_JNI_CallStaticObjectMethod,
3957 _Jv_JNI_CallStaticObjectMethodV,
3958 _Jv_JNI_CallStaticObjectMethodA,
3959 _Jv_JNI_CallStaticBooleanMethod,
3960 _Jv_JNI_CallStaticBooleanMethodV,
3961 _Jv_JNI_CallStaticBooleanMethodA,
3962 _Jv_JNI_CallStaticByteMethod,
3963 _Jv_JNI_CallStaticByteMethodV,
3964 _Jv_JNI_CallStaticByteMethodA,
3965 _Jv_JNI_CallStaticCharMethod,
3966 _Jv_JNI_CallStaticCharMethodV,
3967 _Jv_JNI_CallStaticCharMethodA,
3968 _Jv_JNI_CallStaticShortMethod,
3969 _Jv_JNI_CallStaticShortMethodV,
3970 _Jv_JNI_CallStaticShortMethodA,
3971 _Jv_JNI_CallStaticIntMethod,
3972 _Jv_JNI_CallStaticIntMethodV,
3973 _Jv_JNI_CallStaticIntMethodA,
3974 _Jv_JNI_CallStaticLongMethod,
3975 _Jv_JNI_CallStaticLongMethodV,
3976 _Jv_JNI_CallStaticLongMethodA,
3977 _Jv_JNI_CallStaticFloatMethod,
3978 _Jv_JNI_CallStaticFloatMethodV,
3979 _Jv_JNI_CallStaticFloatMethodA,
3980 _Jv_JNI_CallStaticDoubleMethod,
3981 _Jv_JNI_CallStaticDoubleMethodV,
3982 _Jv_JNI_CallStaticDoubleMethodA,
3983 _Jv_JNI_CallStaticVoidMethod,
3984 _Jv_JNI_CallStaticVoidMethodV,
3985 _Jv_JNI_CallStaticVoidMethodA,
3987 _Jv_JNI_GetStaticFieldID,
3989 _Jv_JNI_GetStaticObjectField,
3990 _Jv_JNI_GetStaticBooleanField,
3991 _Jv_JNI_GetStaticByteField,
3992 _Jv_JNI_GetStaticCharField,
3993 _Jv_JNI_GetStaticShortField,
3994 _Jv_JNI_GetStaticIntField,
3995 _Jv_JNI_GetStaticLongField,
3996 _Jv_JNI_GetStaticFloatField,
3997 _Jv_JNI_GetStaticDoubleField,
3998 _Jv_JNI_SetStaticObjectField,
3999 _Jv_JNI_SetStaticBooleanField,
4000 _Jv_JNI_SetStaticByteField,
4001 _Jv_JNI_SetStaticCharField,
4002 _Jv_JNI_SetStaticShortField,
4003 _Jv_JNI_SetStaticIntField,
4004 _Jv_JNI_SetStaticLongField,
4005 _Jv_JNI_SetStaticFloatField,
4006 _Jv_JNI_SetStaticDoubleField,
4009 jni_GetStringLength,
4011 _Jv_JNI_ReleaseStringChars,
4014 jni_GetStringUTFLength,
4015 _Jv_JNI_GetStringUTFChars,
4016 _Jv_JNI_ReleaseStringUTFChars,
4018 _Jv_JNI_GetArrayLength,
4020 _Jv_JNI_NewObjectArray,
4021 _Jv_JNI_GetObjectArrayElement,
4022 _Jv_JNI_SetObjectArrayElement,
4024 _Jv_JNI_NewBooleanArray,
4025 _Jv_JNI_NewByteArray,
4026 _Jv_JNI_NewCharArray,
4027 _Jv_JNI_NewShortArray,
4028 _Jv_JNI_NewIntArray,
4029 _Jv_JNI_NewLongArray,
4030 _Jv_JNI_NewFloatArray,
4031 _Jv_JNI_NewDoubleArray,
4033 _Jv_JNI_GetBooleanArrayElements,
4034 _Jv_JNI_GetByteArrayElements,
4035 _Jv_JNI_GetCharArrayElements,
4036 _Jv_JNI_GetShortArrayElements,
4037 _Jv_JNI_GetIntArrayElements,
4038 _Jv_JNI_GetLongArrayElements,
4039 _Jv_JNI_GetFloatArrayElements,
4040 _Jv_JNI_GetDoubleArrayElements,
4042 _Jv_JNI_ReleaseBooleanArrayElements,
4043 _Jv_JNI_ReleaseByteArrayElements,
4044 _Jv_JNI_ReleaseCharArrayElements,
4045 _Jv_JNI_ReleaseShortArrayElements,
4046 _Jv_JNI_ReleaseIntArrayElements,
4047 _Jv_JNI_ReleaseLongArrayElements,
4048 _Jv_JNI_ReleaseFloatArrayElements,
4049 _Jv_JNI_ReleaseDoubleArrayElements,
4051 _Jv_JNI_GetBooleanArrayRegion,
4052 _Jv_JNI_GetByteArrayRegion,
4053 _Jv_JNI_GetCharArrayRegion,
4054 _Jv_JNI_GetShortArrayRegion,
4055 _Jv_JNI_GetIntArrayRegion,
4056 _Jv_JNI_GetLongArrayRegion,
4057 _Jv_JNI_GetFloatArrayRegion,
4058 _Jv_JNI_GetDoubleArrayRegion,
4059 _Jv_JNI_SetBooleanArrayRegion,
4060 _Jv_JNI_SetByteArrayRegion,
4061 _Jv_JNI_SetCharArrayRegion,
4062 _Jv_JNI_SetShortArrayRegion,
4063 _Jv_JNI_SetIntArrayRegion,
4064 _Jv_JNI_SetLongArrayRegion,
4065 _Jv_JNI_SetFloatArrayRegion,
4066 _Jv_JNI_SetDoubleArrayRegion,
4068 jni_RegisterNatives,
4069 _Jv_JNI_UnregisterNatives,
4071 _Jv_JNI_MonitorEnter,
4072 _Jv_JNI_MonitorExit,
4076 /* New JNI 1.2 functions. */
4078 jni_GetStringRegion,
4079 jni_GetStringUTFRegion,
4081 jni_GetPrimitiveArrayCritical,
4082 jni_ReleasePrimitiveArrayCritical,
4084 _Jv_JNI_GetStringCritical,
4085 _Jv_JNI_ReleaseStringCritical,
4087 _Jv_JNI_NewWeakGlobalRef,
4088 _Jv_JNI_DeleteWeakGlobalRef,
4090 _Jv_JNI_ExceptionCheck,
4092 /* New JNI 1.4 functions. */
4094 jni_NewDirectByteBuffer,
4095 jni_GetDirectBufferAddress,
4096 jni_GetDirectBufferCapacity,
4098 /* New JNI 1.6 functions. */
4100 jni_GetObjectRefType
4104 /* Invocation API Functions ***************************************************/
4106 /* JNI_GetDefaultJavaVMInitArgs ************************************************
4108 Returns a default configuration for the Java VM.
4110 *******************************************************************************/
4112 jint JNI_GetDefaultJavaVMInitArgs(void *vm_args)
4114 JavaVMInitArgs *_vm_args;
4116 _vm_args = (JavaVMInitArgs *) vm_args;
4118 /* GNU classpath currently supports JNI 1.2 */
4120 switch (_vm_args->version) {
4121 case JNI_VERSION_1_1:
4122 _vm_args->version = JNI_VERSION_1_1;
4125 case JNI_VERSION_1_2:
4126 case JNI_VERSION_1_4:
4127 _vm_args->ignoreUnrecognized = JNI_FALSE;
4128 _vm_args->options = NULL;
4129 _vm_args->nOptions = 0;
4140 /* JNI_GetCreatedJavaVMs *******************************************************
4142 Returns all Java VMs that have been created. Pointers to VMs are written in
4143 the buffer vmBuf in the order they are created. At most bufLen number of
4144 entries will be written. The total number of created VMs is returned in
4147 *******************************************************************************/
4149 jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
4151 TRACEJNICALLS(("JNI_GetCreatedJavaVMs(vmBuf=%p, jsize=%d, jsize=%p)", vmBuf, bufLen, nVMs));
4156 // We currently only support 1 VM running.
4158 vmBuf[0] = VM::get_current()->get_javavm();
4165 /* JNI_CreateJavaVM ************************************************************
4167 Loads and initializes a Java VM. The current thread becomes the main thread.
4168 Sets the env argument to the JNI interface pointer of the main thread.
4170 *******************************************************************************/
4172 jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
4174 TRACEJNICALLS(("JNI_CreateJavaVM(p_vm=%p, p_env=%p, vm_args=%p)", p_vm, p_env, vm_args));
4176 /* actually create the JVM */
4178 if (!VM_create(p_vm, p_env, vm_args))
4188 * These are local overrides for various environment variables in Emacs.
4189 * Please do not remove this and leave it at the end of the file, where
4190 * Emacs will automagically detect them.
4191 * ---------------------------------------------------------------------
4194 * indent-tabs-mode: t
4198 * vim:noexpandtab:sw=4:ts=4: