[DEV] rework userData class
This commit is contained in:
parent
7e62732281
commit
73000df2bb
@ -53,7 +53,7 @@ def configure(target, my_module):
|
|||||||
'rabbit/rabbit.hpp',
|
'rabbit/rabbit.hpp',
|
||||||
'rabbit/sqobject.hpp',
|
'rabbit/sqobject.hpp',
|
||||||
'rabbit/sqopcodes.hpp',
|
'rabbit/sqopcodes.hpp',
|
||||||
'rabbit/squserdata.hpp',
|
'rabbit/UserData.hpp',
|
||||||
'rabbit/squtils.hpp',
|
'rabbit/squtils.hpp',
|
||||||
'rabbit/sqpcheader.hpp',
|
'rabbit/sqpcheader.hpp',
|
||||||
'rabbit/sqfuncproto.hpp',
|
'rabbit/sqfuncproto.hpp',
|
||||||
|
58
rabbit/UserData.hpp
Normal file
58
rabbit/UserData.hpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* @author Alberto DEMICHELIS
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2018, Edouard DUPIN, all right reserved
|
||||||
|
* @copyright 2003-2017, Alberto DEMICHELIS, all right reserved
|
||||||
|
* @license MPL-2 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace rabbit {
|
||||||
|
class UserData : public SQDelegable {
|
||||||
|
public:
|
||||||
|
UserData(SQSharedState *ss) {
|
||||||
|
_delegate = 0;
|
||||||
|
m_hook = NULL;
|
||||||
|
INIT_CHAIN();
|
||||||
|
ADD_TO_CHAIN(&_ss(this)->_gc_chain, this);
|
||||||
|
}
|
||||||
|
~UserData() {
|
||||||
|
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
|
||||||
|
SetDelegate(NULL);
|
||||||
|
}
|
||||||
|
static UserData* Create(SQSharedState *ss, SQInteger size) {
|
||||||
|
UserData* ud = (UserData*)SQ_MALLOC(sq_aligning(sizeof(UserData))+size);
|
||||||
|
new (ud) UserData(ss);
|
||||||
|
ud->m_size = size;
|
||||||
|
ud->m_typetag = 0;
|
||||||
|
return ud;
|
||||||
|
}
|
||||||
|
void Release() {
|
||||||
|
if (m_hook) {
|
||||||
|
m_hook((SQUserPointer)sq_aligning(this + 1),m_size);
|
||||||
|
}
|
||||||
|
SQInteger tsize = m_size;
|
||||||
|
this->~UserData();
|
||||||
|
SQ_FREE(this, sq_aligning(sizeof(UserData)) + tsize);
|
||||||
|
}
|
||||||
|
const SQInteger& getSize() const {
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
const SQUserPointer& getTypeTag() const {
|
||||||
|
return m_typetag;
|
||||||
|
}
|
||||||
|
void setTypeTag(const SQUserPointer& _value) {
|
||||||
|
m_typetag = _value;
|
||||||
|
}
|
||||||
|
const SQRELEASEHOOK& getHook() const {
|
||||||
|
return m_hook;
|
||||||
|
}
|
||||||
|
void setHook(const SQRELEASEHOOK& _value) {
|
||||||
|
m_hook = _value;
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
SQInteger m_size;
|
||||||
|
SQRELEASEHOOK m_hook;
|
||||||
|
SQUserPointer m_typetag;
|
||||||
|
};
|
||||||
|
}
|
@ -36,14 +36,16 @@ struct SQString;
|
|||||||
struct SQClosure;
|
struct SQClosure;
|
||||||
struct SQGenerator;
|
struct SQGenerator;
|
||||||
struct SQNativeClosure;
|
struct SQNativeClosure;
|
||||||
struct SQUserData;
|
|
||||||
struct SQFunctionProto;
|
struct SQFunctionProto;
|
||||||
struct SQRefCounted;
|
struct SQRefCounted;
|
||||||
struct SQClass;
|
struct SQClass;
|
||||||
struct SQInstance;
|
struct SQInstance;
|
||||||
struct SQDelegable;
|
struct SQDelegable;
|
||||||
struct SQOuter;
|
struct SQOuter;
|
||||||
|
namespace rabbit {
|
||||||
|
class UserData;
|
||||||
|
|
||||||
|
}
|
||||||
#ifdef _UNICODE
|
#ifdef _UNICODE
|
||||||
#define SQUNICODE
|
#define SQUNICODE
|
||||||
#endif
|
#endif
|
||||||
@ -124,7 +126,7 @@ typedef union tagSQObjectValue
|
|||||||
struct SQGenerator *pGenerator;
|
struct SQGenerator *pGenerator;
|
||||||
struct SQNativeClosure *pNativeClosure;
|
struct SQNativeClosure *pNativeClosure;
|
||||||
struct SQString *pString;
|
struct SQString *pString;
|
||||||
struct SQUserData *pUserData;
|
struct rabbit::UserData *pUserData;
|
||||||
SQInteger nInteger;
|
SQInteger nInteger;
|
||||||
SQFloat fFloat;
|
SQFloat fFloat;
|
||||||
SQUserPointer pUserPointer;
|
SQUserPointer pUserPointer;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
#include <rabbit/sqarray.hpp>
|
#include <rabbit/sqarray.hpp>
|
||||||
#include <rabbit/sqfuncproto.hpp>
|
#include <rabbit/sqfuncproto.hpp>
|
||||||
#include <rabbit/sqclosure.hpp>
|
#include <rabbit/sqclosure.hpp>
|
||||||
#include <rabbit/squserdata.hpp>
|
#include <rabbit/UserData.hpp>
|
||||||
#include <rabbit/sqcompiler.hpp>
|
#include <rabbit/sqcompiler.hpp>
|
||||||
#include <rabbit/sqfuncstate.hpp>
|
#include <rabbit/sqfuncstate.hpp>
|
||||||
#include <rabbit/sqclass.hpp>
|
#include <rabbit/sqclass.hpp>
|
||||||
@ -257,7 +257,7 @@ void sq_pushthread(HRABBITVM v, HRABBITVM thread)
|
|||||||
|
|
||||||
SQUserPointer sq_newuserdata(HRABBITVM v,SQUnsignedInteger size)
|
SQUserPointer sq_newuserdata(HRABBITVM v,SQUnsignedInteger size)
|
||||||
{
|
{
|
||||||
SQUserData *ud = SQUserData::Create(_ss(v), size + SQ_ALIGNMENT);
|
rabbit::UserData *ud = rabbit::UserData::Create(_ss(v), size + SQ_ALIGNMENT);
|
||||||
v->Push(ud);
|
v->Push(ud);
|
||||||
return (SQUserPointer)sq_aligning(ud + 1);
|
return (SQUserPointer)sq_aligning(ud + 1);
|
||||||
}
|
}
|
||||||
@ -720,7 +720,7 @@ SQInteger sq_getsize(HRABBITVM v, SQInteger idx)
|
|||||||
case OT_STRING: return _string(o)->_len;
|
case OT_STRING: return _string(o)->_len;
|
||||||
case OT_TABLE: return _table(o)->CountUsed();
|
case OT_TABLE: return _table(o)->CountUsed();
|
||||||
case OT_ARRAY: return _array(o)->Size();
|
case OT_ARRAY: return _array(o)->Size();
|
||||||
case OT_USERDATA: return _userdata(o)->_size;
|
case OT_USERDATA: return _userdata(o)->getSize();
|
||||||
case OT_INSTANCE: return _instance(o)->_class->_udsize;
|
case OT_INSTANCE: return _instance(o)->_class->_udsize;
|
||||||
case OT_CLASS: return _class(o)->_udsize;
|
case OT_CLASS: return _class(o)->_udsize;
|
||||||
default:
|
default:
|
||||||
@ -739,7 +739,9 @@ SQRESULT sq_getuserdata(HRABBITVM v,SQInteger idx,SQUserPointer *p,SQUserPointer
|
|||||||
SQObjectPtr *o = NULL;
|
SQObjectPtr *o = NULL;
|
||||||
_GETSAFE_OBJ(v, idx, OT_USERDATA,o);
|
_GETSAFE_OBJ(v, idx, OT_USERDATA,o);
|
||||||
(*p) = _userdataval(*o);
|
(*p) = _userdataval(*o);
|
||||||
if(typetag) *typetag = _userdata(*o)->_typetag;
|
if(typetag) {
|
||||||
|
*typetag = _userdata(*o)->getTypeTag();
|
||||||
|
}
|
||||||
return SQ_OK;
|
return SQ_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -747,9 +749,14 @@ SQRESULT sq_settypetag(HRABBITVM v,SQInteger idx,SQUserPointer typetag)
|
|||||||
{
|
{
|
||||||
SQObjectPtr &o = stack_get(v,idx);
|
SQObjectPtr &o = stack_get(v,idx);
|
||||||
switch(sq_type(o)) {
|
switch(sq_type(o)) {
|
||||||
case OT_USERDATA: _userdata(o)->_typetag = typetag; break;
|
case OT_USERDATA:
|
||||||
case OT_CLASS: _class(o)->_typetag = typetag; break;
|
_userdata(o)->setTypeTag(typetag);
|
||||||
default: return sq_throwerror(v,_SC("invalid object type"));
|
break;
|
||||||
|
case OT_CLASS:
|
||||||
|
_class(o)->_typetag = typetag;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return sq_throwerror(v,_SC("invalid object type"));
|
||||||
}
|
}
|
||||||
return SQ_OK;
|
return SQ_OK;
|
||||||
}
|
}
|
||||||
@ -758,7 +765,7 @@ SQRESULT sq_getobjtypetag(const HSQOBJECT *o,SQUserPointer * typetag)
|
|||||||
{
|
{
|
||||||
switch(sq_type(*o)) {
|
switch(sq_type(*o)) {
|
||||||
case OT_INSTANCE: *typetag = _instance(*o)->_class->_typetag; break;
|
case OT_INSTANCE: *typetag = _instance(*o)->_class->_typetag; break;
|
||||||
case OT_USERDATA: *typetag = _userdata(*o)->_typetag; break;
|
case OT_USERDATA: *typetag = _userdata(*o)->getTypeTag(); break;
|
||||||
case OT_CLASS: *typetag = _class(*o)->_typetag; break;
|
case OT_CLASS: *typetag = _class(*o)->_typetag; break;
|
||||||
default: return SQ_ERROR;
|
default: return SQ_ERROR;
|
||||||
}
|
}
|
||||||
@ -1226,8 +1233,9 @@ SQRESULT sq_wakeupvm(HRABBITVM v,SQBool wakeupret,SQBool retval,SQBool raiseerro
|
|||||||
if(!v->Execute(dummy,-1,-1,ret,raiseerror,throwerror?SQVM::ET_RESUME_THROW_VM : SQVM::ET_RESUME_VM)) {
|
if(!v->Execute(dummy,-1,-1,ret,raiseerror,throwerror?SQVM::ET_RESUME_THROW_VM : SQVM::ET_RESUME_VM)) {
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
if(retval)
|
if(retval) {
|
||||||
v->Push(ret);
|
v->Push(ret);
|
||||||
|
}
|
||||||
return SQ_OK;
|
return SQ_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1235,10 +1243,17 @@ void sq_setreleasehook(HRABBITVM v,SQInteger idx,SQRELEASEHOOK hook)
|
|||||||
{
|
{
|
||||||
SQObjectPtr &ud=stack_get(v,idx);
|
SQObjectPtr &ud=stack_get(v,idx);
|
||||||
switch(sq_type(ud) ) {
|
switch(sq_type(ud) ) {
|
||||||
case OT_USERDATA: _userdata(ud)->_hook = hook; break;
|
case OT_USERDATA:
|
||||||
case OT_INSTANCE: _instance(ud)->_hook = hook; break;
|
_userdata(ud)->setHook(hook);
|
||||||
case OT_CLASS: _class(ud)->_hook = hook; break;
|
break;
|
||||||
default: return;
|
case OT_INSTANCE:
|
||||||
|
_instance(ud)->_hook = hook;
|
||||||
|
break;
|
||||||
|
case OT_CLASS:
|
||||||
|
_class(ud)->_hook = hook;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1246,10 +1261,17 @@ SQRELEASEHOOK sq_getreleasehook(HRABBITVM v,SQInteger idx)
|
|||||||
{
|
{
|
||||||
SQObjectPtr &ud=stack_get(v,idx);
|
SQObjectPtr &ud=stack_get(v,idx);
|
||||||
switch(sq_type(ud) ) {
|
switch(sq_type(ud) ) {
|
||||||
case OT_USERDATA: return _userdata(ud)->_hook; break;
|
case OT_USERDATA:
|
||||||
case OT_INSTANCE: return _instance(ud)->_hook; break;
|
return _userdata(ud)->getHook();
|
||||||
case OT_CLASS: return _class(ud)->_hook; break;
|
break;
|
||||||
default: return NULL;
|
case OT_INSTANCE:
|
||||||
|
return _instance(ud)->_hook;
|
||||||
|
break;
|
||||||
|
case OT_CLASS:
|
||||||
|
return _class(ud)->_hook;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#include <rabbit/sqstring.hpp>
|
#include <rabbit/sqstring.hpp>
|
||||||
#include <rabbit/sqarray.hpp>
|
#include <rabbit/sqarray.hpp>
|
||||||
#include <rabbit/sqtable.hpp>
|
#include <rabbit/sqtable.hpp>
|
||||||
#include <rabbit/squserdata.hpp>
|
#include <rabbit/UserData.hpp>
|
||||||
#include <rabbit/sqfuncproto.hpp>
|
#include <rabbit/sqfuncproto.hpp>
|
||||||
#include <rabbit/sqclass.hpp>
|
#include <rabbit/sqclass.hpp>
|
||||||
#include <rabbit/sqclosure.hpp>
|
#include <rabbit/sqclosure.hpp>
|
||||||
|
@ -236,7 +236,7 @@ struct SQObjectPtr : public SQObject
|
|||||||
_REF_TYPE_DECL(OT_OUTER,SQOuter,pOuter)
|
_REF_TYPE_DECL(OT_OUTER,SQOuter,pOuter)
|
||||||
_REF_TYPE_DECL(OT_GENERATOR,SQGenerator,pGenerator)
|
_REF_TYPE_DECL(OT_GENERATOR,SQGenerator,pGenerator)
|
||||||
_REF_TYPE_DECL(OT_STRING,SQString,pString)
|
_REF_TYPE_DECL(OT_STRING,SQString,pString)
|
||||||
_REF_TYPE_DECL(OT_USERDATA,SQUserData,pUserData)
|
_REF_TYPE_DECL(OT_USERDATA,rabbit::UserData,pUserData)
|
||||||
_REF_TYPE_DECL(OT_WEAKREF,SQWeakRef,pWeakRef)
|
_REF_TYPE_DECL(OT_WEAKREF,SQWeakRef,pWeakRef)
|
||||||
_REF_TYPE_DECL(OT_THREAD,SQVM,pThread)
|
_REF_TYPE_DECL(OT_THREAD,SQVM,pThread)
|
||||||
_REF_TYPE_DECL(OT_FUNCPROTO,SQFunctionProto,pFunctionProto)
|
_REF_TYPE_DECL(OT_FUNCPROTO,SQFunctionProto,pFunctionProto)
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <rabbit/sqstring.hpp>
|
#include <rabbit/sqstring.hpp>
|
||||||
#include <rabbit/sqtable.hpp>
|
#include <rabbit/sqtable.hpp>
|
||||||
#include <rabbit/sqarray.hpp>
|
#include <rabbit/sqarray.hpp>
|
||||||
#include <rabbit/squserdata.hpp>
|
#include <rabbit/UserData.hpp>
|
||||||
#include <rabbit/sqclass.hpp>
|
#include <rabbit/sqclass.hpp>
|
||||||
|
|
||||||
SQSharedState::SQSharedState()
|
SQSharedState::SQSharedState()
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
/**
|
|
||||||
* @author Alberto DEMICHELIS
|
|
||||||
* @author Edouard DUPIN
|
|
||||||
* @copyright 2018, Edouard DUPIN, all right reserved
|
|
||||||
* @copyright 2003-2017, Alberto DEMICHELIS, all right reserved
|
|
||||||
* @license MPL-2 (see license file)
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
struct SQUserData : SQDelegable
|
|
||||||
{
|
|
||||||
SQUserData(SQSharedState *ss){ _delegate = 0; _hook = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain, this); }
|
|
||||||
~SQUserData()
|
|
||||||
{
|
|
||||||
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
|
|
||||||
SetDelegate(NULL);
|
|
||||||
}
|
|
||||||
static SQUserData* Create(SQSharedState *ss, SQInteger size)
|
|
||||||
{
|
|
||||||
SQUserData* ud = (SQUserData*)SQ_MALLOC(sq_aligning(sizeof(SQUserData))+size);
|
|
||||||
new (ud) SQUserData(ss);
|
|
||||||
ud->_size = size;
|
|
||||||
ud->_typetag = 0;
|
|
||||||
return ud;
|
|
||||||
}
|
|
||||||
void Release() {
|
|
||||||
if (_hook) _hook((SQUserPointer)sq_aligning(this + 1),_size);
|
|
||||||
SQInteger tsize = _size;
|
|
||||||
this->~SQUserData();
|
|
||||||
SQ_FREE(this, sq_aligning(sizeof(SQUserData)) + tsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SQInteger _size;
|
|
||||||
SQRELEASEHOOK _hook;
|
|
||||||
SQUserPointer _typetag;
|
|
||||||
//SQChar _val[1];
|
|
||||||
};
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
|||||||
#include <rabbit/sqclosure.hpp>
|
#include <rabbit/sqclosure.hpp>
|
||||||
#include <rabbit/sqstring.hpp>
|
#include <rabbit/sqstring.hpp>
|
||||||
#include <rabbit/sqtable.hpp>
|
#include <rabbit/sqtable.hpp>
|
||||||
#include <rabbit/squserdata.hpp>
|
#include <rabbit/UserData.hpp>
|
||||||
#include <rabbit/sqarray.hpp>
|
#include <rabbit/sqarray.hpp>
|
||||||
#include <rabbit/sqclass.hpp>
|
#include <rabbit/sqclass.hpp>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user