5 print("USAGE: /path/to/lightning.h /path/to/bindings/output.java /path/to/bindings/output.c debug")
6 print("debug should be true or false and indicates whether to track allocations and ensure we don't leak")
10 def __init__(self, rust_obj, java_ty, java_fn_ty_arg, c_ty, passed_as_ptr, is_ptr, var_name, arr_len):
11 self.rust_obj = rust_obj
12 self.java_ty = java_ty
13 self.java_fn_ty_arg = java_fn_ty_arg
15 self.passed_as_ptr = passed_as_ptr
17 self.var_name = var_name
18 self.arr_len = arr_len
21 def __init__(self, ty_info, arg_name, arg_conv, arg_conv_name, ret_conv, ret_conv_name):
22 assert(ty_info.c_ty is not None)
23 assert(ty_info.java_ty is not None)
24 assert(arg_name is not None)
25 self.c_ty = ty_info.c_ty
26 self.java_ty = ty_info.java_ty
27 self.java_fn_ty_arg = ty_info.java_fn_ty_arg
28 self.arg_name = arg_name
29 self.arg_conv = arg_conv
30 self.arg_conv_name = arg_conv_name
31 self.ret_conv = ret_conv
32 self.ret_conv_name = ret_conv_name
35 out_c.write(self.c_ty)
36 out_java.write(self.java_ty)
39 if self.arg_name != "":
40 out_java.write(" " + self.arg_name)
41 out_c.write(" " + self.arg_name)
43 out_java.write(" arg")
46 with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.argv[3], "w") as out_c:
47 opaque_structs = set()
50 var_is_arr_regex = re.compile("\(\*([A-za-z_]*)\)\[([0-9]*)\]")
51 var_ty_regex = re.compile("([A-za-z_0-9]*)(.*)")
52 def java_c_types(fn_arg, ret_arr_len):
53 fn_arg = fn_arg.strip()
54 if fn_arg.startswith("MUST_USE_RES "):
57 if fn_arg.startswith("const "):
64 if fn_arg.startswith("void"):
68 fn_arg = fn_arg[4:].strip()
69 elif fn_arg.startswith("bool"):
73 fn_arg = fn_arg[4:].strip()
74 elif fn_arg.startswith("uint8_t"):
78 fn_arg = fn_arg[7:].strip()
79 elif fn_arg.startswith("uint16_t"):
83 fn_arg = fn_arg[8:].strip()
84 elif fn_arg.startswith("uint32_t"):
88 fn_arg = fn_arg[8:].strip()
89 elif fn_arg.startswith("uint64_t"):
93 fn_arg = fn_arg[8:].strip()
94 elif is_const and fn_arg.startswith("char *"):
97 fn_ty_arg = "Ljava/lang/String;"
98 fn_arg = fn_arg[6:].strip()
100 ma = var_ty_regex.match(fn_arg)
104 fn_arg = ma.group(2).strip()
105 rust_obj = ma.group(1).strip()
108 if fn_arg.startswith(" *") or fn_arg.startswith("*"):
109 fn_arg = fn_arg.replace("*", "").strip()
114 var_is_arr = var_is_arr_regex.match(fn_arg)
115 if var_is_arr is not None or ret_arr_len is not None:
116 assert(not take_by_ptr)
118 java_ty = java_ty + "[]"
119 c_ty = c_ty + "Array"
120 if var_is_arr is not None:
121 return TypeInfo(rust_obj=None, java_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
122 passed_as_ptr=False, is_ptr=False, var_name=var_is_arr.group(1), arr_len=var_is_arr.group(2))
123 return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
124 is_ptr=is_ptr, var_name=fn_arg, arr_len=None)
126 def map_type(fn_arg, print_void, ret_arr_len, is_free):
127 ty_info = java_c_types(fn_arg, ret_arr_len)
129 if ty_info.c_ty == "void":
131 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
132 arg_conv = None, arg_conv_name = None, ret_conv = None, ret_conv_name = None)
134 if ty_info.c_ty.endswith("Array"):
135 arr_len = ty_info.arr_len
136 if arr_len is not None:
137 arr_name = ty_info.var_name
140 arr_len = ret_arr_len
141 assert(ty_info.c_ty == "jbyteArray")
142 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
143 arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n" +
144 "(*_env)->GetByteArrayRegion (_env, """ + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" +
145 "unsigned char (*""" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;",
146 arg_conv_name = arr_name + "_ref",
147 ret_conv = ("jbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" +
148 "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", *",
150 ret_conv_name = arr_name + "_arr")
151 elif ty_info.var_name != "":
152 # If we have a parameter name, print it (noting that it may indicate its a pointer)
153 if ty_info.rust_obj is not None:
154 assert(ty_info.passed_as_ptr)
155 if not ty_info.is_ptr:
156 base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";";
157 if ty_info.rust_obj in trait_structs:
159 base_conv = base_conv + "\n" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);"
161 base_conv = base_conv + "\n" + "FREE((void*)" + ty_info.var_name + ");"
162 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
163 arg_conv = base_conv,
164 arg_conv_name = ty_info.var_name + "_conv",
165 ret_conv = None, ret_conv_name = None)
166 base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");";
167 if ty_info.rust_obj in opaque_structs:
168 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
169 arg_conv = base_conv + "\n" + ty_info.var_name + "_conv._underlying_ref = false;",
170 arg_conv_name = ty_info.var_name + "_conv",
171 ret_conv = None, ret_conv_name = None)
173 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
174 arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv",
175 ret_conv = None, ret_conv_name = None)
178 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
179 arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
180 arg_conv_name = ty_info.var_name + "_conv",
181 ret_conv = None, ret_conv_name = None)
183 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
184 arg_conv = None, arg_conv_name = ty_info.var_name, ret_conv = None, ret_conv_name = None)
185 elif ty_info.java_ty == "String":
186 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
187 arg_conv = None, arg_conv_name = None,
188 ret_conv = ("jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv")
190 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
191 arg_conv = None, arg_conv_name = ty_info.var_name, ret_conv = None, ret_conv_name = None)
193 # We don't have a parameter name, and want one, just call it arg
194 if ty_info.rust_obj is not None:
195 assert(not is_free or ty_info.rust_obj not in opaque_structs);
196 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
197 arg_conv = ty_info.rust_obj + " arg_conv = *(" + ty_info.rust_obj + "*)arg;\nFREE((void*)arg);",
198 arg_conv_name = "arg_conv",
199 ret_conv = None, ret_conv_name = None)
202 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
203 arg_conv = None, arg_conv_name = "arg", ret_conv = None, ret_conv_name = None)
205 # We don't have a parameter name, and don't want one (cause we're returning)
206 if ty_info.rust_obj is not None:
207 if not ty_info.is_ptr:
208 if ty_info.rust_obj in opaque_structs:
209 # If we're returning a newly-allocated struct, we don't want Rust to ever
210 # free, instead relying on the Java GC to lose the ref. We undo this in
211 # any _free function.
212 # To avoid any issues, we first assert that the incoming object is non-ref.
213 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
214 ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";\nassert(!ret->_underlying_ref);\nret->_underlying_ref = true;"),
215 ret_conv_name = "(long)ret",
216 arg_conv = None, arg_conv_name = None)
218 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
219 ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"),
220 ret_conv_name = "(long)ret",
221 arg_conv = None, arg_conv_name = None)
223 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
224 ret_conv = ("long ret = (long)", ";"), ret_conv_name = "ret",
225 arg_conv = None, arg_conv_name = None)
227 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
228 arg_conv = None, arg_conv_name = None, ret_conv = None, ret_conv_name = None)
230 def map_fn(re_match, ret_arr_len):
231 out_java.write("\t/// " + line)
232 out_java.write("\tpublic static native ")
233 out_c.write("JNIEXPORT ")
235 ret_info = map_type(re_match.group(1), True, ret_arr_len, False)
237 if ret_info.ret_conv is not None:
238 ret_conv_pfx, ret_conv_sfx = ret_info.ret_conv
240 out_java.write(" " + re_match.group(2) + "(")
241 out_c.write(" JNICALL Java_org_ldk_impl_bindings_" + re_match.group(2).replace('_', '_1') + "(JNIEnv * _env, jclass _b")
244 for idx, arg in enumerate(re_match.group(3).split(',')):
249 arg_conv_info = map_type(arg, False, None, re_match.group(2).endswith("_free"))
250 if arg_conv_info.c_ty != "void":
251 arg_conv_info.print_ty()
252 arg_conv_info.print_name()
253 arg_names.append(arg_conv_info)
255 out_java.write(");\n")
258 for info in arg_names:
259 if info.arg_conv is not None:
260 out_c.write("\t" + info.arg_conv.replace('\n', "\n\t") + "\n");
262 if ret_info.ret_conv is not None:
263 out_c.write("\t" + ret_conv_pfx.replace('\n', '\n\t'));
265 out_c.write("\treturn ");
267 out_c.write(re_match.group(2) + "(")
268 for idx, info in enumerate(arg_names):
269 if info.arg_conv_name is not None:
272 out_c.write(info.arg_conv_name)
274 if ret_info.ret_conv is not None:
275 out_c.write(ret_conv_sfx.replace('\n', '\n\t'))
276 out_c.write("\n\treturn " + ret_info.ret_conv_name + ";")
279 out_c.write("\n}\n\n")
281 out_java.write("""package org.ldk.impl;
283 public class bindings {
285 System.loadLibrary(\"lightningjni\");
289 out_c.write("#include \"org_ldk_impl_bindings.h\"\n")
290 out_c.write("#include <rust_types.h>\n")
291 out_c.write("#include <lightning.h>\n")
292 out_c.write("#include <assert.h>\n")
293 out_c.write("#include <string.h>\n")
294 out_c.write("#include <stdatomic.h>\n\n")
295 if sys.argv[4] == "false":
296 out_c.write("#define MALLOC(a, _) malloc(a)\n")
297 out_c.write("#define FREE free\n\n")
299 out_c.write("#include <threads.h>\n")
300 out_c.write("static mtx_t allocation_mtx;\n\n")
301 out_c.write("void __attribute__((constructor)) init_mtx() {\n")
302 out_c.write("\tassert(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);\n")
304 out_c.write("typedef struct allocation {\n")
305 out_c.write("\tstruct allocation* next;\n")
306 out_c.write("\tvoid* ptr;\n")
307 out_c.write("\tconst char* struct_name;\n")
308 out_c.write("} allocation;\n")
309 out_c.write("static allocation* allocation_ll = NULL;\n\n")
310 out_c.write("void* MALLOC(size_t len, const char* struct_name) {\n")
311 out_c.write("\tvoid* res = malloc(len);\n")
312 out_c.write("\tallocation* new_alloc = malloc(sizeof(allocation));\n")
313 out_c.write("\tnew_alloc->ptr = res;\n")
314 out_c.write("\tnew_alloc->struct_name = struct_name;\n")
315 out_c.write("\tassert(mtx_lock(&allocation_mtx) == thrd_success);\n")
316 out_c.write("\tnew_alloc->next = allocation_ll;\n")
317 out_c.write("\tallocation_ll = new_alloc;\n")
318 out_c.write("\tassert(mtx_unlock(&allocation_mtx) == thrd_success);\n")
319 out_c.write("\treturn res;\n")
321 out_c.write("void FREE(void* ptr) {\n")
322 out_c.write("\tallocation* p = NULL;\n")
323 out_c.write("\tassert(mtx_lock(&allocation_mtx) == thrd_success);\n")
324 out_c.write("\tallocation* it = allocation_ll;\n")
325 out_c.write("\twhile (it->ptr != ptr) { p = it; it = it->next; }\n")
326 out_c.write("\tif (p) { p->next = it->next; } else { allocation_ll = it->next; }\n")
327 out_c.write("\tassert(mtx_unlock(&allocation_mtx) == thrd_success);\n")
328 out_c.write("\tassert(it->ptr == ptr);\n")
329 out_c.write("\tfree(it);\n")
330 out_c.write("\tfree(ptr);\n")
332 out_c.write("void __attribute__((destructor)) check_leaks() {\n")
333 out_c.write("\tfor (allocation* a = allocation_ll; a != NULL; a = a->next) { fprintf(stderr, \"%s %p remains\\n\", a->struct_name, a->ptr); }\n")
334 out_c.write("\tassert(allocation_ll == NULL);\n")
337 # XXX: Temporarily write out a manual SecretKey_new() for testing, we should auto-gen this kind of thing
338 out_java.write("\tpublic static native long LDKSecretKey_new();\n\n") # TODO: rm me
339 out_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_LDKSecretKey_1new(JNIEnv * _env, jclass _b) {\n") # TODO: rm me
340 out_c.write("\tLDKSecretKey* key = (LDKSecretKey*)MALLOC(sizeof(LDKSecretKey), \"LDKSecretKey\");\n") # TODO: rm me
341 out_c.write("\treturn (long)key;\n") # TODO: rm me
342 out_c.write("}\n") # TODO: rm me
344 in_block_comment = False
345 in_block_enum = False
346 cur_block_struct = None
347 in_block_union = False
349 fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
350 fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
351 reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
352 const_val_regex = re.compile("^extern const ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
354 line_indicates_opaque_regex = re.compile("^ bool _underlying_ref;$")
355 line_indicates_trait_regex = re.compile("^ ([A-Za-z_0-9]* \*?)\(\*([A-Za-z_0-9]*)\)\((const )?void \*this_arg(.*)\);$")
356 assert(line_indicates_trait_regex.match(" uintptr_t (*send_data)(void *this_arg, LDKu8slice data, bool resume_read);"))
357 assert(line_indicates_trait_regex.match(" LDKCVec_MessageSendEventZ (*get_and_clear_pending_msg_events)(const void *this_arg);"))
358 assert(line_indicates_trait_regex.match(" void *(*clone)(const void *this_arg);"))
359 line_field_var_regex = re.compile("^ ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
360 assert(line_field_var_regex.match(" LDKMessageSendEventsProvider MessageSendEventsProvider;"))
361 struct_name_regex = re.compile("^typedef struct (MUST_USE_STRUCT )?(LDK[A-Za-z_0-9]*) {$")
362 assert(struct_name_regex.match("typedef struct LDKCVecTempl_u8 {"))
366 #out_java.write("\t" + line)
367 if line.endswith("*/\n"):
368 in_block_comment = False
369 elif cur_block_struct is not None:
370 cur_block_struct = cur_block_struct + line
371 if line.startswith("} "):
374 struct_lines = cur_block_struct.split("\n")
379 for idx, struct_line in enumerate(struct_lines):
380 if struct_line.strip().startswith("/*"):
381 in_block_comment = True
383 if struct_line.endswith("*/"):
384 in_block_comment = False
386 struct_name_match = struct_name_regex.match(struct_line)
387 if struct_name_match is not None:
388 struct_name = struct_name_match.group(2)
389 if line_indicates_opaque_regex.match(struct_line):
391 trait_fn_match = line_indicates_trait_regex.match(struct_line)
392 if trait_fn_match is not None:
393 trait_fn_lines.append(trait_fn_match)
394 field_var_match = line_field_var_regex.match(struct_line)
395 if field_var_match is not None:
396 field_var_lines.append(field_var_match)
397 field_lines.append(struct_line)
399 assert(struct_name is not None)
400 assert(len(trait_fn_lines) == 0 or not is_opaque)
402 opaque_structs.add(struct_name)
403 if len(trait_fn_lines) > 0:
404 trait_structs.add(struct_name)
405 out_c.write("typedef struct " + struct_name + "_JCalls {\n")
406 out_c.write("\tatomic_size_t refcnt;\n")
407 out_c.write("\tJNIEnv *env;\n")
408 out_c.write("\tjobject o;\n")
409 for var_line in field_var_lines:
410 if var_line.group(1) in trait_structs:
411 out_c.write("\t" + var_line.group(1) + "_JCalls* " + var_line.group(2) + ";\n")
412 for fn_line in trait_fn_lines:
413 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
414 out_c.write("\tjmethodID " + fn_line.group(2) + "_meth;\n")
415 out_c.write("} " + struct_name + "_JCalls;\n")
417 out_java.write("\tpublic interface " + struct_name + " {\n")
419 for fn_line in trait_fn_lines:
420 java_meth_descr = "("
421 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
422 ret_ty_info = java_c_types(fn_line.group(1), None)
424 out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(2) + "(")
425 is_const = fn_line.group(3) is not None
426 out_c.write(fn_line.group(1) + fn_line.group(2) + "_jcall(")
428 out_c.write("const void* this_arg")
430 out_c.write("void* this_arg")
433 for idx, arg in enumerate(fn_line.group(4).split(',')):
439 arg_conv_info = map_type(arg, True, None, False)
440 out_c.write(arg.strip())
441 out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
442 arg_names.append(arg_conv_info)
443 java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
444 java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
445 java_meths.append(java_meth_descr)
447 out_java.write(");\n")
449 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
451 for arg_info in arg_names:
452 if arg_info.ret_conv is not None:
453 out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t').replace("_env", "j_calls->env"));
454 out_c.write(arg_info.arg_name)
455 out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t').replace("_env", "j_calls->env") + "\n")
457 if not ret_ty_info.passed_as_ptr:
458 out_c.write("\treturn (*j_calls->env)->Call" + ret_ty_info.java_ty.title() + "Method(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth")
460 out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth");
462 for arg_info in arg_names:
463 if arg_info.ret_conv is not None:
464 out_c.write(", " + arg_info.ret_conv_name)
466 out_c.write(", " + arg_info.arg_name)
469 if ret_ty_info.passed_as_ptr:
470 out_c.write("\t" + fn_line.group(1).strip() + " res = *ret;\n")
471 out_c.write("\tFREE(ret);\n")
472 out_c.write("\treturn res;\n")
474 elif fn_line.group(2) == "free":
475 out_c.write("static void " + struct_name + "_JCalls_free(void* this_arg) {\n")
476 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
477 out_c.write("\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n")
478 out_c.write("\t\t(*j_calls->env)->DeleteGlobalRef(j_calls->env, j_calls->o);\n")
479 out_c.write("\t\tFREE(j_calls);\n")
480 out_c.write("\t}\n}\n")
482 # Write out a clone function whether we need one or not, as we use them in moving to rust
483 out_c.write("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
484 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
485 out_c.write("\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n")
486 for var_line in field_var_lines:
487 if var_line.group(1) in trait_structs:
488 out_c.write("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
489 out_c.write("\treturn (void*) this_arg;\n")
492 out_java.write("\t}\n")
494 out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
495 out_c.write("static inline " + struct_name + " " + struct_name + "_init (JNIEnv * env, jclass _a, jobject o")
496 for var_line in field_var_lines:
497 if var_line.group(1) in trait_structs:
498 out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
499 out_c.write(", jobject " + var_line.group(2))
500 out_java.write(");\n")
503 out_c.write("\tjclass c = (*env)->GetObjectClass(env, o);\n")
504 out_c.write("\tassert(c != NULL);\n")
505 out_c.write("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
506 out_c.write("\tatomic_init(&calls->refcnt, 1);\n")
507 out_c.write("\tcalls->env = env;\n")
508 out_c.write("\tcalls->o = (*env)->NewGlobalRef(env, o);\n")
509 for (fn_line, java_meth_descr) in zip(trait_fn_lines, java_meths):
510 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
511 out_c.write("\tcalls->" + fn_line.group(2) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(2) + "\", \"" + java_meth_descr + "\");\n")
512 out_c.write("\tassert(calls->" + fn_line.group(2) + "_meth != NULL);\n")
513 out_c.write("\n\t" + struct_name + " ret = {\n")
514 out_c.write("\t\t.this_arg = (void*) calls,\n")
515 for fn_line in trait_fn_lines:
516 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
517 out_c.write("\t\t." + fn_line.group(2) + " = " + fn_line.group(2) + "_jcall,\n")
518 elif fn_line.group(2) == "free":
519 out_c.write("\t\t.free = " + struct_name + "_JCalls_free,\n")
521 out_c.write("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
522 for var_line in field_var_lines:
523 if var_line.group(1) in trait_structs:
524 out_c.write("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, _a, " + var_line.group(2) + "),\n")
525 out_c.write("\t};\n")
526 for var_line in field_var_lines:
527 if var_line.group(1) in trait_structs:
528 out_c.write("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
529 out_c.write("\treturn ret;\n")
532 out_c.write("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, jobject o")
533 for var_line in field_var_lines:
534 if var_line.group(1) in trait_structs:
535 out_c.write(", jobject " + var_line.group(2))
537 out_c.write("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
538 out_c.write("\t*res_ptr = " + struct_name + "_init(env, _a, o")
539 for var_line in field_var_lines:
540 if var_line.group(1) in trait_structs:
541 out_c.write(", " + var_line.group(2))
543 out_c.write("\treturn (long)res_ptr;\n")
546 #out_java.write("/* " + "\n".join(field_lines) + "*/\n")
547 cur_block_struct = None
549 if line.startswith("} "):
550 in_block_union = False
552 if line.startswith("} "):
553 in_block_enum = False
555 fn_ptr = fn_ptr_regex.match(line)
556 fn_ret_arr = fn_ret_arr_regex.match(line)
557 reg_fn = reg_fn_regex.match(line)
558 const_val = const_val_regex.match(line)
560 if line.startswith("#include <"):
562 elif line.startswith("/*"):
563 #out_java.write("\t" + line)
564 if not line.endswith("*/\n"):
565 in_block_comment = True
566 elif line.startswith("typedef enum "):
568 elif line.startswith("typedef struct "):
569 cur_block_struct = line
570 elif line.startswith("typedef union "):
571 in_block_union = True
572 elif line.startswith("typedef "):
574 elif fn_ptr is not None:
576 elif fn_ret_arr is not None:
577 map_fn(fn_ret_arr, fn_ret_arr.group(4))
578 elif reg_fn is not None:
580 elif const_val_regex is not None:
581 # TODO Map const variables
586 out_java.write("}\n")