moved back to old acc
[vox.git] / src / core / closure.hpp
blobed70f495f0e16f53d31da9050a55d51d18d09e85
2 #ifndef _VXCLOSURE_H_
3 #define _VXCLOSURE_H_
5 #include "pcheader.hpp"
6 #include "funcproto.hpp"
7 #include "object.hpp"
8 #include "state.hpp"
10 #define _CALC_CLOSURE_SIZE(func) \
11 (sizeof(VXForeignClosureObj) + (func->_noutervalues*sizeof(VXObject)) + (func->_ndefaultparams*sizeof(VXObject)))
13 //struct VXFuncProtoObj;
14 //struct VXClassObj;
16 struct VXForeignClosureObj : public VXCollectable
18 private:
19 VXForeignClosureObj(VXSharedState *ss,VXFuncProtoObj *func)
21 _function = func;
22 __ObjAddRef(_function);
23 _base = NULL;
24 INIT_CHAIN();
25 ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);
26 _env = NULL;
29 public:
30 static VXForeignClosureObj* Create(VXSharedState *ss,VXFuncProtoObj *func)
32 VXInteger size = _CALC_CLOSURE_SIZE(func);
33 VXForeignClosureObj *nc=(VXForeignClosureObj*)VX_MALLOC(size);
34 new (nc) VXForeignClosureObj(ss,func);
35 nc->_outervalues = (VXObject *)(nc + 1);
36 nc->_defaultparams = &nc->_outervalues[func->_noutervalues];
37 VX_CONSTRUCT_VECTOR(VXObject,func->_noutervalues,nc->_outervalues);
38 VX_CONSTRUCT_VECTOR(VXObject,func->_ndefaultparams,nc->_defaultparams);
39 return nc;
41 void Release()
43 VXFuncProtoObj *f = _function;
44 VXInteger size = _CALC_CLOSURE_SIZE(f);
45 VX_DESTRUCT_VECTOR(VXObject,f->_noutervalues,_outervalues);
46 VX_DESTRUCT_VECTOR(VXObject,f->_ndefaultparams,_defaultparams);
47 __ObjRelease(_function);
48 this->~VXForeignClosureObj();
49 vox_mem_free(this,size);
52 VXForeignClosureObj *Clone()
54 VXFuncProtoObj *f = _function;
55 VXForeignClosureObj * ret = VXForeignClosureObj::Create(_opt_ss(this),f);
56 ret->_env = _env;
57 if(ret->_env) __ObjAddRef(ret->_env);
58 VX_COPY_VECTOR(ret->_outervalues,_outervalues,f->_noutervalues);
59 VX_COPY_VECTOR(ret->_defaultparams,_defaultparams,f->_ndefaultparams);
60 return ret;
62 ~VXForeignClosureObj();
64 bool Save(VXState *v,VXUserPointer up,VXWriteFunc write);
65 static bool Load(VXState *v,VXUserPointer up,VXReadFunc read,VXObject &ret);
66 #ifndef NO_GARBAGE_COLLECTOR
67 void Mark(VXCollectable **chain);
68 void Finalize()
70 VXFuncProtoObj *f = _function;
71 _NULL_VXOBJECT_VECTOR(_outervalues,f->_noutervalues);
72 _NULL_VXOBJECT_VECTOR(_defaultparams,f->_ndefaultparams);
74 VXOType GetType() {return VX_OT_CLOSURE;}
75 #endif
76 VXWeakRefObj *_env;
77 VXClassObj *_base;
78 VXFuncProtoObj *_function;
79 VXObject *_outervalues;
80 VXObject *_defaultparams;
83 //////////////////////////////////////////////
84 struct VXOuterObj : public CHAINABLE_OBJ
87 private:
88 VXOuterObj(VXSharedState *ss, VXObject *outer){_valptr = outer; _next = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); }
90 public:
91 static VXOuterObj *Create(VXSharedState *ss, VXObject *outer)
93 VXOuterObj *nc = (VXOuterObj*)VX_MALLOC(sizeof(VXOuterObj));
94 new (nc) VXOuterObj(ss, outer);
95 return nc;
97 ~VXOuterObj() { REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); }
99 void Release()
101 this->~VXOuterObj();
102 vox_mem_free(this,sizeof(VXOuterObj));
105 #ifndef NO_GARBAGE_COLLECTOR
106 void Mark(VXCollectable **chain);
107 void Finalize() { _value.Null(); }
108 VXOType GetType() {return VX_OT_OUTER;}
109 #endif
111 VXObject *_valptr; /* pointer to value on stack, or _value below */
112 VXInteger _idx; /* idx in stack array, for relocation */
113 VXObject _value; /* value of outer after stack frame is closed */
114 VXOuterObj *_next; /* pointer to next outer when frame is open */
117 //////////////////////////////////////////////
118 struct VXGeneratorObj : public CHAINABLE_OBJ
120 enum VXGeneratorObjState
122 eRunning,
123 eSuspended,
124 eDead
127 private:
128 VXGeneratorObj(VXSharedState *ss,VXForeignClosureObj *closure)
130 _closure = closure;
131 _state = eRunning;
132 _ci._generator = NULL;
133 INIT_CHAIN();
134 ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);
137 public:
138 static VXGeneratorObj *Create(VXSharedState *ss,VXForeignClosureObj *closure)
140 VXGeneratorObj *nc=(VXGeneratorObj*)VX_MALLOC(sizeof(VXGeneratorObj));
141 new (nc) VXGeneratorObj(ss,closure);
142 return nc;
145 ~VXGeneratorObj()
147 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
150 void Kill()
152 _state=eDead;
153 _stack.resize(0);
154 _closure.Null();
157 void Release()
159 vox_delete(this,VXGeneratorObj);
162 bool Yield(VXState *v,VXInteger target);
164 bool Resume(VXState *v,VXObject &dest);
166 void Mark(VXCollectable **chain);
168 void Finalize()
170 _stack.resize(0);
171 _closure.Null();
174 VXOType GetType()
176 return VX_OT_GENERATOR;
179 VXObject _closure;
180 VXObjectVec _stack;
181 VXState::CallInfo _ci;
182 ExceptionsTraps _etraps;
183 VXGeneratorObjState _state;
186 #define _CALC_NATVIVECLOSURE_SIZE(noutervalues) (sizeof(VXNativeClosureObj) + (noutervalues*sizeof(VXObject)))
188 struct VXNativeClosureObj : public CHAINABLE_OBJ
190 private:
191 VXNativeClosureObj(VXSharedState *ss,VXFunction func)
193 _function=func;
194 INIT_CHAIN();
195 ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);
196 _env = NULL;
199 public:
200 static VXNativeClosureObj *Create(VXSharedState *ss,VXFunction func,VXInteger nouters)
202 VXInteger size = _CALC_NATVIVECLOSURE_SIZE(nouters);
203 VXNativeClosureObj *nc=(VXNativeClosureObj*)VX_MALLOC(size);
204 new (nc) VXNativeClosureObj(ss,func);
205 nc->_outervalues = (VXObject *)(nc + 1);
206 nc->_noutervalues = nouters;
207 VX_CONSTRUCT_VECTOR(VXObject,nc->_noutervalues,nc->_outervalues);
208 return nc;
210 VXNativeClosureObj *Clone()
212 VXNativeClosureObj * ret = VXNativeClosureObj::Create(_opt_ss(this),_function,_noutervalues);
213 ret->_env = _env;
214 if(ret->_env) __ObjAddRef(ret->_env);
215 ret->_name = _name;
216 VX_COPY_VECTOR(ret->_outervalues,_outervalues,_noutervalues);
217 ret->_typecheck.copy(_typecheck);
218 ret->_nparamscheck = _nparamscheck;
219 return ret;
221 ~VXNativeClosureObj()
223 __ObjRelease(_env);
224 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
226 void Release()
228 VXInteger size = _CALC_NATVIVECLOSURE_SIZE(_noutervalues);
229 VX_DESTRUCT_VECTOR(VXObject,_noutervalues,_outervalues);
230 this->~VXNativeClosureObj();
231 vox_free(this,size);
234 #ifndef NO_GARBAGE_COLLECTOR
235 void Mark(VXCollectable **chain);
236 void Finalize() { _NULL_VXOBJECT_VECTOR(_outervalues,_noutervalues); }
237 VXOType GetType() {return VX_OT_NATIVECLOSURE;}
238 #endif
239 VXInteger _nparamscheck;
240 VXIntVec _typecheck;
241 VXObject *_outervalues;
242 VXUnsignedInteger _noutervalues;
243 VXWeakRefObj *_env;
244 VXFunction _function;
245 VXObject _name;
247 #endif //_VXCLOSURE_H_