Introduced properties

private var _ffi_ccu: nullable CCompilationUnit

nitc :: JavaLanguage :: _ffi_ccu

init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: JavaLanguage :: defaultinit

protected fun ffi_ccu=(ffi_ccu: nullable CCompilationUnit)

nitc :: JavaLanguage :: ffi_ccu=

Redefined properties

redef type SELF: JavaLanguage

nitc $ JavaLanguage :: SELF

Type of this instance, automatically specialized in every class
redef fun compile_callback(callback: NitniCallback, mmodule: MModule, mainmodule: MModule, ccu: CCompilationUnit)

nitc $ JavaLanguage :: compile_callback

Generate the code to offer this callback if foreign code
redef fun compile_extern_class(block: AExternCodeBlock, m: AClassdef, ccu: CCompilationUnit, mmodule: MModule)

nitc $ JavaLanguage :: compile_extern_class

Generate wrapper code for this extern class
redef fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef, ccu: CCompilationUnit, mmodule: MModule)

nitc $ JavaLanguage :: compile_extern_method

Generate wrapper code for this extern method
redef fun compile_module_block(block: AExternCodeBlock, ccu: CCompilationUnit, mmodule: MModule)

nitc $ JavaLanguage :: compile_module_block

Generate wrapper code for this module/header code block
redef fun compile_to_files(mmodule: MModule, compdir: String)

nitc $ JavaLanguage :: compile_to_files

Complete compilation of generated code
redef fun compile_to_files(mmodule: MModule, compdir: String)

nitc :: extra_java_files $ JavaLanguage :: compile_to_files

Complete compilation of generated code
redef fun get_ftype(block: AExternCodeBlock, m: AClassdef): ForeignType

nitc $ JavaLanguage :: get_ftype

Get the foreign type of this extern class definition
redef fun identify_language(n: AExternCodeBlock): Bool

nitc $ JavaLanguage :: identify_language

Is this block written in this language?

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _ffi_ccu: nullable CCompilationUnit

nitc :: JavaLanguage :: _ffi_ccu

private var _ffi_language_assignation_phase: FFILanguageAssignationPhase

nitc :: FFILanguage :: _ffi_language_assignation_phase

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun compile_callback(callback: NitniCallback, mmodule: MModule, mainmmodule: MModule, ecc: CCompilationUnit)

nitc :: FFILanguage :: compile_callback

Generate the code to offer this callback if foreign code
abstract fun compile_extern_class(block: AExternCodeBlock, m: AClassdef, ecc: CCompilationUnit, mmodule: MModule)

nitc :: FFILanguage :: compile_extern_class

Generate wrapper code for this extern class
abstract fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef, ecc: CCompilationUnit, nmodule: MModule)

nitc :: FFILanguage :: compile_extern_method

Generate wrapper code for this extern method
abstract fun compile_module_block(block: AExternCodeBlock, ecc: CCompilationUnit, mmodule: MModule)

nitc :: FFILanguage :: compile_module_block

Generate wrapper code for this module/header code block
fun compile_to_files(mmodule: MModule, directory: String)

nitc :: FFILanguage :: compile_to_files

Complete compilation of generated code
init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: FFILanguage :: defaultinit

init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: JavaLanguage :: defaultinit

protected fun ffi_ccu=(ffi_ccu: nullable CCompilationUnit)

nitc :: JavaLanguage :: ffi_ccu=

fun ffi_language_assignation_phase: FFILanguageAssignationPhase

