* m4/classpath.m4: Renamed --with-classpath* options to
[cacao.git] / src / native / native.c
index 6d0f4097383a0b9a9c068dee0a3fdcfdd05951c4..9a4cd343f55d984a39cd2b519d419967c23e6e34 100644 (file)
@@ -1,7 +1,7 @@
 /* src/native/native.c - table of native functions
 
    Copyright (C) 1996-2005, 2006, 2007, 2008
-   CACAOVM - Verein zu Foerderung der freien virtuellen Machine CACAO
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -89,6 +89,8 @@ static s4 native_tree_native_methods_comparator(const void *treenode, const void
 
 bool native_init(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("native_init");
+
 #if defined(ENABLE_LTDL)
        /* initialize libltdl */
 
@@ -473,7 +475,7 @@ static functionptr native_method_find(methodinfo *m)
 
        /* fill the temporary structure used for searching the tree */
 
-       tmpnmn.classname  = m->class->name;
+       tmpnmn.classname  = m->clazz->name;
        tmpnmn.name       = m->name;
        tmpnmn.descriptor = m->descriptor;
 
@@ -506,13 +508,13 @@ functionptr native_method_resolve(methodinfo *m)
        utf                            *newname;
        functionptr                     f;
 #if defined(ENABLE_LTDL)
-       classloader                    *cl;
+       classloader_t                  *cl;
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne;
        u4                              key;    /* hashkey                        */
        u4                              slot;   /* slot in hashtable              */
 #endif
-#if defined(WITH_CLASSPATH_SUN)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
        methodinfo                     *method_findNative;
        java_handle_t                  *s;
 #endif
@@ -521,7 +523,7 @@ functionptr native_method_resolve(methodinfo *m)
 
        if (opt_verbosejni) {
                printf("[Dynamic-linking native method ");
-               utf_display_printable_ascii_classname(m->class->name);
+               utf_display_printable_ascii_classname(m->clazz->name);
                printf(".");
                utf_display_printable_ascii(m->name);
                printf(" ... ");
@@ -529,7 +531,7 @@ functionptr native_method_resolve(methodinfo *m)
 
        /* generate method symbol string */
 
-       name = native_method_symbol(m->class->name, m->name);
+       name = native_method_symbol(m->clazz->name, m->name);
 
        /* generate overloaded function (having the types in it's name)           */
 
@@ -543,7 +545,7 @@ functionptr native_method_resolve(methodinfo *m)
 #if defined(ENABLE_LTDL)
        /* Get the classloader. */
 
-       cl = class_get_classloader(m->class);
+       cl = class_get_classloader(m->clazz);
 
        /* normally addresses are aligned to 4, 8 or 16 bytes */
 
@@ -570,7 +572,7 @@ functionptr native_method_resolve(methodinfo *m)
                le = le->hashlink;
        }
 
-# if defined(WITH_CLASSPATH_SUN)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
        if (f == NULL) {
                /* We can resolve the function directly from
                   java.lang.ClassLoader as it's a static function. */
@@ -730,7 +732,7 @@ void native_library_close(lt_dlhandle handle)
 *******************************************************************************/
 
 #if defined(ENABLE_LTDL)
-void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
+void native_library_add(utf *filename, classloader_t *loader, lt_dlhandle handle)
 {
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne; /* library name                       */
@@ -813,7 +815,7 @@ void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
 
 #if defined(ENABLE_LTDL)
 hashtable_library_name_entry *native_library_find(utf *filename,
-                                                                                                 classloader *loader)
+                                                                                                 classloader_t *loader)
 {
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne; /* library name                       */
@@ -858,6 +860,86 @@ hashtable_library_name_entry *native_library_find(utf *filename,
 #endif
 
 
+/* native_library_load *********************************************************
+
+   Load a native library and initialize it, if possible.
+
+   IN:
+       name ... name of the library
+          cl ..... classloader which loads this library
+
+   RETURN:
+       1 ... library loaded successfully
+       0 ... error
+
+*******************************************************************************/
+
+int native_library_load(JNIEnv *env, utf *name, classloader_t *cl)
+{
+#if defined(ENABLE_LTDL)
+       lt_dlhandle        handle;
+# if defined(ENABLE_JNI)
+       lt_ptr             onload;
+       int32_t            version;
+# endif
+
+       if (name == NULL) {
+               exceptions_throw_nullpointerexception();
+               return 0;
+       }
+
+       /* Is the library already loaded? */
+
+       if (native_library_find(name, cl) != NULL)
+               return 1;
+
+       /* Open the library. */
+
+       handle = native_library_open(name);
+
+       if (handle == NULL)
+               return 0;
+
+# if defined(ENABLE_JNI)
+       /* Resolve JNI_OnLoad function. */
+
+       onload = lt_dlsym(handle, "JNI_OnLoad");
+
+       if (onload != NULL) {
+               JNIEXPORT int32_t (JNICALL *JNI_OnLoad) (JavaVM *, void *);
+               JavaVM *vm;
+
+               JNI_OnLoad = (JNIEXPORT int32_t (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
+
+               (*env)->GetJavaVM(env, &vm);
+
+               version = JNI_OnLoad(vm, NULL);
+
+               /* If the version is not 1.2 and not 1.4 the library cannot be
+                  loaded. */
+
+               if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
+                       lt_dlclose(handle);
+                       return 0;
+               }
+       }
+# endif
+
+       /* Insert the library name into the library hash. */
+
+       native_library_add(name, cl, handle);
+
+       return 1;
+#else
+       vm_abort("native_library_load: not available");
+
+       /* Keep compiler happy. */
+
+       return 0;
+#endif
+}
+
+
 /* native_new_and_init *********************************************************
 
    Creates a new object on the heap and calls the initializer.