Safe Haskell | None |
---|
Deals with JavaScript values. These can be
- null
- undefined
- true | false
- a double precision floating point number
- a string
- an object
- type JSValueRef = Ptr OpaqueJSValue
- class MakeValueRef a where
- makeValueRef :: a -> JSC JSValueRef
- data JSNull = JSNull
- type JSUndefined = ()
- type JSBool = Bool
- type JSNumber = Double
- type JSString = Text
- data JSValue
- valToBool :: MakeValueRef val => val -> JSC JSBool
- valToNumber :: MakeValueRef val => val -> JSC JSNumber
- valToStr :: MakeValueRef val => val -> JSC JSStringRef
- valToObject :: MakeValueRef val => val -> JSC JSObjectRef
- valToText :: MakeValueRef val => val -> JSC Text
- val :: MakeValueRef value => value -> JSC JSValueRef
- valMakeNull :: JSC JSValueRef
- valMakeUndefined :: JSC JSValueRef
- valMakeBool :: JSBool -> JSC JSValueRef
- valMakeNumber :: JSNumber -> JSC JSValueRef
- valMakeString :: Text -> JSC JSValueRef
- deRefVal :: MakeValueRef val => val -> JSC JSValue
- valMakeRef :: JSValue -> JSC JSValueRef
JavaScript value references
type JSValueRef = Ptr OpaqueJSValue
class MakeValueRef a where
Anything that can be used to make a JavaScript value reference
makeValueRef :: a -> JSC JSValueRef
MakeValueRef Bool | Make a JavaScript boolean value |
MakeValueRef Double | Makes a JavaScript number |
MakeValueRef String | Makes a JavaScript string |
MakeValueRef Text | Makes a JavaScript string |
MakeValueRef JSValueRef | If we already have a JSValueRef we are fine |
MakeValueRef JSPropRef | We can use a property as a value. |
MakeValueRef JSValue | Makes a JavaScript value from a |
MakeValueRef JSUndefined | Makes an |
MakeValueRef JSNull | Makes a |
MakeValueRef JSCallAsFunction | A callback to Haskell can be used as a JavaScript value. This will create
an anonymous JavaScript function object. Use |
MakeValueRef v => MakeValueRef (JSC v) | JSValueRef can be made by evaluating a function in |
Haskell types for JavaScript values
data JSNull
JSNull | Type that represents a value that can only be null. Haskell of course has no null so we are adding this type. |
MakeObjectRef JSNull | |
MakeArgRefs JSNull | Makes an argument list with just a single |
MakeValueRef JSNull | Makes a |
type JSUndefined
= () | 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 |
type JSNumber
= Double | A number in JavaScript maps nicely to |
type JSString
= Text | JavaScript strings can be represented with the Haskell |
data JSValue
An algebraic data type that can represent a JavaScript value. Any JavaScriptCore
JSValueRef
can be converted into this type.
ValNull | null |
ValUndefined | undefined |
ValBool JSBool | true or false |
ValNumber JSNumber | a number |
ValString JSString | a string |
ValObject JSObjectRef | an object |
Eq JSValue | |
Show JSValue | |
MakeArgRefs JSValue | Makes an argument list with just a single JavaScript value from a |
MakeValueRef JSValue | Makes a JavaScript value from a |
Converting JavaScript values
valToBool :: MakeValueRef val => val -> JSC JSBool
Given a JavaScript value get its boolean value. All values in JavaScript convert to bool.
>>>
testJSC $ valToBool JSNull
false>>>
testJSC $ valToBool ()
false>>>
testJSC $ valToBool True
true>>>
testJSC $ valToBool False
false>>>
testJSC $ valToBool (1.0 :: Double)
true>>>
testJSC $ valToBool (0.0 :: Double)
false>>>
testJSC $ valToBool ""
false>>>
testJSC $ valToBool "1"
true
valToNumber :: MakeValueRef val => val -> JSC JSNumber
Given a JavaScript value get its numeric value. May throw JSException.
>>>
testJSC $ show <$> valToNumber JSNull
0.0>>>
testJSC $ show <$> valToNumber ()
NaN>>>
testJSC $ show <$> valToNumber True
1.0>>>
testJSC $ show <$> valToNumber False
0.0>>>
testJSC $ show <$> valToNumber (1.0 :: Double)
1.0>>>
testJSC $ show <$> valToNumber (0.0 :: Double)
0.0>>>
testJSC $ show <$> valToNumber ""
0.0>>>
testJSC $ show <$> valToNumber "1"
1.0
valToStr :: MakeValueRef val => val -> JSC JSStringRef
Given a JavaScript value get its string value (as a JavaScript string). May throw JSException.
>>>
testJSC $ valToStr JSNull >>= strToText
null>>>
testJSC $ valToStr () >>= strToText
undefined>>>
testJSC $ valToStr True >>= strToText
true>>>
testJSC $ valToStr False >>= strToText
false>>>
testJSC $ valToStr (1.0 :: Double) >>= strToText
1>>>
testJSC $ valToStr (0.0 :: Double) >>= strToText
0>>>
testJSC $ valToStr "" >>= strToText
>>>
testJSC $ valToStr "1" >>= strToText
1
valToObject :: MakeValueRef val => val -> JSC JSObjectRef
Given a JavaScript value get its object value. May throw JSException.
>>>
testJSC $ (valToObject JSNull >>= valToText) `catch` \ (JSException e) -> valToText e
TypeError: 'null' is not an object>>>
testJSC $ (valToObject () >>= valToText) `catch` \ (JSException e) -> valToText e
TypeError: 'undefined' is not an object>>>
testJSC $ valToObject True
true>>>
testJSC $ valToObject False
false>>>
testJSC $ valToObject (1.0 :: Double)
1>>>
testJSC $ valToObject (0.0 :: Double)
0>>>
testJSC $ valToObject ""
>>>
testJSC $ valToObject "1"
1
valToText :: MakeValueRef val => val -> JSC Text
Given a JavaScript value get its string value (as a Haskell Text
).
May throw JSException.
>>>
testJSC $ show <$> valToText JSNull
"null">>>
testJSC $ show <$> valToText ()
"undefined">>>
testJSC $ show <$> valToText True
"true">>>
testJSC $ show <$> valToText False
"false">>>
testJSC $ show <$> valToText (1.0 :: Double)
"1">>>
testJSC $ show <$> valToText (0.0 :: Double)
"0">>>
testJSC $ show <$> valToText ""
"">>>
testJSC $ show <$> valToText "1"
"1"
Make JavaScript values from Haskell ones
:: MakeValueRef value | |
=> value | value to convert to a JavaScript value |
-> JSC JSValueRef |
Convert to a JavaScript value (just an alias for makeValueRef
)
Make a null
JavaScript value
valMakeUndefined :: JSC JSValueRef
Make an undefined
JavaScript value
valMakeBool :: JSBool -> JSC JSValueRef
Make a JavaScript boolean value
valMakeNumber :: JSNumber -> JSC JSValueRef
Make a JavaScript number
valMakeString :: Text -> JSC JSValueRef
Make a JavaScript string
Conver to and from JSValue
deRefVal :: MakeValueRef val => val -> JSC JSValue
Derefernce a value reference.
>>>
testJSC $ show <$> deRefVal JSNull
ValNull>>>
testJSC $ show <$> deRefVal ()
ValUndefined>>>
testJSC $ show <$> deRefVal True
ValBool True>>>
testJSC $ show <$> deRefVal False
ValBool False>>>
testJSC $ show <$> deRefVal (1.0 :: Double)
ValNumber 1.0>>>
testJSC $ show <$> deRefVal (0.0 :: Double)
ValNumber 0.0>>>
testJSC $ show <$> deRefVal ""
ValString "">>>
testJSC $ show <$> deRefVal "1"
ValString "1">>>
testJSC $ show <$> valToObject True >>= deRefVal
ValObject 0x...
valMakeRef :: JSValue -> JSC JSValueRef
Make a JavaScript value out of a JSValue
ADT.
>>>
testJSC $ valMakeRef ValNull
"null">>>
testJSC $ valMakeRef ValUndefined
"undefined">>>
testJSC $ valMakeRef (ValBool True)
"true">>>
testJSC $ valMakeRef (ValNumber 1)
"1">>>
testJSC $ valMakeRef (ValString $ pack "Hello")
"Hello"