Skip to content

Commit eec046b

Browse files
authored
Merge pull request #81 from TheBlueMatt/main
[TS] Unify Web + Node Codebases (with different init functions)
2 parents c3594d0 + 9566288 commit eec046b

File tree

8 files changed

+97
-52
lines changed

8 files changed

+97
-52
lines changed

genbindings.sh

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ usage() {
66
echo "debug should either be true, false, or leaks"
77
echo "debug of leaks turns on leak tracking on an optimized release bianry"
88
echo "android_web should either be true or false and indicates if we build for android (Java) or web (WASM)"
9+
echo "Note that web currently generates the same results as !web (ie Node.JS)"
910
exit 1
1011
}
1112
[ "$1" = "" ] && usage
@@ -219,17 +220,13 @@ else
219220
mv $F.tmp $F
220221
done
221222
rm imports.mts.part
222-
if [ "$4" = "true" ]; then
223-
tsc
224-
else
225-
tsc --types node --typeRoots .
226-
cp ../$WASM_FILE liblightningjs.wasm
227-
echo Ready to publish!
228-
if [ -x "$(which node)" ]; then
229-
NODE_V="$(node --version)"
230-
if [ "${NODE_V:1:2}" -gt 14 ]; then
231-
node test/node.mjs
232-
fi
223+
tsc --types node --typeRoots .
224+
cp ../$WASM_FILE liblightningjs.wasm
225+
echo Ready to publish!
226+
if [ -x "$(which node)" ]; then
227+
NODE_V="$(node --version)"
228+
if [ "${NODE_V:1:2}" -gt 14 ]; then
229+
node test/node.mjs
233230
fi
234231
fi
235232
fi

ts/bindings.mts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ imports.env = {};
66

77
var js_objs: Array<WeakRef<object>> = [];
88
var js_invoke: Function;
9+
var getRandomValues: Function;
910

1011
imports.wasi_snapshot_preview1 = {
1112
"fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
@@ -30,7 +31,7 @@ imports.wasi_snapshot_preview1 = {
3031
},
3132
"random_get": (buf_ptr: number, buf_len: number) => {
3233
const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len);
33-
crypto.getRandomValues(buf);
34+
getRandomValues(buf);
3435
return 0;
3536
},
3637
"environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
@@ -55,11 +56,14 @@ imports.wasi_snapshot_preview1 = {
5556
var wasm: any = null;
5657
let isWasmInitialized: boolean = false;
5758

58-
/* @internal */
59-
export async function initializeWasm(uri: string) {
60-
const stream = fetch(uri);
61-
imports.env["js_invoke_function"] = js_invoke;
62-
const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
59+
async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
60+
if (typeof crypto === "undefined") {
61+
var crypto_import = (await import('crypto')).webcrypto;
62+
getRandomValues = crypto_import.getRandomValues.bind(crypto_import);
63+
} else {
64+
getRandomValues = crypto.getRandomValues.bind(crypto);
65+
}
66+
6367
wasm = wasmInstance.exports;
6468
if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) {
6569
throw new Error("Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint");
@@ -79,8 +83,22 @@ export async function initializeWasm(uri: string) {
7983
console.log("Loaded LDK-Java Bindings with LDK " + ldk_version + " and LDK-C-Bindings " + c_bindings_version);
8084

8185
isWasmInitialized = true;
82-
};
86+
}
8387

88+
/* @internal */
89+
export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
90+
imports.env["js_invoke_function"] = js_invoke;
91+
const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
92+
await finishInitializeWasm(wasmInstance);
93+
}
94+
95+
/* @internal */
96+
export async function initializeWasmFetch(uri: string) {
97+
const stream = fetch(uri);
98+
imports.env["js_invoke_function"] = js_invoke;
99+
const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
100+
await finishInitializeWasm(wasmInstance);
101+
}
84102
// WASM CODEC
85103

86104
const nextMultipleOfFour = (value: number) => {

ts/index.mts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { initializeWasm as bindingsInit } from './bindings.mjs';
2-
export function initializeWasm(path: string) {
3-
bindingsInit(path);
1+
import { initializeWasmFetch, initializeWasmFromUint8Array } from './bindings.mjs';
2+
/** Initializes the WASM backend by calling `fetch()` on the given URI - Browser only */
3+
export async function initializeWasmWebFetch(uri: string) {
4+
await initializeWasmFetch(uri);
45
}
6+
/** Initializes the WASM backend given a Uint8Array of the .wasm binary file - Browser or Node.JS */
7+
export async function initializeWasmFromBinary(bin: Uint8Array) {
8+
await initializeWasmFromUint8Array(bin);
9+
}
10+
511
export * from './structs/TxOut.mjs';
612
export * from './enums/AccessError.mjs';
713
export * from './enums/COption_NoneZ.mjs';

ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lightningdevkit",
3-
"version": "0.0.104.1alpha3",
3+
"version": "0.0.104.1alpha4",
44
"description": "Lightning Development Kit",
55
"main": "index.mjs",
66
"type": "module",

ts/test/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ <h1><div id="results">Running LDK-TS Tests...</div></h1>
77
var test_runner;
88
</script>
99
<script type="module">
10-
import { run_tests } from './tests.mjs';
11-
test_runner = run_tests;
10+
import { run_tests_web } from './tests.mjs';
11+
test_runner = run_tests_web;
1212
try {
13-
const result = await run_tests('../liblightningjs.wasm');
13+
const result = await run_tests_web('../liblightningjs.wasm');
1414
if (result === true) {
1515
document.getElementById("results").innerHTML = "All Tests Passed (note free-time errors may still occurr)!";
1616
} else {

ts/test/node.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { run_tests } from "./tests.mjs";
1+
import { run_tests_node } from "./tests.mjs";
22
import { strict as assert } from 'assert';
3-
const res = await run_tests('./liblightningjs.wasm');
3+
import * as fs from 'fs';
4+
5+
const bin = fs.readFileSync('./liblightningjs.wasm');
6+
const res = await run_tests_node(bin);
47
assert(res);

ts/test/tests.mts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ tests.push(async () => {
197197
return true;
198198
});
199199

200-
export async function run_tests(wasm_path: string, check_leaks: boolean = true) {
201-
await rawldk.initializeWasm(wasm_path);
202-
200+
async function run_tests(check_leaks: boolean) {
203201
var test_runs = [];
204202
for (const test of tests) {
205203
test_runs.push(test());
@@ -229,3 +227,13 @@ export async function run_tests(wasm_path: string, check_leaks: boolean = true)
229227
});
230228
return allocs_finished;
231229
}
230+
231+
export async function run_tests_web(wasm_path: string, check_leaks: boolean = true) {
232+
await ldk.initializeWasmWebFetch(wasm_path);
233+
return await run_tests(check_leaks);
234+
}
235+
236+
export async function run_tests_node(wasm_file: Uint8Array, check_leaks: boolean = true) {
237+
await ldk.initializeWasmFromBinary(wasm_file);
238+
return await run_tests(check_leaks);
239+
}

typescript_strings.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
4242
4343
var js_objs: Array<WeakRef<object>> = [];
4444
var js_invoke: Function;
45+
var getRandomValues: Function;
4546
4647
imports.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
9192
var wasm: any = null;
9293
let 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
135142
const 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

Comments
 (0)