Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 8a96e0a

Browse files
committed
Move helper function to internal SystemUtils module
1 parent 5f6abc9 commit 8a96e0a

3 files changed

Lines changed: 106 additions & 77 deletions

File tree

hslua-module-system.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ library
3535
default-extensions: LambdaCase
3636
default-language: Haskell2010
3737
exposed-modules: Foreign.Lua.Module.System
38+
other-modules: Foreign.Lua.Module.SystemUtils
3839
hs-source-dirs: src
3940
other-extensions: OverloadedStrings
4041

src/Foreign/Lua/Module/System.hs

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ module Foreign.Lua.Module.System
1515
where
1616

1717
import Control.Applicative ((<$>))
18-
import Control.Exception (IOException, try)
1918
import Control.Monad (forM_)
2019
import Control.Monad.Catch (bracket)
2120
import Data.Maybe (fromMaybe)
2221
import 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

2625
import qualified Data.Map as Map
2726
import qualified Foreign.Lua as Lua
@@ -56,52 +55,6 @@ pushModule = do
5655
preloadModule :: String -> Lua ()
5756
preloadModule = 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.
10659
with_env :: Map.Map String String -> Callback -> Lua NumResults
10760
with_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.
15891
ls :: Optional FilePath -> Lua [FilePath]
15992
ls fp = do
@@ -207,11 +140,3 @@ setenv name value = ioToLua (Env.setEnv name value)
207140
-- | Get the current directory for temporary files.
208141
tmpdirname :: Lua FilePath
209142
tmpdirname = 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))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{-|
2+
Module : Foreign.Lua.Module.SystemUtils
3+
Copyright : © 2019 Albert Krewinkel
4+
License : MIT
5+
Maintainer : Albert Krewinkel <albert+hslua@zeitkraut.de>
6+
Stability : alpha
7+
Portability : Requires GHC 8 or later.
8+
9+
Utility functions and types for HsLua's system module.
10+
-}
11+
module Foreign.Lua.Module.SystemUtils
12+
( AnyValue (..)
13+
, Callback (..)
14+
, addPackagePreloader
15+
, addField
16+
, addFunction
17+
, invoke
18+
, invokeWithFilePath
19+
, ioToLua
20+
)
21+
where
22+
23+
import Control.Exception (IOException, try)
24+
import Foreign.Lua (Lua, NumResults(..), Peekable, Pushable,
25+
StackIndex, ToHaskellFunction)
26+
27+
import qualified Foreign.Lua as Lua
28+
29+
-- | Registers a preloading function. Takes an module name and the Lua
30+
-- operation which produces the package.
31+
addPackagePreloader :: String -> Lua NumResults -> Lua ()
32+
addPackagePreloader name modulePusher = do
33+
Lua.getfield Lua.registryindex Lua.preloadTableRegistryField
34+
Lua.pushHaskellFunction modulePusher
35+
Lua.setfield (-2) name
36+
Lua.pop 1
37+
38+
-- | Add a string-indexed field to the table at the top of the stack.
39+
addField :: Pushable a => String -> a -> Lua ()
40+
addField name value = do
41+
Lua.push name
42+
Lua.push value
43+
Lua.rawset (Lua.nthFromTop 3)
44+
45+
-- | Attach a function to the table at the top of the stack, using the
46+
-- given name.
47+
addFunction :: ToHaskellFunction a => String -> a -> Lua ()
48+
addFunction name fn = do
49+
Lua.push name
50+
Lua.pushHaskellFunction fn
51+
Lua.rawset (-3)
52+
53+
-- | Lua callback function
54+
newtype Callback = Callback StackIndex
55+
56+
instance Peekable Callback where
57+
peek idx = do
58+
isFn <- Lua.isfunction idx
59+
if isFn
60+
then return (Callback idx)
61+
else Lua.throwException "Function expected"
62+
63+
instance Pushable Callback where
64+
push (Callback idx) = Lua.pushvalue idx
65+
66+
67+
-- | Any value of unknown type
68+
newtype AnyValue = AnyValue { fromAnyValue :: StackIndex }
69+
70+
instance Peekable AnyValue where
71+
peek = return . AnyValue
72+
73+
instance Pushable AnyValue where
74+
push (AnyValue idx) = Lua.pushvalue idx
75+
76+
-- | Call Lua callback function and return all of its results.
77+
invoke :: Callback -> Lua NumResults
78+
invoke callback = do
79+
oldTop <- Lua.gettop
80+
Lua.push callback
81+
Lua.call 0 Lua.multret
82+
newTop <- Lua.gettop
83+
return . NumResults . fromIntegral . Lua.fromStackIndex $
84+
newTop - oldTop
85+
86+
-- | Call Lua callback function with the given filename as its argument.
87+
invokeWithFilePath :: Callback -> FilePath -> Lua NumResults
88+
invokeWithFilePath callback filename = do
89+
oldTop <- Lua.gettop
90+
Lua.push callback
91+
Lua.push filename
92+
Lua.call (Lua.NumArgs 1) Lua.multret
93+
newTop <- Lua.gettop
94+
return . NumResults . fromIntegral . Lua.fromStackIndex $
95+
newTop - oldTop
96+
97+
-- | Convert a System IO operation to a Lua operation.
98+
ioToLua :: IO a -> Lua a
99+
ioToLua action = do
100+
result <- Lua.liftIO (try action)
101+
case result of
102+
Right result' -> return result'
103+
Left err -> Lua.throwException (show (err :: IOException))

0 commit comments

Comments
 (0)