1 /* src/native/jni.cpp - implementation of the Java Native Interface functions
3 Copyright (C) 1996-2012
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "mm/memory.hpp"
37 #include "native/jni.hpp"
38 #include "native/llni.h"
39 #include "native/localref.hpp"
40 #include "native/native.hpp"
42 #if defined(ENABLE_JVMTI)
43 # include "native/jvmti/cacaodbg.h"
46 #include "threads/lock.hpp"
47 #include "threads/mutex.hpp"
48 #include "threads/thread.hpp"
50 #include "toolbox/logging.hpp"
52 #include "vm/array.hpp"
53 #include "vm/jit/builtin.hpp"
54 #include "vm/exceptions.hpp"
55 #include "vm/global.h"
56 #include "vm/globals.hpp"
57 #include "vm/initialize.hpp"
58 #include "vm/javaobjects.hpp"
59 #include "vm/loader.hpp"
60 #include "vm/options.h"
61 #include "vm/primitive.hpp"
62 #include "vm/resolve.hpp"
63 #include "vm/statistics.h"
64 #include "vm/string.hpp"
67 #include "vm/jit/asmpart.h"
68 #include "vm/jit/jit.hpp"
69 #include "vm/jit/stacktrace.hpp"
72 /* debug **********************************************************************/
76 # define TRACEJNICALLS(x) \
78 if (opt_TraceJNICalls) { \
83 # define TRACEJNICALLSENTER(x) \
85 if (opt_TraceJNICalls) { \
91 # define TRACEJNICALLSEXIT(x) \
93 if (opt_TraceJNICalls) { \
101 # define TRACEJNICALLS(x)
102 # define TRACEJNICALLSENTER(x)
103 # define TRACEJNICALLSEXIT(x)
108 /* global variables ***********************************************************/
110 /* global reference table *****************************************************/
112 /* hashsize must be power of 2 */
114 #define HASHTABLE_GLOBAL_REF_SIZE 64 /* initial size of globalref-hash */
116 static hashtable *hashtable_global_ref; /* hashtable for globalrefs */
119 /* direct buffer stuff ********************************************************/
121 #if defined(ENABLE_JAVASE)
122 static classinfo *class_java_nio_Buffer;
124 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
126 static classinfo *class_java_nio_DirectByteBufferImpl;
127 static classinfo *class_java_nio_DirectByteBufferImpl_ReadWrite;
129 # if SIZEOF_VOID_P == 8
130 static classinfo *class_gnu_classpath_Pointer64;
132 static classinfo *class_gnu_classpath_Pointer32;
135 static methodinfo *dbbirw_init;
137 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
139 static classinfo *class_sun_nio_ch_DirectBuffer;
140 static classinfo *class_java_nio_DirectByteBuffer;
142 static methodinfo *dbb_init;
148 /* some forward declarations **************************************************/
151 jobject jni_NewLocalRef(JNIEnv *env, jobject ref);
155 /* jni_init ********************************************************************
157 Initialize the JNI subsystem.
159 *******************************************************************************/
163 TRACESUBSYSTEMINITIALIZATION("jni_init");
165 /* create global ref hashtable */
167 hashtable_global_ref = NEW(hashtable);
169 hashtable_create(hashtable_global_ref, HASHTABLE_GLOBAL_REF_SIZE);
172 #if defined(ENABLE_JAVASE)
173 /* Direct buffer stuff. */
175 if (!(class_java_nio_Buffer =
176 load_class_bootstrap(utf_new_char("java/nio/Buffer"))) ||
177 !link_class(class_java_nio_Buffer))
180 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
182 if (!(class_java_nio_DirectByteBufferImpl =
183 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl"))) ||
184 !link_class(class_java_nio_DirectByteBufferImpl))
187 if (!(class_java_nio_DirectByteBufferImpl_ReadWrite =
188 load_class_bootstrap(utf_new_char("java/nio/DirectByteBufferImpl$ReadWrite"))) ||
189 !link_class(class_java_nio_DirectByteBufferImpl_ReadWrite))
193 class_resolvemethod(class_java_nio_DirectByteBufferImpl_ReadWrite,
195 utf_new_char("(Ljava/lang/Object;Lgnu/classpath/Pointer;III)V"))))
198 # if SIZEOF_VOID_P == 8
199 if (!(class_gnu_classpath_Pointer64 =
200 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer64"))) ||
201 !link_class(class_gnu_classpath_Pointer64))
204 if (!(class_gnu_classpath_Pointer32 =
205 load_class_bootstrap(utf_new_char("gnu/classpath/Pointer32"))) ||
206 !link_class(class_gnu_classpath_Pointer32))
210 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
212 if (!(class_sun_nio_ch_DirectBuffer =
213 load_class_bootstrap(utf_new_char("sun/nio/ch/DirectBuffer"))))
214 vm_abort("jni_init: loading sun/nio/ch/DirectBuffer failed");
216 if (!link_class(class_sun_nio_ch_DirectBuffer))
217 vm_abort("jni_init: linking sun/nio/ch/DirectBuffer failed");
219 if (!(class_java_nio_DirectByteBuffer =
220 load_class_bootstrap(utf_new_char("java/nio/DirectByteBuffer"))))
221 vm_abort("jni_init: loading java/nio/DirectByteBuffer failed");
223 if (!link_class(class_java_nio_DirectByteBuffer))
224 vm_abort("jni_init: linking java/nio/DirectByteBuffer failed");
227 class_resolvemethod(class_java_nio_DirectByteBuffer,
229 utf_new_char("(JI)V"))))
230 vm_abort("jni_init: resolving java/nio/DirectByteBuffer.init(JI)V failed");
234 #endif /* defined(ENABLE_JAVASE) */
240 /* jni_version_check ***********************************************************
242 Check if the given JNI version is supported.
245 version....JNI version to check
249 false......not supported
251 *******************************************************************************/
253 bool jni_version_check(int version)
256 case JNI_VERSION_1_1:
257 case JNI_VERSION_1_2:
258 case JNI_VERSION_1_4:
259 case JNI_VERSION_1_6:
267 /* _Jv_jni_CallObjectMethod ****************************************************
269 Internal function to call Java Object methods.
271 *******************************************************************************/
273 static java_handle_t *_Jv_jni_CallObjectMethod(java_handle_t *o,
275 methodinfo *m, va_list ap)
280 STATISTICS(jniinvokation());
283 exceptions_throw_nullpointerexception();
287 /* Class initialization is done by the JIT compiler. This is ok
288 since a static method always belongs to the declaring class. */
290 if (m->flags & ACC_STATIC) {
291 /* For static methods we reset the object. */
296 /* for convenience */
301 /* For instance methods we make a virtual function table lookup. */
303 resm = method_vftbl_lookup(vftbl, m);
306 STATISTICS(jnicallXmethodnvokation());
308 ro = vm_call_method_valist(resm, o, ap);
314 /* _Jv_jni_CallObjectMethodA ***************************************************
316 Internal function to call Java Object methods.
318 *******************************************************************************/
320 static java_handle_t *_Jv_jni_CallObjectMethodA(java_handle_t *o,
328 STATISTICS(jniinvokation());
331 exceptions_throw_nullpointerexception();
335 /* Class initialization is done by the JIT compiler. This is ok
336 since a static method always belongs to the declaring class. */
338 if (m->flags & ACC_STATIC) {
339 /* For static methods we reset the object. */
344 /* for convenience */
349 /* For instance methods we make a virtual function table lookup. */
351 resm = method_vftbl_lookup(vftbl, m);
354 STATISTICS(jnicallXmethodnvokation());
356 ro = vm_call_method_jvalue(resm, o, args);
362 /* _Jv_jni_CallIntMethod *******************************************************
364 Internal function to call Java integer class methods (boolean,
365 byte, char, short, int).
367 *******************************************************************************/
369 static jint _Jv_jni_CallIntMethod(java_handle_t *o, vftbl_t *vftbl,
370 methodinfo *m, va_list ap)
375 STATISTICS(jniinvokation());
378 exceptions_throw_nullpointerexception();
382 /* Class initialization is done by the JIT compiler. This is ok
383 since a static method always belongs to the declaring class. */
385 if (m->flags & ACC_STATIC) {
386 /* For static methods we reset the object. */
391 /* for convenience */
396 /* For instance methods we make a virtual function table lookup. */
398 resm = method_vftbl_lookup(vftbl, m);
401 STATISTICS(jnicallXmethodnvokation());
403 i = vm_call_method_int_valist(resm, o, ap);
409 /* _Jv_jni_CallIntMethodA ******************************************************
411 Internal function to call Java integer class methods (boolean,
412 byte, char, short, int).
414 *******************************************************************************/
416 static jint _Jv_jni_CallIntMethodA(java_handle_t *o, vftbl_t *vftbl,
417 methodinfo *m, const jvalue *args)
422 STATISTICS(jniinvokation());
425 exceptions_throw_nullpointerexception();
429 /* Class initialization is done by the JIT compiler. This is ok
430 since a static method always belongs to the declaring class. */
432 if (m->flags & ACC_STATIC) {
433 /* For static methods we reset the object. */
438 /* for convenience */
443 /* For instance methods we make a virtual function table lookup. */
445 resm = method_vftbl_lookup(vftbl, m);
448 STATISTICS(jnicallXmethodnvokation());
450 i = vm_call_method_int_jvalue(resm, o, args);
456 /* _Jv_jni_CallLongMethod ******************************************************
458 Internal function to call Java long methods.
460 *******************************************************************************/
462 static jlong _Jv_jni_CallLongMethod(java_handle_t *o, vftbl_t *vftbl,
463 methodinfo *m, va_list ap)
468 STATISTICS(jniinvokation());
471 exceptions_throw_nullpointerexception();
475 /* Class initialization is done by the JIT compiler. This is ok
476 since a static method always belongs to the declaring class. */
478 if (m->flags & ACC_STATIC) {
479 /* For static methods we reset the object. */
484 /* for convenience */
489 /* For instance methods we make a virtual function table lookup. */
491 resm = method_vftbl_lookup(vftbl, m);
494 STATISTICS(jnicallXmethodnvokation());
496 l = vm_call_method_long_valist(resm, o, ap);
502 /* _Jv_jni_CallLongMethodA *****************************************************
504 Internal function to call Java long methods.
506 *******************************************************************************/
508 static jlong _Jv_jni_CallLongMethodA(java_handle_t *o, vftbl_t *vftbl,
509 methodinfo *m, const jvalue *args)
514 STATISTICS(jniinvokation());
517 exceptions_throw_nullpointerexception();
521 /* Class initialization is done by the JIT compiler. This is ok
522 since a static method always belongs to the declaring class. */
524 if (m->flags & ACC_STATIC) {
525 /* For static methods we reset the object. */
530 /* for convenience */
535 /* For instance methods we make a virtual function table lookup. */
537 resm = method_vftbl_lookup(vftbl, m);
540 STATISTICS(jnicallXmethodnvokation());
542 l = vm_call_method_long_jvalue(resm, o, args);
548 /* _Jv_jni_CallFloatMethod *****************************************************
550 Internal function to call Java float methods.
552 *******************************************************************************/
554 static jfloat _Jv_jni_CallFloatMethod(java_handle_t *o, vftbl_t *vftbl,
555 methodinfo *m, va_list ap)
560 /* Class initialization is done by the JIT compiler. This is ok
561 since a static method always belongs to the declaring class. */
563 if (m->flags & ACC_STATIC) {
564 /* For static methods we reset the object. */
569 /* for convenience */
574 /* For instance methods we make a virtual function table lookup. */
576 resm = method_vftbl_lookup(vftbl, m);
579 STATISTICS(jnicallXmethodnvokation());
581 f = vm_call_method_float_valist(resm, o, ap);
587 /* _Jv_jni_CallFloatMethodA ****************************************************
589 Internal function to call Java float methods.
591 *******************************************************************************/
593 static jfloat _Jv_jni_CallFloatMethodA(java_handle_t *o, vftbl_t *vftbl,
594 methodinfo *m, const jvalue *args)
599 /* Class initialization is done by the JIT compiler. This is ok
600 since a static method always belongs to the declaring class. */
602 if (m->flags & ACC_STATIC) {
603 /* For static methods we reset the object. */
608 /* for convenience */
613 /* For instance methods we make a virtual function table lookup. */
615 resm = method_vftbl_lookup(vftbl, m);
618 STATISTICS(jnicallXmethodnvokation());
620 f = vm_call_method_float_jvalue(resm, o, args);
626 /* _Jv_jni_CallDoubleMethod ****************************************************
628 Internal function to call Java double methods.
630 *******************************************************************************/
632 static jdouble _Jv_jni_CallDoubleMethod(java_handle_t *o, vftbl_t *vftbl,
633 methodinfo *m, va_list ap)
638 /* Class initialization is done by the JIT compiler. This is ok
639 since a static method always belongs to the declaring class. */
641 if (m->flags & ACC_STATIC) {
642 /* For static methods we reset the object. */
647 /* for convenience */
652 /* For instance methods we make a virtual function table lookup. */
654 resm = method_vftbl_lookup(vftbl, m);
657 d = vm_call_method_double_valist(resm, o, ap);
663 /* _Jv_jni_CallDoubleMethodA ***************************************************
665 Internal function to call Java double methods.
667 *******************************************************************************/
669 static jdouble _Jv_jni_CallDoubleMethodA(java_handle_t *o, vftbl_t *vftbl,
670 methodinfo *m, const jvalue *args)
675 /* Class initialization is done by the JIT compiler. This is ok
676 since a static method always belongs to the declaring class. */
678 if (m->flags & ACC_STATIC) {
679 /* For static methods we reset the object. */
684 /* for convenience */
689 /* For instance methods we make a virtual function table lookup. */
691 resm = method_vftbl_lookup(vftbl, m);
694 d = vm_call_method_double_jvalue(resm, o, args);
700 /* _Jv_jni_CallVoidMethod ******************************************************
702 Internal function to call Java void methods.
704 *******************************************************************************/
706 static void _Jv_jni_CallVoidMethod(java_handle_t *o, vftbl_t *vftbl,
707 methodinfo *m, va_list ap)
712 exceptions_throw_nullpointerexception();
716 /* Class initialization is done by the JIT compiler. This is ok
717 since a static method always belongs to the declaring class. */
719 if (m->flags & ACC_STATIC) {
720 /* For static methods we reset the object. */
725 /* for convenience */
730 /* For instance methods we make a virtual function table lookup. */
732 resm = method_vftbl_lookup(vftbl, m);
735 STATISTICS(jnicallXmethodnvokation());
737 (void) vm_call_method_valist(resm, o, ap);
741 /* _Jv_jni_CallVoidMethodA *****************************************************
743 Internal function to call Java void methods.
745 *******************************************************************************/
747 static void _Jv_jni_CallVoidMethodA(java_handle_t *o, vftbl_t *vftbl,
748 methodinfo *m, const jvalue *args)
753 exceptions_throw_nullpointerexception();
757 /* Class initialization is done by the JIT compiler. This is ok
758 since a static method always belongs to the declaring class. */
760 if (m->flags & ACC_STATIC) {
761 /* For static methods we reset the object. */
766 /* for convenience */
771 /* For instance methods we make a virtual function table lookup. */
773 resm = method_vftbl_lookup(vftbl, m);
776 STATISTICS(jnicallXmethodnvokation());
778 (void) vm_call_method_jvalue(resm, o, args);
782 // JNI functions are exported as C functions.
785 /* GetVersion ******************************************************************
787 Returns the major version number in the higher 16 bits and the
788 minor version number in the lower 16 bits.
790 *******************************************************************************/
792 jint _Jv_JNI_GetVersion(JNIEnv *env)
794 TRACEJNICALLS(("_Jv_JNI_GetVersion(env=%p)", env));
796 return JNI_VERSION_SUPPORTED;
800 /* Class Operations ***********************************************************/
802 /* DefineClass *****************************************************************
804 Loads a class from a buffer of raw class data. The buffer
805 containing the raw class data is not referenced by the VM after the
806 DefineClass call returns, and it may be discarded if desired.
808 *******************************************************************************/
810 jclass jni_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize bufLen)
812 #if defined(ENABLE_JAVASE)
818 TRACEJNICALLS(("jni_DefineClass(env=%p, name=%s, loader=%p, buf=%p, bufLen=%d)", env, name, loader, buf, bufLen));
820 u = utf_new_char(name);
821 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
823 c = class_define(u, cl, bufLen, (uint8_t *) buf, NULL);
825 h = LLNI_classinfo_wrap(c);
827 return (jclass) jni_NewLocalRef(env, (jobject) h);
829 vm_abort("jni_DefineClass: Not implemented in this configuration");
831 // Keep compiler happy.
838 /* FindClass *******************************************************************
840 This function loads a locally-defined class. It searches the
841 directories and zip files specified by the CLASSPATH environment
842 variable for the class with the specified name.
844 *******************************************************************************/
846 jclass jni_FindClass(JNIEnv *env, const char *name)
848 #if defined(ENABLE_JAVASE)
855 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
857 /* FIXME If name is NULL we have a problem here. */
859 u = utf_new_char_classname((char *) name);
861 if ((u == NULL) /*|| (int)strlen(name) > symbolOopDesc::max_length() */) {
862 exceptions_throw_noclassdeffounderror(u);
866 /* Check stacktrace for classloader, if one found use it,
867 otherwise use the system classloader. */
869 /* Quote from the JNI documentation:
871 In the Java 2 Platform, FindClass locates the class loader
872 associated with the current native method. If the native code
873 belongs to a system class, no class loader will be
874 involved. Otherwise, the proper class loader will be invoked to
875 load and link the named class. When FindClass is called through
876 the Invocation Interface, there is no current native method or
877 its associated class loader. In that case, the result of
878 ClassLoader.getBaseClassLoader is used." */
880 cc = stacktrace_get_current_class();
883 c = load_class_from_sysloader(u);
885 classloader_t *cl = cc->classloader;
886 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
887 /* See jni_FindClass in Hotspot's src/share/vm/prims/jni.cpp */
888 if (!cl && cc->name == utf_java_lang_ClassLoader_NativeLibrary)
890 methodinfo *m = class_resolveclassmethod(
892 utf_new_char("getFromClass"),
893 utf_new_char("()Ljava/lang/Class;"),
899 h = vm_call_method(m, NULL);
901 if (m && exceptions_get_exception() == NULL)
902 cl = ((classinfo *) LLNI_UNWRAP(h))->classloader;
907 c = load_class_from_classloader(u, cl);
911 resolve_handle_pending_exception(true);
918 h = LLNI_classinfo_wrap(c);
920 return (jclass) jni_NewLocalRef(env, (jobject) h);
922 #elif defined(ENABLE_JAVAME_CLDC1_1)
927 TRACEJNICALLS(("jni_FindClass(env=%p, name=%s)", env, name));
929 u = utf_new_char_classname((char *) name);
930 c = load_class_bootstrap(u);
933 resolve_handle_pending_exception(true);
940 return (jclass) jni_NewLocalRef(env, (jobject) c);
943 vm_abort("jni_FindClass: not implemented in this configuration");
945 /* keep compiler happy */
952 /* GetSuperclass ***************************************************************
954 If clazz represents any class other than the class Object, then
955 this function returns the object that represents the superclass of
956 the class specified by clazz.
958 *******************************************************************************/
960 jclass jni_GetSuperclass(JNIEnv *env, jclass sub)
965 TRACEJNICALLS(("jni_GetSuperclass(env=%p, sub=%p)", env, sub));
967 c = LLNI_classinfo_unwrap(sub);
972 super = class_get_superclass(c);
974 java_handle_t* h = LLNI_classinfo_wrap(super);
976 return (jclass) jni_NewLocalRef(env, (jobject) h);
980 /* IsAssignableFrom ************************************************************
982 Determines whether an object of sub can be safely cast to sup.
984 *******************************************************************************/
986 jboolean _Jv_JNI_IsAssignableFrom(JNIEnv *env, jclass sub, jclass sup)
991 TRACEJNICALLS(("_Jv_JNI_IsAssignableFrom(env=%p, sub=%p, sup=%p)", env, sub, sup));
993 to = (classinfo *) sup;
994 from = (classinfo *) sub;
996 return class_is_assignable_from(to, from);
1000 /* Throw ***********************************************************************
1002 Causes a java.lang.Throwable object to be thrown.
1004 *******************************************************************************/
1006 jint _Jv_JNI_Throw(JNIEnv *env, jthrowable obj)
1010 STATISTICS(jniinvokation());
1012 o = (java_handle_t *) obj;
1014 exceptions_set_exception(o);
1020 /* ThrowNew ********************************************************************
1022 Constructs an exception object from the specified class with the
1023 message specified by message and causes that exception to be
1026 *******************************************************************************/
1028 jint _Jv_JNI_ThrowNew(JNIEnv* env, jclass clazz, const char *msg)
1034 STATISTICS(jniinvokation());
1036 c = LLNI_classinfo_unwrap(clazz);
1039 s = javastring_new_from_utf_string(msg);
1041 /* instantiate exception object */
1043 o = native_new_and_init_string(c, s);
1048 exceptions_set_exception(o);
1054 /* ExceptionOccurred ***********************************************************
1056 Determines if an exception is being thrown. The exception stays
1057 being thrown until either the native code calls ExceptionClear(),
1058 or the Java code handles the exception.
1060 *******************************************************************************/
1062 jthrowable _Jv_JNI_ExceptionOccurred(JNIEnv *env)
1066 TRACEJNICALLS(("_Jv_JNI_ExceptionOccurred(env=%p)", env));
1068 o = exceptions_get_exception();
1070 return (jthrowable) jni_NewLocalRef(env, (jthrowable) o);
1074 /* ExceptionDescribe ***********************************************************
1076 Prints an exception and a backtrace of the stack to a system
1077 error-reporting channel, such as stderr. This is a convenience
1078 routine provided for debugging.
1080 *******************************************************************************/
1082 void jni_ExceptionDescribe(JNIEnv *env)
1084 TRACEJNICALLS(("jni_ExceptionDescribe(env=%p)", env));
1086 exceptions_print_stacktrace();
1090 /* ExceptionClear **************************************************************
1092 Clears any exception that is currently being thrown. If no
1093 exception is currently being thrown, this routine has no effect.
1095 *******************************************************************************/
1097 void jni_ExceptionClear(JNIEnv *env)
1099 TRACEJNICALLS(("jni_ExceptionClear(env=%p)", env));
1101 exceptions_clear_exception();
1105 /* FatalError ******************************************************************
1107 Raises a fatal error and does not expect the VM to recover. This
1108 function does not return.
1110 *******************************************************************************/
1112 void _Jv_JNI_FatalError(JNIEnv *env, const char *msg)
1114 STATISTICS(jniinvokation());
1116 /* this seems to be the best way */
1118 vm_abort("JNI Fatal error: %s", msg);
1122 /* PushLocalFrame **************************************************************
1124 Creates a new local reference frame, in which at least a given
1125 number of local references can be created.
1127 *******************************************************************************/
1129 jint jni_PushLocalFrame(JNIEnv* env, jint capacity)
1131 TRACEJNICALLS(("jni_PushLocalFrame(env=%p, capacity=%d)", env, capacity));
1136 /* add new local reference frame to current table */
1138 if (!localref_frame_push(capacity))
1145 /* PopLocalFrame ***************************************************************
1147 Pops off the current local reference frame, frees all the local
1148 references, and returns a local reference in the previous local
1149 reference frame for the given result object.
1151 *******************************************************************************/
1153 jobject jni_PopLocalFrame(JNIEnv* env, jobject result)
1155 TRACEJNICALLS(("jni_PopLocalFrame(env=%p, result=%p)", env, result));
1157 /* release all current local frames */
1159 localref_frame_pop_all();
1161 /* add local reference and return the value */
1163 return jni_NewLocalRef(env, result);
1167 /* DeleteLocalRef **************************************************************
1169 Deletes the local reference pointed to by localRef.
1171 *******************************************************************************/
1173 void jni_DeleteLocalRef(JNIEnv *env, jobject localRef)
1177 TRACEJNICALLS(("jni_DeleteLocalRef(env=%p, ref=%p)", env, localRef));
1179 o = (java_handle_t *) localRef;
1184 /* delete the reference */
1190 /* IsSameObject ****************************************************************
1192 Tests whether two references refer to the same Java object.
1194 *******************************************************************************/
1196 jboolean _Jv_JNI_IsSameObject(JNIEnv *env, jobject ref1, jobject ref2)
1202 STATISTICS(jniinvokation());
1204 o1 = (java_handle_t *) ref1;
1205 o2 = (java_handle_t *) ref2;
1207 LLNI_CRITICAL_START;
1209 if (LLNI_UNWRAP(o1) == LLNI_UNWRAP(o2))
1220 /* NewLocalRef *****************************************************************
1222 Creates a new local reference that refers to the same object as ref.
1224 *******************************************************************************/
1226 jobject jni_NewLocalRef(JNIEnv *env, jobject ref)
1229 java_handle_t *localref;
1231 TRACEJNICALLS(("jni_NewLocalRef(env=%p, ref=%p)", env, ref));
1233 o = (java_handle_t *) ref;
1238 /* insert the reference */
1240 localref = localref_add(LLNI_DIRECT(o));
1242 return (jobject) localref;
1246 /* EnsureLocalCapacity *********************************************************
1248 Ensures that at least a given number of local references can be
1249 created in the current thread
1251 *******************************************************************************/
1253 jint jni_EnsureLocalCapacity(JNIEnv* env, jint capacity)
1255 localref_table *lrt;
1257 TRACEJNICALLS(("jni_EnsureLocalCapacity(env=%p, capacity=%d)", env, capacity));
1259 /* get local reference table (thread specific) */
1261 lrt = LOCALREFTABLE;
1263 /* check if capacity elements are available in the local references table */
1265 if ((lrt->used + capacity) > lrt->capacity)
1266 return jni_PushLocalFrame(env, capacity);
1272 /* AllocObject *****************************************************************
1274 Allocates a new Java object without invoking any of the
1275 constructors for the object. Returns a reference to the object.
1277 *******************************************************************************/
1279 jobject _Jv_JNI_AllocObject(JNIEnv *env, jclass clazz)
1284 STATISTICS(jniinvokation());
1286 c = LLNI_classinfo_unwrap(clazz);
1288 if ((c->flags & ACC_INTERFACE) || (c->flags & ACC_ABSTRACT)) {
1289 exceptions_throw_instantiationexception(c);
1295 return jni_NewLocalRef(env, (jobject) o);
1299 /* NewObject *******************************************************************
1301 Programmers place all arguments that are to be passed to the
1302 constructor immediately following the methodID
1303 argument. NewObject() accepts these arguments and passes them to
1304 the Java method that the programmer wishes to invoke.
1306 *******************************************************************************/
1308 jobject jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
1315 TRACEJNICALLSENTER(("jni_NewObject(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
1317 c = LLNI_classinfo_unwrap(clazz);
1318 m = (methodinfo *) methodID;
1327 /* call constructor */
1329 va_start(ap, methodID);
1330 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1333 TRACEJNICALLSEXIT(("->%p", o));
1335 return jni_NewLocalRef(env, (jobject) o);
1339 /* NewObjectV ******************************************************************
1341 Programmers place all arguments that are to be passed to the
1342 constructor in an args argument of type va_list that immediately
1343 follows the methodID argument. NewObjectV() accepts these
1344 arguments, and, in turn, passes them to the Java method that the
1345 programmer wishes to invoke.
1347 *******************************************************************************/
1349 jobject _Jv_JNI_NewObjectV(JNIEnv* env, jclass clazz, jmethodID methodID,
1356 STATISTICS(jniinvokation());
1358 c = LLNI_classinfo_unwrap(clazz);
1359 m = (methodinfo *) methodID;
1368 /* call constructor */
1370 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1372 return jni_NewLocalRef(env, (jobject) o);
1376 /* NewObjectA *****************************************************************
1378 Programmers place all arguments that are to be passed to the
1379 constructor in an args array of jvalues that immediately follows
1380 the methodID argument. NewObjectA() accepts the arguments in this
1381 array, and, in turn, passes them to the Java method that the
1382 programmer wishes to invoke.
1384 *******************************************************************************/
1386 jobject _Jv_JNI_NewObjectA(JNIEnv* env, jclass clazz, jmethodID methodID,
1393 STATISTICS(jniinvokation());
1395 c = LLNI_classinfo_unwrap(clazz);
1396 m = (methodinfo *) methodID;
1405 /* call constructor */
1407 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1409 return jni_NewLocalRef(env, (jobject) o);
1413 /* GetObjectClass **************************************************************
1415 Returns the class of an object.
1417 *******************************************************************************/
1419 jclass jni_GetObjectClass(JNIEnv *env, jobject obj)
1424 TRACEJNICALLS(("jni_GetObjectClass(env=%p, obj=%p)", env, obj));
1426 o = (java_handle_t *) obj;
1428 LLNI_class_get(o, c);
1430 java_handle_t* h = LLNI_classinfo_wrap(c);
1432 return (jclass) jni_NewLocalRef(env, (jobject) h);
1436 /* IsInstanceOf ****************************************************************
1438 Tests whether an object is an instance of a class.
1440 *******************************************************************************/
1442 jboolean _Jv_JNI_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)
1447 TRACEJNICALLS(("_Jv_JNI_IsInstanceOf(env=%p, obj=%p, clazz=%p)", env, obj, clazz));
1449 /* XXX Is this correct? */
1450 c = LLNI_classinfo_unwrap(clazz);
1451 h = (java_handle_t *) obj;
1453 return class_is_instance(c, h);
1457 /* Reflection Support *********************************************************/
1459 /* FromReflectedMethod *********************************************************
1461 Converts java.lang.reflect.Method or java.lang.reflect.Constructor
1462 object to a method ID.
1464 *******************************************************************************/
1466 jmethodID jni_FromReflectedMethod(JNIEnv *env, jobject method)
1468 #if defined(ENABLE_JAVASE)
1471 TRACEJNICALLS(("jni_FromReflectedMethod(env=%p, method=%p)", env, method));
1473 java_lang_Object o(method);
1478 if (o.get_Class() == class_java_lang_reflect_Constructor) {
1479 java_lang_reflect_Constructor rc(method);
1480 m = rc.get_method();
1483 assert(o.get_Class() == class_java_lang_reflect_Method);
1485 java_lang_reflect_Method rm(method);
1486 m = rm.get_method();
1489 return (jmethodID) m;
1491 vm_abort("jni_FromReflectedMethod: Not implemented in this configuration.");
1493 // Keep compiler happy.
1499 /* FromReflectedField **********************************************************
1501 Converts a java.lang.reflect.Field to a field ID.
1503 *******************************************************************************/
1505 jfieldID jni_FromReflectedField(JNIEnv* env, jobject field)
1507 #if defined(ENABLE_JAVASE)
1509 TRACEJNICALLS(("jni_FromReflectedField(env=%p, field=%p)", env, field));
1511 java_lang_reflect_Field rf(field);
1516 fieldinfo* f = rf.get_field();
1518 return (jfieldID) f;
1520 vm_abort("jni_FromReflectedField: Not implemented in this configuration.");
1522 // Keep compiler happy.
1528 /* ToReflectedMethod ***********************************************************
1530 Converts a method ID derived from cls to an instance of the
1531 java.lang.reflect.Method class or to an instance of the
1532 java.lang.reflect.Constructor class.
1534 *******************************************************************************/
1536 jobject jni_ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic)
1538 #if defined(ENABLE_JAVASE)
1539 TRACEJNICALLS(("jni_ToReflectedMethod(env=%p, cls=%p, methodID=%p, isStatic=%d)", env, cls, methodID, isStatic));
1541 methodinfo* m = (methodinfo *) methodID;
1543 /* HotSpot does the same assert. */
1545 assert(((m->flags & ACC_STATIC) != 0) == (isStatic != 0));
1549 if (m->name == utf_init) {
1550 h = java_lang_reflect_Constructor(m).get_handle();
1553 h = java_lang_reflect_Method(m).get_handle();
1558 vm_abort("jni_ToReflectedMethod: Not implemented in this configuration.");
1560 /* keep compiler happy */
1567 /* ToReflectedField ************************************************************
1569 Converts a field ID derived from cls to an instance of the
1570 java.lang.reflect.Field class.
1572 *******************************************************************************/
1574 jobject _Jv_JNI_ToReflectedField(JNIEnv* env, jclass cls, jfieldID fieldID,
1577 STATISTICS(jniinvokation());
1579 log_text("JNI-Call: ToReflectedField: IMPLEMENT ME!");
1585 /* Calling Instance Methods ***************************************************/
1587 /* GetMethodID *****************************************************************
1589 Returns the method ID for an instance (nonstatic) method of a class
1590 or interface. The method may be defined in one of the clazz's
1591 superclasses and inherited by clazz. The method is determined by
1592 its name and signature.
1594 GetMethodID() causes an uninitialized class to be initialized.
1596 *******************************************************************************/
1598 jmethodID _Jv_JNI_GetMethodID(JNIEnv* env, jclass clazz, const char *name,
1606 STATISTICS(jniinvokation());
1608 c = LLNI_classinfo_unwrap(clazz);
1613 if (!(c->state & CLASS_INITIALIZED))
1614 if (!initialize_class(c))
1617 /* try to get the method of the class or one of it's superclasses */
1619 uname = utf_new_char((char *) name);
1620 udesc = utf_new_char((char *) sig);
1622 m = class_resolvemethod(c, uname, udesc);
1624 if ((m == NULL) || (m->flags & ACC_STATIC)) {
1625 exceptions_throw_nosuchmethoderror(c, uname, udesc);
1630 return (jmethodID) m;
1634 /* JNI-functions for calling instance methods *********************************/
1636 #define JNI_CALL_VIRTUAL_METHOD(name, type, intern) \
1637 type _Jv_JNI_Call##name##Method(JNIEnv *env, jobject obj, \
1638 jmethodID methodID, ...) \
1645 o = (java_handle_t *) obj; \
1646 m = (methodinfo *) methodID; \
1648 va_start(ap, methodID); \
1649 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, ap); \
1655 JNI_CALL_VIRTUAL_METHOD(Boolean, jboolean, Int)
1656 JNI_CALL_VIRTUAL_METHOD(Byte, jbyte, Int)
1657 JNI_CALL_VIRTUAL_METHOD(Char, jchar, Int)
1658 JNI_CALL_VIRTUAL_METHOD(Short, jshort, Int)
1659 JNI_CALL_VIRTUAL_METHOD(Int, jint, Int)
1660 JNI_CALL_VIRTUAL_METHOD(Long, jlong, Long)
1661 JNI_CALL_VIRTUAL_METHOD(Float, jfloat, Float)
1662 JNI_CALL_VIRTUAL_METHOD(Double, jdouble, Double)
1665 #define JNI_CALL_VIRTUAL_METHOD_V(name, type, intern) \
1666 type _Jv_JNI_Call##name##MethodV(JNIEnv *env, jobject obj, \
1667 jmethodID methodID, va_list args) \
1673 o = (java_handle_t *) obj; \
1674 m = (methodinfo *) methodID; \
1676 ret = _Jv_jni_Call##intern##Method(o, LLNI_vftbl_direct(o), m, args); \
1681 JNI_CALL_VIRTUAL_METHOD_V(Boolean, jboolean, Int)
1682 JNI_CALL_VIRTUAL_METHOD_V(Byte, jbyte, Int)
1683 JNI_CALL_VIRTUAL_METHOD_V(Char, jchar, Int)
1684 JNI_CALL_VIRTUAL_METHOD_V(Short, jshort, Int)
1685 JNI_CALL_VIRTUAL_METHOD_V(Int, jint, Int)
1686 JNI_CALL_VIRTUAL_METHOD_V(Long, jlong, Long)
1687 JNI_CALL_VIRTUAL_METHOD_V(Float, jfloat, Float)
1688 JNI_CALL_VIRTUAL_METHOD_V(Double, jdouble, Double)
1691 #define JNI_CALL_VIRTUAL_METHOD_A(name, type, intern) \
1692 type _Jv_JNI_Call##name##MethodA(JNIEnv *env, jobject obj, \
1693 jmethodID methodID, \
1694 const jvalue *args) \
1700 o = (java_handle_t *) obj; \
1701 m = (methodinfo *) methodID; \
1703 ret = _Jv_jni_Call##intern##MethodA(o, LLNI_vftbl_direct(o), m, args); \
1708 JNI_CALL_VIRTUAL_METHOD_A(Boolean, jboolean, Int)
1709 JNI_CALL_VIRTUAL_METHOD_A(Byte, jbyte, Int)
1710 JNI_CALL_VIRTUAL_METHOD_A(Char, jchar, Int)
1711 JNI_CALL_VIRTUAL_METHOD_A(Short, jshort, Int)
1712 JNI_CALL_VIRTUAL_METHOD_A(Int, jint, Int)
1713 JNI_CALL_VIRTUAL_METHOD_A(Long, jlong, Long)
1714 JNI_CALL_VIRTUAL_METHOD_A(Float, jfloat, Float)
1715 JNI_CALL_VIRTUAL_METHOD_A(Double, jdouble, Double)
1718 jobject _Jv_JNI_CallObjectMethod(JNIEnv *env, jobject obj, jmethodID methodID,
1726 o = (java_handle_t *) obj;
1727 m = (methodinfo *) methodID;
1729 va_start(ap, methodID);
1730 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, ap);
1733 return jni_NewLocalRef(env, (jobject) ret);
1737 jobject _Jv_JNI_CallObjectMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1744 o = (java_handle_t *) obj;
1745 m = (methodinfo *) methodID;
1747 ret = _Jv_jni_CallObjectMethod(o, LLNI_vftbl_direct(o), m, args);
1749 return jni_NewLocalRef(env, (jobject) ret);
1753 jobject _Jv_JNI_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1760 o = (java_handle_t *) obj;
1761 m = (methodinfo *) methodID;
1763 ret = _Jv_jni_CallObjectMethodA(o, LLNI_vftbl_direct(o), m, args);
1765 return jni_NewLocalRef(env, (jobject) ret);
1770 void _Jv_JNI_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
1776 o = (java_handle_t *) obj;
1777 m = (methodinfo *) methodID;
1779 va_start(ap, methodID);
1780 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
1785 void _Jv_JNI_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID,
1791 o = (java_handle_t *) obj;
1792 m = (methodinfo *) methodID;
1794 _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, args);
1798 void _Jv_JNI_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID,
1804 o = (java_handle_t *) obj;
1805 m = (methodinfo *) methodID;
1807 _Jv_jni_CallVoidMethodA(o, LLNI_vftbl_direct(o), m, args);
1812 #define JNI_CALL_NONVIRTUAL_METHOD(name, type, intern) \
1813 type _Jv_JNI_CallNonvirtual##name##Method(JNIEnv *env, jobject obj, \
1814 jclass clazz, jmethodID methodID, \
1823 o = (java_handle_t *) obj; \
1824 c = LLNI_classinfo_unwrap(clazz); \
1825 m = (methodinfo *) methodID; \
1827 va_start(ap, methodID); \
1828 ret = _Jv_jni_Call##intern##Method(o, c->vftbl, m, ap); \
1834 JNI_CALL_NONVIRTUAL_METHOD(Boolean, jboolean, Int)
1835 JNI_CALL_NONVIRTUAL_METHOD(Byte, jbyte, Int)
1836 JNI_CALL_NONVIRTUAL_METHOD(Char, jchar, Int)
1837 JNI_CALL_NONVIRTUAL_METHOD(Short, jshort, Int)
1838 JNI_CALL_NONVIRTUAL_METHOD(Int, jint, Int)
1839 JNI_CALL_NONVIRTUAL_METHOD(Long, jlong, Long)
1840 JNI_CALL_NONVIRTUAL_METHOD(Float, jfloat, Float)
1841 JNI_CALL_NONVIRTUAL_METHOD(Double, jdouble, Double)
1844 #define JNI_CALL_NONVIRTUAL_METHOD_V(name, type, intern) \
1845 type _Jv_JNI_CallNonvirtual##name##MethodV(JNIEnv *env, jobject obj, \
1846 jclass clazz, jmethodID methodID, \
1854 o = (java_handle_t *) obj; \
1855 c = LLNI_classinfo_unwrap(clazz); \
1856 m = (methodinfo *) methodID; \
1858 ret = _Jv_jni_CallIntMethod(o, c->vftbl, m, args); \
1863 JNI_CALL_NONVIRTUAL_METHOD_V(Boolean, jboolean, Int)
1864 JNI_CALL_NONVIRTUAL_METHOD_V(Byte, jbyte, Int)
1865 JNI_CALL_NONVIRTUAL_METHOD_V(Char, jchar, Int)
1866 JNI_CALL_NONVIRTUAL_METHOD_V(Short, jshort, Int)
1867 JNI_CALL_NONVIRTUAL_METHOD_V(Int, jint, Int)
1868 JNI_CALL_NONVIRTUAL_METHOD_V(Long, jlong, Long)
1869 JNI_CALL_NONVIRTUAL_METHOD_V(Float, jfloat, Float)
1870 JNI_CALL_NONVIRTUAL_METHOD_V(Double, jdouble, Double)
1873 #define JNI_CALL_NONVIRTUAL_METHOD_A(name, type, intern) \
1874 type _Jv_JNI_CallNonvirtual##name##MethodA(JNIEnv *env, jobject obj, \
1875 jclass clazz, jmethodID methodID, \
1876 const jvalue *args) \
1878 log_text("JNI-Call: CallNonvirtual##name##MethodA: IMPLEMENT ME!"); \
1883 JNI_CALL_NONVIRTUAL_METHOD_A(Boolean, jboolean, Int)
1884 JNI_CALL_NONVIRTUAL_METHOD_A(Byte, jbyte, Int)
1885 JNI_CALL_NONVIRTUAL_METHOD_A(Char, jchar, Int)
1886 JNI_CALL_NONVIRTUAL_METHOD_A(Short, jshort, Int)
1887 JNI_CALL_NONVIRTUAL_METHOD_A(Int, jint, Int)
1888 JNI_CALL_NONVIRTUAL_METHOD_A(Long, jlong, Long)
1889 JNI_CALL_NONVIRTUAL_METHOD_A(Float, jfloat, Float)
1890 JNI_CALL_NONVIRTUAL_METHOD_A(Double, jdouble, Double)
1892 jobject _Jv_JNI_CallNonvirtualObjectMethod(JNIEnv *env, jobject obj,
1893 jclass clazz, jmethodID methodID,
1902 o = (java_handle_t *) obj;
1903 c = LLNI_classinfo_unwrap(clazz);
1904 m = (methodinfo *) methodID;
1906 va_start(ap, methodID);
1907 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, ap);
1910 return jni_NewLocalRef(env, (jobject) r);
1914 jobject _Jv_JNI_CallNonvirtualObjectMethodV(JNIEnv *env, jobject obj,
1915 jclass clazz, jmethodID methodID,
1923 o = (java_handle_t *) obj;
1924 c = LLNI_classinfo_unwrap(clazz);
1925 m = (methodinfo *) methodID;
1927 r = _Jv_jni_CallObjectMethod(o, c->vftbl, m, args);
1929 return jni_NewLocalRef(env, (jobject) r);
1933 jobject _Jv_JNI_CallNonvirtualObjectMethodA(JNIEnv *env, jobject obj,
1934 jclass clazz, jmethodID methodID,
1937 log_text("JNI-Call: CallNonvirtualObjectMethodA: IMPLEMENT ME!");
1939 return jni_NewLocalRef(env, NULL);
1943 void _Jv_JNI_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass clazz,
1944 jmethodID methodID, ...)
1951 o = (java_handle_t *) obj;
1952 c = LLNI_classinfo_unwrap(clazz);
1953 m = (methodinfo *) methodID;
1955 va_start(ap, methodID);
1956 _Jv_jni_CallVoidMethod(o, c->vftbl, m, ap);
1961 void _Jv_JNI_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass clazz,
1962 jmethodID methodID, va_list args)
1968 o = (java_handle_t *) obj;
1969 c = LLNI_classinfo_unwrap(clazz);
1970 m = (methodinfo *) methodID;
1972 _Jv_jni_CallVoidMethod(o, c->vftbl, m, args);
1976 void _Jv_JNI_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass clazz,
1977 jmethodID methodID, const jvalue * args)
1983 o = (java_handle_t *) obj;
1984 c = LLNI_classinfo_unwrap(clazz);
1985 m = (methodinfo *) methodID;
1987 _Jv_jni_CallVoidMethodA(o, c->vftbl, m, args);
1991 /* Accessing Fields of Objects ************************************************/
1993 /* GetFieldID ******************************************************************
1995 Returns the field ID for an instance (nonstatic) field of a
1996 class. The field is specified by its name and signature. The
1997 Get<type>Field and Set<type>Field families of accessor functions
1998 use field IDs to retrieve object fields.
2000 *******************************************************************************/
2002 jfieldID _Jv_JNI_GetFieldID(JNIEnv *env, jclass clazz, const char *name,
2010 STATISTICS(jniinvokation());
2012 c = LLNI_classinfo_unwrap(clazz);
2014 /* XXX NPE check? */
2016 uname = utf_new_char((char *) name);
2017 udesc = utf_new_char((char *) sig);
2019 f = class_findfield(c, uname, udesc);
2022 exceptions_throw_nosuchfielderror(c, uname);
2024 return (jfieldID) f;
2028 /* Get<type>Field Routines *****************************************************
2030 This family of accessor routines returns the value of an instance
2031 (nonstatic) field of an object. The field to access is specified by
2032 a field ID obtained by calling GetFieldID().
2034 *******************************************************************************/
2036 #define GET_FIELD(o,type,f) \
2037 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset)))
2039 #define JNI_GET_FIELD(name, type, intern) \
2040 type _Jv_JNI_Get##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID) \
2044 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "Field(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID)); \
2046 LLNI_CRITICAL_START; \
2048 ret = GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID); \
2050 LLNI_CRITICAL_END; \
2052 return (type) ret; \
2055 JNI_GET_FIELD(Boolean, jboolean, s4)
2056 JNI_GET_FIELD(Byte, jbyte, s4)
2057 JNI_GET_FIELD(Char, jchar, s4)
2058 JNI_GET_FIELD(Short, jshort, s4)
2059 JNI_GET_FIELD(Int, jint, s4)
2060 JNI_GET_FIELD(Long, jlong, s8)
2061 JNI_GET_FIELD(Float, jfloat, float)
2062 JNI_GET_FIELD(Double, jdouble, double)
2065 jobject _Jv_JNI_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)
2069 TRACEJNICALLS(("_Jv_JNI_GetObjectField(env=%p, obj=%p, fieldId=%p)", env, obj, fieldID));
2071 LLNI_CRITICAL_START;
2073 o = LLNI_WRAP(GET_FIELD(LLNI_DIRECT((java_handle_t *) obj), java_object_t*, fieldID));
2077 return jni_NewLocalRef(env, (jobject) o);
2081 /* Set<type>Field Routines *****************************************************
2083 This family of accessor routines sets the value of an instance
2084 (nonstatic) field of an object. The field to access is specified by
2085 a field ID obtained by calling GetFieldID().
2087 *******************************************************************************/
2089 #define SET_FIELD(o,type,f,value) \
2090 *((type *) (((intptr_t) (o)) + ((intptr_t) ((fieldinfo *) (f))->offset))) = (type) (value)
2092 #define GET_FIELDINFO(f) ((fieldinfo*) (f))
2094 #define JNI_SET_FIELD(name, type, intern) \
2095 void _Jv_JNI_Set##name##Field(JNIEnv *env, jobject obj, jfieldID fieldID, \
2098 TRACEJNICALLS(("_Jv_JNI_Set" STR(name) "Field(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value)); \
2100 LLNI_CRITICAL_START; \
2102 SET_FIELD(LLNI_DIRECT((java_handle_t *) obj), intern, fieldID, value); \
2104 LLNI_CRITICAL_END; \
2106 if (GET_FIELDINFO(fieldID)->flags & ACC_VOLATILE) \
2107 Atomic::memory_barrier(); \
2110 JNI_SET_FIELD(Boolean, jboolean, s4)
2111 JNI_SET_FIELD(Byte, jbyte, s4)
2112 JNI_SET_FIELD(Char, jchar, s4)
2113 JNI_SET_FIELD(Short, jshort, s4)
2114 JNI_SET_FIELD(Int, jint, s4)
2115 JNI_SET_FIELD(Long, jlong, s8)
2116 JNI_SET_FIELD(Float, jfloat, float)
2117 JNI_SET_FIELD(Double, jdouble, double)
2120 void _Jv_JNI_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID,
2123 TRACEJNICALLS(("_Jv_JNI_SetObjectField(env=%p, obj=%p, fieldId=%p, value=%p)", env, obj, fieldID, value));
2125 LLNI_CRITICAL_START;
2127 SET_FIELD(obj, java_handle_t*, fieldID, LLNI_UNWRAP((java_handle_t*) value));
2131 if (GET_FIELDINFO(fieldID)->flags & ACC_VOLATILE)
2132 Atomic::memory_barrier();
2136 /* Calling Static Methods *****************************************************/
2138 /* GetStaticMethodID ***********************************************************
2140 Returns the method ID for a static method of a class. The method is
2141 specified by its name and signature.
2143 GetStaticMethodID() causes an uninitialized class to be
2146 *******************************************************************************/
2148 jmethodID _Jv_JNI_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name,
2156 TRACEJNICALLS(("_Jv_JNI_GetStaticMethodID(env=%p, clazz=%p, name=%s, sig=%s)", env, clazz, name, sig));
2158 c = LLNI_classinfo_unwrap(clazz);
2163 if (!(c->state & CLASS_INITIALIZED))
2164 if (!initialize_class(c))
2167 /* try to get the static method of the class */
2169 uname = utf_new_char((char *) name);
2170 udesc = utf_new_char((char *) sig);
2172 m = class_resolvemethod(c, uname, udesc);
2174 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
2175 exceptions_throw_nosuchmethoderror(c, uname, udesc);
2180 return (jmethodID) m;
2184 #define JNI_CALL_STATIC_METHOD(name, type, intern) \
2185 type _Jv_JNI_CallStatic##name##Method(JNIEnv *env, jclass clazz, \
2186 jmethodID methodID, ...) \
2192 m = (methodinfo *) methodID; \
2194 va_start(ap, methodID); \
2195 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, ap); \
2201 JNI_CALL_STATIC_METHOD(Boolean, jboolean, Int)
2202 JNI_CALL_STATIC_METHOD(Byte, jbyte, Int)
2203 JNI_CALL_STATIC_METHOD(Char, jchar, Int)
2204 JNI_CALL_STATIC_METHOD(Short, jshort, Int)
2205 JNI_CALL_STATIC_METHOD(Int, jint, Int)
2206 JNI_CALL_STATIC_METHOD(Long, jlong, Long)
2207 JNI_CALL_STATIC_METHOD(Float, jfloat, Float)
2208 JNI_CALL_STATIC_METHOD(Double, jdouble, Double)
2211 #define JNI_CALL_STATIC_METHOD_V(name, type, intern) \
2212 type _Jv_JNI_CallStatic##name##MethodV(JNIEnv *env, jclass clazz, \
2213 jmethodID methodID, va_list args) \
2218 m = (methodinfo *) methodID; \
2220 res = _Jv_jni_Call##intern##Method(NULL, NULL, m, args); \
2225 JNI_CALL_STATIC_METHOD_V(Boolean, jboolean, Int)
2226 JNI_CALL_STATIC_METHOD_V(Byte, jbyte, Int)
2227 JNI_CALL_STATIC_METHOD_V(Char, jchar, Int)
2228 JNI_CALL_STATIC_METHOD_V(Short, jshort, Int)
2229 JNI_CALL_STATIC_METHOD_V(Int, jint, Int)
2230 JNI_CALL_STATIC_METHOD_V(Long, jlong, Long)
2231 JNI_CALL_STATIC_METHOD_V(Float, jfloat, Float)
2232 JNI_CALL_STATIC_METHOD_V(Double, jdouble, Double)
2235 #define JNI_CALL_STATIC_METHOD_A(name, type, intern) \
2236 type _Jv_JNI_CallStatic##name##MethodA(JNIEnv *env, jclass clazz, \
2237 jmethodID methodID, const jvalue *args) \
2242 m = (methodinfo *) methodID; \
2244 res = _Jv_jni_Call##intern##MethodA(NULL, NULL, m, args); \
2249 JNI_CALL_STATIC_METHOD_A(Boolean, jboolean, Int)
2250 JNI_CALL_STATIC_METHOD_A(Byte, jbyte, Int)
2251 JNI_CALL_STATIC_METHOD_A(Char, jchar, Int)
2252 JNI_CALL_STATIC_METHOD_A(Short, jshort, Int)
2253 JNI_CALL_STATIC_METHOD_A(Int, jint, Int)
2254 JNI_CALL_STATIC_METHOD_A(Long, jlong, Long)
2255 JNI_CALL_STATIC_METHOD_A(Float, jfloat, Float)
2256 JNI_CALL_STATIC_METHOD_A(Double, jdouble, Double)
2259 jobject _Jv_JNI_CallStaticObjectMethod(JNIEnv *env, jclass clazz,
2260 jmethodID methodID, ...)
2266 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2268 m = (methodinfo *) methodID;
2270 va_start(ap, methodID);
2271 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, ap);
2274 return jni_NewLocalRef(env, (jobject) o);
2278 jobject _Jv_JNI_CallStaticObjectMethodV(JNIEnv *env, jclass clazz,
2279 jmethodID methodID, va_list args)
2284 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodV(env=%p, clazz=%p, methodID=%p)", env, clazz, methodID));
2286 m = (methodinfo *) methodID;
2288 o = _Jv_jni_CallObjectMethod(NULL, NULL, m, args);
2290 return jni_NewLocalRef(env, (jobject) o);
2294 jobject _Jv_JNI_CallStaticObjectMethodA(JNIEnv *env, jclass clazz,
2295 jmethodID methodID, const jvalue *args)
2300 TRACEJNICALLS(("_Jv_JNI_CallStaticObjectMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2302 m = (methodinfo *) methodID;
2304 o = _Jv_jni_CallObjectMethodA(NULL, NULL, m, args);
2306 return jni_NewLocalRef(env, (jobject) o);
2310 void _Jv_JNI_CallStaticVoidMethod(JNIEnv *env, jclass clazz,
2311 jmethodID methodID, ...)
2316 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethod(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
2318 m = (methodinfo *) methodID;
2320 va_start(ap, methodID);
2321 _Jv_jni_CallVoidMethod(NULL, NULL, m, ap);
2326 void _Jv_JNI_CallStaticVoidMethodV(JNIEnv *env, jclass clazz,
2327 jmethodID methodID, va_list args)
2331 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodV(env=%p, clazz=%p, methodID=%p)", env, clazz, methodID));
2333 m = (methodinfo *) methodID;
2335 _Jv_jni_CallVoidMethod(NULL, NULL, m, args);
2339 void _Jv_JNI_CallStaticVoidMethodA(JNIEnv *env, jclass clazz,
2340 jmethodID methodID, const jvalue * args)
2344 TRACEJNICALLS(("_Jv_JNI_CallStaticVoidMethodA(env=%p, clazz=%p, methodID=%p, args=%p)", env, clazz, methodID, args));
2346 m = (methodinfo *) methodID;
2348 _Jv_jni_CallVoidMethodA(NULL, NULL, m, args);
2352 /* Accessing Static Fields ****************************************************/
2354 /* GetStaticFieldID ************************************************************
2356 Returns the field ID for a static field of a class. The field is
2357 specified by its name and signature. The GetStatic<type>Field and
2358 SetStatic<type>Field families of accessor functions use field IDs
2359 to retrieve static fields.
2361 *******************************************************************************/
2363 jfieldID _Jv_JNI_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name,
2371 STATISTICS(jniinvokation());
2373 c = LLNI_classinfo_unwrap(clazz);
2375 uname = utf_new_char((char *) name);
2376 usig = utf_new_char((char *) sig);
2378 f = class_findfield(c, uname, usig);
2381 exceptions_throw_nosuchfielderror(c, uname);
2383 return (jfieldID) f;
2387 /* GetStatic<type>Field ********************************************************
2389 This family of accessor routines returns the value of a static
2392 *******************************************************************************/
2394 #define JNI_GET_STATIC_FIELD(name, type, field) \
2395 type _Jv_JNI_GetStatic##name##Field(JNIEnv *env, jclass clazz, \
2401 STATISTICS(jniinvokation()); \
2403 c = LLNI_classinfo_unwrap(clazz); \
2404 f = (fieldinfo *) fieldID; \
2406 if (!(c->state & CLASS_INITIALIZED)) \
2407 if (!initialize_class(c)) \
2410 return f->value->field; \
2413 JNI_GET_STATIC_FIELD(Boolean, jboolean, i)
2414 JNI_GET_STATIC_FIELD(Byte, jbyte, i)
2415 JNI_GET_STATIC_FIELD(Char, jchar, i)
2416 JNI_GET_STATIC_FIELD(Short, jshort, i)
2417 JNI_GET_STATIC_FIELD(Int, jint, i)
2418 JNI_GET_STATIC_FIELD(Long, jlong, l)
2419 JNI_GET_STATIC_FIELD(Float, jfloat, f)
2420 JNI_GET_STATIC_FIELD(Double, jdouble, d)
2423 jobject _Jv_JNI_GetStaticObjectField(JNIEnv *env, jclass clazz,
2430 STATISTICS(jniinvokation());
2432 c = LLNI_classinfo_unwrap(clazz);
2433 f = (fieldinfo *) fieldID;
2435 if (!(c->state & CLASS_INITIALIZED))
2436 if (!initialize_class(c))
2439 h = (java_handle_t*) LLNI_WRAP(f->value->a);
2441 return jni_NewLocalRef(env, (jobject) h);
2445 /* SetStatic<type>Field *******************************************************
2447 This family of accessor routines sets the value of a static field
2450 *******************************************************************************/
2452 #define JNI_SET_STATIC_FIELD(name, type, field) \
2453 void _Jv_JNI_SetStatic##name##Field(JNIEnv *env, jclass clazz, \
2460 STATISTICS(jniinvokation()); \
2462 c = LLNI_classinfo_unwrap(clazz); \
2463 f = (fieldinfo *) fieldID; \
2465 if (!(c->state & CLASS_INITIALIZED)) \
2466 if (!initialize_class(c)) \
2469 f->value->field = value; \
2471 if (f->flags & ACC_VOLATILE) \
2472 Atomic::memory_barrier(); \
2475 JNI_SET_STATIC_FIELD(Boolean, jboolean, i)
2476 JNI_SET_STATIC_FIELD(Byte, jbyte, i)
2477 JNI_SET_STATIC_FIELD(Char, jchar, i)
2478 JNI_SET_STATIC_FIELD(Short, jshort, i)
2479 JNI_SET_STATIC_FIELD(Int, jint, i)
2480 JNI_SET_STATIC_FIELD(Long, jlong, l)
2481 JNI_SET_STATIC_FIELD(Float, jfloat, f)
2482 JNI_SET_STATIC_FIELD(Double, jdouble, d)
2485 void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID,
2491 STATISTICS(jniinvokation());
2493 c = LLNI_classinfo_unwrap(clazz);
2494 f = (fieldinfo *) fieldID;
2496 if (!(c->state & CLASS_INITIALIZED))
2497 if (!initialize_class(c))
2500 f->value->a = LLNI_UNWRAP((java_handle_t *) value);
2502 if (f->flags & ACC_VOLATILE)
2503 Atomic::memory_barrier();
2507 /* String Operations **********************************************************/
2509 /* NewString *******************************************************************
2511 Create new java.lang.String object from an array of Unicode
2514 *******************************************************************************/
2516 jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len)
2518 TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len));
2528 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2529 for (jsize i = 0; i < len; i++)
2532 java_handle_t* h = builtin_new(class_java_lang_String);
2537 java_lang_String s(h, ca.get_handle(), len, 0);
2539 return (jstring) jni_NewLocalRef(env, (jobject) s.get_handle());
2543 static jchar emptyStringJ[]={0,0};
2545 /* GetStringLength *************************************************************
2547 Returns the length (the count of Unicode characters) of a Java
2550 *******************************************************************************/
2552 jsize jni_GetStringLength(JNIEnv *env, jstring str)
2554 TRACEJNICALLSENTER(("jni_GetStringLength(env=%p, str=%p)", env, str));
2556 java_lang_String s(str);
2557 jsize count = s.get_count();
2559 TRACEJNICALLSEXIT(("->%d)", count));
2565 /* GetStringChars **************************************************************
2567 Returns a pointer to the array of Unicode characters of the
2568 string. This pointer is valid until ReleaseStringChars() is called.
2570 *******************************************************************************/
2572 const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy)
2577 TRACEJNICALLS(("jni_GetStringChars(env=%p, str=%p, isCopy=%p)", env, str, isCopy));
2580 // FIXME This is really ugly.
2581 return emptyStringJ;
2583 java_lang_String s(str);
2585 CharArray ca(s.get_value());
2587 int32_t count = s.get_count();
2588 int32_t offset = s.get_offset();
2593 /* allocate memory */
2595 stringbuffer = MNEW(u2, count + 1);
2600 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2601 for (i = 0; i < count; i++)
2602 stringbuffer[i] = ptr[offset + i];
2604 /* terminate string */
2606 stringbuffer[i] = '\0';
2611 return (jchar*) stringbuffer;
2615 /* ReleaseStringChars **********************************************************
2617 Informs the VM that the native code no longer needs access to
2618 chars. The chars argument is a pointer obtained from string using
2621 *******************************************************************************/
2623 void _Jv_JNI_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)
2625 TRACEJNICALLS(("jni_ReleaseStringChars(env=%p, str=%p, chars=%p)", env, str, chars));
2628 if (chars == emptyStringJ)
2631 java_lang_String s(str);
2632 int32_t count = s.get_count();
2634 MFREE(((jchar*) chars), jchar, count + 1);
2638 /* NewStringUTF ****************************************************************
2640 Constructs a new java.lang.String object from an array of UTF-8
2643 *******************************************************************************/
2645 jstring jni_NewStringUTF(JNIEnv *env, const char *bytes)
2647 TRACEJNICALLS(("jni_NewStringUTF(env=%p, bytes=%s)", env, bytes));
2649 java_handle_t *h = javastring_safe_new_from_utf8(bytes);
2651 return (jstring) jni_NewLocalRef(env, (jobject) h);
2655 /****************** returns the utf8 length in bytes of a string *******************/
2657 jsize jni_GetStringUTFLength(JNIEnv *env, jstring string)
2659 TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string));
2661 java_lang_String s(string);
2662 CharArray ca(s.get_value());
2663 int32_t count = s.get_count();
2665 // FIXME GC critical section!
2666 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
2667 int32_t length = u2_utflength(ptr, count);
2673 /* GetStringUTFChars ***********************************************************
2675 Returns a pointer to an array of UTF-8 characters of the
2676 string. This array is valid until it is released by
2677 ReleaseStringUTFChars().
2679 *******************************************************************************/
2681 const char *_Jv_JNI_GetStringUTFChars(JNIEnv *env, jstring string,
2686 STATISTICS(jniinvokation());
2694 u = javastring_toutf((java_handle_t *) string, false);
2703 /* ReleaseStringUTFChars *******************************************************
2705 Informs the VM that the native code no longer needs access to
2706 utf. The utf argument is a pointer derived from string using
2707 GetStringUTFChars().
2709 *******************************************************************************/
2711 void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf)
2713 STATISTICS(jniinvokation());
2715 /* XXX we don't release utf chars right now, perhaps that should be done
2716 later. Since there is always one reference the garbage collector will
2721 /* Array Operations ***********************************************************/
2723 /* GetArrayLength **************************************************************
2725 Returns the number of elements in the array.
2727 *******************************************************************************/
2729 jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array)
2731 TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array));
2735 jsize size = a.get_length();
2741 /* NewObjectArray **************************************************************
2743 Constructs a new array holding objects in class elementClass. All
2744 elements are initially set to initialElement.
2746 *******************************************************************************/
2748 jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length,
2749 jclass elementClass, jobject initialElement)
2755 STATISTICS(jniinvokation());
2757 c = LLNI_classinfo_unwrap(elementClass);
2758 o = (java_handle_t *) initialElement;
2761 exceptions_throw_negativearraysizeexception();
2765 ObjectArray oa(length, c);
2770 /* set all elements to initialElement */
2772 for (i = 0; i < length; i++)
2773 oa.set_element(i, o);
2775 return (jobjectArray) jni_NewLocalRef(env, (jobject) oa.get_handle());
2779 jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array,
2782 STATISTICS(jniinvokation());
2784 ObjectArray oa(array);
2786 if (index >= oa.get_length()) {
2787 exceptions_throw_arrayindexoutofboundsexception();
2791 java_handle_t* o = oa.get_element(index);
2793 return jni_NewLocalRef(env, (jobject) o);
2797 void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array,
2798 jsize index, jobject val)
2800 STATISTICS(jniinvokation());
2802 ObjectArray oa(array);
2804 if (index >= oa.get_length()) {
2805 exceptions_throw_arrayindexoutofboundsexception();
2809 /* check if the class of value is a subclass of the element class
2812 java_handle_t* o = (java_handle_t *) val;
2814 if (!builtin_canstore(oa.get_handle(), o))
2817 oa.set_element(index, o);
2821 #define JNI_NEW_ARRAY(name, type) \
2822 type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \
2824 STATISTICS(jniinvokation()); \
2827 exceptions_throw_negativearraysizeexception(); \
2831 name##Array a(len); \
2833 return (type) jni_NewLocalRef(env, \
2834 (jobject) a.get_handle()); \
2837 JNI_NEW_ARRAY(Boolean, jbooleanArray)
2838 JNI_NEW_ARRAY(Byte, jbyteArray)
2839 JNI_NEW_ARRAY(Char, jcharArray)
2840 JNI_NEW_ARRAY(Short, jshortArray)
2841 JNI_NEW_ARRAY(Int, jintArray)
2842 JNI_NEW_ARRAY(Long, jlongArray)
2843 JNI_NEW_ARRAY(Float, jfloatArray)
2844 JNI_NEW_ARRAY(Double, jdoubleArray)
2847 /* Get<PrimitiveType>ArrayElements *********************************************
2849 A family of functions that returns the body of the primitive array.
2851 *******************************************************************************/
2853 #define JNI_GET_ARRAY_ELEMENTS(name, type, intern) \
2854 type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \
2857 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \
2859 name##Array a(array); \
2862 *isCopy = JNI_FALSE; \
2864 return (type *) a.get_raw_data_ptr(); \
2867 JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean)
2868 JNI_GET_ARRAY_ELEMENTS(Byte, jbyte, byte)
2869 JNI_GET_ARRAY_ELEMENTS(Char, jchar, char)
2870 JNI_GET_ARRAY_ELEMENTS(Short, jshort, short)
2871 JNI_GET_ARRAY_ELEMENTS(Int, jint, int)
2872 JNI_GET_ARRAY_ELEMENTS(Long, jlong, long)
2873 JNI_GET_ARRAY_ELEMENTS(Float, jfloat, float)
2874 JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double)
2877 /* Release<PrimitiveType>ArrayElements *****************************************
2879 A family of functions that informs the VM that the native code no
2880 longer needs access to elems. The elems argument is a pointer
2881 derived from array using the corresponding
2882 Get<PrimitiveType>ArrayElements() function. If necessary, this
2883 function copies back all changes made to elems to the original
2886 *******************************************************************************/
2888 #define JNI_RELEASE_ARRAY_ELEMENTS(name, type, intern, intern2) \
2889 void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \
2890 type *elems, jint mode) \
2892 STATISTICS(jniinvokation()); \
2894 name##Array a(array); \
2896 if (elems != (type *) a.get_raw_data_ptr()) { \
2899 a.set_region(0, a.get_length(), (intern2 *) elems); \
2902 a.set_region(0, a.get_length(), (intern2 *) elems); \
2903 /* XXX TWISTI how should it be freed? */ \
2906 /* XXX TWISTI how should it be freed? */ \
2912 JNI_RELEASE_ARRAY_ELEMENTS(Boolean, jboolean, boolean, u1)
2913 JNI_RELEASE_ARRAY_ELEMENTS(Byte, jbyte, byte, s1)
2914 JNI_RELEASE_ARRAY_ELEMENTS(Char, jchar, char, u2)
2915 JNI_RELEASE_ARRAY_ELEMENTS(Short, jshort, short, s2)
2916 JNI_RELEASE_ARRAY_ELEMENTS(Int, jint, int, s4)
2917 JNI_RELEASE_ARRAY_ELEMENTS(Long, jlong, long, s8)
2918 JNI_RELEASE_ARRAY_ELEMENTS(Float, jfloat, float, float)
2919 JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double)
2922 /* Get<PrimitiveType>ArrayRegion **********************************************
2924 A family of functions that copies a region of a primitive array
2927 *******************************************************************************/
2929 #define JNI_GET_ARRAY_REGION(name, type, intern, intern2) \
2930 void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \
2931 jsize start, jsize len, type *buf) \
2933 TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \
2935 name##Array a(array); \
2937 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2938 exceptions_throw_arrayindexoutofboundsexception(); \
2940 a.get_region(start, len, (intern2 *) buf); \
2943 JNI_GET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2944 JNI_GET_ARRAY_REGION(Byte, jbyte, byte, s1)
2945 JNI_GET_ARRAY_REGION(Char, jchar, char, u2)
2946 JNI_GET_ARRAY_REGION(Short, jshort, short, s2)
2947 JNI_GET_ARRAY_REGION(Int, jint, int, s4)
2948 JNI_GET_ARRAY_REGION(Long, jlong, long, s8)
2949 JNI_GET_ARRAY_REGION(Float, jfloat, float, float)
2950 JNI_GET_ARRAY_REGION(Double, jdouble, double, double)
2953 /* Set<PrimitiveType>ArrayRegion **********************************************
2955 A family of functions that copies back a region of a primitive
2956 array from a buffer.
2958 *******************************************************************************/
2960 #define JNI_SET_ARRAY_REGION(name, type, intern, intern2) \
2961 void _Jv_JNI_Set##name##ArrayRegion(JNIEnv *env, type##Array array, \
2962 jsize start, jsize len, const type *buf) \
2964 STATISTICS(jniinvokation()); \
2966 name##Array a(array); \
2968 if ((start < 0) || (len < 0) || (start + len > a.get_length())) \
2969 exceptions_throw_arrayindexoutofboundsexception(); \
2971 a.set_region(start, len, (intern2 *) buf); \
2974 JNI_SET_ARRAY_REGION(Boolean, jboolean, boolean, u1)
2975 JNI_SET_ARRAY_REGION(Byte, jbyte, byte, s1)
2976 JNI_SET_ARRAY_REGION(Char, jchar, char, u2)
2977 JNI_SET_ARRAY_REGION(Short, jshort, short, s2)
2978 JNI_SET_ARRAY_REGION(Int, jint, int, s4)
2979 JNI_SET_ARRAY_REGION(Long, jlong, long, s8)
2980 JNI_SET_ARRAY_REGION(Float, jfloat, float, float)
2981 JNI_SET_ARRAY_REGION(Double, jdouble, double, double)
2984 /* Registering Native Methods *************************************************/
2986 /* RegisterNatives *************************************************************
2988 Registers native methods with the class specified by the clazz
2989 argument. The methods parameter specifies an array of
2990 JNINativeMethod structures that contain the names, signatures, and
2991 function pointers of the native methods. The nMethods parameter
2992 specifies the number of native methods in the array.
2994 *******************************************************************************/
2996 jint jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods)
2998 TRACEJNICALLS(("jni_RegisterNatives(env=%p, clazz=%p, methods=%p, nMethods=%d)", env, clazz, methods, nMethods));
3000 classinfo* c = LLNI_classinfo_unwrap(clazz);
3002 NativeMethods& nm = VM::get_current()->get_nativemethods();
3003 nm.register_methods(c->name, methods, nMethods);
3009 /* UnregisterNatives ***********************************************************
3011 Unregisters native methods of a class. The class goes back to the
3012 state before it was linked or registered with its native method
3015 This function should not be used in normal native code. Instead, it
3016 provides special programs a way to reload and relink native
3019 *******************************************************************************/
3021 jint _Jv_JNI_UnregisterNatives(JNIEnv *env, jclass clazz)
3023 STATISTICS(jniinvokation());
3025 /* XXX TWISTI hmm, maybe we should not support that (like kaffe) */
3027 log_text("JNI-Call: UnregisterNatives: IMPLEMENT ME!!!");
3033 /* Monitor Operations *********************************************************/
3035 /* MonitorEnter ****************************************************************
3037 Enters the monitor associated with the underlying Java object
3040 *******************************************************************************/
3042 jint _Jv_JNI_MonitorEnter(JNIEnv *env, jobject obj)
3044 STATISTICS(jniinvokation());
3047 exceptions_throw_nullpointerexception();
3051 LOCK_MONITOR_ENTER(obj);
3057 /* MonitorExit *****************************************************************
3059 The current thread must be the owner of the monitor associated with
3060 the underlying Java object referred to by obj. The thread
3061 decrements the counter indicating the number of times it has
3062 entered this monitor. If the value of the counter becomes zero, the
3063 current thread releases the monitor.
3065 *******************************************************************************/
3067 jint _Jv_JNI_MonitorExit(JNIEnv *env, jobject obj)
3069 STATISTICS(jniinvokation());
3072 exceptions_throw_nullpointerexception();
3076 LOCK_MONITOR_EXIT(obj);
3082 /* JavaVM Interface ***********************************************************/
3084 /* GetJavaVM *******************************************************************
3086 Returns the Java VM interface (used in the Invocation API)
3087 associated with the current thread. The result is placed at the
3088 location pointed to by the second argument, vm.
3090 *******************************************************************************/
3092 jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm)
3094 STATISTICS(jniinvokation());
3096 *javavm = VM::get_current()->get_javavm();
3102 /* GetStringRegion *************************************************************
3104 Copies len number of Unicode characters beginning at offset start
3105 to the given buffer buf.
3107 Throws StringIndexOutOfBoundsException on index overflow.
3109 *******************************************************************************/
3111 void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf)
3113 java_lang_String s(str);
3114 CharArray ca(s.get_value());
3115 int32_t count = s.get_count();
3117 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3118 exceptions_throw_stringindexoutofboundsexception();
3122 ca.get_region(start, len, buf);
3126 /* GetStringUTFRegion **********************************************************
3128 Translates len number of Unicode characters beginning at offset
3129 start into UTF-8 format and place the result in the given buffer
3132 Throws StringIndexOutOfBoundsException on index overflow.
3134 *******************************************************************************/
3136 void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char *buf)
3138 TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf));
3140 java_lang_String s(str);
3142 CharArray ca(s.get_value());
3144 int32_t count = s.get_count();
3145 int32_t offset = s.get_offset();
3147 if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) {
3148 exceptions_throw_stringindexoutofboundsexception();
3155 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
3156 for (i = 0; i < len; i++)
3157 buf[i] = ptr[offset + start + i];
3163 /* GetPrimitiveArrayCritical ***************************************************
3165 Obtain a direct pointer to array elements.
3167 ATTENTION: Critical section keeps open when this function returns!
3168 See ReleasePrimitiveArrayCritical.
3170 *******************************************************************************/
3172 void* jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)
3176 arraydescriptor* ad;
3179 TRACEJNICALLS(("jni_GetPrimitiveArrayCritical(env=%p, array=%p, isCopy=%d)", env, array, isCopy));
3181 if (isCopy != NULL) {
3182 *isCopy = JNI_FALSE;
3185 LLNI_CRITICAL_START;
3187 h = (java_handle_t*) array;
3188 a = (java_array_t*) LLNI_UNWRAP(h);
3189 ad = a->objheader.vftbl->arraydesc;
3195 data = (void*) (((intptr_t) a) + ad->dataoffset);
3201 /* ReleasePrimitiveArrayCritical ***********************************************
3203 No specific documentation.
3205 ATTENTION: This function closes the critical section opened in
3206 GetPrimitiveArrayCritical!
3208 *******************************************************************************/
3210 void jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)
3212 TRACEJNICALLS(("jni_ReleasePrimitiveArrayCritical(env=%p, array=%p, carray=%p, mode=%d)", env, array, carray, mode));
3218 /* GetStringCritical ***********************************************************
3220 The semantics of these two functions are similar to the existing
3221 Get/ReleaseStringChars functions.
3223 *******************************************************************************/
3225 const jchar *_Jv_JNI_GetStringCritical(JNIEnv *env, jstring string,
3228 STATISTICS(jniinvokation());
3230 return jni_GetStringChars(env, string, isCopy);
3234 void _Jv_JNI_ReleaseStringCritical(JNIEnv *env, jstring string,
3235 const jchar *cstring)
3237 STATISTICS(jniinvokation());
3239 _Jv_JNI_ReleaseStringChars(env, string, cstring);
3243 jweak _Jv_JNI_NewWeakGlobalRef(JNIEnv* env, jobject obj)
3245 TRACEJNICALLS(("_Jv_JNI_NewWeakGlobalRef(env=%p, obj=%p): IMPLEMENT ME!", env, obj));
3251 void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
3253 TRACEJNICALLS(("_Jv_JNI_DeleteWeakGlobalRef(env=%p, ref=%p): IMPLEMENT ME", env, ref));
3257 /* NewGlobalRef ****************************************************************
3259 Creates a new global reference to the object referred to by the obj
3262 *******************************************************************************/
3264 jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
3266 hashtable_global_ref_entry *gre;
3267 u4 key; /* hashkey */
3268 u4 slot; /* slot in hashtable */
3271 TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
3273 o = (java_handle_t *) obj;
3275 hashtable_global_ref->mutex->lock();
3277 LLNI_CRITICAL_START;
3279 /* normally addresses are aligned to 4, 8 or 16 bytes */
3281 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3282 slot = key & (hashtable_global_ref->size - 1);
3283 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3285 /* search external hash chain for the entry */
3288 if (gre->o == LLNI_DIRECT(o)) {
3289 /* global object found, increment the reference */
3296 gre = gre->hashlink; /* next element in external chain */
3301 /* global ref not found, create a new one */
3304 gre = (hashtable_global_ref_entry*) heap_alloc_uncollectable(sizeof(hashtable_global_ref_entry));
3306 #if defined(ENABLE_GC_CACAO)
3307 /* register global ref with the GC */
3309 gc_reference_register(&(gre->o), GC_REFTYPE_JNI_GLOBALREF);
3312 LLNI_CRITICAL_START;
3314 gre->o = LLNI_DIRECT(o);
3319 /* insert entry into hashtable */
3321 gre->hashlink = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3323 hashtable_global_ref->ptr[slot] = gre;
3325 /* update number of hashtable-entries */
3327 hashtable_global_ref->entries++;
3330 hashtable_global_ref->mutex->unlock();
3332 #if defined(ENABLE_HANDLES)
3340 /* DeleteGlobalRef *************************************************************
3342 Deletes the global reference pointed to by globalRef.
3344 *******************************************************************************/
3346 void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
3348 hashtable_global_ref_entry *gre;
3349 hashtable_global_ref_entry *prevgre;
3350 u4 key; /* hashkey */
3351 u4 slot; /* slot in hashtable */
3354 TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
3356 o = (java_handle_t *) globalRef;
3358 hashtable_global_ref->mutex->lock();
3360 LLNI_CRITICAL_START;
3362 /* normally addresses are aligned to 4, 8 or 16 bytes */
3364 key = heap_hashcode(LLNI_DIRECT(o)) >> 4; /* align to 16-byte boundaries */
3365 slot = key & (hashtable_global_ref->size - 1);
3366 gre = (hashtable_global_ref_entry*) hashtable_global_ref->ptr[slot];
3368 /* initialize prevgre */
3372 /* search external hash chain for the entry */
3375 if (gre->o == LLNI_DIRECT(o)) {
3376 /* global object found, decrement the reference count */
3380 /* if reference count is 0, remove the entry */
3382 if (gre->refs == 0) {
3383 /* special handling if it's the first in the chain */
3385 if (prevgre == NULL)
3386 hashtable_global_ref->ptr[slot] = gre->hashlink;
3388 prevgre->hashlink = gre->hashlink;
3390 #if defined(ENABLE_GC_CACAO)
3391 /* unregister global ref with the GC */
3393 gc_reference_unregister(&(gre->o));
3401 hashtable_global_ref->mutex->unlock();
3406 prevgre = gre; /* save current pointer for removal */
3407 gre = gre->hashlink; /* next element in external chain */
3410 log_println("jni_DeleteGlobalRef: Global reference not found.");
3414 hashtable_global_ref->mutex->unlock();
3418 /* ExceptionCheck **************************************************************
3420 Returns JNI_TRUE when there is a pending exception; otherwise,
3423 *******************************************************************************/
3425 jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
3429 STATISTICS(jniinvokation());
3431 o = exceptions_get_exception();
3433 return (o != NULL) ? JNI_TRUE : JNI_FALSE;
3437 /* New JNI 1.4 functions ******************************************************/
3439 /* NewDirectByteBuffer *********************************************************
3441 Allocates and returns a direct java.nio.ByteBuffer referring to the
3442 block of memory starting at the memory address address and
3443 extending capacity bytes.
3445 *******************************************************************************/
3447 jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
3449 #if defined(ENABLE_JAVASE)
3450 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3451 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3453 // Allocate a gnu.classpath.Pointer{32,64} object.
3455 # if SIZEOF_VOID_P == 8
3456 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer64);
3458 java_handle_t* h = builtin_new(class_gnu_classpath_Pointer32);
3464 gnu_classpath_Pointer p(h, address);
3466 // Create a java.nio.DirectByteBufferImpl$ReadWrite object.
3468 java_handle_t* nbuf =
3469 (java_handle_t*) jni_NewObject(env, (jclass) class_java_nio_DirectByteBufferImpl_ReadWrite,
3470 (jmethodID) dbbirw_init, NULL, p.get_handle(),
3471 (jint) capacity, (jint) capacity, (jint) 0);
3473 // Add a local reference and return the value.
3475 TRACEJNICALLSEXIT(("->%p", nbuf));
3477 return jni_NewLocalRef(env, (jobject) nbuf);
3479 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3485 TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
3487 /* Be paranoid about address sign-extension. */
3489 addr = (int64_t) ((uintptr_t) address);
3490 cap = (int32_t) capacity;
3492 o = jni_NewObject(env, (jclass) class_java_nio_DirectByteBuffer,
3493 (jmethodID) dbb_init, addr, cap);
3495 /* Add local reference and return the value. */
3497 TRACEJNICALLSEXIT(("->%p", o));
3499 return jni_NewLocalRef(env, o);
3502 # error unknown classpath configuration
3506 vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
3508 /* keep compiler happy */
3515 /* GetDirectBufferAddress ******************************************************
3517 Fetches and returns the starting address of the memory region
3518 referenced by the given direct java.nio.Buffer.
3520 *******************************************************************************/
3522 void* jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3524 #if defined(ENABLE_JAVASE)
3525 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3527 TRACEJNICALLSENTER(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3529 /* Prevent compiler warning. */
3531 java_handle_t* h = (java_handle_t *) buf;
3533 if ((h != NULL) && !builtin_instanceof(h, class_java_nio_Buffer))
3536 java_nio_DirectByteBufferImpl dbb(buf);
3537 java_handle_t* address = dbb.get_address();
3539 if (address == NULL) {
3540 TRACEJNICALLSEXIT(("->%p", NULL));
3544 gnu_classpath_Pointer p(address);
3545 void* data = p.get_data();
3547 TRACEJNICALLSEXIT(("->%p", data));
3551 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3553 TRACEJNICALLS(("jni_GetDirectBufferAddress(env=%p, buf=%p)", env, buf));
3555 java_nio_Buffer jnb(buf);
3557 if (jnb.is_non_null() && !builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3560 void* address = jnb.get_address();
3565 # error unknown classpath configuration
3570 vm_abort("jni_GetDirectBufferAddress: Not implemented in this configuration.");
3572 // Keep compiler happy.
3579 /* GetDirectBufferCapacity *****************************************************
3581 Fetches and returns the capacity in bytes of the memory region
3582 referenced by the given direct java.nio.Buffer.
3584 *******************************************************************************/
3586 jlong jni_GetDirectBufferCapacity(JNIEnv* env, jobject buf)
3588 #if defined(ENABLE_JAVASE)
3589 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
3590 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3592 java_handle_t* h = (java_handle_t *) buf;
3594 if (!builtin_instanceof(h, class_java_nio_DirectByteBufferImpl))
3597 java_nio_Buffer b(h);
3598 jlong capacity = b.get_cap();
3601 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
3603 TRACEJNICALLS(("jni_GetDirectBufferCapacity(env=%p, buf=%p)", env, buf));
3605 java_nio_Buffer jnb(buf);
3607 if (!builtin_instanceof(jnb.get_handle(), class_sun_nio_ch_DirectBuffer))
3610 jlong capacity = jnb.get_capacity();
3615 # error unknown classpath configuration
3619 vm_abort("jni_GetDirectBufferCapacity: not implemented in this configuration");
3621 // Keep compiler happy.
3628 /* GetObjectRefType ************************************************************
3630 Returns the type of the object referred to by the obj argument. The
3631 argument obj can either be a local, global or weak global
3634 *******************************************************************************/
3636 jobjectRefType jni_GetObjectRefType(JNIEnv *env, jobject obj)
3638 log_println("jni_GetObjectRefType: IMPLEMENT ME!");
3640 return (jobjectRefType) NULL;
3644 /* DestroyJavaVM ***************************************************************
3646 Unloads a Java VM and reclaims its resources. Only the main thread
3647 can unload the VM. The system waits until the main thread is only
3648 remaining user thread before it destroys the VM.
3650 *******************************************************************************/
3652 jint _Jv_JNI_DestroyJavaVM(JavaVM *javavm)
3656 TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(javavm=%p)", javavm));
3658 if (VM::get_current()->is_created() == false)
3661 status = vm_destroy(javavm);
3667 /* AttachCurrentThread *********************************************************
3669 Attaches the current thread to a Java VM. Returns a JNI interface
3670 pointer in the JNIEnv argument.
3672 Trying to attach a thread that is already attached is a no-op.
3674 A native thread cannot be attached simultaneously to two Java VMs.
3676 When a thread is attached to the VM, the context class loader is
3677 the bootstrap loader.
3679 *******************************************************************************/
3681 static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
3683 #if defined(ENABLE_THREADS)
3684 JavaVMAttachArgs *vm_aargs;
3687 /* If the current thread has already been attached, this operation
3690 result = thread_current_is_attached();
3692 if (result == true) {
3693 *p_env = VM::get_current()->get_jnienv();
3697 vm_aargs = (JavaVMAttachArgs *) thr_args;
3699 if (vm_aargs != NULL) {
3700 if ((vm_aargs->version != JNI_VERSION_1_2) &&
3701 (vm_aargs->version != JNI_VERSION_1_4))
3702 return JNI_EVERSION;
3705 if (!thread_attach_current_external_thread(vm_aargs, false))
3708 if (!localref_table_init())
3712 *p_env = VM::get_current()->get_jnienv();
3718 jint jni_AttachCurrentThread(JavaVM *javavm, void **p_env, void *thr_args)
3722 TRACEJNICALLS(("jni_AttachCurrentThread(javavm=%p, p_env=%p, thr_args=%p)", javavm, p_env, thr_args));
3724 if (VM::get_current()->is_created() == false)
3727 result = jni_attach_current_thread(p_env, thr_args, false);
3733 /* DetachCurrentThread *********************************************************
3735 Detaches the current thread from a Java VM. All Java monitors held
3736 by this thread are released. All Java threads waiting for this
3737 thread to die are notified.
3739 In JDK 1.1, the main thread cannot be detached from the VM. It must
3740 call DestroyJavaVM to unload the entire VM.
3742 In the JDK, the main thread can be detached from the VM.
3744 The main thread, which is the thread that created the Java VM,
3745 cannot be detached from the VM. Instead, the main thread must call
3746 JNI_DestroyJavaVM() to unload the entire VM.
3748 *******************************************************************************/
3750 jint jni_DetachCurrentThread(JavaVM *vm)
3752 #if defined(ENABLE_THREADS)
3755 TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
3757 /* If the current thread has already been detached, this operation
3760 result = thread_current_is_attached();
3762 if (result == false)
3765 /* We need to pop all frames before we can destroy the table. */
3767 localref_frame_pop_all();
3769 if (!localref_table_destroy())
3772 if (!thread_detach_current_external_thread())
3780 /* GetEnv **********************************************************************
3782 If the current thread is not attached to the VM, sets *env to NULL,
3783 and returns JNI_EDETACHED. If the specified version is not
3784 supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise,
3785 sets *env to the appropriate interface, and returns JNI_OK.
3787 *******************************************************************************/
3789 jint jni_GetEnv(JavaVM *javavm, void **env, jint version)
3791 TRACEJNICALLS(("jni_GetEnv(javavm=%p, env=%p, version=%d)", javavm, env, version));
3793 if (VM::get_current()->is_created() == false) {
3795 return JNI_EDETACHED;
3798 #if defined(ENABLE_THREADS)
3799 if (thread_get_current() == NULL) {
3802 return JNI_EDETACHED;
3806 /* Check the JNI version. */
3808 if (jni_version_check(version) == true) {
3809 *env = VM::get_current()->get_jnienv();
3813 #if defined(ENABLE_JVMTI)
3814 if ((version & JVMTI_VERSION_MASK_INTERFACE_TYPE)
3815 == JVMTI_VERSION_INTERFACE_JVMTI) {
3817 *env = (void *) jvmti_new_environment();
3826 return JNI_EVERSION;
3830 /* AttachCurrentThreadAsDaemon *************************************************
3832 Same semantics as AttachCurrentThread, but the newly-created
3833 java.lang.Thread instance is a daemon.
3835 If the thread has already been attached via either
3836 AttachCurrentThread or AttachCurrentThreadAsDaemon, this routine
3837 simply sets the value pointed to by penv to the JNIEnv of the
3838 current thread. In this case neither AttachCurrentThread nor this
3839 routine have any effect on the daemon status of the thread.
3841 *******************************************************************************/
3843 jint jni_AttachCurrentThreadAsDaemon(JavaVM *javavm, void **penv, void *args)
3847 TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(javavm=%p, penv=%p, args=%p)", javavm, penv, args));
3849 if (VM::get_current()->is_created() == false)
3852 result = jni_attach_current_thread(penv, args, true);
3858 /* JNI invocation table *******************************************************/
3860 const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
3865 _Jv_JNI_DestroyJavaVM,
3866 jni_AttachCurrentThread,
3867 jni_DetachCurrentThread,
3869 jni_AttachCurrentThreadAsDaemon
3873 /* JNI function table *********************************************************/
3875 struct JNINativeInterface_ _Jv_JNINativeInterface = {
3884 jni_FromReflectedMethod,
3885 jni_FromReflectedField,
3886 jni_ToReflectedMethod,
3888 _Jv_JNI_IsAssignableFrom,
3889 _Jv_JNI_ToReflectedField,
3893 _Jv_JNI_ExceptionOccurred,
3894 jni_ExceptionDescribe,
3901 jni_DeleteGlobalRef,
3903 _Jv_JNI_IsSameObject,
3905 jni_EnsureLocalCapacity,
3907 _Jv_JNI_AllocObject,
3913 _Jv_JNI_IsInstanceOf,
3915 _Jv_JNI_GetMethodID,
3917 _Jv_JNI_CallObjectMethod,
3918 _Jv_JNI_CallObjectMethodV,
3919 _Jv_JNI_CallObjectMethodA,
3920 _Jv_JNI_CallBooleanMethod,
3921 _Jv_JNI_CallBooleanMethodV,
3922 _Jv_JNI_CallBooleanMethodA,
3923 _Jv_JNI_CallByteMethod,
3924 _Jv_JNI_CallByteMethodV,
3925 _Jv_JNI_CallByteMethodA,
3926 _Jv_JNI_CallCharMethod,
3927 _Jv_JNI_CallCharMethodV,
3928 _Jv_JNI_CallCharMethodA,
3929 _Jv_JNI_CallShortMethod,
3930 _Jv_JNI_CallShortMethodV,
3931 _Jv_JNI_CallShortMethodA,
3932 _Jv_JNI_CallIntMethod,
3933 _Jv_JNI_CallIntMethodV,
3934 _Jv_JNI_CallIntMethodA,
3935 _Jv_JNI_CallLongMethod,
3936 _Jv_JNI_CallLongMethodV,
3937 _Jv_JNI_CallLongMethodA,
3938 _Jv_JNI_CallFloatMethod,
3939 _Jv_JNI_CallFloatMethodV,
3940 _Jv_JNI_CallFloatMethodA,
3941 _Jv_JNI_CallDoubleMethod,
3942 _Jv_JNI_CallDoubleMethodV,
3943 _Jv_JNI_CallDoubleMethodA,
3944 _Jv_JNI_CallVoidMethod,
3945 _Jv_JNI_CallVoidMethodV,
3946 _Jv_JNI_CallVoidMethodA,
3948 _Jv_JNI_CallNonvirtualObjectMethod,
3949 _Jv_JNI_CallNonvirtualObjectMethodV,
3950 _Jv_JNI_CallNonvirtualObjectMethodA,
3951 _Jv_JNI_CallNonvirtualBooleanMethod,
3952 _Jv_JNI_CallNonvirtualBooleanMethodV,
3953 _Jv_JNI_CallNonvirtualBooleanMethodA,
3954 _Jv_JNI_CallNonvirtualByteMethod,
3955 _Jv_JNI_CallNonvirtualByteMethodV,
3956 _Jv_JNI_CallNonvirtualByteMethodA,
3957 _Jv_JNI_CallNonvirtualCharMethod,
3958 _Jv_JNI_CallNonvirtualCharMethodV,
3959 _Jv_JNI_CallNonvirtualCharMethodA,
3960 _Jv_JNI_CallNonvirtualShortMethod,
3961 _Jv_JNI_CallNonvirtualShortMethodV,
3962 _Jv_JNI_CallNonvirtualShortMethodA,
3963 _Jv_JNI_CallNonvirtualIntMethod,
3964 _Jv_JNI_CallNonvirtualIntMethodV,
3965 _Jv_JNI_CallNonvirtualIntMethodA,
3966 _Jv_JNI_CallNonvirtualLongMethod,
3967 _Jv_JNI_CallNonvirtualLongMethodV,
3968 _Jv_JNI_CallNonvirtualLongMethodA,
3969 _Jv_JNI_CallNonvirtualFloatMethod,
3970 _Jv_JNI_CallNonvirtualFloatMethodV,
3971 _Jv_JNI_CallNonvirtualFloatMethodA,
3972 _Jv_JNI_CallNonvirtualDoubleMethod,
3973 _Jv_JNI_CallNonvirtualDoubleMethodV,
3974 _Jv_JNI_CallNonvirtualDoubleMethodA,
3975 _Jv_JNI_CallNonvirtualVoidMethod,
3976 _Jv_JNI_CallNonvirtualVoidMethodV,
3977 _Jv_JNI_CallNonvirtualVoidMethodA,
3981 _Jv_JNI_GetObjectField,
3982 _Jv_JNI_GetBooleanField,
3983 _Jv_JNI_GetByteField,
3984 _Jv_JNI_GetCharField,
3985 _Jv_JNI_GetShortField,
3986 _Jv_JNI_GetIntField,
3987 _Jv_JNI_GetLongField,
3988 _Jv_JNI_GetFloatField,
3989 _Jv_JNI_GetDoubleField,
3990 _Jv_JNI_SetObjectField,
3991 _Jv_JNI_SetBooleanField,
3992 _Jv_JNI_SetByteField,
3993 _Jv_JNI_SetCharField,
3994 _Jv_JNI_SetShortField,
3995 _Jv_JNI_SetIntField,
3996 _Jv_JNI_SetLongField,
3997 _Jv_JNI_SetFloatField,
3998 _Jv_JNI_SetDoubleField,
4000 _Jv_JNI_GetStaticMethodID,
4002 _Jv_JNI_CallStaticObjectMethod,
4003 _Jv_JNI_CallStaticObjectMethodV,
4004 _Jv_JNI_CallStaticObjectMethodA,
4005 _Jv_JNI_CallStaticBooleanMethod,
4006 _Jv_JNI_CallStaticBooleanMethodV,
4007 _Jv_JNI_CallStaticBooleanMethodA,
4008 _Jv_JNI_CallStaticByteMethod,
4009 _Jv_JNI_CallStaticByteMethodV,
4010 _Jv_JNI_CallStaticByteMethodA,
4011 _Jv_JNI_CallStaticCharMethod,
4012 _Jv_JNI_CallStaticCharMethodV,
4013 _Jv_JNI_CallStaticCharMethodA,
4014 _Jv_JNI_CallStaticShortMethod,
4015 _Jv_JNI_CallStaticShortMethodV,
4016 _Jv_JNI_CallStaticShortMethodA,
4017 _Jv_JNI_CallStaticIntMethod,
4018 _Jv_JNI_CallStaticIntMethodV,
4019 _Jv_JNI_CallStaticIntMethodA,
4020 _Jv_JNI_CallStaticLongMethod,
4021 _Jv_JNI_CallStaticLongMethodV,
4022 _Jv_JNI_CallStaticLongMethodA,
4023 _Jv_JNI_CallStaticFloatMethod,
4024 _Jv_JNI_CallStaticFloatMethodV,
4025 _Jv_JNI_CallStaticFloatMethodA,
4026 _Jv_JNI_CallStaticDoubleMethod,
4027 _Jv_JNI_CallStaticDoubleMethodV,
4028 _Jv_JNI_CallStaticDoubleMethodA,
4029 _Jv_JNI_CallStaticVoidMethod,
4030 _Jv_JNI_CallStaticVoidMethodV,
4031 _Jv_JNI_CallStaticVoidMethodA,
4033 _Jv_JNI_GetStaticFieldID,
4035 _Jv_JNI_GetStaticObjectField,
4036 _Jv_JNI_GetStaticBooleanField,
4037 _Jv_JNI_GetStaticByteField,
4038 _Jv_JNI_GetStaticCharField,
4039 _Jv_JNI_GetStaticShortField,
4040 _Jv_JNI_GetStaticIntField,
4041 _Jv_JNI_GetStaticLongField,
4042 _Jv_JNI_GetStaticFloatField,
4043 _Jv_JNI_GetStaticDoubleField,
4044 _Jv_JNI_SetStaticObjectField,
4045 _Jv_JNI_SetStaticBooleanField,
4046 _Jv_JNI_SetStaticByteField,
4047 _Jv_JNI_SetStaticCharField,
4048 _Jv_JNI_SetStaticShortField,
4049 _Jv_JNI_SetStaticIntField,
4050 _Jv_JNI_SetStaticLongField,
4051 _Jv_JNI_SetStaticFloatField,
4052 _Jv_JNI_SetStaticDoubleField,
4055 jni_GetStringLength,
4057 _Jv_JNI_ReleaseStringChars,
4060 jni_GetStringUTFLength,
4061 _Jv_JNI_GetStringUTFChars,
4062 _Jv_JNI_ReleaseStringUTFChars,
4064 _Jv_JNI_GetArrayLength,
4066 _Jv_JNI_NewObjectArray,
4067 _Jv_JNI_GetObjectArrayElement,
4068 _Jv_JNI_SetObjectArrayElement,
4070 _Jv_JNI_NewBooleanArray,
4071 _Jv_JNI_NewByteArray,
4072 _Jv_JNI_NewCharArray,
4073 _Jv_JNI_NewShortArray,
4074 _Jv_JNI_NewIntArray,
4075 _Jv_JNI_NewLongArray,
4076 _Jv_JNI_NewFloatArray,
4077 _Jv_JNI_NewDoubleArray,
4079 _Jv_JNI_GetBooleanArrayElements,
4080 _Jv_JNI_GetByteArrayElements,
4081 _Jv_JNI_GetCharArrayElements,
4082 _Jv_JNI_GetShortArrayElements,
4083 _Jv_JNI_GetIntArrayElements,
4084 _Jv_JNI_GetLongArrayElements,
4085 _Jv_JNI_GetFloatArrayElements,
4086 _Jv_JNI_GetDoubleArrayElements,
4088 _Jv_JNI_ReleaseBooleanArrayElements,
4089 _Jv_JNI_ReleaseByteArrayElements,
4090 _Jv_JNI_ReleaseCharArrayElements,
4091 _Jv_JNI_ReleaseShortArrayElements,
4092 _Jv_JNI_ReleaseIntArrayElements,
4093 _Jv_JNI_ReleaseLongArrayElements,
4094 _Jv_JNI_ReleaseFloatArrayElements,
4095 _Jv_JNI_ReleaseDoubleArrayElements,
4097 _Jv_JNI_GetBooleanArrayRegion,
4098 _Jv_JNI_GetByteArrayRegion,
4099 _Jv_JNI_GetCharArrayRegion,
4100 _Jv_JNI_GetShortArrayRegion,
4101 _Jv_JNI_GetIntArrayRegion,
4102 _Jv_JNI_GetLongArrayRegion,
4103 _Jv_JNI_GetFloatArrayRegion,
4104 _Jv_JNI_GetDoubleArrayRegion,
4105 _Jv_JNI_SetBooleanArrayRegion,
4106 _Jv_JNI_SetByteArrayRegion,
4107 _Jv_JNI_SetCharArrayRegion,
4108 _Jv_JNI_SetShortArrayRegion,
4109 _Jv_JNI_SetIntArrayRegion,
4110 _Jv_JNI_SetLongArrayRegion,
4111 _Jv_JNI_SetFloatArrayRegion,
4112 _Jv_JNI_SetDoubleArrayRegion,
4114 jni_RegisterNatives,
4115 _Jv_JNI_UnregisterNatives,
4117 _Jv_JNI_MonitorEnter,
4118 _Jv_JNI_MonitorExit,
4122 /* New JNI 1.2 functions. */
4124 jni_GetStringRegion,
4125 jni_GetStringUTFRegion,
4127 jni_GetPrimitiveArrayCritical,
4128 jni_ReleasePrimitiveArrayCritical,
4130 _Jv_JNI_GetStringCritical,
4131 _Jv_JNI_ReleaseStringCritical,
4133 _Jv_JNI_NewWeakGlobalRef,
4134 _Jv_JNI_DeleteWeakGlobalRef,
4136 _Jv_JNI_ExceptionCheck,
4138 /* New JNI 1.4 functions. */
4140 jni_NewDirectByteBuffer,
4141 jni_GetDirectBufferAddress,
4142 jni_GetDirectBufferCapacity,
4144 /* New JNI 1.6 functions. */
4146 jni_GetObjectRefType
4150 /* Invocation API Functions ***************************************************/
4152 /* JNI_GetDefaultJavaVMInitArgs ************************************************
4154 Returns a default configuration for the Java VM.
4156 *******************************************************************************/
4158 jint JNI_GetDefaultJavaVMInitArgs(void *vm_args)
4160 JavaVMInitArgs *_vm_args;
4162 _vm_args = (JavaVMInitArgs *) vm_args;
4164 /* GNU classpath currently supports JNI 1.2 */
4166 switch (_vm_args->version) {
4167 case JNI_VERSION_1_1:
4168 _vm_args->version = JNI_VERSION_1_1;
4171 case JNI_VERSION_1_2:
4172 case JNI_VERSION_1_4:
4173 _vm_args->ignoreUnrecognized = JNI_FALSE;
4174 _vm_args->options = NULL;
4175 _vm_args->nOptions = 0;
4178 case JNI_VERSION_CACAO:
4179 // We reveal ourselves by accepting this version number,
4180 // this actually means we are using the supported JNI version.
4181 _vm_args->version = JNI_VERSION_SUPPORTED;
4192 /* JNI_GetCreatedJavaVMs *******************************************************
4194 Returns all Java VMs that have been created. Pointers to VMs are written in
4195 the buffer vmBuf in the order they are created. At most bufLen number of
4196 entries will be written. The total number of created VMs is returned in
4199 *******************************************************************************/
4201 jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
4203 TRACEJNICALLS(("JNI_GetCreatedJavaVMs(vmBuf=%p, jsize=%d, jsize=%p)", vmBuf, bufLen, nVMs));
4208 // We currently only support 1 VM running.
4210 vmBuf[0] = VM::get_current()->get_javavm();
4217 /* JNI_CreateJavaVM ************************************************************
4219 Loads and initializes a Java VM. The current thread becomes the main thread.
4220 Sets the env argument to the JNI interface pointer of the main thread.
4222 *******************************************************************************/
4224 jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
4226 TRACEJNICALLS(("JNI_CreateJavaVM(p_vm=%p, p_env=%p, vm_args=%p)", p_vm, p_env, vm_args));
4228 /* actually create the JVM */
4230 if (!VM_create(p_vm, p_env, vm_args))
4240 * These are local overrides for various environment variables in Emacs.
4241 * Please do not remove this and leave it at the end of the file, where
4242 * Emacs will automagically detect them.
4243 * ---------------------------------------------------------------------
4246 * indent-tabs-mode: t
4250 * vim:noexpandtab:sw=4:ts=4: