11{-
2- Copyright © 2017 Albert Krewinkel
2+ Copyright © 2017–2018 Albert Krewinkel
33
44Permission is hereby granted, free of charge, to any person obtaining a copy
55of 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,
1919OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020THE SOFTWARE.
2121-}
22-
22+ {-# LANGUAGE OverloadedStrings #-}
2323{-|
2424Module : Foreign.Lua.Module.Text
25- Copyright : © 2017 Albert Krewinkel
25+ Copyright : © 2017–2018 Albert Krewinkel
2626License : MIT
2727Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
2828Stability : alpha
@@ -36,10 +36,10 @@ module Foreign.Lua.Module.Text
3636 )where
3737
3838import Control.Applicative ((<$>) )
39+ import Data.ByteString (ByteString )
3940import Data.Text (Text )
4041import 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 )
4343import qualified Foreign.Lua as Lua
4444import 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.
6464addPackagePreloader :: String -> Lua NumResults -> Lua ()
6565addPackagePreloader 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
8181sub 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
0 commit comments