]> git.bitcoin.ninja Git - ldk-java/commitdiff
Add support for mapping LDKBigEndianScalar manually, ala TxOut
authorMatt Corallo <git@bluematt.me>
Thu, 29 Sep 2022 22:41:08 +0000 (22:41 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 3 Oct 2022 20:38:23 +0000 (20:38 +0000)
gen_type_mapping.py
genbindings.py
java_strings.py
typescript_strings.py

index ccb5f8172e03e85b86d6fbc668e165af376db6c2..647bd959575ad7fac849d448853cf7c0a5db1bf9 100644 (file)
@@ -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,
index b8e58bc5b0e24a20c1e51a909b52965042650cb7..1ade1705010d1f9055fe236b0be157cb9c091146 100755 (executable)
@@ -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)
index c249c200de73f7c0b3c33a393c922bca21c9785d..487df5d612d66475eca01cc196b342147052262c 100644 (file)
@@ -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 <jni.h>
 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
 #define int64_t jlong
index cc7b236315c6be51e1f593b74f3a04be3916fb4f..e5cf6132ed2be9c5b7ee4a572fdd590f3d392074 100644 (file)
@@ -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 <stdatomic.h>
 #include <lightning.h>