From c07bc5eda0b885dd00174cddfd9027a44b428d09 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 22 Jul 2020 16:06:47 +0100 Subject: [PATCH] getting shit to build --- IRC/Connection.hs | 10 +++++----- IRC/Plugins/Loader.hs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/IRC/Connection.hs b/IRC/Connection.hs index 314585c..66479b4 100644 --- a/IRC/Connection.hs +++ b/IRC/Connection.hs @@ -4,9 +4,9 @@ import Data.Word (Word16) import qualified Network.Socket as S -data Connection = Connection { connSocket :: S.Socket, - connNick :: String, - } +--data Connection = Connection { connSocket :: S.Socket, +-- connNick :: String, +-- } -- The host to connect to. type Host = String @@ -14,5 +14,5 @@ type Host = String -- The port to connect to. type Port = Word16 -connect :: Host -> Port -connect = error "unimplemented" +--connect :: Host -> Port +--connect = error "unimplemented" diff --git a/IRC/Plugins/Loader.hs b/IRC/Plugins/Loader.hs index 5a3de6d..2e1ee60 100644 --- a/IRC/Plugins/Loader.hs +++ b/IRC/Plugins/Loader.hs @@ -1,4 +1,36 @@ +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE UnboxedTuples #-} + module IRC.Plugins.Loader where -test :: Int -test = 1 +import GHC.Exts ( addrToAny# ) +import GHC.Ptr ( Ptr(..) ) +import System.Info ( os, arch ) +import GHCi.ObjLink +import Encoding + +load :: IO (Either String (IO ())) +load = do initObjLinker RetainCAFs + loadObj "Plugin.o" + _ret <- resolveObjs + ptr <- lookupSymbol (mangleSymbol Nothing "Plugin" "f") + case ptr of + Nothing -> return $ Left "Couldn’t load symbol" + Just (Ptr addr) -> case addrToAny# addr of + (# f #) -> return $ Right f + +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",_) -> "_" + _ -> ""