* src/vm/exceptions.c (exceptions_throw_unsatisfiedlinkerror)
[cacao.git] / src / native / native.c
index 8d90c43b7a64716cb81866bab2b5e3c19f1f9ab5..a71c7383dcb9aced0ffe47822b26ab0bd54fa61f 100644 (file)
@@ -1,6 +1,6 @@
 /* src/native/native.c - table of native functions
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
    J. Wenninger, Institut f. Computersprachen - TU Wien
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Reinhard Grafl
-            Roman Obermaisser
-            Andreas Krall
-            Christian Thalinger
-
-   $Id: native.c 6035 2006-11-21 23:21:18Z twisti $
+   $Id: native.c 7305 2007-02-09 11:08:14Z twisti $
 
 */
 
@@ -48,6 +41,7 @@
 #include "mm/memory.h"
 #include "native/jni.h"
 #include "native/native.h"
+#include "native/include/java_lang_String.h"
 #include "native/include/java_lang_Throwable.h"
 
 #if defined(ENABLE_THREADS)
 # include "threads/none/lock.h"
 #endif
 
+#include "toolbox/hashtable.h"
 #include "toolbox/logging.h"
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
-#include "vm/hashtable.h"
-#include "vm/loader.h"
-#include "vm/options.h"
-#include "vm/resolve.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/jit.h"
 
+#include "vmcore/loader.h"
+#include "vmcore/options.h"
+#include "vmcore/resolve.h"
+
 #if defined(ENABLE_JVMTI)
 #include "native/jvmti/cacaodbg.h"
 #endif
 
 /* include table of native functions ******************************************/
 
-#include "native/include/java_lang_Cloneable.h"
-#include "native/include/java_util_Properties.h"
+#if defined(ENABLE_JAVASE)
+
 #include "native/include/java_io_InputStream.h"
 #include "native/include/java_io_PrintStream.h"
 
+#include "native/include/java_lang_Cloneable.h"
+#include "native/include/java_util_Properties.h"
+
+#include "native/include/java_lang_Object.h"
+
 #include "native/include/gnu_classpath_VMStackWalker.h"
 #include "native/include/gnu_classpath_VMSystemProperties.h"
 #include "native/include/gnu_java_lang_management_VMClassLoadingMXBeanImpl.h"
 #include "native/include/gnu_java_lang_management_VMMemoryMXBeanImpl.h"
 #include "native/include/gnu_java_lang_management_VMRuntimeMXBeanImpl.h"
-#include "native/include/java_lang_Class.h"
-#include "native/include/java_lang_Object.h"
 #include "native/include/java_lang_VMClass.h"
