1 /* see copyright notice in squirrel.h */ 2 #ifndef _SQCLOSURE_H_ 3 #define _SQCLOSURE_H_ 4 5 struct SQFunctionProto; 6 7 struct SQClosure : public CHAINABLE_OBJ 8 { 9 private: SQClosureSQClosure10 SQClosure(SQSharedState *ss,SQFunctionProto *func){_function=func; INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);} 11 public: CreateSQClosure12 static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func){ 13 SQClosure *nc=(SQClosure*)SQ_MALLOC(sizeof(SQClosure)); 14 new (nc) SQClosure(ss,func); 15 return nc; 16 } ReleaseSQClosure17 void Release(){ 18 sq_delete(this,SQClosure); 19 } ~SQClosureSQClosure20 virtual ~SQClosure() 21 { 22 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); 23 } 24 bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write); 25 bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read); 26 #ifndef NO_GARBAGE_COLLECTOR 27 void Mark(SQCollectable **chain); FinalizeSQClosure28 void Finalize(){_outervalues.resize(0); } 29 #endif 30 SQObjectPtr _function; 31 SQObjectPtrVec _outervalues; 32 }; 33 ////////////////////////////////////////////// 34 struct SQGenerator : public CHAINABLE_OBJ 35 { 36 enum SQGeneratorState{eRunning,eSuspended,eDead}; 37 private: SQGeneratorSQGenerator38 SQGenerator(SQSharedState *ss,SQClosure *closure){_closure=closure;_state=eRunning;_ci._generator=_null_;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);} 39 public: CreateSQGenerator40 static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){ 41 SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator)); 42 new (nc) SQGenerator(ss,closure); 43 return nc; 44 } ~SQGeneratorSQGenerator45 virtual ~SQGenerator() 46 { 47 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); 48 } KillSQGenerator49 void Kill(){ 50 _state=eDead; 51 _stack.resize(0); 52 _closure=_null_;} ReleaseSQGenerator53 void Release(){ 54 sq_delete(this,SQGenerator); 55 } 56 bool Yield(SQVM *v); 57 bool Resume(SQVM *v,SQInteger target); 58 #ifndef NO_GARBAGE_COLLECTOR 59 void Mark(SQCollectable **chain); FinalizeSQGenerator60 void Finalize(){_stack.resize(0);_closure=_null_;} 61 #endif 62 SQObjectPtr _closure; 63 SQObjectPtrVec _stack; 64 SQObjectPtrVec _vargsstack; 65 SQVM::CallInfo _ci; 66 ExceptionsTraps _etraps; 67 SQGeneratorState _state; 68 }; 69 70 struct SQNativeClosure : public CHAINABLE_OBJ 71 { 72 private: SQNativeClosureSQNativeClosure73 SQNativeClosure(SQSharedState *ss,SQFUNCTION func){_function=func;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); } 74 public: CreateSQNativeClosure75 static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func) 76 { 77 SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(sizeof(SQNativeClosure)); 78 new (nc) SQNativeClosure(ss,func); 79 return nc; 80 } ~SQNativeClosureSQNativeClosure81 virtual ~SQNativeClosure() 82 { 83 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); 84 } ReleaseSQNativeClosure85 void Release(){ 86 sq_delete(this,SQNativeClosure); 87 } 88 #ifndef NO_GARBAGE_COLLECTOR 89 void Mark(SQCollectable **chain); FinalizeSQNativeClosure90 void Finalize(){_outervalues.resize(0);} 91 #endif 92 SQFUNCTION _function; 93 SQObjectPtr _name; 94 SQObjectPtrVec _outervalues; 95 SQIntVec _typecheck; 96 SQInteger _nparamscheck; 97 }; 98 99 100 101 #endif //_SQCLOSURE_H_ 102