X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=54159d8d85099d3c0db71e0710dd7194d5618c2f;hb=99d1a3bd6dc5c7ed1499b30bfc2b4c66ffaf858a;hp=4300f1f80064d3bcba2fadf9cce07fef41f3e471;hpb=753d902bc977acbf08b3166f054673236c39c01b;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index 4300f1f8..54159d8d 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -71,6 +71,8 @@ public static native long new_empty_slice_vec(); """ + self.bindings_version_file = "" + self.bindings_footer = """ export async function initializeWasm(allowDoubleInitialization: boolean = false): Promise { if(isWasmInitialized && !allowDoubleInitialization) { @@ -97,8 +99,7 @@ public static native long new_empty_slice_vec(); } """ - self.c_file_pfx = """#include -#include "js-wasm.h" + self.c_file_pfx = """#include "js-wasm.h" #include #include @@ -119,9 +120,10 @@ void *malloc(size_t size); void free(void *ptr); #define MALLOC(a, _) malloc(a) -#define FREE(p) if ((long)(p) > 1024) { free(p); } +#define FREE(p) if ((unsigned long)(p) > 4096) { free(p); } #define DO_ASSERT(a) (void)(a) #define CHECK(a) +#define CHECK_ACCESS(p) """ else: self.c_file_pfx = self.c_file_pfx + """ @@ -173,11 +175,21 @@ static void alloc_freed(void* ptr) { __real_free(it); } static void FREE(void* ptr) { - if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys + if ((unsigned long)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys alloc_freed(ptr); __real_free(ptr); } +static void CHECK_ACCESS(void* ptr) { + allocation* it = allocation_ll; + while (it->ptr != ptr) { + it = it->next; + if (it == NULL) { + return; // addrsan should catch malloc-unknown and print more info than we have + } + } +} + void* __wrap_malloc(size_t len) { void* res = __real_malloc(len); new_allocation(res, "malloc call"); @@ -262,6 +274,8 @@ void __attribute__((visibility("default"))) TS_free(uint32_t ptr) { } """ + self.c_version_file = "" + self.hu_struct_file_prefix = f""" import CommonBase from './CommonBase'; import * as bindings from '../bindings' // TODO: figure out location @@ -273,6 +287,7 @@ import * as bindings from '../bindings' // TODO: figure out location self.ptr_native_ty = "number" self.result_c_ty = "uint32_t" self.ptr_arr = "ptrArray" + self.is_arr_some_check = ("", " != 0") self.get_native_arr_len_call = ("*((uint32_t*)", ")") def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var): @@ -451,7 +466,7 @@ const decodeString = (stringPointer, free = true) => { return "" def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment): - out_c = "static inline " + struct_name + " " + struct_name + "_from_js(int32_t ord) {\n" + out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_js(int32_t ord) {\n" out_c = out_c + "\tswitch (ord) {\n" ord_v = 0 @@ -465,7 +480,7 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "\tabort();\n" out_c = out_c + "}\n" - out_c = out_c + "static inline int32_t " + struct_name + "_to_js(" + struct_name + " val) {\n" + out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_js(LDK" + struct_name + " val) {\n" out_c = out_c + "\tswitch (val) {\n" ord_v = 0 for var in variants: @@ -526,7 +541,7 @@ const decodeString = (stringPointer, free = true) => { java_methods = [] for fn_line in field_function_lines: java_method_descriptor = "" - if fn_line.fn_name != "free" and fn_line.fn_name != "clone": + if fn_line.fn_name != "free" and fn_line.fn_name != "cloned": out_java_interface += fn_line.fn_name + "(" out_interface_implementation_overrides += f"{fn_line.fn_name} (" @@ -643,7 +658,7 @@ const decodeString = (stringPointer, free = true) => { out_typescript_bindings += "\t\texport interface " + struct_name + " {\n" java_meths = [] for fn_line in field_function_lines: - if fn_line.fn_name != "free" and fn_line.fn_name != "clone": + if fn_line.fn_name != "free" and fn_line.fn_name != "cloned": out_typescript_bindings += f"\t\t\t{fn_line.fn_name} (" for idx, arg_conv_info in enumerate(fn_line.args_ty): @@ -680,7 +695,7 @@ const decodeString = (stringPointer, free = true) => { # We're a supertrait out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n" for fn in field_function_lines: - if fn.fn_name != "free" and fn.fn_name != "clone": + if fn.fn_name != "free" and fn.fn_name != "cloned": out_c = out_c + "\tuint32_t " + fn.fn_name + "_meth;\n" out_c = out_c + "} " + struct_name + "_JCalls;\n" @@ -690,13 +705,13 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n" out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n" for fn in field_function_lines: - if fn.fn_name != "free" and fn.fn_name != "clone": + if fn.fn_name != "free" and fn.fn_name != "cloned": out_c = out_c + "\t\tjs_free(j_calls->" + fn.fn_name + "_meth);\n" out_c = out_c + "\t\tFREE(j_calls);\n" out_c = out_c + "\t}\n}\n" for idx, fn_line in enumerate(field_function_lines): - if fn_line.fn_name != "free" and fn_line.fn_name != "clone": + if fn_line.fn_name != "free" and fn_line.fn_name != "cloned": assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == "" out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall(" if fn_line.self_is_const: @@ -720,10 +735,12 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " ret = js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" elif fn_line.ret_ty_info.java_ty == "void": out_c = out_c + "\tjs_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" + elif fn_line.ret_ty_info.java_ty == "String": + out_c = out_c + "\tuint32_t ret = js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" elif not fn_line.ret_ty_info.passed_as_ptr: out_c = out_c + "\treturn js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" else: - out_c = out_c + "\t" + fn_line.ret_ty_info.rust_obj + "* ret = (" + fn_line.ret_ty_info.rust_obj + "*)js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" + out_c = out_c + "\tuint32_t ret = js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth" for idx, arg_info in enumerate(fn_line.args_ty): if arg_info.ret_conv is not None: @@ -737,13 +754,12 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "}\n" # Write out a clone function whether we need one or not, as we use them in moving to rust - out_c = out_c + "static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n" - out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n" + out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n" + out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n" out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n" for var in field_var_conversions: if not isinstance(var, ConvInfo): out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n" - out_c = out_c + "\treturn (void*) this_arg;\n" out_c = out_c + "}\n" out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (/*TODO: JS Object Reference */void* o" @@ -759,7 +775,7 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "\t//TODO: Assign calls->o from o\n" for (fn_name, java_meth_descr) in java_meths: - if fn_name != "free" and fn_name != "clone": + if fn_name != "free" and fn_name != "cloned": out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n" out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n" @@ -769,12 +785,12 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "\n\t" + struct_name + " ret = {\n" out_c = out_c + "\t\t.this_arg = (void*) calls,\n" for fn_line in field_function_lines: - if fn_line.fn_name != "free" and fn_line.fn_name != "clone": + if fn_line.fn_name != "free" and fn_line.fn_name != "cloned": out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n" elif fn_line.fn_name == "free": out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n" else: - out_c = out_c + "\t\t.clone = " + struct_name + "_JCalls_clone,\n" + out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n" for var in field_var_conversions: if isinstance(var, ConvInfo): if var.arg_conv_name is not None: @@ -849,7 +865,7 @@ const decodeString = (stringPointer, free = true) => { init_meth_params = "" init_meth_body = "" hu_conv_body = "" - for idx, field_ty in enumerate(var.fields): + for idx, (field_ty, field_docs) in enumerate(var.fields): out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n" if field_ty.to_hu_conv is not None: @@ -882,7 +898,7 @@ const decodeString = (stringPointer, free = true) => { for var in variant_list: out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") c_params = [] - for idx, field_map in enumerate(var.fields): + for idx, (field_map, _) in enumerate(var.fields): if field_map.ret_conv is not None: out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) if var.tuple_variant: @@ -934,7 +950,10 @@ const decodeString = (stringPointer, free = true) => { """ return out_opaque_struct_human - def map_function(self, argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator, doc_comment): + def map_tuple(self, struct_name): + return self.map_opaque_struct(struct_name, "A Tuple") + + 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 @@ -987,7 +1006,6 @@ const decodeString = (stringPointer, free = true) => { if not args_known: out_java_struct += ("\t// Skipped " + method_name + "\n") else: - meth_n = method_name[len(struct_meth) + 1:] if not takes_self: out_java_struct += ( "\tpublic static " + return_type_info.java_hu_ty + " constructor_" + meth_n + "(")