From eb40adb7279a6394d2931d60e75cce9dc99ea886 Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Sun, 7 Nov 2021 11:24:53 +0000 Subject: [PATCH 1/6] Add support for saving bytecode --- CLI/Repl.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index 323ab1449..b6222f372 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -404,6 +404,39 @@ static bool compileFile(const char* name) } } +static bool dumpFile(const char* name) { + std::optional source = readFile(name); + if (!source) + { + printf("Error opening %s\n", name); + return false; + } + + try + { + Luau::BytecodeBuilder bcb; + Luau::compileOrThrow(bcb, *source); + + std::string Bytecode = bcb.getBytecode(); + std::ofstream out("luau.out"); + out << Bytecode; + out.close(); + + return true; + } + catch (Luau::ParseErrors& e) + { + for (auto& error : e.getErrors()) + reportError(name, error); + return false; + } + catch (Luau::CompileError& e) + { + reportError(name, e); + return false; + } +} + static void displayHelp(const char* argv0) { printf("Usage: %s [--mode] [options] [file list]\n", argv0); @@ -413,6 +446,7 @@ static void displayHelp(const char* argv0) 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(" --binary: compile input file and save binary bytecode blob to luau.out\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"); @@ -444,6 +478,15 @@ int main(int argc, char** argv) return 0; } + if (argc >= 2 && strcmp(argv[1], "--binary") == 0) { + if (!argv[2]) { + printf("Please input a file\n"); + return 1; + } + + return !dumpFile(argv[2]); + } + if (argc >= 2 && strcmp(argv[1], "--compile") == 0) { int failed = 0; From 480ee0df7566944b55173625c584e70a22ac61d3 Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Mon, 8 Nov 2021 07:56:58 +0000 Subject: [PATCH 2/6] Update Repl.cpp --- CLI/Repl.cpp | 86 +++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index b6222f372..892536c2b 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -13,6 +13,12 @@ #include +enum class CompileFormat +{ + Default, + Binary +}; + static int lua_loadstring(lua_State* L) { size_t l = 0; @@ -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 source = readFile(name); if (!source) @@ -387,40 +393,15 @@ static bool compileFile(const char* name) Luau::compileOrThrow(bcb, *source); - printf("%s", bcb.dumpEverything().c_str()); - - return true; - } - catch (Luau::ParseErrors& e) - { - for (auto& error : e.getErrors()) - reportError(name, error); - return false; - } - catch (Luau::CompileError& e) - { - reportError(name, e); - return false; - } -} - -static bool dumpFile(const char* name) { - std::optional source = readFile(name); - if (!source) - { - printf("Error opening %s\n", name); - return false; - } - - try - { - Luau::BytecodeBuilder bcb; - Luau::compileOrThrow(bcb, *source); - - std::string Bytecode = bcb.getBytecode(); - std::ofstream out("luau.out"); - out << Bytecode; - out.close(); + switch (format) + { + case CompileFormat::Default: + printf("%s", bcb.dumpEverything().c_str()); + break; + case CompileFormat::Binary: + printf("%s", bcb.getBytecode().data()); + break; + } return true; } @@ -445,8 +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(" --binary: compile input file and save binary bytecode blob to luau.out\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"); @@ -478,16 +458,32 @@ int main(int argc, char** argv) return 0; } - if (argc >= 2 && strcmp(argv[1], "--binary") == 0) { - if (!argv[2]) { - printf("Please input a file\n"); - return 1; + if (argc >= 2 && strcmp(argv[1], "--compile=text") == 0 || strcmp(argv[1], "--compile") == 0) + { + 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) + failed += !compileFile(name.c_str(), CompileFormat::Default); + }); + } + else + { + failed += !compileFile(argv[i], CompileFormat::Default); + } } - return !dumpFile(argv[2]); + return failed; } - if (argc >= 2 && strcmp(argv[1], "--compile") == 0) + if (argc >= 2 && strcmp(argv[1], "--compile=binary") == 0) { int failed = 0; @@ -500,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); } } From 1f69f37815acb0490d33778ed5abbe1e30f5469a Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Mon, 8 Nov 2021 17:24:11 +0000 Subject: [PATCH 3/6] Create c-cpp.yml --- .github/workflows/c-cpp.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/c-cpp.yml diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml new file mode 100644 index 000000000..e3233268f --- /dev/null +++ b/.github/workflows/c-cpp.yml @@ -0,0 +1,23 @@ +name: C/C++ CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: configure + run: ./configure + - name: make + run: make + - name: make check + run: make check + - name: make distcheck + run: make distcheck From 28037663848a11dc01dce0309ff0d09b69b238ec Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Mon, 8 Nov 2021 17:25:29 +0000 Subject: [PATCH 4/6] Delete c-cpp.yml --- .github/workflows/c-cpp.yml | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 .github/workflows/c-cpp.yml diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml deleted file mode 100644 index e3233268f..000000000 --- a/.github/workflows/c-cpp.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: C/C++ CI - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: configure - run: ./configure - - name: make - run: make - - name: make check - run: make check - - name: make distcheck - run: make distcheck From d42669df0e4785772a748383f23ded8a1acaf739 Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Mon, 8 Nov 2021 17:27:19 +0000 Subject: [PATCH 5/6] Update Repl.cpp --- CLI/Repl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index 892536c2b..47b469bfc 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -458,7 +458,7 @@ int main(int argc, char** argv) return 0; } - if (argc >= 2 && strcmp(argv[1], "--compile=text") == 0 || strcmp(argv[1], "--compile") == 0) + if ((argc >= 2 && strcmp(argv[1], "--compile=text") == 0) || strcmp(argv[1], "--compile") == 0) { int failed = 0; From b69f38e6d167d77a3d080a18ca986ac352631eb3 Mon Sep 17 00:00:00 2001 From: NotDSF <67711164+NotDSF@users.noreply.github.com> Date: Tue, 9 Nov 2021 21:20:54 +0000 Subject: [PATCH 6/6] Update Repl.cpp --- CLI/Repl.cpp | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index 47b469bfc..a5bd5737c 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -13,6 +13,11 @@ #include +#ifdef _WIN32 + #include + #include +#endif + enum class CompileFormat { Default, @@ -399,7 +404,12 @@ static bool compileFile(const char* name, CompileFormat format) printf("%s", bcb.dumpEverything().c_str()); break; case CompileFormat::Binary: - printf("%s", bcb.getBytecode().data()); + #ifdef _WIN32 + _setmode(_fileno(stdout), _O_BINARY); + #endif + + std::string Bytecode = bcb.getBytecode(); + fwrite(Bytecode.c_str(), 1, Bytecode.size(), stdout); break; } @@ -458,33 +468,16 @@ int main(int argc, char** argv) return 0; } - if ((argc >= 2 && strcmp(argv[1], "--compile=text") == 0) || strcmp(argv[1], "--compile") == 0) - { - int failed = 0; - for (int i = 2; i < argc; ++i) - { - if (argv[i][0] == '-') - continue; + if (argc >= 2 && strncmp(argv[1], "--compile", strlen("--compile")) == 0) + { + CompileFormat format = CompileFormat::Default; - if (isDirectory(argv[i])) - { - traverseDirectory(argv[i], [&](const std::string& name) { - if (name.length() > 4 && name.rfind(".lua") == name.length() - 4) - failed += !compileFile(name.c_str(), CompileFormat::Default); - }); - } - else - { - failed += !compileFile(argv[i], CompileFormat::Default); - } + if (strcmp(argv[1], "--compile=binary") == 0) + { + format = CompileFormat::Binary; } - return failed; - } - - if (argc >= 2 && strcmp(argv[1], "--compile=binary") == 0) - { int failed = 0; for (int i = 2; i < argc; ++i) @@ -496,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(), CompileFormat::Binary); + failed += !compileFile(name.c_str(), format); }); } else { - failed += !compileFile(argv[i], CompileFormat::Binary); + failed += !compileFile(argv[i], format); } }