packages feed

hotswap (empty) → 0.1.8.0

raw patch · 4 files changed

+98/−0 lines, 4 filesdep +basedep +pluginssetup-changed

Dependencies added: base, plugins

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Mike Ledger++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Mike Ledger nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hotswap.cabal view
@@ -0,0 +1,23 @@+-- Initial hotswap.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++name:                hotswap+version:             0.1.8.0+synopsis:            Simple code hotswapping.+description:         A simple, high-level interface to plugins.+homepage:            https://github.com/mikeplus64/hotswap+category:            System+license:             BSD3+license-file:        LICENSE+author:              Mike Ledger+maintainer:          eleventynine@gmail.com+-- copyright:           +-- category:            +build-type:          Simple+cabal-version:       >=1.8++library+  hs-source-dirs:      src+  exposed-modules:     System.Plugins.Hotswap+  -- other-modules:       +  build-depends:       base ==4.5.*, plugins ==1.5.*
+ src/System/Plugins/Hotswap.hs view
@@ -0,0 +1,43 @@+module System.Plugins.Hotswap (newPlugin, Plugin (..), usePlugin, reloadPlugin) where++import System.Plugins.Load+import Data.IORef ++data Plugin a = Plugin {+    pluginObject       :: FilePath,    -- ^ Path to object+    pluginIncludes     :: [FilePath],  -- ^ Include paths.+    pluginFunctionName :: String,      -- ^ Name of the symbol to find.+    pluginFunction     :: IORef a,     -- ^ Loaded function.+    pluginModule       :: IORef Module -- ^ Loaded module.+}++-- | 'usePlugin' provides a simple way to use plugins of type 'Plugin (a -> b)', ie, only +-- single argumented ones.+usePlugin :: Plugin (a -> b) -> a -> IO b+usePlugin p x = fmap ($ x) $ readIORef $ pluginFunction p++-- | Create a new plugin. Don't use this to reload plugins.+newPlugin :: FilePath -> [FilePath] -> String -> IO (Plugin a)+newPlugin obj incs name = do+    plugin <- load_ obj incs name++    case plugin of++        LoadSuccess m f -> do+            fref <- newIORef f+            mref <- newIORef m+            return $! Plugin obj incs name fref mref++        _ -> error "no such module or function"++-- | Reload a plugin in-place.+reloadPlugin :: Plugin a -> IO ()+reloadPlugin (Plugin _ _ name fref mref) = do +    m <- readIORef mref+    plugin <- reload m name+    case plugin of+        LoadSuccess newm newf -> do+            writeIORef mref newm+            writeIORef fref newf+        _ -> error "no such module or function"+