GHC API를 사용하여 Haskell 모듈을 동적으로 컴파일하고로드하려고합니다. API가 한 버전에서 다른 버전으로 상당히 변동한다는 것을 이해합니다. 따라서 GHC 7.6. *에 대해 구체적으로 말하고 있습니다.동적으로 컴파일 된 하스켈 모듈로드 - GHC 7.6
MacOS 및 Linux에서 동일한 코드를 실행 해 보았습니다. 두 경우 모두 Plugin 모듈은 정상적으로 컴파일되지만로드시 다음 오류를 제공합니다.
this의 문제는 모듈이 호스트 프로그램의 동일한 실행에서 컴파일 된 경우에만로드됩니다.
-- 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
그리고 플러그인 : 당신이
IIModule
을 사용하고 있는지
-- Plugin.hs
module Plugin where
getInt :: Int
getInt = 33