as_scriptfunction.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. 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. void RegisterScriptFunction(asCScriptEngine *engine);
  68. class asCScriptFunction : public asIScriptFunction
  69. {
  70. public:
  71. // From asIScriptFunction
  72. asIScriptEngine *GetEngine() const;
  73. // Memory management
  74. int AddRef() const;
  75. int Release() const;
  76. int GetId() const;
  77. asEFuncType GetFuncType() const;
  78. const char *GetModuleName() const;
  79. asIObjectType *GetObjectType() const;
  80. const char *GetObjectName() const;
  81. const char *GetName() const;
  82. const char *GetDeclaration(bool includeObjectName = true) const;
  83. const char *GetScriptSectionName() const;
  84. const char *GetConfigGroup() const;
  85. bool IsReadOnly() const;
  86. bool IsPrivate() const;
  87. asUINT GetParamCount() const;
  88. int GetParamTypeId(asUINT index, asDWORD *flags = 0) const;
  89. int GetReturnTypeId() const;
  90. // Debug information
  91. asUINT GetVarCount() const;
  92. int GetVar(asUINT index, const char **name, int *typeId = 0) const;
  93. const char * GetVarDecl(asUINT index) const;
  94. int FindNextLineWithCode(int line) const;
  95. // For JIT compilation
  96. asDWORD *GetByteCode(asUINT *length = 0);
  97. // User data
  98. void *SetUserData(void *userData);
  99. void *GetUserData() const;
  100. public:
  101. //-----------------------------------
  102. // Internal methods
  103. asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
  104. ~asCScriptFunction();
  105. void AddVariable(asCString &name, asCDataType &type, int stackOffset);
  106. int GetSpaceNeededForArguments();
  107. int GetSpaceNeededForReturnValue();
  108. asCString GetDeclarationStr(bool includeObjectName = true) const;
  109. int GetLineNumber(int programPosition);
  110. void ComputeSignatureId();
  111. bool IsSignatureEqual(const asCScriptFunction *func) const;
  112. bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
  113. void JITCompile();
  114. void AddReferences();
  115. void ReleaseReferences();
  116. asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
  117. // GC methods
  118. int GetRefCount();
  119. void SetFlag();
  120. bool GetFlag();
  121. void EnumReferences(asIScriptEngine *engine);
  122. void ReleaseAllHandles(asIScriptEngine *engine);
  123. public:
  124. //-----------------------------------
  125. // Properties
  126. mutable asCAtomic refCount;
  127. mutable bool gcFlag;
  128. asCScriptEngine *engine;
  129. asCModule *module;
  130. void *userData;
  131. // Function signature
  132. asCString name;
  133. asCDataType returnType;
  134. asCArray<asCDataType> parameterTypes;
  135. asCArray<asETypeModifiers> inOutFlags;
  136. asCArray<asCString *> defaultArgs;
  137. bool isReadOnly;
  138. bool isPrivate;
  139. asCObjectType *objectType;
  140. int signatureId;
  141. int id;
  142. asEFuncType funcType;
  143. // Used by asFUNC_SCRIPT
  144. asCArray<asDWORD> byteCode;
  145. asCArray<asCObjectType*> objVariableTypes;
  146. asCArray<int> objVariablePos;
  147. asCArray<bool> objVariableIsOnHeap;
  148. asCArray<asSObjectVariableInfo> objVariableInfo;
  149. int stackNeeded;
  150. asCArray<int> lineNumbers; // debug info
  151. asCArray<asSScriptVariable*> variables; // debug info
  152. int scriptSectionIdx; // debug info
  153. bool dontCleanUpOnException; // Stub functions don't own the object and parameters
  154. // Used by asFUNC_VIRTUAL
  155. int vfTableIdx;
  156. // Used by asFUNC_SYSTEM
  157. asSSystemFunctionInterface *sysFuncIntf;
  158. // JIT compiled code of this function
  159. asJITFunction jitFunction;
  160. };
  161. END_AS_NAMESPACE
  162. #endif