From b90bf4cd52a547d1101426bf3af05ed8e50f27a5 Mon Sep 17 00:00:00 2001 From: Pelanyo Kamara Date: Mon, 8 Nov 2021 19:06:47 +0000 Subject: [PATCH] add emscripten -fexceptions --- CLI/Repl.cpp | 24 ++++++++++++------------ CMakeLists.txt | 5 ++++- docs/_includes/repl.html | 15 ++++++--------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index a0c74016..36159841 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -174,7 +174,7 @@ static std::string runCode(lua_State* L, const std::string& source) else { std::string error; - + if (status == LUA_YIELD) { error = "thread yielded unexpectedly"; @@ -187,6 +187,11 @@ static std::string runCode(lua_State* L, const std::string& source) error += "\nstack backtrace:\n"; error += lua_debugtrace(T); +#ifdef __EMSCRIPTEN__ + // nicer formatting for errors in web repl + error = "Error:" + error; +#endif + fprintf(stdout, "%s", error.c_str()); } @@ -197,16 +202,7 @@ static std::string runCode(lua_State* L, const std::string& source) #ifdef __EMSCRIPTEN__ extern "C" { - // Luau errors are exceptions (see luaD_throw) which cannot be directly - // handled by emscripten. However we can recieve the pointer in JS and - // pass it through to this method to get the string content of the - // exception. - const char* getExceptionFromPtr(int ptr) - { - return reinterpret_cast(ptr)->what(); - } - - void executeScript(const char* source) + const char* executeScript(const char* source) { // setup flags for (Luau::FValue* flag = Luau::FValue::list; flag; flag = flag->next) @@ -220,14 +216,18 @@ extern "C" // setup state setupState(L); + // sandbox thread + luaL_sandboxthread(L); + // run code + collect error std::string error = runCode(L, source); // output error(s) if (error.length()) { - fprintf(stdout, "%s\n", error.c_str()); + return std::move(error.c_str()); } + return NULL; } } #endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 79c7ee31..36014a98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,9 @@ if(LUAU_BUILD_CLI) add_executable(Luau.Repl.CLI) if(NOT EMSCRIPTEN) add_executable(Luau.Analyze.CLI) + else() + # add -fexceptions for emscripten to allow exceptions to be caught in C++ + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions") endif() # This also adds target `name` on Linux/macOS and `name.exe` on Windows @@ -85,7 +88,7 @@ if(LUAU_BUILD_CLI) if(EMSCRIPTEN) # declare exported functions to emscripten - target_link_options(Luau.Repl.CLI PRIVATE -sEXPORTED_FUNCTIONS=['_getExceptionFromPtr','_executeScript'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] -sNO_DISABLE_EXCEPTION_CATCHING) + target_link_options(Luau.Repl.CLI PRIVATE -sEXPORTED_FUNCTIONS=['_executeScript'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] -fexceptions) # custom output directory for wasm + js file set_target_properties(Luau.Repl.CLI PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/docs/assets/luau) diff --git a/docs/_includes/repl.html b/docs/_includes/repl.html index c2189a52..034dc5f7 100644 --- a/docs/_includes/repl.html +++ b/docs/_includes/repl.html @@ -25,12 +25,11 @@ - \ No newline at end of file + \ No newline at end of file