Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Lua/LuaTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,8 @@ internal void EnsureArrayCapacity(int newCapacity)
// Move some of the elements of the hash part to a newly allocated array
foreach (var kv in dictionary)
{
if (kv.Key.TryRead<double>(out var d) && MathEx.IsInteger(d))
if (TryGetInteger(kv.Key, out var index))
{
var index = (int)d;
if (index > prevLength && index <= newLength)
{
indexList.Add((index, kv.Value));
Expand All @@ -267,7 +266,7 @@ internal void EnsureArrayCapacity(int newCapacity)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool TryGetInteger(LuaValue value, out int integer)
{
if (value.TryRead<double>(out var num) && MathEx.IsInteger(num))
if (value.TryReadNumber(out var num) && MathEx.IsInteger(num))
{
integer = (int)num;
return true;
Expand Down
14 changes: 13 additions & 1 deletion src/Lua/LuaValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,19 @@ public bool TryRead<T>(out T result)
result = default!;
return false;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryReadNumber(out double result)
{
if (type == LuaValueType.Number)
{
result = value;
return true;
}
result = default!;
return false;
}

public T Read<T>()
{
if (!TryRead<T>(out var result)) throw new InvalidOperationException($"Cannot convert LuaValueType.{Type} to {typeof(T).FullName}.");
Expand Down
4 changes: 2 additions & 2 deletions src/Lua/Runtime/LuaVirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ await func.InvokeAsync(new()
{
compareResult = StringComparer.Ordinal.Compare(strB, strC) < 0;
}
else if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
else if (vb.TryReadNumber(out var valueB) && vc.TryReadNumber(out var valueC))
{
compareResult = valueB < valueC;
}
Expand Down Expand Up @@ -629,7 +629,7 @@ await func.InvokeAsync(new()
{
compareResult = StringComparer.Ordinal.Compare(strB, strC) <= 0;
}
else if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
else if (vb.TryReadNumber(out var valueB) && vc.TryReadNumber(out var valueC))
{
compareResult = valueB <= valueC;
}
Expand Down