as_scriptfunction.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. };
  46. enum asEObjVarInfoOption
  47. {
  48. asOBJ_UNINIT,
  49. asOBJ_INIT,
  50. asBLOCK_BEGIN,
  51. asBLOCK_END
  52. };
  53. struct asSObjectVariableInfo
  54. {
  55. asUINT programPos;
  56. int variableOffset;
  57. asUINT option;
  58. };
  59. struct asSSystemFunctionInterface;
  60. // TODO: GetModuleName should be removed. A function won't belong to a specific module anymore
  61. // as the function can be removed from the module, but still remain alive. For example
  62. // for dynamically generated functions held by a function pointer.
  63. // TODO: Might be interesting to allow enumeration of accessed global variables, and
  64. // also functions/methods that are being called. This could be used to build a
  65. // code database with call graphs, etc.
  66. void RegisterScriptFunction(asCScriptEngine *engine);
  67. class asCScriptFunction : public asIScriptFunction
  68. {
  69. public:
  70. // From asIScriptFunction
  71. asIScriptEngine *GetEngine() const;
  72. // Memory management
  73. int AddRef() const;
  74. int Release() const;
  75. int GetId() const;
  76. asEFuncType GetFuncType() const;
  77. const char *GetModuleName() const;
  78. asIObjectType *GetObjectType() const;
  79. const char *GetObjectName() const;
  80. const char *GetName() const;
  81. const char *GetDeclaration(bool includeObjectName = true) const;
  82. const char *GetScriptSectionName() const;
  83. const char *GetConfigGroup() const;
  84. bool IsReadOnly() const;
  85. bool IsPrivate() const;
  86. int GetParamCount() const;
  87. int GetParamTypeId(int index, asDWORD *flags = 0) const;
  88. int GetReturnTypeId() const;
  89. // Debug information
  90. int GetVarCount() const;
  91. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  92. const char * GetVarDecl(asUINT index) const;
  93. // For JIT compilation
  94. asDWORD *GetByteCode(asUINT *length = 0);
  95. // User data
  96. void *SetUserData(void *userData);
  97. void *GetUserData() const;
  98. public:
  99. //-----------------------------------
  100. // Internal methods
  101. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  102. ~asCScriptFunction();
  103. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  104. int GetSpaceNeededForArguments();
  105. int GetSpaceNeededForReturnValue();
  106. asCString GetDeclarationStr(bool includeObjectName = true) const;
  107. int GetLineNumber(int programPosition);
  108. void ComputeSignatureId();
  109. bool IsSignatureEqual(const asCScriptFunction *func) const;
  110. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  111. void JITCompile();
  112. void AddReferences();
  113. void ReleaseReferences();
  114. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  115. // GC methods
  116. int GetRefCount();
  117. void SetFlag();
  118. bool GetFlag();
  119. void EnumReferences(asIScriptEngine *engine);
  120. void ReleaseAllHandles(asIScriptEngine *engine);
  121. public:
  122. //-----------------------------------
  123. // Properties
  124. mutable asCAtomic refCount;
  125. mutable bool gcFlag;
  126. asCScriptEngine *engine;
  127. asCModule *module;
  128. void *userData;
  129. // Function signature
  130. asCString name;
  131. asCDataType returnType;
  132. asCArray<asCDataType> parameterTypes;
  133. asCArray<asETypeModifiers> inOutFlags;
  134. asCArray<asCString *> defaultArgs;
  135. bool isReadOnly;
  136. bool isPrivate;
  137. asCObjectType *objectType;
  138. int signatureId;
  139. int id;
  140. asEFuncType funcType;
  141. // Used by asFUNC_SCRIPT
  142. asCArray<asDWORD> byteCode;
  143. asCArray<asCObjectType*> objVariableTypes;
  144. asCArray<int> objVariablePos;
  145. asCArray<bool> objVariableIsOnHeap;
  146. asCArray<asSObjectVariableInfo> objVariableInfo;
  147. int stackNeeded;
  148. asCArray<int> lineNumbers; // debug info
  149. asCArray<asSScriptVariable*> variables; // debug info
  150. int scriptSectionIdx; // debug info
  151. bool dontCleanUpOnException; // Stub functions don't own the object and parameters
  152. // Used by asFUNC_VIRTUAL
  153. int vfTableIdx;
  154. // Used by asFUNC_SYSTEM
  155. asSSystemFunctionInterface *sysFuncIntf;
  156. // JIT compiled code of this function
  157. asJITFunction jitFunction;
  158. };
  159. END_AS_NAMESPACE
  160. #endif