as_scriptfunction.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. [email protected]
  22. */
  23. //
  24. // as_scriptfunction.h
  25. //
  26. // A container for a compiled script function
  27. //
  28. #ifndef AS_SCRIPTFUNCTION_H
  29. #define AS_SCRIPTFUNCTION_H
  30. #include "as_config.h"
  31. #include "as_string.h"
  32. #include "as_array.h"
  33. #include "as_datatype.h"
  34. #include "as_atomic.h"
  35. BEGIN_AS_NAMESPACE
  36. class asCScriptEngine;
  37. class asCModule;
  38. class asCConfigGroup;
  39. class asCGlobalProperty;
  40. class asCScriptNode;
  41. struct asSNameSpace;
  42. struct asSScriptVariable
  43. {
  44. asCString name;
  45. asCDataType type;
  46. int stackOffset;
  47. asUINT declaredAtProgramPos;
  48. };
  49. enum asEListPatternNodeType
  50. {
  51. asLPT_REPEAT,
  52. asLPT_START,
  53. asLPT_END,
  54. asLPT_TYPE
  55. };
  56. struct asSListPatternNode
  57. {
  58. asSListPatternNode(asEListPatternNodeType t) : type(t), next(0) {}
  59. virtual ~asSListPatternNode() {};
  60. virtual asSListPatternNode *Duplicate() { return asNEW(asSListPatternNode)(type); }
  61. asEListPatternNodeType type;
  62. asSListPatternNode *next;
  63. };
  64. struct asSListPatternDataTypeNode : public asSListPatternNode
  65. {
  66. asSListPatternDataTypeNode(const asCDataType &dt) : asSListPatternNode(asLPT_TYPE), dataType(dt) {}
  67. asSListPatternNode *Duplicate() { return asNEW(asSListPatternDataTypeNode)(dataType); }
  68. asCDataType dataType;
  69. };
  70. enum asEObjVarInfoOption
  71. {
  72. asOBJ_UNINIT,
  73. asOBJ_INIT,
  74. asBLOCK_BEGIN,
  75. asBLOCK_END
  76. };
  77. struct asSObjectVariableInfo
  78. {
  79. asUINT programPos;
  80. int variableOffset;
  81. asUINT option;
  82. };
  83. struct asSSystemFunctionInterface;
  84. // TODO: GetModuleName should be removed. A function won't belong to a specific module anymore
  85. // as the function can be removed from the module, but still remain alive. For example
  86. // for dynamically generated functions held by a function pointer.
  87. // TODO: Might be interesting to allow enumeration of accessed global variables, and
  88. // also functions/methods that are being called. This could be used to build a
  89. // code database with call graphs, etc.
  90. void RegisterScriptFunction(asCScriptEngine *engine);
  91. class asCScriptFunction : public asIScriptFunction
  92. {
  93. public:
  94. // From asIScriptFunction
  95. asIScriptEngine *GetEngine() const;
  96. // Memory management
  97. int AddRef() const;
  98. int Release() const;
  99. // Miscellaneous
  100. int GetId() const;
  101. asEFuncType GetFuncType() const;
  102. const char *GetModuleName() const;
  103. asIScriptModule *GetModule() const;
  104. const char *GetScriptSectionName() const;
  105. const char *GetConfigGroup() const;
  106. asDWORD GetAccessMask() const;
  107. // Function signature
  108. asIObjectType *GetObjectType() const;
  109. const char *GetObjectName() const;
  110. const char *GetName() const;
  111. const char *GetNamespace() const;
  112. const char *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false) const;
  113. bool IsReadOnly() const;
  114. bool IsPrivate() const;
  115. bool IsFinal() const;
  116. bool IsOverride() const;
  117. bool IsShared() const;
  118. asUINT GetParamCount() const;
  119. int GetParamTypeId(asUINT index, asDWORD *flags = 0) const;
  120. int GetReturnTypeId(asDWORD *flags = 0) const;
  121. // Type id for function pointers
  122. int GetTypeId() const;
  123. bool IsCompatibleWithTypeId(int typeId) const;
  124. // Delegates
  125. void *GetDelegateObject() const;
  126. asIObjectType *GetDelegateObjectType() const;
  127. asIScriptFunction *GetDelegateFunction() const;
  128. // Debug information
  129. asUINT GetVarCount() const;
  130. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  131. const char * GetVarDecl(asUINT index) const;
  132. int FindNextLineWithCode(int line) const;
  133. // For JIT compilation
  134. asDWORD *GetByteCode(asUINT *length = 0);
  135. // User data
  136. void *SetUserData(void *userData);
  137. void *GetUserData() const;
  138. public:
  139. //-----------------------------------
  140. // Internal methods
  141. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  142. ~asCScriptFunction();
  143. void DestroyInternal();
  144. void Orphan(asIScriptModule *mod);
  145. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  146. int GetSpaceNeededForArguments();
  147. int GetSpaceNeededForReturnValue();
  148. asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false) const;
  149. int GetLineNumber(int programPosition, int *sectionIdx);
  150. void ComputeSignatureId();
  151. bool IsSignatureEqual(const asCScriptFunction *func) const;
  152. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  153. bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  154. bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
  155. bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  156. bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
  157. asCObjectType *GetObjectTypeOfLocalVar(short varOffset);
  158. void MakeDelegate(asCScriptFunction *func, void *obj);
  159. int RegisterListPattern(const char *decl, asCScriptNode *listPattern);
  160. int ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);
  161. bool DoesReturnOnStack() const;
  162. void JITCompile();
  163. void AddReferences();
  164. void ReleaseReferences();
  165. void AllocateScriptFunctionData();
  166. void DeallocateScriptFunctionData();
  167. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  168. // GC methods
  169. int GetRefCount();
  170. void SetFlag();
  171. bool GetFlag();
  172. void EnumReferences(asIScriptEngine *engine);
  173. void ReleaseAllHandles(asIScriptEngine *engine);
  174. public:
  175. //-----------------------------------
  176. // Properties
  177. mutable asCAtomic refCount;
  178. mutable bool gcFlag;
  179. asCScriptEngine *engine;
  180. asCModule *module;
  181. void *userData;
  182. // Function signature
  183. asCString name;
  184. asCDataType returnType;
  185. asCArray<asCDataType> parameterTypes;
  186. asCArray<asETypeModifiers> inOutFlags;
  187. asCArray<asCString *> defaultArgs;
  188. bool isReadOnly;
  189. bool isPrivate;
  190. bool isFinal;
  191. bool isOverride;
  192. asCObjectType *objectType;
  193. int signatureId;
  194. int id;
  195. asEFuncType funcType;
  196. asDWORD accessMask;
  197. bool isShared;
  198. asSNameSpace *nameSpace;
  199. // Used by asFUNC_DELEGATE
  200. void *objForDelegate;
  201. asCScriptFunction *funcForDelegate;
  202. // Used by list factory behaviour
  203. asSListPatternNode *listPattern;
  204. // Used by asFUNC_SCRIPT
  205. struct ScriptFunctionData
  206. {
  207. // Bytecode for the script function
  208. asCArray<asDWORD> byteCode;
  209. // The stack space needed for the local variables
  210. asDWORD variableSpace;
  211. // These hold information objects and function pointers, including temporary
  212. // variables used by exception handler and when saving bytecode
  213. asCArray<asCObjectType*> objVariableTypes;
  214. asCArray<asCScriptFunction*> funcVariableTypes;
  215. asCArray<int> objVariablePos;
  216. // The first variables in above array are allocated on the heap, the rest on the stack.
  217. // This variable shows how many are on the heap.
  218. asUINT objVariablesOnHeap;
  219. // Holds information on scope for object variables on the stack
  220. asCArray<asSObjectVariableInfo> objVariableInfo;
  221. // The stack needed to execute the function
  222. int stackNeeded;
  223. // JIT compiled code of this function
  224. asJITFunction jitFunction;
  225. // Holds debug information on explicitly declared variables
  226. asCArray<asSScriptVariable*> variables;
  227. // Store position, line number pairs for debug information
  228. asCArray<int> lineNumbers;
  229. // Store the script section where the code was declared
  230. int scriptSectionIdx;
  231. // Store position/index pairs if the bytecode is compiled from multiple script sections
  232. asCArray<int> sectionIdxs;
  233. };
  234. ScriptFunctionData *scriptData;
  235. // Stub functions and delegates don't own the object and parameters
  236. bool dontCleanUpOnException;
  237. // Used by asFUNC_VIRTUAL
  238. int vfTableIdx;
  239. // Used by asFUNC_SYSTEM
  240. asSSystemFunctionInterface *sysFuncIntf;
  241. };
  242. const char * const DELEGATE_FACTORY = "%delegate_factory";
  243. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
  244. END_AS_NAMESPACE
  245. #endif