lib/standard: Introduce Byte type in kernel
[nit.git] / src / ffi / java.nit
index f78dc37..3f2a21e 100644 (file)
@@ -105,7 +105,7 @@ class JavaLanguage
                        jni_signature_alt = mclass_type.jni_signature_alt
                        return_type = mclass_type
                else
-                       params.add "recv"
+                       params.add "self"
                        if signature.return_mtype != null then
                                var ret_mtype = signature.return_mtype
                                ret_mtype = ret_mtype.resolve_for(mclass_type, mclass_type, mmodule, true)
@@ -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.add_one("", "-I $(JAVA_HOME)/include/ -I $(JAVA_HOME)/include/linux/")
+               ldflags.add_one("", "-L $(JNI_LIB_PATH) -ljvm")
        end
 
        # Name of the generated Java class where to store all implementation methods of this module
@@ -296,7 +296,7 @@ redef class AMethPropdef
                assert sys_class != null
                var sys_jni_env_meth = modelbuilder.try_get_mproperty_by_name2(self, mmodule, sys_class.mclass_type, "jni_env")
                if sys_jni_env_meth == null or not sys_jni_env_meth isa MMethod then
-                       toolcontext.error(self.location, "Java FFI error: you must import the `java` module when using the FFI with Java")
+                       toolcontext.error(self.location, "Java FFI Error: you must import the `java` module when using the FFI with Java")
                        return
                end
 
@@ -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
@@ -426,7 +424,7 @@ redef class MExplicitCall
                var csignature = mproperty.build_c_implementation_signature(recv_mtype, mmodule, "___indirect", long_signature, from_java_call_context)
                var cf = new CFunction("JNIEXPORT {csignature}")
                cf.exprs.add "\t{mproperty.build_ccall(recv_mtype, mainmodule, null, long_signature, from_java_call_context, null)}\n"
-               ccu.add_local_function cf
+               ccu.add_non_static_local_function cf
 
                # In Java, declare the extern method as a private static local method
                var java_signature = mproperty.build_csignature(recv_mtype, mainmodule, null, short_signature, java_call_context)
@@ -485,6 +483,7 @@ redef class MClassType
                if mclass.name == "Char" then return "char"
                if mclass.name == "Int" then return "long"
                if mclass.name == "Float" then return "double"
+               if mclass.name == "Byte" then return "byte"
                return super
        end
 
@@ -496,6 +495,7 @@ redef class MClassType
                if mclass.name == "Char" then return "jchar"
                if mclass.name == "Int" then return "jlong"
                if mclass.name == "Float" then return "jdouble"
+               if mclass.name == "Byte" then return "jbyte"
                return super
        end
 
@@ -520,23 +520,55 @@ 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"
                if mclass.name == "Char" then return "C"
                if mclass.name == "Int" then return "J"
                if mclass.name == "Float" then return "D"
+               if mclass.name == "Byte" then return "B"
                return super
        end
 
        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"
                if mclass.name == "Int" then return "Long"
                if mclass.name == "Float" then return "Double"
+               if mclass.name == "Byte" then return "Byte"
                return super
        end
 end
@@ -609,7 +641,7 @@ redef class MMethod
                cparams.add "jclass clazz"
 
                if not self.is_init then
-                       cparams.add "{call_context.name_mtype(recv_mtype)} recv"
+                       cparams.add "{call_context.name_mtype(recv_mtype)} self"
                end
                for p in signature.mparameters do
                        var param_mtype = p.mtype.resolve_for(recv_mtype, recv_mtype, from_mmodule, true)
@@ -623,3 +655,17 @@ end
 private fun java_call_context: JavaCallContext do return new JavaCallContext
 private fun to_java_call_context: ToJavaCallContext do return new ToJavaCallContext
 private fun from_java_call_context: FromJavaCallContext do return new FromJavaCallContext
+
+redef class CCompilationUnit
+       # Similar to `add_local_function` but not `static`
+       #
+       # Used when the signature contains a visibility attribute.
+       private fun add_non_static_local_function(c_function: CFunction)
+       do
+               body_decl.add c_function.signature
+               body_decl.add ";\n"
+
+               body_impl.add "\n"
+               body_impl.add c_function.to_writer
+       end
+end