framework for handbook added
[cacao.git] / native.c
index f8f277053ace3155ea26df4855e36af95bae3634..5378f1d7e10c4ecde1f3fa6a25c91251860db75c 100644 (file)
--- a/native.c
+++ b/native.c
@@ -4,16 +4,15 @@
 
        See file COPYRIGHT for information on usage and disclaimer of warranties
 
-       Enth"alt die Tabellen f"ur die native-methods.
-       Die vom Headerfile-Generator erzeugten -.hh - Dateien werden hier
-       eingebunden, und ebenso alle C-Funktionen, mit denen diese
-       Methoden implementiert werden.
-
+    Contains the tables for native methods.
+       The .hh files created with the header file generator are all included here
+       as are the C functions implementing these methods.
+       
        Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at 
                 Roman Obermaisser   EMAIL: cacao@complang.tuwien.ac.at 
                 Andreas Krall       EMAIL: cacao@complang.tuwien.ac.at 
 
-       Last Change: 1996/11/14
+       Last Change: 2003/02/26
 
 *******************************************************************************/
 
 #include <string.h>
 #include <assert.h>
 #include <sys/time.h>
+#include <utime.h>
+#include <sys/utsname.h>
 
 #include "threads/thread.h"                       /* schani */
 #include "threads/locks.h"
 
-/* INCLUDE-Files fuer IO-Funktionen */
+/* Include files for IO functions */
 
 #include <fcntl.h>
 #include <dirent.h>
@@ -69,6 +70,7 @@ static classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
 static classinfo *class_java_lang_NoSuchFieldException;
 static classinfo *class_java_io_SyncFailedException;
 static classinfo *class_java_io_IOException;
+static classinfo *class_java_io_FileNotFoundException;
 static classinfo *class_java_io_UnixFileSystem;
 static classinfo *class_java_security_PrivilegedActionException;
 static classinfo *class_java_net_UnknownHostException;
@@ -92,9 +94,18 @@ java_objectheader* exceptionptr = NULL;
 
 /************* use classinfo structure as java.lang.Class object **************/
 
-static void use_class_as_object (classinfo *c) 
+void use_class_as_object (classinfo *c) 
 {
-       c->header.vftbl = class_java_lang_Class -> vftbl;
+       vftbl *vt = class_java_lang_Class -> vftbl;
+       vftbl *newtbl;
+       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;
+       }
 }
 
 /*********************** include Java Native Interface ************************/ 
@@ -180,6 +191,20 @@ static struct nativecompref {
 static bool nativecompdone = false;
 
 
+/******************************************************************************/
+/******************************************************************************/
+#include "natcalls.h"
+
+/* string call comparison table initialized */
+
+/******************************************************************************/
+/******************************************************************************/
+
+/*--------------- native method calls & classes used -------------------------*/
+
+
+
+
 /*********************** function: native_loadclasses **************************
 
        load classes required for native methods        
@@ -197,6 +222,8 @@ void native_loadclasses()
                class_new ( utf_new_char ("java/lang/Class") );
        class_java_io_IOException = 
                class_new ( utf_new_char ("java/io/IOException") );
+       class_java_io_FileNotFoundException = 
+               class_new ( utf_new_char ("java/io/FileNotFoundException") );
        class_java_lang_ClassNotFoundException =
                class_new ( utf_new_char ("java/lang/ClassNotFoundException") );
        class_java_lang_InstantiationException =
@@ -337,7 +364,6 @@ void init_systemclassloader()
 void systemclassloader_addlibname(java_objectheader *o)
 {
         methodinfo *m;
-       java_objectheader *LibraryNameVector;
        jfieldID id;
 
        m = class_resolvemethod (
@@ -372,21 +398,23 @@ void native_setclasspath (char *path)
 
 void throw_classnotfoundexception()
 {
+    if (!class_java_lang_ClassNotFoundException) {
+        panic("java.lang.ClassNotFoundException not found. Maybe wrong classpath?");
+    }
+
        /* throws a ClassNotFoundException */
        exceptionptr = native_new_and_init (class_java_lang_ClassNotFoundException);
 }
 
 
-/*********************** Funktion: native_findfunction *************************
+/*********************** Function: native_findfunction *************************
 
-       Sucht in der Tabelle die passende Methode (muss mit Klassennamen,
-       Methodennamen, Descriptor und 'static'-Status "ubereinstimmen),
-       und gibt den Funktionszeiger darauf zur"uck.
-       Return: Funktionszeiger oder NULL  (wenn es keine solche Methode gibt)
+       Looks up a method (must have the same class name, method name, descriptor
+       and 'static'ness) and returns a function pointer to it.
+       Returns: function pointer or NULL (if there is no such method)
 
-       Anmerkung: Zu Beschleunigung des Suchens werden die als C-Strings
-          vorliegenden Namen/Descriptors in entsprechende unicode-Symbole
-          umgewandelt (beim ersten Aufruf dieser Funktion).
+       Remark: For faster operation, the names/descriptors are converted from C
+               strings to Unicode the first time this function is called.
 
 *******************************************************************************/
 
