1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implement a non-throwing routine lookup.

This commit is contained in:
Sandu Liviu Catalin 2021-07-14 20:03:24 +03:00
parent 2344189c10
commit 2701b6487a
2 changed files with 33 additions and 8 deletions

View File

@ -559,6 +559,7 @@ void Register_Routine(HSQUIRRELVM vm)
RootTable(vm).SquirrelFunc(_SC("SqRoutine"), &Routine::Create); RootTable(vm).SquirrelFunc(_SC("SqRoutine"), &Routine::Create);
RootTable(vm).FmtFunc(_SC("SqFindRoutineByTag"), &Routine::FindByTag); RootTable(vm).FmtFunc(_SC("SqFindRoutineByTag"), &Routine::FindByTag);
RootTable(vm).FmtFunc(_SC("SqIsRoutineWithTag"), &Routine::IsWithTag); RootTable(vm).FmtFunc(_SC("SqIsRoutineWithTag"), &Routine::IsWithTag);
RootTable(vm).FmtFunc(_SC("SqFetchRoutineWithTag"), &Routine::FetchWithTag);
RootTable(vm).FmtFunc(_SC("SqTerminateRoutineWithTag"), &Routine::TerminateWithTag); RootTable(vm).FmtFunc(_SC("SqTerminateRoutineWithTag"), &Routine::TerminateWithTag);
#ifdef VCMP_ENABLE_OFFICIAL #ifdef VCMP_ENABLE_OFFICIAL
RootTable(vm).SquirrelFunc(_SC("NewTimer"), &Routine::CreateOfficial); RootTable(vm).SquirrelFunc(_SC("NewTimer"), &Routine::CreateOfficial);

View File

@ -314,16 +314,37 @@ public:
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the number of used routine slots. * Locate a routine with a specific name.
*/ */
static const LightObj & FindByTag(StackStrF & tag) static LightObj FindByTag(StackStrF & tag)
{ {
// Is the specified tag valid? // Is the specified tag valid?
if (!tag.mPtr) if (tag.mPtr != nullptr && tag.mLen > 0)
{
// Iterate routine list and look for it
for (const auto & r : s_Instances)
{
if (!r.mInst.IsNull() && r.mTag == tag.mPtr)
{
return r.mInst; // Return this routine instance
}
}
}
// Unable to find such routine
return LightObj{};
}
/* --------------------------------------------------------------------------------------------
* Retrieve a routine with a specific name.
*/
static const LightObj & FetchWithTag(StackStrF & tag)
{
// Is the specified tag valid?
if (tag.mPtr == nullptr || tag.mLen <= 0)
{ {
STHROWF("Invalid routine tag"); STHROWF("Invalid routine tag");
} }
// Iterate routine list // Iterate routine list and look for it
for (const auto & r : s_Instances) for (const auto & r : s_Instances)
{ {
if (!r.mInst.IsNull() && r.mTag == tag.mPtr) if (!r.mInst.IsNull() && r.mTag == tag.mPtr)
@ -332,26 +353,29 @@ public:
} }
} }
// Unable to find such routine // Unable to find such routine
STHROWF("Unable to find a routine with tag ({})", tag.mPtr); STHROWF("Unable to fetch a routine with tag ({}). No such routine", tag.mPtr);
SQ_UNREACHABLE; SQ_UNREACHABLE;
// Should not reach this point but if it did, we have to return something // Should not reach this point but if it did, we have to return something
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warray-bounds" #pragma clang diagnostic ignored "-Warray-bounds"
#endif #endif
return s_Instances[SQMOD_MAX_ROUTINES].mInst; // Intentional Buffer overflow! return s_Instances[SQMOD_MAX_ROUTINES].mInst; // Intentional Buffer overflow!
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic pop #pragma clang diagnostic pop
#endif #endif
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Check if a routine with a certain tag exists. * Check if a routine with a certain tag exists.
*/ */
static bool IsWithTag(StackStrF & tag); static bool IsWithTag(StackStrF & tag);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Check if a routine with a certain tag exists. * Check if a routine with a certain tag exists.
*/ */
static bool TerminateWithTag(StackStrF & tag); static bool TerminateWithTag(StackStrF & tag);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Process all active routines and update elapsed time. * Process all active routines and update elapsed time.
*/ */