diff --git a/src/CBot/CBotCStack.cpp b/src/CBot/CBotCStack.cpp index 4866abba..c8bf4575 100644 --- a/src/CBot/CBotCStack.cpp +++ b/src/CBot/CBotCStack.cpp @@ -351,7 +351,7 @@ CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nId nIdent = 0; CBotTypResult val(-1); - val = CBotExternalCallList::CompileCall(p, ppVars, this); + val = m_prog->GetExternalCalls()->CompileCall(p, ppVars, this); if (val.GetType() < 0) { val = m_prog->GetFunctions()->CompileCall(p->GetString(), ppVars, nIdent); @@ -371,7 +371,7 @@ bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { std::string name = pToken->GetString(); - if ( CBotExternalCallList::CheckCall(name) ) return true; + if ( m_prog->GetExternalCalls()->CheckCall(name) ) return true; CBotFunction* pp = m_prog->GetFunctions(); while ( pp != nullptr ) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 32ce1671..4de2d2e6 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -495,12 +495,11 @@ bool CBotClass::RestoreStaticState(FILE* pf) } //////////////////////////////////////////////////////////////////////////////// -bool CBotClass::CheckCall(CBotToken* &pToken, - CBotDefParam* pParam) +bool CBotClass::CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken) { std::string name = pToken->GetString(); - if ( CBotExternalCallList::CheckCall(name) ) return true; + if ( program->GetExternalCalls()->CheckCall(name) ) return true; CBotFunction* pp = m_pMethod; while ( pp != nullptr ) diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index 262369aa..6233de38 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -354,8 +354,7 @@ public: * \param pParam * \return */ - bool CheckCall(CBotToken* &pToken, - CBotDefParam* pParam); + bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken); private: //! List of classes existing at a given time. diff --git a/src/CBot/CBotExternalCall.cpp b/src/CBot/CBotExternalCall.cpp index 864c26ac..1d88c3b6 100644 --- a/src/CBot/CBotExternalCall.cpp +++ b/src/CBot/CBotExternalCall.cpp @@ -26,10 +26,6 @@ #include "CBot/CBotVar/CBotVar.h" - -std::map> CBotExternalCallList::m_list{}; -void* CBotExternalCallList::m_user = nullptr; - void CBotExternalCallList::Clear() { m_list.clear(); diff --git a/src/CBot/CBotExternalCall.h b/src/CBot/CBotExternalCall.h index 62b25b23..95ff4b6c 100644 --- a/src/CBot/CBotExternalCall.h +++ b/src/CBot/CBotExternalCall.h @@ -115,7 +115,7 @@ public: * \param call Function to add * \return true */ - static bool AddFunction(const std::string& name, std::unique_ptr call); + bool AddFunction(const std::string& name, std::unique_ptr call); /** * \brief Find and call compile function @@ -127,14 +127,14 @@ public: * \param pStack Compilation stack * \return CBotTypResult representing the return type of the function (::CBotTypVar), or an error (::CBotError) */ - static CBotTypResult CompileCall(CBotToken*& p, CBotVar** ppVars, CBotCStack* pStack); + CBotTypResult CompileCall(CBotToken*& p, CBotVar** ppVars, CBotCStack* pStack); /** * \brief Check if function with given name has been defined * \param name Name to check * \return true if function was defined */ - static bool CheckCall(const std::string& name); + bool CheckCall(const std::string& name); /** * \brief Find and call runtime function @@ -147,7 +147,7 @@ public: * \param rettype Return type of the function, as returned by CompileCall() * \return -1 if call failed (no such function), 0 if function requested interruption, 1 on success */ - static int DoCall(CBotToken* token, CBotVar** ppVars, CBotStack* pStack, const CBotTypResult& rettype); + int DoCall(CBotToken* token, CBotVar** ppVars, CBotStack* pStack, const CBotTypResult& rettype); /** * \brief Restore execution status after loading saved state @@ -157,7 +157,7 @@ public: * \param pStack Runtime stack * \return false on failure (e.g. function doesn't exist) */ - static bool RestoreCall(CBotToken* token, CBotVar** ppVar, CBotStack* pStack); + bool RestoreCall(CBotToken* token, CBotVar** ppVar, CBotStack* pStack); /** * \brief Set user pointer to pass to compile functions @@ -166,14 +166,14 @@ public: * * \param pUser User pointer */ - static void SetUserPtr(void* pUser); + void SetUserPtr(void* pUser); /** * \brief Reset the list of registered functions */ - static void Clear(); + void Clear(); private: - static std::map> m_list; - static void* m_user; + std::map> m_list{}; + void* m_user = nullptr; }; \ No newline at end of file diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 5df22120..8d6c8d50 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -315,7 +315,7 @@ CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClas { // looks if the function exists elsewhere if (( pClass != nullptr || !pStack->CheckCall(pp, func->m_Param)) && - ( pClass == nullptr || !pClass->CheckCall(pp, func->m_Param)) ) + ( pClass == nullptr || !pClass->CheckCall(pStack->GetProgram(), func->m_Param, pp)) ) { if (IsOfType(p, ID_OPBLK)) { diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 19c629e6..884e2e1c 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -30,6 +30,8 @@ #include "CBot/stdlib/stdlib.h" +CBotExternalCallList* CBotProgram::m_externalCalls = new CBotExternalCallList(); + CBotProgram::CBotProgram() { } @@ -77,7 +79,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector& CBotToken* p = tokens.get()->GetNext(); // skips the first token (separator) pStack->SetProgram(this); // defined used routines - CBotExternalCallList::SetUserPtr(pUser); + m_externalCalls->SetUserPtr(pUser); // Step 2. Find all function and class definitions while ( pStack->IsOk() && p != nullptr && p->GetType() != 0) @@ -312,7 +314,7 @@ bool CBotProgram::AddFunction(const std::string& name, bool rExec(CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile(CBotVar*& pVar, void* pUser)) { - return CBotExternalCallList::AddFunction(name, std::unique_ptr(new CBotExternalCallDefault(rExec, rCompile))); + return m_externalCalls->AddFunction(name, std::unique_ptr(new CBotExternalCallDefault(rExec, rCompile))); } //////////////////////////////////////////////////////////////////////////////// @@ -435,6 +437,11 @@ void CBotProgram::Init() void CBotProgram::Free() { CBotToken::ClearDefineNum(); - CBotExternalCallList::Clear(); + m_externalCalls->Clear(); CBotClass::Free(); -} \ No newline at end of file +} + +CBotExternalCallList* CBotProgram::GetExternalCalls() +{ + return m_externalCalls; +} diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index 8cc5dc89..80236eaf 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -28,6 +28,7 @@ class CBotFunction; class CBotClass; class CBotStack; class CBotVar; +class CBotExternalCallList; /** * \brief Class that manages a CBot program. This is the main entry point into the CBot engine. @@ -335,7 +336,14 @@ public: */ bool m_bCompileClass; + /** + * \brief Returns static list of all registered external calls + */ + static CBotExternalCallList* GetExternalCalls(); + private: + //! All external calls + static CBotExternalCallList* m_externalCalls; //! All user-defined functions CBotFunction* m_functions = nullptr; //! The entry point function diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 62accc2d..2b2672bf 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -744,7 +744,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo // first looks by the identifier - res = CBotExternalCallList::DoCall(nullptr, ppVar, this, rettype); + res = m_prog->GetExternalCalls()->DoCall(nullptr, ppVar, this, rettype); if (res.GetType() >= 0) return res.GetType(); res = m_prog->GetFunctions()->DoCall(nIdent, "", ppVar, this, token ); @@ -753,7 +753,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo // if not found (recompile?) seeks by name nIdent = 0; - res = CBotExternalCallList::DoCall(token, ppVar, this, rettype); + res = m_prog->GetExternalCalls()->DoCall(token, ppVar, this, rettype); if (res.GetType() >= 0) return res.GetType(); res = m_prog->GetFunctions()->DoCall(nIdent, token->GetString(), ppVar, this, token ); @@ -766,10 +766,12 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo //////////////////////////////////////////////////////////////////////////////// void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) { - if ( m_next == nullptr ) return; + if (m_next == nullptr) return; - if ( !CBotExternalCallList::RestoreCall(token, ppVar, this)) - m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this ); + if (m_prog->GetExternalCalls()->RestoreCall(token, ppVar, this)) + return; + + m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this); } ////////////////////////////////////////////////////////////////////////////////