Yerbacon.hpp: Add a yerbacon_output variable to use in JavaScript
Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
parent
1aada9d23f
commit
e3ae8da799
|
@ -78,7 +78,7 @@ if (${IS_GNU} OR ${IS_CLANG})
|
||||||
else()
|
else()
|
||||||
set(IS_HOST_NOT_ANDROID 0)
|
set(IS_HOST_NOT_ANDROID 0)
|
||||||
endif()
|
endif()
|
||||||
set(CMAKE_EXE_LINKER_FLAGS "--closure 1 -lnodefs.js -sFORCE_FILESYSTEM=1 -sWASM=0 --memory-init-file 0 -sEXPORTED_FUNCTIONS=_main -sEXIT_RUNTIME=0 -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 -lembind -sFORCE_FILESYSTEM=1 -sWASM=0 --memory-init-file 0 -sEXPORTED_FUNCTIONS=_main -sEXIT_RUNTIME=0 -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)
|
if (CMAKE_USE_PTHREADS_INIT)
|
||||||
set(CMAKE_CXX_FLAGS "-pthread ${CMAKE_CXX_FLAGS}") # Emscripten requires the -pthread flag
|
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
|
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
|
||||||
|
|
|
@ -36,11 +36,30 @@
|
||||||
#include <emscripten.h>
|
#include <emscripten.h>
|
||||||
static bool is_node;
|
static bool is_node;
|
||||||
void exit(int status) {
|
void exit(int status) {
|
||||||
|
MAIN_THREAD_EM_ASM("yerbacon_output = Module.get_cout();");
|
||||||
if (is_node) emscripten_force_exit(status); else emscripten_exit_with_live_runtime();
|
if (is_node) emscripten_force_exit(status); else emscripten_exit_with_live_runtime();
|
||||||
std::exit(status);
|
std::exit(status);
|
||||||
}
|
}
|
||||||
|
#include <sstream>
|
||||||
|
static std::ostringstream string_cout;
|
||||||
|
using std::cout;
|
||||||
|
#define cout (is_node ? std::cout : string_cout)
|
||||||
|
#define cerr (is_node ? std::cerr : string_cout)
|
||||||
|
std::string get_cout() {
|
||||||
|
static size_t previous_cout_size = 0;
|
||||||
|
std::string output = string_cout.str();
|
||||||
|
const size_t current_size = output.size();
|
||||||
|
output = output.substr(previous_cout_size, current_size);
|
||||||
|
previous_cout_size = current_size;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
#include <emscripten/bind.h>
|
||||||
|
EMSCRIPTEN_BINDINGS() {
|
||||||
|
emscripten::function("get_cout", &get_cout);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
using std::exit;
|
using std::exit;
|
||||||
|
using std::cout;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Yerbacon {
|
namespace Yerbacon {
|
||||||
|
@ -53,9 +72,9 @@ namespace Yerbacon {
|
||||||
}
|
}
|
||||||
static void fail(const std::initializer_list<const char*> reason) {
|
static void fail(const std::initializer_list<const char*> reason) {
|
||||||
std::for_each(reason.begin(), reason.end(), [](const char* current_string){
|
std::for_each(reason.begin(), reason.end(), [](const char* current_string){
|
||||||
std::cout << current_string;
|
cout << current_string;
|
||||||
});
|
});
|
||||||
std::cout << std::endl;
|
cout << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
inline void fail(const char* reason) { fail({reason}); }
|
inline void fail(const char* reason) { fail({reason}); }
|
||||||
|
|
Loading…
Reference in New Issue