Skip to content
Open
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
2 changes: 1 addition & 1 deletion Analysis/include/Luau/BuiltinDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void attachMagicFunction(TypeId ty, std::shared_ptr<MagicFunction> magic);
Property makeProperty(TypeId ty, std::optional<std::string> documentationSymbol = std::nullopt);
void assignPropDocumentationSymbols(TableType::Props& props, const std::string& baseName);

std::string getBuiltinDefinitionSource();
std::string getBuiltinDefinitionSource(VectorSize vectorSize = VectorSize::Three);
std::string getTypeFunctionDefinitionSource();

void addGlobalBinding(GlobalTypes& globals, const std::string& name, TypeId ty, const std::string& packageName);
Expand Down
4 changes: 4 additions & 0 deletions Analysis/include/Luau/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ struct Frontend
SolverMode getLuauSolverMode() const;
// The default value assuming there is no workspace setup yet
std::atomic<SolverMode> useNewLuauSolver;

void setVectorSize(VectorSize size);
VectorSize getVectorSize() const;
std::atomic<VectorSize> vectorSize;
// Parse module graph and prepare SourceNode/SourceModule data, including required dependencies without running typechecking
void parse(const ModuleName& name);
void parseModules(const std::vector<ModuleName>& name);
Expand Down
7 changes: 7 additions & 0 deletions Analysis/include/Luau/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ enum struct SolverMode
New
};

// Number of components in the 'vector' type, matching the VM's LUA_VECTOR_SIZE (which is always 3 or 4)
enum struct VectorSize
{
Three,
Four
};

/**
* There are three kinds of type variables:
* - `Free` variables are metavariables, which stand for unconstrained types.
Expand Down
2 changes: 1 addition & 1 deletion Analysis/src/BuiltinDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void registerBuiltinGlobals(Frontend& frontend, GlobalTypes& globals, bool typeC


LoadDefinitionFileResult loadResult = frontend.loadDefinitionFile(
globals, globals.globalScope, getBuiltinDefinitionSource(), "@luau", /* captureComments */ false, typeCheckForAutocomplete
globals, globals.globalScope, getBuiltinDefinitionSource(frontend.getVectorSize()), "@luau", /* captureComments */ false, typeCheckForAutocomplete
);
LUAU_ASSERT(loadResult.success);

Expand Down
24 changes: 21 additions & 3 deletions Analysis/src/EmbeddedBuiltinDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ declare buffer: {

)BUILTIN_SRC";

static const char* const kBuiltinDefinitionVectorSrc = R"BUILTIN_SRC(
// The vector library's shape depends on the vector width: 4-wide vectors expose a 'w' component and take a
// fourth argument in 'create'. Both headers are compiled in and selected at runtime; the method list is shared.
static const char* const kBuiltinDefinitionVector3HeaderSrc = R"BUILTIN_SRC(

-- While vector would have been better represented as a built-in primitive type, type solver extern type handling covers most of the properties
declare extern type vector with
Expand All @@ -319,7 +321,22 @@ declare extern type vector with
end

declare vector: {
create: @checked (x: number, y: number, z: number?) -> vector,
create: @checked (x: number, y: number, z: number?) -> vector,)BUILTIN_SRC";

static const char* const kBuiltinDefinitionVector4HeaderSrc = R"BUILTIN_SRC(

-- While vector would have been better represented as a built-in primitive type, type solver extern type handling covers most of the properties
declare extern type vector with
read x: number
read y: number
read z: number
read w: number
end

declare vector: {
create: @checked (x: number, y: number, z: number?, w: number?) -> vector,)BUILTIN_SRC";

static const char* const kBuiltinDefinitionVectorSrc = R"BUILTIN_SRC(
magnitude: @checked (vec: vector) -> number,
normalize: @checked (vec: vector) -> vector,
cross: @checked (vec1: vector, vec2: vector) -> vector,
Expand Down Expand Up @@ -395,7 +412,7 @@ declare class: {
}
)CLASS_SRC";

std::string getBuiltinDefinitionSource()
std::string getBuiltinDefinitionSource(VectorSize vectorSize)
{
std::string result = kBuiltinDefinitionBaseSrc;

Expand All @@ -411,6 +428,7 @@ std::string getBuiltinDefinitionSource()
else
result += kBuiltinDefinitionBufferSrc_NOINTEGER;

result += vectorSize == VectorSize::Four ? kBuiltinDefinitionVector4HeaderSrc : kBuiltinDefinitionVector3HeaderSrc;
result += kBuiltinDefinitionVectorSrc;

if (FFlag::LuauIntegerType2 && FFlag::LuauIntegerLibrary)
Expand Down
12 changes: 12 additions & 0 deletions Analysis/src/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ static TypeCheckLimits makeTypeCheckLimits(const FrontendOptions& options)

Frontend::Frontend(SolverMode mode, FileResolver* fileResolver, ConfigResolver* configResolver, FrontendOptions options)
: useNewLuauSolver(mode)
, vectorSize(VectorSize::Three)
, builtinTypes(NotNull{&builtinTypes_})
, fileResolver(fileResolver)
, moduleResolver(this)
Expand All @@ -459,6 +460,7 @@ Frontend::Frontend(SolverMode mode, FileResolver* fileResolver, ConfigResolver*

Frontend::Frontend(FileResolver* fileResolver, ConfigResolver* configResolver, const FrontendOptions& options)
: useNewLuauSolver(FFlag::LuauSolverV2 ? SolverMode::New : SolverMode::Old)
, vectorSize(VectorSize::Three)
, builtinTypes(NotNull{&builtinTypes_})
, fileResolver(fileResolver)
, moduleResolver(this)
Expand All @@ -480,6 +482,16 @@ SolverMode Frontend::getLuauSolverMode() const
return useNewLuauSolver.load();
}

void Frontend::setVectorSize(VectorSize size)
{
vectorSize.store(size);
}

VectorSize Frontend::getVectorSize() const
{
return vectorSize.load();
}

void Frontend::parse(const ModuleName& name)
{
LUAU_TIMETRACE_SCOPE("Frontend::parse", "Frontend");
Expand Down
1 change: 1 addition & 0 deletions CLI/src/Analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ int main(int argc, char** argv)
};
}

frontend.setVectorSize(LUA_VECTOR_SIZE == 4 ? Luau::VectorSize::Four : Luau::VectorSize::Three);
Luau::registerBuiltinGlobals(frontend, frontend.globals);
Luau::freeze(frontend.globals.globalTypes);

Expand Down
1 change: 1 addition & 0 deletions CLI/src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ extern "C" const char* checkScript(const char* source, int useNewSolver)

Luau::Frontend frontend(&fileResolver, &configResolver, options);
frontend.setLuauSolverMode(useNewSolver ? Luau::SolverMode::New : Luau::SolverMode::Old);
frontend.setVectorSize(LUA_VECTOR_SIZE == 4 ? Luau::VectorSize::Four : Luau::VectorSize::Three);
// Add Luau builtins
Luau::unfreeze(frontend.globals.globalTypes);
Luau::registerBuiltinGlobals(frontend, frontend.globals);
Expand Down
Loading