]> git.bitcoin.ninja Git - ldk-java/commitdiff
[TS] Unify Web and Node.JS output so it can be universal
authorMatt Corallo <git@bluematt.me>
Mon, 17 Jan 2022 22:27:50 +0000 (22:27 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 17 Jan 2022 23:42:15 +0000 (23:42 +0000)
genbindings.sh
ts/test/index.html
ts/test/node.mjs
ts/test/tests.mts
typescript_strings.py

index 128a3afbe481147d0a5df19b4a0df7ed164f7446..7fc10bc9924ecfb0cc68d47208d0c4f3c2fa2929 100755 (executable)
@@ -6,6 +6,7 @@ usage() {
        echo "debug should either be true, false, or leaks"
        echo "debug of leaks turns on leak tracking on an optimized release bianry"
        echo "android_web should either be true or false and indicates if we build for android (Java) or web (WASM)"
+       echo "Note that web currently generates the same results as !web (ie Node.JS)"
        exit 1
 }
 [ "$1" = "" ] && usage
@@ -219,17 +220,13 @@ else
                        mv $F.tmp $F
                done
                rm imports.mts.part
-               if [ "$4" = "true" ]; then
-                       tsc
-               else
-                       tsc --types node --typeRoots .
-                       cp ../$WASM_FILE liblightningjs.wasm
-                       echo Ready to publish!
-                       if [ -x "$(which node)" ]; then
-                               NODE_V="$(node --version)"
-                               if [ "${NODE_V:1:2}" -gt 14 ]; then
-                                       node test/node.mjs
-                               fi
+               tsc --types node --typeRoots .
+               cp ../$WASM_FILE liblightningjs.wasm
+               echo Ready to publish!
+               if [ -x "$(which node)" ]; then
+                       NODE_V="$(node --version)"
+                       if [ "${NODE_V:1:2}" -gt 14 ]; then
+                               node test/node.mjs
                        fi
                fi
        fi
index 3fc55a6d2ef22697d83b9725343cd8de0942ce91..60fd523cf286fbcbc7adebaa0919ad67f5f1dfe5 100644 (file)
@@ -7,10 +7,10 @@
        var test_runner;
 </script>
 <script type="module">
-       import { run_tests } from './tests.mjs';
-       test_runner = run_tests;
+       import { run_tests_web } from './tests.mjs';
+       test_runner = run_tests_web;
        try {
-               const result = await run_tests('../liblightningjs.wasm');
+               const result = await run_tests_web('../liblightningjs.wasm');
                if (result === true) {
                        document.getElementById("results").innerHTML = "All Tests Passed (note free-time errors may still occurr)!";
                } else {
index dfabe66e6b55573281b0276e8c2fdf4e4f206e42..d144281435fca15c2438c2929a941abe6ab618ee 100644 (file)
@@ -1,4 +1,7 @@
-import { run_tests } from "./tests.mjs";
+import { run_tests_node } from "./tests.mjs";
 import { strict as assert } from 'assert';
-const res = await run_tests('./liblightningjs.wasm');
+import * as fs from 'fs';
+
+const bin = fs.readFileSync('./liblightningjs.wasm');
+const res = await run_tests_node(bin);
 assert(res);
index 4299f3b83636f756618a33500fe285a0f13d96c2..35631d25392f68b95bc170efeb7f9678059307a0 100644 (file)
@@ -197,9 +197,7 @@ tests.push(async () => {
        return true;
 });
 
-export async function run_tests(wasm_path: string, check_leaks: boolean = true) {
-       await rawldk.initializeWasm(wasm_path);
-
+async function run_tests(check_leaks: boolean) {
        var test_runs = [];
        for (const test of tests) {
                test_runs.push(test());
@@ -229,3 +227,13 @@ export async function run_tests(wasm_path: string, check_leaks: boolean = true)
        });
        return allocs_finished;
 }
+
+export async function run_tests_web(wasm_path: string, check_leaks: boolean = true) {
+       await ldk.initializeWasmWebFetch(wasm_path);
+       return await run_tests(check_leaks);
+}
+
+export async function run_tests_node(wasm_file: Uint8Array, check_leaks: boolean = true) {
+       await ldk.initializeWasmFromBinary(wasm_file);
+       return await run_tests(check_leaks);
+}
index c0a0cccbf0ed816764fb3a285cf6dae2620400ba..2b0ab1c972b2dff3e3a7944e658bb1890a5a1508 100644 (file)
@@ -42,6 +42,7 @@ imports.env = {};
 
 var js_objs: Array<WeakRef<object>> = [];
 var js_invoke: Function;
+var getRandomValues: Function;
 
 imports.wasi_snapshot_preview1 = {
        "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
@@ -66,7 +67,7 @@ imports.wasi_snapshot_preview1 = {
        },
        "random_get": (buf_ptr: number, buf_len: number) => {
                const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len);
-               crypto.getRandomValues(buf);
+               getRandomValues(buf);
                return 0;
        },
        "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
@@ -90,25 +91,15 @@ imports.wasi_snapshot_preview1 = {
 
 var wasm: any = null;
 let isWasmInitialized: boolean = false;
-"""
 
-        if target == Target.NODEJS:
-            self.bindings_header += """import * as fs from 'fs';
-import { webcrypto as crypto } from 'crypto';
-/* @internal */
-export async function initializeWasm(path: string) {
-       const source = fs.readFileSync(path);
-       imports.env["js_invoke_function"] = js_invoke;
-       const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);"""
-        else:
-            self.bindings_header += """
-/* @internal */
-export async function initializeWasm(uri: string) {
-       const stream = fetch(uri);
-       imports.env["js_invoke_function"] = js_invoke;
-       const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);"""
+async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
+       if (typeof crypto === "undefined") {
+               var crypto_import = (await import('crypto')).webcrypto;
+               getRandomValues = crypto_import.getRandomValues.bind(crypto_import);
+       } else {
+               getRandomValues = crypto.getRandomValues.bind(crypto);
+       }
 
-        self.bindings_header += """
        wasm = wasmInstance.exports;
        if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) {
                throw new Error(\"Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint");
@@ -128,8 +119,24 @@ export async function initializeWasm(uri: string) {
        console.log(\"Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version);
 
        isWasmInitialized = true;
-};
+}
 
+/* @internal */
+export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
+       imports.env["js_invoke_function"] = js_invoke;
+       const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
+       await finishInitializeWasm(wasmInstance);
+}
+
+/* @internal */
+export async function initializeWasmFetch(uri: string) {
+       const stream = fetch(uri);
+       imports.env["js_invoke_function"] = js_invoke;
+       const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
+       await finishInitializeWasm(wasmInstance);
+}"""
+
+        self.bindings_header += """
 // WASM CODEC
 
 const nextMultipleOfFour = (value: number) => {
@@ -247,10 +254,16 @@ export function debugPrintRemainingAllocs() {
             self.bindings_header += "/* @internal */ export function debugPrintRemainingAllocs() { }\n"
 
         with open(outdir + "/index.mts", 'a') as index:
-            index.write("""import { initializeWasm as bindingsInit } from './bindings.mjs';
-export function initializeWasm(path: string) {
-       bindingsInit(path);
+            index.write("""import { initializeWasmFetch, initializeWasmFromUint8Array } from './bindings.mjs';
+/** Initializes the WASM backend by calling `fetch()` on the given URI - Browser only */
+export async function initializeWasmWebFetch(uri: string) {
+       await initializeWasmFetch(uri);
+}
+/** Initializes the WASM backend given a Uint8Array of the .wasm binary file - Browser or Node.JS */
+export async function initializeWasmFromBinary(bin: Uint8Array) {
+       await initializeWasmFromUint8Array(bin);
 }
+
 """)
 
         self.bindings_version_file = """export function get_ldk_java_bindings_version(): String {