4

I'll use a Famo.us example as the starting point:

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({...}));

now I have these GHCJS FFI 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 inline JavaScript code, I always need to refer to things like famous.core.FOO using fully qualified names instead of just FOO like one normally would.

here's the equivalent Haskell snippet using these FFI bindings (hidden behind 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

And here's the question: is it possible to make GHCJS auto-generate e.g. var Engine = famous.core.Engine and var Surface = famous.core.Surface imports in the outputted JavaScript whenever an FFI binding gets used that depends on it? Or are my only options to either always use fully qualified names in my inline JS or to always just manually import a name per every FFI binding?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111

0 Answers0