Skip to content

Commit d9904b1

Browse files
committed
Fix decimal boundary number handling
1 parent 21b2aea commit d9904b1

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/Laylua/Library/Marshaler/DefaultLuaMarshaler.TryGetValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ private static bool TryConvertNumberValue(double value, Type clrType, LuaAllowed
472472

473473
if (clrType == typeof(decimal))
474474
{
475-
if (value is >= (double) decimal.MinValue and <= (double) decimal.MaxValue)
475+
if (LuaTypeServices.IsDecimalValueInRange(value))
476476
{
477477
result = (decimal) value;
478478
return true;

src/Laylua/Library/Marshaler/LuaTypeServices.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ internal static bool IsSingleValueInRange(lua_Number value)
361361
internal static bool IsDecimalValueInRange(lua_Number value)
362362
{
363363
return !double.IsNaN(value) && !double.IsInfinity(value)
364-
&& value is >= (double) decimal.MinValue and <= (double) decimal.MaxValue;
364+
&& value > (double) decimal.MinValue
365+
&& value < (double) decimal.MaxValue;
365366
}
366367

367368
internal static Type NormalizeBoundaryType(Type type)

tests/Laylua.Tests/Tests/Marshaler/LuaMarshalerExplicitReadTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ public void TryGetValue_DoubleToString_WithPrimitiveToString_Succeeds()
108108
Assert.That(value, Is.Not.Null.And.Not.Empty);
109109
}
110110

111+
[Test]
112+
public void TryGetValue_DecimalBoundaryDouble_ReturnsFalseWithoutThrowing()
113+
{
114+
// Arrange
115+
using var _ = Lua.Stack.SnapshotCount();
116+
lua_pushnumber(L, (double) decimal.MaxValue);
117+
118+
var result = false;
119+
120+
// Act & Assert
121+
Assert.That(() => result = Lua.Stack[1].TryGetValue(LuaAllowedValueConversions.None, out decimal _), Throws.Nothing);
122+
Assert.That(result, Is.False);
123+
}
124+
111125
[Test]
112126
public void TryGetValue_SingleCharString_WithStringToChar_Succeeds()
113127
{

tests/Laylua.Tests/Tests/Marshaler/LuaTypeServicesTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ public void GetImplicitCompatibilityCost_FloatToDecimal_ReturnsWideningCost()
554554
Assert.That(cost, Is.EqualTo((int) LuaMarshalingCompatibilityKind.NumericWidening));
555555
}
556556

557+
[Test]
558+
public void GetImplicitCompatibilityCost_DecimalBoundaryDouble_WithRangeValidation_ReturnsIncompatible()
559+
{
560+
// Arrange
561+
using var _ = Lua.Stack.SnapshotCount();
562+
lua_pushnumber(L, (double) decimal.MaxValue);
563+
564+
// Act
565+
var cost = GetCompatibilityCost(Lua, typeof(decimal), LuaAllowedValueConversions.None, validateNumericRange: true);
566+
567+
// Assert
568+
Assert.That(cost, Is.EqualTo(-1));
569+
}
570+
557571
[Test]
558572
public void GetImplicitCompatibilityCost_FloatToDouble_ReturnsExactPrimitiveCost()
559573
{

0 commit comments

Comments
 (0)