11-- | This module represents the functional bindings to JavaScript's `DataView`
22-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details.
3-
43module Data.ArrayBuffer.DataView
5- ( AProxy (..)
6- , Endian (..)
4+ ( Endian (..)
75 , buffer
86 , byteLength
97 , byteOffset
@@ -46,20 +44,25 @@ module Data.ArrayBuffer.DataView
4644 , whole
4745 ) where
4846
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 )
5149import Data.Float32 (Float32 ) as F
5250import Data.Maybe (Maybe )
5351import Data.Nullable (Nullable , toMaybe )
5452import Data.Symbol (SProxy (..), class IsSymbol , reflectSymbol )
55- import Data.Typelevel.Num (toInt' , class Nat )
5653import Data.UInt (UInt )
5754import Effect (Effect )
5855import Effect.Uncurried (EffectFn2 , EffectFn3 , EffectFn4 , runEffectFn2 , runEffectFn3 , runEffectFn4 )
5956import Prelude (class Eq , (<$>), (<>), (==))
6057import Type.Proxy (Proxy (..))
6158
59+ -- | Endianness of a multi-byte type. Little-Endian or Big-Endian.
60+ data Endian = LE | BE
6261
62+ instance eqEndian :: Eq Endian where
63+ eq LE LE = true
64+ eq BE BE = true
65+ eq _ _ = false
6366
6467-- | View mapping the whole `ArrayBuffer`.
6568foreign import whole :: ArrayBuffer -> DataView
@@ -85,15 +88,6 @@ foreign import byteOffset :: DataView -> ByteOffset
8588foreign import byteLength :: DataView -> ByteLength
8689
8790
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-
9791
9892getter :: forall t .
9993 { functionName :: String
@@ -115,38 +109,35 @@ foreign import getterImpl :: forall t
115109
116110
117111
118- get :: forall a name t b
112+ get :: forall a name t
119113 . BinaryValue a t
120- => BytesPerValue a b
114+ => BytesPerType a
121115 => ShowArrayViewType a name
122116 => IsSymbol name
123- => Nat b
124- => Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
117+ => Endian -> Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
125118get endian prx =
126119 let le = endian == LE
127120 pnm = " get" <> reflectSymbol (SProxy :: SProxy name )
128- bpv = toInt' ( Proxy :: Proxy b )
121+ bpv = byteWidth prx
129122 in getter { functionName: pnm
130123 , bytesPerValue: bpv
131124 , littleEndian: le
132125 }
133126
134- getBE :: forall a name t b
127+ getBE :: forall a name t
135128 . BinaryValue a t
136- => BytesPerValue a b
129+ => BytesPerType a
137130 => ShowArrayViewType a name
138131 => IsSymbol name
139- => Nat b
140- => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
132+ => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
141133getBE = get BE
142134
143- getLE :: forall a name t b
135+ getLE :: forall a name t
144136 . BinaryValue a t
145- => BytesPerValue a b
137+ => BytesPerType a
146138 => ShowArrayViewType a name
147139 => IsSymbol name
148- => Nat b
149- => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t )
140+ => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t )
150141getLE = get LE
151142
152143setter :: forall t .
@@ -162,152 +153,149 @@ foreign import setterImpl :: forall t
162153 } DataView ByteOffset t Boolean
163154
164155
165- set :: forall a name t b
156+ set :: forall a name t
166157 . BinaryValue a t
167- => BytesPerValue a b
158+ => BytesPerType a
168159 => ShowArrayViewType a name
169160 => IsSymbol name
170- => Nat b
171- => Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
161+ => Endian -> Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
172162set endian prx =
173163 let le = endian == LE
174164 pnm = " set" <> reflectSymbol (SProxy :: SProxy name )
175- bpv = toInt' ( Proxy :: Proxy b )
165+ bpv = byteWidth prx
176166 in setter { functionName: pnm
177167 , bytesPerValue: bpv
178168 , littleEndian: le
179169 }
180170
181171-- | Fetch int8 value at a certain index in a `DataView`.
182172getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int )
183- getInt8 = getLE (AProxy :: AProxy Int8 )
173+ getInt8 = getLE (Proxy :: Proxy Int8 )
184174
185175-- | Fetch big-endian int16 value at a certain index in a `DataView`.
186176getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int )
187- getInt16be = getBE (AProxy :: AProxy Int16 )
177+ getInt16be = getBE (Proxy :: Proxy Int16 )
188178
189179-- | Fetch little-endian int16 value at a certain index in a `DataView`.
190180getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int )
191- getInt16le = getLE (AProxy :: AProxy Int16 )
181+ getInt16le = getLE (Proxy :: Proxy Int16 )
192182
193183-- | Fetch big-endian int32 value at a certain index in a `DataView`.
194184getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int )
195- getInt32be = getBE (AProxy :: AProxy Int32 )
185+ getInt32be = getBE (Proxy :: Proxy Int32 )
196186
197187-- | Fetch little-endian int32 value at a certain index in a `DataView`.
198188getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int )
199- getInt32le = getLE (AProxy :: AProxy Int32 )
189+ getInt32le = getLE (Proxy :: Proxy Int32 )
200190
201191-- | Fetch uint8 value at a certain index in a `DataView`.
202192getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt )
203- getUint8 = getLE (AProxy :: AProxy Uint8 )
193+ getUint8 = getLE (Proxy :: Proxy Uint8 )
204194
205195-- | Fetch big-endian uint16 value at a certain index in a `DataView`.
206196getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt )
207- getUint16be = getBE (AProxy :: AProxy Uint16 )
197+ getUint16be = getBE (Proxy :: Proxy Uint16 )
208198
209199-- | Fetch little-endian uint16 value at a certain index in a `DataView`.
210200getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt )
211- getUint16le = getLE (AProxy :: AProxy Uint16 )
201+ getUint16le = getLE (Proxy :: Proxy Uint16 )
212202
213203-- | Fetch big-endian uint32 value at a certain index in a `DataView`.
214204getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt )
215- getUint32be = getBE (AProxy :: AProxy Uint32 )
205+ getUint32be = getBE (Proxy :: Proxy Uint32 )
216206
217207-- | Fetch little-endian uint32 value at a certain index in a `DataView`.
218208getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt )
219- getUint32le = getLE (AProxy :: AProxy Uint32 )
209+ getUint32le = getLE (Proxy :: Proxy Uint32 )
220210
221211-- | Fetch big-endian float32 value at a certain index in a `DataView`.
222212getFloat32be :: DataView -> ByteOffset -> Effect (Maybe F.Float32 )
223- getFloat32be = getBE (AProxy :: AProxy Float32 )
213+ getFloat32be = getBE (Proxy :: Proxy Float32 )
224214
225215-- | Fetch little-endian float32 value at a certain index in a `DataView`.
226216getFloat32le :: DataView -> ByteOffset -> Effect (Maybe F.Float32 )
227- getFloat32le = getLE (AProxy :: AProxy Float32 )
217+ getFloat32le = getLE (Proxy :: Proxy Float32 )
228218
229219-- | Fetch big-endian float64 value at a certain index in a `DataView`.
230220getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number )
231- getFloat64be = getBE (AProxy :: AProxy Float64 )
221+ getFloat64be = getBE (Proxy :: Proxy Float64 )
232222
233223-- | Fetch little-endian float64 value at a certain index in a `DataView`.
234224getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number )
235- getFloat64le = getLE (AProxy :: AProxy Float64 )
225+ getFloat64le = getLE (Proxy :: Proxy Float64 )
236226
237227
238228-- | Store big-endian value at a certain index in a `DataView`.
239- setBE :: forall a name t b
229+ setBE :: forall a name t
240230 . BinaryValue a t
241- => BytesPerValue a b
231+ => BytesPerType a
242232 => ShowArrayViewType a name
243233 => IsSymbol name
244- => Nat b
245- => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
234+ => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
246235setBE = set BE
247236
248237-- | Store little-endian value at a certain index in a `DataView`.
249- setLE :: forall a name t b
238+ setLE :: forall a name t
250239 . BinaryValue a t
251- => BytesPerValue a b
240+ => BytesPerType a
252241 => ShowArrayViewType a name
253242 => IsSymbol name
254- => Nat b
255- => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean
243+ => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean
256244setLE = set LE
257245
258246-- | Store int8 value at a certain index in a `DataView`.
259247setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean
260- setInt8 = setLE (AProxy :: AProxy Int8 )
248+ setInt8 = setLE (Proxy :: Proxy Int8 )
261249
262250-- | Store big-endian int16 value at a certain index in a `DataView`.
263251setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean
264- setInt16be = setBE (AProxy :: AProxy Int16 )
252+ setInt16be = setBE (Proxy :: Proxy Int16 )
265253
266254-- | Store little-endian int16 value at a certain index in a `DataView`.
267255setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean
268- setInt16le = setLE (AProxy :: AProxy Int16 )
256+ setInt16le = setLE (Proxy :: Proxy Int16 )
269257
270258-- | Store big-endian int32 value at a certain index in a `DataView`.
271259setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean
272- setInt32be = setBE (AProxy :: AProxy Int32 )
260+ setInt32be = setBE (Proxy :: Proxy Int32 )
273261
274262-- | Store little-endian int32 value at a certain index in a `DataView`.
275263setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean
276- setInt32le = setLE (AProxy :: AProxy Int32 )
264+ setInt32le = setLE (Proxy :: Proxy Int32 )
277265
278266-- | Store uint8 value at a certain index in a `DataView`.
279267setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean
280- setUint8 = setLE (AProxy :: AProxy Uint8 )
268+ setUint8 = setLE (Proxy :: Proxy Uint8 )
281269
282270
283271-- | Store big-endian uint16 value at a certain index in a `DataView`.
284272setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean
285- setUint16be = setBE (AProxy :: AProxy Uint16 )
273+ setUint16be = setBE (Proxy :: Proxy Uint16 )
286274
287275-- | Store little-endian uint16 value at a certain index in a `DataView`.
288276setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean
289- setUint16le = setLE (AProxy :: AProxy Uint16 )
277+ setUint16le = setLE (Proxy :: Proxy Uint16 )
290278
291279-- | Store big-endian uint32 value at a certain index in a `DataView`.
292280setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean
293- setUint32be = setBE (AProxy :: AProxy Uint32 )
281+ setUint32be = setBE (Proxy :: Proxy Uint32 )
294282
295283-- | Store little-endian uint32 value at a certain index in a `DataView`.
296284setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean
297- setUint32le = setLE (AProxy :: AProxy Uint32 )
285+ setUint32le = setLE (Proxy :: Proxy Uint32 )
298286
299287-- | Store big-endian float32 value at a certain index in a `DataView`.
300288setFloat32be :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
301- setFloat32be = setBE (AProxy :: AProxy Float32 )
289+ setFloat32be = setBE (Proxy :: Proxy Float32 )
302290
303291-- | Store little-endian float32 value at a certain index in a `DataView`.
304292setFloat32le :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean
305- setFloat32le = setLE (AProxy :: AProxy Float32 )
293+ setFloat32le = setLE (Proxy :: Proxy Float32 )
306294
307295-- | Store big-endian float64 value at a certain index in a `DataView`.
308296setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean
309- setFloat64be = setBE (AProxy :: AProxy Float64 )
297+ setFloat64be = setBE (Proxy :: Proxy Float64 )
310298
311299-- | Store little-endian float64 value at a certain index in a `DataView`.
312300setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean
313- setFloat64le = setLE (AProxy :: AProxy Float64 )
301+ setFloat64le = setLE (Proxy :: Proxy Float64 )
0 commit comments