as_scriptfunction.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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: Might be interesting to allow enumeration of accessed global variables, and
  85. // also functions/methods that are being called. This could be used to build a
  86. // code database with call graphs, etc.
  87. void RegisterScriptFunction(asCScriptEngine *engine);
  88. class asCScriptFunction : public asIScriptFunction
  89. {
  90. public:
  91. // From asIScriptFunction
  92. asIScriptEngine *GetEngine() const;
  93. // Memory management
  94. int AddRef() const;
  95. int Release() const;
  96. // Miscellaneous
  97. int GetId() const;
  98. asEFuncType GetFuncType() const;
  99. const char *GetModuleName() const;
  100. asIScriptModule *GetModule() const;
  101. const char *GetScriptSectionName() const;
  102. const char *GetConfigGroup() const;
  103. asDWORD GetAccessMask() const;
  104. // Function signature
  105. asIObjectType *GetObjectType() const;
  106. const char *GetObjectName() const;
  107. const char *GetName() const;
  108. const char *GetNamespace() const;
  109. const char *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false) const;
  110. bool IsReadOnly() const;
  111. bool IsPrivate() const;
  112. bool IsFinal() const;
  113. bool IsOverride() const;
  114. bool IsShared() const;
  115. asUINT GetParamCount() const;
  116. int GetParamTypeId(asUINT index, asDWORD *flags = 0) const;
  117. int GetReturnTypeId(asDWORD *flags = 0) const;
  118. // Type id for function pointers
  119. int GetTypeId() const;
  120. bool IsCompatibleWithTypeId(int typeId) const;
  121. // Delegates
  122. void *GetDelegateObject() const;
  123. asIObjectType *GetDelegateObjectType() const;
  124. asIScriptFunction *GetDelegateFunction() const;
  125. // Debug information
  126. asUINT GetVarCount() const;
  127. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  128. const char * GetVarDecl(asUINT index, bool includeNamespace = false) const;
  129. int FindNextLineWithCode(int line) const;
  130. // For JIT compilation
  131. asDWORD *GetByteCode(asUINT *length = 0);
  132. // User data
  133. void *SetUserData(void *userData);
  134. void *GetUserData() const;
  135. public:
  136. //-----------------------------------
  137. // Internal methods
  138. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  139. ~asCScriptFunction();
  140. void DestroyInternal();
  141. void Orphan(asIScriptModule *mod);
  142. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  143. int GetSpaceNeededForArguments();
  144. int GetSpaceNeededForReturnValue();
  145. asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false) const;
  146. int GetLineNumber(int programPosition, int *sectionIdx);
  147. void ComputeSignatureId();
  148. bool IsSignatureEqual(const asCScriptFunction *func) const;
  149. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  150. bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  151. bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
  152. bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  153. bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
  154. asCObjectType *GetObjectTypeOfLocalVar(short varOffset);
  155. void MakeDelegate(asCScriptFunction *func, void *obj);
  156. int RegisterListPattern(const char *decl, asCScriptNode *listPattern);
  157. int ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);
  158. bool DoesReturnOnStack() const;
  159. void JITCompile();
  160. void AddReferences();
  161. void ReleaseReferences();
  162. void AllocateScriptFunctionData();
  163. void DeallocateScriptFunctionData();
  164. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  165. // GC methods
  166. int GetRefCount();
  167. void SetFlag();
  168. bool GetFlag();
  169. void EnumReferences(asIScriptEngine *engine);
  170. void ReleaseAllHandles(asIScriptEngine *engine);
  171. public:
  172. //-----------------------------------
  173. // Properties
  174. mutable asCAtomic refCount;
  175. mutable bool gcFlag;
  176. asCScriptEngine *engine;
  177. asCModule *module;
  178. void *userData;
  179. // Function signature
  180. asCString name;
  181. asCDataType returnType;
  182. asCArray<asCDataType> parameterTypes;
  183. asCArray<asETypeModifiers> inOutFlags;
  184. asCArray<asCString *> defaultArgs;
  185. bool isReadOnly;
  186. bool isPrivate;
  187. bool isFinal;
  188. bool isOverride;
  189. asCObjectType *objectType;
  190. int signatureId;
  191. int id;
  192. asEFuncType funcType;
  193. asDWORD accessMask;
  194. bool isShared;
  195. asSNameSpace *nameSpace;
  196. // Used by asFUNC_DELEGATE
  197. void *objForDelegate;
  198. asCScriptFunction *funcForDelegate;
  199. // Used by list factory behaviour
  200. asSListPatternNode *listPattern;
  201. // Used by asFUNC_SCRIPT
  202. struct ScriptFunctionData
  203. {
  204. // Bytecode for the script function
  205. asCArray<asDWORD> byteCode;
  206. // The stack space needed for the local variables
  207. asDWORD variableSpace;
  208. // These hold information objects and function pointers, including temporary
  209. // variables used by exception handler and when saving bytecode
  210. asCArray<asCObjectType*> objVariableTypes;
  211. asCArray<asCScriptFunction*> funcVariableTypes;
  212. asCArray<int> objVariablePos;
  213. // The first variables in above array are allocated on the heap, the rest on the stack.
  214. // This variable shows how many are on the heap.
  215. asUINT objVariablesOnHeap;
  216. // Holds information on scope for object variables on the stack
  217. asCArray<asSObjectVariableInfo> objVariableInfo;
  218. // The stack needed to execute the function
  219. int stackNeeded;
  220. // JIT compiled code of this function
  221. asJITFunction jitFunction;
  222. // Holds debug information on explicitly declared variables
  223. asCArray<asSScriptVariable*> variables;
  224. // Store position, line number pairs for debug information
  225. asCArray<int> lineNumbers;
  226. // Store the script section where the code was declared
  227. int scriptSectionIdx;
  228. // Store the location where the function was declared
  229. int declaredAt;
  230. // Store position/index pairs if the bytecode is compiled from multiple script sections
  231. asCArray<int> sectionIdxs;
  232. };
  233. ScriptFunctionData *scriptData;
  234. // Stub functions and delegates don't own the object and parameters
  235. bool dontCleanUpOnException;
  236. // Used by asFUNC_VIRTUAL
  237. int vfTableIdx;
  238. // Used by asFUNC_SYSTEM
  239. asSSystemFunctionInterface *sysFuncIntf;
  240. };
  241. const char * const DELEGATE_FACTORY = "%delegate_factory";
  242. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
  243. END_AS_NAMESPACE
  244. #endif