From d1ebbda1732afb14df4c0e0827f29376b7863b32 Mon Sep 17 00:00:00 2001 From: Username404 Date: Sun, 4 Dec 2022 13:50:12 +0100 Subject: [PATCH] Mount the filesystem when running on node.js Signed-off-by: Username404 --- CMakeLists.txt | 2 +- src/headers/Yerbacon.hpp | 3 +++ src/main.cpp | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c78f2c5..b0c03f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ if (${IS_GNU} OR ${IS_CLANG}) else() set(IS_HOST_NOT_ANDROID 0) endif() - set(CMAKE_EXE_LINKER_FLAGS "--closure 1 -sWASM=0 --memory-init-file 0 -sEXPORTED_FUNCTIONS=_main -sEXIT_RUNTIME=1 -sSINGLE_FILE=1 -sSAFE_HEAP=${IS_HOST_NOT_ANDROID} -sABORTING_MALLOC=0 -sJS_MATH=1 -sENVIRONMENT='web,webview,worker,node,shell' -sNODEJS_CATCH_EXIT=0 -sSTRICT=1 -sMINIMAL_RUNTIME=0 -sDISABLE_EXCEPTION_CATCHING=0 -sALLOW_MEMORY_GROWTH=1 -Wno-pthreads-mem-growth -sMEMORY_GROWTH_GEOMETRIC_STEP=0 -sDECLARE_ASM_MODULE_EXPORTS=0 ${CMAKE_EXE_LINKER_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "--closure 1 -lnodefs.js -sFORCE_FILESYSTEM=1 -sWASM=0 --memory-init-file 0 -sEXPORTED_FUNCTIONS=_main -sEXIT_RUNTIME=1 -sSINGLE_FILE=1 -sSAFE_HEAP=${IS_HOST_NOT_ANDROID} -sABORTING_MALLOC=0 -sJS_MATH=1 -sENVIRONMENT='web,webview,worker,node,shell' -sNODEJS_CATCH_EXIT=0 -sSTRICT=1 -sMINIMAL_RUNTIME=0 -sDISABLE_EXCEPTION_CATCHING=0 -sALLOW_MEMORY_GROWTH=1 -Wno-pthreads-mem-growth -sMEMORY_GROWTH_GEOMETRIC_STEP=0 -sDECLARE_ASM_MODULE_EXPORTS=0 ${CMAKE_EXE_LINKER_FLAGS}") if (CMAKE_USE_PTHREADS_INIT) set(CMAKE_CXX_FLAGS "-pthread ${CMAKE_CXX_FLAGS}") # Emscripten requires the -pthread flag set(CMAKE_EXE_LINKER_FLAGS "--closure-args=\"--compilation_level SIMPLE\" -sPROXY_TO_PTHREAD=1 ${CMAKE_EXE_LINKER_FLAGS}") # See https://github.com/emscripten-core/emscripten/issues/16706 diff --git a/src/headers/Yerbacon.hpp b/src/headers/Yerbacon.hpp index 801deed..e24639a 100644 --- a/src/headers/Yerbacon.hpp +++ b/src/headers/Yerbacon.hpp @@ -32,6 +32,9 @@ #include #include #include +#ifdef EMSCRIPTEN +#include +#endif namespace Yerbacon { consteval const char* getVersion() noexcept { return YBCON_VERSION; } diff --git a/src/main.cpp b/src/main.cpp index b5993ee..56fdfa0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,23 @@ int main(int argc, char* argv[]) { #ifdef _OPENMP if (not parallel) omp_set_num_threads(1); #endif + #ifdef EMSCRIPTEN + const string directory (currentArgument.substr(0, currentArgument.find_last_of(filesystem::path::preferred_separator) + 1)); + for (unsigned int i = 1; i < directory.size(); ++i) { + const string sub_directory = directory.substr(0, i); + if (directory[i] == '/' 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)) { + FS.mkdir(sub_directory); + FS.mount(NODEFS, {root: sub_directory}, sub_directory); + }; + } + }, sub_directory.c_str()); + } + } + #endif Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [currentArgument, &text_provided, &target, &newLines]() { unit_result resultingPair; try {