as_scriptfunction.h 11 KB

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