From ebdf8dfd3a6b83c3409ba6cd9fb8760b5b4983ca Mon Sep 17 00:00:00 2001 From: Leonardo Date: Sat, 25 Feb 2023 23:34:58 +0200 Subject: [PATCH] Avoids crashing Rack if the Vult compiler does not run correctly. --- src/VultEngine.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/VultEngine.cpp b/src/VultEngine.cpp index b90ae72..3bfd95d 100644 --- a/src/VultEngine.cpp +++ b/src/VultEngine.cpp @@ -26,7 +26,10 @@ struct VultEngine : ScriptEngine { JSRuntime* rt = NULL; JSContext* ctx = NULL; + bool js_failure; + VultEngine() { + js_failure = false; rt = JS_NewRuntime(); // Create QuickJS context ctx = JS_NewContext(rt); @@ -41,9 +44,11 @@ struct VultEngine : ScriptEngine { JSValue val = JS_Eval(ctx, (const char*)vultc_h, vultc_h_size, "vultc.js", 0); if (JS_IsException(val)) { - display("Error loading the Vult compiler"); + WARN("Error loading the Vult compiler"); + WARN("%s", JS_ToCString(ctx, JS_ToString(ctx,val))); JS_FreeValue(ctx, val); JS_FreeValue(ctx, global_obj); + js_failure = true; return; } } @@ -62,6 +67,11 @@ struct VultEngine : ScriptEngine { } int run(const std::string& path, const std::string& script) override { + if (js_failure) { + display("Error loading the Vult compiler"); + return -1; + } + display("Loading..."); JSValue global_obj = JS_GetGlobalObject(ctx); @@ -142,8 +152,8 @@ struct VultEngine : ScriptEngine { } int process() override { - if (!luaEngine) - return -1; + if (!luaEngine || js_failure) + return -1; return luaEngine->process(); } };