2022-12-02 18:09:59 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "ClassFixture.h"
|
|
|
|
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
|
|
|
|
|
|
|
using std::nullopt;
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
ClassFixture::ClassFixture()
|
|
|
|
{
|
2023-03-10 20:21:07 +00:00
|
|
|
GlobalTypes& globals = frontend.globals;
|
|
|
|
TypeArena& arena = globals.globalTypes;
|
|
|
|
TypeId numberType = builtinTypes->numberType;
|
2023-05-19 20:37:30 +01:00
|
|
|
TypeId stringType = builtinTypes->stringType;
|
2022-12-02 18:09:59 +00:00
|
|
|
|
|
|
|
unfreeze(arena);
|
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId connectionType = arena.addType(ClassType{"Connection", {}, nullopt, nullopt, {}, {}, "Connection", {}});
|
2024-03-01 18:45:26 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId baseClassInstanceType = arena.addType(ClassType{"BaseClass", {}, nullopt, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(baseClassInstanceType)->props = {
|
2024-04-05 21:45:09 +01:00
|
|
|
{"BaseMethod", Property::readonly(makeFunction(arena, baseClassInstanceType, {numberType}, {}))},
|
2022-12-02 18:09:59 +00:00
|
|
|
{"BaseField", {numberType}},
|
2024-03-01 18:45:26 +00:00
|
|
|
|
2024-04-05 21:45:09 +01:00
|
|
|
{"Touched", Property::readonly(connectionType)},
|
2024-03-01 18:45:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
getMutable<ClassType>(connectionType)->props = {
|
2024-08-02 15:30:04 +01:00
|
|
|
{"Connect", {makeFunction(arena, connectionType, {makeFunction(arena, nullopt, {baseClassInstanceType}, {})}, {})}}
|
|
|
|
};
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId baseClassType = arena.addType(ClassType{"BaseClass", {}, nullopt, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(baseClassType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"StaticMethod", {makeFunction(arena, nullopt, {}, {numberType})}},
|
|
|
|
{"Clone", {makeFunction(arena, nullopt, {baseClassInstanceType}, {baseClassInstanceType})}},
|
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {baseClassInstanceType})}},
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["BaseClass"] = TypeFun{{}, baseClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "BaseClass", baseClassType, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId childClassInstanceType = arena.addType(ClassType{"ChildClass", {}, baseClassInstanceType, nullopt, {}, {}, "Test", {}});
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(childClassInstanceType)->props = {
|
2023-05-19 20:37:30 +01:00
|
|
|
{"Method", {makeFunction(arena, childClassInstanceType, {}, {stringType})}},
|
2022-12-02 18:09:59 +00:00
|
|
|
};
|
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId childClassType = arena.addType(ClassType{"ChildClass", {}, baseClassType, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(childClassType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {childClassInstanceType})}},
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["ChildClass"] = TypeFun{{}, childClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "ChildClass", childClassType, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId grandChildInstanceType = arena.addType(ClassType{"GrandChild", {}, childClassInstanceType, nullopt, {}, {}, "Test", {}});
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(grandChildInstanceType)->props = {
|
2023-05-19 20:37:30 +01:00
|
|
|
{"Method", {makeFunction(arena, grandChildInstanceType, {}, {stringType})}},
|
2022-12-02 18:09:59 +00:00
|
|
|
};
|
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId grandChildType = arena.addType(ClassType{"GrandChild", {}, baseClassType, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(grandChildType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {grandChildInstanceType})}},
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["GrandChild"] = TypeFun{{}, grandChildInstanceType};
|
|
|
|
addGlobalBinding(globals, "GrandChild", childClassType, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId anotherChildInstanceType = arena.addType(ClassType{"AnotherChild", {}, baseClassInstanceType, nullopt, {}, {}, "Test", {}});
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(anotherChildInstanceType)->props = {
|
2023-05-19 20:37:30 +01:00
|
|
|
{"Method", {makeFunction(arena, anotherChildInstanceType, {}, {stringType})}},
|
2022-12-02 18:09:59 +00:00
|
|
|
};
|
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId anotherChildType = arena.addType(ClassType{"AnotherChild", {}, baseClassType, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(anotherChildType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {anotherChildInstanceType})}},
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["AnotherChild"] = TypeFun{{}, anotherChildInstanceType};
|
|
|
|
addGlobalBinding(globals, "AnotherChild", childClassType, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId unrelatedClassInstanceType = arena.addType(ClassType{"UnrelatedClass", {}, nullopt, nullopt, {}, {}, "Test", {}});
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId unrelatedClassType = arena.addType(ClassType{"UnrelatedClass", {}, nullopt, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(unrelatedClassType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {unrelatedClassInstanceType})}},
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["UnrelatedClass"] = TypeFun{{}, unrelatedClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "UnrelatedClass", unrelatedClassType, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TypeId vector2MetaType = arena.addType(TableType{});
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
vector2InstanceType = arena.addType(ClassType{"Vector2", {}, nullopt, vector2MetaType, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(vector2InstanceType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"X", {numberType}},
|
|
|
|
{"Y", {numberType}},
|
|
|
|
};
|
|
|
|
|
2024-07-17 15:43:31 +01:00
|
|
|
vector2Type = arena.addType(ClassType{"Vector2", {}, nullopt, nullopt, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<ClassType>(vector2Type)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"New", {makeFunction(arena, nullopt, {numberType, numberType}, {vector2InstanceType})}},
|
|
|
|
};
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<TableType>(vector2MetaType)->props = {
|
2022-12-02 18:09:59 +00:00
|
|
|
{"__add", {makeFunction(arena, nullopt, {vector2InstanceType, vector2InstanceType}, {vector2InstanceType})}},
|
2024-08-02 15:30:04 +01:00
|
|
|
{"__mul",
|
|
|
|
{arena.addType(IntersectionType{{
|
|
|
|
makeFunction(arena, vector2InstanceType, {vector2InstanceType}, {vector2InstanceType}),
|
|
|
|
makeFunction(arena, vector2InstanceType, {builtinTypes->numberType}, {vector2InstanceType}),
|
|
|
|
}})}}
|
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["Vector2"] = TypeFun{{}, vector2InstanceType};
|
|
|
|
addGlobalBinding(globals, "Vector2", vector2Type, "@test");
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TypeId callableClassMetaType = arena.addType(TableType{});
|
2024-07-17 15:43:31 +01:00
|
|
|
TypeId callableClassType = arena.addType(ClassType{"CallableClass", {}, nullopt, callableClassMetaType, {}, {}, "Test", {}});
|
2023-01-04 20:53:17 +00:00
|
|
|
getMutable<TableType>(callableClassMetaType)->props = {
|
2023-05-19 20:37:30 +01:00
|
|
|
{"__call", {makeFunction(arena, nullopt, {callableClassType, stringType}, {numberType})}},
|
2022-12-02 18:09:59 +00:00
|
|
|
};
|
2023-03-10 20:21:07 +00:00
|
|
|
globals.globalScope->exportedTypeBindings["CallableClass"] = TypeFun{{}, callableClassType};
|
2022-12-02 18:09:59 +00:00
|
|
|
|
2024-08-02 15:30:04 +01:00
|
|
|
auto addIndexableClass = [&arena, &globals](const char* className, TypeId keyType, TypeId returnType)
|
|
|
|
{
|
2023-05-12 18:50:47 +01:00
|
|
|
TypeId indexableClassMetaType = arena.addType(TableType{});
|
|
|
|
TypeId indexableClassType =
|
2024-07-17 15:43:31 +01:00
|
|
|
arena.addType(ClassType{className, {}, nullopt, indexableClassMetaType, {}, {}, "Test", {}, TableIndexer{keyType, returnType}});
|
2023-05-12 18:50:47 +01:00
|
|
|
globals.globalScope->exportedTypeBindings[className] = TypeFun{{}, indexableClassType};
|
|
|
|
};
|
|
|
|
|
|
|
|
// IndexableClass has a table indexer with a key type of 'number | string' and a return type of 'number'
|
2023-05-19 20:37:30 +01:00
|
|
|
addIndexableClass("IndexableClass", arena.addType(Luau::UnionType{{stringType, numberType}}), numberType);
|
2023-05-12 18:50:47 +01:00
|
|
|
// IndexableNumericKeyClass has a table indexer with a key type of 'number' and a return type of 'number'
|
|
|
|
addIndexableClass("IndexableNumericKeyClass", numberType, numberType);
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
for (const auto& [name, tf] : globals.globalScope->exportedTypeBindings)
|
2022-12-02 18:09:59 +00:00
|
|
|
persist(tf.type);
|
|
|
|
|
|
|
|
freeze(arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Luau
|