fixed warnings in squirrel
[supertux.git] / src / squirrel / squirrel / sqapi.cpp
1 /*
2         see copyright notice in squirrel.h
3 */
4 #include "sqpcheader.h"
5 #include "sqvm.h"
6 #include "sqstring.h"
7 #include "sqtable.h"
8 #include "sqarray.h"
9 #include "sqfuncproto.h"
10 #include "sqclosure.h"
11 #include "squserdata.h"
12 #include "sqfuncstate.h"
13 #include "sqcompiler.h"
14 #include "sqclass.h"
15
16 bool sq_aux_gettypedarg(HSQUIRRELVM v,int idx,SQObjectType type,SQObjectPtr **o)
17 {
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));
22                 return false;
23         }
24         return true;
25 }
26
27 #define _GETSAFE_OBJ(v,idx,type,o) { if(!sq_aux_gettypedarg(v,idx,type,&o)) return SQ_ERROR; }
28
29 #define sq_aux_paramscheck(v,count) \
30 { \
31         if(sq_gettop(v) < count){ v->Raise_Error(_SC("not enough params in the stack")); return SQ_ERROR; }\
32 }               
33
34 int sq_aux_throwobject(HSQUIRRELVM v,SQObjectPtr &e)
35 {
36         v->_lasterror = e;
37         return SQ_ERROR;
38 }
39
40 int sq_aux_invalidtype(HSQUIRRELVM v,SQObjectType type)
41 {
42         scsprintf(_ss(v)->GetScratchPad(100), _SC("unexpected type %s"), IdType2Name(type));
43         return sq_throwerror(v, _ss(v)->GetScratchPad(-1));
44 }
45
46 HSQUIRRELVM sq_open(int initialstacksize)
47 {
48         SQSharedState *ss;
49         SQVM *v;
50         sq_new(ss, SQSharedState);
51         ss->Init();
52         v = (SQVM *)SQ_MALLOC(sizeof(SQVM));
53         new (v) SQVM(ss);
54         ss->_root_vm = v;
55         if(v->Init(NULL, initialstacksize)) {
56                 return v;
57         } else {
58                 sq_delete(v, SQVM);
59                 return NULL;
60         }
61         return v;
62 }
63
64 HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, int initialstacksize)
65 {
66         SQSharedState *ss;
67         SQVM *v;
68         ss=_ss(friendvm);
69         
70         v= (SQVM *)SQ_MALLOC(sizeof(SQVM));
71         new (v) SQVM(ss);
72         
73         if(v->Init(friendvm, initialstacksize)) {
74                 friendvm->Push(v);
75                 return v;
76         } else {
77                 sq_delete(v, SQVM);
78                 return NULL;
79         }
80 }
81
82 int sq_getvmstate(HSQUIRRELVM v)
83 {
84         if(v->_suspended)
85                 return SQ_VMSTATE_SUSPENDED;
86         else { 
87                 if(v->_callsstack.size() != 0) return SQ_VMSTATE_RUNNING;
88                 else return SQ_VMSTATE_IDLE;
89         }
90 }
91
92 void sq_seterrorhandler(HSQUIRRELVM v)
93 {
94         SQObject o = stack_get(v, -1);
95         if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) {
96                 v->_errorhandler = o;
97                 v->Pop();
98         }
99 }
100
101 void sq_setdebughook(HSQUIRRELVM v)
102 {
103         SQObject o = stack_get(v,-1);
104         if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) {
105                 v->_debughook = o;
106                 v->Pop();
107         }
108 }
109
110 void sq_close(HSQUIRRELVM v)
111 {
112         SQSharedState *ss = _ss(v);
113         _thread(ss->_root_vm)->Finalize();
114         sq_delete(ss, SQSharedState);
115 }
116
117 SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror)
118 {
119         SQObjectPtr o;
120         if(Compile(v, read, p, sourcename, o, raiseerror?true:false, _ss(v)->_debuginfo)) {
121                 v->Push(SQClosure::Create(_ss(v), _funcproto(o)));
122                 return SQ_OK;
123         }
124         return SQ_ERROR;
125 }
126
127 void sq_enabledebuginfo(HSQUIRRELVM v, SQBool debuginfo)
128 {
129         _ss(v)->_debuginfo = debuginfo?true:false;
130 }
131
132 void sq_addref(HSQUIRRELVM v,HSQOBJECT *po)
133 {
134         SQObjectPtr refs;
135         if(!ISREFCOUNTED(type(*po))) return;
136         if(_table(_ss(v)->_refs_table)->Get(*po, refs)) {
137                 refs = _integer(refs) + 1;
138         }
139         else{
140                 refs = 1;
141         }
142         _table(_ss(v)->_refs_table)->NewSlot(*po, refs);
143 }
144
145 SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po)
146 {
147         SQObjectPtr refs;
148         if(!ISREFCOUNTED(type(*po))) return SQTrue;
149         if(_table(_ss(v)->_refs_table)->Get(*po, refs)) {
150                 int n = _integer(refs) - 1;
151                 if(n <= 0) {
152                         _table(_ss(v)->_refs_table)->Remove(*po);
153                         sq_resetobject(po);
154                 }
155                 else {
156                         refs = n;_table(_ss(v)->_refs_table)->Set(*po, refs);
157                         return SQFalse;
158                 }
159         }
160         return SQTrue;
161 }
162
163 const SQChar *sq_objtostring(HSQOBJECT *o) 
164 {
165         if(sq_type(*o) == OT_STRING) {
166                 return o->_unVal.pString->_val;
167         }
168         return NULL;
169 }
170
171 SQInteger sq_objtointeger(HSQOBJECT *o) 
172 {
173         if(sq_isnumeric(*o)) {
174                 return tointeger(*o);
175         }
176         return 0;
177 }
178
179 SQFloat sq_objtofloat(HSQOBJECT *o) 
180 {
181         if(sq_isnumeric(*o)) {
182                 return tofloat(*o);
183         }
184         return 0;
185 }
186
187 void sq_pushnull(HSQUIRRELVM v)
188 {
189         v->Push(_null_);
190 }
191
192 void sq_pushstring(HSQUIRRELVM v,const SQChar *s,int len)
193 {
194         if(s)
195                 v->Push(SQObjectPtr(SQString::Create(_ss(v), s, len)));
196         else v->Push(_null_);
197 }
198
199 void sq_pushinteger(HSQUIRRELVM v,SQInteger n)
200 {
201         v->Push(n);
202 }
203
204 void sq_pushbool(HSQUIRRELVM v,SQBool b)
205 {
206         v->Push(b?true:false);
207 }
208
209 void sq_pushfloat(HSQUIRRELVM v,SQFloat n)
210 {
211         v->Push(n);
212 }
213
214 void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p)
215 {
216         v->Push(p);
217 }
218
219 SQUserPointer sq_newuserdata(HSQUIRRELVM v,unsigned int size)
220 {
221         SQUserData *ud = SQUserData::Create(_ss(v), size);
222         v->Push(ud);
223         return ud->_val;
224 }
225
226 void sq_newtable(HSQUIRRELVM v)
227 {
228         v->Push(SQTable::Create(_ss(v), 0));    
229 }
230
231 void sq_newarray(HSQUIRRELVM v,int size)
232 {
233         v->Push(SQArray::Create(_ss(v), size)); 
234 }
235
236 SQRESULT sq_newclass(HSQUIRRELVM v,SQBool hasbase)
237 {
238         SQClass *baseclass = NULL;
239         if(hasbase) {
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);
244         }
245         SQClass *newclass = SQClass::Create(_ss(v), baseclass);
246         if(baseclass) v->Pop();
247         v->Push(newclass);      
248         return SQ_OK;
249 }
250
251 int sq_instanceof(HSQUIRRELVM v)
252 {
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;
258 }
259
260 SQRESULT sq_arrayappend(HSQUIRRELVM v,int idx)
261 {
262         sq_aux_paramscheck(v,2);
263         SQObjectPtr *arr;
264         _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
265         _array(*arr)->Append(v->GetUp(-1));
266         v->Pop(1);
267         return SQ_OK;
268 }
269
270 SQRESULT sq_arraypop(HSQUIRRELVM v,int idx,SQBool pushval)
271 {
272         sq_aux_paramscheck(v, 1);
273         SQObjectPtr *arr;
274         _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
275         if(_array(*arr)->Size() > 0) {
276         if(pushval != 0){ v->Push(_array(*arr)->Top()); }
277                 _array(*arr)->Pop();
278                 return SQ_OK;
279         }
280         return sq_throwerror(v, _SC("empty array"));
281 }
282
283 SQRESULT sq_arrayresize(HSQUIRRELVM v,int idx,int newsize)
284 {
285         sq_aux_paramscheck(v,1);
286         SQObjectPtr *arr;
287         _GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
288         if(_array(*arr)->Size() > 0) {
289                 _array(*arr)->Resize(newsize);
290                 return SQ_OK;
291         }
292         return sq_throwerror(v, _SC("empty array"));
293 }
294
295 SQRESULT sq_arrayreverse(HSQUIRRELVM v,int idx)
296 {
297         sq_aux_paramscheck(v, 1);
298         SQObjectPtr *o;
299         _GETSAFE_OBJ(v, idx, OT_ARRAY,o);
300         SQArray *arr = _array(*o);
301         if(arr->Size() > 0) {
302                 SQObjectPtr t;
303                 int size = arr->Size();
304                 int n = size >> 1; size -= 1;
305                 for(int i = 0; i < n; i++) {
306                         t = arr->_values[i];
307                         arr->_values[i] = arr->_values[size-i];
308                         arr->_values[size-i] = t;
309                 }
310                 return SQ_OK;
311         }
312         return sq_throwerror(v, _SC("empty array"));
313 }
314
315 void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,unsigned int nfreevars)
316 {
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());
321                 v->Pop();
322         }
323         v->Push(SQObjectPtr(nc));       
324 }
325
326 SQRESULT sq_getclosureinfo(HSQUIRRELVM v,int idx,unsigned int *nparams,unsigned int *nfreevars)
327 {
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();
334                 return SQ_OK;
335         }
336         return sq_throwerror(v,_SC("the object is not a closure"));
337 }
338
339 SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,int idx,const SQChar *name)
340 {
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);
345                 return SQ_OK;
346         }
347         return sq_throwerror(v,_SC("the object is not a nativeclosure"));
348 }
349
350 SQRESULT sq_setparamscheck(HSQUIRRELVM v,int nparamscheck,const SQChar *typemask)
351 {
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;
357         if(typemask) {
358                 SQIntVec res;
359                 if(!CompileTypemask(res, typemask))
360                         return sq_throwerror(v, _SC("invalid typemask"));
361                 nc->_typecheck.copy(res);
362         }
363         else {
364                 nc->_typecheck.resize(0);
365         }
366         return SQ_OK;
367 }
368
369 void sq_pushroottable(HSQUIRRELVM v)
370 {
371         v->Push(v->_roottable);
372 }
373
374 void sq_pushregistrytable(HSQUIRRELVM v)
375 {
376         v->Push(_ss(v)->_registry);
377 }
378
379 SQRESULT sq_setroottable(HSQUIRRELVM v)
380 {
381         SQObject o = stack_get(v, -1);
382         if(sq_istable(o) || sq_isnull(o)) {
383                 v->_roottable = o;
384                 v->Pop();
385                 return SQ_OK;
386         }
387         return sq_throwerror(v, _SC("ivalid type"));
388 }
389
390 void sq_setforeignptr(HSQUIRRELVM v,SQUserPointer p)
391 {
392         v->_foreignptr = p;
393 }
394
395 SQUserPointer sq_getforeignptr(HSQUIRRELVM v)
396 {
397         return v->_foreignptr;
398 }
399
400 void sq_push(HSQUIRRELVM v,int idx)
401 {
402         v->Push(stack_get(v, idx));
403 }
404
405 SQObjectType sq_gettype(HSQUIRRELVM v,int idx)
406 {
407         return type(stack_get(v, idx));
408 }
409
410 SQRESULT sq_getinteger(HSQUIRRELVM v,int idx,SQInteger *i)
411 {
412         SQObjectPtr &o = stack_get(v, idx);
413         if(sq_isnumeric(o)) {
414                 *i = tointeger(o);
415                 return SQ_OK;
416         }
417         return SQ_ERROR;
418 }
419
420 SQRESULT sq_getfloat(HSQUIRRELVM v,int idx,SQFloat *f)
421 {
422         SQObjectPtr &o = stack_get(v, idx);
423         if(sq_isnumeric(o)) {
424                 *f = tofloat(o);
425                 return SQ_OK;
426         }
427         return SQ_ERROR;
428 }
429
430 SQRESULT sq_getbool(HSQUIRRELVM v,int idx,SQBool *b)
431 {
432         SQObjectPtr &o = stack_get(v, idx);
433         if(sq_isbool(o)) {
434                 *b = _integer(o);
435                 return SQ_OK;
436         }
437         return SQ_ERROR;
438 }
439
440 SQRESULT sq_getstring(HSQUIRRELVM v,int idx,const SQChar **c)
441 {
442         SQObjectPtr *o = NULL;
443         _GETSAFE_OBJ(v, idx, OT_STRING,o);
444         *c = _stringval(*o);
445         return SQ_OK;
446 }
447
448 SQRESULT sq_getthread(HSQUIRRELVM v,int idx,HSQUIRRELVM *thread)
449 {
450         SQObjectPtr *o = NULL;
451         _GETSAFE_OBJ(v, idx, OT_THREAD,o);
452         *thread = _thread(*o);
453         return SQ_OK;
454 }
455
456 SQRESULT sq_clone(HSQUIRRELVM v,int idx)
457 {
458         SQObjectPtr &o = stack_get(v,idx);
459         v->Push(_null_);
460         if(!v->Clone(o, stack_get(v, -1))){
461                 v->Pop();
462                 return sq_aux_invalidtype(v, type(o));
463         }
464         return SQ_OK;
465 }
466
467 SQInteger sq_getsize(HSQUIRRELVM v, int idx)
468 {
469         SQObjectPtr &o = stack_get(v, idx);
470         SQObjectType type = type(o);
471         switch(type) {
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;
476         default:
477                 return sq_aux_invalidtype(v, type);
478         }
479 }
480
481 SQRESULT sq_getuserdata(HSQUIRRELVM v,int idx,SQUserPointer *p,unsigned int *typetag)
482 {
483         SQObjectPtr *o = NULL;
484         _GETSAFE_OBJ(v, idx, OT_USERDATA,o);
485         (*p) = _userdataval(*o);
486         if(typetag) *typetag = _userdata(*o)->_typetag;
487         return SQ_OK;
488 }
489
490 SQRESULT sq_settypetag(HSQUIRRELVM v,int idx,unsigned int typetag)
491 {
492         SQObjectPtr &o = stack_get(v,idx);
493         switch(type(o)) {
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"));
497         }
498         return SQ_OK;
499 }
500
501 SQRESULT sq_gettypetag(HSQUIRRELVM v,int idx,unsigned int *typetag)
502 {
503         SQObjectPtr &o = stack_get(v,idx);
504         switch(type(o)) {
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"));
508         }
509         return SQ_OK;
510 }
511
512 SQRESULT sq_getuserpointer(HSQUIRRELVM v, int idx, SQUserPointer *p)
513 {
514         SQObjectPtr *o = NULL;
515         _GETSAFE_OBJ(v, idx, OT_USERPOINTER,o);
516         (*p) = _userpointer(*o);
517         return SQ_OK;
518 }
519
520 SQRESULT sq_setinstanceup(HSQUIRRELVM v, int idx, SQUserPointer p)
521 {
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;
525         return SQ_OK;
526 }
527
528 SQRESULT sq_getinstanceup(HSQUIRRELVM v, int idx, SQUserPointer *p,unsigned int typetag)
529 {
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;
533         if(typetag != 0) {
534                 SQClass *cl = _instance(o)->_class;
535                 do{
536                         if(cl->_typetag == typetag)
537                                 return SQ_OK;
538                         cl = cl->_base;
539                 }while(cl != NULL);
540                 return sq_throwerror(v,_SC("invalid type tag"));
541         }
542         return SQ_OK;
543 }
544
545 int sq_gettop(HSQUIRRELVM v)
546 {
547         return (v->_top) - v->_stackbase;
548 }
549
550 void sq_settop(HSQUIRRELVM v, int newtop)
551 {
552         int top = sq_gettop(v);
553         if(top > newtop)
554                 sq_pop(v, top - newtop);
555         else
556                 while(top < newtop) sq_pushnull(v);
557 }
558
559 void sq_pop(HSQUIRRELVM v, int nelemstopop)
560 {
561         assert(v->_top >= nelemstopop);
562         v->Pop(nelemstopop);
563 }
564
565 void sq_remove(HSQUIRRELVM v, int idx)
566 {
567         v->Remove(idx);
568 }
569
570 int sq_cmp(HSQUIRRELVM v)
571 {
572         int res;
573         v->ObjCmp(stack_get(v, -1), stack_get(v, -2),res);
574         return res;
575 }
576
577 SQRESULT sq_createslot(HSQUIRRELVM v, int idx)
578 {
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));
585                 v->Pop(2);
586         }
587         return SQ_OK;
588 }
589
590 SQRESULT sq_deleteslot(HSQUIRRELVM v,int idx,SQBool pushval)
591 {
592         sq_aux_paramscheck(v, 2);
593         SQObjectPtr *self;
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"));
597         SQObjectPtr res;
598         if(!v->DeleteSlot(*self, key, res)){
599                 return SQ_ERROR;
600         }
601         if(pushval)     v->GetUp(-1) = res;
602         else v->Pop(1);
603         return SQ_OK;
604 }
605
606 SQRESULT sq_set(HSQUIRRELVM v,int idx)
607 {
608         SQObjectPtr &self = stack_get(v, idx);
609         if(v->Set(self, v->GetUp(-2), v->GetUp(-1),false)) {
610                 v->Pop(2);
611                 return SQ_OK;
612         }
613         v->Raise_IdxError(v->GetUp(-2));return SQ_ERROR;
614 }
615
616 SQRESULT sq_rawset(HSQUIRRELVM v,int idx)
617 {
618         SQObjectPtr &self = stack_get(v, idx);
619         if(type(v->GetUp(-2)) == OT_NULL) return sq_throwerror(v, _SC("null key"));
620         switch(type(self)) {
621         case OT_TABLE:
622                 _table(self)->NewSlot(v->GetUp(-2), v->GetUp(-1));
623                 v->Pop(2);
624                 return SQ_OK;
625         break;
626         case OT_CLASS:
627                 _class(self)->NewSlot(v->GetUp(-2), v->GetUp(-1));
628                 v->Pop(2);
629                 return SQ_OK;
630         break;
631         case OT_INSTANCE:
632                 if(_instance(self)->Set(v->GetUp(-2), v->GetUp(-1))) {
633                         v->Pop(2);
634                         return SQ_OK;
635                 }
636         break;
637         case OT_ARRAY:
638                 if(v->Set(self, v->GetUp(-2), v->GetUp(-1),false)) {
639                         v->Pop(2);
640                         return SQ_OK;
641                 }
642         break;
643         default:
644                 v->Pop(2);
645                 return sq_throwerror(v, _SC("rawset works only on arrays,tables,calsses and instances"));
646         }
647         v->Raise_IdxError(v->GetUp(-2));return SQ_ERROR;
648 }
649
650 SQRESULT sq_setdelegate(HSQUIRRELVM v,int idx)
651 {
652         SQObjectPtr &self = stack_get(v, idx);
653         SQObjectPtr &mt = v->GetUp(-1);
654         SQObjectType type = type(self);
655         switch(type) {
656         case OT_TABLE:
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);
662                 break;
663         case OT_USERDATA:
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);
669                 break;
670         default:
671                         return sq_aux_invalidtype(v, type);
672                 break;
673         }
674         return SQ_OK;
675 }
676
677 SQRESULT sq_rawdeleteslot(HSQUIRRELVM v,int idx,SQBool pushval)
678 {
679         sq_aux_paramscheck(v, 2);
680         SQObjectPtr *self;
681         _GETSAFE_OBJ(v, idx, OT_TABLE,self);
682         SQObjectPtr &key = v->GetUp(-1);
683         SQObjectPtr t;
684         if(_table(*self)->Get(key,t)) {
685                 _table(*self)->Remove(key);
686         }
687         if(pushval != 0)
688                 if(pushval)     v->GetUp(-1) = t;
689         else
690                 v->Pop(1);
691         return SQ_OK;
692 }
693
694 SQRESULT sq_getdelegate(HSQUIRRELVM v,int idx)
695 {
696         SQObjectPtr &self=stack_get(v,idx);
697         switch(type(self)){
698         case OT_TABLE:
699                 if(!_table(self)->_delegate)break;
700                 v->Push(SQObjectPtr(_table(self)->_delegate));
701                 return SQ_OK;
702                 break;
703         case OT_USERDATA:
704                 if(!_userdata(self)->_delegate)break;
705                 v->Push(SQObjectPtr(_userdata(self)->_delegate));
706                 return SQ_OK;
707                 break;
708         default:
709                 break;
710         }
711         return sq_throwerror(v,_SC("wrong type"));
712 }
713
714 SQRESULT sq_get(HSQUIRRELVM v,int idx)
715 {
716         SQObjectPtr &self=stack_get(v,idx);
717         if(v->Get(self,v->GetUp(-1),v->GetUp(-1),false,false))
718                 return SQ_OK;
719         v->Pop(1);
720         return sq_throwerror(v,_SC("the index doesn't exist"));
721 }
722
723 SQRESULT sq_rawget(HSQUIRRELVM v,int idx)
724 {
725         SQObjectPtr &self=stack_get(v,idx);
726         switch(type(self)) {
727         case OT_TABLE:
728                 if(_table(self)->Get(v->GetUp(-1),v->GetUp(-1)))
729                         return SQ_OK;
730                 break;
731         case OT_CLASS:
732                 if(_class(self)->Get(v->GetUp(-1),v->GetUp(-1)))
733                         return SQ_OK;
734                 break;
735         case OT_INSTANCE:
736                 if(_instance(self)->Get(v->GetUp(-1),v->GetUp(-1)))
737                         return SQ_OK;
738                 break;
739         case OT_ARRAY:
740                 if(v->Get(self,v->GetUp(-1),v->GetUp(-1),false,false))
741                         return SQ_OK;
742                 break;
743         default:
744                 v->Pop(1);
745                 return sq_throwerror(v,_SC("rawget works only on arrays and tables"));
746         }       
747         v->Pop(1);
748         return sq_throwerror(v,_SC("the index doesn't exist"));
749 }
750
751 SQRESULT sq_getstackobj(HSQUIRRELVM v,int idx,HSQOBJECT *po)
752 {
753         *po=stack_get(v,idx);
754         return SQ_OK;
755 }
756
757 const SQChar *sq_getlocal(HSQUIRRELVM v,unsigned int level,unsigned int idx)
758 {
759         unsigned int cstksize=v->_callsstack.size();
760         unsigned int lvl=(cstksize-level)-1;
761         int stackbase=v->_stackbase;
762         if(lvl<cstksize){
763                 for(unsigned int i=0;i<level;i++){
764                         SQVM::CallInfo &ci=v->_callsstack[(cstksize-i)-1];
765                         stackbase-=ci._prevstkbase;
766                 }
767                 SQVM::CallInfo &ci=v->_callsstack[lvl];
768                 if(type(ci._closure)!=OT_CLOSURE)
769                         return NULL;
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);
773         }
774         return NULL;
775 }
776
777 void sq_pushobject(HSQUIRRELVM v,HSQOBJECT obj)
778 {
779         v->Push(SQObjectPtr(obj));
780 }
781
782 void sq_resetobject(HSQOBJECT *po)
783 {
784         po->_unVal.pUserPointer=NULL;po->_type=OT_NULL;
785 }
786
787 SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err)
788 {
789         v->_lasterror=SQString::Create(_ss(v),err);
790         return -1;
791 }
792
793 void sq_reseterror(HSQUIRRELVM v)
794 {
795         v->_lasterror = _null_;
796 }
797
798 void sq_getlasterror(HSQUIRRELVM v)
799 {
800         v->Push(v->_lasterror);
801 }
802
803 void sq_reservestack(HSQUIRRELVM v,int nsize)
804 {
805         if (((unsigned int)v->_top + nsize) > v->_stack.size()) {
806                 v->_stack.resize(v->_stack.size() + ((v->_top + nsize) - v->_stack.size()));
807         }
808 }
809
810 SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval)
811 {
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;}
816                 if(!retval)
817                         v->Pop();
818                 return SQ_OK;
819         }
820         return sq_throwerror(v,_SC("only generators can be resumed"));
821 }
822
823 SQRESULT sq_call(HSQUIRRELVM v,int params,SQBool retval)
824 {
825         SQObjectPtr res;
826         if(v->Call(v->GetUp(-(params+1)),params,v->_top-params,res)){
827                 v->Pop(params);//pop closure and args
828                 if(retval){
829                         v->Push(res); return SQ_OK;
830                 }
831                 return SQ_OK;
832         }
833         else {
834                 v->Pop(params);
835                 return SQ_ERROR;
836         }
837         if(!v->_suspended)
838                 v->Pop(params);
839         return sq_throwerror(v,_SC("call failed"));
840 }
841
842 SQRESULT sq_suspendvm(HSQUIRRELVM v)
843 {
844         return v->Suspend();
845 }
846
847 SQRESULT sq_wakeupvm(HSQUIRRELVM v,SQBool wakeupret,SQBool retval)
848 {
849         SQObjectPtr ret;
850         if(!v->_suspended)
851                 return sq_throwerror(v,_SC("cannot resume a vm that is not running any code"));
852         if(wakeupret) {
853                 v->GetAt(v->_stackbase+v->_suspended_target)=v->GetUp(-1); //retval
854                 v->Pop();
855         } else v->GetAt(v->_stackbase+v->_suspended_target)=_null_;
856         if(!v->Execute(_null_,v->_top,-1,-1,ret,SQVM::ET_RESUME_VM))
857                 return SQ_ERROR;
858         if(sq_getvmstate(v) == SQ_VMSTATE_IDLE) {
859                 while (v->_top > 1) v->_stack[--v->_top] = _null_;
860         }
861         if(retval)
862                 v->Push(ret);
863         return SQ_OK;
864 }
865
866 void sq_setreleasehook(HSQUIRRELVM v,int idx,SQRELEASEHOOK hook)
867 {
868         if(sq_gettop(v) >= 1){
869                 SQObjectPtr &ud=stack_get(v,idx);
870                 switch( type(ud) ) {
871                 case OT_USERDATA:
872                         _userdata(ud)->_hook = hook;
873                         break;
874                 case OT_INSTANCE:
875                         _instance(ud)->_hook = hook;
876                         break;
877                 default:
878                         break;
879                 }
880         }
881 }
882
883 void sq_setcompilererrorhandler(HSQUIRRELVM v,SQCOMPILERERROR f)
884 {
885         _ss(v)->_compilererrorhandler = f;
886 }
887
888 SQRESULT sq_writeclosure(HSQUIRRELVM v,SQWRITEFUNC w,SQUserPointer up)
889 {
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))
897                 return SQ_ERROR;
898         return SQ_OK;
899 }
900
901 SQRESULT sq_readclosure(HSQUIRRELVM v,SQREADFUNC r,SQUserPointer up)
902 {
903         SQObjectPtr func=SQFunctionProto::Create();
904         SQObjectPtr closure=SQClosure::Create(_ss(v),_funcproto(func));
905         unsigned short tag;
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))
911                 return SQ_ERROR;
912         v->Push(closure);
913         return SQ_OK;
914 }
915
916 SQChar *sq_getscratchpad(HSQUIRRELVM v,int minsize)
917 {
918         return _ss(v)->GetScratchPad(minsize);
919 }
920
921 int sq_collectgarbage(HSQUIRRELVM v)
922 {
923 #ifndef NO_GARBAGE_COLLECTOR
924         return _ss(v)->CollectGarbage(v);
925 #else
926         return -1;
927 #endif
928 }
929
930 SQRESULT sq_setfreevariable(HSQUIRRELVM v,int idx,unsigned int nval)
931 {
932         SQObjectPtr &self=stack_get(v,idx);
933         switch(type(self))
934         {
935         case OT_CLOSURE:
936                 if(_closure(self)->_outervalues.size()>nval){
937                         _closure(self)->_outervalues[nval]=stack_get(v,-1);
938                 }
939                 else return sq_throwerror(v,_SC("invalid free var index"));
940                 break;
941         case OT_NATIVECLOSURE:
942                 if(_nativeclosure(self)->_outervalues.size()>nval){
943                         _nativeclosure(self)->_outervalues[nval]=stack_get(v,-1);
944                 }
945                 else return sq_throwerror(v,_SC("invalid free var index"));
946                 break;
947         default:
948                 return sq_aux_invalidtype(v,type(self));
949         }
950         v->Pop(1);
951         return SQ_OK;
952 }
953
954 SQRESULT sq_setattributes(HSQUIRRELVM v,int idx)
955 {
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);
960         SQObjectPtr attrs;
961         if(type(key) == OT_NULL) {
962                 attrs = _class(*o)->_attributes;
963                 _class(*o)->_attributes = val;
964                 v->Pop(2);
965                 v->Push(attrs);
966                 return SQ_OK;
967         }else if(_class(*o)->GetAttributes(key,attrs)) {
968                 _class(*o)->SetAttributes(key,val);
969                 v->Pop(2);
970                 v->Push(attrs);
971                 return SQ_OK;
972         }
973         return sq_throwerror(v,_SC("wrong index"));
974 }
975
976 SQRESULT sq_getattributes(HSQUIRRELVM v,int idx)
977 {
978         SQObjectPtr *o = NULL;
979         _GETSAFE_OBJ(v, idx, OT_CLASS,o);
980         SQObjectPtr &key = stack_get(v,-1);
981         SQObjectPtr attrs;
982         if(type(key) == OT_NULL) {
983                 attrs = _class(*o)->_attributes;
984                 v->Pop();
985                 v->Push(attrs);
986                 return SQ_OK;
987         }
988         else if(_class(*o)->GetAttributes(key,attrs)) {
989                 v->Pop();
990                 v->Push(attrs);
991                 return SQ_OK;
992         }
993         return sq_throwerror(v,_SC("wrong index"));
994 }
995
996 SQRESULT sq_getclass(HSQUIRRELVM v,int idx)
997 {
998         SQObjectPtr *o = NULL;
999         _GETSAFE_OBJ(v, idx, OT_INSTANCE,o);
1000         v->Push(SQObjectPtr(_instance(*o)->_class));
1001         return SQ_OK;
1002 }
1003
1004 SQRESULT sq_createinstance(HSQUIRRELVM v,int idx)
1005 {
1006         SQObjectPtr *o = NULL;
1007         _GETSAFE_OBJ(v, idx, OT_CLASS,o);
1008         v->Push(_class(*o)->CreateInstance());
1009         return SQ_OK;
1010 }
1011
1012 SQRESULT sq_next(HSQUIRRELVM v,int idx)
1013 {
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"));
1017         }
1018         bool finished;
1019         if(!v->FOREACH_OP(o,realkey,val,refpos,0,finished))
1020                 return SQ_ERROR;
1021         if(!finished) {
1022                 v->Push(realkey);
1023                 v->Push(val);
1024                 return SQ_OK;
1025         }
1026         return SQ_ERROR;
1027 }
1028
1029 struct BufState{
1030         const SQChar *buf;
1031         int ptr;
1032         int size;
1033 };
1034
1035 SQInteger buf_lexfeed(SQUserPointer file)
1036 {
1037         BufState *buf=(BufState*)file;
1038         if(buf->size<(buf->ptr+1))
1039                 return 0;
1040         return buf->buf[buf->ptr++];
1041 }
1042
1043 SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,int size,const SQChar *sourcename,SQBool raiseerror) {
1044         BufState buf;
1045         buf.buf = s;
1046         buf.size = size;
1047         buf.ptr = 0;
1048         return sq_compile(v, buf_lexfeed, &buf, sourcename, raiseerror);
1049 }
1050
1051 void sq_move(HSQUIRRELVM dest,HSQUIRRELVM src,int idx)
1052 {
1053         dest->Push(stack_get(src,idx));
1054 }
1055
1056 void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc)
1057 {
1058         _ss(v)->_printfunc = printfunc;
1059 }
1060
1061 SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v)
1062 {
1063         return _ss(v)->_printfunc;
1064 }
1065
1066 void *sq_malloc(unsigned int size)
1067 {
1068         return SQ_MALLOC(size);
1069 }
1070
1071 void *sq_realloc(void* p,unsigned int oldsize,unsigned int newsize)
1072 {
1073         return SQ_REALLOC(p,oldsize,newsize);
1074 }
1075 void sq_free(void *p,unsigned int size)
1076 {
1077         SQ_FREE(p,size);
1078 }