+#include "native/include/java_security_ProtectionDomain.h"  /* required by... */
 #include "native/include/java_lang_VMClassLoader.h"
 #include "native/include/java_lang_VMObject.h"
 #include "native/include/java_lang_VMRuntime.h"
 #include "native/include/gnu_classpath_jdwp_VMMethod.h"
 #endif
 
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+
+#include "native/include/com_sun_cldchi_io_ConsoleOutputStream.h"
+#include "native/include/java_lang_Class.h"
+#include "native/include/java_lang_Double.h"
+#include "native/include/java_lang_Float.h"
+#include "native/include/java_lang_Math.h"
+#include "native/include/java_lang_Runtime.h"
+#include "native/include/java_lang_System.h"
+#include "native/include/java_lang_Thread.h"
+
+#endif
+
 #if defined(WITH_STATIC_CLASSPATH)
 
 /* these are required to prevent compiler warnings */
 *******************************************************************************/
 
 functionptr dummynativetable[] = {
+#if defined(ENABLE_JAVASE)
        (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
        (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClass,
        (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClassLoader,
@@ -301,6 +316,59 @@ functionptr dummynativetable[] = {
        (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
 #endif
 
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+       (functionptr) Java_com_sun_cldc_io_ResourceInputStream_open,
+
+       (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_open0,
+       (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf,
+       (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte,
+
+       (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
+
+       (functionptr) Java_java_lang_Class_forName,
+       (functionptr) Java_java_lang_Class_newInstance,
+       (functionptr) Java_java_lang_Class_isInstance,
+       (functionptr) Java_java_lang_Class_isAssignableFrom,
+       (functionptr) Java_java_lang_Class_isInterface,
+       (functionptr) Java_java_lang_Class_isArray,
+       (functionptr) Java_java_lang_Class_getName,
+
+       (functionptr) Java_java_lang_Double_doubleToLongBits,
+
+       (functionptr) Java_java_lang_Float_floatToIntBits,
+
+       (functionptr) Java_java_lang_Math_ceil,
+       (functionptr) Java_java_lang_Math_cos,
+       (functionptr) Java_java_lang_Math_floor,
+       (functionptr) Java_java_lang_Math_sin,
+       (functionptr) Java_java_lang_Math_sqrt,
+       (functionptr) Java_java_lang_Math_tan,
+
+       (functionptr) Java_java_lang_Object_hashCode,
+       (functionptr) Java_java_lang_Object_notify,
+       (functionptr) Java_java_lang_Object_wait,
+
+       (functionptr) Java_java_lang_Runtime_exitInternal,
+       (functionptr) Java_java_lang_Runtime_freeMemory,
+       (functionptr) Java_java_lang_Runtime_totalMemory,
+       (functionptr) Java_java_lang_Runtime_gc,
+
+       (functionptr) Java_java_lang_String_hashCode,
+       (functionptr) Java_java_lang_String_indexOf__I,
+       (functionptr) Java_java_lang_String_indexOf__II,
+       (functionptr) Java_java_lang_String_lastIndexOf__II,
+       (functionptr) Java_java_lang_String_intern,
+
+       (functionptr) Java_java_lang_System_getProperty0,
+
+       (functionptr) Java_java_lang_Thread_currentThread,
+       (functionptr) Java_java_lang_Thread_setPriority0,
+       (functionptr) Java_java_lang_Thread_start0,
+       (functionptr) Java_java_lang_Thread_yield,
+
+       (functionptr) Java_java_lang_Throwable_printStackTrace,
+       (functionptr) Java_java_lang_Throwable_fillInStackTrace
+#endif
 };
 
 #endif /* defined(ENABLE_LIBJVM) */
@@ -339,7 +407,7 @@ bool native_init(void)
        /* initialize libltdl */
 
        if (lt_dlinit())
-               vm_abort("lt_dlinit failed: %s\n", lt_dlerror());
+               vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
 
        /* Get the handle for the main program or for the libjvm.so,
           depends on the configuration. */
@@ -350,10 +418,10 @@ bool native_init(void)
 
        if (!(mainhandle = lt_dlopenext("libjvm")))
                if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
-                       vm_abort("lt_dlopenext failed: %s\n", lt_dlerror());
+                       vm_abort("native_init: lt_dlopenext failed: %s\n", lt_dlerror());
 # else
        if (!(mainhandle = lt_dlopen(NULL)))
-               vm_abort("lt_dlopen failed: %s\n", lt_dlerror());
+               vm_abort("native_init: lt_dlopen failed: %s\n", lt_dlerror());
 # endif
 
        /* initialize library hashtable, 10 entries should be enough */
@@ -552,7 +620,6 @@ functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
                        return n->func;
        }
 
-               
        /* no function was found, throw exception */
 
        *exceptionptr =
@@ -876,9 +943,7 @@ functionptr native_resolve_function(methodinfo *m)
                if (opt_verbosejni)
                        printf("failed ]\n");
 
-               *exceptionptr =
-                       new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
-                                                                        m->name);
+               exceptions_throw_unsatisfiedlinkerror(m->name);
        }
 
        /* release memory */
@@ -902,14 +967,14 @@ java_objectheader *native_new_and_init(classinfo *c)
        methodinfo *m;
        java_objectheader *o;
 
-       if (!c)
-               return *exceptionptr;
+       if (c == NULL)
+               vm_abort("native_new_and_init: c == NULL");
 
        /* create object */
 
        o = builtin_new(c);
        
-       if (!o)
+       if (o == NULL)
                return NULL;
 
        /* try to find the initializer */
@@ -919,7 +984,7 @@ java_objectheader *native_new_and_init(classinfo *c)
        /* ATTENTION: returning the object here is ok, since the class may
        not have an initializer */
 
-       if (!m)
+       if (m == NULL)
                return o;
 
        /* call initializer */
@@ -930,19 +995,19 @@ java_objectheader *native_new_and_init(classinfo *c)
 }
 
 
-java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
+java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
 {
-       methodinfo *m;
+       methodinfo        *m;
        java_objectheader *o;
 
-       if (!c)
-               return *exceptionptr;
+       if (c == NULL)
+               vm_abort("native_new_and_init_string: c == NULL");
 
        /* create object */
 
        o = builtin_new(c);
 
-       if (!o)
+       if (o == NULL)
                return NULL;
 
        /* find initializer */
@@ -955,7 +1020,7 @@ java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
 
        /* initializer not found */
 
-       if (!m)
+       if (m == NULL)
                return NULL;
 
        /* call initializer */
@@ -971,14 +1036,14 @@ java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
        methodinfo *m;
        java_objectheader *o;
 
-       if (!c)
-               return *exceptionptr;
+       if (c == NULL)
+               vm_abort("native_new_and_init_int: c == NULL");
 
        /* create object */
 
        o = builtin_new(c);
        
-       if (!o)
+       if (o == NULL)
                return NULL;
 
        /* find initializer */
@@ -987,7 +1052,7 @@ java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
 
        /* initializer not found  */
 
-       if (!m)
+       if (m == NULL)
                return NULL;
 
        /* call initializer */
@@ -998,19 +1063,19 @@ java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
 }
 
 
-java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
+java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
 {
-       methodinfo *m;
        java_objectheader *o;
+       methodinfo        *m;
 
-       if (!c)
-               return *exceptionptr;
+       if (c == NULL)
+               vm_abort("native_new_and_init_throwable: c == NULL");
 
        /* create object */
 
        o = builtin_new(c);
        
-       if (!o)
+       if (o == NULL)
                return NULL;
 
        /* find initializer */
@@ -1019,7 +1084,7 @@ java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwab
                                                      
        /* initializer not found */
 
-       if (!m)
+       if (m == NULL)
                return NULL;
 
        /* call initializer */
@@ -1030,31 +1095,44 @@ java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwab
 }
 
 
-/* native_class_getname ********************************************************
+/* native_class_getdeclaredannotations *****************************************
 
-   Implementation for java.lang.Class.getName()Ljava/lang/String;
+   Implementation for
+   java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
 
 *******************************************************************************/
 
-java_lang_String *native_class_getname(classinfo *c)
+#if defined(ENABLE_JAVASE)
+java_objectarray *native_class_getdeclaredannotations(classinfo *c)
 {
-       java_lang_String *s;
-       u4                i;
+       java_objectarray *oa;
+       s4                count;
+       s4                i;
+
+       classinfo *class_java_lang_annotation_Annotation;
 
-       s = (java_lang_String *) javastring_new(c->name);
+       /* create Annotation-array */
 
-       if (s == NULL)
+       /* XXX should we cache that class? */
+       if (!(class_java_lang_annotation_Annotation =
+                 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
                return NULL;
 
-       /* return string where '/' is replaced by '.' */
+       count = c->runtimevisibleannotationscount;
+
+       oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
 
-       for (i = 0; i < s->value->header.size; i++) {
-               if (s->value->data[i] == '/')
-                       s->value->data[i] = '.';
+       if (oa == NULL)
+               return NULL;
+
+       /* fill the annotations */
+
+       for (i = 0; i < count; i++) {
        }
 
-       return s;
+       return oa;
 }
+#endif
 
 
 /* native_get_parametertypes ***************************************************