This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathqtluauserdata.cc
More file actions
196 lines (151 loc) · 4.35 KB
/
qtluauserdata.cc
File metadata and controls
196 lines (151 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
This file is part of LibQtLua.
LibQtLua is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LibQtLua is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with LibQtLua. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2008, Alexandre Becoulet <alexandre.becoulet@free.fr>
*/
#include <cstdarg>
#ifdef __GNUC__
#include <cxxabi.h>
#endif
extern "C" {
#include <lua.h>
}
#include <QtLua/UserData>
#include <QtLua/Value>
#include <QtLua/State>
#include <QtLua/String>
namespace QtLua {
void UserData::push_ud(lua_State *st)
{
// allocate lua user data to store reference to 'this'
new (lua_newuserdata(st, sizeof (UserData::ptr))) UserData::ptr(*this);
// attach metatable
lua_pushlightuserdata(st, &State::_key_item_metatable);
lua_rawget(st, LUA_REGISTRYINDEX);
lua_setmetatable(st, -2);
}
template <bool pop>
inline QtLua::Ref<UserData> UserData::get_ud_(lua_State *st, int i)
{
#ifndef QTLUA_NO_USERDATA_CHECK
if (lua_getmetatable(st, i))
{
lua_pushlightuserdata(st, &State::_key_item_metatable);
lua_rawget(st, LUA_REGISTRYINDEX);
if (lua_rawequal(st, -2, -1))
{
lua_pop(st, 2);
#endif
UserData::ptr *item = static_cast<UserData::ptr *>(lua_touserdata(st, i));
if (pop)
lua_pop(st, 1);
return *item;
#ifndef QTLUA_NO_USERDATA_CHECK
}
lua_pop(st, 1);
}
lua_pop(st, 1);
if (pop)
lua_pop(st, 1);
throw String("Lua userdata is not a QtLua::UserData.");
#endif
}
QtLua::Ref<UserData> UserData::get_ud(lua_State *st, int i)
{
return get_ud_<false>(st, i);
}
QtLua::Ref<UserData> UserData::pop_ud(lua_State *st)
{
return get_ud_<true>(st, -1);
}
String UserData::get_type_name() const
{
#ifdef __GNUC__
int s;
return abi::__cxa_demangle(typeid(*this).name(), 0, 0, &s);
#else
return typeid(*this).name();
#endif
}
String UserData::get_value_str() const
{
return QString().sprintf("%p", this);
}
Value UserData::meta_operation(State &ls, Value::Operation op,
const Value &a, const Value &b)
{
throw String("Operation not handled by % type").arg(get_type_name());
};
void UserData::meta_newindex(State &ls, const Value &key, const Value &value)
{
throw String("Table write access not handled by % type").arg(get_type_name());
};
Value UserData::meta_index(State &ls, const Value &key)
{
throw String("Table read access not handled by % type").arg(get_type_name());
};
bool UserData::meta_contains(State &ls, const Value &key)
{
try {
return !meta_index(ls, key).is_nil();
} catch (String &e) {
return false;
}
}
Value::List UserData::meta_call(State &ls, const Value::List &args)
{
throw String("Function call not handled by % type").arg(get_type_name());
};
Ref<Iterator> UserData::new_iterator(State &ls)
{
throw String("Table iteration not handled by % type").arg(get_type_name());
}
bool UserData::support(Value::Operation c) const
{
return false;
}
void UserData::meta_call_check_args(const Value::List &args,
int min_count, int max_count, ...)
{
int i;
va_list ap;
if (args.count() < min_count)
throw String("Missing argument(s), at least % arguments expected").arg(min_count);
if (max_count && args.count() > max_count)
throw String("Too many argument(s), at most % arguments expected").arg(max_count);
va_start(ap, max_count);
Value::ValueType type = Value::TNone;
for (i = 0; i < args.size(); i++)
{
if (i < min_count || i < max_count)
type = (Value::ValueType)va_arg(ap, int);
if (type != Value::TNone && type != args[i].type())
{
va_end(ap);
throw String("Wrong type for argument %, lua::% expected instead of %.")
.arg(i+1).arg(lua_typename(0, type)).arg(args[i].type_name());
}
}
va_end(ap);
}
bool UserData::operator==(const UserData &ud)
{
return this == &ud;
}
bool UserData::operator<(const UserData &ud)
{
return this < &ud;
}
void UserData::completion_patch(String &path, String &entry, int &offset)
{
}
}