1 /* see copyright notice in squirrel.h */
7 #define MAX_NATIVE_CALLS 100
8 #define MIN_STACK_OVERHEAD 10
10 #define SQ_SUSPEND_FLAG -666
12 void sq_base_register(HSQUIRRELVM v);
14 struct SQExceptionTrap{
16 SQExceptionTrap(int ss, int stackbase,SQInstruction *ip, int ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
17 SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
25 #define STK(a) _stack._vals[_stackbase+(a)]
26 #define TARGET _stack._vals[_stackbase+arg0]
28 typedef sqvector<SQExceptionTrap> ExceptionsTraps;
30 struct SQVM : public CHAINABLE_OBJ
33 VarArgs() { size = 0; base = 0; }
40 //CallInfo(const CallInfo& ci) { }
41 SQInstructionVec *_iv;
42 SQObjectPtrVec *_literals;
55 typedef sqvector<CallInfo> CallInfoVec;
57 enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM };
58 SQVM(SQSharedState *ss);
60 bool Init(SQVM *friendvm, int stacksize);
61 bool Execute(SQObjectPtr &func, int target, int nargs, int stackbase, SQObjectPtr &outres, ExecutionType et = ET_CALL);
62 //start a native call return when the NATIVE closure returns(returns true if the vm has been suspended)
63 bool CallNative(SQNativeClosure *nclosure, int nargs, int stackbase, bool tailcall, SQObjectPtr &retval,bool &suspend);
64 //start a SQUIRREL call in the same "Execution loop"
65 bool StartCall(SQClosure *closure, int target, int nargs, int stackbase, bool tailcall);
66 bool CreateClassInstance(SQClass *theclass, int nargs, int stackbase, SQObjectPtr &retval);
67 //call a generic closure pure SQUIRREL or NATIVE
68 bool Call(SQObjectPtr &closure, int nparams, int stackbase, SQObjectPtr &outres);
71 void CallDebugHook(int type,int forcedline=0);
72 void CallErrorHandler(SQObjectPtr &e);
73 bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, bool raw, bool fetchroot);
74 bool FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest,bool raw);
75 bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, bool fetchroot);
76 bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val);
77 bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
78 bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
79 bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,int &res);
80 bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
81 bool IsEqual(SQObjectPtr &o1,SQObjectPtr &o2,bool &res);
82 bool IsFalse(SQObjectPtr &o);
83 SQString *PrintObjVal(const SQObject &o);
86 void Raise_Error(const SQChar *s, ...);
87 void Raise_Error(SQObjectPtr &desc);
88 void Raise_IdxError(SQObject &o);
89 void Raise_CompareError(const SQObject &o1, const SQObject &o2);
90 void Raise_ParamTypeError(int nparam,int typemask,int type);
92 void TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
93 bool CallMetaMethod(SQDelegable *del, SQMetaMethod mm, int nparams, SQObjectPtr &outres);
94 bool ArithMetaMethod(int op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
95 //void Modulo(const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
96 bool Return(int _arg0, int _arg1, SQObjectPtr &retval);
98 bool ARITH_OP(unsigned int op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
99 bool BW_OP(unsigned int op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
100 bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
101 bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
102 bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
103 bool GETVARGV_OP(SQObjectPtr &target,SQObjectPtr &idx,CallInfo *ci);
104 bool CLASS_OP(SQObjectPtr &target,int base,int attrs);
105 //return true if the loop is finished
106 bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,int arg_2,bool &finished);
107 bool DELEGATE_OP(SQObjectPtr &trg,SQObjectPtr &o1,SQObjectPtr &o2);
108 bool LOCAL_INC(int op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
109 bool PLOCAL_INC(int op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
110 bool DerefInc(int op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix);
111 void PopVarArgs(VarArgs &vargs);
113 void dumpstack(int stackbase=-1, bool dumpall = false);
116 #ifndef NO_GARBAGE_COLLECTOR
117 void Mark(SQCollectable **chain);
121 void Release(){ sq_delete(this,SQVM); } //does nothing
122 ////////////////////////////////////////////////////////////////////////////
123 //stack functions for the api
128 void Push(const SQObjectPtr &o);
130 SQObjectPtr &PopGet();
131 SQObjectPtr &GetUp(int n);
132 SQObjectPtr &GetAt(int n);
134 SQObjectPtrVec _stack;
135 SQObjectPtrVec _vargsstack;
138 SQObjectPtr _roottable;
139 //SQObjectPtr _thrownerror;
140 SQObjectPtr _lasterror;
141 SQObjectPtr _errorhandler;
142 SQObjectPtr _debughook;
144 SQObjectPtr temp_reg;
145 CallInfoVec _callsstack;
146 ExceptionsTraps _etraps;
149 //VMs sharing the same state
150 SQSharedState *_sharedstate;
154 bool _suspended_root;
155 int _suspended_target;
156 int _suspended_traps;
160 AutoDec(int *n) { _n = n; }
161 ~AutoDec() { (*_n)--; }
165 SQObjectPtr &stack_get(HSQUIRRELVM v, int idx);
166 const SQChar *GetTypeName(const SQObjectPtr &obj1);
167 const SQChar *IdType2Name(SQObjectType type);
169 #define _ss(_vm_) (_vm_)->_sharedstate
171 #ifndef NO_GARBAGE_COLLECTOR
172 #define _opt_ss(_vm_) (_vm_)->_sharedstate
174 #define _opt_ss(_vm_) NULL
177 #define PUSH_CALLINFO(v,nci){ \
178 v->_callsstack.push_back(nci); \
179 v->ci = &v->_callsstack.back(); \
182 #define POP_CALLINFO(v){ \
183 v->_callsstack.pop_back(); \
184 if(v->_callsstack.size()) \
185 v->ci = &v->_callsstack.back() ; \