forked from purescript-contrib/purescript-arraybuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataView.purs
More file actions
313 lines (256 loc) · 10.8 KB
/
DataView.purs
File metadata and controls
313 lines (256 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
-- | This module represents the functional bindings to JavaScript's `DataView`
-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details.
module Data.ArrayBuffer.DataView
( AProxy (..)
, Endian (..)
, buffer
, byteLength
, byteOffset
, get
, getBE
, getFloat32be
, getFloat32le
, getFloat64be
, getFloat64le
, getInt16be
, getInt16le
, getInt32be
, getInt32le
, getInt8
, getLE
, getUint16be
, getUint16le
, getUint32be
, getUint32le
, getUint8
, part
, remainder
, set
, setBE
, setFloat32be
, setFloat32le
, setFloat64be
, setFloat64le
, setInt16be
, setInt16le
, setInt32be
, setInt32le
, setInt8
, setLE
, setUint16be
, setUint16le
, setUint32be
, setUint32le
, setUint8
, whole
) where
import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, kind ArrayViewType)
import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue, class ShowArrayViewType)
import Data.Float32 (Float32) as F
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Data.Symbol (SProxy(..), class IsSymbol, reflectSymbol)
import Data.Typelevel.Num (toInt', class Nat)
import Data.UInt (UInt)
import Effect (Effect)
import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4)
import Prelude (class Eq, (<$>), (<>), (==))
import Type.Proxy (Proxy(..))
-- | View mapping the whole `ArrayBuffer`.
foreign import whole :: ArrayBuffer -> DataView
-- | View mapping the rest of an `ArrayBuffer` after an index.
remainder :: ArrayBuffer -> ByteOffset -> Effect DataView
remainder a o = runEffectFn2 remainderImpl a o
foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView
-- | View mapping a region of the `ArrayBuffer`.
part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView
part a o l = runEffectFn3 partImpl a o l
foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView
-- | `ArrayBuffer` being mapped by the view.
foreign import buffer :: DataView -> ArrayBuffer
-- | Represents the offset of this view from the start of its `ArrayBuffer`.
foreign import byteOffset :: DataView -> ByteOffset
-- | Represents the length of this view.
foreign import byteLength :: DataView -> ByteLength
data AProxy (a :: ArrayViewType) = AProxy
data Endian = LE | BE
instance eqEndian :: Eq Endian where
eq LE LE = true
eq BE BE = true
eq _ _ = false
getter :: forall t.
{ functionName :: String
, bytesPerValue :: ByteLength
, littleEndian :: Boolean
}
-> DataView -> ByteOffset -> Effect (Maybe t)
getter data' d o = toMaybe <$>
runEffectFn3 getterImpl
{ functionName: data'.functionName
, littleEndian: data'.littleEndian
, bytesPerValue: data'.bytesPerValue
} d o
foreign import getterImpl :: forall t
. EffectFn3 { functionName :: String
, littleEndian :: Boolean
, bytesPerValue :: ByteLength
} DataView ByteOffset (Nullable t)
get :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t)
get endian prx =
let le = endian == LE
pnm = "get" <> reflectSymbol (SProxy :: SProxy name)
bpv = toInt' (Proxy :: Proxy b)
in getter { functionName: pnm
, bytesPerValue: bpv
, littleEndian: le
}
getBE :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t)
getBE = get BE
getLE :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t)
getLE = get LE
setter :: forall t.
{ functionName :: String
, bytesPerValue :: ByteLength
, littleEndian :: Boolean
} -> DataView -> ByteOffset -> t -> Effect Boolean
setter d o t = runEffectFn4 setterImpl d o t
foreign import setterImpl :: forall t
. EffectFn4 { functionName :: String
, littleEndian :: Boolean
, bytesPerValue :: ByteLength
} DataView ByteOffset t Boolean
set :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
set endian prx =
let le = endian == LE
pnm = "set" <> reflectSymbol (SProxy :: SProxy name)
bpv = toInt' (Proxy :: Proxy b)
in setter { functionName: pnm
, bytesPerValue: bpv
, littleEndian: le
}
-- | Fetch int8 value at a certain index in a `DataView`.
getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int)
getInt8 = getLE (AProxy :: AProxy Int8)
-- | Fetch big-endian int16 value at a certain index in a `DataView`.
getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int)
getInt16be = getBE (AProxy :: AProxy Int16)
-- | Fetch little-endian int16 value at a certain index in a `DataView`.
getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int)
getInt16le = getLE (AProxy :: AProxy Int16)
-- | Fetch big-endian int32 value at a certain index in a `DataView`.
getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int)
getInt32be = getBE (AProxy :: AProxy Int32)
-- | Fetch little-endian int32 value at a certain index in a `DataView`.
getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int)
getInt32le = getLE (AProxy :: AProxy Int32)
-- | Fetch uint8 value at a certain index in a `DataView`.
getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt)
getUint8 = getLE (AProxy :: AProxy Uint8)
-- | Fetch big-endian uint16 value at a certain index in a `DataView`.
getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt)
getUint16be = getBE (AProxy :: AProxy Uint16)
-- | Fetch little-endian uint16 value at a certain index in a `DataView`.
getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt)
getUint16le = getLE (AProxy :: AProxy Uint16)
-- | Fetch big-endian uint32 value at a certain index in a `DataView`.
getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt)
getUint32be = getBE (AProxy :: AProxy Uint32)
-- | Fetch little-endian uint32 value at a certain index in a `DataView`.
getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt)
getUint32le = getLE (AProxy :: AProxy Uint32)
-- | Fetch big-endian float32 value at a certain index in a `DataView`.
getFloat32be :: DataView -> ByteOffset -> Effect (Maybe F.Float32)
getFloat32be = getBE (AProxy :: AProxy Float32)
-- | Fetch little-endian float32 value at a certain index in a `DataView`.
getFloat32le :: DataView -> ByteOffset -> Effect (Maybe F.Float32)
getFloat32le = getLE (AProxy :: AProxy Float32)
-- | Fetch big-endian float64 value at a certain index in a `DataView`.
getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number)
getFloat64be = getBE (AProxy :: AProxy Float64)
-- | Fetch little-endian float64 value at a certain index in a `DataView`.
getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number)
getFloat64le = getLE (AProxy :: AProxy Float64)
-- | Store big-endian value at a certain index in a `DataView`.
setBE :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
setBE = set BE
-- | Store little-endian value at a certain index in a `DataView`.
setLE :: forall a name t b
. BinaryValue a t
=> BytesPerValue a b
=> ShowArrayViewType a name
=> IsSymbol name
=> Nat b
=> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
setLE = set LE
-- | Store int8 value at a certain index in a `DataView`.
setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean
setInt8 = setLE (AProxy :: AProxy Int8)
-- | Store big-endian int16 value at a certain index in a `DataView`.
setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean
setInt16be = setBE (AProxy :: AProxy Int16)
-- | Store little-endian int16 value at a certain index in a `DataView`.
setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean
setInt16le = setLE (AProxy :: AProxy Int16)
-- | Store big-endian int32 value at a certain index in a `DataView`.
setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean
setInt32be = setBE (AProxy :: AProxy Int32)
-- | Store little-endian int32 value at a certain index in a `DataView`.
setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean
setInt32le = setLE (AProxy :: AProxy Int32)
-- | Store uint8 value at a certain index in a `DataView`.
setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean
setUint8 = setLE (AProxy :: AProxy Uint8)
-- | Store big-endian uint16 value at a certain index in a `DataView`.
setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean
setUint16be = setBE (AProxy :: AProxy Uint16)
-- | Store little-endian uint16 value at a certain index in a `DataView`.
setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean
setUint16le = setLE (AProxy :: AProxy Uint16)
-- | Store big-endian uint32 value at a certain index in a `DataView`.
setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean
setUint32be = setBE (AProxy :: AProxy Uint32)
-- | Store little-endian uint32 value at a certain index in a `DataView`.
setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean
setUint32le = setLE (AProxy :: AProxy Uint32)
-- | Store big-endian float32 value at a certain index in a `DataView`.
setFloat32be :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
setFloat32be = setBE (AProxy :: AProxy Float32)
-- | Store little-endian float32 value at a certain index in a `DataView`.
setFloat32le :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
setFloat32le = setLE (AProxy :: AProxy Float32)
-- | Store big-endian float64 value at a certain index in a `DataView`.
setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean
setFloat64be = setBE (AProxy :: AProxy Float64)
-- | Store little-endian float64 value at a certain index in a `DataView`.
setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean
setFloat64le = setLE (AProxy :: AProxy Float64)