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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/*
===============================================================================
Copyright (C) 2024 benoit Germain <bnt.germain@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
*/
#include "_pch.hpp"
#include "nameof.hpp"
#include "tools.hpp"
// #################################################################################################
DECLARE_UNIQUE_TYPE(FqnLength, decltype(lua_rawlen(nullptr, kIdxTop)));
// Return some name helping to identify an object
[[nodiscard]]
FqnLength DiscoverObjectNameRecur(lua_State* const L_, FqnLength const shortest_)
{
static constexpr StackIndex kWhat{ 1 }; // the object to investigate // L_: o "r" {c} {fqn} ... <>
static constexpr StackIndex kResult{ 2 }; // where the result string is stored
static constexpr StackIndex kCache{ 3 }; // a cache, where visited locations remember the FqnLength to reach them
static constexpr StackIndex kFQN{ 4 }; // the name compositing stack
// no need to scan this location if the name we will discover is longer than one we already know
FqnLength const _fqnLength{ lua_rawlen(L_, kFQN) };
if (shortest_ <= _fqnLength) {
return shortest_;
}
// in: k, v at the top of the stack
static constexpr auto _pushNameOnFQN = [](lua_State* const L_) {
STACK_CHECK_START_REL(L_, 0);
lua_pushvalue(L_, -2); // L_: o "r" {c} {fqn} ... k v k
auto const _keyType{ luaG_type(L_, kIdxTop) };
if (_keyType != LuaType::STRING) {
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... k v
luaG_pushstring(L_, "<%s>", luaG_typename(L_, _keyType).data()); // L_: o "r" {c} {fqn} ... k v "<type of k>"
} else {
// decorate the key string with something that tells us the type of the value
switch (luaG_type(L_, StackIndex{ -2 })) {
default:
LUA_ASSERT(L_, false); // there is something wrong if we end up here
luaG_pushstring(L_, "??"); // L_: o "r" {c} {fqn} ... k v "k" "??"
break;
case LuaType::FUNCTION:
luaG_pushstring(L_, "()"); // L_: o "r" {c} {fqn} ... k v "k" "()"
break;
case LuaType::TABLE:
luaG_pushstring(L_, "[]"); // L_: o "r" {c} {fqn} ... k v "k" "[]"
break;
case LuaType::USERDATA:
luaG_pushstring(L_, "<>"); // L_: o "r" {c} {fqn} ... k v "k" "<>"
break;
}
lua_concat(L_, 2); // L_: o "r" {c} {fqn} ... k v "k??"
}
FqnLength const _depth{ lua_rawlen(L_, kFQN) + 1 };
lua_rawseti(L_, kFQN, static_cast<int>(_depth)); // L_: o "r" {c} {fqn} ... k v
STACK_CHECK(L_, 0);
return _depth;
};
static constexpr auto _popNameFromFQN = [](lua_State* const L_) {
STACK_CHECK_START_REL(L_, 0);
lua_pushnil(L_); // L_: o "r" {c} {fqn} ... nil
lua_rawseti(L_, kFQN, static_cast<int>(lua_rawlen(L_, kFQN))); // L_: o "r" {c} {fqn} ...
STACK_CHECK(L_, 0);
};
static constexpr auto _recurseThenPop = [](lua_State* const L_, FqnLength const shortest_) -> FqnLength {
STACK_CHECK_START_REL(L_, 0); // L_: o "r" {c} {fqn} ... <>
FqnLength r_{ shortest_ };
auto const _type{ luaG_type(L_, kIdxTop) };
if (_type == LuaType::TABLE || _type == LuaType::USERDATA || _type == LuaType::FUNCTION) {
r_ = DiscoverObjectNameRecur(L_, shortest_);
STACK_CHECK(L_, 0);
_popNameFromFQN(L_);
STACK_CHECK(L_, 0);
}
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ...
STACK_CHECK(L_, -1);
return r_;
};
// in: k, v at the top of the stack
// out: v popped from the stack
static constexpr auto _processKeyValue = [](lua_State* const L_, FqnLength const shortest_) -> FqnLength {
FqnLength _r{ shortest_ };
STACK_GROW(L_, 2);
STACK_CHECK_START_REL(L_, 0); // L_: o "r" {c} {fqn} ... k v
// filter out uninteresting values
auto const _valType{ luaG_type(L_, kIdxTop) };
if (_valType == LuaType::NIL || _valType == LuaType::BOOLEAN || _valType == LuaType::LIGHTUSERDATA || _valType == LuaType::NUMBER || _valType == LuaType::STRING) {
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... k
return _r;
}
// append key name to fqn stack
FqnLength const _depth{ _pushNameOnFQN(L_) }; // L_: o "r" {c} {fqn} ... k v
// process the value
if (lua_rawequal(L_, kIdxTop, kWhat)) { // is it what we are looking for?
// update shortest name
if (_depth < _r) {
_r = _depth;
std::ignore = tools::PushFQN(L_, kFQN); // L_: o "r" {c} {fqn} ... k v "fqn"
lua_replace(L_, kResult); // L_: o "r" {c} {fqn} ... k v
}
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... k
_popNameFromFQN(L_);
} else {
// let's see if the value contains what we are looking for
_r = _recurseThenPop(L_, _r); // L_: o "r" {c} {fqn} ... k
}
STACK_CHECK(L_, -1);
return _r;
};
static constexpr auto _scanTable = [](lua_State* const L_, FqnLength const shortest_) -> FqnLength {
FqnLength r_{ shortest_ };
STACK_GROW(L_, 2);
STACK_CHECK_START_REL(L_, 0);
lua_pushnil(L_); // L_: o "r" {c} {fqn} ... {?} nil
while (lua_next(L_, -2)) { // L_: o "r" {c} {fqn} ... {?} k v
r_ = _processKeyValue(L_, r_); // L_: o "r" {c} {fqn} ... {?} k
} // L_: o "r" {c} {fqn} ... {?}
if (lua_getmetatable(L_, kIdxTop)) { // L_: o "r" {c} {fqn} ... {?} {mt}
lua_pushstring(L_, "__metatable"); // L_: o "r" {c} {fqn} ... {?} {mt} "__metatable"
lua_insert(L_, -2); // L_: o "r" {c} {fqn} ... {?} "__metatable" {mt}
r_ = _processKeyValue(L_, r_); // L_: o "r" {c} {fqn} ... {?} "__metatable"
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... {?}
}
STACK_CHECK(L_, 0);
return r_;
};
static constexpr auto _scanUserData = [](lua_State* const L_, FqnLength const shortest_) -> FqnLength {
FqnLength r_{ shortest_ };
STACK_GROW(L_, 2);
STACK_CHECK_START_REL(L_, 0);
if (lua_getmetatable(L_, kIdxTop)) { // L_: o "r" {c} {fqn} ... U {mt}
lua_pushstring(L_, "__metatable"); // L_: o "r" {c} {fqn} ... U {mt} "__metatable"
lua_insert(L_, -2); // L_: o "r" {c} {fqn} ... U "__metatable" {mt}
r_ = _processKeyValue(L_, r_); // L_: o "r" {c} {fqn} ... U "__metatable"
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... U
}
STACK_CHECK(L_, 0);
UserValueIndex _uvi{ 0 };
while (lua_getiuservalue(L_, kIdxTop, ++_uvi) != LUA_TNONE) { // L_: o "r" {c} {fqn} ... U uv
luaG_pushstring(L_, "<uv:%d>", _uvi); // L_: o "r" {c} {fqn} ... U uv name
lua_insert(L_, -2); // L_: o "r" {c} {fqn} ... U name uv
r_ = _processKeyValue(L_, r_); // L_: o "r" {c} {fqn} ... U name
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... U
}
// when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... U
STACK_CHECK(L_, 0);
return r_;
};
static constexpr auto _scanFunction = [](lua_State* const L_, FqnLength const shortest_) -> FqnLength {
FqnLength r_{ shortest_ };
STACK_GROW(L_, 2);
STACK_CHECK_START_REL(L_, 0); // L_: o "r" {c} {fqn} ... F
int _n{ 0 };
for (char const* _upname{}; (_upname = lua_getupvalue(L_, kIdxTop, ++_n));) { // L_: o "r" {c} {fqn} ... F up
if (*_upname == 0) {
_upname = "<C>";
}
luaG_pushstring(L_, "upvalue:%s", _upname); // L_: o "r" {c} {fqn} ... F up name
lua_insert(L_, -2); // L_: o "r" {c} {fqn} ... F name up
r_ = _processKeyValue(L_, r_); // L_: o "r" {c} {fqn} ... F name
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... F
}
STACK_CHECK(L_, 0);
return r_;
};
STACK_GROW(L_, 2);
STACK_CHECK_START_REL(L_, 0);
// stack top contains the location to search in (table, function, userdata)
[[maybe_unused]] auto const _typeWhere{ luaG_type(L_, kIdxTop) };
LUA_ASSERT(L_, _typeWhere == LuaType::TABLE || _typeWhere == LuaType::USERDATA || _typeWhere == LuaType::FUNCTION);
lua_pushvalue(L_, kIdxTop); // L_: o "r" {c} {fqn} ... <> <>
lua_rawget(L_, kCache); // L_: o "r" {c} {fqn} ... <> nil/N
auto const _visitDepth{ lua_isnil(L_, kIdxTop) ? std::numeric_limits<FqnLength::type>::max() : lua_tointeger(L_, kIdxTop) };
lua_pop(L_, 1); // L_: o "r" {c} {fqn} ... <>
// if location is already visited with a name of <= length, we are done
if (_visitDepth <= _fqnLength) {
return shortest_;
}
// examined location is not in the cache, add it now
// cache[o] = _fqnLength
lua_pushvalue(L_, kIdxTop); // L_: o "r" {c} {fqn} ... <> <>
lua_pushinteger(L_, _fqnLength); // L_: o "r" {c} {fqn} ... <> <> N
lua_rawset(L_, kCache); // L_: o "r" {c} {fqn} ... <>
FqnLength r_;
// scan location contents
switch (_typeWhere) { // L_: o "r" {c} {fqn} ... <>
default:
raise_luaL_error(L_, "unexpected error, please investigate");
break;
case LuaType::TABLE:
r_ = _scanTable(L_, shortest_);
break;
case LuaType::USERDATA:
r_ = _scanUserData(L_, shortest_);
break;
case LuaType::FUNCTION:
r_ = _scanFunction(L_, shortest_);
break;
}
STACK_CHECK(L_, 0);
return r_;
}
// #################################################################################################
// "type", "name" = lanes.nameof(o)
LUAG_FUNC(nameof)
{
auto const _argCount{ lua_gettop(L_) };
if (_argCount != 1) {
raise_luaL_argerror(L_, StackIndex{ _argCount }, "exactly 1 argument expected");
}
// nil, boolean, light userdata, number and string aren't identifiable
static constexpr auto _isIdentifiable = [](lua_State* const L_) {
auto const _valType{ luaG_type(L_, kIdxTop) };
return _valType == LuaType::TABLE || _valType == LuaType::FUNCTION || _valType == LuaType::USERDATA || _valType == LuaType::THREAD;
};
if (!_isIdentifiable(L_)) {
luaG_pushstring(L_, luaG_typename(L_, kIdxTop)); // L_: o "type"
lua_insert(L_, -2); // L_: "type" o
return 2;
}
STACK_GROW(L_, 4);
STACK_CHECK_START_REL(L_, 0);
// this slot will contain the shortest name we found when we are done
lua_pushnil(L_); // L_: o nil
// push a cache that will contain all already visited tables
lua_newtable(L_); // L_: o nil {c}
// push a table whose contents are strings that, when concatenated, produce unique name
lua_newtable(L_); // L_: o nil {c} {fqn}
// {fqn}[1] = "_G"
luaG_pushstring(L_, LUA_GNAME); // L_: o nil {c} {fqn} "_G"
lua_rawseti(L_, -2, 1); // L_: o nil {c} {fqn}
// this is where we start the search
luaG_pushglobaltable(L_); // L_: o nil {c} {fqn} _G
auto const _foundInG{ DiscoverObjectNameRecur(L_, FqnLength{ std::numeric_limits<FqnLength::type>::max() }) };
if (lua_isnil(L_, 2)) { // try again with registry, just in case...
LUA_ASSERT(L_, _foundInG == std::numeric_limits<FqnLength::type>::max());
lua_pop(L_, 1); // L_: o nil {c} {fqn}
luaG_pushstring(L_, "_R"); // L_: o nil {c} {fqn} "_R"
lua_rawseti(L_, -2, 1); // L_: o nil {c} {fqn}
lua_pushvalue(L_, kIdxRegistry); // L_: o nil {c} {fqn} _R
[[maybe_unused]] auto const _foundInR{ DiscoverObjectNameRecur(L_, FqnLength{ std::numeric_limits<FqnLength::type>::max() }) };
}
lua_pop(L_, 3); // L_: o "result"
STACK_CHECK(L_, 1);
lua_pushstring(L_, luaL_typename(L_, 1)); // L_: o "result" "type"
lua_replace(L_, -3); // L_: "type" "result"
return 2;
}
|