add emscripten -fexceptions

This commit is contained in:
Pelanyo Kamara 2021-11-08 19:06:47 +00:00
parent 1bff6154e6
commit b90bf4cd52
No known key found for this signature in database
GPG key ID: 1C8B9C40A2527035
3 changed files with 22 additions and 22 deletions

View file

@ -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<std::exception*>(ptr)->what();
}
void executeScript(const char* source)
const char* executeScript(const char* source)
{
// setup flags
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::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

View file

@ -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)

View file

@ -25,12 +25,11 @@
<script>
function output(text) {
document.getElementById("output").value += new Date().toLocaleTimeString() + ": " + text + "\n";
document.getElementById("output").value += "[" + new Date().toLocaleTimeString() + "] " + text.replace('stdin:', '') + "\n";
}
var Module = {
'print': function (msg) { output(msg) },
'printErr': function (err) { } // potentially unused?
'print': function (msg) { output(msg) }
};
function clearInput() {
@ -42,12 +41,10 @@
}
function executeScript() {
try {
Module.ccall('executeScript', null, ['string'], [document.getElementById("script").value]);
}
catch (e) {
output(Module.ccall('getExceptionFromPtr', 'string', ['number'], [e]).replace('stdin', '[ERROR]'));
var err = Module.ccall('executeScript', 'string', ['string'], [document.getElementById("script").value]);
if (err) {
output('Error:' + err.replace('stdin:', ''));
}
}
</script>
<script async src="assets/luau/Luau.Repl.Web.js"></script>
<script async src="assets/luau/luau.js"></script>