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 8 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
51 changes: 45 additions & 6 deletions CLI/Repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

#include <memory>

enum class CompileFormat
{
Default,
Binary
};

static int lua_loadstring(lua_State* L)
{
size_t l = 0;
Expand Down Expand Up @@ -370,7 +376,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 +393,15 @@ 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:
printf("%s", bcb.getBytecode().data());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use fwrite to stdout since the data is binary

break;
}

return true;
}
Expand All @@ -412,7 +426,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,7 +458,32 @@ int main(int argc, char** argv)
return 0;
}

if (argc >= 2 && strcmp(argv[1], "--compile") == 0)
if ((argc >= 2 && strcmp(argv[1], "--compile=text") == 0) || strcmp(argv[1], "--compile") == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be simpler if we just have if (argc >= 2 && strncmp(argv[1], "--compile", strlen("--compile")) == 0), and inside the if we can compare the argv to determine the format. That way you get to reuse the rest of the code between two branches.

{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs something like _setmode(_fileno(stdout), O_BINARY); when _WIN32 is defined, see
https://stackoverflow.com/questions/23107609/is-there-way-to-set-stdout-to-binary-mode

int failed = 0;

for (int i = 2; i < argc; ++i)
{
if (argv[i][0] == '-')
continue;

if (isDirectory(argv[i]))
{
traverseDirectory(argv[i], [&](const std::string& name) {
if (name.length() > 4 && name.rfind(".lua") == name.length() - 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luau too?

failed += !compileFile(name.c_str(), CompileFormat::Default);
});
}
else
{
failed += !compileFile(argv[i], CompileFormat::Default);
}
}

return failed;
}

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

Expand All @@ -457,12 +496,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(), CompileFormat::Binary);
});
}
else
{
failed += !compileFile(argv[i]);
failed += !compileFile(argv[i], CompileFormat::Binary);
}
}

Expand Down