aboutsummaryrefslogtreecommitdiff
path: root/frontend/script.js
blob: 6e95792534be535b1e6706a3681f8f9ae3ca958f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Assuming your WASM file is named "mymodule.wasm"
async function loadWasmModule() {
    // Fetch the WASM file
    const wasmResponse = await fetch('../target/lib/cljcc-lib-wasm.js.wasm');
    const wasmBuffer = await wasmResponse.arrayBuffer();

    // Instantiate the WASM module
    const wasmResult = await WebAssembly.instantiate(wasmBuffer, {
        env: {
            // Any imported functions needed by your WASM module
            // For example, if your Java code uses console logging:
            printString: function(ptr, len) {
                // Implementation to handle string output
                const bytes = new Uint8Array(memory.buffer, ptr, len);
                const string = new TextDecoder('utf8').decode(bytes);
                console.log(string);
                document.getElementById('output').innerText += string + '\n';
            }
        }
    });

    // Store the instance and memory
    const instance = wasmResult.instance;
    const memory = instance.exports.memory;

    // Call the exported function that returns a string
    // For example, if your Java code exports a function called "getString"
    const stringPointer = instance.exports.getString();

    // Now we need to read the string from memory
    // This depends on how your GraalVM WASM image handles strings
    // Typically you would:
    // 1. Get the length of the string (via another export or a convention)
    const stringLength = instance.exports.getStringLength(); // If this is exported

    // 2. Convert the pointer and length to a JavaScript string
    const bytes = new Uint8Array(memory.buffer, stringPointer, stringLength);
    const result = new TextDecoder('utf8').decode(bytes);

    // 3. Display the result
    document.getElementById('output').innerText = result;

    return instance;
}

// Load the WASM module when the page loads
window.onload = function() {
    loadWasmModule().catch(err => {
        console.error("Failed to load WASM module:", err);
        document.getElementById('output').innerText = "Error: " + err.message;
    });
};