diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-20 10:58:06 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-20 10:58:06 +0200 |
commit | 08040747494fe7839d2ab049e37f8e23fb8141b4 (patch) | |
tree | 62dbbc9227d61e7447ad66e377b5c544c3bf118d /src | |
parent | 742cfdd2f9009ed12d7109e152df8c760851a58a (diff) | |
download | lanes-08040747494fe7839d2ab049e37f8e23fb8141b4.tar.gz lanes-08040747494fe7839d2ab049e37f8e23fb8141b4.tar.bz2 lanes-08040747494fe7839d2ab049e37f8e23fb8141b4.zip |
linda:limit errors on limits < 1
Diffstat (limited to 'src')
-rw-r--r-- | src/deep.cpp | 26 | ||||
-rw-r--r-- | src/linda.cpp | 6 | ||||
-rw-r--r-- | src/uniquekey.h | 17 |
3 files changed, 24 insertions, 25 deletions
diff --git a/src/deep.cpp b/src/deep.cpp index 9474666..2f4f1ed 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -159,7 +159,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
159 | bool const isLastRef{ _p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; | 159 | bool const isLastRef{ _p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; |
160 | 160 | ||
161 | if (isLastRef) { | 161 | if (isLastRef) { |
162 | // retrieve wrapped __gc | 162 | // retrieve wrapped __gc, if any |
163 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: self __gc? | 163 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: self __gc? |
164 | if (!lua_isnil(L_, -1)) { | 164 | if (!lua_isnil(L_, -1)) { |
165 | lua_insert(L_, -2); // L_: __gc self | 165 | lua_insert(L_, -2); // L_: __gc self |
@@ -175,26 +175,6 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
175 | 175 | ||
176 | // ################################################################################################# | 176 | // ################################################################################################# |
177 | 177 | ||
178 | // TODO: convert to UniqueKey::getSubTableMode | ||
179 | static void push_registry_subtable_mode(lua_State* L_, RegistryUniqueKey key_, const char* mode_) | ||
180 | { | ||
181 | STACK_GROW(L_, 4); | ||
182 | STACK_CHECK_START_REL(L_, 0); | ||
183 | if (!key_.getSubTable(L_, 0, 0)) { // L_: {} | ||
184 | // Set its metatable if requested | ||
185 | if (mode_) { | ||
186 | lua_createtable(L_, 0, 1); // L_: {} mt | ||
187 | lua_pushliteral(L_, "__mode"); // L_: {} mt "__mode" | ||
188 | lua_pushstring(L_, mode_); // L_: {} mt "__mode" mode | ||
189 | lua_rawset(L_, -3); // L_: {} mt | ||
190 | lua_setmetatable(L_, -2); // L_: {} | ||
191 | } | ||
192 | } | ||
193 | STACK_CHECK(L_, 1); | ||
194 | } | ||
195 | |||
196 | // ################################################################################################# | ||
197 | |||
198 | /* | 178 | /* |
199 | * Push a proxy userdata on the stack. | 179 | * Push a proxy userdata on the stack. |
200 | * returns nullptr if ok, else some error string related to bad factory behavior or module require problem | 180 | * returns nullptr if ok, else some error string related to bad factory behavior or module require problem |
@@ -207,7 +187,7 @@ static void push_registry_subtable_mode(lua_State* L_, RegistryUniqueKey key_, c | |||
207 | char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) | 187 | char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) |
208 | { | 188 | { |
209 | // Check if a proxy already exists | 189 | // Check if a proxy already exists |
210 | push_registry_subtable_mode(L_, kDeepProxyCacheRegKey, "v"); // L_: DPC | 190 | kDeepProxyCacheRegKey.getSubTableMode(L_, "v"); // L_: DPC |
211 | lua_pushlightuserdata(L_, prelude_); // L_: DPC deep | 191 | lua_pushlightuserdata(L_, prelude_); // L_: DPC deep |
212 | lua_rawget(L_, -2); // L_: DPC proxy | 192 | lua_rawget(L_, -2); // L_: DPC proxy |
213 | if (!lua_isnil(L_, -1)) { | 193 | if (!lua_isnil(L_, -1)) { |
@@ -256,7 +236,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int | |||
256 | lua_pushcfunction(L_, deep_userdata_gc); // L_: DPC proxy metatable deep_userdata_gc | 236 | lua_pushcfunction(L_, deep_userdata_gc); // L_: DPC proxy metatable deep_userdata_gc |
257 | } else { | 237 | } else { |
258 | // Add our own '__gc' method wrapping the original | 238 | // Add our own '__gc' method wrapping the original |
259 | lua_pushcclosure(L_, deep_userdata_gc, 1); // DPC proxy metatable deep_userdata_gc | 239 | lua_pushcclosure(L_, deep_userdata_gc, 1); // L_: DPC proxy metatable deep_userdata_gc |
260 | } | 240 | } |
261 | lua_setfield(L_, -2, "__gc"); // L_: DPC proxy metatable | 241 | lua_setfield(L_, -2, "__gc"); // L_: DPC proxy metatable |
262 | 242 | ||
diff --git a/src/linda.cpp b/src/linda.cpp index a7cf011..4086211 100644 --- a/src/linda.cpp +++ b/src/linda.cpp | |||
@@ -487,7 +487,6 @@ LUAG_FUNC(linda_receive) | |||
487 | * [true|lanes.cancel_error] = linda_set( linda_ud, key_num|str|bool|lightuserdata [, value [, ...]]) | 487 | * [true|lanes.cancel_error] = linda_set( linda_ud, key_num|str|bool|lightuserdata [, value [, ...]]) |
488 | * | 488 | * |
489 | * Set one or more value to Linda. | 489 | * Set one or more value to Linda. |
490 | * TODO: what do we do if we set to non-nil and limit is 0? | ||
491 | * | 490 | * |
492 | * Existing slot value is replaced, and possible queued entries removed. | 491 | * Existing slot value is replaced, and possible queued entries removed. |
493 | */ | 492 | */ |
@@ -603,7 +602,10 @@ LUAG_FUNC(linda_limit) | |||
603 | // make sure we got 3 arguments: the linda, a key and a limit | 602 | // make sure we got 3 arguments: the linda, a key and a limit |
604 | luaL_argcheck(L_, lua_gettop(L_) == 3, 2, "wrong number of arguments"); | 603 | luaL_argcheck(L_, lua_gettop(L_) == 3, 2, "wrong number of arguments"); |
605 | // make sure we got a numeric limit | 604 | // make sure we got a numeric limit |
606 | luaL_checknumber(L_, 3); | 605 | lua_Number const _limit{ luaL_checknumber(L_, 3) }; |
606 | if (_limit < 1) { | ||
607 | raise_luaL_argerror(L_, 3, "limit must be >= 1"); | ||
608 | } | ||
607 | // make sure the key is of a valid type | 609 | // make sure the key is of a valid type |
608 | check_key_types(L_, 2, 2); | 610 | check_key_types(L_, 2, 2); |
609 | 611 | ||
diff --git a/src/uniquekey.h b/src/uniquekey.h index 6265a56..67b3279 100644 --- a/src/uniquekey.h +++ b/src/uniquekey.h | |||
@@ -107,6 +107,23 @@ class RegistryUniqueKey | |||
107 | STACK_CHECK(L_, 1); | 107 | STACK_CHECK(L_, 1); |
108 | return false; | 108 | return false; |
109 | } | 109 | } |
110 | // --------------------------------------------------------------------------------------------- | ||
111 | void getSubTableMode(lua_State* L_, const char* mode_) const | ||
112 | { | ||
113 | STACK_CHECK_START_REL(L_, 0); | ||
114 | if (!getSubTable(L_, 0, 0)) { // L_: {} | ||
115 | // Set its metatable if requested | ||
116 | if (mode_) { | ||
117 | STACK_GROW(L_, 3); | ||
118 | lua_createtable(L_, 0, 1); // L_: {} mt | ||
119 | lua_pushliteral(L_, "__mode"); // L_: {} mt "__mode" | ||
120 | lua_pushstring(L_, mode_); // L_: {} mt "__mode" mode | ||
121 | lua_rawset(L_, -3); // L_: {} mt | ||
122 | lua_setmetatable(L_, -2); // L_: {} | ||
123 | } | ||
124 | } | ||
125 | STACK_CHECK(L_, 1); | ||
126 | } | ||
110 | }; | 127 | }; |
111 | 128 | ||
112 | // ################################################################################################# | 129 | // ################################################################################################# |