0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 14:12:36 -05:00
This commit is contained in:
Aitor 2023-03-09 15:01:09 +01:00
parent 94e87f8a7d
commit eb1bc480ca
9 changed files with 79 additions and 0 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"files.associations": {
"stdint.h": "c"
}
}

View file

@ -0,0 +1,2 @@
all:
clang -target wasm32 -Wl,--no-entry -Wl,--export-all -nostdlib -O3 add.c -o add.wasm

View file

@ -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);
}
}

View file

@ -0,0 +1,5 @@
function add(a, b) {
return a + b
}
add(5, 5)

Binary file not shown.

View file

@ -0,0 +1,10 @@
#pragma once
typedef __INT8_TYPE__ int8_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __INT16_TYPE__ int16_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __INT32_TYPE__ int32_t;
typedef __UINT32_TYPE__ uint32_t;

View file

@ -24,6 +24,7 @@
[app.util.dom :as dom]
[app.util.i18n :as i18n]
[app.util.theme :as theme]
[app.wasm :as wasm]
[beicon.core :as rx]
[debug]
[features]
@ -73,6 +74,7 @@
(defn ^:export init
[]
(wasm/init!)
(worker/init!)
(i18n/init! cf/translations)
(theme/init! cf/themes)

View file

@ -199,6 +199,7 @@
(normalize-proportion-lock [[point shift? alt?]]
(let [proportion-lock? (:proportion-lock shape)]
[point (or proportion-lock? shift?) alt?]))]
(reify
ptk/UpdateEvent
(update [_ state]

View file

@ -0,0 +1,35 @@
(ns app.wasm
(:require
[promesa.core :as p]))
(defonce instance (atom nil))
(defn load-wasm
"Loads a WebAssembly module"
[uri]
(->
(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))))