foxygit / fake-08-xbox360 Log in
commit c5c874f34f9d55f6c0571eb3391bb7e3ccab8305
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Wed Dec 31 13:36:38 2025 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Wed Dec 31 13:36:38 2025 -0700

    return to menu instead of freezing on lua error
---
 source/p8GlobalLuaFunctions.h |  3 +++
 source/vm.cpp                 | 23 +++++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index a604bf1..b92705d 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -581,6 +581,9 @@ function __z8_tick()
         __z8_stopped = false -- FIXME: what now?
     elseif not ret then
         printh(tostr(err))
+        -- Return to menu on error
+        __loaddefaultcart()
+        return -1
     end
     return 0
 end
diff --git a/source/vm.cpp b/source/vm.cpp
index d7ba4b8..2603208 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -601,10 +601,20 @@ bool Vm::Step(){
         char const *message = lua_tostring(_luaState, -1);
         Logger_Write("error calling tick function: %s\n", message);
         _cartLoadError = "Error in main loop: " + std::string(message);
+        lua_pop(_luaState, 1);
+        // Return to menu on error
+        QueueCartChange(DefaultCartName);
+        return false;
     }
     else
     {
-        ret = (int)lua_tonumber(_luaState, -1) >= 0;
+        int tickResult = (int)lua_tonumber(_luaState, -1);
+        ret = tickResult >= 0;
+        // If __z8_tick returned -1, it means there was an error (already handled in Lua)
+        // but we should ensure error message is set if it's not already
+        if (tickResult == -1 && _cartLoadError.empty()) {
+            _cartLoadError = "Error in cart execution";
+        }
     }
     lua_pop(_luaState, 1);

@@ -1234,7 +1244,16 @@ void Vm::vm_run() {

     lua_getglobal(_luaState, "__z8_run_cart");
     lua_pushstring(_luaState, _loadedCart->LuaString.c_str());
-    lua_pcall(_luaState, 1, 0, 0);
+    int status = lua_pcall(_luaState, 1, 0, 0);
+    if (status != LUA_OK)
+    {
+        char const *message = lua_tostring(_luaState, -1);
+        Logger_Write("error in vm_run: %s\n", message);
+        _cartLoadError = "Error loading cart: " + std::string(message);
+        lua_pop(_luaState, 1);
+        // Return to menu on error
+        QueueCartChange(DefaultCartName);
+    }

 }