ty_info.var_name = "ret"
if ty_info.rust_obj in self.opaque_structs:
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = untag_ptr(" + ty_info.var_name + ");\n"
opaque_arg_conv += ty_info.var_name + "_conv.is_owned = ptr_is_owned(" + ty_info.var_name + ");\n"
if holds_ref:
opaque_arg_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
else:
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".clone_ptr()", "")
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
elif ty_info.passed_as_ptr:
opaque_arg_conv += "\n// WARNING: we need a move here but no clone is available for " + ty_info.rust_obj + "\n"
# TODO: Once we support features cloning (which just isn't in C yet), we can make this a compile error instead!
if holds_ref:
base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
else:
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".clone_ptr()", "")
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
else:
base_conv = base_conv + self.consts.trait_struct_inc_refcnt(ty_info)
else:
base_conv = base_conv + "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
if from_hu_conv is None:
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), "")
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), "")
from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name))
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,
if holds_ref:
base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + "));"
else:
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".clone_ptr()", "")
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
elif needs_full_clone:
base_conv = base_conv + "\n// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj
else:
ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")
if from_hu_conv is None:
- from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : 0", "")
+ from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
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,
ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, true)",
else:
to_hu_conv_sfx = ""
if from_hu_conv is None:
- from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : 0", "")
+ from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
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,
ret_conv = ret_conv, ret_conv_name = ret_conv_name,
ret_conv = ret_conv, ret_conv_name = "ref_" + ty_info.var_name,
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
to_hu_conv_name = ty_info.var_name + "_hu_conv",
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
elif ty_info.rust_obj in self.trait_structs:
if ty_info.nonnull_ptr:
arg_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
ret_conv_name = "tag_ptr(" + ty_info.var_name + "_clone, true)",
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, "ret_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";\n" + self.consts.add_ref("ret_hu_conv", "this") + ";",
to_hu_conv_name = "ret_hu_conv",
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), ""))
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), ""))
else:
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
ret_conv_name = "ret_" + ty_info.var_name,
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, "ret_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";\n" + self.consts.add_ref("ret_hu_conv", "this") + ";",
to_hu_conv_name = "ret_hu_conv",
- from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name)))
+ from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name)))
ret_conv = (self.consts.ptr_c_ty + " ret_" + ty_info.var_name + " = tag_ptr(", ", true);")
if holds_ref:
ret_conv = (ret_conv[0], ", false);")
arr_ty = "uint64_t"
fn_arg = fn_arg[8:].strip()
else:
- java_ty = consts.ptr_native_ty
- c_ty = consts.ptr_c_ty
+ java_ty = consts.usize_native_ty
+ c_ty = consts.usize_c_ty
arr_ty = "uintptr_t"
rust_obj = "uintptr_t"
fn_arg = fn_arg[9:].strip()
write_c("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struct LDKThirtyTwoBytes *orig) { struct LDKThirtyTwoBytes ret; memcpy(ret.data, orig->data, 32); return ret; }\n\n")
-write_c("static inline void* untag_ptr(uintptr_t ptr) {\n")
+write_c("static inline void* untag_ptr(uint64_t ptr) {\n")
write_c("\tif (ptr < 4096) return (void*)ptr;\n")
write_c("\tif (sizeof(void*) == 4) {\n")
-write_c("\t\treturn (void*)(ptr & ~1);\n")
+write_c("\t\t// For 32-bit systems, store pointers as 64-bit ints and use the 31st bit\n")
+write_c("\t\treturn (void*)(uintptr_t)ptr;\n")
write_c("\t} else {\n")
write_c("\t\t// For 64-bit systems, assume the top byte is used for tagging, then\n")
write_c("\t\t// use bit 9 ^ bit 10.\n")
write_c("\t}\n")
write_c("}\n")
-write_c("static inline bool ptr_is_owned(uintptr_t ptr) {\n")
+write_c("static inline bool ptr_is_owned(uint64_t ptr) {\n")
write_c("\tif(ptr < 4096) return true;\n")
write_c("\tif (sizeof(void*) == 4) {\n")
-write_c("\t\treturn (ptr & 1) ? true : false;\n")
+write_c("\t\treturn ptr & (1ULL << 32);\n")
write_c("\t} else {\n")
write_c("\t\tuintptr_t ninth_bit = (((uintptr_t)ptr) & (1ULL << 55)) >> 55;\n")
write_c("\t\tuintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n")
write_c("\t}\n")
write_c("}\n")
-write_c("static inline uintptr_t tag_ptr(const void* ptr, bool is_owned) {\n")
+write_c("static inline uint64_t tag_ptr(const void* ptr, bool is_owned) {\n")
write_c("\tif ((uintptr_t)ptr < 4096) return (uint64_t)ptr;\n")
write_c("\tif (sizeof(void*) == 4) {\n")
-write_c("\t\treturn ((uintptr_t)ptr) | (is_owned ? 1 : 0);\n")
+write_c("\t\treturn (((uint64_t)ptr) | ((is_owned ? 1ULL : 0) << 32));\n")
write_c("\t} else {\n")
write_c("\t\tCHECK(sizeof(uintptr_t) == 8);\n")
write_c("\t\tuintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n")
return_type_info = type_mapping_generator.map_type(method_return_type.strip() + " ret", True, ret_arr_len, False, force_holds_ref)
if method_name.endswith("_clone") and expected_struct not in unitary_enums:
- meth_line = "uintptr_t " + expected_struct.replace("LDK", "") + "_clone_ptr(" + expected_struct + " *NONNULL_PTR arg)"
+ meth_line = "uint64_t " + expected_struct.replace("LDK", "") + "_clone_ptr(" + expected_struct + " *NONNULL_PTR arg)"
write_c("static inline " + meth_line + " {\n")
write_c("\t" + return_type_info.ret_conv[0].replace("\n", "\n\t"))
write_c(method_name + "(arg)")
write_c(return_type_info.ret_conv[1].replace("\n", "\n\t"))
write_c("\n\treturn " + return_type_info.ret_conv_name + ";\n}\n")
- map_fn(meth_line + ";\n", re.compile("(uintptr_t) ([A-Za-z_0-9]*)\((.*)\)").match(meth_line), None, None, None)
+ map_fn(meth_line + ";\n", re.compile("(uint64_t) ([A-Za-z_0-9]*)\((.*)\)").match(meth_line), None, None, None)
argument_types = []
default_constructor_args = {}
self.file_ext = ".java"
self.ptr_c_ty = "int64_t"
self.ptr_native_ty = "long"
+ self.usize_c_ty = "int64_t"
+ self.usize_native_ty = "long"
+ self.native_zero_ptr = "0"
self.result_c_ty = "jclass"
self.ptr_arr = "jobjectArray"
self.is_arr_some_check = ("", " != NULL")
DECL_IMPORT(u, u, u, u, u, u)
DECL_IMPORT(b, u, u, u, u, u)
+DECL_IMPORT(b, b, u, u, u, u)
+DECL_IMPORT(b, b, b, u, u, u)
+DECL_IMPORT(b, b, b, b, u, u)
DECL_IMPORT(b, b, b, b, b, b)
+DECL_IMPORT(u, b, u, u, b, u)
DECL_IMPORT(u, b, u, u, u, u)
+DECL_IMPORT(u, b, b, u, u, u)
DECL_IMPORT(u, u, b, u, u, u)
+DECL_IMPORT(u, u, b, u, b, u)
#endif
isWasmInitialized = true;
}
-const fn_list = ["uuuuuu", "buuuuu", "bbbbbb", "ubuuuu", "uubuuu"];
+const fn_list = ["uuuuuu", "buuuuu", "bbuuuu", "bbbuuu", "bbbbuu",
+ "bbbbbb", "ubuubu", "ubuuuu", "ubbuuu", "uubuuu", "uububu"];
/* @internal */
export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
+/* @internal */
+export function getU64ArrayElem(arrayPointer: number, idx: number): bigint {
+ const actualArrayViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
+ return actualArrayViewer[idx];
+}
+
/* @internal */
export function getU32ArrayElem(arrayPointer: number, idx: number): number {
const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
self.common_base = """
function freer(f: () => void) { f() }
const finalizer = new FinalizationRegistry(freer);
-function get_freeer(ptr: number, free_fn: (ptr: number) => void) {
+function get_freeer(ptr: bigint, free_fn: (ptr: bigint) => void) {
return () => {
free_fn(ptr);
}
}
export class CommonBase {
- protected ptr: number;
+ protected ptr: bigint;
protected ptrs_to: object[] = [];
- protected constructor(ptr: number, free_fn: (ptr: number) => void) {
+ protected constructor(ptr: bigint, free_fn: (ptr: bigint) => void) {
this.ptr = ptr;
- if (Number.isFinite(ptr) && ptr != 0){
+ if (ptr != 0n){
finalizer.register(this, get_freeer(ptr, free_fn), this);
}
}
return o.ptr;
}
protected static set_null_skip_free(o: CommonBase) {
- o.ptr = 0;
+ o.ptr = 0n;
// @ts-ignore TypeScript is wrong about the returnvalue of unregister here!
const did_unregister: boolean = finalizer.unregister(o);
if (!did_unregister)
public value: bigint;
/* @internal */
- public constructor(_dummy: object, ptr: number) {
+ public constructor(_dummy: object, ptr: bigint) {
super(ptr, bindings.TxOut_free);
this.script_pubkey = bindings.decodeUint8Array(bindings.TxOut_get_script_pubkey(ptr));
this.value = bindings.TxOut_get_value(ptr);
}
DECL_ARR_TYPE(int64_t, int64_t);
+DECL_ARR_TYPE(uint64_t, uint64_t);
DECL_ARR_TYPE(int8_t, int8_t);
DECL_ARR_TYPE(uint32_t, uint32_t);
DECL_ARR_TYPE(void*, ptr);
self.util_fn_sfx = "}"
self.c_fn_ty_pfx = ""
self.file_ext = ".mts"
- self.ptr_c_ty = "uint32_t"
- self.ptr_native_ty = "number"
+ self.ptr_c_ty = "uint64_t"
+ self.ptr_native_ty = "bigint"
+ self.usize_c_ty = "uint32_t"
+ self.usize_native_ty = "number"
+ self.native_zero_ptr = "0n"
self.result_c_ty = "uint32_t"
self.ptr_arr = "ptrArray"
self.is_arr_some_check = ("", " != 0")
def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
if elem_ty.rust_obj == "LDKu5":
return arr_name + " != null ? bindings.uint5ArrToBytes(" + arr_name + ") : null"
- assert elem_ty.c_ty == "uint32_t" or elem_ty.c_ty.endswith("Array")
+ assert elem_ty.c_ty == "uint64_t" or elem_ty.c_ty.endswith("Array")
return arr_name + " != null ? " + arr_name + ".map(" + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null"
def str_ref_to_native_call(self, var_name, str_len):
def get_java_arr_len(self, arr_name):
return "bindings.getArrayLength(" + arr_name + ")"
def get_java_arr_elem(self, elem_ty, arr_name, idx):
- if elem_ty.c_ty == "uint32_t" or elem_ty.c_ty == "uintptr_t" or elem_ty.c_ty.endswith("Array"):
+ if elem_ty.c_ty.endswith("Array") or elem_ty.c_ty == "uintptr_t":
return "bindings.getU32ArrayElem(" + arr_name + ", " + idx + ")"
+ elif elem_ty.c_ty == "uint64_t":
+ return "bindings.getU64ArrayElem(" + arr_name + ", " + idx + ")"
elif elem_ty.rust_obj == "LDKu5":
return "bindings.getU8ArrayElem(" + arr_name + ", " + idx + ")"
else:
return ("bindings.encodeUint8Array(" + inner + ")", "")
elif mapped_ty.c_ty == "uint32_t":
return ("bindings.encodeUint32Array(" + inner + ")", "")
- elif mapped_ty.c_ty == "int64_t":
+ elif mapped_ty.c_ty == "int64_t" or mapped_ty.c_ty == "uint64_t":
return ("bindings.encodeUint64Array(" + inner + ")", "")
else:
print(mapped_ty.c_ty)
public bindings_instance?: bindings.{struct_name};
/* @internal */
- constructor(_dummy: object, ptr: number) {{
+ constructor(_dummy: object, ptr: bigint) {{
super(ptr, bindings.{struct_name.replace("LDK","")}_free);
this.bindings_instance = null;
}}
const impl_holder: {struct_name}Holder = new {struct_name}Holder();
let structImplementation = {{
{out_interface_implementation_overrides} }} as bindings.{struct_name};
-{super_constructor_statements} const ptr: number = bindings.{struct_name}_new(structImplementation{bindings_instantiator});
+{super_constructor_statements} const ptr: bigint = bindings.{struct_name}_new(structImplementation{bindings_instantiator});
impl_holder.held = new {struct_name.replace("LDK", "")}(null, ptr);
impl_holder.held.bindings_instance = structImplementation;
else:
out_typescript_bindings += f", {var[1]}: {var[0]}"
- out_typescript_bindings += f"""): number {{
+ out_typescript_bindings += f"""): bigint {{
if(!isWasmInitialized) {{
throw new Error("initializeWasm() must be awaited first!");
}}
for idx, arg_info in enumerate(fn_line.args_ty):
if arg_info.ret_conv is not None:
- out_c += ", (uint32_t)" + arg_info.ret_conv_name
+ if arg_info.c_ty.endswith("Array"):
+ out_c += ", (uint32_t)" + arg_info.ret_conv_name
+ else:
+ out_c += ", " + arg_info.ret_conv_name
else:
+ assert False # TODO: Would we need some conversion here?
out_c += ", (uint32_t)" + arg_info.arg_name
for i in range(0, 6 - len(fn_line.args_ty)):
out_c += ", 0"
out_c = out_c + "\treturn ret;\n"
out_c = out_c + "}\n"
- out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "JSValue o"
+ out_c = out_c + self.c_fn_ty_pfx + "uint64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "JSValue o"
for var in flattened_field_var_conversions:
if isinstance(var, ConvInfo):
out_c = out_c + ", " + var.c_ty + " " + var.arg_name
java_hu_class = "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
java_hu_class += "export class " + java_hu_type + " extends CommonBase {\n"
- java_hu_class += "\tprotected constructor(_dummy: object, ptr: number) { super(ptr, bindings." + bindings_type + "_free); }\n"
+ java_hu_class += "\tprotected constructor(_dummy: object, ptr: bigint) { super(ptr, bindings." + bindings_type + "_free); }\n"
java_hu_class += "\t/* @internal */\n"
- java_hu_class += f"\tpublic static constr_from_ptr(ptr: number): {java_hu_type} {{\n"
+ java_hu_class += f"\tpublic static constr_from_ptr(ptr: bigint): {java_hu_type} {{\n"
java_hu_class += f"\t\tconst raw_ty: number = bindings." + struct_name + "_ty_from_ptr(ptr);\n"
out_c += self.c_fn_ty_pfx + "uint32_t" + self.c_fn_name_define_pfx(struct_name + "_ty_from_ptr", True) + self.ptr_c_ty + " ptr) {\n"
out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
else:
hu_conv_body += f"\t\tthis.{field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
java_hu_subclasses += "\t/* @internal */\n"
- java_hu_subclasses += "\tpublic constructor(ptr: number) {\n\t\tsuper(null, ptr);\n"
+ java_hu_subclasses += "\tpublic constructor(ptr: bigint) {\n\t\tsuper(null, ptr);\n"
java_hu_subclasses = java_hu_subclasses + hu_conv_body
java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
var_idx += 1
out_java += "}\n"
java_hu_class += "\t\t\tdefault:\n\t\t\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t\t}\n\t}\n\n"
- out_java += self.fn_call_body(struct_name + "_ty_from_ptr", "uint32_t", "number", "ptr: number", "ptr")
+ out_java += self.fn_call_body(struct_name + "_ty_from_ptr", "uint32_t", "number", "ptr: bigint", "ptr")
out_c += ("\t\tdefault: abort();\n")
out_c += ("\t}\n}\n")
else:
out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name + ";\n"
out_c += "}\n"
- out_java += self.fn_call_body(fn_name, field_map.c_ty, field_map.java_ty, "ptr: number", "ptr")
+ out_java += self.fn_call_body(fn_name, field_map.c_ty, field_map.java_ty, "ptr: bigint", "ptr")
out_java_enum += java_hu_class
self.struct_file_suffixes[java_hu_type] = java_hu_subclasses
self.obj_defined([java_hu_type], "structs")
*/
export class {hu_name} extends CommonBase {implementations}{{
/* @internal */
- public constructor(_dummy: object, ptr: number) {{
+ public constructor(_dummy: object, ptr: bigint) {{
{constructor_body}
}}{extra_body}
suffixes += "\tpublic res: " + res_map.java_hu_ty + ";\n"
suffixes += f"""
/* @internal */
- public constructor(_dummy: object, ptr: number) {{
+ public constructor(_dummy: object, ptr: bigint) {{
super(_dummy, ptr);
"""
if res_map.java_hu_ty == "void":
suffixes += "\tpublic err: " + err_map.java_hu_ty + ";\n"
suffixes += f"""
/* @internal */
- public constructor(_dummy: object, ptr: number) {{
+ public constructor(_dummy: object, ptr: bigint) {{
super(_dummy, ptr);
"""
if err_map.java_hu_ty == "void":
return f"""{self.hu_struct_file_prefix}
export class {human_ty} extends CommonBase {{
- protected constructor(_dummy: object, ptr: number) {{
+ protected constructor(_dummy: object, ptr: bigint) {{
super(ptr, bindings.{struct_name.replace("LDK","")}_free);
}}
/* @internal */
- public static constr_from_ptr(ptr: number): {human_ty} {{
+ public static constr_from_ptr(ptr: bigint): {human_ty} {{
if (bindings.{struct_name.replace("LDK", "")}_is_ok(ptr)) {{
return new {human_ty}_OK(null, ptr);
}} else {{