X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=fb3cd99ef5fe4a5644aeda26d90938a6ca050fdb;hb=133666d2e10cde33de2e8b5a58fe443d1ed1f7f9;hp=e3b980830f6d0bce2172be184ef7d248d1abd351;hpb=dbce8d1aa627e4a632610895c5ea1fddfd9a24f1;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index e3b98083..fb3cd99e 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -20,7 +20,7 @@ class Consts: uint8_t = ['number', 'number', 'Uint8Array'], uint16_t = ['number', 'number', 'Uint16Array'], uint32_t = ['number', 'number', 'Uint32Array'], - uint64_t = ['BigInt', 'BigInt', 'BigUint64Array'], + uint64_t = ['bigint', 'bigint', 'BigUint64Array'], ) self.java_type_map = dict( String = "number" @@ -36,7 +36,16 @@ class Consts: self.bindings_header = self.wasm_import_header(target) - self.bindings_version_file = "" + with open(outdir + "/index.mts", 'a') as index: + index.write("""import { initializeWasm as bindingsInit } from './bindings.mjs'; +export function initializeWasm(path: string) { + bindingsInit(path); +} +""") + + self.bindings_version_file = """export function get_ldk_java_bindings_version(): String { + return ""; +}""" self.bindings_footer = "" @@ -78,7 +87,7 @@ export default class CommonBase { /** The script_pubkey in this output */ public script_pubkey: Uint8Array; /** The value, in satoshis, of this output */ - public value: BigInt; + public value: bigint; /* @internal */ public constructor(_dummy: object, ptr: number) { @@ -86,7 +95,7 @@ export default class CommonBase { this.script_pubkey = bindings.decodeUint8Array(bindings.TxOut_get_script_pubkey(ptr)); this.value = bindings.TxOut_get_value(ptr); } - public constructor_new(value: BigInt, script_pubkey: Uint8Array): TxOut { + public constructor_new(value: bigint, script_pubkey: Uint8Array): TxOut { return new TxOut(null, bindings.TxOut_new(bindings.encodeUint8Array(script_pubkey), value)); } }""" @@ -283,14 +292,27 @@ uint32_t __attribute__((export_name("TS_malloc"))) TS_malloc(uint32_t size) { void __attribute__((export_name("TS_free"))) TS_free(uint32_t ptr) { FREE((void*)ptr); } + +jstring __attribute__((export_name("TS_get_ldk_c_bindings_version"))) TS_get_ldk_c_bindings_version() { + const char *res = check_get_ldk_bindings_version(); + if (res == NULL) return NULL; + return str_ref_to_ts(res, strlen(res)); +} +jstring __attribute__((export_name("TS_get_ldk_version"))) get_ldk_version() { + const char *res = check_get_ldk_version(); + if (res == NULL) return NULL; + return str_ref_to_ts(res, strlen(res)); +} +#include "version.c" """ - self.c_version_file = "" + self.c_version_file = """jstring __attribute__((export_name("TS_get_lib_version_string"))) TS_get_lib_version_string() { + return str_ref_to_ts("", strlen("")); +}""" self.hu_struct_file_prefix = """ import CommonBase from './CommonBase.mjs'; import * as bindings from '../bindings.mjs' -import * as InternalUtils from '../InternalUtils.mjs' """ self.util_fn_pfx = self.hu_struct_file_prefix + "\nexport class UtilMethods extends CommonBase {\n" @@ -304,12 +326,6 @@ import * as InternalUtils from '../InternalUtils.mjs' self.is_arr_some_check = ("", " != 0") self.get_native_arr_len_call = ("", "->arr_len") - with open(outdir + "/InternalUtils.mts", "w") as f: - f.write("export function check_arr_len(arr: Uint8Array, len: number): Uint8Array {\n") - f.write("\tif (arr.length != len) { throw new Error(\"Expected array of length \" + len + \"got \" + arr.length); }\n") - f.write("\treturn arr;\n") - f.write("}") - def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var): return None def create_native_arr_call(self, arr_len, ty_info): @@ -362,6 +378,8 @@ import * as InternalUtils from '../InternalUtils.mjs' def wasm_import_header(self, target): res = """ +import * as version from './version.mjs'; + const imports: any = {}; imports.env = {}; @@ -416,34 +434,38 @@ let isWasmInitialized: boolean = false; if target == Target.NODEJS: res += """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); - wasm = wasmInstance.exports; - if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) { - throw new Error(\"Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint"); - } - isWasmInitialized = true; -}; -""" + const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);""" else: res += """ +/* @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); + const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);""" + + return res + """ wasm = wasmInstance.exports; if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) { throw new Error(\"Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint"); } - isWasmInitialized = true; -}; - -""" - return res + """ + if (decodeString(wasm.TS_get_lib_version_string()) !== version.get_ldk_java_bindings_version()) + throw new Error(\"Compiled LDK library and LDK class failes do not match\"); + // Fetching the LDK versions from C also checks that the header and binaries match + if (wasm.TS_get_ldk_c_bindings_version() == 0) + throw new Error(\"LDK version did not match the header we built against\"); + if (wasm.TS_get_ldk_version() == 0) + throw new Error(\"LDK C bindings version did not match the header we built against\"); + const c_bindings_version: string = decodeString(wasm.TS_get_ldk_c_bindings_version()); + const ldk_version: string = decodeString(wasm.TS_get_ldk_version()); + console.log(\"Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version); + isWasmInitialized = true; +}; // WASM CODEC @@ -451,6 +473,7 @@ const nextMultipleOfFour = (value: number) => { return Math.ceil(value / 4) * 4; } +/* @internal */ export function encodeUint8Array (inputArray: Uint8Array): number { const cArrayPointer = wasm.TS_malloc(inputArray.length + 4); const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1); @@ -459,6 +482,7 @@ export function encodeUint8Array (inputArray: Uint8Array): number { arrayMemoryView.set(inputArray); return cArrayPointer; } +/* @internal */ export function encodeUint32Array (inputArray: Uint32Array|Array): number { const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4); const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length); @@ -466,7 +490,8 @@ export function encodeUint32Array (inputArray: Uint32Array|Array): numbe arrayMemoryView[0] = inputArray.length; return cArrayPointer; } -export function encodeUint64Array (inputArray: BigUint64Array|Array): number { +/* @internal */ +export function encodeUint64Array (inputArray: BigUint64Array|Array): number { const cArrayPointer = wasm.TS_malloc(inputArray.length * 8 + 1); const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1); arrayLengthView[0] = inputArray.length; @@ -475,15 +500,18 @@ export function encodeUint64Array (inputArray: BigUint64Array|Array): nu return cArrayPointer; } +/* @internal */ export function check_arr_len(arr: Uint8Array, len: number): Uint8Array { if (arr.length != len) { throw new Error("Expected array of length " + len + "got " + arr.length); } return arr; } +/* @internal */ export function getArrayLength(arrayPointer: number): number { const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1); return arraySizeViewer[0]; } +/* @internal */ export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array { const arraySize = getArrayLength(arrayPointer); const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, arraySize); @@ -512,16 +540,19 @@ const decodeUint32Array = (arrayPointer: number, free = true) => { return actualArray; } +/* @internal */ export function getU32ArrayElem(arrayPointer: number, idx: number): number { const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1); return actualArrayViewer[idx]; } +/* @internal */ export function encodeString(str: string): number { const charArray = new TextEncoder().encode(str); return encodeUint8Array(charArray); } +/* @internal */ export function decodeString(stringPointer: number, free = true): string { const arraySize = getArrayLength(stringPointer); const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize); @@ -605,8 +636,9 @@ export function decodeString(stringPointer: number, free = true): string { out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var) ord_v = ord_v + 1 if var_docs is not None: - out_typescript_enum_fields += f"/**\n * {var_docs}\n */\n" - out_typescript_enum_fields += f"{var},\n\t\t\t\t" + var_docs_repld = var_docs.replace("\n", "\n\t") + out_typescript_enum_fields += f"/**\n\t * {var_docs_repld}\n\t */\n" + out_typescript_enum_fields += f"\t{var},\n\t" out_c = out_c + "\t}\n" out_c = out_c + "\tabort();\n" out_c = out_c + "}\n" @@ -622,9 +654,10 @@ export function decodeString(stringPointer: number, free = true): string { out_c = out_c + "}\n" out_typescript = f""" - export enum {struct_name} {{ - {out_typescript_enum_fields} - }} +/* @internal */ +export enum {struct_name} {{ + {out_typescript_enum_fields} +}} """ out_typescript_enum = f"export {{ {struct_name} }} from \"../bindings.mjs\";" self.obj_defined([struct_name], "enums") @@ -636,8 +669,7 @@ export function decodeString(stringPointer: number, free = true): string { return (ty_info.rust_obj + "_from_js(", ")") def native_c_map_trait(self, struct_name, field_var_conversions, flattened_field_var_conversions, field_function_lines, trait_doc_comment): - out_typescript_bindings = "\n\n\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: START\n\n" - + out_typescript_bindings = "" super_instantiator = "" bindings_instantiator = "" pointer_to_adder = "" @@ -760,7 +792,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ """ self.obj_defined([struct_name.replace("LDK", ""), struct_name.replace("LDK", "") + "Interface"], "structs") - out_typescript_bindings += "export interface " + struct_name + " {\n" + out_typescript_bindings += "/* @internal */\nexport interface " + struct_name + " {\n" java_meths = [] for fn_line in field_function_lines: if fn_line.fn_name != "free" and fn_line.fn_name != "cloned": @@ -775,7 +807,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_typescript_bindings += "}\n\n" - out_typescript_bindings += f"export function {struct_name}_new(impl: {struct_name}" + out_typescript_bindings += f"/* @internal */\nexport function {struct_name}_new(impl: {struct_name}" for var in flattened_field_var_conversions: if isinstance(var, ConvInfo): out_typescript_bindings += f", {var.arg_name}: {var.java_ty}" @@ -795,8 +827,6 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ }} """ - out_typescript_bindings += '\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: END\n\n\n' - # Now that we've written out our java code (and created java_meths), generate C out_c = "typedef struct " + struct_name + "_JCalls {\n" out_c += "\tatomic_size_t refcnt;\n" @@ -971,7 +1001,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ java_hu_class += "\t\tswitch (raw_ty) {\n" java_hu_subclasses = "" - out_java += "export class " + struct_name + " {\n" + out_java += "/* @internal */\nexport class " + struct_name + " {\n" out_java += "\tprotected constructor() {}\n" var_idx = 0 for var in variant_list: @@ -1111,7 +1141,8 @@ export class {human_ty} extends CommonBase {{ if not has_return_value: return_statement = '// debug statements here' - return f"""export function {method_name}({method_argument_string}): {return_java_ty} {{ + return f"""/* @internal */ +export function {method_name}({method_argument_string}): {return_java_ty} {{ if(!isWasmInitialized) {{ throw new Error("initializeWasm() must be awaited first!"); }}