2004-08-28 Martin Baulig <martin@ximian.com>
[mono.git] / ikvm-jni / mono-jni.c
index b015004cb9526a981816ceec1127d89504edbe3a..0997f6a32dd8ded39f113550a126129d57cf816a 100644 (file)
@@ -35,7 +35,8 @@ void mono_jni_jnienv_init (
        void *invokemethod_func,
        void *getmethodarglist_func,
        void *findclass_func,
-       void *getjnienv_func);
+       void *getjnienv_func,
+       void *allocobject_func);
 
 void* mono_jni_get_func_table (void);
 
@@ -74,6 +75,7 @@ typedef struct MonoJniFunctions {
        void * (*GetMethodArgList) (void *cookie);
        void * (*FindClass) (void *name);
        void * (*GetJniEnv) (void);
+       void * (*AllocObject) (void *klass);
 } MonoJniFunctions;
 
 static MonoJniFunctions jniFuncs;
@@ -95,7 +97,8 @@ mono_jni_jnienv_init (
        void *invokemethod_func,
        void *getmethodarglist_func,
        void *findclass_func,
-       void *getjnienv_func)
+       void *getjnienv_func,
+       void *allocobject_func)
 {
        jniFuncs.MakeLocalRef = makelocalref_func;
        jniFuncs.UnwrapRef = unwrap_func;
@@ -113,6 +116,7 @@ mono_jni_jnienv_init (
        jniFuncs.GetMethodArgList = getmethodarglist_func;
        jniFuncs.FindClass = findclass_func;
        jniFuncs.GetJniEnv = getjnienv_func;
+       jniFuncs.AllocObject = allocobject_func;
 }
 
 static void *jni_func_table[256];
@@ -145,8 +149,8 @@ StringFromUTF8 (const char* psz)
        /* TODO: */
        return mono_string_new (mono_domain_get (), psz);
 #if 0
-       // Sun's modified UTF8 encoding is not compatible with System::Text::Encoding::UTF8, so
-       // we need to roll our own
+       /* Sun's modified UTF8 encoding is not compatible with System::Text::Encoding::UTF8, so */
+       /* we need to roll our own */
        int len, res_len, i;
        int *res;
 
@@ -159,16 +163,16 @@ StringFromUTF8 (const char* psz)
                switch (c >> 4)
                {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
-                       // 0xxxxxxx
+                       /* 0xxxxxxx */
                        break;
                case 12: case 13:
-                       // 110x xxxx   10xx xxxx
+                       /* 110x xxxx   10xx xxxx */
                        char2 = *psz++;
                        i++;
                        c = (((c & 0x1F) << 6) | (char2 & 0x3F));
                        break;
                case 14:
-                       // 1110 xxxx  10xx xxxx  10xx xxxx
+                       /* 1110 xxxx  10xx xxxx  10xx xxxx */
                        char2 = *psz++;
                        char3 = *psz++;
                        i++;
@@ -241,7 +245,7 @@ static jboolean JNICALL IsSameObject (JNIEnv *env, jobject obj1, jobject obj2) {
 static jobject JNICALL NewLocalRef (JNIEnv *env, jobject ref) { printf ("JNI Function NewLocalRef is not implemented.\n"); g_assert_not_reached (); return 0; }
 static jint JNICALL EnsureLocalCapacity (JNIEnv *env, jint capacity) { printf ("JNI Function EnsureLocalCapacity is not implemented.\n"); g_assert_not_reached (); return 0; }
 
-static jobject JNICALL AllocObject (JNIEnv *env, jclass clazz) { printf ("JNI Function AllocObject is not implemented.\n"); g_assert_not_reached (); return 0; }
+static jobject JNICALL AllocObject (JNIEnv *env, jclass clazz) { printf ("JNI Function EnsureLocalCapacity is not implemented.\n"); g_assert_not_reached (); return 0; }
 
 static jclass JNICALL GetObjectClass (JNIEnv *env, jobject obj)
 {
@@ -259,50 +263,50 @@ static jobject JNICALL CallObjectMethodA (JNIEnv *env, jobject obj, jmethodID me
 static MonoObject *
 box_Boolean (jboolean val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.boolean_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_boolean_class (), &val);
 }
 
 static MonoObject *
 box_Byte (jbyte val)
 {
        /* Sbyte ! */
-       return mono_value_box (mono_domain_get (), mono_defaults.sbyte_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_sbyte_class (), &val);
 }
 
 static MonoObject *
 box_Char (jchar val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.char_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_char_class (), &val);
 }
 
 static MonoObject *
 box_Short (jshort val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.int16_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_int16_class (), &val);
 }
 
 static MonoObject *
 box_Int (jint val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.int32_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_int32_class (), &val);
 }
 
 static MonoObject *
 box_Long (jlong val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.int64_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_int64_class (), &val);
 }
 
 static MonoObject *
 box_Float (jfloat val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.single_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_single_class (), &val);
 }
 
 static MonoObject *
 box_Double (jdouble val)
 {
-       return mono_value_box (mono_domain_get (), mono_defaults.double_class, &val);
+       return mono_value_box (mono_domain_get (), mono_get_double_class (), &val);
 }
 
 static int 
