Only use stdio.h to read stdin, avoid using C++ I/O libraries

This commit is contained in:
vladmarica 2022-01-24 22:34:31 -08:00
parent 41336505c6
commit f77d1e93c1
3 changed files with 28 additions and 11 deletions

View file

@ -6,9 +6,6 @@
#include "Luau/TypeAttach.h"
#include "Luau/Transpiler.h"
#include <iterator>
#include <iostream>
#include "FileUtils.h"
LUAU_FASTFLAG(DebugLuauTimeTracing)
@ -124,22 +121,25 @@ struct CliFileResolver : Luau::FileResolver
{
std::optional<Luau::SourceCode> readSource(const Luau::ModuleName& name) override
{
Luau::SourceCode::Type sourceType;
std::optional<std::string> 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<char>(std::cin), std::istreambuf_iterator<char>()};
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<std::string> source = readFile(name);
if (!source)
return std::nullopt;
return Luau::SourceCode{*source, Luau::SourceCode::Module};
return Luau::SourceCode{*source, sourceType};
}
std::optional<Luau::ModuleInfo> resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override

View file

@ -15,6 +15,8 @@
#include <string.h>
#define READ_BUFFER_SIZE 4096
#ifdef _WIN32
static std::wstring fromUtf8(const std::string& path)
{
@ -74,6 +76,20 @@ std::optional<std::string> readFile(const std::string& name)
return result;
}
std::optional<std::string> 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<typename Ch>
static void joinPaths(std::basic_string<Ch>& str, const Ch* lhs, const Ch* rhs)
{

View file

@ -7,6 +7,7 @@
#include <vector>
std::optional<std::string> readFile(const std::string& name);
std::optional<std::string> readStdin();
bool isDirectory(const std::string& path);
bool traverseDirectory(const std::string& path, const std::function<void(const std::string& name)>& callback);