Skip to content

Commit a29346f

Browse files
committed
Typed array serialization/deserialization
1 parent 6e69bd9 commit a29346f

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

src/Data/ArrayBuffer/Deserializer.purs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module Data.ArrayBuffer.Deserializer where
22

3+
import Data.Function
4+
import Control.Monad.Eff
5+
import Data.ArrayBuffer.Advancer
36
import Data.ArrayBuffer.Types
7+
import qualified Data.ArrayBuffer.Typed as TA
48
import qualified Data.ArrayBuffer as AB
59
import qualified Data.ArrayBuffer.DataView as DV
6-
import Data.Function
7-
import Data.ArrayBuffer.Advancer
8-
import Control.Monad.Eff
910

1011
type Deserializer = Advancer
1112

@@ -29,6 +30,32 @@ instance isDeserializableFloat32 :: IsDeserializable Float32 where
2930
instance isDeserializableFloat64 :: IsDeserializable Float64 where
3031
get d = advance 8 d >>= DV.getFloat64 d.dv
3132

33+
getDataView :: forall e. Deserializer -> ByteLength -> Eff (reader :: DV.Reader | e) DV.DataView
34+
getDataView d n = do
35+
o <- advance n d
36+
return $ DV.slice o n (DV.buffer d.dv)
37+
38+
type AD t = forall e. Deserializer -> Number -> Eff (reader :: DV.Reader | e) t
39+
40+
getInt8Array :: AD TA.Int8Array
41+
getInt8Array d n = getDataView d n >>= return <<< TA.asInt8Array
42+
getInt16Array :: AD TA.Int16Array
43+
getInt16Array d n = getDataView d (n * 2) >>= return <<< TA.asInt16Array
44+
getInt32Array :: AD TA.Int32Array
45+
getInt32Array d n = getDataView d (n * 4) >>= return <<< TA.asInt32Array
46+
getUint8Array :: AD TA.Uint8Array
47+
getUint8Array d n = getDataView d n >>= return <<< TA.asUint8Array
48+
getUint16Array :: AD TA.Uint16Array
49+
getUint16Array d n = getDataView d (n * 2) >>= return <<< TA.asUint16Array
50+
getUint32Array :: AD TA.Uint32Array
51+
getUint32Array d n = getDataView d (n * 4) >>= return <<< TA.asUint32Array
52+
getUint8ClampedArray :: AD TA.Uint8ClampedArray
53+
getUint8ClampedArray d n = getDataView d n >>= return <<< TA.asUint8ClampedArray
54+
getFloat32Array :: AD TA.Float32Array
55+
getFloat32Array d n = getDataView d (n * 4) >>= return <<< TA.asFloat32Array
56+
getFloat64Array :: AD TA.Float64Array
57+
getFloat64Array d n = getDataView d (n * 8) >>= return <<< TA.asFloat64Array
58+
3259
deserializer :: forall e. AB.ArrayBuffer -> Eff (reader :: DV.Reader | e) Deserializer
3360
deserializer ab = return $ { dv : DV.whole ab, off : 0 }
3461

src/Data/ArrayBuffer/Serializer.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ instance isSerializableFloat32 :: IsSerializable Float32 where
2929
instance isSerializableFloat64 :: IsSerializable Float64 where
3030
put s v = advance 8 s >>= DV.setFloat64 s.dv v
3131

32-
32+
mapDataView :: forall e. Serializer -> ByteLength -> Eff (writer :: DV.Writer | e) DV.DataView
33+
mapDataView s n = do
34+
o <- advance n s
35+
return $ DV.slice o n (DV.buffer s.dv)
3336

3437
serializer :: forall e. ByteLength -> Eff (writer :: DV.Writer | e) Serializer
3538
serializer l = return $ { dv : DV.whole $ AB.create l, off : 0 }

src/Data/ArrayBuffer/Typed.purs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import Data.ArrayBuffer.Types
1010
foreign import data ArrayView :: * -> *
1111

1212
type Int8Array = ArrayView Int8
13+
type Int16Array = ArrayView Int16
14+
type Int32Array = ArrayView Int32
1315
type Uint8Array = ArrayView Uint8
14-
--type Uint8ClampedArray = ArrayView Uint8Clamped
15-
--type Int16Array = ArrayView Int16
16-
--type Uint16Array = ArrayView Uint16
17-
--type Int32Array = ArrayView Int32
18-
--type Uint32Array = ArrayView Uint32
19-
--type Float32Array = ArrayView Float32
20-
--type Float64Array = ArrayView Float64
16+
type Uint16Array = ArrayView Uint16
17+
type Uint32Array = ArrayView Uint32
18+
type Uint8ClampedArray = ArrayView Uint8Clamped
19+
type Float32Array = ArrayView Float32
20+
type Float64Array = ArrayView Float64
2121

2222
foreign import asInt8Array
2323
"""
@@ -26,13 +26,62 @@ function asInt8Array(v) {
2626
}
2727
""":: DataView -> Int8Array
2828

29+
foreign import asInt16Array
30+
"""
31+
function asInt16Array(v) {
32+
return new Int16Array(v.buffer, v.byteOffset, v.byteLength >>> 1);
33+
}
34+
""":: DataView -> Int16Array
35+
36+
foreign import asInt32Array
37+
"""
38+
function asInt32Array(v) {
39+
return new Int32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
40+
}
41+
""":: DataView -> Int32Array
42+
2943
foreign import asUint8Array
3044
"""
3145
function asUint8Array(v) {
3246
return new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
3347
}
3448
""":: DataView -> Uint8Array
3549

50+
foreign import asUint16Array
51+
"""
52+
function asUint16Array(v) {
53+
return new Uint16Array(v.buffer, v.byteOffset, v.byteLength >>> 1);
54+
}
55+
""":: DataView -> Uint16Array
56+
57+
foreign import asUint32Array
58+
"""
59+
function asUint32Array(v) {
60+
return new Uint32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
61+
}
62+
""":: DataView -> Uint32Array
63+
64+
foreign import asUint8ClampedArray
65+
"""
66+
function asUint8ClampedArray(v) {
67+
return new Uint8ClampedArray(v.buffer, v.byteOffset, v.byteLength);
68+
}
69+
""":: DataView -> Uint8ClampedArray
70+
71+
foreign import asFloat32Array
72+
"""
73+
function asFloat32Array(v) {
74+
return new Float32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
75+
}
76+
""":: DataView -> Float32Array
77+
78+
foreign import asFloat64Array
79+
"""
80+
function asFloat64Array(v) {
81+
return new Float64Array(v.buffer, v.byteOffset, v.byteLength >>> 3);
82+
}
83+
""":: DataView -> Float64Array
84+
3685
foreign import dataView
3786
"""
3887
function dataView(a) {
@@ -66,8 +115,9 @@ at a n = if a `hasIndex` n then
66115
foreign import toArray
67116
"""
68117
function toArray(a) {
69-
var ret = [];
70-
for (var i = 0, l = a.length; i < l; i++)
118+
var l = a.length;
119+
var ret = new Array(l);
120+
for (var i = 0; i < l; i++)
71121
ret[i] = a[i];
72122
return ret;
73123
}

0 commit comments

Comments
 (0)