nativeLoad remove #ifdef JOWENNDEBUG
[cacao.git] / jni.c
diff --git a/jni.c b/jni.c
index da6435e187e303df980c58ebc0800c52a22693bb..632125e90d57f9960c4d49b438fbf9d3d073b35b 100644 (file)
--- a/jni.c
+++ b/jni.c
@@ -3,7 +3,7 @@
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
-   P. Tomsich, J. Wenninger
+   P. Tomsich, J. Wenninger, M. Platter
 
    This file is part of CACAO.
 
 
    Authors: ?
 
-   Changes: Joseph Wenninger
+   Changes: Joseph Wenninger, Martin Platter
 
-   $Id: jni.c 1067 2004-05-18 10:25:51Z stefan $
+   $Id: jni.c 1426 2004-11-01 12:21:59Z twisti $
 
 */
 
 
 #include <string.h>
+#include "exceptions.h"
 #include "main.h"
 #include "jni.h"
 #include "global.h"
@@ -41,6 +42,8 @@
 #include "tables.h"
 #include "native.h"
 #include "builtin.h"
+#include "options.h"
+#include "statistics.h"
 #include "threads/thread.h"
 #include "toolbox/logging.h"
 #include "toolbox/memory.h"
@@ -54,7 +57,8 @@
 #include "nat/java_lang_Double.h"
 #include "nat/java_lang_Throwable.h"
 #include "jit/jit.h"
-#include "asmpart.h"   
+#include "asmpart.h"
+#include "mm/boehm.h"
 #define JNI_VERSION       0x00010002
 
 
@@ -69,6 +73,18 @@ static utf* utf_long = 0;
 static utf* utf_float = 0;
 static utf* utf_double = 0;
 
+/* global reference table */
+static jobject *global_ref_table;
+
+/* jmethodID and jclass caching variables for NewGlobalRef and DeleteGlobalRef*/
+static jmethodID getmid = NULL;
+static jmethodID putmid = NULL;
+static jclass intclass = NULL;
+static jmethodID intvalue = NULL;
+static jmethodID newint = NULL;
+static jclass ihmclass = NULL;
+static jmethodID removemid = NULL;
+
 
 /********************* accessing instance-fields **********************************/
 
@@ -506,6 +522,7 @@ jmethodID get_virtual(jobject obj,jmethodID methodID) {
 
 jmethodID get_nonvirtual(jclass clazz,jmethodID methodID) {
        if (clazz==methodID->class) return methodID;
+/*class_resolvemethod -> classfindmethod? (JOWENN)*/
        return class_resolvemethod (clazz, methodID->name, methodID->descriptor);
 }
 
@@ -517,14 +534,7 @@ jobject callObjectMethod (jobject obj, jmethodID methodID, va_list args)
        jni_callblock *blk;
        jobject ret;
 
-       /*
-         log_text("JNI-Call: CallObjectMethodV");
-         utf_display(methodID->name);
-         utf_display(methodID->descriptor);
-         printf("\nParmaeter count: %d\n",argcount);
-         utf_display(obj->vftbl->class->name);
-         printf("\n");
-       */
+
 
        if (methodID == 0) {
                *exceptionptr = new_exception(string_java_lang_NoSuchMethodError); 
@@ -538,30 +548,32 @@ jobject callObjectMethod (jobject obj, jmethodID methodID, va_list args)
                *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
                return 0;
        }
-       
+
        if (obj && !builtin_instanceof(obj, methodID->class)) {
                *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
                return 0;
        }
 
+#ifdef arglimit
+
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. CallObjectMethod does not support that");
                return 0;
        }
+#endif
 
        blk = MNEW(jni_callblock, 4 /*argcount+2*/);
 
        fill_callblock(obj, methodID->descriptor, blk, args, 'O');
-
        /*      printf("parameter: obj: %p",blk[0].item); */
        ret = asm_calljavafunction2(methodID,
                                                                argcount + 1,
                                                                (argcount + 1) * sizeof(jni_callblock),
                                                                blk);
-
        MFREE(blk, jni_callblock, argcount + 1);
        /*      printf("(CallObjectMethodV)-->%p\n",ret); */
