Skip to content

Commit addf664

Browse files
committed
preparing for major redesign of typed interface - supporting all
functions
1 parent a4c7cd0 commit addf664

File tree

2 files changed

+70
-76
lines changed

2 files changed

+70
-76
lines changed

src/Data/ArrayBuffer/Typed.js

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
11
"use strict";
22

3-
// module Data.ArrayBuffer.Typed
43

5-
exports.asInt8Array = function(v) {
6-
return new Int8Array(v.buffer, v.byteOffset, v.byteLength);
7-
}
4+
// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill
5+
var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
6+
Uint16Array, ​​​Int32Array, Uint32Array, ​​​Float32Array, Float64Array];
87

9-
exports.asInt16Array = function(v) {
10-
return new Int16Array(v.buffer, v.byteOffset, v.byteLength >>> 1);
11-
}
8+
for (var k in typedArrayTypes)
9+
for (var v in Array.prototype)
10+
if (Array.prototype.hasOwnProperty(v) &&
11+
!typedArrayTypes[k].prototype.hasOwnProperty(v))
12+
typedArrayTypes[k].prototype[v] = Array.prototype[v];
1213

13-
exports.asInt32Array = function(v) {
14-
return new Int32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
15-
}
1614

17-
exports.asUint8Array = function(v) {
18-
return new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
19-
}
15+
// module Data.ArrayBuffer.Typed
2016

21-
exports.asUint16Array = function(v) {
22-
return new Uint16Array(v.buffer, v.byteOffset, v.byteLength >>> 1);
23-
}
17+
exports.buffer = function buffer (v) {
18+
return v.buffer;
19+
};
2420

25-
exports.asUint32Array = function(v) {
26-
return new Uint32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
27-
}
21+
exports.byteOffset = function byteOffset (v) {
22+
return v.byteOffset;
23+
};
2824

29-
exports.asUint8ClampedArray = function(v) {
30-
return new Uint8ClampedArray(v.buffer, v.byteOffset, v.byteLength);
31-
}
25+
exports.byteLength = function byteLength (v) {
26+
return v.byteLength;
27+
};
3228

33-
exports.asFloat32Array = function(v) {
34-
return new Float32Array(v.buffer, v.byteOffset, v.byteLength >>> 2);
35-
}
29+
exports.lengthImpl = function lemgthImpl (v) {
30+
return v.length;
31+
};
3632

37-
exports.asFloat64Array = function(v) {
38-
return new Float64Array(v.buffer, v.byteOffset, v.byteLength >>> 3);
39-
}
40-
41-
exports.dataView = function(a) {
42-
return new DataView(a.buffer);
43-
}
4433

