@@ -42,6 +42,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
4242
4343var js_objs: Array<WeakRef<object>> = [];
4444var js_invoke: Function;
45+ var getRandomValues: Function;
4546
4647imports.wasi_snapshot_preview1 = {
4748 "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
@@ -66,7 +67,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
6667 },
6768 "random_get": (buf_ptr: number, buf_len: number) => {
6869 const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len);
69- crypto. getRandomValues(buf);
70+ getRandomValues(buf);
7071 return 0;
7172 },
7273 "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
@@ -90,25 +91,15 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
9091
9192var wasm: any = null;
9293let isWasmInitialized: boolean = false;
93- """
9494
95- if target == Target .NODEJS :
96- self .bindings_header += """import * as fs from 'fs';
97- import { webcrypto as crypto } from 'crypto';
98- /* @internal */
99- export async function initializeWasm(path: string) {
100- const source = fs.readFileSync(path);
101- imports.env["js_invoke_function"] = js_invoke;
102- const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);"""
103- else :
104- self .bindings_header += """
105- /* @internal */
106- export async function initializeWasm(uri: string) {
107- const stream = fetch(uri);
108- imports.env["js_invoke_function"] = js_invoke;
109- const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);"""
95+ async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
96+ if (typeof crypto === "undefined") {
97+ var crypto_import = (await import('crypto')).webcrypto;
98+ getRandomValues = crypto_import.getRandomValues.bind(crypto_import);
99+ } else {
100+ getRandomValues = crypto.getRandomValues.bind(crypto);
101+ }
110102
111- self .bindings_header += """
112103 wasm = wasmInstance.exports;
113104 if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) {
114105 throw new Error(\" Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint");
@@ -128,8 +119,24 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
128119 console.log(\" Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version);
129120
130121 isWasmInitialized = true;
131- };
122+ }
132123
124+ /* @internal */
125+ export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
126+ imports.env["js_invoke_function"] = js_invoke;
127+ const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
128+ await finishInitializeWasm(wasmInstance);
129+ }
130+
131+ /* @internal */
132+ export async function initializeWasmFetch(uri: string) {
133+ const stream = fetch(uri);
134+ imports.env["js_invoke_function"] = js_invoke;
135+ const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
136+ await finishInitializeWasm(wasmInstance);
137+ }"""
138+
139+ self .bindings_header += """
133140// WASM CODEC
134141
135142const nextMultipleOfFour = (value: number) => {
@@ -247,10 +254,16 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
247254 self .bindings_header += "/* @internal */ export function debugPrintRemainingAllocs() { }\n "
248255
249256 with open (outdir + "/index.mts" , 'a' ) as index :
250- index .write ("""import { initializeWasm as bindingsInit } from './bindings.mjs';
251- export function initializeWasm(path: string) {
252- bindingsInit(path);
257+ index .write ("""import { initializeWasmFetch, initializeWasmFromUint8Array } from './bindings.mjs';
258+ /** Initializes the WASM backend by calling `fetch()` on the given URI - Browser only */
259+ export async function initializeWasmWebFetch(uri: string) {
260+ await initializeWasmFetch(uri);
261+ }
262+ /** Initializes the WASM backend given a Uint8Array of the .wasm binary file - Browser or Node.JS */
263+ export async function initializeWasmFromBinary(bin: Uint8Array) {
264+ await initializeWasmFromUint8Array(bin);
253265}
266+
254267""" )
255268
256269 self .bindings_version_file = """export function get_ldk_java_bindings_version(): String {
0 commit comments