+
        return ret;
 }
 
@@ -604,12 +616,13 @@ jint callIntegerMethod(jobject obj, jmethodID methodID, char retType, va_list ar
                return 0;
        }
 
-
+#ifdef arglimit
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. CallIntegerMethod does not support that");
                return 0;
        }
+#endif
 
        blk = MNEW(jni_callblock, 4 /*argcount+2*/);
 
@@ -635,14 +648,14 @@ jlong callLongMethod(jobject obj, jmethodID methodID, va_list args)
        jni_callblock *blk;
        jlong ret;
 
-       /*
+/*     
         log_text("JNI-Call: CallObjectMethodV");
         utf_display(methodID->name);
         utf_display(methodID->descriptor);
         printf("\nParmaeter count: %d\n",argcount);
         utf_display(obj->vftbl->class->name);
         printf("\n");
-        */
+*/      
        if (methodID == 0) {
                *exceptionptr = new_exception(string_java_lang_NoSuchMethodError); 
                return 0;
@@ -661,16 +674,17 @@ jlong callLongMethod(jobject obj, jmethodID methodID, va_list args)
                return 0;
        }
 
-
+#ifdef arglimit
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. CallObjectMethod does not support that");
                return 0;
        }
+#endif
 
        blk = MNEW(jni_callblock, 4 /*argcount+2*/);
 
-       fill_callblock(obj, methodID->descriptor, blk, args, 'L');
+       fill_callblock(obj, methodID->descriptor, blk, args, 'J');
 
        /*      printf("parameter: obj: %p",blk[0].item); */
        ret = asm_calljavafunction2long(methodID,
@@ -701,11 +715,13 @@ jdouble callFloatMethod(jobject obj, jmethodID methodID, va_list args,char retTy
         printf("\n");
         */
 
+#ifdef arglimit
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. CallObjectMethod does not support that");
                return 0;
        }
+#endif
 
        blk = MNEW(jni_callblock, 4 /*argcount+2*/);
 
@@ -758,74 +774,64 @@ jint GetVersion (JNIEnv* env)
 }
 
 
-/****************** loads a class from a buffer of raw class data *****************/
+/************** loads a class from a buffer of raw class data *****************/
 
 jclass DefineClass(JNIEnv* env, const char *name, jobject loader, const jbyte *buf, jsize len) 
 {
-       jclass clazz; 
+       jclass c; 
+       jclass r;
        classbuffer *cb;
-       s8 starttime;
-       s8 stoptime;
 
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
-       compiler_lock();
-       tables_lock();
-#else
-       intsDisable();
-#endif
-#endif
+       c = class_new(utf_new_char_classname((char *) name));
+
+       /* enter a monitor on the class */
+
+       builtin_monitorenter((java_objectheader *) c);
 
        /* measure time */
        if (getloadingtime)
-               starttime = getcputime();
-
-       clazz = class_new(utf_new_char((char *) name));
+               loadingtime_start();
 
        /* build a classbuffer with the given data */
        cb = NEW(classbuffer);
-       cb->class = clazz;
+       cb->class = c;
        cb->size = len;
        cb->data = (u1 *) buf;
        cb->pos = cb->data - 1;
 
-       class_load_intern(cb);
+       r = class_load_intern(cb);
+
+       /* if return value is NULL, we had a problem and the class is not loaded */
+       if (!r) {
+               c->loaded = false;
+
+               /* now free the allocated memory, otherwise we could ran into a DOS */
+               class_remove(c);
+       }
 
        /* free memory */
        FREE(cb, classbuffer);
 
        /* measure time */
-       if (getloadingtime) {
-               stoptime = getcputime();
-               loadingtime += (stoptime - starttime);
-       }
+       if (getloadingtime)
+               loadingtime_stop();
 
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
-       tables_unlock();
-       compiler_unlock();
-#else
-       intsRestore();
-#endif
-#endif
+       /* leave the monitor */
 
-       if (*exceptionptr)
-               return NULL;
+       builtin_monitorexit((java_objectheader *) c);
 
        /* XXX link the class here? */
-       class_link(clazz);
-
-       if (*exceptionptr)
-               return NULL;
+/*     if (class_link(c)) */
+/*             return NULL; */
 
-       if (clazz)
-               clazz->classloader = loader;
+       if (r)
+               c->classloader = loader;
 
-       return clazz;
+       return r;
 }
 
 
