{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE LambdaCase #-}
#ifdef ghcjs_HOST_OS
{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
{-# OPTIONS_GHC -Wno-dodgy-exports -Wno-dodgy-imports #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.Javascript.JSaddle.Value (
JSVal
, ToJSVal(..)
, JSNull(..)
, JSUndefined
, JSString
, JSValue(..)
, showJSValue
, isTruthy
, valToBool
, valToNumber
, valToStr
, valToObject
, valToText
, valToJSON
, val
, jsNull
, valNull
, isNull
, valIsNull
, jsUndefined
, valUndefined
, isUndefined
, valIsUndefined
, maybeNullOrUndefined
, maybeNullOrUndefined'
, toJSBool
, jsTrue
, jsFalse
, valBool
, valMakeNumber
, valMakeString
, valMakeText
, valMakeJSON
, deRefVal
, valMakeRef
, strictEqual
, instanceOf
) where
import Data.Text (Text)
import qualified Data.Text as T (pack, unpack)
import Data.Aeson (Value)
import Data.JSString.Text (textToJSString)
#ifdef ghcjs_HOST_OS
import Language.Javascript.JSaddle.Types
(Object(..), JSString(..), JSVal(..), ghcjsPure)
import GHCJS.Marshal (ToJSVal(..))
#else
import Data.Char (chr, ord)
import Data.Word (Word, Word8, Word16, Word32)
import Data.Int (Int8, Int16, Int32)
import GHCJS.Marshal.Internal (ToJSVal(..), FromJSVal(..))
import Language.Javascript.JSaddle.Types
(Object(..), JSString(..), JSVal(..), ghcjsPure)
import Language.Javascript.JSaddle.Native
(valueToNumber, valueToString, valueToJSON, numberToValue, stringToValue, jsonValueToValue)
import qualified Language.Javascript.JSaddle.Native as N
(deRefVal, strictEqual, instanceOf)
import Language.Javascript.JSaddle.Run (Result(..))
#endif
import Language.Javascript.JSaddle.Monad (JSM)
import Language.Javascript.JSaddle.Classes
(MakeObject(..), MakeArgs(..))
import Language.Javascript.JSaddle.Marshal.String (ToJSString(..), FromJSString(..))
import Language.Javascript.JSaddle.String (strToText, textToStr)
import GHCJS.Foreign.Internal (jsTrue, jsFalse, jsNull, toJSBool, jsUndefined, isTruthy, isNull, isUndefined)
data JSNull = JSNull
type JSUndefined = ()
data JSValue = ValNull
| ValUndefined
| ValBool Bool
| ValNumber Double
| ValString Text
| ValObject Object
showJSValue :: JSValue -> String
showJSValue :: JSValue -> String
showJSValue JSValue
ValNull = String
"null"
showJSValue JSValue
ValUndefined = String
"undefined"
showJSValue (ValBool Bool
True) = String
"true"
showJSValue (ValBool Bool
False) = String
"false"
showJSValue (ValNumber Double
x) = Double -> String
forall a. Show a => a -> String
show Double
x
showJSValue (ValString Text
s) = Text -> String
forall a. Show a => a -> String
show Text
s
showJSValue (ValObject Object
_) = String
"object"
valToBool :: ToJSVal value => value -> JSM Bool
valToBool :: forall value. ToJSVal value => value -> JSM Bool
valToBool value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM Bool) -> JSM Bool
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (GHCJSPure Bool -> JSM Bool)
-> (JSVal -> GHCJSPure Bool) -> JSVal -> JSM Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> GHCJSPure Bool
isTruthy
valToNumber :: ToJSVal value => value -> JSM Double
#ifdef ghcjs_HOST_OS
valToNumber value = jsrefToNumber <$> toJSVal value
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1) => { return Number($1); })"
#else
"$r = Number($1);"
#endif
jsrefToNumber :: JSVal -> Double
#else
valToNumber :: forall value. ToJSVal value => value -> JSM Double
valToNumber value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM Double) -> JSM Double
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 Double
valueToNumber
#endif
valToStr :: ToJSVal value => value -> JSM JSString
#ifdef ghcjs_HOST_OS
valToStr value = jsrefToString <$> toJSVal value
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1) => { return $1.toString(); })"
#else
"$r = $1.toString();"
#endif
jsrefToString :: JSVal -> JSString
#else
valToStr :: forall value. ToJSVal value => value -> JSM JSString
valToStr value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM JSString) -> JSM JSString
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 JSString
valueToString
#endif
valToText :: ToJSVal value => value -> JSM Text
valToText :: forall value. ToJSVal value => value -> JSM Text
valToText value
jsvar = JSString -> Text
strToText (JSString -> Text) -> JSM JSString -> JSM Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> value -> JSM JSString
forall value. ToJSVal value => value -> JSM JSString
valToStr value
jsvar
valToJSON :: ToJSVal value => value -> JSM JSString
#ifdef ghcjs_HOST_OS
valToJSON value = jsrefToJSON <$> toJSVal value
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1) => { return $1 === undefined ? \"\" : JSON.stringify($1); })"
#else
"$r = $1 === undefined ? \"\" : JSON.stringify($1);"
#endif
jsrefToJSON :: JSVal -> JSString
#else
valToJSON :: forall value. ToJSVal value => value -> JSM JSString
valToJSON value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM JSString) -> JSM JSString
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 JSString
valueToJSON
#endif
valToObject :: ToJSVal value => value -> JSM Object
valToObject :: forall value. ToJSVal value => value -> JSM Object
valToObject value
value = JSVal -> Object
Object (JSVal -> Object) -> JSM JSVal -> JSM Object
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value
instance MakeObject JSVal where
makeObject :: JSVal -> JSM Object
makeObject = Object -> JSM Object
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Object -> JSM Object) -> (JSVal -> Object) -> JSVal -> JSM Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> Object
Object
instance ToJSVal Object where
toJSVal :: Object -> JSM JSVal
toJSVal (Object JSVal
r) = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
r
val :: ToJSVal value
=> value
-> JSM JSVal
val :: forall a. ToJSVal a => a -> JSM JSVal
val = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
#ifndef ghcjs_HOST_OS
instance ToJSVal JSVal where
toJSVal :: JSVal -> JSM JSVal
toJSVal = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE toJSVal #-}
#endif
instance MakeArgs JSVal where
makeArgs :: JSVal -> JSM [JSVal]
makeArgs JSVal
arg = [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
arg]
instance ToJSVal v => ToJSVal (JSM v) where
toJSVal :: JSM v -> JSM JSVal
toJSVal JSM v
v = JSM v
v JSM v -> (v -> JSM JSVal) -> JSM JSVal
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= v -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
{-# INLINE toJSVal #-}
valNull :: JSVal
valNull :: JSVal
valNull = JSVal
jsNull
{-# INLINE valNull #-}
instance ToJSVal JSNull where
toJSVal :: JSNull -> JSM JSVal
toJSVal = JSM JSVal -> JSNull -> JSM JSVal
forall a b. a -> b -> a
const (JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
jsNull)
{-# INLINE toJSVal #-}
instance MakeArgs JSNull where
makeArgs :: JSNull -> JSM [JSVal]
makeArgs JSNull
_ = [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
jsNull]
#ifndef ghcjs_HOST_OS
instance ToJSVal a => ToJSVal (Maybe a) where
toJSVal :: Maybe a -> JSM JSVal
toJSVal Maybe a
Nothing = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
jsNull
toJSVal (Just a
a) = a -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal a
a
{-# INLINE toJSVal #-}
instance FromJSVal a => FromJSVal (Maybe a) where
fromJSValUnchecked :: JSVal -> JSM (Maybe a)
fromJSValUnchecked JSVal
x =
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
x) JSM Bool -> (Bool -> JSM (Maybe a)) -> JSM (Maybe a)
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 a -> JSM (Maybe a)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Bool
False -> GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isNull JSVal
x) JSM Bool -> (Bool -> JSM (Maybe a)) -> JSM (Maybe a)
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 a -> JSM (Maybe a)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Bool
False -> JSVal -> JSM (Maybe a)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal JSVal
x
{-# INLINE fromJSValUnchecked #-}
fromJSVal :: JSVal -> JSM (Maybe (Maybe a))
fromJSVal JSVal
x =
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
x) JSM Bool
-> (Bool -> JSM (Maybe (Maybe a))) -> JSM (Maybe (Maybe a))
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 (Maybe a) -> JSM (Maybe (Maybe a))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just Maybe a
forall a. Maybe a
Nothing)
Bool
False -> GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isNull JSVal
x) JSM Bool
-> (Bool -> JSM (Maybe (Maybe a))) -> JSM (Maybe (Maybe a))
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 (Maybe a) -> JSM (Maybe (Maybe a))
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just Maybe a
forall a. Maybe a
Nothing)
Bool
False -> (JSM (Maybe a) -> JSM (Maybe (Maybe a)))
-> (JSVal -> JSM (Maybe a)) -> JSVal -> JSM (Maybe (Maybe a))
forall a b. (a -> b) -> (JSVal -> a) -> JSVal -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Maybe a -> Maybe (Maybe a))
-> JSM (Maybe a) -> JSM (Maybe (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 (Maybe a)
forall a. a -> Maybe a
Just) JSVal -> JSM (Maybe a)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal JSVal
x
{-# INLINE fromJSVal #-}
instance ToJSVal a => ToJSVal [a] where
toJSVal :: [a] -> JSM JSVal
toJSVal = [a] -> JSM JSVal
forall a. ToJSVal a => [a] -> JSM JSVal
toJSValListOf
{-# INLINE toJSVal #-}
instance FromJSVal a => FromJSVal [a] where
fromJSVal :: JSVal -> JSM (Maybe [a])
fromJSVal = JSVal -> JSM (Maybe [a])
forall a. FromJSVal a => JSVal -> JSM (Maybe [a])
fromJSValListOf
{-# INLINE fromJSVal #-}
#endif
valIsNull :: ToJSVal value => value -> JSM Bool
valIsNull :: forall value. ToJSVal value => value -> JSM Bool
valIsNull value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM Bool) -> JSM Bool
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (GHCJSPure Bool -> JSM Bool)
-> (JSVal -> GHCJSPure Bool) -> JSVal -> JSM Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> GHCJSPure Bool
isNull
valUndefined :: JSVal
valUndefined :: JSVal
valUndefined = JSVal
jsUndefined
{-# INLINE valUndefined #-}
instance ToJSVal JSUndefined where
toJSVal :: JSUndefined -> JSM JSVal
toJSVal = JSM JSVal -> JSUndefined -> JSM JSVal
forall a b. a -> b -> a
const (JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
jsUndefined)
instance MakeArgs () where
makeArgs :: JSUndefined -> JSM [JSVal]
makeArgs JSUndefined
_ = [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return []
valIsUndefined :: ToJSVal value => value -> JSM Bool
valIsUndefined :: forall value. ToJSVal value => value -> JSM Bool
valIsUndefined value
value = value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value JSM JSVal -> (JSVal -> JSM Bool) -> JSM Bool
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (GHCJSPure Bool -> JSM Bool)
-> (JSVal -> GHCJSPure Bool) -> JSVal -> JSM Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> GHCJSPure Bool
isUndefined
maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal)
maybeNullOrUndefined :: forall value. ToJSVal value => value -> JSM (Maybe JSVal)
maybeNullOrUndefined value
value = do
JSVal
rval <- value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isNull JSVal
rval) JSM Bool -> (Bool -> JSM (Maybe JSVal)) -> JSM (Maybe JSVal)
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 JSVal -> JSM (Maybe JSVal)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe JSVal
forall a. Maybe a
Nothing
Bool
_ ->
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
rval) JSM Bool -> (Bool -> JSM (Maybe JSVal)) -> JSM (Maybe JSVal)
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 JSVal -> JSM (Maybe JSVal)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe JSVal
forall a. Maybe a
Nothing
Bool
_ -> Maybe JSVal -> JSM (Maybe JSVal)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (JSVal -> Maybe JSVal
forall a. a -> Maybe a
Just JSVal
rval)
maybeNullOrUndefined' :: ToJSVal value => (JSVal -> JSM a) -> value -> JSM (Maybe a)
maybeNullOrUndefined' :: forall value a.
ToJSVal value =>
(JSVal -> JSM a) -> value -> JSM (Maybe a)
maybeNullOrUndefined' JSVal -> JSM a
f value
value = do
JSVal
rval <- value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isNull JSVal
rval) JSM Bool -> (Bool -> JSM (Maybe a)) -> JSM (Maybe a)
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 a -> JSM (Maybe a)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Bool
_ ->
GHCJSPure Bool -> JSM Bool
forall a. GHCJSPure a -> JSM a
ghcjsPure (JSVal -> GHCJSPure Bool
isUndefined JSVal
rval) JSM Bool -> (Bool -> JSM (Maybe a)) -> JSM (Maybe a)
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 a -> JSM (Maybe a)
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Bool
_ -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> JSM a -> JSM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> JSM a
f JSVal
rval
valBool :: Bool -> JSVal
valBool :: Bool -> JSVal
valBool = Bool -> JSVal
toJSBool
{-# INLINE valBool #-}
#ifndef ghcjs_HOST_OS
instance ToJSVal Bool where
toJSVal :: Bool -> JSM JSVal
toJSVal = JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (JSVal -> JSM JSVal) -> (Bool -> JSVal) -> Bool -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> JSVal
valBool
{-# INLINE toJSVal #-}
#endif
instance MakeArgs Bool where
makeArgs :: Bool -> JSM [JSVal]
makeArgs Bool
b = [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [Bool -> JSVal
valBool Bool
b]
valMakeNumber :: Double -> JSM JSVal
valMakeNumber :: Double -> JSM JSVal
valMakeNumber = Double -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
{-# INLINE valMakeNumber #-}
#ifndef ghcjs_HOST_OS
instance ToJSVal Double where
toJSVal :: Double -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue
{-# INLINE toJSVal #-}
instance ToJSVal Float where
toJSVal :: Float -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Float -> Double) -> Float -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac
{-# INLINE toJSVal #-}
instance ToJSVal Word where
toJSVal :: Word -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Word -> Double) -> Word -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Word8 where
toJSVal :: Word8 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Word8 -> Double) -> Word8 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Word16 where
toJSVal :: Word16 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Word16 -> Double) -> Word16 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Word32 where
toJSVal :: Word32 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Word32 -> Double) -> Word32 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Int where
toJSVal :: Int -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Int -> Double) -> Int -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Int8 where
toJSVal :: Int8 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Int8 -> Double) -> Int8 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Int16 where
toJSVal :: Int16 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Int16 -> Double) -> Int16 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
instance ToJSVal Int32 where
toJSVal :: Int32 -> JSM JSVal
toJSVal = Double -> JSM JSVal
numberToValue (Double -> JSM JSVal) -> (Int32 -> Double) -> Int32 -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toJSVal #-}
#endif
instance MakeArgs Double where
makeArgs :: Double -> JSM [JSVal]
makeArgs Double
n = Double -> JSM JSVal
valMakeNumber Double
n JSM JSVal -> (JSVal -> JSM [JSVal]) -> JSM [JSVal]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\JSVal
ref -> [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
ref])
valMakeText :: Text -> JSM JSVal
valMakeText :: Text -> JSM JSVal
valMakeText = JSString -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal (JSString -> JSM JSVal) -> (Text -> JSString) -> Text -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> JSString
textToJSString
{-# INLINE valMakeText #-}
valMakeString :: JSString -> JSM JSVal
valMakeString :: JSString -> JSM JSVal
valMakeString = JSString -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
{-# INLINE valMakeString #-}
#ifndef ghcjs_HOST_OS
instance ToJSVal Text where
toJSVal :: Text -> JSM JSVal
toJSVal = JSString -> JSM JSVal
stringToValue (JSString -> JSM JSVal) -> (Text -> JSString) -> Text -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> JSString
JSString
{-# INLINE toJSVal #-}
instance FromJSVal Text where
fromJSValUnchecked :: JSVal -> JSM Text
fromJSValUnchecked = JSVal -> JSM Text
forall value. ToJSVal value => value -> JSM Text
valToText
{-# INLINE fromJSValUnchecked #-}
fromJSVal :: JSVal -> JSM (Maybe Text)
fromJSVal = (Text -> Maybe Text) -> JSM Text -> JSM (Maybe Text)
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Maybe Text
forall a. a -> Maybe a
Just (JSM Text -> JSM (Maybe Text))
-> (JSVal -> JSM Text) -> JSVal -> JSM (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM Text
forall value. ToJSVal value => value -> JSM Text
valToText
{-# INLINE fromJSVal #-}
#endif
instance MakeArgs Text where
makeArgs :: Text -> JSM [JSVal]
makeArgs Text
t = Text -> JSM JSVal
valMakeText Text
t JSM JSVal -> (JSVal -> JSM [JSVal]) -> JSM [JSVal]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\JSVal
ref -> [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
ref])
#ifndef ghcjs_HOST_OS
instance ToJSVal JSString where
toJSVal :: JSString -> JSM JSVal
toJSVal = JSString -> JSM JSVal
stringToValue
{-# INLINE toJSVal #-}
instance FromJSVal JSString where
fromJSValUnchecked :: JSVal -> JSM JSString
fromJSValUnchecked = JSVal -> JSM JSString
forall value. ToJSVal value => value -> JSM JSString
valToStr
{-# INLINE fromJSValUnchecked #-}
fromJSVal :: JSVal -> JSM (Maybe JSString)
fromJSVal = (JSString -> Maybe JSString)
-> JSM JSString -> JSM (Maybe JSString)
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap JSString -> Maybe JSString
forall a. a -> Maybe a
Just (JSM JSString -> JSM (Maybe JSString))
-> (JSVal -> JSM JSString) -> JSVal -> JSM (Maybe JSString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM JSString
forall value. ToJSVal value => value -> JSM JSString
valToStr
{-# INLINE fromJSVal #-}
#endif
instance ToJSString JSString where
toJSString :: JSString -> JSString
toJSString = JSString -> JSString
forall a. a -> a
id
instance ToJSString Text where
toJSString :: Text -> JSString
toJSString = Text -> JSString
textToStr
instance ToJSString String where
toJSString :: String -> JSString
toJSString = Text -> JSString
textToStr (Text -> JSString) -> (String -> Text) -> String -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
instance FromJSString Text where
fromJSString :: JSString -> Text
fromJSString = JSString -> Text
strToText
instance FromJSString String where
fromJSString :: JSString -> String
fromJSString JSString
v = Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ JSString -> Text
strToText JSString
v
instance FromJSString JSString where
fromJSString :: JSString -> JSString
fromJSString = JSString -> JSString
forall a. a -> a
id
#ifndef ghcjs_HOST_OS
instance ToJSVal Char where
toJSVal :: Char -> JSM JSVal
toJSVal = Double -> JSM JSVal
valMakeNumber (Double -> JSM JSVal) -> (Char -> Double) -> Char -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> (Char -> Int) -> Char -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord
{-# INLINE toJSVal #-}
toJSValListOf :: String -> JSM JSVal
toJSValListOf = Text -> JSM JSVal
valMakeText (Text -> JSM JSVal) -> (String -> Text) -> String -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
{-# INLINE toJSValListOf #-}
instance FromJSVal Char where
fromJSValUnchecked :: JSVal -> JSM Char
fromJSValUnchecked = (Double -> Char) -> JSM Double -> JSM Char
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Char
chr (Int -> Char) -> (Double -> Int) -> Double -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round) (JSM Double -> JSM Char)
-> (JSVal -> JSM Double) -> JSVal -> JSM Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM Double
forall value. ToJSVal value => value -> JSM Double
valToNumber
{-# INLINE fromJSValUnchecked #-}
fromJSVal :: JSVal -> JSM (Maybe Char)
fromJSVal = (Double -> Maybe Char) -> JSM Double -> JSM (Maybe Char)
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char) -> (Double -> Char) -> Double -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Char
chr (Int -> Char) -> (Double -> Int) -> Double -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round) (JSM Double -> JSM (Maybe Char))
-> (JSVal -> JSM Double) -> JSVal -> JSM (Maybe Char)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM Double
forall value. ToJSVal value => value -> JSM Double
valToNumber
{-# INLINE fromJSVal #-}
fromJSValUncheckedListOf :: JSVal -> JSM String
fromJSValUncheckedListOf = (JSString -> String) -> JSM JSString -> JSM String
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> String
T.unpack (Text -> String) -> (JSString -> Text) -> JSString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSString -> Text
strToText) (JSM JSString -> JSM String)
-> (JSVal -> JSM JSString) -> JSVal -> JSM String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM JSString
forall value. ToJSVal value => value -> JSM JSString
valToStr
{-# INLINE fromJSValListOf #-}
fromJSValListOf :: JSVal -> JSM (Maybe String)
fromJSValListOf = (JSString -> Maybe String) -> JSM JSString -> JSM (Maybe String)
forall a b. (a -> b) -> JSM a -> JSM b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (JSString -> String) -> JSString -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack (Text -> String) -> (JSString -> Text) -> JSString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSString -> Text
strToText) (JSM JSString -> JSM (Maybe String))
-> (JSVal -> JSM JSString) -> JSVal -> JSM (Maybe String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSVal -> JSM JSString
forall value. ToJSVal value => value -> JSM JSString
valToStr
{-# INLINE fromJSValUncheckedListOf #-}
#endif
valMakeJSON :: Value -> JSM JSVal
valMakeJSON :: Value -> JSM JSVal
valMakeJSON = Value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
#ifndef ghcjs_HOST_OS
instance ToJSVal Value where
toJSVal :: Value -> JSM JSVal
toJSVal = Value -> JSM JSVal
jsonValueToValue
{-# INLINE toJSVal #-}
#endif
instance MakeArgs Value where
makeArgs :: Value -> JSM [JSVal]
makeArgs Value
t = Value -> JSM JSVal
valMakeJSON Value
t JSM JSVal -> (JSVal -> JSM [JSVal]) -> JSM [JSVal]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\JSVal
ref -> [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
ref])
deRefVal :: ToJSVal value => value -> JSM JSValue
#ifdef ghcjs_HOST_OS
deRefVal value = do
valref <- toJSVal value
case (jsrefGetType valref :: Int) of
0 -> return ValUndefined
1 -> return ValNull
2 -> ValBool <$> valToBool valref
3 -> ValNumber <$> valToNumber valref
4 -> ValString <$> valToText valref
5 -> ValObject <$> valToObject valref
_ -> error "Unexpected result dereferencing JSaddle value"
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1) => { return ($1 === undefined)?0:\
($1===null)?1:\
(typeof $1===\"boolean\")?2:\
(typeof $1===\"number\")?3:\
(typeof $1===\"string\")?4:\
(typeof $1===\"object\")?5:-1; })"
#else
"$r = ($1 === undefined)?0:\
($1===null)?1:\
(typeof $1===\"boolean\")?2:\
(typeof $1===\"number\")?3:\
(typeof $1===\"string\")?4:\
(typeof $1===\"object\")?5:-1;"
#endif
jsrefGetType :: JSVal -> Int
#else
deRefVal :: forall value. ToJSVal value => value -> JSM JSValue
deRefVal value
value = do
JSVal
v <- value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value
Result
result <- JSVal -> JSM Result
N.deRefVal JSVal
v
JSValue -> JSM JSValue
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (JSValue -> JSM JSValue) -> JSValue -> JSM JSValue
forall a b. (a -> b) -> a -> b
$ case Result
result of
DeRefValResult JSValueRef
0 Text
_ -> JSValue
ValNull
DeRefValResult JSValueRef
1 Text
_ -> JSValue
ValUndefined
DeRefValResult JSValueRef
2 Text
_ -> Bool -> JSValue
ValBool Bool
False
DeRefValResult JSValueRef
3 Text
_ -> Bool -> JSValue
ValBool Bool
True
DeRefValResult (-1) Text
s -> Double -> JSValue
ValNumber (String -> Double
forall a. Read a => String -> a
read (Text -> String
T.unpack Text
s))
DeRefValResult (-2) Text
s -> Text -> JSValue
ValString Text
s
DeRefValResult (-3) Text
_ -> Object -> JSValue
ValObject (JSVal -> Object
Object JSVal
v)
Result
_ -> String -> JSValue
forall a. HasCallStack => String -> a
error String
"Unexpected result dereferencing JSaddle value"
#endif
valMakeRef :: JSValue -> JSM JSVal
valMakeRef :: JSValue -> JSM JSVal
valMakeRef JSValue
value =
case JSValue
value of
JSValue
ValNull -> JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
valNull
JSValue
ValUndefined -> JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
valUndefined
ValBool Bool
b -> JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (JSVal -> JSM JSVal) -> JSVal -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ Bool -> JSVal
valBool Bool
b
ValNumber Double
n -> Double -> JSM JSVal
valMakeNumber Double
n
ValString Text
s -> Text -> JSM JSVal
valMakeText Text
s
ValObject (Object JSVal
o) -> JSVal -> JSM JSVal
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return JSVal
o
instance ToJSVal JSValue where
toJSVal :: JSValue -> JSM JSVal
toJSVal = JSValue -> JSM JSVal
valMakeRef
{-# INLINE toJSVal #-}
instance MakeArgs JSValue where
makeArgs :: JSValue -> JSM [JSVal]
makeArgs JSValue
v = JSValue -> JSM JSVal
valMakeRef JSValue
v JSM JSVal -> (JSVal -> JSM [JSVal]) -> JSM [JSVal]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\JSVal
ref -> [JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
ref])
#ifdef ghcjs_HOST_OS
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2) => { return $1===$2; })"
#else
"$1===$2"
#endif
jsvalueisstrictequal :: JSVal -> JSVal -> Bool
#endif
strictEqual :: (ToJSVal a, ToJSVal b) => a -> b -> JSM Bool
strictEqual :: forall a b. (ToJSVal a, ToJSVal b) => a -> b -> JSM Bool
strictEqual a
a b
b = do
JSVal
aval <- a -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal a
a
JSVal
bval <- b -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal b
b
#ifdef ghcjs_HOST_OS
return $ jsvalueisstrictequal aval bval
#else
JSVal -> JSVal -> JSM Bool
N.strictEqual JSVal
aval JSVal
bval
#endif
#ifdef ghcjs_HOST_OS
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2) => { return $1 instanceof $2; })"
#else
"$1 instanceof $2"
#endif
js_isInstanceOf :: JSVal -> Object -> Bool
#endif
instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool
instanceOf :: forall value constructor.
(ToJSVal value, MakeObject constructor) =>
value -> constructor -> JSM Bool
instanceOf value
value constructor
constructor = do
JSVal
v <- value -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal value
value
Object
c <- constructor -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject constructor
constructor
#ifdef ghcjs_HOST_OS
return $ js_isInstanceOf v c
#else
JSVal -> Object -> JSM Bool
N.instanceOf JSVal
v Object
c
#endif