2
2
-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details.
3
3
4
4
module Data.ArrayBuffer.DataView
5
- ( whole
6
- , remainder
7
- , part
8
- , buffer
9
- , byteOffset
10
- , byteLength
11
- , AProxy (..)
12
- , class DataView
13
- , getBE , getLE , setBE , setLE
14
- ) where
15
-
16
- import Data.ArrayBuffer.Types (ByteOffset , DataView , ByteLength , ArrayBuffer , kind ArrayViewType , Int32 , Int16 , Int8 , Uint32 , Uint16 , Uint8 , Float32 , Float64 )
5
+ ( AProxy (..)
6
+ , Endian (..)
7
+ , buffer
8
+ , byteLength
9
+ , byteOffset
10
+ , class DataView
11
+ , class ViewTypeName
12
+ , get
13
+ , getBE
14
+ , getFloat32be
15
+ , getFloat32le
16
+ , getFloat64be
17
+ , getFloat64le
18
+ , getInt16be
19
+ , getInt16le
20
+ , getInt32be
21
+ , getInt32le
22
+ , getInt8
23
+ , getLE
24
+ , getUint16be
25
+ , getUint16le
26
+ , getUint32be
27
+ , getUint32le
28
+ , getUint8
29
+ , nm
30
+ , part
31
+ , remainder
32
+ , set
33
+ , setBE
34
+ , setFloat32be
35
+ , setFloat32le
36
+ , setFloat64be
37
+ , setFloat64le
38
+ , setInt16be
39
+ , setInt16le
40
+ , setInt32be
41
+ , setInt32le
42
+ , setInt8
43
+ , setLE
44
+ , setUint16be
45
+ , setUint16le
46
+ , setUint32be
47
+ , setUint32le
48
+ , setUint8
49
+ , whole
50
+ ) where
51
+
52
+ import Prelude
53
+
54
+ import Data.ArrayBuffer.Types (ArrayBuffer , ByteLength , ByteOffset , DataView , Float32 , Float64 , Int16 , Int32 , Int8 , Uint16 , Uint32 , Uint8 , Uint8Clamped , kind ArrayViewType )
17
55
import Data.ArrayBuffer.ValueMapping (class BinaryValue , class BytesPerValue )
18
56
import Data.Maybe (Maybe (..))
19
57
import Data.Typelevel.Num (toInt' , class Nat )
@@ -24,7 +62,6 @@ import Type.Proxy (Proxy(..))
24
62
25
63
26
64
27
-
28
65
-- | View mapping the whole `ArrayBuffer`.
29
66
foreign import whole :: ArrayBuffer -> DataView
30
67
@@ -52,13 +89,47 @@ foreign import byteLength :: DataView -> ByteLength
52
89
53
90
data AProxy (a :: ArrayViewType ) = AProxy
54
91
92
+ data Endian = LE | BE
93
+
94
+ instance eqEndian :: Eq Endian where
95
+ eq LE LE = true
96
+ eq BE BE = true
97
+ eq _ _ = false
98
+
99
+ class (BinaryValue a t , ViewTypeName a ) <= DataView (a :: ArrayViewType ) t | a -> t
100
+
101
+ instance dataViewUint8Clamped :: DataView Uint8Clamped UInt
102
+ instance dataViewUint32 :: DataView Uint32 UInt
103
+ instance dataViewUint16 :: DataView Uint16 UInt
104
+ instance dataViewUint8 :: DataView Uint8 UInt
105
+ instance dataViewInt32 :: DataView Int32 Int
106
+ instance dataViewInt16 :: DataView Int16 Int
107
+ instance dataViewInt8 :: DataView Int8 Int
108
+ instance dataViewFloat32 :: DataView Float32 Number
109
+ instance dataViewFloat64 :: DataView Float64 Number
55
110
56
- class BinaryValue a t <= DataView (a :: ArrayViewType ) t | a -> t where
57
- getLE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
58
- getBE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
59
- setBE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean
60
- setLE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean
61
111
112
+ class ViewTypeName vty where
113
+ nm :: AProxy vty -> String
114
+
115
+ instance viewTypeNameUint8Clamped :: ViewTypeName Uint8Clamped where
116
+ nm _ = " Uint8Clamped"
117
+ instance viewTypeNameViewUint32 :: ViewTypeName Uint32 where
118
+ nm _ = " Uint32"
119
+ instance viewTypeNameViewUint16 :: ViewTypeName Uint16 where
120
+ nm _ = " Uint16"
121
+ instance viewTypeNameViewUint8 :: ViewTypeName Uint8 where
122
+ nm _ = " Uint8"
123
+ instance viewTypeNameViewInt32 :: ViewTypeName Int32 where
124
+ nm _ = " Int32"
125
+ instance viewTypeNameViewInt16 :: ViewTypeName Int16 where
126
+ nm _ = " Int16"
127
+ instance viewTypeNameViewInt8 :: ViewTypeName Int8 where
128
+ nm _ = " Int8"
129
+ instance viewTypeNameViewFloat32 :: ViewTypeName Float32 where
130
+ nm _ = " Float32"
131
+ instance viewTypeNameViewFloat64 :: ViewTypeName Float64 where
132
+ nm _ = " Float64"
62
133
63
134
foreign import getterImpl :: forall t
64
135
. EffectFn3 { just :: t -> Maybe t
@@ -68,8 +139,8 @@ foreign import getterImpl :: forall t
68
139
, bytesPerValue :: ByteLength
69
140
} DataView ByteOffset (Maybe t )
70
141
71
- getter :: forall t
72
- . { functionName :: String
142
+ getter :: forall t .
143
+ { functionName :: String
73
144
, bytesPerValue :: ByteLength
74
145
, littleEndian :: Boolean
75
146
}
@@ -83,64 +154,164 @@ getter data' d o =
83
154
, bytesPerValue: data'.bytesPerValue
84
155
} d o
85
156
157
+
158
+ get :: forall a t b . DataView a t => BytesPerValue a b => Nat b => Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
159
+ get endian prx =
160
+ let le = endian == LE
161
+ pnm = " get" <> nm prx
162
+ bpv = toInt' (Proxy :: Proxy b )
163
+ in getter { functionName: pnm
164
+ , bytesPerValue: bpv
165
+ , littleEndian: le
166
+ }
167
+
168
+ getBE :: forall a t b . DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
169
+ getBE = get BE
170
+
171
+ getLE :: forall a t b . DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
172
+ getLE = get LE
173
+
86
174
foreign import setterImpl :: forall t
87
175
. EffectFn4 { functionName :: String
88
176
, littleEndian :: Boolean
89
177
, bytesPerValue :: ByteLength
90
- } DataView t ByteOffset Boolean
178
+ } DataView ByteOffset t Boolean
91
179
92
- setter :: forall t
93
- . { functionName :: String
180
+ setter :: forall t .
181
+ { functionName :: String
94
182
, bytesPerValue :: ByteLength
95
183
, littleEndian :: Boolean
96
- } -> DataView -> t -> ByteOffset -> Effect Boolean
97
- setter d t o = runEffectFn4 setterImpl d t o
98
-
99
-
100
- instance dataViewInt8 :: (BytesPerValue Int8 b , Nat b ) => DataView Int8 Int where
101
- getBE AProxy = getter {functionName: " getInt8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
102
- setBE AProxy = setter {functionName: " setInt8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
103
- getLE AProxy = getter {functionName: " getInt8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
104
- setLE AProxy = setter {functionName: " setInt8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
105
-
106
- instance dataViewInt16 :: (BytesPerValue Int16 b , Nat b ) => DataView Int16 Int where
107
- getBE AProxy = getter {functionName: " getInt16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
108
- setBE AProxy = setter {functionName: " setInt16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
109
- getLE AProxy = getter {functionName: " getInt16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
110
- setLE AProxy = setter {functionName: " setInt16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
111
-
112
- instance dataViewInt32 :: (BytesPerValue Int32 b , Nat b ) => DataView Int32 Int where
113
- getBE AProxy = getter {functionName: " getInt32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
114
- setBE AProxy = setter {functionName: " setInt32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
115
- getLE AProxy = getter {functionName: " getInt32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
116
- setLE AProxy = setter {functionName: " setInt32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
117
-
118
- instance dataViewUint8 :: (BytesPerValue Uint8 b , Nat b ) => DataView Uint8 UInt where
119
- getBE AProxy = getter {functionName: " getUint8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
120
- setBE AProxy = setter {functionName: " setUint8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
121
- getLE AProxy = getter {functionName: " getUint8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
122
- setLE AProxy = setter {functionName: " setUint8" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
123
-
124
- instance dataViewUint16 :: (BytesPerValue Uint16 b , Nat b ) => DataView Uint16 UInt where
125
- getBE AProxy = getter {functionName: " getUint16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
126
- setBE AProxy = setter {functionName: " setUint16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
127
- getLE AProxy = getter {functionName: " getUint16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
128
- setLE AProxy = setter {functionName: " setUint16" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
129
-
130
- instance dataViewUint32 :: (BytesPerValue Uint32 b , Nat b ) => DataView Uint32 UInt where
131
- getBE AProxy = getter {functionName: " getUint32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
132
- setBE AProxy = setter {functionName: " setUint32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
133
- getLE AProxy = getter {functionName: " getUint32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
134
- setLE AProxy = setter {functionName: " setUint32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
135
-
136
- instance dataViewFloat32 :: (BytesPerValue Float32 b , Nat b ) => DataView Float32 Number where
137
- getBE AProxy = getter {functionName: " getFloat32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
138
- setBE AProxy = setter {functionName: " setFloat32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
139
- getLE AProxy = getter {functionName: " getFloat32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
140
- setLE AProxy = setter {functionName: " setFloat32" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
141
-
142
- instance dataViewFloat64 :: (BytesPerValue Float64 b , Nat b ) => DataView Float64 Number where
143
- getBE AProxy = getter {functionName: " getFloat64" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
144
- setBE AProxy = setter {functionName: " setFloat64" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: false }
145
- getLE AProxy = getter {functionName: " getFloat64" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
146
- setLE AProxy = setter {functionName: " setFloat64" , bytesPerValue: toInt' (Proxy :: Proxy b ), littleEndian: true }
184
+ } -> DataView -> ByteOffset -> t -> Effect Boolean
185
+ setter d o t = runEffectFn4 setterImpl d o t
186
+
187
+ set :: forall a t b . DataView a t => BytesPerValue a b => Nat b => Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
188
+ set endian prx =
189
+ let le = endian == LE
190
+ pnm = " set" <> nm prx
191
+ bpv = toInt' (Proxy :: Proxy b )
192
+ in setter { functionName: pnm
193
+ , bytesPerValue: bpv
194
+ , littleEndian: le
195
+ }
196
+
197
+ -- | Fetch int8 value at a certain index in a `DataView`.
198
+ getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int )
199
+ getInt8 = getLE (AProxy :: AProxy Int8 )
200
+
201
+ -- | Fetch big-endian int16 value at a certain index in a `DataView`.
202
+ getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int )
203
+ getInt16be = getBE (AProxy :: AProxy Int16 )
204
+
205
+ -- | Fetch little-endian int16 value at a certain index in a `DataView`.
206
+ getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int )
207
+ getInt16le = getLE (AProxy :: AProxy Int16 )
208
+
209
+ -- | Fetch big-endian int32 value at a certain index in a `DataView`.
210
+ getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int )
211
+ getInt32be = getBE (AProxy :: AProxy Int32 )
212
+
213
+ -- | Fetch little-endian int32 value at a certain index in a `DataView`.
214
+ getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int )
215
+ getInt32le = getLE (AProxy :: AProxy Int32 )
216
+
217
+ -- | Fetch uint8 value at a certain index in a `DataView`.
218
+ getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt )
219
+ getUint8 = getLE (AProxy :: AProxy Uint8 )
220
+
221
+ -- | Fetch big-endian uint16 value at a certain index in a `DataView`.
222
+ getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt )
223
+ getUint16be = getBE (AProxy :: AProxy Uint16 )
224
+
225
+ -- | Fetch little-endian uint16 value at a certain index in a `DataView`.
226
+ getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt )
227
+ getUint16le = getLE (AProxy :: AProxy Uint16 )
228
+
229
+ -- | Fetch big-endian uint32 value at a certain index in a `DataView`.
230
+ getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt )
231
+ getUint32be = getBE (AProxy :: AProxy Uint32 )
232
+
233
+ -- | Fetch little-endian uint32 value at a certain index in a `DataView`.
234
+ getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt )
235
+ getUint32le = getLE (AProxy :: AProxy Uint32 )
236
+
237
+ -- | Fetch big-endian float32 value at a certain index in a `DataView`.
238
+ getFloat32be :: DataView -> ByteOffset -> Effect (Maybe Number )
239
+ getFloat32be = getBE (AProxy :: AProxy Float32 )
240
+
241
+ -- | Fetch little-endian float32 value at a certain index in a `DataView`.
242
+ getFloat32le :: DataView -> ByteOffset -> Effect (Maybe Number )
243
+ getFloat32le = getLE (AProxy :: AProxy Float32 )
244
+
245
+ -- | Fetch big-endian float64 value at a certain index in a `DataView`.
246
+ getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number )
247
+ getFloat64be = getBE (AProxy :: AProxy Float64 )
248
+
249
+ -- | Fetch little-endian float64 value at a certain index in a `DataView`.
250
+ getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number )
251
+ getFloat64le = getLE (AProxy :: AProxy Float64 )
252
+
253
+
254
+ -- | Store big-endian value at a certain index in a `DataView`.
255
+ setBE :: forall a t b . DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
256
+ setBE = set BE
257
+
258
+ -- | Store little-endian value at a certain index in a `DataView`.
259
+ setLE :: forall a t b . DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
260
+ setLE = set LE
261
+
262
+ -- | Store int8 value at a certain index in a `DataView`.
263
+ setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean
264
+ setInt8 = setLE (AProxy :: AProxy Int8 )
265
+
266
+ -- | Store big-endian int16 value at a certain index in a `DataView`.
267
+ setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean
268
+ setInt16be = setBE (AProxy :: AProxy Int16 )
269
+
270
+ -- | Store little-endian int16 value at a certain index in a `DataView`.
271
+ setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean
272
+ setInt16le = setLE (AProxy :: AProxy Int16 )
273
+
274
+ -- | Store big-endian int32 value at a certain index in a `DataView`.
275
+ setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean
276
+ setInt32be = setBE (AProxy :: AProxy Int32 )
277
+
278
+ -- | Store little-endian int32 value at a certain index in a `DataView`.
279
+ setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean
280
+ setInt32le = setLE (AProxy :: AProxy Int32 )
281
+
282
+ -- | Store uint8 value at a certain index in a `DataView`.
283
+ setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean
284
+ setUint8 = setLE (AProxy :: AProxy Uint8 )
285
+
286
+
287
+ -- | Store big-endian uint16 value at a certain index in a `DataView`.
288
+ setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean
289
+ setUint16be = setBE (AProxy :: AProxy Uint16 )
290
+
291
+ -- | Store little-endian uint16 value at a certain index in a `DataView`.
292
+ setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean
293
+ setUint16le = setLE (AProxy :: AProxy Uint16 )
294
+
295
+ -- | Store big-endian uint32 value at a certain index in a `DataView`.
296
+ setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean
297
+ setUint32be = setBE (AProxy :: AProxy Uint32 )
298
+
299
+ -- | Store little-endian uint32 value at a certain index in a `DataView`.
300
+ setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean
301
+ setUint32le = setLE (AProxy :: AProxy Uint32 )
302
+
303
+ -- | Store big-endian float32 value at a certain index in a `DataView`.
304
+ setFloat32be :: DataView -> ByteOffset -> Number -> Effect Boolean
305
+ setFloat32be = setBE (AProxy :: AProxy Float32 )
306
+
307
+ -- | Store little-endian float32 value at a certain index in a `DataView`.
308
+ setFloat32le :: DataView -> ByteOffset -> Number -> Effect Boolean
309
+ setFloat32le = setLE (AProxy :: AProxy Float32 )
310
+
311
+ -- | Store big-endian float64 value at a certain index in a `DataView`.
312
+ setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean
313
+ setFloat64be = setBE (AProxy :: AProxy Float64 )
314
+
315
+ -- | Store little-endian float64 value at a certain index in a `DataView`.
316
+ setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean
317
+ setFloat64le = setLE (AProxy :: AProxy Float64 )
0 commit comments