{-# LANGUAGE CPP, ScopedTypeVariables, DeriveDataTypeable, DefaultSignatures,
             TypeOperators, TupleSections, FlexibleContexts, FlexibleInstances,
             LambdaCase
  #-}

module GHCJS.Marshal.Internal ( FromJSVal(..)
                              , ToJSVal(..)
                              , PToJSVal(..)
                              , PFromJSVal(..)
                              , Purity(..)
                              , toJSVal_generic
                              , fromJSVal_generic
                              , toJSVal_pure
                              , fromJSVal_pure
                              , fromJSValUnchecked_pure
                              ) where

import           Control.Applicative
import           Control.Monad

import           Data.Data
import           Data.Maybe
import           Data.Coerce (coerce)
import qualified Data.Text as T (pack)

import           GHC.Generics

import qualified GHCJS.Prim.Internal        as Prim
import qualified GHCJS.Foreign.Internal     as F
import           GHCJS.Types

import qualified Data.JSString.Internal.Type as JSS

import qualified JavaScript.Object.Internal as OI (Object(..), create, setProp, getProp)
import qualified JavaScript.Array.Internal as AI (SomeJSArray(..), create, push, read, fromListIO, toListIO)

import           Language.Javascript.JSaddle.Types (JSM, MutableJSArray, GHCJSPure(..), ghcjsPure, ghcjsPureMap, JSadddleHasCallStack)
import           Language.Javascript.JSaddle.String (textToStr)

data Purity = PureShared    -- ^ conversion is pure even if the original value is shared
            | PureExclusive -- ^ conversion is pure if the we only convert once
  deriving (Purity -> Purity -> Bool
(Purity -> Purity -> Bool)
-> (Purity -> Purity -> Bool) -> Eq Purity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Purity -> Purity -> Bool
== :: Purity -> Purity -> Bool
$c/= :: Purity -> Purity -> Bool
/= :: Purity -> Purity -> Bool
Eq, Eq Purity
Eq Purity =>
(Purity -> Purity -> Ordering)
-> (Purity -> Purity -> Bool)
-> (Purity -> Purity -> Bool)
-> (Purity -> Purity -> Bool)
-> (Purity -> Purity -> Bool)
-> (Purity -> Purity -> Purity)
-> (Purity -> Purity -> Purity)
-> Ord Purity
Purity -> Purity -> Bool
Purity -> Purity -> Ordering
Purity -> Purity -> Purity
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Purity -> Purity -> Ordering
compare :: Purity -> Purity -> Ordering
$c< :: Purity -> Purity -> Bool
< :: Purity -> Purity -> Bool
$c<= :: Purity -> Purity -> Bool
<= :: Purity -> Purity -> Bool
$c> :: Purity -> Purity -> Bool
> :: Purity -> Purity -> Bool
$c>= :: Purity -> Purity -> Bool
>= :: Purity -> Purity -> Bool
$cmax :: Purity -> Purity -> Purity
max :: Purity -> Purity -> Purity
$cmin :: Purity -> Purity -> Purity
min :: Purity -> Purity -> Purity
Ord, Typeable, Typeable Purity
Typeable Purity =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Purity -> c Purity)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Purity)
-> (Purity -> Constr)
-> (Purity -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Purity))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Purity))
-> ((forall b. Data b => b -> b) -> Purity -> Purity)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Purity -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Purity -> r)
-> (forall u. (forall d. Data d => d -> u) -> Purity -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Purity -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Purity -> m Purity)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Purity -> m Purity)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Purity -> m Purity)
-> Data Purity
Purity -> Constr
Purity -> DataType
(forall b. Data b => b -> b) -> Purity -> Purity
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Purity -> u
forall u. (forall d. Data d => d -> u) -> Purity -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Purity
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Purity -> c Purity
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Purity)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Purity)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Purity -> c Purity
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Purity -> c Purity
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Purity
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Purity
$ctoConstr :: Purity -> Constr
toConstr :: Purity -> Constr
$cdataTypeOf :: Purity -> DataType
dataTypeOf :: Purity -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Purity)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Purity)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Purity)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Purity)
$cgmapT :: (forall b. Data b => b -> b) -> Purity -> Purity
gmapT :: (forall b. Data b => b -> b) -> Purity -> Purity
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Purity -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Purity -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Purity -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Purity -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Purity -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Purity -> m Purity
Data)