-/*************** loads locally defined class with the specified name **************/
+/*********** loads locally defined class with the specified name **************/
 
 jclass FindClass(JNIEnv* env, const char *name) 
 {
@@ -833,26 +839,22 @@ jclass FindClass(JNIEnv* env, const char *name)
   
        c = class_new(utf_new_char_classname((char *) name));
 
-       class_load(c);
-
-       if (*exceptionptr)
+       if (!class_load(c))
                return NULL;
 
-       class_link(c);
-
-       if (*exceptionptr)
+       if (!class_link(c))
                return NULL;
 
        return c;
 }
   
 
-/*********************************************************************************** 
+/*******************************************************************************
 
        converts java.lang.reflect.Method or 
        java.lang.reflect.Constructor object to a method ID  
   
- **********************************************************************************/   
+*******************************************************************************/
   
 jmethodID FromReflectedMethod(JNIEnv* env, jobject method)
 {
@@ -975,31 +977,18 @@ jint PushLocalFrame(JNIEnv* env, jint capacity)
 
 jobject PopLocalFrame(JNIEnv* env, jobject result)
 {
+    log_text("JNI-Call: PopLocalFrame");
        /* empty */
 
        return NULL;
 }
-    
-
-/** Creates a new global reference to the object referred to by the obj argument **/
-    
-jobject NewGlobalRef(JNIEnv* env, jobject lobj)
-{
-       return lobj;
-}
-
-/*************  Deletes the global reference pointed to by globalRef **************/
-
-void DeleteGlobalRef (JNIEnv* env, jobject gref)
-{
-       /* empty */
-}
 
 
 /*************** Deletes the local reference pointed to by localRef ***************/
 
 void DeleteLocalRef (JNIEnv* env, jobject localRef)
 {
+/*    log_text("JNI-Call: DeleteLocalRef");*/
        /* empty */
 }
 
@@ -1056,12 +1045,13 @@ jobject NewObject (JNIEnv* env, jclass clazz, jmethodID methodID, ...)
 
        /* log_text("JNI-Call: NewObject"); */
 
+#ifdef arglimit
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. NewObject does not support that");
                return 0;
        }
-
+#endif
        
        o = builtin_new (clazz);         /*          create object */
        
@@ -1167,7 +1157,10 @@ jmethodID GetMethodID(JNIEnv* env, jclass clazz, const char *name, const char *s
        );
 
        if (!m) *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
-
+       else if (m->flags & ACC_STATIC)   {
+               m=0;
+               *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
+       }
        return m;
 }
 
@@ -1351,17 +1344,20 @@ jint CallIntMethodA(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args)
 
 jlong CallLongMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)
 {
-       log_text("JNI-Call: CallLongMethod");
+       jlong ret;
+       va_list vaargs;
+       
+       va_start(vaargs,methodID);
+       ret = callLongMethod(obj,get_virtual(obj, methodID),vaargs);
+       va_end(vaargs);
 
-       return 0;
+       return ret;
 }
 
 
 jlong CallLongMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)
 {
-       log_text("JNI-Call: CallLongMethodV");
-
-       return 0;
+       return  callLongMethod(obj,get_virtual(obj, methodID),args);
 }
 
 
@@ -1768,9 +1764,9 @@ jfieldID GetFieldID (JNIEnv *env, jclass clazz, const char *name, const char *si
                            ); 
        
        if (!f) { 
-/*             utf_display(clazz->name);
+               utf_display(clazz->name);
                log_text(name);
-               log_text(sig);*/
+               log_text(sig);
                *exceptionptr = new_exception(string_java_lang_NoSuchFieldError);  
        }
        return f;
