# sqclosure.h

    00001 /*  see copyright notice in squirrel.h */
    00002 #ifndef _SQCLOSURE_H_
    00003 #define _SQCLOSURE_H_
    00004
    00005 struct SQFunctionProto;
    00006
    00007 struct SQClosure : public CHAINABLE_OBJ
    00008 {
    00009 private:
    00010   SQClosure(SQSharedState *ss,SQFunctionProto *func){_function=func; INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
    00011 public:
    00012   static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func){
    00013     SQClosure *nc=(SQClosure*)SQ_MALLOC(sizeof(SQClosure));
    00014     new (nc) SQClosure(ss,func);
    00015     return nc;
    00016   }
    00017   void Release(){
    00018     sq_delete(this,SQClosure);
    00019   }
    00020   SQClosure *Clone()
    00021   {
    00022     SQClosure * ret = SQClosure::Create(_opt_ss(this),_funcproto(_function));
    00023     ret->_env = _env;
    00024     ret->_outervalues.copy(_outervalues);
    00025     ret->_defaultparams.copy(_defaultparams);
    00026     return ret;
    00027   }
    00028   ~SQClosure()
    00029   {
    00030     REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
    00031   }
    00032   bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
    00033   static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
    00034 #ifndef NO_GARBAGE_COLLECTOR
    00035   void Mark(SQCollectable **chain);
    00036   void Finalize(){_outervalues.resize(0); }
    00037 #endif
    00038   SQObjectPtr _env;
    00039   SQObjectPtr _function;
    00040   SQObjectPtrVec _outervalues;
    00041   SQObjectPtrVec _defaultparams;
    00042 };
    00044 struct SQGenerator : public CHAINABLE_OBJ
    00045 {
    00046   enum SQGeneratorState{eRunning,eSuspended,eDead};
    00047 private:
    00048   SQGenerator(SQSharedState *ss,SQClosure *closure){_closure=closure;_state=eRunning;_ci._generator=NULL;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
    00049 public:
    00050   static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){
    00051     SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator));
    00052     new (nc) SQGenerator(ss,closure);
    00053     return nc;
    00054   }
    00055   ~SQGenerator()
    00056   {
    00057     REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
    00058   }
    00059     void Kill(){
    00060     _state=eDead;
    00061     _stack.resize(0);
    00062     _closure=_null_;}
    00063   void Release(){
    00064     sq_delete(this,SQGenerator);
    00065   }
    00066   bool Yield(SQVM *v);
    00067   bool Resume(SQVM *v,SQInteger target);
    00068 #ifndef NO_GARBAGE_COLLECTOR
    00069   void Mark(SQCollectable **chain);
    00070   void Finalize(){_stack.resize(0);_closure=_null_;}
    00071 #endif
    00072   SQObjectPtr _closure;
    00073   SQObjectPtrVec _stack;
    00074   SQObjectPtrVec _vargsstack;
    00075   SQVM::CallInfo _ci;
    00076   ExceptionsTraps _etraps;
    00077   SQGeneratorState _state;
    00078 };
    00079
    00080 struct SQNativeClosure : public CHAINABLE_OBJ
    00081 {
    00082 private:
    00083   SQNativeClosure(SQSharedState *ss,SQFUNCTION func){_function=func;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);  }
    00084 public:
    00085   static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func)
    00086   {
    00087     SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(sizeof(SQNativeClosure));
    00088     new (nc) SQNativeClosure(ss,func);
    00089     return nc;
    00090   }
    00091   SQNativeClosure *Clone()
    00092   {
    00093     SQNativeClosure * ret = SQNativeClosure::Create(_opt_ss(this),_function);
    00094     ret->_env = _env;
    00095     ret->_name = _name;
    00096     ret->_outervalues.copy(_outervalues);
    00097     ret->_typecheck.copy(_typecheck);
    00098     ret->_nparamscheck = _nparamscheck;
    00099     return ret;
    00100   }
    00101   ~SQNativeClosure()
    00102   {
    00103     REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
    00104   }
    00105   void Release(){
    00106     sq_delete(this,SQNativeClosure);
    00107   }
    00108 #ifndef NO_GARBAGE_COLLECTOR
    00109   void Mark(SQCollectable **chain);
    00110   void Finalize(){_outervalues.resize(0);}
    00111 #endif
    00112   SQInteger _nparamscheck;
    00113   SQIntVec _typecheck;
    00114   SQObjectPtrVec _outervalues;
    00115   SQObjectPtr _env;
    00116   SQFUNCTION _function;
    00117   SQObjectPtr _name;
    00118 };
    00119
    00120
    00121
    00122 #endif //_SQCLOSURE_H_