diff options
author | Li Jin <dragon-fly@qq.com> | 2023-07-24 17:55:21 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-07-24 17:55:21 +0800 |
commit | c03b88e6210818e604d7c755fe8f54fe53696ee1 (patch) | |
tree | 3d297ce64171481caeb0e86f42a206e8322abeca /src/yue.cpp | |
parent | 00c4bee811b3c92d9885959db30790b01f8cb3e2 (diff) | |
download | yuescript-c03b88e6210818e604d7c755fe8f54fe53696ee1.tar.gz yuescript-c03b88e6210818e604d7c755fe8f54fe53696ee1.tar.bz2 yuescript-c03b88e6210818e604d7c755fe8f54fe53696ee1.zip |
try fixing issue #141. fix an AST object life expired before accessing issue.
Diffstat (limited to 'src/yue.cpp')
-rw-r--r-- | src/yue.cpp | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index 98bf4b4..4dec0e4 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
@@ -246,6 +246,7 @@ int main(int narg, const char** args) { | |||
246 | #ifndef YUE_COMPILER_ONLY | 246 | #ifndef YUE_COMPILER_ONLY |
247 | " -e str Execute a file or raw codes\n" | 247 | " -e str Execute a file or raw codes\n" |
248 | " -m Generate minified codes\n" | 248 | " -m Generate minified codes\n" |
249 | " -r Rewrite output to match original line numbers\n" | ||
249 | #endif // YUE_COMPILER_ONLY | 250 | #endif // YUE_COMPILER_ONLY |
250 | " -t path Specify where to place compiled files\n" | 251 | " -t path Specify where to place compiled files\n" |
251 | " -o file Write output to file\n" | 252 | " -o file Write output to file\n" |
@@ -409,6 +410,7 @@ int main(int narg, const char** args) { | |||
409 | return 0; | 410 | return 0; |
410 | } | 411 | } |
411 | bool minify = false; | 412 | bool minify = false; |
413 | bool rewrite = false; | ||
412 | #endif // YUE_COMPILER_ONLY | 414 | #endif // YUE_COMPILER_ONLY |
413 | yue::YueConfig config; | 415 | yue::YueConfig config; |
414 | config.implicitReturnRoot = true; | 416 | config.implicitReturnRoot = true; |
@@ -522,6 +524,8 @@ int main(int narg, const char** args) { | |||
522 | } | 524 | } |
523 | } else if (arg == "-m"sv) { | 525 | } else if (arg == "-m"sv) { |
524 | minify = true; | 526 | minify = true; |
527 | } else if (arg == "-r"sv) { | ||
528 | rewrite = true; | ||
525 | #endif // YUE_COMPILER_ONLY | 529 | #endif // YUE_COMPILER_ONLY |
526 | } else if (arg == "-s"sv) { | 530 | } else if (arg == "-s"sv) { |
527 | config.useSpaceOverTab = true; | 531 | config.useSpaceOverTab = true; |
@@ -607,6 +611,16 @@ int main(int narg, const char** args) { | |||
607 | std::cout << "Error: -o can not be used with multiple input files\n"sv; | 611 | std::cout << "Error: -o can not be used with multiple input files\n"sv; |
608 | return 1; | 612 | return 1; |
609 | } | 613 | } |
614 | #ifndef YUE_COMPILER_ONLY | ||
615 | if (minify || rewrite) { | ||
616 | if (minify) { | ||
617 | rewrite = false; | ||
618 | } | ||
619 | if (rewrite) { | ||
620 | config.reserveLineNumber = true; | ||
621 | } | ||
622 | } | ||
623 | #endif // YUE_COMPILER_ONLY | ||
610 | #ifndef YUE_NO_WATCHER | 624 | #ifndef YUE_NO_WATCHER |
611 | if (watchFiles) { | 625 | if (watchFiles) { |
612 | auto fullWorkPath = fs::absolute(fs::path(workPath)).string(); | 626 | auto fullWorkPath = fs::absolute(fs::path(workPath)).string(); |
@@ -744,7 +758,7 @@ int main(int narg, const char** args) { | |||
744 | DEFER({ | 758 | DEFER({ |
745 | if (L) lua_close(L); | 759 | if (L) lua_close(L); |
746 | }); | 760 | }); |
747 | if (minify) { | 761 | if (minify || rewrite) { |
748 | L = luaL_newstate(); | 762 | L = luaL_newstate(); |
749 | luaL_openlibs(L); | 763 | luaL_openlibs(L); |
750 | pushLuaminify(L); | 764 | pushLuaminify(L); |
@@ -766,7 +780,7 @@ int main(int narg, const char** args) { | |||
766 | errs.push_back(msg); | 780 | errs.push_back(msg); |
767 | } else { | 781 | } else { |
768 | #ifndef YUE_COMPILER_ONLY | 782 | #ifndef YUE_COMPILER_ONLY |
769 | if (minify) { | 783 | if (minify || rewrite) { |
770 | std::ifstream input(file, std::ios::in); | 784 | std::ifstream input(file, std::ios::in); |
771 | if (input) { | 785 | if (input) { |
772 | std::string s; | 786 | std::string s; |
@@ -780,27 +794,24 @@ int main(int narg, const char** args) { | |||
780 | input.close(); | 794 | input.close(); |
781 | int top = lua_gettop(L); | 795 | int top = lua_gettop(L); |
782 | DEFER(lua_settop(L, top)); | 796 | DEFER(lua_settop(L, top)); |
783 | lua_pushvalue(L, -1); | 797 | lua_getfield(L, -1, rewrite ? "FormatYue" : "FormatMini"); |
784 | lua_pushlstring(L, s.c_str(), s.size()); | 798 | lua_pushlstring(L, s.c_str(), s.size()); |
785 | if (lua_pcall(L, 1, 1, 0) != 0) { | 799 | if (lua_pcall(L, 1, 1, 0) != 0) { |
786 | ret = 2; | 800 | ret = 2; |
787 | std::string err = lua_tostring(L, -1); | 801 | std::string err = lua_tostring(L, -1); |
788 | errs.push_back("Failed to minify: "s + file + '\n' + err + '\n'); | 802 | errs.push_back((rewrite ? "Failed to rewrite: "s : "Failed to minify: "s) + file + '\n' + err + '\n'); |
789 | } else { | 803 | } else { |
790 | size_t size = 0; | 804 | size_t size = 0; |
791 | const char* minifiedCodes = lua_tolstring(L, -1, &size); | 805 | const char* transformedCodes = lua_tolstring(L, -1, &size); |
792 | if (writeToFile) { | 806 | if (writeToFile) { |
793 | std::ofstream output(file, std::ios::trunc | std::ios::out); | 807 | std::ofstream output(file, std::ios::trunc | std::ios::out); |
794 | output.write(minifiedCodes, size); | 808 | output.write(transformedCodes, size); |
795 | output.close(); | 809 | output.close(); |
796 | std::cout << "Minified built "sv << file << '\n'; | 810 | std::cout << (rewrite ? "Rewrited built "sv : "Minified built "sv) << file << '\n'; |
797 | } else { | 811 | } else { |
798 | std::cout << minifiedCodes << '\n'; | 812 | std::cout << transformedCodes << '\n'; |
799 | } | 813 | } |
800 | } | 814 | } |
801 | } else { | ||
802 | ret = 2; | ||
803 | errs.push_back("Failed to minify: "s + file + '\n'); | ||
804 | } | 815 | } |
805 | } else { | 816 | } else { |
806 | std::cout << msg; | 817 | std::cout << msg; |