Instantly share code, notes, and snippets.

Embed
What would you like to do?
{-# 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",_) -> "_"
_ -> ""
module Plugin(f) where
f :: String
f = "Monads are just monoids in the category of endofunctors, what’s the problem?"
@homam

This comment has been minimized.

homam commented Jun 18, 2017

Main Post: https://purelyfunctional.org/posts/2016-05-20-dynamic-loading-haskell-module.html

Compile with ghc -package ghc -rdynamic main.hs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment