4 #include "sqpcheader.h" 9 #include "squserdata.h" 10 #include "sqfuncproto.h" 12 #include "sqclosure.h" 17 switch(_RAW_TYPE(type))
19 case _RT_NULL:
return _SC(
"null");
20 case _RT_INTEGER:
return _SC(
"integer");
21 case _RT_FLOAT:
return _SC(
"float");
22 case _RT_BOOL:
return _SC(
"bool");
23 case _RT_STRING:
return _SC(
"string");
24 case _RT_TABLE:
return _SC(
"table");
25 case _RT_ARRAY:
return _SC(
"array");
26 case _RT_GENERATOR:
return _SC(
"generator");
28 case _RT_NATIVECLOSURE:
29 return _SC(
"function");
32 return _SC(
"userdata");
33 case _RT_THREAD:
return _SC(
"thread");
34 case _RT_FUNCPROTO:
return _SC(
"function");
35 case _RT_CLASS:
return _SC(
"class");
36 case _RT_INSTANCE:
return _SC(
"instance");
37 case _RT_WEAKREF:
return _SC(
"weakref");
48 SQString *SQString::Create(SQSharedState *ss,
const SQChar *s,SQInteger len)
50 SQString *str=ADD_STRING(ss,s,len);
55 void SQString::Release()
57 REMOVE_STRING(_sharedstate,
this);
60 SQInteger SQString::Next(
const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
64 outkey = (SQInteger)idx;
65 outval = SQInteger(_val[idx]);
79 return (SQUnsignedInteger)_integer(idx);
80 default: assert(0);
break;
85 SQWeakRef *SQRefCounted::GetWeakRef(SQObjectType type)
88 sq_new(_weakref,SQWeakRef);
89 _weakref->_obj._type = type;
90 _weakref->_obj._unVal.pRefCounted =
this;
95 SQRefCounted::~SQRefCounted()
98 _weakref->_obj._type = OT_NULL;
99 _weakref->_obj._unVal.pRefCounted =
NULL;
103 void SQWeakRef::Release() {
104 if(ISREFCOUNTED(_obj._type)) {
105 _obj._unVal.pRefCounted->_weakref =
NULL;
107 sq_delete(
this,SQWeakRef);
110 bool SQDelegable::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res) {
112 return _delegate->Get((*_ss(v)->_metamethods)[mm],res);
117 bool SQDelegable::SetDelegate(SQTable *mt)
120 if(temp ==
this)
return false;
122 if (temp->_delegate ==
this)
return false;
123 temp = temp->_delegate;
125 if (mt) __ObjAddRef(mt);
126 __ObjRelease(_delegate);
131 bool SQGenerator::Yield(SQVM *v)
133 if(_state==eSuspended) { v->Raise_Error(_SC(
"internal vm error, yielding dead generator"));
return false;}
134 if(_state==eDead) { v->Raise_Error(_SC(
"internal vm error, yielding a dead generator"));
return false; }
135 SQInteger size = v->_top-v->_stackbase;
138 for(SQInteger n =0; n<size; n++) {
139 _stack._vals[n] = v->_stack[v->_stackbase+n];
140 v->_stack[v->_stackbase+n] =
_null_;
142 SQInteger nvargs = v->ci->_vargs.size;
143 SQInteger vargsbase = v->ci->_vargs.base;
144 for(SQInteger j = nvargs - 1; j >= 0; j--) {
145 _vargsstack.push_back(v->_vargsstack[vargsbase+j]);
148 for(SQInteger i=0;i<_ci._etraps;i++) {
149 _etraps.push_back(v->_etraps.top());
150 v->_etraps.pop_back();
156 bool SQGenerator::Resume(SQVM *v,SQInteger target)
158 SQInteger size=_stack.size();
159 if(_state==eDead){ v->Raise_Error(_SC(
"resuming dead generator"));
return false; }
160 if(_state==eRunning){ v->Raise_Error(_SC(
"resuming active generator"));
return false; }
161 SQInteger prevtop=v->_top-v->_stackbase;
162 PUSH_CALLINFO(v,_ci);
163 SQInteger oldstackbase=v->_stackbase;
164 v->_stackbase = v->_top;
165 v->ci->_target = (SQInt32)target;
166 v->ci->_generator =
this;
167 v->ci->_vargs.size = (
unsigned short)_vargsstack.size();
169 for(SQInteger i=0;i<_ci._etraps;i++) {
170 v->_etraps.push_back(_etraps.top());
173 for(SQInteger n =0; n<size; n++) {
174 v->_stack[v->_stackbase+n] = _stack._vals[n];
177 while(_vargsstack.size()) {
178 v->_vargsstack.push_back(_vargsstack.back());
179 _vargsstack.pop_back();
181 v->ci->_vargs.base = (
unsigned short)(v->_vargsstack.size() - v->ci->_vargs.size);
182 v->_top=v->_stackbase+size;
183 v->ci->_prevtop = (SQInt32)prevtop;
184 v->ci->_prevstkbase = (SQInt32)(v->_stackbase - oldstackbase);
186 if (type(v->_debughook) != OT_NULL && _rawval(v->_debughook) != _rawval(v->ci->_closure))
187 v->CallDebugHook(_SC(
'c'));
192 void SQArray::Extend(
const SQArray *a){
195 for(SQInteger i=0;i<xlen;i++)
196 Append(a->_values[i]);
199 const SQChar* SQFunctionProto::GetLocal(SQVM *vm,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop)
201 SQUnsignedInteger nvars=_nlocalvarinfos;
204 for(SQUnsignedInteger i=0;i<nvars;i++){
205 if(_localvarinfos[i]._start_op<=nop && _localvarinfos[i]._end_op>=nop)
208 vm->Push(vm->_stack[stackbase+_localvarinfos[i]._pos]);
209 res=_stringval(_localvarinfos[i]._name);
219 SQInteger SQFunctionProto::GetLine(SQInstruction *curr)
221 SQInteger op = (SQInteger)(curr-_instructions);
222 SQInteger line=_lineinfos[0]._line;
223 for(SQInteger i=1;i<_nlineinfos;i++){
224 if(_lineinfos[i]._op>=op)
226 line=_lineinfos[i]._line;
231 #define _CHECK_IO(exp) { if(!exp)return false; } 232 bool SafeWrite(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQUserPointer dest,SQInteger size)
234 if(write(up,dest,size) != size) {
235 v->Raise_Error(_SC(
"io error (write function failure)"));
241 bool SafeRead(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQUserPointer dest,SQInteger size)
243 if(size && read(up,dest,size) != size) {
244 v->Raise_Error(_SC(
"io error, read function failure, the origin stream could be corrupted/trucated"));
250 bool WriteTag(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQInteger tag)
252 return SafeWrite(v,write,up,&tag,
sizeof(tag));
255 bool CheckTag(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQInteger tag)
260 v->Raise_Error(_SC(
"invalid or corrupted closure stream"));
266 bool WriteObject(HSQUIRRELVM v,SQUserPointer up,SQWRITEFUNC write,SQObjectPtr &o)
281 v->Raise_Error(_SC(
"cannot serialize a %s"),
GetTypeName(o));
287 bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
296 o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len);
311 v->Raise_Error(_SC(
"cannot serialize a %s"),
IdType2Name(t));
317 bool SQClosure::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
321 _CHECK_IO(_funcproto(_function)->Save(v,up,write));
326 bool SQClosure::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
331 _CHECK_IO(SQFunctionProto::Load(v,up,read,func));
333 ret = SQClosure::Create(_ss(v),_funcproto(func));
337 bool SQFunctionProto::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
339 SQInteger i,nliterals = _nliterals,nparameters = _nparameters;
340 SQInteger noutervalues = _noutervalues,nlocalvarinfos = _nlocalvarinfos;
341 SQInteger nlineinfos=_nlineinfos,ninstructions = _ninstructions,nfunctions=_nfunctions;
342 SQInteger ndefaultparams = _ndefaultparams;
356 for(i=0;i<nliterals;i++){
361 for(i=0;i<nparameters;i++){
366 for(i=0;i<noutervalues;i++){
373 for(i=0;i<nlocalvarinfos;i++){
374 SQLocalVarInfo &lvi=_localvarinfos[i];
391 for(i=0;i<nfunctions;i++){
392 _CHECK_IO(_funcproto(_functions[i])->Save(v,up,write));
400 bool SQFunctionProto::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
402 SQInteger i, nliterals,nparameters;
403 SQInteger noutervalues ,nlocalvarinfos ;
404 SQInteger nlineinfos,ninstructions ,nfunctions,ndefaultparams ;
405 SQObjectPtr sourcename, name;
422 SQFunctionProto *f = SQFunctionProto::Create(ninstructions,nliterals,nparameters,
423 nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams);
424 SQObjectPtr proto = f;
425 f->_sourcename = sourcename;
430 for(i = 0;i < nliterals; i++){
436 for(i = 0; i < nparameters; i++){
438 f->_parameters[i] = o;
442 for(i = 0; i < noutervalues; i++){
443 SQUnsignedInteger type;
449 f->_outervalues[i] = SQOuterVar(name_,o, (SQOuterType)type);
453 for(i = 0; i < nlocalvarinfos; i++){
459 f->_localvarinfos[i] = lvi;
465 _CHECK_IO(
SafeRead(v,read,up, f->_defaultparams,
sizeof(SQInteger)*ndefaultparams));
468 _CHECK_IO(
SafeRead(v,read,up, f->_instructions,
sizeof(SQInstruction)*ninstructions));
471 for(i = 0; i < nfunctions; i++){
472 _CHECK_IO(_funcproto(o)->Load(v, up, read, o));
473 f->_functions[i] = o;
483 #ifndef NO_GARBAGE_COLLECTOR 485 #define START_MARK() if(!(_uiRef&MARK_FLAG)){ \ 488 #define END_MARK() RemoveFromChain(&_sharedstate->_gc_chain, this); \ 489 AddToChain(chain, this); } 491 void SQVM::Mark(SQCollectable **chain)
494 SQSharedState::MarkObject(_lasterror,chain);
495 SQSharedState::MarkObject(_errorhandler,chain);
496 SQSharedState::MarkObject(_debughook,chain);
497 SQSharedState::MarkObject(_roottable, chain);
498 SQSharedState::MarkObject(temp_reg, chain);
499 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
500 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
501 for(SQInteger k = 0; k < _callsstacksize; k++) SQSharedState::MarkObject(_callsstack[k]._closure, chain);
505 void SQArray::Mark(SQCollectable **chain)
508 SQInteger len = _values.size();
509 for(SQInteger i = 0;i < len; i++) SQSharedState::MarkObject(_values[i], chain);
512 void SQTable::Mark(SQCollectable **chain)
515 if(_delegate) _delegate->Mark(chain);
516 SQInteger len = _numofnodes;
517 for(SQInteger i = 0; i < len; i++){
518 SQSharedState::MarkObject(_nodes[i].key, chain);
519 SQSharedState::MarkObject(_nodes[i].val, chain);
524 void SQClass::Mark(SQCollectable **chain)
527 _members->Mark(chain);
528 if(_base) _base->Mark(chain);
529 SQSharedState::MarkObject(_attributes, chain);
530 for(SQUnsignedInteger i =0; i< _defaultvalues.size(); i++) {
531 SQSharedState::MarkObject(_defaultvalues[i].val, chain);
532 SQSharedState::MarkObject(_defaultvalues[i].attrs, chain);
534 for(SQUnsignedInteger j =0; j< _methods.size(); j++) {
535 SQSharedState::MarkObject(_methods[j].val, chain);
536 SQSharedState::MarkObject(_methods[j].attrs, chain);
538 for(SQUnsignedInteger k =0; k< _metamethods.size(); k++) {
539 SQSharedState::MarkObject(_metamethods[k], chain);
544 void SQInstance::Mark(SQCollectable **chain)
548 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
549 for(SQUnsignedInteger i =0; i< nvalues; i++) {
550 SQSharedState::MarkObject(_values[i], chain);
555 void SQGenerator::Mark(SQCollectable **chain)
558 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
559 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
560 SQSharedState::MarkObject(_closure, chain);
564 void SQClosure::Mark(SQCollectable **chain)
567 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
568 for(SQUnsignedInteger i = 0; i < _defaultparams.size(); i++) SQSharedState::MarkObject(_defaultparams[i], chain);
572 void SQNativeClosure::Mark(SQCollectable **chain)
575 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
579 void SQUserData::Mark(SQCollectable **chain){
581 if(_delegate) _delegate->Mark(chain);
585 void SQCollectable::UnMark() { _uiRef&=~MARK_FLAG; }
bool SafeRead(HSQUIRRELVM v, SQWRITEFUNC read, SQUserPointer up, SQUserPointer dest, SQInteger size)
bool CheckTag(HSQUIRRELVM v, SQWRITEFUNC read, SQUserPointer up, SQInteger tag)
SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx)
bool WriteObject(HSQUIRRELVM v, SQUserPointer up, SQWRITEFUNC write, SQObjectPtr &o)
const SQChar * GetTypeName(const SQObjectPtr &obj1)
bool WriteTag(HSQUIRRELVM v, SQWRITEFUNC write, SQUserPointer up, SQInteger tag)
bool SafeWrite(HSQUIRRELVM v, SQWRITEFUNC write, SQUserPointer up, SQUserPointer dest, SQInteger size)
bool ReadObject(HSQUIRRELVM v, SQUserPointer up, SQREADFUNC read, SQObjectPtr &o)
const SQChar * IdType2Name(SQObjectType type)