From 07fbf865fe5f4a8f6f352c5419bbccccc99eb336 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Sun, 19 Jul 2026 20:14:13 +0500 Subject: js init --- .gitignore | 1 + front/injection.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ script/Makefile | 2 ++ script/entry.c | 16 +++++++++++ 4 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 front/injection.js create mode 100644 script/Makefile create mode 100644 script/entry.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..811f477 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +mod.wasm diff --git a/front/injection.js b/front/injection.js new file mode 100644 index 0000000..ed0d1cd --- /dev/null +++ b/front/injection.js @@ -0,0 +1,78 @@ +const addr = "http://localhost:6969/ws"; + +let ws = new WebSocket(addr); +ws.binaryType = "arraybuffer"; + +let wasmModule; +let wasmInstance; +let isCompiling = false; + +ws.onmessage = async (message) => { + if (!wasmInstance) { + if (isCompiling) return; + isCompiling = true; + try { + const { instance, module } = await WebAssembly.instantiate(message.data); + wasmInstance = instance; + wasmModule = module; + ws.send(JSON.stringify({ + success: true, + message: "WASM module loaded successfully.", + })); + } catch (err) { + ws.send(JSON.stringify({ + success: false, + message: "WASM module loaded successfully.", + })); + } finally { + isCompiling = false; + } + return; + } + + try { + const exports = wasmInstance.exports; + const inputPtr = passArrayBufferToWasm(exports, message.data); + + const outputPtr = exports.entry(inputPtr); + const resultString = getStringFromWasm(exports.memory, outputPtr); + + ws.send(JSON.stringify({ + success: true, + message: resultString, + })); + + exports.deallocate(inputPtr); + exports.deallocate(outputPtr); + } catch (error) { + console.error(error); + ws.send(JSON.stringify({ + success: true, + message: `Failed to run wasm: ${error.message}` + })); + } +}; + +function passArrayBufferToWasm(exports, arrayBuffer) { + const srcBytes = new Uint8Array(arrayBuffer); + const len = srcBytes.length; + const ptr = exports.allocate(len + 1); + const heap = new Uint8Array(exports.memory.buffer); + + heap.set(srcBytes, ptr); + heap[ptr + len] = 0; + + return ptr; +} + +function getStringFromWasm(memory, ptr) { + const bytes = new Uint8Array(memory.buffer); + let endPtr = ptr; + + while (endPtr < bytes.length && bytes[endPtr] !== 0) { + endPtr++; + } + + return new TextDecoder("utf-8").decode(bytes.slice(ptr, endPtr)); +} + diff --git a/script/Makefile b/script/Makefile new file mode 100644 index 0000000..2e24e90 --- /dev/null +++ b/script/Makefile @@ -0,0 +1,2 @@ +all: + emcc entry.c -sEXPORTED_FUNCTIONS="_entry,_allocate,_deallocate" -o ../mod.wasm --no-entry diff --git a/script/entry.c b/script/entry.c new file mode 100644 index 0000000..a8f3b68 --- /dev/null +++ b/script/entry.c @@ -0,0 +1,16 @@ +#include +#include + +void* allocate(size_t size) { + return malloc(size); +} + +// Expose free so JS can clean up memory and prevent leaks +void deallocate(void* ptr) { + free(ptr); +} + + +const char *entry(const char *input) { + return strdup(input); +} -- cgit v1.3