Skip to content

Commit

Permalink
Merge SafeBindings into RawBindings
Browse files Browse the repository at this point in the history
Unsafe bindings were removed in a previous commit. It seemed unnecessary
complex to have safe bindings in a separate module.
  • Loading branch information
tarleb committed Aug 11, 2017
1 parent 09a7321 commit 00166ce
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 89 deletions.
1 change: 0 additions & 1 deletion hslua.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ library
, Foreign.Lua.Api.Constants
, Foreign.Lua.Api.RawBindings
, Foreign.Lua.Api.Types
, Foreign.Lua.Api.SafeBindings
, Foreign.Lua.FunctionCalling
, Foreign.Lua.Types
, Foreign.Lua.Types.Error
Expand Down
1 change: 0 additions & 1 deletion src/Foreign/Lua/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ import Data.Maybe (fromMaybe)
import Foreign.C
import Foreign.Lua.Api.Constants
import Foreign.Lua.Api.RawBindings
import Foreign.Lua.Api.SafeBindings
import Foreign.Lua.Api.Types
import Foreign.Lua.Types.Error
import Foreign.Lua.Types.Lua
Expand Down
67 changes: 53 additions & 14 deletions src/Foreign/Lua/Api/RawBindings.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Foreign.Lua.Api.Types
import Foreign.Ptr

#include "lua.h"
#include "safer-api.h"

-- TODO: lua_getallocf, lua_setallocf
-- TODO: Debugger functions
Expand Down Expand Up @@ -134,9 +135,13 @@ foreign import ccall unsafe "lua.h lua_type"
foreign import ccall unsafe "lua.h lua_typename"
lua_typename :: LuaState -> CInt -> IO (Ptr CChar)

--- lua_compare is unsafe (might cause a longjmp), use hslua_compare instead.

#if LUA_VERSION_NUMBER < 502
-- lua_compare is unsafe (might cause a longjmp), use hslua_compare instead.
#if LUA_VERSION_NUM >= 502
-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_compare \
-- @lua_compare@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_compare"
hslua_compare :: LuaState -> StackIndex -> StackIndex -> CInt -> IO CInt
#else
-- | See <https://www.lua.org/manual/5.1/manual.html#lua_equal lua_equal>
foreign import ccall "lua.h lua_equal"
lua_equal :: LuaState -> StackIndex -> StackIndex -> IO CInt
Expand Down Expand Up @@ -254,8 +259,19 @@ foreign import ccall unsafe "lua.h lua_pushthread"
-- * Get functions

-- lua_gettable is unsafe, use hslua_gettable instead.

-- lua_getfield is unsafe, use hslua_getfield instead.
-- lua_getglobal is unsafe, use hslua_getglobal instead.
-- lua_getfenv (5.1 only) is not supported.

-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_gettable \
-- @lua_gettable@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_gettable"
hslua_gettable :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_getfield \
-- @lua_getfield@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_getfield"
hslua_getfield :: LuaState -> StackIndex -> Ptr CChar -> IO CInt

-- | See <https://www.lua.org/manual/5.3/manual.html#lua_rawget lua_rawget>
foreign import ccall unsafe "lua.h lua_rawget"
Expand All @@ -277,16 +293,29 @@ foreign import ccall unsafe "lua.h lua_newuserdata"
foreign import ccall unsafe "lua.h lua_getmetatable"
lua_getmetatable :: LuaState -> StackIndex -> IO CInt

-- lua_getfenv (5.1 only) is not supported.
-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_getglobal \
-- @lua_getglobal@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_getglobal"
hslua_getglobal :: LuaState -> Ptr CChar -> IO CInt

-- lua_getglobal is unsafe, use lua_getglobal instead.

--------------------------------------------------------------------------------
-- * Set functions

-- lua_settable is unsafe, use hslua_settable instead.

-- lua_setfield is unsafe, use hslua_setfield instead.
-- lua_setglobal is unsafe, use hslua_setglobal instead.
-- lua_setfenv (5.1 only) is not supported.

-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_settable \
-- @lua_settable@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_settable"
hslua_settable :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_setfield \
-- @lua_setfield@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_setfield"
hslua_setfield :: LuaState -> StackIndex -> Ptr CChar -> IO CInt

-- | See <https://www.lua.org/manual/5.3/manual.html#lua_rawset lua_rawset>
foreign import ccall unsafe "lua.h lua_rawset"
Expand All @@ -300,9 +329,11 @@ foreign import ccall unsafe "lua.h lua_rawseti"
foreign import ccall unsafe "lua.h lua_setmetatable"
lua_setmetatable :: LuaState -> StackIndex -> IO ()

-- lua_setfenv (5.1 only) is not supported.
-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_setglobal \
-- @lua_setglobal@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_setglobal"
hslua_setglobal :: LuaState -> Ptr CChar -> IO CInt

-- lua_setglobal is unsafe, use hslua_setglobal instead.

--------------------------------------------------------------------------------
-- * 'load' and 'call' functions (load and run Lua code)
Expand Down Expand Up @@ -354,19 +385,27 @@ foreign import ccall "lua.h lua_gc"
-- * Miscellaneous functions

-- lua_error is unsafe in a haskell context and hence not supported.

-- lua_next is unsafe, use hslua_next instead.

-- lua_concat is unsafe (may trigger a longjmp), use hslua_concat instead.

-- | See <https://www.lua.org/manual/5.3/manual.html#luaL_openlibs luaL_openlibs>
foreign import ccall unsafe "lualib.h luaL_openlibs"
luaL_openlibs :: LuaState -> IO ()
-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_next \
-- @lua_next@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_next"
hslua_next :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <https://lua.org/manual/5.3/manual.html#lua_concat \
-- @lua_concat@> which catches any @longjmp@s.
foreign import ccall "safer-api.h hslua_concat"
hslua_concat :: LuaState -> CInt -> IO CInt


------------------------------------------------------------------------------
-- * Lua Libraries

-- | See <https://www.lua.org/manual/5.3/manual.html#luaL_openlibs luaL_openlibs>
foreign import ccall unsafe "lualib.h luaL_openlibs"
luaL_openlibs :: LuaState -> IO ()

-- | Point to function opening the base library.
foreign import ccall unsafe "lualib.h &luaopen_base"
lua_open_base_ptr :: FunPtr (LuaState -> IO CInt)
Expand Down
73 changes: 0 additions & 73 deletions src/Foreign/Lua/Api/SafeBindings.hsc

This file was deleted.

0 comments on commit 00166ce

Please sign in to comment.