From 0f07ef8ebfcbb7f9077246eec08fd1435cdaee46 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 26 Apr 2025 01:35:36 +0530 Subject: Compile library to WASM using GraalVM. Add main functions for library. Convert to WASM image. Setup public for cljcc website. --- frontend/script.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 frontend/script.js (limited to 'frontend/script.js') diff --git a/frontend/script.js b/frontend/script.js new file mode 100644 index 0000000..6e95792 --- /dev/null +++ b/frontend/script.js @@ -0,0 +1,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; + }); +}; -- cgit v1.2.3