add NewGlobalRef to cacao jni
authormotse <none@none>
Sat, 30 Oct 2004 11:15:23 +0000 (11:15 +0000)
committermotse <none@none>
Sat, 30 Oct 2004 11:15:23 +0000 (11:15 +0000)
jni.c
jni.h
main.c
src/cacao/cacao.c
src/native/jni.c
src/native/jni.h

diff --git a/jni.c b/jni.c
index 28955173b3635c547dc0390bb4d0f81aa94827ad..cae0687476105560d3554deb2bf44f42068e820b 100644 (file)
--- a/jni.c
+++ b/jni.c
@@ -28,7 +28,7 @@
 
    Changes: Joseph Wenninger, Martin Platter
 
-   $Id: jni.c 1411 2004-08-17 15:04:54Z twisti $
+   $Id: jni.c 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -57,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
 
 
@@ -72,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 **********************************/
 
@@ -521,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); 
@@ -542,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;
 }
 
@@ -608,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*/);
 
@@ -665,12 +674,13 @@ 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*/);
 
@@ -705,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*/);
 
@@ -965,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 */
 }
 
@@ -1046,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 */
        
@@ -1764,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;
@@ -1793,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)
@@ -3043,6 +3069,52 @@ 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)
+{      
+       MonitorEnter(env,*global_ref_table);
+       jobject refcount = CallObjectMethod(env, *global_ref_table, getmid, lobj);
+       jint val = (refcount == NULL) ? 0 : CallIntMethod(env,refcount,intvalue);
+       jobject 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)
+{
+       MonitorEnter(env,*global_ref_table);
+       jobject refcount = CallObjectMethod(env, *global_ref_table, getmid, gref);
+       if (refcount == NULL) {
+               log_text("JNI-DeleteGlobalRef: unable to find global reference");
+               return;
+       }
+       jint 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 ***********************/
 
 
@@ -3372,12 +3444,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);
@@ -3575,6 +3648,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.
diff --git a/jni.h b/jni.h
index 8981e4de0f97177485a865a86a00b02329a14beb..cd804c2aa16548a8fd22f5733691788551ea0a11 100644 (file)
--- a/jni.h
+++ b/jni.h
@@ -26,7 +26,7 @@
 
    Authors: ?
 
-   $Id: jni.h 747 2003-12-13 22:10:17Z twisti $
+   $Id: jni.h 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -502,6 +502,8 @@ void SetDoubleField(JNIEnv *env, jobject obj, jfieldID fieldID, jdouble val);
 
 jobject *jni_method_invokeNativeHelper(JNIEnv *env,struct methodinfo *mi,jobject obj, java_objectarray *params);
 
+void jni_init ();
+
 #endif /* _JNI_H */
 
 
diff --git a/main.c b/main.c
index d7532a9a4b82bf87366a142876412c7c3ede1bbc..74dd1ff0b9cbdfc85c7b6889a4ad5af595f71143 100644 (file)
--- a/main.c
+++ b/main.c
@@ -37,7 +37,7 @@
      - Calling the class loader
      - Running the main method
 
-   $Id: main.c 1412 2004-08-17 19:33:16Z twisti $
+   $Id: main.c 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -657,6 +657,9 @@ int main(int argc, char **argv)
        if (!class_init(class_new(utf_new_char("java/lang/System"))))
                throw_main_exception_exit();
 
+       
+       
+        jni_init();
        cacao_initializing = false;
 
        /************************* Start worker routines ********************/
index 7a15bae13e77325fff52c7e7448bb3d956c57e24..37347a5348f9f54a39801feaed32df789601985f 100644 (file)
@@ -37,7 +37,7 @@
      - Calling the class loader
      - Running the main method
 
-   $Id: cacao.c 1412 2004-08-17 19:33:16Z twisti $
+   $Id: cacao.c 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -657,6 +657,9 @@ int main(int argc, char **argv)
        if (!class_init(class_new(utf_new_char("java/lang/System"))))
                throw_main_exception_exit();
 
+       
+       
+        jni_init();
        cacao_initializing = false;
 
        /************************* Start worker routines ********************/
index 28955173b3635c547dc0390bb4d0f81aa94827ad..cae0687476105560d3554deb2bf44f42068e820b 100644 (file)
@@ -28,7 +28,7 @@
 
    Changes: Joseph Wenninger, Martin Platter
 
-   $Id: jni.c 1411 2004-08-17 15:04:54Z twisti $
+   $Id: jni.c 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -57,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
 
 
@@ -72,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 **********************************/
 
@@ -521,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); 
@@ -542,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;
 }
 
@@ -608,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*/);
 
@@ -665,12 +674,13 @@ 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*/);
 
@@ -705,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*/);
 
@@ -965,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 */
 }
 
@@ -1046,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 */
        
@@ -1764,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;
@@ -1793,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)
@@ -3043,6 +3069,52 @@ 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)
+{      
+       MonitorEnter(env,*global_ref_table);
+       jobject refcount = CallObjectMethod(env, *global_ref_table, getmid, lobj);
+       jint val = (refcount == NULL) ? 0 : CallIntMethod(env,refcount,intvalue);
+       jobject 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)
+{
+       MonitorEnter(env,*global_ref_table);
+       jobject refcount = CallObjectMethod(env, *global_ref_table, getmid, gref);
+       if (refcount == NULL) {
+               log_text("JNI-DeleteGlobalRef: unable to find global reference");
+               return;
+       }
+       jint 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 ***********************/
 
 
@@ -3372,12 +3444,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);
@@ -3575,6 +3648,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.
index 8981e4de0f97177485a865a86a00b02329a14beb..cd804c2aa16548a8fd22f5733691788551ea0a11 100644 (file)
@@ -26,7 +26,7 @@
 
    Authors: ?
 
-   $Id: jni.h 747 2003-12-13 22:10:17Z twisti $
+   $Id: jni.h 1424 2004-10-30 11:15:23Z motse $
 
 */
 
@@ -502,6 +502,8 @@ void SetDoubleField(JNIEnv *env, jobject obj, jfieldID fieldID, jdouble val);
 
 jobject *jni_method_invokeNativeHelper(JNIEnv *env,struct methodinfo *mi,jobject obj, java_objectarray *params);
 
+void jni_init ();
+
 #endif /* _JNI_H */