@@ -398,7 +426,7 @@ functionptr native_findfunction (utf *cname, utf *mname,
        struct nativecompref *n;
         /* for warning message if no function is found */
        char *buffer;                   
-       int buffer_len, pos;
+       int buffer_len;
 
        isstatic = isstatic ? true : false;
 
@@ -592,9 +620,11 @@ java_objectheader *native_new_and_init (classinfo *c)
        m = class_findmethod(c, utf_new_char("<init>"), utf_new_char("()V"));
                                                      
        if (!m) {                                       /* initializer not found  */
-               sprintf(logtext, "warning: class has no instance-initializer: ");
-               utf_sprint(logtext + strlen(logtext), c->name);
-               dolog();
+               if (verbose) {
+                       sprintf(logtext, "Warning: class has no instance-initializer: ");
+                       utf_sprint(logtext + strlen(logtext), c->name);
+                       dolog();
+                       }
                return o;
                }
 
@@ -868,7 +898,7 @@ java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode
     return (java_objectheader *) js;
 }
 
-/******************** Funktion: literalstring_new *****************************
+/******************** Function: literalstring_new *****************************
 
     creates a new javastring with the text of the utf-symbol
     and inserts it into the string hashtable
@@ -880,7 +910,6 @@ java_objectheader *literalstring_new (utf *u)
     char *utf_ptr = u->text;         /* pointer to current unicode character in utf string */
     u4 utflength  = utf_strlen(u);   /* length of utf-string if uncompressed */
     java_chararray *a;               /* u2-array constructed from utf string */
-    java_objectheader *js;
     u4 i;
     
     /* allocate memory */ 
@@ -914,5 +943,130 @@ void literalstring_free (java_objectheader* sobj)
 
 
 
+void copy_vftbl(vftbl **dest, vftbl *src)
+{
+       *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
+       memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
+       memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
+}
+
+/*****************************************************************************/
+/*****************************************************************************/
+
+
+/*--------------------------------------------------------*/
+void printNativeCall(nativeCall nc) {
+  int i,j;
+
+  printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
+  for (i=0; i<nc.methCnt; i++) {  
+    printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
+
+    for (j=0; j<nc.callCnt[i]; j++) {  
+      printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
+       nc.methods[i].methodCalls[j].classname, 
+       nc.methods[i].methodCalls[j].methodname, 
+       nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
+      }
+    }
+printf("-+++++--------------------\n");fflush(stdout);
+}
+
+/*--------------------------------------------------------*/
+void printCompNativeCall(nativeCompCall nc) {
+  int i,j;
+printf("printCompNativeCall BEGIN\n");fflush(stdout);
+  printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
+  utf_display(nc.classname); fflush(stdout);
+  
+  for (i=0; i<nc.methCnt; i++) {  
+    printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
+    utf_display(nc.methods[i].methodname); fflush(stdout);
+    utf_display(nc.methods[i].descriptor);fflush(stdout);
+    printf("\n");fflush(stdout);
+
+    for (j=0; j<nc.callCnt[i]; j++) {  
+      printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
+       utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
+       utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
+       utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
+       printf("\n");fflush(stdout);
+      }
+    }
+printf("---------------------\n");fflush(stdout);
+}
+
+
+/*--------------------------------------------------------*/
+classMeth findNativeMethodCalls(utf *c, utf *m, utf *d ) 
+{
+    int i = 0;
+    int j = 0;
+    int cnt = 0;
+    classMeth mc;
+    mc.i_class = i;
+    mc.j_method = j;
+    mc.methCnt = cnt;
+
+    return mc;
+}
+
+/*--------------------------------------------------------*/
+nativeCall* findNativeClassCalls(char *aclassname ) {
+int i;
+
+for (i=0;i<NATIVECALLSSIZE; i++) {
+   /* convert table to utf later to speed up search */ 
+   if (strcmp(nativeCalls[i].classname, aclassname) == 0) 
+       return &nativeCalls[i];
+   }
+
+return NULL;
+}
+/*--------------------------------------------------------*/
+/*--------------------------------------------------------*/
+void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
+  int i,j;
+
+
+  ncc->classname = utf_new_char(nc.classname); 
+  ncc->methCnt = nc.methCnt;
+  
+  for (i=0; i<nc.methCnt; i++) {  
+    ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
+    ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
+    ncc->callCnt[i] = nc.callCnt[i];
+
+    for (j=0; j<nc.callCnt[i]; j++) {  
+
+       ncc->methods[i].methodCalls[j].classname  = utf_new_char(nc.methods[i].methodCalls[j].classname);
+
+        if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
+          ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
+          ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
+          }
+        else {
+          ncc->methods[i].methodCalls[j].methodname = NULL;
+          ncc->methods[i].methodCalls[j].descriptor = NULL;
+          }
+      }
+    }
+}
+
 
 
+/*--------------------------------------------------------*/
+
+bool natcall2utf(bool natcallcompdone) {
+int i;
+
+if (natcallcompdone) 
+       return true;
+
+for (i=0;i<NATIVECALLSSIZE; i++) {
+   utfNativeCall  (nativeCalls[i], &nativeCompCalls[i]);  
+   }
+
+return true;
+}
+/*--------------------------------------------------------*/