mirror of
https://github.com/luau-lang/luau.git
synced 2025-04-15 01:13:48 +01:00
Add support for saving bytecode
This commit is contained in:
parent
c6de3bd2e4
commit
eb40adb727
1 changed files with 43 additions and 0 deletions
43
CLI/Repl.cpp
43
CLI/Repl.cpp
|
@ -404,6 +404,39 @@ static bool compileFile(const char* name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool dumpFile(const char* name) {
|
||||||
|
std::optional<std::string> 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)
|
static void displayHelp(const char* argv0)
|
||||||
{
|
{
|
||||||
printf("Usage: %s [--mode] [options] [file list]\n", 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("Available modes:\n");
|
||||||
printf(" omitted: compile and run input files one by one\n");
|
printf(" omitted: compile and run input files one by one\n");
|
||||||
printf(" --compile: compile input files and output resulting bytecode\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("\n");
|
||||||
printf("Available options:\n");
|
printf("Available options:\n");
|
||||||
printf(" --profile[=N]: profile the code using N Hz sampling (default 10000) and output results to profile.out\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;
|
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)
|
if (argc >= 2 && strcmp(argv[1], "--compile") == 0)
|
||||||
{
|
{
|
||||||
int failed = 0;
|
int failed = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue