nat/GtkLabelPeer.c has been removed
[cacao.git] / native.c
index 01a6160ea945d39bcbc25418fd179bea296170f7..bf903c0e8b37f6f4fd9e9bc2282f5527c57bf01b 100644 (file)
--- a/native.c
+++ b/native.c
@@ -31,7 +31,7 @@
    The .hh files created with the header file generator are all
    included here as are the C functions implementing these methods.
 
-   $Id: native.c 1112 2004-05-31 15:47:20Z jowenn $
+   $Id: native.c 1322 2004-07-17 12:29:18Z motse $
 
 */
 
 
 #include "config.h"
 #include "global.h"
-#include "main.h"
+#include "options.h"
 #include "jni.h"
 #include "native.h"
-#include "nativetypes.hh"
 #include "builtin.h"
 #include "asmpart.h"
 #include "tables.h"
 
 #include "threads/threadio.h"
 
+/* include table of native functions */
+
+#include "nativetable.inc"
+
+
 /* searchpath for classfiles */
 char *classpath;
 
@@ -105,9 +109,11 @@ classinfo *class_java_lang_Integer;
 methodinfo *method_vmclass_init;
 
 
-/* system exception classes required while compiling */
+/* system exception classes required in cacao */
 
 classinfo *class_java_lang_Throwable;
+classinfo *class_java_lang_Exception;
+classinfo *class_java_lang_Error;
 
 
 /* exception/error super class */
@@ -221,46 +227,10 @@ java_objectheader* _exceptionptr = NULL;
 
 void use_class_as_object(classinfo *c) 
 {
-/*     vftbl *vt; */
-/*     vftbl *newtbl; */
-       java_objectheader *vmo;
-
-/*     vt = class_java_lang_Class->vftbl; */
-
        if (!c->classvftbl) {
-               c->classvftbl = true;
-
-               /*                copy_vftbl(&newtbl, vt);
-                                                 newtbl->class = c->header.vftbl->class;
-                                                 newtbl->baseval = c->header.vftbl->baseval;
-                                                 newtbl->diffval = c->header.vftbl->diffval;
-                                                 c->header.vftbl = newtbl;*/
-               
                c->header.vftbl = class_java_lang_Class->vftbl;
-        
-               method_vmclass_init =
-                       class_findmethod(class_java_lang_VMClass,
-                                                        utf_new_char("<init>"),
-                                                        utf_new_char("(Lgnu/classpath/RawData;)V"));
-
-               if (method_vmclass_init == 0) {
-                       class_showmethods(class_java_lang_VMClass);
-                       panic("Needed class initializer for VMClass could not be found");
-               }
-
-               vmo = builtin_new(class_java_lang_VMClass);
-
-               if (!vmo)
-                       panic("Error while creating instance of java/lang/VMClass");
-
-               asm_calljavafunction(method_vmclass_init, vmo, c, NULL, NULL);
-
-               c->vmClass = (java_lang_VMClass *) vmo;
-#if 0
-               setfield_critical(class_java_lang_Class,vmo,"vmClass",          "Ljava/lang/VMClass;",  jobject, (jobject) class_java_lang_Class /*this*/);
-#endif
-
-       }
+               c->classvftbl = true;
+       }
             
 }
 
@@ -273,7 +243,6 @@ void use_class_as_object(classinfo *c)
 #include "nat/GdkPixbufDecoder.c"
 #include "nat/GtkScrollPanePeer.c"
 #include "nat/GtkFileDialogPeer.c"
-#include "nat/GtkLabelPeer.c"
 #endif
 
 
@@ -282,30 +251,10 @@ void use_class_as_object(classinfo *c)
 #undef JOWENN_DEBUG
 #undef JOWENN_DEBUG1
 
-/* table for locating native methods */
-static struct nativeref {
-       char *classname;
-       char *methodname;
-       char *descriptor;
-       bool isstatic;
-       functionptr func;
-} nativetable [] = {
-
-#include "nativetable.hh"
-
-};
-
-
 #define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
 
 /* table for fast string comparison */
-static struct nativecompref {
-       utf *classname;
-       utf *methodname;
-       utf *descriptor;
-       bool isstatic;
-       functionptr func;
-} nativecomptable [NATIVETABLESIZE];
+static nativecompref nativecomptable[NATIVETABLESIZE];
 
 /* string comparsion table initialized */
 static bool nativecompdone = false;
