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

Commit b7e5d70

Browse files
committed
Upgrade to hslua 1.0
1 parent c13503a commit b7e5d70

3 files changed

Lines changed: 23 additions & 29 deletions

File tree

hslua-module-text.cabal

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ source-repository head
2020

2121
library
2222
exposed-modules: Foreign.Lua.Module.Text
23-
build-depends: base >= 4.7 && < 5
24-
, hslua >= 0.9 && < 0.10
25-
, text >= 1 && < 1.3
23+
build-depends: base >= 4.7 && < 5
24+
, bytestring >= 0.10.2 && < 0.11
25+
, hslua >= 1.0 && < 1.1
26+
, text >= 1 && < 1.3
2627
hs-source-dirs: src
2728
default-language: Haskell2010
29+
other-extensions: OverloadedStrings
2830

2931
test-suite test-hslua
3032
default-language: Haskell2010

src/Foreign/Lua/Module/Text.hs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{-
2-
Copyright © 2017 Albert Krewinkel
2+
Copyright © 2017–2018 Albert Krewinkel
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -19,10 +19,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
THE SOFTWARE.
2121
-}
22-
22+
{-# LANGUAGE OverloadedStrings #-}
2323
{-|
2424
Module : Foreign.Lua.Module.Text
25-
Copyright : © 2017 Albert Krewinkel
25+
Copyright : © 2017–2018 Albert Krewinkel
2626
License : MIT
2727
Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
2828
Stability : alpha
@@ -36,10 +36,10 @@ module Foreign.Lua.Module.Text
3636
)where
3737

3838
import Control.Applicative ((<$>))
39+
import Data.ByteString (ByteString)
3940
import Data.Text (Text)
4041
import Data.Maybe (fromMaybe)
41-
import Foreign.Lua (FromLuaStack, NumResults, Lua, LuaInteger, ToLuaStack)
42-
import Foreign.Lua.FunctionCalling (ToHaskellFunction, newCFunction)
42+
import Foreign.Lua (NumResults, Lua, Peekable, Pushable, ToHaskellFunction)
4343
import qualified Foreign.Lua as Lua
4444
import qualified Data.Text as T
4545

@@ -50,7 +50,7 @@ pushModuleText = do
5050
addFunction "lower" (return . T.toLower :: Text -> Lua Text)
5151
addFunction "upper" (return . T.toUpper :: Text -> Lua Text)
5252
addFunction "reverse" (return . T.reverse :: Text -> Lua Text)
53-
addFunction "len" (return . fromIntegral . T.length :: Text -> Lua LuaInteger)
53+
addFunction "len" (return . fromIntegral . T.length :: Text -> Lua Lua.Integer)
5454
addFunction "sub" sub
5555
return 1
5656

@@ -63,8 +63,8 @@ preloadTextModule = flip addPackagePreloader pushModuleText
6363
-- which produces the package.
6464
addPackagePreloader :: String -> Lua NumResults -> Lua ()
6565
addPackagePreloader name modulePusher = do
66-
Lua.getglobal' "package.preload"
67-
Lua.pushcfunction =<< newCFunction modulePusher
66+
Lua.getfield Lua.registryindex Lua.preloadTableRegistryField
67+
Lua.pushHaskellFunction modulePusher
6868
Lua.setfield (-2) name
6969
Lua.pop 1
7070

@@ -77,20 +77,10 @@ addFunction name fn = do
7777
Lua.rawset (-3)
7878

7979
-- | Returns a substring, using Lua's string indexing rules.
80-
sub :: Text -> LuaInteger -> OrNil LuaInteger -> Lua Text
80+
sub :: Text -> Lua.Integer -> Lua.Optional Lua.Integer -> Lua Text
8181
sub s i j =
8282
let i' = fromIntegral i
83-
j' = fromIntegral . fromMaybe (-1) $ toMaybe j
83+
j' = fromIntegral . fromMaybe (-1) $ Lua.fromOptional j
8484
fromStart = if i' >= 0 then i' - 1 else T.length s + i'
8585
fromEnd = if j' < 0 then -j' - 1 else T.length s - j'
8686
in return . T.dropEnd fromEnd . T.drop fromStart $ s
87-
88-
-- A lua value or nil
89-
newtype OrNil a = OrNil { toMaybe :: Maybe a }
90-
91-
instance FromLuaStack a => FromLuaStack (OrNil a) where
92-
peek idx = do
93-
noValue <- Lua.isnoneornil idx
94-
if noValue
95-
then return (OrNil Nothing)
96-
else OrNil . Just <$> Lua.peek idx

test/test-hslua-module-text.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{-
2-
Copyright © 2017 Albert Krewinkel
2+
Copyright © 2017–2018 Albert Krewinkel
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -19,8 +19,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
THE SOFTWARE.
2121
-}
22+
{-# LANGUAGE OverloadedStrings #-}
23+
2224
import Control.Monad (void, when)
23-
import Foreign.Lua (Lua, runLua)
25+
import Foreign.Lua (Lua)
2426
import Foreign.Lua.Module.Text (preloadTextModule, pushModuleText)
2527
import Test.Tasty (TestTree, defaultMain, testGroup)
2628
import Test.Tasty.HUnit (assertEqual, testCase)
@@ -34,22 +36,22 @@ main = defaultMain $ testGroup "hslua-module-text" [tests]
3436
tests :: TestTree
3537
tests = testGroup "FromLuaStack"
3638
[ testCase "text module can be pushed to the stack" $
37-
runLua (void pushModuleText)
39+
Lua.run (void pushModuleText)
3840

39-
, testCase "text module can be added to the preloader" . runLua $ do
41+
, testCase "text module can be added to the preloader" . Lua.run $ do
4042
Lua.openlibs
4143
preloadTextModule "hstext"
4244
assertEqual' "function not added to preloader" Lua.TypeFunction =<< do
4345
Lua.getglobal' "package.preload.hstext"
4446
Lua.ltype (-1)
4547

46-
, testCase "text module can be loaded as hstext" . runLua $ do
48+
, testCase "text module can be loaded as hstext" . Lua.run $ do
4749
Lua.openlibs
4850
preloadTextModule "hstext"
4951
assertEqual' "loading the module fails " Lua.OK =<<
5052
Lua.dostring "require 'hstext'"
5153

52-
, testCase "Lua tests pass" . runLua $ do
54+
, testCase "Lua tests pass" . Lua.run $ do
5355
Lua.openlibs
5456
preloadTextModule "hstext"
5557
assertEqual' "error while running lua tests" Lua.OK =<< do

0 commit comments

Comments
 (0)