Safe Haskell | None |
---|---|
Language | Haskell2010 |
JavaScript.Extras.Cast
Documentation
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
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 # | |
ToJS Char Source # | Char instance converts to string |
ToJS Double Source # | |
ToJS Float Source # | |
ToJS Int Source # | |
ToJS Int8 Source # | |
ToJS Int16 Source # | |
ToJS Int32 Source # | |
ToJS Word Source # | |
ToJS Word8 Source # | |
ToJS Word16 Source # | |
ToJS Word32 Source # | |
ToJS String Source # | |
ToJS Text Source # | |
ToJS Object Source # | |
ToJS JSString Source # | |
ToJS JSVal Source # | |
ToJS JSRep Source # | |
ToJS a => ToJS (Maybe a) Source # | |
ToJS (Nullable a) Source # | |
ToJS (Callback a) Source # | |
ToJS (Export a) Source # | |
ToJS (SomeJSArray m) Source # | |
Defined in JavaScript.Extras.Cast Methods toJS :: SomeJSArray m -> JSVal 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, 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
Instances
FromJS Bool Source # | |
FromJS Char Source # | This will only succeed on a single character string |
FromJS Double Source # | |
FromJS Float Source # | |
FromJS Int Source # | |
FromJS Int8 Source # | |
FromJS Int16 Source # | |
FromJS Int32 Source # | |
FromJS Word Source # | |
FromJS Word8 Source # | |
FromJS Word16 Source # | |
FromJS Word32 Source # | |
FromJS String Source # | |
FromJS Text Source # | |
FromJS Object Source # | |
FromJS JSString Source # | |
FromJS JSVal Source # | |
FromJS JSRep Source # | |
FromJS (SomeJSArray m) Source # | |
Defined in JavaScript.Extras.Cast |