diff options
Diffstat (limited to 'src/tools.cpp')
| -rw-r--r-- | src/tools.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/tools.cpp b/src/tools.cpp index cbfefb0..25949e4 100644 --- a/src/tools.cpp +++ b/src/tools.cpp | |||
| @@ -63,26 +63,26 @@ static int dummy_writer([[maybe_unused]] lua_State* const L_, [[maybe_unused]] v | |||
| 63 | * +-----------------+-------------------+------------+----------+ | 63 | * +-----------------+-------------------+------------+----------+ |
| 64 | * | lua_tocfunction | nullptr | | nullptr | | 64 | * | lua_tocfunction | nullptr | | nullptr | |
| 65 | * +-----------------+-------------------+------------+----------+ | 65 | * +-----------------+-------------------+------------+----------+ |
| 66 | * | luaG_dump | kWriterReturnCode | 1 | 1 | | 66 | * | luaW_dump | kWriterReturnCode | 1 | 1 | |
| 67 | * +-----------------+-------------------+------------+----------+ | 67 | * +-----------------+-------------------+------------+----------+ |
| 68 | */ | 68 | */ |
| 69 | 69 | ||
| 70 | [[nodiscard]] | 70 | [[nodiscard]] |
| 71 | FuncSubType luaG_getfuncsubtype(lua_State* const L_, StackIndex const i_) | 71 | FuncSubType luaW_getfuncsubtype(lua_State* const L_, StackIndex const i_) |
| 72 | { | 72 | { |
| 73 | if (lua_tocfunction(L_, i_)) { // nullptr for LuaJIT-fast && bytecode functions | 73 | if (lua_tocfunction(L_, i_)) { // nullptr for LuaJIT-fast && bytecode functions |
| 74 | return FuncSubType::Native; | 74 | return FuncSubType::Native; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | // luaG_dump expects the function at the top of the stack | 77 | // luaW_dump expects the function at the top of the stack |
| 78 | int const _popCount{ (luaG_absindex(L_, i_) == lua_gettop(L_)) ? 0 : (lua_pushvalue(L_, i_), 1) }; | 78 | int const _popCount{ (luaW_absindex(L_, i_) == lua_gettop(L_)) ? 0 : (lua_pushvalue(L_, i_), 1) }; |
| 79 | // here we either have a Lua bytecode or a LuaJIT-compiled function | 79 | // here we either have a Lua bytecode or a LuaJIT-compiled function |
| 80 | int const _dumpres{ luaG_dump(L_, dummy_writer, nullptr, 0) }; | 80 | int const _dumpres{ luaW_dump(L_, dummy_writer, nullptr, 0) }; |
| 81 | if (_popCount > 0) { | 81 | if (_popCount > 0) { |
| 82 | lua_pop(L_, _popCount); | 82 | lua_pop(L_, _popCount); |
| 83 | } | 83 | } |
| 84 | if (_dumpres == kWriterReturnCode) { | 84 | if (_dumpres == kWriterReturnCode) { |
| 85 | // anytime we get kWriterReturnCode, this means that luaG_dump() attempted a dump | 85 | // anytime we get kWriterReturnCode, this means that luaW_dump() attempted a dump |
| 86 | return FuncSubType::Bytecode; | 86 | return FuncSubType::Bytecode; |
| 87 | } | 87 | } |
| 88 | // we didn't try to dump, therefore this is a LuaJIT-fast function | 88 | // we didn't try to dump, therefore this is a LuaJIT-fast function |
| @@ -115,7 +115,7 @@ namespace tools { | |||
| 115 | // &b is popped at that point (-> replaced by the result) | 115 | // &b is popped at that point (-> replaced by the result) |
| 116 | luaL_pushresult(&_b); // L_: ... {} ... "<result>" | 116 | luaL_pushresult(&_b); // L_: ... {} ... "<result>" |
| 117 | STACK_CHECK(L_, 1); | 117 | STACK_CHECK(L_, 1); |
| 118 | return luaG_tostring(L_, kIdxTop); | 118 | return luaW_tostring(L_, kIdxTop); |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | } // namespace tools | 121 | } // namespace tools |
| @@ -145,10 +145,10 @@ static void update_lookup_entry(lua_State* const L_, StackIndex const ctxBase_, | |||
| 145 | // first, raise an error if the function is already known | 145 | // first, raise an error if the function is already known |
| 146 | lua_pushvalue(L_, -1); // L_: ... {bfc} k o o | 146 | lua_pushvalue(L_, -1); // L_: ... {bfc} k o o |
| 147 | lua_rawget(L_, _dest); // L_: ... {bfc} k o name? | 147 | lua_rawget(L_, _dest); // L_: ... {bfc} k o name? |
| 148 | std::string_view const _prevName{ luaG_tostring(L_, kIdxTop) }; // nullptr if we got nil (first encounter of this object) | 148 | std::string_view const _prevName{ luaW_tostring(L_, kIdxTop) }; // nullptr if we got nil (first encounter of this object) |
| 149 | // push name in fqn stack (note that concatenation will crash if name is a not string or a number) | 149 | // push name in fqn stack (note that concatenation will crash if name is a not string or a number) |
| 150 | lua_pushvalue(L_, -3); // L_: ... {bfc} k o name? k | 150 | lua_pushvalue(L_, -3); // L_: ... {bfc} k o name? k |
| 151 | LUA_ASSERT(L_, luaG_type(L_, kIdxTop) == LuaType::NUMBER || luaG_type(L_, kIdxTop) == LuaType::STRING); | 151 | LUA_ASSERT(L_, luaW_type(L_, kIdxTop) == LuaType::NUMBER || luaW_type(L_, kIdxTop) == LuaType::STRING); |
| 152 | TableIndex const _deeper{ depth_ + 1 }; | 152 | TableIndex const _deeper{ depth_ + 1 }; |
| 153 | lua_rawseti(L_, _fqn, _deeper); // L_: ... {bfc} k o name? | 153 | lua_rawseti(L_, _fqn, _deeper); // L_: ... {bfc} k o name? |
| 154 | // generate name | 154 | // generate name |
| @@ -161,7 +161,7 @@ static void update_lookup_entry(lua_State* const L_, StackIndex const ctxBase_, | |||
| 161 | // Therefore, when we encounter an object for which a name was previously registered, we need to select a single name | 161 | // Therefore, when we encounter an object for which a name was previously registered, we need to select a single name |
| 162 | // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded | 162 | // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded |
| 163 | if (!_prevName.empty() && ((_prevName.size() < _newName.size()) || (_prevName <= _newName))) { | 163 | if (!_prevName.empty() && ((_prevName.size() < _newName.size()) || (_prevName <= _newName))) { |
| 164 | DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, StackIndex{ -3 }) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); | 164 | DEBUGSPEW_CODE(DebugSpew(_U) << luaW_typename(L_, StackIndex{ -3 }) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); |
| 165 | // the previous name is 'smaller' than the one we just generated: keep it! | 165 | // the previous name is 'smaller' than the one we just generated: keep it! |
| 166 | lua_pop(L_, 3); // L_: ... {bfc} k | 166 | lua_pop(L_, 3); // L_: ... {bfc} k |
| 167 | } else { | 167 | } else { |
| @@ -175,7 +175,7 @@ static void update_lookup_entry(lua_State* const L_, StackIndex const ctxBase_, | |||
| 175 | } else { | 175 | } else { |
| 176 | lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" | 176 | lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" |
| 177 | } | 177 | } |
| 178 | DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, StackIndex{ -2 }) << " '" << _newName << "'" << std::endl); | 178 | DEBUGSPEW_CODE(DebugSpew(_U) << luaW_typename(L_, StackIndex{ -2 }) << " '" << _newName << "'" << std::endl); |
| 179 | // prepare the stack for database feed | 179 | // prepare the stack for database feed |
| 180 | lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" | 180 | lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" |
| 181 | lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o | 181 | lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o |
| @@ -210,7 +210,7 @@ static void populate_lookup_table_recur(lua_State* const L_, StackIndex const db | |||
| 210 | STACK_CHECK_START_REL(L_, 0); // L_: ... {i_} | 210 | STACK_CHECK_START_REL(L_, 0); // L_: ... {i_} |
| 211 | 211 | ||
| 212 | // if object is a userdata, replace it by its metatable | 212 | // if object is a userdata, replace it by its metatable |
| 213 | if (luaG_type(L_, i_) == LuaType::USERDATA) { | 213 | if (luaW_type(L_, i_) == LuaType::USERDATA) { |
| 214 | lua_getmetatable(L_, i_); // L_: ... {i_} mt | 214 | lua_getmetatable(L_, i_); // L_: ... {i_} mt |
| 215 | lua_replace(L_, i_); // L_: ... {i_} | 215 | lua_replace(L_, i_); // L_: ... {i_} |
| 216 | } | 216 | } |
| @@ -239,7 +239,7 @@ static void populate_lookup_table_recur(lua_State* const L_, StackIndex const db | |||
| 239 | lua_pushnil(L_); // L_: ... {i_} {bfc} nil | 239 | lua_pushnil(L_); // L_: ... {i_} {bfc} nil |
| 240 | while (lua_next(L_, i_) != 0) { // L_: ... {i_} {bfc} k v | 240 | while (lua_next(L_, i_) != 0) { // L_: ... {i_} {bfc} k v |
| 241 | // just for debug, not actually needed | 241 | // just for debug, not actually needed |
| 242 | // std::string_view const _key{ (luaG_type(L_, -2) == LuaType::STRING) ? luaG_tostring(L_, -2) : "not a string" }; | 242 | // std::string_view const _key{ (luaW_type(L_, -2) == LuaType::STRING) ? luaW_tostring(L_, -2) : "not a string" }; |
| 243 | // subtable: process it recursively | 243 | // subtable: process it recursively |
| 244 | if (lua_istable(L_, kIdxTop)) { // L_: ... {i_} {bfc} k {} | 244 | if (lua_istable(L_, kIdxTop)) { // L_: ... {i_} {bfc} k {} |
| 245 | // increment visit count to make sure we will actually scan it at this recursive level | 245 | // increment visit count to make sure we will actually scan it at this recursive level |
| @@ -256,11 +256,11 @@ static void populate_lookup_table_recur(lua_State* const L_, StackIndex const db | |||
| 256 | lua_rawset(L_, _breadthFirstCache); // L_: ... {i_} {bfc} k {} | 256 | lua_rawset(L_, _breadthFirstCache); // L_: ... {i_} {bfc} k {} |
| 257 | // generate a name, and if we already had one name, keep whichever is the shorter | 257 | // generate a name, and if we already had one name, keep whichever is the shorter |
| 258 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k | 258 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k |
| 259 | } else if (lua_isfunction(L_, kIdxTop) && (luaG_getfuncsubtype(L_, kIdxTop) != FuncSubType::Bytecode)) { | 259 | } else if (lua_isfunction(L_, kIdxTop) && (luaW_getfuncsubtype(L_, kIdxTop) != FuncSubType::Bytecode)) { |
| 260 | // generate a name, and if we already had one name, keep whichever is the shorter | 260 | // generate a name, and if we already had one name, keep whichever is the shorter |
| 261 | // this pops the function from the stack | 261 | // this pops the function from the stack |
| 262 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k | 262 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k |
| 263 | } else if (luaG_type(L_, kIdxTop) == LuaType::USERDATA) { | 263 | } else if (luaW_type(L_, kIdxTop) == LuaType::USERDATA) { |
| 264 | // generate a name, and if we already had one name, keep whichever is the shorter | 264 | // generate a name, and if we already had one name, keep whichever is the shorter |
| 265 | // this pops the userdata from the stack | 265 | // this pops the userdata from the stack |
| 266 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k | 266 | update_lookup_entry(L_, dbIdx_, depth_); // L_: ... {i_} {bfc} k |
| @@ -273,13 +273,13 @@ static void populate_lookup_table_recur(lua_State* const L_, StackIndex const db | |||
| 273 | TableIndex const _deeper{ depth_ + 1 }; | 273 | TableIndex const _deeper{ depth_ + 1 }; |
| 274 | lua_pushnil(L_); // L_: ... {i_} {bfc} nil | 274 | lua_pushnil(L_); // L_: ... {i_} {bfc} nil |
| 275 | while (lua_next(L_, _breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {} | 275 | while (lua_next(L_, _breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {} |
| 276 | DEBUGSPEW_CODE(std::string_view const _key{ (luaG_type(L_, StackIndex{ -2 }) == LuaType::STRING) ? luaG_tostring(L_, StackIndex{ -2 }) : std::string_view{ "<not a string>" } }); | 276 | DEBUGSPEW_CODE(std::string_view const _key{ (luaW_type(L_, StackIndex{ -2 }) == LuaType::STRING) ? luaW_tostring(L_, StackIndex{ -2 }) : std::string_view{ "<not a string>" } }); |
| 277 | DEBUGSPEW_CODE(DebugSpew(_U) << "table '"<< _key <<"'" << std::endl); | 277 | DEBUGSPEW_CODE(DebugSpew(_U) << "table '"<< _key <<"'" << std::endl); |
| 278 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); | 278 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); |
| 279 | // un-visit this table in case we do need to process it | 279 | // un-visit this table in case we do need to process it |
| 280 | lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} | 280 | lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} |
| 281 | lua_rawget(L_, _cache); // L_: ... {i_} {bfc} k {} n | 281 | lua_rawget(L_, _cache); // L_: ... {i_} {bfc} k {} n |
| 282 | LUA_ASSERT(L_, luaG_type(L_, kIdxTop) == LuaType::NUMBER); | 282 | LUA_ASSERT(L_, luaW_type(L_, kIdxTop) == LuaType::NUMBER); |
| 283 | _visit_count = lua_tointeger(L_, -1) - 1; | 283 | _visit_count = lua_tointeger(L_, -1) - 1; |
| 284 | lua_pop(L_, 1); // L_: ... {i_} {bfc} k {} | 284 | lua_pop(L_, 1); // L_: ... {i_} {bfc} k {} |
| 285 | lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} | 285 | lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} |
| @@ -312,7 +312,7 @@ namespace tools { | |||
| 312 | // create a "fully.qualified.name" <-> function equivalence database | 312 | // create a "fully.qualified.name" <-> function equivalence database |
| 313 | void PopulateFuncLookupTable(lua_State* const L_, StackIndex const i_, std::string_view const& name_) | 313 | void PopulateFuncLookupTable(lua_State* const L_, StackIndex const i_, std::string_view const& name_) |
| 314 | { | 314 | { |
| 315 | StackIndex const _in_base{ luaG_absindex(L_, i_) }; | 315 | StackIndex const _in_base{ luaW_absindex(L_, i_) }; |
| 316 | DEBUGSPEW_CODE(Universe* _U = Universe::Get(L_)); | 316 | DEBUGSPEW_CODE(Universe* _U = Universe::Get(L_)); |
| 317 | std::string_view _name{ name_.empty() ? std::string_view{} : name_ }; | 317 | std::string_view _name{ name_.empty() ? std::string_view{} : name_ }; |
| 318 | DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": PopulateFuncLookupTable('" << _name << "')" << std::endl); | 318 | DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": PopulateFuncLookupTable('" << _name << "')" << std::endl); |
| @@ -323,24 +323,24 @@ namespace tools { | |||
| 323 | StackIndex const _dbIdx{ lua_gettop(L_) }; | 323 | StackIndex const _dbIdx{ lua_gettop(L_) }; |
| 324 | STACK_CHECK(L_, 1); | 324 | STACK_CHECK(L_, 1); |
| 325 | LUA_ASSERT(L_, lua_istable(L_, -1)); | 325 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
| 326 | LuaType const _moduleType{ luaG_type(L_, _in_base) }; | 326 | LuaType const _moduleType{ luaW_type(L_, _in_base) }; |
| 327 | if ((_moduleType == LuaType::FUNCTION) || (_moduleType == LuaType::USERDATA)) { // for example when a module is a simple function | 327 | if ((_moduleType == LuaType::FUNCTION) || (_moduleType == LuaType::USERDATA)) { // for example when a module is a simple function |
| 328 | if (_name.empty()) { | 328 | if (_name.empty()) { |
| 329 | _name = "nullptr"; | 329 | _name = "nullptr"; |
| 330 | } | 330 | } |
| 331 | lua_pushvalue(L_, _in_base); // L_: {} f | 331 | lua_pushvalue(L_, _in_base); // L_: {} f |
| 332 | luaG_pushstring(L_, _name); // L_: {} f name_ | 332 | luaW_pushstring(L_, _name); // L_: {} f name_ |
| 333 | lua_rawset(L_, -3); // L_: {} | 333 | lua_rawset(L_, -3); // L_: {} |
| 334 | luaG_pushstring(L_, _name); // L_: {} name_ | 334 | luaW_pushstring(L_, _name); // L_: {} name_ |
| 335 | lua_pushvalue(L_, _in_base); // L_: {} name_ f | 335 | lua_pushvalue(L_, _in_base); // L_: {} name_ f |
| 336 | lua_rawset(L_, -3); // L_: {} | 336 | lua_rawset(L_, -3); // L_: {} |
| 337 | lua_pop(L_, 1); // L_: | 337 | lua_pop(L_, 1); // L_: |
| 338 | } else if (luaG_type(L_, _in_base) == LuaType::TABLE) { | 338 | } else if (luaW_type(L_, _in_base) == LuaType::TABLE) { |
| 339 | lua_newtable(L_); // L_: {} {fqn} | 339 | lua_newtable(L_); // L_: {} {fqn} |
| 340 | TableIndex _startDepth{ 0 }; | 340 | TableIndex _startDepth{ 0 }; |
| 341 | if (!_name.empty()) { | 341 | if (!_name.empty()) { |
| 342 | STACK_CHECK(L_, 2); | 342 | STACK_CHECK(L_, 2); |
| 343 | luaG_pushstring(L_, _name); // L_: {} {fqn} "name" | 343 | luaW_pushstring(L_, _name); // L_: {} {fqn} "name" |
| 344 | // generate a name, and if we already had one name, keep whichever is the shorter | 344 | // generate a name, and if we already had one name, keep whichever is the shorter |
| 345 | lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t | 345 | lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t |
| 346 | update_lookup_entry(L_, _dbIdx, _startDepth); // L_: {} {fqn} "name" | 346 | update_lookup_entry(L_, _dbIdx, _startDepth); // L_: {} {fqn} "name" |
| @@ -355,7 +355,7 @@ namespace tools { | |||
| 355 | lua_pop(L_, 3); // L_: | 355 | lua_pop(L_, 3); // L_: |
| 356 | } else { | 356 | } else { |
| 357 | lua_pop(L_, 1); // L_: | 357 | lua_pop(L_, 1); // L_: |
| 358 | raise_luaL_error(L_, "unsupported module type %s", luaG_typename(L_, _in_base).data()); | 358 | raise_luaL_error(L_, "unsupported module type %s", luaW_typename(L_, _in_base).data()); |
| 359 | } | 359 | } |
| 360 | STACK_CHECK(L_, 0); | 360 | STACK_CHECK(L_, 0); |
| 361 | } | 361 | } |
| @@ -373,7 +373,7 @@ namespace tools { | |||
| 373 | +[](lua_State* L_) | 373 | +[](lua_State* L_) |
| 374 | { | 374 | { |
| 375 | int const _args{ lua_gettop(L_) }; // L_: args... | 375 | int const _args{ lua_gettop(L_) }; // L_: args... |
| 376 | //[[maybe_unused]] std::string_view const _modname{ luaG_checkstring(L_, 1) }; | 376 | //[[maybe_unused]] std::string_view const _modname{ luaW_checkstring(L_, 1) }; |
| 377 | 377 | ||
| 378 | STACK_GROW(L_, 1); | 378 | STACK_GROW(L_, 1); |
| 379 | 379 | ||
