9 imports.wasi_snapshot_preview1 = {
13 "random_get" : () => {
16 "environ_sizes_get" : () => {
17 console.log("wasi_snapshot_preview1:environ_sizes_get");
20 console.log("wasi_snapshot_preview1:proc_exit");
22 "environ_get" : () => {
23 console.log("wasi_snapshot_preview1:environ_get");
28 const memory = new WebAssembly.Memory({initial: 256});
29 imports.env.memoryBase = 0;
30 imports.env.memory = memory;
31 imports.env.tableBase = 0;
32 imports.env.table = new WebAssembly.Table({ initial: 4, element: 'anyfunc' });
34 imports.env["abort"] = function() {
35 console.error("ABORT");
37 imports.env["js_log"] = function(argument) {
39 const res = decodeUint8Array(argument, false);
42 imports.env["js_free"] = function(argument) {
43 console.log('integer passed from wasm:', argument);
45 imports.env["js_invoke_function"] = function(fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {
46 console.log('integer passed from wasm:', argument);
49 const stream = fetch('../../liblightningjs.wasm');
50 const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
51 const wasm = wasmInstance.exports;
54 const encodeUint8Array = (inputArray) => {
55 const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
56 const arrayLengthView = new Uint32Array(memory.buffer, cArrayPointer, 1);
57 arrayLengthView[0] = inputArray.length;
58 const arrayMemoryView = new Uint8Array(memory.buffer, cArrayPointer + 4, inputArray.length);
59 arrayMemoryView.set(inputArray);
63 const encodeUint32Array = (inputArray) => {
64 const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
65 const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length);
66 arrayMemoryView[0] = inputArray.length;
67 arrayMemoryView.set(inputArray, 1);
71 const getArrayLength = (arrayPointer) => {
72 const arraySizeViewer = new Uint32Array(
73 memory.buffer, // value
74 arrayPointer, // offset
77 return arraySizeViewer[0];
79 const decodeUint8Array = (arrayPointer, free = true) => {
80 const arraySize = getArrayLength(arrayPointer);
81 const actualArrayViewer = new Uint8Array(
82 memory.buffer, // value
83 arrayPointer + 4, // offset (ignoring length bytes)
84 arraySize // uint8 count
86 // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
87 // will free the underlying memory when it becomes unreachable instead of copying here.
88 const actualArray = actualArrayViewer.slice(0, arraySize);
90 wasm.TS_free(arrayPointer);
95 const result = wasm.TS_CResult_boolLightningErrorZ_ok(true);
96 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
97 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
98 wasm.TS_CResult_boolLightningErrorZ_free(result);
99 console.assert(wasm.TS_CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does
100 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
101 console.assert(!wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
102 wasm.TS_CResult_boolLightningErrorZ_free(result);
105 for (var i = 0; i < 33; i++) { pk_arr[i] = 42; }
106 const pk_bytes = encodeUint8Array(pk_arr);
107 const pk_res = wasm.TS_CResult_PublicKeySecpErrorZ_ok(pk_bytes);
108 console.assert(wasm.TS_LDKCResult_PublicKeySecpErrorZ_result_ok(pk_res));
109 const pk_res_bytes = wasm.TS_LDKCResult_PublicKeySecpErrorZ_get_ok(pk_res);
110 wasm.TS_LDKCResult_PublicKeySecpErrorZ_free(pk_res);