aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp34
1 files changed, 18 insertions, 16 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
index 5f12995..1d778ef 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -122,15 +122,16 @@ static constexpr int kWriterReturnCode{ 666 };
122 * if we already had an entry of type [o] = ..., replace the name if the new one is shorter 122 * if we already had an entry of type [o] = ..., replace the name if the new one is shorter
123 * pops the processed object from the stack 123 * pops the processed object from the stack
124 */ 124 */
125static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, int ctxBase_, int depth_) 125static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_)
126{ 126{
127 // slot 1 in the stack contains the table that receives everything we found 127 // slot 1 in the stack contains the table that receives everything we found
128 int const _dest{ ctxBase_ }; 128 int const _dest{ ctxBase_ };
129 // slot 2 contains a table that, when concatenated, produces the fully qualified name of scanned elements in the table provided at slot _i 129 // slot 2 contains a table that, when concatenated, produces the fully qualified name of scanned elements in the table provided at slot _i
130 int const _fqn{ ctxBase_ + 1 }; 130 int const _fqn{ ctxBase_ + 1 };
131 131
132 DEBUGSPEW_CODE(DebugSpew(U_) << "update_lookup_entry()" << std::endl); 132 DEBUGSPEW_CODE(Universe* const _U{ universe_get(L_) });
133 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); 133 DEBUGSPEW_CODE(DebugSpew(_U) << "update_lookup_entry()" << std::endl);
134 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
134 135
135 STACK_CHECK_START_REL(L_, 0); 136 STACK_CHECK_START_REL(L_, 0);
136 // first, raise an error if the function is already known 137 // first, raise an error if the function is already known
@@ -154,7 +155,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L
154 // Therefore, when we encounter an object for which a name was previously registered, we need to select the names 155 // Therefore, when we encounter an object for which a name was previously registered, we need to select the names
155 // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded 156 // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded
156 if (!_prevName.empty() && (_prevName.size() < _newName.size() || lua_lessthan(L_, -2, -1))) { 157 if (!_prevName.empty() && (_prevName.size() < _newName.size() || lua_lessthan(L_, -2, -1))) {
157 DEBUGSPEW_CODE(DebugSpew(U_) << lua_typename(L_, lua_type(L_, -3)) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); 158 DEBUGSPEW_CODE(DebugSpew(_U) << lua_typename(L_, lua_type(L_, -3)) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl);
158 // the previous name is 'smaller' than the one we just generated: keep it! 159 // the previous name is 'smaller' than the one we just generated: keep it!
159 lua_pop(L_, 3); // L_: ... {bfc} k 160 lua_pop(L_, 3); // L_: ... {bfc} k
160 } else { 161 } else {
@@ -168,7 +169,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L
168 } else { 169 } else {
169 lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" 170 lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n"
170 } 171 }
171 DEBUGSPEW_CODE(DebugSpew(U_) << lua_typename(L_, lua_type(L_, -2)) << " '" << _newName << "'" << std::endl); 172 DEBUGSPEW_CODE(DebugSpew(_U) << lua_typename(L_, lua_type(L_, -2)) << " '" << _newName << "'" << std::endl);
172 // prepare the stack for database feed 173 // prepare the stack for database feed
173 lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" 174 lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n"
174 lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o 175 lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o
@@ -188,15 +189,16 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L
188 189
189// ################################################################################################# 190// #################################################################################################
190 191
191static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, int dbIdx_, int i_, int depth_) 192static void populate_func_lookup_table_recur(lua_State* L_, int dbIdx_, int i_, int depth_)
192{ 193{
193 // slot dbIdx_ contains the lookup database table 194 // slot dbIdx_ contains the lookup database table
194 // slot dbIdx_ + 1 contains a table that, when concatenated, produces the fully qualified name of scanned elements in the table provided at slot i_ 195 // slot dbIdx_ + 1 contains a table that, when concatenated, produces the fully qualified name of scanned elements in the table provided at slot i_
195 int const _fqn{ dbIdx_ + 1 }; 196 int const _fqn{ dbIdx_ + 1 };
196 // slot dbIdx_ + 2 contains a cache that stores all already visited tables to avoid infinite recursion loops 197 // slot dbIdx_ + 2 contains a cache that stores all already visited tables to avoid infinite recursion loops
197 int const _cache{ dbIdx_ + 2 }; 198 int const _cache{ dbIdx_ + 2 };
198 DEBUGSPEW_CODE(DebugSpew(U_) << "populate_func_lookup_table_recur()" << std::endl); 199 DEBUGSPEW_CODE(Universe* const _U{ universe_get(L_) });
199 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); 200 DEBUGSPEW_CODE(DebugSpew(_U) << "populate_func_lookup_table_recur()" << std::endl);
201 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
200 202
201 STACK_GROW(L_, 6); 203 STACK_GROW(L_, 6);
202 // slot i_ contains a table where we search for functions (or a full userdata with a metatable) 204 // slot i_ contains a table where we search for functions (or a full userdata with a metatable)
@@ -215,7 +217,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
215 lua_pop(L_, 1); // L_: ... {i_} 217 lua_pop(L_, 1); // L_: ... {i_}
216 STACK_CHECK(L_, 0); 218 STACK_CHECK(L_, 0);
217 if (_visit_count > 0) { 219 if (_visit_count > 0) {
218 DEBUGSPEW_CODE(DebugSpew(U_) << "already visited" << std::endl); 220 DEBUGSPEW_CODE(DebugSpew(_U) << "already visited" << std::endl);
219 return; 221 return;
220 } 222 }
221 223
@@ -248,11 +250,11 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
248 lua_pushvalue(L_, -2); // L_: ... {i_} {bfc} k {} k {} 250 lua_pushvalue(L_, -2); // L_: ... {i_} {bfc} k {} k {}
249 lua_rawset(L_, breadthFirstCache); // L_: ... {i_} {bfc} k {} 251 lua_rawset(L_, breadthFirstCache); // L_: ... {i_} {bfc} k {}
250 // generate a name, and if we already had one name, keep whichever is the shorter 252 // generate a name, and if we already had one name, keep whichever is the shorter
251 update_lookup_entry(DEBUGSPEW_PARAM_COMMA(U_) L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k 253 update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k
252 } else if (lua_isfunction(L_, -1) && (luaG_getfuncsubtype(L_, -1) != FuncSubType::Bytecode)) { 254 } else if (lua_isfunction(L_, -1) && (luaG_getfuncsubtype(L_, -1) != FuncSubType::Bytecode)) {
253 // generate a name, and if we already had one name, keep whichever is the shorter 255 // generate a name, and if we already had one name, keep whichever is the shorter
254 // this pops the function from the stack 256 // this pops the function from the stack
255 update_lookup_entry(DEBUGSPEW_PARAM_COMMA(U_) L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k 257 update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k
256 } else { 258 } else {
257 lua_pop(L_, 1); // L_: ... {i_} {bfc} k 259 lua_pop(L_, 1); // L_: ... {i_} {bfc} k
258 } 260 }
@@ -263,8 +265,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
263 lua_pushnil(L_); // L_: ... {i_} {bfc} nil 265 lua_pushnil(L_); // L_: ... {i_} {bfc} nil
264 while (lua_next(L_, breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {} 266 while (lua_next(L_, breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {}
265 DEBUGSPEW_CODE(std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : std::string_view{ "<not a string>" } }); 267 DEBUGSPEW_CODE(std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : std::string_view{ "<not a string>" } });
266 DEBUGSPEW_CODE(DebugSpew(U_) << "table '"<< _key <<"'" << std::endl); 268 DEBUGSPEW_CODE(DebugSpew(_U) << "table '"<< _key <<"'" << std::endl);
267 DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ U_ }); 269 DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U });
268 // un-visit this table in case we do need to process it 270 // un-visit this table in case we do need to process it
269 lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} 271 lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {}
270 lua_rawget(L_, _cache); // L_: ... {i_} {bfc} k {} n 272 lua_rawget(L_, _cache); // L_: ... {i_} {bfc} k {} n
@@ -281,7 +283,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
281 // push table name in fqn stack (note that concatenation will crash if name is a not string!) 283 // push table name in fqn stack (note that concatenation will crash if name is a not string!)
282 lua_pushvalue(L_, -2); // L_: ... {i_} {bfc} k {} k 284 lua_pushvalue(L_, -2); // L_: ... {i_} {bfc} k {} k
283 lua_rawseti(L_, _fqn, depth_); // L_: ... {i_} {bfc} k {} 285 lua_rawseti(L_, _fqn, depth_); // L_: ... {i_} {bfc} k {}
284 populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(U_) L_, dbIdx_, lua_gettop(L_), depth_); 286 populate_func_lookup_table_recur(L_, dbIdx_, lua_gettop(L_), depth_);
285 lua_pop(L_, 1); // L_: ... {i_} {bfc} k 287 lua_pop(L_, 1); // L_: ... {i_} {bfc} k
286 STACK_CHECK(L_, 2); 288 STACK_CHECK(L_, 2);
287 } 289 }
@@ -330,7 +332,7 @@ void populate_func_lookup_table(lua_State* const L_, int const i_, std::string_v
330 std::ignore = lua_pushstringview(L_, _name); // L_: {} {fqn} "name" 332 std::ignore = lua_pushstringview(L_, _name); // L_: {} {fqn} "name"
331 // generate a name, and if we already had one name, keep whichever is the shorter 333 // generate a name, and if we already had one name, keep whichever is the shorter
332 lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t 334 lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t
333 update_lookup_entry(DEBUGSPEW_PARAM_COMMA(_U) L_, _dbIdx, _startDepth); // L_: {} {fqn} "name" 335 update_lookup_entry(L_, _dbIdx, _startDepth); // L_: {} {fqn} "name"
334 // don't forget to store the name at the bottom of the fqn stack 336 // don't forget to store the name at the bottom of the fqn stack
335 lua_rawseti(L_, -2, ++_startDepth); // L_: {} {fqn} 337 lua_rawseti(L_, -2, ++_startDepth); // L_: {} {fqn}
336 STACK_CHECK(L_, 2); 338 STACK_CHECK(L_, 2);
@@ -338,7 +340,7 @@ void populate_func_lookup_table(lua_State* const L_, int const i_, std::string_v
338 // retrieve the cache, create it if we haven't done it yet 340 // retrieve the cache, create it if we haven't done it yet
339 std::ignore = kLookupCacheRegKey.getSubTable(L_, 0, 0); // L_: {} {fqn} {cache} 341 std::ignore = kLookupCacheRegKey.getSubTable(L_, 0, 0); // L_: {} {fqn} {cache}
340 // process everything we find in that table, filling in lookup data for all functions and tables we see there 342 // process everything we find in that table, filling in lookup data for all functions and tables we see there
341 populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(_U) L_, _dbIdx, _in_base, _startDepth); 343 populate_func_lookup_table_recur(L_, _dbIdx, _in_base, _startDepth);
342 lua_pop(L_, 3); // L_: 344 lua_pop(L_, 3); // L_:
343 } else { 345 } else {
344 lua_pop(L_, 1); // L_: 346 lua_pop(L_, 1); // L_: