Skip to content

Commit

Permalink
Re-add pushthread wrapper function
Browse files Browse the repository at this point in the history
It's more difficult to argue why it was left out than to include it.
  • Loading branch information
tarleb committed Jul 8, 2017
1 parent 65665a3 commit 41d0309
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Foreign/Lua.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module Foreign.Lua
, pushnil
, pushnumber
, pushstring
, pushthread
, pushvalue
, rawequal
, rawget
Expand Down
8 changes: 8 additions & 0 deletions src/Foreign/Lua/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ pushstring :: B.ByteString -> Lua ()
pushstring s = liftLua $ \l ->
B.unsafeUseAsCStringLen s $ \(sPtr, z) -> lua_pushlstring l sPtr (fromIntegral z)

-- | Pushes the current thread onto the stack. Returns @True@ if this thread is
-- the main thread of its state, @False@ otherwise.
--
-- See also:
-- <https://www.lua.org/manual/5.3/manual.html#lua_pushthread lua_pushthread>.
pushthread :: Lua Bool
pushthread = (1 ==) <$> liftLua lua_pushthread

-- | Pushes a copy of the element at the given index onto the stack.
--
-- See <https://www.lua.org/manual/5.3/manual.html#lua_pushvalue lua_pushvalue>.
Expand Down
2 changes: 1 addition & 1 deletion src/Foreign/Lua/Api/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import Foreign.Ptr
#include "lua.h"

-- | Synonym for @lua_State *@. See <https://www.lua.org/manual/5.3/#lua_State lua_State>.
newtype LuaState = LuaState (Ptr ())
newtype LuaState = LuaState (Ptr ()) deriving (Eq)

-- | Synonym for @lua_Alloc@. See <https://www.lua.org/manual/5.3/#lua_Alloc lua_Alloc>.
type LuaAlloc = Ptr () -> Ptr () -> CSize -> CSize -> IO (Ptr ())
Expand Down
1 change: 1 addition & 0 deletions src/Foreign/Lua/Types/Lua.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The core Lua types, including mappings of Lua types to Haskell.
-}
module Foreign.Lua.Types.Lua
( Lua (..)
, luaState
, runLuaWith
, liftIO
, liftLua
Expand Down
15 changes: 14 additions & 1 deletion test/Foreign/Lua/ApiTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Test.QuickCheck (Property, (.&&.))
import Test.QuickCheck.Arbitrary (Arbitrary (..), arbitraryBoundedEnum)
import Test.QuickCheck.Monadic (assert, monadicIO, run)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (assertBool, testCase)
import Test.Tasty.QuickCheck (testProperty)

import qualified Prelude
Expand Down Expand Up @@ -71,7 +72,7 @@ tests = testGroup "Haskell version of the C API"
return (movedEl == 9 && newTop == 8)
, luaTestCase "inserts stack elements using negative indices" $ do
pushLuaExpr "1, 2, 3, 4, 5, 6, 7, 8, 9"
insert (4)
insert 4
movedEl <- peek 4 :: Lua LuaInteger
newTop <- peek (-1) :: Lua LuaInteger
return (movedEl == 9 && newTop == 8)
Expand Down Expand Up @@ -112,6 +113,18 @@ tests = testGroup "Haskell version of the C API"
pushLuaExpr "'Moin'"
equal (-1) (-2)

, luaTestCase "can push and receive a thread" $ do
luaSt <- luaState
isMain <- pushthread
liftIO (assertBool "pushing the main thread should return True" isMain)
luaSt' <- peek (-1)
return (luaSt == luaSt')

, testCase "different threads are not equal" $ do
luaSt1 <- newstate
luaSt2 <- newstate
assertBool "different lua threads are equal in haskell" (luaSt1 /= luaSt2)

, testGroup "compare"
[ testProperty "identifies strictly smaller values" $ compareWith (<) OpLT
, testProperty "identifies smaller or equal values" $ compareWith (<=) OpLE
Expand Down

0 comments on commit 41d0309

Please sign in to comment.