从GHCJS FFI绑定触发JavaScript import / require

时间:2015-05-14 12:03:45

标签: javascript haskell cross-compiling ffi ghcjs

我将使用Famo.us示例作为起点:

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

现在我有这些GHCJS FFI绑定:

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

- 如您所见,在内联JavaScript代码中,我总是需要使用完全限定名称来引用famous.core.FOO之类的内容,而不是像通常那样只引用FOO

这是使用这些FFI绑定的等效Haskell片段(为了清晰起见隐藏在高级包装器后面):

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自动生成,例如每当使用依赖于FFI绑定的FFI绑定时,输出的JavaScript中会var Engine = famous.core.Enginevar Surface = famous.core.Surface导入?或者我是唯一的选择,要么总是在我的内联JS中使用完全限定名称,要么总是只为每个FFI绑定手动导入一个名称?

0 个答案:

没有答案