Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save bytecode to file through CLI #170

Merged
merged 9 commits into from
Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions CLI/Repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@

#include <memory>

#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif

enum class CompileFormat
{
Default,
Binary
};

static int lua_loadstring(lua_State* L)
{
size_t l = 0;
Expand Down Expand Up @@ -370,7 +381,7 @@ static void reportError(const char* name, const Luau::CompileError& error)
report(name, error.getLocation(), "CompileError", error.what());
}

static bool compileFile(const char* name)
static bool compileFile(const char* name, CompileFormat format)
{
std::optional<std::string> source = readFile(name);
if (!source)
Expand All @@ -387,7 +398,20 @@ static bool compileFile(const char* name)

Luau::compileOrThrow(bcb, *source);

printf("%s", bcb.dumpEverything().c_str());
switch (format)
{
case CompileFormat::Default:
printf("%s", bcb.dumpEverything().c_str());
break;
case CompileFormat::Binary:
#ifdef _WIN32
_setmode(_fileno(stdout), _O_BINARY);
#endif

std::string Bytecode = bcb.getBytecode();
fwrite(Bytecode.c_str(), 1, Bytecode.size(), stdout);
break;
}

return true;
}
Expand All @@ -412,7 +436,7 @@ static void displayHelp(const char* argv0)
printf("\n");
printf("Available modes:\n");
printf(" omitted: compile and run input files one by one\n");
printf(" --compile: compile input files and output resulting bytecode\n");
printf(" --compile[=format]: compile input files and output resulting formatted bytecode (binary or text)\n");
printf("\n");
printf("Available options:\n");
printf(" --profile[=N]: profile the code using N Hz sampling (default 10000) and output results to profile.out\n");
Expand Down Expand Up @@ -444,8 +468,16 @@ int main(int argc, char** argv)
return 0;
}

if (argc >= 2 && strcmp(argv[1], "--compile") == 0)
{

if (argc >= 2 && strncmp(argv[1], "--compile", strlen("--compile")) == 0)
{
CompileFormat format = CompileFormat::Default;

if (strcmp(argv[1], "--compile=binary") == 0)
{
format = CompileFormat::Binary;
}

int failed = 0;

for (int i = 2; i < argc; ++i)
Expand All @@ -457,12 +489,12 @@ int main(int argc, char** argv)
{
traverseDirectory(argv[i], [&](const std::string& name) {
if (name.length() > 4 && name.rfind(".lua") == name.length() - 4)
failed += !compileFile(name.c_str());
failed += !compileFile(name.c_str(), format);
});
}
else
{
failed += !compileFile(argv[i]);
failed += !compileFile(argv[i], format);
}
}

Expand Down