Running JavaScript import / request from FFI GHCJS bindings

As a starting point, I will use the Famo.us example:

var Engine = require("famous/core/Engine");  // or just var Engine = famous.core.Engine;
var Surface = require("famous/core/Surface");

var ctx = Engine.createContext();
ctx.add(new Surface({...}));

I now have FFI GHCJS bindings:

foreign import javascript unsafe "$r = famous.core.Engine;"
  engine :: Engine

foreign import javascript safe "$r = ($1).createContext();"
  fms_Engine_createContext0 :: Engine -> IO Context

foreign import javascript safe "$r = new famous.core.Surface($1);"
  fms_Surface_new :: JSRef Surface -> IO (JSRef Surface)

foreign import javascript safe "$r = $2.add($1)"
  fms_Context_add :: JSRef a -> Context -> IO RenderNode

- as you can see, in the built-in JavaScript code, I always need to refer to such things as famous.core.FOOusing full names, and not just FOOas usual.

here's the equivalent Haskell snippet using these FFI bindings (hidden for high-level wrappers for clarity):

main = do
  -- calls fms_Engine_createContext0 under the hood
  ctx <- createContext engine
  -- calls fms_Surface_new under the hood
  sf <- surface [ SfSize       $ XY 200 200
                , SfContent    $ content
                , SfProperties $ fromList [ ("backgroundColor", "rgb(240, 238, 233)")
                                          , ("textAlign"      , "center") ] ]
  -- calls fms_Context_add under the hood
  sf `addToContext` ctx

: GHCJS, . var Engine = famous.core.Engine var Surface = famous.core.Surface JavaScript , FFI , ? JS FFI?

+4

All Articles