Repository: penpot/penpot
File: frontend/src/app/render_wasm/api.cljs (ClojureScript)

Code before:
(defn set-shape-transform
  [transform]
  (h/call wasm/internal-module "_set_shape_transform"
          (dm/get-prop transform :a)
          (dm/get-prop transform :b)
          (dm/get-prop transform :c)
          (dm/get-prop transform :d)
          (dm/get-prop transform :e)
          (dm/get-prop transform :f)))

(defn set-shape-rotation
  [rotation]
  (h/call wasm/internal-module "_set_shape_rotation" rotation))

(defn set-shape-children
  [shape-ids]
  (let [num-shapes (count shape-ids)]
    (when (> num-shapes 0)
      (let [ptr (mem/alloc-bytes (* CHILD-ENTRY-SIZE num-shapes))
            heap (mem/get-heap-u32)]

        (loop [entries (seq shape-ids)
               offset  ptr]
          (when-not (empty? entries)
            (let [id (first entries)]
              (sr/uuid->heapu32 id heap (mem/ptr8->ptr32 offset))
              (recur (rest entries) (+ offset CHILD-ENTRY-SIZE)))))

        (h/call wasm/internal-module "_set_children")))))

(defn- get-string-length [string] (+ (count string) 1))

(defn- store-image
  [id]

  (let [buffer (uuid/get-u32 id)
        url    (cf/resolve-file-media {:id id})]
    (->> (http/send! {:method :get
                      :uri url
                      :response-type :blob})
         (rx/map :body)
         (rx/mapcat wapi/read-file-as-array-buffer)
         (rx/map (fn [image]
                   (let [image-size (.-byteLength image)
                         image-ptr  (mem/alloc-bytes image-size)
                         heap       (mem/get-heap-u8)
                         mem        (js/Uint8Array. (.-buffer heap) image-ptr image-size)]
                     (.set heap mem image-ptr)
                     (h/call wasm/internal-module "_store_image"
                             (aget buffer 0)
                             (aget buffer 1)

Review comment (suggestion):
Function names like `uuid->heapu32` induces to think they return heapu32.
I think this kind of functions should be called something like `write-uuid`.

Code after:
(defn set-shape-transform
  [transform]
  (h/call wasm/internal-module "_set_shape_transform"
          (dm/get-prop transform :a)
          (dm/get-prop transform :b)
          (dm/get-prop transform :c)
          (dm/get-prop transform :d)
          (dm/get-prop transform :e)
          (dm/get-prop transform :f)))

(defn set-shape-rotation
  [rotation]
  (h/call wasm/internal-module "_set_shape_rotation" rotation))

(defn set-shape-children
  [shape-ids]
  (let [num-shapes (count shape-ids)]
    (when (> num-shapes 0)
      (let [offset (mem/alloc-bytes (* CHILD-ENTRY-SIZE num-shapes))
            heap (mem/get-heap-u32)]

        (loop [entries (seq shape-ids)
               current-offset  offset]
          (when-not (empty? entries)
            (let [id (first entries)]
              (sr/heapu32-set-uuid id heap (mem/ptr8->ptr32 current-offset))
              (recur (rest entries) (+ current-offset CHILD-ENTRY-SIZE)))))

        (h/call wasm/internal-module "_set_children")))))

(defn- get-string-length [string] (+ (count string) 1))

(defn- store-image
  [id]

  (let [buffer (uuid/get-u32 id)
        url    (cf/resolve-file-media {:id id})]
    (->> (http/send! {:method :get
                      :uri url
                      :response-type :blob})
         (rx/map :body)
         (rx/mapcat wapi/read-file-as-array-buffer)
         (rx/map (fn [image]
                   (let [size (.-byteLength image)
                         offset  (mem/alloc-bytes size)
                         heap       (mem/get-heap-u8)
                         mem        (js/Uint8Array. (.-buffer heap) offset size)]
                     (.set heap mem offset)
                     (h/call wasm/internal-module "_store_image"
                             (aget buffer 0)
                             (aget buffer 1)