nitc :: JavaLanguage
nitc :: JavaLanguage :: _ffi_ccu
nitc :: JavaLanguage :: defaultinit
nitc :: JavaLanguage :: ffi_ccu
nitc :: JavaLanguage :: ffi_ccu=
nitc $ JavaLanguage :: SELF
Type of this instance, automatically specialized in every classnitc $ JavaLanguage :: compile_callback
Generate the code to offer this callback if foreign codenitc $ JavaLanguage :: compile_extern_class
Generate wrapper code for this extern classnitc $ JavaLanguage :: compile_extern_method
Generate wrapper code for this extern methodnitc $ JavaLanguage :: compile_module_block
Generate wrapper code for this module/header code blocknitc $ JavaLanguage :: compile_to_files
Complete compilation of generated codenitc :: extra_java_files $ JavaLanguage :: compile_to_files
Complete compilation of generated codenitc $ JavaLanguage :: get_ftype
Get the foreign type of this extern class definitionnitc $ JavaLanguage :: identify_language
Is thisblock
written in this language?
nitc :: JavaLanguage :: _ffi_ccu
nitc :: FFILanguage :: _ffi_language_assignation_phase
FFILanguageAssignationPhase
assigning self
to AExternCodeBlock
s
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
nitc :: FFILanguage :: compile_callback
Generate the code to offer this callback if foreign codenitc :: FFILanguage :: compile_extern_class
Generate wrapper code for this extern classnitc :: FFILanguage :: compile_extern_method
Generate wrapper code for this extern methodnitc :: FFILanguage :: compile_module_block
Generate wrapper code for this module/header code blocknitc :: FFILanguage :: compile_to_files
Complete compilation of generated codecore :: Object :: defaultinit
nitc :: FFILanguage :: defaultinit
nitc :: JavaLanguage :: defaultinit
nitc :: JavaLanguage :: ffi_ccu
nitc :: JavaLanguage :: ffi_ccu=
nitc :: FFILanguage :: ffi_language_assignation_phase
FFILanguageAssignationPhase
assigning self
to AExternCodeBlock
s
nitc :: FFILanguage :: ffi_language_assignation_phase=
FFILanguageAssignationPhase
assigning self
to AExternCodeBlock
s
nitc :: FFILanguage :: get_ftype
Get the foreign type of this extern class definitionnitc :: FFILanguage :: identify_language
Is thisblock
written in this language?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).nitc :: FFILanguage
Visitor for a specific languages. Works kinda like aPhase
and is executed
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
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