4 #include "sqpcheader.h" 10 SQClass::SQClass(SQSharedState *ss,SQClass *base)
16 _metamethods.resize(MT_LAST);
18 _defaultvalues.copy(base->_defaultvalues);
19 _methods.copy(base->_methods);
20 _metamethods.copy(base->_metamethods);
23 _members = base?base->_members->Clone() : SQTable::Create(ss,0);
24 __ObjAddRef(_members);
27 ADD_TO_CHAIN(&_sharedstate->_gc_chain,
this);
30 void SQClass::Finalize() {
32 _defaultvalues.resize(0);
34 _metamethods.resize(0);
35 __ObjRelease(_members);
43 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain,
this);
47 bool SQClass::NewSlot(SQSharedState *ss,
const SQObjectPtr &key,
const SQObjectPtr &val,
bool bstatic)
52 if(_members->Get(key,temp) && _isfield(temp))
54 _defaultvalues[_member_idx(temp)].val = val;
57 if(type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE || bstatic) {
59 if((type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE) &&
60 (mmidx = ss->GetMetaMethodIdxByName(key)) != -1) {
61 _metamethods[mmidx] = val;
64 if(type(temp) == OT_NULL) {
67 _members->NewSlot(key,SQObjectPtr(_make_method_idx(_methods.size())));
68 _methods.push_back(m);
71 _methods[_member_idx(temp)].val = val;
78 _members->NewSlot(key,SQObjectPtr(_make_field_idx(_defaultvalues.size())));
79 _defaultvalues.push_back(m);
83 SQInstance *SQClass::CreateInstance()
86 return SQInstance::Create(_opt_ss(
this),
this);
89 SQInteger SQClass::Next(
const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
92 SQInteger idx = _members->Next(
false,refpos,outkey,oval);
95 outval = _methods[_member_idx(oval)].val;
98 SQObjectPtr &o = _defaultvalues[_member_idx(oval)].val;
105 bool SQClass::SetAttributes(
const SQObjectPtr &key,
const SQObjectPtr &val)
108 if(_members->Get(key,idx)) {
110 _defaultvalues[_member_idx(idx)].attrs = val;
112 _methods[_member_idx(idx)].attrs = val;
118 bool SQClass::GetAttributes(
const SQObjectPtr &key,SQObjectPtr &outval)
121 if(_members->Get(key,idx)) {
122 outval = (_isfield(idx)?_defaultvalues[_member_idx(idx)].attrs:_methods[_member_idx(idx)].attrs);
129 void SQInstance::Init(SQSharedState *ss)
134 _delegate = _class->_members;
136 ADD_TO_CHAIN(&_sharedstate->_gc_chain,
this);
139 SQInstance::SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize)
143 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
144 for(SQUnsignedInteger n = 0; n < nvalues; n++) {
145 new (&_values[n]) SQObjectPtr(_class->_defaultvalues[n].val);
150 SQInstance::SQInstance(SQSharedState *ss, SQInstance *i, SQInteger memsize)
154 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
155 for(SQUnsignedInteger n = 0; n < nvalues; n++) {
156 new (&_values[n]) SQObjectPtr(i->_values[n]);
161 void SQInstance::Finalize()
163 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
164 __ObjRelease(_class);
165 for(SQUnsignedInteger i = 0; i < nvalues; i++) {
170 SQInstance::~SQInstance()
172 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain,
this);
173 if(_class){ Finalize(); }
177 bool SQInstance::GetMetaMethod(SQVM * ,SQMetaMethod mm,SQObjectPtr &res)
179 if(type(_class->_metamethods[mm]) != OT_NULL) {
180 res = _class->_metamethods[mm];
186 bool SQInstance::InstanceOf(SQClass *trg)
188 SQClass *parent = _class;
189 while(parent !=
NULL) {
192 parent = parent->_base;