5 #include "pcheader.hpp"
6 #include "funcproto.hpp"
10 #define _CALC_CLOSURE_SIZE(func) \
11 (sizeof(VXForeignClosureObj) + (func->_noutervalues*sizeof(VXObject)) + (func->_ndefaultparams*sizeof(VXObject)))
13 //struct VXFuncProtoObj;
16 struct VXForeignClosureObj
: public VXCollectable
19 VXForeignClosureObj(VXSharedState
*ss
,VXFuncProtoObj
*func
)
22 __ObjAddRef(_function
);
25 ADD_TO_CHAIN(&_ss(this)->_gc_chain
,this);
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
);
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
);
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
);
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
);
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
;}
78 VXFuncProtoObj
*_function
;
79 VXObject
*_outervalues
;
80 VXObject
*_defaultparams
;
83 //////////////////////////////////////////////
84 struct VXOuterObj
: public CHAINABLE_OBJ
88 VXOuterObj(VXSharedState
*ss
, VXObject
*outer
){_valptr
= outer
; _next
= NULL
; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain
,this); }
91 static VXOuterObj
*Create(VXSharedState
*ss
, VXObject
*outer
)
93 VXOuterObj
*nc
= (VXOuterObj
*)VX_MALLOC(sizeof(VXOuterObj
));
94 new (nc
) VXOuterObj(ss
, outer
);
97 ~VXOuterObj() { REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain
,this); }
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
;}
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
128 VXGeneratorObj(VXSharedState
*ss
,VXForeignClosureObj
*closure
)
132 _ci
._generator
= NULL
;
134 ADD_TO_CHAIN(&_ss(this)->_gc_chain
,this);
138 static VXGeneratorObj
*Create(VXSharedState
*ss
,VXForeignClosureObj
*closure
)
140 VXGeneratorObj
*nc
=(VXGeneratorObj
*)VX_MALLOC(sizeof(VXGeneratorObj
));
141 new (nc
) VXGeneratorObj(ss
,closure
);
147 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain
,this);
159 vox_delete(this,VXGeneratorObj
);
162 bool Yield(VXState
*v
,VXInteger target
);
164 bool Resume(VXState
*v
,VXObject
&dest
);
166 void Mark(VXCollectable
**chain
);
176 return VX_OT_GENERATOR
;
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
191 VXNativeClosureObj(VXSharedState
*ss
,VXFunction func
)
195 ADD_TO_CHAIN(&_ss(this)->_gc_chain
,this);
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
);
210 VXNativeClosureObj
*Clone()
212 VXNativeClosureObj
* ret
= VXNativeClosureObj::Create(_opt_ss(this),_function
,_noutervalues
);
214 if(ret
->_env
) __ObjAddRef(ret
->_env
);
216 VX_COPY_VECTOR(ret
->_outervalues
,_outervalues
,_noutervalues
);
217 ret
->_typecheck
.copy(_typecheck
);
218 ret
->_nparamscheck
= _nparamscheck
;
221 ~VXNativeClosureObj()
224 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain
,this);
228 VXInteger size
= _CALC_NATVIVECLOSURE_SIZE(_noutervalues
);
229 VX_DESTRUCT_VECTOR(VXObject
,_noutervalues
,_outervalues
);
230 this->~VXNativeClosureObj();
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
;}
239 VXInteger _nparamscheck
;
241 VXObject
*_outervalues
;
242 VXUnsignedInteger _noutervalues
;
244 VXFunction _function
;
247 #endif //_VXCLOSURE_H_