as_scriptfunction.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. struct asSNameSpace;
  41. struct asSScriptVariable
  42. {
  43. asCString name;
  44. asCDataType type;
  45. int stackOffset;
  46. asUINT declaredAtProgramPos;
  47. };
  48. enum asEObjVarInfoOption
  49. {
  50. asOBJ_UNINIT,
  51. asOBJ_INIT,
  52. asBLOCK_BEGIN,
  53. asBLOCK_END
  54. };
  55. struct asSObjectVariableInfo
  56. {
  57. asUINT programPos;
  58. int variableOffset;
  59. asUINT option;
  60. };
  61. struct asSSystemFunctionInterface;
  62. // TODO: GetModuleName should be removed. A function won't belong to a specific module anymore
  63. // as the function can be removed from the module, but still remain alive. For example
  64. // for dynamically generated functions held by a function pointer.
  65. // TODO: Might be interesting to allow enumeration of accessed global variables, and
  66. // also functions/methods that are being called. This could be used to build a
  67. // code database with call graphs, etc.
  68. void RegisterScriptFunction(asCScriptEngine *engine);
  69. class asCScriptFunction : public asIScriptFunction
  70. {
  71. public:
  72. // From asIScriptFunction
  73. asIScriptEngine *GetEngine() const;
  74. // Memory management
  75. int AddRef() const;
  76. int Release() const;
  77. // Miscellaneous
  78. int GetId() const;
  79. asEFuncType GetFuncType() const;
  80. const char *GetModuleName() const;
  81. asIScriptModule *GetModule() const;
  82. const char *GetScriptSectionName() const;
  83. const char *GetConfigGroup() const;
  84. asDWORD GetAccessMask() const;
  85. // Function signature
  86. asIObjectType *GetObjectType() const;
  87. const char *GetObjectName() const;
  88. const char *GetName() const;
  89. const char *GetNamespace() const;
  90. const char *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false) const;
  91. bool IsReadOnly() const;
  92. bool IsPrivate() const;
  93. bool IsFinal() const;
  94. bool IsOverride() const;
  95. bool IsShared() const;
  96. asUINT GetParamCount() const;
  97. int GetParamTypeId(asUINT index, asDWORD *flags = 0) const;
  98. int GetReturnTypeId(asDWORD *flags = 0) const;
  99. // Type id for function pointers
  100. int GetTypeId() const;
  101. bool IsCompatibleWithTypeId(int typeId) const;
  102. // Delegates
  103. void *GetDelegateObject() const;
  104. asIObjectType *GetDelegateObjectType() const;
  105. asIScriptFunction *GetDelegateFunction() const;
  106. // Debug information
  107. asUINT GetVarCount() const;
  108. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  109. const char * GetVarDecl(asUINT index) const;
  110. int FindNextLineWithCode(int line) const;
  111. // For JIT compilation
  112. asDWORD *GetByteCode(asUINT *length = 0);
  113. // User data
  114. void *SetUserData(void *userData);
  115. void *GetUserData() const;
  116. public:
  117. //-----------------------------------
  118. // Internal methods
  119. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  120. ~asCScriptFunction();
  121. void DestroyInternal();
  122. void Orphan(asIScriptModule *mod);
  123. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  124. int GetSpaceNeededForArguments();
  125. int GetSpaceNeededForReturnValue();
  126. asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false) const;
  127. int GetLineNumber(int programPosition, int *sectionIdx);
  128. void ComputeSignatureId();
  129. bool IsSignatureEqual(const asCScriptFunction *func) const;
  130. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  131. bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  132. bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
  133. bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
  134. bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
  135. void MakeDelegate(asCScriptFunction *func, void *obj);
  136. bool DoesReturnOnStack() const;
  137. void JITCompile();
  138. void AddReferences();
  139. void ReleaseReferences();
  140. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  141. // GC methods
  142. int GetRefCount();
  143. void SetFlag();
  144. bool GetFlag();
  145. void EnumReferences(asIScriptEngine *engine);
  146. void ReleaseAllHandles(asIScriptEngine *engine);
  147. public:
  148. //-----------------------------------
  149. // Properties
  150. mutable asCAtomic refCount;
  151. mutable bool gcFlag;
  152. asCScriptEngine *engine;
  153. asCModule *module;
  154. void *userData;
  155. // Function signature
  156. asCString name;
  157. asCDataType returnType;
  158. asCArray<asCDataType> parameterTypes;
  159. asCArray<asETypeModifiers> inOutFlags;
  160. asCArray<asCString *> defaultArgs;
  161. bool isReadOnly;
  162. bool isPrivate;
  163. bool isFinal;
  164. bool isOverride;
  165. asCObjectType *objectType;
  166. int signatureId;
  167. int id;
  168. asEFuncType funcType;
  169. asDWORD accessMask;
  170. bool isShared;
  171. asSNameSpace *nameSpace;
  172. // Used by asFUNC_DELEGATE
  173. void *objForDelegate;
  174. asCScriptFunction *funcForDelegate;
  175. // Used by asFUNC_SCRIPT
  176. asCArray<asDWORD> byteCode;
  177. // The stack space needed for the local variables
  178. asDWORD variableSpace;
  179. // These hold information objects and function pointers, including temporary
  180. // variables used by exception handler and when saving bytecode
  181. asCArray<asCObjectType*> objVariableTypes;
  182. asCArray<asCScriptFunction*> funcVariableTypes;
  183. asCArray<int> objVariablePos;
  184. // The first variables in above array are allocated on the heap, the rest on the stack.
  185. // This variable shows how many are on the heap.
  186. asUINT objVariablesOnHeap;
  187. // Holds information on scope for object variables on the stack
  188. asCArray<asSObjectVariableInfo> objVariableInfo;
  189. // Holds information on explicitly declared variables
  190. asCArray<asSScriptVariable*> variables; // debug info
  191. int stackNeeded;
  192. asCArray<int> lineNumbers; // debug info
  193. int scriptSectionIdx; // debug info
  194. asCArray<int> sectionIdxs; // debug info. Store position/index pairs if the bytecode is compiled from multiple script sections
  195. bool dontCleanUpOnException; // Stub functions don't own the object and parameters
  196. // Used by asFUNC_VIRTUAL
  197. int vfTableIdx;
  198. // Used by asFUNC_SYSTEM
  199. asSSystemFunctionInterface *sysFuncIntf;
  200. // JIT compiled code of this function
  201. asJITFunction jitFunction;
  202. };
  203. const char * const DELEGATE_FACTORY = "%delegate_factory";
  204. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
  205. END_AS_NAMESPACE
  206. #endif