as_scriptfunction.h 8.3 KB

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