Safe Haskell | None |
---|---|
Language | Haskell98 |
Haste.Foreign
Description
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 ()
- safe_ffi :: FFI a => StaticPtr JSString -> a
- data StaticPtr a :: * -> *
Conversion to/from JSAny
Any type that can be converted into a JavaScript value.
Minimal complete definition
Nothing
Methods
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.
Instances
Any type that can be converted from a JavaScript value.
Minimal complete definition
Nothing
Methods
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.
The default instance is the inverse of the default ToAny
instance.
listFromAny :: JSAny -> IO [a] Source
Instances
Any JS value, with one layer of indirection.
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
Minimal complete definition
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.
data StaticPtr a :: * -> *
A reference to a value of type a
.