Skip to content

Commit d1c0d08

Browse files
Copilotkunitoki
andauthored
Restore legacy bool get behavior: remove non-strict type check
Agent-Logs-Url: https://github.com/kunitoki/LuaBridge3/sessions/e6bc5e3f-455b-498f-8e99-d7eea2410da8 Co-authored-by: kunitoki <707032+kunitoki@users.noreply.github.com>
1 parent c5f478f commit d1c0d08

4 files changed

Lines changed: 30 additions & 20 deletions

File tree

‎Distribution/LuaBridge/LuaBridge.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,13 +4059,8 @@ struct Stack<bool>
40594059

40604060
[[nodiscard]] static TypeResult<bool> get(lua_State* L, int index)
40614061
{
4062-
const auto type = lua_type(L, index);
4063-
40644062
#if LUABRIDGE_STRICT_STACK_CONVERSIONS
4065-
if (type != LUA_TBOOLEAN)
4066-
return makeErrorCode(ErrorCode::InvalidTypeCast);
4067-
#else
4068-
if (type != LUA_TBOOLEAN && type != LUA_TNIL)
4063+
if (lua_type(L, index) != LUA_TBOOLEAN)
40694064
return makeErrorCode(ErrorCode::InvalidTypeCast);
40704065
#endif
40714066

‎Source/LuaBridge/detail/Stack.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,8 @@ struct Stack<bool>
176176

177177
[[nodiscard]] static TypeResult<bool> get(lua_State* L, int index)
178178
{
179-
const auto type = lua_type(L, index);
180-
181179
#if LUABRIDGE_STRICT_STACK_CONVERSIONS
182-
if (type != LUA_TBOOLEAN)
183-
return makeErrorCode(ErrorCode::InvalidTypeCast);
184-
#else
185-
if (type != LUA_TBOOLEAN && type != LUA_TNIL)
180+
if (lua_type(L, index) != LUA_TBOOLEAN)
186181
return makeErrorCode(ErrorCode::InvalidTypeCast);
187182
#endif
188183

‎Tests/Source/StackTests.cpp‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,42 @@ TEST_F(StackTests, BoolStackOverflow)
190190

191191
TEST_F(StackTests, BoolInvalidType)
192192
{
193-
// Non-boolean types are rejected even in non-strict mode
193+
#if !LUABRIDGE_STRICT_STACK_CONVERSIONS
194+
// Non-strict mode: any Lua value is accepted via lua_toboolean
194195
{
195196
(void)luabridge::Stack<int>::push(L, 0);
196197
auto result = luabridge::Stack<bool>::get(L, -1);
197-
ASSERT_FALSE(result);
198+
ASSERT_TRUE(result);
199+
EXPECT_TRUE(*result); // integer 0 is truthy in Lua
198200
}
199201

200202
{
201203
(void)luabridge::Stack<int>::push(L, 42);
202204
auto result = luabridge::Stack<bool>::get(L, -1);
203-
ASSERT_FALSE(result);
205+
ASSERT_TRUE(result);
206+
EXPECT_TRUE(*result);
204207
}
205208

206-
#if !LUABRIDGE_STRICT_STACK_CONVERSIONS
207-
// nil converts to false in non-strict mode
208209
{
209210
(void)luabridge::Stack<std::nullptr_t>::push(L, nullptr);
210211
auto result = luabridge::Stack<bool>::get(L, -1);
211212
ASSERT_TRUE(result);
212-
EXPECT_FALSE(*result);
213+
EXPECT_FALSE(*result); // nil is falsy
213214
}
214215
#else
215-
// nil is rejected in strict mode
216+
// Strict mode: only LUA_TBOOLEAN is accepted
217+
{
218+
(void)luabridge::Stack<int>::push(L, 0);
219+
auto result = luabridge::Stack<bool>::get(L, -1);
220+
ASSERT_FALSE(result);
221+
}
222+
223+
{
224+
(void)luabridge::Stack<int>::push(L, 42);
225+
auto result = luabridge::Stack<bool>::get(L, -1);
226+
ASSERT_FALSE(result);
227+
}
228+
216229
{
217230
(void)luabridge::Stack<std::nullptr_t>::push(L, nullptr);
218231
auto result = luabridge::Stack<bool>::get(L, -1);

‎Tests/Source/Tests.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,11 +1243,18 @@ TEST_F(LuaBridgeTest, BooleanNoValue)
12431243
.endClass()
12441244
.endNamespace();
12451245

1246-
// Calling with no argument must fail: missing bool argument is LUA_TNONE, not a valid bool
1246+
#if !LUABRIDGE_STRICT_STACK_CONVERSIONS
1247+
// Non-strict mode: no value at the argument index is accepted as false via lua_toboolean
1248+
runLua("local foo = test.ConstructibleFromBool(); result = foo:val()");
1249+
ASSERT_TRUE(result().isBool());
1250+
EXPECT_FALSE(result<bool>());
1251+
#else
1252+
// Strict mode: missing bool argument must fail
12471253
#if LUABRIDGE_HAS_EXCEPTIONS
12481254
EXPECT_THROW(runLua("local foo = test.ConstructibleFromBool(); result = foo:val()"), std::runtime_error);
12491255
#else
12501256
EXPECT_FALSE(runLua("local foo = test.ConstructibleFromBool(); result = foo:val()"));
1257+
#endif
12511258
#endif
12521259

12531260
// Passing false explicitly must work

0 commit comments

Comments
 (0)