2 see copyright notice in squirrel.h
4 #include "sqpcheader.h"
9 #include "sqfuncproto.h"
10 #include "sqclosure.h"
11 #include "squserdata.h"
12 #include "sqfuncstate.h"
13 #include "sqcompiler.h"
16 bool sq_aux_gettypedarg(HSQUIRRELVM v,int idx,SQObjectType type,SQObjectPtr **o)
18 *o = &stack_get(v,idx);
19 if(type(**o) != type){
20 SQObjectPtr oval = v->PrintObjVal(**o);
21 v->Raise_Error(_SC("wrong argument type, expected '%s' got '%.50s'"),IdType2Name(type),_stringval(oval));
27 #define _GETSAFE_OBJ(v,idx,type,o) { if(!sq_aux_gettypedarg(v,idx,type,&o)) return SQ_ERROR; }
29 #define sq_aux_paramscheck(v,count) \
31 if(sq_gettop(v) < count){ v->Raise_Error(_SC("not enough params in the stack")); return SQ_ERROR; }\
34 int sq_aux_throwobject(HSQUIRRELVM v,SQObjectPtr &e)
40 int sq_aux_invalidtype(HSQUIRRELVM v,SQObjectType type)
42 scsprintf(_ss(v)->GetScratchPad(100), _SC("unexpected type %s"), IdType2Name(type));
43 return sq_throwerror(v, _ss(v)->GetScratchPad(-1));
46 HSQUIRRELVM sq_open(int initialstacksize)
50 sq_new(ss, SQSharedState);
52 v = (SQVM *)SQ_MALLOC(sizeof(SQVM));
55 if(v->Init(NULL, initialstacksize)) {
64 HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, int initialstacksize)
70 v= (SQVM *)SQ_MALLOC(sizeof(SQVM));
73 if(v->Init(friendvm, initialstacksize)) {
82 int sq_getvmstate(HSQUIRRELVM v)
85 return SQ_VMSTATE_SUSPENDED;
87 if(v->_callsstack.size() != 0) return SQ_VMSTATE_RUNNING;
88 else return SQ_VMSTATE_IDLE;
92 void sq_seterrorhandler(HSQUIRRELVM v)
94 SQObject o = stack_get(v, -1);
95 if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) {
101 void sq_setdebughook(HSQUIRRELVM v)
103 SQObject o = stack_get(v,-1);
104 if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) {
110 void sq_close(HSQUIRRELVM v)
112 SQSharedState *ss = _ss(v);
113 _thread(ss->_root_vm)->Finalize();
114 sq_delete(ss, SQSharedState);
117 SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror)
120 if(Compile(v, read, p, sourcename, o, raiseerror?true:false, _ss(v)->_debuginfo)) {
121 v->Push(SQClosure::Create(_ss(v), _funcproto(o)));
127 void sq_enabledebuginfo(HSQUIRRELVM v, SQBool debuginfo)
129 _ss(v)->_debuginfo = debuginfo?true:false;
132 void sq_addref(HSQUIRRELVM v,HSQOBJECT *po)
135 if(!ISREFCOUNTED(type(*po))) return;
136 if(_table(_ss(v)->_refs_table)->Get(*po, refs)) {
137 refs = _integer(refs) + 1;
142 _table(_ss(v)->_refs_table)->NewSlot(*po, refs);
145 SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po)
148 if(!ISREFCOUNTED(type(*po))) return SQTrue;
149 if(_table(_ss(v)->_refs_table)->Get(*po, refs)) {
150 int n = _integer(refs) - 1;
152 _table(_ss(v)->_refs_table)->Remove(*po);
156 refs = n;_table(_ss(v)->_refs_table)->Set(*po, refs);
163 const SQChar *sq_objtostring(HSQOBJECT *o)
165 if(sq_type(*o) == OT_STRING) {
166 return o->_unVal.pString->_val;
171 SQInteger sq_objtointeger(HSQOBJECT *o)
173 if(sq_isnumeric(*o)) {
174 return tointeger(*o);
179 SQFloat sq_objtofloat(HSQOBJECT *o)
181 if(sq_isnumeric(*o)) {
187 void sq_pushnull(HSQUIRRELVM v)
192 void sq_pushstring(HSQUIRRELVM v,const SQChar *s,int len)
195 v->Push(SQObjectPtr(SQString::Create(_ss(v), s, len)));
196 else v->Push(_null_);
199 void sq_pushinteger(HSQUIRRELVM v,SQInteger n)
204 void sq_pushbool(HSQUIRRELVM v,SQBool b)
206 v->Push(b?true:false);
209 void sq_pushfloat(HSQUIRRELVM v,SQFloat n)
214 void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p)
219 SQUserPointer sq_newuserdata(HSQUIRRELVM v,unsigned int size)
221 SQUserData *ud = SQUserData::Create(_ss(v), size);
226 void sq_newtable(HSQUIRRELVM v)
228 v->Push(SQTable::Create(_ss(v), 0));
231 void sq_newarray(HSQUIRRELVM v,int size)
233 v->Push(SQArray::Create(_ss(v), size));
236 SQRESULT sq_newclass(HSQUIRRELVM v,SQBool hasbase)
238 SQClass *baseclass = NULL;
240 SQObjectPtr &base = stack_get(v,-1);
241 if(type(base) != OT_CLASS)
242 return sq_throwerror(v,_SC("invalid base type"));
243 baseclass = _class(base);
245 SQClass *newclass = SQClass::Create(_ss(v), baseclass);
246 if(baseclass) v->Pop();
251 int sq_instanceof(HSQUIRRELVM v)
253 SQObjectPtr &inst = stack_get(v,-1);
254 SQObjectPtr &cl = stack_get(v,-2);
255 if(type(inst) != OT_INSTANCE || type(cl) != OT_CLASS)
256 return sq_throwerror(v,_SC("invalid param type"));
257 return _instance(inst)->InstanceOf(_class(cl))?1:0;
260 SQRESULT sq_arrayappend(HSQUIRRELVM v,int idx)
262 sq_aux_paramscheck(v,2);
264 _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
265 _array(*arr)->Append(v->GetUp(-1));
270 SQRESULT sq_arraypop(HSQUIRRELVM v,int idx,SQBool pushval)
272 sq_aux_paramscheck(v, 1);
274 _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
275 if(_array(*arr)->Size() > 0) {
276 if(pushval != 0){ v->Push(_array(*arr)->Top()); }
280 return sq_throwerror(v, _SC("empty array"));
283 SQRESULT sq_arrayresize(HSQUIRRELVM v,int idx,int newsize)
285 sq_aux_paramscheck(v,1);
287 _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
288 if(_array(*arr)->Size() > 0) {
289 _array(*arr)->Resize(newsize);
292 return sq_throwerror(v, _SC("empty array"));
295 SQRESULT sq_arrayreverse(HSQUIRRELVM v,int idx)
297 sq_aux_paramscheck(v, 1);
299 _GETSAFE_OBJ(v, idx, OT_ARRAY,o);
300 SQArray *arr = _array(*o);
301 if(arr->Size() > 0) {
303 int size = arr->Size();
304 int n = size >> 1; size -= 1;
305 for(int i = 0; i < n; i++) {
307 arr->_values[i] = arr->_values[size-i];
308 arr->_values[size-i] = t;
312 return sq_throwerror(v, _SC("empty array"));
315 void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,unsigned int nfreevars)
317 SQNativeClosure *nc = SQNativeClosure::Create(_ss(v), func);
318 nc->_nparamscheck = 0;
319 for(unsigned int i = 0; i < nfreevars; i++) {
320 nc->_outervalues.push_back(v->Top());
323 v->Push(SQObjectPtr(nc));
326 SQRESULT sq_getclosureinfo(HSQUIRRELVM v,int idx,unsigned int *nparams,unsigned int *nfreevars)
328 SQObject o = stack_get(v, idx);
329 if(sq_isclosure(o)) {
330 SQClosure *c = _closure(o);
331 SQFunctionProto *proto = _funcproto(c->_function);
332 *nparams = (unsigned int)proto->_parameters.size();
333 *nfreevars = (unsigned int)c->_outervalues.size();
336 return sq_throwerror(v,_SC("the object is not a closure"));
339 SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,int idx,const SQChar *name)
341 SQObject o = stack_get(v, idx);
342 if(sq_isnativeclosure(o)) {
343 SQNativeClosure *nc = _nativeclosure(o);
344 nc->_name = SQString::Create(_ss(v),name);
347 return sq_throwerror(v,_SC("the object is not a nativeclosure"));
350 SQRESULT sq_setparamscheck(HSQUIRRELVM v,int nparamscheck,const SQChar *typemask)
352 SQObject o = stack_get(v, -1);
353 if(!sq_isnativeclosure(o))
354 return sq_throwerror(v, _SC("native closure expected"));
355 SQNativeClosure *nc = _nativeclosure(o);
356 nc->_nparamscheck = nparamscheck;
359 if(!CompileTypemask(res, typemask))
360 return sq_throwerror(v, _SC("invalid typemask"));
361 nc->_typecheck.copy(res);
364 nc->_typecheck.resize(0);
369 void sq_pushroottable(HSQUIRRELVM v)
371 v->Push(v->_roottable);
374 void sq_pushregistrytable(HSQUIRRELVM v)
376 v->Push(_ss(v)->_registry);
379 SQRESULT sq_setroottable(HSQUIRRELVM v)
381 SQObject o = stack_get(v, -1);
382 if(sq_istable(o) || sq_isnull(o)) {
387 return sq_throwerror(v, _SC("ivalid type"));
390 void sq_setforeignptr(HSQUIRRELVM v,SQUserPointer p)
395 SQUserPointer sq_getforeignptr(HSQUIRRELVM v)
397 return v->_foreignptr;
400 void sq_push(HSQUIRRELVM v,int idx)
402 v->Push(stack_get(v, idx));
405 SQObjectType sq_gettype(HSQUIRRELVM v,int idx)
407 return type(stack_get(v, idx));
410 SQRESULT sq_getinteger(HSQUIRRELVM v,int idx,SQInteger *i)
412 SQObjectPtr &o = stack_get(v, idx);
413 if(sq_isnumeric(o)) {
420 SQRESULT sq_getfloat(HSQUIRRELVM v,int idx,SQFloat *f)
422 SQObjectPtr &o = stack_get(v, idx);
423 if(sq_isnumeric(o)) {
430 SQRESULT sq_getbool(HSQUIRRELVM v,int idx,SQBool *b)
432 SQObjectPtr &o = stack_get(v, idx);
440 SQRESULT sq_getstring(HSQUIRRELVM v,int idx,const SQChar **c)
442 SQObjectPtr *o = NULL;
443 _GETSAFE_OBJ(v, idx, OT_STRING,o);
448 SQRESULT sq_getthread(HSQUIRRELVM v,int idx,HSQUIRRELVM *thread)
450 SQObjectPtr *o = NULL;
451 _GETSAFE_OBJ(v, idx, OT_THREAD,o);
452 *thread = _thread(*o);
456 SQRESULT sq_clone(HSQUIRRELVM v,int idx)
458 SQObjectPtr &o = stack_get(v,idx);
460 if(!v->Clone(o, stack_get(v, -1))){
462 return sq_aux_invalidtype(v, type(o));
467 SQInteger sq_getsize(HSQUIRRELVM v, int idx)
469 SQObjectPtr &o = stack_get(v, idx);
470 SQObjectType type = type(o);
472 case OT_STRING: return _string(o)->_len;
473 case OT_TABLE: return _table(o)->CountUsed();
474 case OT_ARRAY: return _array(o)->Size();
475 case OT_USERDATA: return _userdata(o)->_size;
477 return sq_aux_invalidtype(v, type);
481 SQRESULT sq_getuserdata(HSQUIRRELVM v,int idx,SQUserPointer *p,unsigned int *typetag)
483 SQObjectPtr *o = NULL;
484 _GETSAFE_OBJ(v, idx, OT_USERDATA,o);
485 (*p) = _userdataval(*o);
486 if(typetag) *typetag = _userdata(*o)->_typetag;
490 SQRESULT sq_settypetag(HSQUIRRELVM v,int idx,unsigned int typetag)
492 SQObjectPtr &o = stack_get(v,idx);
494 case OT_USERDATA: _userdata(o)->_typetag = typetag; break;
495 case OT_CLASS: _class(o)->_typetag = typetag; break;
496 default: return sq_throwerror(v,_SC("invalid object type"));
501 SQRESULT sq_gettypetag(HSQUIRRELVM v,int idx,unsigned int *typetag)
503 SQObjectPtr &o = stack_get(v,idx);
505 case OT_USERDATA: *typetag = _userdata(o)->_typetag; break;
506 case OT_CLASS: *typetag = _class(o)->_typetag; break;
507 default: return sq_throwerror(v,_SC("invalid object type"));
512 SQRESULT sq_getuserpointer(HSQUIRRELVM v, int idx, SQUserPointer *p)
514 SQObjectPtr *o = NULL;
515 _GETSAFE_OBJ(v, idx, OT_USERPOINTER,o);
516 (*p) = _userpointer(*o);
520 SQRESULT sq_setinstanceup(HSQUIRRELVM v, int idx, SQUserPointer p)
522 SQObjectPtr &o = stack_get(v,idx);
523 if(type(o) != OT_INSTANCE) return sq_throwerror(v,_SC("the object is not a class instance"));
524 _instance(o)->_userpointer = p;
528 SQRESULT sq_getinstanceup(HSQUIRRELVM v, int idx, SQUserPointer *p,unsigned int typetag)
530 SQObjectPtr &o = stack_get(v,idx);
531 if(type(o) != OT_INSTANCE) return sq_throwerror(v,_SC("the object is not a class instance"));
532 (*p) = _instance(o)->_userpointer;
534 SQClass *cl = _instance(o)->_class;
536 if(cl->_typetag == typetag)
540 return sq_throwerror(v,_SC("invalid type tag"));
545 int sq_gettop(HSQUIRRELVM v)
547 return (v->_top) - v->_stackbase;
550 void sq_settop(HSQUIRRELVM v, int newtop)
552 int top = sq_gettop(v);
554 sq_pop(v, top - newtop);
556 while(top < newtop) sq_pushnull(v);
559 void sq_pop(HSQUIRRELVM v, int nelemstopop)
561 assert(v->_top >= nelemstopop);
565 void sq_remove(HSQUIRRELVM v, int idx)
570 int sq_cmp(HSQUIRRELVM v)
573 v->ObjCmp(stack_get(v, -1), stack_get(v, -2),res);
577 SQRESULT sq_createslot(HSQUIRRELVM v, int idx)
579 sq_aux_paramscheck(v, 3);
580 SQObjectPtr &self = stack_get(v, idx);
581 if(type(self) == OT_TABLE || type(self) == OT_CLASS) {
582 SQObjectPtr &key = v->GetUp(-2);
583 if(type(key) == OT_NULL) return sq_throwerror(v, _SC("null is not a valid key"));
584 v->NewSlot(self, key, v->GetUp(-1));
590 SQRESULT sq_deleteslot(HSQUIRRELVM v,int idx,SQBool pushval)
592 sq_aux_paramscheck(v, 2);
594 _GETSAFE_OBJ(v, idx, OT_TABLE,self);
595 SQObjectPtr &key = v->GetUp(-1);
596 if(type(key) == OT_NULL) return sq_throwerror(v, _SC("null is not a valid key"));
598 if(!v->DeleteSlot(*self, key, res)){
601 if(pushval) v->GetUp(-1) = res;
606 SQRESULT sq_set(HSQUIRRELVM v,int idx)
608 SQObjectPtr &self = stack_get(v, idx);
609 if(v->Set(self, v->GetUp(-2), v->GetUp(-1),false)) {
613 v->Raise_IdxError(v->GetUp(-2));return SQ_ERROR;
616 SQRESULT sq_rawset(HSQUIRRELVM v,int idx)
618 SQObjectPtr &self = stack_get(v, idx);
619 if(type(v->GetUp(-2)) == OT_NULL) return sq_throwerror(v, _SC("null key"));
622 _table(self)->NewSlot(v->GetUp(-2), v->GetUp(-1));
627 _class(self)->NewSlot(v->GetUp(-2), v->GetUp(-1));
632 if(_instance(self)->Set(v->GetUp(-2), v->GetUp(-1))) {
638 if(v->Set(self, v->GetUp(-2), v->GetUp(-1),false)) {
645 return sq_throwerror(v, _SC("rawset works only on arrays,tables,calsses and instances"));
647 v->Raise_IdxError(v->GetUp(-2));return SQ_ERROR;
650 SQRESULT sq_setdelegate(HSQUIRRELVM v,int idx)
652 SQObjectPtr &self = stack_get(v, idx);
653 SQObjectPtr &mt = v->GetUp(-1);
654 SQObjectType type = type(self);
657 if(type(mt) == OT_TABLE) {
658 if(!_table(self)->SetDelegate(_table(mt))) return sq_throwerror(v, _SC("delagate cycle")); v->Pop();}
659 else if(type(mt)==OT_NULL) {
660 _table(self)->SetDelegate(NULL); v->Pop(); }
661 else return sq_aux_invalidtype(v,type);
664 if(type(mt)==OT_TABLE) {
665 _userdata(self)->SetDelegate(_table(mt)); v->Pop(); }
666 else if(type(mt)==OT_NULL) {
667 _userdata(self)->SetDelegate(NULL); v->Pop(); }
668 else return sq_aux_invalidtype(v, type);
671 return sq_aux_invalidtype(v, type);
677 SQRESULT sq_rawdeleteslot(HSQUIRRELVM v,int idx,SQBool pushval)
679 sq_aux_paramscheck(v, 2);
681 _GETSAFE_OBJ(v, idx, OT_TABLE,self);
682 SQObjectPtr &key = v->GetUp(-1);
684 if(_table(*self)->Get(key,t)) {
685 _table(*self)->Remove(key);
688 if(pushval) v->GetUp(-1) = t;
694 SQRESULT sq_getdelegate(HSQUIRRELVM v,int idx)
696 SQObjectPtr &self=stack_get(v,idx);
699 if(!_table(self)->_delegate)break;
700 v->Push(SQObjectPtr(_table(self)->_delegate));
704 if(!_userdata(self)->_delegate)break;
705 v->Push(SQObjectPtr(_userdata(self)->_delegate));
711 return sq_throwerror(v,_SC("wrong type"));
714 SQRESULT sq_get(HSQUIRRELVM v,int idx)
716 SQObjectPtr &self=stack_get(v,idx);
717 if(v->Get(self,v->GetUp(-1),v->GetUp(-1),false,false))
720 return sq_throwerror(v,_SC("the index doesn't exist"));
723 SQRESULT sq_rawget(HSQUIRRELVM v,int idx)
725 SQObjectPtr &self=stack_get(v,idx);
728 if(_table(self)->Get(v->GetUp(-1),v->GetUp(-1)))
732 if(_class(self)->Get(v->GetUp(-1),v->GetUp(-1)))
736 if(_instance(self)->Get(v->GetUp(-1),v->GetUp(-1)))
740 if(v->Get(self,v->GetUp(-1),v->GetUp(-1),false,false))
745 return sq_throwerror(v,_SC("rawget works only on arrays and tables"));
748 return sq_throwerror(v,_SC("the index doesn't exist"));
751 SQRESULT sq_getstackobj(HSQUIRRELVM v,int idx,HSQOBJECT *po)
753 *po=stack_get(v,idx);
757 const SQChar *sq_getlocal(HSQUIRRELVM v,unsigned int level,unsigned int idx)
759 unsigned int cstksize=v->_callsstack.size();
760 unsigned int lvl=(cstksize-level)-1;
761 int stackbase=v->_stackbase;
763 for(unsigned int i=0;i<level;i++){
764 SQVM::CallInfo &ci=v->_callsstack[(cstksize-i)-1];
765 stackbase-=ci._prevstkbase;
767 SQVM::CallInfo &ci=v->_callsstack[lvl];
768 if(type(ci._closure)!=OT_CLOSURE)
770 SQClosure *c=_closure(ci._closure);
771 SQFunctionProto *func=_funcproto(c->_function);
772 return func->GetLocal(v,stackbase,idx,(ci._ip-func->_instructions._vals)-1);
777 void sq_pushobject(HSQUIRRELVM v,HSQOBJECT obj)
779 v->Push(SQObjectPtr(obj));
782 void sq_resetobject(HSQOBJECT *po)
784 po->_unVal.pUserPointer=NULL;po->_type=OT_NULL;
787 SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err)
789 v->_lasterror=SQString::Create(_ss(v),err);
793 void sq_reseterror(HSQUIRRELVM v)
795 v->_lasterror = _null_;
798 void sq_getlasterror(HSQUIRRELVM v)
800 v->Push(v->_lasterror);
803 void sq_reservestack(HSQUIRRELVM v,int nsize)
805 if (((unsigned int)v->_top + nsize) > v->_stack.size()) {
806 v->_stack.resize(v->_stack.size() + ((v->_top + nsize) - v->_stack.size()));
810 SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval)
812 if(type(v->GetUp(-1))==OT_GENERATOR){
813 v->Push(_null_); //retval
814 if(!v->Execute(v->GetUp(-2),v->_top,0,v->_top,v->GetUp(-1),SQVM::ET_RESUME_GENERATOR))
815 {v->Raise_Error(v->_lasterror); return SQ_ERROR;}
820 return sq_throwerror(v,_SC("only generators can be resumed"));
823 SQRESULT sq_call(HSQUIRRELVM v,int params,SQBool retval)
826 if(v->Call(v->GetUp(-(params+1)),params,v->_top-params,res)){
827 v->Pop(params);//pop closure and args
829 v->Push(res); return SQ_OK;
839 return sq_throwerror(v,_SC("call failed"));
842 SQRESULT sq_suspendvm(HSQUIRRELVM v)
847 SQRESULT sq_wakeupvm(HSQUIRRELVM v,SQBool wakeupret,SQBool retval)
851 return sq_throwerror(v,_SC("cannot resume a vm that is not running any code"));
853 v->GetAt(v->_stackbase+v->_suspended_target)=v->GetUp(-1); //retval
855 } else v->GetAt(v->_stackbase+v->_suspended_target)=_null_;
856 if(!v->Execute(_null_,v->_top,-1,-1,ret,SQVM::ET_RESUME_VM))
858 if(sq_getvmstate(v) == SQ_VMSTATE_IDLE) {
859 while (v->_top > 1) v->_stack[--v->_top] = _null_;
866 void sq_setreleasehook(HSQUIRRELVM v,int idx,SQRELEASEHOOK hook)
868 if(sq_gettop(v) >= 1){
869 SQObjectPtr &ud=stack_get(v,idx);
872 _userdata(ud)->_hook = hook;
875 _instance(ud)->_hook = hook;
883 void sq_setcompilererrorhandler(HSQUIRRELVM v,SQCOMPILERERROR f)
885 _ss(v)->_compilererrorhandler = f;
888 SQRESULT sq_writeclosure(HSQUIRRELVM v,SQWRITEFUNC w,SQUserPointer up)
890 SQObjectPtr *o = NULL;
891 _GETSAFE_OBJ(v, -1, OT_CLOSURE,o);
892 //SQClosure *c=_closure(*o);
893 unsigned short tag = SQ_BYTECODE_STREAM_TAG;
894 if(w(up,&tag,2) != 2)
895 return sq_throwerror(v,_SC("io error"));
896 if(!_closure(*o)->Save(v,up,w))
901 SQRESULT sq_readclosure(HSQUIRRELVM v,SQREADFUNC r,SQUserPointer up)
903 SQObjectPtr func=SQFunctionProto::Create();
904 SQObjectPtr closure=SQClosure::Create(_ss(v),_funcproto(func));
906 if(r(up,&tag,2) != 2)
907 return sq_throwerror(v,_SC("io error"));
908 if(tag != SQ_BYTECODE_STREAM_TAG)
909 return sq_throwerror(v,_SC("invalid stream"));
910 if(!_closure(closure)->Load(v,up,r))
916 SQChar *sq_getscratchpad(HSQUIRRELVM v,int minsize)
918 return _ss(v)->GetScratchPad(minsize);
921 int sq_collectgarbage(HSQUIRRELVM v)
923 #ifndef NO_GARBAGE_COLLECTOR
924 return _ss(v)->CollectGarbage(v);
930 SQRESULT sq_setfreevariable(HSQUIRRELVM v,int idx,unsigned int nval)
932 SQObjectPtr &self=stack_get(v,idx);
936 if(_closure(self)->_outervalues.size()>nval){
937 _closure(self)->_outervalues[nval]=stack_get(v,-1);
939 else return sq_throwerror(v,_SC("invalid free var index"));
941 case OT_NATIVECLOSURE:
942 if(_nativeclosure(self)->_outervalues.size()>nval){
943 _nativeclosure(self)->_outervalues[nval]=stack_get(v,-1);
945 else return sq_throwerror(v,_SC("invalid free var index"));
948 return sq_aux_invalidtype(v,type(self));
954 SQRESULT sq_setattributes(HSQUIRRELVM v,int idx)
956 SQObjectPtr *o = NULL;
957 _GETSAFE_OBJ(v, idx, OT_CLASS,o);
958 SQObjectPtr &key = stack_get(v,-2);
959 SQObjectPtr &val = stack_get(v,-1);
961 if(type(key) == OT_NULL) {
962 attrs = _class(*o)->_attributes;
963 _class(*o)->_attributes = val;
967 }else if(_class(*o)->GetAttributes(key,attrs)) {
968 _class(*o)->SetAttributes(key,val);
973 return sq_throwerror(v,_SC("wrong index"));
976 SQRESULT sq_getattributes(HSQUIRRELVM v,int idx)
978 SQObjectPtr *o = NULL;
979 _GETSAFE_OBJ(v, idx, OT_CLASS,o);
980 SQObjectPtr &key = stack_get(v,-1);
982 if(type(key) == OT_NULL) {
983 attrs = _class(*o)->_attributes;
988 else if(_class(*o)->GetAttributes(key,attrs)) {
993 return sq_throwerror(v,_SC("wrong index"));
996 SQRESULT sq_getclass(HSQUIRRELVM v,int idx)
998 SQObjectPtr *o = NULL;
999 _GETSAFE_OBJ(v, idx, OT_INSTANCE,o);
1000 v->Push(SQObjectPtr(_instance(*o)->_class));
1004 SQRESULT sq_createinstance(HSQUIRRELVM v,int idx)
1006 SQObjectPtr *o = NULL;
1007 _GETSAFE_OBJ(v, idx, OT_CLASS,o);
1008 v->Push(_class(*o)->CreateInstance());
1012 SQRESULT sq_next(HSQUIRRELVM v,int idx)
1014 SQObjectPtr o=stack_get(v,idx),&refpos = stack_get(v,-1),realkey,val;
1015 if(type(o) == OT_GENERATOR) {
1016 return sq_throwerror(v,_SC("cannot iterate a generator"));
1019 if(!v->FOREACH_OP(o,realkey,val,refpos,0,finished))
1035 SQInteger buf_lexfeed(SQUserPointer file)
1037 BufState *buf=(BufState*)file;
1038 if(buf->size<(buf->ptr+1))
1040 return buf->buf[buf->ptr++];
1043 SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,int size,const SQChar *sourcename,SQBool raiseerror) {
1048 return sq_compile(v, buf_lexfeed, &buf, sourcename, raiseerror);
1051 void sq_move(HSQUIRRELVM dest,HSQUIRRELVM src,int idx)
1053 dest->Push(stack_get(src,idx));
1056 void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc)
1058 _ss(v)->_printfunc = printfunc;
1061 SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v)
1063 return _ss(v)->_printfunc;
1066 void *sq_malloc(unsigned int size)
1068 return SQ_MALLOC(size);
1071 void *sq_realloc(void* p,unsigned int oldsize,unsigned int newsize)
1073 return SQ_REALLOC(p,oldsize,newsize);
1075 void sq_free(void *p,unsigned int size)