Ardour  9.0-pre0-582-g084a23a80d
LuaHelpers.h
Go to the documentation of this file.
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 
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 
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 
127 inline bool isfulluserdata (lua_State* L, int index)
128 {
129  return lua_isuserdata (L, index) && !lua_islightuserdata (L, index);
130 }
131 
139 inline bool equalstates (lua_State* L1, lua_State* L2)
140 {
141  return lua_topointer (L1, LUA_REGISTRYINDEX) ==
143 }
#define LUA_OPLT
Definition: LuaHelpers.h:58
#define LUA_OPLE
Definition: LuaHelpers.h:59
bool isfulluserdata(lua_State *L, int index)
Definition: LuaHelpers.h:127
void rawsetfield(lua_State *L, int index, char const *key)
Definition: LuaHelpers.h:116
int lua_absindex(lua_State *L, int idx)
Definition: LuaHelpers.h:33
void lua_rawgetp(lua_State *L, int idx, void const *p)
Definition: LuaHelpers.h:41
int get_length(lua_State *L, int idx)
Definition: LuaHelpers.h:82
void lua_rawsetp(lua_State *L, int idx, void const *p)
Definition: LuaHelpers.h:48
void rawgetfield(lua_State *L, int index, char const *key)
Definition: LuaHelpers.h:106
bool equalstates(lua_State *L1, lua_State *L2)
Definition: LuaHelpers.h:139
#define LUA_OPEQ
Definition: LuaHelpers.h:57
int lua_compare(lua_State *L, int idx1, int idx2, int op)
Definition: LuaHelpers.h:61
lua_Number() luaL_checknumber(lua_State *L, int arg)
#define lua_istable(L, n)
#define lua_insert(L, idx)
void() lua_len(lua_State *L, int idx)
const void *() lua_topointer(lua_State *L, int idx)
int() lua_rawget(lua_State *L, int idx)
#define LUA_REGISTRYINDEX
Definition: lua-5.3.5/lua.h:42
void() lua_pushlightuserdata(lua_State *L, void *p)
#define lua_islightuserdata(L, n)
const char *() lua_pushstring(lua_State *L, const char *s)
int() lua_gettop(lua_State *L)
#define lua_pop(L, n)
int() lua_isuserdata(lua_State *L, int idx)
void() lua_rawset(lua_State *L, int idx)