as_scriptfunction.h 13 KB

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