Próbuję dynamicznie kompilować i ładować moduły Haskell za pomocą interfejsu API GHC. Rozumiem, że interfejs API różni się w zależności od wersji, więc mówię konkretnie o GHC 7.6. *.Dynamiczne ładowanie skompilowanego modułu Haskell - GHC 7.6
Próbowałem uruchomić ten sam kod na MacOS i Linux. W obu przypadkach moduł Plugin kompiluje się poprawnie, ale podaje następujący błąd: Cannot add module Plugin to context: not interpreted
Problem jest podobny do tego w this, gdzie moduł zostałby załadowany tylko wtedy, gdy został skompilowany w tym samym przebiegu programu hosta.
-- 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
A plugin:
-- Plugin.hs
module Plugin where
getInt :: Int
getInt = 33