Compare commits

...

2 Commits

Author SHA1 Message Date
Tiago Carvalho
d2378812ce bye 2020-07-29 23:15:22 +01:00
Tiago Carvalho
8df4670d80 loading code progress 2020-07-28 14:09:19 +01:00
6 changed files with 36 additions and 23 deletions

View File

@ -5,6 +5,8 @@ module IRC.Plugins.Loader ( load ) where
import qualified Data.HashMap.Lazy as M import qualified Data.HashMap.Lazy as M
import IRC.Plugins.Plugin
import Control.Exception ( catch, ErrorCall ) import Control.Exception ( catch, ErrorCall )
import GHC.Exts ( addrToAny# ) import GHC.Exts ( addrToAny# )
import GHC.Ptr ( Ptr(..) ) import GHC.Ptr ( Ptr(..) )
@ -12,13 +14,9 @@ import System.Info ( os, arch )
import GHCi.ObjLink import GHCi.ObjLink
import Encoding import Encoding
type PluginSet = M.HashMap String Plugin load :: PluginSet -> String -> IO (Either String PluginSet)
load set path = do
data Plugin = Plugin { wew :: Int, initObjLinker RetainCAFs
lad :: Int }
load :: PluginSet -> IO (Either String (IO PluginSet))
load = do initObjLinker RetainCAFs
loadObj "Plugin.o" loadObj "Plugin.o"
_ret <- resolveObjs _ret <- resolveObjs
ptr <- lookupSymbol (mangleSymbol Nothing "Plugin" "f") ptr <- lookupSymbol (mangleSymbol Nothing "Plugin" "f")
@ -27,11 +25,15 @@ load = do initObjLinker RetainCAFs
Just (Ptr addr) -> case addrToAny# addr of Just (Ptr addr) -> case addrToAny# addr of
(# f #) -> return $ Right f (# f #) -> return $ Right f
unload :: String -> IO PluginSet unload :: PluginSet -> String -> IO PluginSet
unload plugin = catch (unloadObj $ plugin <> ".o") exceptionHandler unload set plugin = catch unloadPlugin exceptionHandler
where exceptionHandler :: ErrorCall -> IO PluginSet
exceptionHandler _ = return set
exceptionHandler :: ErrorCall -> IO PluginSet unloadPlugin :: IO PluginSet
exceptionHandler _ = return () unloadPlugin = do
unloadObj $ plugin <> ".o"
return $ M.delete plugin set
mangleSymbol :: Maybe String -> String -> String -> String mangleSymbol :: Maybe String -> String -> String -> String
mangleSymbol pkg module' valsym = mangleSymbol pkg module' valsym =

8
IRC/Plugins/Plugin.hs Normal file
View File

@ -0,0 +1,8 @@
module IRC.Plugins.Plugin ( PluginSet, Plugin(..) ) where
import qualified Data.HashMap.Lazy as M
type PluginSet = M.HashMap String Plugin
data Plugin = Plugin { pluginEvent :: String, -- e.g. PRIVMSG
pluginEntry :: Int }

View File

@ -1,3 +1,4 @@
include_rules include_rules
: foreach *.hs |> ghc $(GHCFLAGS) -c %f |> %B.o %B.hi : Plugin.hs |> ghc $(GHCFLAGS) -c %f |> %B.o %B.hi
: Loader.hs | Plugin.hi |> ghc $(GHCFLAGS) -c %f |> %B.o %B.hi

View File

@ -1,4 +1,4 @@
include_rules include_rules
: foreach *.hs | IRC/*.hi |> ghc $(GHCFLAGS) -c %f |> %B.o %B.hi : foreach *.hs | IRC/*.hi IRC/Plugins/*.hi |> ghc $(GHCFLAGS) -c %f |> %B.o %B.hi
: *.o IRC/*.o |> ghc -o Main %f |> Main : *.o IRC/*.o IRC/Plugins/*.o |> ghc -o Main %f |> Main

View File

@ -1 +1 @@
GHCFLAGS += -Wall -O3 GHCFLAGS += -i$(CWD) -Wall -O3

View File

@ -9,6 +9,8 @@ if ! command -v tup >/dev/null; then
exit 1 exit 1
fi fi
export CWD="$(readlink -f .)"
case $1 in case $1 in
run) run)
$0 build >&2 || exit 1 $0 build >&2 || exit 1
@ -22,7 +24,7 @@ case $1 in
tup >&2 tup >&2
;; ;;
clean) clean)
rm -f Main *.o *.hi IRC/*.o IRC/*.hi rm -f Main *.o *.hi IRC/*.o IRC/*.hi IRC/Plugins/*.o IRC/Plugins/*.hi
;; ;;
*) *)
usage usage