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 n = SomeArrayBuffer <$> new (jsg "ArrayBuffer") [n]
freeze :: MutableArrayBuffer -> JSM ArrayBuffer
freeze (SomeArrayBuffer b) = SomeArrayBuffer <$> b ^. js1 "slice" (0 :: Int)
unsafeFreeze :: MutableArrayBuffer -> JSM ArrayBuffer
unsafeFreeze (SomeArrayBuffer b) = pure (SomeArrayBuffer b)
thaw :: ArrayBuffer -> JSM MutableArrayBuffer
thaw (SomeArrayBuffer b) = SomeArrayBuffer <$> b ^. js1 "slice" (0 :: Int)
unsafeThaw :: ArrayBuffer -> JSM MutableArrayBuffer
unsafeThaw (SomeArrayBuffer b) = pure (SomeArrayBuffer b)
sliceIO :: Int -> Maybe Int -> SomeArrayBuffer any -> JSM (SomeArrayBuffer any)
sliceIO begin (Just end) (SomeArrayBuffer b) = SomeArrayBuffer <$> b ^. js2 "slice" begin end
sliceIO begin _ (SomeArrayBuffer b) = SomeArrayBuffer <$> b ^. js1 "slice" begin
byteLengthIO :: SomeArrayBuffer any -> JSM Int
byteLengthIO (SomeArrayBuffer b) = b ^. js "byteLength" >>= fromJSValUnchecked