# Generate wrapper code for this extern method
fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef,
ecc: CCompilationUnit, nmodule: MModule) is abstract
src/ffi/light_ffi_base.nit:123,2--125,54
# We call C++ from C using 2 more files (_ffi.c and _ffi.cpp) and multiple generated functions:
# 1. The standard C implementation function (___impl) expected by the common FFI
# 2. The indirection function (___cpp_impl_mid) is a C function, called from C but implemented as `extern "C"` in C++
# 3. The actual C++ implementation function (___cpp_impl)
redef fun compile_extern_method(block, m, ecc, mmodule)
do
if mmodule.cpp_file == null then mmodule.cpp_file = new CPPCompilationUnit
var mclass_type = m.parent.as(AClassdef).mclass.mclass_type
var mproperty = m.mpropdef.mproperty
# Signature of the indirection function implemented as `extern "C"` in C++
var indirection_sig = mproperty.build_csignature(mclass_type, mmodule, "___cpp_impl_mid", long_signature, internal_call_context)
## In C file (__ffi.c)
# Declare the indirection function in C
ecc.body_decl.add("{indirection_sig};\n")
# Call the indirection function from C (___impl)
var fc: CFunction = new ExternCFunction(m, mmodule)
fc.exprs.add(mproperty.build_ccall(mclass_type, mmodule, "___cpp_impl_mid", long_signature, cpp_call_context, null))
fc.exprs.add("\n")
ecc.add_exported_function( fc )
## In C++ file (__ffi.cpp)
# Declare the indirection function in C++
mmodule.cpp_file.header_decl.add("extern \"C\" \{\n")
mmodule.cpp_file.header_decl.add("{indirection_sig};\n")
mmodule.cpp_file.header_decl.add("\}\n")
# Implement the indirection function as extern in C++
# Will convert C arguments to C++ and call the C++ implementation function.
fc = new CFunction(indirection_sig)
if not mproperty.is_init then
var param_name = "self"
var type_name = to_cpp_call_context.name_mtype(mclass_type)
if mclass_type.mclass.ftype isa ForeignCppType then
fc.exprs.add("{type_name} {param_name}_for_cpp = static_cast<{type_name}>({param_name});\n")
else
fc.exprs.add("{type_name} {param_name}_for_cpp = {param_name};\n")
end
end
for param in m.mpropdef.msignature.mparameters do
var param_name = param.name
var type_name = to_cpp_call_context.name_mtype(param.mtype)
if mclass_type.mclass.ftype isa ForeignCppType then
fc.exprs.add("{type_name} {param_name}_for_cpp = static_cast<{type_name}>({param_name});\n")
else
fc.exprs.add("{type_name} {param_name}_for_cpp = {param_name};\n")
end
end
fc.exprs.add(mproperty.build_ccall(mclass_type, mmodule, "___cpp_impl", long_signature, cpp_call_context, "_for_cpp"))
fc.exprs.add("\n")
mmodule.cpp_file.add_exported_function(fc)
# Custom C++, the body of the Nit C++ method is copied to its own C++ function
var cpp_signature = mproperty.build_csignature(mclass_type, mmodule, "___cpp_impl", long_signature, cpp_call_context)
fc = new CFunction(cpp_signature)
fc.decls.add( block.location.as_line_pragma )
fc.exprs.add( block.code )
mmodule.cpp_file.add_local_function( fc )
end
src/ffi/cpp.nit:52,2--115,4
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
src/ffi/java.nit:46,2--160,4
redef fun compile_extern_method(block, m, ecc, mmodule)
do
if mmodule.objc_file == null then mmodule.objc_file = new ObjCCompilationUnit
var mpropdef = m.mpropdef
var recv_mtype = mpropdef.mclassdef.bound_mtype
var csignature = mpropdef.mproperty.build_csignature(
recv_mtype, mmodule, "___impl", long_signature, from_objc_call_context)
var fc = new CFunction(csignature)
fc.decls.add block.location.as_line_pragma
fc.exprs.add block.code
mmodule.objc_file.add_exported_function fc
end
src/ffi/objc.nit:79,2--92,4