nitc :: FFILanguage :: ffi_language_assignation_phase

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
protected fun ffi_language_assignation_phase=(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: FFILanguage :: ffi_language_assignation_phase=

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
abstract fun get_ftype(block: AExternCodeBlock, m: AClassdef): ForeignType

nitc :: FFILanguage :: get_ftype

Get the foreign type of this extern class definition
fun hash: Int

core :: Object :: hash

The hash code of the object.
abstract fun identify_language(block: AExternCodeBlock): Bool

nitc :: FFILanguage :: identify_language

Is this block written in this language?
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram nitc::JavaLanguage JavaLanguage nitc::FFILanguage FFILanguage nitc::JavaLanguage->nitc::FFILanguage core::Object Object nitc::FFILanguage->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

class FFILanguage

nitc :: FFILanguage

Visitor for a specific languages. Works kinda like a Phase and is executed

Class definitions

nitc $ JavaLanguage
class JavaLanguage
	super FFILanguage

	redef fun identify_language(n) do return n.is_java

	redef fun compile_module_block(block, ccu, mmodule)
	do
		mmodule.ensure_java_files
		var java_file = mmodule.java_file
		assert java_file != null

		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, mmodule)
	do
		ffi_ccu = ccu
		mmodule.ensure_java_files
		var java_file = mmodule.java_file
		assert java_file != null

		var mclass_type = m.parent.as(AClassdef).mclass.mclass_type
		var mmethodef = m.mpropdef
		var mproperty = m.mpropdef.mproperty

		# C function calling the Java method through JNI
		var fc = new ExternCFunction(m, mmodule)

		fc.exprs.add """
	jclass java_class;
	jmethodID java_meth_id;

	// retrieve the current JVM
	Sys sys = Pointer_sys(NULL);
	JNIEnv *nit_ffi_jni_env = Sys_jni_env(sys);

	// retrieve the implementation Java class
	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);
"""

		# Retrieve the Java implementation function id
		var java_fun_name = mproperty.build_cname(mclass_type, mmodule, "___java_impl", long_signature)
		var jni_format = mproperty.build_jni_format(mclass_type, mmodule)
		fc.exprs.add """
	// 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) {
		PRINT_ERROR("Nit FFI with Java error: Java implementation not found.\\n");
		(*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env);
		exit(1);
	}
"""

		# Call the C Java implementation method from C
		var signature = mmethodef.msignature
		assert signature != null

		var jni_signature_alt
		var return_type
		var params = new Array[String]
		params.add "nit_ffi_jni_env"
		params.add "java_class"
		params.add "java_meth_id"

		if mproperty.is_init then
			jni_signature_alt = mclass_type.jni_signature_alt
			return_type = mclass_type
		else
			params.add to_java_call_context.cast_to(mclass_type, "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)
				return_type = signature.return_mtype
				jni_signature_alt = return_type.jni_signature_alt
			else
				jni_signature_alt = "Void"
				return_type = null
			end
		end

		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
		if return_type != null then
			ccall = "{return_type.jni_type} jni_res = {cname}({params.join(", ")});"
		else ccall = "{cname}({params.join(", ")});"

		fc.exprs.add """
	// execute implementation code
	{{{ccall}}}
	if ((*nit_ffi_jni_env)->ExceptionCheck(nit_ffi_jni_env)) {
		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
			fc.exprs.add "\treturn {to_java_call_context.cast_from(return_type, "jni_res")};"
		end

		ccu.add_exported_function( fc )

		# Java implementation function in Java
		var java_csig = mproperty.build_csignature(mclass_type, mmodule, "___java_impl", long_signature, java_call_context)
		mmodule.java_file.class_content.add """
	public static {{{java_csig}}} {
		// from Nit FII at: {{{block.location}}}
		{{{block.code}}}
	}
"""

		mmodule.callbacks_used_from_java.join m.foreign_callbacks
	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(mmodule, compdir)
	do
		var ffi_ccu = ffi_ccu
		assert ffi_ccu != null

		# Make sure we have a .java file
		mmodule.ensure_java_files

		# Needed compiler and linker options
		mmodule.insert_compiler_options

		# Enable linking C callbacks to java native methods
		mmodule.ensure_linking_callback_methods(ffi_ccu)

		# Function to build instances to the Java class NitObject
		var callbacks = mmodule.callbacks_used_from_java.callbacks
		if callbacks.not_empty then
			var cf = new CFunction("jobject nit_ffi_with_java_new_nit_object(JNIEnv *env, void *data)")
			cf.exprs.add """
	// retrieve the current JVM
	Sys sys = Pointer_sys(NULL);

	jclass java_class = Sys_load_jclass(sys, "nit/app/NitObject");
	if (java_class == NULL) {
		PRINT_ERROR("Nit FFI with Java error: failed to load class NitObject.\\n");
		(*env)->ExceptionDescribe(env);
		exit(1);
	}

	jmethodID java_init = (*env)->GetMethodID(env, java_class, "<init>", "(J)V");
	if (java_init == NULL) {
		PRINT_ERROR("Nit FFI with Java error: NitObject constructor not found.\\n");
		(*env)->ExceptionDescribe(env);
		exit(1);
	}

	jobject nit_object = (*env)->NewObject(env, java_class, java_init, (jlong)data);
	if (nit_object == NULL) {
		PRINT_ERROR("Nit FFI with Java error: NitObject construction failed.\\n");
		(*env)->ExceptionDescribe(env);
		exit(1);
	}

	return nit_object;
	"""
			ffi_ccu.add_local_function cf

			# Function to extract the pointer held by instances of the Java class NitObject
			cf = new CFunction("void *nit_ffi_with_java_nit_object_data(JNIEnv *env, jobject nit_object)")
			cf.exprs.add """
	Sys sys = Pointer_sys(NULL);
	jclass java_class = Sys_load_jclass(sys, "nit/app/NitObject");
	if (java_class == NULL) {
		PRINT_ERROR("Nit FFI with Java error: failed to load class NitObject.\\n");
		(*env)->ExceptionDescribe(env);
		exit(1);
	}

	jfieldID java_field = (*env)->GetFieldID(env, java_class, "pointer", "J");
	if (java_field == NULL) {
		PRINT_ERROR("Nit FFI with Java error: NitObject field not found.\\n");
		(*env)->ExceptionDescribe(env);
		exit(1);
	}

	jlong data = (*env)->GetLongField(env, nit_object, java_field);

	return (void*)data;
	"""
			ffi_ccu.add_local_function cf
		end

		# Java implementation code
		var java_file = mmodule.java_file
		assert java_file != null
		var extern_java_file = java_file.write_to_files(compdir)
		mmodule.ffi_files.add(extern_java_file)
	end

	var ffi_ccu: nullable CCompilationUnit = null # HACK

	redef fun compile_callback(callback, mmodule, mainmodule, ccu)
	do
		ffi_ccu = ccu
		callback.compile_callback_to_java(mmodule, mainmodule, ccu)
	end
end
src/ffi/java.nit:30,1--252,3

nitc :: extra_java_files $ JavaLanguage
redef class JavaLanguage
	redef fun compile_to_files(mmodule, compdir)
	do
		super

		# Also copy over the extra Java files
		var extra_java_files = mmodule.extra_java_files
		if extra_java_files != null then for file in extra_java_files do

			var dir = compdir / file.filename.dirname
			dir.mkdir

			file.src_path.file_copy_to(compdir/file.filename)
		end
	end
end
src/ffi/extra_java_files.nit:93,1--108,3