]> git.bitcoin.ninja Git - ldk-java/commitdiff
Make array range iteration language-specific
authorMatt Corallo <git@bluematt.me>
Fri, 7 Jan 2022 22:20:06 +0000 (22:20 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 8 Jan 2022 04:06:53 +0000 (04:06 +0000)
gen_type_mapping.py
java_strings.py
typescript_strings.py

index 76510119d4fa63af06ecb4978bee3e5abebe24cc..fb810c92319b6f3b9152db84b9419c1e25c60691 100644 (file)
@@ -188,7 +188,7 @@ class TypeMappingGenerator:
                     if "[" in ty_info.subty.java_hu_ty.split("<")[0]:
                         # Do a bit of a dance to move any excess [] to the end
                         to_hu_conv += "[" + ty_info.subty.java_hu_ty.split("<")[0].split("[")[1]
-                    to_hu_conv += ";\nfor (int " + idxc + " = 0; " + idxc + " < " + arr_name + ".length; " + idxc + "++) {\n"
+                    to_hu_conv += ";\n" + self.consts.for_n_in_range(idxc, "0", arr_name + ".length") + "\n"
                     to_hu_conv += "\t" + self.consts.var_decl_statement(subty.java_ty, conv_name, arr_name + "[" + idxc + "]") + ";\n"
                     to_hu_conv += "\t" + subty.to_hu_conv.replace("\n", "\n\t") + "\n"
                     to_hu_conv += "\t" + conv_name + "_arr[" + idxc + "] = " + subty.to_hu_conv_name + ";\n}"
@@ -197,7 +197,8 @@ class TypeMappingGenerator:
                 if subty.from_hu_conv is not None:
                     hu_conv_b = ""
                     if subty.from_hu_conv[1] != "":
-                        hu_conv_b = "for (" + subty.java_hu_ty + " " + conv_name + ": " + arr_name + ") { " + subty.from_hu_conv[1] + "; }"
+                        iterator = self.consts.for_n_in_arr(conv_name, arr_name, subty)
+                        hu_conv_b = iterator[0] + subty.from_hu_conv[1] + ";" + iterator[1]
                     from_hu_conv = (self.consts.map_hu_array_elems(arr_name, conv_name, ty_info, subty), hu_conv_b)
 
                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
index cb23b56a6d01332e8e25a8c8de0dc56fab45ae95..535646b94ad623ac0d2b8cbdd73fc83921954014 100644 (file)
@@ -628,6 +628,11 @@ import javax.annotation.Nullable;
     def var_decl_statement(self, ty_string, var_name, statement):
         return ty_string + " " + var_name + " = " + statement
 
+    def for_n_in_range(self, n, minimum, maximum):
+        return "for (int " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
+    def for_n_in_arr(self, n, arr_name, arr_elem_ty):
+        return ("for (" + arr_elem_ty.java_hu_ty + " " + n + ": " + arr_name + ") { ", " }")
+
     def get_ptr(self, var):
         return var + ".ptr"
     def set_null_skip_free(self, var):
index 01096343e7d86d7d1acafcff0c0d0da9127170a8..d76318f3752feeac83a12fbb7d3dce564b40e230 100644 (file)
@@ -534,6 +534,11 @@ const decodeString = (stringPointer, free = true) => {
     def var_decl_statement(self, ty_string, var_name, statement):
         return "const " + var_name + ": " + ty_string + " = " + statement
 
+    def for_n_in_range(self, n, minimum, maximum):
+        return "for (var " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
+    def for_n_in_arr(self, n, arr_name, arr_elem_ty):
+        return (arr_name + ".forEach((" + n + ": " + arr_elem_ty.java_hu_ty + ") => { ", " })")
+
     def get_ptr(self, var):
         return "CommonBase.get_ptr_of(" + var + ")"
     def set_null_skip_free(self, var):