Created
May 20, 2016 17:59
-
-
Save cocreature/2e3ca5d921d08f8e0704b19b7dd186a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MagicHash, UnboxedTuples #-} | |
module Main(main) where | |
import GHC.Exts ( addrToAny# ) | |
import GHC.Ptr ( Ptr(..) ) | |
import System.Info ( os, arch ) | |
import Encoding | |
import ObjLink | |
main :: IO () | |
main = do initObjLinker | |
loadObj "plugin.o" | |
_ret <- resolveObjs | |
ptr <- lookupSymbol (mangleSymbol Nothing "Plugin" "f") | |
case ptr of | |
Nothing -> putStrLn "Couldn’t load symbol" | |
Just (Ptr addr) -> case addrToAny# addr of | |
(# hval #) -> putStrLn hval | |
mangleSymbol :: Maybe String -> String -> String -> String | |
mangleSymbol pkg module' valsym = | |
prefixUnderscore ++ | |
maybe "" (\p -> zEncodeString p ++ "_") pkg ++ | |
zEncodeString module' ++ "_" ++ zEncodeString valsym ++ "_closure" | |
prefixUnderscore :: String | |
prefixUnderscore = | |
case (os,arch) of | |
("mingw32","x86_64") -> "" | |
("cygwin","x86_64") -> "" | |
("mingw32",_) -> "_" | |
("darwin",_) -> "_" | |
("cygwin",_) -> "_" | |
_ -> "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Plugin(f) where | |
f :: String | |
f = "Monads are just monoids in the category of endofunctors, what’s the problem?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main Post: https://purelyfunctional.org/posts/2016-05-20-dynamic-loading-haskell-module.html
Compile with
ghc -package ghc -rdynamic main.hs