# sqobject.cpp

    00001 /*
    00002   see copyright notice in squirrel.h
    00003 */
    00004 #include "sqpcheader.h"
    00005 #include "sqvm.h"
    00006 #include "sqstring.h"
    00007 #include "sqarray.h"
    00008 #include "sqtable.h"
    00009 #include "squserdata.h"
    00010 #include "sqfuncproto.h"
    00011 #include "sqclass.h"
    00012 #include "sqclosure.h"
    00013
    00014
    00015 const SQChar *IdType2Name(SQObjectType type)
    00016 {
    00017   switch(_RAW_TYPE(type))
    00018   {
    00019   case _RT_NULL:return _SC("null");
    00020   case _RT_INTEGER:return _SC("integer");
    00021   case _RT_FLOAT:return _SC("float");
    00022   case _RT_BOOL:return _SC("bool");
    00023   case _RT_STRING:return _SC("string");
    00024   case _RT_TABLE:return _SC("table");
    00025   case _RT_ARRAY:return _SC("array");
    00026   case _RT_GENERATOR:return _SC("generator");
    00027   case _RT_CLOSURE:
    00028   case _RT_NATIVECLOSURE:
    00029     return _SC("function");
    00030   case _RT_USERDATA:
    00031   case _RT_USERPOINTER:
    00032     return _SC("userdata");
    00033   case _RT_THREAD: return _SC("thread");
    00034   case _RT_FUNCPROTO: return _SC("function");
    00035   case _RT_CLASS: return _SC("class");
    00036   case _RT_INSTANCE: return _SC("instance");
    00037   case _RT_WEAKREF: return _SC("weakref");
    00038   default:
    00039     return NULL;
    00040   }
    00041 }
    00042
    00043 const SQChar *GetTypeName(const SQObjectPtr &obj1)
    00044 {
    00045   return IdType2Name(type(obj1));
    00046 }
    00047
    00048 SQString *SQString::Create(SQSharedState *ss,const SQChar *s,SQInteger len)
    00049 {
    00050   SQString *str=ADD_STRING(ss,s,len);
    00051   str->_sharedstate=ss;
    00052   return str;
    00053 }
    00054
    00055 void SQString::Release()
    00056 {
    00057   REMOVE_STRING(_sharedstate,this);
    00058 }
    00059
    00060 SQInteger SQString::Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
    00061 {
    00062   SQInteger idx = (SQInteger)TranslateIndex(refpos);
    00063   while(idx < _len){
    00064     outkey = (SQInteger)idx;
    00065     outval = SQInteger(_val[idx]);
    00066     //return idx for the next iteration
    00067     return ++idx;
    00068   }
    00069   //nothing to iterate anymore
    00070   return -1;
    00071 }
    00072
    00073 SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx)
    00074 {
    00075   switch(type(idx)){
    00076     case OT_NULL:
    00077       return 0;
    00078     case OT_INTEGER:
    00079       return (SQUnsignedInteger)_integer(idx);
    00080     default: assert(0); break;
    00081   }
    00082   return 0;
    00083 }
    00084
    00085 SQWeakRef *SQRefCounted::GetWeakRef(SQObjectType type)
    00086 {
    00087   if(!_weakref) {
    00088     sq_new(_weakref,SQWeakRef);
    00089     _weakref->_obj._type = type;
    00090     _weakref->_obj._unVal.pRefCounted = this;
    00091   }
    00092   return _weakref;
    00093 }
    00094
    00095 SQRefCounted::~SQRefCounted()
    00096 {
    00097   if(_weakref) {
    00098     _weakref->_obj._type = OT_NULL;
    00099     _weakref->_obj._unVal.pRefCounted = NULL;
    00100   }
    00101 }
    00102
    00103 void SQWeakRef::Release() {
    00104   if(ISREFCOUNTED(_obj._type)) {
    00105     _obj._unVal.pRefCounted->_weakref = NULL;
    00106   }
    00107   sq_delete(this,SQWeakRef);
    00108 }
    00109
    00110 bool SQDelegable::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res) {
    00111   if(_delegate) {
    00112     return _delegate->Get((*_ss(v)->_metamethods)[mm],res);
    00113   }
    00114   return false;
    00115 }
    00116
    00117 bool SQDelegable::SetDelegate(SQTable *mt)
    00118 {
    00119   SQTable *temp = mt;
    00120   if(temp == this) return false;
    00121   while (temp) {
    00122     if (temp->_delegate == this) return false; //cycle detected
    00123     temp = temp->_delegate;
    00124   }
    00125   if (mt) __ObjAddRef(mt);
    00126   __ObjRelease(_delegate);
    00127   _delegate = mt;
    00128   return true;
    00129 }
    00130
    00131 bool SQGenerator::Yield(SQVM *v)
    00132 {
    00133   if(_state==eSuspended) { v->Raise_Error(_SC("internal vm error, yielding dead generator"));  return false;}
    00134   if(_state==eDead) { v->Raise_Error(_SC("internal vm error, yielding a dead generator")); return false; }
    00135   SQInteger size = v->_top-v->_stackbase;
    00136   _ci=*v->ci;
    00137   _stack.resize(size);
    00138   for(SQInteger n =0; n<size; n++) {
    00139     _stack._vals[n] = v->_stack[v->_stackbase+n];
    00140     v->_stack[v->_stackbase+n] = _null_;
    00141   }
    00142   SQInteger nvargs = v->ci->_vargs.size;
    00143   SQInteger vargsbase = v->ci->_vargs.base;
    00144   for(SQInteger j = nvargs - 1; j >= 0; j--) {
    00145     _vargsstack.push_back(v->_vargsstack[vargsbase+j]);
    00146   }
    00147   _ci._generator=NULL;
    00148   for(SQInteger i=0;i<_ci._etraps;i++) {
    00149     _etraps.push_back(v->_etraps.top());
    00150     v->_etraps.pop_back();
    00151   }
    00152   _state=eSuspended;
    00153   return true;
    00154 }
    00155
    00156 bool SQGenerator::Resume(SQVM *v,SQInteger target)
    00157 {
    00158   SQInteger size=_stack.size();
    00159   if(_state==eDead){ v->Raise_Error(_SC("resuming dead generator")); return false; }
    00160   if(_state==eRunning){ v->Raise_Error(_SC("resuming active generator")); return false; }
    00161   SQInteger prevtop=v->_top-v->_stackbase;
    00162   PUSH_CALLINFO(v,_ci);
    00163   SQInteger oldstackbase=v->_stackbase;
    00164   v->_stackbase = v->_top;
    00165   v->ci->_target = (SQInt32)target;
    00166   v->ci->_generator = this;
    00167   v->ci->_vargs.size = (unsigned short)_vargsstack.size();
    00168
    00169   for(SQInteger i=0;i<_ci._etraps;i++) {
    00170     v->_etraps.push_back(_etraps.top());
    00171     _etraps.pop_back();
    00172   }
    00173   for(SQInteger n =0; n<size; n++) {
    00174     v->_stack[v->_stackbase+n] = _stack._vals[n];
    00175     _stack._vals[0] = _null_;
    00176   }
    00177   while(_vargsstack.size()) {
    00178     v->_vargsstack.push_back(_vargsstack.back());
    00179     _vargsstack.pop_back();
    00180   }
    00181   v->ci->_vargs.base = (unsigned short)(v->_vargsstack.size() - v->ci->_vargs.size);
    00182   v->_top=v->_stackbase+size;
    00183   v->ci->_prevtop = (SQInt32)prevtop;
    00184   v->ci->_prevstkbase = (SQInt32)(v->_stackbase - oldstackbase);
    00185   _state=eRunning;
    00186   if (type(v->_debughook) != OT_NULL && _rawval(v->_debughook) != _rawval(v->ci->_closure))
    00187     v->CallDebugHook(_SC('c'));
    00188
    00189   return true;
    00190 }
    00191
    00192 void SQArray::Extend(const SQArray *a){
    00193   SQInteger xlen;
    00194   if((xlen=a->Size()))
    00195     for(SQInteger i=0;i<xlen;i++)
    00196       Append(a->_values[i]);
    00197 }
    00198
    00199 const SQChar* SQFunctionProto::GetLocal(SQVM *vm,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop)
    00200 {
    00201   SQUnsignedInteger nvars=_nlocalvarinfos;
    00202   const SQChar *res=NULL;
    00203   if(nvars>=nseq){
    00204     for(SQUnsignedInteger i=0;i<nvars;i++){
    00205       if(_localvarinfos[i]._start_op<=nop && _localvarinfos[i]._end_op>=nop)
    00206       {
    00207         if(nseq==0){
    00208           vm->Push(vm->_stack[stackbase+_localvarinfos[i]._pos]);
    00209           res=_stringval(_localvarinfos[i]._name);
    00210           break;
    00211         }
    00212         nseq--;
    00213       }
    00214     }
    00215   }
    00216   return res;
    00217 }
    00218
    00219 SQInteger SQFunctionProto::GetLine(SQInstruction *curr)
    00220 {
    00221   SQInteger op = (SQInteger)(curr-_instructions);
    00222   SQInteger line=_lineinfos[0]._line;
    00223   for(SQInteger i=1;i<_nlineinfos;i++){
    00224     if(_lineinfos[i]._op>=op)
    00225       return line;
    00226     line=_lineinfos[i]._line;
    00227   }
    00228   return line;
    00229 }
    00230
    00231 #define _CHECK_IO(exp)  { if(!exp)return false; }
    00232 bool SafeWrite(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQUserPointer dest,SQInteger size)
    00233 {
    00234   if(write(up,dest,size) != size) {
    00235     v->Raise_Error(_SC("io error (write function failure)"));
    00236     return false;
    00237   }
    00238   return true;
    00239 }
    00240
    00241 bool SafeRead(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQUserPointer dest,SQInteger size)
    00242 {
    00243   if(size && read(up,dest,size) != size) {
    00244     v->Raise_Error(_SC("io error, read function failure, the origin stream could be corrupted/trucated"));
    00245     return false;
    00246   }
    00247   return true;
    00248 }
    00249
    00250 bool WriteTag(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQInteger tag)
    00251 {
    00252   return SafeWrite(v,write,up,&tag,sizeof(tag));
    00253 }
    00254
    00255 bool CheckTag(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQInteger tag)
    00256 {
    00257   SQInteger t;
    00258   _CHECK_IO(SafeRead(v,read,up,&t,sizeof(t)));
    00259   if(t != tag){
    00260     v->Raise_Error(_SC("invalid or corrupted closure stream"));
    00261     return false;
    00262   }
    00263   return true;
    00264 }
    00265
    00266 bool WriteObject(HSQUIRRELVM v,SQUserPointer up,SQWRITEFUNC write,SQObjectPtr &o)
    00267 {
    00268   _CHECK_IO(SafeWrite(v,write,up,&type(o),sizeof(SQObjectType)));
    00269   switch(type(o)){
    00270   case OT_STRING:
    00271     _CHECK_IO(SafeWrite(v,write,up,&_string(o)->_len,sizeof(SQInteger)));
    00272     _CHECK_IO(SafeWrite(v,write,up,_stringval(o),rsl(_string(o)->_len)));
    00273     break;
    00274   case OT_INTEGER:
    00275     _CHECK_IO(SafeWrite(v,write,up,&_integer(o),sizeof(SQInteger)));break;
    00276   case OT_FLOAT:
    00277     _CHECK_IO(SafeWrite(v,write,up,&_float(o),sizeof(SQFloat)));break;
    00278   case OT_NULL:
    00279     break;
    00280   default:
    00281     v->Raise_Error(_SC("cannot serialize a %s"),GetTypeName(o));
    00282     return false;
    00283   }
    00284   return true;
    00285 }
    00286
    00287 bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
    00288 {
    00289   SQObjectType t;
    00290   _CHECK_IO(SafeRead(v,read,up,&t,sizeof(SQObjectType)));
    00291   switch(t){
    00292   case OT_STRING:{
    00293     SQInteger len;
    00294     _CHECK_IO(SafeRead(v,read,up,&len,sizeof(SQInteger)));
    00295     _CHECK_IO(SafeRead(v,read,up,_ss(v)->GetScratchPad(rsl(len)),rsl(len)));
    00296     o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len);
    00297            }
    00298     break;
    00299   case OT_INTEGER:{
    00300     SQInteger i;
    00301     _CHECK_IO(SafeRead(v,read,up,&i,sizeof(SQInteger))); o = i; break;
    00302           }
    00303   case OT_FLOAT:{
    00304     SQFloat f;
    00305     _CHECK_IO(SafeRead(v,read,up,&f,sizeof(SQFloat))); o = f; break;
    00306           }
    00307   case OT_NULL:
    00308     o=_null_;
    00309     break;
    00310   default:
    00311     v->Raise_Error(_SC("cannot serialize a %s"),IdType2Name(t));
    00312     return false;
    00313   }
    00314   return true;
    00315 }
    00316
    00317 bool SQClosure::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
    00318 {
    00319   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_HEAD));
    00320   _CHECK_IO(WriteTag(v,write,up,sizeof(SQChar)));
    00321   _CHECK_IO(_funcproto(_function)->Save(v,up,write));
    00322   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_TAIL));
    00323   return true;
    00324 }
    00325
    00326 bool SQClosure::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
    00327 {
    00328   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_HEAD));
    00329   _CHECK_IO(CheckTag(v,read,up,sizeof(SQChar)));
    00330   SQObjectPtr func;
    00331   _CHECK_IO(SQFunctionProto::Load(v,up,read,func));
    00332   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_TAIL));
    00333   ret = SQClosure::Create(_ss(v),_funcproto(func));
    00334   return true;
    00335 }
    00336
    00337 bool SQFunctionProto::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
    00338 {
    00339   SQInteger i,nliterals = _nliterals,nparameters = _nparameters;
    00340   SQInteger noutervalues = _noutervalues,nlocalvarinfos = _nlocalvarinfos;
    00341   SQInteger nlineinfos=_nlineinfos,ninstructions = _ninstructions,nfunctions=_nfunctions;
    00342   SQInteger ndefaultparams = _ndefaultparams;
    00343   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00344   _CHECK_IO(WriteObject(v,up,write,_sourcename));
    00345   _CHECK_IO(WriteObject(v,up,write,_name));
    00346   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00347   _CHECK_IO(SafeWrite(v,write,up,&nliterals,sizeof(nliterals)));
    00348   _CHECK_IO(SafeWrite(v,write,up,&nparameters,sizeof(nparameters)));
    00349   _CHECK_IO(SafeWrite(v,write,up,&noutervalues,sizeof(noutervalues)));
    00350   _CHECK_IO(SafeWrite(v,write,up,&nlocalvarinfos,sizeof(nlocalvarinfos)));
    00351   _CHECK_IO(SafeWrite(v,write,up,&nlineinfos,sizeof(nlineinfos)));
    00352   _CHECK_IO(SafeWrite(v,write,up,&ndefaultparams,sizeof(ndefaultparams)));
    00353   _CHECK_IO(SafeWrite(v,write,up,&ninstructions,sizeof(ninstructions)));
    00354   _CHECK_IO(SafeWrite(v,write,up,&nfunctions,sizeof(nfunctions)));
    00355   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00356   for(i=0;i<nliterals;i++){
    00357     _CHECK_IO(WriteObject(v,up,write,_literals[i]));
    00358   }
    00359
    00360   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00361   for(i=0;i<nparameters;i++){
    00362     _CHECK_IO(WriteObject(v,up,write,_parameters[i]));
    00363   }
    00364
    00365   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00366   for(i=0;i<noutervalues;i++){
    00367     _CHECK_IO(SafeWrite(v,write,up,&_outervalues[i]._type,sizeof(SQUnsignedInteger)));
    00368     _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._src));
    00369     _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._name));
    00370   }
    00371
    00372   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00373   for(i=0;i<nlocalvarinfos;i++){
    00374     SQLocalVarInfo &lvi=_localvarinfos[i];
    00375     _CHECK_IO(WriteObject(v,up,write,lvi._name));
    00376     _CHECK_IO(SafeWrite(v,write,up,&lvi._pos,sizeof(SQUnsignedInteger)));
    00377     _CHECK_IO(SafeWrite(v,write,up,&lvi._start_op,sizeof(SQUnsignedInteger)));
    00378     _CHECK_IO(SafeWrite(v,write,up,&lvi._end_op,sizeof(SQUnsignedInteger)));
    00379   }
    00380
    00381   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00382   _CHECK_IO(SafeWrite(v,write,up,_lineinfos,sizeof(SQLineInfo)*nlineinfos));
    00383
    00384   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00385   _CHECK_IO(SafeWrite(v,write,up,_defaultparams,sizeof(SQInteger)*ndefaultparams));
    00386
    00387   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00388   _CHECK_IO(SafeWrite(v,write,up,_instructions,sizeof(SQInstruction)*ninstructions));
    00389
    00390   _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
    00391   for(i=0;i<nfunctions;i++){
    00392     _CHECK_IO(_funcproto(_functions[i])->Save(v,up,write));
    00393   }
    00394   _CHECK_IO(SafeWrite(v,write,up,&_stacksize,sizeof(_stacksize)));
    00395   _CHECK_IO(SafeWrite(v,write,up,&_bgenerator,sizeof(_bgenerator)));
    00396   _CHECK_IO(SafeWrite(v,write,up,&_varparams,sizeof(_varparams)));
    00397   return true;
    00398 }
    00399
    00400 bool SQFunctionProto::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
    00401 {
    00402   SQInteger i, nliterals,nparameters;
    00403   SQInteger noutervalues ,nlocalvarinfos ;
    00404   SQInteger nlineinfos,ninstructions ,nfunctions,ndefaultparams ;
    00405   SQObjectPtr sourcename, name;
    00406   SQObjectPtr o;
    00407   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00408   _CHECK_IO(ReadObject(v, up, read, sourcename));
    00409   _CHECK_IO(ReadObject(v, up, read, name));
    00410
    00411   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00412   _CHECK_IO(SafeRead(v,read,up, &nliterals, sizeof(nliterals)));
    00413   _CHECK_IO(SafeRead(v,read,up, &nparameters, sizeof(nparameters)));
    00414   _CHECK_IO(SafeRead(v,read,up, &noutervalues, sizeof(noutervalues)));
    00415   _CHECK_IO(SafeRead(v,read,up, &nlocalvarinfos, sizeof(nlocalvarinfos)));
    00416   _CHECK_IO(SafeRead(v,read,up, &nlineinfos, sizeof(nlineinfos)));
    00417   _CHECK_IO(SafeRead(v,read,up, &ndefaultparams, sizeof(ndefaultparams)));
    00418   _CHECK_IO(SafeRead(v,read,up, &ninstructions, sizeof(ninstructions)));
    00419   _CHECK_IO(SafeRead(v,read,up, &nfunctions, sizeof(nfunctions)));
    00420
    00421
    00422   SQFunctionProto *f = SQFunctionProto::Create(ninstructions,nliterals,nparameters,
    00423       nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams);
    00424   SQObjectPtr proto = f; //gets a ref in case of failure
    00425   f->_sourcename = sourcename;
    00426   f->_name = name;
    00427
    00428   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00429
    00430   for(i = 0;i < nliterals; i++){
    00431     _CHECK_IO(ReadObject(v, up, read, o));
    00432     f->_literals[i] = o;
    00433   }
    00434   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00435
    00436   for(i = 0; i < nparameters; i++){
    00437     _CHECK_IO(ReadObject(v, up, read, o));
    00438     f->_parameters[i] = o;
    00439   }
    00440   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00441
    00442   for(i = 0; i < noutervalues; i++){
    00443     SQUnsignedInteger type;
    00444     SQObjectPtr name;
    00445     _CHECK_IO(SafeRead(v,read,up, &type, sizeof(SQUnsignedInteger)));
    00446     _CHECK_IO(ReadObject(v, up, read, o));
    00447     _CHECK_IO(ReadObject(v, up, read, name));
    00448     f->_outervalues[i] = SQOuterVar(name,o, (SQOuterType)type);
    00449   }
    00450   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00451
    00452   for(i = 0; i < nlocalvarinfos; i++){
    00453     SQLocalVarInfo lvi;
    00454     _CHECK_IO(ReadObject(v, up, read, lvi._name));
    00455     _CHECK_IO(SafeRead(v,read,up, &lvi._pos, sizeof(SQUnsignedInteger)));
    00456     _CHECK_IO(SafeRead(v,read,up, &lvi._start_op, sizeof(SQUnsignedInteger)));
    00457     _CHECK_IO(SafeRead(v,read,up, &lvi._end_op, sizeof(SQUnsignedInteger)));
    00458     f->_localvarinfos[i] = lvi;
    00459   }
    00460   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00461   _CHECK_IO(SafeRead(v,read,up, f->_lineinfos, sizeof(SQLineInfo)*nlineinfos));
    00462
    00463   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00464   _CHECK_IO(SafeRead(v,read,up, f->_defaultparams, sizeof(SQInteger)*ndefaultparams));
    00465
    00466   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00467   _CHECK_IO(SafeRead(v,read,up, f->_instructions, sizeof(SQInstruction)*ninstructions));
    00468
    00469   _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
    00470   for(i = 0; i < nfunctions; i++){
    00471     _CHECK_IO(_funcproto(o)->Load(v, up, read, o));
    00472     f->_functions[i] = o;
    00473   }
    00474   _CHECK_IO(SafeRead(v,read,up, &f->_stacksize, sizeof(f->_stacksize)));
    00475   _CHECK_IO(SafeRead(v,read,up, &f->_bgenerator, sizeof(f->_bgenerator)));
    00476   _CHECK_IO(SafeRead(v,read,up, &f->_varparams, sizeof(f->_varparams)));
    00477
    00478   ret = f;
    00479   return true;
    00480 }
    00481
    00482 #ifndef NO_GARBAGE_COLLECTOR
    00483
    00484 #define START_MARK()  if(!(_uiRef&MARK_FLAG)){ \
    00485     _uiRef|=MARK_FLAG;
    00486
    00487 #define END_MARK() RemoveFromChain(&_sharedstate->_gc_chain, this); \
    00488     AddToChain(chain, this); }
    00489
    00490 void SQVM::Mark(SQCollectable **chain)
    00491 {
    00492   START_MARK()
    00493     SQSharedState::MarkObject(_lasterror,chain);
    00494     SQSharedState::MarkObject(_errorhandler,chain);
    00495     SQSharedState::MarkObject(_debughook,chain);
    00496     SQSharedState::MarkObject(_roottable, chain);
    00497     SQSharedState::MarkObject(temp_reg, chain);
    00498     for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
    00499     for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
    00500     for(SQInteger k = 0; k < _callsstacksize; k++) SQSharedState::MarkObject(_callsstack[k]._closure, chain);
    00501   END_MARK()
    00502 }
    00503
    00504 void SQArray::Mark(SQCollectable **chain)
    00505 {
    00506   START_MARK()
    00507     SQInteger len = _values.size();
    00508     for(SQInteger i = 0;i < len; i++) SQSharedState::MarkObject(_values[i], chain);
    00509   END_MARK()
    00510 }
    00511 void SQTable::Mark(SQCollectable **chain)
    00512 {
    00513   START_MARK()
    00514     if(_delegate) _delegate->Mark(chain);
    00515     SQInteger len = _numofnodes;
    00516     for(SQInteger i = 0; i < len; i++){
    00517       SQSharedState::MarkObject(_nodes[i].key, chain);
    00518       SQSharedState::MarkObject(_nodes[i].val, chain);
    00519     }
    00520   END_MARK()
    00521 }
    00522
    00523 void SQClass::Mark(SQCollectable **chain)
    00524 {
    00525   START_MARK()
    00526     _members->Mark(chain);
    00527     if(_base) _base->Mark(chain);
    00528     SQSharedState::MarkObject(_attributes, chain);
    00529     for(SQUnsignedInteger i =0; i< _defaultvalues.size(); i++) {
    00530       SQSharedState::MarkObject(_defaultvalues[i].val, chain);
    00531       SQSharedState::MarkObject(_defaultvalues[i].attrs, chain);
    00532     }
    00533     for(SQUnsignedInteger j =0; j< _methods.size(); j++) {
    00534       SQSharedState::MarkObject(_methods[j].val, chain);
    00535       SQSharedState::MarkObject(_methods[j].attrs, chain);
    00536     }
    00537     for(SQUnsignedInteger k =0; k< _metamethods.size(); k++) {
    00538       SQSharedState::MarkObject(_metamethods[k], chain);
    00539     }
    00540   END_MARK()
    00541 }
    00542
    00543 void SQInstance::Mark(SQCollectable **chain)
    00544 {
    00545   START_MARK()
    00546     _class->Mark(chain);
    00547     SQUnsignedInteger nvalues = _class->_defaultvalues.size();
    00548     for(SQUnsignedInteger i =0; i< nvalues; i++) {
    00549       SQSharedState::MarkObject(_values[i], chain);
    00550     }
    00551   END_MARK()
    00552 }
    00553
    00554 void SQGenerator::Mark(SQCollectable **chain)
    00555 {
    00556   START_MARK()
    00557     for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
    00558     for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
    00559     SQSharedState::MarkObject(_closure, chain);
    00560   END_MARK()
    00561 }
    00562
    00563 void SQClosure::Mark(SQCollectable **chain)
    00564 {
    00565   START_MARK()
    00566     for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
    00567     for(SQUnsignedInteger i = 0; i < _defaultparams.size(); i++) SQSharedState::MarkObject(_defaultparams[i], chain);
    00568   END_MARK()
    00569 }
    00570
    00571 void SQNativeClosure::Mark(SQCollectable **chain)
    00572 {
    00573   START_MARK()
    00574     for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
    00575   END_MARK()
    00576 }
    00577
    00578 void SQUserData::Mark(SQCollectable **chain){
    00579   START_MARK()
    00580     if(_delegate) _delegate->Mark(chain);
    00581   END_MARK()
    00582 }
    00583
    00584 void SQCollectable::UnMark() { _uiRef&=~MARK_FLAG; }
    00585
    00586 #endif
    00587