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:
parent
2344189c10
commit
2701b6487a
@ -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);
|
||||||
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user