-package org.ldk.impl;
-import org.ldk.enums.*;
-
-public class bindings {
- public static class VecOrSliceDef {
- public long dataptr;
- public long datalen;
- public long stride;
- public VecOrSliceDef(long dataptr, long datalen, long stride) {
- this.dataptr = dataptr; this.datalen = datalen; this.stride = stride;
- }
- }
- static {
- System.loadLibrary("lightningjni");
- init(java.lang.Enum.class, VecOrSliceDef.class);
- init_class_cache();
- }
- static native void init(java.lang.Class c, java.lang.Class slicedef);
- static native void init_class_cache();
-
- public static native boolean deref_bool(long ptr);
- public static native long deref_long(long ptr);
- public static native void free_heap_ptr(long ptr);
- public static native byte[] read_bytes(long ptr, long len);
- public static native byte[] get_u8_slice_bytes(long slice_ptr);
- public static native long bytes_to_u8_vec(byte[] bytes);
- public static native long new_txpointer_copy_data(byte[] txdata);
- public static native void txpointer_free(long ptr);
- public static native byte[] txpointer_get_buffer(long ptr);
- public static native long vec_slice_len(long vec);
- public static native long new_empty_slice_vec();
-
- public static native long LDKCVec_u8Z_new(byte[] elems);
- public static native long LDKC2Tuple_u64u64Z_new(long a, long b);
- public static native long LDKC2Tuple_u64u64Z_get_a(long ptr);
- public static native long LDKC2Tuple_u64u64Z_get_b(long ptr);
+
+import * as fs from 'fs';
+const source = fs.readFileSync('./ldk.wasm');
+
+const memory = new WebAssembly.Memory({initial: 256});
+const wasmModule = new WebAssembly.Module(source);
+
+const imports: any = {};
+imports.env = {};
+
+imports.env.memoryBase = 0;
+imports.env.memory = memory;
+imports.env.tableBase = 0;
+imports.env.table = new WebAssembly.Table({initial: 4, element: 'anyfunc'});
+
+imports.env["abort"] = function () {
+ console.error("ABORT");
+};
+
+let wasm = null;
+let isWasmInitialized: boolean = false;
+
+
+// WASM CODEC
+
+const nextMultipleOfFour = (value: number) => {
+ return Math.ceil(value / 4) * 4;
+}
+
+const encodeArray = (inputArray) => {
+ const cArrayPointer = wasm.wasm_malloc((inputArray.length + 1) * 4);
+ const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer + 4, inputArray.length);
+ arrayMemoryView.set(inputArray, 1);
+ arrayMemoryView[0] = inputArray.length;
+ return cArrayPointer;
+}
+
+const getArrayLength = (arrayPointer) => {
+ const arraySizeViewer = new Uint32Array(
+ memory.buffer, // value
+ arrayPointer, // offset
+ 1 // one int
+ );
+ return arraySizeViewer[0];
+}
+const decodeUint8Array = (arrayPointer, free = true) => {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint8Array(
+ memory.buffer, // value
+ arrayPointer + 4, // offset (ignoring length bytes)
+ arraySize // uint8 count
+ );
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.free(arrayPointer);
+ }
+ return actualArray;
+}
+const decodeUint32Array = (arrayPointer, free = true) => {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint32Array(
+ memory.buffer, // value
+ arrayPointer + 4, // offset (ignoring length bytes)
+ arraySize // uint32 count
+ );
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.free(arrayPointer);
+ }
+ return actualArray;
+}
+
+const encodeString = (string) => {
+ // make malloc count divisible by 4
+ const memoryNeed = nextMultipleOfFour(string.length + 1);
+ const stringPointer = wasm.wasm_malloc(memoryNeed);
+ const stringMemoryView = new Uint8Array(
+ memory.buffer, // value
+ stringPointer, // offset
+ string.length + 1 // length
+ );
+ for (let i = 0; i < string.length; i++) {
+ stringMemoryView[i] = string.charCodeAt(i);
+ }
+ stringMemoryView[string.length] = 0;
+ return stringPointer;
+}
+
+const decodeString = (stringPointer, free = true) => {
+ const memoryView = new Uint8Array(memory.buffer, stringPointer);
+ let cursor = 0;
+ let result = '';
+
+ while (memoryView[cursor] !== 0) {
+ result += String.fromCharCode(memoryView[cursor]);
+ cursor++;
+ }
+
+ if (free) {
+ wasm.wasm_free(stringPointer);
+ }
+
+ return result;
+};
+
+export class VecOrSliceDef {
+ public dataptr: number;
+ public datalen: number;
+ public stride: number;
+ public constructor(dataptr: number, datalen: number, stride: number) {
+ this.dataptr = dataptr;
+ this.datalen = datalen;
+ this.stride = stride;
+ }
+}
+
+/*
+TODO: load WASM file
+static {
+ System.loadLibrary("lightningjni");
+ init(java.lang.Enum.class, VecOrSliceDef.class);
+ init_class_cache();
+}
+
+static native void init(java.lang.Class c, java.lang.Class slicedef);
+static native void init_class_cache();
+
+public static native boolean deref_bool(long ptr);
+public static native long deref_long(long ptr);
+public static native void free_heap_ptr(long ptr);
+public static native byte[] read_bytes(long ptr, long len);
+public static native byte[] get_u8_slice_bytes(long slice_ptr);
+public static native long bytes_to_u8_vec(byte[] bytes);
+public static native long new_txpointer_copy_data(byte[] txdata);
+public static native void txpointer_free(long ptr);
+public static native byte[] txpointer_get_buffer(long ptr);
+public static native long vec_slice_len(long vec);
+public static native long new_empty_slice_vec();
+*/
+
+ public static native long LDKCVec_u8Z_new(number[] elems);
+ public static native long LDKC2Tuple_u64u64Z_new(number a, number b);
+ public static native number LDKC2Tuple_u64u64Z_get_a(long ptr);
+ public static native number LDKC2Tuple_u64u64Z_get_b(long ptr);