mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
✨ Add several enhacements to buffer namespace
The changes are just for completenes.
This commit is contained in:
@@ -50,6 +50,13 @@
|
|||||||
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})]
|
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})]
|
||||||
`(long (.getInt ~target (unchecked-int ~offset))))))
|
`(long (.getInt ~target (unchecked-int ~offset))))))
|
||||||
|
|
||||||
|
(defmacro read-long
|
||||||
|
[target offset]
|
||||||
|
(if (:ns &env)
|
||||||
|
`(.getInt64 ~target ~offset true)
|
||||||
|
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})]
|
||||||
|
`(.getLong ~target (unchecked-int ~offset)))))
|
||||||
|
|
||||||
(defmacro read-float
|
(defmacro read-float
|
||||||
[target offset]
|
[target offset]
|
||||||
(if (:ns &env)
|
(if (:ns &env)
|
||||||
@@ -75,6 +82,40 @@
|
|||||||
(finally
|
(finally
|
||||||
(.order ~target ByteOrder/LITTLE_ENDIAN))))))
|
(.order ~target ByteOrder/LITTLE_ENDIAN))))))
|
||||||
|
|
||||||
|
(defmacro read-bytes
|
||||||
|
"Get a byte array from buffer. It is potentially unsafe because on
|
||||||
|
JS/CLJS it returns a subarray without doing any copy of data."
|
||||||
|
[target offset size]
|
||||||
|
(if (:ns &env)
|
||||||
|
`(new js/Uint8Array
|
||||||
|
(.-buffer ~target)
|
||||||
|
(+ (.-byteOffset ~target) ~offset)
|
||||||
|
~size)
|
||||||
|
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})
|
||||||
|
bbuf (with-meta (gensym "bbuf") {:tag bytes})]
|
||||||
|
`(let [~bbuf (byte-array ~size)]
|
||||||
|
(.get ~target
|
||||||
|
(unchecked-int ~offset)
|
||||||
|
~bbuf
|
||||||
|
0
|
||||||
|
~size)
|
||||||
|
~bbuf))))
|
||||||
|
|
||||||
|
;; FIXME: implement in cljs
|
||||||
|
(defmacro write-bytes
|
||||||
|
([target offset src size]
|
||||||
|
`(write-bytes ~target ~offset ~src 0 ~size))
|
||||||
|
([target offset src src-offset size]
|
||||||
|
(if (:ns &env)
|
||||||
|
(throw (ex-info "not implemented" {}))
|
||||||
|
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})
|
||||||
|
src (with-meta src {:tag 'bytes})]
|
||||||
|
`(.put ~target
|
||||||
|
(unchecked-int ~offset)
|
||||||
|
~src
|
||||||
|
(unchecked-int ~src-offset)
|
||||||
|
(unchecked-int ~size))))))
|
||||||
|
|
||||||
(defmacro write-byte
|
(defmacro write-byte
|
||||||
[target offset value]
|
[target offset value]
|
||||||
(if (:ns &env)
|
(if (:ns &env)
|
||||||
@@ -144,13 +185,15 @@
|
|||||||
(.setUint32 ~target (+ ~offset 12) (aget barray# 3) true))
|
(.setUint32 ~target (+ ~offset 12) (aget barray# 3) true))
|
||||||
|
|
||||||
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})
|
(let [target (with-meta target {:tag 'java.nio.ByteBuffer})
|
||||||
value (with-meta value {:tag 'java.util.UUID})]
|
value (with-meta value {:tag 'java.util.UUID})
|
||||||
`(try
|
prev (with-meta (gensym "prev-") {:tag 'java.nio.ByteOrder})]
|
||||||
(.order ~target ByteOrder/BIG_ENDIAN)
|
`(let [~prev (.order ~target)]
|
||||||
(.putLong ~target (unchecked-int (+ ~offset 0)) (.getMostSignificantBits ~value))
|
(try
|
||||||
(.putLong ~target (unchecked-int (+ ~offset 8)) (.getLeastSignificantBits ~value))
|
(.order ~target ByteOrder/BIG_ENDIAN)
|
||||||
(finally
|
(.putLong ~target (unchecked-int (+ ~offset 0)) (.getMostSignificantBits ~value))
|
||||||
(.order ~target ByteOrder/LITTLE_ENDIAN))))))
|
(.putLong ~target (unchecked-int (+ ~offset 8)) (.getLeastSignificantBits ~value))
|
||||||
|
(finally
|
||||||
|
(.order ~target ~prev)))))))
|
||||||
|
|
||||||
(defn wrap
|
(defn wrap
|
||||||
[data]
|
[data]
|
||||||
@@ -160,7 +203,7 @@
|
|||||||
|
|
||||||
(defn allocate
|
(defn allocate
|
||||||
[size]
|
[size]
|
||||||
#?(:clj (let [buffer (ByteBuffer/allocate (int size))]
|
#?(:clj (let [buffer (ByteBuffer/allocate (unchecked-int size))]
|
||||||
(.order buffer ByteOrder/LITTLE_ENDIAN))
|
(.order buffer ByteOrder/LITTLE_ENDIAN))
|
||||||
:cljs (new js/DataView (new js/ArrayBuffer size))))
|
:cljs (new js/DataView (new js/ArrayBuffer size))))
|
||||||
|
|
||||||
@@ -181,6 +224,14 @@
|
|||||||
(.set dst-view src-view)
|
(.set dst-view src-view)
|
||||||
(js/DataView. dst-buff))))
|
(js/DataView. dst-buff))))
|
||||||
|
|
||||||
|
;; FIXME: cljs impl
|
||||||
|
#?(:clj
|
||||||
|
(defn copy-bytes
|
||||||
|
[src src-offset size dst dst-offset]
|
||||||
|
(let [tmp (byte-array size)]
|
||||||
|
(.get ^ByteBuffer src src-offset tmp 0 size)
|
||||||
|
(.put ^ByteBuffer dst dst-offset tmp 0 size))))
|
||||||
|
|
||||||
(defn equals?
|
(defn equals?
|
||||||
[buffer-a buffer-b]
|
[buffer-a buffer-b]
|
||||||
#?(:clj
|
#?(:clj
|
||||||
@@ -208,3 +259,18 @@
|
|||||||
[o]
|
[o]
|
||||||
#?(:clj (instance? ByteBuffer o)
|
#?(:clj (instance? ByteBuffer o)
|
||||||
:cljs (instance? js/DataView o)))
|
:cljs (instance? js/DataView o)))
|
||||||
|
|
||||||
|
(defn slice
|
||||||
|
[buffer offset size]
|
||||||
|
#?(:cljs
|
||||||
|
(let [offset (+ (.-byteOffset buffer) offset)]
|
||||||
|
(new js/DataView (.-buffer buffer) offset size))
|
||||||
|
|
||||||
|
:clj
|
||||||
|
(-> (.slice ^ByteBuffer buffer (unchecked-int offset) (unchecked-int size))
|
||||||
|
(.order ByteOrder/LITTLE_ENDIAN))))
|
||||||
|
|
||||||
|
(defn size
|
||||||
|
[o]
|
||||||
|
#?(:cljs (.-byteLength ^js o)
|
||||||
|
:clj (.capacity ^ByteBuffer o)))
|
||||||
|
|||||||
Reference in New Issue
Block a user