mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
add emscripten -fexceptions
This commit is contained in:
parent
1bff6154e6
commit
b90bf4cd52
3 changed files with 22 additions and 22 deletions
22
CLI/Repl.cpp
22
CLI/Repl.cpp
|
@ -187,6 +187,11 @@ static std::string runCode(lua_State* L, const std::string& source)
|
||||||
error += "\nstack backtrace:\n";
|
error += "\nstack backtrace:\n";
|
||||||
error += lua_debugtrace(T);
|
error += lua_debugtrace(T);
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
// nicer formatting for errors in web repl
|
||||||
|
error = "Error:" + error;
|
||||||
|
#endif
|
||||||
|
|
||||||
fprintf(stdout, "%s", error.c_str());
|
fprintf(stdout, "%s", error.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,16 +202,7 @@ static std::string runCode(lua_State* L, const std::string& source)
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
// Luau errors are exceptions (see luaD_throw) which cannot be directly
|
const char* executeScript(const char* source)
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
// setup flags
|
// setup flags
|
||||||
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
|
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
|
||||||
|
@ -220,14 +216,18 @@ extern "C"
|
||||||
// setup state
|
// setup state
|
||||||
setupState(L);
|
setupState(L);
|
||||||
|
|
||||||
|
// sandbox thread
|
||||||
|
luaL_sandboxthread(L);
|
||||||
|
|
||||||
// run code + collect error
|
// run code + collect error
|
||||||
std::string error = runCode(L, source);
|
std::string error = runCode(L, source);
|
||||||
|
|
||||||
// output error(s)
|
// output error(s)
|
||||||
if (error.length())
|
if (error.length())
|
||||||
{
|
{
|
||||||
fprintf(stdout, "%s\n", error.c_str());
|
return std::move(error.c_str());
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -19,6 +19,9 @@ if(LUAU_BUILD_CLI)
|
||||||
add_executable(Luau.Repl.CLI)
|
add_executable(Luau.Repl.CLI)
|
||||||
if(NOT EMSCRIPTEN)
|
if(NOT EMSCRIPTEN)
|
||||||
add_executable(Luau.Analyze.CLI)
|
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()
|
endif()
|
||||||
|
|
||||||
# This also adds target `name` on Linux/macOS and `name.exe` on Windows
|
# This also adds target `name` on Linux/macOS and `name.exe` on Windows
|
||||||
|
@ -85,7 +88,7 @@ if(LUAU_BUILD_CLI)
|
||||||
|
|
||||||
if(EMSCRIPTEN)
|
if(EMSCRIPTEN)
|
||||||
# declare exported functions to 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
|
# custom output directory for wasm + js file
|
||||||
set_target_properties(Luau.Repl.CLI PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/docs/assets/luau)
|
set_target_properties(Luau.Repl.CLI PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/docs/assets/luau)
|
||||||
|
|
|
@ -25,12 +25,11 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function output(text) {
|
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 = {
|
var Module = {
|
||||||
'print': function (msg) { output(msg) },
|
'print': function (msg) { output(msg) }
|
||||||
'printErr': function (err) { } // potentially unused?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function clearInput() {
|
function clearInput() {
|
||||||
|
@ -42,12 +41,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeScript() {
|
function executeScript() {
|
||||||
try {
|
var err = Module.ccall('executeScript', 'string', ['string'], [document.getElementById("script").value]);
|
||||||
Module.ccall('executeScript', null, ['string'], [document.getElementById("script").value]);
|
if (err) {
|
||||||
}
|
output('Error:' + err.replace('stdin:', ''));
|
||||||
catch (e) {
|
|
||||||
output(Module.ccall('getExceptionFromPtr', 'string', ['number'], [e]).replace('stdin', '[ERROR]'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script async src="assets/luau/Luau.Repl.Web.js"></script>
|
<script async src="assets/luau/luau.js"></script>
|
Loading…
Add table
Reference in a new issue