From: Matt Corallo Date: Thu, 29 Sep 2022 22:41:08 +0000 (+0000) Subject: Add support for mapping LDKBigEndianScalar manually, ala TxOut X-Git-Tag: v0.0.111.0^2~9 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=2eb87ba813b7b6c1669a8a0d900a2baa67bfea3e;p=ldk-java Add support for mapping LDKBigEndianScalar manually, ala TxOut --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index ccb5f817..647bd959 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -489,7 +489,7 @@ class TypeMappingGenerator: to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";" + to_hu_conv_sfx, to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv) - # The manually-defined types - TxOut, u5, and Error + # The manually-defined types - TxOut, BigEndianScalar, u5, and Error if ty_info.rust_obj == "LDKError": assert from_hu_conv is None return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, @@ -506,12 +506,12 @@ class TypeMappingGenerator: to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " + ty_info.java_hu_ty + "(" + ty_info.var_name + ")") + ";", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".getVal()", "")) - assert ty_info.rust_obj == "LDKTxOut" + assert ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKBigEndianScalar" if not ty_info.is_ptr and not holds_ref: - ret_conv = ("LDKTxOut* " + ty_info.var_name + "_ref = MALLOC(sizeof(LDKTxOut), \"LDKTxOut\");\n*" + ty_info.var_name + "_ref = ", ";") + ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_ref = ", ";") ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, true)" else: - ret_conv = ("LDKTxOut* " + ty_info.var_name + "_ref = &", ";") + ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = &", ";") ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, false)" return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, diff --git a/genbindings.py b/genbindings.py index b8e58bc5..1ade1705 100755 --- a/genbindings.py +++ b/genbindings.py @@ -607,7 +607,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", expected_struct in complex_enums or expected_struct in complex_enums or expected_struct in result_types or expected_struct in tuple_types) and not is_free impl_on_utils = not impl_on_struct and (not is_free and not method_name.endswith("_clone") and - not method_name.startswith("TxOut") and + not method_name.startswith("TxOut") and not method_name.startswith("BigEndianScalar") and not method_name.startswith("_") and method_name != "check_platform" and method_name != "Result_read" and not expected_struct in unitary_enums and @@ -1054,8 +1054,27 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", write_c("\treturn thing->value;") write_c("}") map_fn(fn_line + "\n", re.compile("(.*) (TxOut_get_value) \((.*)\)").match(fn_line), None, None, None) + elif struct_name == "LDKBigEndianScalar": + with open(f"{sys.argv[3]}/structs/BigEndianScalar{consts.file_ext}", "w") as out_java_struct: + out_java_struct.write(consts.hu_struct_file_prefix) + out_java_struct.write(consts.scalar_defn) + fn_line = "struct LDKThirtyTwoBytes BigEndianScalar_get_bytes (struct LDKBigEndianScalar* thing)" + write_c(fn_line + " {\n") + write_c("\tLDKThirtyTwoBytes ret = { .data = *thing->big_endian_bytes };\n") + write_c("\treturn ret;\n") + write_c("}\n") + map_fn(fn_line + "\n", re.compile("(.*) (BigEndianScalar_get_bytes) \((.*)\)").match(fn_line), None, None, None) + + # We need to be able to FREE a heap-allocated BigEndianScalar, but because + # there's nothing heap-allocated inside it the C bindings don't bother + # exposing a `_free` method. Instead, we have to manually write one here, + # though it doesn't need to do anything, the autogenerated wrapper will do + # the required FREE. + fn_line = "static void BigEndianScalar_free (struct LDKBigEndianScalar thing)" + write_c(fn_line + " {}\n") + map_fn(fn_line + "\n", re.compile("static (.*) (BigEndianScalar_free) \((.*)\)").match(fn_line), None, None, None) else: - pass # Everything remaining is a byte[] or some form + pass # Everything remaining is a byte[] of some form cur_block_obj = None else: fn_ptr = fn_ptr_regex.match(line) diff --git a/java_strings.py b/java_strings.py index c249c200..487df5d6 100644 --- a/java_strings.py +++ b/java_strings.py @@ -151,6 +151,27 @@ class CommonBase { } }""" + self.scalar_defn = """public class BigEndianScalar extends CommonBase { + /** The bytes of the scalar value, in big endian */ + public final byte[] scalar_bytes; + + BigEndianScalar(java.lang.Object _dummy, long ptr) { + super(ptr); + this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr); + } + public BigEndianScalar(byte[] scalar_bytes) { + super(bindings.BigEndianScalar_new(scalar_bytes)); + this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr); + } + + @Override @SuppressWarnings(\"deprecation\") + protected void finalize() throws Throwable { + super.finalize(); + if (ptr != 0) { bindings.BigEndianScalar_free(ptr); } + } +}""" + + self.c_file_pfx = """#include // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here #define int64_t jlong diff --git a/typescript_strings.py b/typescript_strings.py index cc7b2363..e5cf6132 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -403,6 +403,21 @@ export class UnqualifiedError { }""" self.obj_defined(["TxOut"], "structs") + self.scalar_defn = """export class BigEndianScalar extends CommonBase { + /** The bytes of the scalar value, in big endian */ + public scalar_bytes: Uint8Array; + + /* @internal */ + public constructor(_dummy: object, ptr: bigint) { + super(ptr, bindings.BigEndianScalar_free); + this.scalar_bytes = bindings.decodeUint8Array(bindings.BigEndianScalar_get_bytes(ptr)); + } + public static constructor_new(scalar_bytes: Uint8Array): BigEndianScalar { + return new BigEndianScalar(null, bindings.BigEndianScalar_new(bindings.encodeUint8Array(scalar_bytes))); + } +}""" + self.obj_defined(["BigEndianScalar"], "structs") + self.c_file_pfx = """#include "js-wasm.h" #include #include