| 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 #
This is a pure conversion, so instances must be able to convert the same or equivalent JSVal each time.
Instances
| ToJS Bool # | |
| ToJS Char # | Char instance converts to string |
| ToJS Double # | |
| ToJS Float # | |
| ToJS Int # | |
| ToJS Int8 # | |
| ToJS Int16 # | |
| ToJS Int32 # | |
| ToJS Word # | |
| ToJS Word8 # | |
| ToJS Word16 # | |
| ToJS Word32 # | |
| ToJS String # | |
| ToJS Text # | |
| ToJS Object # | |
| ToJS JSString # | |
| ToJS JSVal # | |
| ToJS JSVar # | |
| ToJS a => ToJS (Maybe a) # | |
| ToJS (Nullable a) # | |
| ToJS (Callback a) # | |
| ToJS (Export a) # | |
| ToJS (SomeJSArray * m) # | |
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 # | |
| FromJS Char # | This will only succeed on a single character string |
| FromJS Double # | |
| FromJS Float # | |
| FromJS Int # | |
| FromJS Int8 # | |
| FromJS Int16 # | |
| FromJS Int32 # | |
| FromJS Word # | |
| FromJS Word8 # | |
| FromJS Word16 # | |
| FromJS Word32 # | |
| FromJS String # | |
| FromJS Text # | |
| FromJS Object # | |
| FromJS JSString # | |
| FromJS JSVal # | |
| FromJS JSVar # | |
| FromJS (SomeJSArray * m) # | |