diff options
Diffstat (limited to '')
-rw-r--r-- | src/linda.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/linda.cpp b/src/linda.cpp index 76cd347..f8d6cdc 100644 --- a/src/linda.cpp +++ b/src/linda.cpp | |||
@@ -109,15 +109,10 @@ template <bool OPT> | |||
109 | // #################################### Linda implementation ####################################### | 109 | // #################################### Linda implementation ####################################### |
110 | // ################################################################################################# | 110 | // ################################################################################################# |
111 | 111 | ||
112 | // Any hashing will do that maps pointers to [0..Universe::nb_keepers[ consistently. | ||
113 | // Pointers are often aligned by 8 or so - ignore the low order bits | ||
114 | // have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer | ||
115 | static constexpr uintptr_t kPointerMagicShift{ 3 }; | ||
116 | |||
117 | Linda::Linda(Universe* U_, LindaGroup group_, std::string_view const& name_) | 112 | Linda::Linda(Universe* U_, LindaGroup group_, std::string_view const& name_) |
118 | : DeepPrelude{ LindaFactory::Instance } | 113 | : DeepPrelude{ LindaFactory::Instance } |
119 | , U{ U_ } | 114 | , U{ U_ } |
120 | , keeperIndex{ (group_ ? group_ : static_cast<int>(std::bit_cast<uintptr_t>(this) >> kPointerMagicShift)) % U_->keepers->nb_keepers } | 115 | , keeperIndex{ group_ % U_->keepers.getNbKeepers() } |
121 | { | 116 | { |
122 | setName(name_); | 117 | setName(name_); |
123 | } | 118 | } |
@@ -733,6 +728,7 @@ LUAG_FUNC(linda_tostring) | |||
733 | 728 | ||
734 | // ################################################################################################# | 729 | // ################################################################################################# |
735 | 730 | ||
731 | #if HAVE_DECODA_SUPPORT() | ||
736 | /* | 732 | /* |
737 | * table/string = linda:__towatch() | 733 | * table/string = linda:__towatch() |
738 | * return a table listing all pending data inside the linda, or the stringified linda if empty | 734 | * return a table listing all pending data inside the linda, or the stringified linda if empty |
@@ -748,6 +744,8 @@ LUAG_FUNC(linda_towatch) | |||
748 | return _pushed; | 744 | return _pushed; |
749 | } | 745 | } |
750 | 746 | ||
747 | #endif // HAVE_DECODA_SUPPORT() | ||
748 | |||
751 | // ################################################################################################# | 749 | // ################################################################################################# |
752 | 750 | ||
753 | namespace { | 751 | namespace { |
@@ -755,7 +753,9 @@ namespace { | |||
755 | static luaL_Reg const sLindaMT[] = { | 753 | static luaL_Reg const sLindaMT[] = { |
756 | { "__concat", LG_linda_concat }, | 754 | { "__concat", LG_linda_concat }, |
757 | { "__tostring", LG_linda_tostring }, | 755 | { "__tostring", LG_linda_tostring }, |
756 | #if HAVE_DECODA_SUPPORT() | ||
758 | { "__towatch", LG_linda_towatch }, // Decoda __towatch support | 757 | { "__towatch", LG_linda_towatch }, // Decoda __towatch support |
758 | #endif // HAVE_DECODA_SUPPORT() | ||
759 | { "cancel", LG_linda_cancel }, | 759 | { "cancel", LG_linda_cancel }, |
760 | { "count", LG_linda_count }, | 760 | { "count", LG_linda_count }, |
761 | { "deep", LG_linda_deep }, | 761 | { "deep", LG_linda_deep }, |
@@ -784,13 +784,24 @@ namespace { | |||
784 | LUAG_FUNC(linda) | 784 | LUAG_FUNC(linda) |
785 | { | 785 | { |
786 | int const _top{ lua_gettop(L_) }; | 786 | int const _top{ lua_gettop(L_) }; |
787 | int _groupIdx{}; | ||
787 | luaL_argcheck(L_, _top <= 2, _top, "too many arguments"); | 788 | luaL_argcheck(L_, _top <= 2, _top, "too many arguments"); |
788 | if (_top == 1) { | 789 | if (_top == 1) { |
789 | LuaType const _t{ lua_type_as_enum(L_, 1) }; | 790 | LuaType const _t{ lua_type_as_enum(L_, 1) }; |
790 | luaL_argcheck(L_, _t == LuaType::STRING || _t == LuaType::NUMBER, 1, "wrong parameter (should be a string or a number)"); | 791 | int const _nameIdx{ (_t == LuaType::STRING) ? 1 : 0 }; |
792 | _groupIdx = (_t == LuaType::NUMBER) ? 1 : 0; | ||
793 | luaL_argcheck(L_, _nameIdx || _groupIdx, 1, "wrong parameter (should be a string or a number)"); | ||
791 | } else if (_top == 2) { | 794 | } else if (_top == 2) { |
792 | luaL_checktype(L_, 1, LUA_TSTRING); | 795 | luaL_checktype(L_, 1, LUA_TSTRING); |
793 | luaL_checktype(L_, 2, LUA_TNUMBER); | 796 | luaL_checktype(L_, 2, LUA_TNUMBER); |
797 | _groupIdx = 2; | ||
798 | } | ||
799 | int const _nbKeepers{ Universe::Get(L_)->keepers.getNbKeepers() }; | ||
800 | if (!_groupIdx) { | ||
801 | luaL_argcheck(L_, _nbKeepers < 2, 0, "there are multiple keepers, you must specify a group"); | ||
802 | } else { | ||
803 | int const _group{ static_cast<int>(lua_tointeger(L_, _groupIdx)) }; | ||
804 | luaL_argcheck(L_, _group >= 0 && _group < _nbKeepers, _groupIdx, "group out of range"); | ||
794 | } | 805 | } |
795 | return LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, 0); | 806 | return LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, 0); |
796 | } | 807 | } |