as_scriptfunction.h 12 KB

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