{-# LANGUAGE MagicHash #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module GHCJS.Prim ( module GHCJS.Prim.Internal
                  , fromJSString
                  , toJSString
                  , isNull
                  , isUndefined
                  ) where

import           GHCJS.Prim.Internal
--import           Data.Int (Int64)
--import           Data.Typeable (Typeable)
--import           Unsafe.Coerce (unsafeCoerce)

--import           Data.Aeson (ToJSON(..), FromJSON(..))
import qualified Data.Text as T (unpack, pack)
import           Data.JSString.Text (textFromJSVal)

--import           GHC.Prim
--import qualified GHC.Exception as Ex
--import qualified GHC.Exts as Exts

import Language.Javascript.JSaddle.Types (JSVal(..), JSString(..), GHCJSPure(..), ghcjsPureMap)
import qualified Language.Javascript.JSaddle.Native.Internal as N
       (stringToValue, isNull, isUndefined)
{- | Low-level conversion utilities for packages that cannot
     depend on ghcjs-base
 -}

fromJSString :: JSVal -> GHCJSPure String
fromJSString :: JSVal -> GHCJSPure String
fromJSString = (Text -> String) -> GHCJSPure Text -> GHCJSPure String
forall a b. (a -> b) -> GHCJSPure a -> GHCJSPure b
ghcjsPureMap Text -> String
T.unpack (GHCJSPure Text -> GHCJSPure String)
-> (JSVal -> GHCJSPure Text) -> JSVal -> GHCJSPure String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> GHCJSPure Text
textFromJSVal
{-# INLINE fromJSString #-}

toJSString :: String -> GHCJSPure JSVal
toJSString :: String -> GHCJSPure JSVal
toJSString String
s = JSM JSVal -> GHCJSPure JSVal
forall a. JSM a -> GHCJSPure a
GHCJSPure (JSM JSVal -> GHCJSPure JSVal) -> JSM JSVal -> GHCJSPure JSVal
forall a b. (a -> b) -> a -> b
$ JSString -> JSM JSVal
N.stringToValue (Text -> JSString
JSString (Text -> JSString) -> Text -> JSString
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s)
{-# INLINE toJSString #-}

--fromJSArray :: JSVal -> IO [JSVal]
--fromJSArray = unsafeCoerce . js_fromJSArray
--{-# INLINE fromJSArray #-}
--
--toJSArray :: [JSVal] -> IO JSVal
--toJSArray = js_toJSArray . unsafeCoerce . seqList
--{-# INLINE toJSArray #-}
--
--{- | returns zero if the JSVal does not contain a number
-- -}
--fromJSInt :: JSVal -> Int
--fromJSInt = js_fromJSInt
--{-# INLINE fromJSInt #-}
--
--toJSInt :: Int -> JSVal
--toJSInt = js_toJSInt
--{-# INLINE toJSInt #-}
--

isNull :: JSVal -> GHCJSPure Bool
isNull :: JSVal -> GHCJSPure Bool
isNull = JSM Bool -> GHCJSPure Bool
forall a. JSM a -> GHCJSPure a
GHCJSPure (JSM Bool -> GHCJSPure Bool)
-> (JSVal -> JSM Bool) -> JSVal -> GHCJSPure Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM Bool
N.isNull
{-# INLINE isNull #-}

isUndefined :: JSVal -> GHCJSPure Bool
isUndefined :: JSVal -> GHCJSPure Bool
isUndefined = JSM Bool -> GHCJSPure Bool
forall a. JSM a -> GHCJSPure a
GHCJSPure (JSM Bool -> GHCJSPure Bool)
-> (JSVal -> JSM Bool) -> JSVal -> GHCJSPure Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM Bool
N.isUndefined
{-# INLINE isUndefined #-}

--getProp :: JSVal -> String -> IO JSVal
--getProp o p = js_getProp o (unsafeCoerce $ seqList p)
--{-# INLINE getProp #-}
--
--getProp' :: JSVal -> JSVal -> IO JSVal
--getProp' o p = js_getProp' o p
--{-# INLINE getProp' #-}
--
---- reduce the spine and all list elements to whnf
--seqList :: [a] -> [a]
--seqList xs = go xs `seq` xs
--  where go (x:xs) = x `seq` go xs
--        go []     = ()
--
--seqListSpine :: [a] -> [a]
--seqListSpine xs = go xs `seq` xs
--  where go (x:xs) = go xs
--        go []     = ()