Skip to content

Commit bae93c3

Browse files
authored
New module: Data.ArrayBuffer.Cast (#46)
* Upgrade packages.dhall * New module: Data.ArrayBuffer.Cast
1 parent 3b5ba30 commit bae93c3

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Breaking changes:
88

99
New features:
1010

11+
- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock)
12+
1113
Bugfixes:
1214

1315
Other improvements:

packages.dhall

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
let upstream =
2-
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
2+
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221102/packages.dhall
3+
sha256:8628e413718876ce26983db1d0ce9d9e1588129117fa3bb8ed9f618db6914127
34

45
in upstream

spago-test.dhall

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ in conf // {
55
, "foldable-traversable"
66
, "partial"
77
, "refs"
8-
, "typelevel-prelude"
98
, "tuples"
109
, "quickcheck"
1110
, "quickcheck-laws"

src/Data/ArrayBuffer/Cast.purs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- | `DataView` represents unaligned memory of unknown endianness.
2+
-- |
3+
-- | `ArrayView` represents arrays of aligned elements of
4+
-- | local-machine endianness.
5+
-- | For the cases of `Int8Array`, `Uint8Array`, `Uint8ClampedArray`,
6+
-- | the elements
7+
-- | are single bytes, so they are always aligned and they have no
8+
-- | endianness. Therefore in those cases we can freely cast back and forth
9+
-- | to `DataView`.
10+
module Data.ArrayBuffer.Cast
11+
( fromInt8Array
12+
, fromUint8Array
13+
, fromUint8ClampedArray
14+
, toInt8Array
15+
, toUint8Array
16+
, toUint8ClampedArray
17+
) where
18+
19+
import Data.ArrayBuffer.DataView as DV
20+
import Data.ArrayBuffer.Typed as AT
21+
import Data.ArrayBuffer.Types (DataView, Uint8Array, Uint8ClampedArray, Int8Array)
22+
import Effect (Effect)
23+
24+
-- | Cast an `Int8Array` to a `DataView`.
25+
fromInt8Array :: Int8Array -> Effect DataView
26+
fromInt8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x)
27+
28+
-- | Cast a `DataView` to an `Int8Array`.
29+
toInt8Array :: DataView -> Effect Int8Array
30+
toInt8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x)
31+
32+
-- | Cast a `UInt8Array` to a `DataView`.
33+
fromUint8Array :: Uint8Array -> Effect DataView
34+
fromUint8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x)
35+
36+
-- | Cast a `DataView` to a `Uint8Array`.
37+
toUint8Array :: DataView -> Effect Uint8Array
38+
toUint8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x)
39+
40+
-- | Cast a `UInt8ClampedArray` to a `DataView`.
41+
fromUint8ClampedArray :: Uint8ClampedArray -> Effect DataView
42+
fromUint8ClampedArray x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x)
43+
44+
-- | Cast a `DataView` to a `Uint8ClampedArray`.
45+
toUint8ClampedArray :: DataView -> Effect Uint8ClampedArray
46+
toUint8ClampedArray x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x)

src/Data/ArrayBuffer/ValueMapping.purs

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Data.Float32 (Float32) as F
1212
import Data.UInt (UInt)
1313
import Type.Proxy (Proxy)
1414

15-
-- | Map of each `ArrayViewType` to the number of bytes of storage it requires.
15+
-- | Type-level map of each `ArrayViewType` to the number of bytes of storage
16+
-- | it requires.
1617
class BytesPerType (a :: ArrayViewType) where
1718
byteWidth :: (Proxy a) -> Int
1819

@@ -43,7 +44,8 @@ instance bytesPerTypeFloat32 :: BytesPerType Float32 where
4344
instance bytesPerTypeFloat64 :: BytesPerType Float64 where
4445
byteWidth _ = 8
4546

46-
-- | Maps a `TypedArray`’s binary casted value to its computable representation in JavaScript.
47+
-- | Type-level map of `TypedArray`’s binary casted value to its
48+
-- | representation in JavaScript.
4749
class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t
4850

4951
instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt
@@ -56,6 +58,7 @@ instance binaryValueInt8 :: BinaryValue Int8 Int
5658
instance binaryValueFloat32 :: BinaryValue Float32 F.Float32
5759
instance binaryValueFloat64 :: BinaryValue Float64 Number
5860

61+
-- | Type-level map of `TypedArray` to its element type name.
5962
class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name
6063

6164
instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped"

0 commit comments

Comments
 (0)