X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=2b0ab1c972b2dff3e3a7944e658bb1890a5a1508;hb=6700d1496b231c4b27d91a66be0d5efdb741662c;hp=d669821301b53fdde1bff004c51e86f8f56ab9dd;hpb=cf5dbd06f37b9e98d391a93e9b9299aed0c95995;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index d6698213..2b0ab1c9 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -42,6 +42,7 @@ imports.env = {}; var js_objs: Array> = []; var js_invoke: Function; +var getRandomValues: Function; imports.wasi_snapshot_preview1 = { "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => { @@ -66,7 +67,7 @@ imports.wasi_snapshot_preview1 = { }, "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) => { @@ -90,25 +91,15 @@ imports.wasi_snapshot_preview1 = { 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"); @@ -128,8 +119,24 @@ export async function initializeWasm(uri: string) { 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) => { @@ -247,10 +254,16 @@ export function debugPrintRemainingAllocs() { 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 { @@ -769,6 +782,7 @@ export enum {struct_name} {{ 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} (" @@ -817,9 +831,11 @@ export enum {struct_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}}} @@ -827,6 +843,9 @@ class {struct_name}Holder {{ held: {struct_name.replace("LDK", "")}; }} +/** + * {formatted_trait_docs} + */ export class {struct_name.replace("LDK","")} extends CommonBase {{ /* @internal */ public bindings_instance?: bindings.{struct_name}; @@ -837,7 +856,8 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ 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}; @@ -847,6 +867,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ 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") @@ -1047,7 +1068,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ 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" @@ -1063,13 +1084,16 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ 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" @@ -1121,7 +1145,11 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ 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) {{ @@ -1236,6 +1264,9 @@ export function {method_name}({method_argument_string}): {return_java_ty} {{ 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: