Switch vector library type definition based on flags without string manipulation

This commit is contained in:
Petri Häkkinen 2024-12-17 09:52:12 +02:00
parent e8e84a72c5
commit 186b4e693b

View file

@ -454,7 +454,7 @@ declare buffer: {
)BUILTIN_SRC";
static const std::string kBuiltinDefinitionVectorSrc_DEPRECATED = R"BUILTIN_SRC(
static const std::string kBuiltinDefinitionVectorSrc_NoExtra_NoVector2Ctor_DEPRECATED = R"BUILTIN_SRC(
-- TODO: this will be replaced with a built-in primitive type
declare class vector end
@ -480,6 +480,62 @@ declare vector: {
)BUILTIN_SRC";
static const std::string kBuiltinDefinitionVectorSrc_NoExtra_DEPRECATED = R"BUILTIN_SRC(
-- TODO: this will be replaced with a built-in primitive type
declare class vector end
declare vector: {
create: @checked (x: number, y: number, z: number?) -> vector,
magnitude: @checked (vec: vector) -> number,
normalize: @checked (vec: vector) -> vector,
cross: @checked (vec1: vector, vec2: vector) -> vector,
dot: @checked (vec1: vector, vec2: vector) -> number,
angle: @checked (vec1: vector, vec2: vector, axis: vector?) -> number,
floor: @checked (vec: vector) -> vector,
ceil: @checked (vec: vector) -> vector,
abs: @checked (vec: vector) -> vector,
sign: @checked (vec: vector) -> vector,
clamp: @checked (vec: vector, min: vector, max: vector) -> vector,
max: @checked (vector, ...vector) -> vector,
min: @checked (vector, ...vector) -> vector,
zero: vector,
one: vector,
}
)BUILTIN_SRC";
static const std::string kBuiltinDefinitionVectorSrc_NoVector2Ctor_DEPRECATED = R"BUILTIN_SRC(
-- While vector would have been better represented as a built-in primitive type, type solver class handling covers most of the properties
declare class vector
x: number
y: number
z: number
end
declare vector: {
create: @checked (x: number, y: number, z: number) -> vector,
magnitude: @checked (vec: vector) -> number,
normalize: @checked (vec: vector) -> vector,
cross: @checked (vec1: vector, vec2: vector) -> vector,
dot: @checked (vec1: vector, vec2: vector) -> number,
angle: @checked (vec1: vector, vec2: vector, axis: vector?) -> number,
floor: @checked (vec: vector) -> vector,
ceil: @checked (vec: vector) -> vector,
abs: @checked (vec: vector) -> vector,
sign: @checked (vec: vector) -> vector,
clamp: @checked (vec: vector, min: vector, max: vector) -> vector,
max: @checked (vector, ...vector) -> vector,
min: @checked (vector, ...vector) -> vector,
zero: vector,
one: vector,
}
)BUILTIN_SRC";
static const std::string kBuiltinDefinitionVectorSrc = R"BUILTIN_SRC(
-- While vector would have been better represented as a built-in primitive type, type solver class handling covers most of the properties
@ -490,7 +546,7 @@ declare class vector
end
declare vector: {
create: @checked (x: number, y: number, z: number) -> vector,
create: @checked (x: number, y: number, z: number?) -> vector,
magnitude: @checked (vec: vector) -> number,
normalize: @checked (vec: vector) -> vector,
cross: @checked (vec1: vector, vec2: vector) -> vector,
@ -514,22 +570,14 @@ std::string getBuiltinDefinitionSource()
{
std::string result = FFlag::LuauMathMap ? kBuiltinDefinitionLuaSrcChecked : kBuiltinDefinitionLuaSrcChecked_DEPRECATED;
std::string vectorSrc;
if (FFlag::LuauVectorDefinitionsExtra)
vectorSrc = kBuiltinDefinitionVectorSrc;
else if (FFlag::LuauVectorDefinitions)
vectorSrc = kBuiltinDefinitionVectorSrc_DEPRECATED;
if (FFlag::LuauVector2Constructor && !vectorSrc.empty())
{
std::string what = "create: @checked (x: number, y: number, z: number) -> vector";
std::string replacement = "create: @checked (x: number, y: number, z: number?) -> vector";
std::string::size_type pos = vectorSrc.find(what);
LUAU_ASSERT(pos != std::string::npos);
vectorSrc.replace(pos, what.size(), replacement);
}
result += vectorSrc;
if (FFlag::LuauVectorDefinitionsExtra && FFlag::LuauVector2Constructor)
result += kBuiltinDefinitionVectorSrc;
else if (FFlag::LuauVectorDefinitionsExtra && !FFlag::LuauVector2Constructor)
result += kBuiltinDefinitionVectorSrc_NoVector2Ctor_DEPRECATED;
else if (FFlag::LuauVectorDefinitions && FFlag::LuauVector2Constructor)
result += kBuiltinDefinitionVectorSrc_NoExtra_DEPRECATED;
else if (FFlag::LuauVectorDefinitions && !FFlag::LuauVector2Constructor)
result += kBuiltinDefinitionVectorSrc_NoExtra_NoVector2Ctor_DEPRECATED;
return result;
}