Safe Haskell | None |
---|---|
Language | Haskell2010 |
Deals with JavaScript values. These can be
- null
- undefined
- true | false
- a double precision floating point number
- a string
- an object
Synopsis
- data JSVal
- class ToJSVal a where
- data JSNull = JSNull
- type JSUndefined = ()
- data JSString
- data JSValue
- showJSValue :: JSValue -> String
- isTruthy :: JSVal -> GHCJSPure Bool
- valToBool :: ToJSVal value => value -> JSM Bool
- valToNumber :: ToJSVal value => value -> JSM Double
- valToStr :: ToJSVal value => value -> JSM JSString
- valToObject :: ToJSVal value => value -> JSM Object
- valToText :: ToJSVal value => value -> JSM Text
- valToJSON :: ToJSVal value => value -> JSM JSString
- val :: ToJSVal value => value -> JSM JSVal
- jsNull :: JSVal
- valNull :: JSVal
- isNull :: JSVal -> GHCJSPure Bool
- valIsNull :: ToJSVal value => value -> JSM Bool
- jsUndefined :: JSVal
- valUndefined :: JSVal
- isUndefined :: JSVal -> GHCJSPure Bool
- valIsUndefined :: ToJSVal value => value -> JSM Bool
- maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal)
- maybeNullOrUndefined' :: ToJSVal value => (JSVal -> JSM a) -> value -> JSM (Maybe a)
- toJSBool :: Bool -> JSVal
- jsTrue :: JSVal
- jsFalse :: JSVal
- valBool :: Bool -> JSVal
- valMakeNumber :: Double -> JSM JSVal
- valMakeString :: JSString -> JSM JSVal
- valMakeText :: Text -> JSM JSVal
- valMakeJSON :: Value -> JSM JSVal
- deRefVal :: ToJSVal value => value -> JSM JSValue
- valMakeRef :: JSValue -> JSM JSVal
- strictEqual :: (ToJSVal a, ToJSVal b) => a -> b -> JSM Bool
- instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool
JavaScript value references
Instances
NFData JSVal Source # | |
Defined in GHCJS.Prim.Internal | |
MakeArgs JSVal Source # | A single JSVal can be used as the argument list |
MakeArgs JSCallAsFunction Source # | |
Defined in Language.Javascript.JSaddle.Object | |
MakeObject JSVal Source # | |
Defined in Language.Javascript.JSaddle.Value | |
FromJSVal JSVal Source # | |
ToJSVal JSVal Source # | If we already have a JSVal we are fine |
ToJSVal JSCallAsFunction Source # | A callback to Haskell can be used as a JavaScript value. This will create
an anonymous JavaScript function object. Use |
Defined in Language.Javascript.JSaddle.Object toJSVal :: JSCallAsFunction -> JSM JSVal Source # toJSValListOf :: [JSCallAsFunction] -> JSM JSVal Source # | |
PFromJSVal JSVal Source # | |
Defined in GHCJS.Marshal.Pure pFromJSVal :: JSVal -> JSVal Source # | |
PToJSVal JSVal Source # | |
class ToJSVal a where Source #
Nothing
toJSVal :: a -> JSM JSVal Source #
toJSValListOf :: [a] -> JSM JSVal Source #
toJSVal :: (Generic a, GToJSVal (Rep a ())) => a -> JSM JSVal Source #
Instances
Haskell types for JavaScript values
JSNull | Type that represents a value that can only be null. Haskell of course has no null so we are adding this type. |
type JSUndefined Source #
= () | A type that can only be undefined in JavaScript. Using ()
because functions in JavaScript that have no return, impicitly
return undefined.
type JSBool = Bool -- ^ JavaScript boolean values map the |
A wrapper around a JavaScript string
Instances
An algebraic data type that can represent a JavaScript value. Any JavaScriptCore
JSVal
can be converted into this type.
ValNull | null |
ValUndefined | undefined |
ValBool Bool | true or false |
ValNumber Double | a number |
ValString Text | a string |
ValObject Object | an object |
showJSValue :: JSValue -> String Source #
Show a JSValue but just say "object" if the value is a JavaScript object.
Converting JavaScript values
valToBool :: ToJSVal value => value -> JSM Bool Source #
Given a JavaScript value get its boolean value. All values in JavaScript convert to bool.
>>>
testJSaddle $ valToBool JSNull
false>>>
testJSaddle $ valToBool ()
false>>>
testJSaddle $ valToBool True
true>>>
testJSaddle $ valToBool False
false>>>
testJSaddle $ valToBool (1.0 :: Double)
true>>>
testJSaddle $ valToBool (0.0 :: Double)
false>>>
testJSaddle $ valToBool ""
false>>>
testJSaddle $ valToBool "1"
true
valToNumber :: ToJSVal value => value -> JSM Double Source #
Given a JavaScript value get its numeric value. May throw JSException.
>>>
testJSaddle $ show <$> valToNumber JSNull
0.0>>>
testJSaddle $ show <$> valToNumber ()
NaN>>>
testJSaddle $ show <$> valToNumber True
1.0>>>
testJSaddle $ show <$> valToNumber False
0.0>>>
testJSaddle $ show <$> valToNumber (1.0 :: Double)
1.0>>>
testJSaddle $ show <$> valToNumber (0.0 :: Double)
0.0>>>
testJSaddle $ show <$> valToNumber ""
0.0>>>
testJSaddle $ show <$> valToNumber "1"
1.0
valToStr :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get its string value (as a JavaScript string). May throw JSException.
>>>
testJSaddle $ strToText <$> valToStr JSNull
null>>>
testJSaddle $ strToText <$> valToStr ()
undefined>>>
testJSaddle $ strToText <$> valToStr True
true>>>
testJSaddle $ strToText <$> valToStr False
false>>>
testJSaddle $ strToText <$> valToStr (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToStr (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToStr ""
>>>
testJSaddle $ strToText <$> valToStr "1"
1
valToObject :: ToJSVal value => value -> JSM Object Source #
Given a JavaScript value get its object value. May throw JSException.
>>>
testJSaddle $ (valToObject JSNull >>= valToText) `catch` \ (JSException e) -> valToText e
null>>>
testJSaddle $ (valToObject () >>= valToText) `catch` \ (JSException e) -> valToText e
undefined>>>
testJSaddle $ valToObject True
true>>>
testJSaddle $ valToObject False
false>>>
testJSaddle $ valToObject (1.0 :: Double)
1>>>
testJSaddle $ valToObject (0.0 :: Double)
0>>>
testJSaddle $ valToObject ""
>>>
testJSaddle $ valToObject "1"
1
valToText :: ToJSVal value => value -> JSM Text Source #
Given a JavaScript value get its string value (as a Haskell Text
).
May throw JSException.
>>>
testJSaddle $ show <$> valToText JSNull
"null">>>
testJSaddle $ show <$> valToText ()
"undefined">>>
testJSaddle $ show <$> valToText True
"true">>>
testJSaddle $ show <$> valToText False
"false">>>
testJSaddle $ show <$> valToText (1.0 :: Double)
"1">>>
testJSaddle $ show <$> valToText (0.0 :: Double)
"0">>>
testJSaddle $ show <$> valToText ""
"">>>
testJSaddle $ show <$> valToText "1"
"1"
valToJSON :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get a JSON string value. May throw JSException.
>>>
testJSaddle $ strToText <$> valToJSON JSNull
null>>>
testJSaddle $ strToText <$> valToJSON ()
>>>
testJSaddle $ strToText <$> valToJSON True
true>>>
testJSaddle $ strToText <$> valToJSON False
false>>>
testJSaddle $ strToText <$> valToJSON (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToJSON (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToJSON ""
"">>>
testJSaddle $ strToText <$> valToJSON "1"
"1">>>
testJSaddle $ strToText <$> (obj >>= valToJSON)
{}
Make JavaScript values from Haskell ones
Convert to a JavaScript value (just an alias for toJSVal
)
valIsNull :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is null
jsUndefined :: JSVal Source #
valUndefined :: JSVal Source #
An undefined
JavaScript value
valIsUndefined :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is undefined
maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal) Source #
Convert a JSVal to a Maybe JSVal (converting null and undefined to Nothing)
Convert to and from JSValue
deRefVal :: ToJSVal value => value -> JSM JSValue Source #
Derefernce a value reference.
>>>
testJSaddle $ showJSValue <$> deRefVal JSNull
null>>>
testJSaddle $ showJSValue <$> deRefVal ()
undefined>>>
testJSaddle $ showJSValue <$> deRefVal True
true>>>
testJSaddle $ showJSValue <$> deRefVal False
false>>>
testJSaddle $ showJSValue <$> deRefVal (1.0 :: Double)
1.0>>>
testJSaddle $ showJSValue <$> deRefVal (0.0 :: Double)
0.0>>>
testJSaddle $ showJSValue <$> deRefVal ""
"">>>
testJSaddle $ showJSValue <$> deRefVal "1"
"1">>>
testJSaddle $ showJSValue <$> (valToObject True >>= deRefVal)
true>>>
testJSaddle $ showJSValue <$> (obj >>= deRefVal)
object
valMakeRef :: JSValue -> JSM JSVal Source #
Make a JavaScript value out of a JSValue
ADT.
>>>
testJSaddle $ valMakeRef ValNull
null>>>
testJSaddle $ valMakeRef ValUndefined
undefined>>>
testJSaddle $ valMakeRef (ValBool True)
true>>>
testJSaddle $ valMakeRef (ValNumber 1)
1>>>
testJSaddle $ valMakeRef (ValString $ T.pack "Hello")
Hello
instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool Source #