From d9ed706607bc61a1f7616724113876575fd9db43 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Wed, 27 Jan 2021 10:36:26 -0800 Subject: [PATCH] enable async wasm initialization --- typescript_strings.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/typescript_strings.py b/typescript_strings.py index a1cba27c..1715a54d 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -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 { + if(isWasmInitialized && !allowDoubleInitialization) { + return; + } + const wasmInstance = await WebAssembly.instantiate(wasmModule, imports) + wasm = wasmInstance.exports; + isWasmInitialized = true; + } + """ self.common_base = """ export default class CommonBase { @@ -286,8 +295,8 @@ 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 @@ -889,6 +898,9 @@ const decodeString = (stringPointer, free = true) => { return_value = f"{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}} -- 2.39.5