ScummVM API documentation
as_context.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2018 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_context.h
34 //
35 // This class handles the execution of the byte code
36 //
37 
38 
39 #ifndef AS_CONTEXT_H
40 #define AS_CONTEXT_H
41 
42 #include "as_config.h"
43 #include "as_atomic.h"
44 #include "as_memory.h"
45 #include "as_string.h"
46 #include "as_objecttype.h"
47 #include "as_callfunc.h"
48 
49 BEGIN_AS_NAMESPACE
50 
51 class asCScriptFunction;
52 class asCScriptEngine;
53 
54 class asCContext : public asIScriptContext {
55 public:
56  // Memory management
57  int AddRef() const;
58  int Release() const;
59 
60  // Miscellaneous
61  asIScriptEngine *GetEngine() const;
62 
63  // Execution
64  int Prepare(asIScriptFunction *func);
65  int Unprepare();
66  int Execute();
67  int Abort();
68  int Suspend();
69  asEContextState GetState() const;
70  int PushState();
71  int PopState();
72  bool IsNested(asUINT *nestCount = 0) const;
73 
74  // Object pointer for calling class methods
75  int SetObject(void *obj);
76 
77  // Arguments
78  int SetArgByte(asUINT arg, asBYTE value);
79  int SetArgWord(asUINT arg, asWORD value);
80  int SetArgDWord(asUINT arg, asDWORD value);
81  int SetArgQWord(asUINT arg, asQWORD value);
82  int SetArgFloat(asUINT arg, float value);
83  int SetArgDouble(asUINT arg, double value);
84  int SetArgAddress(asUINT arg, void *addr);
85  int SetArgObject(asUINT arg, void *obj);
86  int SetArgVarType(asUINT arg, void *ptr, int typeId);
87  void *GetAddressOfArg(asUINT arg);
88 
89  // Return value
90  asBYTE GetReturnByte();
91  asWORD GetReturnWord();
92  asDWORD GetReturnDWord();
93  asQWORD GetReturnQWord();
94  float GetReturnFloat();
95  double GetReturnDouble();
96  void *GetReturnAddress();
97  void *GetReturnObject();
98  void *GetAddressOfReturnValue();
99 
100  // Exception handling
101  int SetException(const char *descr, bool allowCatch = true);
102  int GetExceptionLineNumber(int *column, const char **sectionName);
103  asIScriptFunction *GetExceptionFunction();
104  const char *GetExceptionString();
105  bool WillExceptionBeCaught();
106  int SetExceptionCallback(asSFuncPtr callback, void *obj, int callConv);
107  void ClearExceptionCallback();
108 
109  // Debugging
110  int SetLineCallback(asSFuncPtr callback, void *obj, int callConv);
111  void ClearLineCallback();
112  asUINT GetCallstackSize() const;
113  asIScriptFunction *GetFunction(asUINT stackLevel);
114  int GetLineNumber(asUINT stackLevel, int *column, const char **sectionName);
115  int GetVarCount(asUINT stackLevel);
116  const char *GetVarName(asUINT varIndex, asUINT stackLevel);
117  const char *GetVarDeclaration(asUINT varIndex, asUINT stackLevel, bool includeNamespace);
118  int GetVarTypeId(asUINT varIndex, asUINT stackLevel);
119  void *GetAddressOfVar(asUINT varIndex, asUINT stackLevel);
120  bool IsVarInScope(asUINT varIndex, asUINT stackLevel);
121  int GetThisTypeId(asUINT stackLevel);
122  void *GetThisPointer(asUINT stackLevel);
123  asIScriptFunction *GetSystemFunction();
124 
125  // User data
126  void *SetUserData(void *data, asPWORD type);
127  void *GetUserData(asPWORD type) const;
128 
129 public:
130  // Internal public functions
131  asCContext(asCScriptEngine *engine, bool holdRef);
132  virtual ~asCContext();
133 
134 //protected:
135  friend class asCScriptEngine;
136 
137  void CallLineCallback();
138  void CallExceptionCallback();
139 
140  int CallGeneric(asCScriptFunction *func);
141 #ifndef AS_NO_EXCEPTIONS
142  void HandleAppException();
143 #endif
144  void DetachEngine();
145 
146  void ExecuteNext();
147  void CleanStack(bool catchException = false);
148  bool CleanStackFrame(bool catchException = false);
149  void CleanArgsOnStack();
150  void CleanReturnObject();
151  void DetermineLiveObjects(asCArray<int> &liveObjects, asUINT stackLevel);
152 
153  int PushCallState();
154  void PopCallState();
155  void CallScriptFunction(asCScriptFunction *func);
156  void CallInterfaceMethod(asCScriptFunction *func);
157  void PrepareScriptFunction();
158 
159  bool ReserveStackSpace(asUINT size);
160 
161  void SetInternalException(const char *descr, bool allowCatch = true);
162  bool FindExceptionTryCatch();
163 
164  // Must be protected for multiple accesses
165  mutable asCAtomic m_refCount;
166 
167  bool m_holdEngineRef;
168  asCScriptEngine *m_engine;
169 
170  asEContextState m_status;
171  bool m_doSuspend;
172  bool m_doAbort;
173  bool m_externalSuspendRequest;
174 
175  asCScriptFunction *m_currentFunction;
176  asCScriptFunction *m_callingSystemFunction;
177 
178  // The call stack holds program pointer, stack pointer, etc for caller functions
179  asCArray<size_t> m_callStack;
180 
181  // Dynamically growing local stack
182  asCArray<asDWORD *> m_stackBlocks;
183  asUINT m_stackBlockSize;
184  asUINT m_stackIndex;
185  asDWORD *m_originalStackPointer;
186 
187  // Exception handling
188  bool m_isStackMemoryNotAllocated;
189  bool m_needToCleanupArgs;
190  bool m_inExceptionHandler;
191  asCString m_exceptionString;
192  int m_exceptionFunction;
193  int m_exceptionSectionIdx;
194  int m_exceptionLine;
195  int m_exceptionColumn;
196  bool m_exceptionWillBeCaught;
197 
198  // The last prepared function, and some cached values related to it
199  asCScriptFunction *m_initialFunction;
200  int m_returnValueSize;
201  int m_argumentsSize;
202 
203  // callbacks
204  bool m_lineCallback;
205  asSSystemFunctionInterface m_lineCallbackFunc;
206  void *m_lineCallbackObj;
207 
208  bool m_exceptionCallback;
209  asSSystemFunctionInterface m_exceptionCallbackFunc;
210  void *m_exceptionCallbackObj;
211 
212  asCArray<asPWORD> m_userData;
213 
214  // Registers available to JIT compiler functions
215  asSVMRegisters m_regs;
216 };
217 
218 // TODO: Move these to as_utils.h
219 int as_powi(int base, int exponent, bool &isOverflow);
220 asDWORD as_powu(asDWORD base, asDWORD exponent, bool &isOverflow);
221 asINT64 as_powi64(asINT64 base, asINT64 exponent, bool &isOverflow);
222 asQWORD as_powu64(asQWORD base, asQWORD exponent, bool &isOverflow);
223 
224 // Optional template version of powi if overflow detection is not used.
225 #if 0
226 template <class T>
227 T as_powi(T base, T exponent) {
228  // Test for sign bit (huge number is OK)
229  if (exponent & (T(1) << (sizeof(T) * 8 - 1)))
230  return 0;
231  else {
232  int result = 1;
233  while (exponent) {
234  if (exponent & 1)
235  result *= base;
236  exponent >>= 1;
237  base *= base;
238  }
239  return result;
240  }
241 }
242 #endif
243 
244 END_AS_NAMESPACE
245 
246 #endif
Definition: angelscript.h:1325
Definition: angelscript.h:1083
Definition: as_scriptfunction.h:146
Definition: angelscript.h:863
Definition: as_atomic.h:49
Definition: as_callfunc.h:99
Definition: angelscript.h:429
Definition: as_context.h:54
Definition: as_scriptengine.h:64
Definition: angelscript.h:639
Definition: as_string.h:41