
Hi, I was wondering whether Yhc was able to provide dynamic loading of modules. Let's say I'd wanted to do something like : myFiboFunction <- loadModule "myPlugin.hbc" "myFiboFunction" putStrlLn $ myFiboFunction 10 Is it possible to achieve this in the current state of Yhc ? I know there's an API out there to load bytecodes at runtime but I'm not sure it's meant to do that... I'm aware of the hs-plugins library for GHC that allows this kind of hot plugging. Since Yhc is built around the idea of bytecode I figured it should come with built-in dynamic loading facilities. Regards, Cedric

Hi Cedric,
I was wondering whether Yhc was able to provide dynamic loading of modules.
Let's say I'd wanted to do something like :
myFiboFunction <- loadModule "myPlugin.hbc" "myFiboFunction" putStrlLn $ myFiboFunction 10
I can't see any reason why not, and in fact I think you might even be able to implement it in Haskell using Tom's low level API for querying code plus the Robert's API for playing with bytecode files. That would be interesting to see. Of course, Tom's more likely to know for certain. You'd probably also want some kind of: module <- loadModule "myPlugin.hbc" func <- queryFunction module module And you'd also have to worry about type safety, but its certainly feasible I think Thanks Neil

On Sun, 2006-09-17 at 18:31 +0200, Cédric Paternotte wrote:
Hi,
I was wondering whether Yhc was able to provide dynamic loading of modules.
Let's say I'd wanted to do something like :
myFiboFunction <- loadModule "myPlugin.hbc" "myFiboFunction" putStrlLn $ myFiboFunction 10
Is it possible to achieve this in the current state of Yhc ? I know there's an API out there to load bytecodes at runtime but I'm not sure it's meant to do that...
Hi Cedric, There is indeed a RuntimeAPI, though it's still in the process of being written so it's a bit low level. At somepoint we'll write a user-friendly library over the top but at present your example would be: import YHC.Runtime.API main = do let typ = undefined :: Int -> Int fibo <- loadFromModule "MyPlugin" "myFiboFunction" typ putStrLn $ show $ fibo 10 loadFromModule :: String -> String -> a -> IO a loadFromModule mod obj typ = do pluginModule <- moduleLoad mod case pluginModule of Nothing -> error $ "couldn't load module '"++mod++"'" Just m -> do fiboNode <- moduleLookupNode m obj case fiboNode of Nothing -> error $ "could not find '"++obj++ "' in module '"++mod++"'" Just n -> do typRep <- getType typ withNode n typRep $ \ fibo -> return fibo Notice that it's necessary to give a dummy parameter of the correct type to keep the type system happy. Also be wary that the system currently doesn't check that the type is correct so doing: ... let typ = undefined :: Float -> Char .. Will compile and even execute, but will prompty crash horribly. In future we'll add type checking so it'll give a sensible runtime error. Hope that helps :-) Tom

shackell:
On Sun, 2006-09-17 at 18:31 +0200, C??dric Paternotte wrote:
Hi,
I was wondering whether Yhc was able to provide dynamic loading of modules.
Let's say I'd wanted to do something like :
myFiboFunction <- loadModule "myPlugin.hbc" "myFiboFunction" putStrlLn $ myFiboFunction 10
Is it possible to achieve this in the current state of Yhc ? I know there's an API out there to load bytecodes at runtime but I'm not sure it's meant to do that...
Hi Cedric,
There is indeed a RuntimeAPI, though it's still in the process of being written so it's a bit low level. At somepoint we'll write a user-friendly library over the top but at present your example would be:
import YHC.Runtime.API
main = do let typ = undefined :: Int -> Int fibo <- loadFromModule "MyPlugin" "myFiboFunction" typ putStrLn $ show $ fibo 10
loadFromModule :: String -> String -> a -> IO a loadFromModule mod obj typ = do pluginModule <- moduleLoad mod case pluginModule of Nothing -> error $ "couldn't load module '"++mod++"'" Just m -> do fiboNode <- moduleLookupNode m obj case fiboNode of Nothing -> error $ "could not find '"++obj++ "' in module '"++mod++"'" Just n -> do typRep <- getType typ withNode n typRep $ \ fibo -> return fibo
Notice that it's necessary to give a dummy parameter of the correct type to keep the type system happy. Also be wary that the system currently doesn't check that the type is correct so doing:
... let typ = undefined :: Float -> Char ..
Will compile and even execute, but will prompty crash horribly. In future we'll add type checking so it'll give a sensible runtime error.
Hope that helps :-)
Hmm, from here it wouldn't be too hard I think to add eval :: String -> IO a would it? eval s = do writeFile "tmp.hs" s yhc "-c" "tmp.hs" -- run the compiler, get a .o bytecode file (?) loadFromModule "tmp.o" .. or even a nicer interface might exist? Tom? /me has visions of fast bytecode eval for lambdabot. -- Don

Hi Don,
Hmm, from here it wouldn't be too hard I think to add eval :: String -> IO a would it?
eval s = do writeFile "tmp.hs" s yhc "-c" "tmp.hs" -- run the compiler, get a .o bytecode file (?) loadFromModule "tmp.o" ..
/s/.o/.hbc/ Yeah, thats a relatively trivial thing to do. The nicer way would be to dump the .hi file and .hbc file to memory using the Yhc API - unfortunately thats not yet possible, and would probably be quite a lot of work. Thanks Neil
participants (4)
-
Cédric Paternotte
-
dons@cse.unsw.edu.au
-
Neil Mitchell
-
Tom Shackell