From 1aada9d23fd21ce04cc47606e193439890106780 Mon Sep 17 00:00:00 2001 From: Username404 Date: Sun, 8 Jan 2023 01:12:52 +0100 Subject: [PATCH] main.cpp: Force runtime exit when using node.js Signed-off-by: Username404 --- src/headers/Yerbacon.hpp | 9 ++++++++- src/main.cpp | 25 +++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/headers/Yerbacon.hpp b/src/headers/Yerbacon.hpp index e24639a..fdec0b8 100644 --- a/src/headers/Yerbacon.hpp +++ b/src/headers/Yerbacon.hpp @@ -34,6 +34,13 @@ #include #ifdef EMSCRIPTEN #include +static bool is_node; +void exit(int status) { + if (is_node) emscripten_force_exit(status); else emscripten_exit_with_live_runtime(); + std::exit(status); +} +#else +using std::exit; #endif namespace Yerbacon { @@ -49,7 +56,7 @@ namespace Yerbacon { std::cout << current_string; }); std::cout << std::endl; - std::exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } inline void fail(const char* reason) { fail({reason}); } class Exception: public std::exception { diff --git a/src/main.cpp b/src/main.cpp index 8cc182b..11580e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,9 @@ int main(int argc, char* argv[]) { using unit_result = pair>; using unit = future; map Units; + #ifdef EMSCRIPTEN + is_node = MAIN_THREAD_EM_ASM_INT({ return ENVIRONMENT_IS_NODE; }); + #endif for (signed int i = 1; i < argc; ++i) { const string_view currentArgument (argv[i]); @@ -54,17 +57,15 @@ int main(int argc, char* argv[]) { const string directory = filesystem::path(file_path.value()).remove_filename(); for (unsigned int character = 1; character < directory.size(); ++character) { const string sub_directory = directory.substr(0, character); - if ((directory[character] == '/') and not filesystem::is_directory(sub_directory)) { + if (is_node and (directory[character] == '/') and not filesystem::is_directory(sub_directory)) { MAIN_THREAD_EM_ASM({ - if ((typeof process !== 'undefined') && (process.release.name === 'node')) { - const sub_directory = UTF8ToString($0); - if (!FS.isMountpoint(sub_directory)) { - try { - FS.mkdir(sub_directory); - FS.mount(NODEFS, {root: sub_directory}, sub_directory); - } catch (exception) {} - }; - } + const sub_directory = UTF8ToString($0); + if (!FS.isMountpoint(sub_directory)) { + try { + FS.mkdir(sub_directory); + FS.mount(NODEFS, {root: sub_directory}, sub_directory); + } catch (exception) {} + }; }, sub_directory.c_str()); } } @@ -108,9 +109,9 @@ int main(int argc, char* argv[]) { } else { cout << "Compilation"; if (not text_provided) cout << " of " << result.first; cout << " has failed with the following error:" << endl; cerr << result.second.value().what() << '\n'; - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } } else cout << (!text_provided ? "No valid file provided" : "No text was provided") << ".\n"; - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } \ No newline at end of file