4534
exports.setImpl = function(ra, off, a) {
4635
return function() {
@@ -49,9 +38,7 @@ exports.setImpl = function(ra, off, a) {
4938
}
5039

5140
exports.unsafeAtImpl = function(a, i) {
52-
return function() {
53-
return a[i];
54-
};
41+
return a[i];
5542
}
5643

5744
exports.hasIndexImpl = function(a, i) {

src/Data/ArrayBuffer/Typed.purs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,75 @@
1-
module Data.ArrayBuffer.Typed( asInt8Array
2-
, asInt16Array
3-
, asInt32Array
4-
, asUint8Array
5-
, asUint16Array
6-
, asUint32Array
7-
, asUint8ClampedArray
8-
, asFloat32Array
9-
, asFloat64Array
10-
, dataView
11-
, set
12-
, unsafeAt
13-
, hasIndex
14-
, at
15-
, toArray
16-
, toIntArray
17-
) where
1+
module Data.ArrayBuffer.Typed
2+
( buffer
3+
, byteOffset
4+
, byteLength
5+
, class Bytes
6+
, bytesPer
7+
, length
8+
, set
9+
, unsafeAt
10+
, hasIndex
11+
, at
12+
, toArray
13+
, toIntArray
14+
) where
1815

1916
import Prelude
2017
import Effect (Effect)
21-
import Data.ArrayBuffer.Types (ArrayView, ByteOffset, DataView, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array)
18+
import Data.ArrayBuffer.Types
19+
( ArrayView, ByteOffset
20+
, Float64Array, Float32Array
21+
, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array
22+
, Float64, Float32
23+
, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8)
2224
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
2325
import Data.Maybe (Maybe(..))
26+
import Type.Proxy (Proxy(..))
2427

25-
-- | Create typed int8 array viewing the buffer mapped by the `DataView`
26-
foreign import asInt8Array :: DataView -> Int8Array
2728

28-
-- | Create typed int16 array viewing the buffer mapped by the `DataView`
29-
foreign import asInt16Array :: DataView -> Int16Array
29+
-- | `ArrayBuffer` being mapped by the typed array.
30+
foreign import buffer :: forall a. ArrayView a -> ArrayBuffer
3031

31-
-- | Create typed int32 array viewing the buffer mapped by the `DataView`
32-
foreign import asInt32Array :: DataView -> Int32Array
32+
-- | Represents the offset of this view from the start of its `ArrayBuffer`.
33+
foreign import byteOffset :: forall a. ArrayView a -> ByteOffset
3334

34-
-- | Create typed uint8 array viewing the buffer mapped by the `DataView`
35-
foreign import asUint8Array :: DataView -> Uint8Array
35+
-- | Represents the length of this typed array, in bytes.
36+
foreign import byteLength :: forall a. ArrayView a -> ByteLength
3637

37-
-- | Create typed uint16 array viewing the buffer mapped by the `DataView`
38-
foreign import asUint16Array :: DataView -> Uint16Array
38+
foreign import lengthImpl :: forall a. ArrayView a -> Int
3939

40-
-- | Create typed uint32 array viewing the buffer mapped by the `DataView`
41-
foreign import asUint32Array :: DataView -> Uint32Array
40+
class Bytes a where
41+
bytesPer :: Proxy a -> Int
4242

43-
-- | Create typed uint8 clamped array viewing the buffer mapped by the `DataView`
44-
foreign import asUint8ClampedArray :: DataView -> Uint8ClampedArray
43+
instance bytesUint8Clamped :: Bytes Uint8Clamped where
44+
bytesPer Proxy = 1
45+
instance bytesUint32 :: Bytes Uint32 where
46+
bytesPer Proxy = 4
47+
instance bytesUint16 :: Bytes Uint16 where
48+
bytesPer Proxy = 2
49+
instance bytesUint8 :: Bytes Uint8 where
50+
bytesPer Proxy = 1
51+
instance bytesInt32 :: Bytes Int32 where
52+
bytesPer Proxy = 4
53+
instance bytesInt16 :: Bytes Int16 where
54+
bytesPer Proxy = 2
55+
instance bytesInt8 :: Bytes Int8 where
56+
bytesPer Proxy = 1
4557

46-
-- | Create typed float32 array viewing the buffer mapped by the `DataView`
47-
foreign import asFloat32Array :: DataView -> Float32Array
58+
length :: forall a. Bytes a => ArrayView a -> Int
59+
length = lengthImpl
4860

49-
-- | Create typed float64 array viewing the buffer mapped by the `DataView`
50-
foreign import asFloat64Array :: DataView -> Float64Array
51-
52-
-- | Interpret typed array as a `DataView`.
53-
foreign import dataView :: forall a. ArrayView a -> DataView
5461

5562
foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit)
5663

5764
-- | Stores multiple values in the last typed array, reading input values from ther first typed array.
5865
set :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit
5966
set = runFn3 setImpl
6067

61-
foreign import unsafeAtImpl :: forall a. Fn2 (ArrayView a) Int (Effect Number)
68+
foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number
6269

6370
-- | Fetch element at index.
6471
unsafeAt :: forall a. ArrayView a -> Int -> Effect Number
65-
unsafeAt = runFn2 unsafeAtImpl
72+
unsafeAt = runEffectFn2 unsafeAtImpl
6673

6774
foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean
6875

0 commit comments

Comments
 (0)