module JavaScript.TypedArray.ArrayBuffer
( ArrayBuffer
, MutableArrayBuffer
, freeze, unsafeFreeze
, thaw, unsafeThaw
, byteLengthIO
) where
import Control.Lens.Operators ((^.))
import GHCJS.Marshal (fromJSValUnchecked)
import Language.Javascript.JSaddle.Types (JSM)
import Language.Javascript.JSaddle.Object (jsg, new, js, js1, js2)
import JavaScript.TypedArray.ArrayBuffer.Internal
create :: Int -> JSM MutableArrayBuffer
create :: Int -> JSM MutableArrayBuffer
create Int
n = JSVal -> MutableArrayBuffer
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer (JSVal -> MutableArrayBuffer)
-> JSM JSVal -> JSM MutableArrayBuffer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSM JSVal -> [Int] -> JSM JSVal
forall constructor args.
(MakeObject constructor, MakeArgs args) =>
constructor -> args -> JSM JSVal
new (String -> JSM JSVal
forall a. ToJSString a => a -> JSM JSVal
jsg String
"ArrayBuffer") [Int
n]
{-# INLINE create #-}
freeze :: MutableArrayBuffer -> JSM ArrayBuffer
freeze :: MutableArrayBuffer -> JSM ArrayBuffer
freeze (SomeArrayBuffer JSVal
b) = JSVal -> ArrayBuffer
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer (JSVal -> ArrayBuffer) -> JSM JSVal -> JSM ArrayBuffer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal
b JSVal -> Getting (JSM JSVal) JSVal (JSM JSVal) -> JSM JSVal
forall s a. s -> Getting a s a -> a
^. String -> Int -> JSF
forall name a0. (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
js1 String
"slice" (Int
0 :: Int)
{-# INLINE freeze #-}
unsafeFreeze :: MutableArrayBuffer -> JSM ArrayBuffer
unsafeFreeze :: MutableArrayBuffer -> JSM ArrayBuffer
unsafeFreeze (SomeArrayBuffer JSVal
b) = ArrayBuffer -> JSM ArrayBuffer
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JSVal -> ArrayBuffer
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer JSVal
b)
{-# INLINE unsafeFreeze #-}
thaw :: ArrayBuffer -> JSM MutableArrayBuffer
thaw :: ArrayBuffer -> JSM MutableArrayBuffer
thaw (SomeArrayBuffer JSVal
b) = JSVal -> MutableArrayBuffer
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer (JSVal -> MutableArrayBuffer)
-> JSM JSVal -> JSM MutableArrayBuffer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal
b JSVal -> Getting (JSM JSVal) JSVal (JSM JSVal) -> JSM JSVal
forall s a. s -> Getting a s a -> a
^. String -> Int -> JSF
forall name a0. (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
js1 String
"slice" (Int
0 :: Int)
{-# INLINE thaw #-}
unsafeThaw :: ArrayBuffer -> JSM MutableArrayBuffer
unsafeThaw :: ArrayBuffer -> JSM MutableArrayBuffer
unsafeThaw (SomeArrayBuffer JSVal
b) = MutableArrayBuffer -> JSM MutableArrayBuffer
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JSVal -> MutableArrayBuffer
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer JSVal
b)
{-# INLINE unsafeThaw #-}
sliceIO :: Int -> Maybe Int -> SomeArrayBuffer any -> JSM (SomeArrayBuffer any)
sliceIO :: forall (any :: MutabilityType (*)).
Int
-> Maybe Int -> SomeArrayBuffer any -> JSM (SomeArrayBuffer any)
sliceIO Int
begin (Just Int
end) (SomeArrayBuffer JSVal
b) = JSVal -> SomeArrayBuffer any
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer (JSVal -> SomeArrayBuffer any)
-> JSM JSVal -> JSM (SomeArrayBuffer any)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal
b JSVal -> Getting (JSM JSVal) JSVal (JSM JSVal) -> JSM JSVal
forall s a. s -> Getting a s a -> a
^. String -> Int -> Int -> JSF
forall name a0 a1.
(ToJSString name, ToJSVal a0, ToJSVal a1) =>
name -> a0 -> a1 -> JSF
js2 String
"slice" Int
begin Int
end
sliceIO Int
begin Maybe Int
_ (SomeArrayBuffer JSVal
b) = JSVal -> SomeArrayBuffer any
forall s (a :: MutabilityType s). JSVal -> SomeArrayBuffer a
SomeArrayBuffer (JSVal -> SomeArrayBuffer any)
-> JSM JSVal -> JSM (SomeArrayBuffer any)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal
b JSVal -> Getting (JSM JSVal) JSVal (JSM JSVal) -> JSM JSVal
forall s a. s -> Getting a s a -> a
^. String -> Int -> JSF
forall name a0. (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
js1 String
"slice" Int
begin
{-# INLINE sliceIO #-}
byteLengthIO :: SomeArrayBuffer any -> JSM Int
byteLengthIO :: forall (any :: MutabilityType (*)). SomeArrayBuffer any -> JSM Int
byteLengthIO (SomeArrayBuffer JSVal
b) = JSVal
b JSVal -> Getting (JSM JSVal) JSVal (JSM JSVal) -> JSM JSVal
forall s a. s -> Getting a s a -> a
^. String -> IndexPreservingGetter JSVal (JSM JSVal)
forall s name.
(MakeObject s, ToJSString name) =>
name -> IndexPreservingGetter s (JSM JSVal)
js String
"byteLength" JSM JSVal -> (JSVal -> JSM Int) -> JSM Int
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= JSVal -> JSM Int
forall a. FromJSVal a => JSVal -> JSM a
fromJSValUnchecked
{-# INLINE byteLengthIO #-}