Somewhat redundant changes (new file_ext, arg parse, etc)
[ldk-java] / java_strings.py
1 from bindingstypes import *
2
3 from language_constants import LanguageConstants
4
5 class Consts(LanguageConstants):
6     def __init__(self, DEBUG):
7         super().__init__()
8
9         self.file_extension = 'java'
10
11         self.common_base = """package org.ldk.structs;
12 import java.util.LinkedList;
13 class CommonBase {
14         long ptr;
15         LinkedList<Object> ptrs_to = new LinkedList();
16         protected CommonBase(long ptr) { this.ptr = ptr; }
17         public long _test_only_get_ptr() { return this.ptr; }
18 }
19 """
20
21         self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
22 #include <rust_types.h>
23 #include <lightning.h>
24 #include <string.h>
25 #include <stdatomic.h>
26 #include <stdlib.h>
27 """
28
29         if not DEBUG:
30             self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
31 #define FREE(p) if ((long)(p) > 1024) { free(p); }
32 #define DO_ASSERT(a) (void)(a)
33 #define CHECK(a)
34 """
35         else:
36             self.c_file_pfx = self.c_file_pfx + """#include <assert.h>
37 // Always run a, then assert it is true:
38 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
39 // Assert a is true or do nothing
40 #define CHECK(a) DO_ASSERT(a)
41
42 // Running a leak check across all the allocations and frees of the JDK is a mess,
43 // so instead we implement our own naive leak checker here, relying on the -wrap
44 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
45 // and free'd in Rust or C across the generated bindings shared library.
46 #include <threads.h>
47 #include <execinfo.h>
48 #include <unistd.h>
49 static mtx_t allocation_mtx;
50
51 void __attribute__((constructor)) init_mtx() {
52         DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);
53 }
54
55 #define BT_MAX 128
56 typedef struct allocation {
57         struct allocation* next;
58         void* ptr;
59         const char* struct_name;
60         void* bt[BT_MAX];
61         int bt_len;
62 } allocation;
63 static allocation* allocation_ll = NULL;
64
65 void* __real_malloc(size_t len);
66 void* __real_calloc(size_t nmemb, size_t len);
67 static void new_allocation(void* res, const char* struct_name) {
68         allocation* new_alloc = __real_malloc(sizeof(allocation));
69         new_alloc->ptr = res;
70         new_alloc->struct_name = struct_name;
71         new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
72         DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
73         new_alloc->next = allocation_ll;
74         allocation_ll = new_alloc;
75         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
76 }
77 static void* MALLOC(size_t len, const char* struct_name) {
78         void* res = __real_malloc(len);
79         new_allocation(res, struct_name);
80         return res;
81 }
82 void __real_free(void* ptr);
83 static void alloc_freed(void* ptr) {
84         allocation* p = NULL;
85         DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
86         allocation* it = allocation_ll;
87         while (it->ptr != ptr) {
88                 p = it; it = it->next;
89                 if (it == NULL) {
90                         fprintf(stderr, "Tried to free unknown pointer %p at:\\n", ptr);
91                         void* bt[BT_MAX];
92                         int bt_len = backtrace(bt, BT_MAX);
93                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
94                         fprintf(stderr, "\\n\\n");
95                         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
96                         return; // addrsan should catch malloc-unknown and print more info than we have
97                 }
98         }
99         if (p) { p->next = it->next; } else { allocation_ll = it->next; }
100         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
101         DO_ASSERT(it->ptr == ptr);
102         __real_free(it);
103 }
104 static void FREE(void* ptr) {
105         if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
106         alloc_freed(ptr);
107         __real_free(ptr);
108 }
109
110 void* __wrap_malloc(size_t len) {
111         void* res = __real_malloc(len);
112         new_allocation(res, "malloc call");
113         return res;
114 }
115 void* __wrap_calloc(size_t nmemb, size_t len) {
116         void* res = __real_calloc(nmemb, len);
117         new_allocation(res, "calloc call");
118         return res;
119 }
120 void __wrap_free(void* ptr) {
121         if (ptr == NULL) return;
122         alloc_freed(ptr);
123         __real_free(ptr);
124 }
125
126 void* __real_realloc(void* ptr, size_t newlen);
127 void* __wrap_realloc(void* ptr, size_t len) {
128         if (ptr != NULL) alloc_freed(ptr);
129         void* res = __real_realloc(ptr, len);
130         new_allocation(res, "realloc call");
131         return res;
132 }
133 void __wrap_reallocarray(void* ptr, size_t new_sz) {
134         // Rust doesn't seem to use reallocarray currently
135         DO_ASSERT(false);
136 }
137
138 void __attribute__((destructor)) check_leaks() {
139         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
140                 fprintf(stderr, "%s %p remains:\\n", a->struct_name, a->ptr);
141                 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
142                 fprintf(stderr, "\\n\\n");
143         }
144         DO_ASSERT(allocation_ll == NULL);
145 }
146 """
147         self.c_file_pfx = self.c_file_pfx + """
148 static jmethodID ordinal_meth = NULL;
149 static jmethodID slicedef_meth = NULL;
150 static jclass slicedef_cls = NULL;
151 JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class, jclass slicedef_class) {
152         ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
153         CHECK(ordinal_meth != NULL);
154         slicedef_meth = (*env)->GetMethodID(env, slicedef_class, "<init>", "(JJJ)V");
155         CHECK(slicedef_meth != NULL);
156         slicedef_cls = (*env)->NewGlobalRef(env, slicedef_class);
157         CHECK(slicedef_cls != NULL);
158 }
159
160 JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_deref_1bool (JNIEnv * env, jclass _a, jlong ptr) {
161         return *((bool*)ptr);
162 }
163 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_deref_1long (JNIEnv * env, jclass _a, jlong ptr) {
164         return *((long*)ptr);
165 }
166 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_free_1heap_1ptr (JNIEnv * env, jclass _a, jlong ptr) {
167         FREE((void*)ptr);
168 }
169 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_read_1bytes (JNIEnv * env, jclass _b, jlong ptr, jlong len) {
170         jbyteArray ret_arr = (*env)->NewByteArray(env, len);
171         (*env)->SetByteArrayRegion(env, ret_arr, 0, len, (unsigned char*)ptr);
172         return ret_arr;
173 }
174 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (JNIEnv * env, jclass _b, jlong slice_ptr) {
175         LDKu8slice *slice = (LDKu8slice*)slice_ptr;
176         jbyteArray ret_arr = (*env)->NewByteArray(env, slice->datalen);
177         (*env)->SetByteArrayRegion(env, ret_arr, 0, slice->datalen, slice->data);
178         return ret_arr;
179 }
180 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * env, jclass _b, jbyteArray bytes) {
181         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
182         vec->datalen = (*env)->GetArrayLength(env, bytes);
183         vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
184         (*env)->GetByteArrayRegion (env, bytes, 0, vec->datalen, vec->data);
185         return (long)vec;
186 }
187 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_txpointer_1get_1buffer (JNIEnv * env, jclass _b, jlong ptr) {
188         LDKTransaction *txdata = (LDKTransaction*)ptr;
189         LDKu8slice slice;
190         slice.data = txdata->data;
191         slice.datalen = txdata->datalen;
192         return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (long)&slice);
193 }
194 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
195         LDKTransaction *txdata = (LDKTransaction*)MALLOC(sizeof(LDKTransaction), "LDKTransaction");
196         txdata->datalen = (*env)->GetArrayLength(env, bytes);
197         txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
198         txdata->data_is_owned = false;
199         (*env)->GetByteArrayRegion (env, bytes, 0, txdata->datalen, txdata->data);
200         return (long)txdata;
201 }
202 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_txpointer_1free (JNIEnv * env, jclass _b, jlong ptr) {
203         LDKTransaction *tx = (LDKTransaction*)ptr;
204         tx->data_is_owned = true;
205         Transaction_free(*tx);
206         FREE((void*)ptr);
207 }
208 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) {
209         // Check offsets of a few Vec types are all consistent as we're meant to be generic across types
210         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically");
211         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically");
212         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
213         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
214         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
215         return (long)vec->datalen;
216 }
217 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * env, jclass _b) {
218         // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
219         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
220         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
221         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically");
222         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically");
223         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
224         vec->data = NULL;
225         vec->datalen = 0;
226         return (long)vec;
227 }
228
229 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
230 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
231 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
232 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
233
234 _Static_assert(sizeof(jlong) == sizeof(int64_t), "We assume that j-types are the same as C types");
235 _Static_assert(sizeof(jbyte) == sizeof(char), "We assume that j-types are the same as C types");
236 _Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
237
238 typedef jlongArray int64_tArray;
239 typedef jbyteArray int8_tArray;
240
241 """
242
243         self.hu_struct_file_prefix = """package org.ldk.structs;
244
245 import org.ldk.impl.bindings;
246 import org.ldk.enums.*;
247 import org.ldk.util.*;
248 import java.util.Arrays;
249
250 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
251 """
252         self.c_fn_ty_pfx = "JNIEXPORT "
253         self.c_fn_name_pfx = "JNICALL Java_org_ldk_impl_bindings_"
254         self.c_fn_args_pfx = "JNIEnv *env, jclass clz"
255         self.file_ext = ".java"
256         self.ptr_c_ty = "int64_t"
257         self.ptr_native_ty = "long"
258         self.result_c_ty = "jclass"
259         self.ptr_arr = "jobjectArray"
260         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
261         self.get_native_arr_ptr_call = ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
262
263     def release_native_arr_ptr_call(self, arr_var, arr_ptr_var):
264         return "(*env)->ReleasePrimitiveArrayCritical(env, " + arr_var + ", " + arr_ptr_var + ", 0)"
265     def create_native_arr_call(self, arr_len, ty_info):
266         if ty_info.c_ty == "int8_tArray":
267             return "(*env)->NewByteArray(env, " + arr_len + ")"
268         else:
269             return "(*env)->New" + ty_info.java_ty.strip("[]").title() + "Array(env, " + arr_len + ")"
270     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
271         if ty_info.c_ty == "int8_tArray":
272             return ("(*env)->SetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
273         else:
274             assert False
275     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
276         if ty_info.c_ty == "int8_tArray":
277             if copy:
278                 return "(*env)->GetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", " + dest_name + ")"
279             else:
280                 return "(*env)->GetByteArrayElements (env, " + arr_name + ", NULL)"
281         elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
282             return "(*env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (env, " + arr_name + ", NULL)"
283         else:
284             return None
285     def get_native_arr_elem(self, arr_name, idxc, ty_info):
286         if self.get_native_arr_contents(arr_name, "", "", ty_info, False) is None:
287             return "(*env)->GetObjectArrayElement(env, " + arr_name + ", " + idxc + ")"
288         else:
289             assert False # Only called if above is None
290     def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
291         if ty_info.c_ty == "int8_tArray":
292             return "(*env)->ReleaseByteArrayElements(env, " + arr_name + ", (int8_t*)" + dest_name + ", 0);"
293         else:
294             return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)"
295
296     def init_str(self, c_array_class_caches):
297         res = ""
298         for ty in c_array_class_caches:
299             res = res + "static jclass " + ty + "_clz = NULL;\n"
300         res = res + "JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass clz) {\n"
301         for ty in c_array_class_caches:
302             res = res + "\t" + ty + "_clz = (*env)->FindClass(env, \"" + ty.replace("arr_of_", "[") + "\");\n"
303             res = res + "\tCHECK(" + ty + "_clz != NULL);\n"
304             res = res + "\t" + ty + "_clz = (*env)->NewGlobalRef(env, " + ty + "_clz);\n"
305         res = res + "}\n"
306         return res
307
308     def native_c_unitary_enum_map(self, struct_name, variants):
309         out_java_enum = "package org.ldk.enums;\n\n"
310         out_java = ""
311         out_c = ""
312         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_from_java(" + self.c_fn_args_pfx + ") {\n"
313         out_c = out_c + "\tswitch ((*env)->CallIntMethod(env, clz, ordinal_meth)) {\n"
314
315         out_java_enum = out_java_enum + "public enum " + struct_name + " {\n"
316         ord_v = 0
317         for var in variants:
318             out_java_enum = out_java_enum + "\t" + var + ",\n"
319             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
320             ord_v = ord_v + 1
321         out_java_enum = out_java_enum + "\t; static native void init();\n"
322         out_java_enum = out_java_enum + "\tstatic { init(); }\n"
323         out_java_enum = out_java_enum + "}"
324         out_java = out_java + "\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n"
325         out_c = out_c + "\t}\n"
326         out_c = out_c + "\tabort();\n"
327         out_c = out_c + "}\n"
328
329         out_c = out_c + "static jclass " + struct_name + "_class = NULL;\n"
330         for var in variants:
331             out_c = out_c + "static jfieldID " + struct_name + "_" + var + " = NULL;\n"
332         out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
333         out_c = out_c + "\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n"
334         out_c = out_c + "\tCHECK(" + struct_name + "_class != NULL);\n"
335         for var in variants:
336             out_c = out_c + "\t" + struct_name + "_" + var + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + var + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n"
337             out_c = out_c + "\tCHECK(" + struct_name + "_" + var + " != NULL);\n"
338         out_c = out_c + "}\n"
339         out_c = out_c + "static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n"
340         out_c = out_c + "\tswitch (val) {\n"
341         ord_v = 0
342         for var in variants:
343             out_c = out_c + "\t\tcase " + var + ":\n"
344             out_c = out_c + "\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + var + ");\n"
345             ord_v = ord_v + 1
346         out_c = out_c + "\t\tdefault: abort();\n"
347         out_c = out_c + "\t}\n"
348         out_c = out_c + "}\n\n"
349
350         return (out_c, out_java_enum, out_java)
351
352     def c_unitary_enum_to_native_call(self, ty_info):
353         return (ty_info.rust_obj + "_to_java(env, ", ")")
354     def native_unitary_enum_to_c_call(self, ty_info):
355         return (ty_info.rust_obj + "_from_java(env, ", ")")
356
357     def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
358         out_c = ""
359         for var in variants:
360             out_c = out_c + "static jclass " + struct_name + "_" + var + "_class = NULL;\n"
361             out_c = out_c + "static jmethodID " + struct_name + "_" + var + "_meth = NULL;\n"
362         out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
363         for var_name in variants:
364             out_c = out_c + "\t" + struct_name + "_" + var_name + "_class =\n"
365             out_c = out_c + "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n"
366             out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
367             out_c = out_c + "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
368             out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
369         out_c = out_c + "}\n"
370         return out_c
371
372     def c_complex_enum_pass_ty(self, struct_name):
373         return "jobject"
374
375     def c_constr_native_complex_enum(self, struct_name, variant, c_params):
376         ret = "(*env)->NewObject(env, " + struct_name + "_" + variant + "_class, " + struct_name + "_" + variant + "_meth"
377         for param in c_params:
378             ret = ret + ", " + param
379         return ret + ")"
380
381     def native_c_map_trait(self, struct_name, field_vars, field_fns):
382         out_c = "typedef struct " + struct_name + "_JCalls {\n"
383         out_c = out_c + "\tatomic_size_t refcnt;\n"
384         out_c = out_c + "\tJavaVM *vm;\n"
385         out_c = out_c + "\tjweak o;\n"
386         for var in field_vars:
387             if isinstance(var, ConvInfo):
388                 # We're a regular ol' field
389                 pass
390             else:
391                 # We're a supertrait
392                 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
393         for fn in field_fns:
394             if fn.fn_name != "free" and fn.fn_name != "clone":
395                 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
396         out_c = out_c + "} " + struct_name + "_JCalls;\n"
397
398         for fn_line in field_fns:
399             if fn_line.fn_name == "free":
400                 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
401                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
402                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
403                 out_c = out_c + "\t\tJNIEnv *env;\n"
404                 out_c = out_c + "\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
405                 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
406                 out_c = out_c + "\t\tFREE(j_calls);\n"
407                 out_c = out_c + "\t}\n}\n"
408
409         for fn_line in field_fns:
410             if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
411                 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
412                 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_jcall("
413                 if fn_line.self_is_const:
414                     out_c = out_c + "const void* this_arg"
415                 else:
416                     out_c = out_c + "void* this_arg"
417
418                 for idx, arg in enumerate(fn_line.args_ty):
419                     out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
420
421                 out_c = out_c + ") {\n"
422                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
423                 out_c = out_c + "\tJNIEnv *env;\n"
424                 out_c = out_c + "\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
425
426                 for arg_info in fn_line.args_ty:
427                     if arg_info.ret_conv is not None:
428                         out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
429                         out_c = out_c + arg_info.arg_name
430                         out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
431
432                 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
433                 if fn_line.ret_ty_info.c_ty.endswith("Array"):
434                     out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " arg = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
435                 elif not fn_line.ret_ty_info.passed_as_ptr:
436                     out_c = out_c + "\treturn (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
437                 else:
438                     out_c = out_c + "\t" + fn_line.ret_ty_info.rust_obj + "* ret = (" + fn_line.ret_ty_info.rust_obj + "*)(*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
439
440                 for idx, arg_info in enumerate(fn_line.args_ty):
441                     if arg_info.ret_conv is not None:
442                         out_c = out_c + ", " + arg_info.ret_conv_name
443                     else:
444                         out_c = out_c + ", " + arg_info.arg_name
445                 out_c = out_c + ");\n"
446                 if fn_line.ret_ty_info.arg_conv is not None:
447                     out_c = out_c + "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
448
449                 out_c = out_c + "}\n"
450
451         return ("", out_c)