+
+ def map_tuple(self, struct_name):
+ return self.map_opaque_struct(struct_name, "A Tuple")
+
+ def map_result(self, struct_name, res_map, err_map):
+ human_ty = struct_name.replace("LDKCResult", "Result")
+
+ suffixes = f"export class {human_ty}_OK extends {human_ty} {{\n"
+ if res_map.java_hu_ty != "void":
+ suffixes += "\tpublic res: " + res_map.java_hu_ty + ";\n"
+ suffixes += f"""
+ /* @internal */
+ public constructor(_dummy: object, ptr: number) {{
+ super(_dummy, ptr);
+"""
+ if res_map.java_hu_ty == "void":
+ pass
+ elif res_map.to_hu_conv is not None:
+ suffixes += "\t\tconst res: " + res_map.java_ty + " = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
+ suffixes += "\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t")
+ suffixes += "\n\t\tthis.res = " + res_map.to_hu_conv_name + ";\n"
+ else:
+ suffixes += "\t\tthis.res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
+ suffixes += "\t}\n}\n"
+
+ suffixes += f"export class {human_ty}_Err extends {human_ty} {{\n"
+ if err_map.java_hu_ty != "void":
+ suffixes += "\tpublic err: " + err_map.java_hu_ty + ";\n"
+ suffixes += f"""
+ /* @internal */
+ public constructor(_dummy: object, ptr: number) {{
+ super(_dummy, ptr);
+"""
+ if err_map.java_hu_ty == "void":
+ pass
+ elif err_map.to_hu_conv is not None:
+ suffixes += "\t\tconst err: " + err_map.java_ty + " = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
+ suffixes += "\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t")
+ suffixes += "\n\t\tthis.err = " + err_map.to_hu_conv_name + ";\n"
+ else:
+ suffixes += "\t\tthis.err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
+ suffixes += "\t}\n}"
+
+ self.struct_file_suffixes[human_ty] = suffixes
+ self.obj_defined([human_ty], "structs")
+
+ return f"""{self.hu_struct_file_prefix}
+
+export class {human_ty} extends CommonBase {{
+ protected constructor(_dummy: object, ptr: number) {{
+ super(ptr, bindings.{struct_name.replace("LDK","")}_free);
+ }}
+ /* @internal */
+ public static constr_from_ptr(ptr: number): {human_ty} {{
+ if (bindings.{struct_name.replace("LDK", "")}_is_ok(ptr)) {{
+ return new {human_ty}_OK(null, ptr);
+ }} else {{
+ return new {human_ty}_Err(null, ptr);
+ }}
+ }}
+"""
+
+ def fn_call_body(self, method_name, return_c_ty, return_java_ty, method_argument_string, native_call_argument_string):
+ has_return_value = return_c_ty != 'void'
+ return_statement = 'return nativeResponseValue;'
+ if not has_return_value:
+ return_statement = '// debug statements here'
+
+ return f"""export function {method_name}({method_argument_string}): {return_java_ty} {{
+ if(!isWasmInitialized) {{
+ throw new Error("initializeWasm() must be awaited first!");
+ }}
+ const nativeResponseValue = wasm.TS_{method_name}({native_call_argument_string});
+ {return_statement}
+}}
+"""
+ def map_function(self, argument_types, c_call_string, method_name, meth_n, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_as_ref, args_known, type_mapping_generator, doc_comment):
+ out_java = ""
+ out_c = ""
+ out_java_struct = None
+
+ out_java += ("\t")
+ out_c += (self.c_fn_ty_pfx)
+ out_c += (return_type_info.c_ty)
+ out_java += (return_type_info.java_ty)
+ if return_type_info.ret_conv is not None:
+ ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
+ out_java += (" " + method_name + "(")
+ out_c += (" " + self.c_fn_name_define_pfx(method_name, True))
+
+ method_argument_string = ""
+ native_call_argument_string = ""
+ for idx, arg_conv_info in enumerate(argument_types):
+ if idx != 0:
+ method_argument_string += (", ")
+ native_call_argument_string += ', '
+ out_c += (", ")
+ if arg_conv_info.c_ty != "void":
+ out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
+ method_argument_string += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
+ native_call_argument_string += arg_conv_info.arg_name
+ out_java = self.fn_call_body(method_name, return_type_info.c_ty, return_type_info.java_ty, method_argument_string, native_call_argument_string)
+
+ out_java_struct = ""
+ if not args_known:
+ out_java_struct += ("\t// Skipped " + method_name + "\n")
+ else:
+ if not takes_self:
+ out_java_struct += (
+ "\tpublic static constructor_" + meth_n + "(")
+ else:
+ out_java_struct += ("\tpublic " + meth_n + "(")
+ for idx, arg in enumerate(argument_types):
+ if idx != 0:
+ if not takes_self or idx > 1:
+ out_java_struct += (", ")
+ elif takes_self:
+ continue
+ if arg.java_ty != "void":
+ if arg.arg_name in default_constructor_args:
+ for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
+ if explode_idx != 0:
+ out_java_struct += (", ")
+ out_java_struct += arg.arg_name + "_" + explode_arg.arg_name + ": " + explode_arg.java_hu_ty
+ else:
+ out_java_struct += arg.arg_name + ": " + arg.java_hu_ty
+
+ out_c += (") {\n")
+ if out_java_struct is not None:
+ out_java_struct += "): " + return_type_info.java_hu_ty + " {\n"
+ for info in argument_types:
+ if info.arg_conv is not None:
+ out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
+ if return_type_info.ret_conv is not None:
+ out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
+ elif return_type_info.c_ty != "void":
+ out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
+ else:
+ out_c += ("\t")
+ if c_call_string is None:
+ out_c += (method_name + "(")
+ else:
+ out_c += (c_call_string)
+ for idx, info in enumerate(argument_types):
+ if info.arg_conv_name is not None:
+ if idx != 0:
+ out_c += (", ")
+ elif c_call_string is not None:
+ continue
+ out_c += (info.arg_conv_name)
+ out_c += (")")
+ if return_type_info.ret_conv is not None:
+ out_c += (ret_conv_sfx.replace('\n', '\n\t'))
+ else:
+ out_c += (";")
+ for info in argument_types:
+ if info.arg_conv_cleanup is not None:
+ out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
+ if return_type_info.ret_conv is not None:
+ out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
+ elif return_type_info.c_ty != "void":
+ out_c += ("\n\treturn ret_val;")
+ out_c += ("\n}\n\n")
+
+ if args_known:
+ out_java_struct += ("\t\t")
+ if return_type_info.java_ty != "void":
+ out_java_struct += "const ret: " + return_type_info.java_ty + " = "
+ out_java_struct += ("bindings." + method_name + "(")
+ for idx, info in enumerate(argument_types):
+ if idx != 0:
+ out_java_struct += (", ")
+ if idx == 0 and takes_self:
+ out_java_struct += ("this.ptr")
+ elif info.arg_name in default_constructor_args:
+ out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
+ for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
+ if explode_idx != 0:
+ out_java_struct += (", ")
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ if explode_arg.from_hu_conv is not None:
+ out_java_struct += (
+ explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
+ else:
+ out_java_struct += (expl_arg_name)
+ out_java_struct += (")")
+ elif info.from_hu_conv is not None:
+ out_java_struct += (info.from_hu_conv[0])
+ else:
+ out_java_struct += (info.arg_name)
+ out_java_struct += (");\n")
+ if return_type_info.to_hu_conv is not None:
+ if not takes_self:
+ out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t").replace("this",
+ return_type_info.to_hu_conv_name) + "\n")
+ else:
+ out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
+
+ for idx, info in enumerate(argument_types):
+ if idx == 0 and takes_self:
+ pass
+ elif info.arg_name in default_constructor_args:
+ for explode_arg in default_constructor_args[info.arg_name]:
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
+ out_java_struct += ("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name,
+ expl_arg_name).replace(
+ "this", return_type_info.to_hu_conv_name) + ";\n")
+ elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
+ if not takes_self and return_type_info.to_hu_conv_name is not None:
+ out_java_struct += (
+ "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n")
+ else:
+ out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n")
+
+ if return_type_info.to_hu_conv_name is not None:
+ out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
+ elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
+ out_java_struct += ("\t\treturn ret;\n")
+ out_java_struct += ("\t}\n\n")
+
+ return (out_java, out_c, out_java_struct)
+
+ def cleanup(self):
+ for struct in self.struct_file_suffixes:
+ with open(self.outdir + "/structs/" + struct + self.file_ext, "a") as src:
+ src.write(self.struct_file_suffixes[struct])
+
+ with open(self.outdir + "/bindings.mts", "a") as bindings:
+ bindings.write("""
+
+js_invoke = function(obj_ptr: number, fn_id: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
+ const weak: WeakRef<object> = js_objs[obj_ptr];
+ if (weak == null || weak == undefined) {
+ console.error("Got function call on unknown/free'd JS object!");
+ throw new Error("Got function call on unknown/free'd JS object!");
+ }
+ const obj: object = weak.deref();
+ if (obj == null || obj == undefined) {
+ console.error("Got function call on GC'd JS object!");
+ throw new Error("Got function call on GC'd JS object!");
+ }
+ var fn;
+""")
+ bindings.write("\tswitch (fn_id) {\n")
+ for f in self.function_ptrs:
+ bindings.write(f"\t\tcase {str(f)}: fn = Object.getOwnPropertyDescriptor(obj, \"{self.function_ptrs[f][1]}\"); break;\n")
+
+ bindings.write("""\t\tdefault:
+ console.error("Got unknown function call from C!");
+ throw new Error("Got unknown function call from C!");
+ }
+ if (fn == null || fn == undefined) {
+ console.error("Got function call on incorrect JS object!");
+ throw new Error("Got function call on incorrect JS object!");
+ }
+ return fn.value.bind(obj)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
+}""")