Upgraded to Squirrel 2.2
[supertux.git] / src / squirrel / squirrel / sqobject.cpp
1 /*
2         see copyright notice in squirrel.h
3 */
4 #include "sqpcheader.h"
5 #include "sqvm.h"
6 #include "sqstring.h"
7 #include "sqarray.h"
8 #include "sqtable.h"
9 #include "squserdata.h"
10 #include "sqfuncproto.h"
11 #include "sqclass.h"
12 #include "sqclosure.h"
13
14
15 const SQChar *IdType2Name(SQObjectType type)
16 {
17         switch(_RAW_TYPE(type))
18         {
19         case _RT_NULL:return _SC("null");
20         case _RT_INTEGER:return _SC("integer");
21         case _RT_FLOAT:return _SC("float");
22         case _RT_BOOL:return _SC("bool");
23         case _RT_STRING:return _SC("string");
24         case _RT_TABLE:return _SC("table");
25         case _RT_ARRAY:return _SC("array");
26         case _RT_GENERATOR:return _SC("generator");
27         case _RT_CLOSURE:
28         case _RT_NATIVECLOSURE:
29                 return _SC("function");
30         case _RT_USERDATA:
31         case _RT_USERPOINTER:
32                 return _SC("userdata");
33         case _RT_THREAD: return _SC("thread");
34         case _RT_FUNCPROTO: return _SC("function");
35         case _RT_CLASS: return _SC("class");
36         case _RT_INSTANCE: return _SC("instance");
37         case _RT_WEAKREF: return _SC("weakref");
38         default:
39                 return NULL;
40         }
41 }
42
43 const SQChar *GetTypeName(const SQObjectPtr &obj1)
44 {
45         return IdType2Name(type(obj1)); 
46 }
47
48 SQString *SQString::Create(SQSharedState *ss,const SQChar *s,SQInteger len)
49 {
50         SQString *str=ADD_STRING(ss,s,len);
51         str->_sharedstate=ss;
52         return str;
53 }
54
55 void SQString::Release()
56 {
57         REMOVE_STRING(_sharedstate,this);
58 }
59
60 SQInteger SQString::Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
61 {
62         SQInteger idx = (SQInteger)TranslateIndex(refpos);
63         while(idx < _len){
64                 outkey = (SQInteger)idx;
65                 outval = SQInteger(_val[idx]);
66                 //return idx for the next iteration
67                 return ++idx;
68         }
69         //nothing to iterate anymore
70         return -1;
71 }
72
73 SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx)
74 {
75         switch(type(idx)){
76                 case OT_NULL:
77                         return 0;
78                 case OT_INTEGER:
79                         return (SQUnsignedInteger)_integer(idx);
80                 default: assert(0); break;
81         }
82         return 0;
83 }
84
85 SQWeakRef *SQRefCounted::GetWeakRef(SQObjectType type)
86 {
87         if(!_weakref) {
88                 sq_new(_weakref,SQWeakRef);
89                 _weakref->_obj._type = type;
90                 _weakref->_obj._unVal.pRefCounted = this;
91         }
92         return _weakref;
93 }
94
95 SQRefCounted::~SQRefCounted()
96 {
97         if(_weakref) {
98                 _weakref->_obj._type = OT_NULL;
99                 _weakref->_obj._unVal.pRefCounted = NULL;
100         }
101 }
102
103 void SQWeakRef::Release() { 
104         if(ISREFCOUNTED(_obj._type)) { 
105                 _obj._unVal.pRefCounted->_weakref = NULL;
106         } 
107         sq_delete(this,SQWeakRef);
108 }
109
110 bool SQDelegable::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res) {
111         if(_delegate) {
112                 return _delegate->Get((*_ss(v)->_metamethods)[mm],res);
113         }
114         return false;
115 }
116
117 bool SQDelegable::SetDelegate(SQTable *mt)
118 {
119         SQTable *temp = mt;
120         while (temp) {
121                 if (temp->_delegate == this) return false; //cycle detected
122                 temp = temp->_delegate;
123         }
124         if (mt) __ObjAddRef(mt);
125         __ObjRelease(_delegate);
126         _delegate = mt;
127         return true;
128 }
129
130 bool SQGenerator::Yield(SQVM *v)
131 {
132         if(_state==eSuspended) { v->Raise_Error(_SC("internal vm error, yielding dead generator"));  return false;}
133         if(_state==eDead) { v->Raise_Error(_SC("internal vm error, yielding a dead generator")); return false; }
134         SQInteger size = v->_top-v->_stackbase;
135         _ci=*v->ci;
136         _stack.resize(size);
137         for(SQInteger n =0; n<size; n++) {
138                 _stack._vals[n] = v->_stack[v->_stackbase+n];
139                 v->_stack[v->_stackbase+n] = _null_;
140         }
141         SQInteger nvargs = v->ci->_vargs.size;
142         SQInteger vargsbase = v->ci->_vargs.base;
143         for(SQInteger j = nvargs - 1; j >= 0; j--) {
144                 _vargsstack.push_back(v->_vargsstack[vargsbase+j]);
145         }
146         _ci._generator=_null_;
147         for(SQInteger i=0;i<_ci._etraps;i++) {
148                 _etraps.push_back(v->_etraps.top());
149                 v->_etraps.pop_back();
150         }
151         _state=eSuspended;
152         return true;
153 }
154
155 bool SQGenerator::Resume(SQVM *v,SQInteger target)
156 {
157         SQInteger size=_stack.size();
158         if(_state==eDead){ v->Raise_Error(_SC("resuming dead generator")); return false; }
159         if(_state==eRunning){ v->Raise_Error(_SC("resuming active generator")); return false; }
160         SQInteger prevtop=v->_top-v->_stackbase;
161         PUSH_CALLINFO(v,_ci);
162         SQInteger oldstackbase=v->_stackbase;
163         v->_stackbase = v->_top;
164         v->ci->_target = (SQInt32)target;
165         v->ci->_generator = SQObjectPtr(this);
166         v->ci->_vargs.size = (unsigned short)_vargsstack.size();
167         
168         for(SQInteger i=0;i<_ci._etraps;i++) {
169                 v->_etraps.push_back(_etraps.top());
170                 _etraps.pop_back();
171         }
172         for(SQInteger n =0; n<size; n++) {
173                 v->_stack[v->_stackbase+n] = _stack._vals[n];
174                 _stack._vals[0] = _null_;
175         }
176         while(_vargsstack.size()) {
177                 v->_vargsstack.push_back(_vargsstack.back());
178                 _vargsstack.pop_back();
179         }
180         v->ci->_vargs.base = (unsigned short)(v->_vargsstack.size() - v->ci->_vargs.size);
181         v->_top=v->_stackbase+size;
182         v->ci->_prevtop = (SQInt32)prevtop;
183         v->ci->_prevstkbase = (SQInt32)(v->_stackbase - oldstackbase);
184         _state=eRunning;
185         if (type(v->_debughook) != OT_NULL && _rawval(v->_debughook) != _rawval(v->ci->_closure))
186                 v->CallDebugHook(_SC('c'));
187
188         return true;
189 }
190
191 void SQArray::Extend(const SQArray *a){
192         SQInteger xlen;
193         if((xlen=a->Size()))
194                 for(SQInteger i=0;i<xlen;i++)
195                         Append(a->_values[i]);
196 }
197
198 const SQChar* SQFunctionProto::GetLocal(SQVM *vm,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop)
199 {
200         SQUnsignedInteger nvars=_nlocalvarinfos;
201         const SQChar *res=NULL; 
202         if(nvars>=nseq){
203                 for(SQUnsignedInteger i=0;i<nvars;i++){
204                         if(_localvarinfos[i]._start_op<=nop && _localvarinfos[i]._end_op>=nop)
205                         {
206                                 if(nseq==0){
207                                         vm->Push(vm->_stack[stackbase+_localvarinfos[i]._pos]);
208                                         res=_stringval(_localvarinfos[i]._name);
209                                         break;
210                                 }
211                                 nseq--;
212                         }
213                 }
214         }
215         return res;
216 }
217
218 SQInteger SQFunctionProto::GetLine(SQInstruction *curr)
219 {
220         SQInteger op = (SQInteger)(curr-_instructions);
221         SQInteger line=_lineinfos[0]._line;
222         for(SQInteger i=1;i<_nlineinfos;i++){
223                 if(_lineinfos[i]._op>=op)
224                         return line;
225                 line=_lineinfos[i]._line;
226         }
227         return line;
228 }
229
230 #define _CHECK_IO(exp)  { if(!exp)return false; }
231 bool SafeWrite(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQUserPointer dest,SQInteger size)
232 {
233         if(write(up,dest,size) != size) {
234                 v->Raise_Error(_SC("io error (write function failure)"));
235                 return false;
236         }
237         return true;
238 }
239
240 bool SafeRead(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQUserPointer dest,SQInteger size)
241 {
242         if(size && read(up,dest,size) != size) {
243                 v->Raise_Error(_SC("io error, read function failure, the origin stream could be corrupted/trucated"));
244                 return false;
245         }
246         return true;
247 }
248
249 bool WriteTag(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQInteger tag)
250 {
251         return SafeWrite(v,write,up,&tag,sizeof(tag));
252 }
253
254 bool CheckTag(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQInteger tag)
255 {
256         SQInteger t;
257         _CHECK_IO(SafeRead(v,read,up,&t,sizeof(t)));
258         if(t != tag){
259                 v->Raise_Error(_SC("invalid or corrupted closure stream"));
260                 return false;
261         }
262         return true;
263 }
264
265 bool WriteObject(HSQUIRRELVM v,SQUserPointer up,SQWRITEFUNC write,SQObjectPtr &o)
266 {
267         _CHECK_IO(SafeWrite(v,write,up,&type(o),sizeof(SQObjectType)));
268         switch(type(o)){
269         case OT_STRING:
270                 _CHECK_IO(SafeWrite(v,write,up,&_string(o)->_len,sizeof(SQInteger)));
271                 _CHECK_IO(SafeWrite(v,write,up,_stringval(o),rsl(_string(o)->_len)));
272                 break;
273         case OT_INTEGER:
274                 _CHECK_IO(SafeWrite(v,write,up,&_integer(o),sizeof(SQInteger)));break;
275         case OT_FLOAT:
276                 _CHECK_IO(SafeWrite(v,write,up,&_float(o),sizeof(SQFloat)));break;
277         case OT_NULL:
278                 break;
279         default:
280                 v->Raise_Error(_SC("cannot serialize a %s"),GetTypeName(o));
281                 return false;
282         }
283         return true;
284 }
285
286 bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
287 {
288         SQObjectType t;
289         _CHECK_IO(SafeRead(v,read,up,&t,sizeof(SQObjectType)));
290         switch(t){
291         case OT_STRING:{
292                 SQInteger len;
293                 _CHECK_IO(SafeRead(v,read,up,&len,sizeof(SQInteger)));
294                 _CHECK_IO(SafeRead(v,read,up,_ss(v)->GetScratchPad(rsl(len)),rsl(len)));
295                 o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len);
296                                    }
297                 break;
298         case OT_INTEGER:{
299                 SQInteger i;
300                 _CHECK_IO(SafeRead(v,read,up,&i,sizeof(SQInteger))); o = i; break;
301                                         }
302         case OT_FLOAT:{
303                 SQFloat f;
304                 _CHECK_IO(SafeRead(v,read,up,&f,sizeof(SQFloat))); o = f; break;
305                                   }
306         case OT_NULL:
307                 o=_null_;
308                 break;
309         default:
310                 v->Raise_Error(_SC("cannot serialize a %s"),IdType2Name(t));
311                 return false;
312         }
313         return true;
314 }
315
316 bool SQClosure::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
317 {
318         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_HEAD));
319         _CHECK_IO(WriteTag(v,write,up,sizeof(SQChar)));
320         _CHECK_IO(_funcproto(_function)->Save(v,up,write));
321         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_TAIL));
322         return true;
323 }
324
325 bool SQClosure::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
326 {
327         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_HEAD));
328         _CHECK_IO(CheckTag(v,read,up,sizeof(SQChar)));
329         SQObjectPtr func;
330         _CHECK_IO(SQFunctionProto::Load(v,up,read,func));
331         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_TAIL));
332         ret = SQClosure::Create(_ss(v),_funcproto(func));
333         return true;
334 }
335
336 bool SQFunctionProto::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
337 {
338         SQInteger i,nliterals = _nliterals,nparameters = _nparameters;
339         SQInteger noutervalues = _noutervalues,nlocalvarinfos = _nlocalvarinfos;
340         SQInteger nlineinfos=_nlineinfos,ninstructions = _ninstructions,nfunctions=_nfunctions;
341         SQInteger ndefaultparams = _ndefaultparams;
342         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
343         _CHECK_IO(WriteObject(v,up,write,_sourcename));
344         _CHECK_IO(WriteObject(v,up,write,_name));
345         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
346         _CHECK_IO(SafeWrite(v,write,up,&nliterals,sizeof(nliterals)));
347         _CHECK_IO(SafeWrite(v,write,up,&nparameters,sizeof(nparameters)));
348         _CHECK_IO(SafeWrite(v,write,up,&noutervalues,sizeof(noutervalues)));
349         _CHECK_IO(SafeWrite(v,write,up,&nlocalvarinfos,sizeof(nlocalvarinfos)));
350         _CHECK_IO(SafeWrite(v,write,up,&nlineinfos,sizeof(nlineinfos)));
351         _CHECK_IO(SafeWrite(v,write,up,&ndefaultparams,sizeof(ndefaultparams)));
352         _CHECK_IO(SafeWrite(v,write,up,&ninstructions,sizeof(ninstructions)));
353         _CHECK_IO(SafeWrite(v,write,up,&nfunctions,sizeof(nfunctions)));
354         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
355         for(i=0;i<nliterals;i++){
356                 _CHECK_IO(WriteObject(v,up,write,_literals[i]));
357         }
358
359         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
360         for(i=0;i<nparameters;i++){
361                 _CHECK_IO(WriteObject(v,up,write,_parameters[i]));
362         }
363
364         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
365         for(i=0;i<noutervalues;i++){
366                 _CHECK_IO(SafeWrite(v,write,up,&_outervalues[i]._type,sizeof(SQUnsignedInteger)));
367                 _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._src));
368                 _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._name));
369         }
370
371         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
372         for(i=0;i<nlocalvarinfos;i++){
373                 SQLocalVarInfo &lvi=_localvarinfos[i];
374                 _CHECK_IO(WriteObject(v,up,write,lvi._name));
375                 _CHECK_IO(SafeWrite(v,write,up,&lvi._pos,sizeof(SQUnsignedInteger)));
376                 _CHECK_IO(SafeWrite(v,write,up,&lvi._start_op,sizeof(SQUnsignedInteger)));
377                 _CHECK_IO(SafeWrite(v,write,up,&lvi._end_op,sizeof(SQUnsignedInteger)));
378         }
379
380         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
381         _CHECK_IO(SafeWrite(v,write,up,_lineinfos,sizeof(SQLineInfo)*nlineinfos));
382
383         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
384         _CHECK_IO(SafeWrite(v,write,up,_defaultparams,sizeof(SQInteger)*ndefaultparams));
385
386         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
387         _CHECK_IO(SafeWrite(v,write,up,_instructions,sizeof(SQInstruction)*ninstructions));
388
389         _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
390         for(i=0;i<nfunctions;i++){
391                 _CHECK_IO(_funcproto(_functions[i])->Save(v,up,write));
392         }
393         _CHECK_IO(SafeWrite(v,write,up,&_stacksize,sizeof(_stacksize)));
394         _CHECK_IO(SafeWrite(v,write,up,&_bgenerator,sizeof(_bgenerator)));
395         _CHECK_IO(SafeWrite(v,write,up,&_varparams,sizeof(_varparams)));
396         return true;
397 }
398
399 bool SQFunctionProto::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
400 {
401         SQInteger i, nliterals,nparameters;
402         SQInteger noutervalues ,nlocalvarinfos ;
403         SQInteger nlineinfos,ninstructions ,nfunctions,ndefaultparams ;
404         SQObjectPtr sourcename, name;
405         SQObjectPtr o;
406         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
407         _CHECK_IO(ReadObject(v, up, read, sourcename));
408         _CHECK_IO(ReadObject(v, up, read, name));
409         
410         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
411         _CHECK_IO(SafeRead(v,read,up, &nliterals, sizeof(nliterals)));
412         _CHECK_IO(SafeRead(v,read,up, &nparameters, sizeof(nparameters)));
413         _CHECK_IO(SafeRead(v,read,up, &noutervalues, sizeof(noutervalues)));
414         _CHECK_IO(SafeRead(v,read,up, &nlocalvarinfos, sizeof(nlocalvarinfos)));
415         _CHECK_IO(SafeRead(v,read,up, &nlineinfos, sizeof(nlineinfos)));
416         _CHECK_IO(SafeRead(v,read,up, &ndefaultparams, sizeof(ndefaultparams)));
417         _CHECK_IO(SafeRead(v,read,up, &ninstructions, sizeof(ninstructions)));
418         _CHECK_IO(SafeRead(v,read,up, &nfunctions, sizeof(nfunctions)));
419         
420
421         SQFunctionProto *f = SQFunctionProto::Create(ninstructions,nliterals,nparameters,
422                         nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams);
423         SQObjectPtr proto = f; //gets a ref in case of failure
424         f->_sourcename = sourcename;
425         f->_name = name;
426
427         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
428
429         for(i = 0;i < nliterals; i++){
430                 _CHECK_IO(ReadObject(v, up, read, o));
431                 f->_literals[i] = o;
432         }
433         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
434
435         for(i = 0; i < nparameters; i++){
436                 _CHECK_IO(ReadObject(v, up, read, o));
437                 f->_parameters[i] = o;
438         }
439         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
440
441         for(i = 0; i < noutervalues; i++){
442                 SQUnsignedInteger type;
443                 SQObjectPtr name;
444                 _CHECK_IO(SafeRead(v,read,up, &type, sizeof(SQUnsignedInteger)));
445                 _CHECK_IO(ReadObject(v, up, read, o));
446                 _CHECK_IO(ReadObject(v, up, read, name));
447                 f->_outervalues[i] = SQOuterVar(name,o, (SQOuterType)type);
448         }
449         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
450
451         for(i = 0; i < nlocalvarinfos; i++){
452                 SQLocalVarInfo lvi;
453                 _CHECK_IO(ReadObject(v, up, read, lvi._name));
454                 _CHECK_IO(SafeRead(v,read,up, &lvi._pos, sizeof(SQUnsignedInteger)));
455                 _CHECK_IO(SafeRead(v,read,up, &lvi._start_op, sizeof(SQUnsignedInteger)));
456                 _CHECK_IO(SafeRead(v,read,up, &lvi._end_op, sizeof(SQUnsignedInteger)));
457                 f->_localvarinfos[i] = lvi;
458         }
459         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
460         _CHECK_IO(SafeRead(v,read,up, f->_lineinfos, sizeof(SQLineInfo)*nlineinfos));
461
462         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
463         _CHECK_IO(SafeRead(v,read,up, f->_defaultparams, sizeof(SQInteger)*ndefaultparams));
464
465         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
466         _CHECK_IO(SafeRead(v,read,up, f->_instructions, sizeof(SQInstruction)*ninstructions));
467
468         _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
469         for(i = 0; i < nfunctions; i++){
470                 _CHECK_IO(_funcproto(o)->Load(v, up, read, o));
471                 f->_functions[i] = o;
472         }
473         _CHECK_IO(SafeRead(v,read,up, &f->_stacksize, sizeof(f->_stacksize)));
474         _CHECK_IO(SafeRead(v,read,up, &f->_bgenerator, sizeof(f->_bgenerator)));
475         _CHECK_IO(SafeRead(v,read,up, &f->_varparams, sizeof(f->_varparams)));
476         
477         ret = f;
478         return true;
479 }
480
481 #ifndef NO_GARBAGE_COLLECTOR
482
483 #define START_MARK()    if(!(_uiRef&MARK_FLAG)){ \
484                 _uiRef|=MARK_FLAG;
485
486 #define END_MARK() RemoveFromChain(&_sharedstate->_gc_chain, this); \
487                 AddToChain(chain, this); }
488
489 void SQVM::Mark(SQCollectable **chain)
490 {
491         START_MARK()
492                 SQSharedState::MarkObject(_lasterror,chain);
493                 SQSharedState::MarkObject(_errorhandler,chain);
494                 SQSharedState::MarkObject(_debughook,chain);
495                 SQSharedState::MarkObject(_roottable, chain);
496                 SQSharedState::MarkObject(temp_reg, chain);
497                 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
498                 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
499         END_MARK()
500 }
501
502 void SQArray::Mark(SQCollectable **chain)
503 {
504         START_MARK()
505                 SQInteger len = _values.size();
506                 for(SQInteger i = 0;i < len; i++) SQSharedState::MarkObject(_values[i], chain);
507         END_MARK()
508 }
509 void SQTable::Mark(SQCollectable **chain)
510 {
511         START_MARK()
512                 if(_delegate) _delegate->Mark(chain);
513                 SQInteger len = _numofnodes;
514                 for(SQInteger i = 0; i < len; i++){
515                         SQSharedState::MarkObject(_nodes[i].key, chain);
516                         SQSharedState::MarkObject(_nodes[i].val, chain);
517                 }
518         END_MARK()
519 }
520
521 void SQClass::Mark(SQCollectable **chain)
522 {
523         START_MARK()
524                 _members->Mark(chain);
525                 if(_base) _base->Mark(chain);
526                 SQSharedState::MarkObject(_attributes, chain);
527                 for(SQUnsignedInteger i =0; i< _defaultvalues.size(); i++) {
528                         SQSharedState::MarkObject(_defaultvalues[i].val, chain);
529                         SQSharedState::MarkObject(_defaultvalues[i].attrs, chain);
530                 }
531                 for(SQUnsignedInteger j =0; j< _methods.size(); j++) {
532                         SQSharedState::MarkObject(_methods[j].val, chain);
533                         SQSharedState::MarkObject(_methods[j].attrs, chain);
534                 }
535                 for(SQUnsignedInteger k =0; k< _metamethods.size(); k++) {
536                         SQSharedState::MarkObject(_metamethods[k], chain);
537                 }
538         END_MARK()
539 }
540
541 void SQInstance::Mark(SQCollectable **chain)
542 {
543         START_MARK()
544                 _class->Mark(chain);
545                 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
546                 for(SQUnsignedInteger i =0; i< nvalues; i++) {
547                         SQSharedState::MarkObject(_values[i], chain);
548                 }
549         END_MARK()
550 }
551
552 void SQGenerator::Mark(SQCollectable **chain)
553 {
554         START_MARK()
555                 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
556                 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
557                 SQSharedState::MarkObject(_closure, chain);
558         END_MARK()
559 }
560
561 void SQClosure::Mark(SQCollectable **chain)
562 {
563         START_MARK()
564                 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
565                 for(SQUnsignedInteger i = 0; i < _defaultparams.size(); i++) SQSharedState::MarkObject(_defaultparams[i], chain);
566         END_MARK()
567 }
568
569 void SQNativeClosure::Mark(SQCollectable **chain)
570 {
571         START_MARK()
572                 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
573         END_MARK()
574 }
575
576 void SQUserData::Mark(SQCollectable **chain){
577         START_MARK()
578                 if(_delegate) _delegate->Mark(chain);
579         END_MARK()
580 }
581
582 void SQCollectable::UnMark() { _uiRef&=~MARK_FLAG; }
583
584 #endif
585