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

Commit e7646ef

Browse files
committed
Use module helpers available in HsLua 1.0.3
1 parent 0cdf7c4 commit e7646ef

3 files changed

Lines changed: 18 additions & 30 deletions

File tree

hslua-module-text.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ library
2222
exposed-modules: Foreign.Lua.Module.Text
2323
build-depends: base >= 4.7 && < 5
2424
, bytestring >= 0.10.2 && < 0.11
25-
, hslua >= 1.0 && < 1.2
25+
, hslua >= 1.0.3 && < 1.2
2626
, text >= 1 && < 1.3
2727
hs-source-dirs: src
2828
default-language: Haskell2010

src/Foreign/Lua/Module/Text.hs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Portability : ForeignFunctionInterface
1010
Provide a lua module containing a selection of useful Text functions.
1111
-}
1212
module Foreign.Lua.Module.Text
13-
( pushModuleText
13+
( pushModule
14+
, pushModuleText
1415
, preloadTextModule
1516
)where
1617

@@ -22,38 +23,25 @@ import Foreign.Lua (NumResults, Lua, Peekable, Pushable, ToHaskellFunction)
2223
import qualified Foreign.Lua as Lua
2324
import qualified Data.Text as T
2425

25-
-- | Pushes the @text@ module to the lua stack.
26-
pushModuleText :: Lua NumResults
27-
pushModuleText = do
26+
-- | Pushes the @text@ module to the Lua stack.
27+
pushModule :: Lua NumResults
28+
pushModule = do
2829
Lua.newtable
29-
addFunction "lower" (return . T.toLower :: Text -> Lua Text)
30-
addFunction "upper" (return . T.toUpper :: Text -> Lua Text)
31-
addFunction "reverse" (return . T.reverse :: Text -> Lua Text)
32-
addFunction "len" (return . fromIntegral . T.length :: Text -> Lua Lua.Integer)
33-
addFunction "sub" sub
30+
Lua.addfunction "lower" (return . T.toLower :: Text -> Lua Text)
31+
Lua.addfunction "upper" (return . T.toUpper :: Text -> Lua Text)
32+
Lua.addfunction "reverse" (return . T.reverse :: Text -> Lua Text)
33+
Lua.addfunction "len" (return . fromIntegral . T.length :: Text -> Lua Lua.Integer)
34+
Lua.addfunction "sub" sub
3435
return 1
3536

37+
-- | Legacy alias for '@pushModule@'.
38+
pushModuleText :: Lua NumResults
39+
pushModuleText = pushModule
40+
3641
-- | Add the text module under the given name to the table of preloaded
3742
-- packages.
3843
preloadTextModule :: String -> Lua ()
39-
preloadTextModule = flip addPackagePreloader pushModuleText
40-
41-
-- | Registers a preloading function. Takes an module name and the Lua operation
42-
-- which produces the package.
43-
addPackagePreloader :: String -> Lua NumResults -> Lua ()
44-
addPackagePreloader name modulePusher = do
45-
Lua.getfield Lua.registryindex Lua.preloadTableRegistryField
46-
Lua.pushHaskellFunction modulePusher
47-
Lua.setfield (-2) name
48-
Lua.pop 1
49-
50-
-- | Attach a function to the table at the top of the stack, using the given
51-
-- name.
52-
addFunction :: ToHaskellFunction a => String -> a -> Lua ()
53-
addFunction name fn = do
54-
Lua.push name
55-
Lua.pushHaskellFunction fn
56-
Lua.rawset (-3)
44+
preloadTextModule = flip Lua.preloadhs pushModule
5745

5846
-- | Returns a substring, using Lua's string indexing rules.
5947
sub :: Text -> Lua.Integer -> Lua.Optional Lua.Integer -> Lua Text

stack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
resolver: lts-12.10
1+
resolver: lts-13.17
22
packages: ['.']
33
flags: {}
44
extra-deps:
5-
- hslua-1.0.0
5+
- hslua-1.0.3

0 commit comments

Comments
 (0)