|
1 |
| -module Data.ArrayBuffer.Typed( asInt8Array |
2 |
| - , asInt16Array |
3 |
| - , asInt32Array |
4 |
| - , asUint8Array |
5 |
| - , asUint16Array |
6 |
| - , asUint32Array |
7 |
| - , asUint8ClampedArray |
8 |
| - , asFloat32Array |
9 |
| - , asFloat64Array |
10 |
| - , dataView |
11 |
| - , set |
12 |
| - , unsafeAt |
13 |
| - , hasIndex |
14 |
| - , at |
15 |
| - , toArray |
16 |
| - , toIntArray |
17 |
| - ) where |
| 1 | +module Data.ArrayBuffer.Typed |
| 2 | + ( buffer |
| 3 | + , byteOffset |
| 4 | + , byteLength |
| 5 | + , class Bytes |
| 6 | + , bytesPer |
| 7 | + , length |
| 8 | + , set |
| 9 | + , unsafeAt |
| 10 | + , hasIndex |
| 11 | + , at |
| 12 | + , toArray |
| 13 | + , toIntArray |
| 14 | + ) where |
18 | 15 |
|
19 | 16 | import Prelude
|
20 | 17 | import Effect (Effect)
|
21 |
| -import Data.ArrayBuffer.Types (ArrayView, ByteOffset, DataView, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array) |
| 18 | +import Data.ArrayBuffer.Types |
| 19 | + ( ArrayView, ByteOffset |
| 20 | + , Float64Array, Float32Array |
| 21 | + , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array |
| 22 | + , Float64, Float32 |
| 23 | + , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) |
22 | 24 | import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
|
23 | 25 | import Data.Maybe (Maybe(..))
|
| 26 | +import Type.Proxy (Proxy(..)) |
24 | 27 |
|
25 |
| --- | Create typed int8 array viewing the buffer mapped by the `DataView` |
26 |
| -foreign import asInt8Array :: DataView -> Int8Array |
27 | 28 |
|
28 |
| --- | Create typed int16 array viewing the buffer mapped by the `DataView` |
29 |
| -foreign import asInt16Array :: DataView -> Int16Array |
| 29 | +-- | `ArrayBuffer` being mapped by the typed array. |
| 30 | +foreign import buffer :: forall a. ArrayView a -> ArrayBuffer |
30 | 31 |
|
31 |
| --- | Create typed int32 array viewing the buffer mapped by the `DataView` |
32 |
| -foreign import asInt32Array :: DataView -> Int32Array |
| 32 | +-- | Represents the offset of this view from the start of its `ArrayBuffer`. |
| 33 | +foreign import byteOffset :: forall a. ArrayView a -> ByteOffset |
33 | 34 |
|
34 |
| --- | Create typed uint8 array viewing the buffer mapped by the `DataView` |
35 |
| -foreign import asUint8Array :: DataView -> Uint8Array |
| 35 | +-- | Represents the length of this typed array, in bytes. |
| 36 | +foreign import byteLength :: forall a. ArrayView a -> ByteLength |
36 | 37 |
|
37 |
| --- | Create typed uint16 array viewing the buffer mapped by the `DataView` |
38 |
| -foreign import asUint16Array :: DataView -> Uint16Array |
| 38 | +foreign import lengthImpl :: forall a. ArrayView a -> Int |
39 | 39 |
|
40 |
| --- | Create typed uint32 array viewing the buffer mapped by the `DataView` |
41 |
| -foreign import asUint32Array :: DataView -> Uint32Array |
| 40 | +class Bytes a where |
| 41 | + bytesPer :: Proxy a -> Int |
42 | 42 |
|
43 |
| --- | Create typed uint8 clamped array viewing the buffer mapped by the `DataView` |
44 |
| -foreign import asUint8ClampedArray :: DataView -> Uint8ClampedArray |
| 43 | +instance bytesUint8Clamped :: Bytes Uint8Clamped where |
| 44 | + bytesPer Proxy = 1 |
| 45 | +instance bytesUint32 :: Bytes Uint32 where |
| 46 | + bytesPer Proxy = 4 |
| 47 | +instance bytesUint16 :: Bytes Uint16 where |
| 48 | + bytesPer Proxy = 2 |
| 49 | +instance bytesUint8 :: Bytes Uint8 where |
| 50 | + bytesPer Proxy = 1 |
| 51 | +instance bytesInt32 :: Bytes Int32 where |
| 52 | + bytesPer Proxy = 4 |
| 53 | +instance bytesInt16 :: Bytes Int16 where |
| 54 | + bytesPer Proxy = 2 |
| 55 | +instance bytesInt8 :: Bytes Int8 where |
| 56 | + bytesPer Proxy = 1 |
45 | 57 |
|
46 |
| --- | Create typed float32 array viewing the buffer mapped by the `DataView` |
47 |
| -foreign import asFloat32Array :: DataView -> Float32Array |
| 58 | +length :: forall a. Bytes a => ArrayView a -> Int |
| 59 | +length = lengthImpl |
48 | 60 |
|
49 |
| --- | Create typed float64 array viewing the buffer mapped by the `DataView` |
50 |
| -foreign import asFloat64Array :: DataView -> Float64Array |
51 |
| - |
52 |
| --- | Interpret typed array as a `DataView`. |
53 |
| -foreign import dataView :: forall a. ArrayView a -> DataView |
54 | 61 |
|
55 | 62 | foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit)
|
56 | 63 |
|
57 | 64 | -- | Stores multiple values in the last typed array, reading input values from ther first typed array.
|
58 | 65 | set :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit
|
59 | 66 | set = runFn3 setImpl
|
60 | 67 |
|
61 |
| -foreign import unsafeAtImpl :: forall a. Fn2 (ArrayView a) Int (Effect Number) |
| 68 | +foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number |
62 | 69 |
|
63 | 70 | -- | Fetch element at index.
|
64 | 71 | unsafeAt :: forall a. ArrayView a -> Int -> Effect Number
|
65 |
| -unsafeAt = runFn2 unsafeAtImpl |
| 72 | +unsafeAt = runEffectFn2 unsafeAtImpl |
66 | 73 |
|
67 | 74 | foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean
|
68 | 75 |
|
|
0 commit comments