class PToJSVal a where
--  type PureOut a :: Purity
  pToJSVal :: a -> JSVal

class PFromJSVal a where
--  type PureIn a :: Purity
  pFromJSVal :: JSVal -> a

class ToJSVal a where
  toJSVal :: a -> JSM JSVal

  toJSValListOf :: [a] -> JSM JSVal
  toJSValListOf = (SomeJSArray Any -> JSVal) -> JSM (SomeJSArray Any) -> JSM JSVal
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SomeJSArray Any -> JSVal
forall a b. Coercible a b => a -> b
coerce (JSM (SomeJSArray Any) -> JSM JSVal)
-> ([JSVal] -> JSM (SomeJSArray Any)) -> [JSVal] -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [JSVal] -> JSM (SomeJSArray Any)
forall (m :: MutabilityType (*)). [JSVal] -> JSM (SomeJSArray m)
AI.fromListIO ([JSVal] -> JSM JSVal) -> ([a] -> JSM [JSVal]) -> [a] -> JSM JSVal
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> JSM JSVal) -> [a] -> JSM [JSVal]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM a -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal

  -- default toJSVal :: PToJSVal a => a -> JSM (JSVal a)
  -- toJSVal x = return (pToJSVal x)

  default toJSVal :: (Generic a, GToJSVal (Rep a ())) => a -> JSM JSVal
  toJSVal = (String -> String) -> a -> JSM JSVal
forall a.
(Generic a, GToJSVal (Rep a ())) =>
(String -> String) -> a -> JSM JSVal
toJSVal_generic String -> String
forall a. a -> a
id

fromJustWithStack :: JSadddleHasCallStack => Maybe a -> a
fromJustWithStack :: forall a. JSadddleHasCallStack => Maybe a -> a
fromJustWithStack Maybe a
Nothing = String -> a
forall a. HasCallStack => String -> a
error String
"fromJSValUnchecked: fromJSVal result was Nothing"
fromJustWithStack (Just a
x) = a
x

class FromJSVal a where
  fromJSVal :: JSVal -> JSM (Maybe a)

#if MIN_VERSION_base(4,9,0) && defined(JSADDLE_HAS_CALL_STACK)
  fromJSValUnchecked :: JSadddleHasCallStack => JSVal -> JSM a
#ifdef CHECK_UNCHECKED
  fromJSValUnchecked v = fromJSVal v >>= \case
                             Nothing -> error "fromJSValUnchecked: fromJSVal result was Nothing"
                             Just x  -> return x
#else
  fromJSValUnchecked = fmap fromJustWithStack . fromJSVal
