Seamless calls of Haskell code from JavaScript modules
Go to file
2016-06-18 14:50:51 -03:00
example-react-serverside-rendering Get hello world to working state 2016-06-18 12:38:10 -03:00
examples Add failure handling example & implementation 2016-06-18 13:13:27 -03:00
ghcjs-commonjs Remove containers dependency 2016-06-18 13:13:47 -03:00
ghcjs-loader Patches 2016-06-16 20:12:17 -03:00
ghcjs-register Patches 2016-06-16 20:12:17 -03:00
ghcjs-require Get hello world to working state 2016-06-18 12:38:10 -03:00
old-examples Move examples 2016-06-16 22:43:24 -03:00
.gitignore Add .gitignore 2016-06-16 17:05:38 -03:00
README.md Update README 2016-06-18 14:50:51 -03:00

ghcjs-commonjs

ghcjs-commonjs is a work-in-progress collection of little hacks to make GHCJS and CommonJS integration less troublesome.

By that we mean:

  • Easy implementation of calling Haskell code from JavaScript code
  • Easy implementation of calling JavaScript code from Haskell

The first point is where the most friction is, since the GHCJS FFI is pretty stellar.

We provide the following tools:

  • ghcjs-require - Calling Haskell from Node.js and CommonJS
  • ghcjs-register - require('./MyHaskellModule.hs')
  • ghcjs-loader - A webpack loader for GHCJS

ghcjs-require

Utilities for loading GHCJS .jsexes from CommonJS land.

Callling Haskell from JavaScript

This is main.js:

const ghcjsRequire = require('ghcjs-require');
const Main = ghcjsRequire('./Main.jsexe');
// ^^ This is a function that boots the Haskell RTS
Main(({wrapped}) => { // <- This callback is executed after the RTS is loaded
  wrapped.someFunction().then(() => console.log('someFunction is over'));
  // ^^ This function was generated, it'll call Haskell code asynchronously and
  //    return a promise to the result (the result needs to be a JavaScript
  //    value)
});

This is Main.jsexe:

import Control.Concurrency (threadDelay)
import GHCJS.CommonJS (defaultMain, export)
someFunction = do
    putStrLn "Waiting for a second"
    threadDelay (1000 * 1000)
    putStrLn "Done!"
main =
    exportMain [("someFunction", someFunction)]

ghcjs-register

On the likes of coffee-script/register or babel-register:

require('ghcjs-register');
const Main = require('./Main.hs');

ghcjs-loader

This is a webpack loader for GHCJS. See the examples.