Update CI references to 0.0.122
[ldk-java] / csharp_strings.py
index c1873f3d8aefdd1a1f584db301d80d725875de72..a73ee2035d310be2568fa48f71b94ab9994ea3e4 100644 (file)
@@ -3,7 +3,9 @@ from enum import Enum
 import sys
 
 class Target(Enum):
-    CSHARP = 1,
+    WINDOWS = 1,
+    LINUX = 2,
+    PTHREAD = 3,
 
 def first_to_lower(string: str) -> str:
     first = string[0]
@@ -150,6 +152,28 @@ public class CommonBase {
        }
 }"""
 
+        self.witness_program_defn = """public class WitnessProgram : CommonBase {
+       /** The witness program bytes themselves */
+       public readonly byte[] program;
+       /** The witness version */
+       public readonly WitnessVersion version;
+
+       internal WitnessProgram(object _dummy, long ptr) : base(ptr) {
+               this.program = InternalUtils.decodeUint8Array(bindings.WitnessProgram_get_program(ptr));
+               this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
+       }
+       static private long check_args(byte[] program, WitnessVersion version) {
+               if (program.Length < 2 || program.Length > 40) throw new ArgumentException();
+               if (version.getVal() == 0 && program.Length != 20 && program.Length != 32) throw new ArgumentException();
+               return InternalUtils.encodeUint8Array(program);
+       }
+       public WitnessProgram(byte[] program, WitnessVersion version) :
+               this(null, bindings.WitnessProgram_new(version.getVal(), check_args(program, version))) {}
+
+       ~WitnessProgram() {
+               if (ptr != 0) { bindings.WitnessProgram_free(ptr); }
+       }
+}"""
 
         self.c_file_pfx = """
 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
@@ -166,7 +190,25 @@ public class CommonBase {
 
         self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
 
-        if not DEBUG or sys.platform == "darwin":
+        if self.target == Target.WINDOWS:
+            self.c_file_pfx = self.c_file_pfx + """#include <heapapi.h>
+static HANDLE process_heap = NULL;
+static inline void* init_heap() {
+       if (UNLIKELY(process_heap == NULL)) {
+               // Assume pointer writes wont tear, which is true where we need it.
+               process_heap = GetProcessHeap();
+       }
+}
+static inline void* MALLOC(size_t a, const char* _) {
+       init_heap();
+       return HeapAlloc(process_heap, HEAP_ZERO_MEMORY, a);
+}
+#define do_MALLOC(a, b, _c) MALLOC(a, b)
+#define FREE(p) if ((uint64_t)(p) > 4096) { init_heap(); HeapFree(process_heap, 0, p); }
+#define CHECK_ACCESS(p)
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
+"""
+        elif not DEBUG or self.target != Target.LINUX:
             self.c_file_pfx = self.c_file_pfx + """#define do_MALLOC(a, _b, _c) malloc(a)
 #define MALLOC(a, _) malloc(a)
 #define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
@@ -192,7 +234,7 @@ void __attribute__((constructor)) debug_log_version() {
 }
 """
 
-            if sys.platform != "darwin":
+            if self.target == Target.LINUX:
                 self.c_file_pfx += """
 // Running a leak check across all the allocations and frees of the JDK is a mess,
 // so instead we implement our own naive leak checker here, relying on the -wrap
@@ -534,12 +576,12 @@ int CS_LDK_register_{fn_suffix}_invoker(invoker_{fn_suffix} invoker) {{
         return "InternalUtils.getArrayLength(" + arr_name + ")"
 
     def get_java_arr_elem(self, elem_ty, arr_name, idx):
-        if elem_ty.c_ty == "int64_t" or elem_ty.c_ty == "uint64_t" or elem_ty.c_ty.endswith("Array") or elem_ty.c_ty == "uintptr_t":
+        if elem_ty.c_ty == "int64_t" or elem_ty.c_ty == "uint64_t":
+            return "InternalUtils.getU64ArrayElem(" + arr_name + ", " + idx + ")"
+        elif elem_ty.c_ty.endswith("Array") or elem_ty.c_ty == "uintptr_t" or elem_ty.rust_obj == "LDKStr":
             return "InternalUtils.getU64ArrayElem(" + arr_name + ", " + idx + ")"
         elif elem_ty.rust_obj == "LDKU5":
             return "InternalUtils.getU8ArrayElem(" + arr_name + ", " + idx + ")"
-        elif elem_ty.rust_obj == "LDKStr":
-            return "InternalUtils.getU32ArrayElem(" + arr_name + ", " + idx + ")"
         else:
             assert False