diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-20 18:03:33 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-20 18:03:33 +0100 |
commit | 0f6f3c207c46fa28059403e5f16af774d20c7ea2 (patch) | |
tree | 8031145bb2534c33ca0f2c65b9de85696c8a8ee0 /src/intercopycontext.cpp | |
parent | 304e4dfabe4555dff4aa72e75b677405fd30d1b3 (diff) | |
download | lanes-0f6f3c207c46fa28059403e5f16af774d20c7ea2.tar.gz lanes-0f6f3c207c46fa28059403e5f16af774d20c7ea2.tar.bz2 lanes-0f6f3c207c46fa28059403e5f16af774d20c7ea2.zip |
More [[nodiscard]] boyscouting
Diffstat (limited to 'src/intercopycontext.cpp')
-rw-r--r-- | src/intercopycontext.cpp | 69 |
1 files changed, 46 insertions, 23 deletions
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 672d49e..568e4cb 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp | |||
@@ -40,7 +40,8 @@ THE SOFTWARE. | |||
40 | // we have to do it that way because we can't unbalance the stack between buffer operations | 40 | // we have to do it that way because we can't unbalance the stack between buffer operations |
41 | // namely, this means we can't push a function on top of the stack *after* we initialize the buffer! | 41 | // namely, this means we can't push a function on top of the stack *after* we initialize the buffer! |
42 | // luckily, this also works with earlier Lua versions | 42 | // luckily, this also works with earlier Lua versions |
43 | [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) | 43 | [[nodiscard]] |
44 | static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) | ||
44 | { | 45 | { |
45 | luaL_Buffer* const _B{ static_cast<luaL_Buffer*>(ud_) }; | 46 | luaL_Buffer* const _B{ static_cast<luaL_Buffer*>(ud_) }; |
46 | if (!_B->L) { | 47 | if (!_B->L) { |
@@ -53,7 +54,8 @@ THE SOFTWARE. | |||
53 | // ################################################################################################# | 54 | // ################################################################################################# |
54 | 55 | ||
55 | // function sentinel used to transfer native functions from/to keeper states | 56 | // function sentinel used to transfer native functions from/to keeper states |
56 | [[nodiscard]] static int func_lookup_sentinel(lua_State* L_) | 57 | [[nodiscard]] |
58 | static int func_lookup_sentinel(lua_State* L_) | ||
57 | { | 59 | { |
58 | raise_luaL_error(L_, "function lookup sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); | 60 | raise_luaL_error(L_, "function lookup sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); |
59 | } | 61 | } |
@@ -61,7 +63,8 @@ THE SOFTWARE. | |||
61 | // ################################################################################################# | 63 | // ################################################################################################# |
62 | 64 | ||
63 | // function sentinel used to transfer native table from/to keeper states | 65 | // function sentinel used to transfer native table from/to keeper states |
64 | [[nodiscard]] static int table_lookup_sentinel(lua_State* L_) | 66 | [[nodiscard]] |
67 | static int table_lookup_sentinel(lua_State* L_) | ||
65 | { | 68 | { |
66 | raise_luaL_error(L_, "table lookup sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); | 69 | raise_luaL_error(L_, "table lookup sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); |
67 | } | 70 | } |
@@ -69,7 +72,8 @@ THE SOFTWARE. | |||
69 | // ################################################################################################# | 72 | // ################################################################################################# |
70 | 73 | ||
71 | // function sentinel used to transfer cloned full userdata from/to keeper states | 74 | // function sentinel used to transfer cloned full userdata from/to keeper states |
72 | [[nodiscard]] static int userdata_clone_sentinel(lua_State* L_) | 75 | [[nodiscard]] |
76 | static int userdata_clone_sentinel(lua_State* L_) | ||
73 | { | 77 | { |
74 | raise_luaL_error(L_, "userdata clone sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); | 78 | raise_luaL_error(L_, "userdata clone sentinel for %s, should never be called", lua_tostring(L_, lua_upvalueindex(1))); |
75 | } | 79 | } |
@@ -77,7 +81,8 @@ THE SOFTWARE. | |||
77 | // ################################################################################################# | 81 | // ################################################################################################# |
78 | 82 | ||
79 | // retrieve the name of a function/table in the lookup database | 83 | // retrieve the name of a function/table in the lookup database |
80 | [[nodiscard]] std::string_view InterCopyContext::findLookupName() const | 84 | [[nodiscard]] |
85 | std::string_view InterCopyContext::findLookupName() const | ||
81 | { | 86 | { |
82 | LUA_ASSERT(L1, lua_isfunction(L1, L1_i) || lua_istable(L1, L1_i)); // L1: ... v ... | 87 | LUA_ASSERT(L1, lua_isfunction(L1, L1_i) || lua_istable(L1, L1_i)); // L1: ... v ... |
83 | STACK_CHECK_START_REL(L1, 0); | 88 | STACK_CHECK_START_REL(L1, 0); |
@@ -141,7 +146,8 @@ THE SOFTWARE. | |||
141 | static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; | 146 | static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; |
142 | 147 | ||
143 | // get a unique ID for metatable at [i]. | 148 | // get a unique ID for metatable at [i]. |
144 | [[nodiscard]] static lua_Integer get_mt_id(Universe* U_, lua_State* L_, StackIndex const idx_) | 149 | [[nodiscard]] |
150 | static lua_Integer get_mt_id(Universe* U_, lua_State* L_, StackIndex const idx_) | ||
145 | { | 151 | { |
146 | StackIndex const _absidx{ luaG_absindex(L_, idx_) }; | 152 | StackIndex const _absidx{ luaG_absindex(L_, idx_) }; |
147 | 153 | ||
@@ -399,7 +405,8 @@ void InterCopyContext::copyCachedFunction() const | |||
399 | // ################################################################################################# | 405 | // ################################################################################################# |
400 | 406 | ||
401 | // Push a looked-up table, or nothing if we found nothing | 407 | // Push a looked-up table, or nothing if we found nothing |
402 | [[nodiscard]] bool InterCopyContext::lookupTable() const | 408 | [[nodiscard]] |
409 | bool InterCopyContext::lookupTable() const | ||
403 | { | 410 | { |
404 | // get the name of the table we want to send | 411 | // get the name of the table we want to send |
405 | std::string_view const _fqn{ findLookupName() }; | 412 | std::string_view const _fqn{ findLookupName() }; |
@@ -585,7 +592,8 @@ LuaType InterCopyContext::processConversion() const | |||
585 | 592 | ||
586 | // ################################################################################################# | 593 | // ################################################################################################# |
587 | 594 | ||
588 | [[nodiscard]] bool InterCopyContext::pushCachedMetatable() const | 595 | [[nodiscard]] |
596 | bool InterCopyContext::pushCachedMetatable() const | ||
589 | { | 597 | { |
590 | STACK_CHECK_START_REL(L1, 0); | 598 | STACK_CHECK_START_REL(L1, 0); |
591 | if (!lua_getmetatable(L1, L1_i)) { // L1: ... mt | 599 | if (!lua_getmetatable(L1, L1_i)) { // L1: ... mt |
@@ -637,7 +645,8 @@ LuaType InterCopyContext::processConversion() const | |||
637 | // local functions to point to the same table, also in the target. | 645 | // local functions to point to the same table, also in the target. |
638 | // Always pushes a table to 'L2'. | 646 | // Always pushes a table to 'L2'. |
639 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. | 647 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. |
640 | [[nodiscard]] bool InterCopyContext::pushCachedTable() const | 648 | [[nodiscard]] |
649 | bool InterCopyContext::pushCachedTable() const | ||
641 | { | 650 | { |
642 | void const* const _p{ lua_topointer(L1, L1_i) }; | 651 | void const* const _p{ lua_topointer(L1, L1_i) }; |
643 | 652 | ||
@@ -669,7 +678,8 @@ LuaType InterCopyContext::processConversion() const | |||
669 | 678 | ||
670 | // ################################################################################################# | 679 | // ################################################################################################# |
671 | 680 | ||
672 | [[nodiscard]] bool InterCopyContext::tryCopyClonable() const | 681 | [[nodiscard]] |
682 | bool InterCopyContext::tryCopyClonable() const | ||
673 | { | 683 | { |
674 | SourceIndex const _L1_i{ luaG_absindex(L1, L1_i).value() }; | 684 | SourceIndex const _L1_i{ luaG_absindex(L1, L1_i).value() }; |
675 | void* const _source{ lua_touserdata(L1, _L1_i) }; | 685 | void* const _source{ lua_touserdata(L1, _L1_i) }; |
@@ -773,7 +783,8 @@ LuaType InterCopyContext::processConversion() const | |||
773 | 783 | ||
774 | // Copy deep userdata between two separate Lua states (from L1 to L2) | 784 | // Copy deep userdata between two separate Lua states (from L1 to L2) |
775 | // Returns false if not a deep userdata, else true (unless an error occured) | 785 | // Returns false if not a deep userdata, else true (unless an error occured) |
776 | [[nodiscard]] bool InterCopyContext::tryCopyDeep() const | 786 | [[nodiscard]] |
787 | bool InterCopyContext::tryCopyDeep() const | ||
777 | { | 788 | { |
778 | DeepFactory* const _factory{ DeepFactory::LookupFactory(L1, L1_i, mode) }; // L1: ... deep ... | 789 | DeepFactory* const _factory{ DeepFactory::LookupFactory(L1, L1_i, mode) }; // L1: ... deep ... |
779 | if (_factory == nullptr) { | 790 | if (_factory == nullptr) { |
@@ -817,7 +828,8 @@ LuaType InterCopyContext::processConversion() const | |||
817 | 828 | ||
818 | // ################################################################################################# | 829 | // ################################################################################################# |
819 | 830 | ||
820 | [[nodiscard]] bool InterCopyContext::interCopyBoolean() const | 831 | [[nodiscard]] |
832 | bool InterCopyContext::interCopyBoolean() const | ||
821 | { | 833 | { |
822 | int const _v{ lua_toboolean(L1, L1_i) }; | 834 | int const _v{ lua_toboolean(L1, L1_i) }; |
823 | DEBUGSPEW_CODE(DebugSpew(nullptr) << (_v ? "true" : "false") << std::endl); | 835 | DEBUGSPEW_CODE(DebugSpew(nullptr) << (_v ? "true" : "false") << std::endl); |
@@ -827,7 +839,8 @@ LuaType InterCopyContext::processConversion() const | |||
827 | 839 | ||
828 | // ################################################################################################# | 840 | // ################################################################################################# |
829 | 841 | ||
830 | [[nodiscard]] bool InterCopyContext::interCopyFunction() const | 842 | [[nodiscard]] |
843 | bool InterCopyContext::interCopyFunction() const | ||
831 | { | 844 | { |
832 | if (vt == VT::KEY) { | 845 | if (vt == VT::KEY) { |
833 | return false; | 846 | return false; |
@@ -921,7 +934,8 @@ LuaType InterCopyContext::processConversion() const | |||
921 | 934 | ||
922 | // ################################################################################################# | 935 | // ################################################################################################# |
923 | 936 | ||
924 | [[nodiscard]] bool InterCopyContext::interCopyLightuserdata() const | 937 | [[nodiscard]] |
938 | bool InterCopyContext::interCopyLightuserdata() const | ||
925 | { | 939 | { |
926 | void* const _p{ lua_touserdata(L1, L1_i) }; | 940 | void* const _p{ lua_touserdata(L1, L1_i) }; |
927 | // recognize and print known UniqueKey names here | 941 | // recognize and print known UniqueKey names here |
@@ -952,7 +966,8 @@ LuaType InterCopyContext::processConversion() const | |||
952 | 966 | ||
953 | // ################################################################################################# | 967 | // ################################################################################################# |
954 | 968 | ||
955 | [[nodiscard]] bool InterCopyContext::interCopyNil() const | 969 | [[nodiscard]] |
970 | bool InterCopyContext::interCopyNil() const | ||
956 | { | 971 | { |
957 | if (vt == VT::KEY) { | 972 | if (vt == VT::KEY) { |
958 | return false; | 973 | return false; |
@@ -968,7 +983,8 @@ LuaType InterCopyContext::processConversion() const | |||
968 | 983 | ||
969 | // ################################################################################################# | 984 | // ################################################################################################# |
970 | 985 | ||
971 | [[nodiscard]] bool InterCopyContext::interCopyNumber() const | 986 | [[nodiscard]] |
987 | bool InterCopyContext::interCopyNumber() const | ||
972 | { | 988 | { |
973 | // LNUM patch support (keeping integer accuracy) | 989 | // LNUM patch support (keeping integer accuracy) |
974 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 990 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
@@ -988,7 +1004,8 @@ LuaType InterCopyContext::processConversion() const | |||
988 | 1004 | ||
989 | // ################################################################################################# | 1005 | // ################################################################################################# |
990 | 1006 | ||
991 | [[nodiscard]] bool InterCopyContext::interCopyString() const | 1007 | [[nodiscard]] |
1008 | bool InterCopyContext::interCopyString() const | ||
992 | { | 1009 | { |
993 | std::string_view const _s{ luaG_tostring(L1, L1_i) }; | 1010 | std::string_view const _s{ luaG_tostring(L1, L1_i) }; |
994 | DEBUGSPEW_CODE(DebugSpew(nullptr) << "'" << _s << "'" << std::endl); | 1011 | DEBUGSPEW_CODE(DebugSpew(nullptr) << "'" << _s << "'" << std::endl); |
@@ -998,7 +1015,8 @@ LuaType InterCopyContext::processConversion() const | |||
998 | 1015 | ||
999 | // ################################################################################################# | 1016 | // ################################################################################################# |
1000 | 1017 | ||
1001 | [[nodiscard]] bool InterCopyContext::interCopyTable() const | 1018 | [[nodiscard]] |
1019 | bool InterCopyContext::interCopyTable() const | ||
1002 | { | 1020 | { |
1003 | if (vt == VT::KEY) { | 1021 | if (vt == VT::KEY) { |
1004 | return false; | 1022 | return false; |
@@ -1055,7 +1073,8 @@ LuaType InterCopyContext::processConversion() const | |||
1055 | 1073 | ||
1056 | // ################################################################################################# | 1074 | // ################################################################################################# |
1057 | 1075 | ||
1058 | [[nodiscard]] bool InterCopyContext::interCopyUserdata() const | 1076 | [[nodiscard]] |
1077 | bool InterCopyContext::interCopyUserdata() const | ||
1059 | { | 1078 | { |
1060 | STACK_CHECK_START_REL(L1, 0); | 1079 | STACK_CHECK_START_REL(L1, 0); |
1061 | STACK_CHECK_START_REL(L2, 0); | 1080 | STACK_CHECK_START_REL(L2, 0); |
@@ -1120,7 +1139,8 @@ namespace { | |||
1120 | * | 1139 | * |
1121 | * Returns true if value was pushed, false if its type is non-supported. | 1140 | * Returns true if value was pushed, false if its type is non-supported. |
1122 | */ | 1141 | */ |
1123 | [[nodiscard]] InterCopyResult InterCopyContext::interCopyOne() const | 1142 | [[nodiscard]] |
1143 | InterCopyResult InterCopyContext::interCopyOne() const | ||
1124 | { | 1144 | { |
1125 | STACK_GROW(L2, 1); | 1145 | STACK_GROW(L2, 1); |
1126 | STACK_CHECK_START_REL(L1, 0); | 1146 | STACK_CHECK_START_REL(L1, 0); |
@@ -1185,7 +1205,8 @@ namespace { | |||
1185 | // returns InterCopyResult::Success if everything is fine | 1205 | // returns InterCopyResult::Success if everything is fine |
1186 | // returns InterCopyResult::Error if pushed an error message in L1 | 1206 | // returns InterCopyResult::Error if pushed an error message in L1 |
1187 | // else raise an error in whichever state is not a keeper | 1207 | // else raise an error in whichever state is not a keeper |
1188 | [[nodiscard]] InterCopyResult InterCopyContext::interCopyPackage() const | 1208 | [[nodiscard]] |
1209 | InterCopyResult InterCopyContext::interCopyPackage() const | ||
1189 | { | 1210 | { |
1190 | DEBUGSPEW_CODE(DebugSpew(U) << "InterCopyContext::interCopyPackage()" << std::endl); | 1211 | DEBUGSPEW_CODE(DebugSpew(U) << "InterCopyContext::interCopyPackage()" << std::endl); |
1191 | 1212 | ||
@@ -1268,7 +1289,8 @@ namespace { | |||
1268 | 1289 | ||
1269 | // Akin to 'lua_xmove' but copies values between _any_ Lua states. | 1290 | // Akin to 'lua_xmove' but copies values between _any_ Lua states. |
1270 | // NOTE: Both the states must be solely in the current OS thread's possession. | 1291 | // NOTE: Both the states must be solely in the current OS thread's possession. |
1271 | [[nodiscard]] InterCopyResult InterCopyContext::interCopy(int const n_) const | 1292 | [[nodiscard]] |
1293 | InterCopyResult InterCopyContext::interCopy(int const n_) const | ||
1272 | { | 1294 | { |
1273 | LUA_ASSERT(L1, vt == VT::NORMAL); | 1295 | LUA_ASSERT(L1, vt == VT::NORMAL); |
1274 | 1296 | ||
@@ -1328,7 +1350,8 @@ namespace { | |||
1328 | 1350 | ||
1329 | // ################################################################################################# | 1351 | // ################################################################################################# |
1330 | 1352 | ||
1331 | [[nodiscard]] InterCopyResult InterCopyContext::interMove(int const n_) const | 1353 | [[nodiscard]] |
1354 | InterCopyResult InterCopyContext::interMove(int const n_) const | ||
1332 | { | 1355 | { |
1333 | assert(L1_i == 0); // we can only move stuff off the top of the stack | 1356 | assert(L1_i == 0); // we can only move stuff off the top of the stack |
1334 | InterCopyResult const _ret{ interCopy(n_) }; | 1357 | InterCopyResult const _ret{ interCopy(n_) }; |