# Compile C code to call JNI and link C callbacks implementations to Java extern methods
private fun ensure_linking_callback_methods(ccu: CCompilationUnit)
do
var callbacks = callbacks_used_from_java.callbacks
if callbacks.is_empty then
ccu.body_decl.add "static int nit_ffi_with_java_registered_natives = 1;\n"
return
end
ccu.body_decl.add "static int nit_ffi_with_java_registered_natives = 0;\n"
var jni_methods = new Array[String]
for cb in callbacks do
jni_methods.add_all(cb.jni_methods_declaration(self))
end
for cb in callbacks_used_from_java.types do
jni_methods.add_all(cb.jni_methods_declaration(self))
end
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;
jint n_methods = {{{jni_methods.length}}};
JNINativeMethod methods[] = {
{{{jni_methods.join(",\n\t\t")}}}
};
jint res = (*env)->RegisterNatives(env, jclazz, methods, n_methods);
if (res != JNI_OK) {
PRINT_ERROR("RegisterNatives failed\\n");
(*env)->ExceptionDescribe(env);
exit(1);
}
"""
ccu.add_local_function cf
end
src/ffi/java.nit:269,2--304,4