#endif
#else
  fromJSValUnchecked :: JSVal -> JSM a
  fromJSValUnchecked = (Maybe a -> a) -> JSM (Maybe a) -> JSM a
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> a
forall a. HasCallStack => Maybe a -> a
fromJust (JSM (Maybe a) -> JSM a)
-> (JSVal -> JSM (Maybe a)) -> JSVal -> JSM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM (Maybe a)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal
#endif
  {-# INLINE fromJSValUnchecked #-}

  fromJSValListOf :: JSVal -> JSM (Maybe [a])
  fromJSValListOf = ([Maybe a] -> Maybe [a]) -> JSM [Maybe a] -> JSM (Maybe [a])
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe a] -> Maybe [a]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence (JSM [Maybe a] -> JSM (Maybe [a]))
-> (JSVal -> JSM [Maybe a]) -> JSVal -> JSM (Maybe [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((JSVal -> JSM (Maybe a)) -> [JSVal] -> JSM [Maybe a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM JSVal -> JSM (Maybe a)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal ([JSVal] -> JSM [Maybe a])
-> (JSVal -> JSM [JSVal]) -> JSVal -> JSM [Maybe a]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< SomeJSArray Any -> JSM [JSVal]
forall (m :: MutabilityType (*)). SomeJSArray m -> JSM [JSVal]
AI.toListIO (SomeJSArray Any -> JSM [JSVal])
-> (JSVal -> SomeJSArray Any) -> JSVal -> JSM [JSVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> SomeJSArray Any
forall a b. Coercible a b => a -> b
coerce) -- fixme should check that it's an array

#if MIN_VERSION_base(4,9,0) && defined(JSADDLE_HAS_CALL_STACK)
  fromJSValUncheckedListOf :: JSadddleHasCallStack => JSVal -> JSM [a]
#else
  fromJSValUncheckedListOf :: JSVal -> JSM [a]
#endif
  fromJSValUncheckedListOf = (JSVal -> JSM a) -> [JSVal] -> JSM [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM JSVal -> JSM a
forall a. FromJSVal a => JSVal -> JSM a
fromJSValUnchecked ([JSVal] -> JSM [a]) -> (JSVal -> JSM [JSVal]) -> JSVal -> JSM [a]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< SomeJSArray Any -> JSM [JSVal]
forall (m :: MutabilityType (*)). SomeJSArray m -> JSM [JSVal]
AI.toListIO (SomeJSArray Any -> JSM [JSVal])
-> (JSVal -> SomeJSArray Any) -> JSVal -> JSM [JSVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> SomeJSArray Any
forall a b. Coercible a b => a -> b
coerce

  -- default fromJSVal :: PFromJSVal a => JSVal a -> JSM (Maybe a)
  -- fromJSVal x = return (Just (pFromJSVal x))

  default fromJSVal :: (Generic a, GFromJSVal (Rep a ())) => JSVal -> JSM (Maybe a)
  fromJSVal = (String -> String) -> JSVal -> JSM (Maybe a)
forall a.
(Generic a, GFromJSVal (Rep a ())) =>
(String -> String) -> JSVal -> JSM (Maybe a)
fromJSVal_generic String -> String
forall a. a -> a
id

  -- default fromJSValUnchecked :: PFromJSVal a => a -> IO a
  -- fromJSValUnchecked x = return (pFromJSVal x)

-- -----------------------------------------------------------------------------

class GToJSVal a where
  gToJSVal :: (String -> String) -> Bool -> a -> JSM JSVal

class GToJSProp a where
  gToJSProp :: (String -> String) -> JSVal -> a -> JSM ()

class GToJSArr a where
  gToJSArr :: (String -> String) -> MutableJSArray -> a -> JSM ()

instance (ToJSVal b) => GToJSVal (K1 a b c) where
  gToJSVal :: (String -> String) -> Bool -> K1 a b c -> JSM JSVal
gToJSVal String -> String
_ Bool
_ (K1 b
x) = b -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal b
x

instance GToJSVal p => GToJSVal (Par1 p) where
  gToJSVal :: (String -> String) -> Bool -> Par1 p -> JSM JSVal
gToJSVal String -> String
f Bool
b (Par1 p
p) = (String -> String) -> Bool -> p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
b p
p

instance GToJSVal (f p) => GToJSVal (Rec1 f p) where
  gToJSVal :: (String -> String) -> Bool -> Rec1 f p -> JSM JSVal
gToJSVal String -> String
f Bool
b (Rec1 f p
x) = (String -> String) -> Bool -> f p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
b f p
x

instance (GToJSVal (a p), GToJSVal (b p)) => GToJSVal ((a :+: b) p) where
  gToJSVal :: (String -> String) -> Bool -> (:+:) a b p -> JSM JSVal
gToJSVal String -> String
f Bool
_ (L1 a p
x) = (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
True a p
x
  gToJSVal String -> String
f Bool
_ (R1 b p
x) = (String -> String) -> Bool -> b p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
True b p
x

instance (Datatype c, GToJSVal (a p)) => GToJSVal (M1 D c a p) where
  gToJSVal :: (String -> String) -> Bool -> M1 D c a p -> JSM JSVal
gToJSVal String -> String
f Bool
b m :: M1 D c a p
m@(M1 a p
x) = (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
b a p
x

instance (Constructor c, GToJSVal (a p)) => GToJSVal (M1 C c a p) where
  gToJSVal :: (String -> String) -> Bool -> M1 C c a p -> JSM JSVal
gToJSVal String -> String
f Bool
True m :: M1 C c a p
m@(M1 a p
x) = do
    obj :: Object
obj@(OI.Object JSVal
obj') <- JSM Object
OI.create
    JSVal
v   <- (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f (M1 C c a p -> Bool
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Bool
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Bool
conIsRecord M1 C c a p
m) a p
x
    JSString -> JSVal -> Object -> JSM ()
OI.setProp (String -> JSString
packJSS (String -> JSString) -> (String -> String) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
f (String -> JSString) -> String -> JSString
forall a b. (a -> b) -> a -> b
$ M1 C c a p -> String
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> String
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> String
conName M1 C c a p
m) JSVal
v Object
obj
    JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
obj'
  gToJSVal String -> String
f Bool
_ m :: M1 C c a p
m@(M1 a p
x) = (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f (M1 C c a p -> Bool
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Bool
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Bool
conIsRecord M1 C c a p
m) a p
x

instance (GToJSArr (a p), GToJSArr (b p), GToJSProp (a p), GToJSProp (b p)) => GToJSVal ((a :*: b) p) where
  gToJSVal :: (String -> String) -> Bool -> (:*:) a b p -> JSM JSVal
gToJSVal String -> String
f Bool
True (:*:) a b p
xy = do
    (OI.Object JSVal
obj') <- JSM Object
OI.create
    (String -> String) -> JSVal -> (:*:) a b p -> JSM ()
forall a. GToJSProp a => (String -> String) -> JSVal -> a -> JSM ()
gToJSProp String -> String
f JSVal
obj' (:*:) a b p
xy
    JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
obj'
  gToJSVal String -> String
f Bool
False (:*:) a b p
xy = do
    arr :: MutableJSArray
arr@(AI.SomeJSArray JSVal
arr') <- JSM MutableJSArray
AI.create
    (String -> String) -> MutableJSArray -> (:*:) a b p -> JSM ()
forall a.
GToJSArr a =>
(String -> String) -> MutableJSArray -> a -> JSM ()
gToJSArr String -> String
f MutableJSArray
arr (:*:) a b p
xy
    JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
arr'

instance GToJSVal (a p) => GToJSVal (M1 S c a p) where
  gToJSVal :: (String -> String) -> Bool -> M1 S c a p -> JSM JSVal
gToJSVal String -> String
f Bool
b (M1 a p
x) = (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
b a p
x

instance (GToJSProp (a p), GToJSProp (b p)) => GToJSProp ((a :*: b) p) where
  gToJSProp :: (String -> String) -> JSVal -> (:*:) a b p -> JSM ()
gToJSProp String -> String
f JSVal
o (a p
x :*: b p
y) = (String -> String) -> JSVal -> a p -> JSM ()
forall a. GToJSProp a => (String -> String) -> JSVal -> a -> JSM ()
gToJSProp String -> String
f JSVal
o a p
x JSM () -> JSM () -> JSM ()
forall a b. JSM a -> JSM b -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (String -> String) -> JSVal -> b p -> JSM ()
forall a. GToJSProp a => (String -> String) -> JSVal -> a -> JSM ()
gToJSProp String -> String
f JSVal
o b p
y

instance (Selector c, GToJSVal (a p)) => GToJSProp (M1 S c a p) where
  gToJSProp :: (String -> String) -> JSVal -> M1 S c a p -> JSM ()
gToJSProp String -> String
f JSVal
o m :: M1 S c a p
m@(M1 a p
x) = do
    JSVal
r <- (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
False a p
x
    JSString -> JSVal -> Object -> JSM ()
OI.setProp (String -> JSString
packJSS (String -> JSString) -> (String -> String) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
f (String -> JSString) -> String -> JSString
forall a b. (a -> b) -> a -> b
$ M1 S c a p -> String
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> String
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> String
selName M1 S c a p
m) JSVal
r (JSVal -> Object
OI.Object JSVal
o)

instance (GToJSArr (a p), GToJSArr (b p)) => GToJSArr ((a :*: b) p) where
  gToJSArr :: (String -> String) -> MutableJSArray -> (:*:) a b p -> JSM ()
gToJSArr String -> String
f MutableJSArray
a (a p
x :*: b p
y) = (String -> String) -> MutableJSArray -> a p -> JSM ()
forall a.
GToJSArr a =>
(String -> String) -> MutableJSArray -> a -> JSM ()
gToJSArr String -> String
f MutableJSArray
a a p
x JSM () -> JSM () -> JSM ()
forall a b. JSM a -> JSM b -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (String -> String) -> MutableJSArray -> b p -> JSM ()
forall a.
GToJSArr a =>
(String -> String) -> MutableJSArray -> a -> JSM ()
gToJSArr String -> String
f MutableJSArray
a b p
y

instance GToJSVal (a p) => GToJSArr (M1 S c a p) where
  gToJSArr :: (String -> String) -> MutableJSArray -> M1 S c a p -> JSM ()
gToJSArr String -> String
f MutableJSArray
a (M1 a p
x) = do
    JSVal
r <- (String -> String) -> Bool -> a p -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
False a p
x
    JSVal -> MutableJSArray -> JSM ()
AI.push JSVal
r MutableJSArray
a

instance GToJSVal (V1 p) where
  gToJSVal :: (String -> String) -> Bool -> V1 p -> JSM JSVal
gToJSVal String -> String
_ Bool
_ V1 p
_ = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
Prim.jsNull

instance GToJSVal (U1 p) where
  gToJSVal :: (String -> String) -> Bool -> U1 p -> JSM JSVal
gToJSVal String -> String
_ Bool
_ U1 p
_ = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
F.jsTrue

toJSVal_generic :: forall a . (Generic a, GToJSVal (Rep a ()))
                => (String -> String) -> a -> JSM JSVal
toJSVal_generic :: forall a.
(Generic a, GToJSVal (Rep a ())) =>
(String -> String) -> a -> JSM JSVal
toJSVal_generic String -> String
f a
x = (String -> String) -> Bool -> Rep a () -> JSM JSVal
forall a.
GToJSVal a =>
(String -> String) -> Bool -> a -> JSM JSVal
gToJSVal String -> String
f Bool
False (a -> Rep a ()
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from a
x :: Rep a ())

-- -----------------------------------------------------------------------------

class GFromJSVal a where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe a)

class GFromJSProp a where
  gFromJSProp :: (String -> String) -> JSVal -> JSM (Maybe a)

class GFromJSArr a where
  gFromJSArr :: (String -> String) -> MutableJSArray -> Int -> JSM (Maybe (a,Int))

instance FromJSVal b => GFromJSVal (K1 a b c) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (K1 a b c))
gFromJSVal String -> String
_ Bool
_ JSVal
r = (b -> K1 a b c) -> Maybe b -> Maybe (K1 a b c)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> K1 a b c
forall k i c (p :: k). c -> K1 i c p
K1 (Maybe b -> Maybe (K1 a b c))
-> JSM (Maybe b) -> JSM (Maybe (K1 a b c))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> JSM (Maybe b)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal JSVal
r

instance GFromJSVal p => GFromJSVal (Par1 p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (Par1 p))
gFromJSVal String -> String
f Bool
b JSVal
r = (String -> String) -> Bool -> JSVal -> JSM (Maybe (Par1 p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
b JSVal
r

instance GFromJSVal (f p) => GFromJSVal (Rec1 f p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (Rec1 f p))
gFromJSVal String -> String
f Bool
b JSVal
r = (String -> String) -> Bool -> JSVal -> JSM (Maybe (Rec1 f p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
b JSVal
r

instance (GFromJSVal (a p), GFromJSVal (b p)) => GFromJSVal ((a :+: b) p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe ((:+:) a b p))
gFromJSVal String -> String
f Bool
b JSVal
r = do
    Maybe (a p)
l <- (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
True JSVal
r
    case Maybe (a p)
l of
      Just a p
x  -> Maybe ((:+:) a b p) -> JSM (Maybe ((:+:) a b p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a p -> (:+:) a b p
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (a p -> (:+:) a b p) -> Maybe (a p) -> Maybe ((:+:) a b p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a p -> Maybe (a p)
forall a. a -> Maybe a
Just a p
x)
      Maybe (a p)
Nothing -> (b p -> (:+:) a b p) -> Maybe (b p) -> Maybe ((:+:) a b p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b p -> (:+:) a b p
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (Maybe (b p) -> Maybe ((:+:) a b p))
-> JSM (Maybe (b p)) -> JSM (Maybe ((:+:) a b p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (b p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
True JSVal
r

instance (Datatype c, GFromJSVal (a p)) => GFromJSVal (M1 D c a p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (M1 D c a p))
gFromJSVal String -> String
f Bool
b JSVal
r = (a p -> M1 D c a p) -> Maybe (a p) -> Maybe (M1 D c a p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a p -> M1 D c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Maybe (a p) -> Maybe (M1 D c a p))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 D c a p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
b JSVal
r

instance forall c a p . (Constructor c, GFromJSVal (a p)) => GFromJSVal (M1 C c a p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (M1 C c a p))
gFromJSVal String -> String
f Bool
True JSVal
r = do
    JSVal
r' <- JSString -> Object -> JSM JSVal
OI.getProp (String -> JSString
packJSS (String -> JSString) -> (String -> String) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
f (String -> JSString) -> String -> JSString
forall a b. (a -> b) -> a -> b
$ M1 C c a p -> String
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> String
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> String
conName (M1 C c a p
forall a. HasCallStack => a
undefined :: M1 C c a p)) (JSVal -> Object
OI.Object JSVal
r)
    GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
r') JSM Bool
-> (Bool -> JSM (Maybe (M1 C c a p))) -> JSM (Maybe (M1 C c a p))
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Bool
True -> Maybe (M1 C c a p) -> JSM (Maybe (M1 C c a p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (M1 C c a p)
forall a. Maybe a
Nothing
      Bool
False -> (a p -> M1 C c a p) -> Maybe (a p) -> Maybe (M1 C c a p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a p -> M1 C c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Maybe (a p) -> Maybe (M1 C c a p))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 C c a p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f (M1 C c a p -> Bool
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Bool
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Bool
conIsRecord (M1 C c a p
forall a. HasCallStack => a
undefined :: M1 C c a p)) JSVal
r'
  gFromJSVal String -> String
f Bool
_ JSVal
r = (a p -> M1 C c a p) -> Maybe (a p) -> Maybe (M1 C c a p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a p -> M1 C c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Maybe (a p) -> Maybe (M1 C c a p))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 C c a p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f (M1 C c a p -> Bool
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Bool
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Bool
conIsRecord (M1 C c a p
forall a. HasCallStack => a
undefined :: M1 C c a p)) JSVal
r

instance (GFromJSArr (a p), GFromJSArr (b p), GFromJSProp (a p), GFromJSProp (b p)) => GFromJSVal ((a :*: b) p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe ((:*:) a b p))
gFromJSVal String -> String
f Bool
True  JSVal
r = (String -> String) -> JSVal -> JSM (Maybe ((:*:) a b p))
forall a.
GFromJSProp a =>
(String -> String) -> JSVal -> JSM (Maybe a)
gFromJSProp String -> String
f JSVal
r
  gFromJSVal String -> String
f Bool
False JSVal
r = (((:*:) a b p, Int) -> (:*:) a b p)
-> Maybe ((:*:) a b p, Int) -> Maybe ((:*:) a b p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((:*:) a b p, Int) -> (:*:) a b p
forall a b. (a, b) -> a
fst (Maybe ((:*:) a b p, Int) -> Maybe ((:*:) a b p))
-> JSM (Maybe ((:*:) a b p, Int)) -> JSM (Maybe ((:*:) a b p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String)
-> MutableJSArray -> Int -> JSM (Maybe ((:*:) a b p, Int))
forall a.
GFromJSArr a =>
(String -> String) -> MutableJSArray -> Int -> JSM (Maybe (a, Int))
gFromJSArr String -> String
f (JSVal -> MutableJSArray
forall s (m :: MutabilityType s). JSVal -> SomeJSArray m
AI.SomeJSArray JSVal
r) Int
0

instance GFromJSVal (a p) => GFromJSVal (M1 S c a p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (M1 S c a p))
gFromJSVal String -> String
f Bool
b JSVal
r = (a p -> M1 S c a p) -> Maybe (a p) -> Maybe (M1 S c a p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a p -> M1 S c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Maybe (a p) -> Maybe (M1 S c a p))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 S c a p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
b JSVal
r

instance (GFromJSProp (a p), GFromJSProp (b p)) => GFromJSProp ((a :*: b) p) where
  gFromJSProp :: (String -> String) -> JSVal -> JSM (Maybe ((:*:) a b p))
gFromJSProp String -> String
f JSVal
r = do
    Maybe (a p)
a <- (String -> String) -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSProp a =>
(String -> String) -> JSVal -> JSM (Maybe a)
gFromJSProp String -> String
f JSVal
r
    case Maybe (a p)
a of
      Maybe (a p)
Nothing -> Maybe ((:*:) a b p) -> JSM (Maybe ((:*:) a b p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ((:*:) a b p)
forall a. Maybe a
Nothing
      Just a p
a' -> (b p -> (:*:) a b p) -> Maybe (b p) -> Maybe ((:*:) a b p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a p
a'a p -> b p -> (:*:) a b p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*:) (Maybe (b p) -> Maybe ((:*:) a b p))
-> JSM (Maybe (b p)) -> JSM (Maybe ((:*:) a b p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> JSVal -> JSM (Maybe (b p))
forall a.
GFromJSProp a =>
(String -> String) -> JSVal -> JSM (Maybe a)
gFromJSProp String -> String
f JSVal
r

instance forall c a p . (Selector c, GFromJSVal (a p)) => GFromJSProp (M1 S c a p) where
  gFromJSProp :: (String -> String) -> JSVal -> JSM (Maybe (M1 S c a p))
gFromJSProp String -> String
f JSVal
o = do
    JSVal
p <- JSString -> Object -> JSM JSVal
OI.getProp (String -> JSString
packJSS (String -> JSString) -> (String -> String) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
f (String -> JSString) -> String -> JSString
forall a b. (a -> b) -> a -> b
$ M1 S c a p -> String
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> String
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> String
selName (M1 S c a p
forall a. HasCallStack => a
undefined :: M1 S c a p)) (JSVal -> Object
OI.Object JSVal
o)
    GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
p) JSM Bool
-> (Bool -> JSM (Maybe (M1 S c a p))) -> JSM (Maybe (M1 S c a p))
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Bool
True -> Maybe (M1 S c a p) -> JSM (Maybe (M1 S c a p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (M1 S c a p)
forall a. Maybe a
Nothing
      Bool
False -> (a p -> M1 S c a p) -> Maybe (a p) -> Maybe (M1 S c a p)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a p -> M1 S c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Maybe (a p) -> Maybe (M1 S c a p))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 S c a p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
False JSVal
p

instance (GFromJSArr (a p), GFromJSArr (b p)) => GFromJSArr ((a :*: b) p) where
  gFromJSArr :: (String -> String)
-> MutableJSArray -> Int -> JSM (Maybe ((:*:) a b p, Int))
gFromJSArr String -> String
f MutableJSArray
r Int
_n = do
    Maybe (a p, Int)
a <- (String -> String)
-> MutableJSArray -> Int -> JSM (Maybe (a p, Int))
forall a.
GFromJSArr a =>
(String -> String) -> MutableJSArray -> Int -> JSM (Maybe (a, Int))
gFromJSArr String -> String
f MutableJSArray
r Int
0
    case Maybe (a p, Int)
a of
      Just (a p
a',Int
an) -> do
        Maybe (b p, Int)
b <- (String -> String)
-> MutableJSArray -> Int -> JSM (Maybe (b p, Int))
forall a.
GFromJSArr a =>
(String -> String) -> MutableJSArray -> Int -> JSM (Maybe (a, Int))
gFromJSArr String -> String
f MutableJSArray
r Int
an
        case Maybe (b p, Int)
b of
          Just (b p
b',Int
bn) -> Maybe ((:*:) a b p, Int) -> JSM (Maybe ((:*:) a b p, Int))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (((:*:) a b p, Int) -> Maybe ((:*:) a b p, Int)
forall a. a -> Maybe a
Just (a p
a' a p -> b p -> (:*:) a b p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: b p
b',Int
bn))
          Maybe (b p, Int)
_            -> Maybe ((:*:) a b p, Int) -> JSM (Maybe ((:*:) a b p, Int))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ((:*:) a b p, Int)
forall a. Maybe a
Nothing

instance (GFromJSVal (a p)) => GFromJSArr (M1 S c a p) where
  gFromJSArr :: (String -> String)
-> MutableJSArray -> Int -> JSM (Maybe (M1 S c a p, Int))
gFromJSArr String -> String
f MutableJSArray
o Int
n = do
    JSVal
r <- Int -> MutableJSArray -> JSM JSVal
forall (m :: MutabilityType (*)). Int -> SomeJSArray m -> JSM JSVal
AI.read Int
n MutableJSArray
o
    GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
r) JSM Bool
-> (Bool -> JSM (Maybe (M1 S c a p, Int)))
-> JSM (Maybe (M1 S c a p, Int))
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Bool
True -> Maybe (M1 S c a p, Int) -> JSM (Maybe (M1 S c a p, Int))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (M1 S c a p, Int)
forall a. Maybe a
Nothing
      Bool
False -> (a p -> (M1 S c a p, Int))
-> Maybe (a p) -> Maybe (M1 S c a p, Int)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (M1 S c a p -> (M1 S c a p, Int))
-> (a p -> M1 S c a p) -> a p -> (M1 S c a p, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a p -> M1 S c a p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1) (Maybe (a p) -> Maybe (M1 S c a p, Int))
-> JSM (Maybe (a p)) -> JSM (Maybe (M1 S c a p, Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> String) -> Bool -> JSVal -> JSM (Maybe (a p))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
False JSVal
r

instance GFromJSVal (V1 p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (V1 p))
gFromJSVal String -> String
_ Bool
_ JSVal
_ = Maybe (V1 p) -> JSM (Maybe (V1 p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (V1 p)
forall a. Maybe a
Nothing

instance GFromJSVal (U1 p) where
  gFromJSVal :: (String -> String) -> Bool -> JSVal -> JSM (Maybe (U1 p))
gFromJSVal String -> String
_ Bool
_ JSVal
_ = Maybe (U1 p) -> JSM (Maybe (U1 p))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (U1 p -> Maybe (U1 p)
forall a. a -> Maybe a
Just U1 p
forall k (p :: k). U1 p
U1)

fromJSVal_generic :: forall a . (Generic a, GFromJSVal (Rep a ()))
                => (String -> String) -> JSVal -> JSM (Maybe a)
fromJSVal_generic :: forall a.
(Generic a, GFromJSVal (Rep a ())) =>
(String -> String) -> JSVal -> JSM (Maybe a)
fromJSVal_generic String -> String
f JSVal
x = (Rep a () -> a) -> Maybe (Rep a ()) -> Maybe a
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep a () -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Maybe (Rep a ()) -> Maybe a)
-> JSM (Maybe (Rep a ())) -> JSM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((String -> String) -> Bool -> JSVal -> JSM (Maybe (Rep a ()))
forall a.
GFromJSVal a =>
(String -> String) -> Bool -> JSVal -> JSM (Maybe a)
gFromJSVal String -> String
f Bool
False JSVal
x :: JSM (Maybe (Rep a ())))

-- -----------------------------------------------------------------------------

fromJSVal_pure :: PFromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal_pure :: forall a. PFromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal_pure = Maybe a -> JSM (Maybe a)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> JSM (Maybe a))
-> (JSVal -> Maybe a) -> JSVal -> JSM (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> (JSVal -> a) -> JSVal -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> a
forall a. PFromJSVal a => JSVal -> a
pFromJSVal
{-# INLINE fromJSVal_pure #-}

fromJSValUnchecked_pure :: PFromJSVal a => JSVal -> JSM a
fromJSValUnchecked_pure :: forall a. PFromJSVal a => JSVal -> JSM a
fromJSValUnchecked_pure = a -> JSM a
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> JSM a) -> (JSVal -> a) -> JSVal -> JSM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> a
forall a. PFromJSVal a => JSVal -> a
pFromJSVal
{-# INLINE fromJSValUnchecked_pure #-}

toJSVal_pure :: PToJSVal a => a -> JSM JSVal
toJSVal_pure :: forall a. PToJSVal a => a -> JSM JSVal
toJSVal_pure = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (JSVal -> JSM JSVal) -> (a -> JSVal) -> a -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> JSVal
forall a. PToJSVal a => a -> JSVal
pToJSVal
{-# INLINE toJSVal_pure #-}

-- -----------------------------------------------------------------------------

packJSS :: String -> JSString
packJSS :: String -> JSString
packJSS = Text -> JSString
textToStr (Text -> JSString) -> (String -> Text) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack