as_scriptfunction.h 13 KB

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