我有一个现有的 Haskell 函数,它使用 GHC API 从模块中动态加载已编译的代码。它基于博文Dynamic Compilation and Loading of Modules in Haskell 中的代码。
该代码在 GHC 7.0 中运行良好,但必须稍作修改才能在 GHC 7.2 中编译,因为 GHC API 发生了变化。
该代码现在在 GHC 7.2 中引发运行时错误:
mkTopLevEnv: not a home module (module name):(function name)
代码是
evalfuncLoadFFI String moduleName,
String externalFuncName,
String internalFuncName = do
result <- liftIO $ defaultRunGhc $ do
dynflags <- GHC.getSessionDynFlags
_ <- GHC.setSessionDynFlags dynflags
m <- GHC.findModule (GHC.mkModuleName moduleName) Nothing
--------------------------------------------------------
-- The following code works fine in GHC 7.0.4:
--
-- GHC.setContext [] [(m, Nothing)]
--
-- This new code attempts to set context to the module,
-- but throws an error in GHC 7.2:
--
(_,oi) <- GHC.getContext
GHC.setContext [m] oi
--------------------------------------------------------
fetched <- GHC.compileExpr (moduleName ++ "." ++ externalFuncName)
return (Unsafe.Coerce.unsafeCoerce fetched :: [LispVal] -> IOThrowsError LispVal)
defineVar env internalFuncName (IOFunc result)
作为参考,完整的代码可在FFI.hs (github.com)中在线获取。
有谁知道如何解决或解决这个问题?
另外,这可能是由 GHC 7.2 中的新 Safe Haskell 更改引起的,还是仅仅是由于对 GHC API 的修改?