1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-16 15:17:13 +02: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

@ -319,7 +319,7 @@ public:
sq_get(vm, -2);
#endif
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);
return ret;
}

View File

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

View File

@ -485,7 +485,7 @@ public:
sq_get(vm, -2);
#endif
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);
return ret;
}
@ -516,7 +516,7 @@ public:
sq_get(vm, -2);
#endif
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);
return ret;
}