as_context.h 7.3 KB

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