From df7ae0de142f520d47a252d56926bd042406be8a Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 28 Nov 2022 11:16:47 +0900 Subject: [PATCH 1/2] Upgrade packages.dhall --- packages.dhall | 3 ++- spago-test.dhall | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages.dhall b/packages.dhall index 582d6d3..2ffa9a7 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,4 +1,5 @@ let upstream = - https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall + https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221102/packages.dhall + sha256:8628e413718876ce26983db1d0ce9d9e1588129117fa3bb8ed9f618db6914127 in upstream diff --git a/spago-test.dhall b/spago-test.dhall index ab1a012..7037d90 100644 --- a/spago-test.dhall +++ b/spago-test.dhall @@ -5,7 +5,6 @@ in conf // { , "foldable-traversable" , "partial" , "refs" - , "typelevel-prelude" , "tuples" , "quickcheck" , "quickcheck-laws" From 94e031e31c980bb5f456d04fd544b15ff05225fa Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 28 Nov 2022 12:20:33 +0900 Subject: [PATCH 2/2] New module: Data.ArrayBuffer.Cast --- CHANGELOG.md | 2 ++ src/Data/ArrayBuffer/Cast.purs | 46 ++++++++++++++++++++++++++ src/Data/ArrayBuffer/ValueMapping.purs | 7 ++-- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/Data/ArrayBuffer/Cast.purs diff --git a/CHANGELOG.md b/CHANGELOG.md index ad44db1..7d83901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Breaking changes: New features: +- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock) + Bugfixes: Other improvements: diff --git a/src/Data/ArrayBuffer/Cast.purs b/src/Data/ArrayBuffer/Cast.purs new file mode 100644 index 0000000..10e5b0a --- /dev/null +++ b/src/Data/ArrayBuffer/Cast.purs @@ -0,0 +1,46 @@ +-- | `DataView` represents unaligned memory of unknown endianness. +-- | +-- | `ArrayView` represents arrays of aligned elements of +-- | local-machine endianness. +-- | For the cases of `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, +-- | the elements +-- | are single bytes, so they are always aligned and they have no +-- | endianness. Therefore in those cases we can freely cast back and forth +-- | to `DataView`. +module Data.ArrayBuffer.Cast + ( fromInt8Array + , fromUint8Array + , fromUint8ClampedArray + , toInt8Array + , toUint8Array + , toUint8ClampedArray + ) where + +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.Typed as AT +import Data.ArrayBuffer.Types (DataView, Uint8Array, Uint8ClampedArray, Int8Array) +import Effect (Effect) + +-- | Cast an `Int8Array` to a `DataView`. +fromInt8Array :: Int8Array -> Effect DataView +fromInt8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to an `Int8Array`. +toInt8Array :: DataView -> Effect Int8Array +toInt8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8Array` to a `DataView`. +fromUint8Array :: Uint8Array -> Effect DataView +fromUint8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8Array`. +toUint8Array :: DataView -> Effect Uint8Array +toUint8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8ClampedArray` to a `DataView`. +fromUint8ClampedArray :: Uint8ClampedArray -> Effect DataView +fromUint8ClampedArray x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8ClampedArray`. +toUint8ClampedArray :: DataView -> Effect Uint8ClampedArray +toUint8ClampedArray x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 70ac4f7..8694a19 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -12,7 +12,8 @@ import Data.Float32 (Float32) as F import Data.UInt (UInt) import Type.Proxy (Proxy) --- | Map of each `ArrayViewType` to the number of bytes of storage it requires. +-- | Type-level map of each `ArrayViewType` to the number of bytes of storage +-- | it requires. class BytesPerType (a :: ArrayViewType) where byteWidth :: (Proxy a) -> Int @@ -43,7 +44,8 @@ instance bytesPerTypeFloat32 :: BytesPerType Float32 where instance bytesPerTypeFloat64 :: BytesPerType Float64 where byteWidth _ = 8 --- | Maps a `TypedArray`’s binary casted value to its computable representation in JavaScript. +-- | Type-level map of `TypedArray`’s binary casted value to its +-- | representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt @@ -56,6 +58,7 @@ instance binaryValueInt8 :: BinaryValue Int8 Int instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number +-- | Type-level map of `TypedArray` to its element type name. class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped"