diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-26 10:17:09 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-26 10:17:09 +0200 |
commit | 4343dc14c1db7ef9e5d57ed1d40740439e1c51d6 (patch) | |
tree | e3e7d192218291e4d089835fd63ac052142aa673 /src/tools.cpp | |
parent | 0b5108a8a0d9b7a4a63bd6aae0271b71a887beea (diff) | |
download | lanes-4343dc14c1db7ef9e5d57ed1d40740439e1c51d6.tar.gz lanes-4343dc14c1db7ef9e5d57ed1d40740439e1c51d6.tar.bz2 lanes-4343dc14c1db7ef9e5d57ed1d40740439e1c51d6.zip |
Formalize a few coding style rules, start to enforce them for constants
Diffstat (limited to 'src/tools.cpp')
-rw-r--r-- | src/tools.cpp | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/src/tools.cpp b/src/tools.cpp index a25fb2b..aa8f927 100644 --- a/src/tools.cpp +++ b/src/tools.cpp | |||
@@ -37,8 +37,8 @@ THE SOFTWARE. | |||
37 | 37 | ||
38 | DEBUGSPEW_CODE(char const* const DebugSpewIndentScope::debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); | 38 | DEBUGSPEW_CODE(char const* const DebugSpewIndentScope::debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); |
39 | 39 | ||
40 | // crc64/we of string "LOOKUPCACHE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ | 40 | // xxh64 of string "kLookupCacheRegKey" generated at https://www.pelock.com/products/hash-calculator |
41 | static constexpr RegistryUniqueKey LOOKUPCACHE_REGKEY{ 0x837A68DFC6FCB716ull }; | 41 | static constexpr RegistryUniqueKey kLookupCacheRegKey{ 0x9BF75F84E54B691Bull }; |
42 | 42 | ||
43 | // ################################################################################################# | 43 | // ################################################################################################# |
44 | 44 | ||
@@ -480,7 +480,7 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) | |||
480 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 480 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
481 | STACK_GROW(L, 3); | 481 | STACK_GROW(L, 3); |
482 | STACK_CHECK_START_REL(L, 0); | 482 | STACK_CHECK_START_REL(L, 0); |
483 | LOOKUP_REGKEY.pushValue(L); // {} | 483 | kLookupRegKey.pushValue(L); // {} |
484 | STACK_CHECK(L, 1); | 484 | STACK_CHECK(L, 1); |
485 | LUA_ASSERT(L, lua_istable(L, -1)); | 485 | LUA_ASSERT(L, lua_istable(L, -1)); |
486 | if (lua_type(L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function | 486 | if (lua_type(L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function |
@@ -510,12 +510,12 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) | |||
510 | STACK_CHECK(L, 2); | 510 | STACK_CHECK(L, 2); |
511 | } | 511 | } |
512 | // retrieve the cache, create it if we haven't done it yet | 512 | // retrieve the cache, create it if we haven't done it yet |
513 | LOOKUPCACHE_REGKEY.pushValue(L); // {} {fqn} {cache}? | 513 | kLookupCacheRegKey.pushValue(L); // {} {fqn} {cache}? |
514 | if (lua_isnil(L, -1)) | 514 | if (lua_isnil(L, -1)) |
515 | { | 515 | { |
516 | lua_pop(L, 1); // {} {fqn} | 516 | lua_pop(L, 1); // {} {fqn} |
517 | lua_newtable(L); // {} {fqn} {cache} | 517 | lua_newtable(L); // {} {fqn} {cache} |
518 | LOOKUPCACHE_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); | 518 | kLookupCacheRegKey.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); |
519 | STACK_CHECK(L, 3); | 519 | STACK_CHECK(L, 3); |
520 | } | 520 | } |
521 | // process everything we find in that table, filling in lookup data for all functions and tables we see there | 521 | // process everything we find in that table, filling in lookup data for all functions and tables we see there |
@@ -534,8 +534,8 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) | |||
534 | 534 | ||
535 | /*---=== Inter-state copying ===---*/ | 535 | /*---=== Inter-state copying ===---*/ |
536 | 536 | ||
537 | // crc64/we of string "REG_MTID" generated at http://www.nitrxgen.net/hashgen/ | 537 | // xxh64 of string "kMtIdRegKey" generated at https://www.pelock.com/products/hash-calculator |
538 | static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; | 538 | static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; |
539 | 539 | ||
540 | /* | 540 | /* |
541 | * Get a unique ID for metatable at [i]. | 541 | * Get a unique ID for metatable at [i]. |
@@ -547,12 +547,12 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; | |||
547 | STACK_GROW(L, 3); | 547 | STACK_GROW(L, 3); |
548 | 548 | ||
549 | STACK_CHECK_START_REL(L, 0); | 549 | STACK_CHECK_START_REL(L, 0); |
550 | push_registry_subtable(L, REG_MTID); // ... _R[REG_MTID] | 550 | push_registry_subtable(L, kMtIdRegKey); // ... _R[kMtIdRegKey] |
551 | lua_pushvalue(L, i); // ... _R[REG_MTID] {mt} | 551 | lua_pushvalue(L, i); // ... _R[kMtIdRegKey] {mt} |
552 | lua_rawget(L, -2); // ... _R[REG_MTID] mtk? | 552 | lua_rawget(L, -2); // ... _R[kMtIdRegKey] mtk? |
553 | 553 | ||
554 | lua_Integer id{ lua_tointeger(L, -1) }; // 0 for nil | 554 | lua_Integer id{ lua_tointeger(L, -1) }; // 0 for nil |
555 | lua_pop(L, 1); // ... _R[REG_MTID] | 555 | lua_pop(L, 1); // ... _R[kMtIdRegKey] |
556 | STACK_CHECK(L, 1); | 556 | STACK_CHECK(L, 1); |
557 | 557 | ||
558 | if (id == 0) | 558 | if (id == 0) |
@@ -560,13 +560,13 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; | |||
560 | id = U->next_mt_id.fetch_add(1, std::memory_order_relaxed); | 560 | id = U->next_mt_id.fetch_add(1, std::memory_order_relaxed); |
561 | 561 | ||
562 | // Create two-way references: id_uint <-> table | 562 | // Create two-way references: id_uint <-> table |
563 | lua_pushvalue(L, i); // ... _R[REG_MTID] {mt} | 563 | lua_pushvalue(L, i); // ... _R[kMtIdRegKey] {mt} |
564 | lua_pushinteger(L, id); // ... _R[REG_MTID] {mt} id | 564 | lua_pushinteger(L, id); // ... _R[kMtIdRegKey] {mt} id |
565 | lua_rawset(L, -3); // ... _R[REG_MTID] | 565 | lua_rawset(L, -3); // ... _R[kMtIdRegKey] |
566 | 566 | ||
567 | lua_pushinteger(L, id); // ... _R[REG_MTID] id | 567 | lua_pushinteger(L, id); // ... _R[kMtIdRegKey] id |
568 | lua_pushvalue(L, i); // ... _R[REG_MTID] id {mt} | 568 | lua_pushvalue(L, i); // ... _R[kMtIdRegKey] id {mt} |
569 | lua_rawset(L, -3); // ... _R[REG_MTID] | 569 | lua_rawset(L, -3); // ... _R[kMtIdRegKey] |
570 | } | 570 | } |
571 | lua_pop(L, 1); // ... | 571 | lua_pop(L, 1); // ... |
572 | STACK_CHECK(L, 0); | 572 | STACK_CHECK(L, 0); |
@@ -628,7 +628,7 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; | |||
628 | else | 628 | else |
629 | { | 629 | { |
630 | // fetch the name from the source state's lookup table | 630 | // fetch the name from the source state's lookup table |
631 | LOOKUP_REGKEY.pushValue(L); // ... v ... {} | 631 | kLookupRegKey.pushValue(L); // ... v ... {} |
632 | STACK_CHECK( L, 1); | 632 | STACK_CHECK( L, 1); |
633 | LUA_ASSERT(L, lua_istable( L, -1)); | 633 | LUA_ASSERT(L, lua_istable( L, -1)); |
634 | lua_pushvalue( L, i); // ... v ... {} v | 634 | lua_pushvalue( L, i); // ... v ... {} v |
@@ -700,7 +700,7 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; | |||
700 | 700 | ||
701 | case LookupMode::LaneBody: | 701 | case LookupMode::LaneBody: |
702 | case LookupMode::FromKeeper: | 702 | case LookupMode::FromKeeper: |
703 | LOOKUP_REGKEY.pushValue(L2); // {} | 703 | kLookupRegKey.pushValue(L2); // {} |
704 | STACK_CHECK(L2, 1); | 704 | STACK_CHECK(L2, 1); |
705 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 705 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
706 | lua_pushlstring(L2, fqn, len); // {} "f.q.n" | 706 | lua_pushlstring(L2, fqn, len); // {} "f.q.n" |
@@ -1005,7 +1005,7 @@ void InterCopyContext::lookup_native_func() const | |||
1005 | 1005 | ||
1006 | case LookupMode::LaneBody: | 1006 | case LookupMode::LaneBody: |
1007 | case LookupMode::FromKeeper: | 1007 | case LookupMode::FromKeeper: |
1008 | LOOKUP_REGKEY.pushValue(L2); // {} | 1008 | kLookupRegKey.pushValue(L2); // {} |
1009 | STACK_CHECK(L2, 1); | 1009 | STACK_CHECK(L2, 1); |
1010 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 1010 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
1011 | lua_pushlstring(L2, fqn, len); // {} "f.q.n" | 1011 | lua_pushlstring(L2, fqn, len); // {} "f.q.n" |
@@ -1324,30 +1324,30 @@ void InterCopyContext::copy_cached_func() const | |||
1324 | STACK_CHECK_START_REL(L2, 0); | 1324 | STACK_CHECK_START_REL(L2, 0); |
1325 | STACK_GROW(L2, 4); | 1325 | STACK_GROW(L2, 4); |
1326 | // do we already know this metatable? | 1326 | // do we already know this metatable? |
1327 | push_registry_subtable(L2, REG_MTID); // _R[REG_MTID] | 1327 | push_registry_subtable(L2, kMtIdRegKey); // _R[kMtIdRegKey] |
1328 | lua_pushinteger(L2, mt_id); // _R[REG_MTID] id | 1328 | lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] id |
1329 | lua_rawget(L2, -2); // _R[REG_MTID] mt|nil | 1329 | lua_rawget(L2, -2); // _R[kMtIdRegKey] mt|nil |
1330 | STACK_CHECK(L2, 2); | 1330 | STACK_CHECK(L2, 2); |
1331 | 1331 | ||
1332 | if (lua_isnil(L2, -1)) | 1332 | if (lua_isnil(L2, -1)) |
1333 | { // L2 did not know the metatable | 1333 | { // L2 did not know the metatable |
1334 | lua_pop(L2, 1); // _R[REG_MTID] | 1334 | lua_pop(L2, 1); // _R[kMtIdRegKey] |
1335 | InterCopyContext const c{ U, L2, L1, L2_cache_i, SourceIndex{ lua_gettop(L1) }, VT::METATABLE, mode, name }; | 1335 | InterCopyContext const c{ U, L2, L1, L2_cache_i, SourceIndex{ lua_gettop(L1) }, VT::METATABLE, mode, name }; |
1336 | if (!c.inter_copy_one()) // _R[REG_MTID] mt? | 1336 | if (!c.inter_copy_one()) // _R[kMtIdRegKey] mt? |
1337 | { | 1337 | { |
1338 | luaL_error(L1, "Error copying a metatable"); // doesn't return | 1338 | luaL_error(L1, "Error copying a metatable"); // doesn't return |
1339 | } | 1339 | } |
1340 | 1340 | ||
1341 | STACK_CHECK(L2, 2); // _R[REG_MTID] mt | 1341 | STACK_CHECK(L2, 2); // _R[kMtIdRegKey] mt |
1342 | // mt_id -> metatable | 1342 | // mt_id -> metatable |
1343 | lua_pushinteger(L2, mt_id); // _R[REG_MTID] mt id | 1343 | lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] mt id |
1344 | lua_pushvalue(L2, -2); // _R[REG_MTID] mt id mt | 1344 | lua_pushvalue(L2, -2); // _R[kMtIdRegKey] mt id mt |
1345 | lua_rawset(L2, -4); // _R[REG_MTID] mt | 1345 | lua_rawset(L2, -4); // _R[kMtIdRegKey] mt |
1346 | 1346 | ||
1347 | // metatable -> mt_id | 1347 | // metatable -> mt_id |
1348 | lua_pushvalue(L2, -1); // _R[REG_MTID] mt mt | 1348 | lua_pushvalue(L2, -1); // _R[kMtIdRegKey] mt mt |
1349 | lua_pushinteger(L2, mt_id); // _R[REG_MTID] mt mt id | 1349 | lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] mt mt id |
1350 | lua_rawset(L2, -4); // _R[REG_MTID] mt | 1350 | lua_rawset(L2, -4); // _R[kMtIdRegKey] mt |
1351 | STACK_CHECK(L2, 2); | 1351 | STACK_CHECK(L2, 2); |
1352 | } | 1352 | } |
1353 | lua_remove(L2, -2); // mt | 1353 | lua_remove(L2, -2); // mt |
@@ -1432,12 +1432,6 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
1432 | 1432 | ||
1433 | // ################################################################################################# | 1433 | // ################################################################################################# |
1434 | 1434 | ||
1435 | /* | ||
1436 | * The clone cache is a weak valued table listing all clones, indexed by their userdatapointer | ||
1437 | * fnv164 of string "CLONABLES_CACHE_KEY" generated at https://www.pelock.com/products/hash-calculator | ||
1438 | */ | ||
1439 | static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; | ||
1440 | |||
1441 | [[nodiscard]] bool InterCopyContext::copyclone() const | 1435 | [[nodiscard]] bool InterCopyContext::copyclone() const |
1442 | { | 1436 | { |
1443 | SourceIndex const L1_i{ lua_absindex(L1, this->L1_i) }; | 1437 | SourceIndex const L1_i{ lua_absindex(L1, this->L1_i) }; |
@@ -1848,7 +1842,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; | |||
1848 | */ | 1842 | */ |
1849 | [[nodiscard]] bool InterCopyContext::inter_copy_one() const | 1843 | [[nodiscard]] bool InterCopyContext::inter_copy_one() const |
1850 | { | 1844 | { |
1851 | static constexpr int pod_mask = (1 << LUA_TNIL) | (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA) | (1 << LUA_TNUMBER) | (1 << LUA_TSTRING); | 1845 | static constexpr int kPODmask = (1 << LUA_TNIL) | (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA) | (1 << LUA_TNUMBER) | (1 << LUA_TSTRING); |
1852 | STACK_GROW(L2, 1); | 1846 | STACK_GROW(L2, 1); |
1853 | STACK_CHECK_START_REL(L1, 0); // L1 // L2 | 1847 | STACK_CHECK_START_REL(L1, 0); // L1 // L2 |
1854 | STACK_CHECK_START_REL(L2, 0); // L1 // L2 | 1848 | STACK_CHECK_START_REL(L2, 0); // L1 // L2 |
@@ -1860,7 +1854,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; | |||
1860 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[static_cast<int>(val_type)], vt_names[static_cast<int>(vt)])); | 1854 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[static_cast<int>(val_type)], vt_names[static_cast<int>(vt)])); |
1861 | 1855 | ||
1862 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } | 1856 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } |
1863 | if (((1 << static_cast<int>(val_type)) & pod_mask) == 0) | 1857 | if (((1 << static_cast<int>(val_type)) & kPODmask) == 0) |
1864 | { | 1858 | { |
1865 | if (lua_getmetatable(L1, L1_i)) // ... mt | 1859 | if (lua_getmetatable(L1, L1_i)) // ... mt |
1866 | { | 1860 | { |