Safe Haskell | None |
---|---|
Language | Haskell98 |
High level JavaScript foreign interface.
- class ToAny a where
- class FromAny a where
- data JSAny
- data Opaque a
- toOpaque :: a -> Opaque a
- fromOpaque :: Opaque a -> a
- nullValue :: JSAny
- toObject :: [(JSString, JSAny)] -> JSAny
- has :: JSAny -> JSString -> IO Bool
- get :: FromAny a => JSAny -> JSString -> IO a
- index :: FromAny a => JSAny -> Int -> IO a
- class FFI a
- class JSFunc a
- ffi :: FFI a => JSString -> a
- constant :: FromAny a => JSString -> a
- export :: ToAny a => JSString -> a -> IO ()
Conversion to/from JSAny
Any type that can be converted into a JavaScript value.
Nothing
Build a JS object from a Haskell value.
The default instance creates an object from any type that derives
Generic
according to the following rules:
* Records turn into plain JS objects, with record names as field names.
* Non-record product types turn into objects containing a $data
field
which contains all of the constructor's unnamed fields.
* Values of enum types turn into strings matching their constructors.
* Non-enum types with more than one constructor gain an extra field,
$tag
, which contains the name of the constructor used to create the
object.
ToAny Bool | |
ToAny Char | |
ToAny Double | |
ToAny Float | |
ToAny Int | |
ToAny Int8 | |
ToAny Int16 | |
ToAny Int32 | |
ToAny Word | |
ToAny Word8 | |
ToAny Word16 | |
ToAny Word32 | |
ToAny () | |
ToAny JSString | |
ToAny JSAny | |
ToAny Elem | |
ToAny Blob | |
ToAny BlobData | |
ToAny FrameRequest | |
ToAny Canvas | |
ToAny Ctx | |
ToAny Bitmap | |
ToAny WebSocket | |
ToAny a => ToAny [a] | Lists are marshalled into arrays, with the exception of |
ToAny a => ToAny (IO a) | |
ToAny (Ptr a) | |
ToAny a => ToAny (Maybe a) | Maybe is simply a nullable type. Nothing is equivalent to null, and any non-null value is equivalent to x in Just x. |
ToAny (Opaque a) | |
(FromAny a, JSFunc b) => ToAny (a -> b) | |
(ToAny a, ToAny b) => ToAny (a, b) | Tuples are marshalled into arrays. |
(ToAny a, ToAny b, ToAny c) => ToAny (a, b, c) | |
(ToAny a, ToAny b, ToAny c, ToAny d) => ToAny (a, b, c, d) | |
(ToAny a, ToAny b, ToAny c, ToAny d, ToAny e) => ToAny (a, b, c, d, e) | |
(ToAny a, ToAny b, ToAny c, ToAny d, ToAny e, ToAny f) => ToAny (a, b, c, d, e, f) | |
(ToAny a, ToAny b, ToAny c, ToAny d, ToAny e, ToAny f, ToAny g) => ToAny (a, b, c, d, e, f, g) |
Any type that can be converted from a JavaScript value.
Nothing
fromAny :: JSAny -> IO a Source
Convert a value from JS with a reasonable conversion if an exact match is not possible. Examples of reasonable conversions would be truncating floating point numbers to integers, or turning signed integers into unsigned.
listFromAny :: JSAny -> IO [a] Source
FromAny Bool | |
FromAny Char | |
FromAny Double | |
FromAny Float | |
FromAny Int | |
FromAny Int8 | |
FromAny Int16 | |
FromAny Int32 | |
FromAny Word | |
FromAny Word8 | |
FromAny Word16 | |
FromAny Word32 | |
FromAny () | |
FFI a => FromAny a | |
FromAny JSString | |
FromAny JSAny | |
FromAny Elem | |
FromAny Blob | |
FromAny BlobData | |
FromAny MouseButton | |
FromAny FrameRequest | |
FromAny Type | |
FromAny Canvas | |
FromAny Ctx | |
FromAny Bitmap | |
FromAny WebSocket | |
FromAny a => FromAny [a] | |
FromAny (Ptr a) | |
FromAny a => FromAny (Maybe a) | |
FromAny (Opaque a) | |
(FromAny a, FromAny b) => FromAny (a, b) | |
(FromAny a, FromAny b, FromAny c) => FromAny (a, b, c) | |
(FromAny a, FromAny b, FromAny c, FromAny d) => FromAny (a, b, c, d) | |
(FromAny a, FromAny b, FromAny c, FromAny d, FromAny e) => FromAny (a, b, c, d, e) | |
(FromAny a, FromAny b, FromAny c, FromAny d, FromAny e, FromAny f) => FromAny (a, b, c, d, e, f) | |
(FromAny a, FromAny b, FromAny c, FromAny d, FromAny e, FromAny f, FromAny g) => FromAny (a, b, c, d, e, f, g) |
The Opaque type is inhabited by values that can be passed to JavaScript using their raw Haskell representation. Opaque values are completely useless to JS code, and should not be inspected. This is useful for, for instance, storing data in some JS-native data structure for later retrieval.
fromOpaque :: Opaque a -> a Source
toObject :: [(JSString, JSAny)] -> JSAny Source
Build a new JS object from a list of key:value pairs.
get :: FromAny a => JSAny -> JSString -> IO a Source
Read a member from a JS object. Throws an error if the member can not be
marshalled into a value of type a
.
index :: FromAny a => JSAny -> Int -> IO a Source
Read an element from a JS array. Throws an error if the member can not be
marshalled into a value of type a
.
Importing and exporting JavaScript functions
mkJSFunc, arity
ffi :: FFI a => JSString -> a Source
Creates a Haskell function from the given string of JavaScript code. If this code is not well typed or is otherwise incorrect, your program may crash or misbehave in mystifying ways. Haste makes a best-effort try to save you from poorly typed JS here, but there are no guarantees.
For instance, the following WILL cause crazy behavior due to wrong types:
ffi "(function(x) {return x+1;})" :: Int -> Int -> IO Int
In other words, this function is as unsafe as the JS it calls on. You have been warned.
The imported JS is evaluated lazily, unless (a) it is a function object in which case evaluation order does not affect the semantics of the imported code, or if (b) the imported code is explicitly marked as strict:
someFunction = ffi "__strict(someJSFunction)"
Literals which depends on some third party initialization, the existence of a DOM tree or some other condition which is not fulfilled at load time should *not* be marked strict.
export :: ToAny a => JSString -> a -> IO () Source
Export a symbol. That symbol may then be accessed from JavaScript through Haste.name() as a normal function. Remember, however, that if you are using --with-js to include your JS, in conjunction with --opt-minify or any option that implies it, you will instead need to access your exports through Haste['name'](), or Closure will mangle your function names.