VScript: move script run code to dedicated method

This commit is contained in:
Kawe Mazidjatari 2024-04-19 13:12:35 +02:00
parent 1455017419
commit f70cc90eb9
3 changed files with 33 additions and 15 deletions

View File

@ -101,6 +101,35 @@ SQRESULT CSquirrelVM::RegisterConstant(const SQChar* name, SQInteger value)
return CSquirrelVM__RegisterConstant(this, name, value);
}
//---------------------------------------------------------------------------------
// Purpose: runs text as script on the VM
// Input : *script -
// Output : true on success, false otherwise
//---------------------------------------------------------------------------------
bool CSquirrelVM::Run(const SQChar* const script)
{
Assert(m_hVM);
bool success = false;
SQBufState bufState(script);
if (SQ_SUCCEEDED(sq_compilebuffer(m_hVM, &bufState, "unnamed", -1, SQTrue)))
{
SQObject hScript;
sq_getstackobj(m_hVM, -1, &hScript);
sq_addref(m_hVM, &hScript);
sq_pop(m_hVM, 1);
if (ExecuteFunction((HSCRIPT)&hScript, NULL, 0, NULL, NULL) == SCRIPT_DONE)
success = true;
sq_release(m_hVM, &hScript);
}
return success;
}
//---------------------------------------------------------------------------------
// Purpose: executes a code callback
// Input : *name -

View File

@ -31,6 +31,7 @@ public:
FORCEINLINE SQCONTEXT GetContext() const { return m_iContext; }
FORCEINLINE eDLL_T GetNativeContext() const { return (eDLL_T)GetContext(); }
bool Run(const SQChar* const script);
ScriptStatus_t ExecuteFunction(HSCRIPT hFunction, void** pArgs, unsigned int nArgs, void* pReturn, HSCRIPT hScope);
private:

View File

@ -156,22 +156,10 @@ void Script_Execute(const SQChar* code, const SQCONTEXT context)
return;
}
SQBufState bufState(code);
if (SQ_SUCCEEDED(sq_compilebuffer(v, &bufState, "unnamed", -1, SQTrue)))
if (!s->Run(code))
{
SQObject hScript;
sq_getstackobj(v, -1, &hScript);
sq_addref(v, &hScript);
sq_pop(v, 1);
if (s->ExecuteFunction((HSCRIPT)&hScript, NULL, 0, NULL, NULL) == SCRIPT_ERROR)
{
Error(eDLL_T::ENGINE, NO_ERROR, "Failed to execute %s script \"%s\"\n", contextName, code);
}
sq_release(v, &hScript);
Error(eDLL_T::ENGINE, NO_ERROR, "Failed to run %s script \"%s\"\n", contextName, code);
return;
}
}