as_scriptfunction.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2015 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_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 IsProtected() const;
  114. bool IsFinal() const;
  115. bool IsOverride() const;
  116. bool IsShared() const;
  117. asUINT GetParamCount() const;
  118. int GetParam(asUINT index, int *typeId, asDWORD *flags = 0, const char **name = 0, const char **defaultArg = 0) const;
  119. #ifdef AS_DEPRECATED
  120. // Deprecated, since 2.29.0, 2014-04-06
  121. int GetParamTypeId(asUINT index, asDWORD *flags = 0) const;
  122. #endif
  123. int GetReturnTypeId(asDWORD *flags = 0) const;
  124. // Type id for function pointers
  125. int GetTypeId() const;
  126. bool IsCompatibleWithTypeId(int typeId) const;
  127. // Delegates
  128. void *GetDelegateObject() const;
  129. asIObjectType *GetDelegateObjectType() const;
  130. asIScriptFunction *GetDelegateFunction() const;
  131. // Debug information
  132. asUINT GetVarCount() const;
  133. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  134. const char * GetVarDecl(asUINT index, bool includeNamespace = false) const;
  135. int FindNextLineWithCode(int line) const;
  136. // For JIT compilation
  137. asDWORD *GetByteCode(asUINT *length = 0);
  138. // User data
  139. void *SetUserData(void *userData, asPWORD type);
  140. void *GetUserData(asPWORD type) const;
  141. public:
  142. //-----------------------------------
  143. // Internal methods
  144. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  145. ~asCScriptFunction();
  146. // Keep an internal reference counter to separate references coming from
  147. // application or script objects and references coming from the script code
  148. int AddRefInternal();
  149. int ReleaseInternal();
  150. void DestroyHalfCreated();
  151. // TODO: operator==
  152. // TODO: The asIScriptFunction should provide operator== and operator!= that should do a
  153. // a value comparison. Two delegate objects that point to the same object and class method should compare as equal
  154. // TODO: The operator== should also be provided in script as opEquals to allow the same comparison in script
  155. // To do this we'll need some way to adapt the argtype for opEquals for each funcdef, preferrably without instantiating lots of different methods
  156. // Perhaps reusing 'auto' to mean the same type as the object
  157. //bool operator==(const asCScriptFunction &other) const;
  158. void DestroyInternal();
  159. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  160. int GetSpaceNeededForArguments();
  161. int GetSpaceNeededForReturnValue();
  162. asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
  163. int GetLineNumber(int programPosition, int *sectionIdx);
  164. void ComputeSignatureId();
  165. bool IsSignatureEqual(const asCScriptFunction *func) const;
  166. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  167. bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  168. bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
  169. bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  170. bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
  171. asCObjectType *GetObjectTypeOfLocalVar(short varOffset);
  172. void MakeDelegate(asCScriptFunction *func, void *obj);
  173. int RegisterListPattern(const char *decl, asCScriptNode *listPattern);
  174. int ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);
  175. bool DoesReturnOnStack() const;
  176. void JITCompile();
  177. void AddReferences();
  178. void ReleaseReferences();
  179. void AllocateScriptFunctionData();
  180. void DeallocateScriptFunctionData();
  181. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  182. // GC methods (for delegates)
  183. int GetRefCount();
  184. void SetFlag();
  185. bool GetFlag();
  186. void EnumReferences(asIScriptEngine *engine);
  187. void ReleaseAllHandles(asIScriptEngine *engine);
  188. public:
  189. //-----------------------------------
  190. // Properties
  191. mutable asCAtomic externalRefCount; // Used for external referneces
  192. asCAtomic internalRefCount; // Used for internal references
  193. mutable bool gcFlag;
  194. asCScriptEngine *engine;
  195. asCModule *module;
  196. asCArray<asPWORD> userData;
  197. // Function signature
  198. asCString name;
  199. asCDataType returnType;
  200. asCArray<asCDataType> parameterTypes;
  201. asCArray<asCString> parameterNames;
  202. asCArray<asETypeModifiers> inOutFlags;
  203. asCArray<asCString *> defaultArgs;
  204. bool isReadOnly;
  205. bool isPrivate;
  206. bool isProtected;
  207. bool isFinal;
  208. bool isOverride;
  209. asCObjectType *objectType;
  210. int signatureId;
  211. int id;
  212. asEFuncType funcType;
  213. asDWORD accessMask;
  214. bool isShared;
  215. asSNameSpace *nameSpace;
  216. // Used by asFUNC_DELEGATE
  217. void *objForDelegate;
  218. asCScriptFunction *funcForDelegate;
  219. // Used by list factory behaviour
  220. asSListPatternNode *listPattern;
  221. // Used by asFUNC_SCRIPT
  222. struct ScriptFunctionData
  223. {
  224. // Bytecode for the script function
  225. asCArray<asDWORD> byteCode;
  226. // The stack space needed for the local variables
  227. asDWORD variableSpace;
  228. // These hold information on objects and function pointers, including temporary
  229. // variables used by exception handler and when saving bytecode
  230. asCArray<asCObjectType*> objVariableTypes;
  231. asCArray<asCScriptFunction*> funcVariableTypes;
  232. asCArray<int> objVariablePos;
  233. // The first variables in above array are allocated on the heap, the rest on the stack.
  234. // This variable shows how many are on the heap.
  235. asUINT objVariablesOnHeap;
  236. // Holds information on scope for object variables on the stack
  237. asCArray<asSObjectVariableInfo> objVariableInfo;
  238. // The stack needed to execute the function
  239. int stackNeeded;
  240. // JIT compiled code of this function
  241. asJITFunction jitFunction;
  242. // Holds debug information on explicitly declared variables
  243. asCArray<asSScriptVariable*> variables;
  244. // Store position, line number pairs for debug information
  245. asCArray<int> lineNumbers;
  246. // Store the script section where the code was declared
  247. int scriptSectionIdx;
  248. // Store the location where the function was declared (row in the lower 20 bits, and column in the upper 12)
  249. int declaredAt;
  250. // Store position/index pairs if the bytecode is compiled from multiple script sections
  251. asCArray<int> sectionIdxs;
  252. };
  253. ScriptFunctionData *scriptData;
  254. // Stub functions and delegates don't own the object and parameters
  255. bool dontCleanUpOnException;
  256. // Used by asFUNC_VIRTUAL
  257. int vfTableIdx;
  258. // Used by asFUNC_SYSTEM
  259. asSSystemFunctionInterface *sysFuncIntf;
  260. };
  261. const char * const DELEGATE_FACTORY = "$dlgte";
  262. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
  263. END_AS_NAMESPACE
  264. #endif