ScummVM API documentation
sqclosure.h
1 /* see copyright notice in squirrel.h */
2 #ifndef _SQCLOSURE_H_
3 #define _SQCLOSURE_H_
4 
5 
6 #define _CALC_CLOSURE_SIZE(func) (sizeof(SQClosure) + (func->_noutervalues*sizeof(SQObjectPtr)) + (func->_ndefaultparams*sizeof(SQObjectPtr)))
7 
8 struct SQFunctionProto;
9 struct SQClass;
10 struct SQClosure : public CHAINABLE_OBJ
11 {
12 private:
13  SQClosure(SQSharedState *ss,SQFunctionProto *func){_function = func; __ObjAddRef(_function); _base = NULL; INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL; _root=NULL;}
14 public:
15  static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func,SQWeakRef *root){
16  SQInteger size = _CALC_CLOSURE_SIZE(func);
17  SQClosure *nc=(SQClosure*)SQ_MALLOC(size);
18  new (nc) SQClosure(ss,func);
19  nc->_outervalues = (SQObjectPtr *)(nc + 1);
20  nc->_defaultparams = &nc->_outervalues[func->_noutervalues];
21  nc->_root = root;
22  __ObjAddRef(nc->_root);
23  _CONSTRUCT_VECTOR(SQObjectPtr,func->_noutervalues,nc->_outervalues);
24  _CONSTRUCT_VECTOR(SQObjectPtr,func->_ndefaultparams,nc->_defaultparams);
25  return nc;
26  }
27  void Release(){
28  SQFunctionProto *f = _function;
29  SQInteger size = _CALC_CLOSURE_SIZE(f);
30  _DESTRUCT_VECTOR(SQObjectPtr,f->_noutervalues,_outervalues);
31  _DESTRUCT_VECTOR(SQObjectPtr,f->_ndefaultparams,_defaultparams);
32  __ObjRelease(_function);
33  this->~SQClosure();
34  sq_vm_free(this,size);
35  }
36  void SetRoot(SQWeakRef *r)
37  {
38  __ObjRelease(_root);
39  _root = r;
40  __ObjAddRef(_root);
41  }
42  SQClosure *Clone()
43  {
44  SQFunctionProto *f = _function;
45  SQClosure * ret = SQClosure::Create(_opt_ss(this),f,_root);
46  ret->_env = _env;
47  if(ret->_env) __ObjAddRef(ret->_env);
48  _COPY_VECTOR(ret->_outervalues,_outervalues,f->_noutervalues);
49  _COPY_VECTOR(ret->_defaultparams,_defaultparams,f->_ndefaultparams);
50  return ret;
51  }
52  ~SQClosure();
53 
54  bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
55  static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
56 #ifndef NO_GARBAGE_COLLECTOR
57  void Mark(SQCollectable **chain);
58  void Finalize(){
59  SQFunctionProto *f = _function;
60  _NULL_SQOBJECT_VECTOR(_outervalues,f->_noutervalues);
61  _NULL_SQOBJECT_VECTOR(_defaultparams,f->_ndefaultparams);
62  }
63  SQObjectType GetType() {return OT_CLOSURE;}
64 #endif
65  SQWeakRef *_env;
66  SQWeakRef *_root;
67  SQClass *_base;
68  SQFunctionProto *_function;
69  SQObjectPtr *_outervalues = nullptr;
70  SQObjectPtr *_defaultparams = nullptr;
71 };
72 
74 struct SQOuter : public CHAINABLE_OBJ
75 {
76 
77 private:
78  SQOuter(SQSharedState *ss, SQObjectPtr *outer){_valptr = outer; _next = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); }
79 
80 public:
81  static SQOuter *Create(SQSharedState *ss, SQObjectPtr *outer)
82  {
83  SQOuter *nc = (SQOuter*)SQ_MALLOC(sizeof(SQOuter));
84  new (nc) SQOuter(ss, outer);
85  return nc;
86  }
87  ~SQOuter() { REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); }
88 
89  void Release()
90  {
91  this->~SQOuter();
92  sq_vm_free(this,sizeof(SQOuter));
93  }
94 
95 #ifndef NO_GARBAGE_COLLECTOR
96  void Mark(SQCollectable **chain);
97  void Finalize() { _value.Null(); }
98  SQObjectType GetType() {return OT_OUTER;}
99 #endif
100 
101  SQObjectPtr *_valptr; /* pointer to value on stack, or _value below */
102  SQInteger _idx = 0; /* idx in stack array, for relocation */
103  SQObjectPtr _value; /* value of outer after stack frame is closed */
104  SQOuter *_next; /* pointer to next outer when frame is open */
105 };
106 
108 struct SQGenerator : public CHAINABLE_OBJ
109 {
110  enum SQGeneratorState{eRunning,eSuspended,eDead};
111 private:
112  SQGenerator(SQSharedState *ss,SQClosure *closure){_closure=closure;_state=eRunning;_ci._generator=NULL;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
113 public:
114  static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){
115  SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator));
116  new (nc) SQGenerator(ss,closure);
117  return nc;
118  }
119  ~SQGenerator()
120  {
121  REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
122  }
123  void Kill(){
124  _state=eDead;
125  _stack.resize(0);
126  _closure.Null();}
127  void Release(){
128  sq_delete(this,SQGenerator);
129  }
130 
131  bool Yield(SQVM *v,SQInteger target);
132  bool Resume(SQVM *v,SQObjectPtr &dest);
133 #ifndef NO_GARBAGE_COLLECTOR
134  void Mark(SQCollectable **chain);
135  void Finalize(){_stack.resize(0);_closure.Null();}
136  SQObjectType GetType() {return OT_GENERATOR;}
137 #endif
138  SQObjectPtr _closure;
139  SQObjectPtrVec _stack;
140  SQVM::CallInfo _ci;
141  ExceptionsTraps _etraps;
142  SQGeneratorState _state;
143 };
144 
145 #define _CALC_NATVIVECLOSURE_SIZE(noutervalues) (sizeof(SQNativeClosure) + (noutervalues*sizeof(SQObjectPtr)))
146 
147 struct SQNativeClosure : public CHAINABLE_OBJ
148 {
149 private:
150  SQNativeClosure(SQSharedState *ss,SQFUNCTION func){_function=func;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL;}
151 public:
152  static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func,SQInteger nouters)
153  {
154  SQInteger size = _CALC_NATVIVECLOSURE_SIZE(nouters);
155  SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(size);
156  new (nc) SQNativeClosure(ss,func);
157  nc->_outervalues = (SQObjectPtr *)(nc + 1);
158  nc->_noutervalues = nouters;
159  _CONSTRUCT_VECTOR(SQObjectPtr,nc->_noutervalues,nc->_outervalues);
160  return nc;
161  }
162  SQNativeClosure *Clone()
163  {
164  SQNativeClosure * ret = SQNativeClosure::Create(_opt_ss(this),_function,_noutervalues);
165  ret->_env = _env;
166  if(ret->_env) __ObjAddRef(ret->_env);
167  ret->_name = _name;
168  _COPY_VECTOR(ret->_outervalues,_outervalues,_noutervalues);
169  ret->_typecheck.copy(_typecheck);
170  ret->_nparamscheck = _nparamscheck;
171  return ret;
172  }
173  ~SQNativeClosure()
174  {
175  __ObjRelease(_env);
176  REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
177  }
178  void Release(){
179  SQInteger size = _CALC_NATVIVECLOSURE_SIZE(_noutervalues);
180  _DESTRUCT_VECTOR(SQObjectPtr,_noutervalues,_outervalues);
181  this->~SQNativeClosure();
182  sq_free(this,size);
183  }
184 
185 #ifndef NO_GARBAGE_COLLECTOR
186  void Mark(SQCollectable **chain);
187  void Finalize() { _NULL_SQOBJECT_VECTOR(_outervalues,_noutervalues); }
188  SQObjectType GetType() {return OT_NATIVECLOSURE;}
189 #endif
190  SQInteger _nparamscheck = 0;
191  SQIntVec _typecheck;
192  SQObjectPtr *_outervalues = nullptr;
193  SQUnsignedInteger _noutervalues = 0;
194  SQWeakRef *_env;
195  SQFUNCTION _function;
196  SQObjectPtr _name;
197 };
198 
199 
200 
201 #endif //_SQCLOSURE_H_
Definition: sqvm.h:35
Definition: sqclosure.h:10
Definition: sqvm.h:33
Definition: sqclass.h:28
Definition: sqclosure.h:74
Definition: sqclosure.h:108
Definition: sqobject.h:98
Definition: sqstate.h:59
Definition: sqobject.h:205
Definition: sqclosure.h:147
Definition: sqfuncproto.h:49
Definition: sqobject.h:313