unitary_enums = set()
complex_enums = set()
opaque_structs = set()
-trait_structs = set()
+trait_structs = {}
result_types = set()
tuple_types = {}
def map_trait(struct_name, field_var_lines, trait_fn_lines, trait_doc_comment):
with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait:
field_var_convs = []
+ flattened_field_var_convs = []
for var_line in field_var_lines:
if var_line.group(1) in trait_structs:
- field_var_convs.append((var_line.group(1), var_line.group(2)))
+ field_var_convs.append((var_line.group(1), var_line.group(2), trait_structs[var_line.group(1)]))
+ flattened_field_var_convs.append((var_line.group(1), var_line.group(2), ))
+ flattened_field_var_convs.extend(trait_structs[var_line.group(1)])
else:
- field_var_convs.append(
- type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False))
+ mapped = type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False)
+ field_var_convs.append(mapped)
+ flattened_field_var_convs.append(mapped)
+ trait_structs[struct_name] = field_var_convs
field_fns = []
for fn_docs, fn_line in trait_fn_lines:
arg_tys.append(arg_conv_info)
field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys, fn_docs))
- (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, field_fns, trait_doc_comment)
+ (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, flattened_field_var_convs, field_fns, trait_doc_comment)
write_c(out_c_addendum)
out_java_trait.write(out_java_trait_addendum)
out_java.write(out_java_addendum)
map_unitary_enum(struct_name, field_lines, last_block_comment)
last_block_comment = None
elif len(trait_fn_lines) > 0:
- trait_structs.add(struct_name)
map_trait(struct_name, field_var_lines, trait_fn_lines, last_block_comment)
elif struct_name == "LDKTxOut":
with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct:
ret = ret + ", " + param
return ret + ")"
- def native_c_map_trait(self, struct_name, field_vars, field_fns, trait_doc_comment):
+ def native_c_map_trait(self, struct_name, field_vars, flattened_field_vars, field_fns, trait_doc_comment):
out_java_trait = ""
out_java = ""
out_java_trait = out_java_trait + "\tfinal bindings." + struct_name + " bindings_instance;\n"
out_java_trait = out_java_trait + "\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n"
out_java_trait = out_java_trait + "\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
out_java_trait = out_java_trait + ", " + var.java_hu_ty + " " + var.arg_name
else:
out_java_trait = out_java_trait + ", bindings." + var[0] + " " + var[1]
out_java_trait = out_java_trait + ") {\n"
out_java_trait = out_java_trait + "\t\tsuper(bindings." + struct_name + "_new(arg"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
if var.from_hu_conv is not None:
out_java_trait = out_java_trait + ", " + var.from_hu_conv[0]
out_java_trait = out_java_trait + ", " + var[1]
out_java_trait = out_java_trait + "));\n"
out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(arg);\n"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
if var.from_hu_conv is not None and var.from_hu_conv[1] != "":
out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
java_trait_constr = java_trait_constr + ", " + var.java_hu_ty + " " + var.arg_name
else:
if isinstance(var, ConvInfo):
java_trait_constr = java_trait_constr + ", " + var.arg_name
else:
- java_trait_constr = java_trait_constr + ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance"
+ java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ java_trait_constr += ", " + suparg.arg_name
+ else:
+ java_trait_constr += ", " + suparg[1]
+ java_trait_constr += ").bindings_instance"
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ java_trait_constr += ", " + suparg.arg_name
+ else:
+ java_trait_constr += ", " + suparg[1]
out_java_trait = out_java_trait + "\t}\n"
out_java_trait = out_java_trait + java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
out_java = out_java + "\t}\n"
out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
out_java = out_java + ", " + var.java_ty + " " + var.arg_name
else:
out_c = out_c + "\tatomic_size_t refcnt;\n"
out_c = out_c + "\tJavaVM *vm;\n"
out_c = out_c + "\tjweak o;\n"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
# We're a regular ol' field
pass
out_c = out_c + "}\n"
out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.c_ty + " " + var.arg_name
else:
out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo) and var.arg_conv is not None:
out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
out_c = out_c + "\n\t" + struct_name + " ret = {\n"
out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
else:
- out_c = out_c + "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1] + "),\n"
+ out_c += "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1]
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ out_c = out_c + ", " + suparg.arg_name
+ else:
+ out_c = out_c + ", " + suparg[1]
+ out_c += "),\n"
out_c = out_c + "\t};\n"
- for var in field_vars:
+ for var in flattened_field_vars:
if not isinstance(var, ConvInfo):
out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
out_c = out_c + "\treturn ret;\n"
out_c = out_c + "}\n"
out_c = out_c + self.c_fn_ty_pfx + "int64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.c_ty + " " + var.arg_name
else:
out_c = out_c + ") {\n"
out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
- for var in field_vars:
+ for var in flattened_field_vars:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.arg_name
else:
ret = ret + "; (void) " + param
return ret
- def native_c_map_trait(self, struct_name, field_var_conversions, field_function_lines, trait_doc_comment):
+ def native_c_map_trait(self, struct_name, field_var_conversions, flattened_field_var_conversions, field_function_lines, trait_doc_comment):
out_typescript_bindings = "\n\n\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: START\n\n"
constructor_arguments = ""
super_instantiator = ""
pointer_to_adder = ""
impl_constructor_arguments = ""
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
constructor_arguments += f", {first_to_lower(var.arg_name)}?: {var.java_hu_ty}"
impl_constructor_arguments += f", {var.arg_name}: {var.java_hu_ty}"
if isinstance(var, ConvInfo):
trait_constructor_arguments += ", " + var.arg_name
else:
- trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance"
+ trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ trait_constructor_arguments += ", " + suparg.arg_name
+ else:
+ trait_constructor_arguments += ", " + suparg[1]
+ trait_constructor_arguments += ").bindings_instance"
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ trait_constructor_arguments += ", " + suparg.arg_name
+ else:
+ trait_constructor_arguments += ", " + suparg[1]
out_typescript_human = f"""
{self.hu_struct_file_prefix}
out_typescript_bindings = out_typescript_bindings + "\t\t}\n\n"
out_typescript_bindings += f"\t\texport function {struct_name}_new(impl: {struct_name}"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
out_typescript_bindings += f", {var.arg_name}: {var.java_ty}"
else:
# Now that we've written out our java code (and created java_meths), generate C
out_c = "typedef struct " + struct_name + "_JCalls {\n"
out_c = out_c + "\tatomic_size_t refcnt;\n"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
# We're a regular ol' field
pass
out_c = out_c + "}\n"
out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (/*TODO: JS Object Reference */void* o"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.c_ty + " " + var.arg_name
else:
out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo) and var.arg_conv is not None:
out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
out_c = out_c + "\n\t" + struct_name + " ret = {\n"
out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
else:
- out_c = out_c + "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1] + "),\n"
+ out_c += "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1]
+ for suparg in var[2]:
+ if isinstance(suparg, ConvInfo):
+ out_c += ", " + suparg.arg_name
+ else:
+ out_c += ", " + suparg[1]
+ out_c += "),\n"
out_c = out_c + "\t};\n"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if not isinstance(var, ConvInfo):
out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
out_c = out_c + "\treturn ret;\n"
out_c = out_c + "}\n"
out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "/*TODO: JS Object Reference */void* o"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.c_ty + " " + var.arg_name
else:
out_c = out_c + ") {\n"
out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
out_c = out_c + "\t*res_ptr = " + struct_name + "_init(o"
- for var in field_var_conversions:
+ for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.arg_name
else: