-
Notifications
You must be signed in to change notification settings - Fork 392
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
Changes from 8 commits
eb40adb
480ee0d
dcb7b10
0e3e880
1f69f37
2803766
d42669d
ab48847
b69f38e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,12 @@ | |
|
||
#include <memory> | ||
|
||
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<std::string> source = readFile(name); | ||
if (!source) | ||
|
@@ -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()); | ||
break; | ||
} | ||
|
||
return true; | ||
} | ||
|
@@ -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"); | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be simpler if we just have |
||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs something like |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use
fwrite
tostdout
since the data is binary