This commit is contained in:
birds3345 2024-11-06 23:56:11 -03:00 committed by GitHub
commit 43bc8a48b7
Signed by: DevComp
GPG key ID: B5690EEEBB952194

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
@ -72,6 +74,17 @@ static void foldUnary(Constant& result, AstExprUnary::Op op, const Constant& arg
} }
} }
static int compareStrings(const char* string1, const char* string2, unsigned int size1, unsigned int size2)
{
unsigned int size = std::min(size1, size2);
int res = memcmp(string1, string2, size);
if (res != 0 || size1 == size2)
return res;
return size1 < size2 ? -1 : 1;
}
static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& la, const Constant& ra) static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& la, const Constant& ra)
{ {
switch (op) switch (op)
@ -157,6 +170,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 = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) < 0;
}
break; break;
case AstExprBinary::CompareLe: case AstExprBinary::CompareLe:
@ -165,6 +183,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 = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) <= 0;
}
break; break;
case AstExprBinary::CompareGt: case AstExprBinary::CompareGt:
@ -173,6 +196,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 = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) > 0;
}
break; break;
case AstExprBinary::CompareGe: case AstExprBinary::CompareGe:
@ -181,6 +209,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 = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) >= 0;
}
break; break;
case AstExprBinary::And: case AstExprBinary::And: