4 #include "sqpcheader.h" 9 #include "sqfuncproto.h" 10 #include "sqclosure.h" 19 if(scstrstr(s,_SC(
"."))){
20 SQFloat r = SQFloat(scstrtod(s,&end));
21 if(s == end)
return false;
26 SQInteger r = SQInteger(scstrtol(s,&end,10));
27 if(s == end)
return false;
39 #ifndef NO_GARBAGE_COLLECTOR 49 v->Push(v->_roottable);
55 v->Push(_ss(v)->_consts);
62 SQObjectPtr &o=stack_get(v,2);
70 SQObjectPtr &o=stack_get(v,2);
90 SQObjectPtr &o=stack_get(v,2);
104 const SQChar *fn = _SC(
"unknown");
105 const SQChar *src = _SC(
"unknown");
106 if(si.funcname)fn = si.funcname;
107 if(si.source)src = si.source;
111 sq_createslot(v, -3);
114 sq_createslot(v, -3);
117 sq_createslot(v, -3);
124 sq_createslot(v, -4);
128 sq_createslot(v, -3);
137 if(v->IsFalse(stack_get(v,2))){
143 static SQInteger
get_slice_params(HSQUIRRELVM v,SQInteger &sidx,SQInteger &eidx,SQObjectPtr &o)
149 SQObjectPtr &start=stack_get(v,2);
150 if(type(start)!=OT_NULL && sq_isnumeric(start)){
151 sidx=tointeger(start);
154 SQObjectPtr &end=stack_get(v,3);
155 if(sq_isnumeric(end)){
170 if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,_SC(
"%s"),str);
177 const SQChar *src=
NULL,*name=_SC(
"unnamedbuffer");
192 SQObjectPtr &func = stack_get(v,2);
193 SQInteger stksize = (_funcproto(_closure(func)->_function)->_stacksize << 1) +2;
194 HSQUIRRELVM newv =
sq_newthread(v, (stksize < MIN_STACK_OVERHEAD + 2)? MIN_STACK_OVERHEAD + 2 : stksize);
207 SQObject &size = stack_get(v,2);
209 a = SQArray::Create(_ss(v),0);
210 a->Resize(tointeger(size),stack_get(v,3));
213 a = SQArray::Create(_ss(v),tointeger(size));
221 SQObjectPtr &o = stack_get(v,2);
222 v->Push(SQString::Create(_ss(v),
GetTypeName(o),-1));
244 #ifndef NO_GARBAGE_COLLECTOR 285 SQObjectPtr &o=stack_get(v,1);
289 if(
str2num(_stringval(o),res)){
290 v->Push(SQObjectPtr(tofloat(res)));
295 case OT_INTEGER:
case OT_FLOAT:
296 v->Push(SQObjectPtr(tofloat(o)));
299 v->Push(SQObjectPtr((SQFloat)(_integer(o)?1:0)));
310 SQObjectPtr &o=stack_get(v,1);
314 if(
str2num(_stringval(o),res)){
315 v->Push(SQObjectPtr(tointeger(res)));
320 case OT_INTEGER:
case OT_FLOAT:
321 v->Push(SQObjectPtr(tointeger(o)));
324 v->Push(SQObjectPtr(_integer(o)?(SQInteger)1:(SQInteger)0));
353 SQObject &o=stack_get(v,1);
355 v->Push(SQString::Create(_ss(v),(
const SQChar *)&c,1));
389 return SQ_SUCCEEDED(
sq_rawget(v,-2))?1:SQ_ERROR;
393 SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
414 _array(stack_get(v,1))->Extend(_array(stack_get(v,2)));
425 return SQ_SUCCEEDED(
sq_arraypop(v,1,SQTrue))?1:SQ_ERROR;
430 SQObject &o=stack_get(v,1);
431 if(_array(o)->Size()>0){
432 v->Push(_array(o)->Top());
440 SQObject &o=stack_get(v,1);
441 SQObject &idx=stack_get(v,2);
442 SQObject &val=stack_get(v,3);
443 if(!_array(o)->Insert(tointeger(idx),val))
450 SQObject &o = stack_get(v, 1);
451 SQObject &idx = stack_get(v, 2);
452 if(!sq_isnumeric(idx))
return sq_throwerror(v, _SC(
"wrong type"));
454 if(_array(o)->Get(tointeger(idx), val)) {
455 _array(o)->Remove(tointeger(idx));
464 SQObject &o = stack_get(v, 1);
465 SQObject &nsize = stack_get(v, 2);
467 if(sq_isnumeric(nsize)) {
469 fill = stack_get(v, 3);
470 _array(o)->Resize(tointeger(nsize),fill);
477 bool _sort_compare(HSQUIRRELVM v,SQObjectPtr &a,SQObjectPtr &b,SQInteger func,SQInteger &ret)
480 if(!v->ObjCmp(a,b,ret))
return false;
488 if(SQ_FAILED(
sq_call(v, 3, SQTrue, SQFalse))) {
489 if(!sq_isstring( v->_lasterror))
490 v->Raise_Error(_SC(
"compare func failed"));
494 v->Raise_Error(_SC(
"numeric value expected as return value of the compare function"));
509 while (((root2 = root * 2) <= bottom) && (!done))
511 if (root2 == bottom) {
515 if(!
_sort_compare(v,arr->_values[root2],arr->_values[root2 + 1],func,ret))
521 maxChild = root2 + 1;
525 if(!
_sort_compare(v,arr->_values[root],arr->_values[maxChild],func,ret))
528 _Swap(arr->_values[root],arr->_values[maxChild]);
538 bool _hsort(HSQUIRRELVM v,SQObjectPtr &arr, SQInteger l, SQInteger r,SQInteger func)
540 SQArray *a = _array(arr);
542 SQInteger array_size = a->Size();
543 for (i = (array_size / 2); i >= 0; i--) {
547 for (i = array_size-1; i >= 1; i--)
549 _Swap(a->_values[0],a->_values[i]);
558 SQObjectPtr &o = stack_get(v,1);
559 if(_array(o)->Size() > 1) {
561 if(!
_hsort(v, o, 0, _array(o)->Size()-1, func))
573 SQInteger alen = _array(o)->Size();
574 if(sidx < 0)sidx = alen + sidx;
575 if(eidx < 0)eidx = alen + eidx;
577 if(eidx > alen)
return sq_throwerror(v,_SC(
"slice out of range"));
578 SQArray *arr=SQArray::Create(_ss(v),eidx-sidx);
581 for(SQInteger i=sidx;i<eidx;i++){
590 SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
615 SQInteger slen = _string(o)->_len;
616 if(sidx < 0)sidx = slen + sidx;
617 if(eidx < 0)eidx = slen + eidx;
618 if(eidx < sidx)
return sq_throwerror(v,_SC(
"wrong indexes"));
619 if(eidx > slen)
return sq_throwerror(v,_SC(
"slice out of range"));
620 v->Push(SQString::Create(_ss(v),&_stringval(o)[sidx],eidx-sidx));
626 SQInteger top,start_idx=0;
627 const SQChar *str,*substr,*ret;
630 if((
sq_getsize(v,1)>start_idx) && (start_idx>=0)){
631 ret=scstrstr(&str[start_idx],substr);
642 #define STRING_TOFUNCZ(func) static SQInteger string_##func(HSQUIRRELVM v) \ 644 SQObject str=stack_get(v,1); \ 645 SQInteger len=_string(str)->_len; \ 646 const SQChar *sThis=_stringval(str); \ 647 SQChar *sNew=(_ss(v)->GetScratchPad(rsl(len))); \ 648 for(SQInteger i=0;i<len;i++) sNew[i]=func(sThis[i]); \ 649 v->Push(SQString::Create(_ss(v),sNew,len)); \ 657 SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
664 {_SC(
"tolower"),string_tolower,1, _SC(
"s")},
665 {_SC(
"toupper"),string_toupper,1, _SC(
"s")},
671 SQRegFunction SQSharedState::_number_default_delegate_funcz[]={
693 SQArray *aparams=_array(stack_get(v,2));
694 SQInteger nparams=aparams->Size();
695 v->Push(stack_get(v,1));
696 for(SQInteger i=0;i<nparams;i++)v->Push(aparams->_values[i]);
697 return SQ_SUCCEEDED(
sq_call(v,nparams,SQTrue,raiseerror))?1:SQ_ERROR;
718 SQObject o = stack_get(v,1);
719 SQTable *res = SQTable::Create(_ss(v),4);
720 if(type(o) == OT_CLOSURE) {
721 SQFunctionProto *f = _funcproto(_closure(o)->_function);
722 SQInteger nparams = f->_nparameters + (f->_varparams?1:0);
723 SQObjectPtr params = SQArray::Create(_ss(v),nparams);
724 for(SQInteger n = 0; n<f->_nparameters; n++) {
725 _array(params)->Set((SQInteger)n,f->_parameters[n]);
728 _array(params)->Set(nparams-1,SQString::Create(_ss(v),_SC(
"..."),-1));
730 res->NewSlot(SQString::Create(_ss(v),_SC(
"native"),-1),
false);
731 res->NewSlot(SQString::Create(_ss(v),_SC(
"name"),-1),f->_name);
732 res->NewSlot(SQString::Create(_ss(v),_SC(
"src"),-1),f->_sourcename);
733 res->NewSlot(SQString::Create(_ss(v),_SC(
"parameters"),-1),params);
734 res->NewSlot(SQString::Create(_ss(v),_SC(
"varargs"),-1),f->_varparams);
737 SQNativeClosure *nc = _nativeclosure(o);
738 res->NewSlot(SQString::Create(_ss(v),_SC(
"native"),-1),
true);
739 res->NewSlot(SQString::Create(_ss(v),_SC(
"name"),-1),nc->_name);
740 res->NewSlot(SQString::Create(_ss(v),_SC(
"paramscheck"),-1),nc->_nparamscheck);
741 SQObjectPtr typecheck;
742 if(nc->_typecheck.size() > 0) {
744 SQArray::Create(_ss(v), nc->_typecheck.size());
745 for(SQUnsignedInteger n = 0; n<nc->_typecheck.size(); n++) {
746 _array(typecheck)->Set((SQInteger)n,nc->_typecheck[n]);
749 res->NewSlot(SQString::Create(_ss(v),_SC(
"typecheck"),-1),typecheck);
756 SQRegFunction SQSharedState::_closure_default_delegate_funcz[]={
771 SQObject &o=stack_get(v,1);
772 switch(_generator(o)->_state){
773 case SQGenerator::eSuspended:v->Push(SQString::Create(_ss(v),_SC(
"suspended")));
break;
774 case SQGenerator::eRunning:v->Push(SQString::Create(_ss(v),_SC(
"running")));
break;
775 case SQGenerator::eDead:v->Push(SQString::Create(_ss(v),_SC(
"dead")));
break;
780 SQRegFunction SQSharedState::_generator_default_delegate_funcz[]={
792 SQObjectPtr o = stack_get(v,1);
793 if(type(o) == OT_THREAD) {
795 _thread(o)->Push(_thread(o)->_roottable);
796 for(SQInteger i = 2; i<(nparams+1); i++)
798 if(SQ_SUCCEEDED(
sq_call(_thread(o),nparams,SQTrue,SQFalse))) {
803 v->_lasterror = _thread(o)->_lasterror;
811 SQObjectPtr o = stack_get(v,1);
812 if(type(o) == OT_THREAD) {
813 SQVM *thread = _thread(o);
815 if(state != SQ_VMSTATE_SUSPENDED) {
817 case SQ_VMSTATE_IDLE:
820 case SQ_VMSTATE_RUNNING:
821 return sq_throwerror(v,_SC(
"cannot wakeup a running thread"));
826 SQInteger wakeupret =
sq_gettop(v)>1?1:0;
830 if(SQ_SUCCEEDED(
sq_wakeupvm(thread,wakeupret,SQTrue,SQTrue,SQFalse))) {
839 v->_lasterror = thread->_lasterror;
847 SQObjectPtr &o = stack_get(v,1);
849 case SQ_VMSTATE_IDLE:
852 case SQ_VMSTATE_RUNNING:
855 case SQ_VMSTATE_SUSPENDED:
864 SQRegFunction SQSharedState::_thread_default_delegate_funcz[] = {
894 SQRegFunction SQSharedState::_class_default_delegate_funcz[] = {
911 SQRegFunction SQSharedState::_instance_default_delegate_funcz[] = {
926 SQRegFunction SQSharedState::_weakref_default_delegate_funcz[] = {
void sq_setdebughook(HSQUIRRELVM v)
static SQInteger thread_call(HSQUIRRELVM v)
static SQInteger base_getstackinfos(HSQUIRRELVM v)
SQRESULT sq_arraypop(HSQUIRRELVM v, SQInteger idx, SQBool pushval)
SQRESULT sq_setconsttable(HSQUIRRELVM v)
static SQInteger table_rawget(HSQUIRRELVM v)
bool str2num(const SQChar *s, SQObjectPtr &res)
SQRESULT sq_rawget(HSQUIRRELVM v, SQInteger idx)
static SQInteger base_setroottable(HSQUIRRELVM v)
static SQInteger array_remove(HSQUIRRELVM v)
SQRESULT sq_setattributes(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_rawdeleteslot(HSQUIRRELVM v, SQInteger idx, SQBool pushval)
static SQInteger get_slice_params(HSQUIRRELVM v, SQInteger &sidx, SQInteger &eidx, SQObjectPtr &o)
static SQInteger obj_clear(HSQUIRRELVM v)
static SQInteger number_delegate_tochar(HSQUIRRELVM v)
static SQInteger default_delegate_tofloat(HSQUIRRELVM v)
static SQInteger base_newthread(HSQUIRRELVM v)
static SQInteger base_collectgarbage(HSQUIRRELVM v)
void sq_pop(HSQUIRRELVM v, SQInteger nelemstopop)
SQRESULT sq_setroottable(HSQUIRRELVM v)
SQRESULT sq_throwerror(HSQUIRRELVM v, const SQChar *err)
SQInteger sq_collectgarbage(HSQUIRRELVM v)
void sq_newtable(HSQUIRRELVM v)
static SQInteger weakref_ref(HSQUIRRELVM v)
void sq_enabledebuginfo(HSQUIRRELVM v, SQBool enable)
static SQInteger instance_getclass(HSQUIRRELVM v)
static SQInteger array_reverse(HSQUIRRELVM v)
SQRESULT sq_rawset(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_wakeupvm(HSQUIRRELVM v, SQBool wakeupret, SQBool retval, SQBool raiseerror, SQBool throwerror)
static SQInteger array_sort(HSQUIRRELVM v)
static SQInteger class_instance(HSQUIRRELVM v)
static SQInteger default_delegate_tostring(HSQUIRRELVM v)
SQRESULT sq_getweakrefval(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_bindenv(HSQUIRRELVM v, SQInteger idx)
void sq_pushroottable(HSQUIRRELVM v)
static SQInteger base_dummy(HSQUIRRELVM)
static SQInteger array_append(HSQUIRRELVM v)
static SQInteger base_print(HSQUIRRELVM v)
SQRESULT sq_arrayappend(HSQUIRRELVM v, SQInteger idx)
static SQInteger container_rawexists(HSQUIRRELVM v)
static SQInteger base_seterrorhandler(HSQUIRRELVM v)
SQInteger sq_getvmstate(HSQUIRRELVM v)
HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize)
SQRESULT sq_getinteger(HSQUIRRELVM v, SQInteger idx, SQInteger *i)
static SQInteger array_insert(HSQUIRRELVM v)
SQInteger sq_getsize(HSQUIRRELVM v, SQInteger idx)
static SQInteger obj_delegate_weakref(HSQUIRRELVM v)
static SQInteger class_setattributes(HSQUIRRELVM v)
void sq_base_register(HSQUIRRELVM v)
static SQInteger class_getattributes(HSQUIRRELVM v)
const SQChar * GetTypeName(const SQObjectPtr &obj1)
static SQInteger closure_pacall(HSQUIRRELVM v)
SQInteger sq_gettop(HSQUIRRELVM v)
static SQInteger base_compilestring(HSQUIRRELVM v)
void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
#define STRING_TOFUNCZ(func)
static SQInteger base_setdebughook(HSQUIRRELVM v)
void sq_newclosure(HSQUIRRELVM v, SQFUNCTION func, SQUnsignedInteger nfreevars)
SQRESULT sq_call(HSQUIRRELVM v, SQInteger params, SQBool retval, SQBool raiseerror)
static SQInteger closure_call(HSQUIRRELVM v)
void sq_push(HSQUIRRELVM v, SQInteger idx)
static SQInteger generator_getstatus(HSQUIRRELVM v)
SQRESULT sq_clear(HSQUIRRELVM v, SQInteger idx)
static SQInteger string_find(HSQUIRRELVM v)
static SQInteger array_top(HSQUIRRELVM v)
static SQInteger closure_getinfos(HSQUIRRELVM v)
SQRESULT sq_setnativeclosurename(HSQUIRRELVM v, SQInteger idx, const SQChar *name)
void sq_settop(HSQUIRRELVM v, SQInteger newtop)
bool _sort_compare(HSQUIRRELVM v, SQObjectPtr &a, SQObjectPtr &b, SQInteger func, SQInteger &ret)
void sq_tostring(HSQUIRRELVM v, SQInteger idx)
static SQInteger string_slice(HSQUIRRELVM v)
static SQInteger array_extend(HSQUIRRELVM v)
static SQInteger base_enabledebuginfo(HSQUIRRELVM v)
static SQInteger base_getroottable(HSQUIRRELVM v)
static SQInteger closure_acall(HSQUIRRELVM v)
SQRESULT sq_getattributes(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_suspendvm(HSQUIRRELVM v)
static SQInteger base_assert(HSQUIRRELVM v)
SQRESULT sq_setparamscheck(HSQUIRRELVM v, SQInteger nparamscheck, const SQChar *typemask)
bool _hsort_sift_down(HSQUIRRELVM v, SQArray *arr, int root, int bottom, SQInteger func)
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
SQRESULT sq_getclass(HSQUIRRELVM v, SQInteger idx)
static SQInteger closure_pcall(HSQUIRRELVM v)
static SQInteger array_slice(HSQUIRRELVM v)
SQRESULT sq_getstring(HSQUIRRELVM v, SQInteger idx, const SQChar **c)
static SQInteger thread_getstatus(HSQUIRRELVM v)
void sq_seterrorhandler(HSQUIRRELVM v)
const SQChar * sq_getlocal(HSQUIRRELVM v, SQUnsignedInteger level, SQUnsignedInteger idx)
static SQInteger default_delegate_tointeger(HSQUIRRELVM v)
static SQRegFunction base_funcs[]
static SQInteger base_suspend(HSQUIRRELVM v)
static SQInteger base_getconsttable(HSQUIRRELVM v)
static SQInteger base_array(HSQUIRRELVM v)
void sq_pushbool(HSQUIRRELVM v, SQBool b)
void sq_weakref(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
static SQInteger default_delegate_len(HSQUIRRELVM v)
SQRESULT sq_arrayreverse(HSQUIRRELVM v, SQInteger idx)
static SQInteger array_resize(HSQUIRRELVM v)
static SQInteger closure_bindenv(HSQUIRRELVM v)
static SQInteger _closure_acall(HSQUIRRELVM v, SQBool raiseerror)
static SQInteger table_rawset(HSQUIRRELVM v)
bool _hsort(HSQUIRRELVM v, SQObjectPtr &arr, SQInteger l, SQInteger r, SQInteger func)
static SQInteger base_setconsttable(HSQUIRRELVM v)
SQRESULT sq_compilebuffer(HSQUIRRELVM v, const SQChar *s, SQInteger size, const SQChar *sourcename, SQBool raiseerror)
static SQInteger base_type(HSQUIRRELVM v)
static SQInteger table_rawdelete(HSQUIRRELVM v)
SQRESULT sq_createinstance(HSQUIRRELVM v, SQInteger idx)
void sq_move(HSQUIRRELVM dest, HSQUIRRELVM src, SQInteger idx)
static SQInteger thread_wakeup(HSQUIRRELVM v)
static SQInteger array_pop(HSQUIRRELVM v)