This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
904 lines (778 loc) · 33.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/ModuleResolver.h"
#include "Luau/TypeInfer.h"
#include "Luau/BuiltinDefinitions.h"
#include "Luau/Frontend.h"
#include "Luau/TypeAttach.h"
#include "Luau/Transpiler.h"
#include "extern/json.hpp"
#include <filesystem>
#include <vector>
#include "FileUtils.h"
#include "RequireResolver.h"
LUAU_FASTFLAG(DebugLuauTimeTracing)
LUAU_FASTINT(LuauTypeInferRecursionLimit)
LUAU_FASTINT(LuauTypeInferTypePackLoopLimit)
LUAU_FASTINT(LuauCheckRecursionLimit)
LUAU_FASTINT(LuauTableTypeMaximumStringifierLength)
LUAU_FASTINT(LuauTypeInferIterationLimit)
LUAU_FASTINT(LuauTarjanChildLimit)
LUAU_FASTFLAG(DebugLuauFreezeArena)
using json = nlohmann::json;
enum class ReportFormat
{
Default,
Luacheck,
Gnu,
};
static void report(ReportFormat format, const char* name, const Luau::Location& loc, const char* type, const char* message)
{
switch (format)
{
case ReportFormat::Default:
fprintf(stderr, "%s(%d,%d): %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, type, message);
break;
case ReportFormat::Luacheck:
{
// Note: luacheck's end column is inclusive but our end column is exclusive
// In addition, luacheck doesn't support multi-line messages, so if the error is multiline we'll fake end column as 100 and hope for the best
int columnEnd = (loc.begin.line == loc.end.line) ? loc.end.column : 100;
// Use stdout to match luacheck behavior
fprintf(stdout, "%s:%d:%d-%d: (W0) %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, columnEnd, type, message);
break;
}
case ReportFormat::Gnu:
// Note: GNU end column is inclusive but our end column is exclusive
fprintf(stderr, "%s:%d.%d-%d.%d: %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, loc.end.line + 1, loc.end.column, type, message);
break;
}
}
static void reportError(const Luau::Frontend& frontend, ReportFormat format, const Luau::TypeError& error)
{
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(error.moduleName);
if (const Luau::SyntaxError* syntaxError = Luau::get_if<Luau::SyntaxError>(&error.data))
report(format, humanReadableName.c_str(), error.location, "SyntaxError", syntaxError->message.c_str());
else
report(format, humanReadableName.c_str(), error.location, "TypeError", Luau::toString(error).c_str());
}
static void reportWarning(ReportFormat format, const char* name, const Luau::LintWarning& warning)
{
report(format, name, warning.location, Luau::LintWarning::getName(warning.code), warning.text.c_str());
}
static bool analyzeFile(Luau::Frontend& frontend, const char* name, ReportFormat format, bool annotate)
{
Luau::CheckResult cr;
if (frontend.isDirty(name))
cr = frontend.check(name);
if (!frontend.getSourceModule(name))
{
fprintf(stderr, "Error opening %s\n", name);
return false;
}
for (auto& error : cr.errors)
reportError(frontend, format, error);
Luau::LintResult lr = frontend.lint(name);
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(name);
for (auto& error : lr.errors)
reportWarning(format, humanReadableName.c_str(), error);
for (auto& warning : lr.warnings)
reportWarning(format, humanReadableName.c_str(), warning);
if (annotate)
{
Luau::SourceModule* sm = frontend.getSourceModule(name);
Luau::ModulePtr m = frontend.moduleResolver.getModule(name);
Luau::attachTypeData(*sm, *m);
std::string annotated = Luau::transpileWithTypes(*sm->root);
printf("%s", annotated.c_str());
}
return cr.errors.empty() && lr.errors.empty();
}
static void displayHelp(const char* argv0)
{
printf("Usage: %s [--mode] [options] [file list]\n", argv0);
printf("\n");
printf("Available modes:\n");
printf(" omitted: typecheck and lint input files\n");
printf(" --annotate: typecheck input files and output source with type annotations\n");
printf("\n");
printf("Available options:\n");
printf(" --formatter=plain: report analysis errors in Luacheck-compatible format\n");
printf(" --formatter=gnu: report analysis errors in GNU-compatible format\n");
printf(" --timetrace: record compiler time tracing information into trace.json\n");
printf(" --sourcemap=PATH: path to a Rojo-style sourcemap\n");
printf(" --project=PATH: path to Rojo .project.json for resolving [DEPRECATED - USE --sourcemap=PATH INSTEAD]\n");
printf(" --defs=PATH: path to definition file for global types\n");
printf(" --stdin-filepath=PATH: path to file being sent through stdin. Used for require resolution\n");
printf(" --dump-source-map: dump the currently resolved source map\n");
printf(" --exclude-virtual-path: don't include virtual path name in output\n");
printf(" --flag:FlagName=value: sets an internal flag. Use --show-flags to list all internal flags and default values.\n");
printf(" --allow-unknown-flags: disable errors for unknown internal flags\n");
printf("\n");
printf("Other commands:\n");
printf(" %s --help: show this help message\n", argv0);
printf(" %s --show-flags: show all internal flags and their values\n", argv0);
}
static void displayFlags()
{
printf("Available flags:\n");
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
{
printf(" %s=%s\n", flag->name, flag->value ? "true" : "false");
}
for (Luau::FValue<int>* flag = Luau::FValue<int>::list; flag; flag = flag->next)
{
printf(" %s=%d\n", flag->name, flag->value);
}
}
static int assertionHandler(const char* expr, const char* file, int line, const char*)
{
printf("%s(%d): ASSERTION FAILED: %s\n", file, line, expr);
return 1;
}
bool isManagedModule(const Luau::ModuleName& name)
{
return Luau::startsWith(name, "game/") || Luau::startsWith(name, "ProjectRoot/") || name == "game" || name == "ProjectRoot";
}
std::string jsonValueToLuau(const json& val)
{
if (val.is_string() || val.is_number() || val.is_boolean())
{
return val.dump();
}
else if (val.is_null())
{
return "nil";
}
else if (val.is_array())
{
std::string out = "{";
for (auto& elem : val)
{
out += jsonValueToLuau(elem);
out += ";";
}
out += "}";
return out;
}
else if (val.is_object())
{
std::string out = "{";
for (auto& [key, val] : val.items())
{
out += "[\"" + key + "\"] = ";
out += jsonValueToLuau(val);
out += ";";
}
out += "}";
return out;
}
else
{
return ""; // TODO: should we error here?
}
}
std::optional<std::string> getCurrentModuleVirtualPath(
const Luau::ModuleName& name, ResolvedSourceMap sourceMap, std::optional<std::filesystem::path> stdinFilepath)
{
if (isManagedModule(name))
{
return name;
}
else
{
// name is a file path which we need to translate to a Rojo path
std::filesystem::path filePath = name;
if (name == "-")
{
if (stdinFilepath)
{
filePath = stdinFilepath.value();
}
else
{
return std::nullopt;
}
}
auto virtualPath = RojoResolver::resolveRealPathToVirtual(sourceMap, filePath);
if (virtualPath)
return virtualPath.value();
}
return std::nullopt;
}
struct CliFileResolver : Luau::FileResolver
{
ResolvedSourceMap sourceMap;
std::optional<std::filesystem::path> stdinFilepath;
bool excludeVirtualPath = false;
bool sourceMapRegistered = false;
std::optional<Luau::SourceCode> readSource(const Luau::ModuleName& name) override
{
Luau::SourceCode::Type sourceType = Luau::SourceCode::Script;
std::optional<std::string> source = std::nullopt;
// If the module name is "-", then read source from stdin
if (name == "-")
{
source = readStdin();
if (stdinFilepath)
{
sourceType = RojoResolver::sourceCodeTypeFromPath(*stdinFilepath);
}
else
{
sourceType = Luau::SourceCode::Script;
}
}
else if (isManagedModule(name))
{
if (std::optional<SourceNodePtr> sourceNodePtr = RojoResolver::resolveRequireToSourceNode(name, sourceMap.root))
{
SourceNode sourceNode = *sourceNodePtr.value();
if (auto path = RojoResolver::getRelevantFilePath(sourceNode))
{
source = readFile(path.value());
// Handle if its a .json file, convert it into a Luau Module
if (source.has_value() && path.value().extension() == ".json")
{
try
{
auto obj = json::parse(source.value());
source = "return " + jsonValueToLuau(obj);
}
catch (const std::exception& ex)
{
fprintf(stderr, "Failed to load JSON module %s: %s\n", path.value().generic_string().c_str(), ex.what());
return std::nullopt;
}
}
if (sourceNode.className.has_value())
{
sourceType = RojoResolver::sourceCodeTypeFromClassName(sourceNode.className.value());
}
else
{
sourceType = RojoResolver::sourceCodeTypeFromPath(path.value());
}
}
}
}
else
{
source = readFile(name);
sourceType = RojoResolver::sourceCodeTypeFromPath(name);
}
if (!source)
return std::nullopt;
return Luau::SourceCode{*source, sourceType};
}
std::optional<Luau::ModuleInfo> resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override
{
if (Luau::AstExprGlobal* g = node->as<Luau::AstExprGlobal>())
{
if (g->name == "game")
return Luau::ModuleInfo{"game"};
if (g->name == "script")
{
if (auto virtualPath = getCurrentModuleVirtualPath(context->name, sourceMap, stdinFilepath))
{
return Luau::ModuleInfo{virtualPath.value()};
}
}
}
else if (Luau::AstExprIndexName* i = node->as<Luau::AstExprIndexName>())
{
if (context)
{
if (strcmp(i->index.value, "Parent") == 0)
{
// Pop the name instead
// HACK: we use getParentPath which is meant to be for real files... but it works so
auto parentPath = getParentPath(context->name);
if (parentPath.has_value())
return Luau::ModuleInfo{parentPath.value(), context->optional};
}
return Luau::ModuleInfo{context->name + '/' + i->index.value, context->optional};
}
}
else if (Luau::AstExprIndexExpr* i_expr = node->as<Luau::AstExprIndexExpr>())
{
if (Luau::AstExprConstantString* index = i_expr->index->as<Luau::AstExprConstantString>())
{
if (context)
return Luau::ModuleInfo{context->name + '/' + std::string(index->value.data, index->value.size), context->optional};
}
}
else if (Luau::AstExprCall* call = node->as<Luau::AstExprCall>(); call && call->self && call->args.size >= 1 && context)
{
if (Luau::AstExprConstantString* index = call->args.data[0]->as<Luau::AstExprConstantString>())
{
Luau::AstName func = call->func->as<Luau::AstExprIndexName>()->index;
if (func == "GetService" && context->name == "game")
return Luau::ModuleInfo{"game/" + std::string(index->value.data, index->value.size)};
if (func == "WaitForChild" || func == "FindFirstChild")
if (context)
return Luau::ModuleInfo{context->name + '/' + std::string(index->value.data, index->value.size), context->optional};
}
}
return std::nullopt;
}
std::string getHumanReadableModuleName(const Luau::ModuleName& name) const override
{
if (name == "-")
return "stdin";
if (isManagedModule(name))
{
std::optional<std::filesystem::path> realFilePath = RojoResolver::resolveRequireToRealPath(name, sourceMap.root);
if (realFilePath.has_value())
{
if (excludeVirtualPath)
{
return realFilePath.value().relative_path().generic_string();
}
return realFilePath.value().relative_path().generic_string() + "[" + name + "]";
}
return name;
}
else
{
return name;
}
}
};
struct CliConfigResolver : Luau::ConfigResolver
{
Luau::Config defaultConfig;
std::optional<SourceNodePtr> sourceMapRoot;
std::optional<std::filesystem::path> stdinFilepath;
mutable std::unordered_map<std::string, Luau::Config> configCache;
mutable std::vector<std::pair<std::filesystem::path, std::string>> configErrors;
CliConfigResolver()
{
defaultConfig.mode = Luau::Mode::Nonstrict;
}
const Luau::Config& getConfig(const Luau::ModuleName& name) const override
{
std::optional<std::filesystem::path> realPath;
if (name == "-")
{
// Handle stdin
realPath = stdinFilepath;
}
else if (isManagedModule(name))
{
if (sourceMapRoot.has_value())
realPath = RojoResolver::resolveRequireToRealPath(name, *sourceMapRoot);
}
else
{
realPath = name;
}
if (!realPath.has_value() || !realPath->has_parent_path())
return defaultConfig;
return readConfigRec(realPath->parent_path());
}
const Luau::Config& readConfigRec(const std::filesystem::path& path) const
{
auto it = configCache.find(path.generic_string());
if (it != configCache.end())
return it->second;
Luau::Config result = path.has_parent_path() ? readConfigRec(path.parent_path()) : defaultConfig;
auto configPath = path / Luau::kConfigName;
if (std::optional<std::string> contents = readFile(configPath))
{
std::optional<std::string> error = Luau::parseConfig(*contents, result);
if (error)
configErrors.push_back({configPath, *error});
}
return configCache[path.generic_string()] = result;
}
};
std::optional<Luau::TypeId> getTypeIdForClass(const Luau::ScopePtr& globalScope, std::optional<std::string> className)
{
std::optional<Luau::TypeFun> baseType;
if (className.has_value())
{
baseType = globalScope->lookupType(className.value());
}
if (!baseType.has_value())
{
baseType = globalScope->lookupType("Instance");
}
if (baseType.has_value())
{
return baseType.value().type;
}
else
{
// If we reach this stage, we couldn't find the class name nor the "Instance" type
// This most likely means a valid definitions file was not provided
return std::nullopt;
}
}
Luau::TypeId makeLazyInstanceType(Luau::TypeArena& arena, const Luau::ScopePtr& globalScope, const SourceNodePtr& nodePtr,
std::optional<Luau::TypeId> parent, const SourceNodePtr& rootNode, const std::string& virtualPath)
{
Luau::LazyTypeVar ltv;
// TODO: capturing shared_ptr in a lambda is a memory leak, we should probably fix this
ltv.thunk = [&arena, globalScope, nodePtr, parent, virtualPath, rootNode]()
{
auto node = *nodePtr;
// TODO: we should cache created instance types and reuse them where possible
// Look up the base class instance
auto baseTypeId = getTypeIdForClass(globalScope, node.className);
if (!baseTypeId.has_value())
{
return Luau::getSingletonTypes().anyType;
}
// Create the ClassTypeVar representing the instance
Luau::ClassTypeVar ctv{node.name, {}, baseTypeId, std::nullopt, {}, {}, "InstanceModules"};
auto typeId = arena.addType(std::move(ctv));
// Attach Parent and Children info
// Get the mutable version of the type var
if (Luau::ClassTypeVar* ctv = Luau::getMutable<Luau::ClassTypeVar>(typeId))
{
// Add the parent
if (parent.has_value())
{
ctv->props["Parent"] = Luau::makeProperty(parent.value());
}
else
{
// Search for the parent type
if (auto parentPath = getParentPath(virtualPath))
{
// TODO: This is an O(n) search
if (auto parentNode = RojoResolver::resolveRequireToSourceNode(parentPath.value(), rootNode))
{
ctv->props["Parent"] = Luau::makeProperty(
makeLazyInstanceType(arena, globalScope, parentNode.value(), std::nullopt, rootNode, parentPath.value()));
}
}
}
// Add the children
for (const auto& child : node.children)
{
auto childName = (*child).name;
auto childPath = virtualPath + "/" + childName;
ctv->props[childName] = Luau::makeProperty(makeLazyInstanceType(arena, globalScope, child, typeId, rootNode, childPath));
}
}
return typeId;
};
return arena.addType(std::move(ltv));
}
// Magic function for `Instance:IsA("ClassName")` predicate
std::optional<Luau::ExprResult<Luau::TypePackId>> magicFunctionInstanceIsA(
Luau::TypeChecker& typeChecker, const Luau::ScopePtr& scope, const Luau::AstExprCall& expr, Luau::ExprResult<Luau::TypePackId> exprResult)
{
if (expr.args.size != 1)
return std::nullopt;
auto index = expr.func->as<Luau::AstExprIndexName>();
auto str = expr.args.data[0]->as<Luau::AstExprConstantString>();
if (!index || !str)
return std::nullopt;
std::optional<Luau::LValue> lvalue = tryGetLValue(*index->expr);
std::optional<Luau::TypeFun> tfun = scope->lookupType(std::string(str->value.data, str->value.size));
if (!lvalue || !tfun)
return std::nullopt;
Luau::TypePackId booleanPack = typeChecker.globalTypes.addTypePack({typeChecker.booleanType});
return Luau::ExprResult<Luau::TypePackId>{booleanPack, {Luau::IsAPredicate{std::move(*lvalue), expr.location, tfun->type}}};
}
// Magic function for `instance:Clone()`, so that we return the exact subclass that `instance` is, rather than just a generic Instance
static std::optional<Luau::ExprResult<Luau::TypePackId>> magicFunctionInstanceClone(
Luau::TypeChecker& typeChecker, const Luau::ScopePtr& scope, const Luau::AstExprCall& expr, Luau::ExprResult<Luau::TypePackId> exprResult)
{
auto index = expr.func->as<Luau::AstExprIndexName>();
if (!index)
return std::nullopt;
Luau::TypeArena& arena = typeChecker.currentModule->internalTypes;
Luau::TypeId instanceType = typeChecker.checkLValueBinding(scope, *index->expr);
return Luau::ExprResult<Luau::TypePackId>{arena.addTypePack({instanceType})};
}
// Magic function for `Instance:FindFirstChildWhichIsA("ClassName")` and friends
std::optional<Luau::ExprResult<Luau::TypePackId>> magicFunctionFindFirstXWhichIsA(
Luau::TypeChecker& typeChecker, const Luau::ScopePtr& scope, const Luau::AstExprCall& expr, Luau::ExprResult<Luau::TypePackId> exprResult)
{
if (expr.args.size < 1)
return std::nullopt;
auto str = expr.args.data[0]->as<Luau::AstExprConstantString>();
if (!str)
return std::nullopt;
std::optional<Luau::TypeFun> tfun = scope->lookupType(std::string(str->value.data, str->value.size));
if (!tfun)
return std::nullopt;
Luau::TypeId nillableClass = Luau::makeOption(typeChecker, typeChecker.globalTypes, tfun->type);
return Luau::ExprResult<Luau::TypePackId>{typeChecker.globalTypes.addTypePack({nillableClass})};
}
int main(int argc, char** argv)
{
Luau::assertHandler() = assertionHandler;
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
if (strncmp(flag->name, "Luau", 4) == 0)
flag->value = true;
if (argc >= 2 && strcmp(argv[1], "--help") == 0)
{
displayHelp(argv[0]);
return 0;
}
if (argc >= 2 && strcmp(argv[1], "--show-flags") == 0)
{
displayFlags();
return 0;
}
ReportFormat format = ReportFormat::Default;
bool annotate = false;
bool dumpMap = false;
bool excludeVirtualPath = false;
std::optional<std::filesystem::path> sourcemapPath = std::nullopt;
std::optional<std::filesystem::path> projectPath = std::nullopt; // TODO: remove when deprecated
std::vector<std::filesystem::path> globalDefsPaths;
std::optional<std::filesystem::path> stdinFilepath = std::nullopt;
bool errorOnUnknownFlags = true;
std::unordered_map<std::string, std::string>* fastValues = new std::unordered_map<std::string, std::string>();
for (int i = 1; i < argc; ++i)
{
if (argv[i][0] != '-')
continue;
if (strcmp(argv[i], "--formatter=plain") == 0)
format = ReportFormat::Luacheck;
else if (strcmp(argv[i], "--formatter=gnu") == 0)
format = ReportFormat::Gnu;
else if (strcmp(argv[i], "--annotate") == 0)
annotate = true;
else if (strcmp(argv[i], "--timetrace") == 0)
FFlag::DebugLuauTimeTracing.value = true;
else if (strcmp(argv[i], "--dump-source-map") == 0)
dumpMap = true;
else if (strcmp(argv[i], "--exclude-virtual-path") == 0)
excludeVirtualPath = true;
else if (strncmp(argv[i], "--sourcemap=", 12) == 0)
sourcemapPath = std::string(argv[i] + 12);
else if (strncmp(argv[i], "--project=", 10) == 0)
projectPath = std::string(argv[i] + 10);
else if (strncmp(argv[i], "--defs=", 7) == 0)
globalDefsPaths.push_back(std::string(argv[i] + 7));
else if (strncmp(argv[i], "--stdin-filepath=", 17) == 0)
stdinFilepath = std::string(argv[i] + 17);
else if (strcmp(argv[i], "--allow-unknown-flags") == 0)
errorOnUnknownFlags = false;
else if (strncmp(argv[i], "--flag:", 7) == 0)
{
std::string flagSet = std::string(argv[i] + 7);
size_t eqIndex = flagSet.find("=");
if (eqIndex == std::string::npos)
{
printf("Bad flag option, missing =: %s\n", flagSet.c_str());
return 1;
}
std::string flagName = flagSet.substr(0, eqIndex);
std::string flagValue = flagSet.substr(eqIndex + 1, flagSet.length());
fastValues->insert_or_assign(flagName, flagValue);
}
}
// Override Fast Flags
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
{
if (fastValues->find(flag->name) != fastValues->end())
{
std::string valueStr = fastValues->at(flag->name);
if (valueStr == "true")
flag->value = true;
else if (valueStr == "false")
flag->value = false;
else
{
printf("Bad flag option, %s expects a boolean 'true' or 'false'\n", flag->name);
return 1;
}
fastValues->erase(flag->name);
}
}
for (Luau::FValue<int>* flag = Luau::FValue<int>::list; flag; flag = flag->next)
{
if (fastValues->find(flag->name) != fastValues->end())
{
std::string valueStr = fastValues->at(flag->name);
int value = 0;
try
{
value = std::stoi(valueStr);
}
catch (...)
{
printf("Bad flag option, %s expects an int\n", flag->name);
return 1;
}
flag->value = value;
fastValues->erase(flag->name);
}
}
if (errorOnUnknownFlags && fastValues->size() != 0)
{
for (auto entry = fastValues->begin(); entry != fastValues->end(); entry++)
{
printf("Unknown flag: %s\n", entry->first.c_str());
}
return 1;
}
#if !defined(LUAU_ENABLE_TIME_TRACE)
if (FFlag::DebugLuauTimeTracing)
{
printf("To run with --timetrace, Luau has to be built with LUAU_ENABLE_TIME_TRACE enabled\n");
return 1;
}
#endif
if (projectPath.has_value() && sourcemapPath.has_value())
{
fprintf(stderr, "Can only use one of --sourcemap=PATH and --project=PATH\n");
return 1;
}
if (projectPath.has_value() && !std::filesystem::exists(projectPath.value()))
{
fprintf(stderr, "Cannot load project path %s: path does not exist\n", projectPath.value().generic_string().c_str());
return 1;
}
if (sourcemapPath.has_value() && !std::filesystem::exists(sourcemapPath.value()))
{
fprintf(stderr, "Cannot load sourcemap path %s: path does not exist\n", projectPath.value().generic_string().c_str());
return 1;
}
Luau::FrontendOptions frontendOptions;
frontendOptions.retainFullTypeGraphs = annotate;
CliConfigResolver configResolver;
configResolver.stdinFilepath = stdinFilepath;
CliFileResolver fileResolver;
fileResolver.excludeVirtualPath = excludeVirtualPath;
fileResolver.stdinFilepath = stdinFilepath;
if (sourcemapPath.has_value() || projectPath.has_value())
{
std::optional<ResolvedSourceMap> sourceMap;
if (sourcemapPath)
{
sourceMap = RojoResolver::parseSourceMap(sourcemapPath.value());
}
else if (projectPath)
{
sourceMap = RojoResolver::parseProjectFile(projectPath.value());
}
if (sourceMap.has_value())
{
fileResolver.sourceMap = sourceMap.value();
fileResolver.sourceMapRegistered = true;
if (dumpMap)
dumpSourceMap(*fileResolver.sourceMap.root, 0);
configResolver.sourceMapRoot = sourceMap->root;
}
}
Luau::Frontend frontend(&fileResolver, &configResolver, frontendOptions);
Luau::registerBuiltinTypes(frontend.typeChecker);
// If global definitions have been provided, then also register them
if (!globalDefsPaths.empty())
{
bool success = true;
for (auto path : globalDefsPaths)
{
std::optional<std::string> defs = readFile(path);
if (defs.has_value())
{
auto loadResult = Luau::loadDefinitionFile(frontend.typeChecker, frontend.typeChecker.globalScope, defs.value(), "@luau");
if (!loadResult.success)
{
success = false;
fprintf(stderr, "Failed to load definitions\n");
for (const auto& error : loadResult.parseResult.errors)
report(format, path.relative_path().generic_string().c_str(), error.getLocation(), "SyntaxError", error.getMessage().c_str());
for (const auto& error : loadResult.module->errors)
if (const Luau::SyntaxError* syntaxError = Luau::get_if<Luau::SyntaxError>(&error.data))
report(
format, path.relative_path().generic_string().c_str(), error.location, "SyntaxError", syntaxError->message.c_str());
else
report(format, path.relative_path().generic_string().c_str(), error.location, "TypeError", Luau::toString(error).c_str());
}
}
else
{
success = false;
}
}
// Apply type overrides
if (success)
{
// Try to extend the globally registered types with the project format
if (fileResolver.sourceMapRegistered)
{
auto rootPtr = fileResolver.sourceMap.root;
auto root = *rootPtr;
if (root.className.has_value() && root.className.value() == "DataModel")
{
for (const auto& service : root.children)
{
auto serviceName = (*service).name; // TODO: change to className
if (auto serviceType = frontend.typeChecker.globalScope->lookupType(serviceName))
{
if (Luau::ClassTypeVar* ctv = Luau::getMutable<Luau::ClassTypeVar>(serviceType.value().type))
{
// Extend the props to include the children
for (const auto& child : (*service).children)
{
auto childName = (*child).name;
ctv->props[childName] =
Luau::makeProperty(makeLazyInstanceType(frontend.typeChecker.globalTypes, frontend.typeChecker.globalScope,
child, serviceType.value().type, rootPtr, "game/" + serviceName + "/" + childName));
}
}
}
}
}
frontend.typeChecker.prepareModuleScope = [fileResolver, stdinFilepath](const Luau::ModuleName& name, const Luau::ScopePtr& scope)
{
auto virtualPath = getCurrentModuleVirtualPath(name, fileResolver.sourceMap, stdinFilepath);
if (!virtualPath.has_value())
return;
auto node = RojoResolver::resolveRequireToSourceNode(virtualPath.value(), fileResolver.sourceMap.root);
if (!node.has_value())
return;
// HACK: we need a way to get the typeArena for the module, but I don't know how
// we can see that moduleScope->returnType is assigned before prepareModuleScope is called in TypeInfer, so we could try it this
// way...
LUAU_ASSERT(scope->returnType);
auto typeArena = scope->returnType->owningArena;
LUAU_ASSERT(typeArena);
scope->bindings[Luau::AstName("script")] = Luau::Binding{
makeLazyInstanceType(*typeArena, scope, node.value(), std::nullopt, fileResolver.sourceMap.root, virtualPath.value()),
Luau::Location{}, {}, {}, std::nullopt};
};
}
// Register Instance:IsA("ClassName") type predicate
// Register FindFirstChildWhichIsA / FindFirstChildOfClassName / FindFirstAncestorWhichIsA / FindFirstAncestorOfClass magic functions
// Register Instance:Clone() magic function
auto instanceType = frontend.typeChecker.globalScope->lookupType("Instance");
if (instanceType.has_value())
{
if (Luau::ClassTypeVar* ctv = Luau::getMutable<Luau::ClassTypeVar>(instanceType.value().type))
{
Luau::attachMagicFunction(ctv->props["IsA"].type, magicFunctionInstanceIsA);
Luau::attachMagicFunction(ctv->props["FindFirstChildWhichIsA"].type, magicFunctionFindFirstXWhichIsA);
Luau::attachMagicFunction(ctv->props["FindFirstChildOfClass"].type, magicFunctionFindFirstXWhichIsA);
Luau::attachMagicFunction(ctv->props["FindFirstAncestorWhichIsA"].type, magicFunctionFindFirstXWhichIsA);
Luau::attachMagicFunction(ctv->props["FindFirstAncestorOfClass"].type, magicFunctionFindFirstXWhichIsA);
Luau::attachMagicFunction(ctv->props["Clone"].type, magicFunctionInstanceClone);
}
}
}
}
Luau::freeze(frontend.typeChecker.globalTypes);
std::vector<std::filesystem::path> files = getSourceFiles(argc, argv);
int failed = 0;
for (const std::filesystem::path& path : files)
failed += !analyzeFile(frontend, path.relative_path().generic_string().c_str(), format, annotate);
if (!configResolver.configErrors.empty())
{
failed += int(configResolver.configErrors.size());
for (const auto& [path, err] : configResolver.configErrors)
fprintf(stderr, "%s: %s\n", path.generic_string().c_str(), err.c_str());
}
if (format == ReportFormat::Luacheck)
return 0;
else
return failed ? 1 : 0;
}