2022-12-02 18:46:05 +08: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-11 03:20:04 +08:00
|
|
|
GlobalTypes& globals = frontend.globals;
|
|
|
|
TypeArena& arena = globals.globalTypes;
|
|
|
|
TypeId numberType = builtinTypes->numberType;
|
2023-05-20 02:59:59 +08:00
|
|
|
TypeId stringType = builtinTypes->stringType;
|
2022-12-02 18:46:05 +08:00
|
|
|
|
|
|
|
unfreeze(arena);
|
|
|
|
|
2024-03-01 21:58:44 +08:00
|
|
|
TypeId connectionType = arena.addType(ClassType{"Connection", {}, nullopt, nullopt, {}, {}, "Connection"});
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId baseClassInstanceType = arena.addType(ClassType{"BaseClass", {}, nullopt, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(baseClassInstanceType)->props = {
|
2024-04-06 01:41:05 +08:00
|
|
|
{"BaseMethod", Property::readonly(makeFunction(arena, baseClassInstanceType, {numberType}, {}))},
|
2022-12-02 18:46:05 +08:00
|
|
|
{"BaseField", {numberType}},
|
2024-03-01 21:58:44 +08:00
|
|
|
|
2024-04-06 01:41:05 +08:00
|
|
|
{"Touched", Property::readonly(connectionType)},
|
2024-03-01 21:58:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
getMutable<ClassType>(connectionType)->props = {
|
2024-04-20 05:04:30 +08:00
|
|
|
{"Connect", {makeFunction(arena, connectionType, {makeFunction(arena, nullopt, {baseClassInstanceType}, {})}, {})}}};
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId baseClassType = arena.addType(ClassType{"BaseClass", {}, nullopt, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(baseClassType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"StaticMethod", {makeFunction(arena, nullopt, {}, {numberType})}},
|
|
|
|
{"Clone", {makeFunction(arena, nullopt, {baseClassInstanceType}, {baseClassInstanceType})}},
|
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {baseClassInstanceType})}},
|
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["BaseClass"] = TypeFun{{}, baseClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "BaseClass", baseClassType, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId childClassInstanceType = arena.addType(ClassType{"ChildClass", {}, baseClassInstanceType, nullopt, {}, {}, "Test"});
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<ClassType>(childClassInstanceType)->props = {
|
2023-05-20 02:59:59 +08:00
|
|
|
{"Method", {makeFunction(arena, childClassInstanceType, {}, {stringType})}},
|
2022-12-02 18:46:05 +08:00
|
|
|
};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId childClassType = arena.addType(ClassType{"ChildClass", {}, baseClassType, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(childClassType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {childClassInstanceType})}},
|
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["ChildClass"] = TypeFun{{}, childClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "ChildClass", childClassType, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId grandChildInstanceType = arena.addType(ClassType{"GrandChild", {}, childClassInstanceType, nullopt, {}, {}, "Test"});
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<ClassType>(grandChildInstanceType)->props = {
|
2023-05-20 02:59:59 +08:00
|
|
|
{"Method", {makeFunction(arena, grandChildInstanceType, {}, {stringType})}},
|
2022-12-02 18:46:05 +08:00
|
|
|
};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId grandChildType = arena.addType(ClassType{"GrandChild", {}, baseClassType, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(grandChildType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {grandChildInstanceType})}},
|
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["GrandChild"] = TypeFun{{}, grandChildInstanceType};
|
|
|
|
addGlobalBinding(globals, "GrandChild", childClassType, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId anotherChildInstanceType = arena.addType(ClassType{"AnotherChild", {}, baseClassInstanceType, nullopt, {}, {}, "Test"});
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<ClassType>(anotherChildInstanceType)->props = {
|
2023-05-20 02:59:59 +08:00
|
|
|
{"Method", {makeFunction(arena, anotherChildInstanceType, {}, {stringType})}},
|
2022-12-02 18:46:05 +08:00
|
|
|
};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId anotherChildType = arena.addType(ClassType{"AnotherChild", {}, baseClassType, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(anotherChildType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {anotherChildInstanceType})}},
|
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["AnotherChild"] = TypeFun{{}, anotherChildInstanceType};
|
|
|
|
addGlobalBinding(globals, "AnotherChild", childClassType, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId unrelatedClassInstanceType = arena.addType(ClassType{"UnrelatedClass", {}, nullopt, nullopt, {}, {}, "Test"});
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId unrelatedClassType = arena.addType(ClassType{"UnrelatedClass", {}, nullopt, nullopt, {}, {}, "Test"});
|
|
|
|
getMutable<ClassType>(unrelatedClassType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"New", {makeFunction(arena, nullopt, {}, {unrelatedClassInstanceType})}},
|
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["UnrelatedClass"] = TypeFun{{}, unrelatedClassInstanceType};
|
|
|
|
addGlobalBinding(globals, "UnrelatedClass", unrelatedClassType, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId vector2MetaType = arena.addType(TableType{});
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-10-21 04:36:26 +08:00
|
|
|
vector2InstanceType = arena.addType(ClassType{"Vector2", {}, nullopt, vector2MetaType, {}, {}, "Test"});
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<ClassType>(vector2InstanceType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"X", {numberType}},
|
|
|
|
{"Y", {numberType}},
|
|
|
|
};
|
|
|
|
|
2023-10-21 04:36:26 +08:00
|
|
|
vector2Type = arena.addType(ClassType{"Vector2", {}, nullopt, nullopt, {}, {}, "Test"});
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<ClassType>(vector2Type)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"New", {makeFunction(arena, nullopt, {numberType, numberType}, {vector2InstanceType})}},
|
|
|
|
};
|
2023-01-04 01:33:19 +08:00
|
|
|
getMutable<TableType>(vector2MetaType)->props = {
|
2022-12-02 18:46:05 +08:00
|
|
|
{"__add", {makeFunction(arena, nullopt, {vector2InstanceType, vector2InstanceType}, {vector2InstanceType})}},
|
2024-04-20 05:04:30 +08:00
|
|
|
{"__mul", {arena.addType(IntersectionType{{
|
|
|
|
makeFunction(arena, vector2InstanceType, {vector2InstanceType}, {vector2InstanceType}),
|
|
|
|
makeFunction(arena, vector2InstanceType, {builtinTypes->numberType}, {vector2InstanceType}),
|
|
|
|
}})}}};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["Vector2"] = TypeFun{{}, vector2InstanceType};
|
|
|
|
addGlobalBinding(globals, "Vector2", vector2Type, "@test");
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId callableClassMetaType = arena.addType(TableType{});
|
|
|
|
TypeId callableClassType = arena.addType(ClassType{"CallableClass", {}, nullopt, callableClassMetaType, {}, {}, "Test"});
|
|
|
|
getMutable<TableType>(callableClassMetaType)->props = {
|
2023-05-20 02:59:59 +08:00
|
|
|
{"__call", {makeFunction(arena, nullopt, {callableClassType, stringType}, {numberType})}},
|
2022-12-02 18:46:05 +08:00
|
|
|
};
|
2023-03-11 03:20:04 +08:00
|
|
|
globals.globalScope->exportedTypeBindings["CallableClass"] = TypeFun{{}, callableClassType};
|
2022-12-02 18:46:05 +08:00
|
|
|
|
2023-05-12 20:15:01 +08:00
|
|
|
auto addIndexableClass = [&arena, &globals](const char* className, TypeId keyType, TypeId returnType) {
|
|
|
|
TypeId indexableClassMetaType = arena.addType(TableType{});
|
|
|
|
TypeId indexableClassType =
|
|
|
|
arena.addType(ClassType{className, {}, nullopt, indexableClassMetaType, {}, {}, "Test", TableIndexer{keyType, returnType}});
|
|
|
|
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-20 02:59:59 +08:00
|
|
|
addIndexableClass("IndexableClass", arena.addType(Luau::UnionType{{stringType, numberType}}), numberType);
|
2023-05-12 20:15:01 +08:00
|
|
|
// IndexableNumericKeyClass has a table indexer with a key type of 'number' and a return type of 'number'
|
|
|
|
addIndexableClass("IndexableNumericKeyClass", numberType, numberType);
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
for (const auto& [name, tf] : globals.globalScope->exportedTypeBindings)
|
2022-12-02 18:46:05 +08:00
|
|
|
persist(tf.type);
|
|
|
|
|
|
|
|
freeze(arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Luau
|