5 print("USAGE: /path/to/lightning.h /path/to/bindings/output.java /path/to/bindings/ /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")
9 hu_struct_file_prefix = """package org.ldk.structs;
11 import org.ldk.impl.bindings;
12 import org.ldk.enums.*;
13 import org.ldk.util.*;
14 import java.util.Arrays;
16 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
20 def __init__(self, is_native_primitive, rust_obj, java_ty, java_fn_ty_arg, java_hu_ty, c_ty, passed_as_ptr, is_ptr, var_name, arr_len, arr_access, subty=None):
21 self.is_native_primitive = is_native_primitive
22 self.rust_obj = rust_obj
23 self.java_ty = java_ty
24 self.java_hu_ty = java_hu_ty
25 self.java_fn_ty_arg = java_fn_ty_arg
27 self.passed_as_ptr = passed_as_ptr
29 self.var_name = var_name
30 self.arr_len = arr_len
31 self.arr_access = arr_access
33 self.pass_by_ref = is_ptr
36 def __init__(self, ty_info, arg_name, arg_conv, arg_conv_name, arg_conv_cleanup, ret_conv, ret_conv_name, to_hu_conv, to_hu_conv_name, from_hu_conv):
37 assert(ty_info.c_ty is not None)
38 assert(ty_info.java_ty is not None)
39 assert(arg_name is not None)
40 self.passed_as_ptr = ty_info.passed_as_ptr
41 self.rust_obj = ty_info.rust_obj
42 self.c_ty = ty_info.c_ty
43 self.java_ty = ty_info.java_ty
44 self.java_hu_ty = ty_info.java_hu_ty
45 self.java_fn_ty_arg = ty_info.java_fn_ty_arg
46 self.arg_name = arg_name
47 self.arg_conv = arg_conv
48 self.arg_conv_name = arg_conv_name
49 self.arg_conv_cleanup = arg_conv_cleanup
50 self.ret_conv = ret_conv
51 self.ret_conv_name = ret_conv_name
52 self.to_hu_conv = to_hu_conv
53 self.to_hu_conv_name = to_hu_conv_name
54 self.from_hu_conv = from_hu_conv
57 out_c.write(self.c_ty)
58 out_java.write(self.java_ty)
61 if self.arg_name != "":
62 out_java.write(" " + self.arg_name)
63 out_c.write(" " + self.arg_name)
65 out_java.write(" arg")
68 def camel_to_snake(s):
69 # Convert camel case to snake case, in a way that appears to match cbindgen
75 if lastchar.isupper():
76 if not char.isupper() and not lastund:
81 ret = ret + lastchar.lower()
84 if char.isupper() and not lastund:
92 return (ret + lastchar.lower()).strip("_")
96 opaque_structs = set()
100 var_is_arr_regex = re.compile("\(\*([A-za-z0-9_]*)\)\[([a-z0-9]*)\]")
101 var_ty_regex = re.compile("([A-za-z_0-9]*)(.*)")
102 java_c_types_none_allowed = True # Unset when we do the real pass that populates the above sets
103 def java_c_types(fn_arg, ret_arr_len):
104 fn_arg = fn_arg.strip()
105 if fn_arg.startswith("MUST_USE_RES "):
108 if fn_arg.startswith("const "):
117 if fn_arg.startswith("LDKThirtyTwoBytes"):
118 fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]"
119 assert var_is_arr_regex.match(fn_arg[8:])
120 rust_obj = "LDKThirtyTwoBytes"
122 elif fn_arg.startswith("LDKPublicKey"):
123 fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]"
124 assert var_is_arr_regex.match(fn_arg[8:])
125 rust_obj = "LDKPublicKey"
126 arr_access = "compressed_form"
127 elif fn_arg.startswith("LDKSecretKey"):
128 fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]"
129 assert var_is_arr_regex.match(fn_arg[8:])
130 rust_obj = "LDKSecretKey"
132 elif fn_arg.startswith("LDKSignature"):
133 fn_arg = "uint8_t (*" + fn_arg[13:] + ")[64]"
134 assert var_is_arr_regex.match(fn_arg[8:])
135 rust_obj = "LDKSignature"
136 arr_access = "compact_form"
137 elif fn_arg.startswith("LDKThreeBytes"):
138 fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]"
139 assert var_is_arr_regex.match(fn_arg[8:])
140 rust_obj = "LDKThreeBytes"
142 elif fn_arg.startswith("LDKFourBytes"):
143 fn_arg = "uint8_t (*" + fn_arg[13:] + ")[4]"
144 assert var_is_arr_regex.match(fn_arg[8:])
145 rust_obj = "LDKFourBytes"
147 elif fn_arg.startswith("LDKSixteenBytes"):
148 fn_arg = "uint8_t (*" + fn_arg[16:] + ")[16]"
149 assert var_is_arr_regex.match(fn_arg[8:])
150 rust_obj = "LDKSixteenBytes"
152 elif fn_arg.startswith("LDKTenBytes"):
153 fn_arg = "uint8_t (*" + fn_arg[12:] + ")[10]"
154 assert var_is_arr_regex.match(fn_arg[8:])
155 rust_obj = "LDKTenBytes"
157 elif fn_arg.startswith("LDKu8slice"):
158 fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]"
159 assert var_is_arr_regex.match(fn_arg[8:])
160 rust_obj = "LDKu8slice"
162 elif fn_arg.startswith("LDKCVecTempl_u8") or fn_arg.startswith("LDKCVec_u8Z"):
163 if fn_arg.startswith("LDKCVecTempl_u8"):
164 fn_arg = "uint8_t (*" + fn_arg[16:] + ")[datalen]"
165 rust_obj = "LDKCVecTempl_u8"
166 assert var_is_arr_regex.match(fn_arg[8:])
168 fn_arg = "uint8_t (*" + fn_arg[12:] + ")[datalen]"
169 rust_obj = "LDKCVec_u8Z"
170 assert var_is_arr_regex.match(fn_arg[8:])
172 elif fn_arg.startswith("LDKCVecTempl_") or fn_arg.startswith("LDKCVec_"):
175 fn_arg = fn_arg.replace("*", "")
178 if fn_arg.startswith("LDKCVec_"):
179 tyn = fn_arg[8:].split(" ")
180 assert tyn[0].endswith("Z")
184 new_arg = "LDK" + tyn[0][:-1]
186 new_arg = new_arg + " " + a
187 res = java_c_types(new_arg, ret_arr_len)
189 res = java_c_types("LDK" + fn_arg[13:], ret_arr_len)
191 assert java_c_types_none_allowed
194 res.pass_by_ref = True
195 if res.is_native_primitive or res.passed_as_ptr:
196 return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=res.java_ty + "[]", java_hu_ty=res.java_hu_ty + "[]",
197 java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty=res.c_ty + "Array", passed_as_ptr=False, is_ptr=is_ptr,
198 var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
200 return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=res.java_ty + "[]", java_hu_ty=res.java_hu_ty + "[]",
201 java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty="jobjectArray", passed_as_ptr=False, is_ptr=is_ptr,
202 var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
206 if fn_arg.startswith("void"):
210 fn_arg = fn_arg[4:].strip()
212 elif fn_arg.startswith("bool"):
216 fn_arg = fn_arg[4:].strip()
218 elif fn_arg.startswith("uint8_t"):
222 fn_arg = fn_arg[7:].strip()
224 elif fn_arg.startswith("uint16_t"):
228 fn_arg = fn_arg[8:].strip()
230 elif fn_arg.startswith("uint32_t"):
234 fn_arg = fn_arg[8:].strip()
236 elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"):
240 if fn_arg.startswith("uint64_t"):
241 fn_arg = fn_arg[8:].strip()
243 fn_arg = fn_arg[9:].strip()
245 elif is_const and fn_arg.startswith("char *"):
248 fn_ty_arg = "Ljava/lang/String;"
249 fn_arg = fn_arg[6:].strip()
250 elif fn_arg.startswith("LDKStr"):
253 fn_ty_arg = "Ljava/lang/String;"
254 fn_arg = fn_arg[6:].strip()
258 ma = var_ty_regex.match(fn_arg)
259 if ma.group(1).strip() in unitary_enums:
260 java_ty = ma.group(1).strip()
262 fn_ty_arg = "Lorg/ldk/enums/" + ma.group(1).strip() + ";"
263 fn_arg = ma.group(2).strip()
264 rust_obj = ma.group(1).strip()
266 elif ma.group(1).strip().startswith("LDKC2Tuple"):
268 java_hu_ty = "TwoTuple<"
269 if not ma.group(1).strip() in tuple_types:
270 assert java_c_types_none_allowed
272 for idx, ty_info in enumerate(tuple_types[ma.group(1).strip()][0]):
274 java_hu_ty = java_hu_ty + ", "
275 if ty_info.is_native_primitive:
276 java_hu_ty = java_hu_ty + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
278 java_hu_ty = java_hu_ty + ty_info.java_hu_ty
279 java_hu_ty = java_hu_ty + ">"
282 fn_arg = ma.group(2).strip()
283 rust_obj = ma.group(1).strip()
285 elif ma.group(1).strip().startswith("LDKC3Tuple"):
287 java_hu_ty = "ThreeTuple<"
288 if not ma.group(1).strip() in tuple_types:
289 assert java_c_types_none_allowed
291 for idx, ty_info in enumerate(tuple_types[ma.group(1).strip()][0]):
293 java_hu_ty = java_hu_ty + ", "
294 if ty_info.is_native_primitive:
295 java_hu_ty = java_hu_ty + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
297 java_hu_ty = java_hu_ty + ty_info.java_hu_ty
298 java_hu_ty = java_hu_ty + ">"
301 fn_arg = ma.group(2).strip()
302 rust_obj = ma.group(1).strip()
306 java_hu_ty = ma.group(1).strip().replace("LDK", "")
309 fn_arg = ma.group(2).strip()
310 rust_obj = ma.group(1).strip()
313 if fn_arg.startswith(" *") or fn_arg.startswith("*"):
314 fn_arg = fn_arg.replace("*", "").strip()
321 var_is_arr = var_is_arr_regex.match(fn_arg)
322 if var_is_arr is not None or ret_arr_len is not None:
323 assert(not take_by_ptr)
325 java_ty = java_ty + "[]"
326 c_ty = c_ty + "Array"
327 if var_is_arr is not None:
328 if var_is_arr.group(1) == "":
329 return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
330 passed_as_ptr=False, is_ptr=False, var_name="arg", arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
331 return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
332 passed_as_ptr=False, is_ptr=False, var_name=var_is_arr.group(1), arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
334 if java_hu_ty is None:
336 return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
337 is_ptr=is_ptr, var_name=fn_arg, arr_len=arr_len, arr_access=arr_access, is_native_primitive=is_primitive)
339 fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
340 fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
341 reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
344 with open(sys.argv[1]) as in_h:
346 reg_fn = reg_fn_regex.match(line)
347 if reg_fn is not None:
348 if reg_fn.group(2).endswith("_clone"):
349 clone_fns.add(reg_fn.group(2))
351 rty = java_c_types(reg_fn.group(1), None)
352 if rty is not None and rty.rust_obj is not None and reg_fn.group(2) == rty.java_hu_ty + "_new":
353 constructor_fns[rty.rust_obj] = reg_fn.group(3)
355 arr_fn = fn_ret_arr_regex.match(line)
356 if arr_fn is not None:
357 if arr_fn.group(2).endswith("_clone"):
358 clone_fns.add(arr_fn.group(2))
359 # No object constructors return arrays, as then they wouldn't be an object constructor
361 java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order
363 with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.argv[4], "w") as out_c:
364 def map_type(fn_arg, print_void, ret_arr_len, is_free):
365 ty_info = java_c_types(fn_arg, ret_arr_len)
366 return map_type_with_info(ty_info, print_void, ret_arr_len, is_free)
368 def map_type_with_info(ty_info, print_void, ret_arr_len, is_free):
369 if ty_info.c_ty == "void":
371 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
372 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
373 ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
374 if ty_info.c_ty.endswith("Array"):
375 arr_len = ty_info.arr_len
376 if arr_len is not None:
377 arr_name = ty_info.var_name
380 arr_len = ret_arr_len
381 if ty_info.c_ty == "jbyteArray":
382 ret_conv = ("jbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", ", "")
383 arg_conv_cleanup = None
384 if not arr_len.isdigit():
385 arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
386 arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = (*_env)->GetByteArrayElements (_env, " + arr_name + ", NULL);\n"
387 arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");"
388 arg_conv_cleanup = "(*_env)->ReleaseByteArrayElements(_env, " + arr_name + ", (int8_t*)" + arr_name + "_ref." + ty_info.arr_access + ", 0);"
389 ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
390 ret_conv = (ret_conv[0], ";\njbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_name + "_var." + arr_len + ");\n")
391 ret_conv = (ret_conv[0], ret_conv[1] + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_name + "_var." + arr_len + ", " + arr_name + "_var." + ty_info.arr_access + ");")
392 elif ty_info.rust_obj is not None:
393 arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
394 arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
395 arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_ref." + ty_info.arr_access + ");"
396 ret_conv = (ret_conv[0], "." + ty_info.arr_access + ");")
398 arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n"
399 arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
400 arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" + "unsigned char (*" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;"
401 ret_conv = (ret_conv[0] + "*", ");")
402 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
403 arg_conv = arg_conv, arg_conv_name = arr_name + "_ref", arg_conv_cleanup = arg_conv_cleanup,
404 ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
406 assert not arr_len.isdigit() # not implemented
407 assert ty_info.java_ty[len(ty_info.java_ty) - 2:] == "[]"
408 conv_name = "arr_conv_" + str(len(ty_info.java_hu_ty))
409 idxc = chr(ord('a') + (len(ty_info.java_hu_ty) % 26))
410 ty_info.subty.var_name = conv_name
411 subty = map_type_with_info(ty_info.subty, False, None, is_free)
414 arg_conv = ty_info.rust_obj + " " + arr_name + "_constr;\n"
415 arg_conv = arg_conv + arr_name + "_constr." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");\n"
416 arg_conv = arg_conv + "if (" + arr_name + "_constr." + arr_len + " > 0)\n"
417 if subty.rust_obj is None:
420 szof = subty.rust_obj
421 arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = MALLOC(" + arr_name + "_constr." + arr_len + " * sizeof(" + szof + "), \"" + ty_info.rust_obj + " Elements\");\n"
422 arg_conv = arg_conv + "else\n"
423 arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = NULL;\n"
424 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
425 arg_conv = arg_conv + ty_info.java_ty.strip("[]") + "* " + arr_name + "_vals = (*_env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (_env, " + arr_name + ", NULL);\n"
426 arg_conv = arg_conv + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_constr." + arr_len + "; " + idxc + "++) {\n"
427 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
428 arg_conv = arg_conv + "\t" + ty_info.java_ty.strip("[]") + " " + conv_name + " = " + arr_name + "_vals[" + idxc + "];"
429 if subty.arg_conv is not None:
430 arg_conv = arg_conv + "\n\t" + subty.arg_conv.replace("\n", "\n\t")
432 arg_conv = arg_conv + "\tjobject " + conv_name + " = (*_env)->GetObjectArrayElement(_env, " + arr_name + ", " + idxc + ");\n"
433 arg_conv = arg_conv + "\t" + subty.arg_conv.replace("\n", "\n\t")
434 arg_conv = arg_conv + "\n\t" + arr_name + "_constr." + ty_info.arr_access + "[" + idxc + "] = " + subty.arg_conv_name + ";\n}"
435 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
436 arg_conv = arg_conv + "\n(*_env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements (_env, " + arr_name + ", " + arr_name + "_vals, 0);"
438 arg_conv_name = "&" + arr_name + "_constr"
440 arg_conv_name = arr_name + "_constr"
441 arg_conv_cleanup = None
443 arg_conv_cleanup = "FREE(" + arr_name + "_constr." + ty_info.arr_access + ");"
445 if arr_name == "arg":
447 ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
448 if subty.ret_conv is None:
449 ret_conv = ("DUMMY", "DUMMY")
450 elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
451 ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = (*_env)->New" + ty_info.java_ty.strip("[]").title() + "Array(_env, " + arr_name + "_var." + arr_len + ");\n")
452 ret_conv = (ret_conv[0], ret_conv[1] + subty.c_ty + " *" + arr_name + "_arr_ptr = (*_env)->GetPrimitiveArrayCritical(_env, " + arr_name + "_arr, NULL);\n")
453 ret_conv = (ret_conv[0], ret_conv[1] + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_var." + arr_len + "; " + idxc + "++) {\n")
454 ret_conv = (ret_conv[0], ret_conv[1] + "\t" + subty.ret_conv[0].replace("\n", "\n\t"))
455 ret_conv = (ret_conv[0], ret_conv[1] + arr_name + "_var." + ty_info.arr_access + "[" + idxc + "]" + subty.ret_conv[1].replace("\n", "\n\t"))
456 ret_conv = (ret_conv[0], ret_conv[1] + "\n\t" + arr_name + "_arr_ptr[" + idxc + "] = " + subty.ret_conv_name + ";\n")
457 ret_conv = (ret_conv[0], ret_conv[1] + "}\n(*_env)->ReleasePrimitiveArrayCritical(_env, " + arr_name + "_arr, " + arr_name + "_arr_ptr, 0);")
459 ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = (*_env)->NewObjectArray(_env, " + arr_name + "_var." + arr_len + ", NULL, NULL);\n") # XXX: second arg needs to be a clazz!
460 ret_conv = (ret_conv[0], ret_conv[1] + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_var." + arr_len + "; " + idxc + "++) {\n")
461 ret_conv = (ret_conv[0], ret_conv[1] + "\t" + subty.ret_conv[0].replace("\n", "\n\t"))
462 ret_conv = (ret_conv[0], ret_conv[1] + arr_name + "_var." + ty_info.arr_access + "[" + idxc + "]" + subty.ret_conv[1].replace("\n", "\n\t"))
463 ret_conv = (ret_conv[0], ret_conv[1] + "\n\t(*_env)->SetObjectArrayElement(_env, " + arr_name + "_arr, " + idxc + ", " + subty.ret_conv_name + ");")
464 ret_conv = (ret_conv[0], ret_conv[1] + "}")
465 if subty.rust_obj is not None and subty.rust_obj in opaque_structs:
466 ret_conv = (ret_conv[0], ret_conv[1] + "\nFREE(" + arr_name + "_var.data);")
469 to_hu_conv_name = None
470 if subty.to_hu_conv is not None:
471 to_hu_conv = ty_info.java_hu_ty + " " + conv_name + "_arr = new " + ty_info.subty.java_hu_ty.split("<")[0] + "[" + arr_name + ".length];\n"
472 to_hu_conv = to_hu_conv + "for (int " + idxc + " = 0; " + idxc + " < " + arr_name + ".length; " + idxc + "++) {\n"
473 to_hu_conv = to_hu_conv + "\t" + subty.java_ty + " " + conv_name + " = " + arr_name + "[" + idxc + "];\n"
474 to_hu_conv = to_hu_conv + "\t" + subty.to_hu_conv.replace("\n", "\n\t") + "\n"
475 to_hu_conv = to_hu_conv + "\t" + conv_name + "_arr[" + idxc + "] = " + subty.to_hu_conv_name + ";\n}"
476 to_hu_conv_name = conv_name + "_arr"
478 if subty.from_hu_conv is not None:
479 if subty.java_ty == "long" and subty.java_hu_ty != "long":
480 from_hu_conv = ("Arrays.stream(" + arr_name + ").mapToLong(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + " */")
481 elif subty.java_ty == "long":
482 from_hu_conv = ("Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + " */")
484 from_hu_conv = ("(" + ty_info.java_ty + ")Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + " */")
486 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
487 arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = arg_conv_cleanup,
488 ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = to_hu_conv, to_hu_conv_name = to_hu_conv_name, from_hu_conv = from_hu_conv)
489 elif ty_info.java_ty == "String":
490 if ty_info.arr_access is None:
491 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
492 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
493 ret_conv = ("jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv",
494 to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
496 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
497 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
498 ret_conv = ("LDKStr " + ty_info.var_name + "_str = ",
499 ";\nchar* " + ty_info.var_name + "_buf = MALLOC(" + ty_info.var_name + "_str." + ty_info.arr_len + " + 1, \"str conv buf\");\n" +
500 "memcpy(" + ty_info.var_name + "_buf, " + ty_info.var_name + "_str." + ty_info.arr_access + ", " + ty_info.var_name + "_str." + ty_info.arr_len + ");\n" +
501 ty_info.var_name + "_buf[" + ty_info.var_name + "_str." + ty_info.arr_len + "] = 0;\n" +
502 "jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, " + ty_info.var_name + "_str." + ty_info.arr_access + ");\n" +
503 "FREE(" + ty_info.var_name + "_buf);"),
504 ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
505 elif ty_info.var_name != "":
506 # If we have a parameter name, print it (noting that it may indicate its a pointer)
507 if ty_info.rust_obj is not None:
508 assert(ty_info.passed_as_ptr)
509 opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
510 opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = (void*)(" + ty_info.var_name + " & (~1));\n"
511 opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = (" + ty_info.var_name + " & 1) || (" + ty_info.var_name + " == 0);"
512 if not ty_info.is_ptr and not is_free and not ty_info.pass_by_ref:
513 if (ty_info.java_hu_ty + "_clone") in clone_fns:
514 # TODO: This is a bit too naive, even with the checks above, we really need to know if rust wants a ref or not, not just if its pass as a ptr.
515 opaque_arg_conv = opaque_arg_conv + "\nif (" + ty_info.var_name + "_conv.inner != NULL)\n"
516 opaque_arg_conv = opaque_arg_conv + "\t" + ty_info.var_name + "_conv = " + ty_info.java_hu_ty + "_clone(&" + ty_info.var_name + "_conv);"
517 elif ty_info.passed_as_ptr:
518 opaque_arg_conv = opaque_arg_conv + "\n// Warning: we may need a move here but can't clone!"
519 if not ty_info.is_ptr:
520 if ty_info.rust_obj in unitary_enums:
521 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
522 arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_from_java(_env, " + ty_info.var_name + ");",
523 arg_conv_name = ty_info.var_name + "_conv",
524 arg_conv_cleanup = None,
525 ret_conv = ("jclass " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_to_java(_env, ", ");"),
526 ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
527 if ty_info.rust_obj in opaque_structs:
528 ret_conv_suf = ";\nCHECK((((long)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
529 ret_conv_suf = ret_conv_suf + "CHECK((((long)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
530 ret_conv_suf = ret_conv_suf + "long " + ty_info.var_name + "_ref;\n"
531 ret_conv_suf = ret_conv_suf + "if (" + ty_info.var_name + "_var.is_owned) {\n"
532 ret_conv_suf = ret_conv_suf + "\t" + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner | 1;\n"
533 ret_conv_suf = ret_conv_suf + "} else {\n"
534 ret_conv_suf = ret_conv_suf + "\t" + ty_info.var_name + "_ref = (long)&" + ty_info.var_name + "_var;\n"
535 ret_conv_suf = ret_conv_suf + "}"
536 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
537 arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
538 ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", ret_conv_suf),
539 ret_conv_name = ty_info.var_name + "_ref",
540 to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
541 to_hu_conv_name = ty_info.var_name + "_hu_conv",
542 from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
543 base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";";
544 if ty_info.rust_obj in trait_structs:
546 base_conv = base_conv + "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
547 base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
548 base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}"
550 base_conv = base_conv + "\n" + "FREE((void*)" + ty_info.var_name + ");"
551 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
552 arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
553 ret_conv = ("CANT PASS TRAIT TO Java?", ""), ret_conv_name = "NO CONV POSSIBLE",
554 to_hu_conv = "DUMMY", to_hu_conv_name = None,
555 from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
556 if ty_info.rust_obj != "LDKu8slice":
557 # Don't bother free'ing slices passed in - Rust doesn't auto-free the
558 # underlying unlike Vecs, and it gives Java more freedom.
559 base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");";
560 if ty_info.rust_obj in complex_enums:
561 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
562 arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
563 ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref",
564 to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");",
565 to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + ".conv_to_c()", ""))
566 if ty_info.rust_obj in tuple_types:
568 to_hu_conv_sfx = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " + ty_info.java_hu_ty + "("
569 for idx, conv in enumerate(tuple_types[ty_info.rust_obj][0]):
571 to_hu_conv_sfx = to_hu_conv_sfx + ", "
572 conv.var_name = ty_info.var_name + "_" + chr(idx + ord("a"))
573 conv_map = map_type_with_info(conv, False, None, is_free)
574 to_hu_conv_pfx = to_hu_conv_pfx + conv.java_ty + " " + ty_info.var_name + "_" + chr(idx + ord("a")) + " = " + "bindings." + tuple_types[ty_info.rust_obj][1] + "_get_" + chr(idx + ord("a")) + "(" + ty_info.var_name + ");\n"
575 if conv_map.to_hu_conv is not None:
576 to_hu_conv_pfx = to_hu_conv_pfx + conv_map.to_hu_conv + ";\n"
577 to_hu_conv_sfx = to_hu_conv_sfx + conv_map.to_hu_conv_name;
579 to_hu_conv_sfx = to_hu_conv_sfx + ty_info.var_name + "_" + chr(idx + ord("a"));
580 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
581 arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
582 ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref",
583 to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = ("/*TODO b*/0", ""))
585 # The manually-defined types - TxOut and Transaction
586 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
587 arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
588 ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref",
589 to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
590 to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = ("/*TODO 1*/0", ""))
593 if ty_info.rust_obj in opaque_structs:
594 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
595 arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
596 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 2", to_hu_conv_name = None,
597 from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")")) # its a pointer, no conv needed
598 elif ty_info.rust_obj in trait_structs:
599 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
600 arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
601 arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
602 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 2.5", to_hu_conv_name = None,
603 from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) # its a pointer, no conv needed
604 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
605 arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
606 arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
607 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed
609 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
610 arg_conv = None, arg_conv_name = ty_info.var_name, arg_conv_cleanup = None,
611 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 4", to_hu_conv_name = None, from_hu_conv = None)
613 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
614 arg_conv = None, arg_conv_name = ty_info.var_name, arg_conv_cleanup = None,
615 ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
617 # We don't have a parameter name, and want one, just call it arg
618 if ty_info.rust_obj is not None:
619 assert(not is_free or ty_info.rust_obj not in opaque_structs)
620 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
621 arg_conv = ty_info.rust_obj + " arg_conv = *(" + ty_info.rust_obj + "*)arg;\nFREE((void*)arg);",
622 arg_conv_name = "arg_conv", arg_conv_cleanup = None,
623 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 7", to_hu_conv_name = None, from_hu_conv = None)
626 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
627 arg_conv = None, arg_conv_name = "arg", arg_conv_cleanup = None,
628 ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 8", to_hu_conv_name = None, from_hu_conv = None)
630 # We don't have a parameter name, and don't want one (cause we're returning)
631 if ty_info.rust_obj is not None:
632 if not ty_info.is_ptr:
633 if ty_info.rust_obj in unitary_enums:
634 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
635 arg_conv = ty_info.rust_obj + " ret_conv = " + ty_info.rust_obj + "_from_java(_env, ret);",
636 arg_conv_name = "ret_conv", arg_conv_cleanup = None,
637 ret_conv = ("jclass ret = " + ty_info.rust_obj + "_to_java(_env, ", ");"), ret_conv_name = "ret",
638 to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
639 if ty_info.rust_obj in opaque_structs:
640 # If we're returning a newly-allocated struct, we don't want Rust to ever
641 # free, instead relying on the Java GC to lose the ref. We undo this in
642 # any _free function.
643 # To avoid any issues, we first assert that the incoming object is non-ref.
644 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
645 ret_conv = (ty_info.rust_obj + " ret = ", ";"),
646 ret_conv_name = "((long)ret.inner) | (ret.is_owned ? 1 : 0)",
647 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
648 to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);",
649 to_hu_conv_name = "ret_hu_conv", from_hu_conv = None)
650 elif ty_info.rust_obj in trait_structs:
651 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
652 ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"),
653 ret_conv_name = "(long)ret",
654 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
655 to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);",
656 to_hu_conv_name = "ret_hu_conv", from_hu_conv = None)
658 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
659 ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"),
660 ret_conv_name = "(long)ret",
661 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
662 to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);",
663 to_hu_conv_name = "ret_hu_conv", from_hu_conv = None)
664 elif ty_info.rust_obj in trait_structs:
665 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
666 ret_conv = ("long ret = (long)", ";"), ret_conv_name = "ret",
667 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
668 to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);",
669 to_hu_conv_name = "ret_hu_conv", from_hu_conv = None)
671 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
672 ret_conv = ("long ret = (long)", ";"), ret_conv_name = "ret",
673 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
674 to_hu_conv = "TODO c", to_hu_conv_name = None, from_hu_conv = None)
676 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
677 arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
678 ret_conv = None, ret_conv_name = None,
679 to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
681 def map_fn(line, re_match, ret_arr_len, c_call_string):
682 out_java.write("\t// " + line)
683 out_java.write("\tpublic static native ")
684 out_c.write("JNIEXPORT ")
686 is_free = re_match.group(2).endswith("_free")
687 struct_meth = re_match.group(2).split("_")[0]
689 ret_info = map_type(re_match.group(1), True, ret_arr_len, False)
692 if ret_info.ret_conv is not None:
693 ret_conv_pfx, ret_conv_sfx = ret_info.ret_conv
695 out_java.write(" " + re_match.group(2) + "(")
696 out_c.write(" JNICALL Java_org_ldk_impl_bindings_" + re_match.group(2).replace('_', '_1') + "(JNIEnv * _env, jclass _b")
699 default_constructor_args = {}
701 args_known = not ret_info.passed_as_ptr or ret_info.rust_obj in opaque_structs or ret_info.rust_obj in trait_structs
702 for idx, arg in enumerate(re_match.group(3).split(',')):
707 arg_conv_info = map_type(arg, False, None, is_free)
708 if arg_conv_info.c_ty != "void":
709 arg_conv_info.print_ty()
710 arg_conv_info.print_name()
711 if arg_conv_info.arg_name == "this_ptr" or arg_conv_info.arg_name == "this_arg":
713 if arg_conv_info.passed_as_ptr and not arg_conv_info.rust_obj in opaque_structs:
714 if not arg_conv_info.rust_obj in trait_structs and not arg_conv_info.rust_obj in unitary_enums:
716 if arg_conv_info.arg_conv is not None and "Warning" in arg_conv_info.arg_conv:
717 if arg_conv_info.rust_obj in constructor_fns:
719 for explode_arg in constructor_fns[arg_conv_info.rust_obj].split(','):
720 explode_arg_conv = map_type(explode_arg, False, None, False)
721 if explode_arg_conv.c_ty == "void":
722 # We actually want to handle this case, but for now its only used in NetGraphMsgHandler::new()
723 # which ends up resulting in a redundant constructor - both without arguments for the NetworkGraph.
725 assert explode_arg_conv.arg_name != "this_ptr"
726 assert explode_arg_conv.arg_name != "this_arg"
727 if explode_arg_conv.passed_as_ptr and not explode_arg_conv.rust_obj in trait_structs:
729 if not arg_conv_info.arg_name in default_constructor_args:
730 default_constructor_args[arg_conv_info.arg_name] = []
731 default_constructor_args[arg_conv_info.arg_name].append(explode_arg_conv)
734 arg_names.append(arg_conv_info)
736 out_java_struct = None
737 if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
738 out_java_struct = open(sys.argv[3] + "/structs/" + struct_meth + ".java", "a")
740 out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n")
741 out_java_struct.close()
742 out_java_struct = None
744 meth_n = re_match.group(2)[len(struct_meth) + 1:]
745 if ret_info.rust_obj == "LDK" + struct_meth:
746 out_java_struct.write("\tpublic static " + ret_info.java_hu_ty + " constructor_" + meth_n + "(")
748 out_java_struct.write("\tpublic " + ret_info.java_hu_ty + " " + meth_n + "(")
749 for idx, arg in enumerate(arg_names):
751 if not takes_self or idx > 1:
752 out_java_struct.write(", ")
753 if arg.java_ty != "void" and arg.arg_name != "this_ptr" and arg.arg_name != "this_arg":
754 if arg.arg_name in default_constructor_args:
755 for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
757 out_java_struct.write(", ")
758 assert explode_arg.rust_obj in opaque_structs or explode_arg.rust_obj in trait_structs
759 out_java_struct.write(explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name)
761 out_java_struct.write(arg.java_hu_ty + " " + arg.arg_name)
764 out_java.write(");\n")
766 if out_java_struct is not None:
767 out_java_struct.write(") {\n")
769 for info in arg_names:
770 if info.arg_conv is not None:
771 out_c.write("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
773 if ret_info.ret_conv is not None:
774 out_c.write("\t" + ret_conv_pfx.replace('\n', '\n\t'))
775 elif ret_info.c_ty != "void":
776 out_c.write("\t" + ret_info.c_ty + " ret_val = ")
780 if c_call_string is None:
781 out_c.write(re_match.group(2) + "(")
783 out_c.write(c_call_string)
784 for idx, info in enumerate(arg_names):
785 if info.arg_conv_name is not None:
788 elif c_call_string is not None:
790 out_c.write(info.arg_conv_name)
792 if ret_info.ret_conv is not None:
793 out_c.write(ret_conv_sfx.replace('\n', '\n\t'))
796 for info in arg_names:
797 if info.arg_conv_cleanup is not None:
798 out_c.write("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
799 if ret_info.ret_conv is not None:
800 out_c.write("\n\treturn " + ret_info.ret_conv_name + ";")
801 elif ret_info.c_ty != "void":
802 out_c.write("\n\treturn ret_val;")
803 out_c.write("\n}\n\n")
804 if out_java_struct is not None:
805 out_java_struct.write("\t\t")
806 if ret_info.java_ty != "void":
807 out_java_struct.write(ret_info.java_ty + " ret = ")
808 out_java_struct.write("bindings." + re_match.group(2) + "(")
809 for idx, info in enumerate(arg_names):
811 out_java_struct.write(", ")
812 if info.arg_name == "this_ptr" or info.arg_name == "this_arg":
813 out_java_struct.write("this.ptr")
814 elif info.arg_name in default_constructor_args:
815 out_java_struct.write("bindings." + info.java_hu_ty + "_new(")
816 for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
818 out_java_struct.write(", ")
819 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
820 out_java_struct.write(explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
821 out_java_struct.write(")")
822 elif info.from_hu_conv is not None:
823 out_java_struct.write(info.from_hu_conv[0])
825 out_java_struct.write(info.arg_name)
826 out_java_struct.write(");\n")
827 if ret_info.to_hu_conv is not None:
828 out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
830 for info in arg_names:
831 if info.arg_name == "this_ptr" or info.arg_name == "this_arg":
833 elif info.arg_name in default_constructor_args:
834 for explode_arg in default_constructor_args[info.arg_name]:
835 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
836 out_java_struct.write("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name, expl_arg_name).replace("this", ret_info.to_hu_conv_name) + ";\n")
837 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
838 if ret_info.rust_obj == "LDK" + struct_meth and ret_info.to_hu_conv_name is not None:
839 out_java_struct.write("\t\t" + info.from_hu_conv[1].replace("this", ret_info.to_hu_conv_name) + ";\n")
841 out_java_struct.write("\t\t" + info.from_hu_conv[1] + ";\n")
843 if ret_info.to_hu_conv_name is not None:
844 out_java_struct.write("\t\treturn " + ret_info.to_hu_conv_name + ";\n")
845 elif ret_info.java_ty != "void" and ret_info.rust_obj != "LDK" + struct_meth:
846 out_java_struct.write("\t\treturn ret;\n")
847 out_java_struct.write("\t}\n\n")
848 out_java_struct.close()
850 def map_unitary_enum(struct_name, field_lines):
851 with open(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum:
852 out_java_enum.write("package org.ldk.enums;\n\n")
853 unitary_enums.add(struct_name)
854 out_c.write("static inline " + struct_name + " " + struct_name + "_from_java(JNIEnv *env, jclass val) {\n")
855 out_c.write("\tswitch ((*env)->CallIntMethod(env, val, ordinal_meth)) {\n")
857 for idx, struct_line in enumerate(field_lines):
859 out_java_enum.write("public enum " + struct_name + " {\n")
860 elif idx == len(field_lines) - 3:
861 assert(struct_line.endswith("_Sentinel,"))
862 elif idx == len(field_lines) - 2:
863 out_java_enum.write("\t; static native void init();\n")
864 out_java_enum.write("\tstatic { init(); }\n")
865 out_java_enum.write("}")
866 out_java.write("\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n")
867 elif idx == len(field_lines) - 1:
868 assert(struct_line == "")
870 out_java_enum.write(struct_line + "\n")
871 out_c.write("\t\tcase %d: return %s;\n" % (ord_v, struct_line.strip().strip(",")))
874 out_c.write("\tabort();\n")
878 out_c.write("static jclass " + struct_name + "_class = NULL;\n")
879 for idx, struct_line in enumerate(field_lines):
880 if idx > 0 and idx < len(field_lines) - 3:
881 variant = struct_line.strip().strip(",")
882 out_c.write("static jfieldID " + struct_name + "_" + variant + " = NULL;\n")
883 out_c.write("JNIEXPORT void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass clz) {\n")
884 out_c.write("\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n")
885 out_c.write("\tCHECK(" + struct_name + "_class != NULL);\n")
886 for idx, struct_line in enumerate(field_lines):
887 if idx > 0 and idx < len(field_lines) - 3:
888 variant = struct_line.strip().strip(",")
889 out_c.write("\t" + struct_name + "_" + variant + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
890 out_c.write("\tCHECK(" + struct_name + "_" + variant + " != NULL);\n")
892 out_c.write("static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n")
893 out_c.write("\tswitch (val) {\n")
894 for idx, struct_line in enumerate(field_lines):
895 if idx > 0 and idx < len(field_lines) - 3:
896 variant = struct_line.strip().strip(",")
897 out_c.write("\t\tcase " + variant + ":\n")
898 out_c.write("\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + variant + ");\n")
900 out_c.write("\t\tdefault: abort();\n")
904 def map_complex_enum(struct_name, union_enum_items):
905 java_hu_type = struct_name.replace("LDK", "")
906 complex_enums.add(struct_name)
907 with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
908 out_java_enum.write(hu_struct_file_prefix)
909 out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
910 out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
911 out_java_enum.write("\tlong conv_to_c() { assert false; return 0; /* Should only be called on subclasses */ }\n")
912 out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
913 out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
914 java_hu_subclasses = ""
916 tag_field_lines = union_enum_items["field_lines"]
917 init_meth_jty_strs = {}
918 for idx, struct_line in enumerate(tag_field_lines):
920 out_java.write("\tpublic static class " + struct_name + " {\n")
921 out_java.write("\t\tprivate " + struct_name + "() {}\n")
922 elif idx == len(tag_field_lines) - 3:
923 assert(struct_line.endswith("_Sentinel,"))
924 elif idx == len(tag_field_lines) - 2:
925 out_java.write("\t\tstatic native void init();\n")
926 out_java.write("\t}\n")
927 elif idx == len(tag_field_lines) - 1:
928 assert(struct_line == "")
930 var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
931 out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
932 java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
933 out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
934 out_java_enum.write("\t\t\treturn new " + var_name + "(null, ptr);\n") # TODO: Conv values!
935 out_c.write("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
936 out_c.write("static jmethodID " + struct_name + "_" + var_name + "_meth = NULL;\n")
937 init_meth_jty_str = ""
938 init_meth_params = ""
940 if "LDK" + var_name in union_enum_items:
941 enum_var_lines = union_enum_items["LDK" + var_name]
942 for idx, field in enumerate(enum_var_lines):
943 if idx != 0 and idx < len(enum_var_lines) - 2:
944 field_ty = java_c_types(field.strip(' ;'), None)
945 out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.var_name + ";\n")
946 java_hu_subclasses = java_hu_subclasses + "\t\tpublic " + field_ty.java_hu_ty + " " + field_ty.var_name + ";\n"
947 init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
949 init_meth_params = init_meth_params + ", "
950 init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.var_name
951 init_meth_body = init_meth_body + "this." + field_ty.var_name + " = " + field_ty.var_name + "; "
952 out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
953 out_java.write(init_meth_body)
954 out_java.write("}\n")
955 out_java.write("\t\t}\n")
956 out_java_enum.write("\t\t}\n")
957 java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(Object _dummy, long ptr) { super(null, ptr); }\n"
958 java_hu_subclasses = java_hu_subclasses + "\t\t@Override long conv_to_c() { return 0; /*XXX*/ }\n"
959 java_hu_subclasses = java_hu_subclasses + "\t}\n"
960 init_meth_jty_strs[var_name] = init_meth_jty_str
961 out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
962 out_java_enum.write(java_hu_subclasses)
963 out_java.write("\tstatic { " + struct_name + ".init(); }\n")
964 out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
966 out_c.write("JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass _a) {\n")
967 for idx, struct_line in enumerate(tag_field_lines):
968 if idx != 0 and idx < len(tag_field_lines) - 3:
969 var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
970 out_c.write("\t" + struct_name + "_" + var_name + "_class =\n")
971 out_c.write("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
972 out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
973 out_c.write("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
974 out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
976 out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
977 out_c.write("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
978 out_c.write("\tswitch(obj->tag) {\n")
979 for idx, struct_line in enumerate(tag_field_lines):
980 if idx != 0 and idx < len(tag_field_lines) - 3:
981 var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
982 out_c.write("\t\tcase " + struct_name + "_" + var_name + ": {\n")
984 if "LDK" + var_name in union_enum_items:
985 enum_var_lines = union_enum_items["LDK" + var_name]
986 for idx, field in enumerate(enum_var_lines):
987 if idx != 0 and idx < len(enum_var_lines) - 2:
988 field_map = map_type(field.strip(' ;'), False, None, False)
989 if field_map.ret_conv is not None:
990 out_c.write("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
991 out_c.write("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
992 out_c.write(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
993 c_params_text = c_params_text + ", " + field_map.ret_conv_name
995 c_params_text = c_params_text + ", obj->" + camel_to_snake(var_name) + "." + field_map.arg_name
996 out_c.write("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
997 out_c.write("\t\t}\n")
998 out_c.write("\t\tdefault: abort();\n")
999 out_c.write("\t}\n}\n")
1000 out_java_enum.write("}\n")
1002 def map_trait(struct_name, field_var_lines, trait_fn_lines):
1003 with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_trait:
1004 out_c.write("typedef struct " + struct_name + "_JCalls {\n")
1005 out_c.write("\tatomic_size_t refcnt;\n")
1006 out_c.write("\tJavaVM *vm;\n")
1007 out_c.write("\tjweak o;\n")
1008 for var_line in field_var_lines:
1009 if var_line.group(1) in trait_structs:
1010 out_c.write("\t" + var_line.group(1) + "_JCalls* " + var_line.group(2) + ";\n")
1011 for fn_line in trait_fn_lines:
1012 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
1013 out_c.write("\tjmethodID " + fn_line.group(2) + "_meth;\n")
1014 out_c.write("} " + struct_name + "_JCalls;\n")
1016 out_java_trait.write(hu_struct_file_prefix)
1017 out_java_trait.write("public class " + struct_name.replace("LDK","") + " extends CommonBase {\n")
1018 out_java_trait.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); }\n")
1019 out_java_trait.write("\tpublic " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg") # XXX: Should be priv ( but is currently used in tests
1020 for var_line in field_var_lines:
1021 if var_line.group(1) in trait_structs:
1022 out_java_trait.write(", bindings." + var_line.group(1) + " " + var_line.group(2))
1023 out_java_trait.write(") {\n")
1024 out_java_trait.write("\t\tsuper(bindings." + struct_name + "_new(arg")
1025 for var_line in field_var_lines:
1026 if var_line.group(1) in trait_structs:
1027 out_java_trait.write(", " + var_line.group(2))
1028 out_java_trait.write("));\n")
1029 out_java_trait.write("\t\tthis.ptrs_to.add(arg);\n")
1030 out_java_trait.write("\t}\n")
1031 out_java_trait.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
1032 out_java_trait.write("\tprotected void finalize() throws Throwable {\n")
1033 out_java_trait.write("\t\tbindings." + struct_name.replace("LDK","") + "_free(ptr); super.finalize();\n")
1034 out_java_trait.write("\t}\n\n")
1036 java_trait_constr = "\tpublic " + struct_name.replace("LDK", "") + "(" + struct_name.replace("LDK", "") + "Interface arg) {\n"
1037 java_trait_constr = java_trait_constr + "\t\tthis(new bindings." + struct_name + "() {\n"
1038 #out_java_trait.write("\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n")
1039 out_java.write("\tpublic interface " + struct_name + " {\n")
1041 for fn_line in trait_fn_lines:
1042 java_meth_descr = "("
1043 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
1044 ret_ty_info = map_type(fn_line.group(1), True, None, False)
1046 out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(2) + "(")
1047 java_trait_constr = java_trait_constr + "\t\t\t@Override public " + ret_ty_info.java_ty + " " + fn_line.group(2) + "("
1048 #out_java_trait.write("\t\t" + ret_ty_info.java_hu_ty + " " + fn_line.group(2) + "(")
1049 is_const = fn_line.group(3) is not None
1050 out_c.write(fn_line.group(1) + fn_line.group(2) + "_jcall(")
1052 out_c.write("const void* this_arg")
1054 out_c.write("void* this_arg")
1057 for idx, arg in enumerate(fn_line.group(4).split(',')):
1061 out_java.write(", ")
1062 java_trait_constr = java_trait_constr + ", "
1063 #out_java_trait.write(", ")
1065 arg_conv_info = map_type(arg, True, None, False)
1066 out_c.write(arg.strip())
1067 out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
1068 #out_java_trait.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
1069 java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
1070 arg_names.append(arg_conv_info)
1071 java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
1072 java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
1073 java_meths.append(java_meth_descr)
1075 out_java.write(");\n")
1076 #out_java_trait.write(");\n")
1077 java_trait_constr = java_trait_constr + ") {\n"
1078 out_c.write(") {\n")
1079 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1080 out_c.write("\tJNIEnv *_env;\n")
1081 out_c.write("\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&_env, JNI_VERSION_1_8) == JNI_OK);\n")
1083 for arg_info in arg_names:
1084 if arg_info.ret_conv is not None:
1085 out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t'));
1086 out_c.write(arg_info.arg_name)
1087 out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t') + "\n")
1089 out_c.write("\tjobject obj = (*_env)->NewLocalRef(_env, j_calls->o);\n\tCHECK(obj != NULL);\n")
1090 if ret_ty_info.c_ty.endswith("Array"):
1091 out_c.write("\t" + ret_ty_info.c_ty + " ret = (*_env)->CallObjectMethod(_env, obj, j_calls->" + fn_line.group(2) + "_meth")
1092 elif not ret_ty_info.passed_as_ptr:
1093 out_c.write("\treturn (*_env)->Call" + ret_ty_info.java_ty.title() + "Method(_env, obj, j_calls->" + fn_line.group(2) + "_meth")
1095 out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*_env)->CallLongMethod(_env, obj, j_calls->" + fn_line.group(2) + "_meth");
1096 if ret_ty_info.java_ty != "void":
1097 java_trait_constr = java_trait_constr + "\t\t\t\treturn arg." + fn_line.group(2) + "("
1099 java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(2) + "("
1101 for arg_info in arg_names:
1102 if arg_info.ret_conv is not None:
1103 out_c.write(", " + arg_info.ret_conv_name)
1105 out_c.write(", " + arg_info.arg_name)
1106 out_c.write(");\n");
1107 if ret_ty_info.arg_conv is not None:
1108 out_c.write("\t" + ret_ty_info.arg_conv.replace("\n", "\n\t").replace("arg", "ret") + "\n\treturn " + ret_ty_info.arg_conv_name.replace("arg", "ret") + ";\n")
1110 if ret_ty_info.passed_as_ptr:
1111 out_c.write("\t" + fn_line.group(1).strip() + " res = *ret;\n")
1112 out_c.write("\tFREE(ret);\n")
1113 out_c.write("\treturn res;\n")
1115 java_trait_constr = java_trait_constr + ");\n\t\t\t}\n"
1116 elif fn_line.group(2) == "free":
1117 out_c.write("static void " + struct_name + "_JCalls_free(void* this_arg) {\n")
1118 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1119 out_c.write("\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n")
1120 out_c.write("\t\tJNIEnv *env;\n")
1121 out_c.write("\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n")
1122 out_c.write("\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n")
1123 out_c.write("\t\tFREE(j_calls);\n")
1124 out_c.write("\t}\n}\n")
1125 #out_java_trait.write("\t}\n")
1126 #out_java_trait.write(java_trait_constr + "\t\t});\n\t}\n")
1128 # Write out a clone function whether we need one or not, as we use them in moving to rust
1129 out_c.write("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
1130 out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1131 out_c.write("\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n")
1132 for var_line in field_var_lines:
1133 if var_line.group(1) in trait_structs:
1134 out_c.write("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
1135 out_c.write("\treturn (void*) this_arg;\n")
1138 out_java.write("\t}\n")
1140 out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
1141 out_c.write("static inline " + struct_name + " " + struct_name + "_init (JNIEnv * env, jclass _a, jobject o")
1142 for var_line in field_var_lines:
1143 if var_line.group(1) in trait_structs:
1144 out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
1145 out_c.write(", jobject " + var_line.group(2))
1146 out_java.write(");\n")
1147 out_c.write(") {\n")
1149 out_c.write("\tjclass c = (*env)->GetObjectClass(env, o);\n")
1150 out_c.write("\tCHECK(c != NULL);\n")
1151 out_c.write("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
1152 out_c.write("\tatomic_init(&calls->refcnt, 1);\n")
1153 out_c.write("\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n")
1154 out_c.write("\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n")
1155 for (fn_line, java_meth_descr) in zip(trait_fn_lines, java_meths):
1156 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
1157 out_c.write("\tcalls->" + fn_line.group(2) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(2) + "\", \"" + java_meth_descr + "\");\n")
1158 out_c.write("\tCHECK(calls->" + fn_line.group(2) + "_meth != NULL);\n")
1159 out_c.write("\n\t" + struct_name + " ret = {\n")
1160 out_c.write("\t\t.this_arg = (void*) calls,\n")
1161 for fn_line in trait_fn_lines:
1162 if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
1163 out_c.write("\t\t." + fn_line.group(2) + " = " + fn_line.group(2) + "_jcall,\n")
1164 elif fn_line.group(2) == "free":
1165 out_c.write("\t\t.free = " + struct_name + "_JCalls_free,\n")
1167 clone_fns.add(struct_name + "_clone")
1168 out_c.write("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
1169 for var_line in field_var_lines:
1170 if var_line.group(1) in trait_structs:
1171 out_c.write("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, _a, " + var_line.group(2) + "),\n")
1172 out_c.write("\t};\n")
1173 for var_line in field_var_lines:
1174 if var_line.group(1) in trait_structs:
1175 out_c.write("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
1176 out_c.write("\treturn ret;\n")
1179 out_c.write("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, jobject o")
1180 for var_line in field_var_lines:
1181 if var_line.group(1) in trait_structs:
1182 out_c.write(", jobject " + var_line.group(2))
1183 out_c.write(") {\n")
1184 out_c.write("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1185 out_c.write("\t*res_ptr = " + struct_name + "_init(env, _a, o")
1186 for var_line in field_var_lines:
1187 if var_line.group(1) in trait_structs:
1188 out_c.write(", " + var_line.group(2))
1190 out_c.write("\treturn (long)res_ptr;\n")
1193 out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
1194 out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (JNIEnv * env, jclass _a, jlong val) {\n")
1195 out_c.write("\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n")
1196 out_c.write("\tCHECK(ret != NULL);\n")
1197 out_c.write("\treturn ret;\n")
1200 for fn_line in trait_fn_lines:
1201 # For now, just disable enabling the _call_log - we don't know how to inverse-map String
1202 is_log = fn_line.group(2) == "log" and struct_name == "LDKLogger"
1203 if fn_line.group(2) != "free" and fn_line.group(2) != "clone" and fn_line.group(2) != "eq" and not is_log:
1204 dummy_line = fn_line.group(1) + struct_name.replace("LDK", "") + "_" + fn_line.group(2) + " " + struct_name + "* this_arg" + fn_line.group(4) + "\n"
1205 map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, "(this_arg_conv->" + fn_line.group(2) + ")(this_arg_conv->this_arg")
1207 out_c.write("""#include \"org_ldk_impl_bindings.h\"
1208 #include <rust_types.h>
1209 #include <lightning.h>
1211 #include <stdatomic.h>
1214 if sys.argv[4] == "false":
1215 out_c.write("#define MALLOC(a, _) malloc(a)\n")
1216 out_c.write("#define FREE(p) if ((p) > 1024) { free(p); }\n")
1217 out_c.write("#define DO_ASSERT(a) (void)(a)\n")
1218 out_c.write("#define CHECK(a)\n")
1220 out_c.write("""#include <assert.h>
1221 // Always run a, then assert it is true:
1222 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
1223 // Assert a is true or do nothing
1224 #define CHECK(a) DO_ASSERT(a)
1226 // Running a leak check across all the allocations and frees of the JDK is a mess,
1227 // so instead we implement our own naive leak checker here, relying on the -wrap
1228 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
1229 // and free'd in Rust or C across the generated bindings shared library.
1230 #include <threads.h>
1231 #include <execinfo.h>
1233 static mtx_t allocation_mtx;
1235 void __attribute__((constructor)) init_mtx() {
1236 DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);
1240 typedef struct allocation {
1241 struct allocation* next;
1243 const char* struct_name;
1247 static allocation* allocation_ll = NULL;
1249 void* __real_malloc(size_t len);
1250 void* __real_calloc(size_t nmemb, size_t len);
1251 static void new_allocation(void* res, const char* struct_name) {
1252 allocation* new_alloc = __real_malloc(sizeof(allocation));
1253 new_alloc->ptr = res;
1254 new_alloc->struct_name = struct_name;
1255 new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
1256 DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
1257 new_alloc->next = allocation_ll;
1258 allocation_ll = new_alloc;
1259 DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
1261 static void* MALLOC(size_t len, const char* struct_name) {
1262 void* res = __real_malloc(len);
1263 new_allocation(res, struct_name);
1266 void __real_free(void* ptr);
1267 static void alloc_freed(void* ptr) {
1268 allocation* p = NULL;
1269 DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
1270 allocation* it = allocation_ll;
1271 while (it->ptr != ptr) { p = it; it = it->next; }
1272 if (p) { p->next = it->next; } else { allocation_ll = it->next; }
1273 DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
1274 DO_ASSERT(it->ptr == ptr);
1277 static void FREE(void* ptr) {
1278 if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
1283 void* __wrap_malloc(size_t len) {
1284 void* res = __real_malloc(len);
1285 new_allocation(res, "malloc call");
1288 void* __wrap_calloc(size_t nmemb, size_t len) {
1289 void* res = __real_calloc(nmemb, len);
1290 new_allocation(res, "calloc call");
1293 void __wrap_free(void* ptr) {
1298 void* __real_realloc(void* ptr, size_t newlen);
1299 void* __wrap_realloc(void* ptr, size_t len) {
1301 void* res = __real_realloc(ptr, len);
1302 new_allocation(res, "realloc call");
1305 void __wrap_reallocarray(void* ptr, size_t new_sz) {
1306 // Rust doesn't seem to use reallocarray currently
1310 void __attribute__((destructor)) check_leaks() {
1311 for (allocation* a = allocation_ll; a != NULL; a = a->next) {
1312 fprintf(stderr, "%s %p remains:\\n", a->struct_name, a->ptr);
1313 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
1314 fprintf(stderr, "\\n\\n");
1316 DO_ASSERT(allocation_ll == NULL);
1319 out_java.write("""package org.ldk.impl;
1320 import org.ldk.enums.*;
1322 public class bindings {
1323 public static class VecOrSliceDef {
1324 public long dataptr;
1325 public long datalen;
1327 public VecOrSliceDef(long dataptr, long datalen, long stride) {
1328 this.dataptr = dataptr; this.datalen = datalen; this.stride = stride;
1332 System.loadLibrary(\"lightningjni\");
1333 init(java.lang.Enum.class, VecOrSliceDef.class);
1335 static native void init(java.lang.Class c, java.lang.Class slicedef);
1337 public static native boolean deref_bool(long ptr);
1338 public static native long deref_long(long ptr);
1339 public static native void free_heap_ptr(long ptr);
1340 public static native byte[] read_bytes(long ptr, long len);
1341 public static native byte[] get_u8_slice_bytes(long slice_ptr);
1342 public static native long bytes_to_u8_vec(byte[] bytes);
1343 public static native long new_txpointer_copy_data(byte[] txdata);
1344 public static native long vec_slice_len(long vec);
1345 public static native long new_empty_slice_vec();
1349 static jmethodID ordinal_meth = NULL;
1350 static jmethodID slicedef_meth = NULL;
1351 static jclass slicedef_cls = NULL;
1352 JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class, jclass slicedef_class) {
1353 ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
1354 CHECK(ordinal_meth != NULL);
1355 slicedef_meth = (*env)->GetMethodID(env, slicedef_class, "<init>", "(JJJ)V");
1356 CHECK(slicedef_meth != NULL);
1357 slicedef_cls = (*env)->NewGlobalRef(env, slicedef_class);
1358 CHECK(slicedef_cls != NULL);
1361 JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_deref_1bool (JNIEnv * env, jclass _a, jlong ptr) {
1362 return *((bool*)ptr);
1364 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_deref_1long (JNIEnv * env, jclass _a, jlong ptr) {
1365 return *((long*)ptr);
1367 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_free_1heap_1ptr (JNIEnv * env, jclass _a, jlong ptr) {
1370 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_read_1bytes (JNIEnv * _env, jclass _b, jlong ptr, jlong len) {
1371 jbyteArray ret_arr = (*_env)->NewByteArray(_env, len);
1372 (*_env)->SetByteArrayRegion(_env, ret_arr, 0, len, (unsigned char*)ptr);
1375 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (JNIEnv * _env, jclass _b, jlong slice_ptr) {
1376 LDKu8slice *slice = (LDKu8slice*)slice_ptr;
1377 jbyteArray ret_arr = (*_env)->NewByteArray(_env, slice->datalen);
1378 (*_env)->SetByteArrayRegion(_env, ret_arr, 0, slice->datalen, slice->data);
1381 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * _env, jclass _b, jbyteArray bytes) {
1382 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
1383 vec->datalen = (*_env)->GetArrayLength(_env, bytes);
1384 vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
1385 (*_env)->GetByteArrayRegion (_env, bytes, 0, vec->datalen, vec->data);
1388 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
1389 LDKTransaction *txdata = (LDKTransaction*)MALLOC(sizeof(LDKTransaction), "LDKTransaction");
1390 txdata->datalen = (*env)->GetArrayLength(env, bytes);
1391 txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
1392 txdata->data_is_owned = true;
1393 (*env)->GetByteArrayRegion (env, bytes, 0, txdata->datalen, txdata->data);
1394 return (long)txdata;
1396 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) {
1397 // Check offsets of a few Vec types are all consistent as we're meant to be generic across types
1398 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically");
1399 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically");
1400 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
1401 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
1402 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
1403 return (long)vec->datalen;
1405 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * _env, jclass _b) {
1406 // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
1407 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
1408 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
1409 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically");
1410 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically");
1411 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
1417 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
1418 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
1419 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
1420 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
1424 with open(sys.argv[3] + "/structs/CommonBase.java", "a") as out_java_struct:
1425 out_java_struct.write("""package org.ldk.structs;
1426 import java.util.LinkedList;
1429 LinkedList<Object> ptrs_to = new LinkedList();
1430 protected CommonBase(long ptr) { this.ptr = ptr; }
1431 public long _test_only_get_ptr() { return this.ptr; }
1435 in_block_comment = False
1436 cur_block_obj = None
1438 const_val_regex = re.compile("^extern const ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
1440 line_indicates_result_regex = re.compile("^ (LDKCResultPtr_[A-Za-z_0-9]*) contents;$")
1441 line_indicates_vec_regex = re.compile("^ ([A-Za-z_0-9]*) \*data;$")
1442 line_indicates_opaque_regex = re.compile("^ bool is_owned;$")
1443 line_indicates_trait_regex = re.compile("^ ([A-Za-z_0-9]* \*?)\(\*([A-Za-z_0-9]*)\)\((const )?void \*this_arg(.*)\);$")
1444 assert(line_indicates_trait_regex.match(" uintptr_t (*send_data)(void *this_arg, LDKu8slice data, bool resume_read);"))
1445 assert(line_indicates_trait_regex.match(" LDKCVec_MessageSendEventZ (*get_and_clear_pending_msg_events)(const void *this_arg);"))
1446 assert(line_indicates_trait_regex.match(" void *(*clone)(const void *this_arg);"))
1447 line_field_var_regex = re.compile("^ ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
1448 assert(line_field_var_regex.match(" LDKMessageSendEventsProvider MessageSendEventsProvider;"))
1449 struct_name_regex = re.compile("^typedef (struct|enum|union) (MUST_USE_STRUCT )?(LDK[A-Za-z_0-9]*) {$")
1450 assert(struct_name_regex.match("typedef struct LDKCVecTempl_u8 {"))
1451 assert(struct_name_regex.match("typedef enum LDKNetwork {"))
1452 struct_alias_regex = re.compile("^typedef (LDK[A-Za-z_0-9]*) (LDK[A-Za-z_0-9]*);$")
1453 assert(struct_alias_regex.match("typedef LDKCResultTempl_bool__PeerHandleError LDKCResult_boolPeerHandleErrorZ;"))
1455 result_templ_structs = set()
1456 union_enum_items = {}
1457 result_ptr_struct_items = {}
1459 if in_block_comment:
1460 if line.endswith("*/\n"):
1461 in_block_comment = False
1462 elif cur_block_obj is not None:
1463 cur_block_obj = cur_block_obj + line
1464 if line.startswith("} "):
1468 obj_lines = cur_block_obj.split("\n")
1470 result_contents = None
1471 is_unitary_enum = False
1472 is_union_enum = False
1476 field_var_lines = []
1478 for idx, struct_line in enumerate(obj_lines):
1479 if struct_line.strip().startswith("/*"):
1480 in_block_comment = True
1481 if in_block_comment:
1482 if struct_line.endswith("*/"):
1483 in_block_comment = False
1485 struct_name_match = struct_name_regex.match(struct_line)
1486 if struct_name_match is not None:
1487 struct_name = struct_name_match.group(3)
1488 if struct_name_match.group(1) == "enum":
1489 if not struct_name.endswith("_Tag"):
1490 is_unitary_enum = True
1492 is_union_enum = True
1493 elif struct_name_match.group(1) == "union":
1495 if line_indicates_opaque_regex.match(struct_line):
1497 result_match = line_indicates_result_regex.match(struct_line)
1498 if result_match is not None:
1499 result_contents = result_match.group(1)
1500 vec_ty_match = line_indicates_vec_regex.match(struct_line)
1501 if vec_ty_match is not None and struct_name.startswith("LDKCVecTempl_"):
1502 vec_ty = vec_ty_match.group(1)
1503 elif struct_name.startswith("LDKC2TupleTempl_") or struct_name.startswith("LDKC3TupleTempl_"):
1505 trait_fn_match = line_indicates_trait_regex.match(struct_line)
1506 if trait_fn_match is not None:
1507 trait_fn_lines.append(trait_fn_match)
1508 field_var_match = line_field_var_regex.match(struct_line)
1509 if field_var_match is not None:
1510 field_var_lines.append(field_var_match)
1511 field_lines.append(struct_line)
1513 assert(struct_name is not None)
1514 assert(len(trait_fn_lines) == 0 or not (is_opaque or is_unitary_enum or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1515 assert(not is_opaque or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1516 assert(not is_unitary_enum or not (len(trait_fn_lines) != 0 or is_opaque or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1517 assert(not is_union_enum or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_opaque or is_union or result_contents is not None or vec_ty is not None))
1518 assert(not is_union or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or result_contents is not None or vec_ty is not None))
1519 assert(result_contents is None or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or is_union or vec_ty is not None))
1520 assert(vec_ty is None or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or is_union or result_contents is not None))
1523 opaque_structs.add(struct_name)
1524 with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_struct:
1525 out_java_struct.write(hu_struct_file_prefix)
1526 out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase")
1527 if struct_name.startswith("LDKLocked"):
1528 out_java_struct.write(" implements AutoCloseable")
1529 out_java_struct.write(" {\n")
1530 out_java_struct.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); }\n")
1531 if struct_name.startswith("LDKLocked"):
1532 out_java_struct.write("\t@Override public void close() {\n")
1534 out_java_struct.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
1535 out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
1536 out_java_struct.write("\t\tsuper.finalize();\n")
1537 out_java_struct.write("\t\tbindings." + struct_name.replace("LDK","") + "_free(ptr);\n")
1538 out_java_struct.write("\t}\n\n")
1539 elif result_contents is not None:
1540 result_templ_structs.add(struct_name)
1541 assert result_contents in result_ptr_struct_items
1542 elif struct_name.startswith("LDKCResultPtr_"):
1543 for line in field_lines:
1544 if line.endswith("*result;"):
1545 res_ty = line[:-8].strip()
1546 elif line.endswith("*err;"):
1547 err_ty = line[:-5].strip()
1548 result_ptr_struct_items[struct_name] = (res_ty, err_ty)
1550 out_java.write("\tpublic static native long " + struct_name + "_new(")
1551 out_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new(JNIEnv *_env, jclass _b")
1553 for idx, line in enumerate(field_lines):
1554 if idx != 0 and idx < len(field_lines) - 2:
1555 ty_info = java_c_types(line.strip(';'), None)
1557 out_java.write(", ")
1558 e = chr(ord('a') + idx - 1)
1559 out_java.write(ty_info.java_ty + " " + e)
1560 out_c.write(", " + ty_info.c_ty + " " + e)
1561 ty_list.append(ty_info)
1562 tuple_types[struct_name] = (ty_list, struct_name)
1563 out_java.write(");\n")
1564 out_c.write(") {\n")
1565 out_c.write("\t" + struct_name + "* ret = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1566 for idx, line in enumerate(field_lines):
1567 if idx != 0 and idx < len(field_lines) - 2:
1568 ty_info = map_type(line.strip(';'), False, None, False)
1569 e = chr(ord('a') + idx - 1)
1570 if ty_info.arg_conv is not None:
1571 out_c.write("\t" + ty_info.arg_conv.replace("\n", "\n\t"))
1572 out_c.write("\n\tret->" + e + " = " + ty_info.arg_conv_name + ";\n")
1574 out_c.write("\tret->" + e + " = " + e + ";\n")
1575 if ty_info.arg_conv_cleanup is not None:
1576 out_c.write("\t//TODO: Really need to call " + ty_info.arg_conv_cleanup + " here\n")
1577 out_c.write("\treturn (long)ret;\n")
1579 elif vec_ty is not None:
1580 if vec_ty in opaque_structs:
1581 out_java.write("\tpublic static native long[] " + struct_name + "_arr_info(long vec_ptr);\n")
1582 out_c.write("JNIEXPORT jlongArray JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1arr_1info(JNIEnv *env, jclass _b, jlong ptr) {\n")
1584 out_java.write("\tpublic static native VecOrSliceDef " + struct_name + "_arr_info(long vec_ptr);\n")
1585 out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1arr_1info(JNIEnv *env, jclass _b, jlong ptr) {\n")
1586 out_c.write("\t" + struct_name + " *vec = (" + struct_name + "*)ptr;\n")
1587 if vec_ty in opaque_structs:
1588 out_c.write("\tjlongArray ret = (*env)->NewLongArray(env, vec->datalen);\n")
1589 out_c.write("\tjlong *ret_elems = (*env)->GetPrimitiveArrayCritical(env, ret, NULL);\n")
1590 out_c.write("\tfor (size_t i = 0; i < vec->datalen; i++) {\n")
1591 out_c.write("\t\tCHECK((((long)vec->data[i].inner) & 1) == 0);\n")
1592 out_c.write("\t\tret_elems[i] = (long)vec->data[i].inner | (vec->data[i].is_owned ? 1 : 0);\n")
1593 out_c.write("\t}\n")
1594 out_c.write("\t(*env)->ReleasePrimitiveArrayCritical(env, ret, ret_elems, 0);\n")
1595 out_c.write("\treturn ret;\n")
1597 out_c.write("\treturn (*env)->NewObject(env, slicedef_cls, slicedef_meth, (long)vec->data, (long)vec->datalen, sizeof(" + vec_ty + "));\n")
1600 ty_info = map_type(vec_ty + " arr_elem", False, None, False)
1601 if len(ty_info.java_fn_ty_arg) == 1: # ie we're a primitive of some form
1602 out_java.write("\tpublic static native long " + struct_name + "_new(" + ty_info.java_ty + "[] elems);\n")
1603 out_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new(JNIEnv *env, jclass _b, j" + ty_info.java_ty + "Array elems){\n")
1604 out_c.write("\t" + struct_name + " *ret = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1605 out_c.write("\tret->datalen = (*env)->GetArrayLength(env, elems);\n")
1606 out_c.write("\tif (ret->datalen == 0) {\n")
1607 out_c.write("\t\tret->data = NULL;\n")
1608 out_c.write("\t} else {\n")
1609 out_c.write("\t\tret->data = MALLOC(sizeof(" + vec_ty + ") * ret->datalen, \"" + struct_name + " Data\");\n")
1610 out_c.write("\t\t" + ty_info.c_ty + " *java_elems = (*env)->GetPrimitiveArrayCritical(env, elems, NULL);\n")
1611 out_c.write("\t\tfor (size_t i = 0; i < ret->datalen; i++) {\n")
1612 if ty_info.arg_conv is not None:
1613 out_c.write("\t\t\t" + ty_info.c_ty + " arr_elem = java_elems[i];\n")
1614 out_c.write("\t\t\t" + ty_info.arg_conv.replace("\n", "\n\t\t\t") + "\n")
1615 out_c.write("\t\t\tret->data[i] = " + ty_info.arg_conv_name + ";\n")
1616 assert ty_info.arg_conv_cleanup is None
1618 out_c.write("\t\t\tret->data[i] = java_elems[i];\n")
1619 out_c.write("\t\t}\n")
1620 out_c.write("\t\t(*env)->ReleasePrimitiveArrayCritical(env, elems, java_elems, 0);\n")
1621 out_c.write("\t}\n")
1622 out_c.write("\treturn (long)ret;\n")
1625 assert(struct_name.endswith("_Tag"))
1626 struct_name = struct_name[:-4]
1627 union_enum_items[struct_name] = {"field_lines": field_lines}
1628 elif struct_name.endswith("_Body") and struct_name.split("_")[0] in union_enum_items:
1629 enum_var_name = struct_name.split("_")
1630 union_enum_items[enum_var_name[0]][enum_var_name[1]] = field_lines
1631 elif struct_name in union_enum_items:
1632 map_complex_enum(struct_name, union_enum_items[struct_name])
1633 elif is_unitary_enum:
1634 map_unitary_enum(struct_name, field_lines)
1635 elif len(trait_fn_lines) > 0:
1636 trait_structs.add(struct_name)
1637 map_trait(struct_name, field_var_lines, trait_fn_lines)
1638 elif struct_name == "LDKTxOut":
1639 with open(sys.argv[3] + "/structs/TxOut.java", "w") as out_java_struct:
1640 out_java_struct.write(hu_struct_file_prefix)
1641 out_java_struct.write("public class TxOut extends CommonBase{\n")
1642 out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n")
1644 out_java_struct.write("}")
1645 elif struct_name == "LDKTransaction":
1646 with open(sys.argv[3] + "/structs/Transaction.java", "w") as out_java_struct:
1647 out_java_struct.write(hu_struct_file_prefix)
1648 out_java_struct.write("public class Transaction extends CommonBase{\n")
1649 out_java_struct.write("\tTransaction(java.lang.Object _dummy, long ptr) { super(ptr); }\n")
1650 # TODO: Transaction body
1651 out_java_struct.write("}")
1653 pass # Everything remaining is a byte[] or some form
1654 cur_block_obj = None
1656 fn_ptr = fn_ptr_regex.match(line)
1657 fn_ret_arr = fn_ret_arr_regex.match(line)
1658 reg_fn = reg_fn_regex.match(line)
1659 const_val = const_val_regex.match(line)
1661 if line.startswith("#include <"):
1663 elif line.startswith("/*"):
1664 #out_java.write("\t" + line)
1665 if not line.endswith("*/\n"):
1666 in_block_comment = True
1667 elif line.startswith("typedef enum "):
1668 cur_block_obj = line
1669 elif line.startswith("typedef struct "):
1670 cur_block_obj = line
1671 elif line.startswith("typedef union "):
1672 cur_block_obj = line
1673 elif line.startswith("typedef "):
1674 alias_match = struct_alias_regex.match(line)
1675 if alias_match.group(1) in tuple_types:
1676 tuple_types[alias_match.group(2)] = (tuple_types[alias_match.group(1)][0], alias_match.group(2))
1677 tuple_types[alias_match.group(1)] = (tuple_types[alias_match.group(1)][0], alias_match.group(2))
1678 for idx, ty_info in enumerate(tuple_types[alias_match.group(1)][0]):
1679 e = chr(ord('a') + idx)
1680 out_java.write("\tpublic static native " + ty_info.java_ty + " " + alias_match.group(2) + "_get_" + e + "(long ptr);\n")
1681 # XXX: Write C method!
1682 elif alias_match.group(1) in result_templ_structs:
1683 human_ty = alias_match.group(2).replace("LDKCResult", "Result_").replace("__", "_").replace("Templ", "")
1684 with open(sys.argv[3] + "/structs/" + human_ty + ".java", "w") as out_java_struct:
1685 out_java_struct.write(hu_struct_file_prefix)
1686 out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
1687 out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
1688 out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
1689 out_java_struct.write("\t\tbindings." + alias_match.group(2).replace("LDK","") + "_free(ptr); super.finalize();\n")
1690 out_java_struct.write("\t}\n\n")
1692 contents_ty = alias_match.group(1).replace("LDKCResultTempl", "LDKCResultPtr")
1693 res_ty, err_ty = result_ptr_struct_items[contents_ty]
1694 res_map = map_type(res_ty + " res", True, None, False)
1695 err_map = map_type(err_ty + " err", True, None, False)
1697 out_java.write("\tpublic static native boolean " + alias_match.group(2) + "_result_ok(long arg);\n")
1698 out_c.write("JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1result_1ok (JNIEnv * env, jclass _a, jlong arg) {\n")
1699 out_c.write("\treturn ((" + alias_match.group(2) + "*)arg)->result_ok;\n")
1702 out_java.write("\tpublic static native " + res_map.java_ty + " " + alias_match.group(2) + "_get_ok(long arg);\n")
1703 out_c.write("JNIEXPORT " + res_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1get_1ok (JNIEnv * _env, jclass _a, jlong arg) {\n")
1704 out_c.write("\t" + alias_match.group(2) + " *val = (" + alias_match.group(2) + "*)arg;\n")
1705 out_c.write("\tCHECK(val->result_ok);\n\t")
1706 out_java_struct.write("\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n")
1707 if res_map.ret_conv is not None:
1708 out_c.write(res_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.result)")
1709 out_c.write(res_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + res_map.ret_conv_name)
1711 out_c.write("return *val->contents.result")
1712 out_c.write(";\n}\n")
1714 out_java_struct.write("\t\tpublic " + res_map.java_hu_ty + " res;\n")
1715 out_java_struct.write("\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n")
1716 out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
1717 if res_map.to_hu_conv is not None:
1718 out_java_struct.write("\t\t\t" + res_map.java_ty + " res = bindings." + alias_match.group(2) + "_get_ok(ptr);\n")
1719 out_java_struct.write("\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t"))
1720 out_java_struct.write("\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n")
1722 out_java_struct.write("\t\t\tthis.res = bindings." + alias_match.group(2) + "_get_ok(ptr);\n")
1723 out_java_struct.write("\t\t}\n\n")
1725 out_java.write("\tpublic static native " + err_map.java_ty + " " + alias_match.group(2) + "_get_err(long arg);\n")
1726 out_c.write("JNIEXPORT " + err_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1get_1err (JNIEnv * _env, jclass _a, jlong arg) {\n")
1727 out_c.write("\t" + alias_match.group(2) + " *val = (" + alias_match.group(2) + "*)arg;\n")
1728 out_c.write("\tCHECK(!val->result_ok);\n\t")
1729 out_java_struct.write("\t}\n\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n")
1730 if err_map.ret_conv is not None:
1731 out_c.write(err_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.err)")
1732 out_c.write(err_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + err_map.ret_conv_name)
1734 out_c.write("return *val->contents.err")
1735 out_c.write(";\n}\n")
1737 out_java_struct.write("\t\tpublic " + err_map.java_hu_ty + " err;\n")
1738 out_java_struct.write("\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n")
1739 out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
1740 if err_map.to_hu_conv is not None:
1741 out_java_struct.write("\t\t\t" + err_map.java_ty + " err = bindings." + alias_match.group(2) + "_get_err(ptr);\n")
1742 out_java_struct.write("\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t"))
1743 out_java_struct.write("\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n")
1745 out_java_struct.write("\t\t\tthis.err = bindings." + alias_match.group(2) + "_get_err(ptr);\n")
1746 out_java_struct.write("\t\t}\n\t}\n}\n")
1747 elif fn_ptr is not None:
1748 map_fn(line, fn_ptr, None, None)
1749 elif fn_ret_arr is not None:
1750 map_fn(line, fn_ret_arr, fn_ret_arr.group(4), None)
1751 elif reg_fn is not None:
1752 map_fn(line, reg_fn, None, None)
1753 elif const_val_regex is not None:
1754 # TODO Map const variables
1757 assert(line == "\n")
1759 out_java.write("}\n")
1760 for struct_name in opaque_structs:
1761 with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "a") as out_java_struct:
1762 out_java_struct.write("}\n")
1763 for struct_name in trait_structs:
1764 with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "a") as out_java_struct:
1765 out_java_struct.write("}\n")