nitc: rename `c_compiler_option` and cie to `cflags` and `ldflags`
[nit.git] / src / ffi / java.nit
index 3257a60..77c3c56 100644 (file)
@@ -96,7 +96,6 @@ class JavaLanguage
 
                var jni_signature_alt
                var return_type
-               var c_return_type
                var params = new Array[String]
                params.add "nit_ffi_jni_env"
                params.add "java_class"
@@ -105,19 +104,16 @@ class JavaLanguage
                if mproperty.is_init then
                        jni_signature_alt = mclass_type.jni_signature_alt
                        return_type = mclass_type
-                       c_return_type = mclass_type.cname
                else
                        params.add "recv"
                        if signature.return_mtype != null then
                                var ret_mtype = signature.return_mtype
                                ret_mtype = ret_mtype.resolve_for(mclass_type, mclass_type, mmodule, true)
                                return_type = signature.return_mtype
-                               c_return_type = mclass_type.cname
                                jni_signature_alt = return_type.jni_signature_alt
                        else
                                jni_signature_alt = "Void"
                                return_type = null
-                               c_return_type = null
                        end
                end
 
@@ -159,6 +155,8 @@ class JavaLanguage
                {{{block.code}}}
        }
 """
+
+               mmodule.callbacks_used_from_java.join m.foreign_callbacks
        end
 
        redef fun compile_extern_class(block, m, ccu, mmodule) do end
@@ -174,7 +172,7 @@ class JavaLanguage
                mmodule.insert_compiler_options
 
                # Enable linking C callbacks to java native methods
-               mmodule.ensure_linking_callback_methods(ffi_ccu.as(not null), mmodule.ffi_callbacks[self])
+               mmodule.ensure_linking_callback_methods(ffi_ccu.as(not null))
 
                # Java implementation code
                var java_file = mmodule.java_file
@@ -193,6 +191,7 @@ class JavaLanguage
 end
 
 redef class MModule
+       private var callbacks_used_from_java = new ForeignCallbackSet
 
        # Pure java class source file
        private var java_file: nullable JavaClassTemplate = null
@@ -207,8 +206,9 @@ redef class MModule
        end
 
        # Compile C code to call JNI and link C callbacks implementations to Java extern methods
-       private fun ensure_linking_callback_methods(ccu: CCompilationUnit, callbacks: Set[NitniCallback])
+       private fun ensure_linking_callback_methods(ccu: CCompilationUnit)
        do
+               var callbacks = callbacks_used_from_java.callbacks
                if callbacks.is_empty then
                        ccu.body_decl.add "static int nit_ffi_with_java_registered_natives = 1;\n"
                        return
@@ -221,7 +221,7 @@ redef class MModule
                        jni_methods.add_all(cb.jni_methods_declaration(self))
                end
 
-               var cf = new CFunction("static void nit_ffi_with_java_register_natives(JNIEnv* env, jclass jclazz)")
+               var cf = new CFunction("void nit_ffi_with_java_register_natives(JNIEnv* env, jclass jclazz)")
                cf.exprs.add """
        nit_ffi_with_java_registered_natives = 1;
 
@@ -242,8 +242,8 @@ redef class MModule
        # Tell the C compiler where to find jni.h and how to link with libjvm
        private fun insert_compiler_options
        do
-               c_compiler_options = "{c_compiler_options} -I $(JAVA_HOME)/include/"
-               c_linker_options = "{c_linker_options} -L $(JNI_LIB_PATH) -ljvm"
+               cflags = "{cflags} -I $(JAVA_HOME)/include/ -I $(JAVA_HOME)/include/linux/"
+               ldflags = "{ldflags} -L $(JNI_LIB_PATH) -ljvm"
        end
 
        # Name of the generated Java class where to store all implementation methods of this module
@@ -262,12 +262,12 @@ redef class AMethPropdef
                end
        end
 
-       # Insert additionnal explicit calls to get the current `JNIEnv`
+       # Insert additional explicit calls to get the current `JNIEnv`
        #
        # This forces declaration of callbacks to Nit. The callbacks will be available in Java
        # but will be used mainly by the FFI itself.
        #
-       # The developper can aso customize the JNIEnv used by the FFI by redefing `Sys::jni_env`.
+       # The developer can also customize the JNIEnv used by the FFI by redefining `Sys::jni_env`.
        private fun insert_artificial_callbacks(toolcontext: ToolContext)
        do
                var fcc = foreign_callbacks
@@ -334,7 +334,6 @@ class JavaClassTemplate
        super Template
 
        var java_class_name: String
-       init(name: String) do self.java_class_name = name
 
        var header = new Template
        var class_content = new Template
@@ -398,7 +397,6 @@ class ForeignJavaType
        super ForeignType
 
        var java_type: String
-       init (java_type: String) do self.java_type = java_type
 end
 
 redef class NitniCallback
@@ -417,6 +415,8 @@ end
 redef class MExplicitCall
        redef fun compile_callback_to_java(mmodule, mainmodule, ccu)
        do
+               if not mmodule.callbacks_used_from_java.callbacks.has(self) then return
+
                var mproperty = mproperty
                assert mproperty isa MMethod
 
@@ -449,7 +449,7 @@ redef class MType
        # Type name in Java
        #
        # * Primitives common to both languages use their Java primitive type
-       # * Nit extern Java classes are reprensented by their full Java type
+       # * Nit extern Java classes are represented by their full Java type
        # * Other Nit objects are represented by `int` in Java. It holds the
        #       pointer to the underlying C structure.
        #       TODO create static Java types to store and hide the pointer
@@ -501,7 +501,6 @@ redef class MClassType
        do
                var ftype = mclass.ftype
                if ftype isa ForeignJavaType then
-                       var ori_jni_type = jni_type
                        var jni_type = ftype.java_type.
                                replace('.', "/").replace(' ', "").replace('\n', "")
 
@@ -519,6 +518,35 @@ redef class MClassType
                                else break
                        end
 
+                       # Change `float[]` to `[float`
+                       if jni_type.has('[') then
+                               var depth = jni_type.chars.count('[')
+                               var java_type = jni_type.replace("[]", "")
+                               var short
+
+                               if java_type == "boolean" then
+                                       short = "Z"
+                               else if java_type == "byte" then
+                                       short = "B"
+                               else if java_type == "char" then
+                                       short = "C"
+                               else if java_type == "short" then
+                                       short = "S"
+                               else if java_type == "int" then
+                                       short = "I"
+                               else if java_type == "long" then
+                                       short = "J"
+                               else if java_type == "float" then
+                                       short = "F"
+                               else if java_type == "double" then
+                                       short = "D"
+                               else
+                                       short = "L{java_type};"
+                               end
+
+                               return "["*depth + short
+                       end
+
                        return "L{jni_type};"
                end
                if mclass.name == "Bool" then return "Z"
@@ -531,6 +559,7 @@ redef class MClassType
        redef fun jni_signature_alt
        do
                var ftype = mclass.ftype
+
                if ftype isa ForeignJavaType then return "Object"
                if mclass.name == "Bool" then return "Boolean"
                if mclass.name == "Char" then return "Char"