Merge branch 'master' of https://git.bitcoin.ninja/ldk-java into typescript_conversion
authorArik Sosman <git@arik.io>
Wed, 27 Jan 2021 22:48:01 +0000 (14:48 -0800)
committerArik Sosman <git@arik.io>
Wed, 27 Jan 2021 22:48:01 +0000 (14:48 -0800)
\ 1 Conflicts:
\ 1 typescript_strings.py

typescript_strings.py

index 5820e0a6424dba2154608ca83bfd7be225729899..b96340c04e1a5c223e7cd89ac0a3ed14ace741ce 100644 (file)
@@ -71,7 +71,16 @@ public static native long new_empty_slice_vec();
 
 """
 
-        self.bindings_footer = ""
+        self.bindings_footer = """
+        export async function initializeWasm(allowDoubleInitialization: boolean = false): Promise<void> {
+            if(isWasmInitialized && !allowDoubleInitialization) {
+                return;
+            }
+            const wasmInstance = await WebAssembly.instantiate(wasmModule, imports)
+            wasm = wasmInstance.exports;
+            isWasmInitialized = true;
+        }
+        """
 
         self.common_base = """
             export default class CommonBase {
@@ -281,6 +290,7 @@ import * as bindings from '../bindings' // TODO: figure out location
     def wasm_import_header(self, target):
         if target == Target.NODEJS:
             return """
+            
 import * as fs from 'fs';
 const source = fs.readFileSync('./ldk.wasm');
 
@@ -299,8 +309,9 @@ imports.env["abort"] = function () {
     console.error("ABORT");
 };
 
-const wasmInstance = await WebAssembly.instantiate(wasmModule, imports)
-const wasm = wasmInstance.exports;
+let wasm = null;
+let isWasmInitialized: boolean = false;
+
 
 // WASM CODEC
 
@@ -370,7 +381,9 @@ const decodeString = (stringPointer, free = true) => {
 
     return result;
 };
-"""
+
+
+            """
         return ''
 
     def init_str(self):
@@ -869,16 +882,21 @@ const decodeString = (stringPointer, free = true) => {
                 method_argument_string += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
                 native_call_argument_string += native_argument
 
+        has_return_value = return_type_info.c_ty != 'void'
         needs_decoding = return_type_info.c_ty in self.wasm_decoding_map
-        return_value = 'nativeResponseValue'
-        if needs_decoding:
+        return_statement = 'return nativeResponseValue;'
+        if not has_return_value:
+            return_statement = '// debug statements here'
+        elif needs_decoding:
             converter = self.wasm_decoding_map[return_type_info.c_ty]
-            return_value = f"{converter}(nativeResponseValue)"
+            return_statement = f"return {converter}(nativeResponseValue);"
 
         out_java = f"""\texport function {method_name}({method_argument_string}): {return_type_info.java_ty} {{
+            if(!isWasmInitialized){{
+                throw new Error("initializeWasm() must be awaited first!");
+            }}
             const nativeResponseValue = wasm.{method_name}({native_call_argument_string});
-            return {return_value};
-        \n\t}}
+            {return_statement}\n\t}}
         \n"""