@@ -323,7 +327,7 @@ InvokeHelper (JNIEnv *env, jobject object, jmethodID methodID, jvalue* args)
        MonoObject **argarray;
        MonoArray *args2;
 
-//     assert(!pLocalRefs->PendingException);
+/* assert(!pLocalRefs->PendingException); */
        g_assert(methodID);
 
        argc = GetMethodArgs(methodID, sig);
@@ -364,7 +368,7 @@ InvokeHelper (JNIEnv *env, jobject object, jmethodID methodID, jvalue* args)
                }
        }
 
-       args2 = mono_array_new (mono_domain_get (), mono_defaults.object_class, argc);
+       args2 = mono_array_new (mono_domain_get (), mono_get_object_class (), argc);
        for (i = 0; i < argc; ++i)
                mono_array_set (args2, MonoObject*, i, argarray [i]);
 
@@ -471,7 +475,7 @@ METHOD_IMPL(Double,jdouble)
 
 
 
-// TODO: These should be put into the macros above... 
+/* TODO: These should be put into the macros above...  */
 static void JNICALL CallVoidMethodA (JNIEnv *env, jobject obj, jmethodID methodID, jvalue * args) { 
        InvokeHelper(env, obj, methodID, args);
 }
@@ -577,7 +581,7 @@ static type JNICALL GetStatic##Type##Field(JNIEnv *env, jclass clazz, jfieldID f
 
 
 
-//    return *(cpptype*)BOXED_VALUE (jniFuncs.GetFieldValue (fieldID, jniFuncs.UnwrapRef (obj))); 
+/*    return *(cpptype*)BOXED_VALUE (jniFuncs.GetFieldValue (fieldID, jniFuncs.UnwrapRef (obj)));  */
 
 GET_SET_FIELD(Boolean,jboolean,gboolean)
 GET_SET_FIELD(Byte,jbyte, gchar)
