# sqvm.cpp

    00001 /*
    00002   see copyright notice in squirrel.h
    00003 */
    00004 #include <squirrel.h>
    00005 #include "sqpcheader.h"
    00006 #include <math.h>
    00007 #include <stdlib.h>
    00008 #include "sqopcodes.h"
    00009 #include "sqfuncproto.h"
    00010 #include "sqvm.h"
    00011 #include "sqclosure.h"
    00012 #include "sqstring.h"
    00013 #include "sqtable.h"
    00014 #include "squserdata.h"
    00015 #include "sqarray.h"
    00016 #include "sqclass.h"
    00017
    00018 #define TOP() (_stack._vals[_top-1])
    00019
    00020 bool SQVM::BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2)
    00021 {
    00022   SQInteger res;
    00023   SQInteger i1 = _integer(o1), i2 = _integer(o2);
    00024   if((type(o1)==OT_INTEGER) && (type(o2)==OT_INTEGER))
    00025   {
    00026     switch(op) {
    00027       case BW_AND:  res = i1 & i2; break;
    00028       case BW_OR:   res = i1 | i2; break;
    00029       case BW_XOR:  res = i1 ^ i2; break;
    00030       case BW_SHIFTL: res = i1 << i2; break;
    00031       case BW_SHIFTR: res = i1 >> i2; break;
    00032       case BW_USHIFTR:res = (SQInteger)(*((SQUnsignedInteger*)&i1) >> i2); break;
    00033       default: { Raise_Error(_SC("internal vm error bitwise op failed")); return false; }
    00034     }
    00035   }
    00036   else { Raise_Error(_SC("bitwise op between '%s' and '%s'"),GetTypeName(o1),GetTypeName(o2)); return false;}
    00037   trg = res;
    00038   return true;
    00039 }
    00040
    00041 bool SQVM::ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2)
    00042 {
    00043   if(sq_isnumeric(o1) && sq_isnumeric(o2)) {
    00044       if((type(o1)==OT_INTEGER) && (type(o2)==OT_INTEGER)) {
    00045         SQInteger res, i1 = _integer(o1), i2 = _integer(o2);
    00046         switch(op) {
    00047         case '+': res = i1 + i2; break;
    00048         case '-': res = i1 - i2; break;
    00049         case '/': if(i2 == 0) { Raise_Error(_SC("division by zero")); return false; }
    00050           res = i1 / i2;
    00051           break;
    00052         case '*': res = i1 * i2; break;
    00053         case '%': res = i1 % i2; break;
    00054         default: res = 0xDEADBEEF;
    00055         }
    00056         trg = res;
    00057       }else{
    00058         SQFloat res, f1 = tofloat(o1), f2 = tofloat(o2);
    00059         switch(op) {
    00060         case '+': res = f1 + f2; break;
    00061         case '-': res = f1 - f2; break;
    00062         case '/': res = f1 / f2; break;
    00063         case '*': res = f1 * f2; break;
    00064         case '%': res = SQFloat(fmod((double)f1,(double)f2)); break;
    00065         default: res = 0x0f;
    00066         }
    00067         trg = res;
    00068       }
    00069     } else {
    00070       if(op == '+' && (type(o1) == OT_STRING || type(o2) == OT_STRING)){
    00071           if(!StringCat(o1, o2, trg)) return false;
    00072       }
    00073       else if(!ArithMetaMethod(op,o1,o2,trg)) {
    00074         Raise_Error(_SC("arith op %c on between '%s' and '%s'"),op,GetTypeName(o1),GetTypeName(o2)); return false;
    00075       }
    00076     }
    00077     return true;
    00078 }
    00079
    00080 SQVM::SQVM(SQSharedState *ss)
    00081 {
    00082   _sharedstate=ss;
    00083   _suspended = SQFalse;
    00084   _suspended_target=-1;
    00085   _suspended_root = SQFalse;
    00086   _suspended_traps=0;
    00087   _foreignptr=NULL;
    00088   _nnativecalls=0;
    00089   _lasterror = _null_;
    00090   _errorhandler = _null_;
    00091   _debughook = _null_;
    00092   _can_suspend = false;
    00093   _ops_till_suspend = 0;
    00094   ci = NULL;
    00095   INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);
    00096 }
    00097
    00098 void SQVM::Finalize()
    00099 {
    00100   _roottable = _null_;
    00101   _lasterror = _null_;
    00102   _errorhandler = _null_;
    00103   _debughook = _null_;
    00104   temp_reg = _null_;
    00105   _callstackdata.resize(0);
    00106   SQInteger size=_stack.size();
    00107   for(SQInteger i=size - 1;i>=0;i--)
    00108     _stack[i]=_null_;
    00109 }
    00110
    00111 SQVM::~SQVM()
    00112 {
    00113   Finalize();
    00114   //sq_free(_callsstack,_alloccallsstacksize*sizeof(CallInfo));
    00115   REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
    00116 }
    00117
    00118 bool SQVM::ArithMetaMethod(SQInteger op,const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &dest)
    00119 {
    00120   SQMetaMethod mm;
    00121   switch(op){
    00122     case _SC('+'): mm=MT_ADD; break;
    00123     case _SC('-'): mm=MT_SUB; break;
    00124     case _SC('/'): mm=MT_DIV; break;
    00125     case _SC('*'): mm=MT_MUL; break;
    00126     case _SC('%'): mm=MT_MODULO; break;
    00127     default: mm = MT_ADD; assert(0); break; //shutup compiler
    00128   }
    00129   if(is_delegable(o1) && _delegable(o1)->_delegate) {
    00130     Push(o1);Push(o2);
    00131     return CallMetaMethod(_delegable(o1),mm,2,dest);
    00132   }
    00133   return false;
    00134 }
    00135
    00136 bool SQVM::NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o)
    00137 {
    00138
    00139   switch(type(o)) {
    00140   case OT_INTEGER:
    00141     trg = -_integer(o);
    00142     return true;
    00143   case OT_FLOAT:
    00144     trg = -_float(o);
    00145     return true;
    00146   case OT_TABLE:
    00147   case OT_USERDATA:
    00148   case OT_INSTANCE:
    00149     if(_delegable(o)->_delegate) {
    00150       Push(o);
    00151       if(CallMetaMethod(_delegable(o), MT_UNM, 1, temp_reg)) {
    00152         trg = temp_reg;
    00153         return true;
    00154       }
    00155     }
    00156   default:break; //shutup compiler
    00157   }
    00158   Raise_Error(_SC("attempt to negate a %s"), GetTypeName(o));
    00159   return false;
    00160 }
    00161
    00162 #define _RET_SUCCEED(exp) { result = (exp); return true; }
    00163 bool SQVM::ObjCmp(const SQObjectPtr &o1,const SQObjectPtr &o2,SQInteger &result)
    00164 {
    00165   if(type(o1)==type(o2)){
    00166     if(_userpointer(o1)==_userpointer(o2))_RET_SUCCEED(0);
    00167     SQObjectPtr res;
    00168     switch(type(o1)){
    00169     case OT_STRING:
    00170       _RET_SUCCEED(scstrcmp(_stringval(o1),_stringval(o2)));
    00171     case OT_INTEGER:
    00172       _RET_SUCCEED(_integer(o1)-_integer(o2));
    00173     case OT_FLOAT:
    00174       _RET_SUCCEED((_float(o1)<_float(o2))?-1:1);
    00175     case OT_TABLE:
    00176     case OT_USERDATA:
    00177     case OT_INSTANCE:
    00178       if(_delegable(o1)->_delegate) {
    00179         Push(o1);Push(o2);
    00180         if(CallMetaMethod(_delegable(o1),MT_CMP,2,res)) break;
    00181       }
    00182       //continues through (no break needed)
    00183     default:
    00184       _RET_SUCCEED( _userpointer(o1) < _userpointer(o2)?-1:1 );
    00185     }
    00186     if(type(res)!=OT_INTEGER) { Raise_CompareError(o1,o2); return false; }
    00187       _RET_SUCCEED(_integer(res));
    00188
    00189   }
    00190   else{
    00191     if(sq_isnumeric(o1) && sq_isnumeric(o2)){
    00192       if((type(o1)==OT_INTEGER) && (type(o2)==OT_FLOAT)) {
    00193         if( _integer(o1)==_float(o2) ) { _RET_SUCCEED(0); }
    00194         else if( _integer(o1)<_float(o2) ) { _RET_SUCCEED(-1); }
    00195         _RET_SUCCEED(1);
    00196       }
    00197       else{
    00198         if( _float(o1)==_integer(o2) ) { _RET_SUCCEED(0); }
    00199         else if( _float(o1)<_integer(o2) ) { _RET_SUCCEED(-1); }
    00200         _RET_SUCCEED(1);
    00201       }
    00202     }
    00203     else if(type(o1)==OT_NULL) {_RET_SUCCEED(-1);}
    00204     else if(type(o2)==OT_NULL) {_RET_SUCCEED(1);}
    00205     else { Raise_CompareError(o1,o2); return false; }
    00206
    00207   }
    00208   assert(0);
    00209   _RET_SUCCEED(0); //cannot happen
    00210 }
    00211
    00212 bool SQVM::CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res)
    00213 {
    00214   SQInteger r;
    00215   if(ObjCmp(o1,o2,r)) {
    00216     switch(op) {
    00217       case CMP_G: res = (r > 0)?_true_:_false_; return true;
    00218       case CMP_GE: res = (r >= 0)?_true_:_false_; return true;
    00219       case CMP_L: res = (r < 0)?_true_:_false_; return true;
    00220       case CMP_LE: res = (r <= 0)?_true_:_false_; return true;
    00221
    00222     }
    00223     assert(0);
    00224   }
    00225   return false;
    00226 }
    00227
    00228 void SQVM::ToString(const SQObjectPtr &o,SQObjectPtr &res)
    00229 {
    00230   switch(type(o)) {
    00231   case OT_STRING:
    00232     res = o;
    00233     return;
    00234   case OT_FLOAT:
    00235     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)),_SC("%g"),_float(o));
    00236     break;
    00237   case OT_INTEGER:
    00238 #if defined(_SQ64)
    00239     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)),_SC("%ld"),_integer(o));
    00240 #else
    00241     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)),_SC("%d"),_integer(o));
    00242 #endif
    00243     break;
    00244   case OT_BOOL:
    00245     scsprintf(_sp(rsl(6)),_integer(o)?_SC("true"):_SC("false"));
    00246     break;
    00247   case OT_TABLE:
    00248   case OT_USERDATA:
    00249   case OT_INSTANCE:
    00250     if(_delegable(o)->_delegate) {
    00251       Push(o);
    00252       if(CallMetaMethod(_delegable(o),MT_TOSTRING,1,res)) {
    00253         if(type(res) == OT_STRING)
    00254           return;
    00255         //else keeps going to the default
    00256       }
    00257     }
    00258   default:
    00259     scsprintf(_sp(rsl(sizeof(void*)+20)),_SC("(%s : 0x%p)"),GetTypeName(o),(void*)_rawval(o));
    00260   }
    00261   res = SQString::Create(_ss(this),_spval);
    00262 }
    00263
    00264
    00265 bool SQVM::StringCat(const SQObjectPtr &str,const SQObjectPtr &obj,SQObjectPtr &dest)
    00266 {
    00267   SQObjectPtr a, b;
    00268   ToString(str, a);
    00269   ToString(obj, b);
    00270   SQInteger l = _string(a)->_len , ol = _string(b)->_len;
    00271   SQChar *s = _sp(rsl(l + ol + 1));
    00272   memcpy(s, _stringval(a), rsl(l));
    00273   memcpy(s + l, _stringval(b), rsl(ol));
    00274   dest = SQString::Create(_ss(this), _spval, l + ol);
    00275   return true;
    00276 }
    00277
    00278 void SQVM::TypeOf(const SQObjectPtr &obj1,SQObjectPtr &dest)
    00279 {
    00280   if(is_delegable(obj1) && _delegable(obj1)->_delegate) {
    00281     Push(obj1);
    00282     if(CallMetaMethod(_delegable(obj1),MT_TYPEOF,1,dest))
    00283       return;
    00284   }
    00285   dest = SQString::Create(_ss(this),GetTypeName(obj1));
    00286 }
    00287
    00288 bool SQVM::Init(SQVM *friendvm, SQInteger stacksize)
    00289 {
    00290   _stack.resize(stacksize);
    00291   //_callsstack.reserve(4);
    00292   _alloccallsstacksize = 4;
    00293   _callstackdata.resize(_alloccallsstacksize);
    00294   _callsstacksize = 0;
    00295   _callsstack = &_callstackdata[0];
    00296   //_callsstack = (CallInfo*)sq_malloc(_alloccallsstacksize*sizeof(CallInfo));
    00297   _stackbase = 0;
    00298   _top = 0;
    00299   if(!friendvm)
    00300     _roottable = SQTable::Create(_ss(this), 0);
    00301   else {
    00302     _roottable = friendvm->_roottable;
    00303     _errorhandler = friendvm->_errorhandler;
    00304     _debughook = friendvm->_debughook;
    00305   }
    00306
    00307   sq_base_register(this);
    00308   return true;
    00309 }
    00310
    00311 extern SQInstructionDesc g_InstrDesc[];
    00312
    00313 bool SQVM::StartCall(SQClosure *closure,SQInteger target,SQInteger args,SQInteger stackbase,bool tailcall)
    00314 {
    00315   SQFunctionProto *func = _funcproto(closure->_function);
    00316
    00317   const SQInteger paramssize = func->_nparameters;
    00318   const SQInteger newtop = stackbase + func->_stacksize;
    00319   SQInteger nargs = args;
    00320   if (paramssize != nargs) {
    00321     SQInteger ndef = func->_ndefaultparams;
    00322     if(ndef && nargs < paramssize) {
    00323       SQInteger diff = paramssize - nargs;
    00324       for(SQInteger n = ndef - diff; n < ndef; n++) {
    00325         _stack._vals[stackbase + (nargs++)] = closure->_defaultparams[n];
    00326       }
    00327     }
    00328     else if(func->_varparams)
    00329     {
    00330       if (nargs < paramssize) {
    00331         Raise_Error(_SC("wrong number of parameters"));
    00332         return false;
    00333       }
    00334       for(SQInteger n = 0; n < nargs - paramssize; n++) {
    00335         _vargsstack.push_back(_stack._vals[stackbase+paramssize+n]);
    00336         _stack._vals[stackbase+paramssize+n] = _null_;
    00337       }
    00338     }
    00339     else {
    00340       Raise_Error(_SC("wrong number of parameters"));
    00341       return false;
    00342     }
    00343   }
    00344
    00345   if(type(closure->_env) == OT_WEAKREF) {
    00346     _stack._vals[stackbase] = _weakref(closure->_env)->_obj;
    00347   }
    00348
    00349   if (!tailcall) {
    00350     CallInfo lc;
    00351     memset(&lc, 0, sizeof(lc));
    00352     lc._generator = NULL;
    00353     lc._etraps = 0;
    00354     lc._prevstkbase = (SQInt32) ( stackbase - _stackbase );
    00355     lc._target = (SQInt32) target;
    00356     lc._prevtop = (SQInt32) (_top - _stackbase);
    00357     lc._ncalls = 1;
    00358     lc._root = SQFalse;
    00359     PUSH_CALLINFO(this, lc);
    00360   }
    00361   else {
    00362     ci->_ncalls++;
    00363   }
    00364   ci->_vargs.size = (SQInt32)(nargs - paramssize);
    00365   ci->_vargs.base = (SQInt32)(_vargsstack.size()-(ci->_vargs.size));
    00366   ci->_closure = closure;
    00367   ci->_literals = func->_literals;
    00368   ci->_ip = func->_instructions;
    00369   //grows the stack if needed
    00370   if (((SQUnsignedInteger)newtop + (func->_stacksize<<1)) > _stack.size()) {
    00371     _stack.resize(_stack.size() + (func->_stacksize<<1));
    00372   }
    00373
    00374   _top = newtop;
    00375   _stackbase = stackbase;
    00376   if (type(_debughook) != OT_NULL && _rawval(_debughook) != _rawval(ci->_closure))
    00377     CallDebugHook(_SC('c'));
    00378   return true;
    00379 }
    00380
    00381 bool SQVM::Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval)
    00382 {
    00383   if (type(_debughook) != OT_NULL && _rawval(_debughook) != _rawval(ci->_closure))
    00384     for(SQInteger i=0;i<ci->_ncalls;i++)
    00385       CallDebugHook(_SC('r'));
    00386
    00387   SQBool broot = ci->_root;
    00388   SQInteger last_top = _top;
    00389   SQInteger target = ci->_target;
    00390   SQInteger oldstackbase = _stackbase;
    00391   _stackbase -= ci->_prevstkbase;
    00392   _top = _stackbase + ci->_prevtop;
    00393   if(ci->_vargs.size) PopVarArgs(ci->_vargs);
    00394   POP_CALLINFO(this);
    00395   if (broot) {
    00396     if (_arg0 != MAX_FUNC_STACKSIZE) retval = _stack._vals[oldstackbase+_arg1];
    00397     else retval = _null_;
    00398   }
    00399   else {
    00400     if(target != -1) { //-1 is when a class contructor ret value has to be ignored
    00401       if (_arg0 != MAX_FUNC_STACKSIZE)
    00402         STK(target) = _stack._vals[oldstackbase+_arg1];
    00403       else
    00404         STK(target) = _null_;
    00405     }
    00406   }
    00407
    00408   while (last_top > oldstackbase) _stack._vals[last_top--].Null();
    00409   assert(oldstackbase >= _stackbase);
    00410   return broot?true:false;
    00411 }
    00412
    00413 #define _RET_ON_FAIL(exp) { if(!exp) return false; }
    00414
    00415 bool SQVM::LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr)
    00416 {
    00417   _RET_ON_FAIL(ARITH_OP( op , target, a, incr));
    00418   a = target;
    00419   return true;
    00420 }
    00421
    00422 bool SQVM::PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr)
    00423 {
    00424   SQObjectPtr trg;
    00425   _RET_ON_FAIL(ARITH_OP( op , trg, a, incr));
    00426   target = a;
    00427   a = trg;
    00428   return true;
    00429 }
    00430
    00431 bool SQVM::DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix)
    00432 {
    00433   SQObjectPtr tmp, tself = self, tkey = key;
    00434   if (!Get(tself, tkey, tmp, false, true)) { Raise_IdxError(tkey); return false; }
    00435   _RET_ON_FAIL(ARITH_OP( op , target, tmp, incr))
    00436   Set(tself, tkey, target,true);
    00437   if (postfix) target = tmp;
    00438   return true;
    00439 }
    00440
    00441 #define arg0 (_i_._arg0)
    00442 #define arg1 (_i_._arg1)
    00443 #define sarg1 (*((SQInt32 *)&_i_._arg1))
    00444 #define arg2 (_i_._arg2)
    00445 #define arg3 (_i_._arg3)
    00446 #define sarg3 ((SQInteger)*((signed char *)&_i_._arg3))
    00447
    00448 SQRESULT SQVM::Suspend()
    00449 {
    00450   if (_suspended)
    00451     return sq_throwerror(this, _SC("cannot suspend an already suspended vm"));
    00452   if (_nnativecalls!=2)
    00453     return sq_throwerror(this, _SC("cannot suspend through native calls/metamethods"));
    00454   return SQ_SUSPEND_FLAG;
    00455 }
    00456
    00457 void SQVM::PopVarArgs(VarArgs &vargs)
    00458 {
    00459   for(SQInteger n = 0; n< vargs.size; n++)
    00460     _vargsstack.pop_back();
    00461 }
    00462
    00463 #define _FINISH(howmuchtojump) {jump = howmuchtojump; return true; }
    00464 bool SQVM::FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr
    00465 &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump)
    00466 {
    00467   SQInteger nrefidx;
    00468   switch(type(o1)) {
    00469   case OT_TABLE:
    00470     if((nrefidx = _table(o1)->Next(false,o4, o2, o3)) == -1) _FINISH(exitpos);
    00471     o4 = (SQInteger)nrefidx; _FINISH(1);
    00472   case OT_ARRAY:
    00473     if((nrefidx = _array(o1)->Next(o4, o2, o3)) == -1) _FINISH(exitpos);
    00474     o4 = (SQInteger) nrefidx; _FINISH(1);
    00475   case OT_STRING:
    00476     if((nrefidx = _string(o1)->Next(o4, o2, o3)) == -1)_FINISH(exitpos);
    00477     o4 = (SQInteger)nrefidx; _FINISH(1);
    00478   case OT_CLASS:
    00479     if((nrefidx = _class(o1)->Next(o4, o2, o3)) == -1)_FINISH(exitpos);
    00480     o4 = (SQInteger)nrefidx; _FINISH(1);
    00481   case OT_USERDATA:
    00482   case OT_INSTANCE:
    00483     if(_delegable(o1)->_delegate) {
    00484       SQObjectPtr itr;
    00485       Push(o1);
    00486       Push(o4);
    00487       if(CallMetaMethod(_delegable(o1), MT_NEXTI, 2, itr)){
    00488         o4 = o2 = itr;
    00489         if(type(itr) == OT_NULL) _FINISH(exitpos);
    00490         if(!Get(o1, itr, o3, false,false)) {
    00491           Raise_Error(_SC("_nexti returned an invalid idx"));
    00492           return false;
    00493         }
    00494         _FINISH(1);
    00495       }
    00496       Raise_Error(_SC("_nexti failed"));
    00497       return false;
    00498     }
    00499     break;
    00500   case OT_GENERATOR:
    00501     if(_generator(o1)->_state == SQGenerator::eDead) _FINISH(exitpos);
    00502     if(_generator(o1)->_state == SQGenerator::eSuspended) {
    00503       SQInteger idx = 0;
    00504       if(type(o4) == OT_INTEGER) {
    00505         idx = _integer(o4) + 1;
    00506       }
    00507       o2 = idx;
    00508       o4 = idx;
    00509       _generator(o1)->Resume(this, arg_2+1);
    00510       _FINISH(0);
    00511     }
    00512   default:
    00513     Raise_Error(_SC("cannot iterate %s"), GetTypeName(o1));
    00514   }
    00515   return false; //cannot be hit(just to avoid warnings)
    00516 }
    00517
    00518 bool SQVM::DELEGATE_OP(SQObjectPtr &trg,SQObjectPtr &o1,SQObjectPtr &o2)
    00519 {
    00520   if(type(o1) != OT_TABLE) { Raise_Error(_SC("delegating a '%s'"), GetTypeName(o1)); return false; }
    00521   switch(type(o2)) {
    00522   case OT_TABLE:
    00523     if(!_table(o1)->SetDelegate(_table(o2))){
    00524       Raise_Error(_SC("delegate cycle detected"));
    00525       return false;
    00526     }
    00527     break;
    00528   case OT_NULL:
    00529     _table(o1)->SetDelegate(NULL);
    00530     break;
    00531   default:
    00532     Raise_Error(_SC("using '%s' as delegate"), GetTypeName(o2));
    00533     return false;
    00534     break;
    00535   }
    00536   trg = o1;
    00537   return true;
    00538 }
    00539 #define COND_LITERAL (arg3!=0?ci->_literals[arg1]:STK(arg1))
    00540
    00541 #define _GUARD(exp) { if(!exp) { Raise_Error(_lasterror); SQ_THROW();} }
    00542
    00543 #define SQ_THROW() { goto exception_trap; }
    00544
    00545 bool SQVM::CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func)
    00546 {
    00547   SQInteger nouters;
    00548   SQClosure *closure = SQClosure::Create(_ss(this), func);
    00549   if((nouters = func->_noutervalues)) {
    00550     closure->_outervalues.reserve(nouters);
    00551     for(SQInteger i = 0; i<nouters; i++) {
    00552       SQOuterVar &v = func->_outervalues[i];
    00553       switch(v._type){
    00554       case otSYMBOL:
    00555         closure->_outervalues.push_back(_null_);
    00556         if(!Get(_stack._vals[_stackbase]/*STK(0)*/, v._src, closure->_outervalues.top(), false,true))
    00557         {Raise_IdxError(v._src); return false; }
    00558         break;
    00559       case otLOCAL:
    00560         closure->_outervalues.push_back(_stack._vals[_stackbase+_integer(v._src)]);
    00561         break;
    00562       case otOUTER:
    00563         closure->_outervalues.push_back(_closure(ci->_closure)->_outervalues[_integer(v._src)]);
    00564         break;
    00565       }
    00566     }
    00567   }
    00568   SQInteger ndefparams;
    00569   if((ndefparams = func->_ndefaultparams)) {
    00570     closure->_defaultparams.reserve(ndefparams);
    00571     for(SQInteger i = 0; i < ndefparams; i++) {
    00572       SQInteger spos = func->_defaultparams[i];
    00573       closure->_defaultparams.push_back(_stack._vals[_stackbase + spos]);
    00574     }
    00575   }
    00576   target = closure;
    00577   return true;
    00578
    00579 }
    00580
    00581 bool SQVM::GETVARGV_OP(SQObjectPtr &target,SQObjectPtr &index,CallInfo *ci)
    00582 {
    00583   if(ci->_vargs.size == 0) {
    00584     Raise_Error(_SC("the function doesn't have var args"));
    00585     return false;
    00586   }
    00587   if(!sq_isnumeric(index)){
    00588     Raise_Error(_SC("indexing 'vargv' with %s"),GetTypeName(index));
    00589     return false;
    00590   }
    00591   SQInteger idx = tointeger(index);
    00592   if(idx < 0 || idx >= ci->_vargs.size){ Raise_Error(_SC("vargv index out of range")); return false; }
    00593   target = _vargsstack[ci->_vargs.base+idx];
    00594   return true;
    00595 }
    00596
    00597 bool SQVM::CLASS_OP(SQObjectPtr &target,SQInteger baseclass,SQInteger attributes)
    00598 {
    00599   SQClass *base = NULL;
    00600   SQObjectPtr attrs;
    00601   if(baseclass != -1) {
    00602     if(type(_stack._vals[_stackbase+baseclass]) != OT_CLASS) { Raise_Error(_SC("trying to inherit from a %s"),GetTypeName(_stack._vals[_stackbase+baseclass])); return false; }
    00603     base = _class(_stack._vals[_stackbase + baseclass]);
    00604   }
    00605   if(attributes != MAX_FUNC_STACKSIZE) {
    00606     attrs = _stack._vals[_stackbase+attributes];
    00607   }
    00608   target = SQClass::Create(_ss(this),base);
    00609   if(type(_class(target)->_metamethods[MT_INHERITED]) != OT_NULL) {
    00610     int nparams = 2;
    00611     SQObjectPtr ret;
    00612     Push(target); Push(attrs);
    00613     Call(_class(target)->_metamethods[MT_INHERITED],nparams,_top - nparams, ret, false, false);
    00614     Pop(nparams);
    00615   }
    00616   _class(target)->_attributes = attrs;
    00617   return true;
    00618 }
    00619
    00620
    00621
    00622 bool SQVM::IsEqual(SQObjectPtr &o1,SQObjectPtr &o2,bool &res)
    00623 {
    00624   if(type(o1) == type(o2)) {
    00625     res = ((_userpointer(o1) == _userpointer(o2)?true:false));
    00626   }
    00627   else {
    00628     if(sq_isnumeric(o1) && sq_isnumeric(o2)) {
    00629       SQInteger cmpres;
    00630       if(!ObjCmp(o1, o2,cmpres)) return false;
    00631       res = (cmpres == 0);
    00632     }
    00633     else {
    00634       res = false;
    00635     }
    00636   }
    00637   return true;
    00638 }
    00639
    00640 bool SQVM::IsFalse(SQObjectPtr &o)
    00641 {
    00642   if(((type(o) & SQOBJECT_CANBEFALSE) && ( (type(o) == OT_FLOAT) && (_float(o) == SQFloat(0.0)) ))
    00643     || (_integer(o) == 0) ) { //OT_NULL|OT_INTEGER|OT_BOOL
    00644     return true;
    00645   }
    00646   return false;
    00647 }
    00648
    00649 bool SQVM::GETPARENT_OP(SQObjectPtr &o,SQObjectPtr &target)
    00650 {
    00651   switch(type(o)) {
    00652     case OT_TABLE: target = _table(o)->_delegate?SQObjectPtr(_table(o)->_delegate):_null_;
    00653       break;
    00654     case OT_CLASS: target = _class(o)->_base?_class(o)->_base:_null_;
    00655       break;
    00656     default:
    00657       Raise_Error(_SC("the %s type doesn't have a parent slot"), GetTypeName(o));
    00658       return false;
    00659   }
    00660   return true;
    00661 }
    00662
    00663 bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQInteger stackbase,SQObjectPtr &outres, SQBool raiseerror,ExecutionType et)
    00664 {
    00665   if ((_nnativecalls + 1) > MAX_NATIVE_CALLS) { Raise_Error(_SC("Native stack overflow")); return false; }
    00666   _nnativecalls++;
    00667   AutoDec ad(&_nnativecalls);
    00668   SQInteger traps = 0;
    00669   //temp_reg vars for OP_CALL
    00670   SQInteger ct_target;
    00671   SQInteger ct_stackbase;
    00672   bool ct_tailcall;
    00673
    00674   switch(et) {
    00675     case ET_CALL:
    00676       if(!StartCall(_closure(closure), _top - nargs, nargs, stackbase, false)) {
    00677         //call the handler if there are no calls in the stack, if not relies on the previous node
    00678         if(ci == NULL) CallErrorHandler(_lasterror);
    00679         return false;
    00680       }
    00681       ci->_root = SQTrue;
    00682       break;
    00683     case ET_RESUME_GENERATOR: _generator(closure)->Resume(this, target); ci->_root = SQTrue; traps += ci->_etraps; break;
    00684     case ET_RESUME_VM:
    00685       traps = _suspended_traps;
    00686       ci->_root = _suspended_root;
    00687       ci->_vargs = _suspend_varargs;
    00688       _suspended = SQFalse;
    00689       break;
    00690     case ET_RESUME_OPENTTD:
    00691       traps = _suspended_traps;
    00692       _suspended = SQFalse;
    00693       break;
    00694   }
    00695
    00696 exception_restore:
    00697   //
    00698   {
    00699     for(;;)
    00700     {
    00701       DecreaseOps(1);
    00702       if (ShouldSuspend()) { _suspended = SQTrue; _suspended_traps = traps; return true; }
    00703
    00704       const SQInstruction &_i_ = *ci->_ip++;
    00705       //dumpstack(_stackbase);
    00706       //scprintf("%s %d %d %d %d\n",g_InstrDesc[_i_.op].name,arg0,arg1,arg2,arg3);
    00707       switch(_i_.op)
    00708       {
    00709       case _OP_LINE:
    00710         if(type(_debughook) != OT_NULL && _rawval(_debughook) != _rawval(ci->_closure))
    00711           CallDebugHook(_SC('l'),arg1);
    00712         continue;
    00713       case _OP_LOAD: TARGET = ci->_literals[arg1]; continue;
    00714       case _OP_LOADINT: TARGET = (SQInteger)arg1; continue;
    00715       case _OP_LOADFLOAT: TARGET = *((SQFloat *)&arg1); continue;
    00716       case _OP_DLOAD: TARGET = ci->_literals[arg1]; STK(arg2) = ci->_literals[arg3];continue;
    00717       case _OP_TAILCALL:
    00718         temp_reg = STK(arg1);
    00719         if (type(temp_reg) == OT_CLOSURE){
    00720           ct_tailcall = true;
    00721           if(ci->_vargs.size) PopVarArgs(ci->_vargs);
    00722           for (SQInteger i = 0; i < arg3; i++) STK(i) = STK(arg2 + i);
    00723           ct_target = ci->_target;
    00724           ct_stackbase = _stackbase;
    00725           goto common_call;
    00726         }
    00727       case _OP_CALL: {
    00728           ct_tailcall = false;
    00729           ct_target = arg0;
    00730           temp_reg = STK(arg1);
    00731           ct_stackbase = _stackbase+arg2;
    00732
    00733 common_call:
    00734           SQObjectPtr clo = temp_reg;
    00735           SQInteger last_top = _top;
    00736           switch (type(clo)) {
    00737           case OT_CLOSURE:{
    00738             _GUARD(StartCall(_closure(clo), ct_target, arg3, ct_stackbase, ct_tailcall));
    00739             if (_funcproto(_closure(clo)->_function)->_bgenerator) {
    00740               SQGenerator *gen = SQGenerator::Create(_ss(this), _closure(clo));
    00741               _GUARD(gen->Yield(this));
    00742               Return(1, ct_target, clo);
    00743               STK(ct_target) = gen;
    00744               while (last_top >= _top) _stack._vals[last_top--].Null();
    00745               continue;
    00746             }
    00747             }
    00748             continue;
    00749           case OT_NATIVECLOSURE: {
    00750             bool suspend;
    00751             _suspended_target = ct_target;
    00752             try {
    00753               _GUARD(CallNative(_nativeclosure(clo), arg3, ct_stackbase, clo,suspend));
    00754             } catch (...) {
    00755               _suspended = SQTrue;
    00756               _suspended_target = ct_target;
    00757               _suspended_root = ci->_root;
    00758               _suspended_traps = traps;
    00759               _suspend_varargs = ci->_vargs;
    00760               throw;
    00761             }
    00762             if(suspend){
    00763               _suspended = SQTrue;
    00764               _suspended_target = ct_target;
    00765               _suspended_root = ci->_root;
    00766               _suspended_traps = traps;
    00767               _suspend_varargs = ci->_vargs;
    00768               outres = clo;
    00769               return true;
    00770             }
    00771             if(ct_target != -1) { //skip return value for constructors
    00772               STK(ct_target) = clo;
    00773             }
    00774                        }
    00775             continue;
    00776           case OT_CLASS:{
    00777             SQObjectPtr inst;
    00778             _GUARD(CreateClassInstance(_class(clo),inst,temp_reg));
    00779             STK(ct_target) = inst;
    00780             ct_target = -1; //fakes return value target so that is not overwritten by the constructor
    00781             if(type(temp_reg) != OT_NULL) {
    00782               _stack._vals[ct_stackbase] = inst;
    00783               goto common_call; //hard core spaghetti code(reissues the OP_CALL to invoke the constructor)
    00784             }
    00785             }
    00786             break;
    00787           case OT_TABLE:
    00788           case OT_USERDATA:
    00789           case OT_INSTANCE:
    00790             {
    00791             Push(clo);
    00792             for (SQInteger i = 0; i < arg3; i++) Push(STK(arg2 + i));
    00793             if (_delegable(clo) && CallMetaMethod(_delegable(clo), MT_CALL, arg3+1, clo)){
    00794               STK(ct_target) = clo;
    00795               break;
    00796             }
    00797             Raise_Error(_SC("attempt to call '%s'"), GetTypeName(clo));
    00798             SQ_THROW();
    00799             }
    00800           default:
    00801             Raise_Error(_SC("attempt to call '%s'"), GetTypeName(clo));
    00802             SQ_THROW();
    00803           }
    00804         }
    00805           continue;
    00806       case _OP_PREPCALL:
    00807       case _OP_PREPCALLK:
    00808         {
    00809           SQObjectPtr &key = _i_.op == _OP_PREPCALLK?(ci->_literals)[arg1]:STK(arg1);
    00810           SQObjectPtr &o = STK(arg2);
    00811           if (!Get(o, key, temp_reg,false,true)) {
    00812             if(type(o) == OT_CLASS) { //hack?
    00813               if(_class_ddel->Get(key,temp_reg)) {
    00814                 STK(arg3) = o;
    00815                 TARGET = temp_reg;
    00816                 continue;
    00817               }
    00818             }
    00819             { Raise_IdxError(key); SQ_THROW();}
    00820           }
    00821
    00822           STK(arg3) = type(o) == OT_CLASS?STK(0):o;
    00823           TARGET = temp_reg;
    00824         }
    00825         continue;
    00826       case _OP_SCOPE_END:
    00827       {
    00828         SQInteger from = arg0;
    00829         SQInteger count = arg1 - arg0 + 2;
    00830         /* When 'return' is executed, it happens that the stack is already cleaned
    00831          *  (by Return()), but this OP-code is still executed. So check for this
    00832          *  situation, and ignore the cleanup */
    00833         if (_stackbase + count + from <= _top) {
    00834           while (--count >= 0) _stack._vals[_stackbase + count + from].Null();
    00835         }
    00836       } continue;
    00837       case _OP_GETK:
    00838         if (!Get(STK(arg2), ci->_literals[arg1], temp_reg, false,true)) { Raise_IdxError(ci->_literals[arg1]); SQ_THROW();}
    00839         TARGET = temp_reg;
    00840         continue;
    00841       case _OP_MOVE: TARGET = STK(arg1); continue;
    00842       case _OP_NEWSLOT:
    00843         _GUARD(NewSlot(STK(arg1), STK(arg2), STK(arg3),false));
    00844         if(arg0 != arg3) TARGET = STK(arg3);
    00845         continue;
    00846       case _OP_DELETE: _GUARD(DeleteSlot(STK(arg1), STK(arg2), TARGET)); continue;
    00847       case _OP_SET:
    00848         if (!Set(STK(arg1), STK(arg2), STK(arg3),true)) { Raise_IdxError(STK(arg2)); SQ_THROW(); }
    00849         if (arg0 != arg3) TARGET = STK(arg3);
    00850         continue;
    00851       case _OP_GET:
    00852         if (!Get(STK(arg1), STK(arg2), temp_reg, false,true)) { Raise_IdxError(STK(arg2)); SQ_THROW(); }
    00853         TARGET = temp_reg;
    00854         continue;
    00855       case _OP_EQ:{
    00856         bool res;
    00857         if(!IsEqual(STK(arg2),COND_LITERAL,res)) { SQ_THROW(); }
    00858         TARGET = res?_true_:_false_;
    00859         }continue;
    00860       case _OP_NE:{
    00861         bool res;
    00862         if(!IsEqual(STK(arg2),COND_LITERAL,res)) { SQ_THROW(); }
    00863         TARGET = (!res)?_true_:_false_;
    00864         } continue;
    00865       case _OP_ARITH: _GUARD(ARITH_OP( arg3 , temp_reg, STK(arg2), STK(arg1))); TARGET = temp_reg; continue;
    00866       case _OP_BITW:  _GUARD(BW_OP( arg3,TARGET,STK(arg2),STK(arg1))); continue;
    00867       case _OP_RETURN:
    00868         if(ci->_generator) {
    00869           ci->_generator->Kill();
    00870         }
    00871         if(Return(arg0, arg1, temp_reg)){
    00872           assert(traps==0);
    00873           outres = temp_reg;
    00874           return true;
    00875         }
    00876         continue;
    00877       case _OP_LOADNULLS:{ for(SQInt32 n=0; n < arg1; n++) STK(arg0+n) = _null_; }continue;
    00878       case _OP_LOADROOTTABLE: TARGET = _roottable; continue;
    00879       case _OP_LOADBOOL: TARGET = arg1?_true_:_false_; continue;
    00880       case _OP_DMOVE: STK(arg0) = STK(arg1); STK(arg2) = STK(arg3); continue;
    00881       case _OP_JMP: ci->_ip += (sarg1); continue;
    00882       case _OP_JNZ: if(!IsFalse(STK(arg0))) ci->_ip+=(sarg1); continue;
    00883       case _OP_JZ: if(IsFalse(STK(arg0))) ci->_ip+=(sarg1); continue;
    00884       case _OP_LOADFREEVAR: TARGET = _closure(ci->_closure)->_outervalues[arg1]; continue;
    00885       case _OP_VARGC: TARGET = SQInteger(ci->_vargs.size); continue;
    00886       case _OP_GETVARGV:
    00887         if(!GETVARGV_OP(TARGET,STK(arg1),ci)) { SQ_THROW(); }
    00888         continue;
    00889       case _OP_NEWTABLE: TARGET = SQTable::Create(_ss(this), arg1); continue;
    00890       case _OP_NEWARRAY: TARGET = SQArray::Create(_ss(this), 0); _array(TARGET)->Reserve(arg1); continue;
    00891       case _OP_APPENDARRAY: _array(STK(arg0))->Append(COND_LITERAL);  continue;
    00892       case _OP_GETPARENT: _GUARD(GETPARENT_OP(STK(arg1),TARGET)); continue;
    00893       case _OP_COMPARITH: _GUARD(DerefInc(arg3, TARGET, STK((((SQUnsignedInteger)arg1&0xFFFF0000)>>16)), STK(arg2), STK(arg1&0x0000FFFF), false)); continue;
    00894       case _OP_COMPARITHL: _GUARD(LOCAL_INC(arg3, TARGET, STK(arg1), STK(arg2))); continue;
    00895       case _OP_INC: {SQObjectPtr o(sarg3); _GUARD(DerefInc('+',TARGET, STK(arg1), STK(arg2), o, false));} continue;
    00896       case _OP_INCL: {SQObjectPtr o(sarg3); _GUARD(LOCAL_INC('+',TARGET, STK(arg1), o));} continue;
    00897       case _OP_PINC: {SQObjectPtr o(sarg3); _GUARD(DerefInc('+',TARGET, STK(arg1), STK(arg2), o, true));} continue;
    00898       case _OP_PINCL: {SQObjectPtr o(sarg3); _GUARD(PLOCAL_INC('+',TARGET, STK(arg1), o));} continue;
    00899       case _OP_CMP: _GUARD(CMP_OP((CmpOP)arg3,STK(arg2),STK(arg1),TARGET))  continue;
    00900       case _OP_EXISTS: TARGET = Get(STK(arg1), STK(arg2), temp_reg, true,false)?_true_:_false_;continue;
    00901       case _OP_INSTANCEOF:
    00902         if(type(STK(arg1)) != OT_CLASS || type(STK(arg2)) != OT_INSTANCE)
    00903         {Raise_Error(_SC("cannot apply instanceof between a %s and a %s"),GetTypeName(STK(arg1)),GetTypeName(STK(arg2))); SQ_THROW();}
    00904         TARGET = _instance(STK(arg2))->InstanceOf(_class(STK(arg1)))?_true_:_false_;
    00905         continue;
    00906       case _OP_AND:
    00907         if(IsFalse(STK(arg2))) {
    00908           TARGET = STK(arg2);
    00909           ci->_ip += (sarg1);
    00910         }
    00911         continue;
    00912       case _OP_OR:
    00913         if(!IsFalse(STK(arg2))) {
    00914           TARGET = STK(arg2);
    00915           ci->_ip += (sarg1);
    00916         }
    00917         continue;
    00918       case _OP_NEG: _GUARD(NEG_OP(TARGET,STK(arg1))); continue;
    00919       case _OP_NOT: TARGET = (IsFalse(STK(arg1))?_true_:_false_); continue;
    00920       case _OP_BWNOT:
    00921         if(type(STK(arg1)) == OT_INTEGER) {
    00922           SQInteger t = _integer(STK(arg1));
    00923           TARGET = SQInteger(~t);
    00924           continue;
    00925         }
    00926         Raise_Error(_SC("attempt to perform a bitwise op on a %s"), GetTypeName(STK(arg1)));
    00927         SQ_THROW();
    00928       case _OP_CLOSURE: {
    00929         SQClosure *c = ci->_closure._unVal.pClosure;
    00930         SQFunctionProto *fp = c->_function._unVal.pFunctionProto;
    00931         if(!CLOSURE_OP(TARGET,fp->_functions[arg1]._unVal.pFunctionProto)) { SQ_THROW(); }
    00932         continue;
    00933       }
    00934       case _OP_YIELD:{
    00935         if(ci->_generator) {
    00936           if(sarg1 != MAX_FUNC_STACKSIZE) temp_reg = STK(arg1);
    00937           _GUARD(ci->_generator->Yield(this));
    00938           traps -= ci->_etraps;
    00939           if(sarg1 != MAX_FUNC_STACKSIZE) STK(arg1) = temp_reg;
    00940         }
    00941         else { Raise_Error(_SC("trying to yield a '%s',only genenerator can be yielded"), GetTypeName(ci->_closure)); SQ_THROW();}
    00942         if(Return(arg0, arg1, temp_reg)){
    00943           assert(traps == 0);
    00944           outres = temp_reg;
    00945           return true;
    00946         }
    00947
    00948         }
    00949         continue;
    00950       case _OP_RESUME:
    00951         if(type(STK(arg1)) != OT_GENERATOR){ Raise_Error(_SC("trying to resume a '%s',only genenerator can be resumed"), GetTypeName(STK(arg1))); SQ_THROW();}
    00952         _GUARD(_generator(STK(arg1))->Resume(this, arg0));
    00953         traps += ci->_etraps;
    00954                 continue;
    00955       case _OP_FOREACH:{ int tojump;
    00956         _GUARD(FOREACH_OP(STK(arg0),STK(arg2),STK(arg2+1),STK(arg2+2),arg2,sarg1,tojump));
    00957         ci->_ip += tojump; }
    00958         continue;
    00959       case _OP_POSTFOREACH:
    00960         assert(type(STK(arg0)) == OT_GENERATOR);
    00961         if(_generator(STK(arg0))->_state == SQGenerator::eDead)
    00962           ci->_ip += (sarg1 - 1);
    00963         continue;
    00964       case _OP_DELEGATE: _GUARD(DELEGATE_OP(TARGET,STK(arg1),STK(arg2))); continue;
    00965       case _OP_CLONE:
    00966         if(!Clone(STK(arg1), TARGET))
    00967         { Raise_Error(_SC("cloning a %s"), GetTypeName(STK(arg1))); SQ_THROW();}
    00968         continue;
    00969       case _OP_TYPEOF: TypeOf(STK(arg1), TARGET); continue;
    00970       case _OP_PUSHTRAP:{
    00971         SQInstruction *_iv = _funcproto(_closure(ci->_closure)->_function)->_instructions;
    00972         _etraps.push_back(SQExceptionTrap(_top,_stackbase, &_iv[(ci->_ip-_iv)+arg1], arg0)); traps++;
    00973         ci->_etraps++;
    00974                 }
    00975         continue;
    00976       case _OP_POPTRAP: {
    00977         for(SQInteger i = 0; i < arg0; i++) {
    00978           _etraps.pop_back(); traps--;
    00979           ci->_etraps--;
    00980         }
    00981                 }
    00982         continue;
    00983       case _OP_THROW: Raise_Error(TARGET); SQ_THROW(); continue;
    00984       case _OP_CLASS: _GUARD(CLASS_OP(TARGET,arg1,arg2)); continue;
    00985       case _OP_NEWSLOTA:
    00986         bool bstatic = (arg0&NEW_SLOT_STATIC_FLAG)?true:false;
    00987         if(type(STK(arg1)) == OT_CLASS) {
    00988           if(type(_class(STK(arg1))->_metamethods[MT_NEWMEMBER]) != OT_NULL ) {
    00989             Push(STK(arg1)); Push(STK(arg2)); Push(STK(arg3));
    00990             Push((arg0&NEW_SLOT_ATTRIBUTES_FLAG) ? STK(arg2-1) : _null_);
    00991             int nparams = 4;
    00992             if(Call(_class(STK(arg1))->_metamethods[MT_NEWMEMBER], nparams, _top - nparams, temp_reg,SQFalse,SQFalse)) {
    00993               Pop(nparams);
    00994               continue;
    00995             }
    00996           }
    00997         }
    00998         _GUARD(NewSlot(STK(arg1), STK(arg2), STK(arg3),bstatic));
    00999         if((arg0&NEW_SLOT_ATTRIBUTES_FLAG)) {
    01000           _class(STK(arg1))->SetAttributes(STK(arg2),STK(arg2-1));
    01001         }
    01002         continue;
    01003       }
    01004
    01005     }
    01006   }
    01007 exception_trap:
    01008   {
    01009     SQObjectPtr currerror = _lasterror;
    01010 //    dumpstack(_stackbase);
    01011     SQInteger n = 0;
    01012     SQInteger last_top = _top;
    01013     if(ci) {
    01014       if(_ss(this)->_notifyallexceptions) CallErrorHandler(currerror);
    01015
    01016       if(traps) {
    01017         do {
    01018           if(ci->_etraps > 0) {
    01019             SQExceptionTrap &et = _etraps.top();
    01020             ci->_ip = et._ip;
    01021             _top = et._stacksize;
    01022             _stackbase = et._stackbase;
    01023             _stack._vals[_stackbase+et._extarget] = currerror;
    01024             _etraps.pop_back(); traps--; ci->_etraps--;
    01025             while(last_top >= _top) _stack._vals[last_top--].Null();
    01026             goto exception_restore;
    01027           }
    01028           //if is a native closure
    01029           if(type(ci->_closure) != OT_CLOSURE && n)
    01030             break;
    01031           if(ci->_generator) ci->_generator->Kill();
    01032           PopVarArgs(ci->_vargs);
    01033           POP_CALLINFO(this);
    01034           n++;
    01035         } while(_callsstacksize);
    01036       }
    01037       else {
    01038         //call the hook
    01039         if(raiseerror && !_ss(this)->_notifyallexceptions)
    01040           CallErrorHandler(currerror);
    01041       }
    01042       //remove call stack until a C function is found or the cstack is empty
    01043       if(ci) do {
    01044         SQBool exitafterthisone = ci->_root;
    01045         if(ci->_generator) ci->_generator->Kill();
    01046         _stackbase -= ci->_prevstkbase;
    01047         _top = _stackbase + ci->_prevtop;
    01048         PopVarArgs(ci->_vargs);
    01049         POP_CALLINFO(this);
    01050         if( (ci && type(ci->_closure) != OT_CLOSURE) || exitafterthisone) break;
    01051       } while(_callsstacksize);
    01052
    01053       while(last_top >= _top) _stack._vals[last_top--].Null();
    01054     }
    01055     _lasterror = currerror;
    01056     return false;
    01057   }
    01058   assert(0);
    01059 }
    01060
    01061 bool SQVM::CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor)
    01062 {
    01063   inst = theclass->CreateInstance();
    01064   if(!theclass->Get(_ss(this)->_constructoridx,constructor)) {
    01065     //if(!Call(constr,nargs,stackbase,constr,false))
    01066     //  return false;
    01067     constructor = _null_;
    01068   }
    01069   return true;
    01070 }
    01071
    01072 void SQVM::CallErrorHandler(SQObjectPtr &error)
    01073 {
    01074   if(type(_errorhandler) != OT_NULL) {
    01075     SQObjectPtr out;
    01076     Push(_roottable); Push(error);
    01077     Call(_errorhandler, 2, _top-2, out,SQFalse,SQFalse);
    01078     Pop(2);
    01079   }
    01080 }
    01081
    01082 void SQVM::CallDebugHook(SQInteger type,SQInteger forcedline)
    01083 {
    01084   SQObjectPtr temp_reg;
    01085   SQInteger nparams=5;
    01086   SQFunctionProto *func=_funcproto(_closure(ci->_closure)->_function);
    01087   Push(_roottable); Push(type); Push(func->_sourcename); Push(forcedline?forcedline:func->GetLine(ci->_ip)); Push(func->_name);
    01088   Call(_debughook,nparams,_top-nparams,temp_reg,SQFalse,SQFalse);
    01089   Pop(nparams);
    01090 }
    01091
    01092 bool SQVM::CallNative(SQNativeClosure *nclosure,SQInteger nargs,SQInteger stackbase,SQObjectPtr &retval,bool &suspend)
    01093 {
    01094   if (_nnativecalls + 1 > MAX_NATIVE_CALLS) { Raise_Error(_SC("Native stack overflow")); return false; }
    01095   SQInteger nparamscheck = nclosure->_nparamscheck;
    01096   if(((nparamscheck > 0) && (nparamscheck != nargs))
    01097     || ((nparamscheck < 0) && (nargs < (-nparamscheck)))) {
    01098     Raise_Error(_SC("wrong number of parameters"));
    01099     return false;
    01100     }
    01101
    01102   SQInteger tcs;
    01103   if((tcs = nclosure->_typecheck.size())) {
    01104     for(SQInteger i = 0; i < nargs && i < tcs; i++)
    01105       if((nclosure->_typecheck._vals[i] != -1) && !(type(_stack._vals[stackbase+i]) & nclosure->_typecheck[i])) {
    01106                 Raise_ParamTypeError(i,nclosure->_typecheck._vals[i],type(_stack._vals[stackbase+i]));
    01107         return false;
    01108       }
    01109   }
    01110   _nnativecalls++;
    01111   if ((_top + MIN_STACK_OVERHEAD) > (SQInteger)_stack.size()) {
    01112     _stack.resize(_stack.size() + (MIN_STACK_OVERHEAD<<1));
    01113   }
    01114   SQInteger oldtop = _top;
    01115   SQInteger oldstackbase = _stackbase;
    01116   _top = stackbase + nargs;
    01117   CallInfo lci;
    01118   memset(&lci, 0, sizeof(lci));
    01119   lci._closure = nclosure;
    01120   lci._generator = NULL;
    01121   lci._etraps = 0;
    01122   lci._prevstkbase = (SQInt32) (stackbase - _stackbase);
    01123   lci._ncalls = 1;
    01124   lci._prevtop = (SQInt32) (oldtop - oldstackbase);
    01125   PUSH_CALLINFO(this, lci);
    01126   _stackbase = stackbase;
    01127   //push free variables
    01128   SQInteger outers = nclosure->_outervalues.size();
    01129   for (SQInteger i = 0; i < outers; i++) {
    01130     Push(nclosure->_outervalues[i]);
    01131   }
    01132
    01133   if(type(nclosure->_env) == OT_WEAKREF) {
    01134     _stack[stackbase] = _weakref(nclosure->_env)->_obj;
    01135   }
    01136
    01137
    01138   SQInteger ret;
    01139   try {
    01140     SQBool can_suspend = this->_can_suspend;
    01141     this->_can_suspend = false;
    01142     ret = (nclosure->_function)(this);
    01143     this->_can_suspend = can_suspend;
    01144   } catch (...) {
    01145     _nnativecalls--;
    01146     suspend = false;
    01147
    01148     _stackbase = oldstackbase;
    01149     _top = oldtop;
    01150
    01151     POP_CALLINFO(this);
    01152
    01153     while(oldtop > _stackbase + stackbase) _stack._vals[oldtop--].Null();
    01154     throw;
    01155   }
    01156
    01157   _nnativecalls--;
    01158   suspend = false;
    01159   if( ret == SQ_SUSPEND_FLAG) suspend = true;
    01160   else if (ret < 0) {
    01161     _stackbase = oldstackbase;
    01162     _top = oldtop;
    01163     POP_CALLINFO(this);
    01164     while(oldtop > _stackbase + stackbase) _stack._vals[oldtop--].Null();
    01165     Raise_Error(_lasterror);
    01166     return false;
    01167   }
    01168
    01169   if (ret != 0){ retval = TOP(); TOP().Null(); }
    01170   else { retval = _null_; }
    01171   _stackbase = oldstackbase;
    01172   _top = oldtop;
    01173   POP_CALLINFO(this);
    01174   while(oldtop > _stackbase + stackbase) _stack._vals[oldtop--].Null();
    01175   return true;
    01176 }
    01177
    01178 bool SQVM::Get(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest,bool raw, bool fetchroot)
    01179 {
    01180   switch(type(self)){
    01181   case OT_TABLE:
    01182     if(_table(self)->Get(key,dest))return true;
    01183     break;
    01184   case OT_ARRAY:
    01185     if(sq_isnumeric(key)){
    01186       return _array(self)->Get(tointeger(key),dest);
    01187     }
    01188     break;
    01189   case OT_INSTANCE:
    01190     if(_instance(self)->Get(key,dest)) return true;
    01191     break;
    01192   default:break; //shut up compiler
    01193   }
    01194   if(FallBackGet(self,key,dest,raw)) return true;
    01195
    01196   if(fetchroot) {
    01197     if(_rawval(STK(0)) == _rawval(self) &&
    01198       type(STK(0)) == type(self)) {
    01199         return _table(_roottable)->Get(key,dest);
    01200     }
    01201   }
    01202   return false;
    01203 }
    01204
    01205 bool SQVM::FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest,bool raw)
    01206 {
    01207   switch(type(self)){
    01208   case OT_CLASS:
    01209     return _class(self)->Get(key,dest);
    01210     break;
    01211   case OT_TABLE:
    01212   case OT_USERDATA:
    01213         //delegation
    01214     if(_delegable(self)->_delegate) {
    01215       if(Get(SQObjectPtr(_delegable(self)->_delegate),key,dest,raw,false))
    01216         return true;
    01217       if(raw)return false;
    01218       Push(self);Push(key);
    01219       if(CallMetaMethod(_delegable(self),MT_GET,2,dest))
    01220         return true;
    01221     }
    01222     if(type(self) == OT_TABLE) {
    01223       if(raw) return false;
    01224       return _table_ddel->Get(key,dest);
    01225     }
    01226     return false;
    01227     break;
    01228   case OT_ARRAY:
    01229     if(raw)return false;
    01230     return _array_ddel->Get(key,dest);
    01231   case OT_STRING:
    01232     if(sq_isnumeric(key)){
    01233       SQInteger n=tointeger(key);
    01234       if(abs((int)n)<_string(self)->_len){
    01235         if(n<0)n=_string(self)->_len-n;
    01236         dest=SQInteger(_stringval(self)[n]);
    01237         return true;
    01238       }
    01239       return false;
    01240     }
    01241     else {
    01242       if(raw)return false;
    01243       return _string_ddel->Get(key,dest);
    01244     }
    01245     break;
    01246   case OT_INSTANCE:
    01247     if(raw)return false;
    01248     Push(self);Push(key);
    01249     if(!CallMetaMethod(_delegable(self),MT_GET,2,dest)) {
    01250       return _instance_ddel->Get(key,dest);
    01251     }
    01252     return true;
    01253   case OT_INTEGER:case OT_FLOAT:case OT_BOOL:
    01254     if(raw)return false;
    01255     return _number_ddel->Get(key,dest);
    01256   case OT_GENERATOR:
    01257     if(raw)return false;
    01258     return _generator_ddel->Get(key,dest);
    01259   case OT_CLOSURE: case OT_NATIVECLOSURE:
    01260     if(raw)return false;
    01261     return _closure_ddel->Get(key,dest);
    01262   case OT_THREAD:
    01263     if(raw)return false;
    01264     return  _thread_ddel->Get(key,dest);
    01265   case OT_WEAKREF:
    01266     if(raw)return false;
    01267     return  _weakref_ddel->Get(key,dest);
    01268   default:return false;
    01269   }
    01270   return false;
    01271 }
    01272
    01273 bool SQVM::Set(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,bool fetchroot)
    01274 {
    01275   switch(type(self)){
    01276   case OT_TABLE:
    01277     if(_table(self)->Set(key,val))
    01278       return true;
    01279     if(_table(self)->_delegate) {
    01280       if(Set(_table(self)->_delegate,key,val,false)) {
    01281         return true;
    01282       }
    01283     }
    01284     //keeps going
    01285   case OT_USERDATA:
    01286     if(_delegable(self)->_delegate) {
    01287       SQObjectPtr t;
    01288       Push(self);Push(key);Push(val);
    01289       if(CallMetaMethod(_delegable(self),MT_SET,3,t)) return true;
    01290     }
    01291     break;
    01292   case OT_INSTANCE:{
    01293     if(_instance(self)->Set(key,val))
    01294       return true;
    01295     SQObjectPtr t;
    01296     Push(self);Push(key);Push(val);
    01297     if(CallMetaMethod(_delegable(self),MT_SET,3,t)) return true;
    01298     }
    01299     break;
    01300   case OT_ARRAY:
    01301     if(!sq_isnumeric(key)) {Raise_Error(_SC("indexing %s with %s"),GetTypeName(self),GetTypeName(key)); return false; }
    01302     return _array(self)->Set(tointeger(key),val);
    01303   default:
    01304     Raise_Error(_SC("trying to set '%s'"),GetTypeName(self));
    01305     return false;
    01306   }
    01307   if(fetchroot) {
    01308     if(_rawval(STK(0)) == _rawval(self) &&
    01309       type(STK(0)) == type(self)) {
    01310         return _table(_roottable)->Set(key,val);
    01311       }
    01312   }
    01313   return false;
    01314 }
    01315
    01316 bool SQVM::Clone(const SQObjectPtr &self,SQObjectPtr &target)
    01317 {
    01318   SQObjectPtr temp_reg;
    01319   SQObjectPtr newobj;
    01320   switch(type(self)){
    01321   case OT_TABLE:
    01322     newobj = _table(self)->Clone();
    01323     goto cloned_mt;
    01324   case OT_INSTANCE:
    01325     newobj = _instance(self)->Clone(_ss(this));
    01326 cloned_mt:
    01327     if(_delegable(newobj)->_delegate){
    01328       Push(newobj);
    01329       Push(self);
    01330       CallMetaMethod(_delegable(newobj),MT_CLONED,2,temp_reg);
    01331     }
    01332     target = newobj;
    01333     return true;
    01334   case OT_ARRAY:
    01335     target = _array(self)->Clone();
    01336     return true;
    01337   default: return false;
    01338   }
    01339 }
    01340
    01341 bool SQVM::NewSlot(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic)
    01342 {
    01343   if(type(key) == OT_NULL) { Raise_Error(_SC("null cannot be used as index")); return false; }
    01344   switch(type(self)) {
    01345   case OT_TABLE: {
    01346     bool rawcall = true;
    01347     if(_table(self)->_delegate) {
    01348       SQObjectPtr res;
    01349       if(!_table(self)->Get(key,res)) {
    01350         Push(self);Push(key);Push(val);
    01351         rawcall = !CallMetaMethod(_table(self),MT_NEWSLOT,3,res);
    01352       }
    01353     }
    01354     if(rawcall) _table(self)->NewSlot(key,val); //cannot fail
    01355
    01356     break;}
    01357   case OT_INSTANCE: {
    01358     SQObjectPtr res;
    01359     Push(self);Push(key);Push(val);
    01360     if(!CallMetaMethod(_instance(self),MT_NEWSLOT,3,res)) {
    01361       Raise_Error(_SC("class instances do not support the new slot operator"));
    01362       return false;
    01363     }
    01364     break;}
    01365   case OT_CLASS:
    01366     if(!_class(self)->NewSlot(_ss(this),key,val,bstatic)) {
    01367       if(_class(self)->_locked) {
    01368         Raise_Error(_SC("trying to modify a class that has already been instantiated"));
    01369         return false;
    01370       }
    01371       else {
    01372         SQObjectPtr oval = PrintObjVal(key);
    01373         Raise_Error(_SC("the property '%s' already exists"),_stringval(oval));
    01374         return false;
    01375       }
    01376     }
    01377     break;
    01378   default:
    01379     Raise_Error(_SC("indexing %s with %s"),GetTypeName(self),GetTypeName(key));
    01380     return false;
    01381     break;
    01382   }
    01383   return true;
    01384 }
    01385
    01386 bool SQVM::DeleteSlot(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &res)
    01387 {
    01388   switch(type(self)) {
    01389   case OT_TABLE:
    01390   case OT_INSTANCE:
    01391   case OT_USERDATA: {
    01392     SQObjectPtr t;
    01393     bool handled = false;
    01394     if(_delegable(self)->_delegate) {
    01395       Push(self);Push(key);
    01396       handled = CallMetaMethod(_delegable(self),MT_DELSLOT,2,t);
    01397     }
    01398
    01399     if(!handled) {
    01400       if(type(self) == OT_TABLE) {
    01401         if(_table(self)->Get(key,t)) {
    01402           _table(self)->Remove(key);
    01403         }
    01404         else {
    01405           Raise_IdxError((SQObject &)key);
    01406           return false;
    01407         }
    01408       }
    01409       else {
    01410         Raise_Error(_SC("cannot delete a slot from %s"),GetTypeName(self));
    01411         return false;
    01412       }
    01413     }
    01414     res = t;
    01415         }
    01416     break;
    01417   default:
    01418     Raise_Error(_SC("attempt to delete a slot from a %s"),GetTypeName(self));
    01419     return false;
    01420   }
    01421   return true;
    01422 }
    01423
    01424 bool SQVM::Call(SQObjectPtr &closure,SQInteger nparams,SQInteger stackbase,SQObjectPtr &outres,SQBool raiseerror,SQBool can_suspend)
    01425 {
    01426 #ifdef _DEBUG
    01427 SQInteger prevstackbase = _stackbase;
    01428 #endif
    01429   switch(type(closure)) {
    01430   case OT_CLOSURE: {
    01431     assert(!can_suspend || this->_can_suspend);
    01432     SQBool backup_suspend = this->_can_suspend;
    01433     this->_can_suspend = can_suspend;
    01434     bool ret = Execute(closure, _top - nparams, nparams, stackbase,outres,raiseerror);
    01435     this->_can_suspend = backup_suspend;
    01436     return ret;
    01437            }
    01438     break;
    01439   case OT_NATIVECLOSURE:{
    01440     bool suspend;
    01441     return CallNative(_nativeclosure(closure), nparams, stackbase, outres,suspend);
    01442
    01443               }
    01444     break;
    01445   case OT_CLASS: {
    01446     SQObjectPtr constr;
    01447     SQObjectPtr temp;
    01448     CreateClassInstance(_class(closure),outres,constr);
    01449     if(type(constr) != OT_NULL) {
    01450       _stack[stackbase] = outres;
    01451       return Call(constr,nparams,stackbase,temp,raiseerror,false);
    01452     }
    01453     return true;
    01454            }
    01455     break;
    01456   default:
    01457     return false;
    01458   }
    01459 #ifdef _DEBUG
    01460   if(!_suspended) {
    01461     assert(_stackbase == prevstackbase);
    01462   }
    01463 #endif
    01464   return true;
    01465 }
    01466
    01467 bool SQVM::CallMetaMethod(SQDelegable *del,SQMetaMethod mm,SQInteger nparams,SQObjectPtr &outres)
    01468 {
    01469   SQObjectPtr closure;
    01470   if(del->GetMetaMethod(this, mm, closure)) {
    01471     if(Call(closure, nparams, _top - nparams, outres, SQFalse, SQFalse)) {
    01472       Pop(nparams);
    01473       return true;
    01474     }
    01475   }
    01476   Pop(nparams);
    01477   return false;
    01478 }
    01479
    01480 void SQVM::Remove(SQInteger n) {
    01481   n = (n >= 0)?n + _stackbase - 1:_top + n;
    01482   for(SQInteger i = n; i < _top; i++){
    01483     _stack[i] = _stack[i+1];
    01484   }
    01485   _stack[_top] = _null_;
    01486   _top--;
    01487 }
    01488
    01489 void SQVM::Pop() {
    01490   _stack[--_top] = _null_;
    01491 }
    01492
    01493 void SQVM::Pop(SQInteger n) {
    01494   for(SQInteger i = 0; i < n; i++){
    01495     _stack[--_top] = _null_;
    01496   }
    01497 }
    01498
    01499 void SQVM::Push(const SQObjectPtr &o) { _stack[_top++] = o; }
    01500 SQObjectPtr &SQVM::Top() { return _stack[_top-1]; }
    01501 SQObjectPtr &SQVM::PopGet() { return _stack[--_top]; }
    01502 SQObjectPtr &SQVM::GetUp(SQInteger n) { return _stack[_top+n]; }
    01503 SQObjectPtr &SQVM::GetAt(SQInteger n) { return _stack[n]; }
    01504
    01505 #ifdef _DEBUG_DUMP
    01506 void SQVM::dumpstack(SQInteger stackbase,bool dumpall)
    01507 {
    01508   SQInteger size=dumpall?_stack.size():_top;
    01509   SQInteger n=0;
    01510   scprintf(_SC("\n>>>>stack dump<<<<\n"));
    01511   CallInfo &ci=_callsstack[_callsstacksize-1];
    01512   scprintf(_SC("IP: %p\n"),ci._ip);
    01513   scprintf(_SC("prev stack base: %d\n"),ci._prevstkbase);
    01514   scprintf(_SC("prev top: %d\n"),ci._prevtop);
    01515   for(SQInteger i=0;i<size;i++){
    01516     SQObjectPtr &obj=_stack[i];
    01517     if(stackbase==i)scprintf(_SC(">"));else scprintf(_SC(" "));
    01518     scprintf(_SC("[%d]:"),n);
    01519     switch(type(obj)){
    01520     case OT_FLOAT:      scprintf(_SC("FLOAT %.3f"),_float(obj));break;
    01521     case OT_INTEGER:    scprintf(_SC("INTEGER %d"),_integer(obj));break;
    01522     case OT_BOOL:     scprintf(_SC("BOOL %s"),_integer(obj)?"true":"false");break;
    01523     case OT_STRING:     scprintf(_SC("STRING %s"),_stringval(obj));break;
    01524     case OT_NULL:     scprintf(_SC("NULL"));  break;
    01525     case OT_TABLE:      scprintf(_SC("TABLE %p[%p]"),_table(obj),_table(obj)->_delegate);break;
    01526     case OT_ARRAY:      scprintf(_SC("ARRAY %p"),_array(obj));break;
    01527     case OT_CLOSURE:    scprintf(_SC("CLOSURE [%p]"),_closure(obj));break;
    01528     case OT_NATIVECLOSURE:  scprintf(_SC("NATIVECLOSURE"));break;
    01529     case OT_USERDATA:   scprintf(_SC("USERDATA %p[%p]"),_userdataval(obj),_userdata(obj)->_delegate);break;
    01530     case OT_GENERATOR:    scprintf(_SC("GENERATOR %p"),_generator(obj));break;
    01531     case OT_THREAD:     scprintf(_SC("THREAD [%p]"),_thread(obj));break;
    01532     case OT_USERPOINTER:  scprintf(_SC("USERPOINTER %p"),_userpointer(obj));break;
    01533     case OT_CLASS:      scprintf(_SC("CLASS %p"),_class(obj));break;
    01534     case OT_INSTANCE:   scprintf(_SC("INSTANCE %p"),_instance(obj));break;
    01535     case OT_WEAKREF:    scprintf(_SC("WEAKERF %p"),_weakref(obj));break;
    01536     default:
    01537       assert(0);
    01538       break;
    01539     };
    01540     scprintf(_SC("\n"));
    01541     ++n;
    01542   }
    01543 }
    01544
    01545
    01546
    01547 #endif