Vector constants with two components

This commit is contained in:
Petri Häkkinen 2024-12-16 11:47:45 +02:00
parent 2e6fdd90a0
commit 1387f6d3e7
2 changed files with 22 additions and 9 deletions

View file

@ -5,6 +5,8 @@
#include <math.h> #include <math.h>
LUAU_FASTFLAGVARIABLE(LuauVector2Constructor)
namespace Luau namespace Luau
{ {
namespace Compile namespace Compile
@ -471,11 +473,13 @@ Constant foldBuiltin(int bfid, const Constant* args, size_t count)
break; break;
case LBF_VECTOR: case LBF_VECTOR:
if (count >= 3 && args[0].type == Constant::Type_Number && args[1].type == Constant::Type_Number && args[2].type == Constant::Type_Number) if (count >= 2 && args[0].type == Constant::Type_Number && args[1].type == Constant::Type_Number)
{ {
if (count == 3) if (count == 2 && FFlag::LuauVector2Constructor)
return cvector(args[0].valueNumber, args[1].valueNumber, 0.0, 0.0);
else if (count == 3 && args[2].type == Constant::Type_Number)
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, 0.0); return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, 0.0);
else if (count == 4 && args[3].type == Constant::Type_Number) else if (count == 4 && args[2].type == Constant::Type_Number && args[3].type == Constant::Type_Number)
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, args[3].valueNumber); return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, args[3].valueNumber);
} }
break; break;

View file

@ -28,6 +28,7 @@ LUAU_FASTFLAG(LuauCompileOptimizeRevArith)
LUAU_FASTFLAG(LuauCompileLibraryConstants) LUAU_FASTFLAG(LuauCompileLibraryConstants)
LUAU_FASTFLAG(LuauVectorBuiltins) LUAU_FASTFLAG(LuauVectorBuiltins)
LUAU_FASTFLAG(LuauVectorFolding) LUAU_FASTFLAG(LuauVectorFolding)
LUAU_FASTFLAG(LuauVector2Constructor)
LUAU_FASTFLAG(LuauCompileDisabledBuiltins) LUAU_FASTFLAG(LuauCompileDisabledBuiltins)
using namespace Luau; using namespace Luau;
@ -5102,34 +5103,42 @@ L0: RETURN R3 -1
)"); )");
} }
TEST_CASE("VectorLiterals") TEST_CASE("VectorConstants")
{ {
CHECK_EQ("\n" + compileFunction("return Vector3.new(1, 2, 3)", 0, 2, 0, /*enableVectors*/ true), R"( ScopedFastFlag luauVectorBuiltins{FFlag::LuauVectorBuiltins, true};
ScopedFastFlag luauVector2Constructor{FFlag::LuauVector2Constructor, true};
CHECK_EQ("\n" + compileFunction("return vector.create(1, 2)", 0, 2, 0, /*enableVectors*/ true), R"(
LOADK R0 K0 [1, 2, 0]
RETURN R0 1
)");
CHECK_EQ("\n" + compileFunction("return vector.create(1, 2, 3)", 0, 2, 0, /*enableVectors*/ true), R"(
LOADK R0 K0 [1, 2, 3] LOADK R0 K0 [1, 2, 3]
RETURN R0 1 RETURN R0 1
)"); )");
CHECK_EQ("\n" + compileFunction("print(Vector3.new(1, 2, 3))", 0, 2, 0, /*enableVectors*/ true), R"( CHECK_EQ("\n" + compileFunction("print(vector.create(1, 2, 3))", 0, 2, 0, /*enableVectors*/ true), R"(
GETIMPORT R0 1 [print] GETIMPORT R0 1 [print]
LOADK R1 K2 [1, 2, 3] LOADK R1 K2 [1, 2, 3]
CALL R0 1 0 CALL R0 1 0
RETURN R0 0 RETURN R0 0
)"); )");
CHECK_EQ("\n" + compileFunction("print(Vector3.new(1, 2, 3, 4))", 0, 2, 0, /*enableVectors*/ true), R"( CHECK_EQ("\n" + compileFunction("print(vector.create(1, 2, 3, 4))", 0, 2, 0, /*enableVectors*/ true), R"(
GETIMPORT R0 1 [print] GETIMPORT R0 1 [print]
LOADK R1 K2 [1, 2, 3, 4] LOADK R1 K2 [1, 2, 3, 4]
CALL R0 1 0 CALL R0 1 0
RETURN R0 0 RETURN R0 0
)"); )");
CHECK_EQ("\n" + compileFunction("return Vector3.new(0, 0, 0), Vector3.new(-0, 0, 0)", 0, 2, 0, /*enableVectors*/ true), R"( CHECK_EQ("\n" + compileFunction("return vector.create(0, 0, 0), vector.create(-0, 0, 0)", 0, 2, 0, /*enableVectors*/ true), R"(
LOADK R0 K0 [0, 0, 0] LOADK R0 K0 [0, 0, 0]
LOADK R1 K1 [-0, 0, 0] LOADK R1 K1 [-0, 0, 0]
RETURN R0 2 RETURN R0 2
)"); )");
CHECK_EQ("\n" + compileFunction("return type(Vector3.new(0, 0, 0))", 0, 2, 0, /*enableVectors*/ true), R"( CHECK_EQ("\n" + compileFunction("return type(vector.create(0, 0, 0))", 0, 2, 0, /*enableVectors*/ true), R"(
LOADK R0 K0 ['vector'] LOADK R0 K0 ['vector']
RETURN R0 1 RETURN R0 1
)"); )");