1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6 Copyright 2007, Nathan Reed
8 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 //==============================================================================
30 // These are for Lua versions prior to 5.2.0.
32 #if LUA_VERSION_NUM < 502
33 inline int lua_absindex (lua_State* L, int idx)
35 if (idx > LUA_REGISTRYINDEX && idx < 0)
36 return lua_gettop (L) + idx + 1;
41 inline void lua_rawgetp (lua_State* L, int idx, void const* p)
43 idx = lua_absindex (L, idx);
44 lua_pushlightuserdata (L, const_cast <void*> (p));
48 inline void lua_rawsetp (lua_State* L, int idx, void const* p)
50 idx = lua_absindex (L, idx);
51 lua_pushlightuserdata (L, const_cast <void*> (p));
52 // put key behind value
61 inline int lua_compare (lua_State* L, int idx1, int idx2, int op)
66 return lua_equal (L, idx1, idx2);
70 return lua_lessthan (L, idx1, idx2);
74 return lua_equal (L, idx1, idx2) || lua_lessthan (L, idx1, idx2);
82 inline int get_length (lua_State* L, int idx)
84 return int (lua_objlen (L, idx));
88 inline int get_length (lua_State* L, int idx)
91 int len = int (luaL_checknumber (L, -1));
99 # define LUABRIDGE_LUA_OK 0
101 # define LUABRIDGE_LUA_OK LUA_OK
104 /** Get a table value, bypassing metamethods.
106 inline void rawgetfield (lua_State* L, int index, char const* key)
108 assert (lua_istable (L, index));
109 index = lua_absindex (L, index);
110 lua_pushstring (L, key);
111 lua_rawget (L, index);
114 /** Set a table value, bypassing metamethods.
116 inline void rawsetfield (lua_State* L, int index, char const* key)
118 assert (lua_istable (L, index));
119 index = lua_absindex (L, index);
120 lua_pushstring (L, key);
122 lua_rawset (L, index);
125 /** Returns true if the value is a full userdata (not light).
127 inline bool isfulluserdata (lua_State* L, int index)
129 return lua_isuserdata (L, index) && !lua_islightuserdata (L, index);
132 /** Test lua_State objects for global equality.
134 This can determine if two different lua_State objects really point
135 to the same global state, such as when using coroutines.
137 @note This is used for assertions.
139 inline bool equalstates (lua_State* L1, lua_State* L2)
141 return lua_topointer (L1, LUA_REGISTRYINDEX) ==
142 lua_topointer (L2, LUA_REGISTRYINDEX);