X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=892c63b802488a20b73b7101fa348e11b58afaee;hb=1a849ce9a84a17a58ba0e19cb60dfdf95242356b;hp=8002aa7c16ad3309cc32b5fe81a512c8488c55ac;hpb=1f10a8405a1e6fb2c133a31909a1601a5691ef77;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index 8002aa7c..892c63b8 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -22,6 +22,12 @@ class Consts: uint32_t = ['number', 'Uint32Array'], uint64_t = ['BigInt'], ) + self.java_type_map = dict( + String = "number" + ) + self.java_hu_type_map = dict( + String = "string" + ) self.wasm_decoding_map = dict( int8_tArray = 'decodeUint8Array' @@ -353,6 +359,10 @@ import * as InternalUtils from '../InternalUtils.mjs' return "str_ref_to_ts(" + var_name + ", " + str_len + ")" def str_ref_to_c_call(self, var_name): return "str_ref_to_owned_c(" + var_name + ")" + def str_to_hu_conv(self, var_name): + return "const " + var_name + "_conv: string = bindings.decodeString(" + var_name + ");" + def str_from_hu_conv(self, var_name): + return ("bindings.encodeString(" + var_name + ")", "") def c_fn_name_define_pfx(self, fn_name, have_args): return " __attribute__((export_name(\"TS_" + fn_name + "\"))) TS_" + fn_name + "(" @@ -366,21 +376,44 @@ var js_objs: Array> = []; var js_invoke: Function; imports.wasi_snapshot_preview1 = { - "fd_write" : () => { - console.log("ABORT"); + "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => { + // This should generally only be used to print panic messages + console.log("FD_WRITE to " + fd + " in " + iovec_array_len + " chunks."); + const ptr_len_view = new Uint32Array(wasm.memory.buffer, iovec_array_ptr, iovec_array_len * 2); + for (var i = 0; i < iovec_array_len; i++) { + const bytes_view = new Uint8Array(wasm.memory.buffer, ptr_len_view[i*2], ptr_len_view[i*2+1]); + console.log(String.fromCharCode(...bytes_view)); + } + return 0; }, - "random_get" : () => { - console.log("RAND GET"); + "random_get": (buf_ptr: number, buf_len: number) => { + const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len); + crypto.getRandomValues(buf); + return 0; }, - "environ_sizes_get" : () => { + "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => { + // This is called before fd_write to format + print panic messages console.log("wasi_snapshot_preview1:environ_sizes_get"); + const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1); + out_count_view[0] = 1; + const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1); + out_len_view[0] = "RUST_BACKTRACE=1".length + 1; // Note that string must be NULL-terminated + return 0; + }, + "environ_get": (environ_ptr: number, environ_buf_ptr: number) => { + // This is called before fd_write to format + print panic messages + console.log("wasi_snapshot_preview1:environ_get"); + const out_ptrs = new Uint32Array(wasm.memory.buffer, environ_ptr, 2); + out_ptrs[0] = environ_buf_ptr; + out_ptrs[1] = "RUST_BACKTRACE=1".length; + const out_environ = new Uint8Array(wasm.memory.buffer, environ_buf_ptr, out_ptrs[1]); + for (var i = 0; i < out_ptrs[1]; i++) { out_environ[i] = "RUST_BACKTRACE=1".codePointAt(i); } + out_environ[out_ptrs[1]] = 0; + return 0; }, "proc_exit" : () => { console.log("wasi_snapshot_preview1:proc_exit"); }, - "environ_get" : () => { - console.log("wasi_snapshot_preview1:environ_get"); - }, }; var wasm: any = null; @@ -389,6 +422,7 @@ let isWasmInitialized: boolean = false; if target == Target.NODEJS: res += """import * as fs from 'fs'; +import { webcrypto as crypto } from 'crypto'; export async function initializeWasm(path: string) { const source = fs.readFileSync(path); imports.env["js_invoke_function"] = js_invoke; @@ -480,38 +514,22 @@ const decodeUint32Array = (arrayPointer: number, free = true) => { return actualArray; } -const encodeString = (string: string) => { - // make malloc count divisible by 4 - const memoryNeed = nextMultipleOfFour(string.length + 1); - const stringPointer = wasm.TS_malloc(memoryNeed); - const stringMemoryView = new Uint8Array( - wasm.memory.buffer, // value - stringPointer, // offset - string.length + 1 // length - ); - for (let i = 0; i < string.length; i++) { - stringMemoryView[i] = string.charCodeAt(i); - } - stringMemoryView[string.length] = 0; - return stringPointer; +export function encodeString(str: string): number { + const charArray = new TextEncoder().encode(str); + return encodeUint8Array(charArray); } -const decodeString = (stringPointer: number, free = true) => { - const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer); - let cursor = 0; - let result = ''; - - while (memoryView[cursor] !== 0) { - result += String.fromCharCode(memoryView[cursor]); - cursor++; - } +export function decodeString(stringPointer: number, free = true): string { + const arraySize = getArrayLength(stringPointer); + const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize); + const result = new TextDecoder("utf-8").decode(memoryView); if (free) { - wasm.wasm_free(stringPointer); + wasm.TS_free(stringPointer); } return result; -}; +} """ def init_str(self): @@ -792,7 +810,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_c += "js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) elif fn_line.ret_ty_info.java_ty == "void": out_c = out_c + "\tjs_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) - elif fn_line.ret_ty_info.java_ty == "String": + elif fn_line.ret_ty_info.java_hu_ty == "string": out_c = out_c + "\tjstring ret = (jstring)js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) 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->instance_ptr, " + str(self.function_ptr_counter)