569bd4329595b872c38cb7c04833ffd95fde11c7
[supertux.git] / src / squirrel / squirrel / sqvm.h
1 /*      see copyright notice in squirrel.h */\r
2 #ifndef _SQVM_H_\r
3 #define _SQVM_H_\r
4 \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
9 \r
10 #define SQ_SUSPEND_FLAG -666\r
11 //base lib\r
12 void sq_base_register(HSQUIRRELVM v);\r
13 \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
20         SQInstruction *_ip;\r
21         SQInteger _extarget;\r
22 };\r
23 \r
24 #define _INLINE \r
25 \r
26 #define STK(a) _stack._vals[_stackbase+(a)]\r
27 #define TARGET _stack._vals[_stackbase+arg0]\r
28 \r
29 typedef sqvector<SQExceptionTrap> ExceptionsTraps;\r
30 \r
31 struct SQVM : public CHAINABLE_OBJ\r
32 {\r
33         struct VarArgs {\r
34                 VarArgs() { size = 0; base = 0; }\r
35                 SQInteger size;\r
36                 SQInteger base;\r
37         };\r
38 \r
39         struct CallInfo{\r
40                 CallInfo() { _generator._type = OT_NULL;}\r
41                 //CallInfo(const CallInfo& ci) {  }\r
42                 SQInstructionVec *_iv;\r
43                 SQObjectPtrVec *_literals;\r
44                 SQObject _closure;\r
45                 SQObject _generator;\r
46                 SQInteger _etraps;\r
47                 SQInteger _prevstkbase;\r
48                 SQInteger _prevtop;\r
49                 SQInteger _target;\r
50                 SQInstruction *_ip;\r
51                 SQInteger _ncalls;\r
52                 SQBool _root;\r
53                 VarArgs _vargs;\r
54         };\r
55         \r
56 typedef sqvector<CallInfo> CallInfoVec;\r
57 public:\r
58         enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM };\r
59         SQVM(SQSharedState *ss);\r
60         ~SQVM();\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
70         SQRESULT Suspend();\r
71 \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
85 \r
86  \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
92 \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
97         //new stuff\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
113 #ifdef _DEBUG_DUMP\r
114         void dumpstack(SQInteger stackbase=-1, bool dumpall = false);\r
115 #endif\r
116 \r
117 #ifndef NO_GARBAGE_COLLECTOR\r
118         void Mark(SQCollectable **chain);\r
119 #endif\r
120         void Finalize();\r
121 \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
126 \r
127         bool IsFalse(SQObjectPtr &o);\r
128         \r
129         void Pop();\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
136 \r
137         SQObjectPtrVec _stack;\r
138         SQObjectPtrVec _vargsstack;\r
139         SQInteger _top;\r
140         SQInteger _stackbase;\r
141         SQObjectPtr _roottable;\r
142         SQObjectPtr _lasterror;\r
143         SQObjectPtr _errorhandler;\r
144         SQObjectPtr _debughook;\r
145 \r
146         SQObjectPtr temp_reg;\r
147         CallInfoVec _callsstack;\r
148         ExceptionsTraps _etraps;\r
149         CallInfo *ci;\r
150         void *_foreignptr;\r
151         //VMs sharing the same state\r
152         SQSharedState *_sharedstate;\r
153         SQInteger _nnativecalls;\r
154         //suspend infos\r
155         SQBool _suspended;\r
156         SQBool _suspended_root;\r
157         SQInteger _suspended_target;\r
158         SQInteger _suspended_traps;\r
159 };\r
160 \r
161 struct AutoDec{\r
162         AutoDec(SQInteger *n) { _n = n; }\r
163         ~AutoDec() { (*_n)--; }\r
164         SQInteger *_n;\r
165 };\r
166 \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
170 \r
171 #define _ss(_vm_) (_vm_)->_sharedstate\r
172 \r
173 #ifndef NO_GARBAGE_COLLECTOR\r
174 #define _opt_ss(_vm_) (_vm_)->_sharedstate\r
175 #else\r
176 #define _opt_ss(_vm_) NULL\r
177 #endif\r
178 \r
179 #define PUSH_CALLINFO(v,nci){ \\r
180         v->ci = &v->_callsstack.push_back(nci); \\r
181 }\r
182 \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
187         else    \\r
188                 v->ci = NULL; \\r
189 }\r
190 #endif //_SQVM_H_\r