2 see copyright notice in squirrel.h
4 #include "sqpcheader.h"
9 #include "sqfuncproto.h"
10 #include "sqclosure.h"
16 bool str2num(const SQChar *s,SQObjectPtr &res)
19 if(scstrstr(s,_SC("."))){
20 SQFloat r = SQFloat(scstrtod(s,&end));
21 if(s == end) return false;
22 while (scisspace((*end)) ) end++;
23 if (*end != _SC('\0')) return false;
29 while(*t != _SC('\0')) if(!scisdigit(*t++)) return false;
30 res = SQInteger(scatoi(s));
35 #ifndef NO_GARBAGE_COLLECTOR
36 static int base_collectgarbage(HSQUIRRELVM v)
38 sq_pushinteger(v, sq_collectgarbage(v));
43 static int base_getroottable(HSQUIRRELVM v)
45 v->Push(v->_roottable);
49 static int base_setroottable(HSQUIRRELVM v)
51 SQObjectPtr &o=stack_get(v,2);
52 if(SQ_FAILED(sq_setroottable(v))) return SQ_ERROR;
57 static int base_seterrorhandler(HSQUIRRELVM v)
59 sq_seterrorhandler(v);
63 static int base_setdebughook(HSQUIRRELVM v)
69 static int base_enabledebuginfo(HSQUIRRELVM v)
71 SQObjectPtr &o=stack_get(v,2);
72 sq_enabledebuginfo(v,(type(o) != OT_NULL)?1:0);
76 static int base_getstackinfos(HSQUIRRELVM v)
81 const SQChar *name = NULL;
82 sq_getinteger(v, -1, &level);
83 if (SQ_SUCCEEDED(sq_stackinfos(v, level, &si)))
85 const SQChar *fn = _SC("unknown");
86 const SQChar *src = _SC("unknown");
87 if(si.funcname)fn = si.funcname;
88 if(si.source)src = si.source;
90 sq_pushstring(v, _SC("func"), -1);
91 sq_pushstring(v, fn, -1);
93 sq_pushstring(v, _SC("src"), -1);
94 sq_pushstring(v, src, -1);
96 sq_pushstring(v, _SC("line"), -1);
97 sq_pushinteger(v, si.line);
99 sq_pushstring(v, _SC("locals"), -1);
102 while ( (name = sq_getlocal(v, level, seq)) ) {
103 sq_pushstring(v, name, -1);
105 sq_createslot(v, -4);
109 sq_createslot(v, -3);
116 static int base_assert(HSQUIRRELVM v)
118 if(v->IsFalse(stack_get(v,2))){
119 return sq_throwerror(v,_SC("assertion failed"));
124 static int get_slice_params(HSQUIRRELVM v,int &sidx,int &eidx,SQObjectPtr &o)
126 int top = sq_gettop(v);
130 SQObjectPtr &start=stack_get(v,2);
131 if(type(start)!=OT_NULL && sq_isnumeric(start)){
132 sidx=tointeger(start);
135 SQObjectPtr &end=stack_get(v,3);
136 if(sq_isnumeric(end)){
141 eidx = sq_getsize(v,1);
146 static int base_print(HSQUIRRELVM v)
148 SQObjectPtr &o=stack_get(v,2);
151 if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,_SC("%s"),_stringval(o));
154 if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,_SC("%d"),_integer(o));
157 if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,_SC("%.14g"),_float(o));
162 if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,_SC("(%s)"),_stringval(tname));
169 static int base_compilestring(HSQUIRRELVM v)
171 int nargs=sq_gettop(v);
172 const SQChar *src=NULL,*name=_SC("unnamedbuffer");
174 sq_getstring(v,2,&src);
175 size=sq_getsize(v,2);
177 sq_getstring(v,3,&name);
179 if(SQ_SUCCEEDED(sq_compilebuffer(v,src,size,name,SQFalse)))
185 static int base_newthread(HSQUIRRELVM v)
187 SQObjectPtr &func = stack_get(v,2);
188 int stksize = (_funcproto(_closure(func)->_function)->_stacksize << 1) +2;
189 HSQUIRRELVM newv = sq_newthread(v, (stksize < MIN_STACK_OVERHEAD + 2)? MIN_STACK_OVERHEAD + 2 : stksize);
194 static int base_suspend(HSQUIRRELVM v)
196 return sq_suspendvm(v);
199 static int base_array(HSQUIRRELVM v)
202 SQObject &size = stack_get(v,2);
203 if(sq_gettop(v) > 2) {
204 a = SQArray::Create(_ss(v),0);
205 a->Resize(tointeger(size),stack_get(v,3));
208 a = SQArray::Create(_ss(v),tointeger(size));
214 static int base_type(HSQUIRRELVM v)
216 SQObjectPtr &o = stack_get(v,2);
217 v->Push(SQString::Create(_ss(v),GetTypeName(o),-1));
221 static SQRegFunction base_funcs[]={
223 {_SC("seterrorhandler"),base_seterrorhandler,2, NULL},
224 {_SC("setdebughook"),base_setdebughook,2, NULL},
225 {_SC("enabledebuginfo"),base_enabledebuginfo,2, NULL},
226 {_SC("getstackinfos"),base_getstackinfos,2, _SC(".n")},
227 {_SC("getroottable"),base_getroottable,1, NULL},
228 {_SC("setroottable"),base_setroottable,2, NULL},
229 {_SC("assert"),base_assert,2, NULL},
230 {_SC("print"),base_print,2, NULL},
231 {_SC("compilestring"),base_compilestring,-2, _SC(".ss")},
232 {_SC("newthread"),base_newthread,2, _SC(".c")},
233 {_SC("suspend"),base_suspend,-1, NULL},
234 {_SC("array"),base_array,-2, _SC(".n")},
235 {_SC("type"),base_type,2, NULL},
236 #ifndef NO_GARBAGE_COLLECTOR
237 {_SC("collectgarbage"),base_collectgarbage,1, _SC("t")},
242 void sq_base_register(HSQUIRRELVM v)
246 while(base_funcs[i].name!=0) {
247 sq_pushstring(v,base_funcs[i].name,-1);
248 sq_newclosure(v,base_funcs[i].f,0);
249 sq_setnativeclosurename(v,-1,base_funcs[i].name);
250 sq_setparamscheck(v,base_funcs[i].nparamscheck,base_funcs[i].typemask);
254 sq_pushstring(v,_SC("_charsize_"),-1);
255 sq_pushinteger(v,sizeof(SQChar));
260 static int default_delegate_len(HSQUIRRELVM v)
262 v->Push(SQInteger(sq_getsize(v,1)));
266 static int default_delegate_tofloat(HSQUIRRELVM v)
268 SQObjectPtr &o=stack_get(v,1);
272 if(str2num(_stringval(o),res)){
273 v->Push(SQObjectPtr(tofloat(res)));
276 case OT_INTEGER:case OT_FLOAT:
277 v->Push(SQObjectPtr(tofloat(o)));
280 v->Push(SQObjectPtr((SQFloat)(_integer(o)?1:0)));
289 static int default_delegate_tointeger(HSQUIRRELVM v)
291 SQObjectPtr &o=stack_get(v,1);
295 if(str2num(_stringval(o),res)){
296 v->Push(SQObjectPtr(tointeger(res)));
299 case OT_INTEGER:case OT_FLOAT:
300 v->Push(SQObjectPtr(tointeger(o)));
303 v->Push(SQObjectPtr(_integer(o)?1:0));
312 static int default_delegate_tostring(HSQUIRRELVM v)
314 SQObjectPtr &o=stack_get(v,1);
320 scsprintf(_ss(v)->GetScratchPad(rsl(NUMBER_MAX_CHAR+1)),_SC("%d"),_integer(o));
321 v->Push(SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1)));
324 scsprintf(_ss(v)->GetScratchPad(rsl(NUMBER_MAX_CHAR+1)),_SC("%.14g"),_float(o));
325 v->Push(SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1)));
328 v->Push(SQObjectPtr(SQString::Create(_ss(v),_integer(o)?_SC("true"):_SC("false"))));
337 static int number_delegate_tochar(HSQUIRRELVM v)
339 SQObject &o=stack_get(v,1);
340 SQChar c=tointeger(o);
341 v->Push(SQString::Create(_ss(v),(const SQChar *)&c,1));
346 /////////////////////////////////////////////////////////////////
347 //TABLE DEFAULT DELEGATE
349 static int table_rawdelete(HSQUIRRELVM v)
351 if(SQ_FAILED(sq_rawdeleteslot(v,1,SQTrue)))
357 static int container_rawexists(HSQUIRRELVM v)
359 if(SQ_SUCCEEDED(sq_rawget(v,-2))) {
360 sq_pushbool(v,SQTrue);
363 sq_pushbool(v,SQFalse);
367 static int table_rawset(HSQUIRRELVM v)
369 return sq_rawset(v,-3);
373 static int table_rawget(HSQUIRRELVM v)
375 return SQ_SUCCEEDED(sq_rawget(v,-2))?1:SQ_ERROR;
378 SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
379 {_SC("len"),default_delegate_len,1, _SC("t")},
380 {_SC("rawget"),table_rawget,2, _SC("t")},
381 {_SC("rawset"),table_rawset,3, _SC("t")},
382 {_SC("rawdelete"),table_rawdelete,2, _SC("t")},
383 {_SC("rawin"),container_rawexists,2, _SC("t")},
387 //ARRAY DEFAULT DELEGATE///////////////////////////////////////
389 static int array_append(HSQUIRRELVM v)
391 return sq_arrayappend(v,-2);
394 static int array_extend(HSQUIRRELVM v)
396 _array(stack_get(v,1))->Extend(_array(stack_get(v,2)));
400 static int array_reverse(HSQUIRRELVM v)
402 return sq_arrayreverse(v,-1);
405 static int array_pop(HSQUIRRELVM v)
407 return SQ_SUCCEEDED(sq_arraypop(v,1,SQTrue))?1:SQ_ERROR;
410 static int array_top(HSQUIRRELVM v)
412 SQObject &o=stack_get(v,1);
413 if(_array(o)->Size()>0){
414 v->Push(_array(o)->Top());
417 else return sq_throwerror(v,_SC("top() on a empty array"));
420 static int array_insert(HSQUIRRELVM v)
422 SQObject &o=stack_get(v,1);
423 SQObject &idx=stack_get(v,2);
424 SQObject &val=stack_get(v,3);
425 _array(o)->Insert(idx,val);
429 static int array_remove(HSQUIRRELVM v)
431 SQObject &o = stack_get(v, 1);
432 SQObject &idx = stack_get(v, 2);
433 if(!sq_isnumeric(idx)) return sq_throwerror(v, _SC("wrong type"));
435 if(_array(o)->Get(tointeger(idx), val)) {
436 _array(o)->Remove(tointeger(idx));
440 return sq_throwerror(v, _SC("idx out of range"));
443 static int array_resize(HSQUIRRELVM v)
445 SQObject &o = stack_get(v, 1);
446 SQObject &nsize = stack_get(v, 2);
448 if(sq_isnumeric(nsize)) {
450 fill = stack_get(v, 3);
451 _array(o)->Resize(tointeger(nsize),fill);
454 return sq_throwerror(v, _SC("size must be a number"));
458 //QSORT ala Sedgewick
459 bool _qsort_compare(HSQUIRRELVM v,SQObjectPtr &arr,SQObjectPtr &a,SQObjectPtr &b,int func,int &ret)
464 if(!v->ObjCmp(a,b,ret)) return false;
467 int top = sq_gettop(v);
472 if(SQ_FAILED(sq_call(v, 3, SQTrue))) {
473 v->Raise_Error(_SC("compare func failed"));
476 sq_getinteger(v, -1, &ret);
482 //QSORT ala Sedgewick
483 bool _qsort(HSQUIRRELVM v,SQObjectPtr &arr, int l, int r,int func)
486 SQArray *a=_array(arr);
489 pivot = a->_values[l];
496 if(!_qsort_compare(v,arr,a->_values[i],pivot,func,ret))
501 if(!_qsort_compare(v,arr,a->_values[j],pivot,func,ret))
506 t = a->_values[i]; a->_values[i] = a->_values[j]; a->_values[j] = t;
508 t = a->_values[l]; a->_values[l] = a->_values[j]; a->_values[j] = t;
509 if(!_qsort( v, arr, l, j-1,func)) return false;
510 if(!_qsort( v, arr, j+1, r,func)) return false;
515 static int array_sort(HSQUIRRELVM v)
519 SQObjectPtr &o = stack_get(v,1);
520 SQObject &funcobj = stack_get(v,2);
521 if(_array(o)->Size() > 1) {
522 if(type(funcobj) == OT_CLOSURE || type(funcobj) == OT_NATIVECLOSURE) func = 2;
523 if(!_qsort(v, o, 0, _array(o)->Size()-1, func))
529 static int array_slice(HSQUIRRELVM v)
533 if(get_slice_params(v,sidx,eidx,o)==-1)return -1;
534 if(sidx<0)sidx=_array(o)->Size()+sidx;
535 if(eidx<0)eidx=_array(o)->Size()+eidx;
536 if(eidx <= sidx)return sq_throwerror(v,_SC("wrong indexes"));
537 SQArray *arr=SQArray::Create(_ss(v),eidx-sidx);
540 for(int i=sidx;i<eidx;i++){
549 SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
550 {_SC("len"),default_delegate_len,1, _SC("a")},
551 {_SC("append"),array_append,2, _SC("a")},
552 {_SC("extend"),array_extend,2, _SC("aa")},
553 {_SC("push"),array_append,2, _SC("a")},
554 {_SC("pop"),array_pop,1, _SC("a")},
555 {_SC("top"),array_top,1, _SC("a")},
556 {_SC("insert"),array_insert,3, _SC("an")},
557 {_SC("remove"),array_remove,2, _SC("an")},
558 {_SC("resize"),array_resize,-2, _SC("an")},
559 {_SC("reverse"),array_reverse,1, _SC("a")},
560 {_SC("sort"),array_sort,-1, _SC("ac")},
561 {_SC("slice"),array_slice,-1, _SC("ann")},
565 //STRING DEFAULT DELEGATE//////////////////////////
566 static int string_slice(HSQUIRRELVM v)
570 if(SQ_FAILED(get_slice_params(v,sidx,eidx,o)))return -1;
571 if(sidx<0)sidx=_string(o)->_len+sidx;
572 if(eidx<0)eidx=_string(o)->_len+eidx;
574 return sq_throwerror(v,_SC("wrong indexes"));
575 v->Push(SQString::Create(_ss(v),&_stringval(o)[sidx],eidx-sidx));
579 static int string_find(HSQUIRRELVM v)
582 const SQChar *str,*substr,*ret;
583 if(((top=sq_gettop(v))>1) && SQ_SUCCEEDED(sq_getstring(v,1,&str)) && SQ_SUCCEEDED(sq_getstring(v,2,&substr))){
584 if(top>2)sq_getinteger(v,3,&start_idx);
585 if((sq_getsize(v,1)>start_idx) && (start_idx>=0)){
586 ret=scstrstr(&str[start_idx],substr);
588 sq_pushinteger(v,(int)(ret-str));
594 return sq_throwerror(v,_SC("invalid param"));
597 #define STRING_TOFUNCZ(func) static int string_##func(HSQUIRRELVM v) \
599 SQObject str=stack_get(v,1); \
600 int len=_string(str)->_len; \
601 const SQChar *sThis=_stringval(str); \
602 SQChar *sNew=(_ss(v)->GetScratchPad(rsl(len))); \
603 for(int i=0;i<len;i++) sNew[i]=func(sThis[i]); \
604 v->Push(SQString::Create(_ss(v),sNew,len)); \
609 STRING_TOFUNCZ(tolower)
610 STRING_TOFUNCZ(toupper)
612 SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
613 {_SC("len"),default_delegate_len,1, _SC("s")},
614 {_SC("tointeger"),default_delegate_tointeger,1, _SC("s")},
615 {_SC("tofloat"),default_delegate_tofloat,1, _SC("s")},
616 {_SC("tostring"),default_delegate_tostring,1, _SC("s")},
617 {_SC("slice"),string_slice,-1, _SC(" s n n")},
618 {_SC("find"),string_find,-2, _SC("s s n ")},
619 {_SC("tolower"),string_tolower,1, _SC("s")},
620 {_SC("toupper"),string_toupper,1, _SC("s")},
624 //INTEGER DEFAULT DELEGATE//////////////////////////
625 SQRegFunction SQSharedState::_number_default_delegate_funcz[]={
626 {_SC("tointeger"),default_delegate_tointeger,1, _SC("n|b")},
627 {_SC("tofloat"),default_delegate_tofloat,1, _SC("n|b")},
628 {_SC("tostring"),default_delegate_tostring,1, _SC("n|b")},
629 {_SC("tochar"),number_delegate_tochar,1, _SC("n|b")},
633 //CLOSURE DEFAULT DELEGATE//////////////////////////
634 static int closure_call(HSQUIRRELVM v)
636 return SQ_SUCCEEDED(sq_call(v,sq_gettop(v)-1,SQTrue))?1:SQ_ERROR;
639 static int closure_acall(HSQUIRRELVM v)
641 SQArray *aparams=_array(stack_get(v,2));
642 int nparams=aparams->Size();
643 v->Push(stack_get(v,1));
644 for(int i=0;i<nparams;i++)v->Push(aparams->_values[i]);
645 return SQ_SUCCEEDED(sq_call(v,nparams,SQTrue))?1:SQ_ERROR;
648 SQRegFunction SQSharedState::_closure_default_delegate_funcz[]={
649 {_SC("call"),closure_call,-1, _SC("c")},
650 {_SC("acall"),closure_acall,2, _SC("ca")},
654 //GENERATOR DEFAULT DELEGATE
655 static int generator_getstatus(HSQUIRRELVM v)
657 SQObject &o=stack_get(v,1);
658 switch(_generator(o)->_state){
659 case SQGenerator::eSuspended:v->Push(SQString::Create(_ss(v),_SC("suspended")));break;
660 case SQGenerator::eRunning:v->Push(SQString::Create(_ss(v),_SC("running")));break;
661 case SQGenerator::eDead:v->Push(SQString::Create(_ss(v),_SC("dead")));break;
666 SQRegFunction SQSharedState::_generator_default_delegate_funcz[]={
667 {_SC("getstatus"),generator_getstatus,1, _SC("g")},
671 //THREAD DEFAULT DELEGATE
673 static int thread_call(HSQUIRRELVM v)
675 SQObjectPtr o = stack_get(v,1);
676 if(type(o) == OT_THREAD) {
677 int nparams = sq_gettop(v);
678 _thread(o)->Push(_thread(o)->_roottable);
679 for(int i = 2; i<(nparams+1); i++)
680 sq_move(_thread(o),v,i);
681 if(SQ_SUCCEEDED(sq_call(_thread(o),nparams,SQTrue))) {
682 sq_move(v,_thread(o),-1);
687 return sq_throwerror(v,_SC("wrong parameter"));
690 static int thread_wakeup(HSQUIRRELVM v)
692 SQObjectPtr o = stack_get(v,1);
693 if(type(o) == OT_THREAD) {
694 SQVM *thread = _thread(o);
695 int state = sq_getvmstate(thread);
696 if(state != SQ_VMSTATE_SUSPENDED) {
698 case SQ_VMSTATE_IDLE:
699 return sq_throwerror(v,_SC("cannot wakeup a idle thread"));
701 case SQ_VMSTATE_RUNNING:
702 return sq_throwerror(v,_SC("cannot wakeup a running thread"));
707 int wakeupret = sq_gettop(v)>1?1:0;
711 if(SQ_SUCCEEDED(sq_wakeupvm(thread,wakeupret,1))) {
712 sq_move(v,thread,-1);
714 if(sq_getvmstate(thread) == SQ_VMSTATE_IDLE) {
721 return sq_throwerror(v,_SC("wrong parameter"));
724 static int thread_getstatus(HSQUIRRELVM v)
726 SQObjectPtr &o = stack_get(v,1);
727 switch(sq_getvmstate(_thread(o))) {
728 case SQ_VMSTATE_IDLE:
729 sq_pushstring(v,_SC("idle"),-1);
731 case SQ_VMSTATE_RUNNING:
732 sq_pushstring(v,_SC("running"),-1);
734 case SQ_VMSTATE_SUSPENDED:
735 sq_pushstring(v,_SC("suspended"),-1);
738 return sq_throwerror(v,_SC("internal VM error"));
743 SQRegFunction SQSharedState::_thread_default_delegate_funcz[] = {
744 {_SC("call"), thread_call, -1, _SC("v")},
745 {_SC("wakeup"), thread_wakeup, -1, _SC("v")},
746 {_SC("getstatus"), thread_getstatus, 1, _SC("v")},
750 static int class_getattributes(HSQUIRRELVM v)
752 if(SQ_SUCCEEDED(sq_getattributes(v,-2)))
757 static int class_setattributes(HSQUIRRELVM v)
759 if(SQ_SUCCEEDED(sq_setattributes(v,-3)))
764 SQRegFunction SQSharedState::_class_default_delegate_funcz[] = {
765 {_SC("getattributes"), class_getattributes, 2, _SC("y.")},
766 {_SC("setattributes"), class_setattributes, 3, _SC("y..")},
767 {_SC("rawin"),container_rawexists,2, _SC("y")},
771 static int instance_getclass(HSQUIRRELVM v)
773 if(SQ_SUCCEEDED(sq_getclass(v,1)))
778 SQRegFunction SQSharedState::_instance_default_delegate_funcz[] = {
779 {_SC("getclass"), instance_getclass, 1, _SC("x")},
780 {_SC("rawin"),container_rawexists,2, _SC("x")},