4

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

4

0 回答 0