From eb1bc480ca52e42e3f5723396730639cc6055c6f Mon Sep 17 00:00:00 2001 From: Aitor Date: Thu, 9 Mar 2023 15:01:09 +0100 Subject: [PATCH] wip --- .vscode/settings.json | 5 +++ frontend/resources/public/wasm/Makefile | 2 + frontend/resources/public/wasm/add.c | 19 ++++++++++ frontend/resources/public/wasm/add.js | 5 +++ frontend/resources/public/wasm/add.wasm | Bin 0 -> 458 bytes frontend/resources/public/wasm/int.h | 10 +++++ frontend/src/app/main.cljs | 2 + .../app/main/data/workspace/transforms.cljs | 1 + frontend/src/app/wasm.cljs | 35 ++++++++++++++++++ 9 files changed, 79 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 frontend/resources/public/wasm/Makefile create mode 100644 frontend/resources/public/wasm/add.c create mode 100644 frontend/resources/public/wasm/add.js create mode 100755 frontend/resources/public/wasm/add.wasm create mode 100644 frontend/resources/public/wasm/int.h create mode 100644 frontend/src/app/wasm.cljs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..43511cb38 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdint.h": "c" + } +} diff --git a/frontend/resources/public/wasm/Makefile b/frontend/resources/public/wasm/Makefile new file mode 100644 index 000000000..5cc6b69bd --- /dev/null +++ b/frontend/resources/public/wasm/Makefile @@ -0,0 +1,2 @@ +all: + clang -target wasm32 -Wl,--no-entry -Wl,--export-all -nostdlib -O3 add.c -o add.wasm diff --git a/frontend/resources/public/wasm/add.c b/frontend/resources/public/wasm/add.c new file mode 100644 index 000000000..db7a8a6cd --- /dev/null +++ b/frontend/resources/public/wasm/add.c @@ -0,0 +1,19 @@ +#include "int.h" + +#define MAX_OPERATIONS 2048 + +typedef struct _operations { + int32_t a, b, r; +} operations_t; + +operations_t operations[MAX_OPERATIONS]; + +int32_t add(int32_t a, int32_t b) { + return a + b; +} + +void compute() { + for (int32_t i = 0; i < MAX_OPERATIONS; i++) { + operations[i].r = add(operations[i].a, operations[i].b); + } +} diff --git a/frontend/resources/public/wasm/add.js b/frontend/resources/public/wasm/add.js new file mode 100644 index 000000000..a87d16e25 --- /dev/null +++ b/frontend/resources/public/wasm/add.js @@ -0,0 +1,5 @@ +function add(a, b) { + return a + b +} + +add(5, 5) diff --git a/frontend/resources/public/wasm/add.wasm b/frontend/resources/public/wasm/add.wasm new file mode 100755 index 0000000000000000000000000000000000000000..9fe76a24dac01556a09ee4b3ce273e521a5a56c3 GIT binary patch literal 458 zcmZvYu};G<6h+^&+oZ&;5KN4r6H6;Vq8k#K_yT6vajdo>iKEyN>XZx&9T*t;L5%$@ z+=fUUSgdn=ul=M4!sG}57?35vk~9rAbU*=NND+wlIBGCz4@1^K)Q-;11D?w%hvN-0 ze~-+|Rj#Wofh*2G+z2j3mT_ryWdNv15 + (p/let [response (js/fetch uri) + array-buffer (.arrayBuffer response) + assembly (.instantiate js/WebAssembly array-buffer)] + assembly) + (p/catch (fn [error] (prn "error: " error))))) + +(defn init! + "Initializes WebAssembly module" + [] + (p/then + (load-wasm "wasm/add.wasm") + (fn [assembly] + (let [operations (js/Int32Array. + assembly.instance.exports.memory.buffer ;; buffer we want to use + assembly.instance.exports.operations.value ;; offset of pointer 'operations' + (* 2048 12))] + (aset operations 0 2) + (aset operations 1 2) + (.set operations #js [4 5 -1] 3) + (js/console.time "compute") + (assembly.instance.exports.compute) + (js/console.timeEnd "compute") + (js/console.log assembly) + ) + (reset! instance assembly.instance))))