aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
index ba0785b..5f12995 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -129,7 +129,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L
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(fprintf(stderr, INDENT_BEGIN "update_lookup_entry()\n" INDENT_END(U_))); 132 DEBUGSPEW_CODE(DebugSpew(U_) << "update_lookup_entry()" << std::endl);
133 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); 133 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ });
134 134
135 STACK_CHECK_START_REL(L_, 0); 135 STACK_CHECK_START_REL(L_, 0);
@@ -154,7 +154,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 154 // 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 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 if (!_prevName.empty() && (_prevName.size() < _newName.size() || lua_lessthan(L_, -2, -1))) { 156 if (!_prevName.empty() && (_prevName.size() < _newName.size() || lua_lessthan(L_, -2, -1))) {
157 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s' remained named '%s'\n" INDENT_END(U_), lua_typename(L_, lua_type(L_, -3)), _newName.data(), _prevName.data())); 157 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! 158 // the previous name is 'smaller' than the one we just generated: keep it!
159 lua_pop(L_, 3); // L_: ... {bfc} k 159 lua_pop(L_, 3); // L_: ... {bfc} k
160 } else { 160 } else {
@@ -168,7 +168,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L
168 } else { 168 } else {
169 lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" 169 lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n"
170 } 170 }
171 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s'\n" INDENT_END(U_), lua_typename(L_, lua_type(L_, -2)), _newName.data())); 171 DEBUGSPEW_CODE(DebugSpew(U_) << lua_typename(L_, lua_type(L_, -2)) << " '" << _newName << "'" << std::endl);
172 // prepare the stack for database feed 172 // prepare the stack for database feed
173 lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" 173 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 174 lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o
@@ -195,7 +195,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
195 int const _fqn{ dbIdx_ + 1 }; 195 int const _fqn{ dbIdx_ + 1 };
196 // slot dbIdx_ + 2 contains a cache that stores all already visited tables to avoid infinite recursion loops 196 // slot dbIdx_ + 2 contains a cache that stores all already visited tables to avoid infinite recursion loops
197 int const _cache{ dbIdx_ + 2 }; 197 int const _cache{ dbIdx_ + 2 };
198 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "populate_func_lookup_table_recur()\n" INDENT_END(U_))); 198 DEBUGSPEW_CODE(DebugSpew(U_) << "populate_func_lookup_table_recur()" << std::endl);
199 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); 199 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ });
200 200
201 STACK_GROW(L_, 6); 201 STACK_GROW(L_, 6);
@@ -215,7 +215,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
215 lua_pop(L_, 1); // L_: ... {i_} 215 lua_pop(L_, 1); // L_: ... {i_}
216 STACK_CHECK(L_, 0); 216 STACK_CHECK(L_, 0);
217 if (_visit_count > 0) { 217 if (_visit_count > 0) {
218 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "already visited\n" INDENT_END(U_))); 218 DEBUGSPEW_CODE(DebugSpew(U_) << "already visited" << std::endl);
219 return; 219 return;
220 } 220 }
221 221
@@ -262,8 +262,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
262 ++depth_; 262 ++depth_;
263 lua_pushnil(L_); // L_: ... {i_} {bfc} nil 263 lua_pushnil(L_); // L_: ... {i_} {bfc} nil
264 while (lua_next(L_, breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {} 264 while (lua_next(L_, breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {}
265 DEBUGSPEW_CODE(char const* _key = (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostring(L_, -2) : "not a string"); 265 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(fprintf(stderr, INDENT_BEGIN "table '%s'\n" INDENT_END(U_), _key)); 266 DEBUGSPEW_CODE(DebugSpew(U_) << "table '"<< _key <<"'" << std::endl);
267 DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ U_ }); 267 DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ U_ });
268 // un-visit this table in case we do need to process it 268 // un-visit this table in case we do need to process it
269 lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} 269 lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {}
@@ -298,11 +298,12 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U_)
298// ################################################################################################# 298// #################################################################################################
299 299
300// create a "fully.qualified.name" <-> function equivalence database 300// create a "fully.qualified.name" <-> function equivalence database
301void populate_func_lookup_table(lua_State* L_, int i_, char const* name_) 301void populate_func_lookup_table(lua_State* const L_, int const i_, std::string_view const& name_)
302{ 302{
303 int const _in_base = lua_absindex(L_, i_); 303 int const _in_base = lua_absindex(L_, i_);
304 DEBUGSPEW_CODE(Universe* _U = universe_get(L_)); 304 DEBUGSPEW_CODE(Universe* _U = universe_get(L_));
305 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END(_U), L_, name_ ? name_ : "nullptr")); 305 std::string_view _name{ name_.empty() ? std::string_view{} : name_ };
306 DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": populate_func_lookup_table('" << _name << "')" << std::endl);
306 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 307 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
307 STACK_GROW(L_, 3); 308 STACK_GROW(L_, 3);
308 STACK_CHECK_START_REL(L_, 0); 309 STACK_CHECK_START_REL(L_, 0);
@@ -311,20 +312,22 @@ void populate_func_lookup_table(lua_State* L_, int i_, char const* name_)
311 STACK_CHECK(L_, 1); 312 STACK_CHECK(L_, 1);
312 LUA_ASSERT(L_, lua_istable(L_, -1)); 313 LUA_ASSERT(L_, lua_istable(L_, -1));
313 if (lua_type(L_, _in_base) == LUA_TFUNCTION) { // for example when a module is a simple function 314 if (lua_type(L_, _in_base) == LUA_TFUNCTION) { // for example when a module is a simple function
314 name_ = name_ ? name_ : "nullptr"; 315 if (_name.empty()) {
316 _name = "nullptr";
317 }
315 lua_pushvalue(L_, _in_base); // L_: {} f 318 lua_pushvalue(L_, _in_base); // L_: {} f
316 lua_pushstring(L_, name_); // L_: {} f _name 319 std::ignore = lua_pushstringview(L_, _name); // L_: {} f name_
317 lua_rawset(L_, -3); // L_: {} 320 lua_rawset(L_, -3); // L_: {}
318 lua_pushstring(L_, name_); // L_: {} _name 321 std::ignore = lua_pushstringview(L_, _name); // L_: {} name_
319 lua_pushvalue(L_, _in_base); // L_: {} _name f 322 lua_pushvalue(L_, _in_base); // L_: {} name_ f
320 lua_rawset(L_, -3); // L_: {} 323 lua_rawset(L_, -3); // L_: {}
321 lua_pop(L_, 1); // L_: 324 lua_pop(L_, 1); // L_:
322 } else if (lua_type(L_, _in_base) == LUA_TTABLE) { 325 } else if (lua_type(L_, _in_base) == LUA_TTABLE) {
323 lua_newtable(L_); // L_: {} {fqn} 326 lua_newtable(L_); // L_: {} {fqn}
324 int _startDepth{ 0 }; 327 int _startDepth{ 0 };
325 if (name_) { 328 if (!_name.empty()) {
326 STACK_CHECK(L_, 2); 329 STACK_CHECK(L_, 2);
327 lua_pushstring(L_, name_); // L_: {} {fqn} "name" 330 std::ignore = lua_pushstringview(L_, _name); // L_: {} {fqn} "name"
328 // generate a name, and if we already had one name, keep whichever is the shorter 331 // generate a name, and if we already had one name, keep whichever is the shorter
329 lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t 332 lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t
330 update_lookup_entry(DEBUGSPEW_PARAM_COMMA(_U) L_, _dbIdx, _startDepth); // L_: {} {fqn} "name" 333 update_lookup_entry(DEBUGSPEW_PARAM_COMMA(_U) L_, _dbIdx, _startDepth); // L_: {} {fqn} "name"