2 see copyright notice in squirrel.h
4 #include "sqpcheader.h"
9 #include "squserdata.h"
10 #include "sqfuncproto.h"
12 #include "sqclosure.h"
15 const SQChar *IdType2Name(SQObjectType type)
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");
43 const SQChar *GetTypeName(const SQObjectPtr &obj1)
45 return IdType2Name(type(obj1));
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)
62 SQInteger idx = (SQInteger)TranslateIndex(refpos);
64 outkey = (SQInteger)idx;
65 outval = SQInteger(_val[idx]);
66 //return idx for the next iteration
69 //nothing to iterate anymore
73 SQUnsignedInteger TranslateIndex(const SQObjectPtr &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)
121 if (temp->_delegate == this) return false; //cycle detected
122 temp = temp->_delegate;
124 if (mt) __ObjAddRef(mt);
125 __ObjRelease(_delegate);
130 bool SQGenerator::Yield(SQVM *v)
132 if(_state==eSuspended) { v->Raise_Error(_SC("internal vm error, yielding dead generator")); return false;}
133 if(_state==eDead) { v->Raise_Error(_SC("internal vm error, yielding a dead generator")); return false; }
134 SQInteger size = v->_top-v->_stackbase;
137 for(SQInteger n =0; n<size; n++) {
138 _stack._vals[n] = v->_stack[v->_stackbase+n];
139 v->_stack[v->_stackbase+n] = _null_;
141 SQInteger nvargs = v->ci->_vargs.size;
142 SQInteger vargsbase = v->ci->_vargs.base;
143 for(SQInteger j = nvargs - 1; j >= 0; j--) {
144 _vargsstack.push_back(v->_vargsstack[vargsbase+j]);
146 _ci._generator=_null_;
147 for(SQInteger i=0;i<_ci._etraps;i++) {
148 _etraps.push_back(v->_etraps.top());
149 v->_etraps.pop_back();
155 bool SQGenerator::Resume(SQVM *v,SQInteger target)
157 SQInteger size=_stack.size();
158 if(_state==eDead){ v->Raise_Error(_SC("resuming dead generator")); return false; }
159 if(_state==eRunning){ v->Raise_Error(_SC("resuming active generator")); return false; }
160 SQInteger prevtop=v->_top-v->_stackbase;
161 PUSH_CALLINFO(v,_ci);
162 SQInteger oldstackbase=v->_stackbase;
163 v->_stackbase = v->_top;
164 v->ci->_target = (SQInt32)target;
165 v->ci->_generator = SQObjectPtr(this);
166 v->ci->_vargs.size = (unsigned short)_vargsstack.size();
168 for(SQInteger i=0;i<_ci._etraps;i++) {
169 v->_etraps.push_back(_etraps.top());
172 for(SQInteger n =0; n<size; n++) {
173 v->_stack[v->_stackbase+n] = _stack._vals[n];
174 _stack._vals[0] = _null_;
176 while(_vargsstack.size()) {
177 v->_vargsstack.push_back(_vargsstack.back());
178 _vargsstack.pop_back();
180 v->ci->_vargs.base = (unsigned short)(v->_vargsstack.size() - v->ci->_vargs.size);
181 v->_top=v->_stackbase+size;
182 v->ci->_prevtop = (SQInt32)prevtop;
183 v->ci->_prevstkbase = (SQInt32)(v->_stackbase - oldstackbase);
185 if (type(v->_debughook) != OT_NULL && _rawval(v->_debughook) != _rawval(v->ci->_closure))
186 v->CallDebugHook(_SC('c'));
191 void SQArray::Extend(const SQArray *a){
194 for(SQInteger i=0;i<xlen;i++)
195 Append(a->_values[i]);
198 const SQChar* SQFunctionProto::GetLocal(SQVM *vm,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop)
200 SQUnsignedInteger nvars=_nlocalvarinfos;
201 const SQChar *res=NULL;
203 for(SQUnsignedInteger i=0;i<nvars;i++){
204 if(_localvarinfos[i]._start_op<=nop && _localvarinfos[i]._end_op>=nop)
207 vm->Push(vm->_stack[stackbase+_localvarinfos[i]._pos]);
208 res=_stringval(_localvarinfos[i]._name);
218 SQInteger SQFunctionProto::GetLine(SQInstruction *curr)
220 SQInteger op = (SQInteger)(curr-_instructions);
221 SQInteger line=_lineinfos[0]._line;
222 for(SQInteger i=1;i<_nlineinfos;i++){
223 if(_lineinfos[i]._op>=op)
225 line=_lineinfos[i]._line;
230 #define _CHECK_IO(exp) { if(!exp)return false; }
231 bool SafeWrite(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQUserPointer dest,SQInteger size)
233 if(write(up,dest,size) != size) {
234 v->Raise_Error(_SC("io error (write function failure)"));
240 bool SafeRead(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQUserPointer dest,SQInteger size)
242 if(size && read(up,dest,size) != size) {
243 v->Raise_Error(_SC("io error, read function failure, the origin stream could be corrupted/trucated"));
249 bool WriteTag(HSQUIRRELVM v,SQWRITEFUNC write,SQUserPointer up,SQInteger tag)
251 return SafeWrite(v,write,up,&tag,sizeof(tag));
254 bool CheckTag(HSQUIRRELVM v,SQWRITEFUNC read,SQUserPointer up,SQInteger tag)
257 _CHECK_IO(SafeRead(v,read,up,&t,sizeof(t)));
259 v->Raise_Error(_SC("invalid or corrupted closure stream"));
265 bool WriteObject(HSQUIRRELVM v,SQUserPointer up,SQWRITEFUNC write,SQObjectPtr &o)
267 _CHECK_IO(SafeWrite(v,write,up,&type(o),sizeof(SQObjectType)));
270 _CHECK_IO(SafeWrite(v,write,up,&_string(o)->_len,sizeof(SQInteger)));
271 _CHECK_IO(SafeWrite(v,write,up,_stringval(o),rsl(_string(o)->_len)));
274 _CHECK_IO(SafeWrite(v,write,up,&_integer(o),sizeof(SQInteger)));break;
276 _CHECK_IO(SafeWrite(v,write,up,&_float(o),sizeof(SQFloat)));break;
280 v->Raise_Error(_SC("cannot serialize a %s"),GetTypeName(o));
286 bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
289 _CHECK_IO(SafeRead(v,read,up,&t,sizeof(SQObjectType)));
293 _CHECK_IO(SafeRead(v,read,up,&len,sizeof(SQInteger)));
294 _CHECK_IO(SafeRead(v,read,up,_ss(v)->GetScratchPad(rsl(len)),rsl(len)));
295 o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len);
300 _CHECK_IO(SafeRead(v,read,up,&i,sizeof(SQInteger))); o = i; break;
304 _CHECK_IO(SafeRead(v,read,up,&f,sizeof(SQFloat))); o = f; break;
310 v->Raise_Error(_SC("cannot serialize a %s"),IdType2Name(t));
316 bool SQClosure::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
318 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_HEAD));
319 _CHECK_IO(WriteTag(v,write,up,sizeof(SQChar)));
320 _CHECK_IO(_funcproto(_function)->Save(v,up,write));
321 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_TAIL));
325 bool SQClosure::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
327 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_HEAD));
328 _CHECK_IO(CheckTag(v,read,up,sizeof(SQChar)));
330 _CHECK_IO(SQFunctionProto::Load(v,up,read,func));
331 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_TAIL));
332 ret = SQClosure::Create(_ss(v),_funcproto(func));
336 bool SQFunctionProto::Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write)
338 SQInteger i,nliterals = _nliterals,nparameters = _nparameters;
339 SQInteger noutervalues = _noutervalues,nlocalvarinfos = _nlocalvarinfos;
340 SQInteger nlineinfos=_nlineinfos,ninstructions = _ninstructions,nfunctions=_nfunctions;
341 SQInteger ndefaultparams = _ndefaultparams;
342 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
343 _CHECK_IO(WriteObject(v,up,write,_sourcename));
344 _CHECK_IO(WriteObject(v,up,write,_name));
345 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
346 _CHECK_IO(SafeWrite(v,write,up,&nliterals,sizeof(nliterals)));
347 _CHECK_IO(SafeWrite(v,write,up,&nparameters,sizeof(nparameters)));
348 _CHECK_IO(SafeWrite(v,write,up,&noutervalues,sizeof(noutervalues)));
349 _CHECK_IO(SafeWrite(v,write,up,&nlocalvarinfos,sizeof(nlocalvarinfos)));
350 _CHECK_IO(SafeWrite(v,write,up,&nlineinfos,sizeof(nlineinfos)));
351 _CHECK_IO(SafeWrite(v,write,up,&ndefaultparams,sizeof(ndefaultparams)));
352 _CHECK_IO(SafeWrite(v,write,up,&ninstructions,sizeof(ninstructions)));
353 _CHECK_IO(SafeWrite(v,write,up,&nfunctions,sizeof(nfunctions)));
354 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
355 for(i=0;i<nliterals;i++){
356 _CHECK_IO(WriteObject(v,up,write,_literals[i]));
359 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
360 for(i=0;i<nparameters;i++){
361 _CHECK_IO(WriteObject(v,up,write,_parameters[i]));
364 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
365 for(i=0;i<noutervalues;i++){
366 _CHECK_IO(SafeWrite(v,write,up,&_outervalues[i]._type,sizeof(SQUnsignedInteger)));
367 _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._src));
368 _CHECK_IO(WriteObject(v,up,write,_outervalues[i]._name));
371 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
372 for(i=0;i<nlocalvarinfos;i++){
373 SQLocalVarInfo &lvi=_localvarinfos[i];
374 _CHECK_IO(WriteObject(v,up,write,lvi._name));
375 _CHECK_IO(SafeWrite(v,write,up,&lvi._pos,sizeof(SQUnsignedInteger)));
376 _CHECK_IO(SafeWrite(v,write,up,&lvi._start_op,sizeof(SQUnsignedInteger)));
377 _CHECK_IO(SafeWrite(v,write,up,&lvi._end_op,sizeof(SQUnsignedInteger)));
380 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
381 _CHECK_IO(SafeWrite(v,write,up,_lineinfos,sizeof(SQLineInfo)*nlineinfos));
383 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
384 _CHECK_IO(SafeWrite(v,write,up,_defaultparams,sizeof(SQInteger)*ndefaultparams));
386 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
387 _CHECK_IO(SafeWrite(v,write,up,_instructions,sizeof(SQInstruction)*ninstructions));
389 _CHECK_IO(WriteTag(v,write,up,SQ_CLOSURESTREAM_PART));
390 for(i=0;i<nfunctions;i++){
391 _CHECK_IO(_funcproto(_functions[i])->Save(v,up,write));
393 _CHECK_IO(SafeWrite(v,write,up,&_stacksize,sizeof(_stacksize)));
394 _CHECK_IO(SafeWrite(v,write,up,&_bgenerator,sizeof(_bgenerator)));
395 _CHECK_IO(SafeWrite(v,write,up,&_varparams,sizeof(_varparams)));
399 bool SQFunctionProto::Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret)
401 SQInteger i, nliterals,nparameters;
402 SQInteger noutervalues ,nlocalvarinfos ;
403 SQInteger nlineinfos,ninstructions ,nfunctions,ndefaultparams ;
404 SQObjectPtr sourcename, name;
406 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
407 _CHECK_IO(ReadObject(v, up, read, sourcename));
408 _CHECK_IO(ReadObject(v, up, read, name));
410 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
411 _CHECK_IO(SafeRead(v,read,up, &nliterals, sizeof(nliterals)));
412 _CHECK_IO(SafeRead(v,read,up, &nparameters, sizeof(nparameters)));
413 _CHECK_IO(SafeRead(v,read,up, &noutervalues, sizeof(noutervalues)));
414 _CHECK_IO(SafeRead(v,read,up, &nlocalvarinfos, sizeof(nlocalvarinfos)));
415 _CHECK_IO(SafeRead(v,read,up, &nlineinfos, sizeof(nlineinfos)));
416 _CHECK_IO(SafeRead(v,read,up, &ndefaultparams, sizeof(ndefaultparams)));
417 _CHECK_IO(SafeRead(v,read,up, &ninstructions, sizeof(ninstructions)));
418 _CHECK_IO(SafeRead(v,read,up, &nfunctions, sizeof(nfunctions)));
421 SQFunctionProto *f = SQFunctionProto::Create(ninstructions,nliterals,nparameters,
422 nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams);
423 SQObjectPtr proto = f; //gets a ref in case of failure
424 f->_sourcename = sourcename;
427 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
429 for(i = 0;i < nliterals; i++){
430 _CHECK_IO(ReadObject(v, up, read, o));
433 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
435 for(i = 0; i < nparameters; i++){
436 _CHECK_IO(ReadObject(v, up, read, o));
437 f->_parameters[i] = o;
439 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
441 for(i = 0; i < noutervalues; i++){
442 SQUnsignedInteger type;
444 _CHECK_IO(SafeRead(v,read,up, &type, sizeof(SQUnsignedInteger)));
445 _CHECK_IO(ReadObject(v, up, read, o));
446 _CHECK_IO(ReadObject(v, up, read, name));
447 f->_outervalues[i] = SQOuterVar(name,o, (SQOuterType)type);
449 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
451 for(i = 0; i < nlocalvarinfos; i++){
453 _CHECK_IO(ReadObject(v, up, read, lvi._name));
454 _CHECK_IO(SafeRead(v,read,up, &lvi._pos, sizeof(SQUnsignedInteger)));
455 _CHECK_IO(SafeRead(v,read,up, &lvi._start_op, sizeof(SQUnsignedInteger)));
456 _CHECK_IO(SafeRead(v,read,up, &lvi._end_op, sizeof(SQUnsignedInteger)));
457 f->_localvarinfos[i] = lvi;
459 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
460 _CHECK_IO(SafeRead(v,read,up, f->_lineinfos, sizeof(SQLineInfo)*nlineinfos));
462 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
463 _CHECK_IO(SafeRead(v,read,up, f->_defaultparams, sizeof(SQInteger)*ndefaultparams));
465 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
466 _CHECK_IO(SafeRead(v,read,up, f->_instructions, sizeof(SQInstruction)*ninstructions));
468 _CHECK_IO(CheckTag(v,read,up,SQ_CLOSURESTREAM_PART));
469 for(i = 0; i < nfunctions; i++){
470 _CHECK_IO(_funcproto(o)->Load(v, up, read, o));
471 f->_functions[i] = o;
473 _CHECK_IO(SafeRead(v,read,up, &f->_stacksize, sizeof(f->_stacksize)));
474 _CHECK_IO(SafeRead(v,read,up, &f->_bgenerator, sizeof(f->_bgenerator)));
475 _CHECK_IO(SafeRead(v,read,up, &f->_varparams, sizeof(f->_varparams)));
481 #ifndef NO_GARBAGE_COLLECTOR
483 #define START_MARK() if(!(_uiRef&MARK_FLAG)){ \
486 #define END_MARK() RemoveFromChain(&_sharedstate->_gc_chain, this); \
487 AddToChain(chain, this); }
489 void SQVM::Mark(SQCollectable **chain)
492 SQSharedState::MarkObject(_lasterror,chain);
493 SQSharedState::MarkObject(_errorhandler,chain);
494 SQSharedState::MarkObject(_debughook,chain);
495 SQSharedState::MarkObject(_roottable, chain);
496 SQSharedState::MarkObject(temp_reg, chain);
497 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
498 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
502 void SQArray::Mark(SQCollectable **chain)
505 SQInteger len = _values.size();
506 for(SQInteger i = 0;i < len; i++) SQSharedState::MarkObject(_values[i], chain);
509 void SQTable::Mark(SQCollectable **chain)
512 if(_delegate) _delegate->Mark(chain);
513 SQInteger len = _numofnodes;
514 for(SQInteger i = 0; i < len; i++){
515 SQSharedState::MarkObject(_nodes[i].key, chain);
516 SQSharedState::MarkObject(_nodes[i].val, chain);
521 void SQClass::Mark(SQCollectable **chain)
524 _members->Mark(chain);
525 if(_base) _base->Mark(chain);
526 SQSharedState::MarkObject(_attributes, chain);
527 for(SQUnsignedInteger i =0; i< _defaultvalues.size(); i++) {
528 SQSharedState::MarkObject(_defaultvalues[i].val, chain);
529 SQSharedState::MarkObject(_defaultvalues[i].attrs, chain);
531 for(SQUnsignedInteger j =0; j< _methods.size(); j++) {
532 SQSharedState::MarkObject(_methods[j].val, chain);
533 SQSharedState::MarkObject(_methods[j].attrs, chain);
535 for(SQUnsignedInteger k =0; k< _metamethods.size(); k++) {
536 SQSharedState::MarkObject(_metamethods[k], chain);
541 void SQInstance::Mark(SQCollectable **chain)
545 SQUnsignedInteger nvalues = _class->_defaultvalues.size();
546 for(SQUnsignedInteger i =0; i< nvalues; i++) {
547 SQSharedState::MarkObject(_values[i], chain);
552 void SQGenerator::Mark(SQCollectable **chain)
555 for(SQUnsignedInteger i = 0; i < _stack.size(); i++) SQSharedState::MarkObject(_stack[i], chain);
556 for(SQUnsignedInteger j = 0; j < _vargsstack.size(); j++) SQSharedState::MarkObject(_vargsstack[j], chain);
557 SQSharedState::MarkObject(_closure, chain);
561 void SQClosure::Mark(SQCollectable **chain)
564 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
565 for(SQUnsignedInteger i = 0; i < _defaultparams.size(); i++) SQSharedState::MarkObject(_defaultparams[i], chain);
569 void SQNativeClosure::Mark(SQCollectable **chain)
572 for(SQUnsignedInteger i = 0; i < _outervalues.size(); i++) SQSharedState::MarkObject(_outervalues[i], chain);
576 void SQUserData::Mark(SQCollectable **chain){
578 if(_delegate) _delegate->Mark(chain);
582 void SQCollectable::UnMark() { _uiRef&=~MARK_FLAG; }