as_scriptfunction.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2018 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, // object is uninitialized/destroyed
  75. asOBJ_INIT, // object is initialized
  76. asBLOCK_BEGIN, // scope block begins
  77. asBLOCK_END, // scope block ends
  78. asOBJ_VARDECL // object variable is declared (but not necessarily initialized)
  79. };
  80. enum asEFuncTrait
  81. {
  82. asTRAIT_CONSTRUCTOR = 1,
  83. asTRAIT_DESTRUCTOR = 2,
  84. asTRAIT_CONST = 4,
  85. asTRAIT_PRIVATE = 8,
  86. asTRAIT_PROTECTED = 16,
  87. asTRAIT_FINAL = 32,
  88. asTRAIT_OVERRIDE = 64,
  89. asTRAIT_SHARED = 128,
  90. asTRAIT_EXTERNAL = 256,
  91. asTRAIT_EXPLICIT = 512
  92. };
  93. struct asSFunctionTraits
  94. {
  95. asSFunctionTraits() : traits(0) {}
  96. void SetTrait(asEFuncTrait trait, bool set) { if (set) traits |= trait; else traits &= ~trait; }
  97. bool GetTrait(asEFuncTrait trait) const { return (traits & trait) ? true : false; }
  98. protected:
  99. asDWORD traits;
  100. };
  101. struct asSObjectVariableInfo
  102. {
  103. asUINT programPos;
  104. int variableOffset;
  105. asEObjVarInfoOption option;
  106. };
  107. struct asSTryCatchInfo
  108. {
  109. asUINT tryPos;
  110. asUINT catchPos;
  111. };
  112. struct asSSystemFunctionInterface;
  113. // TODO: Might be interesting to allow enumeration of accessed global variables, and
  114. // also functions/methods that are being called. This could be used to build a
  115. // code database with call graphs, etc.
  116. void RegisterScriptFunction(asCScriptEngine *engine);
  117. class asCScriptFunction : public asIScriptFunction
  118. {
  119. public:
  120. // From asIScriptFunction
  121. asIScriptEngine *GetEngine() const;
  122. // Memory management
  123. int AddRef() const;
  124. int Release() const;
  125. // Miscellaneous
  126. int GetId() const;
  127. asEFuncType GetFuncType() const;
  128. const char *GetModuleName() const;
  129. asIScriptModule *GetModule() const;
  130. const char *GetScriptSectionName() const;
  131. const char *GetConfigGroup() const;
  132. asDWORD GetAccessMask() const;
  133. void *GetAuxiliary() const;
  134. // Function signature
  135. asITypeInfo *GetObjectType() const;
  136. const char *GetObjectName() const;
  137. const char *GetName() const;
  138. const char *GetNamespace() const;
  139. const char *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
  140. bool IsReadOnly() const;
  141. bool IsPrivate() const;
  142. bool IsProtected() const;
  143. bool IsFinal() const;
  144. bool IsOverride() const;
  145. bool IsShared() const;
  146. bool IsExplicit() const;
  147. asUINT GetParamCount() const;
  148. int GetParam(asUINT index, int *typeId, asDWORD *flags = 0, const char **name = 0, const char **defaultArg = 0) const;
  149. int GetReturnTypeId(asDWORD *flags = 0) const;
  150. // Type id for function pointers
  151. int GetTypeId() const;
  152. bool IsCompatibleWithTypeId(int typeId) const;
  153. // Delegates
  154. void *GetDelegateObject() const;
  155. asITypeInfo *GetDelegateObjectType() const;
  156. asIScriptFunction *GetDelegateFunction() const;
  157. // Debug information
  158. asUINT GetVarCount() const;
  159. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  160. const char * GetVarDecl(asUINT index, bool includeNamespace = false) const;
  161. int FindNextLineWithCode(int line) const;
  162. // For JIT compilation
  163. asDWORD *GetByteCode(asUINT *length = 0);
  164. // User data
  165. void *SetUserData(void *userData, asPWORD type);
  166. void *GetUserData(asPWORD type) const;
  167. public:
  168. //-----------------------------------
  169. // Internal methods
  170. void SetShared(bool set) { traits.SetTrait(asTRAIT_SHARED, set); }
  171. void SetReadOnly(bool set) { traits.SetTrait(asTRAIT_CONST, set); }
  172. void SetFinal(bool set) { traits.SetTrait(asTRAIT_FINAL, set); }
  173. void SetOverride(bool set) { traits.SetTrait(asTRAIT_OVERRIDE, set); }
  174. void SetExplicit(bool set) { traits.SetTrait(asTRAIT_EXPLICIT, set); }
  175. void SetProtected(bool set) { traits.SetTrait(asTRAIT_PROTECTED, set); }
  176. void SetPrivate(bool set) { traits.SetTrait(asTRAIT_PRIVATE, set); }
  177. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  178. ~asCScriptFunction();
  179. // Keep an internal reference counter to separate references coming from
  180. // application or script objects and references coming from the script code
  181. int AddRefInternal();
  182. int ReleaseInternal();
  183. void DestroyHalfCreated();
  184. // TODO: operator==
  185. // TODO: The asIScriptFunction should provide operator== and operator!= that should do a
  186. // a value comparison. Two delegate objects that point to the same object and class method should compare as equal
  187. // TODO: The operator== should also be provided in script as opEquals to allow the same comparison in script
  188. // To do this we'll need some way to adapt the argtype for opEquals for each funcdef, preferrably without instantiating lots of different methods
  189. // Perhaps reusing 'auto' to mean the same type as the object
  190. //bool operator==(const asCScriptFunction &other) const;
  191. void DestroyInternal();
  192. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  193. int GetSpaceNeededForArguments();
  194. int GetSpaceNeededForReturnValue();
  195. asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
  196. int GetLineNumber(int programPosition, int *sectionIdx);
  197. void ComputeSignatureId();
  198. bool IsSignatureEqual(const asCScriptFunction *func) const;
  199. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  200. bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  201. bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
  202. bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  203. bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
  204. asCTypeInfo *GetTypeInfoOfLocalVar(short varOffset);
  205. void MakeDelegate(asCScriptFunction *func, void *obj);
  206. int RegisterListPattern(const char *decl, asCScriptNode *listPattern);
  207. int ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);
  208. bool DoesReturnOnStack() const;
  209. void JITCompile();
  210. void AddReferences();
  211. void ReleaseReferences();
  212. void AllocateScriptFunctionData();
  213. void DeallocateScriptFunctionData();
  214. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  215. // GC methods (for delegates)
  216. int GetRefCount();
  217. void SetFlag();
  218. bool GetFlag();
  219. void EnumReferences(asIScriptEngine *engine);
  220. void ReleaseAllHandles(asIScriptEngine *engine);
  221. public:
  222. //-----------------------------------
  223. // Properties
  224. mutable asCAtomic externalRefCount; // Used for external referneces
  225. asCAtomic internalRefCount; // Used for internal references
  226. mutable bool gcFlag;
  227. asCScriptEngine *engine;
  228. asCModule *module;
  229. asCArray<asPWORD> userData;
  230. // Function signature
  231. asCString name;
  232. asCDataType returnType;
  233. asCArray<asCDataType> parameterTypes;
  234. asCArray<asCString> parameterNames;
  235. asCArray<asETypeModifiers> inOutFlags;
  236. asCArray<asCString *> defaultArgs;
  237. asSFunctionTraits traits;
  238. asCObjectType *objectType;
  239. int signatureId;
  240. int id;
  241. asEFuncType funcType;
  242. asDWORD accessMask;
  243. // Namespace will be null for funcdefs that are declared as child funcdefs
  244. // of a class. In this case the namespace shall be taken from the parentClass
  245. // in the funcdefType
  246. asSNameSpace *nameSpace;
  247. asCFuncdefType *funcdefType; // Doesn't increase refCount
  248. // Used by asFUNC_DELEGATE
  249. void *objForDelegate;
  250. asCScriptFunction *funcForDelegate;
  251. // Used by list factory behaviour
  252. asSListPatternNode *listPattern;
  253. // Used by asFUNC_SCRIPT
  254. struct ScriptFunctionData
  255. {
  256. // Bytecode for the script function
  257. asCArray<asDWORD> byteCode;
  258. // The stack space needed for the local variables
  259. asDWORD variableSpace;
  260. // These hold information on objects and function pointers, including temporary
  261. // variables used by exception handler and when saving bytecode
  262. asCArray<asCTypeInfo*> objVariableTypes;
  263. asCArray<int> objVariablePos; // offset on stackframe
  264. // The first variables in above array are allocated on the heap, the rest on the stack.
  265. // This variable shows how many are on the heap.
  266. asUINT objVariablesOnHeap;
  267. // Holds information on scope for object variables on the stack
  268. asCArray<asSObjectVariableInfo> objVariableInfo;
  269. // Holds information on try/catch blocks for exception handling
  270. asCArray<asSTryCatchInfo> tryCatchInfo;
  271. // The stack needed to execute the function
  272. int stackNeeded;
  273. // JIT compiled code of this function
  274. asJITFunction jitFunction;
  275. // Holds debug information on explicitly declared variables
  276. asCArray<asSScriptVariable*> variables;
  277. // Store position, line number pairs for debug information
  278. asCArray<int> lineNumbers;
  279. // Store the script section where the code was declared
  280. int scriptSectionIdx;
  281. // Store the location where the function was declared (row in the lower 20 bits, and column in the upper 12)
  282. int declaredAt;
  283. // Store position/index pairs if the bytecode is compiled from multiple script sections
  284. asCArray<int> sectionIdxs;
  285. };
  286. ScriptFunctionData *scriptData;
  287. // Stub functions and delegates don't own the object and parameters
  288. bool dontCleanUpOnException;
  289. // Used by asFUNC_VIRTUAL
  290. int vfTableIdx;
  291. // Used by asFUNC_SYSTEM
  292. asSSystemFunctionInterface *sysFuncIntf;
  293. };
  294. const char * const DELEGATE_FACTORY = "$dlgte";
  295. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
  296. END_AS_NAMESPACE
  297. #endif