reduce header dependencies (part 1/2)
[ardour.git] / libs / lua / LuaBridge / detail / LuaHelpers.h
1 //------------------------------------------------------------------------------
2 /*
3   https://github.com/vinniefalco/LuaBridge
4
5   Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6   Copyright 2007, Nathan Reed
7
8   License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
9
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:
16
17   The above copyright notice and this permission notice shall be included in all
18   copies or substantial portions of the Software.
19
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
26   SOFTWARE.
27 */
28 //==============================================================================
29
30 // These are for Lua versions prior to 5.2.0.
31 //
32 #if LUA_VERSION_NUM < 502
33 inline int lua_absindex (lua_State* L, int idx)
34 {
35   if (idx > LUA_REGISTRYINDEX && idx < 0)
36     return lua_gettop (L) + idx + 1;
37   else
38     return idx;
39 }
40
41 inline void lua_rawgetp (lua_State* L, int idx, void const* p)
42 {
43   idx = lua_absindex (L, idx);
44   lua_pushlightuserdata (L, const_cast <void*> (p));
45   lua_rawget (L,idx);
46 }
47
48 inline void lua_rawsetp (lua_State* L, int idx, void const* p)
49 {
50   idx = lua_absindex (L, idx);
51   lua_pushlightuserdata (L, const_cast <void*> (p));
52   // put key behind value
53   lua_insert (L, -2);
54   lua_rawset (L, idx);
55 }
56
57 #define LUA_OPEQ 1
58 #define LUA_OPLT 2
59 #define LUA_OPLE 3
60
61 inline int lua_compare (lua_State* L, int idx1, int idx2, int op)
62 {
63   switch (op)
64   {
65   case LUA_OPEQ:
66     return lua_equal (L, idx1, idx2);
67     break;
68
69   case LUA_OPLT:
70     return lua_lessthan (L, idx1, idx2);
71     break;
72
73   case LUA_OPLE:
74     return lua_equal (L, idx1, idx2) || lua_lessthan (L, idx1, idx2);
75     break;
76
77   default:
78     return 0;
79   };
80 }
81
82 inline int get_length (lua_State* L, int idx)
83 {
84   return int (lua_objlen (L, idx));
85 }
86
87 #else
88 inline int get_length (lua_State* L, int idx)
89 {
90   lua_len (L, idx);
91   int len = int (luaL_checknumber (L, -1));
92   lua_pop (L, 1);
93   return len;
94 }
95
96 #endif
97
98 #ifndef LUA_OK
99 # define LUABRIDGE_LUA_OK 0
100 #else
101 # define LUABRIDGE_LUA_OK LUA_OK
102 #endif
103
104 /** Get a table value, bypassing metamethods.
105 */
106 inline void rawgetfield (lua_State* L, int index, char const* key)
107 {
108   assert (lua_istable (L, index));
109   index = lua_absindex (L, index);
110   lua_pushstring (L, key);
111   lua_rawget (L, index);
112 }
113
114 /** Set a table value, bypassing metamethods.
115 */
116 inline void rawsetfield (lua_State* L, int index, char const* key)
117 {
118   assert (lua_istable (L, index));
119   index = lua_absindex (L, index);
120   lua_pushstring (L, key);
121   lua_insert (L, -2);
122   lua_rawset (L, index);
123 }
124
125 /** Returns true if the value is a full userdata (not light).
126 */
127 inline bool isfulluserdata (lua_State* L, int index)
128 {
129   return lua_isuserdata (L, index) && !lua_islightuserdata (L, index);
130 }
131
132 /** Test lua_State objects for global equality.
133
134     This can determine if two different lua_State objects really point
135     to the same global state, such as when using coroutines.
136
137     @note This is used for assertions.
138 */
139 inline bool equalstates (lua_State* L1, lua_State* L2)
140 {
141   return lua_topointer (L1, LUA_REGISTRYINDEX) ==
142          lua_topointer (L2, LUA_REGISTRYINDEX);
143 }