sqvm.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* see copyright notice in squirrel.h */
  2. #ifndef _SQVM_H_
  3. #define _SQVM_H_
  4. #include "sqopcodes.h"
  5. #include "sqobject.h"
  6. #define MAX_NATIVE_CALLS 100
  7. #define MIN_STACK_OVERHEAD 15
  8. #define SQ_SUSPEND_FLAG -666
  9. #define DONT_FALL_BACK 666
  10. //#define EXISTS_FALL_BACK -1
  11. #define GET_FLAG_RAW 0x00000001
  12. #define GET_FLAG_DO_NOT_RAISE_ERROR 0x00000002
  13. //base lib
  14. void sq_base_register(HSQUIRRELVM v);
  15. struct SQExceptionTrap{
  16. SQExceptionTrap() {}
  17. SQExceptionTrap(SQInteger ss, SQInteger stackbase,SQInstruction *ip, SQInteger ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
  18. SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
  19. SQInteger _stackbase;
  20. SQInteger _stacksize;
  21. SQInstruction *_ip;
  22. SQInteger _extarget;
  23. };
  24. #define _INLINE
  25. typedef sqvector<SQExceptionTrap> ExceptionsTraps;
  26. struct SQVM : public CHAINABLE_OBJ
  27. {
  28. struct CallInfo{
  29. //CallInfo() { _generator = NULL;}
  30. SQInstruction *_ip;
  31. SQObjectPtr *_literals;
  32. SQObjectPtr _closure;
  33. SQGenerator *_generator;
  34. SQInt32 _etraps;
  35. SQInt32 _prevstkbase;
  36. SQInt32 _prevtop;
  37. SQInt32 _target;
  38. SQInt32 _ncalls;
  39. SQBool _root;
  40. };
  41. typedef sqvector<CallInfo> CallInfoVec;
  42. public:
  43. void DebugHookProxy(SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
  44. static void _DebugHookProxy(HSQUIRRELVM v, SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
  45. enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM,ET_RESUME_THROW_VM };
  46. SQVM(SQSharedState *ss);
  47. ~SQVM();
  48. bool Init(SQVM *friendvm, SQInteger stacksize);
  49. bool Execute(SQObjectPtr &func, SQInteger nargs, SQInteger stackbase, SQObjectPtr &outres, SQBool raiseerror, ExecutionType et = ET_CALL);
  50. //starts a native call return when the NATIVE closure returns
  51. bool CallNative(SQNativeClosure *nclosure, SQInteger nargs, SQInteger newbase, SQObjectPtr &retval,bool &suspend);
  52. //starts a SQUIRREL call in the same "Execution loop"
  53. bool StartCall(SQClosure *closure, SQInteger target, SQInteger nargs, SQInteger stackbase, bool tailcall);
  54. bool CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor);
  55. //call a generic closure pure SQUIRREL or NATIVE
  56. bool Call(SQObjectPtr &closure, SQInteger nparams, SQInteger stackbase, SQObjectPtr &outres,SQBool raiseerror);
  57. SQRESULT Suspend();
  58. void CallDebugHook(SQInteger type,SQInteger forcedline=0);
  59. void CallErrorHandler(SQObjectPtr &e);
  60. bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, SQUnsignedInteger getflags, SQInteger selfidx);
  61. SQInteger FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  62. bool InvokeDefaultDelegate(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  63. bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, SQInteger selfidx);
  64. SQInteger FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val);
  65. bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
  66. bool NewSlotA(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,const SQObjectPtr &attrs,bool bstatic,bool raw);
  67. bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
  68. bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
  69. bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
  70. bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
  71. static bool IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res);
  72. bool ToString(const SQObjectPtr &o,SQObjectPtr &res);
  73. SQString *PrintObjVal(const SQObjectPtr &o);
  74. void Raise_Error(const SQChar *s, ...);
  75. void Raise_Error(const SQObjectPtr &desc);
  76. void Raise_IdxError(const SQObjectPtr &o);
  77. void Raise_CompareError(const SQObject &o1, const SQObject &o2);
  78. void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
  79. void FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex);
  80. void RelocateOuters();
  81. void CloseOuters(SQObjectPtr *stackindex);
  82. bool TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
  83. bool CallMetaMethod(SQObjectPtr &closure, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
  84. bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
  85. bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
  86. //new stuff
  87. _INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  88. _INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  89. _INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
  90. _INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
  91. bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
  92. bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
  93. //return true if the loop is finished
  94. bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump);
  95. //_INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  96. _INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  97. _INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix,SQInteger arg0);
  98. #ifdef _DEBUG_DUMP
  99. void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
  100. #endif
  101. #ifndef NO_GARBAGE_COLLECTOR
  102. void Mark(SQCollectable **chain);
  103. SQObjectType GetType() {return OT_THREAD;}
  104. #endif
  105. void Finalize();
  106. void GrowCallStack() {
  107. SQInteger newsize = _alloccallsstacksize*2;
  108. _callstackdata.resize(newsize);
  109. _callsstack = &_callstackdata[0];
  110. _alloccallsstacksize = newsize;
  111. }
  112. bool EnterFrame(SQInteger newbase, SQInteger newtop, bool tailcall);
  113. void LeaveFrame();
  114. void Release(){ sq_delete(this,SQVM); }
  115. ////////////////////////////////////////////////////////////////////////////
  116. //stack functions for the api
  117. void Remove(SQInteger n);
  118. static bool IsFalse(SQObjectPtr &o);
  119. void Pop();
  120. void Pop(SQInteger n);
  121. void Push(const SQObjectPtr &o);
  122. void PushNull();
  123. SQObjectPtr &Top();
  124. SQObjectPtr &PopGet();
  125. SQObjectPtr &GetUp(SQInteger n);
  126. SQObjectPtr &GetAt(SQInteger n);
  127. SQObjectPtrVec _stack;
  128. SQInteger _top;
  129. SQInteger _stackbase;
  130. SQOuter *_openouters;
  131. SQObjectPtr _roottable;
  132. SQObjectPtr _lasterror;
  133. SQObjectPtr _errorhandler;
  134. bool _debughook;
  135. SQDEBUGHOOK _debughook_native;
  136. SQObjectPtr _debughook_closure;
  137. SQObjectPtr temp_reg;
  138. CallInfo* _callsstack;
  139. SQInteger _callsstacksize;
  140. SQInteger _alloccallsstacksize;
  141. sqvector<CallInfo> _callstackdata;
  142. ExceptionsTraps _etraps;
  143. CallInfo *ci;
  144. SQUserPointer _foreignptr;
  145. //VMs sharing the same state
  146. SQSharedState *_sharedstate;
  147. SQInteger _nnativecalls;
  148. SQInteger _nmetamethodscall;
  149. SQRELEASEHOOK _releasehook;
  150. //suspend infos
  151. SQBool _suspended;
  152. SQBool _suspended_root;
  153. SQInteger _suspended_target;
  154. SQInteger _suspended_traps;
  155. };
  156. struct AutoDec{
  157. AutoDec(SQInteger *n) { _n = n; }
  158. ~AutoDec() { (*_n)--; }
  159. SQInteger *_n;
  160. };
  161. inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->GetAt(idx+v->_stackbase-1)):(v->GetUp(idx)));}
  162. #define _ss(_vm_) (_vm_)->_sharedstate
  163. #ifndef NO_GARBAGE_COLLECTOR
  164. #define _opt_ss(_vm_) (_vm_)->_sharedstate
  165. #else
  166. #define _opt_ss(_vm_) NULL
  167. #endif
  168. #define PUSH_CALLINFO(v,nci){ \
  169. SQInteger css = v->_callsstacksize; \
  170. if(css == v->_alloccallsstacksize) { \
  171. v->GrowCallStack(); \
  172. } \
  173. v->ci = &v->_callsstack[css]; \
  174. *(v->ci) = nci; \
  175. v->_callsstacksize++; \
  176. }
  177. #define POP_CALLINFO(v){ \
  178. SQInteger css = --v->_callsstacksize; \
  179. v->ci->_closure.Null(); \
  180. v->ci = css?&v->_callsstack[css-1]:NULL; \
  181. }
  182. #endif //_SQVM_H_