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

Refactor.

This commit is contained in:
Sandu Liviu Catalin 2020-04-27 13:53:16 +03:00
parent 22a17fe3c4
commit b86c4cea2b
9 changed files with 60 additions and 169 deletions

View File

@ -661,7 +661,7 @@ Int32 SQLiteConnHnd::Flush(Uint32 num, Object & env, Function & func)
num = mQueue.size(); num = mQueue.size();
} }
// Generate the function that should be called upon error // Generate the function that should be called upon error
Function callback = Function(env.GetVM(), env.GetObj(), func.GetFunc()); Function callback = Function(env.GetObj(), func.GetFunc(), env.GetVM());
// Obtain iterators to the range of queries that should be flushed // Obtain iterators to the range of queries that should be flushed
auto itr = mQueue.begin(); auto itr = mQueue.begin();
auto end = mQueue.begin() + num; auto end = mQueue.begin() + num;

View File

@ -280,7 +280,7 @@ void Logger::SetLogFilename(CCStr filename)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Logger::BindCb(Uint8 level, Object & env, Function & func) void Logger::BindCb(Uint8 level, Function & func)
{ {
// Get the index of this log level // Get the index of this log level
const Uint8 idx = GetLevelIdx(level); const Uint8 idx = GetLevelIdx(level);
@ -291,24 +291,8 @@ void Logger::BindCb(Uint8 level, Object & env, Function & func)
} }
// Obtain the function instance called for this log level // Obtain the function instance called for this log level
Function & cb = m_LogCb[idx]; Function & cb = m_LogCb[idx];
// Is the specified callback function null?
if (func.IsNull())
{
cb.Release(); // Then release the current callback
}
// Does this function need a custom environment?
else if (env.IsNull())
{
// Use the root table instead
RootTable root(SqVM());
// Bind the root table with the function
cb = Function(env.GetVM(), root, func.GetFunc());
}
// Assign the specified environment and function // Assign the specified environment and function
else cb = std::move(func);
{
cb = Function(env.GetVM(), env, func.GetFunc());
}
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -753,9 +737,9 @@ template < Uint8 L, bool S > static SQInteger LogBasicMessage(HSQUIRRELVM vm)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
template < Uint8 L > static void BindLogCallback(Object & env, Function & func) template < Uint8 L > static void BindLogCallback(Function & func)
{ {
Logger::Get().BindCb(L, env, func); Logger::Get().BindCb(L, func);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -293,7 +293,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Bind a script callback to a log level. * Bind a script callback to a log level.
*/ */
void BindCb(Uint8 level, Object & env, Function & func); void BindCb(Uint8 level, Function & func);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Send a log message. * Send a log message.

View File

@ -370,18 +370,13 @@ Vector2i AreaManager::LocateCell(float x, float y)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static void Areas_TestPointEx(Object & env, Function & func, float x, float y) static void Areas_TestPointEx(float x, float y, Function & func)
{ {
// Is the function valid? // Is the function valid?
if (func.IsNull()) if (func.IsNull())
{ {
STHROWF("Invalid callback object"); STHROWF("Invalid callback object");
} }
// Should we use a custom environment?
else if (!env.IsNull())
{
func = Function(env.GetVM(), env, func.GetFunc());
}
// Begin testing // Begin testing
AreaManager::Get().TestPoint([&func](AreaCell::Areas::reference ap) -> void { AreaManager::Get().TestPoint([&func](AreaCell::Areas::reference ap) -> void {
func.Execute(ap.second); func.Execute(ap.second);
@ -389,24 +384,14 @@ static void Areas_TestPointEx(Object & env, Function & func, float x, float y)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static void Areas_TestPoint(Object & env, Function & func, const Vector2 & v) static void Areas_TestPoint(const Vector2 & v, Function & func)
{ {
Areas_TestPointEx(env, func, v.x, v.y); Areas_TestPointEx(v.x, v.y, func);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static void Areas_TestPointOnEx(Object & ctx, Object & env, Function & func, float x, float y) static void Areas_TestPointOnEx(float x, float y, Object & ctx, Function & func)
{ {
// Is the function valid?
if (func.IsNull())
{
STHROWF("Invalid callback object");
}
// Should we use a custom environment?
else if (!env.IsNull())
{
func = Function(env.GetVM(), env, func.GetFunc());
}
// Begin testing // Begin testing
AreaManager::Get().TestPoint([&ctx, &func](AreaCell::Areas::reference ap) -> void { AreaManager::Get().TestPoint([&ctx, &func](AreaCell::Areas::reference ap) -> void {
func.Execute(ctx, ap.second); func.Execute(ctx, ap.second);
@ -414,9 +399,9 @@ static void Areas_TestPointOnEx(Object & ctx, Object & env, Function & func, flo
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static void Areas_TestPointOn(Object & ctx, Object & env, Function & func, const Vector2 & v) static void Areas_TestPointOn(const Vector2 & v, Object & ctx, Function & func)
{ {
Areas_TestPointOnEx(ctx, env, func, v.x, v.y); Areas_TestPointOnEx(v.x, v.y, ctx, func);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -1048,11 +1048,11 @@ void Register(HSQUIRRELVM vm)
.Func(_SC("Clear"), &Manager::Clear) .Func(_SC("Clear"), &Manager::Clear)
.Func(_SC("Attach"), &Manager::Attach) .Func(_SC("Attach"), &Manager::Attach)
.FmtFunc(_SC("FindByName"), &Manager::FindByName) .FmtFunc(_SC("FindByName"), &Manager::FindByName)
.Func(_SC("BindFail"), &Manager::SetOnFail) .CbFunc(_SC("BindFail"), &Manager::SetOnFail)
.Func(_SC("BindAuth"), &Manager::SetOnAuth) .CbFunc(_SC("BindAuth"), &Manager::SetOnAuth)
.Func(_SC("GetArray"), &Manager::GetCommandsArray) .Func(_SC("GetArray"), &Manager::GetCommandsArray)
.Func(_SC("GetTable"), &Manager::GetCommandsTable) .Func(_SC("GetTable"), &Manager::GetCommandsTable)
.Func(_SC("Foreach"), &Manager::ForeachCommand) .CbFunc(_SC("Foreach"), &Manager::ForeachCommand)
// Member Overloads // Member Overloads
.Overload(_SC("Create"), &Manager::Create1) .Overload(_SC("Create"), &Manager::Create1)
.Overload(_SC("Create"), &Manager::Create2) .Overload(_SC("Create"), &Manager::Create2)
@ -1107,10 +1107,10 @@ void Register(HSQUIRRELVM vm)
.FmtFunc(_SC("SetSpec"), &Listener::SetSpec) .FmtFunc(_SC("SetSpec"), &Listener::SetSpec)
.FmtFunc(_SC("SetHelp"), &Listener::SetHelp) .FmtFunc(_SC("SetHelp"), &Listener::SetHelp)
.FmtFunc(_SC("SetInfo"), &Listener::SetInfo) .FmtFunc(_SC("SetInfo"), &Listener::SetInfo)
.Func(_SC("BindExec"), &Listener::SetOnExec) .CbFunc(_SC("BindExec"), &Listener::SetOnExec)
.Func(_SC("BindAuth"), &Listener::SetOnAuth) .CbFunc(_SC("BindAuth"), &Listener::SetOnAuth)
.Func(_SC("BindPost"), &Listener::SetOnPost) .CbFunc(_SC("BindPost"), &Listener::SetOnPost)
.Func(_SC("BindFail"), &Listener::SetOnFail) .CbFunc(_SC("BindFail"), &Listener::SetOnFail)
.Func(_SC("GetArgTag"), &Listener::GetArgTag) .Func(_SC("GetArgTag"), &Listener::GetArgTag)
.FmtFunc(_SC("SetArgTag"), &Listener::SetArgTag) .FmtFunc(_SC("SetArgTag"), &Listener::SetArgTag)
.Func(_SC("GetArgFlags"), &Listener::GetArgFlags) .Func(_SC("GetArgFlags"), &Listener::GetArgFlags)

View File

@ -626,22 +626,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the error callback. * Modify the error callback.
*/ */
void SetOnFail(Object & env, Function & func) void SetOnFail(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnFail = std::move(func);
if (func.IsNull())
{
m_OnFail.Release();
}
// Was there a custom environment specified?
else if (env.IsNull())
{
m_OnFail = func;
}
else
{
m_OnFail = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -655,22 +642,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the authentication callback. * Modify the authentication callback.
*/ */
void SetOnAuth(Object & env, Function & func) void SetOnAuth(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnAuth = std::move(func);
if (func.IsNull())
{
m_OnAuth.Release();
}
// Was there a custom environment specified?
if (env.IsNull())
{
m_OnAuth = func;
}
else
{
m_OnAuth = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -971,9 +945,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the error callback. * Modify the error callback.
*/ */
void SetOnFail(Object & env, Function & func) void SetOnFail(Function & func)
{ {
GetValid()->SetOnFail(env, func); GetValid()->SetOnFail(func);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -987,9 +961,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the authentication callback. * Modify the authentication callback.
*/ */
void SetOnAuth(Object & env, Function & func) void SetOnAuth(Function & func)
{ {
GetValid()->SetOnAuth(env, func); GetValid()->SetOnAuth(func);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1043,17 +1017,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Process all command listeners with a function. * Process all command listeners with a function.
*/ */
void ForeachCommand(Object & env, Function & func) const void ForeachCommand(Function & func) const
{ {
if (env.IsNull()) GetValid()->ForeachCommand(func);
{
GetValid()->ForeachCommand(func);
}
else
{
Function fn(env.GetVM(), env, func.GetFunc());
GetValid()->ForeachCommand(fn);
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1747,22 +1713,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the function that must be called when this command listener is executed. * Modify the function that must be called when this command listener is executed.
*/ */
void SetOnExec(Object & env, Function & func) void SetOnExec(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnExec = std::move(func);
if (func.IsNull())
{
m_OnExec.Release();
}
// Was there a custom environment specified?
else if (env.IsNull())
{
m_OnExec = func;
}
else
{
m_OnExec = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1776,22 +1729,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the function that must be called when this command listener needs to authenticate. * Modify the function that must be called when this command listener needs to authenticate.
*/ */
void SetOnAuth(Object & env, Function & func) void SetOnAuth(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnAuth = std::move(func);
if (func.IsNull())
{
m_OnAuth.Release();
}
// Was there a custom environment specified?
else if (env.IsNull())
{
m_OnAuth = func;
}
else
{
m_OnAuth = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1805,22 +1745,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the function that must be called when this command listener finished execution. * Modify the function that must be called when this command listener finished execution.
*/ */
void SetOnPost(Object & env, Function & func) void SetOnPost(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnPost = std::move(func);
if (func.IsNull())
{
m_OnPost.Release();
}
// Was there a custom environment specified?
else if (env.IsNull())
{
m_OnPost = func;
}
else
{
m_OnPost = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1834,22 +1761,9 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the function that must be called when this command listener failed execution. * Modify the function that must be called when this command listener failed execution.
*/ */
void SetOnFail(Object & env, Function & func) void SetOnFail(Function & func)
{ {
// Are we supposed to unbind current callback? m_OnFail = std::move(func);
if (func.IsNull())
{
m_OnFail.Release();
}
// Was there a custom environment specified?
else if (env.IsNull())
{
m_OnFail = func;
}
else
{
m_OnFail = Function(env.GetVM(), env.GetObj(), func.GetFunc());
}
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------

View File

@ -319,7 +319,7 @@ public:
sq_get(vm, -2); sq_get(vm, -2);
#endif #endif
sq_getstackobj(vm, -1, &funcObj); sq_getstackobj(vm, -1, &funcObj);
Function ret(vm, GetObj(), funcObj); // must addref before the pop! Function ret(GetObj(), funcObj, vm); // must addref before the pop!
sq_pop(vm, 2); sq_pop(vm, 2);
return ret; return ret;
} }

View File

@ -61,8 +61,8 @@ struct Function {
} }
// Move constructor // Move constructor
Function(Function&& sf) noexcept : mEnv(sf.mEnv), mObj(sf.mObj) { Function(Function&& sf) noexcept : mEnv(sf.mEnv), mObj(sf.mObj) {
sq_resetobject(&sf.GetEnv()); sq_resetobject(&sf.mEnv);
sq_resetobject(&sf.GetFunc()); sq_resetobject(&sf.mObj);
} }
// Constructs a Function from a slot in an Object // Constructs a Function from a slot in an Object
Function(const Object& e, const SQChar* slot) : mEnv(e.GetObj()) { Function(const Object& e, const SQChar* slot) : mEnv(e.GetObj()) {
@ -101,7 +101,7 @@ struct Function {
Assign(vm, idx1, idx2); Assign(vm, idx1, idx2);
} }
// Constructs a Function from two Squirrel objects (one is the environment object and the other is the function object) // Constructs a Function from two Squirrel objects (one is the environment object and the other is the function object)
Function(HSQUIRRELVM vm, HSQOBJECT e, HSQOBJECT o) : mEnv(e), mObj(o) { Function(HSQOBJECT e, HSQOBJECT o, HSQUIRRELVM vm) : mEnv(e), mObj(o) {
sq_addref(vm, &mEnv); sq_addref(vm, &mEnv);
sq_addref(vm, &mObj); sq_addref(vm, &mObj);
} }
@ -114,19 +114,19 @@ struct Function {
Release(); Release();
mEnv = sf.mEnv; mEnv = sf.mEnv;
mObj = sf.mObj; mObj = sf.mObj;
if (!sf.IsNull()) { sq_addref(SqVM(), &mEnv);
sq_addref(SqVM(), &mEnv); sq_addref(SqVM(), &mObj);
sq_addref(SqVM(), &mObj);
}
return *this; return *this;
} }
// Move Assignment operator // Move Assignment operator
Function& operator=(Function&& sf) noexcept { Function& operator=(Function&& sf) noexcept {
Release(); if (this != &sf) {
mEnv = sf.mEnv; Release();
mObj = sf.mObj; mEnv = sf.mEnv;
sq_resetobject(&sf.GetEnv()); mObj = sf.mObj;
sq_resetobject(&sf.GetFunc()); sq_resetobject(&sf.mEnv);
sq_resetobject(&sf.mObj);
}
return *this; return *this;
} }
// Checks whether the Function is null // Checks whether the Function is null
@ -172,6 +172,7 @@ struct Function {
bool Assign(HSQUIRRELVM vm, SQInteger idx1, SQInteger idx2, bool bind_null=false) { bool Assign(HSQUIRRELVM vm, SQInteger idx1, SQInteger idx2, bool bind_null=false) {
// Release current callback, if any // Release current callback, if any
Release(); Release();
printf("assigning ty1 %lld ty2 %lld top %lld\n", idx1, idx2, sq_gettop(vm));
// Tells if the current environment was used // Tells if the current environment was used
bool cenv = false; bool cenv = false;
// Retrieve the environment type // Retrieve the environment type
@ -181,14 +182,17 @@ struct Function {
sq_pushroottable(vm); // Push it on the stack sq_pushroottable(vm); // Push it on the stack
sq_getstackobj(vm, -1, &mEnv); // Grab it sq_getstackobj(vm, -1, &mEnv); // Grab it
sq_poptop(vm); // Clean up the stack sq_poptop(vm); // Clean up the stack
printf("assigning ty1 null\n");
// Should we use current environment? // Should we use current environment?
} else if (ty1 & (_RT_CLOSURE | _RT_NATIVECLOSURE)) { } else if (ty1 & (_RT_CLOSURE | _RT_NATIVECLOSURE)) {
sq_getstackobj(vm, 1, &mEnv); // `this` as environment sq_getstackobj(vm, 1, &mEnv); // `this` as environment
idx2 = idx1; // There is no explicit environment given idx2 = idx1; // There is no explicit environment given
cenv = true; cenv = true;
printf("assigning ty1 closure\n");
// Is there a specific environment? // Is there a specific environment?
} else if (ty1 & (_RT_TABLE | _RT_CLASS | _RT_INSTANCE)) { } else if (ty1 & (_RT_TABLE | _RT_CLASS | _RT_INSTANCE)) {
sq_getstackobj(vm, idx1, &mEnv); // Grab the given environment sq_getstackobj(vm, idx1, &mEnv); // Grab the given environment
printf("assigning ty1 table\n");
} else { } else {
#if !defined (SCRAT_NO_ERROR_CHECKING) #if !defined (SCRAT_NO_ERROR_CHECKING)
SQTHROW(vm, FormatTypeError(vm, idx1, _SC("object"))); SQTHROW(vm, FormatTypeError(vm, idx1, _SC("object")));
@ -201,19 +205,23 @@ struct Function {
// Can we bind null? // Can we bind null?
if (bind_null && ty2 == OT_NULL) { if (bind_null && ty2 == OT_NULL) {
sq_resetobject(&mEnv); // Drop the environment, if any sq_resetobject(&mEnv); // Drop the environment, if any
printf("assigning ty2 null\n");
return cenv; // Just stop knowing this is what we want return cenv; // Just stop knowing this is what we want
// Is the callback is valid? // Is the callback is valid?
} else if (ty2 & (_RT_CLOSURE | _RT_NATIVECLOSURE)) { } else if (ty2 & (_RT_CLOSURE | _RT_NATIVECLOSURE)) {
printf("assigning ty2 closure\n");
sq_getstackobj(vm, idx2, &mObj); sq_getstackobj(vm, idx2, &mObj);
// Reference the environment and function // Reference the environment and function
sq_addref(vm, &mEnv); sq_addref(vm, &mEnv);
sq_addref(vm, &mObj); sq_addref(vm, &mObj);
} else { } else {
printf("assigning ty2 null\n");
sq_resetobject(&mEnv); // Discard obtained environment sq_resetobject(&mEnv); // Discard obtained environment
#if !defined (SCRAT_NO_ERROR_CHECKING) #if !defined (SCRAT_NO_ERROR_CHECKING)
SQTHROW(vm, FormatTypeError(vm, idx2, _SC("closure"))); SQTHROW(vm, FormatTypeError(vm, idx2, _SC("closure")));
#endif #endif
} }
puts("");
// Return whether current environment was used // Return whether current environment was used
return cenv; return cenv;
} }

View File

@ -485,7 +485,7 @@ public:
sq_get(vm, -2); sq_get(vm, -2);
#endif #endif
sq_getstackobj(vm, -1, &funcObj); sq_getstackobj(vm, -1, &funcObj);
Function ret(vm, GetObj(), funcObj); // must addref before the pop! Function ret(GetObj(), funcObj, vm); // must addref before the pop!
sq_pop(vm, 2); sq_pop(vm, 2);
return ret; return ret;
} }
@ -516,7 +516,7 @@ public:
sq_get(vm, -2); sq_get(vm, -2);
#endif #endif
sq_getstackobj(vm, -1, &funcObj); sq_getstackobj(vm, -1, &funcObj);
Function ret(vm, GetObj(), funcObj); // must addref before the pop! Function ret(GetObj(), funcObj, vm); // must addref before the pop!
sq_pop(vm, 2); sq_pop(vm, 2);
return ret; return ret;
} }