@@ -15,13 +15,12 @@ module Foreign.Lua.Module.System
1515where
1616
1717import Control.Applicative ((<$>) )
18- import Control.Exception (IOException , try )
1918import Control.Monad (forM_ )
2019import Control.Monad.Catch (bracket )
2120import Data.Maybe (fromMaybe )
2221import Data.Version (versionBranch )
23- import Foreign.Lua (Lua , NumResults (.. ), Optional (.. ), Peekable , Pushable ,
24- StackIndex , ToHaskellFunction )
22+ import Foreign.Lua (Lua , NumResults (.. ), Optional (.. ))
23+ import Foreign.Lua.Module.SystemUtils
2524
2625import qualified Data.Map as Map
2726import qualified Foreign.Lua as Lua
@@ -56,52 +55,6 @@ pushModule = do
5655preloadModule :: String -> Lua ()
5756preloadModule = flip addPackagePreloader pushModule
5857
59- -- | Registers a preloading function. Takes an module name and the Lua
60- -- operation which produces the package.
61- addPackagePreloader :: String -> Lua NumResults -> Lua ()
62- addPackagePreloader name modulePusher = do
63- Lua. getfield Lua. registryindex Lua. preloadTableRegistryField
64- Lua. pushHaskellFunction modulePusher
65- Lua. setfield (- 2 ) name
66- Lua. pop 1
67-
68- addField :: Pushable a => String -> a -> Lua ()
69- addField name value = do
70- Lua. push name
71- Lua. push value
72- Lua. rawset (Lua. nthFromTop 3 )
73-
74- -- | Attach a function to the table at the top of the stack, using the
75- -- given name.
76- addFunction :: ToHaskellFunction a => String -> a -> Lua ()
77- addFunction name fn = do
78- Lua. push name
79- Lua. pushHaskellFunction fn
80- Lua. rawset (- 3 )
81-
82- -- | Lua callback function
83- newtype Callback = Callback StackIndex
84-
85- instance Peekable Callback where
86- peek idx = do
87- isFn <- Lua. isfunction idx
88- if isFn
89- then return (Callback idx)
90- else Lua. throwException " Function expected"
91-
92- instance Pushable Callback where
93- push (Callback idx) = Lua. pushvalue idx
94-
95-
96- -- | Any value of unknown type
97- newtype AnyValue = AnyValue { fromAnyValue :: StackIndex }
98-
99- instance Peekable AnyValue where
100- peek = return . AnyValue
101-
102- instance Pushable AnyValue where
103- push (AnyValue idx) = Lua. pushvalue idx
104-
10558-- | Run an action, then restore the old environment variable values.
10659with_env :: Map. Map String String -> Callback -> Lua NumResults
10760with_env environment callback =
@@ -134,26 +87,6 @@ with_tmpdir parentDir tmpl callback =
13487 tmpl' <- Lua. peek (fromAnyValue tmpl)
13588 Temp. withTempDirectory parentDir tmpl' (invokeWithFilePath callback')
13689
137- invoke :: Callback -> Lua NumResults
138- invoke callback = do
139- oldTop <- Lua. gettop
140- Lua. push callback
141- Lua. call 0 Lua. multret
142- newTop <- Lua. gettop
143- return . NumResults . fromIntegral . Lua. fromStackIndex $
144- newTop - oldTop
145-
146- -- | Call Lua callback function with the given filename as its argument.
147- invokeWithFilePath :: Callback -> FilePath -> Lua NumResults
148- invokeWithFilePath callback filename = do
149- oldTop <- Lua. gettop
150- Lua. push callback
151- Lua. push filename
152- Lua. call (Lua. NumArgs 1 ) Lua. multret
153- newTop <- Lua. gettop
154- return . NumResults . fromIntegral . Lua. fromStackIndex $
155- newTop - oldTop
156-
15790-- | List the contents of a directory.
15891ls :: Optional FilePath -> Lua [FilePath ]
15992ls fp = do
@@ -207,11 +140,3 @@ setenv name value = ioToLua (Env.setEnv name value)
207140-- | Get the current directory for temporary files.
208141tmpdirname :: Lua FilePath
209142tmpdirname = ioToLua Directory. getTemporaryDirectory
210-
211- -- | Convert a System IO operation to a Lua operation.
212- ioToLua :: IO a -> Lua a
213- ioToLua action = do
214- result <- Lua. liftIO (try action)
215- case result of
216- Right result' -> return result'
217- Left err -> Lua. throwException (show (err :: IOException ))
0 commit comments