X-Git-Url: http://nitlanguage.org diff --git a/src/common_ffi/java.nit b/src/common_ffi/java.nit index 44a9d86..571c319 100644 --- a/src/common_ffi/java.nit +++ b/src/common_ffi/java.nit @@ -32,23 +32,24 @@ class JavaLanguage redef fun identify_language(n) do return n.is_java - redef fun compile_module_block(block, ccu, nmodule) + redef fun compile_module_block(block, ccu, mmodule) do - nmodule.ensure_java_files - var java_file = nmodule.java_file + mmodule.ensure_java_files + var java_file = mmodule.java_file assert java_file != null - java_file.header.add(block.code) + if block.is_inner_java then + java_file.class_content.add(block.code) + else java_file.header.add(block.code) end - redef fun compile_extern_method(block, m, ccu, nmodule) + redef fun compile_extern_method(block, m, ccu, mmodule) do ffi_ccu = ccu - nmodule.ensure_java_files - var java_file = nmodule.java_file + mmodule.ensure_java_files + var java_file = mmodule.java_file assert java_file != null - var mmodule = nmodule.mmodule.as(not null) var mclass_type = m.parent.as(AClassdef).mclass.mclass_type var mmethodef = m.mpropdef var mproperty = m.mpropdef.mproperty @@ -61,11 +62,16 @@ class JavaLanguage jmethodID java_meth_id; // retrieve the current JVM - Sys sys = {{{mmodule.name}}}___Pointer_sys(NULL); - JNIEnv *nit_ffi_jni_env = {{{mmodule.name}}}___Sys_jni_env(sys); + Sys sys = Pointer_sys(NULL); + JNIEnv *nit_ffi_jni_env = Sys_jni_env(sys); // retrieve the implementation Java class - java_class = (*nit_ffi_jni_env)->FindClass(nit_ffi_jni_env, "{{{mmodule.impl_java_class_name}}}"); + java_class = Sys_load_jclass(sys, "{{{mmodule.impl_java_class_name}}}"); + if (java_class == NULL) { + PRINT_ERROR("Nit FFI with Java error: failed to load class.\\n"); + (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env); + exit(1); + } // register callbacks (only once per Nit module) if (!nit_ffi_with_java_registered_natives) nit_ffi_with_java_register_natives(nit_ffi_jni_env, java_class); @@ -78,7 +84,7 @@ class JavaLanguage // retreive the implementation static function java_meth_id = (*nit_ffi_jni_env)->GetStaticMethodID(nit_ffi_jni_env, java_class, "{{{java_fun_name}}}", "{{{jni_format}}}"); if (java_meth_id == NULL) { - fprintf(stderr, "Nit FFI with Java error: Java implementation not found.\\n"); + PRINT_ERROR("Nit FFI with Java error: Java implementation not found.\\n"); (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env); exit(1); } @@ -115,7 +121,11 @@ class JavaLanguage end end - for p in signature.mparameters do params.add(p.name) + for p in signature.mparameters do + var param_mtype = p.mtype + param_mtype = param_mtype.resolve_for(mclass_type, mclass_type, mmodule, true) + params.add(to_java_call_context.cast_to(param_mtype, p.name)) + end var cname = "(*nit_ffi_jni_env)->CallStatic{jni_signature_alt}Method" var ccall @@ -127,10 +137,12 @@ class JavaLanguage // execute implementation code {{{ccall}}} if ((*nit_ffi_jni_env)->ExceptionCheck(nit_ffi_jni_env)) { - fprintf(stderr, "Nit FFI with Java error: Exception after call.\\n"); + PRINT_ERROR("Nit FFI with Java error: Exception after call.\\n"); (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env); exit(1); } + + (*nit_ffi_jni_env)->DeleteLocalRef(nit_ffi_jni_env, java_class); """ if return_type != null then @@ -141,7 +153,7 @@ class JavaLanguage # Java implementation function in Java var java_csig = mproperty.build_csignature(mclass_type, mmodule, "___java_impl", long_signature, java_call_context) - nmodule.java_file.class_content.add """ + mmodule.java_file.class_content.add """ public static {{{java_csig}}} { // from Nit FII at: {{{block.location}}} {{{block.code}}} @@ -149,40 +161,38 @@ class JavaLanguage """ end - redef fun compile_extern_class(block, m, ccu, nmodule) do end + redef fun compile_extern_class(block, m, ccu, mmodule) do end redef fun get_ftype(block, m) do return new ForeignJavaType(block.code) - redef fun compile_to_files(nmodule, compdir) + redef fun compile_to_files(mmodule, compdir) do # Make sure we have a .java file - nmodule.ensure_java_files + mmodule.ensure_java_files # Needed compiler and linker options - nmodule.insert_compiler_options + mmodule.insert_compiler_options # Enable linking C callbacks to java native methods - nmodule.ensure_linking_callback_methods(ffi_ccu, self.callbacks) + mmodule.ensure_linking_callback_methods(ffi_ccu.as(not null), mmodule.ffi_callbacks[self]) # Java implementation code - var java_file = nmodule.java_file + var java_file = mmodule.java_file assert java_file != null var extern_java_file = java_file.write_to_files(compdir) - nmodule.ffi_files.add(extern_java_file) + mmodule.ffi_files.add(extern_java_file) end - var callbacks = new HashSet[NitniCallback] # HACK - var ffi_ccu: CCompilationUnit # HACK + var ffi_ccu: nullable CCompilationUnit = null # HACK - redef fun compile_callback(callback, nmodule, mainmodule, ccu) + redef fun compile_callback(callback, mmodule, mainmodule, ccu) do ffi_ccu = ccu - callbacks.add callback - callback.compile_callback_to_java(nmodule, ccu) + callback.compile_callback_to_java(mmodule, mainmodule, ccu) end end -redef class AModule +redef class MModule # Pure java class source file private var java_file: nullable JavaClassTemplate = null @@ -193,7 +203,7 @@ redef class AModule if java_file != null then return # Java implementation code - java_file = new JavaClassTemplate(mmodule.impl_java_class_name) + java_file = new JavaClassTemplate(impl_java_class_name) end # Compile C code to call JNI and link C callbacks implementations to Java extern methods @@ -208,7 +218,7 @@ redef class AModule var jni_methods = new Array[String] for cb in callbacks do - jni_methods.add_all(cb.jni_methods_declaration(mmodule.as(not null))) + 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)") @@ -221,7 +231,7 @@ redef class AModule }; jint res = (*env)->RegisterNatives(env, jclazz, methods, n_methods); if (res != JNI_OK) { - fprintf(stderr, "RegisterNatives failed\\n"); + PRINT_ERROR("RegisterNatives failed\\n"); (*env)->ExceptionDescribe(env); exit(1); } @@ -232,15 +242,13 @@ redef class AModule # Tell the C compiler where to find jni.h and how to link with libjvm private fun insert_compiler_options do - mmodule.c_compiler_options = "{mmodule.c_compiler_options} -I $(JAVA_HOME)/include/" - mmodule.c_linker_options = "{mmodule.c_linker_options} -L $(JNI_LIB_PATH) -ljvm" + c_compiler_options = "{c_compiler_options} -I $(JAVA_HOME)/include/" + c_linker_options = "{c_linker_options} -L $(JNI_LIB_PATH) -ljvm" end -end -redef class MModule # Name of the generated Java class where to store all implementation methods of this module # as well as generated callbacks. - private fun impl_java_class_name: String do return "NitFFIWithJava_{name}" + private fun impl_java_class_name: String do return "Nit_{name}" end redef class AExternPropdef @@ -250,7 +258,7 @@ redef class AExternPropdef var block = n_extern_code_block if block != null and block.is_java then - insert_articifial_callbacks(toolcontext) + insert_artificial_callbacks(toolcontext) end end @@ -260,7 +268,7 @@ redef class AExternPropdef # 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`. - private fun insert_articifial_callbacks(toolcontext: ToolContext) + private fun insert_artificial_callbacks(toolcontext: ToolContext) do var fcc = foreign_callbacks assert fcc != null @@ -268,6 +276,12 @@ redef class AExternPropdef var modelbuilder = toolcontext.modelbuilder var mmodule = mpropdef.mclassdef.mmodule + # We use callbacks from the C FFI since they will be called from generated C + var c_language_visitor = toolcontext.ffi_language_assignation_phase.as(FFILanguageAssignationPhase).c_language + if not mmodule.ffi_callbacks.keys.has(c_language_visitor) then + mmodule.ffi_callbacks[c_language_visitor] = new HashSet[NitniCallback] + end + # Pointer::sys var pointer_class = modelbuilder.try_get_mclass_by_name(self, mmodule, "Pointer") assert pointer_class != null @@ -276,24 +290,44 @@ redef class AExternPropdef var explicit_call = new MExplicitCall(pointer_class.mclass_type, pointer_sys_meth, mmodule) fcc.callbacks.add(explicit_call) - explicit_call.fill_type_for(fcc, mmodule) + mmodule.ffi_callbacks[c_language_visitor].add(explicit_call) # Sys::jni_env var sys_class = modelbuilder.try_get_mclass_by_name(self, mmodule, "Sys") assert sys_class != null var sys_jni_env_meth = modelbuilder.try_get_mproperty_by_name2(self, mmodule, sys_class.mclass_type, "jni_env") - assert sys_jni_env_meth != null - assert sys_jni_env_meth isa MMethod + 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") + return + end explicit_call = new MExplicitCall(sys_class.mclass_type, sys_jni_env_meth, mmodule) fcc.callbacks.add(explicit_call) + mmodule.ffi_callbacks[c_language_visitor].add(explicit_call) + + # Sys::load_jclass + var sys_jni_load_jclass_meth = modelbuilder.try_get_mproperty_by_name2(self, mmodule, sys_class.mclass_type, "load_jclass") + assert sys_jni_load_jclass_meth != null + assert sys_jni_load_jclass_meth isa MMethod + + explicit_call = new MExplicitCall(sys_class.mclass_type, sys_jni_load_jclass_meth, mmodule) + fcc.callbacks.add(explicit_call) + mmodule.ffi_callbacks[c_language_visitor].add(explicit_call) explicit_call.fill_type_for(fcc, mmodule) end end redef class AExternCodeBlock - fun is_java : Bool do return language_name != null and + # Is this code block in Java? + fun is_java: Bool do return is_default_java or (parent isa AModule and is_inner_java) + + # Is this code block in Java, with the default mode? (On module blocks it targets the file header) + private fun is_default_java: Bool do return language_name != null and language_name_lowered == "java" + + # Is this code block in Java, and for a module block to generate in the class? + private fun is_inner_java: Bool do return language_name != null and + language_name_lowered == "java inner" end # Java class source template @@ -331,7 +365,7 @@ class JavaFile super ExternFile redef fun makefile_rule_name do return "{filename.basename(".java")}.class" - redef fun makefile_rule_content do return "javac {filename} -d ." + redef fun makefile_rule_content do return "javac {filename.basename("")} -d ." redef fun add_to_jar do return true end @@ -370,33 +404,32 @@ end redef class NitniCallback # Compile C and Java code to implement this callback - fun compile_callback_to_java(nmodule: AModule, ccu: CCompilationUnit) do end + fun compile_callback_to_java(mmodule: MModule, mainmodule: MModule, ccu: CCompilationUnit) do end # Returns the list of C functions to link with extern Java methods, as required # to enable this callback from Java code. # - # Return used by `AModule::ensure_linking_callback_methods` + # Return used by `MModule::ensure_linking_callback_methods` # # TODO we return an Array to support cast and other features like that fun jni_methods_declaration(from_module: MModule): Array[String] do return new Array[String] end redef class MExplicitCall - redef fun compile_callback_to_java(nmodule, ccu) + redef fun compile_callback_to_java(mmodule, mainmodule, ccu) do var mproperty = mproperty assert mproperty isa MMethod - var mmodule = nmodule.mmodule.as(not null) # In C, indirection implementing the Java extern methods 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, mmodule, null, long_signature, from_java_call_context, null)}\n" + cf.exprs.add "\t{mproperty.build_ccall(recv_mtype, mainmodule, null, long_signature, from_java_call_context, null)}\n" ccu.add_local_function cf # In Java, declare the extern method as a private static local method - var java_signature = mproperty.build_csignature(recv_mtype, mmodule, null, short_signature, java_call_context) - nmodule.java_file.class_content.add "private native static {java_signature};\n" + var java_signature = mproperty.build_csignature(recv_mtype, mainmodule, null, short_signature, java_call_context) + mmodule.java_file.class_content.add "private native static {java_signature};\n" end redef fun jni_methods_declaration(from_mmodule) @@ -445,10 +478,11 @@ redef class MClassType redef fun java_type do var ftype = mclass.ftype - if ftype isa ForeignJavaType then return ftype.java_type + if ftype isa ForeignJavaType then return ftype.java_type. + replace('/', ".").replace('$', ".").replace(' ', "").replace('\n',"") if mclass.name == "Bool" then return "boolean" if mclass.name == "Char" then return "char" - if mclass.name == "Int" then return "int" + if mclass.name == "Int" then return "long" if mclass.name == "Float" then return "double" return super end @@ -459,7 +493,7 @@ redef class MClassType if ftype isa ForeignJavaType then return "jobject" if mclass.name == "Bool" then return "jboolean" if mclass.name == "Char" then return "jchar" - if mclass.name == "Int" then return "jint" + if mclass.name == "Int" then return "jlong" if mclass.name == "Float" then return "jdouble" return super end @@ -467,10 +501,30 @@ redef class MClassType redef fun jni_format do var ftype = mclass.ftype - if ftype isa ForeignJavaType then return "L{ftype.java_type.replace('.', "/").replace(' ', "")};" + if ftype isa ForeignJavaType then + var ori_jni_type = jni_type + var jni_type = ftype.java_type. + replace('.', "/").replace(' ', "").replace('\n', "") + + # Remove parameters of generic types + loop + var i = jni_type.last_index_of('<') + if i >= 0 then + var j = jni_type.index_of_from('>', i) + if j == -1 then + print "Error: missing closing '>' in extern Java type of \"{mclass.name}\"" + exit 1 + end + jni_type = jni_type.substring(0, i) + + jni_type.substring(j+1, jni_type.length) + else break + 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 "I" + if mclass.name == "Int" then return "J" if mclass.name == "Float" then return "D" return super end @@ -479,9 +533,9 @@ redef class MClassType do var ftype = mclass.ftype if ftype isa ForeignJavaType then return "Object" - if mclass.name == "Bool" then return "Bool" + if mclass.name == "Bool" then return "Boolean" if mclass.name == "Char" then return "Char" - if mclass.name == "Int" then return "Int" + if mclass.name == "Int" then return "Long" if mclass.name == "Float" then return "Double" return super end @@ -516,6 +570,7 @@ redef class MMethod else var return_mtype = msignature.return_mtype if return_mtype != null then + return_mtype = return_mtype.resolve_for(recv_mtype, recv_mtype, from_mmodule, true) format.add return_mtype.jni_format else format.add "V" end