@@ -730,42 +734,42 @@ static jobjectArray JNICALL NewObjectArray (JNIEnv *env, jsize len, jclass clazz
 
 static jbooleanArray JNICALL NewBooleanArray (JNIEnv *env, jsize len)
 {
-       return (jbooleanArray)new_java_array (env, mono_defaults.boolean_class, len);
+       return (jbooleanArray)new_java_array (env, mono_get_boolean_class (), len);
 }      
 
 static jbyteArray JNICALL NewByteArray (JNIEnv *env, jsize len)
 {
-       return (jbyteArray)new_java_array (env, mono_defaults.sbyte_class, len);
+       return (jbyteArray)new_java_array (env, mono_get_sbyte_class (), len);
 }
        
 static jcharArray JNICALL NewCharArray (JNIEnv *env, jsize len)
 {
-       return (jcharArray)new_java_array (env, mono_defaults.char_class, len);
+       return (jcharArray)new_java_array (env, mono_get_char_class (), len);
 }
        
 static jshortArray JNICALL NewShortArray (JNIEnv *env, jsize len)
 {
-       return (jshortArray)new_java_array (env, mono_defaults.int16_class, len);
+       return (jshortArray)new_java_array (env, mono_get_int16_class (), len);
 }      
 
 static jintArray JNICALL NewIntArray (JNIEnv *env, jsize len)
 {
-       return (jintArray)new_java_array (env, mono_defaults.int32_class, len);
+       return (jintArray)new_java_array (env, mono_get_int32_class (), len);
 }
 
 static jlongArray JNICALL NewLongArray (JNIEnv *env, jsize len)
 {
-       return (jlongArray)new_java_array (env, mono_defaults.int64_class, len);
+       return (jlongArray)new_java_array (env, mono_get_int64_class (), len);
 }
 
 static jfloatArray JNICALL NewFloatArray (JNIEnv *env, jsize len)
 {
-       return (jfloatArray)new_java_array (env, mono_defaults.single_class, len);
+       return (jfloatArray)new_java_array (env, mono_get_single_class (), len);
 }
 
 static jdoubleArray JNICALL NewDoubleArray (JNIEnv *env, jsize len)
 {
-       return (jdoubleArray)new_java_array (env, mono_defaults.double_class, len);
+       return (jdoubleArray)new_java_array (env, mono_get_double_class (), len);
 }
 
 /* Original version with copy */
@@ -832,6 +836,18 @@ static type* JNICALL Get##Type##ArrayElements(JNIEnv *env, type##Array array, jb
 static void JNICALL Release##Type##ArrayElements(JNIEnv *env, type##Array array, type *elems, jint mode)\
 {\
     return; \
+} \
+static void JNICALL Get##Type##ArrayRegion (JNIEnv *env, type##Array array, jsize start, jsize l, type *buf) \
+{\
+    MonoArray *obj; \
+       obj = jniFuncs.UnwrapRef (env, (void*)array); \
+    memcpy (buf, mono_array_addr (obj, sizeof (type), start), (sizeof (type) * l)); \
+} \
+static void JNICALL Set##Type##ArrayRegion (JNIEnv *env, type##Array array, jsize start, jsize l, type *buf) \
+{ \
+    MonoArray *obj; \
+       obj = jniFuncs.UnwrapRef (env, (void*)array); \
+    memcpy (mono_array_addr (obj, sizeof (type), start), buf, (sizeof (type) * l)); \
 }
 
 GET_SET_ARRAY_ELEMENTS(Boolean,jboolean,gboolean)
@@ -843,23 +859,32 @@ GET_SET_ARRAY_ELEMENTS(Long,jlong,glong)
 GET_SET_ARRAY_ELEMENTS(Float,jfloat,float)
 GET_SET_ARRAY_ELEMENTS(Double,jdouble,double)
 
-static void JNICALL GetBooleanArrayRegion (JNIEnv *env, jbooleanArray array, jsize start, jsize l, jboolean *buf) { printf ("JNI Function GetBooleanArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetByteArrayRegion (JNIEnv *env, jbyteArray array, jsize start, jsize len, jbyte *buf) { printf ("JNI Function GetByteArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetCharArrayRegion (JNIEnv *env, jcharArray array, jsize start, jsize len, jchar *buf) { printf ("JNI Function GetCharArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetShortArrayRegion (JNIEnv *env, jshortArray array, jsize start, jsize len, jshort *buf) { printf ("JNI Function GetShortArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetIntArrayRegion (JNIEnv *env, jintArray array, jsize start, jsize len, jint *buf) { printf ("JNI Function GetIntArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetLongArrayRegion (JNIEnv *env, jlongArray array, jsize start, jsize len, jlong *buf) { printf ("JNI Function GetLongArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetFloatArrayRegion (JNIEnv *env, jfloatArray array, jsize start, jsize len, jfloat *buf) { printf ("JNI Function GetFloatArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL GetDoubleArrayRegion (JNIEnv *env, jdoubleArray array, jsize start, jsize len, jdouble *buf) { printf ("JNI Function GetDoubleArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-
-static void JNICALL SetBooleanArrayRegion (JNIEnv *env, jbooleanArray array, jsize start, jsize l, jboolean *buf) { printf ("JNI Function SetBooleanArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetByteArrayRegion (JNIEnv *env, jbyteArray array, jsize start, jsize len, jbyte *buf) { printf ("JNI Function SetByteArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetCharArrayRegion (JNIEnv *env, jcharArray array, jsize start, jsize len, jchar *buf) { printf ("JNI Function SetCharArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetShortArrayRegion (JNIEnv *env, jshortArray array, jsize start, jsize len, jshort *buf) { printf ("JNI Function SetShortArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetIntArrayRegion (JNIEnv *env, jintArray array, jsize start, jsize len, jint *buf) { printf ("JNI Function SetIntArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetLongArrayRegion (JNIEnv *env, jlongArray array, jsize start, jsize len, jlong *buf) { printf ("JNI Function SetLongArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetFloatArrayRegion (JNIEnv *env, jfloatArray array, jsize start, jsize len, jfloat *buf) { printf ("JNI Function SetFloatArrayRegion is not implemented.\n"); g_assert_not_reached (); }
-static void JNICALL SetDoubleArrayRegion (JNIEnv *env, jdoubleArray array, jsize start, jsize len, jdouble *buf) { printf ("JNI Function SetDoubleArrayRegion is not implemented.\n"); g_assert_not_reached (); }
+static void * JNICALL GetPrimitiveArrayCritical (JNIEnv *env, jarray array, jboolean *isCopy) {
+       MonoArray *obj;
+
+       obj = jniFuncs.UnwrapRef (env, (void*)array);
+    if (isCopy)
+      *isCopy = JNI_FALSE;
+    return mono_array_addr (obj, void*, 0);
+}
+
+static void JNICALL ReleasePrimitiveArrayCritical (JNIEnv *env, jarray array, void *carray, jint mode) {
+}
+
+static const jchar * JNICALL GetStringCritical (JNIEnv *env, jstring string, jboolean *isCopy) {
+       MonoString *obj;
+
+       obj = jniFuncs.UnwrapRef (env, (void*)string);
+
+       if (isCopy)
+               *isCopy = JNI_FALSE;
+
+       return mono_string_chars (obj);
+}
+
+static void JNICALL ReleaseStringCritical (JNIEnv *env, jstring string, const jchar *cstring)
+{
+}
 
 static jobject JNICALL NewObjectA (JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args)
 {
@@ -933,12 +958,6 @@ jint JNICALL GetJavaVM (JNIEnv *env, JavaVM **vm)
 static void JNICALL GetStringRegion (JNIEnv *env, jstring str, jsize start, jsize len, jchar *buf) { printf ("JNI Function GetStringRegion is not implemented.\n"); g_assert_not_reached (); }
 static void JNICALL GetStringUTFRegion (JNIEnv *env, jstring str, jsize start, jsize len, char *buf) { printf ("JNI Function GetStringUTFRegion is not implemented.\n"); g_assert_not_reached (); }
 
-static void * JNICALL GetPrimitiveArrayCritical (JNIEnv *env, jarray array, jboolean *isCopy) { printf ("JNI Function GetPrimitiveArrayCritical is not implemented.\n"); g_assert_not_reached (); return NULL; }
-static void JNICALL ReleasePrimitiveArrayCritical (JNIEnv *env, jarray array, void *carray, jint mode) { printf ("JNI Function ReleasePrimitiveArrayCritical is not implemented.\n"); g_assert_not_reached (); }
-
-static const jchar * JNICALL GetStringCritical (JNIEnv *env, jstring string, jboolean *isCopy) { printf ("JNI Function GetStringCritical is not implemented.\n"); g_assert_not_reached (); return NULL; }
-static void JNICALL ReleaseStringCritical (JNIEnv *env, jstring string, const jchar *cstring) { printf ("JNI Function ReleaseStringCritical is not implemented.\n"); g_assert_not_reached (); }
-
 static jweak JNICALL NewWeakGlobalRef (JNIEnv *env, jobject obj) { printf ("JNI Function NewWeakGlobalRef is not implemented.\n"); g_assert_not_reached (); return 0; }
 static void JNICALL DeleteWeakGlobalRef (JNIEnv *env, jweak ref) { printf ("JNI Function DeleteWeakGlobalRef is not implemented.\n"); g_assert_not_reached (); }