X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fnative%2Fjni.cpp;h=d33892469c10a7621c96efdc14b6e8dd2a921708;hb=6e920e48a314cfed80ff5db1b026c28049cba600;hp=00b6afd8f8fcd49853d74493c61f84035ad51a7b;hpb=bf89824bc40714a11244f5f577936ba527977e65;p=cacao.git diff --git a/src/native/jni.cpp b/src/native/jni.cpp index 00b6afd8f..d33892469 100644 --- a/src/native/jni.cpp +++ b/src/native/jni.cpp @@ -32,40 +32,40 @@ #include "vm/types.h" #include "mm/gc.hpp" -#include "mm/memory.h" +#include "mm/memory.hpp" #include "native/jni.hpp" #include "native/llni.h" -#include "native/localref.h" -#include "native/native.h" +#include "native/localref.hpp" +#include "native/native.hpp" #if defined(ENABLE_JVMTI) # include "native/jvmti/cacaodbg.h" #endif -#include "threads/lock-common.h" +#include "threads/lock.hpp" +#include "threads/mutex.hpp" #include "threads/thread.hpp" -#include "toolbox/logging.h" +#include "toolbox/logging.hpp" -#include "vm/array.h" -#include "vm/builtin.h" +#include "vm/array.hpp" +#include "vm/jit/builtin.hpp" #include "vm/exceptions.hpp" #include "vm/global.h" #include "vm/globals.hpp" -#include "vm/initialize.h" +#include "vm/initialize.hpp" #include "vm/javaobjects.hpp" -#include "vm/loader.h" +#include "vm/loader.hpp" #include "vm/options.h" #include "vm/primitive.hpp" -#include "vm/resolve.h" +#include "vm/resolve.hpp" #include "vm/statistics.h" #include "vm/string.hpp" #include "vm/vm.hpp" -#include "vm/jit/argument.h" #include "vm/jit/asmpart.h" -#include "vm/jit/jit.h" +#include "vm/jit/jit.hpp" #include "vm/jit/stacktrace.hpp" @@ -793,9 +793,7 @@ jint _Jv_JNI_GetVersion(JNIEnv *env) { TRACEJNICALLS(("_Jv_JNI_GetVersion(env=%p)", env)); - /* We support JNI 1.6. */ - - return JNI_VERSION_1_6; + return JNI_VERSION_SUPPORTED; } @@ -2484,22 +2482,25 @@ void _Jv_JNI_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jstring jni_NewString(JNIEnv *env, const jchar *buf, jsize len) { TRACEJNICALLS(("jni_NewString(env=%p, buf=%p, len=%d)", env, buf, len)); - - java_handle_chararray_t* a = builtin_newarray_char(len); - if (a == NULL) + CharArray ca(len); + + if (ca.is_null()) return NULL; /* copy text */ + + // XXX: Fix me! + uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr(); for (jsize i = 0; i < len; i++) - LLNI_array_direct(a, i) = buf[i]; + ptr[i] = buf[i]; java_handle_t* h = builtin_new(class_java_lang_String); if (h == NULL) return NULL; - java_lang_String s(h, a, len, 0); + java_lang_String s(h, ca.get_handle(), len, 0); return (jstring) jni_NewLocalRef(env, (jobject) s.get_handle()); } @@ -2547,11 +2548,12 @@ const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy) java_lang_String s(str); - java_handle_chararray_t* ca = s.get_value(); - int32_t count = s.get_count(); - int32_t offset = s.get_offset(); + CharArray ca(s.get_value()); + + int32_t count = s.get_count(); + int32_t offset = s.get_offset(); - if (ca == NULL) + if (ca.is_null()) return NULL; /* allocate memory */ @@ -2560,8 +2562,10 @@ const jchar* jni_GetStringChars(JNIEnv *env, jstring str, jboolean *isCopy) /* copy text */ + // XXX: Fix me! + uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr(); for (i = 0; i < count; i++) - stringbuffer[i] = LLNI_array_direct(ca, offset + i); + stringbuffer[i] = ptr[offset + i]; /* terminate string */ @@ -2621,11 +2625,12 @@ jsize jni_GetStringUTFLength(JNIEnv *env, jstring string) TRACEJNICALLS(("jni_GetStringUTFLength(env=%p, string=%p)", env, string)); java_lang_String s(string); - java_handle_chararray_t* ca = s.get_value(); - int32_t count = s.get_count(); + CharArray ca(s.get_value()); + int32_t count = s.get_count(); // FIXME GC critical section! - int32_t length = u2_utflength(ca->data, count); + uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr(); + int32_t length = u2_utflength(ptr, count); return length; } @@ -2689,14 +2694,11 @@ void _Jv_JNI_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf) jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array) { - java_handle_t *a; - jsize size; - TRACEJNICALLS(("_Jv_JNI_GetArrayLength(env=%p, array=%p)", env, array)); - a = (java_handle_t *) array; + Array a(array); - size = LLNI_array_size(a); + jsize size = a.get_length(); return size; } @@ -2712,10 +2714,9 @@ jsize _Jv_JNI_GetArrayLength(JNIEnv *env, jarray array) jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement) { - classinfo *c; - java_handle_t *o; - java_handle_objectarray_t *oa; - s4 i; + classinfo* c; + java_handle_t* o; + s4 i; STATISTICS(jniinvokation()); @@ -2727,36 +2728,33 @@ jobjectArray _Jv_JNI_NewObjectArray(JNIEnv *env, jsize length, return NULL; } - oa = builtin_anewarray(length, c); + ObjectArray oa(length, c); - if (oa == NULL) + if (oa.is_null()) return NULL; /* set all elements to initialElement */ for (i = 0; i < length; i++) - array_objectarray_element_set(oa, i, o); + oa.set_element(i, o); - return (jobjectArray) jni_NewLocalRef(env, (jobject) oa); + return (jobjectArray) jni_NewLocalRef(env, (jobject) oa.get_handle()); } jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index) { - java_handle_objectarray_t *oa; - java_handle_t *o; - STATISTICS(jniinvokation()); - oa = (java_handle_objectarray_t *) array; + ObjectArray oa(array); - if (index >= LLNI_array_size(oa)) { + if (index >= oa.get_length()) { exceptions_throw_arrayindexoutofboundsexception(); return NULL; } - o = array_objectarray_element_get(oa, index); + java_handle_t* o = oa.get_element(index); return jni_NewLocalRef(env, (jobject) o); } @@ -2765,15 +2763,11 @@ jobject _Jv_JNI_GetObjectArrayElement(JNIEnv *env, jobjectArray array, void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject val) { - java_handle_objectarray_t *oa; - java_handle_t *o; - STATISTICS(jniinvokation()); - oa = (java_handle_objectarray_t *) array; - o = (java_handle_t *) val; + ObjectArray oa(array); - if (index >= LLNI_array_size(oa)) { + if (index >= oa.get_length()) { exceptions_throw_arrayindexoutofboundsexception(); return; } @@ -2781,18 +2775,18 @@ void _Jv_JNI_SetObjectArrayElement(JNIEnv *env, jobjectArray array, /* check if the class of value is a subclass of the element class of the array */ - if (!builtin_canstore(oa, o)) + java_handle_t* o = (java_handle_t *) val; + + if (!builtin_canstore(oa.get_handle(), o)) return; - array_objectarray_element_set(oa, index, o); + oa.set_element(index, o); } -#define JNI_NEW_ARRAY(name, type, intern) \ +#define JNI_NEW_ARRAY(name, type) \ type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \ { \ - java_handle_##intern##array_t *a; \ - \ STATISTICS(jniinvokation()); \ \ if (len < 0) { \ @@ -2800,19 +2794,20 @@ type _Jv_JNI_New##name##Array(JNIEnv *env, jsize len) \ return NULL; \ } \ \ - a = builtin_newarray_##intern(len); \ + name##Array a(len); \ \ - return (type) jni_NewLocalRef(env, (jobject) a); \ + return (type) jni_NewLocalRef(env, \ + (jobject) a.get_handle()); \ } -JNI_NEW_ARRAY(Boolean, jbooleanArray, boolean) -JNI_NEW_ARRAY(Byte, jbyteArray, byte) -JNI_NEW_ARRAY(Char, jcharArray, char) -JNI_NEW_ARRAY(Short, jshortArray, short) -JNI_NEW_ARRAY(Int, jintArray, int) -JNI_NEW_ARRAY(Long, jlongArray, long) -JNI_NEW_ARRAY(Float, jfloatArray, float) -JNI_NEW_ARRAY(Double, jdoubleArray, double) +JNI_NEW_ARRAY(Boolean, jbooleanArray) +JNI_NEW_ARRAY(Byte, jbyteArray) +JNI_NEW_ARRAY(Char, jcharArray) +JNI_NEW_ARRAY(Short, jshortArray) +JNI_NEW_ARRAY(Int, jintArray) +JNI_NEW_ARRAY(Long, jlongArray) +JNI_NEW_ARRAY(Float, jfloatArray) +JNI_NEW_ARRAY(Double, jdoubleArray) /* GetArrayElements ********************************************* @@ -2825,16 +2820,14 @@ JNI_NEW_ARRAY(Double, jdoubleArray, double) type *_Jv_JNI_Get##name##ArrayElements(JNIEnv *env, type##Array array, \ jboolean *isCopy) \ { \ - java_handle_##intern##array_t *a; \ - \ TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayElements(env=%p, array=%p, isCopy=%d)", env, array, isCopy)); \ \ - a = (java_handle_##intern##array_t *) array; \ + name##Array a(array); \ \ if (isCopy) \ *isCopy = JNI_FALSE; \ \ - return (type *) LLNI_array_data(a); \ + return (type *) a.get_raw_data_ptr(); \ } JNI_GET_ARRAY_ELEMENTS(Boolean, jboolean, boolean) @@ -2862,19 +2855,17 @@ JNI_GET_ARRAY_ELEMENTS(Double, jdouble, double) void _Jv_JNI_Release##name##ArrayElements(JNIEnv *env, type##Array array, \ type *elems, jint mode) \ { \ - java_handle_##intern##array_t *a; \ - \ STATISTICS(jniinvokation()); \ \ - a = (java_handle_##intern##array_t *) array; \ + name##Array a(array); \ \ - if (elems != (type *) LLNI_array_data(a)) { \ + if (elems != (type *) a.get_raw_data_ptr()) { \ switch (mode) { \ case JNI_COMMIT: \ - MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \ + a.set_region(0, a.get_length(), (intern2 *) elems); \ break; \ case 0: \ - MCOPY(LLNI_array_data(a), elems, intern2, LLNI_array_size(a)); \ + a.set_region(0, a.get_length(), (intern2 *) elems); \ /* XXX TWISTI how should it be freed? */ \ break; \ case JNI_ABORT: \ @@ -2905,16 +2896,14 @@ JNI_RELEASE_ARRAY_ELEMENTS(Double, jdouble, double, double) void _Jv_JNI_Get##name##ArrayRegion(JNIEnv *env, type##Array array, \ jsize start, jsize len, type *buf) \ { \ - java_handle_##intern##array_t *a; \ - \ TRACEJNICALLS(("_Jv_JNI_Get" STR(name) "ArrayRegion(env=%p, array=%p, start=%d, len=%d, buf=%p)", env, array, start, len, buf)); \ \ - a = (java_handle_##intern##array_t *) array; \ + name##Array a(array); \ \ - if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \ + if ((start < 0) || (len < 0) || (start + len > a.get_length())) \ exceptions_throw_arrayindexoutofboundsexception(); \ else \ - MCOPY(buf, &LLNI_array_direct(a, start), intern2, len); \ + a.get_region(start, len, (intern2 *) buf); \ } JNI_GET_ARRAY_REGION(Boolean, jboolean, boolean, u1) @@ -2938,16 +2927,14 @@ JNI_GET_ARRAY_REGION(Double, jdouble, double, double) void _Jv_JNI_Set##name##ArrayRegion(JNIEnv *env, type##Array array, \ jsize start, jsize len, const type *buf) \ { \ - java_handle_##intern##array_t *a; \ - \ STATISTICS(jniinvokation()); \ \ - a = (java_handle_##intern##array_t *) array; \ + name##Array a(array); \ \ - if ((start < 0) || (len < 0) || (start + len > LLNI_array_size(a))) \ + if ((start < 0) || (len < 0) || (start + len > a.get_length())) \ exceptions_throw_arrayindexoutofboundsexception(); \ else \ - MCOPY(&LLNI_array_direct(a, start), buf, intern2, len); \ + a.set_region(start, len, (intern2 *) buf); \ } JNI_SET_ARRAY_REGION(Boolean, jboolean, boolean, u1) @@ -2972,20 +2959,18 @@ JNI_SET_ARRAY_REGION(Double, jdouble, double, double) *******************************************************************************/ -jint _Jv_JNI_RegisterNatives(JNIEnv *env, jclass clazz, - const JNINativeMethod *methods, jint nMethods) +jint jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { - classinfo *c; + TRACEJNICALLS(("jni_RegisterNatives(env=%p, clazz=%p, methods=%p, nMethods=%d)", env, clazz, methods, nMethods)); - STATISTICS(jniinvokation()); - - c = LLNI_classinfo_unwrap(clazz); + classinfo* c = LLNI_classinfo_unwrap(clazz); /* XXX: if implemented this needs a call to jvmti_NativeMethodBind if (jvmti) jvmti_NativeMethodBind(method, address, new_address_ptr); */ - native_method_register(c->name, methods, nMethods); + NativeMethods& nm = VM::get_current()->get_nativemethods(); + nm.register_methods(c->name, methods, nMethods); return 0; } @@ -3096,15 +3081,15 @@ jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **javavm) void jni_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar *buf) { java_lang_String s(str); - java_handle_chararray_t* ca = s.get_value(); - int32_t count = s.get_count(); + CharArray ca(s.get_value()); + int32_t count = s.get_count(); if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) { exceptions_throw_stringindexoutofboundsexception(); return; } - MCOPY(buf, &LLNI_array_direct(ca, start), u2, len); + ca.get_region(start, len, buf); } @@ -3123,9 +3108,11 @@ void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, ch TRACEJNICALLS(("jni_GetStringUTFRegion(env=%p, str=%p, start=%d, len=%d, buf=%p)", env, str, start, len, buf)); java_lang_String s(str); - java_handle_chararray_t* ca = s.get_value(); - int32_t count = s.get_count(); - int32_t offset = s.get_offset(); + + CharArray ca(s.get_value()); + + int32_t count = s.get_count(); + int32_t offset = s.get_offset(); if ((start < 0) || (len < 0) || (start > count) || (start + len > count)) { exceptions_throw_stringindexoutofboundsexception(); @@ -3134,8 +3121,10 @@ void jni_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, ch int32_t i; + // XXX: Fix me! + uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr(); for (i = 0; i < len; i++) - buf[i] = LLNI_array_direct(ca, offset + start + i); + buf[i] = ptr[offset + start + i]; buf[i] = '\0'; } @@ -3253,7 +3242,7 @@ jobject jni_NewGlobalRef(JNIEnv* env, jobject obj) o = (java_handle_t *) obj; - LOCK_MONITOR_ENTER(hashtable_global_ref->header); + hashtable_global_ref->mutex->lock(); LLNI_CRITICAL_START; @@ -3308,7 +3297,7 @@ jobject jni_NewGlobalRef(JNIEnv* env, jobject obj) hashtable_global_ref->entries++; } - LOCK_MONITOR_EXIT(hashtable_global_ref->header); + hashtable_global_ref->mutex->unlock(); #if defined(ENABLE_HANDLES) return gre; @@ -3336,7 +3325,7 @@ void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef) o = (java_handle_t *) globalRef; - LOCK_MONITOR_ENTER(hashtable_global_ref->header); + hashtable_global_ref->mutex->lock(); LLNI_CRITICAL_START; @@ -3379,7 +3368,7 @@ void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef) LLNI_CRITICAL_END; - LOCK_MONITOR_EXIT(hashtable_global_ref->header); + hashtable_global_ref->mutex->unlock(); return; } @@ -3392,7 +3381,7 @@ void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef) LLNI_CRITICAL_END; - LOCK_MONITOR_EXIT(hashtable_global_ref->header); + hashtable_global_ref->mutex->unlock(); } @@ -4074,7 +4063,7 @@ struct JNINativeInterface_ _Jv_JNINativeInterface = { _Jv_JNI_SetFloatArrayRegion, _Jv_JNI_SetDoubleArrayRegion, - _Jv_JNI_RegisterNatives, + jni_RegisterNatives, _Jv_JNI_UnregisterNatives, _Jv_JNI_MonitorEnter, @@ -4127,22 +4116,28 @@ jint JNI_GetDefaultJavaVMInitArgs(void *vm_args) /* GNU classpath currently supports JNI 1.2 */ switch (_vm_args->version) { - case JNI_VERSION_1_1: + case JNI_VERSION_1_1: _vm_args->version = JNI_VERSION_1_1; break; - case JNI_VERSION_1_2: - case JNI_VERSION_1_4: + case JNI_VERSION_1_2: + case JNI_VERSION_1_4: _vm_args->ignoreUnrecognized = JNI_FALSE; _vm_args->options = NULL; _vm_args->nOptions = 0; break; - default: - return -1; + case JNI_VERSION_CACAO: + // We reveal ourselves by accepting this version number, + // this actually means we are using the supported JNI version. + _vm_args->version = JNI_VERSION_SUPPORTED; + break; + + default: + return JNI_ERR; } - - return 0; + + return JNI_OK; }