@@ -1797,7 +1793,33 @@ jfieldID getFieldID_critical(JNIEnv *env, jclass clazz, char *name, char *sig)
 
 jobject GetObjectField (JNIEnv *env, jobject obj, jfieldID fieldID)
 {
-       return getField(obj,jobject,fieldID);
+       jobject dbg,dretval,*dpretval;  
+       long int dli1, dli2, dli3;
+
+/*     printf("GetObjectField(1): thread: %s obj: %p name: %s desc: %s \n",GetStringUTFChars(env,
+                        ((threadobject *) THREADOBJECT)->o
+                        .thread->name,NULL)
+                        ,obj,((fieldinfo*)fieldID)->name->text,(fieldID->descriptor)->text);*/
+
+       dbg = getField(obj,jobject,fieldID);
+       dli1 = (long int) obj;
+       dli2 = (long int) fieldID->offset;
+       dli3 = dli1+dli2;
+       dpretval = (jobject*) dli3;
+       dretval = *dpretval;
+/*     jclass tmp;
+       jmethodID mid;
+       jstring jstr;
+
+       tmp = FindClass(env, "java/lang/Object");
+       mid = GetMethodID(env,tmp,"toString","()Ljava/lang/String;");
+       jstr = CallObjectMethod(env,dbg,mid);*/
+
+/*     printf("GetObjectField(2): retval %p (obj: %#lx + offset: %#lx = %#lx (jobject*) %p (jobject) %p\n"
+       ,dbg, dli1, dli2, dli3,dpretval, dretval);*/
+
+
+       return dbg;
 }
 
 jboolean GetBooleanField (JNIEnv *env, jobject obj, jfieldID fieldID)
@@ -1912,6 +1934,10 @@ jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const c
                                                        utf_new_char((char *) sig));
 
        if (!m) *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
+       else if (!(m->flags & ACC_STATIC))   {
+               m=0;
+               *exceptionptr = new_exception(string_java_lang_NoSuchMethodError);
+       }
 
        return m;
 }
@@ -1919,17 +1945,24 @@ jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const c
 
 jobject CallStaticObjectMethod(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
 {
-       log_text("JNI-Call: CallStaticObjectMethod");
+       jobject ret;
+       va_list vaargs;
 
-       return NULL;
+       /* log_text("JNI-Call: CallStaticObjectMethod");*/
+
+       va_start(vaargs, methodID);
+       ret = callObjectMethod(0, methodID, vaargs);
+       va_end(vaargs);
+
+       return ret;
 }
 
 
 jobject CallStaticObjectMethodV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args)
 {
-       log_text("JNI-Call: CallStaticObjectMethodV");
-
-       return NULL;
+       /* log_text("JNI-Call: CallStaticObjectMethodV"); */
+       
+       return callObjectMethod(0,methodID,args);
 }
 
 
@@ -3036,6 +3069,67 @@ void DeleteWeakGlobalRef (JNIEnv* env, jweak ref)
 }
 
 
+/** Creates a new global reference to the object referred to by the obj argument **/
+    
+jobject NewGlobalRef(JNIEnv* env, jobject lobj)
+{
+       jobject refcount;
+       jint val;
+       jobject newval;
+
+       MonitorEnter(env, *global_ref_table);
+       
+       refcount = CallObjectMethod(env, *global_ref_table, getmid, lobj);
+       val = (refcount == NULL) ? 0 : CallIntMethod(env, refcount, intvalue);
+       newval = NewObject(env, intclass, newint, val + 1);
+
+       if (newval != NULL) {
+               CallObjectMethod(env, *global_ref_table, putmid, lobj, newval);
+               MonitorExit(env, *global_ref_table);
+               return lobj;
+
+       } else {
+               log_text("JNI-NewGlobalRef: unable to create new java.lang.Integer");
+               MonitorExit(env, *global_ref_table);
+               return NULL;
+       }
+}
+
+/*************  Deletes the global reference pointed to by globalRef **************/
+
+void DeleteGlobalRef(JNIEnv* env, jobject gref)
+{
+       jobject refcount;
+       jint val;
+
+       MonitorEnter(env, *global_ref_table);
+       refcount = CallObjectMethod(env, *global_ref_table, getmid, gref);
+
+       if (refcount == NULL) {
+               log_text("JNI-DeleteGlobalRef: unable to find global reference");
+               return;
+       }
+
+       val = CallIntMethod(env, refcount, intvalue);
+       val--;
+
+       if (val == 0) {
+               CallObjectMethod(env, *global_ref_table, removemid,refcount);
+
+       } else {
+               jobject newval = NewObject(env, intclass, newint, val);
+
+               if (newval != NULL) {
+                       CallObjectMethod(env,*global_ref_table, putmid,newval);
+
+               } else {
+                       log_text("JNI-DeleteGlobalRef: unable to create new java.lang.Integer");
+               }
+       }
+
+       MonitorExit(env,*global_ref_table);
+}
+
 /******************************* check for pending exception ***********************/
 
 
