Add test HTML file demonstrating linking and calling
[ldk-java] / ts / test / index.html
1 <!DOCTYPE html>
2 <!-- add.html -->
3 <html>
4 <head></head>
5 <body>
6 <script type="module">
7         (async() => {
8                 const imports = {};
9                 imports.wasi_snapshot_preview1 = {
10                         "fd_write" : () => {
11                                 console.log("ABORT");
12                         },
13                         "random_get" : () => {
14                                 console.log("ABORT");
15                         },
16                         "environ_sizes_get" : () => {
17                                 console.log("wasi_snapshot_preview1:environ_sizes_get");
18                         },
19                         "proc_exit" : () => {
20                                 console.log("wasi_snapshot_preview1:proc_exit");
21                         },
22                         "environ_get" : () => {
23                                 console.log("wasi_snapshot_preview1:environ_get");
24                         },
25                 };
26                 imports.env = {};
27
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' });
33
34                 imports.env["abort"] = function() {
35                         console.error("ABORT");
36                 };
37                 imports.env["js_free"] = function(argument) {
38                         console.log('integer passed from wasm:', argument);
39                 };
40                 imports.env["js_invoke_function"] = function(fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {
41                         console.log('integer passed from wasm:', argument);
42                 };
43
44                 const stream = fetch('../../liblightningjs.wasm');
45                 const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
46                 const wasm = wasmInstance.exports;
47
48                 const result = wasm.TS_CResult_boolLightningErrorZ_ok(true);
49                 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
50                 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
51                 wasm.TS_CResult_boolLightningErrorZ_free(result);
52                 console.assert(wasm.TS_CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does
53                 console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
54                 console.assert(!wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
55                 wasm.TS_CResult_boolLightningErrorZ_free(result);
56                 console.log("pass");
57         })();
58 </script>
59 </body>
60 </html>