constant fold string comparisons

This commit is contained in:
birds3345 2024-07-12 01:33:12 -04:00
parent 6bfc38e61a
commit 931d8d619d

View File

@ -6,6 +6,8 @@
#include <vector> #include <vector>
#include <math.h> #include <math.h>
LUAU_FASTFLAGVARIABLE(LuauConstantFoldStringComparisons, false)
namespace Luau namespace Luau
{ {
namespace Compile namespace Compile
@ -157,6 +159,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean; result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber < ra.valueNumber; result.valueBoolean = la.valueNumber < ra.valueNumber;
} }
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = strcmp(la.valueString, ra.valueString) < 0;
}
break; break;
case AstExprBinary::CompareLe: case AstExprBinary::CompareLe:
@ -165,6 +172,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean; result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber <= ra.valueNumber; result.valueBoolean = la.valueNumber <= ra.valueNumber;
} }
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = strcmp(la.valueString, ra.valueString) <= 0;
}
break; break;
case AstExprBinary::CompareGt: case AstExprBinary::CompareGt:
@ -173,6 +185,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean; result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber > ra.valueNumber; result.valueBoolean = la.valueNumber > ra.valueNumber;
} }
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = strcmp(la.valueString, ra.valueString) > 0;
}
break; break;
case AstExprBinary::CompareGe: case AstExprBinary::CompareGe:
@ -181,6 +198,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean; result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber >= ra.valueNumber; result.valueBoolean = la.valueNumber >= ra.valueNumber;
} }
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = strcmp(la.valueString, ra.valueString) >= 0;
}
break; break;
case AstExprBinary::And: case AstExprBinary::And: