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

Rename the lightweight object type.

Few adjustments to the tasks to overcome the limitation of removing themselves. Currently broken because they require a special Sqrat allocator.
This commit is contained in:
Sandu Liviu Catalin 2016-11-17 11:27:07 +02:00
parent 15c824ddae
commit 641e51e03c
5 changed files with 108 additions and 80 deletions

View File

@ -651,10 +651,10 @@ struct Var<const Object&> : Var<Object> {Var(HSQUIRRELVM vm, SQInteger idx) : Va
/// A lightweight wrapper arround the Squirrel objects that implements RAII and still remains a POD type.
///
/// \remarks
/// All LightObject and derived classes MUST be destroyed before calling sq_close or your application will crash when exiting.
/// All LightObj and derived classes MUST be destroyed before calling sq_close or your application will crash when exiting.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct LightObject {
struct LightObj {
HSQOBJECT mObj;
@ -662,48 +662,48 @@ struct LightObject {
/// Default constructor (null)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject() {
LightObj() {
sq_resetobject(&mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Copy constructor
///
/// \param so LightObject to copy
/// \param so LightObj to copy
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject(const LightObject& so) : mObj(so.mObj) {
LightObj(const LightObj& so) : mObj(so.mObj) {
sq_addref(DefaultVM::Get(), &mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move constructor
///
/// \param so LightObject to move
/// \param so LightObj to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject(LightObject&& so) : mObj(so.mObj) {
LightObj(LightObj&& so) : mObj(so.mObj) {
sq_resetobject(&so.mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObject from a Squirrel object
/// Constructs a LightObj from a Squirrel object
///
/// \param o Squirrel object
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject(HSQOBJECT o) : mObj(o) {
LightObj(HSQOBJECT o) : mObj(o) {
sq_addref(DefaultVM::Get(), &mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObject from a Squirrel object at a certain index on the stack
/// Constructs a LightObj from a Squirrel object at a certain index on the stack
///
/// \param i Index of the Squirrel object on stack
/// \param v VM that the object will exist in
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject(SQInteger i, HSQUIRRELVM v = DefaultVM::Get()) {
LightObj(SQInteger i, HSQUIRRELVM v = DefaultVM::Get()) {
if (SQ_FAILED(sq_getstackobj(v, i, &mObj))) {
sq_resetobject(&mObj);
} else {
@ -712,19 +712,19 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Construct a LightObject from a regular Object instance.
/// Construct a LightObj from a regular Object instance.
///
/// \param so Object to copy
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject(const Object& obj) : mObj(obj.GetObject()) {
LightObj(const Object& obj) : mObj(obj.GetObject()) {
if (!sq_isnull(mObj)) {
sq_addref(obj.GetVM(), &mObj);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs an LightObject from a C++ instance
/// Constructs an LightObj from a C++ instance
///
/// \param instance Pointer to a C++ class instance that has been bound already
/// \param v VM that the object will exist in
@ -733,7 +733,7 @@ struct LightObject {
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
LightObject(T* instance, HSQUIRRELVM v = DefaultVM::Get()) {
LightObj(T* instance, HSQUIRRELVM v = DefaultVM::Get()) {
// Preserve the stack state
const StackGuard sg(v);
// Push the instance on the stack
@ -750,19 +750,19 @@ struct LightObject {
/// Destructor
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~LightObject() {
~LightObj() {
Release();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param so LightObject to copy
/// \param so LightObj to copy
///
/// \return The LightObject itself
/// \return The LightObj itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject& operator=(const LightObject& so) {
LightObj& operator=(const LightObj& so) {
if (this != &so) {
Release();
mObj = so.mObj;
@ -774,12 +774,12 @@ struct LightObject {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param so LightObject to move
/// \param so LightObj to move
///
/// \return The LightObject itself
/// \return The LightObj itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LightObject& operator=(LightObject&& so) {
LightObj& operator=(LightObj&& so) {
if (this != &so) {
Release();
mObj = so.mObj;
@ -789,9 +789,9 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the type of the LightObject as defined by the Squirrel API
/// Gets the type of the LightObj as defined by the Squirrel API
///
/// \return SQObjectType for the LightObject
/// \return SQObjectType for the LightObj
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQObjectType GetType() const {
@ -799,9 +799,9 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Checks whether the LightObject is null
/// Checks whether the LightObj is null
///
/// \return True if the LightObject currently has a null value, otherwise false
/// \return True if the LightObj currently has a null value, otherwise false
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool IsNull() const {
@ -809,7 +809,7 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the Squirrel object for this LightObject (copy)
/// Gets the Squirrel object for this LightObj (copy)
///
/// \return Squirrel object
///
@ -819,7 +819,7 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the Squirrel object for this LightObject (reference)
/// Gets the Squirrel object for this LightObj (reference)
///
/// \return Squirrel object
///
@ -829,7 +829,7 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Allows the LightObject to be inputted directly into places that expect a HSQOBJECT
/// Allows the LightObj to be inputted directly into places that expect a HSQOBJECT
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
operator HSQOBJECT&() {
@ -837,7 +837,7 @@ struct LightObject {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Sets the LightObject to null (removing its references to underlying Squirrel objects)
/// Sets the LightObj to null (removing its references to underlying Squirrel objects)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Release() {
@ -850,15 +850,15 @@ struct LightObject {
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push LightObject instances to and from the stack as references (LightObject is always a reference)
/// Used to get and push LightObj instances to and from the stack as references (LightObj is always a reference)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<>
struct Var<LightObject> {
struct Var<LightObj> {
LightObject value; ///< The actual value of get operations
LightObj value; ///< The actual value of get operations
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Attempts to get the value off the stack at idx as an LightObject
/// Attempts to get the value off the stack at idx as an LightObj
///
/// \param vm Target VM
/// \param idx Index trying to be read
@ -871,28 +871,28 @@ struct Var<LightObject> {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Called by Sqrat::PushVar to put an LightObject on the stack
/// Called by Sqrat::PushVar to put an LightObj on the stack
///
/// \param vm Target VM
/// \param value Value to push on to the VM's stack
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const LightObject& value) {
static void push(HSQUIRRELVM vm, const LightObj& value) {
sq_pushobject(vm, value.mObj);
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push LightObject instances to and from the stack as references (LightObject is always a reference)
/// Used to get and push LightObj instances to and from the stack as references (LightObj is always a reference)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<>
struct Var<LightObject&> : Var<LightObject> {Var(HSQUIRRELVM vm, SQInteger idx) : Var<LightObject>(vm, idx) {}};
struct Var<LightObj&> : Var<LightObj> {Var(HSQUIRRELVM vm, SQInteger idx) : Var<LightObj>(vm, idx) {}};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push LightObject instances to and from the stack as references (LightObject is always a reference)
/// Used to get and push LightObj instances to and from the stack as references (LightObj is always a reference)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<>
struct Var<const LightObject&> : Var<LightObject> {Var(HSQUIRRELVM vm, SQInteger idx) : Var<LightObject>(vm, idx) {}};
struct Var<const LightObj&> : Var<LightObj> {Var(HSQUIRRELVM vm, SQInteger idx) : Var<LightObj>(vm, idx) {}};
}

View File

@ -49,6 +49,7 @@ extern void Register_Log(HSQUIRRELVM vm);
extern void Register_Core(HSQUIRRELVM vm);
extern void Register_Command(HSQUIRRELVM vm);
extern void Register_Routine(HSQUIRRELVM vm);
extern void RegisterTask(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Misc(HSQUIRRELVM vm);
@ -91,6 +92,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_Core(vm);
Register_Command(vm);
Register_Routine(vm);
RegisterTask(vm);
Register_Misc(vm);
Register_Signal(vm);

View File

@ -140,8 +140,8 @@ namespace Sqrat {
class Object;
class Table;
class Function;
class LightObj;
class StackStrF;
class LightObject;
} // Namespace:: Sqrat
/* ------------------------------------------------------------------------------------------------

View File

@ -14,6 +14,9 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("SqTask"))
// ------------------------------------------------------------------------------------------------
Uint32 Tasks::s_Used = 0;
Tasks::Time Tasks::s_Last = 0;
@ -22,13 +25,13 @@ Tasks::Interval Tasks::s_Intervals[SQMOD_MAX_TASKS];
Tasks::Task Tasks::s_Tasks[SQMOD_MAX_TASKS];
// ------------------------------------------------------------------------------------------------
void Tasks::Task::Init(HSQOBJECT & env, HSQOBJECT & func, Interval intrv, Iterator itr, Int32 id, Int32 type)
void Tasks::Task::Init(HSQOBJECT & func, HSQOBJECT & inst, Interval intrv, Iterator itr, Int32 id, Int32 type)
{
// Initialize the callback hash
mHash = 0;
// Initialize the callback objects
mEnv = env;
mFunc = func;
mFunc = LightObj(func);
mInst = LightObj(inst);
// Initialize the task options
mIterations = itr;
mInterval = intrv;
@ -37,18 +40,11 @@ void Tasks::Task::Init(HSQOBJECT & env, HSQOBJECT & func, Interval intrv, Iterat
mType = ConvTo< Uint8 >::From(type);
// Grab the virtual machine once
HSQUIRRELVM vm = DefaultVM::Get();
// Is there a valid environment?
if (!sq_isnull(mEnv))
{
sq_addref(vm, &mEnv); // Keep a reference to this environment
}
// Remember the current stack size
const StackGuard sg(vm);
// Is there a valid function?
if (!sq_isnull(mFunc))
if (!mFunc.IsNull())
{
// Keep a reference to this function
sq_addref(vm, &mFunc);
// Push the callback on the stack
sq_pushobject(vm, mFunc);
// Grab the hash of the callback object
@ -59,20 +55,9 @@ void Tasks::Task::Init(HSQOBJECT & env, HSQOBJECT & func, Interval intrv, Iterat
// ------------------------------------------------------------------------------------------------
void Tasks::Task::Release()
{
// Should we release any environment object?
if (!sq_isnull(mEnv))
{
sq_release(DefaultVM::Get(), &mEnv);
sq_resetobject(&mEnv);
}
// Should we release any callback object?
if (!sq_isnull(mFunc))
{
sq_release(DefaultVM::Get(), &mFunc);
sq_resetobject(&mFunc);
}
// Reset member variables as well
mHash = 0;
mFunc.Release();
mInst.Release();
mIterations = 0;
mInterval = 0;
mEntity = -1;
@ -92,7 +77,7 @@ Tasks::Interval Tasks::Task::Execute()
// Push the function on the stack
sq_pushobject(vm, mFunc);
// Push the environment on the stack
sq_pushobject(vm, mEnv);
sq_pushobject(vm, mSelf);
// Push function parameters, if any
for (Uint32 n = 0; n < mArgc; ++n)
{
@ -156,6 +141,23 @@ void Tasks::Process()
void Tasks::Initialize()
{
std::memset(s_Intervals, 0, sizeof(s_Intervals));
// Transform all task instances to script objects
for (auto & t : s_Tasks)
{
// This is fine because they'll always outlive the virtual machine
t.mSelf = LightObj(&t);
}
}
// ------------------------------------------------------------------------------------------------
void Tasks::Register(HSQUIRRELVM vm)
{
RootTable(vm).Bind(Typename::Str,
Class< Task, NoConstructor< Task > >(vm, Typename::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
//.Func(_SC("_tostring"), &CBlip::ToString)
);
}
// ------------------------------------------------------------------------------------------------
@ -166,6 +168,7 @@ void Tasks::Deinitialize()
{
t.Release();
t.Clear();
t.mSelf.Release();
}
}
@ -219,17 +222,26 @@ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm)
{
return sq_throwerror(vm, "Missing task callback");
}
SQRESULT res = SQ_OK;
// Prepare some objects for the environment and callback
HSQOBJECT env = FindEntity(id, type).GetObject(), func;
// Validate the callback type
if (sq_gettype(vm, 2) != OT_CLOSURE && sq_gettype(vm, 2) != OT_NATIVECLOSURE)
else if (sq_gettype(vm, 2) != OT_CLOSURE && sq_gettype(vm, 2) != OT_NATIVECLOSURE)
{
return sq_throwerror(vm, "Invalid callback type");
}
// Prepare an entity instance object
HSQOBJECT inst;
// Attempt to retrieve the entity instance
try
{
inst = FindEntity(id, type).GetObject();
}
catch (const std::exception & e)
{
return sq_throwerror(vm, e.what());
}
// Prepare the function object
HSQOBJECT func;
// Fetch the specified callback
res = sq_getstackobj(vm, 2, &func);
SQRESULT res = sq_getstackobj(vm, 2, &func);
// Validate the result
if (SQ_FAILED(res))
{
@ -297,7 +309,7 @@ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm)
}
}
// Alright, at this point we can initialize the slot
task.Init(env, func, intrv, itr, id, type);
task.Init(func, inst, intrv, itr, id, type);
// Now initialize the interval
s_Intervals[slot] = intrv;
// Increase the number of used slots
@ -462,6 +474,14 @@ void InitializeTasks()
Tasks::Initialize();
}
/* ------------------------------------------------------------------------------------------------
* Forward the call to register tasks.
*/
void RegisterTask(HSQUIRRELVM vm)
{
Tasks::Register(vm);
}
/* ------------------------------------------------------------------------------------------------
* Forward the call to terminate tasks.
*/

View File

@ -20,7 +20,7 @@ public:
typedef Int64 Time;
typedef SQInteger Interval;
typedef Uint32 Iterator;
typedef LightObject Argument;
typedef LightObj Argument;
private:
@ -31,8 +31,9 @@ private:
{
// ----------------------------------------------------------------------------------------
SQHash mHash; // The hash of the referenced function object.
HSQOBJECT mEnv; // A reference to the managed environment object.
HSQOBJECT mFunc; // A reference to the managed function object.
LightObj mSelf; // A reference to `this`as a script object.
LightObj mFunc; // A reference to the managed function object.
LightObj mInst; // A reference to the associated entity object.
Iterator mIterations; // Number of iterations before self destruct.
Interval mInterval; // Interval between task invocations.
Int16 mEntity; // The identifier of the entity to which is belongs.
@ -45,8 +46,9 @@ private:
*/
Task()
: mHash(0)
, mEnv()
, mSelf()
, mFunc()
, mInst()
, mIterations(0)
, mInterval(0)
, mEntity(-1)
@ -54,8 +56,7 @@ private:
, mArgc(0)
, mArgv()
{
sq_resetobject(&mEnv);
sq_resetobject(&mFunc);
/* ... */
}
/* ----------------------------------------------------------------------------------------
@ -90,7 +91,7 @@ private:
/* ----------------------------------------------------------------------------------------
* Initializes the task parameters. (assumes previous values are already released)
*/
void Init(HSQOBJECT & env, HSQOBJECT & func, Interval intrv, Iterator itr, Int32 id, Int32 type);
void Init(HSQOBJECT & inst, HSQOBJECT & func, Interval intrv, Iterator itr, Int32 id, Int32 type);
/* ----------------------------------------------------------------------------------------
* Clear the arguments.
@ -166,6 +167,11 @@ public:
*/
static void Initialize();
/* --------------------------------------------------------------------------------------------
* Register the task class.
*/
static void Register(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Release all resources and prepare for shutdown.
*/