1
1
-- | This module represents the functional bindings to JavaScript's `DataView`
2
2
-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details.
3
-
4
3
module Data.ArrayBuffer.DataView
5
- ( AProxy (..)
6
- , Endian (..)
4
+ ( Endian (..)
7
5
, buffer
8
6
, byteLength
9
7
, byteOffset
@@ -46,20 +44,25 @@ module Data.ArrayBuffer.DataView
46
44
, whole
47
45
) where
48
46
49
- import Data.ArrayBuffer.Types (ArrayBuffer , ByteLength , ByteOffset , DataView , Float32 , Float64 , Int16 , Int32 , Int8 , Uint16 , Uint32 , Uint8 , kind ArrayViewType )
50
- import Data.ArrayBuffer.ValueMapping (class BinaryValue , class BytesPerValue , class ShowArrayViewType )
47
+ import Data.ArrayBuffer.Types (ArrayBuffer , ByteLength , ByteOffset , DataView , Float32 , Float64 , Int16 , Int32 , Int8 , Uint16 , Uint32 , Uint8 )
48
+ import Data.ArrayBuffer.ValueMapping (class BinaryValue , class BytesPerType , class ShowArrayViewType , byteWidth )
51
49
import Data.Float32 (Float32 ) as F
52
50
import Data.Maybe (Maybe )
53
51
import Data.Nullable (Nullable , toMaybe )
54
52
import Data.Symbol (SProxy (..), class IsSymbol , reflectSymbol )
55
- import Data.Typelevel.Num (toInt' , class Nat )
56
53
import Data.UInt (UInt )
57
54
import Effect (Effect )
58
55
import Effect.Uncurried (EffectFn2 , EffectFn3 , EffectFn4 , runEffectFn2 , runEffectFn3 , runEffectFn4 )
59
56
import Prelude (class Eq , (<$>), (<>), (==))
60
57
import Type.Proxy (Proxy (..))
61
58
59
+ -- | Endianness of a multi-byte type. Little-Endian or Big-Endian.
60
+ data Endian = LE | BE
62
61
62
+ instance eqEndian :: Eq Endian where
63
+ eq LE LE = true
64
+ eq BE BE = true
65
+ eq _ _ = false
63
66
64
67
-- | View mapping the whole `ArrayBuffer`.
65
68
foreign import whole :: ArrayBuffer -> DataView
@@ -85,15 +88,6 @@ foreign import byteOffset :: DataView -> ByteOffset
85
88
foreign import byteLength :: DataView -> ByteLength
86
89
87
90
88
- data AProxy (a :: ArrayViewType ) = AProxy
89
-
90
- data Endian = LE | BE
91
-
92
- instance eqEndian :: Eq Endian where
93
- eq LE LE = true
94
- eq BE BE = true
95
- eq _ _ = false
96
-
97
91
98
92
getter :: forall t .
99
93
{ functionName :: String
@@ -115,38 +109,35 @@ foreign import getterImpl :: forall t
115
109
116
110
117
111
118
- get :: forall a name t b
112
+ get :: forall a name t
119
113
. BinaryValue a t
120
- => BytesPerValue a b
114
+ => BytesPerType a
121
115
=> ShowArrayViewType a name
122
116
=> IsSymbol name
123
- => Nat b
124
- => Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
117
+ => Endian -> Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
125
118
get endian prx =
126
119
let le = endian == LE
127
120
pnm = " get" <> reflectSymbol (SProxy :: SProxy name )
128
- bpv = toInt' ( Proxy :: Proxy b )
121
+ bpv = byteWidth prx
129
122
in getter { functionName: pnm
130
123
, bytesPerValue: bpv
131
124
, littleEndian: le
132
125
}
133
126
134
- getBE :: forall a name t b
127
+ getBE :: forall a name t
135
128
. BinaryValue a t
136
- => BytesPerValue a b
129
+ => BytesPerType a
137
130
=> ShowArrayViewType a name
138
131
=> IsSymbol name
139
- => Nat b
140
- => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
132
+ => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
141
133
getBE = get BE
142
134
143
- getLE :: forall a name t b
135
+ getLE :: forall a name t
144
136
. BinaryValue a t
145
- => BytesPerValue a b
137
+ => BytesPerType a
146
138
=> ShowArrayViewType a name
147
139
=> IsSymbol name
148
- => Nat b
149
- => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
140
+ => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
150
141
getLE = get LE
151
142
152
143
setter :: forall t .
@@ -162,152 +153,149 @@ foreign import setterImpl :: forall t
162
153
} DataView ByteOffset t Boolean
163
154
164
155
165
- set :: forall a name t b
156
+ set :: forall a name t
166
157
. BinaryValue a t
167
- => BytesPerValue a b
158
+ => BytesPerType a
168
159
=> ShowArrayViewType a name
169
160
=> IsSymbol name
170
- => Nat b
171
- => Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
161
+ => Endian -> Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
172
162
set endian prx =
173
163
let le = endian == LE
174
164
pnm = " set" <> reflectSymbol (SProxy :: SProxy name )
175
- bpv = toInt' ( Proxy :: Proxy b )
165
+ bpv = byteWidth prx
176
166
in setter { functionName: pnm
177
167
, bytesPerValue: bpv
178
168
, littleEndian: le
179
169
}
180
170
181
171
-- | Fetch int8 value at a certain index in a `DataView`.
182
172
getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int )
183
- getInt8 = getLE (AProxy :: AProxy Int8 )
173
+ getInt8 = getLE (Proxy :: Proxy Int8 )
184
174
185
175
-- | Fetch big-endian int16 value at a certain index in a `DataView`.
186
176
getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int )
187
- getInt16be = getBE (AProxy :: AProxy Int16 )
177
+ getInt16be = getBE (Proxy :: Proxy Int16 )
188
178
189
179
-- | Fetch little-endian int16 value at a certain index in a `DataView`.
190
180
getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int )
191
- getInt16le = getLE (AProxy :: AProxy Int16 )
181
+ getInt16le = getLE (Proxy :: Proxy Int16 )
192
182
193
183
-- | Fetch big-endian int32 value at a certain index in a `DataView`.
194
184
getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int )
195
- getInt32be = getBE (AProxy :: AProxy Int32 )
185
+ getInt32be = getBE (Proxy :: Proxy Int32 )
196
186
197
187
-- | Fetch little-endian int32 value at a certain index in a `DataView`.
198
188
getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int )
199
- getInt32le = getLE (AProxy :: AProxy Int32 )
189
+ getInt32le = getLE (Proxy :: Proxy Int32 )
200
190
201
191
-- | Fetch uint8 value at a certain index in a `DataView`.
202
192
getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt )
203
- getUint8 = getLE (AProxy :: AProxy Uint8 )
193
+ getUint8 = getLE (Proxy :: Proxy Uint8 )
204
194
205
195
-- | Fetch big-endian uint16 value at a certain index in a `DataView`.
206
196
getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt )
207
- getUint16be = getBE (AProxy :: AProxy Uint16 )
197
+ getUint16be = getBE (Proxy :: Proxy Uint16 )
208
198
209
199
-- | Fetch little-endian uint16 value at a certain index in a `DataView`.
210
200
getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt )
211
- getUint16le = getLE (AProxy :: AProxy Uint16 )
201
+ getUint16le = getLE (Proxy :: Proxy Uint16 )
212
202
213
203
-- | Fetch big-endian uint32 value at a certain index in a `DataView`.
214
204
getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt )
215
- getUint32be = getBE (AProxy :: AProxy Uint32 )
205
+ getUint32be = getBE (Proxy :: Proxy Uint32 )
216
206
217
207
-- | Fetch little-endian uint32 value at a certain index in a `DataView`.
218
208
getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt )
219
- getUint32le = getLE (AProxy :: AProxy Uint32 )
209
+ getUint32le = getLE (Proxy :: Proxy Uint32 )
220
210
221
211
-- | Fetch big-endian float32 value at a certain index in a `DataView`.
222
212
getFloat32be :: DataView -> ByteOffset -> Effect (Maybe F.Float32 )
223
- getFloat32be = getBE (AProxy :: AProxy Float32 )
213
+ getFloat32be = getBE (Proxy :: Proxy Float32 )
224
214
225
215
-- | Fetch little-endian float32 value at a certain index in a `DataView`.
226
216
getFloat32le :: DataView -> ByteOffset -> Effect (Maybe F.Float32 )
227
- getFloat32le = getLE (AProxy :: AProxy Float32 )
217
+ getFloat32le = getLE (Proxy :: Proxy Float32 )
228
218
229
219
-- | Fetch big-endian float64 value at a certain index in a `DataView`.
230
220
getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number )
231
- getFloat64be = getBE (AProxy :: AProxy Float64 )
221
+ getFloat64be = getBE (Proxy :: Proxy Float64 )
232
222
233
223
-- | Fetch little-endian float64 value at a certain index in a `DataView`.
234
224
getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number )
235
- getFloat64le = getLE (AProxy :: AProxy Float64 )
225
+ getFloat64le = getLE (Proxy :: Proxy Float64 )
236
226
237
227
238
228
-- | Store big-endian value at a certain index in a `DataView`.
239
- setBE :: forall a name t b
229
+ setBE :: forall a name t
240
230
. BinaryValue a t
241
- => BytesPerValue a b
231
+ => BytesPerType a
242
232
=> ShowArrayViewType a name
243
233
=> IsSymbol name
244
- => Nat b
245
- => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
234
+ => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
246
235
setBE = set BE
247
236
248
237
-- | Store little-endian value at a certain index in a `DataView`.
249
- setLE :: forall a name t b
238
+ setLE :: forall a name t
250
239
. BinaryValue a t
251
- => BytesPerValue a b
240
+ => BytesPerType a
252
241
=> ShowArrayViewType a name
253
242
=> IsSymbol name
254
- => Nat b
255
- => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
243
+ => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
256
244
setLE = set LE
257
245
258
246
-- | Store int8 value at a certain index in a `DataView`.
259
247
setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean
260
- setInt8 = setLE (AProxy :: AProxy Int8 )
248
+ setInt8 = setLE (Proxy :: Proxy Int8 )
261
249
262
250
-- | Store big-endian int16 value at a certain index in a `DataView`.
263
251
setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean
264
- setInt16be = setBE (AProxy :: AProxy Int16 )
252
+ setInt16be = setBE (Proxy :: Proxy Int16 )
265
253
266
254
-- | Store little-endian int16 value at a certain index in a `DataView`.
267
255
setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean
268
- setInt16le = setLE (AProxy :: AProxy Int16 )
256
+ setInt16le = setLE (Proxy :: Proxy Int16 )
269
257
270
258
-- | Store big-endian int32 value at a certain index in a `DataView`.
271
259
setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean
272
- setInt32be = setBE (AProxy :: AProxy Int32 )
260
+ setInt32be = setBE (Proxy :: Proxy Int32 )
273
261
274
262
-- | Store little-endian int32 value at a certain index in a `DataView`.
275
263
setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean
276
- setInt32le = setLE (AProxy :: AProxy Int32 )
264
+ setInt32le = setLE (Proxy :: Proxy Int32 )
277
265
278
266
-- | Store uint8 value at a certain index in a `DataView`.
279
267
setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean
280
- setUint8 = setLE (AProxy :: AProxy Uint8 )
268
+ setUint8 = setLE (Proxy :: Proxy Uint8 )
281
269
282
270
283
271
-- | Store big-endian uint16 value at a certain index in a `DataView`.
284
272
setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean
285
- setUint16be = setBE (AProxy :: AProxy Uint16 )
273
+ setUint16be = setBE (Proxy :: Proxy Uint16 )
286
274
287
275
-- | Store little-endian uint16 value at a certain index in a `DataView`.
288
276
setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean
289
- setUint16le = setLE (AProxy :: AProxy Uint16 )
277
+ setUint16le = setLE (Proxy :: Proxy Uint16 )
290
278
291
279
-- | Store big-endian uint32 value at a certain index in a `DataView`.
292
280
setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean
293
- setUint32be = setBE (AProxy :: AProxy Uint32 )
281
+ setUint32be = setBE (Proxy :: Proxy Uint32 )
294
282
295
283
-- | Store little-endian uint32 value at a certain index in a `DataView`.
296
284
setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean
297
- setUint32le = setLE (AProxy :: AProxy Uint32 )
285
+ setUint32le = setLE (Proxy :: Proxy Uint32 )
298
286
299
287
-- | Store big-endian float32 value at a certain index in a `DataView`.
300
288
setFloat32be :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
301
- setFloat32be = setBE (AProxy :: AProxy Float32 )
289
+ setFloat32be = setBE (Proxy :: Proxy Float32 )
302
290
303
291
-- | Store little-endian float32 value at a certain index in a `DataView`.
304
292
setFloat32le :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
305
- setFloat32le = setLE (AProxy :: AProxy Float32 )
293
+ setFloat32le = setLE (Proxy :: Proxy Float32 )
306
294
307
295
-- | Store big-endian float64 value at a certain index in a `DataView`.
308
296
setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean
309
- setFloat64be = setBE (AProxy :: AProxy Float64 )
297
+ setFloat64be = setBE (Proxy :: Proxy Float64 )
310
298
311
299
-- | Store little-endian float64 value at a certain index in a `DataView`.
312
300
setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean
313
- setFloat64le = setLE (AProxy :: AProxy Float64 )
301
+ setFloat64le = setLE (Proxy :: Proxy Float64 )
0 commit comments