@@ -3365,12 +3459,13 @@ jobject *jni_method_invokeNativeHelper(JNIEnv *env, struct methodinfo *methodID,
        }
 
 
-
+#ifdef arglimit
        if (argcount > 3) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
                log_text("Too many arguments. invokeNativeHelper does not support that");
                return 0;
        }
+#endif
 
        if (((!params) && (argcount != 0)) || (params && (params->header.size != argcount))) {
                *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
@@ -3568,6 +3663,66 @@ jobject *jni_method_invokeNativeHelper(JNIEnv *env, struct methodinfo *methodID,
        return (jobject *) retVal;
 }
 
+void jni_init() {
+       jmethodID mid;
+
+       log_text("JNI-Init: initialize global_ref_table");
+       // initalize global reference table
+       ihmclass = FindClass(NULL, "java/util/IdentityHashMap");
+       
+       if (ihmclass == NULL) {
+               log_text("JNI-Init: unable to find java.util.IdentityHashMap");
+       }
+
+       mid = GetMethodID(NULL, ihmclass, "<init>","()V");
+       if (mid == NULL) {
+               log_text("JNI-Init: unable to find constructor in java.util.IdentityHashMap");
+       }
+       
+       global_ref_table = (jobject*)heap_allocate(sizeof(jobject),true,NULL);
+
+       *global_ref_table = NewObject(NULL,ihmclass,mid);
+
+       if (*global_ref_table == NULL) {
+               log_text("JNI-Init: unable to create new global_ref_table");
+       }
+       
+       getmid = GetMethodID(NULL, ihmclass, "get","(Ljava/lang/Object;)Ljava/lang/Object;");
+       if (mid == NULL) {
+               log_text("JNI-Init: unable to find method \"get\" in java.util.IdentityHashMap");
+       }
+
+       getmid = GetMethodID(NULL ,ihmclass, "get","(Ljava/lang/Object;)Ljava/lang/Object;");
+       if (getmid == NULL) {
+               log_text("JNI-Init: unable to find method \"get\" in java.util.IdentityHashMap");
+       }
+
+       putmid = GetMethodID(NULL, ihmclass, "put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+       if (putmid == NULL) {
+               log_text("JNI-Init: unable to find method \"put\" in java.util.IdentityHashMap");
+       }
+
+       intclass = FindClass(NULL, "java/lang/Integer");
+       if (intclass == NULL) {
+               log_text("JNI-Init: unable to find java.lang.Integer");
+       }
+
+       newint = GetMethodID(NULL, intclass, "<init>","(I)V");
+       if (newint == NULL) {
+               log_text("JNI-Init: unable to find constructor in java.lang.Integer");
+       }
+
+       intvalue = GetMethodID(NULL, intclass, "intValue","()I");
+       if (intvalue == NULL) {
+               log_text("JNI-Init: unable to find method \"intValue\" in java.lang.Integer");
+       }
+
+       removemid = GetMethodID(NULL, ihmclass, "remove","(Ljava/lang/Object;)Ljava/lang/Object;");
+       if (removemid == NULL) {
+               log_text("JNI-DeleteGlobalRef: unable to find method \"remove\" in java.lang.Object");
+       }
+}
+
 
 /*
  * These are local overrides for various environment variables in Emacs.