Skip to content

New module: Data.ArrayBuffer.Cast #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Breaking changes:

New features:

- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock)

Bugfixes:

Other improvements:
Expand Down
3 changes: 2 additions & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion spago-test.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ in conf // {
, "foldable-traversable"
, "partial"
, "refs"
, "typelevel-prelude"
, "tuples"
, "quickcheck"
, "quickcheck-laws"
Expand Down
46 changes: 46 additions & 0 deletions src/Data/ArrayBuffer/Cast.purs
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 5 additions & 2 deletions src/Data/ArrayBuffer/ValueMapping.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down