@@ -328,99 +277,26 @@ static bool nativecompdone = false;
 
 void init_system_exceptions()
 {
-       classinfo *c;
-
        /* java/lang/Throwable */
 
        class_java_lang_Throwable =
                class_new(utf_new_char(string_java_lang_Throwable));
        class_load(class_java_lang_Throwable);
        class_link(class_java_lang_Throwable);
-       compile_all_class_methods(class_java_lang_Throwable);
-
-       /* java/lang/VMThrowable */
-
-       c = class_new(utf_new_char(string_java_lang_VMThrowable));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/ClassFormatError */
-
-       c = class_new(utf_new_char(string_java_lang_ClassFormatError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/Error */
-
-       c = class_new(utf_new_char(string_java_lang_Error));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
 
        /* java/lang/Exception */
 
-       c = class_new(utf_new_char(string_java_lang_Exception));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/IncompatibleClassChangeError */
-
-       c = class_new(utf_new_char(string_java_lang_IncompatibleClassChangeError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/LinkageError */
-
-       c = class_new(utf_new_char(string_java_lang_LinkageError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/NoClassDefFoundError */
-
-       c = class_new(utf_new_char(string_java_lang_NoClassDefFoundError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/NoSuchFieldError */
-
-       c = class_new(utf_new_char(string_java_lang_NoSuchFieldError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/NoSuchMethodError */
-
-       c = class_new(utf_new_char(string_java_lang_NoSuchMethodError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/OutOfMemoryError */
+       class_java_lang_Exception =
+               class_new(utf_new_char(string_java_lang_Exception));
+       class_load(class_java_lang_Exception);
+       class_link(class_java_lang_Exception);
 
-       c = class_new(utf_new_char(string_java_lang_OutOfMemoryError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/VerifyError */
-
-       c = class_new(utf_new_char(string_java_lang_VerifyError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
-
-       /* java/lang/VirtualMachineError */
+       /* java/lang/Error */
 
-       c = class_new(utf_new_char(string_java_lang_VirtualMachineError));
-       class_load(c);
-       class_link(c);
-       compile_all_class_methods(c);
+       class_java_lang_Error =
+               class_new(utf_new_char(string_java_lang_Error));
+       class_load(class_java_lang_Error);
+       class_link(class_java_lang_Error);
 }
 
 
@@ -434,7 +310,7 @@ void compile_all_class_methods(classinfo *c)
 }
 
 
-void throw_exception_exit()
+static void throw_exception_exit_intern(bool doexit)
 {
        java_objectheader *xptr;
        classinfo *c;
@@ -446,25 +322,8 @@ void throw_exception_exit()
                /* clear exception, because we are calling jit code again */
                *exceptionptr = NULL;
 
-               fprintf(stderr, "Exception in thread \"main\" ");
-               fflush(stderr);
-
                c = xptr->vftbl->class;
 
-/*             ts = class_resolveclassmethod(c, */
-/*                                                                       utf_new_char("toString"), */
-/*                                                                       utf_new_char("()Ljava/lang/String;"), */
-/*                                                                       class_java_lang_Object, */
-/*                                                                       false); */
-
-/*             if (!ts) { */
-/*                     panic("internal error"); */
-/*             } */
-
-/*             tostring = asm_calljavafunction(ts, xptr, NULL, NULL, NULL); */
-
-/*             utf_display(javastring_toutf(tostring, false)); */
-
                pss = class_resolveclassmethod(c,
                                                                           utf_new_char("printStackTrace"),
                                                                           utf_new_char("()V"),
@@ -490,11 +349,43 @@ void throw_exception_exit()
                fflush(stderr);
 
                /* good bye! */
-               exit(1);
+               if (doexit) {
+                       exit(1);
+               }
        }
 }
 
 
+void throw_exception()
+{
+       throw_exception_exit_intern(false);
+}
+
+
+void throw_exception_exit()
+{
+       throw_exception_exit_intern(true);
+}
+
+
+void throw_main_exception()
+{
+       fprintf(stderr, "Exception in thread \"main\" ");
+       fflush(stderr);
+
+       throw_exception_exit_intern(false);
+}
+
+
+void throw_main_exception_exit()
+{
+       fprintf(stderr, "Exception in thread \"main\" ");
+       fflush(stderr);
+
+       throw_exception_exit_intern(true);
+}
+
+
 void throw_cacao_exception_exit(char *exception, char *message)
 {
        s4 i;
@@ -652,6 +543,7 @@ void native_loadclasses()
        class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
        class_load(class_java_lang_Void);
        class_link(class_java_lang_Void);
+
 }
 
 
@@ -1204,12 +1096,6 @@ utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
        buflength = u2_utflength(unicode_pos, unicode_length); 
        buffer    = MNEW(char, buflength);
  
-       /* memory allocation failed */
-       if (!buffer) {
-               printf("length: %d\n",buflength);
-               log_text("utf_new_u2:buffer==NULL");
-       }
-
        left = buflength;
        pos  = buffer;
 
@@ -1291,8 +1177,9 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
     literalstring *s;                /* hashtable element */
     java_lang_String *js;            /* u2-array wrapped in javastring */
     java_chararray *stringdata;      /* copy of u2-array */      
-    u4 key;   
-    u4 slot;  
+       classinfo *c;
+    u4 key;
+    u4 slot;
     u2 i;
 
 //#define DEBUG_LITERALSTRING_U2
@@ -1350,22 +1237,38 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
     stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
     stringdata->header.size = length;
 
+       /* if we use eager loading, we have to check loaded String class */
+       if (opt_eager) {
+               class_java_lang_String =
+                       class_new_intern(utf_new_char("java/lang/String"));
+
+               if (!class_load(class_java_lang_String))
+                       return NULL;
+
+               list_addfirst(&unlinkedclasses, class_java_lang_String);
+       }
+
     /* create new javastring */
     js = LNEW(java_lang_String);
-       /* TWISTI */
-/*      js->header.vftbl = class_java_lang_String->vftbl; */
-    js->header.vftbl = class_load(class_new(utf_new_char("java/lang/String")))->vftbl;
+       js->header.vftbl = class_java_lang_String->vftbl;
     js->value  = stringdata;
     js->offset = 0;
     js->count  = length;
 
+#ifdef DEBUG_LITERALSTRING_U2
+       printf("literalstring_u2: newly created at %p\n", js);
+       utf_display(javastring_toutf(js, 0));
+       printf("\n\n");
+       fflush(stdout);
+#endif
+                       
     /* create new literalstring */
     s = NEW(literalstring);
     s->hashlink = string_hash.ptr[slot];
     s->string   = (java_objectheader *) js;
     string_hash.ptr[slot] = s;
 
-    /* update numbe of hashtable entries */
+    /* update number of hashtable entries */
     string_hash.entries++;
 
     /* reorganization of hashtable */       
@@ -1374,7 +1277,7 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
          the external chains is approx. 2                */  
 
                u4 i;
-               literalstring *s;     
+               literalstring *s;
                hashtable newhash; /* the new hashtable */
       
                /* create new hashtable, double the size */
@@ -1402,13 +1305,6 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
                string_hash = newhash;
     }
 
-#ifdef DEBUG_LITERALSTRING_U2
-       printf("literalstring_u2: newly created at %p\n", js);
-       utf_display(javastring_toutf(js, 0));
-       printf("\n\n");
-       fflush(stdout);
-#endif
-                       
     return (java_objectheader *) js;
 }
 
@@ -1457,7 +1353,7 @@ void literalstring_free(java_objectheader* sobj)
 }
 
 
-void copy_vftbl(vftbl **dest, vftbl *src)
+void copy_vftbl(vftbl_t **dest, vftbl_t *src)
 {
     *dest = src;
 #if 0
@@ -1593,12 +1489,12 @@ classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
 }
 
 
-/******************************************************************************************
+/* get_parametertypes **********************************************************
 
-       use the descriptor of a method to generate a java/lang/Class array
-       which contains the classes of the parametertypes of the method
+   use the descriptor of a method to generate a java/lang/Class array
+   which contains the classes of the parametertypes of the method
 
-*******************************************************************************************/
+*******************************************************************************/
 
 java_objectarray* get_parametertypes(methodinfo *m) 
 {
@@ -1613,33 +1509,31 @@ java_objectarray* get_parametertypes(methodinfo *m)
     utf_nextu2(&utf_ptr);
   
     /* determine number of parameters */
-    while ( *utf_ptr != ')' ) {
-       get_type(&utf_ptr,desc_end,true);
-       parametercount++;
+    while (*utf_ptr != ')') {
+       get_type(&utf_ptr, desc_end, true);
+               parametercount++;
     }
 
     /* create class-array */
     result = builtin_anewarray(parametercount, class_java_lang_Class);
 
-    utf_ptr  =  descr->text;
+    utf_ptr = descr->text;
     utf_nextu2(&utf_ptr);
 
     /* get returntype classes */
     for (i = 0; i < parametercount; i++)
-           result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
+           result->data[i] =
+                       (java_objectheader *) get_type(&utf_ptr, desc_end, false);
 
     return result;
 }
 
 
+/* get_exceptiontypes **********************************************************
 
+   get the exceptions which can be thrown by a method
 
-
-/******************************************************************************************
-
-       get the exceptions which can be thrown by a method      
-
-*******************************************************************************************/
+*******************************************************************************/
 
 java_objectarray* get_exceptiontypes(methodinfo *m) {
     u2 exccount=m->thrownexceptionscount;
@@ -1805,14 +1699,17 @@ return true;
 /*--------------------------------------------------------*/
 
 
-java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start) {
+java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start)
+{
+#if defined(__GNUC__)
 #warning platform dependend
+#endif
         java_objectarray *tmpArray;
         int i;
         classinfo **current;
        classinfo *c;
         size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
-/*      printf("end %p, start %p, size %ld\n",end,start,size);*/
+        /*printf("end %p, start %p, size %ld\n",end,start,size);*/
         if (!class_java_lang_Class)
                 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
         if (!class_java_lang_SecurityManager)
@@ -1836,8 +1733,11 @@ java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo
 
 }
 
-java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start) {
+java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start)
+{
+#if defined(__GNUC__)
 #warning platform dependend
+#endif
         int i;
         classinfo **current;
        classinfo *c;