var js_objs: Array<WeakRef<object>> = [];
var js_invoke: Function;
+var getRandomValues: Function;
imports.wasi_snapshot_preview1 = {
"fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
},
"random_get": (buf_ptr: number, buf_len: number) => {
const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len);
- crypto.getRandomValues(buf);
+ getRandomValues(buf);
return 0;
},
"environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
var wasm: any = null;
let isWasmInitialized: boolean = false;
-"""
- if target == Target.NODEJS:
- self.bindings_header += """import * as fs from 'fs';
-import { webcrypto as crypto } from 'crypto';
-/* @internal */
-export async function initializeWasm(path: string) {
- const source = fs.readFileSync(path);
- imports.env["js_invoke_function"] = js_invoke;
- const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);"""
- else:
- self.bindings_header += """
-/* @internal */
-export async function initializeWasm(uri: string) {
- const stream = fetch(uri);
- imports.env["js_invoke_function"] = js_invoke;
- const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);"""
+async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
+ if (typeof crypto === "undefined") {
+ var crypto_import = (await import('crypto')).webcrypto;
+ getRandomValues = crypto_import.getRandomValues.bind(crypto_import);
+ } else {
+ getRandomValues = crypto.getRandomValues.bind(crypto);
+ }
- self.bindings_header += """
wasm = wasmInstance.exports;
if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) {
throw new Error(\"Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint");
console.log(\"Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version);
isWasmInitialized = true;
-};
+}
+
+/* @internal */
+export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
+ imports.env["js_invoke_function"] = js_invoke;
+ const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
+ await finishInitializeWasm(wasmInstance);
+}
+/* @internal */
+export async function initializeWasmFetch(uri: string) {
+ const stream = fetch(uri);
+ imports.env["js_invoke_function"] = js_invoke;
+ const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
+ await finishInitializeWasm(wasmInstance);
+}"""
+
+ self.bindings_header += """
// WASM CODEC
const nextMultipleOfFour = (value: number) => {
self.bindings_header += "/* @internal */ export function debugPrintRemainingAllocs() { }\n"
with open(outdir + "/index.mts", 'a') as index:
- index.write("""import { initializeWasm as bindingsInit } from './bindings.mjs';
-export function initializeWasm(path: string) {
- bindingsInit(path);
+ index.write("""import { initializeWasmFetch, initializeWasmFromUint8Array } from './bindings.mjs';
+/** Initializes the WASM backend by calling `fetch()` on the given URI - Browser only */
+export async function initializeWasmWebFetch(uri: string) {
+ await initializeWasmFetch(uri);
+}
+/** Initializes the WASM backend given a Uint8Array of the .wasm binary file - Browser or Node.JS */
+export async function initializeWasmFromBinary(bin: Uint8Array) {
+ await initializeWasmFromUint8Array(bin);
}
+
""")
self.bindings_version_file = """export function get_ldk_java_bindings_version(): String {
for fn_line in field_function_lines:
java_method_descriptor = ""
if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
+ out_java_interface += "\t/**" + fn_line.docs.replace("\n", "\n\t * ") + "\n\t */\n"
out_java_interface += "\t" + fn_line.fn_name + "("
out_interface_implementation_overrides += f"\t\t\t{fn_line.fn_name} ("
out_interface_implementation_overrides += "\t\t\t\treturn ret;\n"
out_interface_implementation_overrides += f"\t\t\t}},\n"
+ formatted_trait_docs = trait_doc_comment.replace("\n", "\n * ")
out_typescript_human = f"""
{self.hu_struct_file_prefix}
+/** An implementation of {struct_name.replace("LDK","")} */
export interface {struct_name.replace("LDK", "")}Interface {{
{out_java_interface}}}
held: {struct_name.replace("LDK", "")};
}}
+/**
+ * {formatted_trait_docs}
+ */
export class {struct_name.replace("LDK","")} extends CommonBase {{
/* @internal */
public bindings_instance?: bindings.{struct_name};
this.bindings_instance = null;
}}
- static new_impl(arg: {struct_name.replace("LDK", "")}Interface{impl_constructor_arguments}): {struct_name.replace("LDK", "")} {{
+ /** Creates a new instance of {struct_name.replace("LDK","")} from a given implementation */
+ public static new_impl(arg: {struct_name.replace("LDK", "")}Interface{impl_constructor_arguments}): {struct_name.replace("LDK", "")} {{
const impl_holder: {struct_name}Holder = new {struct_name}Holder();
let structImplementation = {{
{out_interface_implementation_overrides} }} as bindings.{struct_name};
impl_holder.held.bindings_instance = structImplementation;
{pointer_to_adder} return impl_holder.held;
}}
+
"""
self.obj_defined([struct_name.replace("LDK", ""), struct_name.replace("LDK", "") + "Interface"], "structs")
out_java_enum += (self.hu_struct_file_prefix)
- java_hu_class = ""
+ 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 += "\t/* @internal */\n"
out_java += "\tprotected constructor() {}\n"
var_idx = 0
for var in variant_list:
- java_hu_subclasses = java_hu_subclasses + "export class " + java_hu_type + "_" + var.var_name + " extends " + java_hu_type + " {\n"
+ java_hu_subclasses += "/** A " + java_hu_type + " of type " + var.var_name + " */\n"
+ java_hu_subclasses += "export class " + java_hu_type + "_" + var.var_name + " extends " + java_hu_type + " {\n"
java_hu_class += f"\t\t\tcase {var_idx}: "
java_hu_class += "return new " + java_hu_type + "_" + var.var_name + "(ptr);\n"
out_c += f"\t\tcase {struct_name}_{var.var_name}: return {var_idx};\n"
hu_conv_body = ""
for idx, (field_ty, field_docs) in enumerate(var.fields):
- java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
+ if field_docs is not None:
+ java_hu_subclasses += "\t/**\n\t * " + field_docs.replace("\n", "\n\t * ") + "\n\t */\n"
+ java_hu_subclasses += "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
if field_ty.to_hu_conv is not None:
hu_conv_body += f"\t\tconst {field_ty.arg_name}: {field_ty.java_ty} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
hu_conv_body += f"\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
out_opaque_struct_human = f"{self.hu_struct_file_prefix}"
if struct_name.startswith("LDKLocked"):
out_opaque_struct_human += "/** XXX: DO NOT USE THIS - it remains locked until the GC runs (if that ever happens */"
+ formatted_doc_comment = struct_doc_comment.replace("\n", "\n * ")
out_opaque_struct_human += f"""
+/**
+ * {formatted_doc_comment}
+ */
export class {hu_name} extends CommonBase {implementations}{{
/* @internal */
public constructor(_dummy: object, ptr: number) {{
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 doc_comment is not None:
+ out_java_struct = "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
+
if not args_known:
out_java_struct += ("\t// Skipped " + method_name + "\n")
else: