aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
authorYour Name <agrawalshagun07@gmail.com>2025-04-26 01:35:36 +0530
committerYour Name <agrawalshagun07@gmail.com>2025-04-26 01:35:36 +0530
commit0f07ef8ebfcbb7f9077246eec08fd1435cdaee46 (patch)
treed4b943c77faab3a9c77b107341c02a9754830e4b /frontend
parent18ae507a42c1e45b6e552fe95c675d1e4db58737 (diff)
Compile library to WASM using GraalVM.
Add main functions for library. Convert to WASM image. Setup public for cljcc website.
Diffstat (limited to 'frontend')
-rw-r--r--frontend/index.html19
-rw-r--r--frontend/script.js52
2 files changed, 71 insertions, 0 deletions
diff --git a/frontend/index.html b/frontend/index.html
new file mode 100644
index 0000000..7499d08
--- /dev/null
+++ b/frontend/index.html
@@ -0,0 +1,19 @@
+<!doctype html>
+<html class="no-js" lang="">
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="x-ua-compatible" content="ie=edge">
+ <title>GraalVM test</title>
+ <meta name="description" content="">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+
+ <link rel="apple-touch-icon" href="/apple-touch-icon.png">
+
+ </head>
+ <body>
+
+
+ <div id="output"></div>
+ <script src="script.js"></script>
+ </body>
+</html>
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;
+ });
+};