javascript-extras-0.3.2.0: Extra javascript functions when using GHCJS

Safe HaskellNone
LanguageHaskell2010

JavaScript.Extras.Cast

Synopsis

Documentation

class ToJS a where #

This provides a consistent way to convert to JSVal, with different semantics for Char. In the Char's instance of ToJS, it converts to a string instead of integer - IMHO this is less surprising.

The other reason for this class is while GHCJS base provide both IsJSVal and PToJSVal to convert to jsval, some types are instances of one or the other class. This means you can't use the "Maybe a" instance of PToJSVal if it contains IsISJVal but not pToJSVal.

Methods

toJS :: a -> JSVal #

This is a pure conversion, so instances must be able to convert the same or equivalent JSVal each time.

toJS :: IsJSVal a => a -> JSVal #

This is a pure conversion, so instances must be able to convert the same or equivalent JSVal each time.

Instances

ToJS Bool # 

Methods

toJS :: Bool -> JSVal #

ToJS Char #

Char instance converts to string

Methods

toJS :: Char -> JSVal #

ToJS Double # 

Methods

toJS :: Double -> JSVal #

ToJS Float # 

Methods

toJS :: Float -> JSVal #

ToJS Int # 

Methods

toJS :: Int -> JSVal #

ToJS Int8 # 

Methods

toJS :: Int8 -> JSVal #

ToJS Int16 # 

Methods

toJS :: Int16 -> JSVal #

ToJS Int32 # 

Methods

toJS :: Int32 -> JSVal #

ToJS Word # 

Methods

toJS :: Word -> JSVal #

ToJS Word8 # 

Methods

toJS :: Word8 -> JSVal #

ToJS Word16 # 

Methods

toJS :: Word16 -> JSVal #

ToJS Word32 # 

Methods

toJS :: Word32 -> JSVal #

ToJS String # 

Methods

toJS :: String -> JSVal #

ToJS Text # 

Methods

toJS :: Text -> JSVal #

ToJS Object # 

Methods

toJS :: Object -> JSVal #

ToJS JSString # 

Methods

toJS :: JSString -> JSVal #

ToJS JSVal # 

Methods

toJS :: JSVal -> JSVal #

ToJS JSVar # 

Methods

toJS :: JSVar -> JSVal #

ToJS a => ToJS (Maybe a) # 

Methods

toJS :: Maybe a -> JSVal #

ToJS (Nullable a) # 

Methods

toJS :: Nullable a -> JSVal #

ToJS (Callback a) # 

Methods

toJS :: Callback a -> JSVal #

ToJS (Export a) # 

Methods

toJS :: Export a -> JSVal #

ToJS (SomeJSArray * m) # 

Methods

toJS :: SomeJSArray * m -> JSVal #

class FromJS a where #

This provides a consistent way to safely convert from JSVal. The semantics is that if the return value is a Just, then the JSVal is not a null value. Also, Nothing is also returned for values out of range. They are not silently truncated. (Except for Float where there may be loss of precision) during conversion.

The reason for this class is because GHCJS.Marshal.fromJSVal and GHCJS.Marshal.pFromJSVal are not safe to use as it assumes that the JSVal are of the correct type and not null. (https:/github.comghcjsghcjs-baseissues/87). The safe way to convert from JSVal is to use JavaScript.Cast or to use the 'Maybe a' instance of FromJSVal, ie fromJSVal :: JSVal -> IO (Maybe (Maybe a)), which is a bit more awkward to use, and requires IO. Also, Javascript.Cast doesn't have much instances, and it hardcodes the instance detection method to javascript isinstance which is not sufficient for complex types (https:/github.comghcjsghcjs-baseissues/86).

It is actually safe to convert from JSVal without IO because every JSVal is a copy of a value or reference. The copy never change, so the conversion will always convert to the same result/object every time.

Minimal complete definition

fromJS

Methods

fromJS :: JSVal -> Maybe a #

Instances

FromJS Bool # 

Methods

fromJS :: JSVal -> Maybe Bool #

FromJS Char #

This will only succeed on a single character string

Methods

fromJS :: JSVal -> Maybe Char #

FromJS Double # 

Methods

fromJS :: JSVal -> Maybe Double #

FromJS Float # 

Methods

fromJS :: JSVal -> Maybe Float #

FromJS Int # 

Methods

fromJS :: JSVal -> Maybe Int #

FromJS Int8 # 

Methods

fromJS :: JSVal -> Maybe Int8 #

FromJS Int16 # 

Methods

fromJS :: JSVal -> Maybe Int16 #

FromJS Int32 # 

Methods

fromJS :: JSVal -> Maybe Int32 #

FromJS Word # 

Methods

fromJS :: JSVal -> Maybe Word #

FromJS Word8 # 

Methods

fromJS :: JSVal -> Maybe Word8 #

FromJS Word16 # 

Methods

fromJS :: JSVal -> Maybe Word16 #

FromJS Word32 # 

Methods

fromJS :: JSVal -> Maybe Word32 #

FromJS String # 

Methods

fromJS :: JSVal -> Maybe String #

FromJS Text # 

Methods

fromJS :: JSVal -> Maybe Text #

FromJS Object # 

Methods

fromJS :: JSVal -> Maybe Object #

FromJS JSString # 

Methods

fromJS :: JSVal -> Maybe JSString #

FromJS JSVal # 

Methods

fromJS :: JSVal -> Maybe JSVal #

FromJS JSVar # 

Methods

fromJS :: JSVal -> Maybe JSVar #

FromJS (SomeJSArray * m) # 

Methods

fromJS :: JSVal -> Maybe (SomeJSArray * m) #