threepenny-gui- GUI framework that uses the web browser as a display.

Safe HaskellNone





A JavaScript foreign function interface (FFI).

This module implements a web server that communicates with a web browser and allows you to execute arbitrary JavaScript code on it.

Note: This module is used internally by the Graphics.UI.Threepenny library, but the types are not compatible. Use Foreign.JavaScript only if you want to roll your own interface to the web browser.


serve Source


:: Config

Configuration options.

-> (Window -> IO ())

Initialization whenever a client connects.

-> IO () 

Run a Foreign.JavaScript server.

data Config Source

Configuration of a Foreign.JavaScript server.




jsPort :: Maybe Int

Port number. Nothing means that the port number is read from the environment variable PORT. Alternatively, port 8023 is used if this variable is not set.

jsAddr :: Maybe ByteString

Bind address. Nothing means that the bind address is read from the environment variable ADDR. Alternatively, address is used if this variable is not set.

jsCustomHTML :: Maybe FilePath

Custom HTML file to replace the default one.

jsStatic :: Maybe FilePath

Directory that is served under /static.

jsLog :: ByteString -> IO ()

Print a single log message.

defaultConfig :: Config Source

Default configuration.

Port from environment variable or 8023, listening on localhost, no custom HTML, no static directory, logging to stderr.

data Window Source

Representation of a browser window.

root :: Window -> RemotePtr () Source

For the purpose of controlling garbage collection, every Window as an associated RemotePtr that is alive as long as the external JavaScript connection is alive.

JavaScript FFI

class ToJS a where Source

Helper class for rendering Haskell values as JavaScript expressions.

Minimal complete definition



render :: a -> IO JSCode Source

renderList :: [a] -> IO JSCode Source

class FromJS a Source

Helper class for converting JavaScript values to Haskell values.

Minimal complete definition


data JSFunction a Source

A JavaScript function with a given output type a.


Functor JSFunction

Change the output type of a JSFunction.

FromJS b => FFI (JSFunction b) 

type JSObject = RemotePtr JSPtr Source

A mutable JavaScript object.

class FFI a Source

Helper class for making ffi a variable argument function.

Minimal complete definition



FromJS b => FFI (JSFunction b) 
(ToJS a, FFI b) => FFI (a -> b) 

ffi :: FFI a => String -> a Source

Simple JavaScript FFI with string substitution.

Inspired by the Fay language.

example :: String -> Int -> JSFunction String
example = ffi "$(%1).prop('checked',%2)"

The ffi function takes a string argument representing the JavaScript code to be executed on the client. Occurrences of the substrings %1 to %9 will be replaced by subequent arguments.

Note: Always specify a type signature! The types automate how values are marshalled between Haskell and JavaScript. The class instances for the FFI class show which conversions are supported.

runFunction :: Window -> JSFunction () -> IO () Source

Run a JavaScript function, but do not wait for a result.

callFunction :: Window -> JSFunction a -> IO a Source

Call a JavaScript function and wait for the result.

class IsHandler a Source

Helper class for exporting Haskell functions to JavaScript as event handlers.

Minimal complete definition

convertArgs, handle


IsHandler (IO ()) 
(FromJS a, IsHandler b) => IsHandler (a -> b) 

exportHandler :: IsHandler a => Window -> a -> IO JSObject Source

Export a Haskell function as an event handler.

The result is a JavaScript Function object that can be called from JavaScript like a regular function. However, the corresponding Haskell function will not be run immediately, rather it will be added to the event queue and processed like an event. In other words, this the Haskell code is only called asynchronously.

WARNING: The event handler will be garbage collected unless you keep a reference to it on the Haskell side! Registering it with a JavaScript function will generally not keep it alive.

onDisconnect :: Window -> IO () -> IO () Source

Register an action to be performed when the client disconnects.

debug :: Window -> String -> IO () Source

Send a debug message to the JavaScript console.