From: Arik Sosman Date: Tue, 12 Jan 2021 16:26:26 +0000 (-0500) Subject: Use file_ext everywhere, common base/header for TS files X-Git-Tag: v0.0.1~63 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=55918fad1311e286d1698576e4ba163ab55f815b;p=ldk-java Use file_ext everywhere, common base/header for TS files --- diff --git a/genbindings.py b/genbindings.py index 24dbf20c..e684a420 100755 --- a/genbindings.py +++ b/genbindings.py @@ -788,7 +788,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_struct = None if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free: - out_java_struct = open(sys.argv[3] + "/structs/" + struct_meth + ".java", "a") + out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a") if not args_known: out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n") out_java_struct.close() @@ -909,7 +909,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_struct.close() def map_unitary_enum(struct_name, field_lines): - with open(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum: + with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum: unitary_enums.add(struct_name) for idx, struct_line in enumerate(field_lines): if idx == 0: @@ -1029,7 +1029,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_enum.write("}\n") def map_trait(struct_name, field_var_lines, trait_fn_lines): - with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "w") as out_java_trait: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait: field_var_convs = [] for var_line in field_var_lines: if var_line.group(1) in trait_structs: @@ -1272,7 +1272,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: def map_result(struct_name, res_ty, err_ty): result_types.add(struct_name) human_ty = struct_name.replace("LDKCResult", "Result") - with open(sys.argv[3] + "/structs/" + human_ty + consts.file_ext, "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class " + human_ty + " extends CommonBase {\n") out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n") @@ -1495,16 +1495,8 @@ public class bindings { """) - with open(sys.argv[3] + "/structs/CommonBase" + consts.file_ext, "a") as out_java_struct: - out_java_struct.write("""package org.ldk.structs; -import java.util.LinkedList; -class CommonBase { - long ptr; - LinkedList ptrs_to = new LinkedList(); - protected CommonBase(long ptr) { this.ptr = ptr; } - public long _test_only_get_ptr() { return this.ptr; } -} -""") + with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "a") as out_java_struct: + out_java_struct.write(consts.common_base) in_block_comment = False cur_block_obj = None @@ -1594,7 +1586,7 @@ class CommonBase { if is_opaque: opaque_structs.add(struct_name) - with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase") if struct_name.startswith("LDKLocked"): @@ -1680,7 +1672,7 @@ class CommonBase { trait_structs.add(struct_name) map_trait(struct_name, field_var_lines, trait_fn_lines) elif struct_name == "LDKTxOut": - with open(sys.argv[3] + "/structs/TxOut" + consts.file_ext, "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class TxOut extends CommonBase{\n") out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n") @@ -1727,10 +1719,10 @@ class CommonBase { out_java.write("}\n") for struct_name in opaque_structs: - with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "a") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct: out_java_struct.write("}\n") for struct_name in trait_structs: - with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "a") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct: out_java_struct.write("}\n") with open(sys.argv[4], "w") as out_c: out_c.write(consts.c_file_pfx) diff --git a/java_strings.py b/java_strings.py index 45bb7e82..5aefd223 100644 --- a/java_strings.py +++ b/java_strings.py @@ -2,6 +2,16 @@ from bindingstypes import * class Consts: def __init__(self, DEBUG): + self.common_base = """package org.ldk.structs; +import java.util.LinkedList; +class CommonBase { + long ptr; + LinkedList ptrs_to = new LinkedList(); + protected CommonBase(long ptr) { this.ptr = ptr; } + public long _test_only_get_ptr() { return this.ptr; } +} +""" + self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\" #include #include diff --git a/typescript_strings.py b/typescript_strings.py index 70e37331..dccd8cf3 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -1,5 +1,14 @@ class Consts: def __init__(self, DEBUG): + self.common_base = """ + export default class CommonBase { + ptr: number; + ptrs_to: object[] = new Array(); // new LinkedList(); TODO: build linked list implementation + protected constructor(ptr: number) { this.ptr = ptr; } + public _test_only_get_ptr(): number { return this.ptr; } + } + """ + self.c_file_pfx = """#include #include #include @@ -129,14 +138,10 @@ typedef bool jboolean; """ - self.hu_struct_file_prefix = """package org.ldk.structs; - -import org.ldk.impl.bindings; -import org.ldk.enums.*; -import org.ldk.util.*; -import java.util.Arrays; + self.hu_struct_file_prefix = f""" +import CommonBase from './CommonBase'; +import * as bindings from '../bindings' // TODO: figure out location -@SuppressWarnings("unchecked") // We correctly assign various generic arrays """ self.c_fn_ty_pfx = "" self.c_fn_name_pfx = ""