diff --git a/Core/LuaApi.cpp b/Core/LuaApi.cpp index 90233d2d..44bdb982 100644 --- a/Core/LuaApi.cpp +++ b/Core/LuaApi.cpp @@ -36,6 +36,7 @@ #define errorCond(cond, text) if(cond) { luaL_error(lua, text); return 0; } #define checkparams() if(!l.CheckParamCount()) { return 0; } #define checkminparams(x) if(!l.CheckParamCount(x)) { return 0; } +#define checkinitdone() if(!_context->CheckInitDone()) { error("This function cannot be called outside a callback"); return 0; } #define checksavestateconditions() if(!_context->CheckInStartFrameEvent() && !_context->CheckInExecOpEvent()) { error("This function must be called inside a StartFrame event callback or a CpuExec memory operation callback"); return 0; } enum class ExecuteCountType @@ -532,6 +533,7 @@ int LuaApi::Reset(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _console->Reset(true); return l.ReturnCount(); } @@ -541,6 +543,7 @@ int LuaApi::Stop(lua_State *lua) LuaCallHelper l(lua); int32_t stopCode = l.ReadInteger(0); checkminparams(0); + checkinitdone(); _console->Stop(stopCode); return l.ReturnCount(); } @@ -549,6 +552,7 @@ int LuaApi::Break(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _debugger->Step(1); return l.ReturnCount(); } @@ -557,6 +561,7 @@ int LuaApi::Resume(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _debugger->Run(); return l.ReturnCount(); } @@ -567,6 +572,7 @@ int LuaApi::Execute(lua_State *lua) ExecuteCountType type = (ExecuteCountType)l.ReadInteger(); int count = l.ReadInteger(); checkparams(); + checkinitdone(); errorCond(count <= 0, "count must be >= 1"); errorCond(type < ExecuteCountType::CpuCycles || type > ExecuteCountType::CpuInstructions, "type is invalid"); diff --git a/Core/LuaScriptingContext.cpp b/Core/LuaScriptingContext.cpp index 1d9aa5ae..ec9c4733 100644 --- a/Core/LuaScriptingContext.cpp +++ b/Core/LuaScriptingContext.cpp @@ -83,6 +83,7 @@ bool LuaScriptingContext::LoadScript(string scriptName, string scriptContent, De if((iErr = lua_pcall(_lua, 0, LUA_MULTRET, 0)) == 0) { //Script loaded properly Log("Script loaded successfully."); + _initDone = true; return true; } } @@ -90,6 +91,7 @@ bool LuaScriptingContext::LoadScript(string scriptName, string scriptContent, De if(lua_isstring(_lua, -1)) { Log(lua_tostring(_lua, -1)); } + return false; } diff --git a/Core/ScriptingContext.cpp b/Core/ScriptingContext.cpp index a7629899..6a1b5b81 100644 --- a/Core/ScriptingContext.cpp +++ b/Core/ScriptingContext.cpp @@ -59,6 +59,11 @@ int ScriptingContext::CallEventCallback(EventType type) return returnValue; } +bool ScriptingContext::CheckInitDone() +{ + return _initDone; +} + bool ScriptingContext::CheckInStartFrameEvent() { return _inStartFrameEvent; diff --git a/Core/ScriptingContext.h b/Core/ScriptingContext.h index 2fccc408..49f4589f 100644 --- a/Core/ScriptingContext.h +++ b/Core/ScriptingContext.h @@ -36,6 +36,7 @@ private: protected: string _scriptName; + bool _initDone = false; vector _callbacks[5][0x10000]; vector _eventCallbacks[(int)EventType::EventTypeSize]; @@ -65,6 +66,7 @@ public: void CallMemoryCallback(uint16_t addr, uint8_t &value, CallbackType type); int CallEventCallback(EventType type); + bool CheckInitDone(); bool CheckInStartFrameEvent(); bool CheckInExecOpEvent(); bool CheckStateLoadedFlag();