From: Matt Corallo Date: Fri, 5 Aug 2022 19:52:27 +0000 (+0000) Subject: [TS] Redo C -> TS call function naming to make it harder to screw up X-Git-Tag: v0.0.110.1^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=6e1119f31549cabc63bd4f12fcff907ef28383e1;p=ldk-java [TS] Redo C -> TS call function naming to make it harder to screw up --- diff --git a/ts/js-wasm.h b/ts/js-wasm.h index 94466cf4..41db531d 100644 --- a/ts/js-wasm.h +++ b/ts/js-wasm.h @@ -10,40 +10,37 @@ extern size_t strlen(const char *s); // to use proper types. typedef uint32_t JSValue; -extern uint64_t js_invoke_function_u(JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue) __attribute__((import_name("js_invoke_function_u"))); -extern uint64_t js_invoke_function_b(JSValue,JSValue,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t) __attribute__((import_name("js_invoke_function_b"))); - -static inline JSValue js_invoke_function_u_(JSValue obj, JSValue fn){ - return js_invoke_function_u(obj,fn,0,0,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_u(JSValue obj, JSValue fn, JSValue a){ - return js_invoke_function_u(obj,fn,a,0,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_uu(JSValue obj, JSValue fn, JSValue a, JSValue b){ - return js_invoke_function_u(obj,fn,a,b,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_uuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c){ - return js_invoke_function_u(obj,fn,a,b,c,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_uuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){ - return js_invoke_function_u(obj,fn,a,b,c,d,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_uuuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e){ - return js_invoke_function_u(obj,fn,a,b,c,d,e,0,0,0,0,0); -} - -static inline uint64_t js_invoke_function_b_(JSValue obj, JSValue fn){ - return js_invoke_function_u(obj,fn,0,0,0,0,0,0,0,0,0,0); -} - -static inline uint64_t js_invoke_function_b_uuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){ - return js_invoke_function_u(obj,fn,a,b,c,d,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_b(JSValue obj, JSValue fn, uint64_t a){ - return js_invoke_function_b(obj,fn,a,0,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_u_bb(JSValue obj, JSValue fn, uint64_t a, uint64_t b){ - return js_invoke_function_b(obj,fn,a,b,0,0,0,0,0,0,0,0); -} + +#define N_TYPE_u uint32_t +#define N_TYPE_b uint64_t + +#define TOK(a) a +#define R_CAT2(a, b) a ## b +#define CAT2(a, b) R_CAT2(a, b) +#define R_CAT3(a, b, c) a ## b ## c +#define CAT3(a, b, c) R_CAT3(a, b, c) +#define R_CAT4(a, b, c, d) a ## b ## c ## d +#define CAT4(a, b, c, d) R_CAT4(a, b, c, d) +#define R_CAT5(a, b, c, d, e) a ## b ## c ## d ## e +#define CAT5(a, b, c, d, e) R_CAT5(a, b, c, d, e) +#define R_CAT6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f +#define CAT6(a, b, c, d, e, f) R_CAT6(a, b, c, d, e, f) + +#define _GET_CONCAT(_1, _2, _3, _4, _5, _6, CONC, ...) CONC +#define CAT(...) _GET_CONCAT(__VA_ARGS__, CAT6, CAT5, CAT4, CAT3, CAT2, TOK)(__VA_ARGS__) + +#define DO_STRING(a) #a +#define STRINGIZE(a) DO_STRING(a) + +#define DECL_IMPORT(A1, A2, A3, A4, A5, A6) \ + extern uint64_t CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6)) \ + (JSValue obj, JSValue fn, N_TYPE_##A1, N_TYPE_##A2, N_TYPE_##A3, N_TYPE_##A4, N_TYPE_##A5, N_TYPE_##A6) \ + __attribute__((import_name(STRINGIZE(CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6)))))); + +DECL_IMPORT(u, u, u, u, u, u) +DECL_IMPORT(b, u, u, u, u, u) +DECL_IMPORT(b, b, b, b, b, b) +DECL_IMPORT(u, b, u, u, u, u) +DECL_IMPORT(u, u, b, u, u, u) #endif diff --git a/typescript_strings.py b/typescript_strings.py index fc2b7dde..a665019f 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -125,19 +125,19 @@ async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) { isWasmInitialized = true; } +const fn_list = ["uuuuuu", "buuuuu", "bbbbbb", "ubuuuu", "uubuuu"]; + /* @internal */ export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) { - imports.env["js_invoke_function_u"] = js_invoke; - imports.env["js_invoke_function_b"] = js_invoke; + for (const fn of fn_list) { imports.env["js_invoke_function_" + fn] = js_invoke; } const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports); await finishInitializeWasm(wasmInstance); } /* @internal */ export async function initializeWasmFetch(uri: string) { + for (const fn of fn_list) { imports.env["js_invoke_function_" + fn] = js_invoke; } const stream = fetch(uri); - imports.env["js_invoke_function_u"] = js_invoke; - imports.env["js_invoke_function_b"] = js_invoke; const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports); await finishInitializeWasm(wasmInstance); }""" @@ -388,7 +388,7 @@ export class UnqualifiedError { 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 static constructor_new(value: bigint, script_pubkey: Uint8Array): TxOut { return new TxOut(null, bindings.TxOut_new(bindings.encodeUint8Array(script_pubkey), value)); } }""" @@ -1034,15 +1034,14 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n" fn_suffix = "" - if fn_line.ret_ty_info.c_ty == "uint64_t" or fn_line.ret_ty_info.c_ty == "int64_t": - fn_suffix += "b_" - else: - fn_suffix += "u_" - for arg in fn_line.args_ty: + assert len(fn_line.args_ty) < 6 + for arg_info in fn_line.args_ty: if arg_info.c_ty == "uint64_t" or arg_info.c_ty == "int64_t": fn_suffix += "b" else: fn_suffix += "u" + for i in range(0, 6 - len(fn_line.args_ty)): + fn_suffix += "u" if fn_line.ret_ty_info.c_ty.endswith("Array"): out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (" + fn_line.ret_ty_info.c_ty + ")" out_c += "js_invoke_function_" + fn_suffix + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) @@ -1060,9 +1059,11 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ for idx, arg_info in enumerate(fn_line.args_ty): if arg_info.ret_conv is not None: - out_c = out_c + ", (uint32_t)" + arg_info.ret_conv_name + out_c += ", (uint32_t)" + arg_info.ret_conv_name else: - out_c = out_c + ", (uint32_t)" + arg_info.arg_name + 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 + ");\n" if fn_line.ret_ty_info.arg_conv is not None: out_c = out_c + "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"