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

Safe HaskellNone
LanguageHaskell2010

JavaScript.Extras.Recast

Synopsis

Documentation

class ToJS a where Source #

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 Source #

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

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

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

Instances

ToJS Bool Source # 

Methods

toJS :: Bool -> JSVal Source #

ToJS Char Source #

Char instance converts to string

Methods

toJS :: Char -> JSVal Source #

ToJS Double Source # 

Methods

toJS :: Double -> JSVal Source #

ToJS Float Source # 

Methods

toJS :: Float -> JSVal Source #

ToJS Int Source # 

Methods

toJS :: Int -> JSVal Source #

ToJS Int8 Source # 

Methods

toJS :: Int8 -> JSVal Source #

ToJS Int16 Source # 

Methods

toJS :: Int16 -> JSVal Source #

ToJS Int32 Source # 

Methods

toJS :: Int32 -> JSVal Source #

ToJS Word Source # 

Methods

toJS :: Word -> JSVal Source #

ToJS Word8 Source # 

Methods

toJS :: Word8 -> JSVal Source #

ToJS Word16 Source # 

Methods

toJS :: Word16 -> JSVal Source #

ToJS Word32 Source # 

Methods

toJS :: Word32 -> JSVal Source #

ToJS Text Source # 

Methods

toJS :: Text -> JSVal Source #

ToJS Object Source # 

Methods

toJS :: Object -> JSVal Source #

ToJS JSString Source # 

Methods

toJS :: JSString -> JSVal Source #

ToJS JSVal Source # 

Methods

toJS :: JSVal -> JSVal Source #

ToJS [Char] Source # 

Methods

toJS :: [Char] -> JSVal Source #

ToJS a => ToJS (Maybe a) Source # 

Methods

toJS :: Maybe a -> JSVal Source #

ToJS (Nullable a) Source # 

Methods

toJS :: Nullable a -> JSVal Source #

ToJS (Export a) Source # 

Methods

toJS :: Export a -> JSVal Source #

ToJS (Callback a) Source # 

Methods

toJS :: Callback a -> JSVal Source #

ToJS (SomeJSArray * m) Source # 

Methods

toJS :: SomeJSArray * m -> JSVal Source #

class FromJS a where Source #

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. 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).

Minimal complete definition

fromJS

Methods

fromJS :: JSVal -> IO (Maybe a) Source #

This is an IO because since JSVal is mutable, this function may different results for the same JSVal at later points in time.