1 /* see copyright notice in squirrel.h */
\r
5 #include "sqopcodes.h"
\r
6 #include "sqobject.h"
\r
7 #define MAX_NATIVE_CALLS 100
\r
8 #define MIN_STACK_OVERHEAD 10
\r
10 #define SQ_SUSPEND_FLAG -666
\r
12 void sq_base_register(HSQUIRRELVM v);
\r
14 struct SQExceptionTrap{
\r
15 SQExceptionTrap() {}
\r
16 SQExceptionTrap(SQInteger ss, SQInteger stackbase,SQInstruction *ip, SQInteger ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
\r
17 SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
\r
18 SQInteger _stackbase;
\r
19 SQInteger _stacksize;
\r
21 SQInteger _extarget;
\r
26 #define STK(a) _stack._vals[_stackbase+(a)]
\r
27 #define TARGET _stack._vals[_stackbase+arg0]
\r
29 typedef sqvector<SQExceptionTrap> ExceptionsTraps;
\r
31 struct SQVM : public CHAINABLE_OBJ
\r
34 VarArgs() { size = 0; base = 0; }
\r
40 CallInfo() { _generator._type = OT_NULL;}
\r
41 //CallInfo(const CallInfo& ci) { }
\r
42 SQInstructionVec *_iv;
\r
43 SQObjectPtrVec *_literals;
\r
45 SQObject _generator;
\r
47 SQInteger _prevstkbase;
\r
56 typedef sqvector<CallInfo> CallInfoVec;
\r
58 enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM };
\r
59 SQVM(SQSharedState *ss);
\r
61 bool Init(SQVM *friendvm, SQInteger stacksize);
\r
62 bool Execute(SQObjectPtr &func, SQInteger target, SQInteger nargs, SQInteger stackbase, SQObjectPtr &outres, SQBool raiseerror, ExecutionType et = ET_CALL);
\r
63 //starts a native call return when the NATIVE closure returns
\r
64 bool CallNative(SQNativeClosure *nclosure, SQInteger nargs, SQInteger stackbase, bool tailcall, SQObjectPtr &retval,bool &suspend);
\r
65 //starts a SQUIRREL call in the same "Execution loop"
\r
66 bool StartCall(SQClosure *closure, SQInteger target, SQInteger nargs, SQInteger stackbase, bool tailcall);
\r
67 bool CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor);
\r
68 //call a generic closure pure SQUIRREL or NATIVE
\r
69 bool Call(SQObjectPtr &closure, SQInteger nparams, SQInteger stackbase, SQObjectPtr &outres,SQBool raiseerror);
\r
72 void CallDebugHook(SQInteger type,SQInteger forcedline=0);
\r
73 void CallErrorHandler(SQObjectPtr &e);
\r
74 bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, bool raw, bool fetchroot);
\r
75 bool FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest,bool raw);
\r
76 bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, bool fetchroot);
\r
77 bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
\r
78 bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
\r
79 bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
\r
80 bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
\r
81 bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
\r
82 bool IsEqual(SQObjectPtr &o1,SQObjectPtr &o2,bool &res);
\r
83 void ToString(const SQObjectPtr &o,SQObjectPtr &res);
\r
84 SQString *PrintObjVal(const SQObject &o);
\r
87 void Raise_Error(const SQChar *s, ...);
\r
88 void Raise_Error(SQObjectPtr &desc);
\r
89 void Raise_IdxError(SQObject &o);
\r
90 void Raise_CompareError(const SQObject &o1, const SQObject &o2);
\r
91 void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
\r
93 void TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
\r
94 bool CallMetaMethod(SQDelegable *del, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
\r
95 bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
\r
96 bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
\r
98 _INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
\r
99 _INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
\r
100 _INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
\r
101 _INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
\r
102 bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
\r
103 bool GETVARGV_OP(SQObjectPtr &target,SQObjectPtr &idx,CallInfo *ci);
\r
104 bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
\r
105 bool GETPARENT_OP(SQObjectPtr &o,SQObjectPtr &target);
\r
106 //return true if the loop is finished
\r
107 bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,bool &finished);
\r
108 bool DELEGATE_OP(SQObjectPtr &trg,SQObjectPtr &o1,SQObjectPtr &o2);
\r
109 _INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
\r
110 _INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
\r
111 _INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix);
\r
112 void PopVarArgs(VarArgs &vargs);
\r
114 void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
\r
117 #ifndef NO_GARBAGE_COLLECTOR
\r
118 void Mark(SQCollectable **chain);
\r
122 void Release(){ sq_delete(this,SQVM); } //does nothing
\r
123 ////////////////////////////////////////////////////////////////////////////
\r
124 //stack functions for the api
\r
125 void Remove(SQInteger n);
\r
127 bool IsFalse(SQObjectPtr &o);
\r
130 void Pop(SQInteger n);
\r
131 void Push(const SQObjectPtr &o);
\r
132 SQObjectPtr &Top();
\r
133 SQObjectPtr &PopGet();
\r
134 SQObjectPtr &GetUp(SQInteger n);
\r
135 SQObjectPtr &GetAt(SQInteger n);
\r
137 SQObjectPtrVec _stack;
\r
138 SQObjectPtrVec _vargsstack;
\r
140 SQInteger _stackbase;
\r
141 SQObjectPtr _roottable;
\r
142 SQObjectPtr _lasterror;
\r
143 SQObjectPtr _errorhandler;
\r
144 SQObjectPtr _debughook;
\r
146 SQObjectPtr temp_reg;
\r
147 CallInfoVec _callsstack;
\r
148 ExceptionsTraps _etraps;
\r
151 //VMs sharing the same state
\r
152 SQSharedState *_sharedstate;
\r
153 SQInteger _nnativecalls;
\r
156 SQBool _suspended_root;
\r
157 SQInteger _suspended_target;
\r
158 SQInteger _suspended_traps;
\r
162 AutoDec(SQInteger *n) { _n = n; }
\r
163 ~AutoDec() { (*_n)--; }
\r
167 inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->GetAt(idx+v->_stackbase-1)):(v->GetUp(idx)));}
\r
168 const SQChar *GetTypeName(const SQObjectPtr &obj1);
\r
169 const SQChar *IdType2Name(SQObjectType type);
\r
171 #define _ss(_vm_) (_vm_)->_sharedstate
\r
173 #ifndef NO_GARBAGE_COLLECTOR
\r
174 #define _opt_ss(_vm_) (_vm_)->_sharedstate
\r
176 #define _opt_ss(_vm_) NULL
\r
179 #define PUSH_CALLINFO(v,nci){ \
\r
180 v->ci = &v->_callsstack.push_back(nci); \
\r
183 #define POP_CALLINFO(v){ \
\r
184 v->_callsstack.pop_back(); \
\r
185 if(v->_callsstack.size()) \
\r
186 v->ci = &v->_callsstack.back() ; \
\r