繁体   English   中英

动态加载已编译的Haskell模块 - GHC 7.6

[英]Dynamically loading compiled Haskell module - GHC 7.6

我正在尝试使用GHC API动态编译和加载Haskell模块。 我理解API从一个版本到另一个版本的波动很大,所以我特别谈到GHC 7.6。*。

我试过在MacOS和Linux上运行相同的代码。 在这两种情况下,插件模块编译正常但在加载时出现以下错误: Cannot add module Plugin to context: not interpreted

问题类似于本文中的问题,只有在主机程序的同一运行中编译模块时才会加载该问题。

-- Host.hs: compile with ghc-7.6.*
-- $ ghc -package ghc -package ghc-paths Host.hs
-- Needs Plugin.hs in the same directory.
module Main where

import GHC
import GHC.Paths ( libdir )
import DynFlags
import Unsafe.Coerce

main :: IO ()
main =
    defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
      result <- runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "Plugin.hs" Nothing
        setTargets [target]
        r <- load LoadAllTargets
        case r of
          Failed    -> error "Compilation failed"
          Succeeded -> do
            setContext [IIModule (mkModuleName "Plugin")]
            result <- compileExpr ("Plugin.getInt")
            let result' = unsafeCoerce result :: Int
            return result'
      print result

和插件:

-- Plugin.hs
module Plugin where

getInt :: Int
getInt = 33

问题是你正在使用IIModule 这表示您希望将模块及其中的所有内容(包括未导出的内容)放入上下文中。 它基本上与以下相同:load在GHCi中:load星号。 正如您所注意到的,这只适用于解释代码,因为它让您“查看”模块。

但这不是你需要的。 你想要的是加载它,就像你使用:moduleimport声明,它与编译模块一起使用。 为此,您使用IIDecl这需要进口报关单,你可以做simpleImportDecl

setContext [IIDecl $ simpleImportDecl (mkModuleName "Plugin")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM