diff --git a/CLI/Analyze.cpp b/CLI/Analyze.cpp index 2c411416..6f7bb5f8 100644 --- a/CLI/Analyze.cpp +++ b/CLI/Analyze.cpp @@ -6,9 +6,6 @@ #include "Luau/TypeAttach.h" #include "Luau/Transpiler.h" -#include -#include - #include "FileUtils.h" LUAU_FASTFLAG(DebugLuauTimeTracing) @@ -124,22 +121,25 @@ struct CliFileResolver : Luau::FileResolver { std::optional readSource(const Luau::ModuleName& name) override { + Luau::SourceCode::Type sourceType; + std::optional source = std::nullopt; + // If the module name is "-", then read source from stdin if (name == "-") { - std::cin >> std::noskipws; // Do not skip whitespace when reading from stdin - std::string source {std::istreambuf_iterator(std::cin), std::istreambuf_iterator()}; - if (source.empty()) - return std::nullopt; - - return Luau::SourceCode{source, Luau::SourceCode::Script}; + source = readStdin(); + sourceType = Luau::SourceCode::Script; + } + else + { + source = readFile(name); + sourceType = Luau::SourceCode::Module; } - std::optional source = readFile(name); if (!source) return std::nullopt; - return Luau::SourceCode{*source, Luau::SourceCode::Module}; + return Luau::SourceCode{*source, sourceType}; } std::optional resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override diff --git a/CLI/FileUtils.cpp b/CLI/FileUtils.cpp index 5e1e9047..d76f36ab 100644 --- a/CLI/FileUtils.cpp +++ b/CLI/FileUtils.cpp @@ -15,6 +15,8 @@ #include +#define READ_BUFFER_SIZE 4096 + #ifdef _WIN32 static std::wstring fromUtf8(const std::string& path) { @@ -74,6 +76,20 @@ std::optional readFile(const std::string& name) return result; } +std::optional readStdin() { + std::string result; + char buffer[READ_BUFFER_SIZE] = { }; + + while (fgets(buffer, READ_BUFFER_SIZE, stdin) != nullptr) + result.append(buffer); + + // If eof was not reached for stdin, then a read error occurred + if (!feof(stdin)) + return std::nullopt; + + return result; +} + template static void joinPaths(std::basic_string& str, const Ch* lhs, const Ch* rhs) { diff --git a/CLI/FileUtils.h b/CLI/FileUtils.h index da11f512..97471cdc 100644 --- a/CLI/FileUtils.h +++ b/CLI/FileUtils.h @@ -7,6 +7,7 @@ #include std::optional readFile(const std::string& name); +std::optional readStdin(); bool isDirectory(const std::string& path); bool traverseDirectory(const std::string& path, const std::function& callback);